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