ioremap.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * arch/i386/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/fixmap.h>
  16. #include <asm/cacheflush.h>
  17. #include <asm/tlbflush.h>
  18. #include <asm/pgtable.h>
  19. #define ISA_START_ADDRESS 0xa0000
  20. #define ISA_END_ADDRESS 0x100000
  21. static int ioremap_pte_range(pmd_t *pmd, unsigned long addr,
  22. unsigned long end, unsigned long phys_addr, unsigned long flags)
  23. {
  24. pte_t *pte;
  25. unsigned long pfn;
  26. pfn = phys_addr >> PAGE_SHIFT;
  27. pte = pte_alloc_kernel(&init_mm, pmd, addr);
  28. if (!pte)
  29. return -ENOMEM;
  30. do {
  31. BUG_ON(!pte_none(*pte));
  32. set_pte(pte, pfn_pte(pfn, __pgprot(_PAGE_PRESENT | _PAGE_RW |
  33. _PAGE_DIRTY | _PAGE_ACCESSED | flags)));
  34. pfn++;
  35. } while (pte++, addr += PAGE_SIZE, addr != end);
  36. return 0;
  37. }
  38. static inline int ioremap_pmd_range(pud_t *pud, unsigned long addr,
  39. unsigned long end, unsigned long phys_addr, unsigned long flags)
  40. {
  41. pmd_t *pmd;
  42. unsigned long next;
  43. phys_addr -= addr;
  44. pmd = pmd_alloc(&init_mm, pud, addr);
  45. if (!pmd)
  46. return -ENOMEM;
  47. do {
  48. next = pmd_addr_end(addr, end);
  49. if (ioremap_pte_range(pmd, addr, next, phys_addr + addr, flags))
  50. return -ENOMEM;
  51. } while (pmd++, addr = next, addr != end);
  52. return 0;
  53. }
  54. static inline int ioremap_pud_range(pgd_t *pgd, unsigned long addr,
  55. unsigned long end, unsigned long phys_addr, unsigned long flags)
  56. {
  57. pud_t *pud;
  58. unsigned long next;
  59. phys_addr -= addr;
  60. pud = pud_alloc(&init_mm, pgd, addr);
  61. if (!pud)
  62. return -ENOMEM;
  63. do {
  64. next = pud_addr_end(addr, end);
  65. if (ioremap_pmd_range(pud, addr, next, phys_addr + addr, flags))
  66. return -ENOMEM;
  67. } while (pud++, addr = next, addr != end);
  68. return 0;
  69. }
  70. static int ioremap_page_range(unsigned long addr,
  71. unsigned long end, unsigned long phys_addr, unsigned long flags)
  72. {
  73. pgd_t *pgd;
  74. unsigned long next;
  75. int err;
  76. BUG_ON(addr >= end);
  77. flush_cache_all();
  78. phys_addr -= addr;
  79. pgd = pgd_offset_k(addr);
  80. spin_lock(&init_mm.page_table_lock);
  81. do {
  82. next = pgd_addr_end(addr, end);
  83. err = ioremap_pud_range(pgd, addr, next, phys_addr+addr, flags);
  84. if (err)
  85. break;
  86. } while (pgd++, addr = next, addr != end);
  87. spin_unlock(&init_mm.page_table_lock);
  88. flush_tlb_all();
  89. return err;
  90. }
  91. /*
  92. * Generic mapping function (not visible outside):
  93. */
  94. /*
  95. * Remap an arbitrary physical address space into the kernel virtual
  96. * address space. Needed when the kernel wants to access high addresses
  97. * directly.
  98. *
  99. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  100. * have to convert them into an offset in a page-aligned mapping, but the
  101. * caller shouldn't need to know that small detail.
  102. */
  103. void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  104. {
  105. void __iomem * addr;
  106. struct vm_struct * area;
  107. unsigned long offset, last_addr;
  108. /* Don't allow wraparound or zero size */
  109. last_addr = phys_addr + size - 1;
  110. if (!size || last_addr < phys_addr)
  111. return NULL;
  112. /*
  113. * Don't remap the low PCI/ISA area, it's always mapped..
  114. */
  115. if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
  116. return (void __iomem *) phys_to_virt(phys_addr);
  117. /*
  118. * Don't allow anybody to remap normal RAM that we're using..
  119. */
  120. if (phys_addr <= virt_to_phys(high_memory - 1)) {
  121. char *t_addr, *t_end;
  122. struct page *page;
  123. t_addr = __va(phys_addr);
  124. t_end = t_addr + (size - 1);
  125. for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
  126. if(!PageReserved(page))
  127. return NULL;
  128. }
  129. /*
  130. * Mappings have to be page-aligned
  131. */
  132. offset = phys_addr & ~PAGE_MASK;
  133. phys_addr &= PAGE_MASK;
  134. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  135. /*
  136. * Ok, go for it..
  137. */
  138. area = get_vm_area(size, VM_IOREMAP | (flags << 20));
  139. if (!area)
  140. return NULL;
  141. area->phys_addr = phys_addr;
  142. addr = (void __iomem *) area->addr;
  143. if (ioremap_page_range((unsigned long) addr,
  144. (unsigned long) addr + size, phys_addr, flags)) {
  145. vunmap((void __force *) addr);
  146. return NULL;
  147. }
  148. return (void __iomem *) (offset + (char __iomem *)addr);
  149. }
  150. EXPORT_SYMBOL(__ioremap);
  151. /**
  152. * ioremap_nocache - map bus memory into CPU space
  153. * @offset: bus address of the memory
  154. * @size: size of the resource to map
  155. *
  156. * ioremap_nocache performs a platform specific sequence of operations to
  157. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  158. * writew/writel functions and the other mmio helpers. The returned
  159. * address is not guaranteed to be usable directly as a virtual
  160. * address.
  161. *
  162. * This version of ioremap ensures that the memory is marked uncachable
  163. * on the CPU as well as honouring existing caching rules from things like
  164. * the PCI bus. Note that there are other caches and buffers on many
  165. * busses. In particular driver authors should read up on PCI writes
  166. *
  167. * It's useful if some control registers are in such an area and
  168. * write combining or read caching is not desirable:
  169. *
  170. * Must be freed with iounmap.
  171. */
  172. void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
  173. {
  174. unsigned long last_addr;
  175. void __iomem *p = __ioremap(phys_addr, size, _PAGE_PCD);
  176. if (!p)
  177. return p;
  178. /* Guaranteed to be > phys_addr, as per __ioremap() */
  179. last_addr = phys_addr + size - 1;
  180. if (last_addr < virt_to_phys(high_memory) - 1) {
  181. struct page *ppage = virt_to_page(__va(phys_addr));
  182. unsigned long npages;
  183. phys_addr &= PAGE_MASK;
  184. /* This might overflow and become zero.. */
  185. last_addr = PAGE_ALIGN(last_addr);
  186. /* .. but that's ok, because modulo-2**n arithmetic will make
  187. * the page-aligned "last - first" come out right.
  188. */
  189. npages = (last_addr - phys_addr) >> PAGE_SHIFT;
  190. if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) {
  191. iounmap(p);
  192. p = NULL;
  193. }
  194. global_flush_tlb();
  195. }
  196. return p;
  197. }
  198. EXPORT_SYMBOL(ioremap_nocache);
  199. void iounmap(volatile void __iomem *addr)
  200. {
  201. struct vm_struct *p;
  202. if ((void __force *)addr <= high_memory)
  203. return;
  204. /*
  205. * __ioremap special-cases the PCI/ISA range by not instantiating a
  206. * vm_area and by simply returning an address into the kernel mapping
  207. * of ISA space. So handle that here.
  208. */
  209. if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
  210. addr < phys_to_virt(ISA_END_ADDRESS))
  211. return;
  212. write_lock(&vmlist_lock);
  213. p = __remove_vm_area((void *)(PAGE_MASK & (unsigned long __force)addr));
  214. if (!p) {
  215. printk(KERN_WARNING "iounmap: bad address %p\n", addr);
  216. dump_stack();
  217. goto out_unlock;
  218. }
  219. if ((p->flags >> 20) && p->phys_addr < virt_to_phys(high_memory) - 1) {
  220. change_page_attr(virt_to_page(__va(p->phys_addr)),
  221. p->size >> PAGE_SHIFT,
  222. PAGE_KERNEL);
  223. global_flush_tlb();
  224. }
  225. out_unlock:
  226. write_unlock(&vmlist_lock);
  227. kfree(p);
  228. }
  229. EXPORT_SYMBOL(iounmap);
  230. void __init *bt_ioremap(unsigned long phys_addr, unsigned long size)
  231. {
  232. unsigned long offset, last_addr;
  233. unsigned int nrpages;
  234. enum fixed_addresses idx;
  235. /* Don't allow wraparound or zero size */
  236. last_addr = phys_addr + size - 1;
  237. if (!size || last_addr < phys_addr)
  238. return NULL;
  239. /*
  240. * Don't remap the low PCI/ISA area, it's always mapped..
  241. */
  242. if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
  243. return phys_to_virt(phys_addr);
  244. /*
  245. * Mappings have to be page-aligned
  246. */
  247. offset = phys_addr & ~PAGE_MASK;
  248. phys_addr &= PAGE_MASK;
  249. size = PAGE_ALIGN(last_addr) - phys_addr;
  250. /*
  251. * Mappings have to fit in the FIX_BTMAP area.
  252. */
  253. nrpages = size >> PAGE_SHIFT;
  254. if (nrpages > NR_FIX_BTMAPS)
  255. return NULL;
  256. /*
  257. * Ok, go for it..
  258. */
  259. idx = FIX_BTMAP_BEGIN;
  260. while (nrpages > 0) {
  261. set_fixmap(idx, phys_addr);
  262. phys_addr += PAGE_SIZE;
  263. --idx;
  264. --nrpages;
  265. }
  266. return (void*) (offset + fix_to_virt(FIX_BTMAP_BEGIN));
  267. }
  268. void __init bt_iounmap(void *addr, unsigned long size)
  269. {
  270. unsigned long virt_addr;
  271. unsigned long offset;
  272. unsigned int nrpages;
  273. enum fixed_addresses idx;
  274. virt_addr = (unsigned long)addr;
  275. if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN))
  276. return;
  277. offset = virt_addr & ~PAGE_MASK;
  278. nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
  279. idx = FIX_BTMAP_BEGIN;
  280. while (nrpages > 0) {
  281. clear_fixmap(idx);
  282. --idx;
  283. --nrpages;
  284. }
  285. }