blob: acfb89273dad7d9eaa1529f07e94b3b439733309 [file] [log] [blame]
Chris Dearman9318c512006-06-20 17:15:20 +01001/*
2 * Copyright (C) 2006 Chris Dearman (chris@mips.com),
3 */
4#include <linux/init.h>
5#include <linux/kernel.h>
6#include <linux/sched.h>
7#include <linux/mm.h>
8
Ralf Baechle69f24d12013-09-17 10:25:47 +02009#include <asm/cpu-type.h>
Chris Dearman9318c512006-06-20 17:15:20 +010010#include <asm/mipsregs.h>
11#include <asm/bcache.h>
12#include <asm/cacheops.h>
13#include <asm/page.h>
14#include <asm/pgtable.h>
Chris Dearman9318c512006-06-20 17:15:20 +010015#include <asm/mmu_context.h>
16#include <asm/r4kcache.h>
Paul Burtone83f7e02017-08-12 19:49:41 -070017#include <asm/mips-cps.h>
Chris Dearman9318c512006-06-20 17:15:20 +010018
19/*
20 * MIPS32/MIPS64 L2 cache handling
21 */
22
23/*
24 * Writeback and invalidate the secondary cache before DMA.
25 */
26static void mips_sc_wback_inv(unsigned long addr, unsigned long size)
27{
Atsushi Nemotoa2c2bc42006-06-22 19:42:43 +090028 blast_scache_range(addr, addr + size);
Chris Dearman9318c512006-06-20 17:15:20 +010029}
30
31/*
32 * Invalidate the secondary cache before DMA.
33 */
34static void mips_sc_inv(unsigned long addr, unsigned long size)
35{
Kevin Cernekee96983ff2009-09-18 19:12:45 -070036 unsigned long lsize = cpu_scache_line_size();
37 unsigned long almask = ~(lsize - 1);
38
39 cache_op(Hit_Writeback_Inv_SD, addr & almask);
40 cache_op(Hit_Writeback_Inv_SD, (addr + size - 1) & almask);
Atsushi Nemotoa2c2bc42006-06-22 19:42:43 +090041 blast_inv_scache_range(addr, addr + size);
Chris Dearman9318c512006-06-20 17:15:20 +010042}
43
44static void mips_sc_enable(void)
45{
46 /* L2 cache is permanently enabled */
47}
48
49static void mips_sc_disable(void)
50{
51 /* L2 cache is permanently enabled */
52}
53
Paul Burton4d035512015-09-22 10:10:54 -070054static void mips_sc_prefetch_enable(void)
55{
56 unsigned long pftctl;
57
58 if (mips_cm_revision() < CM_REV_CM2_5)
59 return;
60
61 /*
62 * If there is one or more L2 prefetch unit present then enable
63 * prefetching for both code & data, for all ports.
64 */
65 pftctl = read_gcr_l2_pft_control();
Paul Burton93c5bba52017-08-12 19:49:27 -070066 if (pftctl & CM_GCR_L2_PFT_CONTROL_NPFT) {
67 pftctl &= ~CM_GCR_L2_PFT_CONTROL_PAGEMASK;
68 pftctl |= PAGE_MASK & CM_GCR_L2_PFT_CONTROL_PAGEMASK;
69 pftctl |= CM_GCR_L2_PFT_CONTROL_PFTEN;
Paul Burton4d035512015-09-22 10:10:54 -070070 write_gcr_l2_pft_control(pftctl);
71
Paul Burton846e1912017-08-12 19:49:31 -070072 set_gcr_l2_pft_control_b(CM_GCR_L2_PFT_CONTROL_B_PORTID |
73 CM_GCR_L2_PFT_CONTROL_B_CEN);
Paul Burton4d035512015-09-22 10:10:54 -070074 }
75}
76
77static void mips_sc_prefetch_disable(void)
78{
Paul Burton4d035512015-09-22 10:10:54 -070079 if (mips_cm_revision() < CM_REV_CM2_5)
80 return;
81
Paul Burton846e1912017-08-12 19:49:31 -070082 clear_gcr_l2_pft_control(CM_GCR_L2_PFT_CONTROL_PFTEN);
83 clear_gcr_l2_pft_control_b(CM_GCR_L2_PFT_CONTROL_B_PORTID |
84 CM_GCR_L2_PFT_CONTROL_B_CEN);
Paul Burton4d035512015-09-22 10:10:54 -070085}
86
87static bool mips_sc_prefetch_is_enabled(void)
88{
89 unsigned long pftctl;
90
91 if (mips_cm_revision() < CM_REV_CM2_5)
92 return false;
93
94 pftctl = read_gcr_l2_pft_control();
Paul Burton93c5bba52017-08-12 19:49:27 -070095 if (!(pftctl & CM_GCR_L2_PFT_CONTROL_NPFT))
Paul Burton4d035512015-09-22 10:10:54 -070096 return false;
Paul Burton93c5bba52017-08-12 19:49:27 -070097 return !!(pftctl & CM_GCR_L2_PFT_CONTROL_PFTEN);
Paul Burton4d035512015-09-22 10:10:54 -070098}
99
Chris Dearman9318c512006-06-20 17:15:20 +0100100static struct bcache_ops mips_sc_ops = {
101 .bc_enable = mips_sc_enable,
102 .bc_disable = mips_sc_disable,
103 .bc_wback_inv = mips_sc_wback_inv,
Paul Burton4d035512015-09-22 10:10:54 -0700104 .bc_inv = mips_sc_inv,
105 .bc_prefetch_enable = mips_sc_prefetch_enable,
106 .bc_prefetch_disable = mips_sc_prefetch_disable,
107 .bc_prefetch_is_enabled = mips_sc_prefetch_is_enabled,
Chris Dearman9318c512006-06-20 17:15:20 +0100108};
109
Kevin Cernekeeea31a6b2010-10-20 20:05:42 -0700110/*
111 * Check if the L2 cache controller is activated on a particular platform.
112 * MTI's L2 controller and the L2 cache controller of Broadcom's BMIPS
113 * cores both use c0_config2's bit 12 as "L2 Bypass" bit, that is the
114 * cache being disabled. However there is no guarantee for this to be
115 * true on all platforms. In an act of stupidity the spec defined bits
116 * 12..15 as implementation defined so below function will eventually have
117 * to be replaced by a platform specific probe.
118 */
119static inline int mips_sc_is_activated(struct cpuinfo_mips *c)
120{
Kevin Cernekee081d8352010-11-02 22:28:01 -0700121 unsigned int config2 = read_c0_config2();
122 unsigned int tmp;
123
Kevin Cernekeeea31a6b2010-10-20 20:05:42 -0700124 /* Check the bypass bit (L2B) */
Ralf Baechle69f24d12013-09-17 10:25:47 +0200125 switch (current_cpu_type()) {
Kevin Cernekeeea31a6b2010-10-20 20:05:42 -0700126 case CPU_34K:
127 case CPU_74K:
128 case CPU_1004K:
Steven J. Hill442e14a2014-01-17 15:03:50 -0600129 case CPU_1074K:
Leonid Yegoshin26ab96d2013-11-27 10:07:53 +0000130 case CPU_INTERAPTIV:
Leonid Yegoshin708ac4b2013-11-14 16:12:27 +0000131 case CPU_PROAPTIV:
James Hoganaced4cb2014-01-22 16:19:38 +0000132 case CPU_P5600:
Kevin Cernekeeea31a6b2010-10-20 20:05:42 -0700133 case CPU_BMIPS5000:
Leonid Yegoshin46950892014-11-24 12:59:01 +0000134 case CPU_QEMU_GENERIC:
Paul Burton1091bfa2016-02-03 03:26:38 +0000135 case CPU_P6600:
Kevin Cernekeeea31a6b2010-10-20 20:05:42 -0700136 if (config2 & (1 << 12))
137 return 0;
138 }
139
140 tmp = (config2 >> 4) & 0x0f;
141 if (0 < tmp && tmp <= 7)
142 c->scache.linesz = 2 << tmp;
143 else
144 return 0;
Kevin Cernekee081d8352010-11-02 22:28:01 -0700145 return 1;
Kevin Cernekeeea31a6b2010-10-20 20:05:42 -0700146}
147
Paul Burton7d53e9c2015-07-09 10:40:42 +0100148static int __init mips_sc_probe_cm3(void)
149{
150 struct cpuinfo_mips *c = &current_cpu_data;
151 unsigned long cfg = read_gcr_l2_config();
152 unsigned long sets, line_sz, assoc;
153
Paul Burton93c5bba52017-08-12 19:49:27 -0700154 if (cfg & CM_GCR_L2_CONFIG_BYPASS)
Paul Burton7d53e9c2015-07-09 10:40:42 +0100155 return 0;
156
Paul Burton93c5bba52017-08-12 19:49:27 -0700157 sets = cfg & CM_GCR_L2_CONFIG_SET_SIZE;
158 sets >>= __ffs(CM_GCR_L2_CONFIG_SET_SIZE);
Govindraj Raja56fa81f2016-02-29 11:41:20 +0000159 if (sets)
160 c->scache.sets = 64 << sets;
Paul Burton7d53e9c2015-07-09 10:40:42 +0100161
Paul Burton93c5bba52017-08-12 19:49:27 -0700162 line_sz = cfg & CM_GCR_L2_CONFIG_LINE_SIZE;
163 line_sz >>= __ffs(CM_GCR_L2_CONFIG_LINE_SIZE);
Govindraj Raja56fa81f2016-02-29 11:41:20 +0000164 if (line_sz)
165 c->scache.linesz = 2 << line_sz;
Paul Burton7d53e9c2015-07-09 10:40:42 +0100166
Paul Burton93c5bba52017-08-12 19:49:27 -0700167 assoc = cfg & CM_GCR_L2_CONFIG_ASSOC;
168 assoc >>= __ffs(CM_GCR_L2_CONFIG_ASSOC);
Paul Burton7d53e9c2015-07-09 10:40:42 +0100169 c->scache.ways = assoc + 1;
170 c->scache.waysize = c->scache.sets * c->scache.linesz;
171 c->scache.waybit = __ffs(c->scache.waysize);
172
Govindraj Raja56fa81f2016-02-29 11:41:20 +0000173 if (c->scache.linesz) {
174 c->scache.flags &= ~MIPS_CACHE_NOT_PRESENT;
Paul Burton48ed33c2016-08-19 18:13:36 +0100175 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
Govindraj Raja56fa81f2016-02-29 11:41:20 +0000176 return 1;
177 }
Paul Burton7d53e9c2015-07-09 10:40:42 +0100178
Govindraj Raja56fa81f2016-02-29 11:41:20 +0000179 return 0;
Paul Burton7d53e9c2015-07-09 10:40:42 +0100180}
181
Chris Dearman9318c512006-06-20 17:15:20 +0100182static inline int __init mips_sc_probe(void)
183{
184 struct cpuinfo_mips *c = &current_cpu_data;
185 unsigned int config1, config2;
186 unsigned int tmp;
187
188 /* Mark as not present until probe completed */
189 c->scache.flags |= MIPS_CACHE_NOT_PRESENT;
190
Paul Burton7d53e9c2015-07-09 10:40:42 +0100191 if (mips_cm_revision() >= CM_REV_CM3)
192 return mips_sc_probe_cm3();
193
Chris Dearman9318c512006-06-20 17:15:20 +0100194 /* Ignore anything but MIPSxx processors */
Deng-Cheng Zhuadb37892013-04-01 18:14:28 +0000195 if (!(c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M32R2 |
Markos Chandrasb5ad2c22015-01-15 10:28:29 +0000196 MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R1 |
197 MIPS_CPU_ISA_M64R2 | MIPS_CPU_ISA_M64R6)))
Chris Dearman9318c512006-06-20 17:15:20 +0100198 return 0;
199
200 /* Does this MIPS32/MIPS64 CPU have a config2 register? */
201 config1 = read_c0_config1();
202 if (!(config1 & MIPS_CONF_M))
203 return 0;
204
205 config2 = read_c0_config2();
Kevin Cernekeeea31a6b2010-10-20 20:05:42 -0700206
207 if (!mips_sc_is_activated(c))
Chris Dearman9318c512006-06-20 17:15:20 +0100208 return 0;
209
210 tmp = (config2 >> 8) & 0x0f;
Andrzej Hajda05513992015-09-21 15:34:08 +0200211 if (tmp <= 7)
Chris Dearman9318c512006-06-20 17:15:20 +0100212 c->scache.sets = 64 << tmp;
213 else
214 return 0;
215
216 tmp = (config2 >> 0) & 0x0f;
Andrzej Hajda05513992015-09-21 15:34:08 +0200217 if (tmp <= 7)
Chris Dearman9318c512006-06-20 17:15:20 +0100218 c->scache.ways = tmp + 1;
219 else
220 return 0;
221
222 c->scache.waysize = c->scache.sets * c->scache.linesz;
Atsushi Nemotoa2c2bc42006-06-22 19:42:43 +0900223 c->scache.waybit = __ffs(c->scache.waysize);
Chris Dearman9318c512006-06-20 17:15:20 +0100224
225 c->scache.flags &= ~MIPS_CACHE_NOT_PRESENT;
226
227 return 1;
228}
229
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000230int mips_sc_init(void)
Chris Dearman9318c512006-06-20 17:15:20 +0100231{
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100232 int found = mips_sc_probe();
Chris Dearman9318c512006-06-20 17:15:20 +0100233 if (found) {
234 mips_sc_enable();
Paul Burton4d035512015-09-22 10:10:54 -0700235 mips_sc_prefetch_enable();
Chris Dearman9318c512006-06-20 17:15:20 +0100236 bcops = &mips_sc_ops;
237 }
238 return found;
239}