ioremap.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * (C) Copyright 1995 1996 Linus Torvalds
  7. * (C) Copyright 2001, 2002 Ralf Baechle
  8. */
  9. #include <linux/module.h>
  10. #include <asm/addrspace.h>
  11. #include <asm/byteorder.h>
  12. #include <linux/sched.h>
  13. #include <linux/vmalloc.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/io.h>
  16. #include <asm/tlbflush.h>
  17. static inline void remap_area_pte(pte_t * pte, unsigned long address,
  18. phys_t size, phys_t phys_addr, unsigned long flags)
  19. {
  20. phys_t end;
  21. unsigned long pfn;
  22. pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE
  23. | __WRITEABLE | flags);
  24. address &= ~PMD_MASK;
  25. end = address + size;
  26. if (end > PMD_SIZE)
  27. end = PMD_SIZE;
  28. if (address >= end)
  29. BUG();
  30. pfn = phys_addr >> PAGE_SHIFT;
  31. do {
  32. if (!pte_none(*pte)) {
  33. printk("remap_area_pte: page already exists\n");
  34. BUG();
  35. }
  36. set_pte(pte, pfn_pte(pfn, pgprot));
  37. address += PAGE_SIZE;
  38. pfn++;
  39. pte++;
  40. } while (address && (address < end));
  41. }
  42. static inline int remap_area_pmd(pmd_t * pmd, unsigned long address,
  43. phys_t size, phys_t phys_addr, unsigned long flags)
  44. {
  45. phys_t end;
  46. address &= ~PGDIR_MASK;
  47. end = address + size;
  48. if (end > PGDIR_SIZE)
  49. end = PGDIR_SIZE;
  50. phys_addr -= address;
  51. if (address >= end)
  52. BUG();
  53. do {
  54. pte_t * pte = pte_alloc_kernel(pmd, address);
  55. if (!pte)
  56. return -ENOMEM;
  57. remap_area_pte(pte, address, end - address, address + phys_addr, flags);
  58. address = (address + PMD_SIZE) & PMD_MASK;
  59. pmd++;
  60. } while (address && (address < end));
  61. return 0;
  62. }
  63. static int remap_area_pages(unsigned long address, phys_t phys_addr,
  64. phys_t size, unsigned long flags)
  65. {
  66. int error;
  67. pgd_t * dir;
  68. unsigned long end = address + size;
  69. phys_addr -= address;
  70. dir = pgd_offset(&init_mm, address);
  71. flush_cache_all();
  72. if (address >= end)
  73. BUG();
  74. do {
  75. pud_t *pud;
  76. pmd_t *pmd;
  77. error = -ENOMEM;
  78. pud = pud_alloc(&init_mm, dir, address);
  79. if (!pud)
  80. break;
  81. pmd = pmd_alloc(&init_mm, pud, address);
  82. if (!pmd)
  83. break;
  84. if (remap_area_pmd(pmd, address, end - address,
  85. phys_addr + address, flags))
  86. break;
  87. error = 0;
  88. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  89. dir++;
  90. } while (address && (address < end));
  91. flush_tlb_all();
  92. return error;
  93. }
  94. /*
  95. * Generic mapping function (not visible outside):
  96. */
  97. /*
  98. * Remap an arbitrary physical address space into the kernel virtual
  99. * address space. Needed when the kernel wants to access high addresses
  100. * directly.
  101. *
  102. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  103. * have to convert them into an offset in a page-aligned mapping, but the
  104. * caller shouldn't need to know that small detail.
  105. */
  106. #define IS_LOW512(addr) (!((phys_t)(addr) & (phys_t) ~0x1fffffffULL))
  107. void __iomem * __ioremap(phys_t phys_addr, phys_t size, unsigned long flags)
  108. {
  109. struct vm_struct * area;
  110. unsigned long offset;
  111. phys_t last_addr;
  112. void * addr;
  113. phys_addr = fixup_bigphys_addr(phys_addr, size);
  114. /* Don't allow wraparound or zero size */
  115. last_addr = phys_addr + size - 1;
  116. if (!size || last_addr < phys_addr)
  117. return NULL;
  118. /*
  119. * Map uncached objects in the low 512mb of address space using KSEG1,
  120. * otherwise map using page tables.
  121. */
  122. if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) &&
  123. flags == _CACHE_UNCACHED)
  124. return (void __iomem *) CKSEG1ADDR(phys_addr);
  125. /*
  126. * Don't allow anybody to remap normal RAM that we're using..
  127. */
  128. if (phys_addr < virt_to_phys(high_memory)) {
  129. char *t_addr, *t_end;
  130. struct page *page;
  131. t_addr = __va(phys_addr);
  132. t_end = t_addr + (size - 1);
  133. for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
  134. if(!PageReserved(page))
  135. return NULL;
  136. }
  137. /*
  138. * Mappings have to be page-aligned
  139. */
  140. offset = phys_addr & ~PAGE_MASK;
  141. phys_addr &= PAGE_MASK;
  142. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  143. /*
  144. * Ok, go for it..
  145. */
  146. area = get_vm_area(size, VM_IOREMAP);
  147. if (!area)
  148. return NULL;
  149. addr = area->addr;
  150. if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
  151. vunmap(addr);
  152. return NULL;
  153. }
  154. return (void __iomem *) (offset + (char *)addr);
  155. }
  156. #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == CKSEG1)
  157. void __iounmap(const volatile void __iomem *addr)
  158. {
  159. struct vm_struct *p;
  160. if (IS_KSEG1(addr))
  161. return;
  162. p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
  163. if (!p)
  164. printk(KERN_ERR "iounmap: bad address %p\n", addr);
  165. kfree(p);
  166. }
  167. EXPORT_SYMBOL(__ioremap);
  168. EXPORT_SYMBOL(__iounmap);