ioremap.c 4.8 KB

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