ioremap.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * linux/arch/unicore32/mm/ioremap.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Copyright (C) 2001-2010 GUAN Xue-tao
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. *
  13. * Re-map IO memory to kernel address space so that we can access it.
  14. *
  15. * This allows a driver to remap an arbitrary region of bus memory into
  16. * virtual space. One should *only* use readl, writel, memcpy_toio and
  17. * so on with such remapped areas.
  18. *
  19. * Because UniCore only has a 32-bit address space we can't address the
  20. * whole of the (physical) PCI space at once. PCI huge-mode addressing
  21. * allows us to circumvent this restriction by splitting PCI space into
  22. * two 2GB chunks and mapping only one at a time into processor memory.
  23. * We use MMU protection domains to trap any attempt to access the bank
  24. * that is not currently mapped. (This isn't fully implemented yet.)
  25. */
  26. #include <linux/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/mm.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/io.h>
  31. #include <asm/cputype.h>
  32. #include <asm/cacheflush.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/pgalloc.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/sizes.h>
  37. #include <mach/map.h>
  38. #include "mm.h"
  39. /*
  40. * Used by ioremap() and iounmap() code to mark (super)section-mapped
  41. * I/O regions in vm_struct->flags field.
  42. */
  43. #define VM_UNICORE_SECTION_MAPPING 0x80000000
  44. int ioremap_page(unsigned long virt, unsigned long phys,
  45. const struct mem_type *mtype)
  46. {
  47. return ioremap_page_range(virt, virt + PAGE_SIZE, phys,
  48. __pgprot(mtype->prot_pte));
  49. }
  50. EXPORT_SYMBOL(ioremap_page);
  51. /*
  52. * Section support is unsafe on SMP - If you iounmap and ioremap a region,
  53. * the other CPUs will not see this change until their next context switch.
  54. * Meanwhile, (eg) if an interrupt comes in on one of those other CPUs
  55. * which requires the new ioremap'd region to be referenced, the CPU will
  56. * reference the _old_ region.
  57. *
  58. * Note that get_vm_area_caller() allocates a guard 4K page, so we need to
  59. * mask the size back to 4MB aligned or we will overflow in the loop below.
  60. */
  61. static void unmap_area_sections(unsigned long virt, unsigned long size)
  62. {
  63. unsigned long addr = virt, end = virt + (size & ~(SZ_4M - 1));
  64. pgd_t *pgd;
  65. flush_cache_vunmap(addr, end);
  66. pgd = pgd_offset_k(addr);
  67. do {
  68. pmd_t pmd, *pmdp = pmd_offset((pud_t *)pgd, addr);
  69. pmd = *pmdp;
  70. if (!pmd_none(pmd)) {
  71. /*
  72. * Clear the PMD from the page table, and
  73. * increment the kvm sequence so others
  74. * notice this change.
  75. *
  76. * Note: this is still racy on SMP machines.
  77. */
  78. pmd_clear(pmdp);
  79. /*
  80. * Free the page table, if there was one.
  81. */
  82. if ((pmd_val(pmd) & PMD_TYPE_MASK) == PMD_TYPE_TABLE)
  83. pte_free_kernel(&init_mm, pmd_page_vaddr(pmd));
  84. }
  85. addr += PGDIR_SIZE;
  86. pgd++;
  87. } while (addr < end);
  88. flush_tlb_kernel_range(virt, end);
  89. }
  90. static int
  91. remap_area_sections(unsigned long virt, unsigned long pfn,
  92. size_t size, const struct mem_type *type)
  93. {
  94. unsigned long addr = virt, end = virt + size;
  95. pgd_t *pgd;
  96. /*
  97. * Remove and free any PTE-based mapping, and
  98. * sync the current kernel mapping.
  99. */
  100. unmap_area_sections(virt, size);
  101. pgd = pgd_offset_k(addr);
  102. do {
  103. pmd_t *pmd = pmd_offset((pud_t *)pgd, addr);
  104. set_pmd(pmd, __pmd(__pfn_to_phys(pfn) | type->prot_sect));
  105. pfn += SZ_4M >> PAGE_SHIFT;
  106. flush_pmd_entry(pmd);
  107. addr += PGDIR_SIZE;
  108. pgd++;
  109. } while (addr < end);
  110. return 0;
  111. }
  112. void __iomem *__uc32_ioremap_pfn_caller(unsigned long pfn,
  113. unsigned long offset, size_t size, unsigned int mtype, void *caller)
  114. {
  115. const struct mem_type *type;
  116. int err;
  117. unsigned long addr;
  118. struct vm_struct *area;
  119. /*
  120. * High mappings must be section aligned
  121. */
  122. if (pfn >= 0x100000 && (__pfn_to_phys(pfn) & ~SECTION_MASK))
  123. return NULL;
  124. /*
  125. * Don't allow RAM to be mapped
  126. */
  127. if (pfn_valid(pfn)) {
  128. WARN(1, "BUG: Your driver calls ioremap() on\n"
  129. "system memory. This leads to architecturally\n"
  130. "unpredictable behaviour, and ioremap() will fail in\n"
  131. "the next kernel release. Please fix your driver.\n");
  132. return NULL;
  133. }
  134. type = get_mem_type(mtype);
  135. if (!type)
  136. return NULL;
  137. /*
  138. * Page align the mapping size, taking account of any offset.
  139. */
  140. size = PAGE_ALIGN(offset + size);
  141. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  142. if (!area)
  143. return NULL;
  144. addr = (unsigned long)area->addr;
  145. if (!((__pfn_to_phys(pfn) | size | addr) & ~PMD_MASK)) {
  146. area->flags |= VM_UNICORE_SECTION_MAPPING;
  147. err = remap_area_sections(addr, pfn, size, type);
  148. } else
  149. err = ioremap_page_range(addr, addr + size, __pfn_to_phys(pfn),
  150. __pgprot(type->prot_pte));
  151. if (err) {
  152. vunmap((void *)addr);
  153. return NULL;
  154. }
  155. flush_cache_vmap(addr, addr + size);
  156. return (void __iomem *) (offset + addr);
  157. }
  158. void __iomem *__uc32_ioremap_caller(unsigned long phys_addr, size_t size,
  159. unsigned int mtype, void *caller)
  160. {
  161. unsigned long last_addr;
  162. unsigned long offset = phys_addr & ~PAGE_MASK;
  163. unsigned long pfn = __phys_to_pfn(phys_addr);
  164. /*
  165. * Don't allow wraparound or zero size
  166. */
  167. last_addr = phys_addr + size - 1;
  168. if (!size || last_addr < phys_addr)
  169. return NULL;
  170. return __uc32_ioremap_pfn_caller(pfn, offset, size, mtype, caller);
  171. }
  172. /*
  173. * Remap an arbitrary physical address space into the kernel virtual
  174. * address space. Needed when the kernel wants to access high addresses
  175. * directly.
  176. *
  177. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  178. * have to convert them into an offset in a page-aligned mapping, but the
  179. * caller shouldn't need to know that small detail.
  180. */
  181. void __iomem *
  182. __uc32_ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
  183. unsigned int mtype)
  184. {
  185. return __uc32_ioremap_pfn_caller(pfn, offset, size, mtype,
  186. __builtin_return_address(0));
  187. }
  188. EXPORT_SYMBOL(__uc32_ioremap_pfn);
  189. void __iomem *
  190. __uc32_ioremap(unsigned long phys_addr, size_t size)
  191. {
  192. return __uc32_ioremap_caller(phys_addr, size, MT_DEVICE,
  193. __builtin_return_address(0));
  194. }
  195. EXPORT_SYMBOL(__uc32_ioremap);
  196. void __iomem *
  197. __uc32_ioremap_cached(unsigned long phys_addr, size_t size)
  198. {
  199. return __uc32_ioremap_caller(phys_addr, size, MT_DEVICE_CACHED,
  200. __builtin_return_address(0));
  201. }
  202. EXPORT_SYMBOL(__uc32_ioremap_cached);
  203. void __uc32_iounmap(volatile void __iomem *io_addr)
  204. {
  205. void *addr = (void *)(PAGE_MASK & (unsigned long)io_addr);
  206. struct vm_struct *vm;
  207. /*
  208. * If this is a section based mapping we need to handle it
  209. * specially as the VM subsystem does not know how to handle
  210. * such a beast. We need the lock here b/c we need to clear
  211. * all the mappings before the area can be reclaimed
  212. * by someone else.
  213. */
  214. vm = find_vm_area(addr);
  215. if (vm && (vm->flags & VM_IOREMAP) &&
  216. (vm->flags & VM_UNICORE_SECTION_MAPPING))
  217. unmap_area_sections((unsigned long)vm->addr, vm->size);
  218. vunmap(addr);
  219. }
  220. EXPORT_SYMBOL(__uc32_iounmap);