ioremap.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * linux/arch/arm/mm/ioremap.c
  3. *
  4. * Re-map IO memory to kernel address space so that we can access it.
  5. *
  6. * (C) Copyright 1995 1996 Linus Torvalds
  7. *
  8. * Hacked for ARM by Phil Blundell <philb@gnu.org>
  9. * Hacked to allow all architectures to build, and various cleanups
  10. * by Russell King
  11. *
  12. * This allows a driver to remap an arbitrary region of bus memory into
  13. * virtual space. One should *only* use readl, writel, memcpy_toio and
  14. * so on with such remapped areas.
  15. *
  16. * Because the ARM only has a 32-bit address space we can't address the
  17. * whole of the (physical) PCI space at once. PCI huge-mode addressing
  18. * allows us to circumvent this restriction by splitting PCI space into
  19. * two 2GB chunks and mapping only one at a time into processor memory.
  20. * We use MMU protection domains to trap any attempt to access the bank
  21. * that is not currently mapped. (This isn't fully implemented yet.)
  22. */
  23. #include <linux/module.h>
  24. #include <linux/errno.h>
  25. #include <linux/mm.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/io.h>
  28. #include <asm/cputype.h>
  29. #include <asm/cacheflush.h>
  30. #include <asm/mmu_context.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/tlbflush.h>
  33. #include <asm/sizes.h>
  34. #include <asm/mach/map.h>
  35. #include "mm.h"
  36. /*
  37. * Used by ioremap() and iounmap() code to mark (super)section-mapped
  38. * I/O regions in vm_struct->flags field.
  39. */
  40. #define VM_ARM_SECTION_MAPPING 0x80000000
  41. static int remap_area_pte(pmd_t *pmd, unsigned long addr, unsigned long end,
  42. unsigned long phys_addr, const struct mem_type *type)
  43. {
  44. pgprot_t prot = __pgprot(type->prot_pte);
  45. pte_t *pte;
  46. pte = pte_alloc_kernel(pmd, addr);
  47. if (!pte)
  48. return -ENOMEM;
  49. do {
  50. if (!pte_none(*pte))
  51. goto bad;
  52. set_pte_ext(pte, pfn_pte(phys_addr >> PAGE_SHIFT, prot), 0);
  53. phys_addr += PAGE_SIZE;
  54. } while (pte++, addr += PAGE_SIZE, addr != end);
  55. return 0;
  56. bad:
  57. printk(KERN_CRIT "remap_area_pte: page already exists\n");
  58. BUG();
  59. }
  60. static inline int remap_area_pmd(pgd_t *pgd, unsigned long addr,
  61. unsigned long end, unsigned long phys_addr,
  62. const struct mem_type *type)
  63. {
  64. unsigned long next;
  65. pmd_t *pmd;
  66. int ret = 0;
  67. pmd = pmd_alloc(&init_mm, pgd, addr);
  68. if (!pmd)
  69. return -ENOMEM;
  70. do {
  71. next = pmd_addr_end(addr, end);
  72. ret = remap_area_pte(pmd, addr, next, phys_addr, type);
  73. if (ret)
  74. return ret;
  75. phys_addr += next - addr;
  76. } while (pmd++, addr = next, addr != end);
  77. return ret;
  78. }
  79. static int remap_area_pages(unsigned long start, unsigned long pfn,
  80. size_t size, const struct mem_type *type)
  81. {
  82. unsigned long addr = start;
  83. unsigned long next, end = start + size;
  84. unsigned long phys_addr = __pfn_to_phys(pfn);
  85. pgd_t *pgd;
  86. int err = 0;
  87. BUG_ON(addr >= end);
  88. pgd = pgd_offset_k(addr);
  89. do {
  90. next = pgd_addr_end(addr, end);
  91. err = remap_area_pmd(pgd, addr, next, phys_addr, type);
  92. if (err)
  93. break;
  94. phys_addr += next - addr;
  95. } while (pgd++, addr = next, addr != end);
  96. return err;
  97. }
  98. int ioremap_page(unsigned long virt, unsigned long phys,
  99. const struct mem_type *mtype)
  100. {
  101. return remap_area_pages(virt, __phys_to_pfn(phys), PAGE_SIZE, mtype);
  102. }
  103. EXPORT_SYMBOL(ioremap_page);
  104. void __check_kvm_seq(struct mm_struct *mm)
  105. {
  106. unsigned int seq;
  107. do {
  108. seq = init_mm.context.kvm_seq;
  109. memcpy(pgd_offset(mm, VMALLOC_START),
  110. pgd_offset_k(VMALLOC_START),
  111. sizeof(pgd_t) * (pgd_index(VMALLOC_END) -
  112. pgd_index(VMALLOC_START)));
  113. mm->context.kvm_seq = seq;
  114. } while (seq != init_mm.context.kvm_seq);
  115. }
  116. #ifndef CONFIG_SMP
  117. /*
  118. * Section support is unsafe on SMP - If you iounmap and ioremap a region,
  119. * the other CPUs will not see this change until their next context switch.
  120. * Meanwhile, (eg) if an interrupt comes in on one of those other CPUs
  121. * which requires the new ioremap'd region to be referenced, the CPU will
  122. * reference the _old_ region.
  123. *
  124. * Note that get_vm_area() allocates a guard 4K page, so we need to mask
  125. * the size back to 1MB aligned or we will overflow in the loop below.
  126. */
  127. static void unmap_area_sections(unsigned long virt, unsigned long size)
  128. {
  129. unsigned long addr = virt, end = virt + (size & ~(SZ_1M - 1));
  130. pgd_t *pgd;
  131. flush_cache_vunmap(addr, end);
  132. pgd = pgd_offset_k(addr);
  133. do {
  134. pmd_t pmd, *pmdp = pmd_offset(pgd, addr);
  135. pmd = *pmdp;
  136. if (!pmd_none(pmd)) {
  137. /*
  138. * Clear the PMD from the page table, and
  139. * increment the kvm sequence so others
  140. * notice this change.
  141. *
  142. * Note: this is still racy on SMP machines.
  143. */
  144. pmd_clear(pmdp);
  145. init_mm.context.kvm_seq++;
  146. /*
  147. * Free the page table, if there was one.
  148. */
  149. if ((pmd_val(pmd) & PMD_TYPE_MASK) == PMD_TYPE_TABLE)
  150. pte_free_kernel(&init_mm, pmd_page_vaddr(pmd));
  151. }
  152. addr += PGDIR_SIZE;
  153. pgd++;
  154. } while (addr < end);
  155. /*
  156. * Ensure that the active_mm is up to date - we want to
  157. * catch any use-after-iounmap cases.
  158. */
  159. if (current->active_mm->context.kvm_seq != init_mm.context.kvm_seq)
  160. __check_kvm_seq(current->active_mm);
  161. flush_tlb_kernel_range(virt, end);
  162. }
  163. static int
  164. remap_area_sections(unsigned long virt, unsigned long pfn,
  165. size_t size, const struct mem_type *type)
  166. {
  167. unsigned long addr = virt, end = virt + size;
  168. pgd_t *pgd;
  169. /*
  170. * Remove and free any PTE-based mapping, and
  171. * sync the current kernel mapping.
  172. */
  173. unmap_area_sections(virt, size);
  174. pgd = pgd_offset_k(addr);
  175. do {
  176. pmd_t *pmd = pmd_offset(pgd, addr);
  177. pmd[0] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
  178. pfn += SZ_1M >> PAGE_SHIFT;
  179. pmd[1] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
  180. pfn += SZ_1M >> PAGE_SHIFT;
  181. flush_pmd_entry(pmd);
  182. addr += PGDIR_SIZE;
  183. pgd++;
  184. } while (addr < end);
  185. return 0;
  186. }
  187. static int
  188. remap_area_supersections(unsigned long virt, unsigned long pfn,
  189. size_t size, const struct mem_type *type)
  190. {
  191. unsigned long addr = virt, end = virt + size;
  192. pgd_t *pgd;
  193. /*
  194. * Remove and free any PTE-based mapping, and
  195. * sync the current kernel mapping.
  196. */
  197. unmap_area_sections(virt, size);
  198. pgd = pgd_offset_k(virt);
  199. do {
  200. unsigned long super_pmd_val, i;
  201. super_pmd_val = __pfn_to_phys(pfn) | type->prot_sect |
  202. PMD_SECT_SUPER;
  203. super_pmd_val |= ((pfn >> (32 - PAGE_SHIFT)) & 0xf) << 20;
  204. for (i = 0; i < 8; i++) {
  205. pmd_t *pmd = pmd_offset(pgd, addr);
  206. pmd[0] = __pmd(super_pmd_val);
  207. pmd[1] = __pmd(super_pmd_val);
  208. flush_pmd_entry(pmd);
  209. addr += PGDIR_SIZE;
  210. pgd++;
  211. }
  212. pfn += SUPERSECTION_SIZE >> PAGE_SHIFT;
  213. } while (addr < end);
  214. return 0;
  215. }
  216. #endif
  217. /*
  218. * Remap an arbitrary physical address space into the kernel virtual
  219. * address space. Needed when the kernel wants to access high addresses
  220. * directly.
  221. *
  222. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  223. * have to convert them into an offset in a page-aligned mapping, but the
  224. * caller shouldn't need to know that small detail.
  225. *
  226. * 'flags' are the extra L_PTE_ flags that you want to specify for this
  227. * mapping. See <asm/pgtable.h> for more information.
  228. */
  229. void __iomem *
  230. __arm_ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
  231. unsigned int mtype)
  232. {
  233. const struct mem_type *type;
  234. int err;
  235. unsigned long addr;
  236. struct vm_struct * area;
  237. /*
  238. * High mappings must be supersection aligned
  239. */
  240. if (pfn >= 0x100000 && (__pfn_to_phys(pfn) & ~SUPERSECTION_MASK))
  241. return NULL;
  242. type = get_mem_type(mtype);
  243. if (!type)
  244. return NULL;
  245. /*
  246. * Page align the mapping size, taking account of any offset.
  247. */
  248. size = PAGE_ALIGN(offset + size);
  249. area = get_vm_area(size, VM_IOREMAP);
  250. if (!area)
  251. return NULL;
  252. addr = (unsigned long)area->addr;
  253. #ifndef CONFIG_SMP
  254. if (DOMAIN_IO == 0 &&
  255. (((cpu_architecture() >= CPU_ARCH_ARMv6) && (get_cr() & CR_XP)) ||
  256. cpu_is_xsc3()) && pfn >= 0x100000 &&
  257. !((__pfn_to_phys(pfn) | size | addr) & ~SUPERSECTION_MASK)) {
  258. area->flags |= VM_ARM_SECTION_MAPPING;
  259. err = remap_area_supersections(addr, pfn, size, type);
  260. } else if (!((__pfn_to_phys(pfn) | size | addr) & ~PMD_MASK)) {
  261. area->flags |= VM_ARM_SECTION_MAPPING;
  262. err = remap_area_sections(addr, pfn, size, type);
  263. } else
  264. #endif
  265. err = remap_area_pages(addr, pfn, size, type);
  266. if (err) {
  267. vunmap((void *)addr);
  268. return NULL;
  269. }
  270. flush_cache_vmap(addr, addr + size);
  271. return (void __iomem *) (offset + addr);
  272. }
  273. EXPORT_SYMBOL(__arm_ioremap_pfn);
  274. void __iomem *
  275. __arm_ioremap(unsigned long phys_addr, size_t size, unsigned int mtype)
  276. {
  277. unsigned long last_addr;
  278. unsigned long offset = phys_addr & ~PAGE_MASK;
  279. unsigned long pfn = __phys_to_pfn(phys_addr);
  280. /*
  281. * Don't allow wraparound or zero size
  282. */
  283. last_addr = phys_addr + size - 1;
  284. if (!size || last_addr < phys_addr)
  285. return NULL;
  286. return __arm_ioremap_pfn(pfn, offset, size, mtype);
  287. }
  288. EXPORT_SYMBOL(__arm_ioremap);
  289. void __iounmap(volatile void __iomem *io_addr)
  290. {
  291. void *addr = (void *)(PAGE_MASK & (unsigned long)io_addr);
  292. #ifndef CONFIG_SMP
  293. struct vm_struct **p, *tmp;
  294. /*
  295. * If this is a section based mapping we need to handle it
  296. * specially as the VM subsystem does not know how to handle
  297. * such a beast. We need the lock here b/c we need to clear
  298. * all the mappings before the area can be reclaimed
  299. * by someone else.
  300. */
  301. write_lock(&vmlist_lock);
  302. for (p = &vmlist ; (tmp = *p) ; p = &tmp->next) {
  303. if ((tmp->flags & VM_IOREMAP) && (tmp->addr == addr)) {
  304. if (tmp->flags & VM_ARM_SECTION_MAPPING) {
  305. unmap_area_sections((unsigned long)tmp->addr,
  306. tmp->size);
  307. }
  308. break;
  309. }
  310. }
  311. write_unlock(&vmlist_lock);
  312. #endif
  313. vunmap(addr);
  314. }
  315. EXPORT_SYMBOL(__iounmap);