ioremap.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * arch/parisc/mm/ioremap.c
  3. *
  4. * (C) Copyright 1995 1996 Linus Torvalds
  5. * (C) Copyright 2001 Helge Deller <deller@gmx.de>
  6. * (C) Copyright 2005 Kyle McMartin <kyle@parisc-linux.org>
  7. */
  8. #include <linux/vmalloc.h>
  9. #include <linux/errno.h>
  10. #include <linux/module.h>
  11. #include <asm/io.h>
  12. #include <asm/pgalloc.h>
  13. #include <asm/tlbflush.h>
  14. #include <asm/cacheflush.h>
  15. static inline void
  16. remap_area_pte(pte_t *pte, unsigned long address, unsigned long size,
  17. unsigned long phys_addr, unsigned long flags)
  18. {
  19. unsigned long end, pfn;
  20. pgprot_t pgprot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY |
  21. _PAGE_ACCESSED | flags);
  22. address &= ~PMD_MASK;
  23. end = address + size;
  24. if (end > PMD_SIZE)
  25. end = PMD_SIZE;
  26. BUG_ON(address >= end);
  27. pfn = phys_addr >> PAGE_SHIFT;
  28. do {
  29. BUG_ON(!pte_none(*pte));
  30. set_pte(pte, pfn_pte(pfn, pgprot));
  31. address += PAGE_SIZE;
  32. pfn++;
  33. pte++;
  34. } while (address && (address < end));
  35. }
  36. static inline int
  37. remap_area_pmd(pmd_t *pmd, unsigned long address, unsigned long size,
  38. unsigned long phys_addr, unsigned long flags)
  39. {
  40. unsigned long end;
  41. address &= ~PGDIR_MASK;
  42. end = address + size;
  43. if (end > PGDIR_SIZE)
  44. end = PGDIR_SIZE;
  45. BUG_ON(address >= end);
  46. phys_addr -= address;
  47. do {
  48. pte_t *pte = pte_alloc_kernel(pmd, address);
  49. if (!pte)
  50. return -ENOMEM;
  51. remap_area_pte(pte, address, end - address,
  52. address + phys_addr, flags);
  53. address = (address + PMD_SIZE) & PMD_MASK;
  54. pmd++;
  55. } while (address && (address < end));
  56. return 0;
  57. }
  58. #if USE_HPPA_IOREMAP
  59. static int
  60. remap_area_pages(unsigned long address, unsigned long phys_addr,
  61. unsigned long size, unsigned long flags)
  62. {
  63. pgd_t *dir;
  64. int error = 0;
  65. unsigned long end = address + size;
  66. BUG_ON(address >= end);
  67. phys_addr -= address;
  68. dir = pgd_offset_k(address);
  69. flush_cache_all();
  70. do {
  71. pud_t *pud;
  72. pmd_t *pmd;
  73. error = -ENOMEM;
  74. pud = pud_alloc(&init_mm, dir, address);
  75. if (!pud)
  76. break;
  77. pmd = pmd_alloc(&init_mm, pud, address);
  78. if (!pmd)
  79. break;
  80. if (remap_area_pmd(pmd, address, end - address,
  81. phys_addr + address, flags))
  82. break;
  83. error = 0;
  84. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  85. dir++;
  86. } while (address && (address < end));
  87. flush_tlb_all();
  88. return error;
  89. }
  90. #endif /* USE_HPPA_IOREMAP */
  91. #ifdef CONFIG_DEBUG_IOREMAP
  92. static unsigned long last = 0;
  93. void gsc_bad_addr(unsigned long addr)
  94. {
  95. if (time_after(jiffies, last + HZ*10)) {
  96. printk("gsc_foo() called with bad address 0x%lx\n", addr);
  97. dump_stack();
  98. last = jiffies;
  99. }
  100. }
  101. EXPORT_SYMBOL(gsc_bad_addr);
  102. void __raw_bad_addr(const volatile void __iomem *addr)
  103. {
  104. if (time_after(jiffies, last + HZ*10)) {
  105. printk("__raw_foo() called with bad address 0x%p\n", addr);
  106. dump_stack();
  107. last = jiffies;
  108. }
  109. }
  110. EXPORT_SYMBOL(__raw_bad_addr);
  111. #endif
  112. /*
  113. * Generic mapping function (not visible outside):
  114. */
  115. /*
  116. * Remap an arbitrary physical address space into the kernel virtual
  117. * address space.
  118. *
  119. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  120. * have to convert them into an offset in a page-aligned mapping, but the
  121. * caller shouldn't need to know that small detail.
  122. */
  123. void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  124. {
  125. #if !(USE_HPPA_IOREMAP)
  126. unsigned long end = phys_addr + size - 1;
  127. /* Support EISA addresses */
  128. if ((phys_addr >= 0x00080000 && end < 0x000fffff)
  129. || (phys_addr >= 0x00500000 && end < 0x03bfffff)) {
  130. phys_addr |= 0xfc000000;
  131. }
  132. #ifdef CONFIG_DEBUG_IOREMAP
  133. return (void __iomem *)(phys_addr - (0x1UL << NYBBLE_SHIFT));
  134. #else
  135. return (void __iomem *)phys_addr;
  136. #endif
  137. #else
  138. void *addr;
  139. struct vm_struct *area;
  140. unsigned long offset, last_addr;
  141. /* Don't allow wraparound or zero size */
  142. last_addr = phys_addr + size - 1;
  143. if (!size || last_addr < phys_addr)
  144. return NULL;
  145. /*
  146. * Don't allow anybody to remap normal RAM that we're using..
  147. */
  148. if (phys_addr < virt_to_phys(high_memory)) {
  149. char *t_addr, *t_end;
  150. struct page *page;
  151. t_addr = __va(phys_addr);
  152. t_end = t_addr + (size - 1);
  153. for (page = virt_to_page(t_addr);
  154. page <= virt_to_page(t_end); page++) {
  155. if(!PageReserved(page))
  156. return NULL;
  157. }
  158. }
  159. /*
  160. * Mappings have to be page-aligned
  161. */
  162. offset = phys_addr & ~PAGE_MASK;
  163. phys_addr &= PAGE_MASK;
  164. size = PAGE_ALIGN(last_addr) - phys_addr;
  165. /*
  166. * Ok, go for it..
  167. */
  168. area = get_vm_area(size, VM_IOREMAP);
  169. if (!area)
  170. return NULL;
  171. addr = area->addr;
  172. if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
  173. vfree(addr);
  174. return NULL;
  175. }
  176. return (void __iomem *) (offset + (char *)addr);
  177. #endif
  178. }
  179. void iounmap(void __iomem *addr)
  180. {
  181. #if !(USE_HPPA_IOREMAP)
  182. return;
  183. #else
  184. if (addr > high_memory)
  185. return vfree((void *) (PAGE_MASK & (unsigned long __force) addr));
  186. #endif
  187. }