linux/drivers/bus/fsl-mc/fsl-mc-bus.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Freescale Management Complex (MC) bus driver
   4 *
   5 * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
   6 * Author: German Rivera <German.Rivera@freescale.com>
   7 *
   8 */
   9
  10#define pr_fmt(fmt) "fsl-mc: " fmt
  11
  12#include <linux/module.h>
  13#include <linux/of_device.h>
  14#include <linux/of_address.h>
  15#include <linux/ioport.h>
  16#include <linux/slab.h>
  17#include <linux/limits.h>
  18#include <linux/bitops.h>
  19#include <linux/msi.h>
  20#include <linux/dma-mapping.h>
  21#include <linux/acpi.h>
  22#include <linux/iommu.h>
  23
  24#include "fsl-mc-private.h"
  25
  26/**
  27 * Default DMA mask for devices on a fsl-mc bus
  28 */
  29#define FSL_MC_DEFAULT_DMA_MASK (~0ULL)
  30
  31static struct fsl_mc_version mc_version;
  32
  33/**
  34 * struct fsl_mc - Private data of a "fsl,qoriq-mc" platform device
  35 * @root_mc_bus_dev: fsl-mc device representing the root DPRC
  36 * @num_translation_ranges: number of entries in addr_translation_ranges
  37 * @translation_ranges: array of bus to system address translation ranges
  38 */
  39struct fsl_mc {
  40        struct fsl_mc_device *root_mc_bus_dev;
  41        u8 num_translation_ranges;
  42        struct fsl_mc_addr_translation_range *translation_ranges;
  43        void *fsl_mc_regs;
  44};
  45
  46/**
  47 * struct fsl_mc_addr_translation_range - bus to system address translation
  48 * range
  49 * @mc_region_type: Type of MC region for the range being translated
  50 * @start_mc_offset: Start MC offset of the range being translated
  51 * @end_mc_offset: MC offset of the first byte after the range (last MC
  52 * offset of the range is end_mc_offset - 1)
  53 * @start_phys_addr: system physical address corresponding to start_mc_addr
  54 */
  55struct fsl_mc_addr_translation_range {
  56        enum dprc_region_type mc_region_type;
  57        u64 start_mc_offset;
  58        u64 end_mc_offset;
  59        phys_addr_t start_phys_addr;
  60};
  61
  62#define FSL_MC_FAPR     0x28
  63#define MC_FAPR_PL      BIT(18)
  64#define MC_FAPR_BMT     BIT(17)
  65
  66/**
  67 * fsl_mc_bus_match - device to driver matching callback
  68 * @dev: the fsl-mc device to match against
  69 * @drv: the device driver to search for matching fsl-mc object type
  70 * structures
  71 *
  72 * Returns 1 on success, 0 otherwise.
  73 */
  74static int fsl_mc_bus_match(struct device *dev, struct device_driver *drv)
  75{
  76        const struct fsl_mc_device_id *id;
  77        struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
  78        struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(drv);
  79        bool found = false;
  80
  81        if (!mc_drv->match_id_table)
  82                goto out;
  83
  84        /*
  85         * If the object is not 'plugged' don't match.
  86         * Only exception is the root DPRC, which is a special case.
  87         */
  88        if ((mc_dev->obj_desc.state & FSL_MC_OBJ_STATE_PLUGGED) == 0 &&
  89            !fsl_mc_is_root_dprc(&mc_dev->dev))
  90                goto out;
  91
  92        /*
  93         * Traverse the match_id table of the given driver, trying to find
  94         * a matching for the given device.
  95         */
  96        for (id = mc_drv->match_id_table; id->vendor != 0x0; id++) {
  97                if (id->vendor == mc_dev->obj_desc.vendor &&
  98                    strcmp(id->obj_type, mc_dev->obj_desc.type) == 0) {
  99                        found = true;
 100
 101                        break;
 102                }
 103        }
 104
 105out:
 106        dev_dbg(dev, "%smatched\n", found ? "" : "not ");
 107        return found;
 108}
 109
 110/**
 111 * fsl_mc_bus_uevent - callback invoked when a device is added
 112 */
 113static int fsl_mc_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
 114{
 115        struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
 116
 117        if (add_uevent_var(env, "MODALIAS=fsl-mc:v%08Xd%s",
 118                           mc_dev->obj_desc.vendor,
 119                           mc_dev->obj_desc.type))
 120                return -ENOMEM;
 121
 122        return 0;
 123}
 124
 125static int fsl_mc_dma_configure(struct device *dev)
 126{
 127        struct device *dma_dev = dev;
 128        struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
 129        u32 input_id = mc_dev->icid;
 130
 131        while (dev_is_fsl_mc(dma_dev))
 132                dma_dev = dma_dev->parent;
 133
 134        if (dev_of_node(dma_dev))
 135                return of_dma_configure_id(dev, dma_dev->of_node, 0, &input_id);
 136
 137        return acpi_dma_configure_id(dev, DEV_DMA_COHERENT, &input_id);
 138}
 139
 140static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
 141                             char *buf)
 142{
 143        struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
 144
 145        return sprintf(buf, "fsl-mc:v%08Xd%s\n", mc_dev->obj_desc.vendor,
 146                       mc_dev->obj_desc.type);
 147}
 148static DEVICE_ATTR_RO(modalias);
 149
 150static struct attribute *fsl_mc_dev_attrs[] = {
 151        &dev_attr_modalias.attr,
 152        NULL,
 153};
 154
 155ATTRIBUTE_GROUPS(fsl_mc_dev);
 156
 157struct bus_type fsl_mc_bus_type = {
 158        .name = "fsl-mc",
 159        .match = fsl_mc_bus_match,
 160        .uevent = fsl_mc_bus_uevent,
 161        .dma_configure  = fsl_mc_dma_configure,
 162        .dev_groups = fsl_mc_dev_groups,
 163};
 164EXPORT_SYMBOL_GPL(fsl_mc_bus_type);
 165
 166struct device_type fsl_mc_bus_dprc_type = {
 167        .name = "fsl_mc_bus_dprc"
 168};
 169EXPORT_SYMBOL_GPL(fsl_mc_bus_dprc_type);
 170
 171struct device_type fsl_mc_bus_dpni_type = {
 172        .name = "fsl_mc_bus_dpni"
 173};
 174EXPORT_SYMBOL_GPL(fsl_mc_bus_dpni_type);
 175
 176struct device_type fsl_mc_bus_dpio_type = {
 177        .name = "fsl_mc_bus_dpio"
 178};
 179EXPORT_SYMBOL_GPL(fsl_mc_bus_dpio_type);
 180
 181struct device_type fsl_mc_bus_dpsw_type = {
 182        .name = "fsl_mc_bus_dpsw"
 183};
 184EXPORT_SYMBOL_GPL(fsl_mc_bus_dpsw_type);
 185
 186struct device_type fsl_mc_bus_dpbp_type = {
 187        .name = "fsl_mc_bus_dpbp"
 188};
 189EXPORT_SYMBOL_GPL(fsl_mc_bus_dpbp_type);
 190
 191struct device_type fsl_mc_bus_dpcon_type = {
 192        .name = "fsl_mc_bus_dpcon"
 193};
 194EXPORT_SYMBOL_GPL(fsl_mc_bus_dpcon_type);
 195
 196struct device_type fsl_mc_bus_dpmcp_type = {
 197        .name = "fsl_mc_bus_dpmcp"
 198};
 199EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmcp_type);
 200
 201struct device_type fsl_mc_bus_dpmac_type = {
 202        .name = "fsl_mc_bus_dpmac"
 203};
 204EXPORT_SYMBOL_GPL(fsl_mc_bus_dpmac_type);
 205
 206struct device_type fsl_mc_bus_dprtc_type = {
 207        .name = "fsl_mc_bus_dprtc"
 208};
 209EXPORT_SYMBOL_GPL(fsl_mc_bus_dprtc_type);
 210
 211struct device_type fsl_mc_bus_dpseci_type = {
 212        .name = "fsl_mc_bus_dpseci"
 213};
 214EXPORT_SYMBOL_GPL(fsl_mc_bus_dpseci_type);
 215
 216struct device_type fsl_mc_bus_dpdmux_type = {
 217        .name = "fsl_mc_bus_dpdmux"
 218};
 219EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmux_type);
 220
 221struct device_type fsl_mc_bus_dpdcei_type = {
 222        .name = "fsl_mc_bus_dpdcei"
 223};
 224EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdcei_type);
 225
 226struct device_type fsl_mc_bus_dpaiop_type = {
 227        .name = "fsl_mc_bus_dpaiop"
 228};
 229EXPORT_SYMBOL_GPL(fsl_mc_bus_dpaiop_type);
 230
 231struct device_type fsl_mc_bus_dpci_type = {
 232        .name = "fsl_mc_bus_dpci"
 233};
 234EXPORT_SYMBOL_GPL(fsl_mc_bus_dpci_type);
 235
 236struct device_type fsl_mc_bus_dpdmai_type = {
 237        .name = "fsl_mc_bus_dpdmai"
 238};
 239EXPORT_SYMBOL_GPL(fsl_mc_bus_dpdmai_type);
 240
 241static struct device_type *fsl_mc_get_device_type(const char *type)
 242{
 243        static const struct {
 244                struct device_type *dev_type;
 245                const char *type;
 246        } dev_types[] = {
 247                { &fsl_mc_bus_dprc_type, "dprc" },
 248                { &fsl_mc_bus_dpni_type, "dpni" },
 249                { &fsl_mc_bus_dpio_type, "dpio" },
 250                { &fsl_mc_bus_dpsw_type, "dpsw" },
 251                { &fsl_mc_bus_dpbp_type, "dpbp" },
 252                { &fsl_mc_bus_dpcon_type, "dpcon" },
 253                { &fsl_mc_bus_dpmcp_type, "dpmcp" },
 254                { &fsl_mc_bus_dpmac_type, "dpmac" },
 255                { &fsl_mc_bus_dprtc_type, "dprtc" },
 256                { &fsl_mc_bus_dpseci_type, "dpseci" },
 257                { &fsl_mc_bus_dpdmux_type, "dpdmux" },
 258                { &fsl_mc_bus_dpdcei_type, "dpdcei" },
 259                { &fsl_mc_bus_dpaiop_type, "dpaiop" },
 260                { &fsl_mc_bus_dpci_type, "dpci" },
 261                { &fsl_mc_bus_dpdmai_type, "dpdmai" },
 262                { NULL, NULL }
 263        };
 264        int i;
 265
 266        for (i = 0; dev_types[i].dev_type; i++)
 267                if (!strcmp(dev_types[i].type, type))
 268                        return dev_types[i].dev_type;
 269
 270        return NULL;
 271}
 272
 273static int fsl_mc_driver_probe(struct device *dev)
 274{
 275        struct fsl_mc_driver *mc_drv;
 276        struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
 277        int error;
 278
 279        mc_drv = to_fsl_mc_driver(dev->driver);
 280
 281        error = mc_drv->probe(mc_dev);
 282        if (error < 0) {
 283                if (error != -EPROBE_DEFER)
 284                        dev_err(dev, "%s failed: %d\n", __func__, error);
 285                return error;
 286        }
 287
 288        return 0;
 289}
 290
 291static int fsl_mc_driver_remove(struct device *dev)
 292{
 293        struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
 294        struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
 295        int error;
 296
 297        error = mc_drv->remove(mc_dev);
 298        if (error < 0) {
 299                dev_err(dev, "%s failed: %d\n", __func__, error);
 300                return error;
 301        }
 302
 303        return 0;
 304}
 305
 306static void fsl_mc_driver_shutdown(struct device *dev)
 307{
 308        struct fsl_mc_driver *mc_drv = to_fsl_mc_driver(dev->driver);
 309        struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
 310
 311        mc_drv->shutdown(mc_dev);
 312}
 313
 314/**
 315 * __fsl_mc_driver_register - registers a child device driver with the
 316 * MC bus
 317 *
 318 * This function is implicitly invoked from the registration function of
 319 * fsl_mc device drivers, which is generated by the
 320 * module_fsl_mc_driver() macro.
 321 */
 322int __fsl_mc_driver_register(struct fsl_mc_driver *mc_driver,
 323                             struct module *owner)
 324{
 325        int error;
 326
 327        mc_driver->driver.owner = owner;
 328        mc_driver->driver.bus = &fsl_mc_bus_type;
 329
 330        if (mc_driver->probe)
 331                mc_driver->driver.probe = fsl_mc_driver_probe;
 332
 333        if (mc_driver->remove)
 334                mc_driver->driver.remove = fsl_mc_driver_remove;
 335
 336        if (mc_driver->shutdown)
 337                mc_driver->driver.shutdown = fsl_mc_driver_shutdown;
 338
 339        error = driver_register(&mc_driver->driver);
 340        if (error < 0) {
 341                pr_err("driver_register() failed for %s: %d\n",
 342                       mc_driver->driver.name, error);
 343                return error;
 344        }
 345
 346        return 0;
 347}
 348EXPORT_SYMBOL_GPL(__fsl_mc_driver_register);
 349
 350/**
 351 * fsl_mc_driver_unregister - unregisters a device driver from the
 352 * MC bus
 353 */
 354void fsl_mc_driver_unregister(struct fsl_mc_driver *mc_driver)
 355{
 356        driver_unregister(&mc_driver->driver);
 357}
 358EXPORT_SYMBOL_GPL(fsl_mc_driver_unregister);
 359
 360/**
 361 * mc_get_version() - Retrieves the Management Complex firmware
 362 *                      version information
 363 * @mc_io:              Pointer to opaque I/O object
 364 * @cmd_flags:          Command flags; one or more of 'MC_CMD_FLAG_'
 365 * @mc_ver_info:        Returned version information structure
 366 *
 367 * Return:      '0' on Success; Error code otherwise.
 368 */
 369static int mc_get_version(struct fsl_mc_io *mc_io,
 370                          u32 cmd_flags,
 371                          struct fsl_mc_version *mc_ver_info)
 372{
 373        struct fsl_mc_command cmd = { 0 };
 374        struct dpmng_rsp_get_version *rsp_params;
 375        int err;
 376
 377        /* prepare command */
 378        cmd.header = mc_encode_cmd_header(DPMNG_CMDID_GET_VERSION,
 379                                          cmd_flags,
 380                                          0);
 381
 382        /* send command to mc*/
 383        err = mc_send_command(mc_io, &cmd);
 384        if (err)
 385                return err;
 386
 387        /* retrieve response parameters */
 388        rsp_params = (struct dpmng_rsp_get_version *)cmd.params;
 389        mc_ver_info->revision = le32_to_cpu(rsp_params->revision);
 390        mc_ver_info->major = le32_to_cpu(rsp_params->version_major);
 391        mc_ver_info->minor = le32_to_cpu(rsp_params->version_minor);
 392
 393        return 0;
 394}
 395
 396/**
 397 * fsl_mc_get_version - function to retrieve the MC f/w version information
 398 *
 399 * Return:      mc version when called after fsl-mc-bus probe; NULL otherwise.
 400 */
 401struct fsl_mc_version *fsl_mc_get_version(void)
 402{
 403        if (mc_version.major)
 404                return &mc_version;
 405
 406        return NULL;
 407}
 408EXPORT_SYMBOL_GPL(fsl_mc_get_version);
 409
 410/**
 411 * fsl_mc_get_root_dprc - function to traverse to the root dprc
 412 */
 413void fsl_mc_get_root_dprc(struct device *dev,
 414                         struct device **root_dprc_dev)
 415{
 416        if (!dev) {
 417                *root_dprc_dev = NULL;
 418        } else if (!dev_is_fsl_mc(dev)) {
 419                *root_dprc_dev = NULL;
 420        } else {
 421                *root_dprc_dev = dev;
 422                while (dev_is_fsl_mc((*root_dprc_dev)->parent))
 423                        *root_dprc_dev = (*root_dprc_dev)->parent;
 424        }
 425}
 426
 427static int get_dprc_attr(struct fsl_mc_io *mc_io,
 428                         int container_id, struct dprc_attributes *attr)
 429{
 430        u16 dprc_handle;
 431        int error;
 432
 433        error = dprc_open(mc_io, 0, container_id, &dprc_handle);
 434        if (error < 0) {
 435                dev_err(mc_io->dev, "dprc_open() failed: %d\n", error);
 436                return error;
 437        }
 438
 439        memset(attr, 0, sizeof(struct dprc_attributes));
 440        error = dprc_get_attributes(mc_io, 0, dprc_handle, attr);
 441        if (error < 0) {
 442                dev_err(mc_io->dev, "dprc_get_attributes() failed: %d\n",
 443                        error);
 444                goto common_cleanup;
 445        }
 446
 447        error = 0;
 448
 449common_cleanup:
 450        (void)dprc_close(mc_io, 0, dprc_handle);
 451        return error;
 452}
 453
 454static int get_dprc_icid(struct fsl_mc_io *mc_io,
 455                         int container_id, u16 *icid)
 456{
 457        struct dprc_attributes attr;
 458        int error;
 459
 460        error = get_dprc_attr(mc_io, container_id, &attr);
 461        if (error == 0)
 462                *icid = attr.icid;
 463
 464        return error;
 465}
 466
 467static int translate_mc_addr(struct fsl_mc_device *mc_dev,
 468                             enum dprc_region_type mc_region_type,
 469                             u64 mc_offset, phys_addr_t *phys_addr)
 470{
 471        int i;
 472        struct device *root_dprc_dev;
 473        struct fsl_mc *mc;
 474
 475        fsl_mc_get_root_dprc(&mc_dev->dev, &root_dprc_dev);
 476        mc = dev_get_drvdata(root_dprc_dev->parent);
 477
 478        if (mc->num_translation_ranges == 0) {
 479                /*
 480                 * Do identity mapping:
 481                 */
 482                *phys_addr = mc_offset;
 483                return 0;
 484        }
 485
 486        for (i = 0; i < mc->num_translation_ranges; i++) {
 487                struct fsl_mc_addr_translation_range *range =
 488                        &mc->translation_ranges[i];
 489
 490                if (mc_region_type == range->mc_region_type &&
 491                    mc_offset >= range->start_mc_offset &&
 492                    mc_offset < range->end_mc_offset) {
 493                        *phys_addr = range->start_phys_addr +
 494                                     (mc_offset - range->start_mc_offset);
 495                        return 0;
 496                }
 497        }
 498
 499        return -EFAULT;
 500}
 501
 502static int fsl_mc_device_get_mmio_regions(struct fsl_mc_device *mc_dev,
 503                                          struct fsl_mc_device *mc_bus_dev)
 504{
 505        int i;
 506        int error;
 507        struct resource *regions;
 508        struct fsl_mc_obj_desc *obj_desc = &mc_dev->obj_desc;
 509        struct device *parent_dev = mc_dev->dev.parent;
 510        enum dprc_region_type mc_region_type;
 511
 512        if (is_fsl_mc_bus_dprc(mc_dev) ||
 513            is_fsl_mc_bus_dpmcp(mc_dev)) {
 514                mc_region_type = DPRC_REGION_TYPE_MC_PORTAL;
 515        } else if (is_fsl_mc_bus_dpio(mc_dev)) {
 516                mc_region_type = DPRC_REGION_TYPE_QBMAN_PORTAL;
 517        } else {
 518                /*
 519                 * This function should not have been called for this MC object
 520                 * type, as this object type is not supposed to have MMIO
 521                 * regions
 522                 */
 523                return -EINVAL;
 524        }
 525
 526        regions = kmalloc_array(obj_desc->region_count,
 527                                sizeof(regions[0]), GFP_KERNEL);
 528        if (!regions)
 529                return -ENOMEM;
 530
 531        for (i = 0; i < obj_desc->region_count; i++) {
 532                struct dprc_region_desc region_desc;
 533
 534                error = dprc_get_obj_region(mc_bus_dev->mc_io,
 535                                            0,
 536                                            mc_bus_dev->mc_handle,
 537                                            obj_desc->type,
 538                                            obj_desc->id, i, &region_desc);
 539                if (error < 0) {
 540                        dev_err(parent_dev,
 541                                "dprc_get_obj_region() failed: %d\n", error);
 542                        goto error_cleanup_regions;
 543                }
 544                /*
 545                 * Older MC only returned region offset and no base address
 546                 * If base address is in the region_desc use it otherwise
 547                 * revert to old mechanism
 548                 */
 549                if (region_desc.base_address)
 550                        regions[i].start = region_desc.base_address +
 551                                                region_desc.base_offset;
 552                else
 553                        error = translate_mc_addr(mc_dev, mc_region_type,
 554                                          region_desc.base_offset,
 555                                          &regions[i].start);
 556
 557                if (error < 0) {
 558                        dev_err(parent_dev,
 559                                "Invalid MC offset: %#x (for %s.%d\'s region %d)\n",
 560                                region_desc.base_offset,
 561                                obj_desc->type, obj_desc->id, i);
 562                        goto error_cleanup_regions;
 563                }
 564
 565                regions[i].end = regions[i].start + region_desc.size - 1;
 566                regions[i].name = "fsl-mc object MMIO region";
 567                regions[i].flags = IORESOURCE_IO;
 568                if (region_desc.flags & DPRC_REGION_CACHEABLE)
 569                        regions[i].flags |= IORESOURCE_CACHEABLE;
 570                if (region_desc.flags & DPRC_REGION_SHAREABLE)
 571                        regions[i].flags |= IORESOURCE_MEM;
 572        }
 573
 574        mc_dev->regions = regions;
 575        return 0;
 576
 577error_cleanup_regions:
 578        kfree(regions);
 579        return error;
 580}
 581
 582/**
 583 * fsl_mc_is_root_dprc - function to check if a given device is a root dprc
 584 */
 585bool fsl_mc_is_root_dprc(struct device *dev)
 586{
 587        struct device *root_dprc_dev;
 588
 589        fsl_mc_get_root_dprc(dev, &root_dprc_dev);
 590        if (!root_dprc_dev)
 591                return false;
 592        return dev == root_dprc_dev;
 593}
 594
 595static void fsl_mc_device_release(struct device *dev)
 596{
 597        struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
 598
 599        kfree(mc_dev->regions);
 600
 601        if (is_fsl_mc_bus_dprc(mc_dev))
 602                kfree(to_fsl_mc_bus(mc_dev));
 603        else
 604                kfree(mc_dev);
 605}
 606
 607/**
 608 * Add a newly discovered fsl-mc device to be visible in Linux
 609 */
 610int fsl_mc_device_add(struct fsl_mc_obj_desc *obj_desc,
 611                      struct fsl_mc_io *mc_io,
 612                      struct device *parent_dev,
 613                      struct fsl_mc_device **new_mc_dev)
 614{
 615        int error;
 616        struct fsl_mc_device *mc_dev = NULL;
 617        struct fsl_mc_bus *mc_bus = NULL;
 618        struct fsl_mc_device *parent_mc_dev;
 619
 620        if (dev_is_fsl_mc(parent_dev))
 621                parent_mc_dev = to_fsl_mc_device(parent_dev);
 622        else
 623                parent_mc_dev = NULL;
 624
 625        if (strcmp(obj_desc->type, "dprc") == 0) {
 626                /*
 627                 * Allocate an MC bus device object:
 628                 */
 629                mc_bus = kzalloc(sizeof(*mc_bus), GFP_KERNEL);
 630                if (!mc_bus)
 631                        return -ENOMEM;
 632
 633                mc_dev = &mc_bus->mc_dev;
 634        } else {
 635                /*
 636                 * Allocate a regular fsl_mc_device object:
 637                 */
 638                mc_dev = kzalloc(sizeof(*mc_dev), GFP_KERNEL);
 639                if (!mc_dev)
 640                        return -ENOMEM;
 641        }
 642
 643        mc_dev->obj_desc = *obj_desc;
 644        mc_dev->mc_io = mc_io;
 645        device_initialize(&mc_dev->dev);
 646        mc_dev->dev.parent = parent_dev;
 647        mc_dev->dev.bus = &fsl_mc_bus_type;
 648        mc_dev->dev.release = fsl_mc_device_release;
 649        mc_dev->dev.type = fsl_mc_get_device_type(obj_desc->type);
 650        if (!mc_dev->dev.type) {
 651                error = -ENODEV;
 652                dev_err(parent_dev, "unknown device type %s\n", obj_desc->type);
 653                goto error_cleanup_dev;
 654        }
 655        dev_set_name(&mc_dev->dev, "%s.%d", obj_desc->type, obj_desc->id);
 656
 657        if (strcmp(obj_desc->type, "dprc") == 0) {
 658                struct fsl_mc_io *mc_io2;
 659
 660                mc_dev->flags |= FSL_MC_IS_DPRC;
 661
 662                /*
 663                 * To get the DPRC's ICID, we need to open the DPRC
 664                 * in get_dprc_icid(). For child DPRCs, we do so using the
 665                 * parent DPRC's MC portal instead of the child DPRC's MC
 666                 * portal, in case the child DPRC is already opened with
 667                 * its own portal (e.g., the DPRC used by AIOP).
 668                 *
 669                 * NOTE: There cannot be more than one active open for a
 670                 * given MC object, using the same MC portal.
 671                 */
 672                if (parent_mc_dev) {
 673                        /*
 674                         * device being added is a child DPRC device
 675                         */
 676                        mc_io2 = parent_mc_dev->mc_io;
 677                } else {
 678                        /*
 679                         * device being added is the root DPRC device
 680                         */
 681                        if (!mc_io) {
 682                                error = -EINVAL;
 683                                goto error_cleanup_dev;
 684                        }
 685
 686                        mc_io2 = mc_io;
 687                }
 688
 689                error = get_dprc_icid(mc_io2, obj_desc->id, &mc_dev->icid);
 690                if (error < 0)
 691                        goto error_cleanup_dev;
 692        } else {
 693                /*
 694                 * A non-DPRC object has to be a child of a DPRC, use the
 695                 * parent's ICID and interrupt domain.
 696                 */
 697                mc_dev->icid = parent_mc_dev->icid;
 698                mc_dev->dma_mask = FSL_MC_DEFAULT_DMA_MASK;
 699                mc_dev->dev.dma_mask = &mc_dev->dma_mask;
 700                mc_dev->dev.coherent_dma_mask = mc_dev->dma_mask;
 701                dev_set_msi_domain(&mc_dev->dev,
 702                                   dev_get_msi_domain(&parent_mc_dev->dev));
 703        }
 704
 705        /*
 706         * Get MMIO regions for the device from the MC:
 707         *
 708         * NOTE: the root DPRC is a special case as its MMIO region is
 709         * obtained from the device tree
 710         */
 711        if (parent_mc_dev && obj_desc->region_count != 0) {
 712                error = fsl_mc_device_get_mmio_regions(mc_dev,
 713                                                       parent_mc_dev);
 714                if (error < 0)
 715                        goto error_cleanup_dev;
 716        }
 717
 718        /*
 719         * The device-specific probe callback will get invoked by device_add()
 720         */
 721        error = device_add(&mc_dev->dev);
 722        if (error < 0) {
 723                dev_err(parent_dev,
 724                        "device_add() failed for device %s: %d\n",
 725                        dev_name(&mc_dev->dev), error);
 726                goto error_cleanup_dev;
 727        }
 728
 729        dev_dbg(parent_dev, "added %s\n", dev_name(&mc_dev->dev));
 730
 731        *new_mc_dev = mc_dev;
 732        return 0;
 733
 734error_cleanup_dev:
 735        kfree(mc_dev->regions);
 736        kfree(mc_bus);
 737        kfree(mc_dev);
 738
 739        return error;
 740}
 741EXPORT_SYMBOL_GPL(fsl_mc_device_add);
 742
 743/**
 744 * fsl_mc_device_remove - Remove an fsl-mc device from being visible to
 745 * Linux
 746 *
 747 * @mc_dev: Pointer to an fsl-mc device
 748 */
 749void fsl_mc_device_remove(struct fsl_mc_device *mc_dev)
 750{
 751        /*
 752         * The device-specific remove callback will get invoked by device_del()
 753         */
 754        device_del(&mc_dev->dev);
 755        put_device(&mc_dev->dev);
 756}
 757EXPORT_SYMBOL_GPL(fsl_mc_device_remove);
 758
 759struct fsl_mc_device *fsl_mc_get_endpoint(struct fsl_mc_device *mc_dev)
 760{
 761        struct fsl_mc_device *mc_bus_dev, *endpoint;
 762        struct fsl_mc_obj_desc endpoint_desc = {{ 0 }};
 763        struct dprc_endpoint endpoint1 = {{ 0 }};
 764        struct dprc_endpoint endpoint2 = {{ 0 }};
 765        int state, err;
 766
 767        mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
 768        strcpy(endpoint1.type, mc_dev->obj_desc.type);
 769        endpoint1.id = mc_dev->obj_desc.id;
 770
 771        err = dprc_get_connection(mc_bus_dev->mc_io, 0,
 772                                  mc_bus_dev->mc_handle,
 773                                  &endpoint1, &endpoint2,
 774                                  &state);
 775
 776        if (err == -ENOTCONN || state == -1)
 777                return ERR_PTR(-ENOTCONN);
 778
 779        if (err < 0) {
 780                dev_err(&mc_bus_dev->dev, "dprc_get_connection() = %d\n", err);
 781                return ERR_PTR(err);
 782        }
 783
 784        strcpy(endpoint_desc.type, endpoint2.type);
 785        endpoint_desc.id = endpoint2.id;
 786        endpoint = fsl_mc_device_lookup(&endpoint_desc, mc_bus_dev);
 787
 788        return endpoint;
 789}
 790EXPORT_SYMBOL_GPL(fsl_mc_get_endpoint);
 791
 792static int parse_mc_ranges(struct device *dev,
 793                           int *paddr_cells,
 794                           int *mc_addr_cells,
 795                           int *mc_size_cells,
 796                           const __be32 **ranges_start)
 797{
 798        const __be32 *prop;
 799        int range_tuple_cell_count;
 800        int ranges_len;
 801        int tuple_len;
 802        struct device_node *mc_node = dev->of_node;
 803
 804        *ranges_start = of_get_property(mc_node, "ranges", &ranges_len);
 805        if (!(*ranges_start) || !ranges_len) {
 806                dev_warn(dev,
 807                         "missing or empty ranges property for device tree node '%pOFn'\n",
 808                         mc_node);
 809                return 0;
 810        }
 811
 812        *paddr_cells = of_n_addr_cells(mc_node);
 813
 814        prop = of_get_property(mc_node, "#address-cells", NULL);
 815        if (prop)
 816                *mc_addr_cells = be32_to_cpup(prop);
 817        else
 818                *mc_addr_cells = *paddr_cells;
 819
 820        prop = of_get_property(mc_node, "#size-cells", NULL);
 821        if (prop)
 822                *mc_size_cells = be32_to_cpup(prop);
 823        else
 824                *mc_size_cells = of_n_size_cells(mc_node);
 825
 826        range_tuple_cell_count = *paddr_cells + *mc_addr_cells +
 827                                 *mc_size_cells;
 828
 829        tuple_len = range_tuple_cell_count * sizeof(__be32);
 830        if (ranges_len % tuple_len != 0) {
 831                dev_err(dev, "malformed ranges property '%pOFn'\n", mc_node);
 832                return -EINVAL;
 833        }
 834
 835        return ranges_len / tuple_len;
 836}
 837
 838static int get_mc_addr_translation_ranges(struct device *dev,
 839                                          struct fsl_mc_addr_translation_range
 840                                                **ranges,
 841                                          u8 *num_ranges)
 842{
 843        int ret;
 844        int paddr_cells;
 845        int mc_addr_cells;
 846        int mc_size_cells;
 847        int i;
 848        const __be32 *ranges_start;
 849        const __be32 *cell;
 850
 851        ret = parse_mc_ranges(dev,
 852                              &paddr_cells,
 853                              &mc_addr_cells,
 854                              &mc_size_cells,
 855                              &ranges_start);
 856        if (ret < 0)
 857                return ret;
 858
 859        *num_ranges = ret;
 860        if (!ret) {
 861                /*
 862                 * Missing or empty ranges property ("ranges;") for the
 863                 * 'fsl,qoriq-mc' node. In this case, identity mapping
 864                 * will be used.
 865                 */
 866                *ranges = NULL;
 867                return 0;
 868        }
 869
 870        *ranges = devm_kcalloc(dev, *num_ranges,
 871                               sizeof(struct fsl_mc_addr_translation_range),
 872                               GFP_KERNEL);
 873        if (!(*ranges))
 874                return -ENOMEM;
 875
 876        cell = ranges_start;
 877        for (i = 0; i < *num_ranges; ++i) {
 878                struct fsl_mc_addr_translation_range *range = &(*ranges)[i];
 879
 880                range->mc_region_type = of_read_number(cell, 1);
 881                range->start_mc_offset = of_read_number(cell + 1,
 882                                                        mc_addr_cells - 1);
 883                cell += mc_addr_cells;
 884                range->start_phys_addr = of_read_number(cell, paddr_cells);
 885                cell += paddr_cells;
 886                range->end_mc_offset = range->start_mc_offset +
 887                                     of_read_number(cell, mc_size_cells);
 888
 889                cell += mc_size_cells;
 890        }
 891
 892        return 0;
 893}
 894
 895/**
 896 * fsl_mc_bus_probe - callback invoked when the root MC bus is being
 897 * added
 898 */
 899static int fsl_mc_bus_probe(struct platform_device *pdev)
 900{
 901        struct fsl_mc_obj_desc obj_desc;
 902        int error;
 903        struct fsl_mc *mc;
 904        struct fsl_mc_device *mc_bus_dev = NULL;
 905        struct fsl_mc_io *mc_io = NULL;
 906        int container_id;
 907        phys_addr_t mc_portal_phys_addr;
 908        u32 mc_portal_size, mc_stream_id;
 909        struct resource *plat_res;
 910
 911        if (!iommu_present(&fsl_mc_bus_type))
 912                return -EPROBE_DEFER;
 913
 914        mc = devm_kzalloc(&pdev->dev, sizeof(*mc), GFP_KERNEL);
 915        if (!mc)
 916                return -ENOMEM;
 917
 918        platform_set_drvdata(pdev, mc);
 919
 920        plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 921        mc->fsl_mc_regs = devm_ioremap_resource(&pdev->dev, plat_res);
 922        if (IS_ERR(mc->fsl_mc_regs))
 923                return PTR_ERR(mc->fsl_mc_regs);
 924
 925        if (IS_ENABLED(CONFIG_ACPI) && !dev_of_node(&pdev->dev)) {
 926                mc_stream_id = readl(mc->fsl_mc_regs + FSL_MC_FAPR);
 927                /*
 928                 * HW ORs the PL and BMT bit, places the result in bit 15 of
 929                 * the StreamID and ORs in the ICID. Calculate it accordingly.
 930                 */
 931                mc_stream_id = (mc_stream_id & 0xffff) |
 932                                ((mc_stream_id & (MC_FAPR_PL | MC_FAPR_BMT)) ?
 933                                        0x4000 : 0);
 934                error = acpi_dma_configure_id(&pdev->dev, DEV_DMA_COHERENT,
 935                                              &mc_stream_id);
 936                if (error)
 937                        dev_warn(&pdev->dev, "failed to configure dma: %d.\n",
 938                                 error);
 939        }
 940
 941        /*
 942         * Get physical address of MC portal for the root DPRC:
 943         */
 944        plat_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 945        mc_portal_phys_addr = plat_res->start;
 946        mc_portal_size = resource_size(plat_res);
 947        error = fsl_create_mc_io(&pdev->dev, mc_portal_phys_addr,
 948                                 mc_portal_size, NULL,
 949                                 FSL_MC_IO_ATOMIC_CONTEXT_PORTAL, &mc_io);
 950        if (error < 0)
 951                return error;
 952
 953        error = mc_get_version(mc_io, 0, &mc_version);
 954        if (error != 0) {
 955                dev_err(&pdev->dev,
 956                        "mc_get_version() failed with error %d\n", error);
 957                goto error_cleanup_mc_io;
 958        }
 959
 960        dev_info(&pdev->dev, "MC firmware version: %u.%u.%u\n",
 961                 mc_version.major, mc_version.minor, mc_version.revision);
 962
 963        if (dev_of_node(&pdev->dev)) {
 964                error = get_mc_addr_translation_ranges(&pdev->dev,
 965                                                &mc->translation_ranges,
 966                                                &mc->num_translation_ranges);
 967                if (error < 0)
 968                        goto error_cleanup_mc_io;
 969        }
 970
 971        error = dprc_get_container_id(mc_io, 0, &container_id);
 972        if (error < 0) {
 973                dev_err(&pdev->dev,
 974                        "dprc_get_container_id() failed: %d\n", error);
 975                goto error_cleanup_mc_io;
 976        }
 977
 978        memset(&obj_desc, 0, sizeof(struct fsl_mc_obj_desc));
 979        error = dprc_get_api_version(mc_io, 0,
 980                                     &obj_desc.ver_major,
 981                                     &obj_desc.ver_minor);
 982        if (error < 0)
 983                goto error_cleanup_mc_io;
 984
 985        obj_desc.vendor = FSL_MC_VENDOR_FREESCALE;
 986        strcpy(obj_desc.type, "dprc");
 987        obj_desc.id = container_id;
 988        obj_desc.irq_count = 1;
 989        obj_desc.region_count = 0;
 990
 991        error = fsl_mc_device_add(&obj_desc, mc_io, &pdev->dev, &mc_bus_dev);
 992        if (error < 0)
 993                goto error_cleanup_mc_io;
 994
 995        mc->root_mc_bus_dev = mc_bus_dev;
 996        mc_bus_dev->dev.fwnode = pdev->dev.fwnode;
 997        return 0;
 998
 999error_cleanup_mc_io:
1000        fsl_destroy_mc_io(mc_io);
1001        return error;
1002}
1003
1004/**
1005 * fsl_mc_bus_remove - callback invoked when the root MC bus is being
1006 * removed
1007 */
1008static int fsl_mc_bus_remove(struct platform_device *pdev)
1009{
1010        struct fsl_mc *mc = platform_get_drvdata(pdev);
1011
1012        if (!fsl_mc_is_root_dprc(&mc->root_mc_bus_dev->dev))
1013                return -EINVAL;
1014
1015        fsl_mc_device_remove(mc->root_mc_bus_dev);
1016
1017        fsl_destroy_mc_io(mc->root_mc_bus_dev->mc_io);
1018        mc->root_mc_bus_dev->mc_io = NULL;
1019
1020        return 0;
1021}
1022
1023static const struct of_device_id fsl_mc_bus_match_table[] = {
1024        {.compatible = "fsl,qoriq-mc",},
1025        {},
1026};
1027
1028MODULE_DEVICE_TABLE(of, fsl_mc_bus_match_table);
1029
1030static const struct acpi_device_id fsl_mc_bus_acpi_match_table[] = {
1031        {"NXP0008", 0 },
1032        { }
1033};
1034MODULE_DEVICE_TABLE(acpi, fsl_mc_bus_acpi_match_table);
1035
1036static struct platform_driver fsl_mc_bus_driver = {
1037        .driver = {
1038                   .name = "fsl_mc_bus",
1039                   .pm = NULL,
1040                   .of_match_table = fsl_mc_bus_match_table,
1041                   .acpi_match_table = fsl_mc_bus_acpi_match_table,
1042                   },
1043        .probe = fsl_mc_bus_probe,
1044        .remove = fsl_mc_bus_remove,
1045};
1046
1047static int __init fsl_mc_bus_driver_init(void)
1048{
1049        int error;
1050
1051        error = bus_register(&fsl_mc_bus_type);
1052        if (error < 0) {
1053                pr_err("bus type registration failed: %d\n", error);
1054                goto error_cleanup_cache;
1055        }
1056
1057        error = platform_driver_register(&fsl_mc_bus_driver);
1058        if (error < 0) {
1059                pr_err("platform_driver_register() failed: %d\n", error);
1060                goto error_cleanup_bus;
1061        }
1062
1063        error = dprc_driver_init();
1064        if (error < 0)
1065                goto error_cleanup_driver;
1066
1067        error = fsl_mc_allocator_driver_init();
1068        if (error < 0)
1069                goto error_cleanup_dprc_driver;
1070
1071        return 0;
1072
1073error_cleanup_dprc_driver:
1074        dprc_driver_exit();
1075
1076error_cleanup_driver:
1077        platform_driver_unregister(&fsl_mc_bus_driver);
1078
1079error_cleanup_bus:
1080        bus_unregister(&fsl_mc_bus_type);
1081
1082error_cleanup_cache:
1083        return error;
1084}
1085postcore_initcall(fsl_mc_bus_driver_init);
1086