ioremap.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * linux/arch/arm/mm/ioremap.c
  3. *
  4. * Re-map IO memory to kernel address space so that we can access it.
  5. *
  6. * (C) Copyright 1995 1996 Linus Torvalds
  7. *
  8. * Hacked for ARM by Phil Blundell <philb@gnu.org>
  9. * Hacked to allow all architectures to build, and various cleanups
  10. * by Russell King
  11. *
  12. * This allows a driver to remap an arbitrary region of bus memory into
  13. * virtual space. One should *only* use readl, writel, memcpy_toio and
  14. * so on with such remapped areas.
  15. *
  16. * Because the ARM only has a 32-bit address space we can't address the
  17. * whole of the (physical) PCI space at once. PCI huge-mode addressing
  18. * allows us to circumvent this restriction by splitting PCI space into
  19. * two 2GB chunks and mapping only one at a time into processor memory.
  20. * We use MMU protection domains to trap any attempt to access the bank
  21. * that is not currently mapped. (This isn't fully implemented yet.)
  22. */
  23. #include <linux/module.h>
  24. #include <linux/errno.h>
  25. #include <linux/mm.h>
  26. #include <linux/vmalloc.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/hardware.h>
  29. #include <asm/io.h>
  30. #include <asm/tlbflush.h>
  31. static inline void
  32. remap_area_pte(pte_t * pte, unsigned long address, unsigned long size,
  33. unsigned long phys_addr, pgprot_t pgprot)
  34. {
  35. unsigned long end;
  36. address &= ~PMD_MASK;
  37. end = address + size;
  38. if (end > PMD_SIZE)
  39. end = PMD_SIZE;
  40. BUG_ON(address >= end);
  41. do {
  42. if (!pte_none(*pte))
  43. goto bad;
  44. set_pte(pte, pfn_pte(phys_addr >> PAGE_SHIFT, pgprot));
  45. address += PAGE_SIZE;
  46. phys_addr += PAGE_SIZE;
  47. pte++;
  48. } while (address && (address < end));
  49. return;
  50. bad:
  51. printk("remap_area_pte: page already exists\n");
  52. BUG();
  53. }
  54. static inline int
  55. remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
  56. unsigned long phys_addr, unsigned long flags)
  57. {
  58. unsigned long end;
  59. pgprot_t pgprot;
  60. address &= ~PGDIR_MASK;
  61. end = address + size;
  62. if (end > PGDIR_SIZE)
  63. end = PGDIR_SIZE;
  64. phys_addr -= address;
  65. BUG_ON(address >= end);
  66. pgprot = __pgprot(L_PTE_PRESENT | L_PTE_YOUNG | L_PTE_DIRTY | L_PTE_WRITE | flags);
  67. do {
  68. pte_t * pte = pte_alloc_kernel(pmd, address);
  69. if (!pte)
  70. return -ENOMEM;
  71. remap_area_pte(pte, address, end - address, address + phys_addr, pgprot);
  72. address = (address + PMD_SIZE) & PMD_MASK;
  73. pmd++;
  74. } while (address && (address < end));
  75. return 0;
  76. }
  77. static int
  78. remap_area_pages(unsigned long start, unsigned long pfn,
  79. unsigned long size, unsigned long flags)
  80. {
  81. unsigned long address = start;
  82. unsigned long end = start + size;
  83. unsigned long phys_addr = __pfn_to_phys(pfn);
  84. int err = 0;
  85. pgd_t * dir;
  86. phys_addr -= address;
  87. dir = pgd_offset(&init_mm, address);
  88. BUG_ON(address >= end);
  89. do {
  90. pmd_t *pmd = pmd_alloc(&init_mm, dir, address);
  91. if (!pmd) {
  92. err = -ENOMEM;
  93. break;
  94. }
  95. if (remap_area_pmd(pmd, address, end - address,
  96. phys_addr + address, flags)) {
  97. err = -ENOMEM;
  98. break;
  99. }
  100. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  101. dir++;
  102. } while (address && (address < end));
  103. flush_cache_vmap(start, end);
  104. return err;
  105. }
  106. /*
  107. * Remap an arbitrary physical address space into the kernel virtual
  108. * address space. Needed when the kernel wants to access high addresses
  109. * directly.
  110. *
  111. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  112. * have to convert them into an offset in a page-aligned mapping, but the
  113. * caller shouldn't need to know that small detail.
  114. *
  115. * 'flags' are the extra L_PTE_ flags that you want to specify for this
  116. * mapping. See include/asm-arm/proc-armv/pgtable.h for more information.
  117. */
  118. void __iomem *
  119. __ioremap_pfn(unsigned long pfn, unsigned long offset, size_t size,
  120. unsigned long flags)
  121. {
  122. unsigned long addr;
  123. struct vm_struct * area;
  124. area = get_vm_area(size, VM_IOREMAP);
  125. if (!area)
  126. return NULL;
  127. addr = (unsigned long)area->addr;
  128. if (remap_area_pages(addr, pfn, size, flags)) {
  129. vfree(addr);
  130. return NULL;
  131. }
  132. return (void __iomem *) (offset + (char *)addr);
  133. }
  134. EXPORT_SYMBOL(__ioremap_pfn);
  135. void __iomem *
  136. __ioremap(unsigned long phys_addr, size_t size, unsigned long flags)
  137. {
  138. unsigned long last_addr;
  139. unsigned long offset = phys_addr & ~PAGE_MASK;
  140. unsigned long pfn = __phys_to_pfn(phys_addr);
  141. /*
  142. * Don't allow wraparound or zero size
  143. */
  144. last_addr = phys_addr + size - 1;
  145. if (!size || last_addr < phys_addr)
  146. return NULL;
  147. /*
  148. * Page align the mapping size
  149. */
  150. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  151. return __ioremap_pfn(pfn, offset, size, flags);
  152. }
  153. EXPORT_SYMBOL(__ioremap);
  154. void __iounmap(void __iomem *addr)
  155. {
  156. vfree((void *) (PAGE_MASK & (unsigned long) addr));
  157. }
  158. EXPORT_SYMBOL(__iounmap);
  159. #ifdef __io
  160. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  161. {
  162. return __io(port);
  163. }
  164. EXPORT_SYMBOL(ioport_map);
  165. void ioport_unmap(void __iomem *addr)
  166. {
  167. }
  168. EXPORT_SYMBOL(ioport_unmap);
  169. #endif
  170. #ifdef CONFIG_PCI
  171. #include <linux/pci.h>
  172. #include <linux/ioport.h>
  173. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  174. {
  175. unsigned long start = pci_resource_start(dev, bar);
  176. unsigned long len = pci_resource_len(dev, bar);
  177. unsigned long flags = pci_resource_flags(dev, bar);
  178. if (!len || !start)
  179. return NULL;
  180. if (maxlen && len > maxlen)
  181. len = maxlen;
  182. if (flags & IORESOURCE_IO)
  183. return ioport_map(start, len);
  184. if (flags & IORESOURCE_MEM) {
  185. if (flags & IORESOURCE_CACHEABLE)
  186. return ioremap(start, len);
  187. return ioremap_nocache(start, len);
  188. }
  189. return NULL;
  190. }
  191. EXPORT_SYMBOL(pci_iomap);
  192. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  193. {
  194. if ((unsigned long)addr >= VMALLOC_START &&
  195. (unsigned long)addr < VMALLOC_END)
  196. iounmap(addr);
  197. }
  198. EXPORT_SYMBOL(pci_iounmap);
  199. #endif