cache.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. * arch/sh64/mm/cache.c
  7. *
  8. * Original version Copyright (C) 2000, 2001 Paolo Alberelli
  9. * Second version Copyright (C) benedict.gaster@superh.com 2002
  10. * Third version Copyright Richard.Curnow@superh.com 2003
  11. * Hacks to third version Copyright (C) 2003 Paul Mundt
  12. */
  13. /****************************************************************************/
  14. #include <linux/config.h>
  15. #include <linux/init.h>
  16. #include <linux/mman.h>
  17. #include <linux/mm.h>
  18. #include <linux/threads.h>
  19. #include <asm/page.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/processor.h>
  22. #include <asm/cache.h>
  23. #include <asm/tlb.h>
  24. #include <asm/io.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/mmu_context.h>
  27. #include <asm/pgalloc.h> /* for flush_itlb_range */
  28. #include <linux/proc_fs.h>
  29. /* This function is in entry.S */
  30. extern unsigned long switch_and_save_asid(unsigned long new_asid);
  31. /* Wired TLB entry for the D-cache */
  32. static unsigned long long dtlb_cache_slot;
  33. /**
  34. * sh64_cache_init()
  35. *
  36. * This is pretty much just a straightforward clone of the SH
  37. * detect_cpu_and_cache_system().
  38. *
  39. * This function is responsible for setting up all of the cache
  40. * info dynamically as well as taking care of CPU probing and
  41. * setting up the relevant subtype data.
  42. *
  43. * FIXME: For the time being, we only really support the SH5-101
  44. * out of the box, and don't support dynamic probing for things
  45. * like the SH5-103 or even cut2 of the SH5-101. Implement this
  46. * later!
  47. */
  48. int __init sh64_cache_init(void)
  49. {
  50. /*
  51. * First, setup some sane values for the I-cache.
  52. */
  53. cpu_data->icache.ways = 4;
  54. cpu_data->icache.sets = 256;
  55. cpu_data->icache.linesz = L1_CACHE_BYTES;
  56. /*
  57. * FIXME: This can probably be cleaned up a bit as well.. for example,
  58. * do we really need the way shift _and_ the way_step_shift ?? Judging
  59. * by the existing code, I would guess no.. is there any valid reason
  60. * why we need to be tracking this around?
  61. */
  62. cpu_data->icache.way_shift = 13;
  63. cpu_data->icache.entry_shift = 5;
  64. cpu_data->icache.set_shift = 4;
  65. cpu_data->icache.way_step_shift = 16;
  66. cpu_data->icache.asid_shift = 2;
  67. /*
  68. * way offset = cache size / associativity, so just don't factor in
  69. * associativity in the first place..
  70. */
  71. cpu_data->icache.way_ofs = cpu_data->icache.sets *
  72. cpu_data->icache.linesz;
  73. cpu_data->icache.asid_mask = 0x3fc;
  74. cpu_data->icache.idx_mask = 0x1fe0;
  75. cpu_data->icache.epn_mask = 0xffffe000;
  76. cpu_data->icache.flags = 0;
  77. /*
  78. * Next, setup some sane values for the D-cache.
  79. *
  80. * On the SH5, these are pretty consistent with the I-cache settings,
  81. * so we just copy over the existing definitions.. these can be fixed
  82. * up later, especially if we add runtime CPU probing.
  83. *
  84. * Though in the meantime it saves us from having to duplicate all of
  85. * the above definitions..
  86. */
  87. cpu_data->dcache = cpu_data->icache;
  88. /*
  89. * Setup any cache-related flags here
  90. */
  91. #if defined(CONFIG_DCACHE_WRITE_THROUGH)
  92. set_bit(SH_CACHE_MODE_WT, &(cpu_data->dcache.flags));
  93. #elif defined(CONFIG_DCACHE_WRITE_BACK)
  94. set_bit(SH_CACHE_MODE_WB, &(cpu_data->dcache.flags));
  95. #endif
  96. /*
  97. * We also need to reserve a slot for the D-cache in the DTLB, so we
  98. * do this now ..
  99. */
  100. dtlb_cache_slot = sh64_get_wired_dtlb_entry();
  101. return 0;
  102. }
  103. #ifdef CONFIG_DCACHE_DISABLED
  104. #define sh64_dcache_purge_all() do { } while (0)
  105. #define sh64_dcache_purge_coloured_phy_page(paddr, eaddr) do { } while (0)
  106. #define sh64_dcache_purge_user_range(mm, start, end) do { } while (0)
  107. #define sh64_dcache_purge_phy_page(paddr) do { } while (0)
  108. #define sh64_dcache_purge_virt_page(mm, eaddr) do { } while (0)
  109. #define sh64_dcache_purge_kernel_range(start, end) do { } while (0)
  110. #define sh64_dcache_wback_current_user_range(start, end) do { } while (0)
  111. #endif
  112. /*##########################################################################*/
  113. /* From here onwards, a rewrite of the implementation,
  114. by Richard.Curnow@superh.com.
  115. The major changes in this compared to the old version are;
  116. 1. use more selective purging through OCBP instead of using ALLOCO to purge
  117. by natural replacement. This avoids purging out unrelated cache lines
  118. that happen to be in the same set.
  119. 2. exploit the APIs copy_user_page and clear_user_page better
  120. 3. be more selective about I-cache purging, in particular use invalidate_all
  121. more sparingly.
  122. */
  123. /*##########################################################################
  124. SUPPORT FUNCTIONS
  125. ##########################################################################*/
  126. /****************************************************************************/
  127. /* The following group of functions deal with mapping and unmapping a temporary
  128. page into the DTLB slot that have been set aside for our exclusive use. */
  129. /* In order to accomplish this, we use the generic interface for adding and
  130. removing a wired slot entry as defined in arch/sh64/mm/tlb.c */
  131. /****************************************************************************/
  132. static unsigned long slot_own_flags;
  133. static inline void sh64_setup_dtlb_cache_slot(unsigned long eaddr, unsigned long asid, unsigned long paddr)
  134. {
  135. local_irq_save(slot_own_flags);
  136. sh64_setup_tlb_slot(dtlb_cache_slot, eaddr, asid, paddr);
  137. }
  138. static inline void sh64_teardown_dtlb_cache_slot(void)
  139. {
  140. sh64_teardown_tlb_slot(dtlb_cache_slot);
  141. local_irq_restore(slot_own_flags);
  142. }
  143. /****************************************************************************/
  144. #ifndef CONFIG_ICACHE_DISABLED
  145. static void __inline__ sh64_icache_inv_all(void)
  146. {
  147. unsigned long long addr, flag, data;
  148. unsigned int flags;
  149. addr=ICCR0;
  150. flag=ICCR0_ICI;
  151. data=0;
  152. /* Make this a critical section for safety (probably not strictly necessary.) */
  153. local_irq_save(flags);
  154. /* Without %1 it gets unexplicably wrong */
  155. asm volatile("getcfg %3, 0, %0\n\t"
  156. "or %0, %2, %0\n\t"
  157. "putcfg %3, 0, %0\n\t"
  158. "synci"
  159. : "=&r" (data)
  160. : "0" (data), "r" (flag), "r" (addr));
  161. local_irq_restore(flags);
  162. }
  163. static void sh64_icache_inv_kernel_range(unsigned long start, unsigned long end)
  164. {
  165. /* Invalidate range of addresses [start,end] from the I-cache, where
  166. * the addresses lie in the kernel superpage. */
  167. unsigned long long ullend, addr, aligned_start;
  168. #if (NEFF == 32)
  169. aligned_start = (unsigned long long)(signed long long)(signed long) start;
  170. #else
  171. #error "NEFF != 32"
  172. #endif
  173. aligned_start &= L1_CACHE_ALIGN_MASK;
  174. addr = aligned_start;
  175. #if (NEFF == 32)
  176. ullend = (unsigned long long) (signed long long) (signed long) end;
  177. #else
  178. #error "NEFF != 32"
  179. #endif
  180. while (addr <= ullend) {
  181. asm __volatile__ ("icbi %0, 0" : : "r" (addr));
  182. addr += L1_CACHE_BYTES;
  183. }
  184. }
  185. static void sh64_icache_inv_user_page(struct vm_area_struct *vma, unsigned long eaddr)
  186. {
  187. /* If we get called, we know that vma->vm_flags contains VM_EXEC.
  188. Also, eaddr is page-aligned. */
  189. unsigned long long addr, end_addr;
  190. unsigned long flags = 0;
  191. unsigned long running_asid, vma_asid;
  192. addr = eaddr;
  193. end_addr = addr + PAGE_SIZE;
  194. /* Check whether we can use the current ASID for the I-cache
  195. invalidation. For example, if we're called via
  196. access_process_vm->flush_cache_page->here, (e.g. when reading from
  197. /proc), 'running_asid' will be that of the reader, not of the
  198. victim.
  199. Also, note the risk that we might get pre-empted between the ASID
  200. compare and blocking IRQs, and before we regain control, the
  201. pid->ASID mapping changes. However, the whole cache will get
  202. invalidated when the mapping is renewed, so the worst that can
  203. happen is that the loop below ends up invalidating somebody else's
  204. cache entries.
  205. */
  206. running_asid = get_asid();
  207. vma_asid = (vma->vm_mm->context & MMU_CONTEXT_ASID_MASK);
  208. if (running_asid != vma_asid) {
  209. local_irq_save(flags);
  210. switch_and_save_asid(vma_asid);
  211. }
  212. while (addr < end_addr) {
  213. /* Worth unrolling a little */
  214. asm __volatile__("icbi %0, 0" : : "r" (addr));
  215. asm __volatile__("icbi %0, 32" : : "r" (addr));
  216. asm __volatile__("icbi %0, 64" : : "r" (addr));
  217. asm __volatile__("icbi %0, 96" : : "r" (addr));
  218. addr += 128;
  219. }
  220. if (running_asid != vma_asid) {
  221. switch_and_save_asid(running_asid);
  222. local_irq_restore(flags);
  223. }
  224. }
  225. /****************************************************************************/
  226. static void sh64_icache_inv_user_page_range(struct mm_struct *mm,
  227. unsigned long start, unsigned long end)
  228. {
  229. /* Used for invalidating big chunks of I-cache, i.e. assume the range
  230. is whole pages. If 'start' or 'end' is not page aligned, the code
  231. is conservative and invalidates to the ends of the enclosing pages.
  232. This is functionally OK, just a performance loss. */
  233. /* See the comments below in sh64_dcache_purge_user_range() regarding
  234. the choice of algorithm. However, for the I-cache option (2) isn't
  235. available because there are no physical tags so aliases can't be
  236. resolved. The icbi instruction has to be used through the user
  237. mapping. Because icbi is cheaper than ocbp on a cache hit, it
  238. would be cheaper to use the selective code for a large range than is
  239. possible with the D-cache. Just assume 64 for now as a working
  240. figure.
  241. */
  242. int n_pages;
  243. if (!mm) return;
  244. n_pages = ((end - start) >> PAGE_SHIFT);
  245. if (n_pages >= 64) {
  246. sh64_icache_inv_all();
  247. } else {
  248. unsigned long aligned_start;
  249. unsigned long eaddr;
  250. unsigned long after_last_page_start;
  251. unsigned long mm_asid, current_asid;
  252. unsigned long long flags = 0ULL;
  253. mm_asid = mm->context & MMU_CONTEXT_ASID_MASK;
  254. current_asid = get_asid();
  255. if (mm_asid != current_asid) {
  256. /* Switch ASID and run the invalidate loop under cli */
  257. local_irq_save(flags);
  258. switch_and_save_asid(mm_asid);
  259. }
  260. aligned_start = start & PAGE_MASK;
  261. after_last_page_start = PAGE_SIZE + ((end - 1) & PAGE_MASK);
  262. while (aligned_start < after_last_page_start) {
  263. struct vm_area_struct *vma;
  264. unsigned long vma_end;
  265. vma = find_vma(mm, aligned_start);
  266. if (!vma || (aligned_start <= vma->vm_end)) {
  267. /* Avoid getting stuck in an error condition */
  268. aligned_start += PAGE_SIZE;
  269. continue;
  270. }
  271. vma_end = vma->vm_end;
  272. if (vma->vm_flags & VM_EXEC) {
  273. /* Executable */
  274. eaddr = aligned_start;
  275. while (eaddr < vma_end) {
  276. sh64_icache_inv_user_page(vma, eaddr);
  277. eaddr += PAGE_SIZE;
  278. }
  279. }
  280. aligned_start = vma->vm_end; /* Skip to start of next region */
  281. }
  282. if (mm_asid != current_asid) {
  283. switch_and_save_asid(current_asid);
  284. local_irq_restore(flags);
  285. }
  286. }
  287. }
  288. static void sh64_icache_inv_user_small_range(struct mm_struct *mm,
  289. unsigned long start, int len)
  290. {
  291. /* Invalidate a small range of user context I-cache, not necessarily
  292. page (or even cache-line) aligned. */
  293. unsigned long long eaddr = start;
  294. unsigned long long eaddr_end = start + len;
  295. unsigned long current_asid, mm_asid;
  296. unsigned long long flags;
  297. unsigned long long epage_start;
  298. /* Since this is used inside ptrace, the ASID in the mm context
  299. typically won't match current_asid. We'll have to switch ASID to do
  300. this. For safety, and given that the range will be small, do all
  301. this under cli.
  302. Note, there is a hazard that the ASID in mm->context is no longer
  303. actually associated with mm, i.e. if the mm->context has started a
  304. new cycle since mm was last active. However, this is just a
  305. performance issue: all that happens is that we invalidate lines
  306. belonging to another mm, so the owning process has to refill them
  307. when that mm goes live again. mm itself can't have any cache
  308. entries because there will have been a flush_cache_all when the new
  309. mm->context cycle started. */
  310. /* Align to start of cache line. Otherwise, suppose len==8 and start
  311. was at 32N+28 : the last 4 bytes wouldn't get invalidated. */
  312. eaddr = start & L1_CACHE_ALIGN_MASK;
  313. eaddr_end = start + len;
  314. local_irq_save(flags);
  315. mm_asid = mm->context & MMU_CONTEXT_ASID_MASK;
  316. current_asid = switch_and_save_asid(mm_asid);
  317. epage_start = eaddr & PAGE_MASK;
  318. while (eaddr < eaddr_end)
  319. {
  320. asm __volatile__("icbi %0, 0" : : "r" (eaddr));
  321. eaddr += L1_CACHE_BYTES;
  322. }
  323. switch_and_save_asid(current_asid);
  324. local_irq_restore(flags);
  325. }
  326. static void sh64_icache_inv_current_user_range(unsigned long start, unsigned long end)
  327. {
  328. /* The icbi instruction never raises ITLBMISS. i.e. if there's not a
  329. cache hit on the virtual tag the instruction ends there, without a
  330. TLB lookup. */
  331. unsigned long long aligned_start;
  332. unsigned long long ull_end;
  333. unsigned long long addr;
  334. ull_end = end;
  335. /* Just invalidate over the range using the natural addresses. TLB
  336. miss handling will be OK (TBC). Since it's for the current process,
  337. either we're already in the right ASID context, or the ASIDs have
  338. been recycled since we were last active in which case we might just
  339. invalidate another processes I-cache entries : no worries, just a
  340. performance drop for him. */
  341. aligned_start = start & L1_CACHE_ALIGN_MASK;
  342. addr = aligned_start;
  343. while (addr < ull_end) {
  344. asm __volatile__ ("icbi %0, 0" : : "r" (addr));
  345. asm __volatile__ ("nop");
  346. asm __volatile__ ("nop");
  347. addr += L1_CACHE_BYTES;
  348. }
  349. }
  350. #endif /* !CONFIG_ICACHE_DISABLED */
  351. /****************************************************************************/
  352. #ifndef CONFIG_DCACHE_DISABLED
  353. /* Buffer used as the target of alloco instructions to purge data from cache
  354. sets by natural eviction. -- RPC */
  355. #define DUMMY_ALLOCO_AREA_SIZE L1_CACHE_SIZE_BYTES + (1024 * 4)
  356. static unsigned char dummy_alloco_area[DUMMY_ALLOCO_AREA_SIZE] __cacheline_aligned = { 0, };
  357. /****************************************************************************/
  358. static void __inline__ sh64_dcache_purge_sets(int sets_to_purge_base, int n_sets)
  359. {
  360. /* Purge all ways in a particular block of sets, specified by the base
  361. set number and number of sets. Can handle wrap-around, if that's
  362. needed. */
  363. int dummy_buffer_base_set;
  364. unsigned long long eaddr, eaddr0, eaddr1;
  365. int j;
  366. int set_offset;
  367. dummy_buffer_base_set = ((int)&dummy_alloco_area & cpu_data->dcache.idx_mask) >> cpu_data->dcache.entry_shift;
  368. set_offset = sets_to_purge_base - dummy_buffer_base_set;
  369. for (j=0; j<n_sets; j++, set_offset++) {
  370. set_offset &= (cpu_data->dcache.sets - 1);
  371. eaddr0 = (unsigned long long)dummy_alloco_area + (set_offset << cpu_data->dcache.entry_shift);
  372. /* Do one alloco which hits the required set per cache way. For
  373. write-back mode, this will purge the #ways resident lines. There's
  374. little point unrolling this loop because the allocos stall more if
  375. they're too close together. */
  376. eaddr1 = eaddr0 + cpu_data->dcache.way_ofs * cpu_data->dcache.ways;
  377. for (eaddr=eaddr0; eaddr<eaddr1; eaddr+=cpu_data->dcache.way_ofs) {
  378. asm __volatile__ ("alloco %0, 0" : : "r" (eaddr));
  379. asm __volatile__ ("synco"); /* TAKum03020 */
  380. }
  381. eaddr1 = eaddr0 + cpu_data->dcache.way_ofs * cpu_data->dcache.ways;
  382. for (eaddr=eaddr0; eaddr<eaddr1; eaddr+=cpu_data->dcache.way_ofs) {
  383. /* Load from each address. Required because alloco is a NOP if
  384. the cache is write-through. Write-through is a config option. */
  385. if (test_bit(SH_CACHE_MODE_WT, &(cpu_data->dcache.flags)))
  386. *(volatile unsigned char *)(int)eaddr;
  387. }
  388. }
  389. /* Don't use OCBI to invalidate the lines. That costs cycles directly.
  390. If the dummy block is just left resident, it will naturally get
  391. evicted as required. */
  392. return;
  393. }
  394. /****************************************************************************/
  395. static void sh64_dcache_purge_all(void)
  396. {
  397. /* Purge the entire contents of the dcache. The most efficient way to
  398. achieve this is to use alloco instructions on a region of unused
  399. memory equal in size to the cache, thereby causing the current
  400. contents to be discarded by natural eviction. The alternative,
  401. namely reading every tag, setting up a mapping for the corresponding
  402. page and doing an OCBP for the line, would be much more expensive.
  403. */
  404. sh64_dcache_purge_sets(0, cpu_data->dcache.sets);
  405. return;
  406. }
  407. /****************************************************************************/
  408. static void sh64_dcache_purge_kernel_range(unsigned long start, unsigned long end)
  409. {
  410. /* Purge the range of addresses [start,end] from the D-cache. The
  411. addresses lie in the superpage mapping. There's no harm if we
  412. overpurge at either end - just a small performance loss. */
  413. unsigned long long ullend, addr, aligned_start;
  414. #if (NEFF == 32)
  415. aligned_start = (unsigned long long)(signed long long)(signed long) start;
  416. #else
  417. #error "NEFF != 32"
  418. #endif
  419. aligned_start &= L1_CACHE_ALIGN_MASK;
  420. addr = aligned_start;
  421. #if (NEFF == 32)
  422. ullend = (unsigned long long) (signed long long) (signed long) end;
  423. #else
  424. #error "NEFF != 32"
  425. #endif
  426. while (addr <= ullend) {
  427. asm __volatile__ ("ocbp %0, 0" : : "r" (addr));
  428. addr += L1_CACHE_BYTES;
  429. }
  430. return;
  431. }
  432. /* Assumes this address (+ (2**n_synbits) pages up from it) aren't used for
  433. anything else in the kernel */
  434. #define MAGIC_PAGE0_START 0xffffffffec000000ULL
  435. static void sh64_dcache_purge_coloured_phy_page(unsigned long paddr, unsigned long eaddr)
  436. {
  437. /* Purge the physical page 'paddr' from the cache. It's known that any
  438. cache lines requiring attention have the same page colour as the the
  439. address 'eaddr'.
  440. This relies on the fact that the D-cache matches on physical tags
  441. when no virtual tag matches. So we create an alias for the original
  442. page and purge through that. (Alternatively, we could have done
  443. this by switching ASID to match the original mapping and purged
  444. through that, but that involves ASID switching cost + probably a
  445. TLBMISS + refill anyway.)
  446. */
  447. unsigned long long magic_page_start;
  448. unsigned long long magic_eaddr, magic_eaddr_end;
  449. magic_page_start = MAGIC_PAGE0_START + (eaddr & CACHE_OC_SYN_MASK);
  450. /* As long as the kernel is not pre-emptible, this doesn't need to be
  451. under cli/sti. */
  452. sh64_setup_dtlb_cache_slot(magic_page_start, get_asid(), paddr);
  453. magic_eaddr = magic_page_start;
  454. magic_eaddr_end = magic_eaddr + PAGE_SIZE;
  455. while (magic_eaddr < magic_eaddr_end) {
  456. /* Little point in unrolling this loop - the OCBPs are blocking
  457. and won't go any quicker (i.e. the loop overhead is parallel
  458. to part of the OCBP execution.) */
  459. asm __volatile__ ("ocbp %0, 0" : : "r" (magic_eaddr));
  460. magic_eaddr += L1_CACHE_BYTES;
  461. }
  462. sh64_teardown_dtlb_cache_slot();
  463. }
  464. /****************************************************************************/
  465. static void sh64_dcache_purge_phy_page(unsigned long paddr)
  466. {
  467. /* Pure a page given its physical start address, by creating a
  468. temporary 1 page mapping and purging across that. Even if we know
  469. the virtual address (& vma or mm) of the page, the method here is
  470. more elegant because it avoids issues of coping with page faults on
  471. the purge instructions (i.e. no special-case code required in the
  472. critical path in the TLB miss handling). */
  473. unsigned long long eaddr_start, eaddr, eaddr_end;
  474. int i;
  475. /* As long as the kernel is not pre-emptible, this doesn't need to be
  476. under cli/sti. */
  477. eaddr_start = MAGIC_PAGE0_START;
  478. for (i=0; i < (1 << CACHE_OC_N_SYNBITS); i++) {
  479. sh64_setup_dtlb_cache_slot(eaddr_start, get_asid(), paddr);
  480. eaddr = eaddr_start;
  481. eaddr_end = eaddr + PAGE_SIZE;
  482. while (eaddr < eaddr_end) {
  483. asm __volatile__ ("ocbp %0, 0" : : "r" (eaddr));
  484. eaddr += L1_CACHE_BYTES;
  485. }
  486. sh64_teardown_dtlb_cache_slot();
  487. eaddr_start += PAGE_SIZE;
  488. }
  489. }
  490. static void sh64_dcache_purge_user_page(struct mm_struct *mm, unsigned long eaddr)
  491. {
  492. pgd_t *pgd;
  493. pmd_t *pmd;
  494. pte_t *pte;
  495. pte_t entry;
  496. unsigned long paddr;
  497. /* NOTE : all the callers of this have mm->page_table_lock held, so the
  498. following page table traversal is safe even on SMP/pre-emptible. */
  499. if (!mm) return; /* No way to find physical address of page */
  500. pgd = pgd_offset(mm, eaddr);
  501. if (pgd_bad(*pgd)) return;
  502. pmd = pmd_offset(pgd, eaddr);
  503. if (pmd_none(*pmd) || pmd_bad(*pmd)) return;
  504. pte = pte_offset_kernel(pmd, eaddr);
  505. entry = *pte;
  506. if (pte_none(entry) || !pte_present(entry)) return;
  507. paddr = pte_val(entry) & PAGE_MASK;
  508. sh64_dcache_purge_coloured_phy_page(paddr, eaddr);
  509. }
  510. /****************************************************************************/
  511. static void sh64_dcache_purge_user_range(struct mm_struct *mm,
  512. unsigned long start, unsigned long end)
  513. {
  514. /* There are at least 5 choices for the implementation of this, with
  515. pros (+), cons(-), comments(*):
  516. 1. ocbp each line in the range through the original user's ASID
  517. + no lines spuriously evicted
  518. - tlbmiss handling (must either handle faults on demand => extra
  519. special-case code in tlbmiss critical path), or map the page in
  520. advance (=> flush_tlb_range in advance to avoid multiple hits)
  521. - ASID switching
  522. - expensive for large ranges
  523. 2. temporarily map each page in the range to a special effective
  524. address and ocbp through the temporary mapping; relies on the
  525. fact that SH-5 OCB* always do TLB lookup and match on ptags (they
  526. never look at the etags)
  527. + no spurious evictions
  528. - expensive for large ranges
  529. * surely cheaper than (1)
  530. 3. walk all the lines in the cache, check the tags, if a match
  531. occurs create a page mapping to ocbp the line through
  532. + no spurious evictions
  533. - tag inspection overhead
  534. - (especially for small ranges)
  535. - potential cost of setting up/tearing down page mapping for
  536. every line that matches the range
  537. * cost partly independent of range size
  538. 4. walk all the lines in the cache, check the tags, if a match
  539. occurs use 4 * alloco to purge the line (+3 other probably
  540. innocent victims) by natural eviction
  541. + no tlb mapping overheads
  542. - spurious evictions
  543. - tag inspection overhead
  544. 5. implement like flush_cache_all
  545. + no tag inspection overhead
  546. - spurious evictions
  547. - bad for small ranges
  548. (1) can be ruled out as more expensive than (2). (2) appears best
  549. for small ranges. The choice between (3), (4) and (5) for large
  550. ranges and the range size for the large/small boundary need
  551. benchmarking to determine.
  552. For now use approach (2) for small ranges and (5) for large ones.
  553. */
  554. int n_pages;
  555. n_pages = ((end - start) >> PAGE_SHIFT);
  556. if (n_pages >= 64) {
  557. #if 1
  558. sh64_dcache_purge_all();
  559. #else
  560. unsigned long long set, way;
  561. unsigned long mm_asid = mm->context & MMU_CONTEXT_ASID_MASK;
  562. for (set = 0; set < cpu_data->dcache.sets; set++) {
  563. unsigned long long set_base_config_addr = CACHE_OC_ADDRESS_ARRAY + (set << cpu_data->dcache.set_shift);
  564. for (way = 0; way < cpu_data->dcache.ways; way++) {
  565. unsigned long long config_addr = set_base_config_addr + (way << cpu_data->dcache.way_step_shift);
  566. unsigned long long tag0;
  567. unsigned long line_valid;
  568. asm __volatile__("getcfg %1, 0, %0" : "=r" (tag0) : "r" (config_addr));
  569. line_valid = tag0 & SH_CACHE_VALID;
  570. if (line_valid) {
  571. unsigned long cache_asid;
  572. unsigned long epn;
  573. cache_asid = (tag0 & cpu_data->dcache.asid_mask) >> cpu_data->dcache.asid_shift;
  574. /* The next line needs some
  575. explanation. The virtual tags
  576. encode bits [31:13] of the virtual
  577. address, bit [12] of the 'tag' being
  578. implied by the cache set index. */
  579. epn = (tag0 & cpu_data->dcache.epn_mask) | ((set & 0x80) << cpu_data->dcache.entry_shift);
  580. if ((cache_asid == mm_asid) && (start <= epn) && (epn < end)) {
  581. /* TODO : could optimise this
  582. call by batching multiple
  583. adjacent sets together. */
  584. sh64_dcache_purge_sets(set, 1);
  585. break; /* Don't waste time inspecting other ways for this set */
  586. }
  587. }
  588. }
  589. }
  590. #endif
  591. } else {
  592. /* 'Small' range */
  593. unsigned long aligned_start;
  594. unsigned long eaddr;
  595. unsigned long last_page_start;
  596. aligned_start = start & PAGE_MASK;
  597. /* 'end' is 1 byte beyond the end of the range */
  598. last_page_start = (end - 1) & PAGE_MASK;
  599. eaddr = aligned_start;
  600. while (eaddr <= last_page_start) {
  601. sh64_dcache_purge_user_page(mm, eaddr);
  602. eaddr += PAGE_SIZE;
  603. }
  604. }
  605. return;
  606. }
  607. static void sh64_dcache_wback_current_user_range(unsigned long start, unsigned long end)
  608. {
  609. unsigned long long aligned_start;
  610. unsigned long long ull_end;
  611. unsigned long long addr;
  612. ull_end = end;
  613. /* Just wback over the range using the natural addresses. TLB miss
  614. handling will be OK (TBC) : the range has just been written to by
  615. the signal frame setup code, so the PTEs must exist.
  616. Note, if we have CONFIG_PREEMPT and get preempted inside this loop,
  617. it doesn't matter, even if the pid->ASID mapping changes whilst
  618. we're away. In that case the cache will have been flushed when the
  619. mapping was renewed. So the writebacks below will be nugatory (and
  620. we'll doubtless have to fault the TLB entry/ies in again with the
  621. new ASID), but it's a rare case.
  622. */
  623. aligned_start = start & L1_CACHE_ALIGN_MASK;
  624. addr = aligned_start;
  625. while (addr < ull_end) {
  626. asm __volatile__ ("ocbwb %0, 0" : : "r" (addr));
  627. addr += L1_CACHE_BYTES;
  628. }
  629. }
  630. /****************************************************************************/
  631. /* These *MUST* lie in an area of virtual address space that's otherwise unused. */
  632. #define UNIQUE_EADDR_START 0xe0000000UL
  633. #define UNIQUE_EADDR_END 0xe8000000UL
  634. static unsigned long sh64_make_unique_eaddr(unsigned long user_eaddr, unsigned long paddr)
  635. {
  636. /* Given a physical address paddr, and a user virtual address
  637. user_eaddr which will eventually be mapped to it, create a one-off
  638. kernel-private eaddr mapped to the same paddr. This is used for
  639. creating special destination pages for copy_user_page and
  640. clear_user_page */
  641. static unsigned long current_pointer = UNIQUE_EADDR_START;
  642. unsigned long coloured_pointer;
  643. if (current_pointer == UNIQUE_EADDR_END) {
  644. sh64_dcache_purge_all();
  645. current_pointer = UNIQUE_EADDR_START;
  646. }
  647. coloured_pointer = (current_pointer & ~CACHE_OC_SYN_MASK) | (user_eaddr & CACHE_OC_SYN_MASK);
  648. sh64_setup_dtlb_cache_slot(coloured_pointer, get_asid(), paddr);
  649. current_pointer += (PAGE_SIZE << CACHE_OC_N_SYNBITS);
  650. return coloured_pointer;
  651. }
  652. /****************************************************************************/
  653. static void sh64_copy_user_page_coloured(void *to, void *from, unsigned long address)
  654. {
  655. void *coloured_to;
  656. /* Discard any existing cache entries of the wrong colour. These are
  657. present quite often, if the kernel has recently used the page
  658. internally, then given it up, then it's been allocated to the user.
  659. */
  660. sh64_dcache_purge_coloured_phy_page(__pa(to), (unsigned long) to);
  661. coloured_to = (void *) sh64_make_unique_eaddr(address, __pa(to));
  662. sh64_page_copy(from, coloured_to);
  663. sh64_teardown_dtlb_cache_slot();
  664. }
  665. static void sh64_clear_user_page_coloured(void *to, unsigned long address)
  666. {
  667. void *coloured_to;
  668. /* Discard any existing kernel-originated lines of the wrong colour (as
  669. above) */
  670. sh64_dcache_purge_coloured_phy_page(__pa(to), (unsigned long) to);
  671. coloured_to = (void *) sh64_make_unique_eaddr(address, __pa(to));
  672. sh64_page_clear(coloured_to);
  673. sh64_teardown_dtlb_cache_slot();
  674. }
  675. #endif /* !CONFIG_DCACHE_DISABLED */
  676. /****************************************************************************/
  677. /*##########################################################################
  678. EXTERNALLY CALLABLE API.
  679. ##########################################################################*/
  680. /* These functions are described in Documentation/cachetlb.txt.
  681. Each one of these functions varies in behaviour depending on whether the
  682. I-cache and/or D-cache are configured out.
  683. Note that the Linux term 'flush' corresponds to what is termed 'purge' in
  684. the sh/sh64 jargon for the D-cache, i.e. write back dirty data then
  685. invalidate the cache lines, and 'invalidate' for the I-cache.
  686. */
  687. #undef FLUSH_TRACE
  688. void flush_cache_all(void)
  689. {
  690. /* Invalidate the entire contents of both caches, after writing back to
  691. memory any dirty data from the D-cache. */
  692. sh64_dcache_purge_all();
  693. sh64_icache_inv_all();
  694. }
  695. /****************************************************************************/
  696. void flush_cache_mm(struct mm_struct *mm)
  697. {
  698. /* Invalidate an entire user-address space from both caches, after
  699. writing back dirty data (e.g. for shared mmap etc). */
  700. /* This could be coded selectively by inspecting all the tags then
  701. doing 4*alloco on any set containing a match (as for
  702. flush_cache_range), but fork/exit/execve (where this is called from)
  703. are expensive anyway. */
  704. /* Have to do a purge here, despite the comments re I-cache below.
  705. There could be odd-coloured dirty data associated with the mm still
  706. in the cache - if this gets written out through natural eviction
  707. after the kernel has reused the page there will be chaos.
  708. */
  709. sh64_dcache_purge_all();
  710. /* The mm being torn down won't ever be active again, so any Icache
  711. lines tagged with its ASID won't be visible for the rest of the
  712. lifetime of this ASID cycle. Before the ASID gets reused, there
  713. will be a flush_cache_all. Hence we don't need to touch the
  714. I-cache. This is similar to the lack of action needed in
  715. flush_tlb_mm - see fault.c. */
  716. }
  717. /****************************************************************************/
  718. void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  719. unsigned long end)
  720. {
  721. struct mm_struct *mm = vma->vm_mm;
  722. /* Invalidate (from both caches) the range [start,end) of virtual
  723. addresses from the user address space specified by mm, after writing
  724. back any dirty data.
  725. Note(1), 'end' is 1 byte beyond the end of the range to flush.
  726. Note(2), this is called with mm->page_table_lock held.*/
  727. sh64_dcache_purge_user_range(mm, start, end);
  728. sh64_icache_inv_user_page_range(mm, start, end);
  729. }
  730. /****************************************************************************/
  731. void flush_cache_page(struct vm_area_struct *vma, unsigned long eaddr, unsigned long pfn)
  732. {
  733. /* Invalidate any entries in either cache for the vma within the user
  734. address space vma->vm_mm for the page starting at virtual address
  735. 'eaddr'. This seems to be used primarily in breaking COW. Note,
  736. the I-cache must be searched too in case the page in question is
  737. both writable and being executed from (e.g. stack trampolines.)
  738. Note(1), this is called with mm->page_table_lock held.
  739. */
  740. sh64_dcache_purge_phy_page(pfn << PAGE_SHIFT);
  741. if (vma->vm_flags & VM_EXEC) {
  742. sh64_icache_inv_user_page(vma, eaddr);
  743. }
  744. }
  745. /****************************************************************************/
  746. #ifndef CONFIG_DCACHE_DISABLED
  747. void copy_user_page(void *to, void *from, unsigned long address, struct page *page)
  748. {
  749. /* 'from' and 'to' are kernel virtual addresses (within the superpage
  750. mapping of the physical RAM). 'address' is the user virtual address
  751. where the copy 'to' will be mapped after. This allows a custom
  752. mapping to be used to ensure that the new copy is placed in the
  753. right cache sets for the user to see it without having to bounce it
  754. out via memory. Note however : the call to flush_page_to_ram in
  755. (generic)/mm/memory.c:(break_cow) undoes all this good work in that one
  756. very important case!
  757. TBD : can we guarantee that on every call, any cache entries for
  758. 'from' are in the same colour sets as 'address' also? i.e. is this
  759. always used just to deal with COW? (I suspect not). */
  760. /* There are two possibilities here for when the page 'from' was last accessed:
  761. * by the kernel : this is OK, no purge required.
  762. * by the/a user (e.g. for break_COW) : need to purge.
  763. If the potential user mapping at 'address' is the same colour as
  764. 'from' there is no need to purge any cache lines from the 'from'
  765. page mapped into cache sets of colour 'address'. (The copy will be
  766. accessing the page through 'from').
  767. */
  768. if (((address ^ (unsigned long) from) & CACHE_OC_SYN_MASK) != 0) {
  769. sh64_dcache_purge_coloured_phy_page(__pa(from), address);
  770. }
  771. if (((address ^ (unsigned long) to) & CACHE_OC_SYN_MASK) == 0) {
  772. /* No synonym problem on destination */
  773. sh64_page_copy(from, to);
  774. } else {
  775. sh64_copy_user_page_coloured(to, from, address);
  776. }
  777. /* Note, don't need to flush 'from' page from the cache again - it's
  778. done anyway by the generic code */
  779. }
  780. void clear_user_page(void *to, unsigned long address, struct page *page)
  781. {
  782. /* 'to' is a kernel virtual address (within the superpage
  783. mapping of the physical RAM). 'address' is the user virtual address
  784. where the 'to' page will be mapped after. This allows a custom
  785. mapping to be used to ensure that the new copy is placed in the
  786. right cache sets for the user to see it without having to bounce it
  787. out via memory.
  788. */
  789. if (((address ^ (unsigned long) to) & CACHE_OC_SYN_MASK) == 0) {
  790. /* No synonym problem on destination */
  791. sh64_page_clear(to);
  792. } else {
  793. sh64_clear_user_page_coloured(to, address);
  794. }
  795. }
  796. #endif /* !CONFIG_DCACHE_DISABLED */
  797. /****************************************************************************/
  798. void flush_dcache_page(struct page *page)
  799. {
  800. sh64_dcache_purge_phy_page(page_to_phys(page));
  801. wmb();
  802. }
  803. /****************************************************************************/
  804. void flush_icache_range(unsigned long start, unsigned long end)
  805. {
  806. /* Flush the range [start,end] of kernel virtual adddress space from
  807. the I-cache. The corresponding range must be purged from the
  808. D-cache also because the SH-5 doesn't have cache snooping between
  809. the caches. The addresses will be visible through the superpage
  810. mapping, therefore it's guaranteed that there no cache entries for
  811. the range in cache sets of the wrong colour.
  812. Primarily used for cohering the I-cache after a module has
  813. been loaded. */
  814. /* We also make sure to purge the same range from the D-cache since
  815. flush_page_to_ram() won't be doing this for us! */
  816. sh64_dcache_purge_kernel_range(start, end);
  817. wmb();
  818. sh64_icache_inv_kernel_range(start, end);
  819. }
  820. /****************************************************************************/
  821. void flush_icache_user_range(struct vm_area_struct *vma,
  822. struct page *page, unsigned long addr, int len)
  823. {
  824. /* Flush the range of user (defined by vma->vm_mm) address space
  825. starting at 'addr' for 'len' bytes from the cache. The range does
  826. not straddle a page boundary, the unique physical page containing
  827. the range is 'page'. This seems to be used mainly for invalidating
  828. an address range following a poke into the program text through the
  829. ptrace() call from another process (e.g. for BRK instruction
  830. insertion). */
  831. sh64_dcache_purge_coloured_phy_page(page_to_phys(page), addr);
  832. mb();
  833. if (vma->vm_flags & VM_EXEC) {
  834. sh64_icache_inv_user_small_range(vma->vm_mm, addr, len);
  835. }
  836. }
  837. /*##########################################################################
  838. ARCH/SH64 PRIVATE CALLABLE API.
  839. ##########################################################################*/
  840. void flush_cache_sigtramp(unsigned long start, unsigned long end)
  841. {
  842. /* For the address range [start,end), write back the data from the
  843. D-cache and invalidate the corresponding region of the I-cache for
  844. the current process. Used to flush signal trampolines on the stack
  845. to make them executable. */
  846. sh64_dcache_wback_current_user_range(start, end);
  847. wmb();
  848. sh64_icache_inv_current_user_range(start, end);
  849. }