ioremap.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <linux/io.h>
  15. #include <asm/pgalloc.h>
  16. #include <asm/fixmap.h>
  17. #include <asm/tlbflush.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/proto.h>
  20. #define ISA_START_ADDRESS 0xa0000
  21. #define ISA_END_ADDRESS 0x100000
  22. /*
  23. * Fix up the linear direct mapping of the kernel to avoid cache attribute
  24. * conflicts.
  25. */
  26. static int
  27. ioremap_change_attr(unsigned long phys_addr, unsigned long size,
  28. unsigned long flags)
  29. {
  30. int err = 0;
  31. if (phys_addr + size - 1 < (end_pfn_map << PAGE_SHIFT)) {
  32. unsigned long npages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  33. unsigned long vaddr = (unsigned long) __va(phys_addr);
  34. /*
  35. * Must use a address here and not struct page because the phys addr
  36. * can be a in hole between nodes and not have an memmap entry.
  37. */
  38. err = change_page_attr_addr(vaddr,npages,__pgprot(__PAGE_KERNEL|flags));
  39. if (!err)
  40. global_flush_tlb();
  41. }
  42. return err;
  43. }
  44. /*
  45. * Generic mapping function
  46. */
  47. /*
  48. * Remap an arbitrary physical address space into the kernel virtual
  49. * address space. Needed when the kernel wants to access high addresses
  50. * directly.
  51. *
  52. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  53. * have to convert them into an offset in a page-aligned mapping, but the
  54. * caller shouldn't need to know that small detail.
  55. */
  56. void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  57. {
  58. void * addr;
  59. struct vm_struct * area;
  60. unsigned long offset, last_addr;
  61. pgprot_t pgprot;
  62. /* Don't allow wraparound or zero size */
  63. last_addr = phys_addr + size - 1;
  64. if (!size || last_addr < phys_addr)
  65. return NULL;
  66. /*
  67. * Don't remap the low PCI/ISA area, it's always mapped..
  68. */
  69. if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
  70. return (__force void __iomem *)phys_to_virt(phys_addr);
  71. #ifdef CONFIG_FLATMEM
  72. /*
  73. * Don't allow anybody to remap normal RAM that we're using..
  74. */
  75. if (last_addr < virt_to_phys(high_memory)) {
  76. char *t_addr, *t_end;
  77. struct page *page;
  78. t_addr = __va(phys_addr);
  79. t_end = t_addr + (size - 1);
  80. for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
  81. if(!PageReserved(page))
  82. return NULL;
  83. }
  84. #endif
  85. pgprot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_GLOBAL
  86. | _PAGE_DIRTY | _PAGE_ACCESSED | flags);
  87. /*
  88. * Mappings have to be page-aligned
  89. */
  90. offset = phys_addr & ~PAGE_MASK;
  91. phys_addr &= PAGE_MASK;
  92. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  93. /*
  94. * Ok, go for it..
  95. */
  96. area = get_vm_area(size, VM_IOREMAP | (flags << 20));
  97. if (!area)
  98. return NULL;
  99. area->phys_addr = phys_addr;
  100. addr = area->addr;
  101. if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
  102. phys_addr, pgprot)) {
  103. remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
  104. return NULL;
  105. }
  106. if (flags && ioremap_change_attr(phys_addr, size, flags) < 0) {
  107. area->flags &= 0xffffff;
  108. vunmap(addr);
  109. return NULL;
  110. }
  111. return (__force void __iomem *) (offset + (char *)addr);
  112. }
  113. EXPORT_SYMBOL(__ioremap);
  114. /**
  115. * ioremap_nocache - map bus memory into CPU space
  116. * @offset: bus address of the memory
  117. * @size: size of the resource to map
  118. *
  119. * ioremap_nocache performs a platform specific sequence of operations to
  120. * make bus memory CPU accessible via the readb/readw/readl/writeb/
  121. * writew/writel functions and the other mmio helpers. The returned
  122. * address is not guaranteed to be usable directly as a virtual
  123. * address.
  124. *
  125. * This version of ioremap ensures that the memory is marked uncachable
  126. * on the CPU as well as honouring existing caching rules from things like
  127. * the PCI bus. Note that there are other caches and buffers on many
  128. * busses. In particular driver authors should read up on PCI writes
  129. *
  130. * It's useful if some control registers are in such an area and
  131. * write combining or read caching is not desirable:
  132. *
  133. * Must be freed with iounmap.
  134. */
  135. void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
  136. {
  137. return __ioremap(phys_addr, size, _PAGE_PCD);
  138. }
  139. EXPORT_SYMBOL(ioremap_nocache);
  140. /**
  141. * iounmap - Free a IO remapping
  142. * @addr: virtual address from ioremap_*
  143. *
  144. * Caller must ensure there is only one unmapping for the same pointer.
  145. */
  146. void iounmap(volatile void __iomem *addr)
  147. {
  148. struct vm_struct *p, *o;
  149. if (addr <= high_memory)
  150. return;
  151. if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
  152. addr < phys_to_virt(ISA_END_ADDRESS))
  153. return;
  154. addr = (volatile void __iomem *)(PAGE_MASK & (unsigned long __force)addr);
  155. /* Use the vm area unlocked, assuming the caller
  156. ensures there isn't another iounmap for the same address
  157. in parallel. Reuse of the virtual address is prevented by
  158. leaving it in the global lists until we're done with it.
  159. cpa takes care of the direct mappings. */
  160. read_lock(&vmlist_lock);
  161. for (p = vmlist; p; p = p->next) {
  162. if (p->addr == addr)
  163. break;
  164. }
  165. read_unlock(&vmlist_lock);
  166. if (!p) {
  167. printk("iounmap: bad address %p\n", addr);
  168. dump_stack();
  169. return;
  170. }
  171. /* Reset the direct mapping. Can block */
  172. if (p->flags >> 20)
  173. ioremap_change_attr(p->phys_addr, p->size, 0);
  174. /* Finally remove it */
  175. o = remove_vm_area((void *)addr);
  176. BUG_ON(p != o || o == NULL);
  177. kfree(p);
  178. }
  179. EXPORT_SYMBOL(iounmap);