ioremap.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:4k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 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/io.h>
  14. #include <asm/pgalloc.h>
  15. static inline void remap_area_pte(pte_t * pte, unsigned long address,
  16. phys_t size, phys_t phys_addr, unsigned long flags)
  17. {
  18. phys_t end;
  19. pgprot_t pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | __READABLE
  20.                            | __WRITEABLE | flags);
  21. address &= ~PMD_MASK;
  22. end = address + size;
  23. if (end > PMD_SIZE)
  24. end = PMD_SIZE;
  25. if (address >= end)
  26. BUG();
  27. do {
  28. if (!pte_none(*pte)) {
  29. printk("remap_area_pte: page already existsn");
  30. BUG();
  31. }
  32. set_pte(pte, mk_pte_phys(phys_addr, pgprot));
  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,
  39. phys_t size, phys_t phys_addr, unsigned long flags)
  40. {
  41. phys_t 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(&init_mm, 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. static int remap_area_pages(unsigned long address, phys_t phys_addr,
  60. phys_t size, unsigned long flags)
  61. {
  62. int error;
  63. pgd_t * dir;
  64. unsigned long end = address + size;
  65. phys_addr -= address;
  66. dir = pgd_offset(&init_mm, address);
  67. flush_cache_all();
  68. if (address >= end)
  69. BUG();
  70. spin_lock(&init_mm.page_table_lock);
  71. do {
  72. pmd_t *pmd;
  73. pmd = pmd_alloc(&init_mm, dir, address);
  74. error = -ENOMEM;
  75. if (!pmd)
  76. break;
  77. if (remap_area_pmd(pmd, address, end - address,
  78.  phys_addr + address, flags))
  79. break;
  80. error = 0;
  81. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  82. dir++;
  83. } while (address && (address < end));
  84. spin_unlock(&init_mm.page_table_lock);
  85. flush_tlb_all();
  86. return error;
  87. }
  88. /*
  89.  * Generic mapping function (not visible outside):
  90.  */
  91. /*
  92.  * Remap an arbitrary physical address space into the kernel virtual
  93.  * address space. Needed when the kernel wants to access high addresses
  94.  * directly.
  95.  *
  96.  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  97.  * have to convert them into an offset in a page-aligned mapping, but the
  98.  * caller shouldn't need to know that small detail.
  99.  */
  100. #define IS_LOW512(addr) (!((phys_t)(addr) & ~0x1fffffffUL))
  101. void * __ioremap(phys_t phys_addr, phys_t size, unsigned long flags)
  102. {
  103. struct vm_struct * area;
  104. unsigned long offset;
  105. phys_t last_addr;
  106. void * addr;
  107. /* Don't allow wraparound or zero size */
  108. last_addr = phys_addr + size - 1;
  109. if (!size || last_addr < phys_addr)
  110. return NULL;
  111. /*
  112.  * Map uncached objects in the low 512mb of address space using KSEG1,
  113.  * otherwise map using page tables.
  114.  */
  115. if (IS_LOW512(phys_addr) && IS_LOW512(last_addr) &&
  116.     flags == _CACHE_UNCACHED)
  117. return (void *) KSEG1ADDR(phys_addr);
  118. /*
  119.  * Don't allow anybody to remap normal RAM that we're using..
  120.  */
  121. if (phys_addr < virt_to_phys(high_memory)) {
  122. char *t_addr, *t_end;
  123. struct page *page;
  124. t_addr = __va(phys_addr);
  125. t_end = t_addr + (size - 1);
  126. for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
  127. if(!PageReserved(page))
  128. return NULL;
  129. }
  130. /*
  131.  * Mappings have to be page-aligned
  132.  */
  133. offset = phys_addr & ~PAGE_MASK;
  134. phys_addr &= PAGE_MASK;
  135. size = PAGE_ALIGN(last_addr) - phys_addr;
  136. /*
  137.  * Ok, go for it..
  138.  */
  139. area = get_vm_area(size, VM_IOREMAP);
  140. if (!area)
  141. return NULL;
  142. addr = area->addr;
  143. if (remap_area_pages(VMALLOC_VMADDR(addr), phys_addr, size, flags)) {
  144. vfree(addr);
  145. return NULL;
  146. }
  147. return (void *) (offset + (char *)addr);
  148. }
  149. #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == KSEG1)
  150. void iounmap(void *addr)
  151. {
  152. if (!IS_KSEG1(addr))
  153. return vfree((void *) (PAGE_MASK & (unsigned long) addr));
  154. }
  155. EXPORT_SYMBOL(__ioremap);
  156. EXPORT_SYMBOL(iounmap);