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