Path: blob/master/arch/mips/cavium-octeon/executive/cvmx-l2c.c
26481 views
/***********************license start***************1* Author: Cavium Networks2*3* Contact: [email protected]4* This file is part of the OCTEON SDK5*6* Copyright (c) 2003-2017 Cavium, Inc.7*8* This file is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License, Version 2, as10* published by the Free Software Foundation.11*12* This file is distributed in the hope that it will be useful, but13* AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty14* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or15* NONINFRINGEMENT. See the GNU General Public License for more16* details.17*18* You should have received a copy of the GNU General Public License19* along with this file; if not, write to the Free Software20* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA21* or visit http://www.gnu.org/licenses/.22*23* This file may also be available under a different license from Cavium.24* Contact Cavium Networks for more information25***********************license end**************************************/2627/*28* Implementation of the Level 2 Cache (L2C) control,29* measurement, and debugging facilities.30*/3132#include <linux/compiler.h>33#include <linux/irqflags.h>34#include <asm/octeon/cvmx.h>35#include <asm/octeon/cvmx-l2c.h>36#include <asm/octeon/cvmx-spinlock.h>3738/*39* This spinlock is used internally to ensure that only one core is40* performing certain L2 operations at a time.41*42* NOTE: This only protects calls from within a single application -43* if multiple applications or operating systems are running, then it44* is up to the user program to coordinate between them.45*/46static cvmx_spinlock_t cvmx_l2c_spinlock;4748int cvmx_l2c_get_core_way_partition(uint32_t core)49{50uint32_t field;5152/* Validate the core number */53if (core >= cvmx_octeon_num_cores())54return -1;5556if (OCTEON_IS_MODEL(OCTEON_CN63XX))57return cvmx_read_csr(CVMX_L2C_WPAR_PPX(core)) & 0xffff;5859/*60* Use the lower two bits of the coreNumber to determine the61* bit offset of the UMSK[] field in the L2C_SPAR register.62*/63field = (core & 0x3) * 8;6465/*66* Return the UMSK[] field from the appropriate L2C_SPAR67* register based on the coreNumber.68*/6970switch (core & 0xC) {71case 0x0:72return (cvmx_read_csr(CVMX_L2C_SPAR0) & (0xFF << field)) >> field;73case 0x4:74return (cvmx_read_csr(CVMX_L2C_SPAR1) & (0xFF << field)) >> field;75case 0x8:76return (cvmx_read_csr(CVMX_L2C_SPAR2) & (0xFF << field)) >> field;77case 0xC:78return (cvmx_read_csr(CVMX_L2C_SPAR3) & (0xFF << field)) >> field;79}80return 0;81}8283int cvmx_l2c_set_core_way_partition(uint32_t core, uint32_t mask)84{85uint32_t field;86uint32_t valid_mask;8788valid_mask = (0x1 << cvmx_l2c_get_num_assoc()) - 1;8990mask &= valid_mask;9192/* A UMSK setting which blocks all L2C Ways is an error on some chips */93if (mask == valid_mask && !OCTEON_IS_MODEL(OCTEON_CN63XX))94return -1;9596/* Validate the core number */97if (core >= cvmx_octeon_num_cores())98return -1;99100if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {101cvmx_write_csr(CVMX_L2C_WPAR_PPX(core), mask);102return 0;103}104105/*106* Use the lower two bits of core to determine the bit offset of the107* UMSK[] field in the L2C_SPAR register.108*/109field = (core & 0x3) * 8;110111/*112* Assign the new mask setting to the UMSK[] field in the appropriate113* L2C_SPAR register based on the core_num.114*115*/116switch (core & 0xC) {117case 0x0:118cvmx_write_csr(CVMX_L2C_SPAR0,119(cvmx_read_csr(CVMX_L2C_SPAR0) & ~(0xFF << field)) |120mask << field);121break;122case 0x4:123cvmx_write_csr(CVMX_L2C_SPAR1,124(cvmx_read_csr(CVMX_L2C_SPAR1) & ~(0xFF << field)) |125mask << field);126break;127case 0x8:128cvmx_write_csr(CVMX_L2C_SPAR2,129(cvmx_read_csr(CVMX_L2C_SPAR2) & ~(0xFF << field)) |130mask << field);131break;132case 0xC:133cvmx_write_csr(CVMX_L2C_SPAR3,134(cvmx_read_csr(CVMX_L2C_SPAR3) & ~(0xFF << field)) |135mask << field);136break;137}138return 0;139}140141int cvmx_l2c_set_hw_way_partition(uint32_t mask)142{143uint32_t valid_mask;144145valid_mask = (0x1 << cvmx_l2c_get_num_assoc()) - 1;146mask &= valid_mask;147148/* A UMSK setting which blocks all L2C Ways is an error on some chips */149if (mask == valid_mask && !OCTEON_IS_MODEL(OCTEON_CN63XX))150return -1;151152if (OCTEON_IS_MODEL(OCTEON_CN63XX))153cvmx_write_csr(CVMX_L2C_WPAR_IOBX(0), mask);154else155cvmx_write_csr(CVMX_L2C_SPAR4,156(cvmx_read_csr(CVMX_L2C_SPAR4) & ~0xFF) | mask);157return 0;158}159160int cvmx_l2c_get_hw_way_partition(void)161{162if (OCTEON_IS_MODEL(OCTEON_CN63XX))163return cvmx_read_csr(CVMX_L2C_WPAR_IOBX(0)) & 0xffff;164else165return cvmx_read_csr(CVMX_L2C_SPAR4) & (0xFF);166}167168void cvmx_l2c_config_perf(uint32_t counter, enum cvmx_l2c_event event,169uint32_t clear_on_read)170{171if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX)) {172union cvmx_l2c_pfctl pfctl;173174pfctl.u64 = cvmx_read_csr(CVMX_L2C_PFCTL);175176switch (counter) {177case 0:178pfctl.s.cnt0sel = event;179pfctl.s.cnt0ena = 1;180pfctl.s.cnt0rdclr = clear_on_read;181break;182case 1:183pfctl.s.cnt1sel = event;184pfctl.s.cnt1ena = 1;185pfctl.s.cnt1rdclr = clear_on_read;186break;187case 2:188pfctl.s.cnt2sel = event;189pfctl.s.cnt2ena = 1;190pfctl.s.cnt2rdclr = clear_on_read;191break;192case 3:193default:194pfctl.s.cnt3sel = event;195pfctl.s.cnt3ena = 1;196pfctl.s.cnt3rdclr = clear_on_read;197break;198}199200cvmx_write_csr(CVMX_L2C_PFCTL, pfctl.u64);201} else {202union cvmx_l2c_tadx_prf l2c_tadx_prf;203int tad;204205cvmx_dprintf("L2C performance counter events are different for this chip, mapping 'event' to cvmx_l2c_tad_event_t\n");206if (clear_on_read)207cvmx_dprintf("L2C counters don't support clear on read for this chip\n");208209l2c_tadx_prf.u64 = cvmx_read_csr(CVMX_L2C_TADX_PRF(0));210211switch (counter) {212case 0:213l2c_tadx_prf.s.cnt0sel = event;214break;215case 1:216l2c_tadx_prf.s.cnt1sel = event;217break;218case 2:219l2c_tadx_prf.s.cnt2sel = event;220break;221default:222case 3:223l2c_tadx_prf.s.cnt3sel = event;224break;225}226for (tad = 0; tad < CVMX_L2C_TADS; tad++)227cvmx_write_csr(CVMX_L2C_TADX_PRF(tad),228l2c_tadx_prf.u64);229}230}231232uint64_t cvmx_l2c_read_perf(uint32_t counter)233{234switch (counter) {235case 0:236if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))237return cvmx_read_csr(CVMX_L2C_PFC0);238else {239uint64_t counter = 0;240int tad;241242for (tad = 0; tad < CVMX_L2C_TADS; tad++)243counter += cvmx_read_csr(CVMX_L2C_TADX_PFC0(tad));244return counter;245}246case 1:247if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))248return cvmx_read_csr(CVMX_L2C_PFC1);249else {250uint64_t counter = 0;251int tad;252253for (tad = 0; tad < CVMX_L2C_TADS; tad++)254counter += cvmx_read_csr(CVMX_L2C_TADX_PFC1(tad));255return counter;256}257case 2:258if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))259return cvmx_read_csr(CVMX_L2C_PFC2);260else {261uint64_t counter = 0;262int tad;263264for (tad = 0; tad < CVMX_L2C_TADS; tad++)265counter += cvmx_read_csr(CVMX_L2C_TADX_PFC2(tad));266return counter;267}268case 3:269default:270if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))271return cvmx_read_csr(CVMX_L2C_PFC3);272else {273uint64_t counter = 0;274int tad;275276for (tad = 0; tad < CVMX_L2C_TADS; tad++)277counter += cvmx_read_csr(CVMX_L2C_TADX_PFC3(tad));278return counter;279}280}281}282283/*284* @INTERNAL285* Helper function use to fault in cache lines for L2 cache locking286*287* @addr: Address of base of memory region to read into L2 cache288* @len: Length (in bytes) of region to fault in289*/290static void fault_in(uint64_t addr, int len)291{292char *ptr;293294/*295* Adjust addr and length so we get all cache lines even for296* small ranges spanning two cache lines.297*/298len += addr & CVMX_CACHE_LINE_MASK;299addr &= ~CVMX_CACHE_LINE_MASK;300ptr = cvmx_phys_to_ptr(addr);301/*302* Invalidate L1 cache to make sure all loads result in data303* being in L2.304*/305CVMX_DCACHE_INVALIDATE;306while (len > 0) {307READ_ONCE(*ptr);308len -= CVMX_CACHE_LINE_SIZE;309ptr += CVMX_CACHE_LINE_SIZE;310}311}312313int cvmx_l2c_lock_line(uint64_t addr)314{315if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {316int shift = CVMX_L2C_TAG_ADDR_ALIAS_SHIFT;317uint64_t assoc = cvmx_l2c_get_num_assoc();318uint64_t tag = addr >> shift;319uint64_t index = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, cvmx_l2c_address_to_index(addr) << CVMX_L2C_IDX_ADDR_SHIFT);320uint64_t way;321union cvmx_l2c_tadx_tag l2c_tadx_tag;322323CVMX_CACHE_LCKL2(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, addr), 0);324325/* Make sure we were able to lock the line */326for (way = 0; way < assoc; way++) {327CVMX_CACHE_LTGL2I(index | (way << shift), 0);328/* make sure CVMX_L2C_TADX_TAG is updated */329CVMX_SYNC;330l2c_tadx_tag.u64 = cvmx_read_csr(CVMX_L2C_TADX_TAG(0));331if (l2c_tadx_tag.s.valid && l2c_tadx_tag.s.tag == tag)332break;333}334335/* Check if a valid line is found */336if (way >= assoc) {337/* cvmx_dprintf("ERROR: cvmx_l2c_lock_line: line not found for locking at 0x%llx address\n", (unsigned long long)addr); */338return -1;339}340341/* Check if lock bit is not set */342if (!l2c_tadx_tag.s.lock) {343/* cvmx_dprintf("ERROR: cvmx_l2c_lock_line: Not able to lock at 0x%llx address\n", (unsigned long long)addr); */344return -1;345}346return way;347} else {348int retval = 0;349union cvmx_l2c_dbg l2cdbg;350union cvmx_l2c_lckbase lckbase;351union cvmx_l2c_lckoff lckoff;352union cvmx_l2t_err l2t_err;353354cvmx_spinlock_lock(&cvmx_l2c_spinlock);355356l2cdbg.u64 = 0;357lckbase.u64 = 0;358lckoff.u64 = 0;359360/* Clear l2t error bits if set */361l2t_err.u64 = cvmx_read_csr(CVMX_L2T_ERR);362l2t_err.s.lckerr = 1;363l2t_err.s.lckerr2 = 1;364cvmx_write_csr(CVMX_L2T_ERR, l2t_err.u64);365366addr &= ~CVMX_CACHE_LINE_MASK;367368/* Set this core as debug core */369l2cdbg.s.ppnum = cvmx_get_core_num();370CVMX_SYNC;371cvmx_write_csr(CVMX_L2C_DBG, l2cdbg.u64);372cvmx_read_csr(CVMX_L2C_DBG);373374lckoff.s.lck_offset = 0; /* Only lock 1 line at a time */375cvmx_write_csr(CVMX_L2C_LCKOFF, lckoff.u64);376cvmx_read_csr(CVMX_L2C_LCKOFF);377378if (((union cvmx_l2c_cfg)(cvmx_read_csr(CVMX_L2C_CFG))).s.idxalias) {379int alias_shift = CVMX_L2C_IDX_ADDR_SHIFT + 2 * CVMX_L2_SET_BITS - 1;380uint64_t addr_tmp = addr ^ (addr & ((1 << alias_shift) - 1)) >> CVMX_L2_SET_BITS;381382lckbase.s.lck_base = addr_tmp >> 7;383384} else {385lckbase.s.lck_base = addr >> 7;386}387388lckbase.s.lck_ena = 1;389cvmx_write_csr(CVMX_L2C_LCKBASE, lckbase.u64);390/* Make sure it gets there */391cvmx_read_csr(CVMX_L2C_LCKBASE);392393fault_in(addr, CVMX_CACHE_LINE_SIZE);394395lckbase.s.lck_ena = 0;396cvmx_write_csr(CVMX_L2C_LCKBASE, lckbase.u64);397/* Make sure it gets there */398cvmx_read_csr(CVMX_L2C_LCKBASE);399400/* Stop being debug core */401cvmx_write_csr(CVMX_L2C_DBG, 0);402cvmx_read_csr(CVMX_L2C_DBG);403404l2t_err.u64 = cvmx_read_csr(CVMX_L2T_ERR);405if (l2t_err.s.lckerr || l2t_err.s.lckerr2)406retval = 1; /* We were unable to lock the line */407408cvmx_spinlock_unlock(&cvmx_l2c_spinlock);409return retval;410}411}412413int cvmx_l2c_lock_mem_region(uint64_t start, uint64_t len)414{415int retval = 0;416417/* Round start/end to cache line boundaries */418len += start & CVMX_CACHE_LINE_MASK;419start &= ~CVMX_CACHE_LINE_MASK;420len = (len + CVMX_CACHE_LINE_MASK) & ~CVMX_CACHE_LINE_MASK;421422while (len) {423retval += cvmx_l2c_lock_line(start);424start += CVMX_CACHE_LINE_SIZE;425len -= CVMX_CACHE_LINE_SIZE;426}427return retval;428}429430void cvmx_l2c_flush(void)431{432uint64_t assoc, set;433uint64_t n_assoc, n_set;434435n_set = cvmx_l2c_get_num_sets();436n_assoc = cvmx_l2c_get_num_assoc();437438if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {439uint64_t address;440/* These may look like constants, but they aren't... */441int assoc_shift = CVMX_L2C_TAG_ADDR_ALIAS_SHIFT;442int set_shift = CVMX_L2C_IDX_ADDR_SHIFT;443444for (set = 0; set < n_set; set++) {445for (assoc = 0; assoc < n_assoc; assoc++) {446address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,447(assoc << assoc_shift) | (set << set_shift));448CVMX_CACHE_WBIL2I(address, 0);449}450}451} else {452for (set = 0; set < n_set; set++)453for (assoc = 0; assoc < n_assoc; assoc++)454cvmx_l2c_flush_line(assoc, set);455}456}457458459int cvmx_l2c_unlock_line(uint64_t address)460{461462if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {463int assoc;464union cvmx_l2c_tag tag;465uint32_t tag_addr;466uint32_t index = cvmx_l2c_address_to_index(address);467468tag_addr = ((address >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) & ((1 << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) - 1));469470/*471* For 63XX, we can flush a line by using the physical472* address directly, so finding the cache line used by473* the address is only required to provide the proper474* return value for the function.475*/476for (assoc = 0; assoc < CVMX_L2_ASSOC; assoc++) {477tag = cvmx_l2c_get_tag(assoc, index);478479if (tag.s.V && (tag.s.addr == tag_addr)) {480CVMX_CACHE_WBIL2(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, address), 0);481return tag.s.L;482}483}484} else {485int assoc;486union cvmx_l2c_tag tag;487uint32_t tag_addr;488489uint32_t index = cvmx_l2c_address_to_index(address);490491/* Compute portion of address that is stored in tag */492tag_addr = ((address >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) & ((1 << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) - 1));493for (assoc = 0; assoc < CVMX_L2_ASSOC; assoc++) {494tag = cvmx_l2c_get_tag(assoc, index);495496if (tag.s.V && (tag.s.addr == tag_addr)) {497cvmx_l2c_flush_line(assoc, index);498return tag.s.L;499}500}501}502return 0;503}504505int cvmx_l2c_unlock_mem_region(uint64_t start, uint64_t len)506{507int num_unlocked = 0;508/* Round start/end to cache line boundaries */509len += start & CVMX_CACHE_LINE_MASK;510start &= ~CVMX_CACHE_LINE_MASK;511len = (len + CVMX_CACHE_LINE_MASK) & ~CVMX_CACHE_LINE_MASK;512while (len > 0) {513num_unlocked += cvmx_l2c_unlock_line(start);514start += CVMX_CACHE_LINE_SIZE;515len -= CVMX_CACHE_LINE_SIZE;516}517518return num_unlocked;519}520521/*522* Internal l2c tag types. These are converted to a generic structure523* that can be used on all chips.524*/525union __cvmx_l2c_tag {526uint64_t u64;527struct cvmx_l2c_tag_cn50xx {528__BITFIELD_FIELD(uint64_t reserved:40,529__BITFIELD_FIELD(uint64_t V:1, /* Line valid */530__BITFIELD_FIELD(uint64_t D:1, /* Line dirty */531__BITFIELD_FIELD(uint64_t L:1, /* Line locked */532__BITFIELD_FIELD(uint64_t U:1, /* Use, LRU eviction */533__BITFIELD_FIELD(uint64_t addr:20, /* Phys addr (33..14) */534;))))))535} cn50xx;536struct cvmx_l2c_tag_cn30xx {537__BITFIELD_FIELD(uint64_t reserved:41,538__BITFIELD_FIELD(uint64_t V:1, /* Line valid */539__BITFIELD_FIELD(uint64_t D:1, /* Line dirty */540__BITFIELD_FIELD(uint64_t L:1, /* Line locked */541__BITFIELD_FIELD(uint64_t U:1, /* Use, LRU eviction */542__BITFIELD_FIELD(uint64_t addr:19, /* Phys addr (33..15) */543;))))))544} cn30xx;545struct cvmx_l2c_tag_cn31xx {546__BITFIELD_FIELD(uint64_t reserved:42,547__BITFIELD_FIELD(uint64_t V:1, /* Line valid */548__BITFIELD_FIELD(uint64_t D:1, /* Line dirty */549__BITFIELD_FIELD(uint64_t L:1, /* Line locked */550__BITFIELD_FIELD(uint64_t U:1, /* Use, LRU eviction */551__BITFIELD_FIELD(uint64_t addr:18, /* Phys addr (33..16) */552;))))))553} cn31xx;554struct cvmx_l2c_tag_cn38xx {555__BITFIELD_FIELD(uint64_t reserved:43,556__BITFIELD_FIELD(uint64_t V:1, /* Line valid */557__BITFIELD_FIELD(uint64_t D:1, /* Line dirty */558__BITFIELD_FIELD(uint64_t L:1, /* Line locked */559__BITFIELD_FIELD(uint64_t U:1, /* Use, LRU eviction */560__BITFIELD_FIELD(uint64_t addr:17, /* Phys addr (33..17) */561;))))))562} cn38xx;563struct cvmx_l2c_tag_cn58xx {564__BITFIELD_FIELD(uint64_t reserved:44,565__BITFIELD_FIELD(uint64_t V:1, /* Line valid */566__BITFIELD_FIELD(uint64_t D:1, /* Line dirty */567__BITFIELD_FIELD(uint64_t L:1, /* Line locked */568__BITFIELD_FIELD(uint64_t U:1, /* Use, LRU eviction */569__BITFIELD_FIELD(uint64_t addr:16, /* Phys addr (33..18) */570;))))))571} cn58xx;572struct cvmx_l2c_tag_cn58xx cn56xx; /* 2048 sets */573struct cvmx_l2c_tag_cn31xx cn52xx; /* 512 sets */574};575576577/*578* @INTERNAL579* Function to read a L2C tag. This code make the current core580* the 'debug core' for the L2. This code must only be executed by581* 1 core at a time.582*583* @assoc: Association (way) of the tag to dump584* @index: Index of the cacheline585*586* Returns The Octeon model specific tag structure. This is587* translated by a wrapper function to a generic form that is588* easier for applications to use.589*/590static union __cvmx_l2c_tag __read_l2_tag(uint64_t assoc, uint64_t index)591{592593uint64_t debug_tag_addr = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, (index << 7) + 96);594uint64_t core = cvmx_get_core_num();595union __cvmx_l2c_tag tag_val;596uint64_t dbg_addr = CVMX_L2C_DBG;597unsigned long flags;598union cvmx_l2c_dbg debug_val;599600debug_val.u64 = 0;601/*602* For low core count parts, the core number is always small603* enough to stay in the correct field and not set any604* reserved bits.605*/606debug_val.s.ppnum = core;607debug_val.s.l2t = 1;608debug_val.s.set = assoc;609610local_irq_save(flags);611/*612* Make sure core is quiet (no prefetches, etc.) before613* entering debug mode.614*/615CVMX_SYNC;616/* Flush L1 to make sure debug load misses L1 */617CVMX_DCACHE_INVALIDATE;618619/*620* The following must be done in assembly as when in debug621* mode all data loads from L2 return special debug data, not622* normal memory contents. Also, interrupts must be disabled,623* since if an interrupt occurs while in debug mode the ISR624* will get debug data from all its memory * reads instead of625* the contents of memory.626*/627628asm volatile (629".set push\n\t"630".set mips64\n\t"631".set noreorder\n\t"632"sd %[dbg_val], 0(%[dbg_addr])\n\t" /* Enter debug mode, wait for store */633"ld $0, 0(%[dbg_addr])\n\t"634"ld %[tag_val], 0(%[tag_addr])\n\t" /* Read L2C tag data */635"sd $0, 0(%[dbg_addr])\n\t" /* Exit debug mode, wait for store */636"ld $0, 0(%[dbg_addr])\n\t"637"cache 9, 0($0)\n\t" /* Invalidate dcache to discard debug data */638".set pop"639: [tag_val] "=r" (tag_val)640: [dbg_addr] "r" (dbg_addr), [dbg_val] "r" (debug_val), [tag_addr] "r" (debug_tag_addr)641: "memory");642643local_irq_restore(flags);644645return tag_val;646}647648649union cvmx_l2c_tag cvmx_l2c_get_tag(uint32_t association, uint32_t index)650{651union cvmx_l2c_tag tag;652653tag.u64 = 0;654if ((int)association >= cvmx_l2c_get_num_assoc()) {655cvmx_dprintf("ERROR: cvmx_l2c_get_tag association out of range\n");656return tag;657}658if ((int)index >= cvmx_l2c_get_num_sets()) {659cvmx_dprintf("ERROR: cvmx_l2c_get_tag index out of range (arg: %d, max: %d)\n",660(int)index, cvmx_l2c_get_num_sets());661return tag;662}663if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {664union cvmx_l2c_tadx_tag l2c_tadx_tag;665uint64_t address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,666(association << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) |667(index << CVMX_L2C_IDX_ADDR_SHIFT));668/*669* Use L2 cache Index load tag cache instruction, as670* hardware loads the virtual tag for the L2 cache671* block with the contents of L2C_TAD0_TAG672* register.673*/674CVMX_CACHE_LTGL2I(address, 0);675CVMX_SYNC; /* make sure CVMX_L2C_TADX_TAG is updated */676l2c_tadx_tag.u64 = cvmx_read_csr(CVMX_L2C_TADX_TAG(0));677678tag.s.V = l2c_tadx_tag.s.valid;679tag.s.D = l2c_tadx_tag.s.dirty;680tag.s.L = l2c_tadx_tag.s.lock;681tag.s.U = l2c_tadx_tag.s.use;682tag.s.addr = l2c_tadx_tag.s.tag;683} else {684union __cvmx_l2c_tag tmp_tag;685/* __read_l2_tag is intended for internal use only */686tmp_tag = __read_l2_tag(association, index);687688/*689* Convert all tag structure types to generic version,690* as it can represent all models.691*/692if (OCTEON_IS_MODEL(OCTEON_CN58XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {693tag.s.V = tmp_tag.cn58xx.V;694tag.s.D = tmp_tag.cn58xx.D;695tag.s.L = tmp_tag.cn58xx.L;696tag.s.U = tmp_tag.cn58xx.U;697tag.s.addr = tmp_tag.cn58xx.addr;698} else if (OCTEON_IS_MODEL(OCTEON_CN38XX)) {699tag.s.V = tmp_tag.cn38xx.V;700tag.s.D = tmp_tag.cn38xx.D;701tag.s.L = tmp_tag.cn38xx.L;702tag.s.U = tmp_tag.cn38xx.U;703tag.s.addr = tmp_tag.cn38xx.addr;704} else if (OCTEON_IS_MODEL(OCTEON_CN31XX) || OCTEON_IS_MODEL(OCTEON_CN52XX)) {705tag.s.V = tmp_tag.cn31xx.V;706tag.s.D = tmp_tag.cn31xx.D;707tag.s.L = tmp_tag.cn31xx.L;708tag.s.U = tmp_tag.cn31xx.U;709tag.s.addr = tmp_tag.cn31xx.addr;710} else if (OCTEON_IS_MODEL(OCTEON_CN30XX)) {711tag.s.V = tmp_tag.cn30xx.V;712tag.s.D = tmp_tag.cn30xx.D;713tag.s.L = tmp_tag.cn30xx.L;714tag.s.U = tmp_tag.cn30xx.U;715tag.s.addr = tmp_tag.cn30xx.addr;716} else if (OCTEON_IS_MODEL(OCTEON_CN50XX)) {717tag.s.V = tmp_tag.cn50xx.V;718tag.s.D = tmp_tag.cn50xx.D;719tag.s.L = tmp_tag.cn50xx.L;720tag.s.U = tmp_tag.cn50xx.U;721tag.s.addr = tmp_tag.cn50xx.addr;722} else {723cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);724}725}726return tag;727}728729uint32_t cvmx_l2c_address_to_index(uint64_t addr)730{731uint64_t idx = addr >> CVMX_L2C_IDX_ADDR_SHIFT;732int indxalias = 0;733734if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {735union cvmx_l2c_ctl l2c_ctl;736737l2c_ctl.u64 = cvmx_read_csr(CVMX_L2C_CTL);738indxalias = !l2c_ctl.s.disidxalias;739} else {740union cvmx_l2c_cfg l2c_cfg;741742l2c_cfg.u64 = cvmx_read_csr(CVMX_L2C_CFG);743indxalias = l2c_cfg.s.idxalias;744}745746if (indxalias) {747if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {748uint32_t a_14_12 = (idx / (CVMX_L2C_MEMBANK_SELECT_SIZE/(1<<CVMX_L2C_IDX_ADDR_SHIFT))) & 0x7;749750idx ^= idx / cvmx_l2c_get_num_sets();751idx ^= a_14_12;752} else {753idx ^= ((addr & CVMX_L2C_ALIAS_MASK) >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT);754}755}756idx &= CVMX_L2C_IDX_MASK;757return idx;758}759760int cvmx_l2c_get_cache_size_bytes(void)761{762return cvmx_l2c_get_num_sets() * cvmx_l2c_get_num_assoc() *763CVMX_CACHE_LINE_SIZE;764}765766/*767* Return log base 2 of the number of sets in the L2 cache768*/769int cvmx_l2c_get_set_bits(void)770{771int l2_set_bits;772773if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN58XX))774l2_set_bits = 11; /* 2048 sets */775else if (OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN63XX))776l2_set_bits = 10; /* 1024 sets */777else if (OCTEON_IS_MODEL(OCTEON_CN31XX) || OCTEON_IS_MODEL(OCTEON_CN52XX))778l2_set_bits = 9; /* 512 sets */779else if (OCTEON_IS_MODEL(OCTEON_CN30XX))780l2_set_bits = 8; /* 256 sets */781else if (OCTEON_IS_MODEL(OCTEON_CN50XX))782l2_set_bits = 7; /* 128 sets */783else {784cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);785l2_set_bits = 11; /* 2048 sets */786}787return l2_set_bits;788}789790/* Return the number of sets in the L2 Cache */791int cvmx_l2c_get_num_sets(void)792{793return 1 << cvmx_l2c_get_set_bits();794}795796/* Return the number of associations in the L2 Cache */797int cvmx_l2c_get_num_assoc(void)798{799int l2_assoc;800801if (OCTEON_IS_MODEL(OCTEON_CN56XX) ||802OCTEON_IS_MODEL(OCTEON_CN52XX) ||803OCTEON_IS_MODEL(OCTEON_CN58XX) ||804OCTEON_IS_MODEL(OCTEON_CN50XX) ||805OCTEON_IS_MODEL(OCTEON_CN38XX))806l2_assoc = 8;807else if (OCTEON_IS_MODEL(OCTEON_CN63XX))808l2_assoc = 16;809else if (OCTEON_IS_MODEL(OCTEON_CN31XX) ||810OCTEON_IS_MODEL(OCTEON_CN30XX))811l2_assoc = 4;812else {813cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);814l2_assoc = 8;815}816817/* Check to see if part of the cache is disabled */818if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {819union cvmx_mio_fus_dat3 mio_fus_dat3;820821mio_fus_dat3.u64 = cvmx_read_csr(CVMX_MIO_FUS_DAT3);822/*823* cvmx_mio_fus_dat3.s.l2c_crip fuses map as follows824* <2> will be not used for 63xx825* <1> disables 1/2 ways826* <0> disables 1/4 ways827* They are cumulative, so for 63xx:828* <1> <0>829* 0 0 16-way 2MB cache830* 0 1 12-way 1.5MB cache831* 1 0 8-way 1MB cache832* 1 1 4-way 512KB cache833*/834835if (mio_fus_dat3.s.l2c_crip == 3)836l2_assoc = 4;837else if (mio_fus_dat3.s.l2c_crip == 2)838l2_assoc = 8;839else if (mio_fus_dat3.s.l2c_crip == 1)840l2_assoc = 12;841} else {842uint64_t l2d_fus3;843844l2d_fus3 = cvmx_read_csr(CVMX_L2D_FUS3);845/*846* Using shifts here, as bit position names are847* different for each model but they all mean the848* same.849*/850if ((l2d_fus3 >> 35) & 0x1)851l2_assoc = l2_assoc >> 2;852else if ((l2d_fus3 >> 34) & 0x1)853l2_assoc = l2_assoc >> 1;854}855return l2_assoc;856}857858/*859* Flush a line from the L2 cache860* This should only be called from one core at a time, as this routine861* sets the core to the 'debug' core in order to flush the line.862*863* @assoc: Association (or way) to flush864* @index: Index to flush865*/866void cvmx_l2c_flush_line(uint32_t assoc, uint32_t index)867{868/* Check the range of the index. */869if (index > (uint32_t)cvmx_l2c_get_num_sets()) {870cvmx_dprintf("ERROR: cvmx_l2c_flush_line index out of range.\n");871return;872}873874/* Check the range of association. */875if (assoc > (uint32_t)cvmx_l2c_get_num_assoc()) {876cvmx_dprintf("ERROR: cvmx_l2c_flush_line association out of range.\n");877return;878}879880if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {881uint64_t address;882/* Create the address based on index and association.883* Bits<20:17> select the way of the cache block involved in884* the operation885* Bits<16:7> of the effect address select the index886*/887address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,888(assoc << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) |889(index << CVMX_L2C_IDX_ADDR_SHIFT));890CVMX_CACHE_WBIL2I(address, 0);891} else {892union cvmx_l2c_dbg l2cdbg;893894l2cdbg.u64 = 0;895if (!OCTEON_IS_MODEL(OCTEON_CN30XX))896l2cdbg.s.ppnum = cvmx_get_core_num();897l2cdbg.s.finv = 1;898899l2cdbg.s.set = assoc;900cvmx_spinlock_lock(&cvmx_l2c_spinlock);901/*902* Enter debug mode, and make sure all other writes903* complete before we enter debug mode904*/905CVMX_SYNC;906cvmx_write_csr(CVMX_L2C_DBG, l2cdbg.u64);907cvmx_read_csr(CVMX_L2C_DBG);908909CVMX_PREPARE_FOR_STORE(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,910index * CVMX_CACHE_LINE_SIZE),9110);912/* Exit debug mode */913CVMX_SYNC;914cvmx_write_csr(CVMX_L2C_DBG, 0);915cvmx_read_csr(CVMX_L2C_DBG);916cvmx_spinlock_unlock(&cvmx_l2c_spinlock);917}918}919920921