linux/drivers/pci/pcie/portdrv_core.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * File:        portdrv_core.c
   4 * Purpose:     PCI Express Port Bus Driver's Core Functions
   5 *
   6 * Copyright (C) 2004 Intel
   7 * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
   8 */
   9
  10#include <linux/module.h>
  11#include <linux/pci.h>
  12#include <linux/kernel.h>
  13#include <linux/errno.h>
  14#include <linux/pm.h>
  15#include <linux/pm_runtime.h>
  16#include <linux/string.h>
  17#include <linux/slab.h>
  18#include <linux/pcieport_if.h>
  19#include <linux/aer.h>
  20
  21#include "../pci.h"
  22#include "portdrv.h"
  23
  24bool pciehp_msi_disabled;
  25
  26static int __init pciehp_setup(char *str)
  27{
  28        if (!strncmp(str, "nomsi", 5))
  29                pciehp_msi_disabled = true;
  30
  31        return 1;
  32}
  33__setup("pcie_hp=", pciehp_setup);
  34
  35/**
  36 * release_pcie_device - free PCI Express port service device structure
  37 * @dev: Port service device to release
  38 *
  39 * Invoked automatically when device is being removed in response to
  40 * device_unregister(dev).  Release all resources being claimed.
  41 */
  42static void release_pcie_device(struct device *dev)
  43{
  44        kfree(to_pcie_device(dev));
  45}
  46
  47/*
  48 * Fill in *pme, *aer, *dpc with the relevant Interrupt Message Numbers if
  49 * services are enabled in "mask".  Return the number of MSI/MSI-X vectors
  50 * required to accommodate the largest Message Number.
  51 */
  52static int pcie_message_numbers(struct pci_dev *dev, int mask,
  53                                u32 *pme, u32 *aer, u32 *dpc)
  54{
  55        u32 nvec = 0, pos, reg32;
  56        u16 reg16;
  57
  58        /*
  59         * The Interrupt Message Number indicates which vector is used, i.e.,
  60         * the MSI-X table entry or the MSI offset between the base Message
  61         * Data and the generated interrupt message.  See PCIe r3.1, sec
  62         * 7.8.2, 7.10.10, 7.31.2.
  63         */
  64
  65        if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
  66                pcie_capability_read_word(dev, PCI_EXP_FLAGS, &reg16);
  67                *pme = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
  68                nvec = *pme + 1;
  69        }
  70
  71        if (mask & PCIE_PORT_SERVICE_AER) {
  72                pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
  73                if (pos) {
  74                        pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS,
  75                                              &reg32);
  76                        *aer = (reg32 & PCI_ERR_ROOT_AER_IRQ) >> 27;
  77                        nvec = max(nvec, *aer + 1);
  78                }
  79        }
  80
  81        if (mask & PCIE_PORT_SERVICE_DPC) {
  82                pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC);
  83                if (pos) {
  84                        pci_read_config_word(dev, pos + PCI_EXP_DPC_CAP,
  85                                             &reg16);
  86                        *dpc = reg16 & PCI_EXP_DPC_IRQ;
  87                        nvec = max(nvec, *dpc + 1);
  88                }
  89        }
  90
  91        return nvec;
  92}
  93
  94/**
  95 * pcie_port_enable_irq_vec - try to set up MSI-X or MSI as interrupt mode
  96 * for given port
  97 * @dev: PCI Express port to handle
  98 * @irqs: Array of interrupt vectors to populate
  99 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
 100 *
 101 * Return value: 0 on success, error code on failure
 102 */
 103static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
 104{
 105        int nr_entries, nvec;
 106        u32 pme = 0, aer = 0, dpc = 0;
 107
 108        /* Allocate the maximum possible number of MSI/MSI-X vectors */
 109        nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSI_ENTRIES,
 110                        PCI_IRQ_MSIX | PCI_IRQ_MSI);
 111        if (nr_entries < 0)
 112                return nr_entries;
 113
 114        /* See how many and which Interrupt Message Numbers we actually use */
 115        nvec = pcie_message_numbers(dev, mask, &pme, &aer, &dpc);
 116        if (nvec > nr_entries) {
 117                pci_free_irq_vectors(dev);
 118                return -EIO;
 119        }
 120
 121        /*
 122         * If we allocated more than we need, free them and reallocate fewer.
 123         *
 124         * Reallocating may change the specific vectors we get, so
 125         * pci_irq_vector() must be done *after* the reallocation.
 126         *
 127         * If we're using MSI, hardware is *allowed* to change the Interrupt
 128         * Message Numbers when we free and reallocate the vectors, but we
 129         * assume it won't because we allocate enough vectors for the
 130         * biggest Message Number we found.
 131         */
 132        if (nvec != nr_entries) {
 133                pci_free_irq_vectors(dev);
 134
 135                nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
 136                                PCI_IRQ_MSIX | PCI_IRQ_MSI);
 137                if (nr_entries < 0)
 138                        return nr_entries;
 139        }
 140
 141        /* PME and hotplug share an MSI/MSI-X vector */
 142        if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
 143                irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pci_irq_vector(dev, pme);
 144                irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pci_irq_vector(dev, pme);
 145        }
 146
 147        if (mask & PCIE_PORT_SERVICE_AER)
 148                irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, aer);
 149
 150        if (mask & PCIE_PORT_SERVICE_DPC)
 151                irqs[PCIE_PORT_SERVICE_DPC_SHIFT] = pci_irq_vector(dev, dpc);
 152
 153        return 0;
 154}
 155
 156/**
 157 * pcie_init_service_irqs - initialize irqs for PCI Express port services
 158 * @dev: PCI Express port to handle
 159 * @irqs: Array of irqs to populate
 160 * @mask: Bitmask of port capabilities returned by get_port_device_capability()
 161 *
 162 * Return value: Interrupt mode associated with the port
 163 */
 164static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
 165{
 166        int ret, i;
 167
 168        for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
 169                irqs[i] = -1;
 170
 171        /*
 172         * If we support PME or hotplug, but we can't use MSI/MSI-X for
 173         * them, we have to fall back to INTx or other interrupts, e.g., a
 174         * system shared interrupt.
 175         */
 176        if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
 177                goto legacy_irq;
 178
 179        if ((mask & PCIE_PORT_SERVICE_HP) && pciehp_no_msi())
 180                goto legacy_irq;
 181
 182        /* Try to use MSI-X or MSI if supported */
 183        if (pcie_port_enable_irq_vec(dev, irqs, mask) == 0)
 184                return 0;
 185
 186legacy_irq:
 187        /* fall back to legacy IRQ */
 188        ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY);
 189        if (ret < 0)
 190                return -ENODEV;
 191
 192        for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
 193                if (i != PCIE_PORT_SERVICE_VC_SHIFT)
 194                        irqs[i] = pci_irq_vector(dev, 0);
 195        }
 196
 197        return 0;
 198}
 199
 200/**
 201 * get_port_device_capability - discover capabilities of a PCI Express port
 202 * @dev: PCI Express port to examine
 203 *
 204 * The capabilities are read from the port's PCI Express configuration registers
 205 * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
 206 * 7.9 - 7.11.
 207 *
 208 * Return value: Bitmask of discovered port capabilities
 209 */
 210static int get_port_device_capability(struct pci_dev *dev)
 211{
 212        int services = 0;
 213        int cap_mask = 0;
 214
 215        if (pcie_ports_disabled)
 216                return 0;
 217
 218        cap_mask = PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP
 219                        | PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_DPC;
 220        if (pci_aer_available())
 221                cap_mask |= PCIE_PORT_SERVICE_AER;
 222
 223        if (pcie_ports_auto)
 224                pcie_port_platform_notify(dev, &cap_mask);
 225
 226        /* Hot-Plug Capable */
 227        if ((cap_mask & PCIE_PORT_SERVICE_HP) && dev->is_hotplug_bridge) {
 228                services |= PCIE_PORT_SERVICE_HP;
 229                /*
 230                 * Disable hot-plug interrupts in case they have been enabled
 231                 * by the BIOS and the hot-plug service driver is not loaded.
 232                 */
 233                pcie_capability_clear_word(dev, PCI_EXP_SLTCTL,
 234                          PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
 235        }
 236        /* AER capable */
 237        if ((cap_mask & PCIE_PORT_SERVICE_AER)
 238            && pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)) {
 239                services |= PCIE_PORT_SERVICE_AER;
 240                /*
 241                 * Disable AER on this port in case it's been enabled by the
 242                 * BIOS (the AER service driver will enable it when necessary).
 243                 */
 244                pci_disable_pcie_error_reporting(dev);
 245        }
 246        /* VC support */
 247        if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_VC))
 248                services |= PCIE_PORT_SERVICE_VC;
 249        /* Root ports are capable of generating PME too */
 250        if ((cap_mask & PCIE_PORT_SERVICE_PME)
 251            && pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
 252                services |= PCIE_PORT_SERVICE_PME;
 253                /*
 254                 * Disable PME interrupt on this port in case it's been enabled
 255                 * by the BIOS (the PME service driver will enable it when
 256                 * necessary).
 257                 */
 258                pcie_pme_interrupt_enable(dev, false);
 259        }
 260        if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC))
 261                services |= PCIE_PORT_SERVICE_DPC;
 262
 263        return services;
 264}
 265
 266/**
 267 * pcie_device_init - allocate and initialize PCI Express port service device
 268 * @pdev: PCI Express port to associate the service device with
 269 * @service: Type of service to associate with the service device
 270 * @irq: Interrupt vector to associate with the service device
 271 */
 272static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
 273{
 274        int retval;
 275        struct pcie_device *pcie;
 276        struct device *device;
 277
 278        pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
 279        if (!pcie)
 280                return -ENOMEM;
 281        pcie->port = pdev;
 282        pcie->irq = irq;
 283        pcie->service = service;
 284
 285        /* Initialize generic device interface */
 286        device = &pcie->device;
 287        device->bus = &pcie_port_bus_type;
 288        device->release = release_pcie_device;  /* callback to free pcie dev */
 289        dev_set_name(device, "%s:pcie%03x",
 290                     pci_name(pdev),
 291                     get_descriptor_id(pci_pcie_type(pdev), service));
 292        device->parent = &pdev->dev;
 293        device_enable_async_suspend(device);
 294
 295        retval = device_register(device);
 296        if (retval) {
 297                put_device(device);
 298                return retval;
 299        }
 300
 301        pm_runtime_no_callbacks(device);
 302
 303        return 0;
 304}
 305
 306/**
 307 * pcie_port_device_register - register PCI Express port
 308 * @dev: PCI Express port to register
 309 *
 310 * Allocate the port extension structure and register services associated with
 311 * the port.
 312 */
 313int pcie_port_device_register(struct pci_dev *dev)
 314{
 315        int status, capabilities, i, nr_service;
 316        int irqs[PCIE_PORT_DEVICE_MAXSERVICES];
 317
 318        /* Enable PCI Express port device */
 319        status = pci_enable_device(dev);
 320        if (status)
 321                return status;
 322
 323        /* Get and check PCI Express port services */
 324        capabilities = get_port_device_capability(dev);
 325        if (!capabilities)
 326                return 0;
 327
 328        pci_set_master(dev);
 329        /*
 330         * Initialize service irqs. Don't use service devices that
 331         * require interrupts if there is no way to generate them.
 332         * However, some drivers may have a polling mode (e.g. pciehp_poll_mode)
 333         * that can be used in the absence of irqs.  Allow them to determine
 334         * if that is to be used.
 335         */
 336        status = pcie_init_service_irqs(dev, irqs, capabilities);
 337        if (status) {
 338                capabilities &= PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_HP;
 339                if (!capabilities)
 340                        goto error_disable;
 341        }
 342
 343        /* Allocate child services if any */
 344        status = -ENODEV;
 345        nr_service = 0;
 346        for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
 347                int service = 1 << i;
 348                if (!(capabilities & service))
 349                        continue;
 350                if (!pcie_device_init(dev, service, irqs[i]))
 351                        nr_service++;
 352        }
 353        if (!nr_service)
 354                goto error_cleanup_irqs;
 355
 356        return 0;
 357
 358error_cleanup_irqs:
 359        pci_free_irq_vectors(dev);
 360error_disable:
 361        pci_disable_device(dev);
 362        return status;
 363}
 364
 365#ifdef CONFIG_PM
 366static int suspend_iter(struct device *dev, void *data)
 367{
 368        struct pcie_port_service_driver *service_driver;
 369
 370        if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
 371                service_driver = to_service_driver(dev->driver);
 372                if (service_driver->suspend)
 373                        service_driver->suspend(to_pcie_device(dev));
 374        }
 375        return 0;
 376}
 377
 378/**
 379 * pcie_port_device_suspend - suspend port services associated with a PCIe port
 380 * @dev: PCI Express port to handle
 381 */
 382int pcie_port_device_suspend(struct device *dev)
 383{
 384        return device_for_each_child(dev, NULL, suspend_iter);
 385}
 386
 387static int resume_iter(struct device *dev, void *data)
 388{
 389        struct pcie_port_service_driver *service_driver;
 390
 391        if ((dev->bus == &pcie_port_bus_type) &&
 392            (dev->driver)) {
 393                service_driver = to_service_driver(dev->driver);
 394                if (service_driver->resume)
 395                        service_driver->resume(to_pcie_device(dev));
 396        }
 397        return 0;
 398}
 399
 400/**
 401 * pcie_port_device_resume - resume port services associated with a PCIe port
 402 * @dev: PCI Express port to handle
 403 */
 404int pcie_port_device_resume(struct device *dev)
 405{
 406        return device_for_each_child(dev, NULL, resume_iter);
 407}
 408#endif /* PM */
 409
 410static int remove_iter(struct device *dev, void *data)
 411{
 412        if (dev->bus == &pcie_port_bus_type)
 413                device_unregister(dev);
 414        return 0;
 415}
 416
 417/**
 418 * pcie_port_device_remove - unregister PCI Express port service devices
 419 * @dev: PCI Express port the service devices to unregister are associated with
 420 *
 421 * Remove PCI Express port service devices associated with given port and
 422 * disable MSI-X or MSI for the port.
 423 */
 424void pcie_port_device_remove(struct pci_dev *dev)
 425{
 426        device_for_each_child(&dev->dev, NULL, remove_iter);
 427        pci_free_irq_vectors(dev);
 428        pci_disable_device(dev);
 429}
 430
 431/**
 432 * pcie_port_probe_service - probe driver for given PCI Express port service
 433 * @dev: PCI Express port service device to probe against
 434 *
 435 * If PCI Express port service driver is registered with
 436 * pcie_port_service_register(), this function will be called by the driver core
 437 * whenever match is found between the driver and a port service device.
 438 */
 439static int pcie_port_probe_service(struct device *dev)
 440{
 441        struct pcie_device *pciedev;
 442        struct pcie_port_service_driver *driver;
 443        int status;
 444
 445        if (!dev || !dev->driver)
 446                return -ENODEV;
 447
 448        driver = to_service_driver(dev->driver);
 449        if (!driver || !driver->probe)
 450                return -ENODEV;
 451
 452        pciedev = to_pcie_device(dev);
 453        status = driver->probe(pciedev);
 454        if (status)
 455                return status;
 456
 457        get_device(dev);
 458        return 0;
 459}
 460
 461/**
 462 * pcie_port_remove_service - detach driver from given PCI Express port service
 463 * @dev: PCI Express port service device to handle
 464 *
 465 * If PCI Express port service driver is registered with
 466 * pcie_port_service_register(), this function will be called by the driver core
 467 * when device_unregister() is called for the port service device associated
 468 * with the driver.
 469 */
 470static int pcie_port_remove_service(struct device *dev)
 471{
 472        struct pcie_device *pciedev;
 473        struct pcie_port_service_driver *driver;
 474
 475        if (!dev || !dev->driver)
 476                return 0;
 477
 478        pciedev = to_pcie_device(dev);
 479        driver = to_service_driver(dev->driver);
 480        if (driver && driver->remove) {
 481                driver->remove(pciedev);
 482                put_device(dev);
 483        }
 484        return 0;
 485}
 486
 487/**
 488 * pcie_port_shutdown_service - shut down given PCI Express port service
 489 * @dev: PCI Express port service device to handle
 490 *
 491 * If PCI Express port service driver is registered with
 492 * pcie_port_service_register(), this function will be called by the driver core
 493 * when device_shutdown() is called for the port service device associated
 494 * with the driver.
 495 */
 496static void pcie_port_shutdown_service(struct device *dev) {}
 497
 498/**
 499 * pcie_port_service_register - register PCI Express port service driver
 500 * @new: PCI Express port service driver to register
 501 */
 502int pcie_port_service_register(struct pcie_port_service_driver *new)
 503{
 504        if (pcie_ports_disabled)
 505                return -ENODEV;
 506
 507        new->driver.name = new->name;
 508        new->driver.bus = &pcie_port_bus_type;
 509        new->driver.probe = pcie_port_probe_service;
 510        new->driver.remove = pcie_port_remove_service;
 511        new->driver.shutdown = pcie_port_shutdown_service;
 512
 513        return driver_register(&new->driver);
 514}
 515EXPORT_SYMBOL(pcie_port_service_register);
 516
 517/**
 518 * pcie_port_service_unregister - unregister PCI Express port service driver
 519 * @drv: PCI Express port service driver to unregister
 520 */
 521void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
 522{
 523        driver_unregister(&drv->driver);
 524}
 525EXPORT_SYMBOL(pcie_port_service_unregister);
 526