ioremap.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * arch/cris/mm/ioremap.c
  3. *
  4. * Re-map IO memory to kernel address space so that we can access it.
  5. * Needed for memory-mapped I/O devices mapped outside our normal DRAM
  6. * window (that is, all memory-mapped I/O devices).
  7. *
  8. * (C) Copyright 1995 1996 Linus Torvalds
  9. * CRIS-port by Axis Communications AB
  10. */
  11. #include <linux/vmalloc.h>
  12. #include <asm/io.h>
  13. #include <asm/pgalloc.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/tlbflush.h>
  16. extern inline void 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;
  20. address &= ~PMD_MASK;
  21. end = address + size;
  22. if (end > PMD_SIZE)
  23. end = PMD_SIZE;
  24. if (address >= end)
  25. BUG();
  26. do {
  27. if (!pte_none(*pte)) {
  28. printk("remap_area_pte: page already exists\n");
  29. BUG();
  30. }
  31. set_pte(pte, mk_pte_phys(phys_addr, __pgprot(_PAGE_PRESENT | __READABLE |
  32. __WRITEABLE | _PAGE_GLOBAL |
  33. _PAGE_KERNEL | flags)));
  34. address += PAGE_SIZE;
  35. phys_addr += PAGE_SIZE;
  36. pte++;
  37. } while (address && (address < end));
  38. }
  39. static inline int remap_area_pmd(pmd_t * pmd, unsigned long address, unsigned long size,
  40. unsigned long phys_addr, unsigned long flags)
  41. {
  42. unsigned long end;
  43. address &= ~PGDIR_MASK;
  44. end = address + size;
  45. if (end > PGDIR_SIZE)
  46. end = PGDIR_SIZE;
  47. phys_addr -= address;
  48. if (address >= end)
  49. BUG();
  50. do {
  51. pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
  52. if (!pte)
  53. return -ENOMEM;
  54. remap_area_pte(pte, address, end - address, address + phys_addr, flags);
  55. address = (address + PMD_SIZE) & PMD_MASK;
  56. pmd++;
  57. } while (address && (address < end));
  58. return 0;
  59. }
  60. static int remap_area_pages(unsigned long address, unsigned long phys_addr,
  61. unsigned long size, unsigned long flags)
  62. {
  63. int error;
  64. pgd_t * dir;
  65. unsigned long end = address + size;
  66. phys_addr -= address;
  67. dir = pgd_offset(&init_mm, address);
  68. flush_cache_all();
  69. if (address >= end)
  70. BUG();
  71. spin_lock(&init_mm.page_table_lock);
  72. do {
  73. pmd_t *pmd;
  74. pmd = pmd_alloc(&init_mm, dir, address);
  75. error = -ENOMEM;
  76. if (!pmd)
  77. break;
  78. if (remap_area_pmd(pmd, address, end - address,
  79. phys_addr + address, flags))
  80. break;
  81. error = 0;
  82. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  83. dir++;
  84. } while (address && (address < end));
  85. spin_unlock(&init_mm.page_table_lock);
  86. flush_tlb_all();
  87. return error;
  88. }
  89. /*
  90. * Generic mapping function (not visible outside):
  91. */
  92. /*
  93. * Remap an arbitrary physical address space into the kernel virtual
  94. * address space. Needed when the kernel wants to access high addresses
  95. * directly.
  96. *
  97. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  98. * have to convert them into an offset in a page-aligned mapping, but the
  99. * caller shouldn't need to know that small detail.
  100. */
  101. void * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  102. {
  103. void * addr;
  104. struct vm_struct * area;
  105. unsigned long offset, last_addr;
  106. /* Don't allow wraparound or zero size */
  107. last_addr = phys_addr + size - 1;
  108. if (!size || last_addr < phys_addr)
  109. return NULL;
  110. /*
  111. * Mappings have to be page-aligned
  112. */
  113. offset = phys_addr & ~PAGE_MASK;
  114. phys_addr &= PAGE_MASK;
  115. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  116. /*
  117. * Ok, go for it..
  118. */
  119. area = get_vm_area(size, VM_IOREMAP);
  120. if (!area)
  121. return NULL;
  122. addr = area->addr;
  123. if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
  124. vfree(addr);
  125. return NULL;
  126. }
  127. return (void *) (offset + (char *)addr);
  128. }
  129. void iounmap(void *addr)
  130. {
  131. if (addr > high_memory)
  132. return vfree((void *) (PAGE_MASK & (unsigned long) addr));
  133. }