xref: /linux/drivers/bus/arm-cci.c (revision 2d7eaeb05ae265bd64270376e6a2c0d80244771f)
1 /*
2  * CCI cache coherent interconnect driver
3  *
4  * Copyright (C) 2013 ARM Ltd.
5  * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
6  *
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.
10  *
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.
15  */
16 
17 #include <linux/arm-cci.h>
18 #include <linux/io.h>
19 #include <linux/module.h>
20 #include <linux/of_address.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 
25 #include <asm/cacheflush.h>
26 #include <asm/smp_plat.h>
27 
28 static void __iomem *cci_ctrl_base __ro_after_init;
29 static unsigned long cci_ctrl_phys __ro_after_init;
30 
31 #ifdef CONFIG_ARM_CCI400_PORT_CTRL
32 struct cci_nb_ports {
33 	unsigned int nb_ace;
34 	unsigned int nb_ace_lite;
35 };
36 
37 static const struct cci_nb_ports cci400_ports = {
38 	.nb_ace = 2,
39 	.nb_ace_lite = 3
40 };
41 
42 #define CCI400_PORTS_DATA	(&cci400_ports)
43 #else
44 #define CCI400_PORTS_DATA	(NULL)
45 #endif
46 
47 static const struct of_device_id arm_cci_matches[] = {
48 #ifdef CONFIG_ARM_CCI400_COMMON
49 	{.compatible = "arm,cci-400", .data = CCI400_PORTS_DATA },
50 #endif
51 #ifdef CONFIG_ARM_CCI5xx_PMU
52 	{ .compatible = "arm,cci-500", },
53 	{ .compatible = "arm,cci-550", },
54 #endif
55 	{},
56 };
57 
58 static const struct of_dev_auxdata arm_cci_auxdata[] = {
59 	OF_DEV_AUXDATA("arm,cci-400-pmu", 0, NULL, &cci_ctrl_base),
60 	OF_DEV_AUXDATA("arm,cci-400-pmu,r0", 0, NULL, &cci_ctrl_base),
61 	OF_DEV_AUXDATA("arm,cci-400-pmu,r1", 0, NULL, &cci_ctrl_base),
62 	OF_DEV_AUXDATA("arm,cci-500-pmu,r0", 0, NULL, &cci_ctrl_base),
63 	OF_DEV_AUXDATA("arm,cci-550-pmu,r0", 0, NULL, &cci_ctrl_base),
64 	{}
65 };
66 
67 #define DRIVER_NAME		"ARM-CCI"
68 
69 static int cci_platform_probe(struct platform_device *pdev)
70 {
71 	if (!cci_probed())
72 		return -ENODEV;
73 
74 	return of_platform_populate(pdev->dev.of_node, NULL,
75 				    arm_cci_auxdata, &pdev->dev);
76 }
77 
78 static struct platform_driver cci_platform_driver = {
79 	.driver = {
80 		   .name = DRIVER_NAME,
81 		   .of_match_table = arm_cci_matches,
82 		  },
83 	.probe = cci_platform_probe,
84 };
85 
86 static int __init cci_platform_init(void)
87 {
88 	return platform_driver_register(&cci_platform_driver);
89 }
90 
91 #ifdef CONFIG_ARM_CCI400_PORT_CTRL
92 
93 #define CCI_PORT_CTRL		0x0
94 #define CCI_CTRL_STATUS		0xc
95 
96 #define CCI_ENABLE_SNOOP_REQ	0x1
97 #define CCI_ENABLE_DVM_REQ	0x2
98 #define CCI_ENABLE_REQ		(CCI_ENABLE_SNOOP_REQ | CCI_ENABLE_DVM_REQ)
99 
100 enum cci_ace_port_type {
101 	ACE_INVALID_PORT = 0x0,
102 	ACE_PORT,
103 	ACE_LITE_PORT,
104 };
105 
106 struct cci_ace_port {
107 	void __iomem *base;
108 	unsigned long phys;
109 	enum cci_ace_port_type type;
110 	struct device_node *dn;
111 };
112 
113 static struct cci_ace_port *ports;
114 static unsigned int nb_cci_ports;
115 
116 struct cpu_port {
117 	u64 mpidr;
118 	u32 port;
119 };
120 
121 /*
122  * Use the port MSB as valid flag, shift can be made dynamic
123  * by computing number of bits required for port indexes.
124  * Code disabling CCI cpu ports runs with D-cache invalidated
125  * and SCTLR bit clear so data accesses must be kept to a minimum
126  * to improve performance; for now shift is left static to
127  * avoid one more data access while disabling the CCI port.
128  */
129 #define PORT_VALID_SHIFT	31
130 #define PORT_VALID		(0x1 << PORT_VALID_SHIFT)
131 
132 static inline void init_cpu_port(struct cpu_port *port, u32 index, u64 mpidr)
133 {
134 	port->port = PORT_VALID | index;
135 	port->mpidr = mpidr;
136 }
137 
138 static inline bool cpu_port_is_valid(struct cpu_port *port)
139 {
140 	return !!(port->port & PORT_VALID);
141 }
142 
143 static inline bool cpu_port_match(struct cpu_port *port, u64 mpidr)
144 {
145 	return port->mpidr == (mpidr & MPIDR_HWID_BITMASK);
146 }
147 
148 static struct cpu_port cpu_port[NR_CPUS];
149 
150 /**
151  * __cci_ace_get_port - Function to retrieve the port index connected to
152  *			a cpu or device.
153  *
154  * @dn: device node of the device to look-up
155  * @type: port type
156  *
157  * Return value:
158  *	- CCI port index if success
159  *	- -ENODEV if failure
160  */
161 static int __cci_ace_get_port(struct device_node *dn, int type)
162 {
163 	int i;
164 	bool ace_match;
165 	struct device_node *cci_portn;
166 
167 	cci_portn = of_parse_phandle(dn, "cci-control-port", 0);
168 	for (i = 0; i < nb_cci_ports; i++) {
169 		ace_match = ports[i].type == type;
170 		if (ace_match && cci_portn == ports[i].dn) {
171 			of_node_put(cci_portn);
172 			return i;
173 		}
174 	}
175 	of_node_put(cci_portn);
176 	return -ENODEV;
177 }
178 
179 int cci_ace_get_port(struct device_node *dn)
180 {
181 	return __cci_ace_get_port(dn, ACE_LITE_PORT);
182 }
183 EXPORT_SYMBOL_GPL(cci_ace_get_port);
184 
185 static void cci_ace_init_ports(void)
186 {
187 	int port, cpu;
188 	struct device_node *cpun;
189 
190 	/*
191 	 * Port index look-up speeds up the function disabling ports by CPU,
192 	 * since the logical to port index mapping is done once and does
193 	 * not change after system boot.
194 	 * The stashed index array is initialized for all possible CPUs
195 	 * at probe time.
196 	 */
197 	for_each_possible_cpu(cpu) {
198 		/* too early to use cpu->of_node */
199 		cpun = of_get_cpu_node(cpu, NULL);
200 
201 		if (WARN(!cpun, "Missing cpu device node\n"))
202 			continue;
203 
204 		port = __cci_ace_get_port(cpun, ACE_PORT);
205 		of_node_put(cpun);
206 		if (port < 0)
207 			continue;
208 
209 		init_cpu_port(&cpu_port[cpu], port, cpu_logical_map(cpu));
210 	}
211 
212 	for_each_possible_cpu(cpu) {
213 		WARN(!cpu_port_is_valid(&cpu_port[cpu]),
214 			"CPU %u does not have an associated CCI port\n",
215 			cpu);
216 	}
217 }
218 /*
219  * Functions to enable/disable a CCI interconnect slave port
220  *
221  * They are called by low-level power management code to disable slave
222  * interfaces snoops and DVM broadcast.
223  * Since they may execute with cache data allocation disabled and
224  * after the caches have been cleaned and invalidated the functions provide
225  * no explicit locking since they may run with D-cache disabled, so normal
226  * cacheable kernel locks based on ldrex/strex may not work.
227  * Locking has to be provided by BSP implementations to ensure proper
228  * operations.
229  */
230 
231 /**
232  * cci_port_control() - function to control a CCI port
233  *
234  * @port: index of the port to setup
235  * @enable: if true enables the port, if false disables it
236  */
237 static void notrace cci_port_control(unsigned int port, bool enable)
238 {
239 	void __iomem *base = ports[port].base;
240 
241 	writel_relaxed(enable ? CCI_ENABLE_REQ : 0, base + CCI_PORT_CTRL);
242 	/*
243 	 * This function is called from power down procedures
244 	 * and must not execute any instruction that might
245 	 * cause the processor to be put in a quiescent state
246 	 * (eg wfi). Hence, cpu_relax() can not be added to this
247 	 * read loop to optimize power, since it might hide possibly
248 	 * disruptive operations.
249 	 */
250 	while (readl_relaxed(cci_ctrl_base + CCI_CTRL_STATUS) & 0x1)
251 			;
252 }
253 
254 /**
255  * cci_disable_port_by_cpu() - function to disable a CCI port by CPU
256  *			       reference
257  *
258  * @mpidr: mpidr of the CPU whose CCI port should be disabled
259  *
260  * Disabling a CCI port for a CPU implies disabling the CCI port
261  * controlling that CPU cluster. Code disabling CPU CCI ports
262  * must make sure that the CPU running the code is the last active CPU
263  * in the cluster ie all other CPUs are quiescent in a low power state.
264  *
265  * Return:
266  *	0 on success
267  *	-ENODEV on port look-up failure
268  */
269 int notrace cci_disable_port_by_cpu(u64 mpidr)
270 {
271 	int cpu;
272 	bool is_valid;
273 	for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
274 		is_valid = cpu_port_is_valid(&cpu_port[cpu]);
275 		if (is_valid && cpu_port_match(&cpu_port[cpu], mpidr)) {
276 			cci_port_control(cpu_port[cpu].port, false);
277 			return 0;
278 		}
279 	}
280 	return -ENODEV;
281 }
282 EXPORT_SYMBOL_GPL(cci_disable_port_by_cpu);
283 
284 /**
285  * cci_enable_port_for_self() - enable a CCI port for calling CPU
286  *
287  * Enabling a CCI port for the calling CPU implies enabling the CCI
288  * port controlling that CPU's cluster. Caller must make sure that the
289  * CPU running the code is the first active CPU in the cluster and all
290  * other CPUs are quiescent in a low power state  or waiting for this CPU
291  * to complete the CCI initialization.
292  *
293  * Because this is called when the MMU is still off and with no stack,
294  * the code must be position independent and ideally rely on callee
295  * clobbered registers only.  To achieve this we must code this function
296  * entirely in assembler.
297  *
298  * On success this returns with the proper CCI port enabled.  In case of
299  * any failure this never returns as the inability to enable the CCI is
300  * fatal and there is no possible recovery at this stage.
301  */
302 asmlinkage void __naked cci_enable_port_for_self(void)
303 {
304 	asm volatile ("\n"
305 "	.arch armv7-a\n"
306 "	mrc	p15, 0, r0, c0, c0, 5	@ get MPIDR value \n"
307 "	and	r0, r0, #"__stringify(MPIDR_HWID_BITMASK)" \n"
308 "	adr	r1, 5f \n"
309 "	ldr	r2, [r1] \n"
310 "	add	r1, r1, r2		@ &cpu_port \n"
311 "	add	ip, r1, %[sizeof_cpu_port] \n"
312 
313 	/* Loop over the cpu_port array looking for a matching MPIDR */
314 "1:	ldr	r2, [r1, %[offsetof_cpu_port_mpidr_lsb]] \n"
315 "	cmp	r2, r0 			@ compare MPIDR \n"
316 "	bne	2f \n"
317 
318 	/* Found a match, now test port validity */
319 "	ldr	r3, [r1, %[offsetof_cpu_port_port]] \n"
320 "	tst	r3, #"__stringify(PORT_VALID)" \n"
321 "	bne	3f \n"
322 
323 	/* no match, loop with the next cpu_port entry */
324 "2:	add	r1, r1, %[sizeof_struct_cpu_port] \n"
325 "	cmp	r1, ip			@ done? \n"
326 "	blo	1b \n"
327 
328 	/* CCI port not found -- cheaply try to stall this CPU */
329 "cci_port_not_found: \n"
330 "	wfi \n"
331 "	wfe \n"
332 "	b	cci_port_not_found \n"
333 
334 	/* Use matched port index to look up the corresponding ports entry */
335 "3:	bic	r3, r3, #"__stringify(PORT_VALID)" \n"
336 "	adr	r0, 6f \n"
337 "	ldmia	r0, {r1, r2} \n"
338 "	sub	r1, r1, r0 		@ virt - phys \n"
339 "	ldr	r0, [r0, r2] 		@ *(&ports) \n"
340 "	mov	r2, %[sizeof_struct_ace_port] \n"
341 "	mla	r0, r2, r3, r0		@ &ports[index] \n"
342 "	sub	r0, r0, r1		@ virt_to_phys() \n"
343 
344 	/* Enable the CCI port */
345 "	ldr	r0, [r0, %[offsetof_port_phys]] \n"
346 "	mov	r3, %[cci_enable_req]\n"
347 "	str	r3, [r0, #"__stringify(CCI_PORT_CTRL)"] \n"
348 
349 	/* poll the status reg for completion */
350 "	adr	r1, 7f \n"
351 "	ldr	r0, [r1] \n"
352 "	ldr	r0, [r0, r1]		@ cci_ctrl_base \n"
353 "4:	ldr	r1, [r0, #"__stringify(CCI_CTRL_STATUS)"] \n"
354 "	tst	r1, %[cci_control_status_bits] \n"
355 "	bne	4b \n"
356 
357 "	mov	r0, #0 \n"
358 "	bx	lr \n"
359 
360 "	.align	2 \n"
361 "5:	.word	cpu_port - . \n"
362 "6:	.word	. \n"
363 "	.word	ports - 6b \n"
364 "7:	.word	cci_ctrl_phys - . \n"
365 	: :
366 	[sizeof_cpu_port] "i" (sizeof(cpu_port)),
367 	[cci_enable_req] "i" cpu_to_le32(CCI_ENABLE_REQ),
368 	[cci_control_status_bits] "i" cpu_to_le32(1),
369 #ifndef __ARMEB__
370 	[offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)),
371 #else
372 	[offsetof_cpu_port_mpidr_lsb] "i" (offsetof(struct cpu_port, mpidr)+4),
373 #endif
374 	[offsetof_cpu_port_port] "i" (offsetof(struct cpu_port, port)),
375 	[sizeof_struct_cpu_port] "i" (sizeof(struct cpu_port)),
376 	[sizeof_struct_ace_port] "i" (sizeof(struct cci_ace_port)),
377 	[offsetof_port_phys] "i" (offsetof(struct cci_ace_port, phys)) );
378 }
379 
380 /**
381  * __cci_control_port_by_device() - function to control a CCI port by device
382  *				    reference
383  *
384  * @dn: device node pointer of the device whose CCI port should be
385  *      controlled
386  * @enable: if true enables the port, if false disables it
387  *
388  * Return:
389  *	0 on success
390  *	-ENODEV on port look-up failure
391  */
392 int notrace __cci_control_port_by_device(struct device_node *dn, bool enable)
393 {
394 	int port;
395 
396 	if (!dn)
397 		return -ENODEV;
398 
399 	port = __cci_ace_get_port(dn, ACE_LITE_PORT);
400 	if (WARN_ONCE(port < 0, "node %pOF ACE lite port look-up failure\n",
401 				dn))
402 		return -ENODEV;
403 	cci_port_control(port, enable);
404 	return 0;
405 }
406 EXPORT_SYMBOL_GPL(__cci_control_port_by_device);
407 
408 /**
409  * __cci_control_port_by_index() - function to control a CCI port by port index
410  *
411  * @port: port index previously retrieved with cci_ace_get_port()
412  * @enable: if true enables the port, if false disables it
413  *
414  * Return:
415  *	0 on success
416  *	-ENODEV on port index out of range
417  *	-EPERM if operation carried out on an ACE PORT
418  */
419 int notrace __cci_control_port_by_index(u32 port, bool enable)
420 {
421 	if (port >= nb_cci_ports || ports[port].type == ACE_INVALID_PORT)
422 		return -ENODEV;
423 	/*
424 	 * CCI control for ports connected to CPUS is extremely fragile
425 	 * and must be made to go through a specific and controlled
426 	 * interface (ie cci_disable_port_by_cpu(); control by general purpose
427 	 * indexing is therefore disabled for ACE ports.
428 	 */
429 	if (ports[port].type == ACE_PORT)
430 		return -EPERM;
431 
432 	cci_port_control(port, enable);
433 	return 0;
434 }
435 EXPORT_SYMBOL_GPL(__cci_control_port_by_index);
436 
437 static const struct of_device_id arm_cci_ctrl_if_matches[] = {
438 	{.compatible = "arm,cci-400-ctrl-if", },
439 	{},
440 };
441 
442 static int cci_probe_ports(struct device_node *np)
443 {
444 	struct cci_nb_ports const *cci_config;
445 	int ret, i, nb_ace = 0, nb_ace_lite = 0;
446 	struct device_node *cp;
447 	struct resource res;
448 	const char *match_str;
449 	bool is_ace;
450 
451 
452 	cci_config = of_match_node(arm_cci_matches, np)->data;
453 	if (!cci_config)
454 		return -ENODEV;
455 
456 	nb_cci_ports = cci_config->nb_ace + cci_config->nb_ace_lite;
457 
458 	ports = kzalloc_objs(*ports, nb_cci_ports);
459 	if (!ports)
460 		return -ENOMEM;
461 
462 	for_each_available_child_of_node(np, cp) {
463 		if (!of_match_node(arm_cci_ctrl_if_matches, cp))
464 			continue;
465 
466 		i = nb_ace + nb_ace_lite;
467 
468 		if (i >= nb_cci_ports)
469 			break;
470 
471 		if (of_property_read_string(cp, "interface-type",
472 					&match_str)) {
473 			WARN(1, "node %pOF missing interface-type property\n",
474 				  cp);
475 			continue;
476 		}
477 		is_ace = strcmp(match_str, "ace") == 0;
478 		if (!is_ace && strcmp(match_str, "ace-lite")) {
479 			WARN(1, "node %pOF containing invalid interface-type property, skipping it\n",
480 					cp);
481 			continue;
482 		}
483 
484 		ret = of_address_to_resource(cp, 0, &res);
485 		if (!ret) {
486 			ports[i].base = ioremap(res.start, resource_size(&res));
487 			ports[i].phys = res.start;
488 		}
489 		if (ret || !ports[i].base) {
490 			WARN(1, "unable to ioremap CCI port %d\n", i);
491 			continue;
492 		}
493 
494 		if (is_ace) {
495 			if (WARN_ON(nb_ace >= cci_config->nb_ace))
496 				continue;
497 			ports[i].type = ACE_PORT;
498 			++nb_ace;
499 		} else {
500 			if (WARN_ON(nb_ace_lite >= cci_config->nb_ace_lite))
501 				continue;
502 			ports[i].type = ACE_LITE_PORT;
503 			++nb_ace_lite;
504 		}
505 		ports[i].dn = cp;
506 	}
507 
508 	/*
509 	 * If there is no CCI port that is under kernel control
510 	 * return early and report probe status.
511 	 */
512 	if (!nb_ace && !nb_ace_lite)
513 		return -ENODEV;
514 
515 	 /* initialize a stashed array of ACE ports to speed-up look-up */
516 	cci_ace_init_ports();
517 
518 	/*
519 	 * Multi-cluster systems may need this data when non-coherent, during
520 	 * cluster power-up/power-down. Make sure it reaches main memory.
521 	 */
522 	sync_cache_w(&cci_ctrl_base);
523 	sync_cache_w(&cci_ctrl_phys);
524 	sync_cache_w(&ports);
525 	sync_cache_w(&cpu_port);
526 	__sync_cache_range_w(ports, sizeof(*ports) * nb_cci_ports);
527 	pr_info("ARM CCI driver probed\n");
528 
529 	return 0;
530 }
531 #else /* !CONFIG_ARM_CCI400_PORT_CTRL */
532 static inline int cci_probe_ports(struct device_node *np)
533 {
534 	return 0;
535 }
536 #endif /* CONFIG_ARM_CCI400_PORT_CTRL */
537 
538 static int cci_probe(void)
539 {
540 	int ret;
541 	struct device_node *np;
542 	struct resource res;
543 
544 	np = of_find_matching_node(NULL, arm_cci_matches);
545 	if (!of_device_is_available(np))
546 		return -ENODEV;
547 
548 	ret = of_address_to_resource(np, 0, &res);
549 	if (!ret) {
550 		cci_ctrl_base = ioremap(res.start, resource_size(&res));
551 		cci_ctrl_phys =	res.start;
552 	}
553 	if (ret || !cci_ctrl_base) {
554 		WARN(1, "unable to ioremap CCI ctrl\n");
555 		return -ENXIO;
556 	}
557 
558 	return cci_probe_ports(np);
559 }
560 
561 static int cci_init_status = -EAGAIN;
562 static DEFINE_MUTEX(cci_probing);
563 
564 static int cci_init(void)
565 {
566 	if (cci_init_status != -EAGAIN)
567 		return cci_init_status;
568 
569 	mutex_lock(&cci_probing);
570 	if (cci_init_status == -EAGAIN)
571 		cci_init_status = cci_probe();
572 	mutex_unlock(&cci_probing);
573 	return cci_init_status;
574 }
575 
576 /*
577  * To sort out early init calls ordering a helper function is provided to
578  * check if the CCI driver has beed initialized. Function check if the driver
579  * has been initialized, if not it calls the init function that probes
580  * the driver and updates the return value.
581  */
582 bool cci_probed(void)
583 {
584 	return cci_init() == 0;
585 }
586 EXPORT_SYMBOL_GPL(cci_probed);
587 
588 early_initcall(cci_init);
589 core_initcall(cci_platform_init);
590 MODULE_LICENSE("GPL");
591 MODULE_DESCRIPTION("ARM CCI support");
592