ioremap_64.c 5.2 KB

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