ioremap_64.c 5.6 KB

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