cache.c 15 KB

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