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/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(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. do {
  74. pud_t *pud;
  75. pmd_t *pmd;
  76. error = -ENOMEM;
  77. pud = pud_alloc(&init_mm, dir, address);
  78. if (!pud)
  79. break;
  80. pmd = pmd_alloc(&init_mm, pud, address);
  81. if (!pmd)
  82. break;
  83. if (remap_area_pmd(pmd, address, end - address,
  84. phys_addr + address, flags))
  85. break;
  86. error = 0;
  87. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  88. dir++;
  89. } while (address && (address < end));
  90. flush_tlb_all();
  91. return error;
  92. }
  93. /*
  94. * Generic mapping function (not visible outside):
  95. */
  96. /*
  97. * Remap an arbitrary physical address space into the kernel virtual
  98. * address space. Needed when the kernel wants to access high addresses
  99. * directly.
  100. *
  101. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  102. * have to convert them into an offset in a page-aligned mapping, but the
  103. * caller shouldn't need to know that small detail.
  104. */
  105. #define IS_LOW512(addr) (!((phys_t)(addr) & (phys_t) ~0x1fffffffULL))
  106. void __iomem * __ioremap(phys_t phys_addr, phys_t size, unsigned long flags)
  107. {
  108. struct vm_struct * area;
  109. unsigned long offset;
  110. phys_t last_addr;
  111. void * addr;
  112. phys_addr = fixup_bigphys_addr(phys_addr, size);
  113. /* Don't allow wraparound or zero size */
  114. last_addr = phys_addr + size - 1;
  115. if (!size || last_addr < phys_addr)
  116. return NULL;
  117. /*
  118. * Map uncached objects in the low 512mb of address space using KSEG1,
  119. * otherwise map using page tables.
  120. */
  121. if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) &&
  122. flags == _CACHE_UNCACHED)
  123. return (void __iomem *) CKSEG1ADDR(phys_addr);
  124. /*
  125. * Don't allow anybody to remap normal RAM that we're using..
  126. */
  127. if (phys_addr < virt_to_phys(high_memory)) {
  128. char *t_addr, *t_end;
  129. struct page *page;
  130. t_addr = __va(phys_addr);
  131. t_end = t_addr + (size - 1);
  132. for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
  133. if(!PageReserved(page))
  134. return NULL;
  135. }
  136. /*
  137. * Mappings have to be page-aligned
  138. */
  139. offset = phys_addr & ~PAGE_MASK;
  140. phys_addr &= PAGE_MASK;
  141. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  142. /*
  143. * Ok, go for it..
  144. */
  145. area = get_vm_area(size, VM_IOREMAP);
  146. if (!area)
  147. return NULL;
  148. addr = area->addr;
  149. if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
  150. vunmap(addr);
  151. return NULL;
  152. }
  153. return (void __iomem *) (offset + (char *)addr);
  154. }
  155. #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == CKSEG1)
  156. void __iounmap(volatile void __iomem *addr)
  157. {
  158. struct vm_struct *p;
  159. if (IS_KSEG1(addr))
  160. return;
  161. p = remove_vm_area((void *) (PAGE_MASK & (unsigned long __force) addr));
  162. if (!p)
  163. printk(KERN_ERR "iounmap: bad address %p\n", addr);
  164. kfree(p);
  165. }
  166. EXPORT_SYMBOL(__ioremap);
  167. EXPORT_SYMBOL(__iounmap);