cache.c 35 KB

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