ioremap.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * (C) Copyright 1995 1996 Linus Torvalds
  7. * (C) Copyright 2001, 2002 Ralf Baechle
  8. */
  9. #include <linux/export.h>
  10. #include <asm/addrspace.h>
  11. #include <asm/byteorder.h>
  12. #include <linux/ioport.h>
  13. #include <linux/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/mm_types.h>
  17. #include <linux/io.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/tlbflush.h>
  20. #include <ioremap.h>
  21. #define IS_LOW512(addr) (!((phys_addr_t)(addr) & (phys_addr_t) ~0x1fffffffULL))
  22. #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == CKSEG1)
  23. static int __ioremap_check_ram(unsigned long start_pfn, unsigned long nr_pages,
  24. void *arg)
  25. {
  26. unsigned long i;
  27. for (i = 0; i < nr_pages; i++) {
  28. if (pfn_valid(start_pfn + i) &&
  29. !PageReserved(pfn_to_page(start_pfn + i)))
  30. return 1;
  31. }
  32. return 0;
  33. }
  34. /*
  35. * ioremap_prot - map bus memory into CPU space
  36. * @phys_addr: bus address of the memory
  37. * @size: size of the resource to map
  38. *
  39. * ioremap_prot gives the caller control over cache coherency attributes (CCA)
  40. */
  41. void __iomem *ioremap_prot(phys_addr_t phys_addr, unsigned long size,
  42. unsigned long prot_val)
  43. {
  44. unsigned long flags = prot_val & _CACHE_MASK;
  45. unsigned long offset, pfn, last_pfn;
  46. struct vm_struct *area;
  47. phys_addr_t last_addr;
  48. unsigned long vaddr;
  49. void __iomem *cpu_addr;
  50. cpu_addr = plat_ioremap(phys_addr, size, flags);
  51. if (cpu_addr)
  52. return cpu_addr;
  53. phys_addr = fixup_bigphys_addr(phys_addr, size);
  54. /* Don't allow wraparound or zero size */
  55. last_addr = phys_addr + size - 1;
  56. if (!size || last_addr < phys_addr)
  57. return NULL;
  58. /*
  59. * Map uncached objects in the low 512mb of address space using KSEG1,
  60. * otherwise map using page tables.
  61. */
  62. if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) &&
  63. flags == _CACHE_UNCACHED)
  64. return (void __iomem *) CKSEG1ADDR(phys_addr);
  65. /*
  66. * Don't allow anybody to remap RAM that may be allocated by the page
  67. * allocator, since that could lead to races & data clobbering.
  68. */
  69. pfn = PFN_DOWN(phys_addr);
  70. last_pfn = PFN_DOWN(last_addr);
  71. if (walk_system_ram_range(pfn, last_pfn - pfn + 1, NULL,
  72. __ioremap_check_ram) == 1) {
  73. WARN_ONCE(1, "ioremap on RAM at %pa - %pa\n",
  74. &phys_addr, &last_addr);
  75. return NULL;
  76. }
  77. /*
  78. * Mappings have to be page-aligned
  79. */
  80. offset = phys_addr & ~PAGE_MASK;
  81. phys_addr &= PAGE_MASK;
  82. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  83. /*
  84. * Ok, go for it..
  85. */
  86. area = get_vm_area(size, VM_IOREMAP);
  87. if (!area)
  88. return NULL;
  89. vaddr = (unsigned long)area->addr;
  90. flags |= _PAGE_GLOBAL | _PAGE_PRESENT | __READABLE | __WRITEABLE;
  91. if (ioremap_page_range(vaddr, vaddr + size, phys_addr,
  92. __pgprot(flags))) {
  93. free_vm_area(area);
  94. return NULL;
  95. }
  96. return (void __iomem *)(vaddr + offset);
  97. }
  98. EXPORT_SYMBOL(ioremap_prot);
  99. void iounmap(const volatile void __iomem *addr)
  100. {
  101. if (!plat_iounmap(addr) && !IS_KSEG1(addr))
  102. vunmap((void *)((unsigned long)addr & PAGE_MASK));
  103. }
  104. EXPORT_SYMBOL(iounmap);