ioremap.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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(NULL, 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. spin_lock(&init_mm.page_table_lock);
  72. do {
  73. pmd_t *pmd;
  74. pmd = pmd_alloc(dir, address);
  75. error = -ENOMEM;
  76. if (!pmd)
  77. break;
  78. if (remap_area_pmd(pmd, address, end - address,
  79. phys_addr + address, flags))
  80. break;
  81. error = 0;
  82. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  83. dir++;
  84. } while (address && (address < end));
  85. spin_unlock(&init_mm.page_table_lock);
  86. flush_tlb_all();
  87. return error;
  88. }
  89. #endif /* USE_HPPA_IOREMAP */
  90. #ifdef CONFIG_DEBUG_IOREMAP
  91. static unsigned long last = 0;
  92. void gsc_bad_addr(unsigned long addr)
  93. {
  94. if (time_after(jiffies, last + HZ*10)) {
  95. printk("gsc_foo() called with bad address 0x%lx\n", addr);
  96. dump_stack();
  97. last = jiffies;
  98. }
  99. }
  100. EXPORT_SYMBOL(gsc_bad_addr);
  101. void __raw_bad_addr(const volatile void __iomem *addr)
  102. {
  103. if (time_after(jiffies, last + HZ*10)) {
  104. printk("__raw_foo() called with bad address 0x%p\n", addr);
  105. dump_stack();
  106. last = jiffies;
  107. }
  108. }
  109. EXPORT_SYMBOL(__raw_bad_addr);
  110. #endif
  111. /*
  112. * Generic mapping function (not visible outside):
  113. */
  114. /*
  115. * Remap an arbitrary physical address space into the kernel virtual
  116. * address space. Needed when the kernel wants to access high addresses
  117. * directly.
  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); page <= virt_to_page(t_end); page++)
  154. if(!PageReserved(page))
  155. return NULL;
  156. }
  157. /*
  158. * Mappings have to be page-aligned
  159. */
  160. offset = phys_addr & ~PAGE_MASK;
  161. phys_addr &= PAGE_MASK;
  162. size = PAGE_ALIGN(last_addr) - phys_addr;
  163. /*
  164. * Ok, go for it..
  165. */
  166. area = get_vm_area(size, VM_IOREMAP);
  167. if (!area)
  168. return NULL;
  169. addr = area->addr;
  170. if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
  171. vfree(addr);
  172. return NULL;
  173. }
  174. return (void __iomem *) (offset + (char *)addr);
  175. #endif
  176. }
  177. void iounmap(void __iomem *addr)
  178. {
  179. #if !(USE_HPPA_IOREMAP)
  180. return;
  181. #else
  182. if (addr > high_memory)
  183. return vfree((void *) (PAGE_MASK & (unsigned long __force) addr));
  184. #endif
  185. }