cache.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /* $Id: cache.c,v 1.4 2000/01/25 00:11:38 prumpf Exp $
  2. *
  3. * This file is subject to the terms and conditions of the GNU General Public
  4. * License. See the file "COPYING" in the main directory of this archive
  5. * for more details.
  6. *
  7. * Copyright (C) 1999-2006 Helge Deller <deller@gmx.de> (07-13-1999)
  8. * Copyright (C) 1999 SuSE GmbH Nuernberg
  9. * Copyright (C) 2000 Philipp Rumpf (prumpf@tux.org)
  10. *
  11. * Cache and TLB management
  12. *
  13. */
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/module.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/sched.h>
  21. #include <asm/pdc.h>
  22. #include <asm/cache.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/tlbflush.h>
  25. #include <asm/system.h>
  26. #include <asm/page.h>
  27. #include <asm/pgalloc.h>
  28. #include <asm/processor.h>
  29. #include <asm/sections.h>
  30. int split_tlb __read_mostly;
  31. int dcache_stride __read_mostly;
  32. int icache_stride __read_mostly;
  33. EXPORT_SYMBOL(dcache_stride);
  34. /* On some machines (e.g. ones with the Merced bus), there can be
  35. * only a single PxTLB broadcast at a time; this must be guaranteed
  36. * by software. We put a spinlock around all TLB flushes to
  37. * ensure this.
  38. */
  39. DEFINE_SPINLOCK(pa_tlb_lock);
  40. struct pdc_cache_info cache_info __read_mostly;
  41. #ifndef CONFIG_PA20
  42. static struct pdc_btlb_info btlb_info __read_mostly;
  43. #endif
  44. #ifdef CONFIG_SMP
  45. void
  46. flush_data_cache(void)
  47. {
  48. on_each_cpu(flush_data_cache_local, NULL, 1);
  49. }
  50. void
  51. flush_instruction_cache(void)
  52. {
  53. on_each_cpu(flush_instruction_cache_local, NULL, 1);
  54. }
  55. #endif
  56. void
  57. flush_cache_all_local(void)
  58. {
  59. flush_instruction_cache_local(NULL);
  60. flush_data_cache_local(NULL);
  61. }
  62. EXPORT_SYMBOL(flush_cache_all_local);
  63. void
  64. update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  65. {
  66. struct page *page = pte_page(pte);
  67. if (pfn_valid(page_to_pfn(page)) && page_mapping(page) &&
  68. test_bit(PG_dcache_dirty, &page->flags)) {
  69. flush_kernel_dcache_page(page);
  70. clear_bit(PG_dcache_dirty, &page->flags);
  71. } else if (parisc_requires_coherency())
  72. flush_kernel_dcache_page(page);
  73. }
  74. void
  75. show_cache_info(struct seq_file *m)
  76. {
  77. char buf[32];
  78. seq_printf(m, "I-cache\t\t: %ld KB\n",
  79. cache_info.ic_size/1024 );
  80. if (cache_info.dc_loop != 1)
  81. snprintf(buf, 32, "%lu-way associative", cache_info.dc_loop);
  82. seq_printf(m, "D-cache\t\t: %ld KB (%s%s, %s)\n",
  83. cache_info.dc_size/1024,
  84. (cache_info.dc_conf.cc_wt ? "WT":"WB"),
  85. (cache_info.dc_conf.cc_sh ? ", shared I/D":""),
  86. ((cache_info.dc_loop == 1) ? "direct mapped" : buf));
  87. seq_printf(m, "ITLB entries\t: %ld\n" "DTLB entries\t: %ld%s\n",
  88. cache_info.it_size,
  89. cache_info.dt_size,
  90. cache_info.dt_conf.tc_sh ? " - shared with ITLB":""
  91. );
  92. #ifndef CONFIG_PA20
  93. /* BTLB - Block TLB */
  94. if (btlb_info.max_size==0) {
  95. seq_printf(m, "BTLB\t\t: not supported\n" );
  96. } else {
  97. seq_printf(m,
  98. "BTLB fixed\t: max. %d pages, pagesize=%d (%dMB)\n"
  99. "BTLB fix-entr.\t: %d instruction, %d data (%d combined)\n"
  100. "BTLB var-entr.\t: %d instruction, %d data (%d combined)\n",
  101. btlb_info.max_size, (int)4096,
  102. btlb_info.max_size>>8,
  103. btlb_info.fixed_range_info.num_i,
  104. btlb_info.fixed_range_info.num_d,
  105. btlb_info.fixed_range_info.num_comb,
  106. btlb_info.variable_range_info.num_i,
  107. btlb_info.variable_range_info.num_d,
  108. btlb_info.variable_range_info.num_comb
  109. );
  110. }
  111. #endif
  112. }
  113. void __init
  114. parisc_cache_init(void)
  115. {
  116. if (pdc_cache_info(&cache_info) < 0)
  117. panic("parisc_cache_init: pdc_cache_info failed");
  118. #if 0
  119. printk("ic_size %lx dc_size %lx it_size %lx\n",
  120. cache_info.ic_size,
  121. cache_info.dc_size,
  122. cache_info.it_size);
  123. printk("DC base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx\n",
  124. cache_info.dc_base,
  125. cache_info.dc_stride,
  126. cache_info.dc_count,
  127. cache_info.dc_loop);
  128. printk("dc_conf = 0x%lx alias %d blk %d line %d shift %d\n",
  129. *(unsigned long *) (&cache_info.dc_conf),
  130. cache_info.dc_conf.cc_alias,
  131. cache_info.dc_conf.cc_block,
  132. cache_info.dc_conf.cc_line,
  133. cache_info.dc_conf.cc_shift);
  134. printk(" wt %d sh %d cst %d hv %d\n",
  135. cache_info.dc_conf.cc_wt,
  136. cache_info.dc_conf.cc_sh,
  137. cache_info.dc_conf.cc_cst,
  138. cache_info.dc_conf.cc_hv);
  139. printk("IC base 0x%lx stride 0x%lx count 0x%lx loop 0x%lx\n",
  140. cache_info.ic_base,
  141. cache_info.ic_stride,
  142. cache_info.ic_count,
  143. cache_info.ic_loop);
  144. printk("ic_conf = 0x%lx alias %d blk %d line %d shift %d\n",
  145. *(unsigned long *) (&cache_info.ic_conf),
  146. cache_info.ic_conf.cc_alias,
  147. cache_info.ic_conf.cc_block,
  148. cache_info.ic_conf.cc_line,
  149. cache_info.ic_conf.cc_shift);
  150. printk(" wt %d sh %d cst %d hv %d\n",
  151. cache_info.ic_conf.cc_wt,
  152. cache_info.ic_conf.cc_sh,
  153. cache_info.ic_conf.cc_cst,
  154. cache_info.ic_conf.cc_hv);
  155. printk("D-TLB conf: sh %d page %d cst %d aid %d pad1 %d \n",
  156. cache_info.dt_conf.tc_sh,
  157. cache_info.dt_conf.tc_page,
  158. cache_info.dt_conf.tc_cst,
  159. cache_info.dt_conf.tc_aid,
  160. cache_info.dt_conf.tc_pad1);
  161. printk("I-TLB conf: sh %d page %d cst %d aid %d pad1 %d \n",
  162. cache_info.it_conf.tc_sh,
  163. cache_info.it_conf.tc_page,
  164. cache_info.it_conf.tc_cst,
  165. cache_info.it_conf.tc_aid,
  166. cache_info.it_conf.tc_pad1);
  167. #endif
  168. split_tlb = 0;
  169. if (cache_info.dt_conf.tc_sh == 0 || cache_info.dt_conf.tc_sh == 2) {
  170. if (cache_info.dt_conf.tc_sh == 2)
  171. printk(KERN_WARNING "Unexpected TLB configuration. "
  172. "Will flush I/D separately (could be optimized).\n");
  173. split_tlb = 1;
  174. }
  175. /* "New and Improved" version from Jim Hull
  176. * (1 << (cc_block-1)) * (cc_line << (4 + cnf.cc_shift))
  177. * The following CAFL_STRIDE is an optimized version, see
  178. * http://lists.parisc-linux.org/pipermail/parisc-linux/2004-June/023625.html
  179. * http://lists.parisc-linux.org/pipermail/parisc-linux/2004-June/023671.html
  180. */
  181. #define CAFL_STRIDE(cnf) (cnf.cc_line << (3 + cnf.cc_block + cnf.cc_shift))
  182. dcache_stride = CAFL_STRIDE(cache_info.dc_conf);
  183. icache_stride = CAFL_STRIDE(cache_info.ic_conf);
  184. #undef CAFL_STRIDE
  185. #ifndef CONFIG_PA20
  186. if (pdc_btlb_info(&btlb_info) < 0) {
  187. memset(&btlb_info, 0, sizeof btlb_info);
  188. }
  189. #endif
  190. if ((boot_cpu_data.pdc.capabilities & PDC_MODEL_NVA_MASK) ==
  191. PDC_MODEL_NVA_UNSUPPORTED) {
  192. printk(KERN_WARNING "parisc_cache_init: Only equivalent aliasing supported!\n");
  193. #if 0
  194. panic("SMP kernel required to avoid non-equivalent aliasing");
  195. #endif
  196. }
  197. }
  198. void disable_sr_hashing(void)
  199. {
  200. int srhash_type, retval;
  201. unsigned long space_bits;
  202. switch (boot_cpu_data.cpu_type) {
  203. case pcx: /* We shouldn't get this far. setup.c should prevent it. */
  204. BUG();
  205. return;
  206. case pcxs:
  207. case pcxt:
  208. case pcxt_:
  209. srhash_type = SRHASH_PCXST;
  210. break;
  211. case pcxl:
  212. srhash_type = SRHASH_PCXL;
  213. break;
  214. case pcxl2: /* pcxl2 doesn't support space register hashing */
  215. return;
  216. default: /* Currently all PA2.0 machines use the same ins. sequence */
  217. srhash_type = SRHASH_PA20;
  218. break;
  219. }
  220. disable_sr_hashing_asm(srhash_type);
  221. retval = pdc_spaceid_bits(&space_bits);
  222. /* If this procedure isn't implemented, don't panic. */
  223. if (retval < 0 && retval != PDC_BAD_OPTION)
  224. panic("pdc_spaceid_bits call failed.\n");
  225. if (space_bits != 0)
  226. panic("SpaceID hashing is still on!\n");
  227. }
  228. /* Simple function to work out if we have an existing address translation
  229. * for a user space vma. */
  230. static inline int translation_exists(struct vm_area_struct *vma,
  231. unsigned long addr, unsigned long pfn)
  232. {
  233. pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
  234. pmd_t *pmd;
  235. pte_t pte;
  236. if(pgd_none(*pgd))
  237. return 0;
  238. pmd = pmd_offset(pgd, addr);
  239. if(pmd_none(*pmd) || pmd_bad(*pmd))
  240. return 0;
  241. /* We cannot take the pte lock here: flush_cache_page is usually
  242. * called with pte lock already held. Whereas flush_dcache_page
  243. * takes flush_dcache_mmap_lock, which is lower in the hierarchy:
  244. * the vma itself is secure, but the pte might come or go racily.
  245. */
  246. pte = *pte_offset_map(pmd, addr);
  247. /* But pte_unmap() does nothing on this architecture */
  248. /* Filter out coincidental file entries and swap entries */
  249. if (!(pte_val(pte) & (_PAGE_FLUSH|_PAGE_PRESENT)))
  250. return 0;
  251. return pte_pfn(pte) == pfn;
  252. }
  253. /* Private function to flush a page from the cache of a non-current
  254. * process. cr25 contains the Page Directory of the current user
  255. * process; we're going to hijack both it and the user space %sr3 to
  256. * temporarily make the non-current process current. We have to do
  257. * this because cache flushing may cause a non-access tlb miss which
  258. * the handlers have to fill in from the pgd of the non-current
  259. * process. */
  260. static inline void
  261. flush_user_cache_page_non_current(struct vm_area_struct *vma,
  262. unsigned long vmaddr)
  263. {
  264. /* save the current process space and pgd */
  265. unsigned long space = mfsp(3), pgd = mfctl(25);
  266. /* we don't mind taking interrupts since they may not
  267. * do anything with user space, but we can't
  268. * be preempted here */
  269. preempt_disable();
  270. /* make us current */
  271. mtctl(__pa(vma->vm_mm->pgd), 25);
  272. mtsp(vma->vm_mm->context, 3);
  273. flush_user_dcache_page(vmaddr);
  274. if(vma->vm_flags & VM_EXEC)
  275. flush_user_icache_page(vmaddr);
  276. /* put the old current process back */
  277. mtsp(space, 3);
  278. mtctl(pgd, 25);
  279. preempt_enable();
  280. }
  281. static inline void
  282. __flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr)
  283. {
  284. if (likely(vma->vm_mm->context == mfsp(3))) {
  285. flush_user_dcache_page(vmaddr);
  286. if (vma->vm_flags & VM_EXEC)
  287. flush_user_icache_page(vmaddr);
  288. } else {
  289. flush_user_cache_page_non_current(vma, vmaddr);
  290. }
  291. }
  292. void flush_dcache_page(struct page *page)
  293. {
  294. struct address_space *mapping = page_mapping(page);
  295. struct vm_area_struct *mpnt;
  296. struct prio_tree_iter iter;
  297. unsigned long offset;
  298. unsigned long addr;
  299. pgoff_t pgoff;
  300. unsigned long pfn = page_to_pfn(page);
  301. if (mapping && !mapping_mapped(mapping)) {
  302. set_bit(PG_dcache_dirty, &page->flags);
  303. return;
  304. }
  305. flush_kernel_dcache_page(page);
  306. if (!mapping)
  307. return;
  308. pgoff = page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  309. /* We have carefully arranged in arch_get_unmapped_area() that
  310. * *any* mappings of a file are always congruently mapped (whether
  311. * declared as MAP_PRIVATE or MAP_SHARED), so we only need
  312. * to flush one address here for them all to become coherent */
  313. flush_dcache_mmap_lock(mapping);
  314. vma_prio_tree_foreach(mpnt, &iter, &mapping->i_mmap, pgoff, pgoff) {
  315. offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
  316. addr = mpnt->vm_start + offset;
  317. /* Flush instructions produce non access tlb misses.
  318. * On PA, we nullify these instructions rather than
  319. * taking a page fault if the pte doesn't exist.
  320. * This is just for speed. If the page translation
  321. * isn't there, there's no point exciting the
  322. * nadtlb handler into a nullification frenzy.
  323. *
  324. * Make sure we really have this page: the private
  325. * mappings may cover this area but have COW'd this
  326. * particular page.
  327. */
  328. if (translation_exists(mpnt, addr, pfn)) {
  329. __flush_cache_page(mpnt, addr);
  330. break;
  331. }
  332. }
  333. flush_dcache_mmap_unlock(mapping);
  334. }
  335. EXPORT_SYMBOL(flush_dcache_page);
  336. /* Defined in arch/parisc/kernel/pacache.S */
  337. EXPORT_SYMBOL(flush_kernel_dcache_range_asm);
  338. EXPORT_SYMBOL(flush_kernel_dcache_page_asm);
  339. EXPORT_SYMBOL(flush_data_cache_local);
  340. EXPORT_SYMBOL(flush_kernel_icache_range_asm);
  341. void clear_user_page_asm(void *page, unsigned long vaddr)
  342. {
  343. /* This function is implemented in assembly in pacache.S */
  344. extern void __clear_user_page_asm(void *page, unsigned long vaddr);
  345. purge_tlb_start();
  346. __clear_user_page_asm(page, vaddr);
  347. purge_tlb_end();
  348. }
  349. #define FLUSH_THRESHOLD 0x80000 /* 0.5MB */
  350. int parisc_cache_flush_threshold __read_mostly = FLUSH_THRESHOLD;
  351. void __init parisc_setup_cache_timing(void)
  352. {
  353. unsigned long rangetime, alltime;
  354. unsigned long size;
  355. alltime = mfctl(16);
  356. flush_data_cache();
  357. alltime = mfctl(16) - alltime;
  358. size = (unsigned long)(_end - _text);
  359. rangetime = mfctl(16);
  360. flush_kernel_dcache_range((unsigned long)_text, size);
  361. rangetime = mfctl(16) - rangetime;
  362. printk(KERN_DEBUG "Whole cache flush %lu cycles, flushing %lu bytes %lu cycles\n",
  363. alltime, size, rangetime);
  364. /* Racy, but if we see an intermediate value, it's ok too... */
  365. parisc_cache_flush_threshold = size * alltime / rangetime;
  366. parisc_cache_flush_threshold = (parisc_cache_flush_threshold + L1_CACHE_BYTES - 1) &~ (L1_CACHE_BYTES - 1);
  367. if (!parisc_cache_flush_threshold)
  368. parisc_cache_flush_threshold = FLUSH_THRESHOLD;
  369. if (parisc_cache_flush_threshold > cache_info.dc_size)
  370. parisc_cache_flush_threshold = cache_info.dc_size;
  371. printk(KERN_INFO "Setting cache flush threshold to %x (%d CPUs online)\n", parisc_cache_flush_threshold, num_online_cpus());
  372. }
  373. extern void purge_kernel_dcache_page(unsigned long);
  374. extern void clear_user_page_asm(void *page, unsigned long vaddr);
  375. void clear_user_page(void *page, unsigned long vaddr, struct page *pg)
  376. {
  377. purge_kernel_dcache_page((unsigned long)page);
  378. purge_tlb_start();
  379. pdtlb_kernel(page);
  380. purge_tlb_end();
  381. clear_user_page_asm(page, vaddr);
  382. }
  383. EXPORT_SYMBOL(clear_user_page);
  384. void flush_kernel_dcache_page_addr(void *addr)
  385. {
  386. flush_kernel_dcache_page_asm(addr);
  387. purge_tlb_start();
  388. pdtlb_kernel(addr);
  389. purge_tlb_end();
  390. }
  391. EXPORT_SYMBOL(flush_kernel_dcache_page_addr);
  392. void copy_user_page(void *vto, void *vfrom, unsigned long vaddr,
  393. struct page *pg)
  394. {
  395. /* no coherency needed (all in kmap/kunmap) */
  396. copy_user_page_asm(vto, vfrom);
  397. if (!parisc_requires_coherency())
  398. flush_kernel_dcache_page_asm(vto);
  399. }
  400. EXPORT_SYMBOL(copy_user_page);
  401. #ifdef CONFIG_PA8X00
  402. void kunmap_parisc(void *addr)
  403. {
  404. if (parisc_requires_coherency())
  405. flush_kernel_dcache_page_addr(addr);
  406. }
  407. EXPORT_SYMBOL(kunmap_parisc);
  408. #endif
  409. void __flush_tlb_range(unsigned long sid, unsigned long start,
  410. unsigned long end)
  411. {
  412. unsigned long npages;
  413. npages = ((end - (start & PAGE_MASK)) + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  414. if (npages >= 512) /* 2MB of space: arbitrary, should be tuned */
  415. flush_tlb_all();
  416. else {
  417. mtsp(sid, 1);
  418. purge_tlb_start();
  419. if (split_tlb) {
  420. while (npages--) {
  421. pdtlb(start);
  422. pitlb(start);
  423. start += PAGE_SIZE;
  424. }
  425. } else {
  426. while (npages--) {
  427. pdtlb(start);
  428. start += PAGE_SIZE;
  429. }
  430. }
  431. purge_tlb_end();
  432. }
  433. }
  434. static void cacheflush_h_tmp_function(void *dummy)
  435. {
  436. flush_cache_all_local();
  437. }
  438. void flush_cache_all(void)
  439. {
  440. on_each_cpu(cacheflush_h_tmp_function, NULL, 1);
  441. }
  442. void flush_cache_mm(struct mm_struct *mm)
  443. {
  444. #ifdef CONFIG_SMP
  445. flush_cache_all();
  446. #else
  447. flush_cache_all_local();
  448. #endif
  449. }
  450. void
  451. flush_user_dcache_range(unsigned long start, unsigned long end)
  452. {
  453. if ((end - start) < parisc_cache_flush_threshold)
  454. flush_user_dcache_range_asm(start,end);
  455. else
  456. flush_data_cache();
  457. }
  458. void
  459. flush_user_icache_range(unsigned long start, unsigned long end)
  460. {
  461. if ((end - start) < parisc_cache_flush_threshold)
  462. flush_user_icache_range_asm(start,end);
  463. else
  464. flush_instruction_cache();
  465. }
  466. void flush_cache_range(struct vm_area_struct *vma,
  467. unsigned long start, unsigned long end)
  468. {
  469. int sr3;
  470. if (!vma->vm_mm->context) {
  471. BUG();
  472. return;
  473. }
  474. sr3 = mfsp(3);
  475. if (vma->vm_mm->context == sr3) {
  476. flush_user_dcache_range(start,end);
  477. flush_user_icache_range(start,end);
  478. } else {
  479. flush_cache_all();
  480. }
  481. }
  482. void
  483. flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr, unsigned long pfn)
  484. {
  485. BUG_ON(!vma->vm_mm->context);
  486. if (likely(translation_exists(vma, vmaddr, pfn)))
  487. __flush_cache_page(vma, vmaddr);
  488. }