cache-sh5.c 35 KB

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