/*1* arch/sh/mm/cache-sh5.c2*3* Copyright (C) 2000, 2001 Paolo Alberelli4* Copyright (C) 2002 Benedict Gaster5* Copyright (C) 2003 Richard Curnow6* Copyright (C) 2003 - 2008 Paul Mundt7*8* This file is subject to the terms and conditions of the GNU General Public9* License. See the file "COPYING" in the main directory of this archive10* for more details.11*/12#include <linux/init.h>13#include <linux/mman.h>14#include <linux/mm.h>15#include <asm/tlb.h>16#include <asm/processor.h>17#include <asm/cache.h>18#include <asm/pgalloc.h>19#include <asm/uaccess.h>20#include <asm/mmu_context.h>2122extern void __weak sh4__flush_region_init(void);2324/* Wired TLB entry for the D-cache */25static unsigned long long dtlb_cache_slot;2627/*28* The following group of functions deal with mapping and unmapping a29* temporary page into a DTLB slot that has been set aside for exclusive30* use.31*/32static inline void33sh64_setup_dtlb_cache_slot(unsigned long eaddr, unsigned long asid,34unsigned long paddr)35{36local_irq_disable();37sh64_setup_tlb_slot(dtlb_cache_slot, eaddr, asid, paddr);38}3940static inline void sh64_teardown_dtlb_cache_slot(void)41{42sh64_teardown_tlb_slot(dtlb_cache_slot);43local_irq_enable();44}4546static inline void sh64_icache_inv_all(void)47{48unsigned long long addr, flag, data;49unsigned long flags;5051addr = ICCR0;52flag = ICCR0_ICI;53data = 0;5455/* Make this a critical section for safety (probably not strictly necessary.) */56local_irq_save(flags);5758/* Without %1 it gets unexplicably wrong */59__asm__ __volatile__ (60"getcfg %3, 0, %0\n\t"61"or %0, %2, %0\n\t"62"putcfg %3, 0, %0\n\t"63"synci"64: "=&r" (data)65: "0" (data), "r" (flag), "r" (addr));6667local_irq_restore(flags);68}6970static void sh64_icache_inv_kernel_range(unsigned long start, unsigned long end)71{72/* Invalidate range of addresses [start,end] from the I-cache, where73* the addresses lie in the kernel superpage. */7475unsigned long long ullend, addr, aligned_start;76aligned_start = (unsigned long long)(signed long long)(signed long) start;77addr = L1_CACHE_ALIGN(aligned_start);78ullend = (unsigned long long) (signed long long) (signed long) end;7980while (addr <= ullend) {81__asm__ __volatile__ ("icbi %0, 0" : : "r" (addr));82addr += L1_CACHE_BYTES;83}84}8586static 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.89Also, eaddr is page-aligned. */90unsigned int cpu = smp_processor_id();91unsigned long long addr, end_addr;92unsigned long flags = 0;93unsigned long running_asid, vma_asid;94addr = eaddr;95end_addr = addr + PAGE_SIZE;9697/* Check whether we can use the current ASID for the I-cache98invalidation. For example, if we're called via99access_process_vm->flush_cache_page->here, (e.g. when reading from100/proc), 'running_asid' will be that of the reader, not of the101victim.102103Also, note the risk that we might get pre-empted between the ASID104compare and blocking IRQs, and before we regain control, the105pid->ASID mapping changes. However, the whole cache will get106invalidated when the mapping is renewed, so the worst that can107happen is that the loop below ends up invalidating somebody else's108cache entries.109*/110111running_asid = get_asid();112vma_asid = cpu_asid(cpu, vma->vm_mm);113if (running_asid != vma_asid) {114local_irq_save(flags);115switch_and_save_asid(vma_asid);116}117while (addr < end_addr) {118/* Worth unrolling a little */119__asm__ __volatile__("icbi %0, 0" : : "r" (addr));120__asm__ __volatile__("icbi %0, 32" : : "r" (addr));121__asm__ __volatile__("icbi %0, 64" : : "r" (addr));122__asm__ __volatile__("icbi %0, 96" : : "r" (addr));123addr += 128;124}125if (running_asid != vma_asid) {126switch_and_save_asid(running_asid);127local_irq_restore(flags);128}129}130131static void sh64_icache_inv_user_page_range(struct mm_struct *mm,132unsigned long start, unsigned long end)133{134/* Used for invalidating big chunks of I-cache, i.e. assume the range135is whole pages. If 'start' or 'end' is not page aligned, the code136is conservative and invalidates to the ends of the enclosing pages.137This is functionally OK, just a performance loss. */138139/* See the comments below in sh64_dcache_purge_user_range() regarding140the choice of algorithm. However, for the I-cache option (2) isn't141available because there are no physical tags so aliases can't be142resolved. The icbi instruction has to be used through the user143mapping. Because icbi is cheaper than ocbp on a cache hit, it144would be cheaper to use the selective code for a large range than is145possible with the D-cache. Just assume 64 for now as a working146figure.147*/148int n_pages;149150if (!mm)151return;152153n_pages = ((end - start) >> PAGE_SHIFT);154if (n_pages >= 64) {155sh64_icache_inv_all();156} else {157unsigned long aligned_start;158unsigned long eaddr;159unsigned long after_last_page_start;160unsigned long mm_asid, current_asid;161unsigned long flags = 0;162163mm_asid = cpu_asid(smp_processor_id(), mm);164current_asid = get_asid();165166if (mm_asid != current_asid) {167/* Switch ASID and run the invalidate loop under cli */168local_irq_save(flags);169switch_and_save_asid(mm_asid);170}171172aligned_start = start & PAGE_MASK;173after_last_page_start = PAGE_SIZE + ((end - 1) & PAGE_MASK);174175while (aligned_start < after_last_page_start) {176struct vm_area_struct *vma;177unsigned long vma_end;178vma = find_vma(mm, aligned_start);179if (!vma || (aligned_start <= vma->vm_end)) {180/* Avoid getting stuck in an error condition */181aligned_start += PAGE_SIZE;182continue;183}184vma_end = vma->vm_end;185if (vma->vm_flags & VM_EXEC) {186/* Executable */187eaddr = aligned_start;188while (eaddr < vma_end) {189sh64_icache_inv_user_page(vma, eaddr);190eaddr += PAGE_SIZE;191}192}193aligned_start = vma->vm_end; /* Skip to start of next region */194}195196if (mm_asid != current_asid) {197switch_and_save_asid(current_asid);198local_irq_restore(flags);199}200}201}202203static void sh64_icache_inv_current_user_range(unsigned long start, unsigned long end)204{205/* The icbi instruction never raises ITLBMISS. i.e. if there's not a206cache hit on the virtual tag the instruction ends there, without a207TLB lookup. */208209unsigned long long aligned_start;210unsigned long long ull_end;211unsigned long long addr;212213ull_end = end;214215/* Just invalidate over the range using the natural addresses. TLB216miss handling will be OK (TBC). Since it's for the current process,217either we're already in the right ASID context, or the ASIDs have218been recycled since we were last active in which case we might just219invalidate another processes I-cache entries : no worries, just a220performance drop for him. */221aligned_start = L1_CACHE_ALIGN(start);222addr = aligned_start;223while (addr < ull_end) {224__asm__ __volatile__ ("icbi %0, 0" : : "r" (addr));225__asm__ __volatile__ ("nop");226__asm__ __volatile__ ("nop");227addr += L1_CACHE_BYTES;228}229}230231/* Buffer used as the target of alloco instructions to purge data from cache232sets by natural eviction. -- RPC */233#define DUMMY_ALLOCO_AREA_SIZE ((L1_CACHE_BYTES << 10) + (1024 * 4))234static unsigned char dummy_alloco_area[DUMMY_ALLOCO_AREA_SIZE] __cacheline_aligned = { 0, };235236static void inline sh64_dcache_purge_sets(int sets_to_purge_base, int n_sets)237{238/* Purge all ways in a particular block of sets, specified by the base239set number and number of sets. Can handle wrap-around, if that's240needed. */241242int dummy_buffer_base_set;243unsigned long long eaddr, eaddr0, eaddr1;244int j;245int set_offset;246247dummy_buffer_base_set = ((int)&dummy_alloco_area &248cpu_data->dcache.entry_mask) >>249cpu_data->dcache.entry_shift;250set_offset = sets_to_purge_base - dummy_buffer_base_set;251252for (j = 0; j < n_sets; j++, set_offset++) {253set_offset &= (cpu_data->dcache.sets - 1);254eaddr0 = (unsigned long long)dummy_alloco_area +255(set_offset << cpu_data->dcache.entry_shift);256257/*258* Do one alloco which hits the required set per cache259* way. For write-back mode, this will purge the #ways260* resident lines. There's little point unrolling this261* loop because the allocos stall more if they're too262* close together.263*/264eaddr1 = eaddr0 + cpu_data->dcache.way_size *265cpu_data->dcache.ways;266267for (eaddr = eaddr0; eaddr < eaddr1;268eaddr += cpu_data->dcache.way_size) {269__asm__ __volatile__ ("alloco %0, 0" : : "r" (eaddr));270__asm__ __volatile__ ("synco"); /* TAKum03020 */271}272273eaddr1 = eaddr0 + cpu_data->dcache.way_size *274cpu_data->dcache.ways;275276for (eaddr = eaddr0; eaddr < eaddr1;277eaddr += cpu_data->dcache.way_size) {278/*279* Load from each address. Required because280* alloco is a NOP if the cache is write-through.281*/282if (test_bit(SH_CACHE_MODE_WT, &(cpu_data->dcache.flags)))283__raw_readb((unsigned long)eaddr);284}285}286287/*288* Don't use OCBI to invalidate the lines. That costs cycles289* directly. If the dummy block is just left resident, it will290* naturally get evicted as required.291*/292}293294/*295* Purge the entire contents of the dcache. The most efficient way to296* achieve this is to use alloco instructions on a region of unused297* memory equal in size to the cache, thereby causing the current298* contents to be discarded by natural eviction. The alternative, namely299* reading every tag, setting up a mapping for the corresponding page and300* doing an OCBP for the line, would be much more expensive.301*/302static void sh64_dcache_purge_all(void)303{304305sh64_dcache_purge_sets(0, cpu_data->dcache.sets);306}307308309/* Assumes this address (+ (2**n_synbits) pages up from it) aren't used for310anything else in the kernel */311#define MAGIC_PAGE0_START 0xffffffffec000000ULL312313/* Purge the physical page 'paddr' from the cache. It's known that any314* cache lines requiring attention have the same page colour as the the315* address 'eaddr'.316*317* This relies on the fact that the D-cache matches on physical tags when318* no virtual tag matches. So we create an alias for the original page319* and purge through that. (Alternatively, we could have done this by320* switching ASID to match the original mapping and purged through that,321* but that involves ASID switching cost + probably a TLBMISS + refill322* anyway.)323*/324static void sh64_dcache_purge_coloured_phy_page(unsigned long paddr,325unsigned long eaddr)326{327unsigned long long magic_page_start;328unsigned long long magic_eaddr, magic_eaddr_end;329330magic_page_start = MAGIC_PAGE0_START + (eaddr & CACHE_OC_SYN_MASK);331332/* As long as the kernel is not pre-emptible, this doesn't need to be333under cli/sti. */334sh64_setup_dtlb_cache_slot(magic_page_start, get_asid(), paddr);335336magic_eaddr = magic_page_start;337magic_eaddr_end = magic_eaddr + PAGE_SIZE;338339while (magic_eaddr < magic_eaddr_end) {340/* Little point in unrolling this loop - the OCBPs are blocking341and won't go any quicker (i.e. the loop overhead is parallel342to part of the OCBP execution.) */343__asm__ __volatile__ ("ocbp %0, 0" : : "r" (magic_eaddr));344magic_eaddr += L1_CACHE_BYTES;345}346347sh64_teardown_dtlb_cache_slot();348}349350/*351* Purge a page given its physical start address, by creating a temporary352* 1 page mapping and purging across that. Even if we know the virtual353* address (& vma or mm) of the page, the method here is more elegant354* because it avoids issues of coping with page faults on the purge355* instructions (i.e. no special-case code required in the critical path356* in the TLB miss handling).357*/358static void sh64_dcache_purge_phy_page(unsigned long paddr)359{360unsigned long long eaddr_start, eaddr, eaddr_end;361int i;362363/* As long as the kernel is not pre-emptible, this doesn't need to be364under cli/sti. */365eaddr_start = MAGIC_PAGE0_START;366for (i = 0; i < (1 << CACHE_OC_N_SYNBITS); i++) {367sh64_setup_dtlb_cache_slot(eaddr_start, get_asid(), paddr);368369eaddr = eaddr_start;370eaddr_end = eaddr + PAGE_SIZE;371while (eaddr < eaddr_end) {372__asm__ __volatile__ ("ocbp %0, 0" : : "r" (eaddr));373eaddr += L1_CACHE_BYTES;374}375376sh64_teardown_dtlb_cache_slot();377eaddr_start += PAGE_SIZE;378}379}380381static void sh64_dcache_purge_user_pages(struct mm_struct *mm,382unsigned long addr, unsigned long end)383{384pgd_t *pgd;385pud_t *pud;386pmd_t *pmd;387pte_t *pte;388pte_t entry;389spinlock_t *ptl;390unsigned long paddr;391392if (!mm)393return; /* No way to find physical address of page */394395pgd = pgd_offset(mm, addr);396if (pgd_bad(*pgd))397return;398399pud = pud_offset(pgd, addr);400if (pud_none(*pud) || pud_bad(*pud))401return;402403pmd = pmd_offset(pud, addr);404if (pmd_none(*pmd) || pmd_bad(*pmd))405return;406407pte = pte_offset_map_lock(mm, pmd, addr, &ptl);408do {409entry = *pte;410if (pte_none(entry) || !pte_present(entry))411continue;412paddr = pte_val(entry) & PAGE_MASK;413sh64_dcache_purge_coloured_phy_page(paddr, addr);414} while (pte++, addr += PAGE_SIZE, addr != end);415pte_unmap_unlock(pte - 1, ptl);416}417418/*419* There are at least 5 choices for the implementation of this, with420* pros (+), cons(-), comments(*):421*422* 1. ocbp each line in the range through the original user's ASID423* + no lines spuriously evicted424* - tlbmiss handling (must either handle faults on demand => extra425* special-case code in tlbmiss critical path), or map the page in426* advance (=> flush_tlb_range in advance to avoid multiple hits)427* - ASID switching428* - expensive for large ranges429*430* 2. temporarily map each page in the range to a special effective431* address and ocbp through the temporary mapping; relies on the432* fact that SH-5 OCB* always do TLB lookup and match on ptags (they433* never look at the etags)434* + no spurious evictions435* - expensive for large ranges436* * surely cheaper than (1)437*438* 3. walk all the lines in the cache, check the tags, if a match439* occurs create a page mapping to ocbp the line through440* + no spurious evictions441* - tag inspection overhead442* - (especially for small ranges)443* - potential cost of setting up/tearing down page mapping for444* every line that matches the range445* * cost partly independent of range size446*447* 4. walk all the lines in the cache, check the tags, if a match448* occurs use 4 * alloco to purge the line (+3 other probably449* innocent victims) by natural eviction450* + no tlb mapping overheads451* - spurious evictions452* - tag inspection overhead453*454* 5. implement like flush_cache_all455* + no tag inspection overhead456* - spurious evictions457* - bad for small ranges458*459* (1) can be ruled out as more expensive than (2). (2) appears best460* for small ranges. The choice between (3), (4) and (5) for large461* ranges and the range size for the large/small boundary need462* benchmarking to determine.463*464* For now use approach (2) for small ranges and (5) for large ones.465*/466static void sh64_dcache_purge_user_range(struct mm_struct *mm,467unsigned long start, unsigned long end)468{469int n_pages = ((end - start) >> PAGE_SHIFT);470471if (n_pages >= 64 || ((start ^ (end - 1)) & PMD_MASK)) {472sh64_dcache_purge_all();473} else {474/* Small range, covered by a single page table page */475start &= PAGE_MASK; /* should already be so */476end = PAGE_ALIGN(end); /* should already be so */477sh64_dcache_purge_user_pages(mm, start, end);478}479}480481/*482* Invalidate the entire contents of both caches, after writing back to483* memory any dirty data from the D-cache.484*/485static void sh5_flush_cache_all(void *unused)486{487sh64_dcache_purge_all();488sh64_icache_inv_all();489}490491/*492* Invalidate an entire user-address space from both caches, after493* writing back dirty data (e.g. for shared mmap etc).494*495* This could be coded selectively by inspecting all the tags then496* doing 4*alloco on any set containing a match (as for497* flush_cache_range), but fork/exit/execve (where this is called from)498* are expensive anyway.499*500* Have to do a purge here, despite the comments re I-cache below.501* There could be odd-coloured dirty data associated with the mm still502* in the cache - if this gets written out through natural eviction503* after the kernel has reused the page there will be chaos.504*505* The mm being torn down won't ever be active again, so any Icache506* lines tagged with its ASID won't be visible for the rest of the507* lifetime of this ASID cycle. Before the ASID gets reused, there508* will be a flush_cache_all. Hence we don't need to touch the509* I-cache. This is similar to the lack of action needed in510* flush_tlb_mm - see fault.c.511*/512static void sh5_flush_cache_mm(void *unused)513{514sh64_dcache_purge_all();515}516517/*518* Invalidate (from both caches) the range [start,end) of virtual519* addresses from the user address space specified by mm, after writing520* back any dirty data.521*522* Note, 'end' is 1 byte beyond the end of the range to flush.523*/524static void sh5_flush_cache_range(void *args)525{526struct flusher_data *data = args;527struct vm_area_struct *vma;528unsigned long start, end;529530vma = data->vma;531start = data->addr1;532end = data->addr2;533534sh64_dcache_purge_user_range(vma->vm_mm, start, end);535sh64_icache_inv_user_page_range(vma->vm_mm, start, end);536}537538/*539* Invalidate any entries in either cache for the vma within the user540* address space vma->vm_mm for the page starting at virtual address541* '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 is543* both writable and being executed from (e.g. stack trampolines.)544*545* Note, this is called with pte lock held.546*/547static void sh5_flush_cache_page(void *args)548{549struct flusher_data *data = args;550struct vm_area_struct *vma;551unsigned long eaddr, pfn;552553vma = data->vma;554eaddr = data->addr1;555pfn = data->addr2;556557sh64_dcache_purge_phy_page(pfn << PAGE_SHIFT);558559if (vma->vm_flags & VM_EXEC)560sh64_icache_inv_user_page(vma, eaddr);561}562563static void sh5_flush_dcache_page(void *page)564{565sh64_dcache_purge_phy_page(page_to_phys((struct page *)page));566wmb();567}568569/*570* Flush the range [start,end] of kernel virtual address space from571* the I-cache. The corresponding range must be purged from the572* D-cache also because the SH-5 doesn't have cache snooping between573* the caches. The addresses will be visible through the superpage574* mapping, therefore it's guaranteed that there no cache entries for575* the range in cache sets of the wrong colour.576*/577static void sh5_flush_icache_range(void *args)578{579struct flusher_data *data = args;580unsigned long start, end;581582start = data->addr1;583end = data->addr2;584585__flush_purge_region((void *)start, end);586wmb();587sh64_icache_inv_kernel_range(start, end);588}589590/*591* For the address range [start,end), write back the data from the592* D-cache and invalidate the corresponding region of the I-cache for the593* current process. Used to flush signal trampolines on the stack to594* make them executable.595*/596static void sh5_flush_cache_sigtramp(void *vaddr)597{598unsigned long end = (unsigned long)vaddr + L1_CACHE_BYTES;599600__flush_wback_region(vaddr, L1_CACHE_BYTES);601wmb();602sh64_icache_inv_current_user_range((unsigned long)vaddr, end);603}604605void __init sh5_cache_init(void)606{607local_flush_cache_all = sh5_flush_cache_all;608local_flush_cache_mm = sh5_flush_cache_mm;609local_flush_cache_dup_mm = sh5_flush_cache_mm;610local_flush_cache_page = sh5_flush_cache_page;611local_flush_cache_range = sh5_flush_cache_range;612local_flush_dcache_page = sh5_flush_dcache_page;613local_flush_icache_range = sh5_flush_icache_range;614local_flush_cache_sigtramp = sh5_flush_cache_sigtramp;615616/* Reserve a slot for dcache colouring in the DTLB */617dtlb_cache_slot = sh64_get_wired_dtlb_entry();618619sh4__flush_region_init();620}621622623