cache-sh5.c 26 KB

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