ioremap_32.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <linux/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. /*
  22. * Generic mapping function (not visible outside):
  23. */
  24. /*
  25. * Remap an arbitrary physical address space into the kernel virtual
  26. * address space. Needed when the kernel wants to access high addresses
  27. * directly.
  28. *
  29. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  30. * have to convert them into an offset in a page-aligned mapping, but the
  31. * caller shouldn't need to know that small detail.
  32. */
  33. void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  34. {
  35. void __iomem * addr;
  36. struct vm_struct * area;
  37. unsigned long offset, last_addr;
  38. pgprot_t prot;
  39. /* Don't allow wraparound or zero size */
  40. last_addr = phys_addr + size - 1;
  41. if (!size || last_addr < phys_addr)
  42. return NULL;
  43. /*
  44. * Don't remap the low PCI/ISA area, it's always mapped..
  45. */
  46. if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
  47. return (void __iomem *) phys_to_virt(phys_addr);
  48. /*
  49. * Don't allow anybody to remap normal RAM that we're using..
  50. */
  51. if (phys_addr <= virt_to_phys(high_memory - 1)) {
  52. char *t_addr, *t_end;
  53. struct page *page;
  54. t_addr = __va(phys_addr);
  55. t_end = t_addr + (size - 1);
  56. for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
  57. if(!PageReserved(page))
  58. return NULL;
  59. }
  60. prot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY
  61. | _PAGE_ACCESSED | flags);
  62. /*
  63. * Mappings have to be page-aligned
  64. */
  65. offset = phys_addr & ~PAGE_MASK;
  66. phys_addr &= PAGE_MASK;
  67. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  68. /*
  69. * Ok, go for it..
  70. */
  71. area = get_vm_area(size, VM_IOREMAP | (flags << 20));
  72. if (!area)
  73. return NULL;
  74. area->phys_addr = phys_addr;
  75. addr = (void __iomem *) area->addr;
  76. if (ioremap_page_range((unsigned long) addr,
  77. (unsigned long) addr + size, phys_addr, prot)) {
  78. vunmap((void __force *) addr);
  79. return NULL;
  80. }
  81. return (void __iomem *) (offset + (char __iomem *)addr);
  82. }
  83. EXPORT_SYMBOL(__ioremap);
  84. /**
  85. * ioremap_nocache - map bus memory into CPU space
  86. * @offset: bus address of the memory
  87. * @size: size of the resource to map
  88. *
  89. * ioremap_nocache performs a platform specific sequence of operations to
  90. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  91. * writew/writel functions and the other mmio helpers. The returned
  92. * address is not guaranteed to be usable directly as a virtual
  93. * address.
  94. *
  95. * This version of ioremap ensures that the memory is marked uncachable
  96. * on the CPU as well as honouring existing caching rules from things like
  97. * the PCI bus. Note that there are other caches and buffers on many
  98. * busses. In particular driver authors should read up on PCI writes
  99. *
  100. * It's useful if some control registers are in such an area and
  101. * write combining or read caching is not desirable:
  102. *
  103. * Must be freed with iounmap.
  104. */
  105. void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
  106. {
  107. unsigned long last_addr;
  108. void __iomem *p = __ioremap(phys_addr, size, _PAGE_PCD);
  109. if (!p)
  110. return p;
  111. /* Guaranteed to be > phys_addr, as per __ioremap() */
  112. last_addr = phys_addr + size - 1;
  113. if (last_addr < virt_to_phys(high_memory) - 1) {
  114. struct page *ppage = virt_to_page(__va(phys_addr));
  115. unsigned long npages;
  116. phys_addr &= PAGE_MASK;
  117. /* This might overflow and become zero.. */
  118. last_addr = PAGE_ALIGN(last_addr);
  119. /* .. but that's ok, because modulo-2**n arithmetic will make
  120. * the page-aligned "last - first" come out right.
  121. */
  122. npages = (last_addr - phys_addr) >> PAGE_SHIFT;
  123. if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) {
  124. iounmap(p);
  125. p = NULL;
  126. }
  127. global_flush_tlb();
  128. }
  129. return p;
  130. }
  131. EXPORT_SYMBOL(ioremap_nocache);
  132. /**
  133. * iounmap - Free a IO remapping
  134. * @addr: virtual address from ioremap_*
  135. *
  136. * Caller must ensure there is only one unmapping for the same pointer.
  137. */
  138. void iounmap(volatile void __iomem *addr)
  139. {
  140. struct vm_struct *p, *o;
  141. if ((void __force *)addr <= high_memory)
  142. return;
  143. /*
  144. * __ioremap special-cases the PCI/ISA range by not instantiating a
  145. * vm_area and by simply returning an address into the kernel mapping
  146. * of ISA space. So handle that here.
  147. */
  148. if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
  149. addr < phys_to_virt(ISA_END_ADDRESS))
  150. return;
  151. addr = (volatile void __iomem *)(PAGE_MASK & (unsigned long __force)addr);
  152. /* Use the vm area unlocked, assuming the caller
  153. ensures there isn't another iounmap for the same address
  154. in parallel. Reuse of the virtual address is prevented by
  155. leaving it in the global lists until we're done with it.
  156. cpa takes care of the direct mappings. */
  157. read_lock(&vmlist_lock);
  158. for (p = vmlist; p; p = p->next) {
  159. if (p->addr == addr)
  160. break;
  161. }
  162. read_unlock(&vmlist_lock);
  163. if (!p) {
  164. printk("iounmap: bad address %p\n", addr);
  165. dump_stack();
  166. return;
  167. }
  168. /* Reset the direct mapping. Can block */
  169. if ((p->flags >> 20) && p->phys_addr < virt_to_phys(high_memory) - 1) {
  170. change_page_attr(virt_to_page(__va(p->phys_addr)),
  171. get_vm_area_size(p) >> PAGE_SHIFT,
  172. PAGE_KERNEL);
  173. global_flush_tlb();
  174. }
  175. /* Finally remove it */
  176. o = remove_vm_area((void *)addr);
  177. BUG_ON(p != o || o == NULL);
  178. kfree(p);
  179. }
  180. EXPORT_SYMBOL(iounmap);
  181. void __init *bt_ioremap(unsigned long phys_addr, unsigned long size)
  182. {
  183. unsigned long offset, last_addr;
  184. unsigned int nrpages;
  185. enum fixed_addresses idx;
  186. /* Don't allow wraparound or zero size */
  187. last_addr = phys_addr + size - 1;
  188. if (!size || last_addr < phys_addr)
  189. return NULL;
  190. /*
  191. * Don't remap the low PCI/ISA area, it's always mapped..
  192. */
  193. if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
  194. return phys_to_virt(phys_addr);
  195. /*
  196. * Mappings have to be page-aligned
  197. */
  198. offset = phys_addr & ~PAGE_MASK;
  199. phys_addr &= PAGE_MASK;
  200. size = PAGE_ALIGN(last_addr) - phys_addr;
  201. /*
  202. * Mappings have to fit in the FIX_BTMAP area.
  203. */
  204. nrpages = size >> PAGE_SHIFT;
  205. if (nrpages > NR_FIX_BTMAPS)
  206. return NULL;
  207. /*
  208. * Ok, go for it..
  209. */
  210. idx = FIX_BTMAP_BEGIN;
  211. while (nrpages > 0) {
  212. set_fixmap(idx, phys_addr);
  213. phys_addr += PAGE_SIZE;
  214. --idx;
  215. --nrpages;
  216. }
  217. return (void*) (offset + fix_to_virt(FIX_BTMAP_BEGIN));
  218. }
  219. void __init bt_iounmap(void *addr, unsigned long size)
  220. {
  221. unsigned long virt_addr;
  222. unsigned long offset;
  223. unsigned int nrpages;
  224. enum fixed_addresses idx;
  225. virt_addr = (unsigned long)addr;
  226. if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN))
  227. return;
  228. offset = virt_addr & ~PAGE_MASK;
  229. nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
  230. idx = FIX_BTMAP_BEGIN;
  231. while (nrpages > 0) {
  232. clear_fixmap(idx);
  233. --idx;
  234. --nrpages;
  235. }
  236. }