xref: /OK3568_Linux_fs/kernel/arch/mips/cavium-octeon/executive/cvmx-l2c.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /***********************license start***************
2*4882a593Smuzhiyun  * Author: Cavium Networks
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Contact: support@caviumnetworks.com
5*4882a593Smuzhiyun  * This file is part of the OCTEON SDK
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (c) 2003-2017 Cavium, Inc.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This file is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of the GNU General Public License, Version 2, as
11*4882a593Smuzhiyun  * published by the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * This file is distributed in the hope that it will be useful, but
14*4882a593Smuzhiyun  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15*4882a593Smuzhiyun  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16*4882a593Smuzhiyun  * NONINFRINGEMENT.  See the GNU General Public License for more
17*4882a593Smuzhiyun  * details.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  * You should have received a copy of the GNU General Public License
20*4882a593Smuzhiyun  * along with this file; if not, write to the Free Software
21*4882a593Smuzhiyun  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22*4882a593Smuzhiyun  * or visit http://www.gnu.org/licenses/.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  * This file may also be available under a different license from Cavium.
25*4882a593Smuzhiyun  * Contact Cavium Networks for more information
26*4882a593Smuzhiyun  ***********************license end**************************************/
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun  * Implementation of the Level 2 Cache (L2C) control,
30*4882a593Smuzhiyun  * measurement, and debugging facilities.
31*4882a593Smuzhiyun  */
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include <linux/compiler.h>
34*4882a593Smuzhiyun #include <linux/irqflags.h>
35*4882a593Smuzhiyun #include <asm/octeon/cvmx.h>
36*4882a593Smuzhiyun #include <asm/octeon/cvmx-l2c.h>
37*4882a593Smuzhiyun #include <asm/octeon/cvmx-spinlock.h>
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun  * This spinlock is used internally to ensure that only one core is
41*4882a593Smuzhiyun  * performing certain L2 operations at a time.
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * NOTE: This only protects calls from within a single application -
44*4882a593Smuzhiyun  * if multiple applications or operating systems are running, then it
45*4882a593Smuzhiyun  * is up to the user program to coordinate between them.
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun static cvmx_spinlock_t cvmx_l2c_spinlock;
48*4882a593Smuzhiyun 
cvmx_l2c_get_core_way_partition(uint32_t core)49*4882a593Smuzhiyun int cvmx_l2c_get_core_way_partition(uint32_t core)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	uint32_t field;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	/* Validate the core number */
54*4882a593Smuzhiyun 	if (core >= cvmx_octeon_num_cores())
55*4882a593Smuzhiyun 		return -1;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN63XX))
58*4882a593Smuzhiyun 		return cvmx_read_csr(CVMX_L2C_WPAR_PPX(core)) & 0xffff;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	/*
61*4882a593Smuzhiyun 	 * Use the lower two bits of the coreNumber to determine the
62*4882a593Smuzhiyun 	 * bit offset of the UMSK[] field in the L2C_SPAR register.
63*4882a593Smuzhiyun 	 */
64*4882a593Smuzhiyun 	field = (core & 0x3) * 8;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	/*
67*4882a593Smuzhiyun 	 * Return the UMSK[] field from the appropriate L2C_SPAR
68*4882a593Smuzhiyun 	 * register based on the coreNumber.
69*4882a593Smuzhiyun 	 */
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	switch (core & 0xC) {
72*4882a593Smuzhiyun 	case 0x0:
73*4882a593Smuzhiyun 		return (cvmx_read_csr(CVMX_L2C_SPAR0) & (0xFF << field)) >> field;
74*4882a593Smuzhiyun 	case 0x4:
75*4882a593Smuzhiyun 		return (cvmx_read_csr(CVMX_L2C_SPAR1) & (0xFF << field)) >> field;
76*4882a593Smuzhiyun 	case 0x8:
77*4882a593Smuzhiyun 		return (cvmx_read_csr(CVMX_L2C_SPAR2) & (0xFF << field)) >> field;
78*4882a593Smuzhiyun 	case 0xC:
79*4882a593Smuzhiyun 		return (cvmx_read_csr(CVMX_L2C_SPAR3) & (0xFF << field)) >> field;
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 	return 0;
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
cvmx_l2c_set_core_way_partition(uint32_t core,uint32_t mask)84*4882a593Smuzhiyun int cvmx_l2c_set_core_way_partition(uint32_t core, uint32_t mask)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	uint32_t field;
87*4882a593Smuzhiyun 	uint32_t valid_mask;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	valid_mask = (0x1 << cvmx_l2c_get_num_assoc()) - 1;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	mask &= valid_mask;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	/* A UMSK setting which blocks all L2C Ways is an error on some chips */
94*4882a593Smuzhiyun 	if (mask == valid_mask && !OCTEON_IS_MODEL(OCTEON_CN63XX))
95*4882a593Smuzhiyun 		return -1;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/* Validate the core number */
98*4882a593Smuzhiyun 	if (core >= cvmx_octeon_num_cores())
99*4882a593Smuzhiyun 		return -1;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
102*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_WPAR_PPX(core), mask);
103*4882a593Smuzhiyun 		return 0;
104*4882a593Smuzhiyun 	}
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	/*
107*4882a593Smuzhiyun 	 * Use the lower two bits of core to determine the bit offset of the
108*4882a593Smuzhiyun 	 * UMSK[] field in the L2C_SPAR register.
109*4882a593Smuzhiyun 	 */
110*4882a593Smuzhiyun 	field = (core & 0x3) * 8;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	/*
113*4882a593Smuzhiyun 	 * Assign the new mask setting to the UMSK[] field in the appropriate
114*4882a593Smuzhiyun 	 * L2C_SPAR register based on the core_num.
115*4882a593Smuzhiyun 	 *
116*4882a593Smuzhiyun 	 */
117*4882a593Smuzhiyun 	switch (core & 0xC) {
118*4882a593Smuzhiyun 	case 0x0:
119*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_SPAR0,
120*4882a593Smuzhiyun 			       (cvmx_read_csr(CVMX_L2C_SPAR0) & ~(0xFF << field)) |
121*4882a593Smuzhiyun 			       mask << field);
122*4882a593Smuzhiyun 		break;
123*4882a593Smuzhiyun 	case 0x4:
124*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_SPAR1,
125*4882a593Smuzhiyun 			       (cvmx_read_csr(CVMX_L2C_SPAR1) & ~(0xFF << field)) |
126*4882a593Smuzhiyun 			       mask << field);
127*4882a593Smuzhiyun 		break;
128*4882a593Smuzhiyun 	case 0x8:
129*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_SPAR2,
130*4882a593Smuzhiyun 			       (cvmx_read_csr(CVMX_L2C_SPAR2) & ~(0xFF << field)) |
131*4882a593Smuzhiyun 			       mask << field);
132*4882a593Smuzhiyun 		break;
133*4882a593Smuzhiyun 	case 0xC:
134*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_SPAR3,
135*4882a593Smuzhiyun 			       (cvmx_read_csr(CVMX_L2C_SPAR3) & ~(0xFF << field)) |
136*4882a593Smuzhiyun 			       mask << field);
137*4882a593Smuzhiyun 		break;
138*4882a593Smuzhiyun 	}
139*4882a593Smuzhiyun 	return 0;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
cvmx_l2c_set_hw_way_partition(uint32_t mask)142*4882a593Smuzhiyun int cvmx_l2c_set_hw_way_partition(uint32_t mask)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	uint32_t valid_mask;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 	valid_mask = (0x1 << cvmx_l2c_get_num_assoc()) - 1;
147*4882a593Smuzhiyun 	mask &= valid_mask;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* A UMSK setting which blocks all L2C Ways is an error on some chips */
150*4882a593Smuzhiyun 	if (mask == valid_mask	&& !OCTEON_IS_MODEL(OCTEON_CN63XX))
151*4882a593Smuzhiyun 		return -1;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN63XX))
154*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_WPAR_IOBX(0), mask);
155*4882a593Smuzhiyun 	else
156*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_SPAR4,
157*4882a593Smuzhiyun 			       (cvmx_read_csr(CVMX_L2C_SPAR4) & ~0xFF) | mask);
158*4882a593Smuzhiyun 	return 0;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
cvmx_l2c_get_hw_way_partition(void)161*4882a593Smuzhiyun int cvmx_l2c_get_hw_way_partition(void)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN63XX))
164*4882a593Smuzhiyun 		return cvmx_read_csr(CVMX_L2C_WPAR_IOBX(0)) & 0xffff;
165*4882a593Smuzhiyun 	else
166*4882a593Smuzhiyun 		return cvmx_read_csr(CVMX_L2C_SPAR4) & (0xFF);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
cvmx_l2c_config_perf(uint32_t counter,enum cvmx_l2c_event event,uint32_t clear_on_read)169*4882a593Smuzhiyun void cvmx_l2c_config_perf(uint32_t counter, enum cvmx_l2c_event event,
170*4882a593Smuzhiyun 			  uint32_t clear_on_read)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX)) {
173*4882a593Smuzhiyun 		union cvmx_l2c_pfctl pfctl;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 		pfctl.u64 = cvmx_read_csr(CVMX_L2C_PFCTL);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 		switch (counter) {
178*4882a593Smuzhiyun 		case 0:
179*4882a593Smuzhiyun 			pfctl.s.cnt0sel = event;
180*4882a593Smuzhiyun 			pfctl.s.cnt0ena = 1;
181*4882a593Smuzhiyun 			pfctl.s.cnt0rdclr = clear_on_read;
182*4882a593Smuzhiyun 			break;
183*4882a593Smuzhiyun 		case 1:
184*4882a593Smuzhiyun 			pfctl.s.cnt1sel = event;
185*4882a593Smuzhiyun 			pfctl.s.cnt1ena = 1;
186*4882a593Smuzhiyun 			pfctl.s.cnt1rdclr = clear_on_read;
187*4882a593Smuzhiyun 			break;
188*4882a593Smuzhiyun 		case 2:
189*4882a593Smuzhiyun 			pfctl.s.cnt2sel = event;
190*4882a593Smuzhiyun 			pfctl.s.cnt2ena = 1;
191*4882a593Smuzhiyun 			pfctl.s.cnt2rdclr = clear_on_read;
192*4882a593Smuzhiyun 			break;
193*4882a593Smuzhiyun 		case 3:
194*4882a593Smuzhiyun 		default:
195*4882a593Smuzhiyun 			pfctl.s.cnt3sel = event;
196*4882a593Smuzhiyun 			pfctl.s.cnt3ena = 1;
197*4882a593Smuzhiyun 			pfctl.s.cnt3rdclr = clear_on_read;
198*4882a593Smuzhiyun 			break;
199*4882a593Smuzhiyun 		}
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_PFCTL, pfctl.u64);
202*4882a593Smuzhiyun 	} else {
203*4882a593Smuzhiyun 		union cvmx_l2c_tadx_prf l2c_tadx_prf;
204*4882a593Smuzhiyun 		int tad;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 		cvmx_dprintf("L2C performance counter events are different for this chip, mapping 'event' to cvmx_l2c_tad_event_t\n");
207*4882a593Smuzhiyun 		if (clear_on_read)
208*4882a593Smuzhiyun 			cvmx_dprintf("L2C counters don't support clear on read for this chip\n");
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 		l2c_tadx_prf.u64 = cvmx_read_csr(CVMX_L2C_TADX_PRF(0));
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 		switch (counter) {
213*4882a593Smuzhiyun 		case 0:
214*4882a593Smuzhiyun 			l2c_tadx_prf.s.cnt0sel = event;
215*4882a593Smuzhiyun 			break;
216*4882a593Smuzhiyun 		case 1:
217*4882a593Smuzhiyun 			l2c_tadx_prf.s.cnt1sel = event;
218*4882a593Smuzhiyun 			break;
219*4882a593Smuzhiyun 		case 2:
220*4882a593Smuzhiyun 			l2c_tadx_prf.s.cnt2sel = event;
221*4882a593Smuzhiyun 			break;
222*4882a593Smuzhiyun 		default:
223*4882a593Smuzhiyun 		case 3:
224*4882a593Smuzhiyun 			l2c_tadx_prf.s.cnt3sel = event;
225*4882a593Smuzhiyun 			break;
226*4882a593Smuzhiyun 		}
227*4882a593Smuzhiyun 		for (tad = 0; tad < CVMX_L2C_TADS; tad++)
228*4882a593Smuzhiyun 			cvmx_write_csr(CVMX_L2C_TADX_PRF(tad),
229*4882a593Smuzhiyun 				       l2c_tadx_prf.u64);
230*4882a593Smuzhiyun 	}
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun 
cvmx_l2c_read_perf(uint32_t counter)233*4882a593Smuzhiyun uint64_t cvmx_l2c_read_perf(uint32_t counter)
234*4882a593Smuzhiyun {
235*4882a593Smuzhiyun 	switch (counter) {
236*4882a593Smuzhiyun 	case 0:
237*4882a593Smuzhiyun 		if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))
238*4882a593Smuzhiyun 			return cvmx_read_csr(CVMX_L2C_PFC0);
239*4882a593Smuzhiyun 		else {
240*4882a593Smuzhiyun 			uint64_t counter = 0;
241*4882a593Smuzhiyun 			int tad;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 			for (tad = 0; tad < CVMX_L2C_TADS; tad++)
244*4882a593Smuzhiyun 				counter += cvmx_read_csr(CVMX_L2C_TADX_PFC0(tad));
245*4882a593Smuzhiyun 			return counter;
246*4882a593Smuzhiyun 		}
247*4882a593Smuzhiyun 	case 1:
248*4882a593Smuzhiyun 		if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))
249*4882a593Smuzhiyun 			return cvmx_read_csr(CVMX_L2C_PFC1);
250*4882a593Smuzhiyun 		else {
251*4882a593Smuzhiyun 			uint64_t counter = 0;
252*4882a593Smuzhiyun 			int tad;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 			for (tad = 0; tad < CVMX_L2C_TADS; tad++)
255*4882a593Smuzhiyun 				counter += cvmx_read_csr(CVMX_L2C_TADX_PFC1(tad));
256*4882a593Smuzhiyun 			return counter;
257*4882a593Smuzhiyun 		}
258*4882a593Smuzhiyun 	case 2:
259*4882a593Smuzhiyun 		if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))
260*4882a593Smuzhiyun 			return cvmx_read_csr(CVMX_L2C_PFC2);
261*4882a593Smuzhiyun 		else {
262*4882a593Smuzhiyun 			uint64_t counter = 0;
263*4882a593Smuzhiyun 			int tad;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 			for (tad = 0; tad < CVMX_L2C_TADS; tad++)
266*4882a593Smuzhiyun 				counter += cvmx_read_csr(CVMX_L2C_TADX_PFC2(tad));
267*4882a593Smuzhiyun 			return counter;
268*4882a593Smuzhiyun 		}
269*4882a593Smuzhiyun 	case 3:
270*4882a593Smuzhiyun 	default:
271*4882a593Smuzhiyun 		if (OCTEON_IS_MODEL(OCTEON_CN5XXX) || OCTEON_IS_MODEL(OCTEON_CN3XXX))
272*4882a593Smuzhiyun 			return cvmx_read_csr(CVMX_L2C_PFC3);
273*4882a593Smuzhiyun 		else {
274*4882a593Smuzhiyun 			uint64_t counter = 0;
275*4882a593Smuzhiyun 			int tad;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 			for (tad = 0; tad < CVMX_L2C_TADS; tad++)
278*4882a593Smuzhiyun 				counter += cvmx_read_csr(CVMX_L2C_TADX_PFC3(tad));
279*4882a593Smuzhiyun 			return counter;
280*4882a593Smuzhiyun 		}
281*4882a593Smuzhiyun 	}
282*4882a593Smuzhiyun }
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun /**
285*4882a593Smuzhiyun  * @INTERNAL
286*4882a593Smuzhiyun  * Helper function use to fault in cache lines for L2 cache locking
287*4882a593Smuzhiyun  *
288*4882a593Smuzhiyun  * @addr:   Address of base of memory region to read into L2 cache
289*4882a593Smuzhiyun  * @len:    Length (in bytes) of region to fault in
290*4882a593Smuzhiyun  */
fault_in(uint64_t addr,int len)291*4882a593Smuzhiyun static void fault_in(uint64_t addr, int len)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	char *ptr;
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	/*
296*4882a593Smuzhiyun 	 * Adjust addr and length so we get all cache lines even for
297*4882a593Smuzhiyun 	 * small ranges spanning two cache lines.
298*4882a593Smuzhiyun 	 */
299*4882a593Smuzhiyun 	len += addr & CVMX_CACHE_LINE_MASK;
300*4882a593Smuzhiyun 	addr &= ~CVMX_CACHE_LINE_MASK;
301*4882a593Smuzhiyun 	ptr = cvmx_phys_to_ptr(addr);
302*4882a593Smuzhiyun 	/*
303*4882a593Smuzhiyun 	 * Invalidate L1 cache to make sure all loads result in data
304*4882a593Smuzhiyun 	 * being in L2.
305*4882a593Smuzhiyun 	 */
306*4882a593Smuzhiyun 	CVMX_DCACHE_INVALIDATE;
307*4882a593Smuzhiyun 	while (len > 0) {
308*4882a593Smuzhiyun 		READ_ONCE(*ptr);
309*4882a593Smuzhiyun 		len -= CVMX_CACHE_LINE_SIZE;
310*4882a593Smuzhiyun 		ptr += CVMX_CACHE_LINE_SIZE;
311*4882a593Smuzhiyun 	}
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun 
cvmx_l2c_lock_line(uint64_t addr)314*4882a593Smuzhiyun int cvmx_l2c_lock_line(uint64_t addr)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
317*4882a593Smuzhiyun 		int shift = CVMX_L2C_TAG_ADDR_ALIAS_SHIFT;
318*4882a593Smuzhiyun 		uint64_t assoc = cvmx_l2c_get_num_assoc();
319*4882a593Smuzhiyun 		uint64_t tag = addr >> shift;
320*4882a593Smuzhiyun 		uint64_t index = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, cvmx_l2c_address_to_index(addr) << CVMX_L2C_IDX_ADDR_SHIFT);
321*4882a593Smuzhiyun 		uint64_t way;
322*4882a593Smuzhiyun 		union cvmx_l2c_tadx_tag l2c_tadx_tag;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 		CVMX_CACHE_LCKL2(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, addr), 0);
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 		/* Make sure we were able to lock the line */
327*4882a593Smuzhiyun 		for (way = 0; way < assoc; way++) {
328*4882a593Smuzhiyun 			CVMX_CACHE_LTGL2I(index | (way << shift), 0);
329*4882a593Smuzhiyun 			/* make sure CVMX_L2C_TADX_TAG is updated */
330*4882a593Smuzhiyun 			CVMX_SYNC;
331*4882a593Smuzhiyun 			l2c_tadx_tag.u64 = cvmx_read_csr(CVMX_L2C_TADX_TAG(0));
332*4882a593Smuzhiyun 			if (l2c_tadx_tag.s.valid && l2c_tadx_tag.s.tag == tag)
333*4882a593Smuzhiyun 				break;
334*4882a593Smuzhiyun 		}
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 		/* Check if a valid line is found */
337*4882a593Smuzhiyun 		if (way >= assoc) {
338*4882a593Smuzhiyun 			/* cvmx_dprintf("ERROR: cvmx_l2c_lock_line: line not found for locking at 0x%llx address\n", (unsigned long long)addr); */
339*4882a593Smuzhiyun 			return -1;
340*4882a593Smuzhiyun 		}
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 		/* Check if lock bit is not set */
343*4882a593Smuzhiyun 		if (!l2c_tadx_tag.s.lock) {
344*4882a593Smuzhiyun 			/* cvmx_dprintf("ERROR: cvmx_l2c_lock_line: Not able to lock at 0x%llx address\n", (unsigned long long)addr); */
345*4882a593Smuzhiyun 			return -1;
346*4882a593Smuzhiyun 		}
347*4882a593Smuzhiyun 		return way;
348*4882a593Smuzhiyun 	} else {
349*4882a593Smuzhiyun 		int retval = 0;
350*4882a593Smuzhiyun 		union cvmx_l2c_dbg l2cdbg;
351*4882a593Smuzhiyun 		union cvmx_l2c_lckbase lckbase;
352*4882a593Smuzhiyun 		union cvmx_l2c_lckoff lckoff;
353*4882a593Smuzhiyun 		union cvmx_l2t_err l2t_err;
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 		cvmx_spinlock_lock(&cvmx_l2c_spinlock);
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 		l2cdbg.u64 = 0;
358*4882a593Smuzhiyun 		lckbase.u64 = 0;
359*4882a593Smuzhiyun 		lckoff.u64 = 0;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 		/* Clear l2t error bits if set */
362*4882a593Smuzhiyun 		l2t_err.u64 = cvmx_read_csr(CVMX_L2T_ERR);
363*4882a593Smuzhiyun 		l2t_err.s.lckerr = 1;
364*4882a593Smuzhiyun 		l2t_err.s.lckerr2 = 1;
365*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2T_ERR, l2t_err.u64);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 		addr &= ~CVMX_CACHE_LINE_MASK;
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 		/* Set this core as debug core */
370*4882a593Smuzhiyun 		l2cdbg.s.ppnum = cvmx_get_core_num();
371*4882a593Smuzhiyun 		CVMX_SYNC;
372*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_DBG, l2cdbg.u64);
373*4882a593Smuzhiyun 		cvmx_read_csr(CVMX_L2C_DBG);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 		lckoff.s.lck_offset = 0; /* Only lock 1 line at a time */
376*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_LCKOFF, lckoff.u64);
377*4882a593Smuzhiyun 		cvmx_read_csr(CVMX_L2C_LCKOFF);
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 		if (((union cvmx_l2c_cfg)(cvmx_read_csr(CVMX_L2C_CFG))).s.idxalias) {
380*4882a593Smuzhiyun 			int alias_shift = CVMX_L2C_IDX_ADDR_SHIFT + 2 * CVMX_L2_SET_BITS - 1;
381*4882a593Smuzhiyun 			uint64_t addr_tmp = addr ^ (addr & ((1 << alias_shift) - 1)) >> CVMX_L2_SET_BITS;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 			lckbase.s.lck_base = addr_tmp >> 7;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 		} else {
386*4882a593Smuzhiyun 			lckbase.s.lck_base = addr >> 7;
387*4882a593Smuzhiyun 		}
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 		lckbase.s.lck_ena = 1;
390*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_LCKBASE, lckbase.u64);
391*4882a593Smuzhiyun 		/* Make sure it gets there */
392*4882a593Smuzhiyun 		cvmx_read_csr(CVMX_L2C_LCKBASE);
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 		fault_in(addr, CVMX_CACHE_LINE_SIZE);
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 		lckbase.s.lck_ena = 0;
397*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_LCKBASE, lckbase.u64);
398*4882a593Smuzhiyun 		/* Make sure it gets there */
399*4882a593Smuzhiyun 		cvmx_read_csr(CVMX_L2C_LCKBASE);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 		/* Stop being debug core */
402*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_DBG, 0);
403*4882a593Smuzhiyun 		cvmx_read_csr(CVMX_L2C_DBG);
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 		l2t_err.u64 = cvmx_read_csr(CVMX_L2T_ERR);
406*4882a593Smuzhiyun 		if (l2t_err.s.lckerr || l2t_err.s.lckerr2)
407*4882a593Smuzhiyun 			retval = 1;  /* We were unable to lock the line */
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 		cvmx_spinlock_unlock(&cvmx_l2c_spinlock);
410*4882a593Smuzhiyun 		return retval;
411*4882a593Smuzhiyun 	}
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun 
cvmx_l2c_lock_mem_region(uint64_t start,uint64_t len)414*4882a593Smuzhiyun int cvmx_l2c_lock_mem_region(uint64_t start, uint64_t len)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun 	int retval = 0;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	/* Round start/end to cache line boundaries */
419*4882a593Smuzhiyun 	len += start & CVMX_CACHE_LINE_MASK;
420*4882a593Smuzhiyun 	start &= ~CVMX_CACHE_LINE_MASK;
421*4882a593Smuzhiyun 	len = (len + CVMX_CACHE_LINE_MASK) & ~CVMX_CACHE_LINE_MASK;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	while (len) {
424*4882a593Smuzhiyun 		retval += cvmx_l2c_lock_line(start);
425*4882a593Smuzhiyun 		start += CVMX_CACHE_LINE_SIZE;
426*4882a593Smuzhiyun 		len -= CVMX_CACHE_LINE_SIZE;
427*4882a593Smuzhiyun 	}
428*4882a593Smuzhiyun 	return retval;
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun 
cvmx_l2c_flush(void)431*4882a593Smuzhiyun void cvmx_l2c_flush(void)
432*4882a593Smuzhiyun {
433*4882a593Smuzhiyun 	uint64_t assoc, set;
434*4882a593Smuzhiyun 	uint64_t n_assoc, n_set;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	n_set = cvmx_l2c_get_num_sets();
437*4882a593Smuzhiyun 	n_assoc = cvmx_l2c_get_num_assoc();
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
440*4882a593Smuzhiyun 		uint64_t address;
441*4882a593Smuzhiyun 		/* These may look like constants, but they aren't... */
442*4882a593Smuzhiyun 		int assoc_shift = CVMX_L2C_TAG_ADDR_ALIAS_SHIFT;
443*4882a593Smuzhiyun 		int set_shift = CVMX_L2C_IDX_ADDR_SHIFT;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 		for (set = 0; set < n_set; set++) {
446*4882a593Smuzhiyun 			for (assoc = 0; assoc < n_assoc; assoc++) {
447*4882a593Smuzhiyun 				address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
448*4882a593Smuzhiyun 						       (assoc << assoc_shift) | (set << set_shift));
449*4882a593Smuzhiyun 				CVMX_CACHE_WBIL2I(address, 0);
450*4882a593Smuzhiyun 			}
451*4882a593Smuzhiyun 		}
452*4882a593Smuzhiyun 	} else {
453*4882a593Smuzhiyun 		for (set = 0; set < n_set; set++)
454*4882a593Smuzhiyun 			for (assoc = 0; assoc < n_assoc; assoc++)
455*4882a593Smuzhiyun 				cvmx_l2c_flush_line(assoc, set);
456*4882a593Smuzhiyun 	}
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 
cvmx_l2c_unlock_line(uint64_t address)460*4882a593Smuzhiyun int cvmx_l2c_unlock_line(uint64_t address)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
464*4882a593Smuzhiyun 		int assoc;
465*4882a593Smuzhiyun 		union cvmx_l2c_tag tag;
466*4882a593Smuzhiyun 		uint32_t tag_addr;
467*4882a593Smuzhiyun 		uint32_t index = cvmx_l2c_address_to_index(address);
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 		tag_addr = ((address >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) & ((1 << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) - 1));
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 		/*
472*4882a593Smuzhiyun 		 * For 63XX, we can flush a line by using the physical
473*4882a593Smuzhiyun 		 * address directly, so finding the cache line used by
474*4882a593Smuzhiyun 		 * the address is only required to provide the proper
475*4882a593Smuzhiyun 		 * return value for the function.
476*4882a593Smuzhiyun 		 */
477*4882a593Smuzhiyun 		for (assoc = 0; assoc < CVMX_L2_ASSOC; assoc++) {
478*4882a593Smuzhiyun 			tag = cvmx_l2c_get_tag(assoc, index);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 			if (tag.s.V && (tag.s.addr == tag_addr)) {
481*4882a593Smuzhiyun 				CVMX_CACHE_WBIL2(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, address), 0);
482*4882a593Smuzhiyun 				return tag.s.L;
483*4882a593Smuzhiyun 			}
484*4882a593Smuzhiyun 		}
485*4882a593Smuzhiyun 	} else {
486*4882a593Smuzhiyun 		int assoc;
487*4882a593Smuzhiyun 		union cvmx_l2c_tag tag;
488*4882a593Smuzhiyun 		uint32_t tag_addr;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 		uint32_t index = cvmx_l2c_address_to_index(address);
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun 		/* Compute portion of address that is stored in tag */
493*4882a593Smuzhiyun 		tag_addr = ((address >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) & ((1 << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) - 1));
494*4882a593Smuzhiyun 		for (assoc = 0; assoc < CVMX_L2_ASSOC; assoc++) {
495*4882a593Smuzhiyun 			tag = cvmx_l2c_get_tag(assoc, index);
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 			if (tag.s.V && (tag.s.addr == tag_addr)) {
498*4882a593Smuzhiyun 				cvmx_l2c_flush_line(assoc, index);
499*4882a593Smuzhiyun 				return tag.s.L;
500*4882a593Smuzhiyun 			}
501*4882a593Smuzhiyun 		}
502*4882a593Smuzhiyun 	}
503*4882a593Smuzhiyun 	return 0;
504*4882a593Smuzhiyun }
505*4882a593Smuzhiyun 
cvmx_l2c_unlock_mem_region(uint64_t start,uint64_t len)506*4882a593Smuzhiyun int cvmx_l2c_unlock_mem_region(uint64_t start, uint64_t len)
507*4882a593Smuzhiyun {
508*4882a593Smuzhiyun 	int num_unlocked = 0;
509*4882a593Smuzhiyun 	/* Round start/end to cache line boundaries */
510*4882a593Smuzhiyun 	len += start & CVMX_CACHE_LINE_MASK;
511*4882a593Smuzhiyun 	start &= ~CVMX_CACHE_LINE_MASK;
512*4882a593Smuzhiyun 	len = (len + CVMX_CACHE_LINE_MASK) & ~CVMX_CACHE_LINE_MASK;
513*4882a593Smuzhiyun 	while (len > 0) {
514*4882a593Smuzhiyun 		num_unlocked += cvmx_l2c_unlock_line(start);
515*4882a593Smuzhiyun 		start += CVMX_CACHE_LINE_SIZE;
516*4882a593Smuzhiyun 		len -= CVMX_CACHE_LINE_SIZE;
517*4882a593Smuzhiyun 	}
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 	return num_unlocked;
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun /*
523*4882a593Smuzhiyun  * Internal l2c tag types.  These are converted to a generic structure
524*4882a593Smuzhiyun  * that can be used on all chips.
525*4882a593Smuzhiyun  */
526*4882a593Smuzhiyun union __cvmx_l2c_tag {
527*4882a593Smuzhiyun 	uint64_t u64;
528*4882a593Smuzhiyun 	struct cvmx_l2c_tag_cn50xx {
529*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t reserved:40,
530*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t V:1,		/* Line valid */
531*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t D:1,		/* Line dirty */
532*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t L:1,		/* Line locked */
533*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t U:1,		/* Use, LRU eviction */
534*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t addr:20,	/* Phys addr (33..14) */
535*4882a593Smuzhiyun 		;))))))
536*4882a593Smuzhiyun 	} cn50xx;
537*4882a593Smuzhiyun 	struct cvmx_l2c_tag_cn30xx {
538*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t reserved:41,
539*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t V:1,		/* Line valid */
540*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t D:1,		/* Line dirty */
541*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t L:1,		/* Line locked */
542*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t U:1,		/* Use, LRU eviction */
543*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t addr:19,	/* Phys addr (33..15) */
544*4882a593Smuzhiyun 		;))))))
545*4882a593Smuzhiyun 	} cn30xx;
546*4882a593Smuzhiyun 	struct cvmx_l2c_tag_cn31xx {
547*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t reserved:42,
548*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t V:1,		/* Line valid */
549*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t D:1,		/* Line dirty */
550*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t L:1,		/* Line locked */
551*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t U:1,		/* Use, LRU eviction */
552*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t addr:18,	/* Phys addr (33..16) */
553*4882a593Smuzhiyun 		;))))))
554*4882a593Smuzhiyun 	} cn31xx;
555*4882a593Smuzhiyun 	struct cvmx_l2c_tag_cn38xx {
556*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t reserved:43,
557*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t V:1,		/* Line valid */
558*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t D:1,		/* Line dirty */
559*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t L:1,		/* Line locked */
560*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t U:1,		/* Use, LRU eviction */
561*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t addr:17,	/* Phys addr (33..17) */
562*4882a593Smuzhiyun 		;))))))
563*4882a593Smuzhiyun 	} cn38xx;
564*4882a593Smuzhiyun 	struct cvmx_l2c_tag_cn58xx {
565*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t reserved:44,
566*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t V:1,		/* Line valid */
567*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t D:1,		/* Line dirty */
568*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t L:1,		/* Line locked */
569*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t U:1,		/* Use, LRU eviction */
570*4882a593Smuzhiyun 		__BITFIELD_FIELD(uint64_t addr:16,	/* Phys addr (33..18) */
571*4882a593Smuzhiyun 		;))))))
572*4882a593Smuzhiyun 	} cn58xx;
573*4882a593Smuzhiyun 	struct cvmx_l2c_tag_cn58xx cn56xx;	/* 2048 sets */
574*4882a593Smuzhiyun 	struct cvmx_l2c_tag_cn31xx cn52xx;	/* 512 sets */
575*4882a593Smuzhiyun };
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun /**
579*4882a593Smuzhiyun  * @INTERNAL
580*4882a593Smuzhiyun  * Function to read a L2C tag.  This code make the current core
581*4882a593Smuzhiyun  * the 'debug core' for the L2.  This code must only be executed by
582*4882a593Smuzhiyun  * 1 core at a time.
583*4882a593Smuzhiyun  *
584*4882a593Smuzhiyun  * @assoc:  Association (way) of the tag to dump
585*4882a593Smuzhiyun  * @index:  Index of the cacheline
586*4882a593Smuzhiyun  *
587*4882a593Smuzhiyun  * Returns The Octeon model specific tag structure.  This is
588*4882a593Smuzhiyun  *	   translated by a wrapper function to a generic form that is
589*4882a593Smuzhiyun  *	   easier for applications to use.
590*4882a593Smuzhiyun  */
__read_l2_tag(uint64_t assoc,uint64_t index)591*4882a593Smuzhiyun static union __cvmx_l2c_tag __read_l2_tag(uint64_t assoc, uint64_t index)
592*4882a593Smuzhiyun {
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	uint64_t debug_tag_addr = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS, (index << 7) + 96);
595*4882a593Smuzhiyun 	uint64_t core = cvmx_get_core_num();
596*4882a593Smuzhiyun 	union __cvmx_l2c_tag tag_val;
597*4882a593Smuzhiyun 	uint64_t dbg_addr = CVMX_L2C_DBG;
598*4882a593Smuzhiyun 	unsigned long flags;
599*4882a593Smuzhiyun 	union cvmx_l2c_dbg debug_val;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 	debug_val.u64 = 0;
602*4882a593Smuzhiyun 	/*
603*4882a593Smuzhiyun 	 * For low core count parts, the core number is always small
604*4882a593Smuzhiyun 	 * enough to stay in the correct field and not set any
605*4882a593Smuzhiyun 	 * reserved bits.
606*4882a593Smuzhiyun 	 */
607*4882a593Smuzhiyun 	debug_val.s.ppnum = core;
608*4882a593Smuzhiyun 	debug_val.s.l2t = 1;
609*4882a593Smuzhiyun 	debug_val.s.set = assoc;
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 	local_irq_save(flags);
612*4882a593Smuzhiyun 	/*
613*4882a593Smuzhiyun 	 * Make sure core is quiet (no prefetches, etc.) before
614*4882a593Smuzhiyun 	 * entering debug mode.
615*4882a593Smuzhiyun 	 */
616*4882a593Smuzhiyun 	CVMX_SYNC;
617*4882a593Smuzhiyun 	/* Flush L1 to make sure debug load misses L1 */
618*4882a593Smuzhiyun 	CVMX_DCACHE_INVALIDATE;
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	/*
621*4882a593Smuzhiyun 	 * The following must be done in assembly as when in debug
622*4882a593Smuzhiyun 	 * mode all data loads from L2 return special debug data, not
623*4882a593Smuzhiyun 	 * normal memory contents.  Also, interrupts must be disabled,
624*4882a593Smuzhiyun 	 * since if an interrupt occurs while in debug mode the ISR
625*4882a593Smuzhiyun 	 * will get debug data from all its memory * reads instead of
626*4882a593Smuzhiyun 	 * the contents of memory.
627*4882a593Smuzhiyun 	 */
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	asm volatile (
630*4882a593Smuzhiyun 		".set push\n\t"
631*4882a593Smuzhiyun 		".set mips64\n\t"
632*4882a593Smuzhiyun 		".set noreorder\n\t"
633*4882a593Smuzhiyun 		"sd    %[dbg_val], 0(%[dbg_addr])\n\t"	 /* Enter debug mode, wait for store */
634*4882a593Smuzhiyun 		"ld    $0, 0(%[dbg_addr])\n\t"
635*4882a593Smuzhiyun 		"ld    %[tag_val], 0(%[tag_addr])\n\t"	 /* Read L2C tag data */
636*4882a593Smuzhiyun 		"sd    $0, 0(%[dbg_addr])\n\t"		/* Exit debug mode, wait for store */
637*4882a593Smuzhiyun 		"ld    $0, 0(%[dbg_addr])\n\t"
638*4882a593Smuzhiyun 		"cache 9, 0($0)\n\t"		 /* Invalidate dcache to discard debug data */
639*4882a593Smuzhiyun 		".set pop"
640*4882a593Smuzhiyun 		: [tag_val] "=r" (tag_val)
641*4882a593Smuzhiyun 		: [dbg_addr] "r" (dbg_addr), [dbg_val] "r" (debug_val), [tag_addr] "r" (debug_tag_addr)
642*4882a593Smuzhiyun 		: "memory");
643*4882a593Smuzhiyun 
644*4882a593Smuzhiyun 	local_irq_restore(flags);
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	return tag_val;
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 
cvmx_l2c_get_tag(uint32_t association,uint32_t index)650*4882a593Smuzhiyun union cvmx_l2c_tag cvmx_l2c_get_tag(uint32_t association, uint32_t index)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun 	union cvmx_l2c_tag tag;
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 	tag.u64 = 0;
655*4882a593Smuzhiyun 	if ((int)association >= cvmx_l2c_get_num_assoc()) {
656*4882a593Smuzhiyun 		cvmx_dprintf("ERROR: cvmx_l2c_get_tag association out of range\n");
657*4882a593Smuzhiyun 		return tag;
658*4882a593Smuzhiyun 	}
659*4882a593Smuzhiyun 	if ((int)index >= cvmx_l2c_get_num_sets()) {
660*4882a593Smuzhiyun 		cvmx_dprintf("ERROR: cvmx_l2c_get_tag index out of range (arg: %d, max: %d)\n",
661*4882a593Smuzhiyun 			     (int)index, cvmx_l2c_get_num_sets());
662*4882a593Smuzhiyun 		return tag;
663*4882a593Smuzhiyun 	}
664*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
665*4882a593Smuzhiyun 		union cvmx_l2c_tadx_tag l2c_tadx_tag;
666*4882a593Smuzhiyun 		uint64_t address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
667*4882a593Smuzhiyun 						(association << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) |
668*4882a593Smuzhiyun 						(index << CVMX_L2C_IDX_ADDR_SHIFT));
669*4882a593Smuzhiyun 		/*
670*4882a593Smuzhiyun 		 * Use L2 cache Index load tag cache instruction, as
671*4882a593Smuzhiyun 		 * hardware loads the virtual tag for the L2 cache
672*4882a593Smuzhiyun 		 * block with the contents of L2C_TAD0_TAG
673*4882a593Smuzhiyun 		 * register.
674*4882a593Smuzhiyun 		 */
675*4882a593Smuzhiyun 		CVMX_CACHE_LTGL2I(address, 0);
676*4882a593Smuzhiyun 		CVMX_SYNC;   /* make sure CVMX_L2C_TADX_TAG is updated */
677*4882a593Smuzhiyun 		l2c_tadx_tag.u64 = cvmx_read_csr(CVMX_L2C_TADX_TAG(0));
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 		tag.s.V	    = l2c_tadx_tag.s.valid;
680*4882a593Smuzhiyun 		tag.s.D	    = l2c_tadx_tag.s.dirty;
681*4882a593Smuzhiyun 		tag.s.L	    = l2c_tadx_tag.s.lock;
682*4882a593Smuzhiyun 		tag.s.U	    = l2c_tadx_tag.s.use;
683*4882a593Smuzhiyun 		tag.s.addr  = l2c_tadx_tag.s.tag;
684*4882a593Smuzhiyun 	} else {
685*4882a593Smuzhiyun 		union __cvmx_l2c_tag tmp_tag;
686*4882a593Smuzhiyun 		/* __read_l2_tag is intended for internal use only */
687*4882a593Smuzhiyun 		tmp_tag = __read_l2_tag(association, index);
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 		/*
690*4882a593Smuzhiyun 		 * Convert all tag structure types to generic version,
691*4882a593Smuzhiyun 		 * as it can represent all models.
692*4882a593Smuzhiyun 		 */
693*4882a593Smuzhiyun 		if (OCTEON_IS_MODEL(OCTEON_CN58XX) || OCTEON_IS_MODEL(OCTEON_CN56XX)) {
694*4882a593Smuzhiyun 			tag.s.V	   = tmp_tag.cn58xx.V;
695*4882a593Smuzhiyun 			tag.s.D	   = tmp_tag.cn58xx.D;
696*4882a593Smuzhiyun 			tag.s.L	   = tmp_tag.cn58xx.L;
697*4882a593Smuzhiyun 			tag.s.U	   = tmp_tag.cn58xx.U;
698*4882a593Smuzhiyun 			tag.s.addr = tmp_tag.cn58xx.addr;
699*4882a593Smuzhiyun 		} else if (OCTEON_IS_MODEL(OCTEON_CN38XX)) {
700*4882a593Smuzhiyun 			tag.s.V	   = tmp_tag.cn38xx.V;
701*4882a593Smuzhiyun 			tag.s.D	   = tmp_tag.cn38xx.D;
702*4882a593Smuzhiyun 			tag.s.L	   = tmp_tag.cn38xx.L;
703*4882a593Smuzhiyun 			tag.s.U	   = tmp_tag.cn38xx.U;
704*4882a593Smuzhiyun 			tag.s.addr = tmp_tag.cn38xx.addr;
705*4882a593Smuzhiyun 		} else if (OCTEON_IS_MODEL(OCTEON_CN31XX) || OCTEON_IS_MODEL(OCTEON_CN52XX)) {
706*4882a593Smuzhiyun 			tag.s.V	   = tmp_tag.cn31xx.V;
707*4882a593Smuzhiyun 			tag.s.D	   = tmp_tag.cn31xx.D;
708*4882a593Smuzhiyun 			tag.s.L	   = tmp_tag.cn31xx.L;
709*4882a593Smuzhiyun 			tag.s.U	   = tmp_tag.cn31xx.U;
710*4882a593Smuzhiyun 			tag.s.addr = tmp_tag.cn31xx.addr;
711*4882a593Smuzhiyun 		} else if (OCTEON_IS_MODEL(OCTEON_CN30XX)) {
712*4882a593Smuzhiyun 			tag.s.V	   = tmp_tag.cn30xx.V;
713*4882a593Smuzhiyun 			tag.s.D	   = tmp_tag.cn30xx.D;
714*4882a593Smuzhiyun 			tag.s.L	   = tmp_tag.cn30xx.L;
715*4882a593Smuzhiyun 			tag.s.U	   = tmp_tag.cn30xx.U;
716*4882a593Smuzhiyun 			tag.s.addr = tmp_tag.cn30xx.addr;
717*4882a593Smuzhiyun 		} else if (OCTEON_IS_MODEL(OCTEON_CN50XX)) {
718*4882a593Smuzhiyun 			tag.s.V	   = tmp_tag.cn50xx.V;
719*4882a593Smuzhiyun 			tag.s.D	   = tmp_tag.cn50xx.D;
720*4882a593Smuzhiyun 			tag.s.L	   = tmp_tag.cn50xx.L;
721*4882a593Smuzhiyun 			tag.s.U	   = tmp_tag.cn50xx.U;
722*4882a593Smuzhiyun 			tag.s.addr = tmp_tag.cn50xx.addr;
723*4882a593Smuzhiyun 		} else {
724*4882a593Smuzhiyun 			cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);
725*4882a593Smuzhiyun 		}
726*4882a593Smuzhiyun 	}
727*4882a593Smuzhiyun 	return tag;
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
cvmx_l2c_address_to_index(uint64_t addr)730*4882a593Smuzhiyun uint32_t cvmx_l2c_address_to_index(uint64_t addr)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun 	uint64_t idx = addr >> CVMX_L2C_IDX_ADDR_SHIFT;
733*4882a593Smuzhiyun 	int indxalias = 0;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) {
736*4882a593Smuzhiyun 		union cvmx_l2c_ctl l2c_ctl;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 		l2c_ctl.u64 = cvmx_read_csr(CVMX_L2C_CTL);
739*4882a593Smuzhiyun 		indxalias = !l2c_ctl.s.disidxalias;
740*4882a593Smuzhiyun 	} else {
741*4882a593Smuzhiyun 		union cvmx_l2c_cfg l2c_cfg;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 		l2c_cfg.u64 = cvmx_read_csr(CVMX_L2C_CFG);
744*4882a593Smuzhiyun 		indxalias = l2c_cfg.s.idxalias;
745*4882a593Smuzhiyun 	}
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 	if (indxalias) {
748*4882a593Smuzhiyun 		if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
749*4882a593Smuzhiyun 			uint32_t a_14_12 = (idx / (CVMX_L2C_MEMBANK_SELECT_SIZE/(1<<CVMX_L2C_IDX_ADDR_SHIFT))) & 0x7;
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 			idx ^= idx / cvmx_l2c_get_num_sets();
752*4882a593Smuzhiyun 			idx ^= a_14_12;
753*4882a593Smuzhiyun 		} else {
754*4882a593Smuzhiyun 			idx ^= ((addr & CVMX_L2C_ALIAS_MASK) >> CVMX_L2C_TAG_ADDR_ALIAS_SHIFT);
755*4882a593Smuzhiyun 		}
756*4882a593Smuzhiyun 	}
757*4882a593Smuzhiyun 	idx &= CVMX_L2C_IDX_MASK;
758*4882a593Smuzhiyun 	return idx;
759*4882a593Smuzhiyun }
760*4882a593Smuzhiyun 
cvmx_l2c_get_cache_size_bytes(void)761*4882a593Smuzhiyun int cvmx_l2c_get_cache_size_bytes(void)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun 	return cvmx_l2c_get_num_sets() * cvmx_l2c_get_num_assoc() *
764*4882a593Smuzhiyun 		CVMX_CACHE_LINE_SIZE;
765*4882a593Smuzhiyun }
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun /**
768*4882a593Smuzhiyun  * Return log base 2 of the number of sets in the L2 cache
769*4882a593Smuzhiyun  * Returns
770*4882a593Smuzhiyun  */
cvmx_l2c_get_set_bits(void)771*4882a593Smuzhiyun int cvmx_l2c_get_set_bits(void)
772*4882a593Smuzhiyun {
773*4882a593Smuzhiyun 	int l2_set_bits;
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN56XX) || OCTEON_IS_MODEL(OCTEON_CN58XX))
776*4882a593Smuzhiyun 		l2_set_bits = 11;	/* 2048 sets */
777*4882a593Smuzhiyun 	else if (OCTEON_IS_MODEL(OCTEON_CN38XX) || OCTEON_IS_MODEL(OCTEON_CN63XX))
778*4882a593Smuzhiyun 		l2_set_bits = 10;	/* 1024 sets */
779*4882a593Smuzhiyun 	else if (OCTEON_IS_MODEL(OCTEON_CN31XX) || OCTEON_IS_MODEL(OCTEON_CN52XX))
780*4882a593Smuzhiyun 		l2_set_bits = 9;	/* 512 sets */
781*4882a593Smuzhiyun 	else if (OCTEON_IS_MODEL(OCTEON_CN30XX))
782*4882a593Smuzhiyun 		l2_set_bits = 8;	/* 256 sets */
783*4882a593Smuzhiyun 	else if (OCTEON_IS_MODEL(OCTEON_CN50XX))
784*4882a593Smuzhiyun 		l2_set_bits = 7;	/* 128 sets */
785*4882a593Smuzhiyun 	else {
786*4882a593Smuzhiyun 		cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);
787*4882a593Smuzhiyun 		l2_set_bits = 11;	/* 2048 sets */
788*4882a593Smuzhiyun 	}
789*4882a593Smuzhiyun 	return l2_set_bits;
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun /* Return the number of sets in the L2 Cache */
cvmx_l2c_get_num_sets(void)793*4882a593Smuzhiyun int cvmx_l2c_get_num_sets(void)
794*4882a593Smuzhiyun {
795*4882a593Smuzhiyun 	return 1 << cvmx_l2c_get_set_bits();
796*4882a593Smuzhiyun }
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun /* Return the number of associations in the L2 Cache */
cvmx_l2c_get_num_assoc(void)799*4882a593Smuzhiyun int cvmx_l2c_get_num_assoc(void)
800*4882a593Smuzhiyun {
801*4882a593Smuzhiyun 	int l2_assoc;
802*4882a593Smuzhiyun 
803*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN56XX) ||
804*4882a593Smuzhiyun 	    OCTEON_IS_MODEL(OCTEON_CN52XX) ||
805*4882a593Smuzhiyun 	    OCTEON_IS_MODEL(OCTEON_CN58XX) ||
806*4882a593Smuzhiyun 	    OCTEON_IS_MODEL(OCTEON_CN50XX) ||
807*4882a593Smuzhiyun 	    OCTEON_IS_MODEL(OCTEON_CN38XX))
808*4882a593Smuzhiyun 		l2_assoc = 8;
809*4882a593Smuzhiyun 	else if (OCTEON_IS_MODEL(OCTEON_CN63XX))
810*4882a593Smuzhiyun 		l2_assoc = 16;
811*4882a593Smuzhiyun 	else if (OCTEON_IS_MODEL(OCTEON_CN31XX) ||
812*4882a593Smuzhiyun 		 OCTEON_IS_MODEL(OCTEON_CN30XX))
813*4882a593Smuzhiyun 		l2_assoc = 4;
814*4882a593Smuzhiyun 	else {
815*4882a593Smuzhiyun 		cvmx_dprintf("Unsupported OCTEON Model in %s\n", __func__);
816*4882a593Smuzhiyun 		l2_assoc = 8;
817*4882a593Smuzhiyun 	}
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun 	/* Check to see if part of the cache is disabled */
820*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
821*4882a593Smuzhiyun 		union cvmx_mio_fus_dat3 mio_fus_dat3;
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 		mio_fus_dat3.u64 = cvmx_read_csr(CVMX_MIO_FUS_DAT3);
824*4882a593Smuzhiyun 		/*
825*4882a593Smuzhiyun 		 * cvmx_mio_fus_dat3.s.l2c_crip fuses map as follows
826*4882a593Smuzhiyun 		 * <2> will be not used for 63xx
827*4882a593Smuzhiyun 		 * <1> disables 1/2 ways
828*4882a593Smuzhiyun 		 * <0> disables 1/4 ways
829*4882a593Smuzhiyun 		 * They are cumulative, so for 63xx:
830*4882a593Smuzhiyun 		 * <1> <0>
831*4882a593Smuzhiyun 		 * 0 0 16-way 2MB cache
832*4882a593Smuzhiyun 		 * 0 1 12-way 1.5MB cache
833*4882a593Smuzhiyun 		 * 1 0 8-way 1MB cache
834*4882a593Smuzhiyun 		 * 1 1 4-way 512KB cache
835*4882a593Smuzhiyun 		 */
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 		if (mio_fus_dat3.s.l2c_crip == 3)
838*4882a593Smuzhiyun 			l2_assoc = 4;
839*4882a593Smuzhiyun 		else if (mio_fus_dat3.s.l2c_crip == 2)
840*4882a593Smuzhiyun 			l2_assoc = 8;
841*4882a593Smuzhiyun 		else if (mio_fus_dat3.s.l2c_crip == 1)
842*4882a593Smuzhiyun 			l2_assoc = 12;
843*4882a593Smuzhiyun 	} else {
844*4882a593Smuzhiyun 		uint64_t l2d_fus3;
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 		l2d_fus3 = cvmx_read_csr(CVMX_L2D_FUS3);
847*4882a593Smuzhiyun 		/*
848*4882a593Smuzhiyun 		 * Using shifts here, as bit position names are
849*4882a593Smuzhiyun 		 * different for each model but they all mean the
850*4882a593Smuzhiyun 		 * same.
851*4882a593Smuzhiyun 		 */
852*4882a593Smuzhiyun 		if ((l2d_fus3 >> 35) & 0x1)
853*4882a593Smuzhiyun 			l2_assoc = l2_assoc >> 2;
854*4882a593Smuzhiyun 		else if ((l2d_fus3 >> 34) & 0x1)
855*4882a593Smuzhiyun 			l2_assoc = l2_assoc >> 1;
856*4882a593Smuzhiyun 	}
857*4882a593Smuzhiyun 	return l2_assoc;
858*4882a593Smuzhiyun }
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun /**
861*4882a593Smuzhiyun  * Flush a line from the L2 cache
862*4882a593Smuzhiyun  * This should only be called from one core at a time, as this routine
863*4882a593Smuzhiyun  * sets the core to the 'debug' core in order to flush the line.
864*4882a593Smuzhiyun  *
865*4882a593Smuzhiyun  * @assoc:  Association (or way) to flush
866*4882a593Smuzhiyun  * @index:  Index to flush
867*4882a593Smuzhiyun  */
cvmx_l2c_flush_line(uint32_t assoc,uint32_t index)868*4882a593Smuzhiyun void cvmx_l2c_flush_line(uint32_t assoc, uint32_t index)
869*4882a593Smuzhiyun {
870*4882a593Smuzhiyun 	/* Check the range of the index. */
871*4882a593Smuzhiyun 	if (index > (uint32_t)cvmx_l2c_get_num_sets()) {
872*4882a593Smuzhiyun 		cvmx_dprintf("ERROR: cvmx_l2c_flush_line index out of range.\n");
873*4882a593Smuzhiyun 		return;
874*4882a593Smuzhiyun 	}
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	/* Check the range of association. */
877*4882a593Smuzhiyun 	if (assoc > (uint32_t)cvmx_l2c_get_num_assoc()) {
878*4882a593Smuzhiyun 		cvmx_dprintf("ERROR: cvmx_l2c_flush_line association out of range.\n");
879*4882a593Smuzhiyun 		return;
880*4882a593Smuzhiyun 	}
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	if (OCTEON_IS_MODEL(OCTEON_CN63XX)) {
883*4882a593Smuzhiyun 		uint64_t address;
884*4882a593Smuzhiyun 		/* Create the address based on index and association.
885*4882a593Smuzhiyun 		 * Bits<20:17> select the way of the cache block involved in
886*4882a593Smuzhiyun 		 *	       the operation
887*4882a593Smuzhiyun 		 * Bits<16:7> of the effect address select the index
888*4882a593Smuzhiyun 		 */
889*4882a593Smuzhiyun 		address = CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
890*4882a593Smuzhiyun 				(assoc << CVMX_L2C_TAG_ADDR_ALIAS_SHIFT) |
891*4882a593Smuzhiyun 				(index << CVMX_L2C_IDX_ADDR_SHIFT));
892*4882a593Smuzhiyun 		CVMX_CACHE_WBIL2I(address, 0);
893*4882a593Smuzhiyun 	} else {
894*4882a593Smuzhiyun 		union cvmx_l2c_dbg l2cdbg;
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 		l2cdbg.u64 = 0;
897*4882a593Smuzhiyun 		if (!OCTEON_IS_MODEL(OCTEON_CN30XX))
898*4882a593Smuzhiyun 			l2cdbg.s.ppnum = cvmx_get_core_num();
899*4882a593Smuzhiyun 		l2cdbg.s.finv = 1;
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 		l2cdbg.s.set = assoc;
902*4882a593Smuzhiyun 		cvmx_spinlock_lock(&cvmx_l2c_spinlock);
903*4882a593Smuzhiyun 		/*
904*4882a593Smuzhiyun 		 * Enter debug mode, and make sure all other writes
905*4882a593Smuzhiyun 		 * complete before we enter debug mode
906*4882a593Smuzhiyun 		 */
907*4882a593Smuzhiyun 		CVMX_SYNC;
908*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_DBG, l2cdbg.u64);
909*4882a593Smuzhiyun 		cvmx_read_csr(CVMX_L2C_DBG);
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 		CVMX_PREPARE_FOR_STORE(CVMX_ADD_SEG(CVMX_MIPS_SPACE_XKPHYS,
912*4882a593Smuzhiyun 						    index * CVMX_CACHE_LINE_SIZE),
913*4882a593Smuzhiyun 				       0);
914*4882a593Smuzhiyun 		/* Exit debug mode */
915*4882a593Smuzhiyun 		CVMX_SYNC;
916*4882a593Smuzhiyun 		cvmx_write_csr(CVMX_L2C_DBG, 0);
917*4882a593Smuzhiyun 		cvmx_read_csr(CVMX_L2C_DBG);
918*4882a593Smuzhiyun 		cvmx_spinlock_unlock(&cvmx_l2c_spinlock);
919*4882a593Smuzhiyun 	}
920*4882a593Smuzhiyun }
921