ioremap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. static int
  59. remap_area_pages(unsigned long address, unsigned long phys_addr,
  60. unsigned long size, unsigned long flags)
  61. {
  62. pgd_t *dir;
  63. int error = 0;
  64. unsigned long end = address + size;
  65. BUG_ON(address >= end);
  66. phys_addr -= address;
  67. dir = pgd_offset_k(address);
  68. flush_cache_all();
  69. do {
  70. pud_t *pud;
  71. pmd_t *pmd;
  72. error = -ENOMEM;
  73. pud = pud_alloc(&init_mm, dir, address);
  74. if (!pud)
  75. break;
  76. pmd = pmd_alloc(&init_mm, pud, address);
  77. if (!pmd)
  78. break;
  79. if (remap_area_pmd(pmd, address, end - address,
  80. phys_addr + address, flags))
  81. break;
  82. error = 0;
  83. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  84. dir++;
  85. } while (address && (address < end));
  86. flush_tlb_all();
  87. return error;
  88. }
  89. #ifdef CONFIG_DEBUG_IOREMAP
  90. static unsigned long last = 0;
  91. void gsc_bad_addr(unsigned long addr)
  92. {
  93. if (time_after(jiffies, last + HZ*10)) {
  94. printk("gsc_foo() called with bad address 0x%lx\n", addr);
  95. dump_stack();
  96. last = jiffies;
  97. }
  98. }
  99. EXPORT_SYMBOL(gsc_bad_addr);
  100. void __raw_bad_addr(const volatile void __iomem *addr)
  101. {
  102. if (time_after(jiffies, last + HZ*10)) {
  103. printk("__raw_foo() called with bad address 0x%p\n", addr);
  104. dump_stack();
  105. last = jiffies;
  106. }
  107. }
  108. EXPORT_SYMBOL(__raw_bad_addr);
  109. #endif
  110. /*
  111. * Generic mapping function (not visible outside):
  112. */
  113. /*
  114. * Remap an arbitrary physical address space into the kernel virtual
  115. * address space.
  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. void *addr;
  124. struct vm_struct *area;
  125. unsigned long offset, last_addr;
  126. #ifdef CONFIG_EISA
  127. unsigned long end = phys_addr + size - 1;
  128. /* Support EISA addresses */
  129. if ((phys_addr >= 0x00080000 && end < 0x000fffff)
  130. || (phys_addr >= 0x00500000 && end < 0x03bfffff)) {
  131. phys_addr |= 0xfc000000;
  132. #warning "FIXME: EISA regions do not work yet..."
  133. return NULL; /* XXX */
  134. }
  135. #endif
  136. /* Don't allow wraparound or zero size */
  137. last_addr = phys_addr + size - 1;
  138. if (!size || last_addr < phys_addr)
  139. return NULL;
  140. /*
  141. * Don't allow anybody to remap normal RAM that we're using..
  142. */
  143. if (phys_addr < virt_to_phys(high_memory)) {
  144. char *t_addr, *t_end;
  145. struct page *page;
  146. t_addr = __va(phys_addr);
  147. t_end = t_addr + (size - 1);
  148. for (page = virt_to_page(t_addr);
  149. page <= virt_to_page(t_end); page++) {
  150. if(!PageReserved(page))
  151. return NULL;
  152. }
  153. }
  154. /*
  155. * Mappings have to be page-aligned
  156. */
  157. offset = phys_addr & ~PAGE_MASK;
  158. phys_addr &= PAGE_MASK;
  159. size = PAGE_ALIGN(last_addr) - phys_addr;
  160. /*
  161. * Ok, go for it..
  162. */
  163. area = get_vm_area(size, VM_IOREMAP);
  164. if (!area)
  165. return NULL;
  166. addr = area->addr;
  167. if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
  168. vfree(addr);
  169. return NULL;
  170. }
  171. return (void __iomem *) (offset + (char *)addr);
  172. }
  173. void iounmap(void __iomem *addr)
  174. {
  175. if (addr > high_memory)
  176. return vfree((void *) (PAGE_MASK & (unsigned long __force) addr));
  177. }