Path: blob/master/arch/mips/cavium-octeon/executive/cvmx-l2c.c
10818 views
/***********************license start***************1* Author: Cavium Networks2*3* Contact: [email protected]4* This file is part of the OCTEON SDK5*6* Copyright (c) 2003-2010 Cavium Networks7*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 <asm/octeon/cvmx.h>33#include <asm/octeon/cvmx-l2c.h>34#include <asm/octeon/cvmx-spinlock.h>3536/*37* This spinlock is used internally to ensure that only one core is38* performing certain L2 operations at a time.39*40* NOTE: This only protects calls from within a single application -41* if multiple applications or operating systems are running, then it42* is up to the user program to coordinate between them.43*/44cvmx_spinlock_t cvmx_l2c_spinlock;4546int cvmx_l2c_get_core_way_partition(uint32_t core)47{48uint32_t field;4950/* Validate the core number */51if (core >= cvmx_octeon_num_cores())52return -1;5354if (OCTEON_IS_MODEL(OCTEON_CN63XX))55return cvmx_read_csr(CVMX_L2C_WPAR_PPX(core)) & 0xffff;5657/*58* Use the lower two bits of the coreNumber to determine the59* bit offset of the UMSK[] field in the L2C_SPAR register.60*/61field = (core & 0x3) * 8;6263/*64* Return the UMSK[] field from the appropriate L2C_SPAR65* register based on the coreNumber.66*/6768switch (core & 0xC) {69case 0x0:70return (cvmx_read_csr(CVMX_L2C_SPAR0) & (0xFF << field)) >> field;71case 0x4:72return (cvmx_read_csr(CVMX_L2C_SPAR1) & (0xFF << field)) >> field;73case 0x8:74return (cvmx_read_csr(CVMX_L2C_SPAR2) & (0xFF << field)) >> field;75case 0xC:76return (cvmx_read_csr(CVMX_L2C_SPAR3) & (0xFF << field)) >> field;77}78return 0;79}8081int cvmx_l2c_set_core_way_partition(uint32_t core, uint32_t mask)82{83uint32_t field;84uint32_t valid_mask;8586valid_mask = (0x1 << cvmx_l2c_get_num_assoc()) - 1;8788mask &= valid_mask;8990/* A UMSK setting which blocks all L2C Ways is an error on some chips */91if (mask == valid_mask && !OCTEON_IS_MODEL(OCTEON_CN63XX))92return -1;9394/* Validate the core number */95if (core >= cvmx_octeon_num_cores())96return -1;9798if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {99cvmx_write_csr(CVMX_L2C_WPAR_PPX(core), mask);100return 0;101}102103/*104* Use the lower two bits of core to determine the bit offset of the105* UMSK[] field in the L2C_SPAR register.106*/107field = (core & 0x3) * 8;108109/*110* Assign the new mask setting to the UMSK[] field in the appropriate111* L2C_SPAR register based on the core_num.112*113*/114switch (core & 0xC) {115case 0x0:116cvmx_write_csr(CVMX_L2C_SPAR0,117(cvmx_read_csr(CVMX_L2C_SPAR0) & ~(0xFF << field)) |118mask << field);119break;120case 0x4:121cvmx_write_csr(CVMX_L2C_SPAR1,122(cvmx_read_csr(CVMX_L2C_SPAR1) & ~(0xFF << field)) |123mask << field);124break;125case 0x8:126cvmx_write_csr(CVMX_L2C_SPAR2,127(cvmx_read_csr(CVMX_L2C_SPAR2) & ~(0xFF << field)) |128mask << field);129break;130case 0xC:131cvmx_write_csr(CVMX_L2C_SPAR3,132(cvmx_read_csr(CVMX_L2C_SPAR3) & ~(0xFF << field)) |133mask << field);134break;135}136return 0;137}138139int cvmx_l2c_set_hw_way_partition(uint32_t mask)140{141uint32_t valid_mask;142143valid_mask = (0x1 << cvmx_l2c_get_num_assoc()) - 1;144mask &= valid_mask;145146/* A UMSK setting which blocks all L2C Ways is an error on some chips */147if (mask == valid_mask && !OCTEON_IS_MODEL(OCTEON_CN63XX))148return -1;149150if (OCTEON_IS_MODEL(OCTEON_CN63XX))151cvmx_write_csr(CVMX_L2C_WPAR_IOBX(0), mask);152else153cvmx_write_csr(CVMX_L2C_SPAR4,154(cvmx_read_csr(CVMX_L2C_SPAR4) & ~0xFF) | mask);155return 0;156}157158int cvmx_l2c_get_hw_way_partition(void)159{160if (OCTEON_IS_MODEL(OCTEON_CN63XX))161return cvmx_read_csr(CVMX_L2C_WPAR_IOBX(0)) & 0xffff;162else163return cvmx_read_csr(CVMX_L2C_SPAR4) & (0xFF);164}165166void cvmx_l2c_config_perf(uint32_t counter, enum cvmx_l2c_event event,167uint32_t clear_on_read)168{169if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX)) {170union cvmx_l2c_pfctl pfctl;171172pfctl.u64 = cvmx_read_csr(CVMX_L2C_PFCTL);173174switch (counter) {175case 0:176pfctl.s.cnt0sel = event;177pfctl.s.cnt0ena = 1;178pfctl.s.cnt0rdclr = clear_on_read;179break;180case 1:181pfctl.s.cnt1sel = event;182pfctl.s.cnt1ena = 1;183pfctl.s.cnt1rdclr = clear_on_read;184break;185case 2:186pfctl.s.cnt2sel = event;187pfctl.s.cnt2ena = 1;188pfctl.s.cnt2rdclr = clear_on_read;189break;190case 3:191default:192pfctl.s.cnt3sel = event;193pfctl.s.cnt3ena = 1;194pfctl.s.cnt3rdclr = clear_on_read;195break;196}197198cvmx_write_csr(CVMX_L2C_PFCTL, pfctl.u64);199} else {200union cvmx_l2c_tadx_prf l2c_tadx_prf;201int tad;202203cvmx_dprintf("L2C performance counter events are different for this chip, mapping 'event' to cvmx_l2c_tad_event_t\n");204if (clear_on_read)205cvmx_dprintf("L2C counters don't support clear on read for this chip\n");206207l2c_tadx_prf.u64 = cvmx_read_csr(CVMX_L2C_TADX_PRF(0));208209switch (counter) {210case 0:211l2c_tadx_prf.s.cnt0sel = event;212break;213case 1:214l2c_tadx_prf.s.cnt1sel = event;215break;216case 2:217l2c_tadx_prf.s.cnt2sel = event;218break;219default:220case 3:221l2c_tadx_prf.s.cnt3sel = event;222break;223}224for (tad = 0; tad < CVMX_L2C_TADS; tad++)225cvmx_write_csr(CVMX_L2C_TADX_PRF(tad),226l2c_tadx_prf.u64);227}228}229230uint64_t cvmx_l2c_read_perf(uint32_t counter)231{232switch (counter) {233case 0:234if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))235return cvmx_read_csr(CVMX_L2C_PFC0);236else {237uint64_t counter = 0;238int tad;239for (tad = 0; tad < CVMX_L2C_TADS; tad++)240counter += cvmx_read_csr(CVMX_L2C_TADX_PFC0(tad));241return counter;242}243case 1:244if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))245return cvmx_read_csr(CVMX_L2C_PFC1);246else {247uint64_t counter = 0;248int tad;249for (tad = 0; tad < CVMX_L2C_TADS; tad++)250counter += cvmx_read_csr(CVMX_L2C_TADX_PFC1(tad));251return counter;252}253case 2:254if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))255return cvmx_read_csr(CVMX_L2C_PFC2);256else {257uint64_t counter = 0;258int tad;259for (tad = 0; tad < CVMX_L2C_TADS; tad++)260counter += cvmx_read_csr(CVMX_L2C_TADX_PFC2(tad));261return counter;262}263case 3:264default:265if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))266return cvmx_read_csr(CVMX_L2C_PFC3);267else {268uint64_t counter = 0;269int tad;270for (tad = 0; tad < CVMX_L2C_TADS; tad++)271counter += cvmx_read_csr(CVMX_L2C_TADX_PFC3(tad));272return counter;273}274}275}276277/**278* @INTERNAL279* Helper function use to fault in cache lines for L2 cache locking280*281* @addr: Address of base of memory region to read into L2 cache282* @len: Length (in bytes) of region to fault in283*/284static void fault_in(uint64_t addr, int len)285{286volatile char *ptr;287volatile char dummy;288/*289* Adjust addr and length so we get all cache lines even for290* small ranges spanning two cache lines.291*/292len += addr & CVMX_CACHE_LINE_MASK;293addr &= ~CVMX_CACHE_LINE_MASK;294ptr = (volatile char *)cvmx_phys_to_ptr(addr);295/*296* Invalidate L1 cache to make sure all loads result in data297* being in L2.298*/299CVMX_DCACHE_INVALIDATE;300while (len > 0) {301dummy += *ptr;302len -= CVMX_CACHE_LINE_SIZE;303ptr += CVMX_CACHE_LINE_SIZE;304}305}306307int cvmx_l2c_lock_line(uint64_t addr)308{309if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {310int shift = CVMX_L2C_TAG_ADDR_ALIAS_SHIFT;311uint64_t assoc = cvmx_l2c_get_num_assoc();312uint64_t tag = addr >> shift;313uint64_t index = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, cvmx_l2c_address_to_index(addr) << CVMX_L2C_IDX_ADDR_SHIFT);314uint64_t way;315union cvmx_l2c_tadx_tag l2c_tadx_tag;316317CVMX_CACHE_LCKL2(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, addr), 0);318319/* Make sure we were able to lock the line */320for (way = 0; way < assoc; way++) {321CVMX_CACHE_LTGL2I(index | (way << shift), 0);322/* make sure CVMX_L2C_TADX_TAG is updated */323CVMX_SYNC;324l2c_tadx_tag.u64 = cvmx_read_csr(CVMX_L2C_TADX_TAG(0));325if (l2c_tadx_tag.s.valid && l2c_tadx_tag.s.tag == tag)326break;327}328329/* Check if a valid line is found */330if (way >= assoc) {331/* cvmx_dprintf("ERROR: cvmx_l2c_lock_line: line not found for locking at 0x%llx address\n", (unsigned long long)addr); */332return -1;333}334335/* Check if lock bit is not set */336if (!l2c_tadx_tag.s.lock) {337/* cvmx_dprintf("ERROR: cvmx_l2c_lock_line: Not able to lock at 0x%llx address\n", (unsigned long long)addr); */338return -1;339}340return way;341} else {342int retval = 0;343union cvmx_l2c_dbg l2cdbg;344union cvmx_l2c_lckbase lckbase;345union cvmx_l2c_lckoff lckoff;346union cvmx_l2t_err l2t_err;347348cvmx_spinlock_lock(&cvmx_l2c_spinlock);349350l2cdbg.u64 = 0;351lckbase.u64 = 0;352lckoff.u64 = 0;353354/* Clear l2t error bits if set */355l2t_err.u64 = cvmx_read_csr(CVMX_L2T_ERR);356l2t_err.s.lckerr = 1;357l2t_err.s.lckerr2 = 1;358cvmx_write_csr(CVMX_L2T_ERR, l2t_err.u64);359360addr &= ~CVMX_CACHE_LINE_MASK;361362/* Set this core as debug core */363l2cdbg.s.ppnum = cvmx_get_core_num();364CVMX_SYNC;365cvmx_write_csr(CVMX_L2C_DBG, l2cdbg.u64);366cvmx_read_csr(CVMX_L2C_DBG);367368lckoff.s.lck_offset = 0; /* Only lock 1 line at a time */369cvmx_write_csr(CVMX_L2C_LCKOFF, lckoff.u64);370cvmx_read_csr(CVMX_L2C_LCKOFF);371372if (((union cvmx_l2c_cfg)(cvmx_read_csr(CVMX_L2C_CFG))).s.idxalias) {373int alias_shift = CVMX_L2C_IDX_ADDR_SHIFT + 2 * CVMX_L2_SET_BITS - 1;374uint64_t addr_tmp = addr ^ (addr & ((1 << alias_shift) - 1)) >> CVMX_L2_SET_BITS;375lckbase.s.lck_base = addr_tmp >> 7;376} else {377lckbase.s.lck_base = addr >> 7;378}379380lckbase.s.lck_ena = 1;381cvmx_write_csr(CVMX_L2C_LCKBASE, lckbase.u64);382/* Make sure it gets there */383cvmx_read_csr(CVMX_L2C_LCKBASE);384385fault_in(addr, CVMX_CACHE_LINE_SIZE);386387lckbase.s.lck_ena = 0;388cvmx_write_csr(CVMX_L2C_LCKBASE, lckbase.u64);389/* Make sure it gets there */390cvmx_read_csr(CVMX_L2C_LCKBASE);391392/* Stop being debug core */393cvmx_write_csr(CVMX_L2C_DBG, 0);394cvmx_read_csr(CVMX_L2C_DBG);395396l2t_err.u64 = cvmx_read_csr(CVMX_L2T_ERR);397if (l2t_err.s.lckerr || l2t_err.s.lckerr2)398retval = 1; /* We were unable to lock the line */399400cvmx_spinlock_unlock(&cvmx_l2c_spinlock);401return retval;402}403}404405int cvmx_l2c_lock_mem_region(uint64_t start, uint64_t len)406{407int retval = 0;408409/* Round start/end to cache line boundaries */410len += start & CVMX_CACHE_LINE_MASK;411start &= ~CVMX_CACHE_LINE_MASK;412len = (len + CVMX_CACHE_LINE_MASK) & ~CVMX_CACHE_LINE_MASK;413414while (len) {415retval += cvmx_l2c_lock_line(start);416start += CVMX_CACHE_LINE_SIZE;417len -= CVMX_CACHE_LINE_SIZE;418}419return retval;420}421422void cvmx_l2c_flush(void)423{424uint64_t assoc, set;425uint64_t n_assoc, n_set;426427n_set = cvmx_l2c_get_num_sets();428n_assoc = cvmx_l2c_get_num_assoc();429430if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {431uint64_t address;432/* These may look like constants, but they aren't... */433int assoc_shift = CVMX_L2C_TAG_ADDR_ALIAS_SHIFT;434int set_shift = CVMX_L2C_IDX_ADDR_SHIFT;435for (set = 0; set < n_set; set++) {436for (assoc = 0; assoc < n_assoc; assoc++) {437address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,438(assoc << assoc_shift) | (set << set_shift));439CVMX_CACHE_WBIL2I(address, 0);440}441}442} else {443for (set = 0; set < n_set; set++)444for (assoc = 0; assoc < n_assoc; assoc++)445cvmx_l2c_flush_line(assoc, set);446}447}448449450int cvmx_l2c_unlock_line(uint64_t address)451{452453if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {454int assoc;455union cvmx_l2c_tag tag;456uint32_t tag_addr;457uint32_t index = cvmx_l2c_address_to_index(address);458459tag_addr = ((address >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) & ((1 << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) - 1));460461/*462* For 63XX, we can flush a line by using the physical463* address directly, so finding the cache line used by464* the address is only required to provide the proper465* return value for the function.466*/467for (assoc = 0; assoc < CVMX_L2_ASSOC; assoc++) {468tag = cvmx_l2c_get_tag(assoc, index);469470if (tag.s.V && (tag.s.addr == tag_addr)) {471CVMX_CACHE_WBIL2(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, address), 0);472return tag.s.L;473}474}475} else {476int assoc;477union cvmx_l2c_tag tag;478uint32_t tag_addr;479480uint32_t index = cvmx_l2c_address_to_index(address);481482/* Compute portion of address that is stored in tag */483tag_addr = ((address >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) & ((1 << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) - 1));484for (assoc = 0; assoc < CVMX_L2_ASSOC; assoc++) {485tag = cvmx_l2c_get_tag(assoc, index);486487if (tag.s.V && (tag.s.addr == tag_addr)) {488cvmx_l2c_flush_line(assoc, index);489return tag.s.L;490}491}492}493return 0;494}495496int cvmx_l2c_unlock_mem_region(uint64_t start, uint64_t len)497{498int num_unlocked = 0;499/* Round start/end to cache line boundaries */500len += start & CVMX_CACHE_LINE_MASK;501start &= ~CVMX_CACHE_LINE_MASK;502len = (len + CVMX_CACHE_LINE_MASK) & ~CVMX_CACHE_LINE_MASK;503while (len > 0) {504num_unlocked += cvmx_l2c_unlock_line(start);505start += CVMX_CACHE_LINE_SIZE;506len -= CVMX_CACHE_LINE_SIZE;507}508509return num_unlocked;510}511512/*513* Internal l2c tag types. These are converted to a generic structure514* that can be used on all chips.515*/516union __cvmx_l2c_tag {517uint64_t u64;518struct cvmx_l2c_tag_cn50xx {519uint64_t reserved:40;520uint64_t V:1; /* Line valid */521uint64_t D:1; /* Line dirty */522uint64_t L:1; /* Line locked */523uint64_t U:1; /* Use, LRU eviction */524uint64_t addr:20; /* Phys mem addr (33..14) */525} cn50xx;526struct cvmx_l2c_tag_cn30xx {527uint64_t reserved:41;528uint64_t V:1; /* Line valid */529uint64_t D:1; /* Line dirty */530uint64_t L:1; /* Line locked */531uint64_t U:1; /* Use, LRU eviction */532uint64_t addr:19; /* Phys mem addr (33..15) */533} cn30xx;534struct cvmx_l2c_tag_cn31xx {535uint64_t reserved:42;536uint64_t V:1; /* Line valid */537uint64_t D:1; /* Line dirty */538uint64_t L:1; /* Line locked */539uint64_t U:1; /* Use, LRU eviction */540uint64_t addr:18; /* Phys mem addr (33..16) */541} cn31xx;542struct cvmx_l2c_tag_cn38xx {543uint64_t reserved:43;544uint64_t V:1; /* Line valid */545uint64_t D:1; /* Line dirty */546uint64_t L:1; /* Line locked */547uint64_t U:1; /* Use, LRU eviction */548uint64_t addr:17; /* Phys mem addr (33..17) */549} cn38xx;550struct cvmx_l2c_tag_cn58xx {551uint64_t reserved:44;552uint64_t V:1; /* Line valid */553uint64_t D:1; /* Line dirty */554uint64_t L:1; /* Line locked */555uint64_t U:1; /* Use, LRU eviction */556uint64_t addr:16; /* Phys mem addr (33..18) */557} cn58xx;558struct cvmx_l2c_tag_cn58xx cn56xx; /* 2048 sets */559struct cvmx_l2c_tag_cn31xx cn52xx; /* 512 sets */560};561562563/**564* @INTERNAL565* Function to read a L2C tag. This code make the current core566* the 'debug core' for the L2. This code must only be executed by567* 1 core at a time.568*569* @assoc: Association (way) of the tag to dump570* @index: Index of the cacheline571*572* Returns The Octeon model specific tag structure. This is573* translated by a wrapper function to a generic form that is574* easier for applications to use.575*/576static union __cvmx_l2c_tag __read_l2_tag(uint64_t assoc, uint64_t index)577{578579uint64_t debug_tag_addr = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, (index << 7) + 96);580uint64_t core = cvmx_get_core_num();581union __cvmx_l2c_tag tag_val;582uint64_t dbg_addr = CVMX_L2C_DBG;583unsigned long flags;584585union cvmx_l2c_dbg debug_val;586debug_val.u64 = 0;587/*588* For low core count parts, the core number is always small589* enough to stay in the correct field and not set any590* reserved bits.591*/592debug_val.s.ppnum = core;593debug_val.s.l2t = 1;594debug_val.s.set = assoc;595596local_irq_save(flags);597/*598* Make sure core is quiet (no prefetches, etc.) before599* entering debug mode.600*/601CVMX_SYNC;602/* Flush L1 to make sure debug load misses L1 */603CVMX_DCACHE_INVALIDATE;604605/*606* The following must be done in assembly as when in debug607* mode all data loads from L2 return special debug data, not608* normal memory contents. Also, interrupts must be disabled,609* since if an interrupt occurs while in debug mode the ISR610* will get debug data from all its memory * reads instead of611* the contents of memory.612*/613614asm volatile (615".set push\n\t"616".set mips64\n\t"617".set noreorder\n\t"618"sd %[dbg_val], 0(%[dbg_addr])\n\t" /* Enter debug mode, wait for store */619"ld $0, 0(%[dbg_addr])\n\t"620"ld %[tag_val], 0(%[tag_addr])\n\t" /* Read L2C tag data */621"sd $0, 0(%[dbg_addr])\n\t" /* Exit debug mode, wait for store */622"ld $0, 0(%[dbg_addr])\n\t"623"cache 9, 0($0)\n\t" /* Invalidate dcache to discard debug data */624".set pop"625: [tag_val] "=r" (tag_val)626: [dbg_addr] "r" (dbg_addr), [dbg_val] "r" (debug_val), [tag_addr] "r" (debug_tag_addr)627: "memory");628629local_irq_restore(flags);630631return tag_val;632}633634635union cvmx_l2c_tag cvmx_l2c_get_tag(uint32_t association, uint32_t index)636{637union cvmx_l2c_tag tag;638tag.u64 = 0;639640if ((int)association >= cvmx_l2c_get_num_assoc()) {641cvmx_dprintf("ERROR: cvmx_l2c_get_tag association out of range\n");642return tag;643}644if ((int)index >= cvmx_l2c_get_num_sets()) {645cvmx_dprintf("ERROR: cvmx_l2c_get_tag index out of range (arg: %d, max: %d)\n",646(int)index, cvmx_l2c_get_num_sets());647return tag;648}649if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {650union cvmx_l2c_tadx_tag l2c_tadx_tag;651uint64_t address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,652(association << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) |653(index << CVMX_L2C_IDX_ADDR_SHIFT));654/*655* Use L2 cache Index load tag cache instruction, as656* hardware loads the virtual tag for the L2 cache657* block with the contents of L2C_TAD0_TAG658* register.659*/660CVMX_CACHE_LTGL2I(address, 0);661CVMX_SYNC; /* make sure CVMX_L2C_TADX_TAG is updated */662l2c_tadx_tag.u64 = cvmx_read_csr(CVMX_L2C_TADX_TAG(0));663664tag.s.V = l2c_tadx_tag.s.valid;665tag.s.D = l2c_tadx_tag.s.dirty;666tag.s.L = l2c_tadx_tag.s.lock;667tag.s.U = l2c_tadx_tag.s.use;668tag.s.addr = l2c_tadx_tag.s.tag;669} else {670union __cvmx_l2c_tag tmp_tag;671/* __read_l2_tag is intended for internal use only */672tmp_tag = __read_l2_tag(association, index);673674/*675* Convert all tag structure types to generic version,676* as it can represent all models.677*/678if (OCTEON_IS_MODEL(OCTEON_CN58XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {679tag.s.V = tmp_tag.cn58xx.V;680tag.s.D = tmp_tag.cn58xx.D;681tag.s.L = tmp_tag.cn58xx.L;682tag.s.U = tmp_tag.cn58xx.U;683tag.s.addr = tmp_tag.cn58xx.addr;684} else if (OCTEON_IS_MODEL(OCTEON_CN38XX)) {685tag.s.V = tmp_tag.cn38xx.V;686tag.s.D = tmp_tag.cn38xx.D;687tag.s.L = tmp_tag.cn38xx.L;688tag.s.U = tmp_tag.cn38xx.U;689tag.s.addr = tmp_tag.cn38xx.addr;690} else if (OCTEON_IS_MODEL(OCTEON_CN31XX) || OCTEON_IS_MODEL(OCTEON_CN52XX)) {691tag.s.V = tmp_tag.cn31xx.V;692tag.s.D = tmp_tag.cn31xx.D;693tag.s.L = tmp_tag.cn31xx.L;694tag.s.U = tmp_tag.cn31xx.U;695tag.s.addr = tmp_tag.cn31xx.addr;696} else if (OCTEON_IS_MODEL(OCTEON_CN30XX)) {697tag.s.V = tmp_tag.cn30xx.V;698tag.s.D = tmp_tag.cn30xx.D;699tag.s.L = tmp_tag.cn30xx.L;700tag.s.U = tmp_tag.cn30xx.U;701tag.s.addr = tmp_tag.cn30xx.addr;702} else if (OCTEON_IS_MODEL(OCTEON_CN50XX)) {703tag.s.V = tmp_tag.cn50xx.V;704tag.s.D = tmp_tag.cn50xx.D;705tag.s.L = tmp_tag.cn50xx.L;706tag.s.U = tmp_tag.cn50xx.U;707tag.s.addr = tmp_tag.cn50xx.addr;708} else {709cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);710}711}712return tag;713}714715uint32_t cvmx_l2c_address_to_index(uint64_t addr)716{717uint64_t idx = addr >> CVMX_L2C_IDX_ADDR_SHIFT;718int indxalias = 0;719720if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {721union cvmx_l2c_ctl l2c_ctl;722l2c_ctl.u64 = cvmx_read_csr(CVMX_L2C_CTL);723indxalias = !l2c_ctl.s.disidxalias;724} else {725union cvmx_l2c_cfg l2c_cfg;726l2c_cfg.u64 = cvmx_read_csr(CVMX_L2C_CFG);727indxalias = l2c_cfg.s.idxalias;728}729730if (indxalias) {731if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {732uint32_t a_14_12 = (idx / (CVMX_L2C_MEMBANK_SELECT_SIZE/(1<<CVMX_L2C_IDX_ADDR_SHIFT))) & 0x7;733idx ^= idx / cvmx_l2c_get_num_sets();734idx ^= a_14_12;735} else {736idx ^= ((addr & CVMX_L2C_ALIAS_MASK) >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT);737}738}739idx &= CVMX_L2C_IDX_MASK;740return idx;741}742743int cvmx_l2c_get_cache_size_bytes(void)744{745return cvmx_l2c_get_num_sets() * cvmx_l2c_get_num_assoc() *746CVMX_CACHE_LINE_SIZE;747}748749/**750* Return log base 2 of the number of sets in the L2 cache751* Returns752*/753int cvmx_l2c_get_set_bits(void)754{755int l2_set_bits;756if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN58XX))757l2_set_bits = 11; /* 2048 sets */758else if (OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN63XX))759l2_set_bits = 10; /* 1024 sets */760else if (OCTEON_IS_MODEL(OCTEON_CN31XX) || OCTEON_IS_MODEL(OCTEON_CN52XX))761l2_set_bits = 9; /* 512 sets */762else if (OCTEON_IS_MODEL(OCTEON_CN30XX))763l2_set_bits = 8; /* 256 sets */764else if (OCTEON_IS_MODEL(OCTEON_CN50XX))765l2_set_bits = 7; /* 128 sets */766else {767cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);768l2_set_bits = 11; /* 2048 sets */769}770return l2_set_bits;771}772773/* Return the number of sets in the L2 Cache */774int cvmx_l2c_get_num_sets(void)775{776return 1 << cvmx_l2c_get_set_bits();777}778779/* Return the number of associations in the L2 Cache */780int cvmx_l2c_get_num_assoc(void)781{782int l2_assoc;783if (OCTEON_IS_MODEL(OCTEON_CN56XX) ||784OCTEON_IS_MODEL(OCTEON_CN52XX) ||785OCTEON_IS_MODEL(OCTEON_CN58XX) ||786OCTEON_IS_MODEL(OCTEON_CN50XX) ||787OCTEON_IS_MODEL(OCTEON_CN38XX))788l2_assoc = 8;789else if (OCTEON_IS_MODEL(OCTEON_CN63XX))790l2_assoc = 16;791else if (OCTEON_IS_MODEL(OCTEON_CN31XX) ||792OCTEON_IS_MODEL(OCTEON_CN30XX))793l2_assoc = 4;794else {795cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);796l2_assoc = 8;797}798799/* Check to see if part of the cache is disabled */800if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {801union cvmx_mio_fus_dat3 mio_fus_dat3;802803mio_fus_dat3.u64 = cvmx_read_csr(CVMX_MIO_FUS_DAT3);804/*805* cvmx_mio_fus_dat3.s.l2c_crip fuses map as follows806* <2> will be not used for 63xx807* <1> disables 1/2 ways808* <0> disables 1/4 ways809* They are cumulative, so for 63xx:810* <1> <0>811* 0 0 16-way 2MB cache812* 0 1 12-way 1.5MB cache813* 1 0 8-way 1MB cache814* 1 1 4-way 512KB cache815*/816817if (mio_fus_dat3.s.l2c_crip == 3)818l2_assoc = 4;819else if (mio_fus_dat3.s.l2c_crip == 2)820l2_assoc = 8;821else if (mio_fus_dat3.s.l2c_crip == 1)822l2_assoc = 12;823} else {824union cvmx_l2d_fus3 val;825val.u64 = cvmx_read_csr(CVMX_L2D_FUS3);826/*827* Using shifts here, as bit position names are828* different for each model but they all mean the829* same.830*/831if ((val.u64 >> 35) & 0x1)832l2_assoc = l2_assoc >> 2;833else if ((val.u64 >> 34) & 0x1)834l2_assoc = l2_assoc >> 1;835}836return l2_assoc;837}838839/**840* Flush a line from the L2 cache841* This should only be called from one core at a time, as this routine842* sets the core to the 'debug' core in order to flush the line.843*844* @assoc: Association (or way) to flush845* @index: Index to flush846*/847void cvmx_l2c_flush_line(uint32_t assoc, uint32_t index)848{849/* Check the range of the index. */850if (index > (uint32_t)cvmx_l2c_get_num_sets()) {851cvmx_dprintf("ERROR: cvmx_l2c_flush_line index out of range.\n");852return;853}854855/* Check the range of association. */856if (assoc > (uint32_t)cvmx_l2c_get_num_assoc()) {857cvmx_dprintf("ERROR: cvmx_l2c_flush_line association out of range.\n");858return;859}860861if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {862uint64_t address;863/* Create the address based on index and association.864* Bits<20:17> select the way of the cache block involved in865* the operation866* Bits<16:7> of the effect address select the index867*/868address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,869(assoc << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) |870(index << CVMX_L2C_IDX_ADDR_SHIFT));871CVMX_CACHE_WBIL2I(address, 0);872} else {873union cvmx_l2c_dbg l2cdbg;874875l2cdbg.u64 = 0;876if (!OCTEON_IS_MODEL(OCTEON_CN30XX))877l2cdbg.s.ppnum = cvmx_get_core_num();878l2cdbg.s.finv = 1;879880l2cdbg.s.set = assoc;881cvmx_spinlock_lock(&cvmx_l2c_spinlock);882/*883* Enter debug mode, and make sure all other writes884* complete before we enter debug mode885*/886CVMX_SYNC;887cvmx_write_csr(CVMX_L2C_DBG, l2cdbg.u64);888cvmx_read_csr(CVMX_L2C_DBG);889890CVMX_PREPARE_FOR_STORE(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,891index * CVMX_CACHE_LINE_SIZE),8920);893/* Exit debug mode */894CVMX_SYNC;895cvmx_write_csr(CVMX_L2C_DBG, 0);896cvmx_read_csr(CVMX_L2C_DBG);897cvmx_spinlock_unlock(&cvmx_l2c_spinlock);898}899}900901902