ioremap.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * arch/x86_64/mm/ioremap.c
  3. *
  4. * Re-map IO memory to kernel address space so that we can access it.
  5. * This is needed for high PCI addresses that aren't mapped in the
  6. * 640k-1MB IO memory area on PC's
  7. *
  8. * (C) Copyright 1995 1996 Linus Torvalds
  9. */
  10. #include <linux/vmalloc.h>
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <asm/io.h>
  15. #include <asm/pgalloc.h>
  16. #include <asm/fixmap.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/tlbflush.h>
  19. #include <asm/proto.h>
  20. #define ISA_START_ADDRESS 0xa0000
  21. #define ISA_END_ADDRESS 0x100000
  22. static inline void remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
  23. unsigned long phys_addr, unsigned long flags)
  24. {
  25. unsigned long end;
  26. unsigned long pfn;
  27. address &= ~PMD_MASK;
  28. end = address + size;
  29. if (end > PMD_SIZE)
  30. end = PMD_SIZE;
  31. if (address >= end)
  32. BUG();
  33. pfn = phys_addr >> PAGE_SHIFT;
  34. do {
  35. if (!pte_none(*pte)) {
  36. printk("remap_area_pte: page already exists\n");
  37. BUG();
  38. }
  39. set_pte(pte, pfn_pte(pfn, __pgprot(_PAGE_PRESENT | _PAGE_RW |
  40. _PAGE_GLOBAL | _PAGE_DIRTY | _PAGE_ACCESSED | flags)));
  41. address += PAGE_SIZE;
  42. pfn++;
  43. pte++;
  44. } while (address && (address < end));
  45. }
  46. static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
  47. unsigned long phys_addr, unsigned long flags)
  48. {
  49. unsigned long end;
  50. address &= ~PUD_MASK;
  51. end = address + size;
  52. if (end > PUD_SIZE)
  53. end = PUD_SIZE;
  54. phys_addr -= address;
  55. if (address >= end)
  56. BUG();
  57. do {
  58. pte_t * pte = pte_alloc_kernel(pmd, address);
  59. if (!pte)
  60. return -ENOMEM;
  61. remap_area_pte(pte, address, end - address, address + phys_addr, flags);
  62. address = (address + PMD_SIZE) & PMD_MASK;
  63. pmd++;
  64. } while (address && (address < end));
  65. return 0;
  66. }
  67. static inline int remap_area_pud(pud_t * pud, unsigned long address, unsigned long size,
  68. unsigned long phys_addr, unsigned long flags)
  69. {
  70. unsigned long end;
  71. address &= ~PGDIR_MASK;
  72. end = address + size;
  73. if (end > PGDIR_SIZE)
  74. end = PGDIR_SIZE;
  75. phys_addr -= address;
  76. if (address >= end)
  77. BUG();
  78. do {
  79. pmd_t * pmd = pmd_alloc(&init_mm, pud, address);
  80. if (!pmd)
  81. return -ENOMEM;
  82. remap_area_pmd(pmd, address, end - address, address + phys_addr, flags);
  83. address = (address + PUD_SIZE) & PUD_MASK;
  84. pud++;
  85. } while (address && (address < end));
  86. return 0;
  87. }
  88. static int remap_area_pages(unsigned long address, unsigned long phys_addr,
  89. unsigned long size, unsigned long flags)
  90. {
  91. int error;
  92. pgd_t *pgd;
  93. unsigned long end = address + size;
  94. phys_addr -= address;
  95. pgd = pgd_offset_k(address);
  96. flush_cache_all();
  97. if (address >= end)
  98. BUG();
  99. do {
  100. pud_t *pud;
  101. pud = pud_alloc(&init_mm, pgd, address);
  102. error = -ENOMEM;
  103. if (!pud)
  104. break;
  105. if (remap_area_pud(pud, address, end - address,
  106. phys_addr + address, flags))
  107. break;
  108. error = 0;
  109. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  110. pgd++;
  111. } while (address && (address < end));
  112. flush_tlb_all();
  113. return error;
  114. }
  115. /*
  116. * Fix up the linear direct mapping of the kernel to avoid cache attribute
  117. * conflicts.
  118. */
  119. static int
  120. ioremap_change_attr(unsigned long phys_addr, unsigned long size,
  121. unsigned long flags)
  122. {
  123. int err = 0;
  124. if (phys_addr + size - 1 < (end_pfn_map << PAGE_SHIFT)) {
  125. unsigned long npages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  126. unsigned long vaddr = (unsigned long) __va(phys_addr);
  127. /*
  128. * Must use a address here and not struct page because the phys addr
  129. * can be a in hole between nodes and not have an memmap entry.
  130. */
  131. err = change_page_attr_addr(vaddr,npages,__pgprot(__PAGE_KERNEL|flags));
  132. if (!err)
  133. global_flush_tlb();
  134. }
  135. return err;
  136. }
  137. /*
  138. * Generic mapping function
  139. */
  140. /*
  141. * Remap an arbitrary physical address space into the kernel virtual
  142. * address space. Needed when the kernel wants to access high addresses
  143. * directly.
  144. *
  145. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  146. * have to convert them into an offset in a page-aligned mapping, but the
  147. * caller shouldn't need to know that small detail.
  148. */
  149. void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  150. {
  151. void * addr;
  152. struct vm_struct * area;
  153. unsigned long offset, last_addr;
  154. /* Don't allow wraparound or zero size */
  155. last_addr = phys_addr + size - 1;
  156. if (!size || last_addr < phys_addr)
  157. return NULL;
  158. /*
  159. * Don't remap the low PCI/ISA area, it's always mapped..
  160. */
  161. if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
  162. return (__force void __iomem *)phys_to_virt(phys_addr);
  163. #ifdef CONFIG_FLATMEM
  164. /*
  165. * Don't allow anybody to remap normal RAM that we're using..
  166. */
  167. if (last_addr < virt_to_phys(high_memory)) {
  168. char *t_addr, *t_end;
  169. struct page *page;
  170. t_addr = __va(phys_addr);
  171. t_end = t_addr + (size - 1);
  172. for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
  173. if(!PageReserved(page))
  174. return NULL;
  175. }
  176. #endif
  177. /*
  178. * Mappings have to be page-aligned
  179. */
  180. offset = phys_addr & ~PAGE_MASK;
  181. phys_addr &= PAGE_MASK;
  182. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  183. /*
  184. * Ok, go for it..
  185. */
  186. area = get_vm_area(size, VM_IOREMAP | (flags << 20));
  187. if (!area)
  188. return NULL;
  189. area->phys_addr = phys_addr;
  190. addr = area->addr;
  191. if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
  192. remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
  193. return NULL;
  194. }
  195. if (flags && ioremap_change_attr(phys_addr, size, flags) < 0) {
  196. area->flags &= 0xffffff;
  197. vunmap(addr);
  198. return NULL;
  199. }
  200. return (__force void __iomem *) (offset + (char *)addr);
  201. }
  202. EXPORT_SYMBOL(__ioremap);
  203. /**
  204. * ioremap_nocache - map bus memory into CPU space
  205. * @offset: bus address of the memory
  206. * @size: size of the resource to map
  207. *
  208. * ioremap_nocache performs a platform specific sequence of operations to
  209. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  210. * writew/writel functions and the other mmio helpers. The returned
  211. * address is not guaranteed to be usable directly as a virtual
  212. * address.
  213. *
  214. * This version of ioremap ensures that the memory is marked uncachable
  215. * on the CPU as well as honouring existing caching rules from things like
  216. * the PCI bus. Note that there are other caches and buffers on many
  217. * busses. In particular driver authors should read up on PCI writes
  218. *
  219. * It's useful if some control registers are in such an area and
  220. * write combining or read caching is not desirable:
  221. *
  222. * Must be freed with iounmap.
  223. */
  224. void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
  225. {
  226. return __ioremap(phys_addr, size, _PAGE_PCD);
  227. }
  228. EXPORT_SYMBOL(ioremap_nocache);
  229. /**
  230. * iounmap - Free a IO remapping
  231. * @addr: virtual address from ioremap_*
  232. *
  233. * Caller must ensure there is only one unmapping for the same pointer.
  234. */
  235. void iounmap(volatile void __iomem *addr)
  236. {
  237. struct vm_struct *p, *o;
  238. if (addr <= high_memory)
  239. return;
  240. if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
  241. addr < phys_to_virt(ISA_END_ADDRESS))
  242. return;
  243. addr = (volatile void __iomem *)(PAGE_MASK & (unsigned long __force)addr);
  244. /* Use the vm area unlocked, assuming the caller
  245. ensures there isn't another iounmap for the same address
  246. in parallel. Reuse of the virtual address is prevented by
  247. leaving it in the global lists until we're done with it.
  248. cpa takes care of the direct mappings. */
  249. read_lock(&vmlist_lock);
  250. for (p = vmlist; p; p = p->next) {
  251. if (p->addr == addr)
  252. break;
  253. }
  254. read_unlock(&vmlist_lock);
  255. if (!p) {
  256. printk("iounmap: bad address %p\n", addr);
  257. dump_stack();
  258. return;
  259. }
  260. /* Reset the direct mapping. Can block */
  261. if (p->flags >> 20)
  262. ioremap_change_attr(p->phys_addr, p->size, 0);
  263. /* Finally remove it */
  264. o = remove_vm_area((void *)addr);
  265. BUG_ON(p != o || o == NULL);
  266. kfree(p);
  267. }
  268. EXPORT_SYMBOL(iounmap);