Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[cascardo/linux.git] / drivers / soc / fsl / qbman / bman_portal.c
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 void 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;
63
64         pcfg = bman_get_bm_portal_config(p);
65         if (!pcfg)
66                 return;
67
68         irq_set_affinity(pcfg->irq, cpumask_of(0));
69 }
70
71 static void bman_online_cpu(unsigned int cpu)
72 {
73         struct bman_portal *p = affine_bportals[cpu];
74         const struct bm_portal_config *pcfg;
75
76         if (!p)
77                 return;
78
79         pcfg = bman_get_bm_portal_config(p);
80         if (!pcfg)
81                 return;
82
83         irq_set_affinity(pcfg->irq, cpumask_of(cpu));
84 }
85
86 static int bman_hotplug_cpu_callback(struct notifier_block *nfb,
87                                      unsigned long action, void *hcpu)
88 {
89         unsigned int cpu = (unsigned long)hcpu;
90
91         switch (action) {
92         case CPU_ONLINE:
93         case CPU_ONLINE_FROZEN:
94                 bman_online_cpu(cpu);
95                 break;
96         case CPU_DOWN_PREPARE:
97         case CPU_DOWN_PREPARE_FROZEN:
98                 bman_offline_cpu(cpu);
99         }
100
101         return NOTIFY_OK;
102 }
103
104 static struct notifier_block bman_hotplug_cpu_notifier = {
105         .notifier_call = bman_hotplug_cpu_callback,
106 };
107
108 static int bman_portal_probe(struct platform_device *pdev)
109 {
110         struct device *dev = &pdev->dev;
111         struct device_node *node = dev->of_node;
112         struct bm_portal_config *pcfg;
113         struct resource *addr_phys[2];
114         void __iomem *va;
115         int irq, cpu;
116
117         pcfg = devm_kmalloc(dev, sizeof(*pcfg), GFP_KERNEL);
118         if (!pcfg)
119                 return -ENOMEM;
120
121         pcfg->dev = dev;
122
123         addr_phys[0] = platform_get_resource(pdev, IORESOURCE_MEM,
124                                              DPAA_PORTAL_CE);
125         if (!addr_phys[0]) {
126                 dev_err(dev, "Can't get %s property 'reg::CE'\n",
127                         node->full_name);
128                 return -ENXIO;
129         }
130
131         addr_phys[1] = platform_get_resource(pdev, IORESOURCE_MEM,
132                                              DPAA_PORTAL_CI);
133         if (!addr_phys[1]) {
134                 dev_err(dev, "Can't get %s property 'reg::CI'\n",
135                         node->full_name);
136                 return -ENXIO;
137         }
138
139         pcfg->cpu = -1;
140
141         irq = platform_get_irq(pdev, 0);
142         if (irq <= 0) {
143                 dev_err(dev, "Can't get %s IRQ'\n", node->full_name);
144                 return -ENXIO;
145         }
146         pcfg->irq = irq;
147
148         va = ioremap_prot(addr_phys[0]->start, resource_size(addr_phys[0]), 0);
149         if (!va)
150                 goto err_ioremap1;
151
152         pcfg->addr_virt[DPAA_PORTAL_CE] = va;
153
154         va = ioremap_prot(addr_phys[1]->start, resource_size(addr_phys[1]),
155                           _PAGE_GUARDED | _PAGE_NO_CACHE);
156         if (!va)
157                 goto err_ioremap2;
158
159         pcfg->addr_virt[DPAA_PORTAL_CI] = va;
160
161         spin_lock(&bman_lock);
162         cpu = cpumask_next_zero(-1, &portal_cpus);
163         if (cpu >= nr_cpu_ids) {
164                 /* unassigned portal, skip init */
165                 spin_unlock(&bman_lock);
166                 return 0;
167         }
168
169         cpumask_set_cpu(cpu, &portal_cpus);
170         spin_unlock(&bman_lock);
171         pcfg->cpu = cpu;
172
173         if (!init_pcfg(pcfg))
174                 goto err_ioremap2;
175
176         /* clear irq affinity if assigned cpu is offline */
177         if (!cpu_online(cpu))
178                 bman_offline_cpu(cpu);
179
180         return 0;
181
182 err_ioremap2:
183         iounmap(pcfg->addr_virt[DPAA_PORTAL_CE]);
184 err_ioremap1:
185         dev_err(dev, "ioremap failed\n");
186         return -ENXIO;
187 }
188
189 static const struct of_device_id bman_portal_ids[] = {
190         {
191                 .compatible = "fsl,bman-portal",
192         },
193         {}
194 };
195 MODULE_DEVICE_TABLE(of, bman_portal_ids);
196
197 static struct platform_driver bman_portal_driver = {
198         .driver = {
199                 .name = KBUILD_MODNAME,
200                 .of_match_table = bman_portal_ids,
201         },
202         .probe = bman_portal_probe,
203 };
204
205 static int __init bman_portal_driver_register(struct platform_driver *drv)
206 {
207         int ret;
208
209         ret = platform_driver_register(drv);
210         if (ret < 0)
211                 return ret;
212
213         register_hotcpu_notifier(&bman_hotplug_cpu_notifier);
214
215         return 0;
216 }
217
218 module_driver(bman_portal_driver,
219               bman_portal_driver_register, platform_driver_unregister);