1 /***********************license start***************
2 * Author: Cavium Networks
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
7 * Copyright (c) 2003-2010 Cavium Networks
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this file; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * or visit http://www.gnu.org/licenses/.
24 * This file may also be available under a different license from Cavium.
25 * Contact Cavium Networks for more information
26 ***********************license end**************************************/
29 * Implementation of the Level 2 Cache (L2C) control,
30 * measurement, and debugging facilities.
33 #include <linux/compiler.h>
34 #include <linux/irqflags.h>
35 #include <asm/octeon/cvmx.h>
36 #include <asm/octeon/cvmx-l2c.h>
37 #include <asm/octeon/cvmx-spinlock.h>
40 * This spinlock is used internally to ensure that only one core is
41 * performing certain L2 operations at a time.
43 * NOTE: This only protects calls from within a single application -
44 * if multiple applications or operating systems are running, then it
45 * is up to the user program to coordinate between them.
47 cvmx_spinlock_t cvmx_l2c_spinlock
;
49 int cvmx_l2c_get_core_way_partition(uint32_t core
)
53 /* Validate the core number */
54 if (core
>= cvmx_octeon_num_cores())
57 if (OCTEON_IS_MODEL(OCTEON_CN63XX
))
58 return cvmx_read_csr(CVMX_L2C_WPAR_PPX(core
)) & 0xffff;
61 * Use the lower two bits of the coreNumber to determine the
62 * bit offset of the UMSK[] field in the L2C_SPAR register.
64 field
= (core
& 0x3) * 8;
67 * Return the UMSK[] field from the appropriate L2C_SPAR
68 * register based on the coreNumber.
73 return (cvmx_read_csr(CVMX_L2C_SPAR0
) & (0xFF << field
)) >> field
;
75 return (cvmx_read_csr(CVMX_L2C_SPAR1
) & (0xFF << field
)) >> field
;
77 return (cvmx_read_csr(CVMX_L2C_SPAR2
) & (0xFF << field
)) >> field
;
79 return (cvmx_read_csr(CVMX_L2C_SPAR3
) & (0xFF << field
)) >> field
;
84 int cvmx_l2c_set_core_way_partition(uint32_t core
, uint32_t mask
)
89 valid_mask
= (0x1 << cvmx_l2c_get_num_assoc()) - 1;
93 /* A UMSK setting which blocks all L2C Ways is an error on some chips */
94 if (mask
== valid_mask
&& !OCTEON_IS_MODEL(OCTEON_CN63XX
))
97 /* Validate the core number */
98 if (core
>= cvmx_octeon_num_cores())
101 if (OCTEON_IS_MODEL(OCTEON_CN63XX
)) {
102 cvmx_write_csr(CVMX_L2C_WPAR_PPX(core
), mask
);
107 * Use the lower two bits of core to determine the bit offset of the
108 * UMSK[] field in the L2C_SPAR register.
110 field
= (core
& 0x3) * 8;
113 * Assign the new mask setting to the UMSK[] field in the appropriate
114 * L2C_SPAR register based on the core_num.
117 switch (core
& 0xC) {
119 cvmx_write_csr(CVMX_L2C_SPAR0
,
120 (cvmx_read_csr(CVMX_L2C_SPAR0
) & ~(0xFF << field
)) |
124 cvmx_write_csr(CVMX_L2C_SPAR1
,
125 (cvmx_read_csr(CVMX_L2C_SPAR1
) & ~(0xFF << field
)) |
129 cvmx_write_csr(CVMX_L2C_SPAR2
,
130 (cvmx_read_csr(CVMX_L2C_SPAR2
) & ~(0xFF << field
)) |
134 cvmx_write_csr(CVMX_L2C_SPAR3
,
135 (cvmx_read_csr(CVMX_L2C_SPAR3
) & ~(0xFF << field
)) |
142 int cvmx_l2c_set_hw_way_partition(uint32_t mask
)
146 valid_mask
= (0x1 << cvmx_l2c_get_num_assoc()) - 1;
149 /* A UMSK setting which blocks all L2C Ways is an error on some chips */
150 if (mask
== valid_mask
&& !OCTEON_IS_MODEL(OCTEON_CN63XX
))
153 if (OCTEON_IS_MODEL(OCTEON_CN63XX
))
154 cvmx_write_csr(CVMX_L2C_WPAR_IOBX(0), mask
);
156 cvmx_write_csr(CVMX_L2C_SPAR4
,
157 (cvmx_read_csr(CVMX_L2C_SPAR4
) & ~0xFF) | mask
);
161 int cvmx_l2c_get_hw_way_partition(void)
163 if (OCTEON_IS_MODEL(OCTEON_CN63XX
))
164 return cvmx_read_csr(CVMX_L2C_WPAR_IOBX(0)) & 0xffff;
166 return cvmx_read_csr(CVMX_L2C_SPAR4
) & (0xFF);
169 void cvmx_l2c_config_perf(uint32_t counter
, enum cvmx_l2c_event event
,
170 uint32_t clear_on_read
)
172 if (OCTEON_IS_MODEL(OCTEON_CN5XXX
) || OCTEON_IS_MODEL(OCTEON_CN3XXX
)) {
173 union cvmx_l2c_pfctl pfctl
;
175 pfctl
.u64
= cvmx_read_csr(CVMX_L2C_PFCTL
);
179 pfctl
.s
.cnt0sel
= event
;
181 pfctl
.s
.cnt0rdclr
= clear_on_read
;
184 pfctl
.s
.cnt1sel
= event
;
186 pfctl
.s
.cnt1rdclr
= clear_on_read
;
189 pfctl
.s
.cnt2sel
= event
;
191 pfctl
.s
.cnt2rdclr
= clear_on_read
;
195 pfctl
.s
.cnt3sel
= event
;
197 pfctl
.s
.cnt3rdclr
= clear_on_read
;
201 cvmx_write_csr(CVMX_L2C_PFCTL
, pfctl
.u64
);
203 union cvmx_l2c_tadx_prf l2c_tadx_prf
;
206 cvmx_dprintf("L2C performance counter events are different for this chip, mapping 'event' to cvmx_l2c_tad_event_t\n");
208 cvmx_dprintf("L2C counters don't support clear on read for this chip\n");
210 l2c_tadx_prf
.u64
= cvmx_read_csr(CVMX_L2C_TADX_PRF(0));
214 l2c_tadx_prf
.s
.cnt0sel
= event
;
217 l2c_tadx_prf
.s
.cnt1sel
= event
;
220 l2c_tadx_prf
.s
.cnt2sel
= event
;
224 l2c_tadx_prf
.s
.cnt3sel
= event
;
227 for (tad
= 0; tad
< CVMX_L2C_TADS
; tad
++)
228 cvmx_write_csr(CVMX_L2C_TADX_PRF(tad
),
233 uint64_t cvmx_l2c_read_perf(uint32_t counter
)
237 if (OCTEON_IS_MODEL(OCTEON_CN5XXX
) || OCTEON_IS_MODEL(OCTEON_CN3XXX
))
238 return cvmx_read_csr(CVMX_L2C_PFC0
);
240 uint64_t counter
= 0;
242 for (tad
= 0; tad
< CVMX_L2C_TADS
; tad
++)
243 counter
+= cvmx_read_csr(CVMX_L2C_TADX_PFC0(tad
));
247 if (OCTEON_IS_MODEL(OCTEON_CN5XXX
) || OCTEON_IS_MODEL(OCTEON_CN3XXX
))
248 return cvmx_read_csr(CVMX_L2C_PFC1
);
250 uint64_t counter
= 0;
252 for (tad
= 0; tad
< CVMX_L2C_TADS
; tad
++)
253 counter
+= cvmx_read_csr(CVMX_L2C_TADX_PFC1(tad
));
257 if (OCTEON_IS_MODEL(OCTEON_CN5XXX
) || OCTEON_IS_MODEL(OCTEON_CN3XXX
))
258 return cvmx_read_csr(CVMX_L2C_PFC2
);
260 uint64_t counter
= 0;
262 for (tad
= 0; tad
< CVMX_L2C_TADS
; tad
++)
263 counter
+= cvmx_read_csr(CVMX_L2C_TADX_PFC2(tad
));
268 if (OCTEON_IS_MODEL(OCTEON_CN5XXX
) || OCTEON_IS_MODEL(OCTEON_CN3XXX
))
269 return cvmx_read_csr(CVMX_L2C_PFC3
);
271 uint64_t counter
= 0;
273 for (tad
= 0; tad
< CVMX_L2C_TADS
; tad
++)
274 counter
+= cvmx_read_csr(CVMX_L2C_TADX_PFC3(tad
));
282 * Helper function use to fault in cache lines for L2 cache locking
284 * @addr: Address of base of memory region to read into L2 cache
285 * @len: Length (in bytes) of region to fault in
287 static void fault_in(uint64_t addr
, int len
)
292 * Adjust addr and length so we get all cache lines even for
293 * small ranges spanning two cache lines.
295 len
+= addr
& CVMX_CACHE_LINE_MASK
;
296 addr
&= ~CVMX_CACHE_LINE_MASK
;
297 ptr
= cvmx_phys_to_ptr(addr
);
299 * Invalidate L1 cache to make sure all loads result in data
302 CVMX_DCACHE_INVALIDATE
;
305 len
-= CVMX_CACHE_LINE_SIZE
;
306 ptr
+= CVMX_CACHE_LINE_SIZE
;
310 int cvmx_l2c_lock_line(uint64_t addr
)
312 if (OCTEON_IS_MODEL(OCTEON_CN63XX
)) {
313 int shift
= CVMX_L2C_TAG_ADDR_ALIAS_SHIFT
;
314 uint64_t assoc
= cvmx_l2c_get_num_assoc();
315 uint64_t tag
= addr
>> shift
;
316 uint64_t index
= CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
, cvmx_l2c_address_to_index(addr
) << CVMX_L2C_IDX_ADDR_SHIFT
);
318 union cvmx_l2c_tadx_tag l2c_tadx_tag
;
320 CVMX_CACHE_LCKL2(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
, addr
), 0);
322 /* Make sure we were able to lock the line */
323 for (way
= 0; way
< assoc
; way
++) {
324 CVMX_CACHE_LTGL2I(index
| (way
<< shift
), 0);
325 /* make sure CVMX_L2C_TADX_TAG is updated */
327 l2c_tadx_tag
.u64
= cvmx_read_csr(CVMX_L2C_TADX_TAG(0));
328 if (l2c_tadx_tag
.s
.valid
&& l2c_tadx_tag
.s
.tag
== tag
)
332 /* Check if a valid line is found */
334 /* cvmx_dprintf("ERROR: cvmx_l2c_lock_line: line not found for locking at 0x%llx address\n", (unsigned long long)addr); */
338 /* Check if lock bit is not set */
339 if (!l2c_tadx_tag
.s
.lock
) {
340 /* cvmx_dprintf("ERROR: cvmx_l2c_lock_line: Not able to lock at 0x%llx address\n", (unsigned long long)addr); */
346 union cvmx_l2c_dbg l2cdbg
;
347 union cvmx_l2c_lckbase lckbase
;
348 union cvmx_l2c_lckoff lckoff
;
349 union cvmx_l2t_err l2t_err
;
351 cvmx_spinlock_lock(&cvmx_l2c_spinlock
);
357 /* Clear l2t error bits if set */
358 l2t_err
.u64
= cvmx_read_csr(CVMX_L2T_ERR
);
359 l2t_err
.s
.lckerr
= 1;
360 l2t_err
.s
.lckerr2
= 1;
361 cvmx_write_csr(CVMX_L2T_ERR
, l2t_err
.u64
);
363 addr
&= ~CVMX_CACHE_LINE_MASK
;
365 /* Set this core as debug core */
366 l2cdbg
.s
.ppnum
= cvmx_get_core_num();
368 cvmx_write_csr(CVMX_L2C_DBG
, l2cdbg
.u64
);
369 cvmx_read_csr(CVMX_L2C_DBG
);
371 lckoff
.s
.lck_offset
= 0; /* Only lock 1 line at a time */
372 cvmx_write_csr(CVMX_L2C_LCKOFF
, lckoff
.u64
);
373 cvmx_read_csr(CVMX_L2C_LCKOFF
);
375 if (((union cvmx_l2c_cfg
)(cvmx_read_csr(CVMX_L2C_CFG
))).s
.idxalias
) {
376 int alias_shift
= CVMX_L2C_IDX_ADDR_SHIFT
+ 2 * CVMX_L2_SET_BITS
- 1;
377 uint64_t addr_tmp
= addr
^ (addr
& ((1 << alias_shift
) - 1)) >> CVMX_L2_SET_BITS
;
378 lckbase
.s
.lck_base
= addr_tmp
>> 7;
380 lckbase
.s
.lck_base
= addr
>> 7;
383 lckbase
.s
.lck_ena
= 1;
384 cvmx_write_csr(CVMX_L2C_LCKBASE
, lckbase
.u64
);
385 /* Make sure it gets there */
386 cvmx_read_csr(CVMX_L2C_LCKBASE
);
388 fault_in(addr
, CVMX_CACHE_LINE_SIZE
);
390 lckbase
.s
.lck_ena
= 0;
391 cvmx_write_csr(CVMX_L2C_LCKBASE
, lckbase
.u64
);
392 /* Make sure it gets there */
393 cvmx_read_csr(CVMX_L2C_LCKBASE
);
395 /* Stop being debug core */
396 cvmx_write_csr(CVMX_L2C_DBG
, 0);
397 cvmx_read_csr(CVMX_L2C_DBG
);
399 l2t_err
.u64
= cvmx_read_csr(CVMX_L2T_ERR
);
400 if (l2t_err
.s
.lckerr
|| l2t_err
.s
.lckerr2
)
401 retval
= 1; /* We were unable to lock the line */
403 cvmx_spinlock_unlock(&cvmx_l2c_spinlock
);
408 int cvmx_l2c_lock_mem_region(uint64_t start
, uint64_t len
)
412 /* Round start/end to cache line boundaries */
413 len
+= start
& CVMX_CACHE_LINE_MASK
;
414 start
&= ~CVMX_CACHE_LINE_MASK
;
415 len
= (len
+ CVMX_CACHE_LINE_MASK
) & ~CVMX_CACHE_LINE_MASK
;
418 retval
+= cvmx_l2c_lock_line(start
);
419 start
+= CVMX_CACHE_LINE_SIZE
;
420 len
-= CVMX_CACHE_LINE_SIZE
;
425 void cvmx_l2c_flush(void)
428 uint64_t n_assoc
, n_set
;
430 n_set
= cvmx_l2c_get_num_sets();
431 n_assoc
= cvmx_l2c_get_num_assoc();
433 if (OCTEON_IS_MODEL(OCTEON_CN6XXX
)) {
435 /* These may look like constants, but they aren't... */
436 int assoc_shift
= CVMX_L2C_TAG_ADDR_ALIAS_SHIFT
;
437 int set_shift
= CVMX_L2C_IDX_ADDR_SHIFT
;
438 for (set
= 0; set
< n_set
; set
++) {
439 for (assoc
= 0; assoc
< n_assoc
; assoc
++) {
440 address
= CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
441 (assoc
<< assoc_shift
) | (set
<< set_shift
));
442 CVMX_CACHE_WBIL2I(address
, 0);
446 for (set
= 0; set
< n_set
; set
++)
447 for (assoc
= 0; assoc
< n_assoc
; assoc
++)
448 cvmx_l2c_flush_line(assoc
, set
);
453 int cvmx_l2c_unlock_line(uint64_t address
)
456 if (OCTEON_IS_MODEL(OCTEON_CN63XX
)) {
458 union cvmx_l2c_tag tag
;
460 uint32_t index
= cvmx_l2c_address_to_index(address
);
462 tag_addr
= ((address
>> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT
) & ((1 << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT
) - 1));
465 * For 63XX, we can flush a line by using the physical
466 * address directly, so finding the cache line used by
467 * the address is only required to provide the proper
468 * return value for the function.
470 for (assoc
= 0; assoc
< CVMX_L2_ASSOC
; assoc
++) {
471 tag
= cvmx_l2c_get_tag(assoc
, index
);
473 if (tag
.s
.V
&& (tag
.s
.addr
== tag_addr
)) {
474 CVMX_CACHE_WBIL2(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
, address
), 0);
480 union cvmx_l2c_tag tag
;
483 uint32_t index
= cvmx_l2c_address_to_index(address
);
485 /* Compute portion of address that is stored in tag */
486 tag_addr
= ((address
>> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT
) & ((1 << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT
) - 1));
487 for (assoc
= 0; assoc
< CVMX_L2_ASSOC
; assoc
++) {
488 tag
= cvmx_l2c_get_tag(assoc
, index
);
490 if (tag
.s
.V
&& (tag
.s
.addr
== tag_addr
)) {
491 cvmx_l2c_flush_line(assoc
, index
);
499 int cvmx_l2c_unlock_mem_region(uint64_t start
, uint64_t len
)
501 int num_unlocked
= 0;
502 /* Round start/end to cache line boundaries */
503 len
+= start
& CVMX_CACHE_LINE_MASK
;
504 start
&= ~CVMX_CACHE_LINE_MASK
;
505 len
= (len
+ CVMX_CACHE_LINE_MASK
) & ~CVMX_CACHE_LINE_MASK
;
507 num_unlocked
+= cvmx_l2c_unlock_line(start
);
508 start
+= CVMX_CACHE_LINE_SIZE
;
509 len
-= CVMX_CACHE_LINE_SIZE
;
516 * Internal l2c tag types. These are converted to a generic structure
517 * that can be used on all chips.
519 union __cvmx_l2c_tag
{
521 struct cvmx_l2c_tag_cn50xx
{
522 #ifdef __BIG_ENDIAN_BITFIELD
523 uint64_t reserved
:40;
524 uint64_t V
:1; /* Line valid */
525 uint64_t D
:1; /* Line dirty */
526 uint64_t L
:1; /* Line locked */
527 uint64_t U
:1; /* Use, LRU eviction */
528 uint64_t addr
:20; /* Phys mem addr (33..14) */
530 uint64_t addr
:20; /* Phys mem addr (33..14) */
531 uint64_t U
:1; /* Use, LRU eviction */
532 uint64_t L
:1; /* Line locked */
533 uint64_t D
:1; /* Line dirty */
534 uint64_t V
:1; /* Line valid */
535 uint64_t reserved
:40;
538 struct cvmx_l2c_tag_cn30xx
{
539 #ifdef __BIG_ENDIAN_BITFIELD
540 uint64_t reserved
:41;
541 uint64_t V
:1; /* Line valid */
542 uint64_t D
:1; /* Line dirty */
543 uint64_t L
:1; /* Line locked */
544 uint64_t U
:1; /* Use, LRU eviction */
545 uint64_t addr
:19; /* Phys mem addr (33..15) */
547 uint64_t addr
:19; /* Phys mem addr (33..15) */
548 uint64_t U
:1; /* Use, LRU eviction */
549 uint64_t L
:1; /* Line locked */
550 uint64_t D
:1; /* Line dirty */
551 uint64_t V
:1; /* Line valid */
552 uint64_t reserved
:41;
555 struct cvmx_l2c_tag_cn31xx
{
556 #ifdef __BIG_ENDIAN_BITFIELD
557 uint64_t reserved
:42;
558 uint64_t V
:1; /* Line valid */
559 uint64_t D
:1; /* Line dirty */
560 uint64_t L
:1; /* Line locked */
561 uint64_t U
:1; /* Use, LRU eviction */
562 uint64_t addr
:18; /* Phys mem addr (33..16) */
564 uint64_t addr
:18; /* Phys mem addr (33..16) */
565 uint64_t U
:1; /* Use, LRU eviction */
566 uint64_t L
:1; /* Line locked */
567 uint64_t D
:1; /* Line dirty */
568 uint64_t V
:1; /* Line valid */
569 uint64_t reserved
:42;
572 struct cvmx_l2c_tag_cn38xx
{
573 #ifdef __BIG_ENDIAN_BITFIELD
574 uint64_t reserved
:43;
575 uint64_t V
:1; /* Line valid */
576 uint64_t D
:1; /* Line dirty */
577 uint64_t L
:1; /* Line locked */
578 uint64_t U
:1; /* Use, LRU eviction */
579 uint64_t addr
:17; /* Phys mem addr (33..17) */
581 uint64_t addr
:17; /* Phys mem addr (33..17) */
582 uint64_t U
:1; /* Use, LRU eviction */
583 uint64_t L
:1; /* Line locked */
584 uint64_t D
:1; /* Line dirty */
585 uint64_t V
:1; /* Line valid */
586 uint64_t reserved
:43;
589 struct cvmx_l2c_tag_cn58xx
{
590 #ifdef __BIG_ENDIAN_BITFIELD
591 uint64_t reserved
:44;
592 uint64_t V
:1; /* Line valid */
593 uint64_t D
:1; /* Line dirty */
594 uint64_t L
:1; /* Line locked */
595 uint64_t U
:1; /* Use, LRU eviction */
596 uint64_t addr
:16; /* Phys mem addr (33..18) */
598 uint64_t addr
:16; /* Phys mem addr (33..18) */
599 uint64_t U
:1; /* Use, LRU eviction */
600 uint64_t L
:1; /* Line locked */
601 uint64_t D
:1; /* Line dirty */
602 uint64_t V
:1; /* Line valid */
603 uint64_t reserved
:44;
606 struct cvmx_l2c_tag_cn58xx cn56xx
; /* 2048 sets */
607 struct cvmx_l2c_tag_cn31xx cn52xx
; /* 512 sets */
613 * Function to read a L2C tag. This code make the current core
614 * the 'debug core' for the L2. This code must only be executed by
617 * @assoc: Association (way) of the tag to dump
618 * @index: Index of the cacheline
620 * Returns The Octeon model specific tag structure. This is
621 * translated by a wrapper function to a generic form that is
622 * easier for applications to use.
624 static union __cvmx_l2c_tag
__read_l2_tag(uint64_t assoc
, uint64_t index
)
627 uint64_t debug_tag_addr
= CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
, (index
<< 7) + 96);
628 uint64_t core
= cvmx_get_core_num();
629 union __cvmx_l2c_tag tag_val
;
630 uint64_t dbg_addr
= CVMX_L2C_DBG
;
633 union cvmx_l2c_dbg debug_val
;
636 * For low core count parts, the core number is always small
637 * enough to stay in the correct field and not set any
640 debug_val
.s
.ppnum
= core
;
642 debug_val
.s
.set
= assoc
;
644 local_irq_save(flags
);
646 * Make sure core is quiet (no prefetches, etc.) before
647 * entering debug mode.
650 /* Flush L1 to make sure debug load misses L1 */
651 CVMX_DCACHE_INVALIDATE
;
654 * The following must be done in assembly as when in debug
655 * mode all data loads from L2 return special debug data, not
656 * normal memory contents. Also, interrupts must be disabled,
657 * since if an interrupt occurs while in debug mode the ISR
658 * will get debug data from all its memory * reads instead of
659 * the contents of memory.
666 "sd %[dbg_val], 0(%[dbg_addr])\n\t" /* Enter debug mode, wait for store */
667 "ld $0, 0(%[dbg_addr])\n\t"
668 "ld %[tag_val], 0(%[tag_addr])\n\t" /* Read L2C tag data */
669 "sd $0, 0(%[dbg_addr])\n\t" /* Exit debug mode, wait for store */
670 "ld $0, 0(%[dbg_addr])\n\t"
671 "cache 9, 0($0)\n\t" /* Invalidate dcache to discard debug data */
673 : [tag_val
] "=r" (tag_val
)
674 : [dbg_addr
] "r" (dbg_addr
), [dbg_val
] "r" (debug_val
), [tag_addr
] "r" (debug_tag_addr
)
677 local_irq_restore(flags
);
683 union cvmx_l2c_tag
cvmx_l2c_get_tag(uint32_t association
, uint32_t index
)
685 union cvmx_l2c_tag tag
;
688 if ((int)association
>= cvmx_l2c_get_num_assoc()) {
689 cvmx_dprintf("ERROR: cvmx_l2c_get_tag association out of range\n");
692 if ((int)index
>= cvmx_l2c_get_num_sets()) {
693 cvmx_dprintf("ERROR: cvmx_l2c_get_tag index out of range (arg: %d, max: %d)\n",
694 (int)index
, cvmx_l2c_get_num_sets());
697 if (OCTEON_IS_MODEL(OCTEON_CN63XX
)) {
698 union cvmx_l2c_tadx_tag l2c_tadx_tag
;
699 uint64_t address
= CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
700 (association
<< CVMX_L2C_TAG_ADDR_ALIAS_SHIFT
) |
701 (index
<< CVMX_L2C_IDX_ADDR_SHIFT
));
703 * Use L2 cache Index load tag cache instruction, as
704 * hardware loads the virtual tag for the L2 cache
705 * block with the contents of L2C_TAD0_TAG
708 CVMX_CACHE_LTGL2I(address
, 0);
709 CVMX_SYNC
; /* make sure CVMX_L2C_TADX_TAG is updated */
710 l2c_tadx_tag
.u64
= cvmx_read_csr(CVMX_L2C_TADX_TAG(0));
712 tag
.s
.V
= l2c_tadx_tag
.s
.valid
;
713 tag
.s
.D
= l2c_tadx_tag
.s
.dirty
;
714 tag
.s
.L
= l2c_tadx_tag
.s
.lock
;
715 tag
.s
.U
= l2c_tadx_tag
.s
.use
;
716 tag
.s
.addr
= l2c_tadx_tag
.s
.tag
;
718 union __cvmx_l2c_tag tmp_tag
;
719 /* __read_l2_tag is intended for internal use only */
720 tmp_tag
= __read_l2_tag(association
, index
);
723 * Convert all tag structure types to generic version,
724 * as it can represent all models.
726 if (OCTEON_IS_MODEL(OCTEON_CN58XX
) || OCTEON_IS_MODEL(OCTEON_CN56XX
)) {
727 tag
.s
.V
= tmp_tag
.cn58xx
.V
;
728 tag
.s
.D
= tmp_tag
.cn58xx
.D
;
729 tag
.s
.L
= tmp_tag
.cn58xx
.L
;
730 tag
.s
.U
= tmp_tag
.cn58xx
.U
;
731 tag
.s
.addr
= tmp_tag
.cn58xx
.addr
;
732 } else if (OCTEON_IS_MODEL(OCTEON_CN38XX
)) {
733 tag
.s
.V
= tmp_tag
.cn38xx
.V
;
734 tag
.s
.D
= tmp_tag
.cn38xx
.D
;
735 tag
.s
.L
= tmp_tag
.cn38xx
.L
;
736 tag
.s
.U
= tmp_tag
.cn38xx
.U
;
737 tag
.s
.addr
= tmp_tag
.cn38xx
.addr
;
738 } else if (OCTEON_IS_MODEL(OCTEON_CN31XX
) || OCTEON_IS_MODEL(OCTEON_CN52XX
)) {
739 tag
.s
.V
= tmp_tag
.cn31xx
.V
;
740 tag
.s
.D
= tmp_tag
.cn31xx
.D
;
741 tag
.s
.L
= tmp_tag
.cn31xx
.L
;
742 tag
.s
.U
= tmp_tag
.cn31xx
.U
;
743 tag
.s
.addr
= tmp_tag
.cn31xx
.addr
;
744 } else if (OCTEON_IS_MODEL(OCTEON_CN30XX
)) {
745 tag
.s
.V
= tmp_tag
.cn30xx
.V
;
746 tag
.s
.D
= tmp_tag
.cn30xx
.D
;
747 tag
.s
.L
= tmp_tag
.cn30xx
.L
;
748 tag
.s
.U
= tmp_tag
.cn30xx
.U
;
749 tag
.s
.addr
= tmp_tag
.cn30xx
.addr
;
750 } else if (OCTEON_IS_MODEL(OCTEON_CN50XX
)) {
751 tag
.s
.V
= tmp_tag
.cn50xx
.V
;
752 tag
.s
.D
= tmp_tag
.cn50xx
.D
;
753 tag
.s
.L
= tmp_tag
.cn50xx
.L
;
754 tag
.s
.U
= tmp_tag
.cn50xx
.U
;
755 tag
.s
.addr
= tmp_tag
.cn50xx
.addr
;
757 cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__
);
763 uint32_t cvmx_l2c_address_to_index(uint64_t addr
)
765 uint64_t idx
= addr
>> CVMX_L2C_IDX_ADDR_SHIFT
;
768 if (OCTEON_IS_MODEL(OCTEON_CN6XXX
)) {
769 union cvmx_l2c_ctl l2c_ctl
;
770 l2c_ctl
.u64
= cvmx_read_csr(CVMX_L2C_CTL
);
771 indxalias
= !l2c_ctl
.s
.disidxalias
;
773 union cvmx_l2c_cfg l2c_cfg
;
774 l2c_cfg
.u64
= cvmx_read_csr(CVMX_L2C_CFG
);
775 indxalias
= l2c_cfg
.s
.idxalias
;
779 if (OCTEON_IS_MODEL(OCTEON_CN63XX
)) {
780 uint32_t a_14_12
= (idx
/ (CVMX_L2C_MEMBANK_SELECT_SIZE
/(1<<CVMX_L2C_IDX_ADDR_SHIFT
))) & 0x7;
781 idx
^= idx
/ cvmx_l2c_get_num_sets();
784 idx
^= ((addr
& CVMX_L2C_ALIAS_MASK
) >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT
);
787 idx
&= CVMX_L2C_IDX_MASK
;
791 int cvmx_l2c_get_cache_size_bytes(void)
793 return cvmx_l2c_get_num_sets() * cvmx_l2c_get_num_assoc() *
794 CVMX_CACHE_LINE_SIZE
;
798 * Return log base 2 of the number of sets in the L2 cache
801 int cvmx_l2c_get_set_bits(void)
804 if (OCTEON_IS_MODEL(OCTEON_CN56XX
) || OCTEON_IS_MODEL(OCTEON_CN58XX
))
805 l2_set_bits
= 11; /* 2048 sets */
806 else if (OCTEON_IS_MODEL(OCTEON_CN38XX
) || OCTEON_IS_MODEL(OCTEON_CN63XX
))
807 l2_set_bits
= 10; /* 1024 sets */
808 else if (OCTEON_IS_MODEL(OCTEON_CN31XX
) || OCTEON_IS_MODEL(OCTEON_CN52XX
))
809 l2_set_bits
= 9; /* 512 sets */
810 else if (OCTEON_IS_MODEL(OCTEON_CN30XX
))
811 l2_set_bits
= 8; /* 256 sets */
812 else if (OCTEON_IS_MODEL(OCTEON_CN50XX
))
813 l2_set_bits
= 7; /* 128 sets */
815 cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__
);
816 l2_set_bits
= 11; /* 2048 sets */
821 /* Return the number of sets in the L2 Cache */
822 int cvmx_l2c_get_num_sets(void)
824 return 1 << cvmx_l2c_get_set_bits();
827 /* Return the number of associations in the L2 Cache */
828 int cvmx_l2c_get_num_assoc(void)
831 if (OCTEON_IS_MODEL(OCTEON_CN56XX
) ||
832 OCTEON_IS_MODEL(OCTEON_CN52XX
) ||
833 OCTEON_IS_MODEL(OCTEON_CN58XX
) ||
834 OCTEON_IS_MODEL(OCTEON_CN50XX
) ||
835 OCTEON_IS_MODEL(OCTEON_CN38XX
))
837 else if (OCTEON_IS_MODEL(OCTEON_CN63XX
))
839 else if (OCTEON_IS_MODEL(OCTEON_CN31XX
) ||
840 OCTEON_IS_MODEL(OCTEON_CN30XX
))
843 cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__
);
847 /* Check to see if part of the cache is disabled */
848 if (OCTEON_IS_MODEL(OCTEON_CN63XX
)) {
849 union cvmx_mio_fus_dat3 mio_fus_dat3
;
851 mio_fus_dat3
.u64
= cvmx_read_csr(CVMX_MIO_FUS_DAT3
);
853 * cvmx_mio_fus_dat3.s.l2c_crip fuses map as follows
854 * <2> will be not used for 63xx
855 * <1> disables 1/2 ways
856 * <0> disables 1/4 ways
857 * They are cumulative, so for 63xx:
859 * 0 0 16-way 2MB cache
860 * 0 1 12-way 1.5MB cache
861 * 1 0 8-way 1MB cache
862 * 1 1 4-way 512KB cache
865 if (mio_fus_dat3
.s
.l2c_crip
== 3)
867 else if (mio_fus_dat3
.s
.l2c_crip
== 2)
869 else if (mio_fus_dat3
.s
.l2c_crip
== 1)
872 union cvmx_l2d_fus3 val
;
873 val
.u64
= cvmx_read_csr(CVMX_L2D_FUS3
);
875 * Using shifts here, as bit position names are
876 * different for each model but they all mean the
879 if ((val
.u64
>> 35) & 0x1)
880 l2_assoc
= l2_assoc
>> 2;
881 else if ((val
.u64
>> 34) & 0x1)
882 l2_assoc
= l2_assoc
>> 1;
888 * Flush a line from the L2 cache
889 * This should only be called from one core at a time, as this routine
890 * sets the core to the 'debug' core in order to flush the line.
892 * @assoc: Association (or way) to flush
893 * @index: Index to flush
895 void cvmx_l2c_flush_line(uint32_t assoc
, uint32_t index
)
897 /* Check the range of the index. */
898 if (index
> (uint32_t)cvmx_l2c_get_num_sets()) {
899 cvmx_dprintf("ERROR: cvmx_l2c_flush_line index out of range.\n");
903 /* Check the range of association. */
904 if (assoc
> (uint32_t)cvmx_l2c_get_num_assoc()) {
905 cvmx_dprintf("ERROR: cvmx_l2c_flush_line association out of range.\n");
909 if (OCTEON_IS_MODEL(OCTEON_CN63XX
)) {
911 /* Create the address based on index and association.
912 * Bits<20:17> select the way of the cache block involved in
914 * Bits<16:7> of the effect address select the index
916 address
= CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
917 (assoc
<< CVMX_L2C_TAG_ADDR_ALIAS_SHIFT
) |
918 (index
<< CVMX_L2C_IDX_ADDR_SHIFT
));
919 CVMX_CACHE_WBIL2I(address
, 0);
921 union cvmx_l2c_dbg l2cdbg
;
924 if (!OCTEON_IS_MODEL(OCTEON_CN30XX
))
925 l2cdbg
.s
.ppnum
= cvmx_get_core_num();
928 l2cdbg
.s
.set
= assoc
;
929 cvmx_spinlock_lock(&cvmx_l2c_spinlock
);
931 * Enter debug mode, and make sure all other writes
932 * complete before we enter debug mode
935 cvmx_write_csr(CVMX_L2C_DBG
, l2cdbg
.u64
);
936 cvmx_read_csr(CVMX_L2C_DBG
);
938 CVMX_PREPARE_FOR_STORE(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS
,
939 index
* CVMX_CACHE_LINE_SIZE
),
941 /* Exit debug mode */
943 cvmx_write_csr(CVMX_L2C_DBG
, 0);
944 cvmx_read_csr(CVMX_L2C_DBG
);
945 cvmx_spinlock_unlock(&cvmx_l2c_spinlock
);