homecache.c 12 KB

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