cache-sh5.c 27 KB

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