2 * CCI cache coherent interconnect driver
4 * Copyright (C) 2013 ARM Ltd.
5 * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/arm-cci.h>
19 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/slab.h>
23 #include <asm/cacheflush.h>
24 #include <asm/smp_plat.h>
26 #define CCI_PORT_CTRL 0x0
27 #define CCI_CTRL_STATUS 0xc
29 #define CCI_ENABLE_SNOOP_REQ 0x1
30 #define CCI_ENABLE_DVM_REQ 0x2
31 #define CCI_ENABLE_REQ (CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
35 unsigned int nb_ace_lite
;
38 enum cci_ace_port_type
{
39 ACE_INVALID_PORT
= 0x0,
47 enum cci_ace_port_type type
;
48 struct device_node
*dn
;
51 static struct cci_ace_port
*ports
;
52 static unsigned int nb_cci_ports
;
54 static void __iomem
*cci_ctrl_base
;
55 static unsigned long cci_ctrl_phys
;
63 * Use the port MSB as valid flag, shift can be made dynamic
64 * by computing number of bits required for port indexes.
65 * Code disabling CCI cpu ports runs with D-cache invalidated
66 * and SCTLR bit clear so data accesses must be kept to a minimum
67 * to improve performance; for now shift is left static to
68 * avoid one more data access while disabling the CCI port.
70 #define PORT_VALID_SHIFT 31
71 #define PORT_VALID (0x1 << PORT_VALID_SHIFT)
73 static inline void init_cpu_port(struct cpu_port
*port
, u32 index
, u64 mpidr
)
75 port
->port
= PORT_VALID
| index
;
79 static inline bool cpu_port_is_valid(struct cpu_port
*port
)
81 return !!(port
->port
& PORT_VALID
);
84 static inline bool cpu_port_match(struct cpu_port
*port
, u64 mpidr
)
86 return port
->mpidr
== (mpidr
& MPIDR_HWID_BITMASK
);
89 static struct cpu_port cpu_port
[NR_CPUS
];
92 * __cci_ace_get_port - Function to retrieve the port index connected to
95 * @dn: device node of the device to look-up
99 * - CCI port index if success
100 * - -ENODEV if failure
102 static int __cci_ace_get_port(struct device_node
*dn
, int type
)
106 struct device_node
*cci_portn
;
108 cci_portn
= of_parse_phandle(dn
, "cci-control-port", 0);
109 for (i
= 0; i
< nb_cci_ports
; i
++) {
110 ace_match
= ports
[i
].type
== type
;
111 if (ace_match
&& cci_portn
== ports
[i
].dn
)
117 int cci_ace_get_port(struct device_node
*dn
)
119 return __cci_ace_get_port(dn
, ACE_LITE_PORT
);
121 EXPORT_SYMBOL_GPL(cci_ace_get_port
);
123 static void __init
cci_ace_init_ports(void)
128 struct device_node
*cpun
, *cpus
;
130 cpus
= of_find_node_by_path("/cpus");
131 if (WARN(!cpus
, "Missing cpus node, bailing out\n"))
134 if (WARN_ON(of_property_read_u32(cpus
, "#address-cells", &ac
)))
135 ac
= of_n_addr_cells(cpus
);
138 * Port index look-up speeds up the function disabling ports by CPU,
139 * since the logical to port index mapping is done once and does
140 * not change after system boot.
141 * The stashed index array is initialized for all possible CPUs
144 for_each_child_of_node(cpus
, cpun
) {
145 if (of_node_cmp(cpun
->type
, "cpu"))
147 cell
= of_get_property(cpun
, "reg", NULL
);
148 if (WARN(!cell
, "%s: missing reg property\n", cpun
->full_name
))
151 hwid
= of_read_number(cell
, ac
);
152 cpu
= get_logical_index(hwid
& MPIDR_HWID_BITMASK
);
154 if (cpu
< 0 || !cpu_possible(cpu
))
156 port
= __cci_ace_get_port(cpun
, ACE_PORT
);
160 init_cpu_port(&cpu_port
[cpu
], port
, cpu_logical_map(cpu
));
163 for_each_possible_cpu(cpu
) {
164 WARN(!cpu_port_is_valid(&cpu_port
[cpu
]),
165 "CPU %u does not have an associated CCI port\n",
170 * Functions to enable/disable a CCI interconnect slave port
172 * They are called by low-level power management code to disable slave
173 * interfaces snoops and DVM broadcast.
174 * Since they may execute with cache data allocation disabled and
175 * after the caches have been cleaned and invalidated the functions provide
176 * no explicit locking since they may run with D-cache disabled, so normal
177 * cacheable kernel locks based on ldrex/strex may not work.
178 * Locking has to be provided by BSP implementations to ensure proper
183 * cci_port_control() - function to control a CCI port
185 * @port: index of the port to setup
186 * @enable: if true enables the port, if false disables it
188 static void notrace
cci_port_control(unsigned int port
, bool enable
)
190 void __iomem
*base
= ports
[port
].base
;
192 writel_relaxed(enable
? CCI_ENABLE_REQ
: 0, base
+ CCI_PORT_CTRL
);
194 * This function is called from power down procedures
195 * and must not execute any instruction that might
196 * cause the processor to be put in a quiescent state
197 * (eg wfi). Hence, cpu_relax() can not be added to this
198 * read loop to optimize power, since it might hide possibly
199 * disruptive operations.
201 while (readl_relaxed(cci_ctrl_base
+ CCI_CTRL_STATUS
) & 0x1)
206 * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
209 * @mpidr: mpidr of the CPU whose CCI port should be disabled
211 * Disabling a CCI port for a CPU implies disabling the CCI port
212 * controlling that CPU cluster. Code disabling CPU CCI ports
213 * must make sure that the CPU running the code is the last active CPU
214 * in the cluster ie all other CPUs are quiescent in a low power state.
218 * -ENODEV on port look-up failure
220 int notrace
cci_disable_port_by_cpu(u64 mpidr
)
224 for (cpu
= 0; cpu
< nr_cpu_ids
; cpu
++) {
225 is_valid
= cpu_port_is_valid(&cpu_port
[cpu
]);
226 if (is_valid
&& cpu_port_match(&cpu_port
[cpu
], mpidr
)) {
227 cci_port_control(cpu_port
[cpu
].port
, false);
233 EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu
);
236 * cci_enable_port_for_self() - enable a CCI port for calling CPU
238 * Enabling a CCI port for the calling CPU implies enabling the CCI
239 * port controlling that CPU's cluster. Caller must make sure that the
240 * CPU running the code is the first active CPU in the cluster and all
241 * other CPUs are quiescent in a low power state or waiting for this CPU
242 * to complete the CCI initialization.
244 * Because this is called when the MMU is still off and with no stack,
245 * the code must be position independent and ideally rely on callee
246 * clobbered registers only. To achieve this we must code this function
247 * entirely in assembler.
249 * On success this returns with the proper CCI port enabled. In case of
250 * any failure this never returns as the inability to enable the CCI is
251 * fatal and there is no possible recovery at this stage.
253 asmlinkage
void __naked
cci_enable_port_for_self(void)
257 " mrc p15, 0, r0, c0, c0, 5 @ get MPIDR value \n"
258 " and r0, r0, #"__stringify(MPIDR_HWID_BITMASK
)" \n"
261 " add r1, r1, r2 @ &cpu_port \n"
262 " add ip, r1, %[sizeof_cpu_port] \n"
264 /* Loop over the cpu_port array looking for a matching MPIDR */
265 "1: ldr r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
266 " cmp r2, r0 @ compare MPIDR \n"
269 /* Found a match, now test port validity */
270 " ldr r3, [r1, %[offsetof_cpu_port_port]] \n"
271 " tst r3, #"__stringify(PORT_VALID
)" \n"
274 /* no match, loop with the next cpu_port entry */
275 "2: add r1, r1, %[sizeof_struct_cpu_port] \n"
276 " cmp r1, ip @ done? \n"
279 /* CCI port not found -- cheaply try to stall this CPU */
280 "cci_port_not_found: \n"
283 " b cci_port_not_found \n"
285 /* Use matched port index to look up the corresponding ports entry */
286 "3: bic r3, r3, #"__stringify(PORT_VALID
)" \n"
288 " ldmia r0, {r1, r2} \n"
289 " sub r1, r1, r0 @ virt - phys \n"
290 " ldr r0, [r0, r2] @ *(&ports) \n"
291 " mov r2, %[sizeof_struct_ace_port] \n"
292 " mla r0, r2, r3, r0 @ &ports[index] \n"
293 " sub r0, r0, r1 @ virt_to_phys() \n"
295 /* Enable the CCI port */
296 " ldr r0, [r0, %[offsetof_port_phys]] \n"
297 " mov r3, #"__stringify(CCI_ENABLE_REQ
)" \n"
298 " str r3, [r0, #"__stringify(CCI_PORT_CTRL
)"] \n"
300 /* poll the status reg for completion */
303 " ldr r0, [r0, r1] @ cci_ctrl_base \n"
304 "4: ldr r1, [r0, #"__stringify(CCI_CTRL_STATUS
)"] \n"
312 "5: .word cpu_port - . \n"
314 " .word ports - 6b \n"
315 "7: .word cci_ctrl_phys - . \n"
317 [sizeof_cpu_port
] "i" (sizeof(cpu_port
)),
319 [offsetof_cpu_port_mpidr_lsb
] "i" (offsetof(struct cpu_port
, mpidr
)),
321 [offsetof_cpu_port_mpidr_lsb
] "i" (offsetof(struct cpu_port
, mpidr
)+4),
323 [offsetof_cpu_port_port
] "i" (offsetof(struct cpu_port
, port
)),
324 [sizeof_struct_cpu_port
] "i" (sizeof(struct cpu_port
)),
325 [sizeof_struct_ace_port
] "i" (sizeof(struct cci_ace_port
)),
326 [offsetof_port_phys
] "i" (offsetof(struct cci_ace_port
, phys
)) );
332 * __cci_control_port_by_device() - function to control a CCI port by device
335 * @dn: device node pointer of the device whose CCI port should be
337 * @enable: if true enables the port, if false disables it
341 * -ENODEV on port look-up failure
343 int notrace
__cci_control_port_by_device(struct device_node
*dn
, bool enable
)
350 port
= __cci_ace_get_port(dn
, ACE_LITE_PORT
);
351 if (WARN_ONCE(port
< 0, "node %s ACE lite port look-up failure\n",
354 cci_port_control(port
, enable
);
357 EXPORT_SYMBOL_GPL(__cci_control_port_by_device
);
360 * __cci_control_port_by_index() - function to control a CCI port by port index
362 * @port: port index previously retrieved with cci_ace_get_port()
363 * @enable: if true enables the port, if false disables it
367 * -ENODEV on port index out of range
368 * -EPERM if operation carried out on an ACE PORT
370 int notrace
__cci_control_port_by_index(u32 port
, bool enable
)
372 if (port
>= nb_cci_ports
|| ports
[port
].type
== ACE_INVALID_PORT
)
375 * CCI control for ports connected to CPUS is extremely fragile
376 * and must be made to go through a specific and controlled
377 * interface (ie cci_disable_port_by_cpu(); control by general purpose
378 * indexing is therefore disabled for ACE ports.
380 if (ports
[port
].type
== ACE_PORT
)
383 cci_port_control(port
, enable
);
386 EXPORT_SYMBOL_GPL(__cci_control_port_by_index
);
388 static const struct cci_nb_ports cci400_ports
= {
393 static const struct of_device_id arm_cci_matches
[] = {
394 {.compatible
= "arm,cci-400", .data
= &cci400_ports
},
398 static const struct of_device_id arm_cci_ctrl_if_matches
[] = {
399 {.compatible
= "arm,cci-400-ctrl-if", },
403 static int __init
cci_probe(void)
405 struct cci_nb_ports
const *cci_config
;
406 int ret
, i
, nb_ace
= 0, nb_ace_lite
= 0;
407 struct device_node
*np
, *cp
;
409 const char *match_str
;
412 np
= of_find_matching_node(NULL
, arm_cci_matches
);
416 cci_config
= of_match_node(arm_cci_matches
, np
)->data
;
420 nb_cci_ports
= cci_config
->nb_ace
+ cci_config
->nb_ace_lite
;
422 ports
= kcalloc(sizeof(*ports
), nb_cci_ports
, GFP_KERNEL
);
426 ret
= of_address_to_resource(np
, 0, &res
);
428 cci_ctrl_base
= ioremap(res
.start
, resource_size(&res
));
429 cci_ctrl_phys
= res
.start
;
431 if (ret
|| !cci_ctrl_base
) {
432 WARN(1, "unable to ioremap CCI ctrl\n");
437 for_each_child_of_node(np
, cp
) {
438 if (!of_match_node(arm_cci_ctrl_if_matches
, cp
))
441 i
= nb_ace
+ nb_ace_lite
;
443 if (i
>= nb_cci_ports
)
446 if (of_property_read_string(cp
, "interface-type",
448 WARN(1, "node %s missing interface-type property\n",
452 is_ace
= strcmp(match_str
, "ace") == 0;
453 if (!is_ace
&& strcmp(match_str
, "ace-lite")) {
454 WARN(1, "node %s containing invalid interface-type property, skipping it\n",
459 ret
= of_address_to_resource(cp
, 0, &res
);
461 ports
[i
].base
= ioremap(res
.start
, resource_size(&res
));
462 ports
[i
].phys
= res
.start
;
464 if (ret
|| !ports
[i
].base
) {
465 WARN(1, "unable to ioremap CCI port %d\n", i
);
470 if (WARN_ON(nb_ace
>= cci_config
->nb_ace
))
472 ports
[i
].type
= ACE_PORT
;
475 if (WARN_ON(nb_ace_lite
>= cci_config
->nb_ace_lite
))
477 ports
[i
].type
= ACE_LITE_PORT
;
483 /* initialize a stashed array of ACE ports to speed-up look-up */
484 cci_ace_init_ports();
487 * Multi-cluster systems may need this data when non-coherent, during
488 * cluster power-up/power-down. Make sure it reaches main memory.
490 sync_cache_w(&cci_ctrl_base
);
491 sync_cache_w(&cci_ctrl_phys
);
492 sync_cache_w(&ports
);
493 sync_cache_w(&cpu_port
);
494 __sync_cache_range_w(ports
, sizeof(*ports
) * nb_cci_ports
);
495 pr_info("ARM CCI driver probed\n");
504 static int cci_init_status
= -EAGAIN
;
505 static DEFINE_MUTEX(cci_probing
);
507 static int __init
cci_init(void)
509 if (cci_init_status
!= -EAGAIN
)
510 return cci_init_status
;
512 mutex_lock(&cci_probing
);
513 if (cci_init_status
== -EAGAIN
)
514 cci_init_status
= cci_probe();
515 mutex_unlock(&cci_probing
);
516 return cci_init_status
;
520 * To sort out early init calls ordering a helper function is provided to
521 * check if the CCI driver has beed initialized. Function check if the driver
522 * has been initialized, if not it calls the init function that probes
523 * the driver and updates the return value.
525 bool __init
cci_probed(void)
527 return cci_init() == 0;
529 EXPORT_SYMBOL_GPL(cci_probed
);
531 early_initcall(cci_init
);
532 MODULE_LICENSE("GPL");
533 MODULE_DESCRIPTION("ARM CCI support");