kmap.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * kmap/page table map and unmap support routines
  3. *
  4. * Copyright 1999,2000 Hewlett-Packard Company
  5. * Copyright 2000 John Marvin <jsm at hp.com>
  6. * Copyright 2000 Grant Grundler <grundler at parisc-linux.org>
  7. * Copyright 2000 Philipp Rumpf <prumpf@tux.org>
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. /*
  25. ** Stolen mostly from arch/parisc/kernel/pci-dma.c
  26. */
  27. #include <linux/types.h>
  28. #include <linux/mm.h>
  29. #include <linux/string.h>
  30. #include <linux/pci.h>
  31. #include <linux/slab.h>
  32. #include <linux/vmalloc.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/pgalloc.h>
  35. #include <asm/io.h>
  36. #include <asm/page.h> /* get_order */
  37. #undef flush_cache_all
  38. #define flush_cache_all flush_all_caches
  39. typedef void (*pte_iterator_t) (pte_t * pte, unsigned long arg);
  40. #if 0
  41. /* XXX This routine could be used with iterate_page() to replace
  42. * unmap_uncached_page() and save a little code space but I didn't
  43. * do that since I'm not certain whether this is the right path. -PB
  44. */
  45. static void unmap_cached_pte(pte_t * pte, unsigned long addr, unsigned long arg)
  46. {
  47. pte_t page = *pte;
  48. pte_clear(&init_mm, addr, pte);
  49. if (!pte_none(page)) {
  50. if (pte_present(page)) {
  51. unsigned long map_nr = pte_pagenr(page);
  52. if (map_nr < max_mapnr)
  53. __free_page(mem_map + map_nr);
  54. } else {
  55. printk(KERN_CRIT
  56. "Whee.. Swapped out page in kernel page table\n");
  57. }
  58. }
  59. }
  60. #endif
  61. /* These two routines should probably check a few things... */
  62. static void set_uncached(pte_t * pte, unsigned long arg)
  63. {
  64. pte_val(*pte) |= _PAGE_NO_CACHE;
  65. }
  66. static void set_cached(pte_t * pte, unsigned long arg)
  67. {
  68. pte_val(*pte) &= ~_PAGE_NO_CACHE;
  69. }
  70. static inline void iterate_pte(pmd_t * pmd, unsigned long address,
  71. unsigned long size, pte_iterator_t op,
  72. unsigned long arg)
  73. {
  74. pte_t *pte;
  75. unsigned long end;
  76. if (pmd_none(*pmd))
  77. return;
  78. if (pmd_bad(*pmd)) {
  79. pmd_ERROR(*pmd);
  80. pmd_clear(pmd);
  81. return;
  82. }
  83. pte = pte_offset(pmd, address);
  84. address &= ~PMD_MASK;
  85. end = address + size;
  86. if (end > PMD_SIZE)
  87. end = PMD_SIZE;
  88. do {
  89. op(pte, arg);
  90. address += PAGE_SIZE;
  91. pte++;
  92. } while (address < end);
  93. }
  94. static inline void iterate_pmd(pgd_t * dir, unsigned long address,
  95. unsigned long size, pte_iterator_t op,
  96. unsigned long arg)
  97. {
  98. pmd_t *pmd;
  99. unsigned long end;
  100. if (pgd_none(*dir))
  101. return;
  102. if (pgd_bad(*dir)) {
  103. pgd_ERROR(*dir);
  104. pgd_clear(dir);
  105. return;
  106. }
  107. pmd = pmd_offset(dir, address);
  108. address &= ~PGDIR_MASK;
  109. end = address + size;
  110. if (end > PGDIR_SIZE)
  111. end = PGDIR_SIZE;
  112. do {
  113. iterate_pte(pmd, address, end - address, op, arg);
  114. address = (address + PMD_SIZE) & PMD_MASK;
  115. pmd++;
  116. } while (address < end);
  117. }
  118. static void iterate_pages(unsigned long address, unsigned long size,
  119. pte_iterator_t op, unsigned long arg)
  120. {
  121. pgd_t *dir;
  122. unsigned long end = address + size;
  123. dir = pgd_offset_k(address);
  124. flush_cache_all();
  125. do {
  126. iterate_pmd(dir, address, end - address, op, arg);
  127. address = (address + PGDIR_SIZE) & PGDIR_MASK;
  128. dir++;
  129. } while (address && (address < end));
  130. flush_tlb_all();
  131. }
  132. void
  133. kernel_set_cachemode(unsigned long vaddr, unsigned long size, int what)
  134. {
  135. switch (what) {
  136. case IOMAP_FULL_CACHING:
  137. iterate_pages(vaddr, size, set_cached, 0);
  138. flush_tlb_range(NULL, vaddr, size);
  139. break;
  140. case IOMAP_NOCACHE_SER:
  141. iterate_pages(vaddr, size, set_uncached, 0);
  142. flush_tlb_range(NULL, vaddr, size);
  143. break;
  144. default:
  145. printk(KERN_CRIT
  146. "kernel_set_cachemode mode %d not understood\n",
  147. what);
  148. break;
  149. }
  150. }