1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Copyright (C) 2006 Chris Dearman (chris@mips.com),
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun #include <linux/init.h>
6*4882a593Smuzhiyun #include <linux/kernel.h>
7*4882a593Smuzhiyun #include <linux/sched.h>
8*4882a593Smuzhiyun #include <linux/mm.h>
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <asm/cpu-type.h>
11*4882a593Smuzhiyun #include <asm/mipsregs.h>
12*4882a593Smuzhiyun #include <asm/bcache.h>
13*4882a593Smuzhiyun #include <asm/cacheops.h>
14*4882a593Smuzhiyun #include <asm/page.h>
15*4882a593Smuzhiyun #include <asm/mmu_context.h>
16*4882a593Smuzhiyun #include <asm/r4kcache.h>
17*4882a593Smuzhiyun #include <asm/mips-cps.h>
18*4882a593Smuzhiyun #include <asm/bootinfo.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun /*
21*4882a593Smuzhiyun * MIPS32/MIPS64 L2 cache handling
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun * Writeback and invalidate the secondary cache before DMA.
26*4882a593Smuzhiyun */
mips_sc_wback_inv(unsigned long addr,unsigned long size)27*4882a593Smuzhiyun static void mips_sc_wback_inv(unsigned long addr, unsigned long size)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun blast_scache_range(addr, addr + size);
30*4882a593Smuzhiyun }
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * Invalidate the secondary cache before DMA.
34*4882a593Smuzhiyun */
mips_sc_inv(unsigned long addr,unsigned long size)35*4882a593Smuzhiyun static void mips_sc_inv(unsigned long addr, unsigned long size)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun unsigned long lsize = cpu_scache_line_size();
38*4882a593Smuzhiyun unsigned long almask = ~(lsize - 1);
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun cache_op(Hit_Writeback_Inv_SD, addr & almask);
41*4882a593Smuzhiyun cache_op(Hit_Writeback_Inv_SD, (addr + size - 1) & almask);
42*4882a593Smuzhiyun blast_inv_scache_range(addr, addr + size);
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
mips_sc_enable(void)45*4882a593Smuzhiyun static void mips_sc_enable(void)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun /* L2 cache is permanently enabled */
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
mips_sc_disable(void)50*4882a593Smuzhiyun static void mips_sc_disable(void)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun /* L2 cache is permanently enabled */
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
mips_sc_prefetch_enable(void)55*4882a593Smuzhiyun static void mips_sc_prefetch_enable(void)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun unsigned long pftctl;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun if (mips_cm_revision() < CM_REV_CM2_5)
60*4882a593Smuzhiyun return;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /*
63*4882a593Smuzhiyun * If there is one or more L2 prefetch unit present then enable
64*4882a593Smuzhiyun * prefetching for both code & data, for all ports.
65*4882a593Smuzhiyun */
66*4882a593Smuzhiyun pftctl = read_gcr_l2_pft_control();
67*4882a593Smuzhiyun if (pftctl & CM_GCR_L2_PFT_CONTROL_NPFT) {
68*4882a593Smuzhiyun pftctl &= ~CM_GCR_L2_PFT_CONTROL_PAGEMASK;
69*4882a593Smuzhiyun pftctl |= PAGE_MASK & CM_GCR_L2_PFT_CONTROL_PAGEMASK;
70*4882a593Smuzhiyun pftctl |= CM_GCR_L2_PFT_CONTROL_PFTEN;
71*4882a593Smuzhiyun write_gcr_l2_pft_control(pftctl);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun set_gcr_l2_pft_control_b(CM_GCR_L2_PFT_CONTROL_B_PORTID |
74*4882a593Smuzhiyun CM_GCR_L2_PFT_CONTROL_B_CEN);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
mips_sc_prefetch_disable(void)78*4882a593Smuzhiyun static void mips_sc_prefetch_disable(void)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun if (mips_cm_revision() < CM_REV_CM2_5)
81*4882a593Smuzhiyun return;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun clear_gcr_l2_pft_control(CM_GCR_L2_PFT_CONTROL_PFTEN);
84*4882a593Smuzhiyun clear_gcr_l2_pft_control_b(CM_GCR_L2_PFT_CONTROL_B_PORTID |
85*4882a593Smuzhiyun CM_GCR_L2_PFT_CONTROL_B_CEN);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
mips_sc_prefetch_is_enabled(void)88*4882a593Smuzhiyun static bool mips_sc_prefetch_is_enabled(void)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun unsigned long pftctl;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun if (mips_cm_revision() < CM_REV_CM2_5)
93*4882a593Smuzhiyun return false;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun pftctl = read_gcr_l2_pft_control();
96*4882a593Smuzhiyun if (!(pftctl & CM_GCR_L2_PFT_CONTROL_NPFT))
97*4882a593Smuzhiyun return false;
98*4882a593Smuzhiyun return !!(pftctl & CM_GCR_L2_PFT_CONTROL_PFTEN);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun static struct bcache_ops mips_sc_ops = {
102*4882a593Smuzhiyun .bc_enable = mips_sc_enable,
103*4882a593Smuzhiyun .bc_disable = mips_sc_disable,
104*4882a593Smuzhiyun .bc_wback_inv = mips_sc_wback_inv,
105*4882a593Smuzhiyun .bc_inv = mips_sc_inv,
106*4882a593Smuzhiyun .bc_prefetch_enable = mips_sc_prefetch_enable,
107*4882a593Smuzhiyun .bc_prefetch_disable = mips_sc_prefetch_disable,
108*4882a593Smuzhiyun .bc_prefetch_is_enabled = mips_sc_prefetch_is_enabled,
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /*
112*4882a593Smuzhiyun * Check if the L2 cache controller is activated on a particular platform.
113*4882a593Smuzhiyun * MTI's L2 controller and the L2 cache controller of Broadcom's BMIPS
114*4882a593Smuzhiyun * cores both use c0_config2's bit 12 as "L2 Bypass" bit, that is the
115*4882a593Smuzhiyun * cache being disabled. However there is no guarantee for this to be
116*4882a593Smuzhiyun * true on all platforms. In an act of stupidity the spec defined bits
117*4882a593Smuzhiyun * 12..15 as implementation defined so below function will eventually have
118*4882a593Smuzhiyun * to be replaced by a platform specific probe.
119*4882a593Smuzhiyun */
mips_sc_is_activated(struct cpuinfo_mips * c)120*4882a593Smuzhiyun static inline int mips_sc_is_activated(struct cpuinfo_mips *c)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun unsigned int config2 = read_c0_config2();
123*4882a593Smuzhiyun unsigned int tmp;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* Check the bypass bit (L2B) */
126*4882a593Smuzhiyun switch (current_cpu_type()) {
127*4882a593Smuzhiyun case CPU_34K:
128*4882a593Smuzhiyun case CPU_74K:
129*4882a593Smuzhiyun case CPU_1004K:
130*4882a593Smuzhiyun case CPU_1074K:
131*4882a593Smuzhiyun case CPU_INTERAPTIV:
132*4882a593Smuzhiyun case CPU_PROAPTIV:
133*4882a593Smuzhiyun case CPU_P5600:
134*4882a593Smuzhiyun case CPU_BMIPS5000:
135*4882a593Smuzhiyun case CPU_QEMU_GENERIC:
136*4882a593Smuzhiyun case CPU_P6600:
137*4882a593Smuzhiyun if (config2 & (1 << 12))
138*4882a593Smuzhiyun return 0;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun tmp = (config2 >> 4) & 0x0f;
142*4882a593Smuzhiyun if (0 < tmp && tmp <= 7)
143*4882a593Smuzhiyun c->scache.linesz = 2 << tmp;
144*4882a593Smuzhiyun else
145*4882a593Smuzhiyun return 0;
146*4882a593Smuzhiyun return 1;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
mips_sc_probe_cm3(void)149*4882a593Smuzhiyun static int mips_sc_probe_cm3(void)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun struct cpuinfo_mips *c = ¤t_cpu_data;
152*4882a593Smuzhiyun unsigned long cfg = read_gcr_l2_config();
153*4882a593Smuzhiyun unsigned long sets, line_sz, assoc;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun if (cfg & CM_GCR_L2_CONFIG_BYPASS)
156*4882a593Smuzhiyun return 0;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun sets = cfg & CM_GCR_L2_CONFIG_SET_SIZE;
159*4882a593Smuzhiyun sets >>= __ffs(CM_GCR_L2_CONFIG_SET_SIZE);
160*4882a593Smuzhiyun if (sets)
161*4882a593Smuzhiyun c->scache.sets = 64 << sets;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun line_sz = cfg & CM_GCR_L2_CONFIG_LINE_SIZE;
164*4882a593Smuzhiyun line_sz >>= __ffs(CM_GCR_L2_CONFIG_LINE_SIZE);
165*4882a593Smuzhiyun if (line_sz)
166*4882a593Smuzhiyun c->scache.linesz = 2 << line_sz;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun assoc = cfg & CM_GCR_L2_CONFIG_ASSOC;
169*4882a593Smuzhiyun assoc >>= __ffs(CM_GCR_L2_CONFIG_ASSOC);
170*4882a593Smuzhiyun c->scache.ways = assoc + 1;
171*4882a593Smuzhiyun c->scache.waysize = c->scache.sets * c->scache.linesz;
172*4882a593Smuzhiyun c->scache.waybit = __ffs(c->scache.waysize);
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun if (c->scache.linesz) {
175*4882a593Smuzhiyun c->scache.flags &= ~MIPS_CACHE_NOT_PRESENT;
176*4882a593Smuzhiyun c->options |= MIPS_CPU_INCLUSIVE_CACHES;
177*4882a593Smuzhiyun return 1;
178*4882a593Smuzhiyun }
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun return 0;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
mips_sc_probe(void)183*4882a593Smuzhiyun static inline int mips_sc_probe(void)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun struct cpuinfo_mips *c = ¤t_cpu_data;
186*4882a593Smuzhiyun unsigned int config1, config2;
187*4882a593Smuzhiyun unsigned int tmp;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun /* Mark as not present until probe completed */
190*4882a593Smuzhiyun c->scache.flags |= MIPS_CACHE_NOT_PRESENT;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun if (mips_cm_revision() >= CM_REV_CM3)
193*4882a593Smuzhiyun return mips_sc_probe_cm3();
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /* Ignore anything but MIPSxx processors */
196*4882a593Smuzhiyun if (!(c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M64R1 |
197*4882a593Smuzhiyun MIPS_CPU_ISA_M32R2 | MIPS_CPU_ISA_M64R2 |
198*4882a593Smuzhiyun MIPS_CPU_ISA_M32R5 | MIPS_CPU_ISA_M64R5 |
199*4882a593Smuzhiyun MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R6)))
200*4882a593Smuzhiyun return 0;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /* Does this MIPS32/MIPS64 CPU have a config2 register? */
203*4882a593Smuzhiyun config1 = read_c0_config1();
204*4882a593Smuzhiyun if (!(config1 & MIPS_CONF_M))
205*4882a593Smuzhiyun return 0;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun config2 = read_c0_config2();
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (!mips_sc_is_activated(c))
210*4882a593Smuzhiyun return 0;
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun tmp = (config2 >> 8) & 0x0f;
213*4882a593Smuzhiyun if (tmp <= 7)
214*4882a593Smuzhiyun c->scache.sets = 64 << tmp;
215*4882a593Smuzhiyun else
216*4882a593Smuzhiyun return 0;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun tmp = (config2 >> 0) & 0x0f;
219*4882a593Smuzhiyun if (tmp <= 7)
220*4882a593Smuzhiyun c->scache.ways = tmp + 1;
221*4882a593Smuzhiyun else
222*4882a593Smuzhiyun return 0;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun if (current_cpu_type() == CPU_XBURST) {
225*4882a593Smuzhiyun switch (mips_machtype) {
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun * According to config2 it would be 5-ways, but that is
228*4882a593Smuzhiyun * contradicted by all documentation.
229*4882a593Smuzhiyun */
230*4882a593Smuzhiyun case MACH_INGENIC_JZ4770:
231*4882a593Smuzhiyun case MACH_INGENIC_JZ4775:
232*4882a593Smuzhiyun c->scache.ways = 4;
233*4882a593Smuzhiyun break;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /*
236*4882a593Smuzhiyun * According to config2 it would be 5-ways and 512-sets,
237*4882a593Smuzhiyun * but that is contradicted by all documentation.
238*4882a593Smuzhiyun */
239*4882a593Smuzhiyun case MACH_INGENIC_X1000:
240*4882a593Smuzhiyun case MACH_INGENIC_X1000E:
241*4882a593Smuzhiyun c->scache.sets = 256;
242*4882a593Smuzhiyun c->scache.ways = 4;
243*4882a593Smuzhiyun break;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun }
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun c->scache.waysize = c->scache.sets * c->scache.linesz;
248*4882a593Smuzhiyun c->scache.waybit = __ffs(c->scache.waysize);
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun c->scache.flags &= ~MIPS_CACHE_NOT_PRESENT;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun return 1;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
mips_sc_init(void)255*4882a593Smuzhiyun int mips_sc_init(void)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun int found = mips_sc_probe();
258*4882a593Smuzhiyun if (found) {
259*4882a593Smuzhiyun mips_sc_enable();
260*4882a593Smuzhiyun mips_sc_prefetch_enable();
261*4882a593Smuzhiyun bcops = &mips_sc_ops;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun return found;
264*4882a593Smuzhiyun }
265