2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #define pr_fmt(fmt) "uniphier: " fmt
17 #include <linux/init.h>
19 #include <linux/log2.h>
20 #include <linux/of_address.h>
21 #include <linux/slab.h>
22 #include <asm/hardware/cache-uniphier.h>
23 #include <asm/outercache.h>
25 /* control registers */
26 #define UNIPHIER_SSCC 0x0 /* Control Register */
27 #define UNIPHIER_SSCC_BST BIT(20) /* UCWG burst read */
28 #define UNIPHIER_SSCC_ACT BIT(19) /* Inst-Data separate */
29 #define UNIPHIER_SSCC_WTG BIT(18) /* WT gathering on */
30 #define UNIPHIER_SSCC_PRD BIT(17) /* enable pre-fetch */
31 #define UNIPHIER_SSCC_ON BIT(0) /* enable cache */
32 #define UNIPHIER_SSCLPDAWCR 0x30 /* Unified/Data Active Way Control */
33 #define UNIPHIER_SSCLPIAWCR 0x34 /* Instruction Active Way Control */
35 /* revision registers */
36 #define UNIPHIER_SSCID 0x0 /* ID Register */
38 /* operation registers */
39 #define UNIPHIER_SSCOPE 0x244 /* Cache Operation Primitive Entry */
40 #define UNIPHIER_SSCOPE_CM_INV 0x0 /* invalidate */
41 #define UNIPHIER_SSCOPE_CM_CLEAN 0x1 /* clean */
42 #define UNIPHIER_SSCOPE_CM_FLUSH 0x2 /* flush */
43 #define UNIPHIER_SSCOPE_CM_SYNC 0x8 /* sync (drain bufs) */
44 #define UNIPHIER_SSCOPE_CM_FLUSH_PREFETCH 0x9 /* flush p-fetch buf */
45 #define UNIPHIER_SSCOQM 0x248 /* Cache Operation Queue Mode */
46 #define UNIPHIER_SSCOQM_TID_MASK (0x3 << 21)
47 #define UNIPHIER_SSCOQM_TID_LRU_DATA (0x0 << 21)
48 #define UNIPHIER_SSCOQM_TID_LRU_INST (0x1 << 21)
49 #define UNIPHIER_SSCOQM_TID_WAY (0x2 << 21)
50 #define UNIPHIER_SSCOQM_S_MASK (0x3 << 17)
51 #define UNIPHIER_SSCOQM_S_RANGE (0x0 << 17)
52 #define UNIPHIER_SSCOQM_S_ALL (0x1 << 17)
53 #define UNIPHIER_SSCOQM_S_WAY (0x2 << 17)
54 #define UNIPHIER_SSCOQM_CE BIT(15) /* notify completion */
55 #define UNIPHIER_SSCOQM_CM_INV 0x0 /* invalidate */
56 #define UNIPHIER_SSCOQM_CM_CLEAN 0x1 /* clean */
57 #define UNIPHIER_SSCOQM_CM_FLUSH 0x2 /* flush */
58 #define UNIPHIER_SSCOQM_CM_PREFETCH 0x3 /* prefetch to cache */
59 #define UNIPHIER_SSCOQM_CM_PREFETCH_BUF 0x4 /* prefetch to pf-buf */
60 #define UNIPHIER_SSCOQM_CM_TOUCH 0x5 /* touch */
61 #define UNIPHIER_SSCOQM_CM_TOUCH_ZERO 0x6 /* touch to zero */
62 #define UNIPHIER_SSCOQM_CM_TOUCH_DIRTY 0x7 /* touch with dirty */
63 #define UNIPHIER_SSCOQAD 0x24c /* Cache Operation Queue Address */
64 #define UNIPHIER_SSCOQSZ 0x250 /* Cache Operation Queue Size */
65 #define UNIPHIER_SSCOQMASK 0x254 /* Cache Operation Queue Address Mask */
66 #define UNIPHIER_SSCOQWN 0x258 /* Cache Operation Queue Way Number */
67 #define UNIPHIER_SSCOPPQSEF 0x25c /* Cache Operation Queue Set Complete*/
68 #define UNIPHIER_SSCOPPQSEF_FE BIT(1)
69 #define UNIPHIER_SSCOPPQSEF_OE BIT(0)
70 #define UNIPHIER_SSCOLPQS 0x260 /* Cache Operation Queue Status */
71 #define UNIPHIER_SSCOLPQS_EF BIT(2)
72 #define UNIPHIER_SSCOLPQS_EST BIT(1)
73 #define UNIPHIER_SSCOLPQS_QST BIT(0)
75 /* Is the touch/pre-fetch destination specified by ways? */
76 #define UNIPHIER_SSCOQM_TID_IS_WAY(op) \
77 ((op & UNIPHIER_SSCOQM_TID_MASK) == UNIPHIER_SSCOQM_TID_WAY)
78 /* Is the operation region specified by address range? */
79 #define UNIPHIER_SSCOQM_S_IS_RANGE(op) \
80 ((op & UNIPHIER_SSCOQM_S_MASK) == UNIPHIER_SSCOQM_S_RANGE)
83 * uniphier_cache_data - UniPhier outer cache specific data
85 * @ctrl_base: virtual base address of control registers
86 * @rev_base: virtual base address of revision registers
87 * @op_base: virtual base address of operation registers
88 * @way_present_mask: each bit specifies if the way is present
89 * @way_locked_mask: each bit specifies if the way is locked
90 * @nsets: number of associativity sets
91 * @line_size: line size in bytes
92 * @range_op_max_size: max size that can be handled by a single range operation
93 * @list: list node to include this level in the whole cache hierarchy
95 struct uniphier_cache_data
{
96 void __iomem
*ctrl_base
;
97 void __iomem
*rev_base
;
98 void __iomem
*op_base
;
103 u32 range_op_max_size
;
104 struct list_head list
;
108 * List of the whole outer cache hierarchy. This list is only modified during
109 * the early boot stage, so no mutex is taken for the access to the list.
111 static LIST_HEAD(uniphier_cache_list
);
114 * __uniphier_cache_sync - perform a sync point for a particular cache level
116 * @data: cache controller specific data
118 static void __uniphier_cache_sync(struct uniphier_cache_data
*data
)
120 /* This sequence need not be atomic. Do not disable IRQ. */
121 writel_relaxed(UNIPHIER_SSCOPE_CM_SYNC
,
122 data
->op_base
+ UNIPHIER_SSCOPE
);
123 /* need a read back to confirm */
124 readl_relaxed(data
->op_base
+ UNIPHIER_SSCOPE
);
128 * __uniphier_cache_maint_common - run a queue operation for a particular level
130 * @data: cache controller specific data
131 * @start: start address of range operation (don't care for "all" operation)
132 * @size: data size of range operation (don't care for "all" operation)
133 * @operation: flags to specify the desired cache operation
135 static void __uniphier_cache_maint_common(struct uniphier_cache_data
*data
,
143 * No spin lock is necessary here because:
145 * [1] This outer cache controller is able to accept maintenance
146 * operations from multiple CPUs at a time in an SMP system; if a
147 * maintenance operation is under way and another operation is issued,
148 * the new one is stored in the queue. The controller performs one
149 * operation after another. If the queue is full, the status register,
150 * UNIPHIER_SSCOPPQSEF, indicates that the queue registration has
151 * failed. The status registers, UNIPHIER_{SSCOPPQSEF, SSCOLPQS}, have
152 * different instances for each CPU, i.e. each CPU can track the status
153 * of the maintenance operations triggered by itself.
155 * [2] The cache command registers, UNIPHIER_{SSCOQM, SSCOQAD, SSCOQSZ,
156 * SSCOQWN}, are shared between multiple CPUs, but the hardware still
157 * guarantees the registration sequence is atomic; the write access to
158 * them are arbitrated by the hardware. The first accessor to the
159 * register, UNIPHIER_SSCOQM, holds the access right and it is released
160 * by reading the status register, UNIPHIER_SSCOPPQSEF. While one CPU
161 * is holding the access right, other CPUs fail to register operations.
162 * One CPU should not hold the access right for a long time, so local
163 * IRQs should be disabled while the following sequence.
165 local_irq_save(flags
);
167 /* clear the complete notification flag */
168 writel_relaxed(UNIPHIER_SSCOLPQS_EF
, data
->op_base
+ UNIPHIER_SSCOLPQS
);
171 /* set cache operation */
172 writel_relaxed(UNIPHIER_SSCOQM_CE
| operation
,
173 data
->op_base
+ UNIPHIER_SSCOQM
);
175 /* set address range if needed */
176 if (likely(UNIPHIER_SSCOQM_S_IS_RANGE(operation
))) {
177 writel_relaxed(start
, data
->op_base
+ UNIPHIER_SSCOQAD
);
178 writel_relaxed(size
, data
->op_base
+ UNIPHIER_SSCOQSZ
);
181 /* set target ways if needed */
182 if (unlikely(UNIPHIER_SSCOQM_TID_IS_WAY(operation
)))
183 writel_relaxed(data
->way_locked_mask
,
184 data
->op_base
+ UNIPHIER_SSCOQWN
);
185 } while (unlikely(readl_relaxed(data
->op_base
+ UNIPHIER_SSCOPPQSEF
) &
186 (UNIPHIER_SSCOPPQSEF_FE
| UNIPHIER_SSCOPPQSEF_OE
)));
188 /* wait until the operation is completed */
189 while (likely(readl_relaxed(data
->op_base
+ UNIPHIER_SSCOLPQS
) !=
190 UNIPHIER_SSCOLPQS_EF
))
193 local_irq_restore(flags
);
196 static void __uniphier_cache_maint_all(struct uniphier_cache_data
*data
,
199 __uniphier_cache_maint_common(data
, 0, 0,
200 UNIPHIER_SSCOQM_S_ALL
| operation
);
202 __uniphier_cache_sync(data
);
205 static void __uniphier_cache_maint_range(struct uniphier_cache_data
*data
,
206 unsigned long start
, unsigned long end
,
212 * If the start address is not aligned,
213 * perform a cache operation for the first cache-line
215 start
= start
& ~(data
->line_size
- 1);
219 if (unlikely(size
>= (unsigned long)(-data
->line_size
))) {
220 /* this means cache operation for all range */
221 __uniphier_cache_maint_all(data
, operation
);
226 * If the end address is not aligned,
227 * perform a cache operation for the last cache-line
229 size
= ALIGN(size
, data
->line_size
);
232 unsigned long chunk_size
= min_t(unsigned long, size
,
233 data
->range_op_max_size
);
235 __uniphier_cache_maint_common(data
, start
, chunk_size
,
236 UNIPHIER_SSCOQM_S_RANGE
| operation
);
242 __uniphier_cache_sync(data
);
245 static void __uniphier_cache_enable(struct uniphier_cache_data
*data
, bool on
)
250 val
= UNIPHIER_SSCC_WTG
| UNIPHIER_SSCC_PRD
| UNIPHIER_SSCC_ON
;
252 writel_relaxed(val
, data
->ctrl_base
+ UNIPHIER_SSCC
);
255 static void __init
__uniphier_cache_set_locked_ways(
256 struct uniphier_cache_data
*data
,
259 data
->way_locked_mask
= way_mask
& data
->way_present_mask
;
261 writel_relaxed(~data
->way_locked_mask
& data
->way_present_mask
,
262 data
->ctrl_base
+ UNIPHIER_SSCLPDAWCR
);
265 static void uniphier_cache_maint_range(unsigned long start
, unsigned long end
,
268 struct uniphier_cache_data
*data
;
270 list_for_each_entry(data
, &uniphier_cache_list
, list
)
271 __uniphier_cache_maint_range(data
, start
, end
, operation
);
274 static void uniphier_cache_maint_all(u32 operation
)
276 struct uniphier_cache_data
*data
;
278 list_for_each_entry(data
, &uniphier_cache_list
, list
)
279 __uniphier_cache_maint_all(data
, operation
);
282 static void uniphier_cache_inv_range(unsigned long start
, unsigned long end
)
284 uniphier_cache_maint_range(start
, end
, UNIPHIER_SSCOQM_CM_INV
);
287 static void uniphier_cache_clean_range(unsigned long start
, unsigned long end
)
289 uniphier_cache_maint_range(start
, end
, UNIPHIER_SSCOQM_CM_CLEAN
);
292 static void uniphier_cache_flush_range(unsigned long start
, unsigned long end
)
294 uniphier_cache_maint_range(start
, end
, UNIPHIER_SSCOQM_CM_FLUSH
);
297 static void __init
uniphier_cache_inv_all(void)
299 uniphier_cache_maint_all(UNIPHIER_SSCOQM_CM_INV
);
302 static void uniphier_cache_flush_all(void)
304 uniphier_cache_maint_all(UNIPHIER_SSCOQM_CM_FLUSH
);
307 static void uniphier_cache_disable(void)
309 struct uniphier_cache_data
*data
;
311 list_for_each_entry_reverse(data
, &uniphier_cache_list
, list
)
312 __uniphier_cache_enable(data
, false);
314 uniphier_cache_flush_all();
317 static void __init
uniphier_cache_enable(void)
319 struct uniphier_cache_data
*data
;
321 uniphier_cache_inv_all();
323 list_for_each_entry(data
, &uniphier_cache_list
, list
) {
324 __uniphier_cache_enable(data
, true);
325 __uniphier_cache_set_locked_ways(data
, 0);
329 static void uniphier_cache_sync(void)
331 struct uniphier_cache_data
*data
;
333 list_for_each_entry(data
, &uniphier_cache_list
, list
)
334 __uniphier_cache_sync(data
);
337 int __init
uniphier_cache_l2_is_enabled(void)
339 struct uniphier_cache_data
*data
;
341 data
= list_first_entry_or_null(&uniphier_cache_list
,
342 struct uniphier_cache_data
, list
);
346 return !!(readl_relaxed(data
->ctrl_base
+ UNIPHIER_SSCC
) &
350 void __init
uniphier_cache_l2_touch_range(unsigned long start
,
353 struct uniphier_cache_data
*data
;
355 data
= list_first_entry_or_null(&uniphier_cache_list
,
356 struct uniphier_cache_data
, list
);
358 __uniphier_cache_maint_range(data
, start
, end
,
359 UNIPHIER_SSCOQM_TID_WAY
|
360 UNIPHIER_SSCOQM_CM_TOUCH
);
363 void __init
uniphier_cache_l2_set_locked_ways(u32 way_mask
)
365 struct uniphier_cache_data
*data
;
367 data
= list_first_entry_or_null(&uniphier_cache_list
,
368 struct uniphier_cache_data
, list
);
370 __uniphier_cache_set_locked_ways(data
, way_mask
);
373 static const struct of_device_id uniphier_cache_match
[] __initconst
= {
375 .compatible
= "socionext,uniphier-system-cache",
380 static int __init
__uniphier_cache_init(struct device_node
*np
,
381 unsigned int *cache_level
)
383 struct uniphier_cache_data
*data
;
384 u32 level
, cache_size
;
385 struct device_node
*next_np
;
388 if (!of_match_node(uniphier_cache_match
, np
)) {
389 pr_err("L%d: not compatible with uniphier cache\n",
394 if (of_property_read_u32(np
, "cache-level", &level
)) {
395 pr_err("L%d: cache-level is not specified\n", *cache_level
);
399 if (level
!= *cache_level
) {
400 pr_err("L%d: cache-level is unexpected value %d\n",
401 *cache_level
, level
);
405 if (!of_property_read_bool(np
, "cache-unified")) {
406 pr_err("L%d: cache-unified is not specified\n", *cache_level
);
410 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
414 if (of_property_read_u32(np
, "cache-line-size", &data
->line_size
) ||
415 !is_power_of_2(data
->line_size
)) {
416 pr_err("L%d: cache-line-size is unspecified or invalid\n",
422 if (of_property_read_u32(np
, "cache-sets", &data
->nsets
) ||
423 !is_power_of_2(data
->nsets
)) {
424 pr_err("L%d: cache-sets is unspecified or invalid\n",
430 if (of_property_read_u32(np
, "cache-size", &cache_size
) ||
431 cache_size
== 0 || cache_size
% (data
->nsets
* data
->line_size
)) {
432 pr_err("L%d: cache-size is unspecified or invalid\n",
438 data
->way_present_mask
=
439 ((u32
)1 << cache_size
/ data
->nsets
/ data
->line_size
) - 1;
441 data
->ctrl_base
= of_iomap(np
, 0);
442 if (!data
->ctrl_base
) {
443 pr_err("L%d: failed to map control register\n", *cache_level
);
448 data
->rev_base
= of_iomap(np
, 1);
449 if (!data
->rev_base
) {
450 pr_err("L%d: failed to map revision register\n", *cache_level
);
455 data
->op_base
= of_iomap(np
, 2);
456 if (!data
->op_base
) {
457 pr_err("L%d: failed to map operation register\n", *cache_level
);
462 if (*cache_level
== 2) {
463 u32 revision
= readl(data
->rev_base
+ UNIPHIER_SSCID
);
465 * The size of range operation is limited to (1 << 22) or less
466 * for PH-sLD8 or older SoCs.
468 if (revision
<= 0x16)
469 data
->range_op_max_size
= (u32
)1 << 22;
472 data
->range_op_max_size
-= data
->line_size
;
474 INIT_LIST_HEAD(&data
->list
);
475 list_add_tail(&data
->list
, &uniphier_cache_list
); /* no mutex */
478 * OK, this level has been successfully initialized. Look for the next
479 * level cache. Do not roll back even if the initialization of the
480 * next level cache fails because we want to continue with available
483 next_np
= of_find_next_cache_node(np
);
486 ret
= __uniphier_cache_init(next_np
, cache_level
);
488 of_node_put(next_np
);
492 iounmap(data
->op_base
);
493 iounmap(data
->rev_base
);
494 iounmap(data
->ctrl_base
);
500 int __init
uniphier_cache_init(void)
502 struct device_node
*np
= NULL
;
503 unsigned int cache_level
;
506 /* look for level 2 cache */
507 while ((np
= of_find_matching_node(np
, uniphier_cache_match
)))
508 if (!of_property_read_u32(np
, "cache-level", &cache_level
) &&
515 ret
= __uniphier_cache_init(np
, &cache_level
);
520 * Error out iif L2 initialization fails. Continue with any
521 * error on L3 or outer because they are optional.
523 if (cache_level
== 2) {
524 pr_err("failed to initialize L2 cache\n");
532 outer_cache
.inv_range
= uniphier_cache_inv_range
;
533 outer_cache
.clean_range
= uniphier_cache_clean_range
;
534 outer_cache
.flush_range
= uniphier_cache_flush_range
;
535 outer_cache
.flush_all
= uniphier_cache_flush_all
;
536 outer_cache
.disable
= uniphier_cache_disable
;
537 outer_cache
.sync
= uniphier_cache_sync
;
539 uniphier_cache_enable();
541 pr_info("enabled outer cache (cache level: %d)\n", cache_level
);