1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
3 * Copyright 2014-2016 Freescale Semiconductor Inc.
8 #include <linux/types.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/interrupt.h>
13 #include <linux/msi.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/delay.h>
18 #include <linux/fsl/mc.h>
19 #include <soc/fsl/dpaa2-io.h>
21 #include "qbman-portal.h"
25 MODULE_LICENSE("Dual BSD/GPL");
26 MODULE_AUTHOR("Freescale Semiconductor, Inc");
27 MODULE_DESCRIPTION("DPIO Driver");
33 static irqreturn_t dpio_irq_handler(int irq_num, void *arg)
35 struct device *dev = (struct device *)arg;
36 struct dpio_priv *priv = dev_get_drvdata(dev);
38 return dpaa2_io_irq(priv->io);
41 static void unregister_dpio_irq_handlers(struct fsl_mc_device *dpio_dev)
43 struct fsl_mc_device_irq *irq;
45 irq = dpio_dev->irqs[0];
47 /* clear the affinity hint */
48 irq_set_affinity_hint(irq->msi_desc->irq, NULL);
51 static int register_dpio_irq_handlers(struct fsl_mc_device *dpio_dev, int cpu)
53 struct dpio_priv *priv;
55 struct fsl_mc_device_irq *irq;
57 priv = dev_get_drvdata(&dpio_dev->dev);
59 irq = dpio_dev->irqs[0];
60 error = devm_request_irq(&dpio_dev->dev,
64 dev_name(&dpio_dev->dev),
67 dev_err(&dpio_dev->dev,
68 "devm_request_irq() failed: %d\n",
73 /* set the affinity hint */
74 if (irq_set_affinity_hint(irq->msi_desc->irq, cpumask_of(cpu)))
75 dev_err(&dpio_dev->dev,
76 "irq_set_affinity failed irq %d cpu %d\n",
77 irq->msi_desc->irq, cpu);
82 static int dpaa2_dpio_probe(struct fsl_mc_device *dpio_dev)
84 struct dpio_attr dpio_attrs;
85 struct dpaa2_io_desc desc;
86 struct dpio_priv *priv;
88 struct device *dev = &dpio_dev->dev;
89 static int next_cpu = -1;
91 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
95 dev_set_drvdata(dev, priv);
97 err = fsl_mc_portal_allocate(dpio_dev, 0, &dpio_dev->mc_io);
99 dev_dbg(dev, "MC portal allocation failed\n");
104 err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
105 &dpio_dev->mc_handle);
107 dev_err(dev, "dpio_open() failed\n");
111 err = dpio_get_attributes(dpio_dev->mc_io, 0, dpio_dev->mc_handle,
114 dev_err(dev, "dpio_get_attributes() failed %d\n", err);
117 desc.qman_version = dpio_attrs.qbman_version;
119 err = dpio_enable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
121 dev_err(dev, "dpio_enable() failed %d\n", err);
125 /* initialize DPIO descriptor */
126 desc.receives_notifications = dpio_attrs.num_priorities ? 1 : 0;
127 desc.has_8prio = dpio_attrs.num_priorities == 8 ? 1 : 0;
128 desc.dpio_id = dpio_dev->obj_desc.id;
130 /* get the cpu to use for the affinity hint */
132 next_cpu = cpumask_first(cpu_online_mask);
134 next_cpu = cpumask_next(next_cpu, cpu_online_mask);
136 if (!cpu_possible(next_cpu)) {
137 dev_err(dev, "probe failed. Number of DPIOs exceeds NR_CPUS.\n");
139 goto err_allocate_irqs;
144 * Set the CENA regs to be the cache inhibited area of the portal to
145 * avoid coherency issues if a user migrates to another core.
147 desc.regs_cena = devm_memremap(dev, dpio_dev->regions[1].start,
148 resource_size(&dpio_dev->regions[1]),
150 if (IS_ERR(desc.regs_cena)) {
151 dev_err(dev, "devm_memremap failed\n");
152 err = PTR_ERR(desc.regs_cena);
153 goto err_allocate_irqs;
156 desc.regs_cinh = devm_ioremap(dev, dpio_dev->regions[1].start,
157 resource_size(&dpio_dev->regions[1]));
158 if (!desc.regs_cinh) {
160 dev_err(dev, "devm_ioremap failed\n");
161 goto err_allocate_irqs;
164 err = fsl_mc_allocate_irqs(dpio_dev);
166 dev_err(dev, "fsl_mc_allocate_irqs failed. err=%d\n", err);
167 goto err_allocate_irqs;
170 err = register_dpio_irq_handlers(dpio_dev, desc.cpu);
172 goto err_register_dpio_irq;
174 priv->io = dpaa2_io_create(&desc);
176 dev_err(dev, "dpaa2_io_create failed\n");
178 goto err_dpaa2_io_create;
181 dev_info(dev, "probed\n");
182 dev_dbg(dev, " receives_notifications = %d\n",
183 desc.receives_notifications);
184 dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
185 fsl_mc_portal_free(dpio_dev->mc_io);
190 unregister_dpio_irq_handlers(dpio_dev);
191 err_register_dpio_irq:
192 fsl_mc_free_irqs(dpio_dev);
194 dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
196 dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
198 fsl_mc_portal_free(dpio_dev->mc_io);
203 /* Tear down interrupts for a given DPIO object */
204 static void dpio_teardown_irqs(struct fsl_mc_device *dpio_dev)
206 unregister_dpio_irq_handlers(dpio_dev);
207 fsl_mc_free_irqs(dpio_dev);
210 static int dpaa2_dpio_remove(struct fsl_mc_device *dpio_dev)
213 struct dpio_priv *priv;
216 dev = &dpio_dev->dev;
217 priv = dev_get_drvdata(dev);
219 dpaa2_io_down(priv->io);
221 dpio_teardown_irqs(dpio_dev);
223 err = fsl_mc_portal_allocate(dpio_dev, 0, &dpio_dev->mc_io);
225 dev_err(dev, "MC portal allocation failed\n");
229 err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
230 &dpio_dev->mc_handle);
232 dev_err(dev, "dpio_open() failed\n");
236 dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
238 dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
240 fsl_mc_portal_free(dpio_dev->mc_io);
245 fsl_mc_portal_free(dpio_dev->mc_io);
250 static const struct fsl_mc_device_id dpaa2_dpio_match_id_table[] = {
252 .vendor = FSL_MC_VENDOR_FREESCALE,
258 static struct fsl_mc_driver dpaa2_dpio_driver = {
260 .name = KBUILD_MODNAME,
261 .owner = THIS_MODULE,
263 .probe = dpaa2_dpio_probe,
264 .remove = dpaa2_dpio_remove,
265 .match_id_table = dpaa2_dpio_match_id_table
268 static int dpio_driver_init(void)
270 return fsl_mc_driver_register(&dpaa2_dpio_driver);
273 static void dpio_driver_exit(void)
275 fsl_mc_driver_unregister(&dpaa2_dpio_driver);
277 module_init(dpio_driver_init);
278 module_exit(dpio_driver_exit);