1 /* Copyright 2008 - 2016 Freescale Semiconductor, Inc.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5  *     * Redistributions of source code must retain the above copyright
6  *	 notice, this list of conditions and the following disclaimer.
7  *     * Redistributions in binary form must reproduce the above copyright
8  *	 notice, this list of conditions and the following disclaimer in the
9  *	 documentation and/or other materials provided with the distribution.
10  *     * Neither the name of Freescale Semiconductor nor the
11  *	 names of its contributors may be used to endorse or promote products
12  *	 derived from this software without specific prior written permission.
13  *
14  * ALTERNATIVELY, this software may be distributed under the terms of the
15  * GNU General Public License ("GPL") as published by the Free Software
16  * Foundation, either version 2 of that License or (at your option) any
17  * later version.
18  *
19  * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "bman_priv.h"
32 
33 static struct bman_portal *affine_bportals[NR_CPUS];
34 static struct cpumask portal_cpus;
35 /* protect bman global registers and global data shared among portals */
36 static DEFINE_SPINLOCK(bman_lock);
37 
38 static struct bman_portal *init_pcfg(struct bm_portal_config *pcfg)
39 {
40 	struct bman_portal *p = bman_create_affine_portal(pcfg);
41 
42 	if (!p) {
43 		dev_crit(pcfg->dev, "%s: Portal failure on cpu %d\n",
44 			 __func__, pcfg->cpu);
45 		return NULL;
46 	}
47 
48 	bman_p_irqsource_add(p, BM_PIRQ_RCRI);
49 	affine_bportals[pcfg->cpu] = p;
50 
51 	dev_info(pcfg->dev, "Portal initialised, cpu %d\n", pcfg->cpu);
52 
53 	return p;
54 }
55 
56 static int bman_offline_cpu(unsigned int cpu)
57 {
58 	struct bman_portal *p = affine_bportals[cpu];
59 	const struct bm_portal_config *pcfg;
60 
61 	if (!p)
62 		return 0;
63 
64 	pcfg = bman_get_bm_portal_config(p);
65 	if (!pcfg)
66 		return 0;
67 
68 	irq_set_affinity(pcfg->irq, cpumask_of(0));
69 	return 0;
70 }
71 
72 static int bman_online_cpu(unsigned int cpu)
73 {
74 	struct bman_portal *p = affine_bportals[cpu];
75 	const struct bm_portal_config *pcfg;
76 
77 	if (!p)
78 		return 0;
79 
80 	pcfg = bman_get_bm_portal_config(p);
81 	if (!pcfg)
82 		return 0;
83 
84 	irq_set_affinity(pcfg->irq, cpumask_of(cpu));
85 	return 0;
86 }
87 
88 static int bman_portal_probe(struct platform_device *pdev)
89 {
90 	struct device *dev = &pdev->dev;
91 	struct device_node *node = dev->of_node;
92 	struct bm_portal_config *pcfg;
93 	struct resource *addr_phys[2];
94 	void __iomem *va;
95 	int irq, cpu;
96 
97 	pcfg = devm_kmalloc(dev, sizeof(*pcfg), GFP_KERNEL);
98 	if (!pcfg)
99 		return -ENOMEM;
100 
101 	pcfg->dev = dev;
102 
103 	addr_phys[0] = platform_get_resource(pdev, IORESOURCE_MEM,
104 					     DPAA_PORTAL_CE);
105 	if (!addr_phys[0]) {
106 		dev_err(dev, "Can't get %s property 'reg::CE'\n",
107 			node->full_name);
108 		return -ENXIO;
109 	}
110 
111 	addr_phys[1] = platform_get_resource(pdev, IORESOURCE_MEM,
112 					     DPAA_PORTAL_CI);
113 	if (!addr_phys[1]) {
114 		dev_err(dev, "Can't get %s property 'reg::CI'\n",
115 			node->full_name);
116 		return -ENXIO;
117 	}
118 
119 	pcfg->cpu = -1;
120 
121 	irq = platform_get_irq(pdev, 0);
122 	if (irq <= 0) {
123 		dev_err(dev, "Can't get %s IRQ'\n", node->full_name);
124 		return -ENXIO;
125 	}
126 	pcfg->irq = irq;
127 
128 	va = ioremap_prot(addr_phys[0]->start, resource_size(addr_phys[0]), 0);
129 	if (!va) {
130 		dev_err(dev, "ioremap::CE failed\n");
131 		goto err_ioremap1;
132 	}
133 
134 	pcfg->addr_virt[DPAA_PORTAL_CE] = va;
135 
136 	va = ioremap_prot(addr_phys[1]->start, resource_size(addr_phys[1]),
137 			  _PAGE_GUARDED | _PAGE_NO_CACHE);
138 	if (!va) {
139 		dev_err(dev, "ioremap::CI failed\n");
140 		goto err_ioremap2;
141 	}
142 
143 	pcfg->addr_virt[DPAA_PORTAL_CI] = va;
144 
145 	spin_lock(&bman_lock);
146 	cpu = cpumask_next_zero(-1, &portal_cpus);
147 	if (cpu >= nr_cpu_ids) {
148 		/* unassigned portal, skip init */
149 		spin_unlock(&bman_lock);
150 		return 0;
151 	}
152 
153 	cpumask_set_cpu(cpu, &portal_cpus);
154 	spin_unlock(&bman_lock);
155 	pcfg->cpu = cpu;
156 
157 	if (!init_pcfg(pcfg)) {
158 		dev_err(dev, "portal init failed\n");
159 		goto err_portal_init;
160 	}
161 
162 	/* clear irq affinity if assigned cpu is offline */
163 	if (!cpu_online(cpu))
164 		bman_offline_cpu(cpu);
165 
166 	return 0;
167 
168 err_portal_init:
169 	iounmap(pcfg->addr_virt[DPAA_PORTAL_CI]);
170 err_ioremap2:
171 	iounmap(pcfg->addr_virt[DPAA_PORTAL_CE]);
172 err_ioremap1:
173 	return -ENXIO;
174 }
175 
176 static const struct of_device_id bman_portal_ids[] = {
177 	{
178 		.compatible = "fsl,bman-portal",
179 	},
180 	{}
181 };
182 MODULE_DEVICE_TABLE(of, bman_portal_ids);
183 
184 static struct platform_driver bman_portal_driver = {
185 	.driver = {
186 		.name = KBUILD_MODNAME,
187 		.of_match_table = bman_portal_ids,
188 	},
189 	.probe = bman_portal_probe,
190 };
191 
192 static int __init bman_portal_driver_register(struct platform_driver *drv)
193 {
194 	int ret;
195 
196 	ret = platform_driver_register(drv);
197 	if (ret < 0)
198 		return ret;
199 
200 	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
201 					"soc/qbman_portal:online",
202 					bman_online_cpu, bman_offline_cpu);
203 	if (ret < 0) {
204 		pr_err("bman: failed to register hotplug callbacks.\n");
205 		platform_driver_unregister(drv);
206 		return ret;
207 	}
208 	return 0;
209 }
210 
211 module_driver(bman_portal_driver,
212 	      bman_portal_driver_register, platform_driver_unregister);
213