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