linux/drivers/soc/fsl/dpio/dpio-driver.c
<<
>>
Prefs
   1// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
   2/*
   3 * Copyright 2014-2016 Freescale Semiconductor Inc.
   4 * Copyright NXP 2016
   5 *
   6 */
   7
   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>
  16#include <linux/io.h>
  17#include <linux/sys_soc.h>
  18
  19#include <linux/fsl/mc.h>
  20#include <soc/fsl/dpaa2-io.h>
  21
  22#include "qbman-portal.h"
  23#include "dpio.h"
  24#include "dpio-cmd.h"
  25
  26MODULE_LICENSE("Dual BSD/GPL");
  27MODULE_AUTHOR("Freescale Semiconductor, Inc");
  28MODULE_DESCRIPTION("DPIO Driver");
  29
  30struct dpio_priv {
  31        struct dpaa2_io *io;
  32};
  33
  34static cpumask_var_t cpus_unused_mask;
  35
  36static const struct soc_device_attribute ls1088a_soc[] = {
  37        {.family = "QorIQ LS1088A"},
  38        { /* sentinel */ }
  39};
  40
  41static const struct soc_device_attribute ls2080a_soc[] = {
  42        {.family = "QorIQ LS2080A"},
  43        { /* sentinel */ }
  44};
  45
  46static const struct soc_device_attribute ls2088a_soc[] = {
  47        {.family = "QorIQ LS2088A"},
  48        { /* sentinel */ }
  49};
  50
  51static const struct soc_device_attribute lx2160a_soc[] = {
  52        {.family = "QorIQ LX2160A"},
  53        { /* sentinel */ }
  54};
  55
  56static int dpaa2_dpio_get_cluster_sdest(struct fsl_mc_device *dpio_dev, int cpu)
  57{
  58        int cluster_base, cluster_size;
  59
  60        if (soc_device_match(ls1088a_soc)) {
  61                cluster_base = 2;
  62                cluster_size = 4;
  63        } else if (soc_device_match(ls2080a_soc) ||
  64                   soc_device_match(ls2088a_soc) ||
  65                   soc_device_match(lx2160a_soc)) {
  66                cluster_base = 0;
  67                cluster_size = 2;
  68        } else {
  69                dev_err(&dpio_dev->dev, "unknown SoC version\n");
  70                return -1;
  71        }
  72
  73        return cluster_base + cpu / cluster_size;
  74}
  75
  76static irqreturn_t dpio_irq_handler(int irq_num, void *arg)
  77{
  78        struct device *dev = (struct device *)arg;
  79        struct dpio_priv *priv = dev_get_drvdata(dev);
  80
  81        return dpaa2_io_irq(priv->io);
  82}
  83
  84static void unregister_dpio_irq_handlers(struct fsl_mc_device *dpio_dev)
  85{
  86        struct fsl_mc_device_irq *irq;
  87
  88        irq = dpio_dev->irqs[0];
  89
  90        /* clear the affinity hint */
  91        irq_set_affinity_hint(irq->msi_desc->irq, NULL);
  92}
  93
  94static int register_dpio_irq_handlers(struct fsl_mc_device *dpio_dev, int cpu)
  95{
  96        int error;
  97        struct fsl_mc_device_irq *irq;
  98        cpumask_t mask;
  99
 100        irq = dpio_dev->irqs[0];
 101        error = devm_request_irq(&dpio_dev->dev,
 102                                 irq->msi_desc->irq,
 103                                 dpio_irq_handler,
 104                                 0,
 105                                 dev_name(&dpio_dev->dev),
 106                                 &dpio_dev->dev);
 107        if (error < 0) {
 108                dev_err(&dpio_dev->dev,
 109                        "devm_request_irq() failed: %d\n",
 110                        error);
 111                return error;
 112        }
 113
 114        /* set the affinity hint */
 115        cpumask_clear(&mask);
 116        cpumask_set_cpu(cpu, &mask);
 117        if (irq_set_affinity_hint(irq->msi_desc->irq, &mask))
 118                dev_err(&dpio_dev->dev,
 119                        "irq_set_affinity failed irq %d cpu %d\n",
 120                        irq->msi_desc->irq, cpu);
 121
 122        return 0;
 123}
 124
 125static int dpaa2_dpio_probe(struct fsl_mc_device *dpio_dev)
 126{
 127        struct dpio_attr dpio_attrs;
 128        struct dpaa2_io_desc desc;
 129        struct dpio_priv *priv;
 130        int err = -ENOMEM;
 131        struct device *dev = &dpio_dev->dev;
 132        int possible_next_cpu;
 133        int sdest;
 134
 135        priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
 136        if (!priv)
 137                goto err_priv_alloc;
 138
 139        dev_set_drvdata(dev, priv);
 140
 141        err = fsl_mc_portal_allocate(dpio_dev, 0, &dpio_dev->mc_io);
 142        if (err) {
 143                dev_dbg(dev, "MC portal allocation failed\n");
 144                err = -EPROBE_DEFER;
 145                goto err_priv_alloc;
 146        }
 147
 148        err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
 149                        &dpio_dev->mc_handle);
 150        if (err) {
 151                dev_err(dev, "dpio_open() failed\n");
 152                goto err_open;
 153        }
 154
 155        err = dpio_reset(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
 156        if (err) {
 157                dev_err(dev, "dpio_reset() failed\n");
 158                goto err_reset;
 159        }
 160
 161        err = dpio_get_attributes(dpio_dev->mc_io, 0, dpio_dev->mc_handle,
 162                                  &dpio_attrs);
 163        if (err) {
 164                dev_err(dev, "dpio_get_attributes() failed %d\n", err);
 165                goto err_get_attr;
 166        }
 167        desc.qman_version = dpio_attrs.qbman_version;
 168
 169        err = dpio_enable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
 170        if (err) {
 171                dev_err(dev, "dpio_enable() failed %d\n", err);
 172                goto err_get_attr;
 173        }
 174
 175        /* initialize DPIO descriptor */
 176        desc.receives_notifications = dpio_attrs.num_priorities ? 1 : 0;
 177        desc.has_8prio = dpio_attrs.num_priorities == 8 ? 1 : 0;
 178        desc.dpio_id = dpio_dev->obj_desc.id;
 179
 180        /* get the cpu to use for the affinity hint */
 181        possible_next_cpu = cpumask_first(cpus_unused_mask);
 182        if (possible_next_cpu >= nr_cpu_ids) {
 183                dev_err(dev, "probe failed. Number of DPIOs exceeds NR_CPUS.\n");
 184                err = -ERANGE;
 185                goto err_allocate_irqs;
 186        }
 187        desc.cpu = possible_next_cpu;
 188        cpumask_clear_cpu(possible_next_cpu, cpus_unused_mask);
 189
 190        sdest = dpaa2_dpio_get_cluster_sdest(dpio_dev, desc.cpu);
 191        if (sdest >= 0) {
 192                err = dpio_set_stashing_destination(dpio_dev->mc_io, 0,
 193                                                    dpio_dev->mc_handle,
 194                                                    sdest);
 195                if (err)
 196                        dev_err(dev, "dpio_set_stashing_destination failed for cpu%d\n",
 197                                desc.cpu);
 198        }
 199
 200        if (dpio_dev->obj_desc.region_count < 3) {
 201                /* No support for DDR backed portals, use classic mapping */
 202                /*
 203                 * Set the CENA regs to be the cache inhibited area of the
 204                 * portal to avoid coherency issues if a user migrates to
 205                 * another core.
 206                 */
 207                desc.regs_cena = devm_memremap(dev, dpio_dev->regions[1].start,
 208                                        resource_size(&dpio_dev->regions[1]),
 209                                        MEMREMAP_WC);
 210        } else {
 211                desc.regs_cena = devm_memremap(dev, dpio_dev->regions[2].start,
 212                                        resource_size(&dpio_dev->regions[2]),
 213                                        MEMREMAP_WB);
 214        }
 215
 216        if (IS_ERR(desc.regs_cena)) {
 217                dev_err(dev, "devm_memremap failed\n");
 218                err = PTR_ERR(desc.regs_cena);
 219                goto err_allocate_irqs;
 220        }
 221
 222        desc.regs_cinh = devm_ioremap(dev, dpio_dev->regions[1].start,
 223                                      resource_size(&dpio_dev->regions[1]));
 224        if (!desc.regs_cinh) {
 225                err = -ENOMEM;
 226                dev_err(dev, "devm_ioremap failed\n");
 227                goto err_allocate_irqs;
 228        }
 229
 230        err = fsl_mc_allocate_irqs(dpio_dev);
 231        if (err) {
 232                dev_err(dev, "fsl_mc_allocate_irqs failed. err=%d\n", err);
 233                goto err_allocate_irqs;
 234        }
 235
 236        err = register_dpio_irq_handlers(dpio_dev, desc.cpu);
 237        if (err)
 238                goto err_register_dpio_irq;
 239
 240        priv->io = dpaa2_io_create(&desc, dev);
 241        if (!priv->io) {
 242                dev_err(dev, "dpaa2_io_create failed\n");
 243                err = -ENOMEM;
 244                goto err_dpaa2_io_create;
 245        }
 246
 247        dev_info(dev, "probed\n");
 248        dev_dbg(dev, "   receives_notifications = %d\n",
 249                desc.receives_notifications);
 250        dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
 251
 252        return 0;
 253
 254err_dpaa2_io_create:
 255        unregister_dpio_irq_handlers(dpio_dev);
 256err_register_dpio_irq:
 257        fsl_mc_free_irqs(dpio_dev);
 258err_allocate_irqs:
 259        dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
 260err_get_attr:
 261err_reset:
 262        dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
 263err_open:
 264        fsl_mc_portal_free(dpio_dev->mc_io);
 265err_priv_alloc:
 266        return err;
 267}
 268
 269/* Tear down interrupts for a given DPIO object */
 270static void dpio_teardown_irqs(struct fsl_mc_device *dpio_dev)
 271{
 272        unregister_dpio_irq_handlers(dpio_dev);
 273        fsl_mc_free_irqs(dpio_dev);
 274}
 275
 276static int dpaa2_dpio_remove(struct fsl_mc_device *dpio_dev)
 277{
 278        struct device *dev;
 279        struct dpio_priv *priv;
 280        int err = 0, cpu;
 281
 282        dev = &dpio_dev->dev;
 283        priv = dev_get_drvdata(dev);
 284        cpu = dpaa2_io_get_cpu(priv->io);
 285
 286        dpaa2_io_down(priv->io);
 287
 288        dpio_teardown_irqs(dpio_dev);
 289
 290        cpumask_set_cpu(cpu, cpus_unused_mask);
 291
 292        err = dpio_open(dpio_dev->mc_io, 0, dpio_dev->obj_desc.id,
 293                        &dpio_dev->mc_handle);
 294        if (err) {
 295                dev_err(dev, "dpio_open() failed\n");
 296                goto err_open;
 297        }
 298
 299        dpio_disable(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
 300
 301        dpio_close(dpio_dev->mc_io, 0, dpio_dev->mc_handle);
 302
 303        fsl_mc_portal_free(dpio_dev->mc_io);
 304
 305        return 0;
 306
 307err_open:
 308        fsl_mc_portal_free(dpio_dev->mc_io);
 309
 310        return err;
 311}
 312
 313static const struct fsl_mc_device_id dpaa2_dpio_match_id_table[] = {
 314        {
 315                .vendor = FSL_MC_VENDOR_FREESCALE,
 316                .obj_type = "dpio",
 317        },
 318        { .vendor = 0x0 }
 319};
 320
 321static struct fsl_mc_driver dpaa2_dpio_driver = {
 322        .driver = {
 323                .name           = KBUILD_MODNAME,
 324                .owner          = THIS_MODULE,
 325        },
 326        .probe          = dpaa2_dpio_probe,
 327        .remove         = dpaa2_dpio_remove,
 328        .match_id_table = dpaa2_dpio_match_id_table
 329};
 330
 331static int dpio_driver_init(void)
 332{
 333        if (!zalloc_cpumask_var(&cpus_unused_mask, GFP_KERNEL))
 334                return -ENOMEM;
 335        cpumask_copy(cpus_unused_mask, cpu_online_mask);
 336
 337        return fsl_mc_driver_register(&dpaa2_dpio_driver);
 338}
 339
 340static void dpio_driver_exit(void)
 341{
 342        free_cpumask_var(cpus_unused_mask);
 343        fsl_mc_driver_unregister(&dpaa2_dpio_driver);
 344}
 345module_init(dpio_driver_init);
 346module_exit(dpio_driver_exit);
 347