ioremap.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 <asm/cacheflush.h>
  28. #include <asm/io.h>
  29. #include <asm/mmu_context.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/tlbflush.h>
  32. #include <asm/sizes.h>
  33. #include <asm/mach/map.h>
  34. #include "mm.h"
  35. /*
  36. * Used by ioremap() and iounmap() code to mark (super)section-mapped
  37. * I/O regions in vm_struct->flags field.
  38. */
  39. #define VM_ARM_SECTION_MAPPING 0x80000000
  40. static int remap_area_pte(pmd_t *pmd, unsigned long addr, unsigned long end,
  41. unsigned long phys_addr, const struct mem_type *type)
  42. {
  43. pgprot_t prot = __pgprot(type->prot_pte);
  44. pte_t *pte;
  45. pte = pte_alloc_kernel(pmd, addr);
  46. if (!pte)
  47. return -ENOMEM;
  48. do {
  49. if (!pte_none(*pte))
  50. goto bad;
  51. set_pte_ext(pte, pfn_pte(phys_addr >> PAGE_SHIFT, prot),
  52. type->prot_pte_ext);
  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. void __check_kvm_seq(struct mm_struct *mm)
  99. {
  100. unsigned int seq;
  101. do {
  102. seq = init_mm.context.kvm_seq;
  103. memcpy(pgd_offset(mm, VMALLOC_START),
  104. pgd_offset_k(VMALLOC_START),
  105. sizeof(pgd_t) * (pgd_index(VMALLOC_END) -
  106. pgd_index(VMALLOC_START)));
  107. mm->context.kvm_seq = seq;
  108. } while (seq != init_mm.context.kvm_seq);
  109. }
  110. #ifndef CONFIG_SMP
  111. /*
  112. * Section support is unsafe on SMP - If you iounmap and ioremap a region,
  113. * the other CPUs will not see this change until their next context switch.
  114. * Meanwhile, (eg) if an interrupt comes in on one of those other CPUs
  115. * which requires the new ioremap'd region to be referenced, the CPU will
  116. * reference the _old_ region.
  117. *
  118. * Note that get_vm_area() allocates a guard 4K page, so we need to mask
  119. * the size back to 1MB aligned or we will overflow in the loop below.
  120. */
  121. static void unmap_area_sections(unsigned long virt, unsigned long size)
  122. {
  123. unsigned long addr = virt, end = virt + (size & ~SZ_1M);
  124. pgd_t *pgd;
  125. flush_cache_vunmap(addr, end);
  126. pgd = pgd_offset_k(addr);
  127. do {
  128. pmd_t pmd, *pmdp = pmd_offset(pgd, addr);
  129. pmd = *pmdp;
  130. if (!pmd_none(pmd)) {
  131. /*
  132. * Clear the PMD from the page table, and
  133. * increment the kvm sequence so others
  134. * notice this change.
  135. *
  136. * Note: this is still racy on SMP machines.
  137. */
  138. pmd_clear(pmdp);
  139. init_mm.context.kvm_seq++;
  140. /*
  141. * Free the page table, if there was one.
  142. */
  143. if ((pmd_val(pmd) & PMD_TYPE_MASK) == PMD_TYPE_TABLE)
  144. pte_free_kernel(pmd_page_vaddr(pmd));
  145. }
  146. addr += PGDIR_SIZE;
  147. pgd++;
  148. } while (addr < end);
  149. /*
  150. * Ensure that the active_mm is up to date - we want to
  151. * catch any use-after-iounmap cases.
  152. */
  153. if (current->active_mm->context.kvm_seq != init_mm.context.kvm_seq)
  154. __check_kvm_seq(current->active_mm);
  155. flush_tlb_kernel_range(virt, end);
  156. }
  157. static int
  158. remap_area_sections(unsigned long virt, unsigned long pfn,
  159. size_t size, const struct mem_type *type)
  160. {
  161. unsigned long addr = virt, end = virt + size;
  162. pgd_t *pgd;
  163. /*
  164. * Remove and free any PTE-based mapping, and
  165. * sync the current kernel mapping.
  166. */
  167. unmap_area_sections(virt, size);
  168. pgd = pgd_offset_k(addr);
  169. do {
  170. pmd_t *pmd = pmd_offset(pgd, addr);
  171. pmd[0] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
  172. pfn += SZ_1M >> PAGE_SHIFT;
  173. pmd[1] = __pmd(__pfn_to_phys(pfn) | type->prot_sect);
  174. pfn += SZ_1M >> PAGE_SHIFT;
  175. flush_pmd_entry(pmd);
  176. addr += PGDIR_SIZE;
  177. pgd++;
  178. } while (addr < end);
  179. return 0;
  180. }
  181. static int
  182. remap_area_supersections(unsigned long virt, unsigned long pfn,
  183. size_t size, const struct mem_type *type)
  184. {
  185. unsigned long addr = virt, end = virt + size;
  186. pgd_t *pgd;
  187. /*
  188. * Remove and free any PTE-based mapping, and
  189. * sync the current kernel mapping.
  190. */
  191. unmap_area_sections(virt, size);
  192. pgd = pgd_offset_k(virt);
  193. do {
  194. unsigned long super_pmd_val, i;
  195. super_pmd_val = __pfn_to_phys(pfn) | type->prot_sect |
  196. PMD_SECT_SUPER;
  197. super_pmd_val |= ((pfn >> (32 - PAGE_SHIFT)) & 0xf) << 20;
  198. for (i = 0; i < 8; i++) {
  199. pmd_t *pmd = pmd_offset(pgd, addr);
  200. pmd[0] = __pmd(super_pmd_val);
  201. pmd[1] = __pmd(super_pmd_val);
  202. flush_pmd_entry(pmd);
  203. addr += PGDIR_SIZE;
  204. pgd++;
  205. }
  206. pfn += SUPERSECTION_SIZE >> PAGE_SHIFT;
  207. } while (addr < end);
  208. return 0;
  209. }
  210. #endif
  211. /*
  212. * Remap an arbitrary physical address space into the kernel virtual
  213. * address space. Needed when the kernel wants to access high addresses
  214. * directly.
  215. *
  216. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  217. * have to convert them into an offset in a page-aligned mapping, but the
  218. * caller shouldn't need to know that small detail.
  219. *
  220. * 'flags' are the extra L_PTE_ flags that you want to specify for this
  221. * mapping. See include/asm-arm/proc-armv/pgtable.h for more information.
  222. */
  223. void __iomem *
  224. __arm_ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
  225. unsigned int mtype)
  226. {
  227. const struct mem_type *type;
  228. int err;
  229. unsigned long addr;
  230. struct vm_struct * area;
  231. /*
  232. * High mappings must be supersection aligned
  233. */
  234. if (pfn >= 0x100000 && (__pfn_to_phys(pfn) & ~SUPERSECTION_MASK))
  235. return NULL;
  236. type = get_mem_type(mtype);
  237. if (!type)
  238. return NULL;
  239. /*
  240. * Page align the mapping size, taking account of any offset.
  241. */
  242. size = PAGE_ALIGN(offset + size);
  243. area = get_vm_area(size, VM_IOREMAP);
  244. if (!area)
  245. return NULL;
  246. addr = (unsigned long)area->addr;
  247. #ifndef CONFIG_SMP
  248. if (DOMAIN_IO == 0 &&
  249. (((cpu_architecture() >= CPU_ARCH_ARMv6) && (get_cr() & CR_XP)) ||
  250. cpu_is_xsc3()) && pfn >= 0x100000 &&
  251. !((__pfn_to_phys(pfn) | size | addr) & ~SUPERSECTION_MASK)) {
  252. area->flags |= VM_ARM_SECTION_MAPPING;
  253. err = remap_area_supersections(addr, pfn, size, type);
  254. } else if (!((__pfn_to_phys(pfn) | size | addr) & ~PMD_MASK)) {
  255. area->flags |= VM_ARM_SECTION_MAPPING;
  256. err = remap_area_sections(addr, pfn, size, type);
  257. } else
  258. #endif
  259. err = remap_area_pages(addr, pfn, size, type);
  260. if (err) {
  261. vunmap((void *)addr);
  262. return NULL;
  263. }
  264. flush_cache_vmap(addr, addr + size);
  265. return (void __iomem *) (offset + addr);
  266. }
  267. EXPORT_SYMBOL(__arm_ioremap_pfn);
  268. void __iomem *
  269. __arm_ioremap(unsigned long phys_addr, size_t size, unsigned int mtype)
  270. {
  271. unsigned long last_addr;
  272. unsigned long offset = phys_addr & ~PAGE_MASK;
  273. unsigned long pfn = __phys_to_pfn(phys_addr);
  274. /*
  275. * Don't allow wraparound or zero size
  276. */
  277. last_addr = phys_addr + size - 1;
  278. if (!size || last_addr < phys_addr)
  279. return NULL;
  280. return __arm_ioremap_pfn(pfn, offset, size, mtype);
  281. }
  282. EXPORT_SYMBOL(__arm_ioremap);
  283. void __iounmap(volatile void __iomem *addr)
  284. {
  285. #ifndef CONFIG_SMP
  286. struct vm_struct **p, *tmp;
  287. #endif
  288. unsigned int section_mapping = 0;
  289. addr = (volatile void __iomem *)(PAGE_MASK & (unsigned long)addr);
  290. #ifndef CONFIG_SMP
  291. /*
  292. * If this is a section based mapping we need to handle it
  293. * specially as the VM subsystem does not know how to handle
  294. * such a beast. We need the lock here b/c we need to clear
  295. * all the mappings before the area can be reclaimed
  296. * by someone else.
  297. */
  298. write_lock(&vmlist_lock);
  299. for (p = &vmlist ; (tmp = *p) ; p = &tmp->next) {
  300. if((tmp->flags & VM_IOREMAP) && (tmp->addr == addr)) {
  301. if (tmp->flags & VM_ARM_SECTION_MAPPING) {
  302. *p = tmp->next;
  303. unmap_area_sections((unsigned long)tmp->addr,
  304. tmp->size);
  305. kfree(tmp);
  306. section_mapping = 1;
  307. }
  308. break;
  309. }
  310. }
  311. write_unlock(&vmlist_lock);
  312. #endif
  313. if (!section_mapping)
  314. vunmap((void __force *)addr);
  315. }
  316. EXPORT_SYMBOL(__iounmap);