1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/kernel.h>
4 #include <linux/delay.h>
6 #include <asm/l2cache.h>
7 #include <asm/metag_isa.h>
9 /* If non-0, then initialise the L2 cache */
10 static int l2cache_init
= 1;
11 /* If non-0, then initialise the L2 cache prefetch */
12 static int l2cache_init_pf
= 1;
16 static volatile u32 l2c_testdata
[16] __initdata
__aligned(64);
18 static int __init
parse_l2cache(char *p
)
22 if (get_option(&cp
, &l2cache_init
) != 1) {
23 pr_err("Bad l2cache parameter (%s)\n", p
);
28 early_param("l2cache", parse_l2cache
);
30 static int __init
parse_l2cache_pf(char *p
)
34 if (get_option(&cp
, &l2cache_init_pf
) != 1) {
35 pr_err("Bad l2cache_pf parameter (%s)\n", p
);
40 early_param("l2cache_pf", parse_l2cache_pf
);
42 static int __init
meta_l2c_setup(void)
45 * If the L2 cache isn't even present, don't do anything, but say so in
48 if (!meta_l2c_is_present()) {
49 pr_info("L2 Cache: Not present\n");
54 * Check whether the line size is recognised.
56 if (!meta_l2c_linesize()) {
57 pr_warn_once("L2 Cache: unknown line size id (config=0x%08x)\n",
64 l2c_pfenable
= _meta_l2c_pf_is_enabled();
67 * Enable the L2 cache and print to log whether it was already enabled
71 pr_info("L2 Cache: Enabling... ");
72 if (meta_l2c_enable())
73 pr_cont("already enabled\n");
77 pr_info("L2 Cache: Not enabling\n");
81 * Enable L2 cache prefetch.
83 if (l2cache_init_pf
) {
84 pr_info("L2 Cache: Enabling prefetch... ");
85 if (meta_l2c_pf_enable(1))
86 pr_cont("already enabled\n");
90 pr_info("L2 Cache: Not enabling prefetch\n");
95 core_initcall(meta_l2c_setup
);
97 int meta_l2c_disable(void)
102 if (!meta_l2c_is_present())
106 * Prevent other threads writing during the writeback, otherwise the
107 * writes will get "lost" when the L2 is disabled.
109 __global_lock2(flags
);
110 en
= meta_l2c_is_enabled();
112 _meta_l2c_pf_enable(0);
117 __global_unlock2(flags
);
122 int meta_l2c_enable(void)
127 if (!meta_l2c_is_present())
131 * Init (clearing the L2) can happen while the L2 is disabled, so other
132 * threads are safe to continue executing, however we must not init the
133 * cache if it's already enabled (dirty lines would be discarded), so
134 * this operation should still be atomic with other threads.
136 __global_lock1(flags
);
137 en
= meta_l2c_is_enabled();
141 _meta_l2c_pf_enable(l2c_pfenable
);
143 __global_unlock1(flags
);
148 int meta_l2c_pf_enable(int pfenable
)
151 int en
= l2c_pfenable
;
153 if (!meta_l2c_is_present())
157 * We read modify write the enable register, so this operation must be
158 * atomic with other threads.
160 __global_lock1(flags
);
162 l2c_pfenable
= pfenable
;
163 if (meta_l2c_is_enabled())
164 _meta_l2c_pf_enable(pfenable
);
165 __global_unlock1(flags
);
170 int meta_l2c_flush(void)
176 * Prevent other threads writing during the writeback. This also
177 * involves read modify writes.
179 __global_lock2(flags
);
180 en
= meta_l2c_is_enabled();
182 _meta_l2c_pf_enable(0);
188 _meta_l2c_pf_enable(l2c_pfenable
);
190 __global_unlock2(flags
);