ioremap.c 4.6 KB

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