linux/drivers/iommu/irq_remapping.c
<<
>>
Prefs
   1#include <linux/seq_file.h>
   2#include <linux/cpumask.h>
   3#include <linux/kernel.h>
   4#include <linux/string.h>
   5#include <linux/errno.h>
   6#include <linux/msi.h>
   7#include <linux/irq.h>
   8#include <linux/pci.h>
   9
  10#include <asm/hw_irq.h>
  11#include <asm/irq_remapping.h>
  12#include <asm/processor.h>
  13#include <asm/x86_init.h>
  14#include <asm/apic.h>
  15
  16#include "irq_remapping.h"
  17
  18int irq_remapping_enabled;
  19
  20int disable_irq_remap;
  21int disable_sourceid_checking;
  22int no_x2apic_optout;
  23
  24static struct irq_remap_ops *remap_ops;
  25
  26static int msi_alloc_remapped_irq(struct pci_dev *pdev, int irq, int nvec);
  27static int msi_setup_remapped_irq(struct pci_dev *pdev, unsigned int irq,
  28                                  int index, int sub_handle);
  29static int set_remapped_irq_affinity(struct irq_data *data,
  30                                     const struct cpumask *mask,
  31                                     bool force);
  32
  33static bool irq_remapped(struct irq_cfg *cfg)
  34{
  35        return (cfg->remapped == 1);
  36}
  37
  38static void irq_remapping_disable_io_apic(void)
  39{
  40        /*
  41         * With interrupt-remapping, for now we will use virtual wire A
  42         * mode, as virtual wire B is little complex (need to configure
  43         * both IOAPIC RTE as well as interrupt-remapping table entry).
  44         * As this gets called during crash dump, keep this simple for
  45         * now.
  46         */
  47        if (cpu_has_apic || apic_from_smp_config())
  48                disconnect_bsp_APIC(0);
  49}
  50
  51static int do_setup_msi_irqs(struct pci_dev *dev, int nvec)
  52{
  53        int node, ret, sub_handle, index = 0;
  54        unsigned int irq;
  55        struct msi_desc *msidesc;
  56
  57        nvec = __roundup_pow_of_two(nvec);
  58
  59        WARN_ON(!list_is_singular(&dev->msi_list));
  60        msidesc = list_entry(dev->msi_list.next, struct msi_desc, list);
  61        WARN_ON(msidesc->irq);
  62        WARN_ON(msidesc->msi_attrib.multiple);
  63
  64        node = dev_to_node(&dev->dev);
  65        irq = __create_irqs(get_nr_irqs_gsi(), nvec, node);
  66        if (irq == 0)
  67                return -ENOSPC;
  68
  69        msidesc->msi_attrib.multiple = ilog2(nvec);
  70        for (sub_handle = 0; sub_handle < nvec; sub_handle++) {
  71                if (!sub_handle) {
  72                        index = msi_alloc_remapped_irq(dev, irq, nvec);
  73                        if (index < 0) {
  74                                ret = index;
  75                                goto error;
  76                        }
  77                } else {
  78                        ret = msi_setup_remapped_irq(dev, irq + sub_handle,
  79                                                     index, sub_handle);
  80                        if (ret < 0)
  81                                goto error;
  82                }
  83                ret = setup_msi_irq(dev, msidesc, irq, sub_handle);
  84                if (ret < 0)
  85                        goto error;
  86        }
  87        return 0;
  88
  89error:
  90        destroy_irqs(irq, nvec);
  91
  92        /*
  93         * Restore altered MSI descriptor fields and prevent just destroyed
  94         * IRQs from tearing down again in default_teardown_msi_irqs()
  95         */
  96        msidesc->irq = 0;
  97        msidesc->msi_attrib.multiple = 0;
  98
  99        return ret;
 100}
 101
 102static int do_setup_msix_irqs(struct pci_dev *dev, int nvec)
 103{
 104        int node, ret, sub_handle, index = 0;
 105        struct msi_desc *msidesc;
 106        unsigned int irq;
 107
 108        node            = dev_to_node(&dev->dev);
 109        irq             = get_nr_irqs_gsi();
 110        sub_handle      = 0;
 111
 112        list_for_each_entry(msidesc, &dev->msi_list, list) {
 113
 114                irq = create_irq_nr(irq, node);
 115                if (irq == 0)
 116                        return -1;
 117
 118                if (sub_handle == 0)
 119                        ret = index = msi_alloc_remapped_irq(dev, irq, nvec);
 120                else
 121                        ret = msi_setup_remapped_irq(dev, irq, index, sub_handle);
 122
 123                if (ret < 0)
 124                        goto error;
 125
 126                ret = setup_msi_irq(dev, msidesc, irq, 0);
 127                if (ret < 0)
 128                        goto error;
 129
 130                sub_handle += 1;
 131                irq        += 1;
 132        }
 133
 134        return 0;
 135
 136error:
 137        destroy_irq(irq);
 138        return ret;
 139}
 140
 141static int irq_remapping_setup_msi_irqs(struct pci_dev *dev,
 142                                        int nvec, int type)
 143{
 144        if (type == PCI_CAP_ID_MSI)
 145                return do_setup_msi_irqs(dev, nvec);
 146        else
 147                return do_setup_msix_irqs(dev, nvec);
 148}
 149
 150void eoi_ioapic_pin_remapped(int apic, int pin, int vector)
 151{
 152        /*
 153         * Intr-remapping uses pin number as the virtual vector
 154         * in the RTE. Actual vector is programmed in
 155         * intr-remapping table entry. Hence for the io-apic
 156         * EOI we use the pin number.
 157         */
 158        io_apic_eoi(apic, pin);
 159}
 160
 161static void __init irq_remapping_modify_x86_ops(void)
 162{
 163        x86_io_apic_ops.disable         = irq_remapping_disable_io_apic;
 164        x86_io_apic_ops.set_affinity    = set_remapped_irq_affinity;
 165        x86_io_apic_ops.setup_entry     = setup_ioapic_remapped_entry;
 166        x86_io_apic_ops.eoi_ioapic_pin  = eoi_ioapic_pin_remapped;
 167        x86_msi.setup_msi_irqs          = irq_remapping_setup_msi_irqs;
 168        x86_msi.setup_hpet_msi          = setup_hpet_msi_remapped;
 169        x86_msi.compose_msi_msg         = compose_remapped_msi_msg;
 170}
 171
 172static __init int setup_nointremap(char *str)
 173{
 174        disable_irq_remap = 1;
 175        return 0;
 176}
 177early_param("nointremap", setup_nointremap);
 178
 179static __init int setup_irqremap(char *str)
 180{
 181        if (!str)
 182                return -EINVAL;
 183
 184        while (*str) {
 185                if (!strncmp(str, "on", 2))
 186                        disable_irq_remap = 0;
 187                else if (!strncmp(str, "off", 3))
 188                        disable_irq_remap = 1;
 189                else if (!strncmp(str, "nosid", 5))
 190                        disable_sourceid_checking = 1;
 191                else if (!strncmp(str, "no_x2apic_optout", 16))
 192                        no_x2apic_optout = 1;
 193
 194                str += strcspn(str, ",");
 195                while (*str == ',')
 196                        str++;
 197        }
 198
 199        return 0;
 200}
 201early_param("intremap", setup_irqremap);
 202
 203void __init setup_irq_remapping_ops(void)
 204{
 205        remap_ops = &intel_irq_remap_ops;
 206
 207#ifdef CONFIG_AMD_IOMMU
 208        if (amd_iommu_irq_ops.prepare() == 0)
 209                remap_ops = &amd_iommu_irq_ops;
 210#endif
 211}
 212
 213int irq_remapping_supported(void)
 214{
 215        if (disable_irq_remap)
 216                return 0;
 217
 218        if (!remap_ops || !remap_ops->supported)
 219                return 0;
 220
 221        return remap_ops->supported();
 222}
 223
 224int __init irq_remapping_prepare(void)
 225{
 226        if (!remap_ops || !remap_ops->prepare)
 227                return -ENODEV;
 228
 229        return remap_ops->prepare();
 230}
 231
 232int __init irq_remapping_enable(void)
 233{
 234        int ret;
 235
 236        if (!remap_ops || !remap_ops->enable)
 237                return -ENODEV;
 238
 239        ret = remap_ops->enable();
 240
 241        if (irq_remapping_enabled)
 242                irq_remapping_modify_x86_ops();
 243
 244        return ret;
 245}
 246
 247void irq_remapping_disable(void)
 248{
 249        if (!irq_remapping_enabled ||
 250            !remap_ops ||
 251            !remap_ops->disable)
 252                return;
 253
 254        remap_ops->disable();
 255}
 256
 257int irq_remapping_reenable(int mode)
 258{
 259        if (!irq_remapping_enabled ||
 260            !remap_ops ||
 261            !remap_ops->reenable)
 262                return 0;
 263
 264        return remap_ops->reenable(mode);
 265}
 266
 267int __init irq_remap_enable_fault_handling(void)
 268{
 269        if (!irq_remapping_enabled)
 270                return 0;
 271
 272        if (!remap_ops || !remap_ops->enable_faulting)
 273                return -ENODEV;
 274
 275        return remap_ops->enable_faulting();
 276}
 277
 278int setup_ioapic_remapped_entry(int irq,
 279                                struct IO_APIC_route_entry *entry,
 280                                unsigned int destination, int vector,
 281                                struct io_apic_irq_attr *attr)
 282{
 283        if (!remap_ops || !remap_ops->setup_ioapic_entry)
 284                return -ENODEV;
 285
 286        return remap_ops->setup_ioapic_entry(irq, entry, destination,
 287                                             vector, attr);
 288}
 289
 290int set_remapped_irq_affinity(struct irq_data *data, const struct cpumask *mask,
 291                              bool force)
 292{
 293        if (!config_enabled(CONFIG_SMP) || !remap_ops ||
 294            !remap_ops->set_affinity)
 295                return 0;
 296
 297        return remap_ops->set_affinity(data, mask, force);
 298}
 299
 300void free_remapped_irq(int irq)
 301{
 302        struct irq_cfg *cfg = irq_get_chip_data(irq);
 303
 304        if (!remap_ops || !remap_ops->free_irq)
 305                return;
 306
 307        if (irq_remapped(cfg))
 308                remap_ops->free_irq(irq);
 309}
 310
 311void compose_remapped_msi_msg(struct pci_dev *pdev,
 312                              unsigned int irq, unsigned int dest,
 313                              struct msi_msg *msg, u8 hpet_id)
 314{
 315        struct irq_cfg *cfg = irq_get_chip_data(irq);
 316
 317        if (!irq_remapped(cfg))
 318                native_compose_msi_msg(pdev, irq, dest, msg, hpet_id);
 319        else if (remap_ops && remap_ops->compose_msi_msg)
 320                remap_ops->compose_msi_msg(pdev, irq, dest, msg, hpet_id);
 321}
 322
 323static int msi_alloc_remapped_irq(struct pci_dev *pdev, int irq, int nvec)
 324{
 325        if (!remap_ops || !remap_ops->msi_alloc_irq)
 326                return -ENODEV;
 327
 328        return remap_ops->msi_alloc_irq(pdev, irq, nvec);
 329}
 330
 331static int msi_setup_remapped_irq(struct pci_dev *pdev, unsigned int irq,
 332                                  int index, int sub_handle)
 333{
 334        if (!remap_ops || !remap_ops->msi_setup_irq)
 335                return -ENODEV;
 336
 337        return remap_ops->msi_setup_irq(pdev, irq, index, sub_handle);
 338}
 339
 340int setup_hpet_msi_remapped(unsigned int irq, unsigned int id)
 341{
 342        if (!remap_ops || !remap_ops->setup_hpet_msi)
 343                return -ENODEV;
 344
 345        return remap_ops->setup_hpet_msi(irq, id);
 346}
 347
 348void panic_if_irq_remap(const char *msg)
 349{
 350        if (irq_remapping_enabled)
 351                panic(msg);
 352}
 353
 354static void ir_ack_apic_edge(struct irq_data *data)
 355{
 356        ack_APIC_irq();
 357}
 358
 359static void ir_ack_apic_level(struct irq_data *data)
 360{
 361        ack_APIC_irq();
 362        eoi_ioapic_irq(data->irq, data->chip_data);
 363}
 364
 365static void ir_print_prefix(struct irq_data *data, struct seq_file *p)
 366{
 367        seq_printf(p, " IR-%s", data->chip->name);
 368}
 369
 370void irq_remap_modify_chip_defaults(struct irq_chip *chip)
 371{
 372        chip->irq_print_chip = ir_print_prefix;
 373        chip->irq_ack = ir_ack_apic_edge;
 374        chip->irq_eoi = ir_ack_apic_level;
 375        chip->irq_set_affinity = x86_io_apic_ops.set_affinity;
 376}
 377
 378bool setup_remapped_irq(int irq, struct irq_cfg *cfg, struct irq_chip *chip)
 379{
 380        if (!irq_remapped(cfg))
 381                return false;
 382        irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
 383        irq_remap_modify_chip_defaults(chip);
 384        return true;
 385}
 386
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.