cache-sh5.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /*
  2. * arch/sh/mm/cache-sh5.c
  3. *
  4. * Copyright (C) 2000, 2001 Paolo Alberelli
  5. * Copyright (C) 2002 Benedict Gaster
  6. * Copyright (C) 2003 Richard Curnow
  7. * Copyright (C) 2003 - 2008 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 <asm/tlb.h>
  17. #include <asm/processor.h>
  18. #include <asm/cache.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/mmu_context.h>
  22. /* Wired TLB entry for the D-cache */
  23. static unsigned long long dtlb_cache_slot;
  24. void __init cpu_cache_init(void)
  25. {
  26. /* Reserve a slot for dcache colouring in the DTLB */
  27. dtlb_cache_slot = sh64_get_wired_dtlb_entry();
  28. }
  29. void __init kmap_coherent_init(void)
  30. {
  31. /* XXX ... */
  32. }
  33. void *kmap_coherent(struct page *page, unsigned long addr)
  34. {
  35. /* XXX ... */
  36. return NULL;
  37. }
  38. void kunmap_coherent(void)
  39. {
  40. }
  41. #ifdef CONFIG_DCACHE_DISABLED
  42. #define sh64_dcache_purge_all() do { } while (0)
  43. #define sh64_dcache_purge_coloured_phy_page(paddr, eaddr) do { } while (0)
  44. #define sh64_dcache_purge_user_range(mm, start, end) do { } while (0)
  45. #define sh64_dcache_purge_phy_page(paddr) do { } while (0)
  46. #define sh64_dcache_purge_virt_page(mm, eaddr) do { } while (0)
  47. #endif
  48. /*
  49. * The following group of functions deal with mapping and unmapping a
  50. * temporary page into a DTLB slot that has been set aside for exclusive
  51. * use.
  52. */
  53. static inline void
  54. sh64_setup_dtlb_cache_slot(unsigned long eaddr, unsigned long asid,
  55. unsigned long paddr)
  56. {
  57. local_irq_disable();
  58. sh64_setup_tlb_slot(dtlb_cache_slot, eaddr, asid, paddr);
  59. }
  60. static inline void sh64_teardown_dtlb_cache_slot(void)
  61. {
  62. sh64_teardown_tlb_slot(dtlb_cache_slot);
  63. local_irq_enable();
  64. }
  65. #ifndef CONFIG_ICACHE_DISABLED
  66. static inline void sh64_icache_inv_all(void)
  67. {
  68. unsigned long long addr, flag, data;
  69. unsigned long flags;
  70. addr = ICCR0;
  71. flag = ICCR0_ICI;
  72. data = 0;
  73. /* Make this a critical section for safety (probably not strictly necessary.) */
  74. local_irq_save(flags);
  75. /* Without %1 it gets unexplicably wrong */
  76. __asm__ __volatile__ (
  77. "getcfg %3, 0, %0\n\t"
  78. "or %0, %2, %0\n\t"
  79. "putcfg %3, 0, %0\n\t"
  80. "synci"
  81. : "=&r" (data)
  82. : "0" (data), "r" (flag), "r" (addr));
  83. local_irq_restore(flags);
  84. }
  85. static void sh64_icache_inv_kernel_range(unsigned long start, unsigned long end)
  86. {
  87. /* Invalidate range of addresses [start,end] from the I-cache, where
  88. * the addresses lie in the kernel superpage. */
  89. unsigned long long ullend, addr, aligned_start;
  90. aligned_start = (unsigned long long)(signed long long)(signed long) start;
  91. addr = L1_CACHE_ALIGN(aligned_start);
  92. ullend = (unsigned long long) (signed long long) (signed long) end;
  93. while (addr <= ullend) {
  94. __asm__ __volatile__ ("icbi %0, 0" : : "r" (addr));
  95. addr += L1_CACHE_BYTES;
  96. }
  97. }
  98. static void sh64_icache_inv_user_page(struct vm_area_struct *vma, unsigned long eaddr)
  99. {
  100. /* If we get called, we know that vma->vm_flags contains VM_EXEC.
  101. Also, eaddr is page-aligned. */
  102. unsigned int cpu = smp_processor_id();
  103. unsigned long long addr, end_addr;
  104. unsigned long flags = 0;
  105. unsigned long running_asid, vma_asid;
  106. addr = eaddr;
  107. end_addr = addr + PAGE_SIZE;
  108. /* Check whether we can use the current ASID for the I-cache
  109. invalidation. For example, if we're called via
  110. access_process_vm->flush_cache_page->here, (e.g. when reading from
  111. /proc), 'running_asid' will be that of the reader, not of the
  112. victim.
  113. Also, note the risk that we might get pre-empted between the ASID
  114. compare and blocking IRQs, and before we regain control, the
  115. pid->ASID mapping changes. However, the whole cache will get
  116. invalidated when the mapping is renewed, so the worst that can
  117. happen is that the loop below ends up invalidating somebody else's
  118. cache entries.
  119. */
  120. running_asid = get_asid();
  121. vma_asid = cpu_asid(cpu, vma->vm_mm);
  122. if (running_asid != vma_asid) {
  123. local_irq_save(flags);
  124. switch_and_save_asid(vma_asid);
  125. }
  126. while (addr < end_addr) {
  127. /* Worth unrolling a little */
  128. __asm__ __volatile__("icbi %0, 0" : : "r" (addr));
  129. __asm__ __volatile__("icbi %0, 32" : : "r" (addr));
  130. __asm__ __volatile__("icbi %0, 64" : : "r" (addr));
  131. __asm__ __volatile__("icbi %0, 96" : : "r" (addr));
  132. addr += 128;
  133. }
  134. if (running_asid != vma_asid) {
  135. switch_and_save_asid(running_asid);
  136. local_irq_restore(flags);
  137. }
  138. }
  139. static void sh64_icache_inv_user_page_range(struct mm_struct *mm,
  140. unsigned long start, unsigned long end)
  141. {
  142. /* Used for invalidating big chunks of I-cache, i.e. assume the range
  143. is whole pages. If 'start' or 'end' is not page aligned, the code
  144. is conservative and invalidates to the ends of the enclosing pages.
  145. This is functionally OK, just a performance loss. */
  146. /* See the comments below in sh64_dcache_purge_user_range() regarding
  147. the choice of algorithm. However, for the I-cache option (2) isn't
  148. available because there are no physical tags so aliases can't be
  149. resolved. The icbi instruction has to be used through the user
  150. mapping. Because icbi is cheaper than ocbp on a cache hit, it
  151. would be cheaper to use the selective code for a large range than is
  152. possible with the D-cache. Just assume 64 for now as a working
  153. figure.
  154. */
  155. int n_pages;
  156. if (!mm)
  157. return;
  158. n_pages = ((end - start) >> PAGE_SHIFT);
  159. if (n_pages >= 64) {
  160. sh64_icache_inv_all();
  161. } else {
  162. unsigned long aligned_start;
  163. unsigned long eaddr;
  164. unsigned long after_last_page_start;
  165. unsigned long mm_asid, current_asid;
  166. unsigned long flags = 0;
  167. mm_asid = cpu_asid(smp_processor_id(), mm);
  168. current_asid = get_asid();
  169. if (mm_asid != current_asid) {
  170. /* Switch ASID and run the invalidate loop under cli */
  171. local_irq_save(flags);
  172. switch_and_save_asid(mm_asid);
  173. }
  174. aligned_start = start & PAGE_MASK;
  175. after_last_page_start = PAGE_SIZE + ((end - 1) & PAGE_MASK);
  176. while (aligned_start < after_last_page_start) {
  177. struct vm_area_struct *vma;
  178. unsigned long vma_end;
  179. vma = find_vma(mm, aligned_start);
  180. if (!vma || (aligned_start <= vma->vm_end)) {
  181. /* Avoid getting stuck in an error condition */
  182. aligned_start += PAGE_SIZE;
  183. continue;
  184. }
  185. vma_end = vma->vm_end;
  186. if (vma->vm_flags & VM_EXEC) {
  187. /* Executable */
  188. eaddr = aligned_start;
  189. while (eaddr < vma_end) {
  190. sh64_icache_inv_user_page(vma, eaddr);
  191. eaddr += PAGE_SIZE;
  192. }
  193. }
  194. aligned_start = vma->vm_end; /* Skip to start of next region */
  195. }
  196. if (mm_asid != current_asid) {
  197. switch_and_save_asid(current_asid);
  198. local_irq_restore(flags);
  199. }
  200. }
  201. }
  202. /*
  203. * Invalidate a small range of user context I-cache, not necessarily page
  204. * (or even cache-line) aligned.
  205. *
  206. * Since this is used inside ptrace, the ASID in the mm context typically
  207. * won't match current_asid. We'll have to switch ASID to do this. For
  208. * safety, and given that the range will be small, do all this under cli.
  209. *
  210. * Note, there is a hazard that the ASID in mm->context is no longer
  211. * actually associated with mm, i.e. if the mm->context has started a new
  212. * cycle since mm was last active. However, this is just a performance
  213. * issue: all that happens is that we invalidate lines belonging to
  214. * another mm, so the owning process has to refill them when that mm goes
  215. * live again. mm itself can't have any cache entries because there will
  216. * have been a flush_cache_all when the new mm->context cycle started.
  217. */
  218. static void sh64_icache_inv_user_small_range(struct mm_struct *mm,
  219. unsigned long start, int len)
  220. {
  221. unsigned long long eaddr = start;
  222. unsigned long long eaddr_end = start + len;
  223. unsigned long current_asid, mm_asid;
  224. unsigned long flags;
  225. unsigned long long epage_start;
  226. /*
  227. * Align to start of cache line. Otherwise, suppose len==8 and
  228. * start was at 32N+28 : the last 4 bytes wouldn't get invalidated.
  229. */
  230. eaddr = L1_CACHE_ALIGN(start);
  231. eaddr_end = start + len;
  232. mm_asid = cpu_asid(smp_processor_id(), mm);
  233. local_irq_save(flags);
  234. current_asid = switch_and_save_asid(mm_asid);
  235. epage_start = eaddr & PAGE_MASK;
  236. while (eaddr < eaddr_end) {
  237. __asm__ __volatile__("icbi %0, 0" : : "r" (eaddr));
  238. eaddr += L1_CACHE_BYTES;
  239. }
  240. switch_and_save_asid(current_asid);
  241. local_irq_restore(flags);
  242. }
  243. static void sh64_icache_inv_current_user_range(unsigned long start, unsigned long end)
  244. {
  245. /* The icbi instruction never raises ITLBMISS. i.e. if there's not a
  246. cache hit on the virtual tag the instruction ends there, without a
  247. TLB lookup. */
  248. unsigned long long aligned_start;
  249. unsigned long long ull_end;
  250. unsigned long long addr;
  251. ull_end = end;
  252. /* Just invalidate over the range using the natural addresses. TLB
  253. miss handling will be OK (TBC). Since it's for the current process,
  254. either we're already in the right ASID context, or the ASIDs have
  255. been recycled since we were last active in which case we might just
  256. invalidate another processes I-cache entries : no worries, just a
  257. performance drop for him. */
  258. aligned_start = L1_CACHE_ALIGN(start);
  259. addr = aligned_start;
  260. while (addr < ull_end) {
  261. __asm__ __volatile__ ("icbi %0, 0" : : "r" (addr));
  262. __asm__ __volatile__ ("nop");
  263. __asm__ __volatile__ ("nop");
  264. addr += L1_CACHE_BYTES;
  265. }
  266. }
  267. #endif /* !CONFIG_ICACHE_DISABLED */
  268. #ifndef CONFIG_DCACHE_DISABLED
  269. /* Buffer used as the target of alloco instructions to purge data from cache
  270. sets by natural eviction. -- RPC */
  271. #define DUMMY_ALLOCO_AREA_SIZE ((L1_CACHE_BYTES << 10) + (1024 * 4))
  272. static unsigned char dummy_alloco_area[DUMMY_ALLOCO_AREA_SIZE] __cacheline_aligned = { 0, };
  273. static void inline sh64_dcache_purge_sets(int sets_to_purge_base, int n_sets)
  274. {
  275. /* Purge all ways in a particular block of sets, specified by the base
  276. set number and number of sets. Can handle wrap-around, if that's
  277. needed. */
  278. int dummy_buffer_base_set;
  279. unsigned long long eaddr, eaddr0, eaddr1;
  280. int j;
  281. int set_offset;
  282. dummy_buffer_base_set = ((int)&dummy_alloco_area &
  283. cpu_data->dcache.entry_mask) >>
  284. cpu_data->dcache.entry_shift;
  285. set_offset = sets_to_purge_base - dummy_buffer_base_set;
  286. for (j = 0; j < n_sets; j++, set_offset++) {
  287. set_offset &= (cpu_data->dcache.sets - 1);
  288. eaddr0 = (unsigned long long)dummy_alloco_area +
  289. (set_offset << cpu_data->dcache.entry_shift);
  290. /*
  291. * Do one alloco which hits the required set per cache
  292. * way. For write-back mode, this will purge the #ways
  293. * resident lines. There's little point unrolling this
  294. * loop because the allocos stall more if they're too
  295. * close together.
  296. */
  297. eaddr1 = eaddr0 + cpu_data->dcache.way_size *
  298. cpu_data->dcache.ways;
  299. for (eaddr = eaddr0; eaddr < eaddr1;
  300. eaddr += cpu_data->dcache.way_size) {
  301. __asm__ __volatile__ ("alloco %0, 0" : : "r" (eaddr));
  302. __asm__ __volatile__ ("synco"); /* TAKum03020 */
  303. }
  304. eaddr1 = eaddr0 + cpu_data->dcache.way_size *
  305. cpu_data->dcache.ways;
  306. for (eaddr = eaddr0; eaddr < eaddr1;
  307. eaddr += cpu_data->dcache.way_size) {
  308. /*
  309. * Load from each address. Required because
  310. * alloco is a NOP if the cache is write-through.
  311. */
  312. if (test_bit(SH_CACHE_MODE_WT, &(cpu_data->dcache.flags)))
  313. __raw_readb((unsigned long)eaddr);
  314. }
  315. }
  316. /*
  317. * Don't use OCBI to invalidate the lines. That costs cycles
  318. * directly. If the dummy block is just left resident, it will
  319. * naturally get evicted as required.
  320. */
  321. }
  322. /*
  323. * Purge the entire contents of the dcache. The most efficient way to
  324. * achieve this is to use alloco instructions on a region of unused
  325. * memory equal in size to the cache, thereby causing the current
  326. * contents to be discarded by natural eviction. The alternative, namely
  327. * reading every tag, setting up a mapping for the corresponding page and
  328. * doing an OCBP for the line, would be much more expensive.
  329. */
  330. static void sh64_dcache_purge_all(void)
  331. {
  332. sh64_dcache_purge_sets(0, cpu_data->dcache.sets);
  333. }
  334. /* Assumes this address (+ (2**n_synbits) pages up from it) aren't used for
  335. anything else in the kernel */
  336. #define MAGIC_PAGE0_START 0xffffffffec000000ULL
  337. /* Purge the physical page 'paddr' from the cache. It's known that any
  338. * cache lines requiring attention have the same page colour as the the
  339. * address 'eaddr'.
  340. *
  341. * This relies on the fact that the D-cache matches on physical tags when
  342. * no virtual tag matches. So we create an alias for the original page
  343. * and purge through that. (Alternatively, we could have done this by
  344. * switching ASID to match the original mapping and purged through that,
  345. * but that involves ASID switching cost + probably a TLBMISS + refill
  346. * anyway.)
  347. */
  348. static void sh64_dcache_purge_coloured_phy_page(unsigned long paddr,
  349. unsigned long eaddr)
  350. {
  351. unsigned long long magic_page_start;
  352. unsigned long long magic_eaddr, magic_eaddr_end;
  353. magic_page_start = MAGIC_PAGE0_START + (eaddr & CACHE_OC_SYN_MASK);
  354. /* As long as the kernel is not pre-emptible, this doesn't need to be
  355. under cli/sti. */
  356. sh64_setup_dtlb_cache_slot(magic_page_start, get_asid(), paddr);
  357. magic_eaddr = magic_page_start;
  358. magic_eaddr_end = magic_eaddr + PAGE_SIZE;
  359. while (magic_eaddr < magic_eaddr_end) {
  360. /* Little point in unrolling this loop - the OCBPs are blocking
  361. and won't go any quicker (i.e. the loop overhead is parallel
  362. to part of the OCBP execution.) */
  363. __asm__ __volatile__ ("ocbp %0, 0" : : "r" (magic_eaddr));
  364. magic_eaddr += L1_CACHE_BYTES;
  365. }
  366. sh64_teardown_dtlb_cache_slot();
  367. }
  368. /*
  369. * Purge a page given its physical start address, by creating a temporary
  370. * 1 page mapping and purging across that. Even if we know the virtual
  371. * address (& vma or mm) of the page, the method here is more elegant
  372. * because it avoids issues of coping with page faults on the purge
  373. * instructions (i.e. no special-case code required in the critical path
  374. * in the TLB miss handling).
  375. */
  376. static void sh64_dcache_purge_phy_page(unsigned long paddr)
  377. {
  378. unsigned long long eaddr_start, eaddr, eaddr_end;
  379. int i;
  380. /* As long as the kernel is not pre-emptible, this doesn't need to be
  381. under cli/sti. */
  382. eaddr_start = MAGIC_PAGE0_START;
  383. for (i = 0; i < (1 << CACHE_OC_N_SYNBITS); i++) {
  384. sh64_setup_dtlb_cache_slot(eaddr_start, get_asid(), paddr);
  385. eaddr = eaddr_start;
  386. eaddr_end = eaddr + PAGE_SIZE;
  387. while (eaddr < eaddr_end) {
  388. __asm__ __volatile__ ("ocbp %0, 0" : : "r" (eaddr));
  389. eaddr += L1_CACHE_BYTES;
  390. }
  391. sh64_teardown_dtlb_cache_slot();
  392. eaddr_start += PAGE_SIZE;
  393. }
  394. }
  395. static void sh64_dcache_purge_user_pages(struct mm_struct *mm,
  396. unsigned long addr, unsigned long end)
  397. {
  398. pgd_t *pgd;
  399. pud_t *pud;
  400. pmd_t *pmd;
  401. pte_t *pte;
  402. pte_t entry;
  403. spinlock_t *ptl;
  404. unsigned long paddr;
  405. if (!mm)
  406. return; /* No way to find physical address of page */
  407. pgd = pgd_offset(mm, addr);
  408. if (pgd_bad(*pgd))
  409. return;
  410. pud = pud_offset(pgd, addr);
  411. if (pud_none(*pud) || pud_bad(*pud))
  412. return;
  413. pmd = pmd_offset(pud, addr);
  414. if (pmd_none(*pmd) || pmd_bad(*pmd))
  415. return;
  416. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  417. do {
  418. entry = *pte;
  419. if (pte_none(entry) || !pte_present(entry))
  420. continue;
  421. paddr = pte_val(entry) & PAGE_MASK;
  422. sh64_dcache_purge_coloured_phy_page(paddr, addr);
  423. } while (pte++, addr += PAGE_SIZE, addr != end);
  424. pte_unmap_unlock(pte - 1, ptl);
  425. }
  426. /*
  427. * There are at least 5 choices for the implementation of this, with
  428. * pros (+), cons(-), comments(*):
  429. *
  430. * 1. ocbp each line in the range through the original user's ASID
  431. * + no lines spuriously evicted
  432. * - tlbmiss handling (must either handle faults on demand => extra
  433. * special-case code in tlbmiss critical path), or map the page in
  434. * advance (=> flush_tlb_range in advance to avoid multiple hits)
  435. * - ASID switching
  436. * - expensive for large ranges
  437. *
  438. * 2. temporarily map each page in the range to a special effective
  439. * address and ocbp through the temporary mapping; relies on the
  440. * fact that SH-5 OCB* always do TLB lookup and match on ptags (they
  441. * never look at the etags)
  442. * + no spurious evictions
  443. * - expensive for large ranges
  444. * * surely cheaper than (1)
  445. *
  446. * 3. walk all the lines in the cache, check the tags, if a match
  447. * occurs create a page mapping to ocbp the line through
  448. * + no spurious evictions
  449. * - tag inspection overhead
  450. * - (especially for small ranges)
  451. * - potential cost of setting up/tearing down page mapping for
  452. * every line that matches the range
  453. * * cost partly independent of range size
  454. *
  455. * 4. walk all the lines in the cache, check the tags, if a match
  456. * occurs use 4 * alloco to purge the line (+3 other probably
  457. * innocent victims) by natural eviction
  458. * + no tlb mapping overheads
  459. * - spurious evictions
  460. * - tag inspection overhead
  461. *
  462. * 5. implement like flush_cache_all
  463. * + no tag inspection overhead
  464. * - spurious evictions
  465. * - bad for small ranges
  466. *
  467. * (1) can be ruled out as more expensive than (2). (2) appears best
  468. * for small ranges. The choice between (3), (4) and (5) for large
  469. * ranges and the range size for the large/small boundary need
  470. * benchmarking to determine.
  471. *
  472. * For now use approach (2) for small ranges and (5) for large ones.
  473. */
  474. static void sh64_dcache_purge_user_range(struct mm_struct *mm,
  475. unsigned long start, unsigned long end)
  476. {
  477. int n_pages = ((end - start) >> PAGE_SHIFT);
  478. if (n_pages >= 64 || ((start ^ (end - 1)) & PMD_MASK)) {
  479. sh64_dcache_purge_all();
  480. } else {
  481. /* Small range, covered by a single page table page */
  482. start &= PAGE_MASK; /* should already be so */
  483. end = PAGE_ALIGN(end); /* should already be so */
  484. sh64_dcache_purge_user_pages(mm, start, end);
  485. }
  486. }
  487. #endif /* !CONFIG_DCACHE_DISABLED */
  488. /*
  489. * Invalidate the entire contents of both caches, after writing back to
  490. * memory any dirty data from the D-cache.
  491. */
  492. void flush_cache_all(void)
  493. {
  494. sh64_dcache_purge_all();
  495. sh64_icache_inv_all();
  496. }
  497. /*
  498. * Invalidate an entire user-address space from both caches, after
  499. * writing back dirty data (e.g. for shared mmap etc).
  500. *
  501. * This could be coded selectively by inspecting all the tags then
  502. * doing 4*alloco on any set containing a match (as for
  503. * flush_cache_range), but fork/exit/execve (where this is called from)
  504. * are expensive anyway.
  505. *
  506. * Have to do a purge here, despite the comments re I-cache below.
  507. * There could be odd-coloured dirty data associated with the mm still
  508. * in the cache - if this gets written out through natural eviction
  509. * after the kernel has reused the page there will be chaos.
  510. *
  511. * The mm being torn down won't ever be active again, so any Icache
  512. * lines tagged with its ASID won't be visible for the rest of the
  513. * lifetime of this ASID cycle. Before the ASID gets reused, there
  514. * will be a flush_cache_all. Hence we don't need to touch the
  515. * I-cache. This is similar to the lack of action needed in
  516. * flush_tlb_mm - see fault.c.
  517. */
  518. void flush_cache_mm(struct mm_struct *mm)
  519. {
  520. sh64_dcache_purge_all();
  521. }
  522. /*
  523. * Invalidate (from both caches) the range [start,end) of virtual
  524. * addresses from the user address space specified by mm, after writing
  525. * back any dirty data.
  526. *
  527. * Note, 'end' is 1 byte beyond the end of the range to flush.
  528. */
  529. void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
  530. unsigned long end)
  531. {
  532. struct mm_struct *mm = vma->vm_mm;
  533. sh64_dcache_purge_user_range(mm, start, end);
  534. sh64_icache_inv_user_page_range(mm, start, end);
  535. }
  536. /*
  537. * Invalidate any entries in either cache for the vma within the user
  538. * address space vma->vm_mm for the page starting at virtual address
  539. * 'eaddr'. This seems to be used primarily in breaking COW. Note,
  540. * the I-cache must be searched too in case the page in question is
  541. * both writable and being executed from (e.g. stack trampolines.)
  542. *
  543. * Note, this is called with pte lock held.
  544. */
  545. void flush_cache_page(struct vm_area_struct *vma, unsigned long eaddr,
  546. unsigned long pfn)
  547. {
  548. sh64_dcache_purge_phy_page(pfn << PAGE_SHIFT);
  549. if (vma->vm_flags & VM_EXEC)
  550. sh64_icache_inv_user_page(vma, eaddr);
  551. }
  552. void flush_dcache_page(struct page *page)
  553. {
  554. sh64_dcache_purge_phy_page(page_to_phys(page));
  555. wmb();
  556. }
  557. /*
  558. * Flush the range [start,end] of kernel virtual adddress space from
  559. * the I-cache. The corresponding range must be purged from the
  560. * D-cache also because the SH-5 doesn't have cache snooping between
  561. * the caches. The addresses will be visible through the superpage
  562. * mapping, therefore it's guaranteed that there no cache entries for
  563. * the range in cache sets of the wrong colour.
  564. */
  565. void flush_icache_range(unsigned long start, unsigned long end)
  566. {
  567. __flush_purge_region((void *)start, end);
  568. wmb();
  569. sh64_icache_inv_kernel_range(start, end);
  570. }
  571. /*
  572. * Flush the range of user (defined by vma->vm_mm) address space starting
  573. * at 'addr' for 'len' bytes from the cache. The range does not straddle
  574. * a page boundary, the unique physical page containing the range is
  575. * 'page'. This seems to be used mainly for invalidating an address
  576. * range following a poke into the program text through the ptrace() call
  577. * from another process (e.g. for BRK instruction insertion).
  578. */
  579. void flush_icache_user_range(struct vm_area_struct *vma,
  580. struct page *page, unsigned long addr, int len)
  581. {
  582. sh64_dcache_purge_coloured_phy_page(page_to_phys(page), addr);
  583. mb();
  584. if (vma->vm_flags & VM_EXEC)
  585. sh64_icache_inv_user_small_range(vma->vm_mm, addr, len);
  586. }
  587. /*
  588. * For the address range [start,end), write back the data from the
  589. * D-cache and invalidate the corresponding region of the I-cache for the
  590. * current process. Used to flush signal trampolines on the stack to
  591. * make them executable.
  592. */
  593. void flush_cache_sigtramp(unsigned long vaddr)
  594. {
  595. unsigned long end = vaddr + L1_CACHE_BYTES;
  596. __flush_wback_region((void *)vaddr, L1_CACHE_BYTES);
  597. wmb();
  598. sh64_icache_inv_current_user_range(vaddr, end);
  599. }
  600. #ifdef CONFIG_MMU
  601. /*
  602. * These *MUST* lie in an area of virtual address space that's otherwise
  603. * unused.
  604. */
  605. #define UNIQUE_EADDR_START 0xe0000000UL
  606. #define UNIQUE_EADDR_END 0xe8000000UL
  607. /*
  608. * Given a physical address paddr, and a user virtual address user_eaddr
  609. * which will eventually be mapped to it, create a one-off kernel-private
  610. * eaddr mapped to the same paddr. This is used for creating special
  611. * destination pages for copy_user_page and clear_user_page.
  612. */
  613. static unsigned long sh64_make_unique_eaddr(unsigned long user_eaddr,
  614. unsigned long paddr)
  615. {
  616. static unsigned long current_pointer = UNIQUE_EADDR_START;
  617. unsigned long coloured_pointer;
  618. if (current_pointer == UNIQUE_EADDR_END) {
  619. sh64_dcache_purge_all();
  620. current_pointer = UNIQUE_EADDR_START;
  621. }
  622. coloured_pointer = (current_pointer & ~CACHE_OC_SYN_MASK) |
  623. (user_eaddr & CACHE_OC_SYN_MASK);
  624. sh64_setup_dtlb_cache_slot(coloured_pointer, get_asid(), paddr);
  625. current_pointer += (PAGE_SIZE << CACHE_OC_N_SYNBITS);
  626. return coloured_pointer;
  627. }
  628. static void sh64_copy_user_page_coloured(void *to, void *from,
  629. unsigned long address)
  630. {
  631. void *coloured_to;
  632. /*
  633. * Discard any existing cache entries of the wrong colour. These are
  634. * present quite often, if the kernel has recently used the page
  635. * internally, then given it up, then it's been allocated to the user.
  636. */
  637. sh64_dcache_purge_coloured_phy_page(__pa(to), (unsigned long)to);
  638. coloured_to = (void *)sh64_make_unique_eaddr(address, __pa(to));
  639. copy_page(from, coloured_to);
  640. sh64_teardown_dtlb_cache_slot();
  641. }
  642. static void sh64_clear_user_page_coloured(void *to, unsigned long address)
  643. {
  644. void *coloured_to;
  645. /*
  646. * Discard any existing kernel-originated lines of the wrong
  647. * colour (as above)
  648. */
  649. sh64_dcache_purge_coloured_phy_page(__pa(to), (unsigned long)to);
  650. coloured_to = (void *)sh64_make_unique_eaddr(address, __pa(to));
  651. clear_page(coloured_to);
  652. sh64_teardown_dtlb_cache_slot();
  653. }
  654. /*
  655. * 'from' and 'to' are kernel virtual addresses (within the superpage
  656. * mapping of the physical RAM). 'address' is the user virtual address
  657. * where the copy 'to' will be mapped after. This allows a custom
  658. * mapping to be used to ensure that the new copy is placed in the
  659. * right cache sets for the user to see it without having to bounce it
  660. * out via memory. Note however : the call to flush_page_to_ram in
  661. * (generic)/mm/memory.c:(break_cow) undoes all this good work in that one
  662. * very important case!
  663. *
  664. * TBD : can we guarantee that on every call, any cache entries for
  665. * 'from' are in the same colour sets as 'address' also? i.e. is this
  666. * always used just to deal with COW? (I suspect not).
  667. *
  668. * There are two possibilities here for when the page 'from' was last accessed:
  669. * - by the kernel : this is OK, no purge required.
  670. * - by the/a user (e.g. for break_COW) : need to purge.
  671. *
  672. * If the potential user mapping at 'address' is the same colour as
  673. * 'from' there is no need to purge any cache lines from the 'from'
  674. * page mapped into cache sets of colour 'address'. (The copy will be
  675. * accessing the page through 'from').
  676. */
  677. void copy_user_page(void *to, void *from, unsigned long address,
  678. struct page *page)
  679. {
  680. if (((address ^ (unsigned long) from) & CACHE_OC_SYN_MASK) != 0)
  681. sh64_dcache_purge_coloured_phy_page(__pa(from), address);
  682. if (((address ^ (unsigned long) to) & CACHE_OC_SYN_MASK) == 0)
  683. copy_page(to, from);
  684. else
  685. sh64_copy_user_page_coloured(to, from, address);
  686. }
  687. /*
  688. * 'to' is a kernel virtual address (within the superpage mapping of the
  689. * physical RAM). 'address' is the user virtual address where the 'to'
  690. * page will be mapped after. This allows a custom mapping to be used to
  691. * ensure that the new copy is placed in the right cache sets for the
  692. * user to see it without having to bounce it out via memory.
  693. */
  694. void clear_user_page(void *to, unsigned long address, struct page *page)
  695. {
  696. if (((address ^ (unsigned long) to) & CACHE_OC_SYN_MASK) == 0)
  697. clear_page(to);
  698. else
  699. sh64_clear_user_page_coloured(to, address);
  700. }
  701. void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
  702. unsigned long vaddr, void *dst, const void *src,
  703. unsigned long len)
  704. {
  705. flush_cache_page(vma, vaddr, page_to_pfn(page));
  706. memcpy(dst, src, len);
  707. flush_icache_user_range(vma, page, vaddr, len);
  708. }
  709. void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
  710. unsigned long vaddr, void *dst, const void *src,
  711. unsigned long len)
  712. {
  713. flush_cache_page(vma, vaddr, page_to_pfn(page));
  714. memcpy(dst, src, len);
  715. }
  716. #endif