homecache.c 13 KB

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