homecache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. * This code maintains the "home" for each page in the system.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/list.h>
  20. #include <linux/bootmem.h>
  21. #include <linux/rmap.h>
  22. #include <linux/pagemap.h>
  23. #include <linux/mutex.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/sysctl.h>
  26. #include <linux/pagevec.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/timex.h>
  29. #include <linux/cache.h>
  30. #include <linux/smp.h>
  31. #include <linux/module.h>
  32. #include <linux/hugetlb.h>
  33. #include <asm/page.h>
  34. #include <asm/sections.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/pgalloc.h>
  37. #include <asm/homecache.h>
  38. #include <arch/sim.h>
  39. #include "migrate.h"
  40. /*
  41. * The noallocl2 option suppresses all use of the L2 cache to cache
  42. * locally from a remote home.
  43. */
  44. static int __write_once noallocl2;
  45. static int __init set_noallocl2(char *str)
  46. {
  47. noallocl2 = 1;
  48. return 0;
  49. }
  50. early_param("noallocl2", set_noallocl2);
  51. /*
  52. * Update the irq_stat for cpus that we are going to interrupt
  53. * with TLB or cache flushes. Also handle removing dataplane cpus
  54. * from the TLB flush set, and setting dataplane_tlb_state instead.
  55. */
  56. static void hv_flush_update(const struct cpumask *cache_cpumask,
  57. struct cpumask *tlb_cpumask,
  58. unsigned long tlb_va, unsigned long tlb_length,
  59. HV_Remote_ASID *asids, int asidcount)
  60. {
  61. struct cpumask mask;
  62. int i, cpu;
  63. cpumask_clear(&mask);
  64. if (cache_cpumask)
  65. cpumask_or(&mask, &mask, cache_cpumask);
  66. if (tlb_cpumask && tlb_length) {
  67. cpumask_or(&mask, &mask, tlb_cpumask);
  68. }
  69. for (i = 0; i < asidcount; ++i)
  70. cpumask_set_cpu(asids[i].y * smp_width + asids[i].x, &mask);
  71. /*
  72. * Don't bother to update atomically; losing a count
  73. * here is not that critical.
  74. */
  75. for_each_cpu(cpu, &mask)
  76. ++per_cpu(irq_stat, cpu).irq_hv_flush_count;
  77. }
  78. /*
  79. * This wrapper function around hv_flush_remote() does several things:
  80. *
  81. * - Provides a return value error-checking panic path, since
  82. * there's never any good reason for hv_flush_remote() to fail.
  83. * - Accepts a 32-bit PFN rather than a 64-bit PA, which generally
  84. * is the type that Linux wants to pass around anyway.
  85. * - Canonicalizes that lengths of zero make cpumasks NULL.
  86. * - Handles deferring TLB flushes for dataplane tiles.
  87. * - Tracks remote interrupts in the per-cpu irq_cpustat_t.
  88. *
  89. * Note that we have to wait until the cache flush completes before
  90. * updating the per-cpu last_cache_flush word, since otherwise another
  91. * concurrent flush can race, conclude the flush has already
  92. * completed, and start to use the page while it's still dirty
  93. * remotely (running concurrently with the actual evict, presumably).
  94. */
  95. void flush_remote(unsigned long cache_pfn, unsigned long cache_control,
  96. const struct cpumask *cache_cpumask_orig,
  97. HV_VirtAddr tlb_va, unsigned long tlb_length,
  98. unsigned long tlb_pgsize,
  99. const struct cpumask *tlb_cpumask_orig,
  100. HV_Remote_ASID *asids, int asidcount)
  101. {
  102. int rc;
  103. struct cpumask cache_cpumask_copy, tlb_cpumask_copy;
  104. struct cpumask *cache_cpumask, *tlb_cpumask;
  105. HV_PhysAddr cache_pa;
  106. char cache_buf[NR_CPUS*5], tlb_buf[NR_CPUS*5];
  107. mb(); /* provided just to simplify "magic hypervisor" mode */
  108. /*
  109. * Canonicalize and copy the cpumasks.
  110. */
  111. if (cache_cpumask_orig && cache_control) {
  112. cpumask_copy(&cache_cpumask_copy, cache_cpumask_orig);
  113. cache_cpumask = &cache_cpumask_copy;
  114. } else {
  115. cpumask_clear(&cache_cpumask_copy);
  116. cache_cpumask = NULL;
  117. }
  118. if (cache_cpumask == NULL)
  119. cache_control = 0;
  120. if (tlb_cpumask_orig && tlb_length) {
  121. cpumask_copy(&tlb_cpumask_copy, tlb_cpumask_orig);
  122. tlb_cpumask = &tlb_cpumask_copy;
  123. } else {
  124. cpumask_clear(&tlb_cpumask_copy);
  125. tlb_cpumask = NULL;
  126. }
  127. hv_flush_update(cache_cpumask, tlb_cpumask, tlb_va, tlb_length,
  128. asids, asidcount);
  129. cache_pa = (HV_PhysAddr)cache_pfn << PAGE_SHIFT;
  130. rc = hv_flush_remote(cache_pa, cache_control,
  131. cpumask_bits(cache_cpumask),
  132. tlb_va, tlb_length, tlb_pgsize,
  133. cpumask_bits(tlb_cpumask),
  134. asids, asidcount);
  135. if (rc == 0)
  136. return;
  137. cpumask_scnprintf(cache_buf, sizeof(cache_buf), &cache_cpumask_copy);
  138. cpumask_scnprintf(tlb_buf, sizeof(tlb_buf), &tlb_cpumask_copy);
  139. pr_err("hv_flush_remote(%#llx, %#lx, %p [%s],"
  140. " %#lx, %#lx, %#lx, %p [%s], %p, %d) = %d\n",
  141. cache_pa, cache_control, cache_cpumask, cache_buf,
  142. (unsigned long)tlb_va, tlb_length, tlb_pgsize,
  143. tlb_cpumask, tlb_buf,
  144. asids, asidcount, rc);
  145. panic("Unsafe to continue.");
  146. }
  147. static void homecache_finv_page_va(void* va, int home)
  148. {
  149. int cpu = get_cpu();
  150. if (home == cpu) {
  151. finv_buffer_local(va, PAGE_SIZE);
  152. } else if (home == PAGE_HOME_HASH) {
  153. finv_buffer_remote(va, PAGE_SIZE, 1);
  154. } else {
  155. BUG_ON(home < 0 || home >= NR_CPUS);
  156. finv_buffer_remote(va, PAGE_SIZE, 0);
  157. }
  158. put_cpu();
  159. }
  160. void homecache_finv_map_page(struct page *page, int home)
  161. {
  162. unsigned long flags;
  163. unsigned long va;
  164. pte_t *ptep;
  165. pte_t pte;
  166. if (home == PAGE_HOME_UNCACHED)
  167. return;
  168. local_irq_save(flags);
  169. #ifdef CONFIG_HIGHMEM
  170. va = __fix_to_virt(FIX_KMAP_BEGIN + kmap_atomic_idx_push() +
  171. (KM_TYPE_NR * smp_processor_id()));
  172. #else
  173. va = __fix_to_virt(FIX_HOMECACHE_BEGIN + smp_processor_id());
  174. #endif
  175. ptep = virt_to_kpte(va);
  176. pte = pfn_pte(page_to_pfn(page), PAGE_KERNEL);
  177. __set_pte(ptep, pte_set_home(pte, home));
  178. homecache_finv_page_va((void *)va, home);
  179. __pte_clear(ptep);
  180. hv_flush_page(va, PAGE_SIZE);
  181. #ifdef CONFIG_HIGHMEM
  182. kmap_atomic_idx_pop();
  183. #endif
  184. local_irq_restore(flags);
  185. }
  186. static void homecache_finv_page_home(struct page *page, int home)
  187. {
  188. if (!PageHighMem(page) && home == page_home(page))
  189. homecache_finv_page_va(page_address(page), home);
  190. else
  191. homecache_finv_map_page(page, home);
  192. }
  193. static inline bool incoherent_home(int home)
  194. {
  195. return home == PAGE_HOME_IMMUTABLE || home == PAGE_HOME_INCOHERENT;
  196. }
  197. static void homecache_finv_page_internal(struct page *page, int force_map)
  198. {
  199. int home = page_home(page);
  200. if (home == PAGE_HOME_UNCACHED)
  201. return;
  202. if (incoherent_home(home)) {
  203. int cpu;
  204. for_each_cpu(cpu, &cpu_cacheable_map)
  205. homecache_finv_map_page(page, cpu);
  206. } else if (force_map) {
  207. /* Force if, e.g., the normal mapping is migrating. */
  208. homecache_finv_map_page(page, home);
  209. } else {
  210. homecache_finv_page_home(page, home);
  211. }
  212. sim_validate_lines_evicted(PFN_PHYS(page_to_pfn(page)), PAGE_SIZE);
  213. }
  214. void homecache_finv_page(struct page *page)
  215. {
  216. homecache_finv_page_internal(page, 0);
  217. }
  218. void homecache_evict(const struct cpumask *mask)
  219. {
  220. flush_remote(0, HV_FLUSH_EVICT_L2, mask, 0, 0, 0, NULL, NULL, 0);
  221. }
  222. /* Report the home corresponding to a given PTE. */
  223. static int pte_to_home(pte_t pte)
  224. {
  225. if (hv_pte_get_nc(pte))
  226. return PAGE_HOME_IMMUTABLE;
  227. switch (hv_pte_get_mode(pte)) {
  228. case HV_PTE_MODE_CACHE_TILE_L3:
  229. return get_remote_cache_cpu(pte);
  230. case HV_PTE_MODE_CACHE_NO_L3:
  231. return PAGE_HOME_INCOHERENT;
  232. case HV_PTE_MODE_UNCACHED:
  233. return PAGE_HOME_UNCACHED;
  234. case HV_PTE_MODE_CACHE_HASH_L3:
  235. return PAGE_HOME_HASH;
  236. }
  237. panic("Bad PTE %#llx\n", pte.val);
  238. }
  239. /* Update the home of a PTE if necessary (can also be used for a pgprot_t). */
  240. pte_t pte_set_home(pte_t pte, int home)
  241. {
  242. /* Check for non-linear file mapping "PTEs" and pass them through. */
  243. if (pte_file(pte))
  244. return pte;
  245. #if CHIP_HAS_MMIO()
  246. /* Check for MMIO mappings and pass them through. */
  247. if (hv_pte_get_mode(pte) == HV_PTE_MODE_MMIO)
  248. return pte;
  249. #endif
  250. /*
  251. * Only immutable pages get NC mappings. If we have a
  252. * non-coherent PTE, but the underlying page is not
  253. * immutable, it's likely the result of a forced
  254. * caching setting running up against ptrace setting
  255. * the page to be writable underneath. In this case,
  256. * just keep the PTE coherent.
  257. */
  258. if (hv_pte_get_nc(pte) && home != PAGE_HOME_IMMUTABLE) {
  259. pte = hv_pte_clear_nc(pte);
  260. pr_err("non-immutable page incoherently referenced: %#llx\n",
  261. pte.val);
  262. }
  263. switch (home) {
  264. case PAGE_HOME_UNCACHED:
  265. pte = hv_pte_set_mode(pte, HV_PTE_MODE_UNCACHED);
  266. break;
  267. case PAGE_HOME_INCOHERENT:
  268. pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_NO_L3);
  269. break;
  270. case PAGE_HOME_IMMUTABLE:
  271. /*
  272. * We could home this page anywhere, since it's immutable,
  273. * but by default just home it to follow "hash_default".
  274. */
  275. BUG_ON(hv_pte_get_writable(pte));
  276. if (pte_get_forcecache(pte)) {
  277. /* Upgrade "force any cpu" to "No L3" for immutable. */
  278. if (hv_pte_get_mode(pte) == HV_PTE_MODE_CACHE_TILE_L3
  279. && pte_get_anyhome(pte)) {
  280. pte = hv_pte_set_mode(pte,
  281. HV_PTE_MODE_CACHE_NO_L3);
  282. }
  283. } else
  284. if (hash_default)
  285. pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_HASH_L3);
  286. else
  287. pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_NO_L3);
  288. pte = hv_pte_set_nc(pte);
  289. break;
  290. case PAGE_HOME_HASH:
  291. pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_HASH_L3);
  292. break;
  293. default:
  294. BUG_ON(home < 0 || home >= NR_CPUS ||
  295. !cpu_is_valid_lotar(home));
  296. pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_TILE_L3);
  297. pte = set_remote_cache_cpu(pte, home);
  298. break;
  299. }
  300. if (noallocl2)
  301. pte = hv_pte_set_no_alloc_l2(pte);
  302. /* Simplify "no local and no l3" to "uncached" */
  303. if (hv_pte_get_no_alloc_l2(pte) && hv_pte_get_no_alloc_l1(pte) &&
  304. hv_pte_get_mode(pte) == HV_PTE_MODE_CACHE_NO_L3) {
  305. pte = hv_pte_set_mode(pte, HV_PTE_MODE_UNCACHED);
  306. }
  307. /* Checking this case here gives a better panic than from the hv. */
  308. BUG_ON(hv_pte_get_mode(pte) == 0);
  309. return pte;
  310. }
  311. EXPORT_SYMBOL(pte_set_home);
  312. /*
  313. * The routines in this section are the "static" versions of the normal
  314. * dynamic homecaching routines; they just set the home cache
  315. * of a kernel page once, and require a full-chip cache/TLB flush,
  316. * so they're not suitable for anything but infrequent use.
  317. */
  318. int page_home(struct page *page)
  319. {
  320. if (PageHighMem(page)) {
  321. return PAGE_HOME_HASH;
  322. } else {
  323. unsigned long kva = (unsigned long)page_address(page);
  324. return pte_to_home(*virt_to_kpte(kva));
  325. }
  326. }
  327. EXPORT_SYMBOL(page_home);
  328. void homecache_change_page_home(struct page *page, int order, int home)
  329. {
  330. int i, pages = (1 << order);
  331. unsigned long kva;
  332. BUG_ON(PageHighMem(page));
  333. BUG_ON(page_count(page) > 1);
  334. BUG_ON(page_mapcount(page) != 0);
  335. kva = (unsigned long) page_address(page);
  336. flush_remote(0, HV_FLUSH_EVICT_L2, &cpu_cacheable_map,
  337. kva, pages * PAGE_SIZE, PAGE_SIZE, cpu_online_mask,
  338. NULL, 0);
  339. for (i = 0; i < pages; ++i, kva += PAGE_SIZE) {
  340. pte_t *ptep = virt_to_kpte(kva);
  341. pte_t pteval = *ptep;
  342. BUG_ON(!pte_present(pteval) || pte_huge(pteval));
  343. __set_pte(ptep, pte_set_home(pteval, home));
  344. }
  345. }
  346. EXPORT_SYMBOL(homecache_change_page_home);
  347. struct page *homecache_alloc_pages(gfp_t gfp_mask,
  348. unsigned int order, int home)
  349. {
  350. struct page *page;
  351. BUG_ON(gfp_mask & __GFP_HIGHMEM); /* must be lowmem */
  352. page = alloc_pages(gfp_mask, order);
  353. if (page)
  354. homecache_change_page_home(page, order, home);
  355. return page;
  356. }
  357. EXPORT_SYMBOL(homecache_alloc_pages);
  358. struct page *homecache_alloc_pages_node(int nid, gfp_t gfp_mask,
  359. unsigned int order, int home)
  360. {
  361. struct page *page;
  362. BUG_ON(gfp_mask & __GFP_HIGHMEM); /* must be lowmem */
  363. page = alloc_pages_node(nid, gfp_mask, order);
  364. if (page)
  365. homecache_change_page_home(page, order, home);
  366. return page;
  367. }
  368. void __homecache_free_pages(struct page *page, unsigned int order)
  369. {
  370. if (put_page_testzero(page)) {
  371. homecache_change_page_home(page, order, PAGE_HOME_HASH);
  372. if (order == 0) {
  373. free_hot_cold_page(page, 0);
  374. } else {
  375. init_page_count(page);
  376. __free_pages(page, order);
  377. }
  378. }
  379. }
  380. EXPORT_SYMBOL(__homecache_free_pages);
  381. void homecache_free_pages(unsigned long addr, unsigned int order)
  382. {
  383. if (addr != 0) {
  384. VM_BUG_ON(!virt_addr_valid((void *)addr));
  385. __homecache_free_pages(virt_to_page((void *)addr), order);
  386. }
  387. }
  388. EXPORT_SYMBOL(homecache_free_pages);