highmem.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/highmem.h>
  15. #include <linux/module.h>
  16. #include <linux/pagemap.h>
  17. #include <asm/homecache.h>
  18. #define kmap_get_pte(vaddr) \
  19. pte_offset_kernel(pmd_offset(pud_offset(pgd_offset_k(vaddr), (vaddr)),\
  20. (vaddr)), (vaddr))
  21. void *kmap(struct page *page)
  22. {
  23. void *kva;
  24. unsigned long flags;
  25. pte_t *ptep;
  26. might_sleep();
  27. if (!PageHighMem(page))
  28. return page_address(page);
  29. kva = kmap_high(page);
  30. /*
  31. * Rewrite the PTE under the lock. This ensures that the page
  32. * is not currently migrating.
  33. */
  34. ptep = kmap_get_pte((unsigned long)kva);
  35. flags = homecache_kpte_lock();
  36. set_pte_at(&init_mm, kva, ptep, mk_pte(page, page_to_kpgprot(page)));
  37. homecache_kpte_unlock(flags);
  38. return kva;
  39. }
  40. EXPORT_SYMBOL(kmap);
  41. void kunmap(struct page *page)
  42. {
  43. if (in_interrupt())
  44. BUG();
  45. if (!PageHighMem(page))
  46. return;
  47. kunmap_high(page);
  48. }
  49. EXPORT_SYMBOL(kunmap);
  50. /*
  51. * Describe a single atomic mapping of a page on a given cpu at a
  52. * given address, and allow it to be linked into a list.
  53. */
  54. struct atomic_mapped_page {
  55. struct list_head list;
  56. struct page *page;
  57. int cpu;
  58. unsigned long va;
  59. };
  60. static spinlock_t amp_lock = __SPIN_LOCK_UNLOCKED(&amp_lock);
  61. static struct list_head amp_list = LIST_HEAD_INIT(amp_list);
  62. /*
  63. * Combining this structure with a per-cpu declaration lets us give
  64. * each cpu an atomic_mapped_page structure per type.
  65. */
  66. struct kmap_amps {
  67. struct atomic_mapped_page per_type[KM_TYPE_NR];
  68. };
  69. static DEFINE_PER_CPU(struct kmap_amps, amps);
  70. /*
  71. * Add a page and va, on this cpu, to the list of kmap_atomic pages,
  72. * and write the new pte to memory. Writing the new PTE under the
  73. * lock guarantees that it is either on the list before migration starts
  74. * (if we won the race), or set_pte() sets the migrating bit in the PTE
  75. * (if we lost the race). And doing it under the lock guarantees
  76. * that when kmap_atomic_fix_one_pte() comes along, it finds a valid
  77. * PTE in memory, iff the mapping is still on the amp_list.
  78. *
  79. * Finally, doing it under the lock lets us safely examine the page
  80. * to see if it is immutable or not, for the generic kmap_atomic() case.
  81. * If we examine it earlier we are exposed to a race where it looks
  82. * writable earlier, but becomes immutable before we write the PTE.
  83. */
  84. static void kmap_atomic_register(struct page *page, int type,
  85. unsigned long va, pte_t *ptep, pte_t pteval)
  86. {
  87. unsigned long flags;
  88. struct atomic_mapped_page *amp;
  89. flags = homecache_kpte_lock();
  90. spin_lock(&amp_lock);
  91. /* With interrupts disabled, now fill in the per-cpu info. */
  92. amp = this_cpu_ptr(&amps.per_type[type]);
  93. amp->page = page;
  94. amp->cpu = smp_processor_id();
  95. amp->va = va;
  96. /* For generic kmap_atomic(), choose the PTE writability now. */
  97. if (!pte_read(pteval))
  98. pteval = mk_pte(page, page_to_kpgprot(page));
  99. list_add(&amp->list, &amp_list);
  100. set_pte(ptep, pteval);
  101. spin_unlock(&amp_lock);
  102. homecache_kpte_unlock(flags);
  103. }
  104. /*
  105. * Remove a page and va, on this cpu, from the list of kmap_atomic pages.
  106. * Linear-time search, but we count on the lists being short.
  107. * We don't need to adjust the PTE under the lock (as opposed to the
  108. * kmap_atomic_register() case), since we're just unconditionally
  109. * zeroing the PTE after it's off the list.
  110. */
  111. static void kmap_atomic_unregister(struct page *page, unsigned long va)
  112. {
  113. unsigned long flags;
  114. struct atomic_mapped_page *amp;
  115. int cpu = smp_processor_id();
  116. spin_lock_irqsave(&amp_lock, flags);
  117. list_for_each_entry(amp, &amp_list, list) {
  118. if (amp->page == page && amp->cpu == cpu && amp->va == va)
  119. break;
  120. }
  121. BUG_ON(&amp->list == &amp_list);
  122. list_del(&amp->list);
  123. spin_unlock_irqrestore(&amp_lock, flags);
  124. }
  125. /* Helper routine for kmap_atomic_fix_kpte(), below. */
  126. static void kmap_atomic_fix_one_kpte(struct atomic_mapped_page *amp,
  127. int finished)
  128. {
  129. pte_t *ptep = kmap_get_pte(amp->va);
  130. if (!finished) {
  131. set_pte(ptep, pte_mkmigrate(*ptep));
  132. flush_remote(0, 0, NULL, amp->va, PAGE_SIZE, PAGE_SIZE,
  133. cpumask_of(amp->cpu), NULL, 0);
  134. } else {
  135. /*
  136. * Rewrite a default kernel PTE for this page.
  137. * We rely on the fact that set_pte() writes the
  138. * present+migrating bits last.
  139. */
  140. pte_t pte = mk_pte(amp->page, page_to_kpgprot(amp->page));
  141. set_pte(ptep, pte);
  142. }
  143. }
  144. /*
  145. * This routine is a helper function for homecache_fix_kpte(); see
  146. * its comments for more information on the "finished" argument here.
  147. *
  148. * Note that we hold the lock while doing the remote flushes, which
  149. * will stall any unrelated cpus trying to do kmap_atomic operations.
  150. * We could just update the PTEs under the lock, and save away copies
  151. * of the structs (or just the va+cpu), then flush them after we
  152. * release the lock, but it seems easier just to do it all under the lock.
  153. */
  154. void kmap_atomic_fix_kpte(struct page *page, int finished)
  155. {
  156. struct atomic_mapped_page *amp;
  157. unsigned long flags;
  158. spin_lock_irqsave(&amp_lock, flags);
  159. list_for_each_entry(amp, &amp_list, list) {
  160. if (amp->page == page)
  161. kmap_atomic_fix_one_kpte(amp, finished);
  162. }
  163. spin_unlock_irqrestore(&amp_lock, flags);
  164. }
  165. /*
  166. * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap
  167. * because the kmap code must perform a global TLB invalidation when
  168. * the kmap pool wraps.
  169. *
  170. * Note that they may be slower than on x86 (etc.) because unlike on
  171. * those platforms, we do have to take a global lock to map and unmap
  172. * pages on Tile (see above).
  173. *
  174. * When holding an atomic kmap is is not legal to sleep, so atomic
  175. * kmaps are appropriate for short, tight code paths only.
  176. */
  177. void *kmap_atomic_prot(struct page *page, pgprot_t prot)
  178. {
  179. unsigned long vaddr;
  180. int idx, type;
  181. pte_t *pte;
  182. preempt_disable();
  183. pagefault_disable();
  184. /* Avoid icache flushes by disallowing atomic executable mappings. */
  185. BUG_ON(pte_exec(prot));
  186. if (!PageHighMem(page))
  187. return page_address(page);
  188. type = kmap_atomic_idx_push();
  189. idx = type + KM_TYPE_NR*smp_processor_id();
  190. vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx);
  191. pte = kmap_get_pte(vaddr);
  192. BUG_ON(!pte_none(*pte));
  193. /* Register that this page is mapped atomically on this cpu. */
  194. kmap_atomic_register(page, type, vaddr, pte, mk_pte(page, prot));
  195. return (void *)vaddr;
  196. }
  197. EXPORT_SYMBOL(kmap_atomic_prot);
  198. void *kmap_atomic(struct page *page)
  199. {
  200. /* PAGE_NONE is a magic value that tells us to check immutability. */
  201. return kmap_atomic_prot(page, PAGE_NONE);
  202. }
  203. EXPORT_SYMBOL(kmap_atomic);
  204. void __kunmap_atomic(void *kvaddr)
  205. {
  206. unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK;
  207. if (vaddr >= __fix_to_virt(FIX_KMAP_END) &&
  208. vaddr <= __fix_to_virt(FIX_KMAP_BEGIN)) {
  209. pte_t *pte = kmap_get_pte(vaddr);
  210. pte_t pteval = *pte;
  211. int idx, type;
  212. type = kmap_atomic_idx();
  213. idx = type + KM_TYPE_NR*smp_processor_id();
  214. /*
  215. * Force other mappings to Oops if they try to access this pte
  216. * without first remapping it. Keeping stale mappings around
  217. * is a bad idea.
  218. */
  219. BUG_ON(!pte_present(pteval) && !pte_migrating(pteval));
  220. kmap_atomic_unregister(pte_page(pteval), vaddr);
  221. kpte_clear_flush(pte, vaddr);
  222. kmap_atomic_idx_pop();
  223. } else {
  224. /* Must be a lowmem page */
  225. BUG_ON(vaddr < PAGE_OFFSET);
  226. BUG_ON(vaddr >= (unsigned long)high_memory);
  227. }
  228. pagefault_enable();
  229. preempt_enable();
  230. }
  231. EXPORT_SYMBOL(__kunmap_atomic);
  232. /*
  233. * This API is supposed to allow us to map memory without a "struct page".
  234. * Currently we don't support this, though this may change in the future.
  235. */
  236. void *kmap_atomic_pfn(unsigned long pfn)
  237. {
  238. return kmap_atomic(pfn_to_page(pfn));
  239. }
  240. void *kmap_atomic_prot_pfn(unsigned long pfn, pgprot_t prot)
  241. {
  242. return kmap_atomic_prot(pfn_to_page(pfn), prot);
  243. }