ioremap.c 6.9 KB

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