homecache.c 13 KB

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