ioremap.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * arch/sh/mm/ioremap.c
  3. *
  4. * Re-map IO memory to kernel address space so that we can access it.
  5. * This is needed for high PCI addresses that aren't mapped in the
  6. * 640k-1MB IO memory area on PC's
  7. *
  8. * (C) Copyright 1995 1996 Linus Torvalds
  9. */
  10. #include <linux/vmalloc.h>
  11. #include <linux/mm.h>
  12. #include <asm/io.h>
  13. #include <asm/page.h>
  14. #include <asm/pgalloc.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/tlbflush.h>
  17. static inline void remap_area_pte(pte_t * pte, unsigned long address,
  18. unsigned long size, unsigned long phys_addr, unsigned long flags)
  19. {
  20. unsigned long end;
  21. unsigned long pfn;
  22. pgprot_t pgprot = __pgprot(_PAGE_PRESENT | _PAGE_RW |
  23. _PAGE_DIRTY | _PAGE_ACCESSED |
  24. _PAGE_HW_SHARED | _PAGE_FLAGS_HARD | flags);
  25. address &= ~PMD_MASK;
  26. end = address + size;
  27. if (end > PMD_SIZE)
  28. end = PMD_SIZE;
  29. if (address >= end)
  30. BUG();
  31. pfn = phys_addr >> PAGE_SHIFT;
  32. do {
  33. if (!pte_none(*pte)) {
  34. printk("remap_area_pte: page already exists\n");
  35. BUG();
  36. }
  37. set_pte(pte, pfn_pte(pfn, pgprot));
  38. address += PAGE_SIZE;
  39. pfn++;
  40. pte++;
  41. } while (address && (address < end));
  42. }
  43. static inline int remap_area_pmd(pmd_t * pmd, unsigned long address,
  44. unsigned long size, unsigned long phys_addr, unsigned long flags)
  45. {
  46. unsigned long end;
  47. address &= ~PGDIR_MASK;
  48. end = address + size;
  49. if (end > PGDIR_SIZE)
  50. end = PGDIR_SIZE;
  51. phys_addr -= address;
  52. if (address >= end)
  53. BUG();
  54. do {
  55. pte_t * pte = pte_alloc_kernel(&init_mm, pmd, address);
  56. if (!pte)
  57. return -ENOMEM;
  58. remap_area_pte(pte, address, end - address, address + phys_addr, flags);
  59. address = (address + PMD_SIZE) & PMD_MASK;
  60. pmd++;
  61. } while (address && (address < end));
  62. return 0;
  63. }
  64. int remap_area_pages(unsigned long address, unsigned long phys_addr,
  65. unsigned long size, unsigned long flags)
  66. {
  67. int error;
  68. pgd_t * dir;
  69. unsigned long end = address + size;
  70. phys_addr -= address;
  71. dir = pgd_offset_k(address);
  72. flush_cache_all();
  73. if (address >= end)
  74. BUG();
  75. spin_lock(&init_mm.page_table_lock);
  76. do {
  77. pmd_t *pmd;
  78. pmd = pmd_alloc(&init_mm, dir, address);
  79. error = -ENOMEM;
  80. if (!pmd)
  81. break;
  82. if (remap_area_pmd(pmd, address, end - address,
  83. phys_addr + address, flags))
  84. break;
  85. error = 0;
  86. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  87. dir++;
  88. } while (address && (address < end));
  89. spin_unlock(&init_mm.page_table_lock);
  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. void * p3_ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  106. {
  107. void * addr;
  108. struct vm_struct * area;
  109. unsigned long offset, last_addr;
  110. /* Don't allow wraparound or zero size */
  111. last_addr = phys_addr + size - 1;
  112. if (!size || last_addr < phys_addr)
  113. return NULL;
  114. /*
  115. * Don't remap the low PCI/ISA area, it's always mapped..
  116. */
  117. if (phys_addr >= 0xA0000 && last_addr < 0x100000)
  118. return phys_to_virt(phys_addr);
  119. /*
  120. * Don't allow anybody to remap normal RAM that we're using..
  121. */
  122. if (phys_addr < virt_to_phys(high_memory))
  123. return NULL;
  124. /*
  125. * Mappings have to be page-aligned
  126. */
  127. offset = phys_addr & ~PAGE_MASK;
  128. phys_addr &= PAGE_MASK;
  129. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  130. /*
  131. * Ok, go for it..
  132. */
  133. area = get_vm_area(size, VM_IOREMAP);
  134. if (!area)
  135. return NULL;
  136. area->phys_addr = phys_addr;
  137. addr = area->addr;
  138. if (remap_area_pages((unsigned long) addr, phys_addr, size, flags)) {
  139. vunmap(addr);
  140. return NULL;
  141. }
  142. return (void *) (offset + (char *)addr);
  143. }
  144. void p3_iounmap(void *addr)
  145. {
  146. if (addr > high_memory)
  147. vfree((void *)(PAGE_MASK & (unsigned long)addr));
  148. }