2 * Xen PCI - handle PCI (INTx) and MSI infrastructure calls for PV, HVM and
3 * initial domain support. We also handle the DSDT _PRT callbacks for GSI's
4 * used in HVM and initial domain mode (PV does not parse ACPI, so it has no
5 * concept of GSIs). Under PV we hook under the pnbbios API for IRQs and
6 * 0xcf8 PCI configuration read/write.
8 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
9 * Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
10 * Stefano Stabellini <stefano.stabellini@eu.citrix.com>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/pci.h>
15 #include <linux/acpi.h>
18 #include <asm/io_apic.h>
19 #include <asm/pci_x86.h>
21 #include <asm/xen/hypervisor.h>
23 #include <xen/features.h>
24 #include <xen/events.h>
25 #include <asm/xen/pci.h>
27 static int xen_pcifront_enable_irq(struct pci_dev
*dev
)
34 rc
= pci_read_config_byte(dev
, PCI_INTERRUPT_LINE
, &gsi
);
36 dev_warn(&dev
->dev
, "Xen PCI: failed to read interrupt line: %d\n",
41 rc
= xen_allocate_pirq_gsi(gsi
);
43 dev_warn(&dev
->dev
, "Xen PCI: failed to allocate a PIRQ for GSI%d: %d\n",
49 if (gsi
< NR_IRQS_LEGACY
)
52 rc
= xen_bind_pirq_gsi_to_irq(gsi
, pirq
, share
, "pcifront");
54 dev_warn(&dev
->dev
, "Xen PCI: failed to bind GSI%d (PIRQ%d) to IRQ: %d\n",
60 dev_info(&dev
->dev
, "Xen PCI mapped GSI%d to IRQ%d\n", gsi
, dev
->irq
);
65 static int xen_register_pirq(u32 gsi
, int gsi_override
, int triggering
,
68 int rc
, pirq
= -1, irq
= -1;
69 struct physdev_map_pirq map_irq
;
74 pirq
= xen_allocate_pirq_gsi(gsi
);
78 map_irq
.domid
= DOMID_SELF
;
79 map_irq
.type
= MAP_PIRQ_TYPE_GSI
;
83 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq
, &map_irq
);
85 printk(KERN_WARNING
"xen map irq failed %d\n", rc
);
89 if (triggering
== ACPI_EDGE_SENSITIVE
) {
94 name
= "ioapic-level";
97 if (gsi_override
>= 0)
100 irq
= xen_bind_pirq_gsi_to_irq(gsi
, map_irq
.pirq
, shareable
, name
);
104 printk(KERN_DEBUG
"xen: --> pirq=%d -> irq=%d (gsi=%d)\n", map_irq
.pirq
, irq
, gsi
);
109 static int acpi_register_gsi_xen_hvm(struct device
*dev
, u32 gsi
,
110 int trigger
, int polarity
)
112 if (!xen_hvm_domain())
115 return xen_register_pirq(gsi
, -1 /* no GSI override */,
116 trigger
, false /* no PIRQ allocation */);
119 #ifdef CONFIG_XEN_DOM0
120 static int xen_register_gsi(u32 gsi
, int gsi_override
, int triggering
, int polarity
)
123 struct physdev_setup_gsi setup_gsi
;
125 if (!xen_pv_domain())
128 printk(KERN_DEBUG
"xen: registering gsi %u triggering %d polarity %d\n",
129 gsi
, triggering
, polarity
);
131 irq
= xen_register_pirq(gsi
, gsi_override
, triggering
, true);
134 setup_gsi
.triggering
= (triggering
== ACPI_EDGE_SENSITIVE
? 0 : 1);
135 setup_gsi
.polarity
= (polarity
== ACPI_ACTIVE_HIGH
? 0 : 1);
137 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi
, &setup_gsi
);
139 printk(KERN_INFO
"Already setup the GSI :%d\n", gsi
);
141 printk(KERN_ERR
"Failed to setup GSI :%d, err_code:%d\n",
148 static int acpi_register_gsi_xen(struct device
*dev
, u32 gsi
,
149 int trigger
, int polarity
)
151 return xen_register_gsi(gsi
, -1 /* no GSI override */, trigger
, polarity
);
156 #if defined(CONFIG_PCI_MSI)
157 #include <linux/msi.h>
158 #include <asm/msidef.h>
160 struct xen_pci_frontend_ops
*xen_pci_frontend
;
161 EXPORT_SYMBOL_GPL(xen_pci_frontend
);
163 static int xen_setup_msi_irqs(struct pci_dev
*dev
, int nvec
, int type
)
166 struct msi_desc
*msidesc
;
169 v
= kzalloc(sizeof(int) * max(1, nvec
), GFP_KERNEL
);
173 if (type
== PCI_CAP_ID_MSIX
)
174 ret
= xen_pci_frontend_enable_msix(dev
, v
, nvec
);
176 ret
= xen_pci_frontend_enable_msi(dev
, v
);
180 list_for_each_entry(msidesc
, &dev
->msi_list
, list
) {
181 irq
= xen_bind_pirq_msi_to_irq(dev
, msidesc
, v
[i
], 0,
182 (type
== PCI_CAP_ID_MSIX
) ?
194 dev_err(&dev
->dev
, "Xen PCI frontend has not registered MSI/MSI-X support!\n");
200 #define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \
201 MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0))
203 static void xen_msi_compose_msg(struct pci_dev
*pdev
, unsigned int pirq
,
206 /* We set vector == 0 to tell the hypervisor we don't care about it,
207 * but we want a pirq setup instead.
208 * We use the dest_id field to pass the pirq that we want. */
209 msg
->address_hi
= MSI_ADDR_BASE_HI
| MSI_ADDR_EXT_DEST_ID(pirq
);
212 MSI_ADDR_DEST_MODE_PHYSICAL
|
213 MSI_ADDR_REDIRECTION_CPU
|
214 MSI_ADDR_DEST_ID(pirq
);
216 msg
->data
= XEN_PIRQ_MSI_DATA
;
219 static int xen_hvm_setup_msi_irqs(struct pci_dev
*dev
, int nvec
, int type
)
222 struct msi_desc
*msidesc
;
225 list_for_each_entry(msidesc
, &dev
->msi_list
, list
) {
226 __read_msi_msg(msidesc
, &msg
);
227 pirq
= MSI_ADDR_EXT_DEST_ID(msg
.address_hi
) |
228 ((msg
.address_lo
>> MSI_ADDR_DEST_ID_SHIFT
) & 0xff);
229 if (msg
.data
!= XEN_PIRQ_MSI_DATA
||
230 xen_irq_from_pirq(pirq
) < 0) {
231 pirq
= xen_allocate_pirq_msi(dev
, msidesc
);
234 xen_msi_compose_msg(dev
, pirq
, &msg
);
235 __write_msi_msg(msidesc
, &msg
);
236 dev_dbg(&dev
->dev
, "xen: msi bound to pirq=%d\n", pirq
);
239 "xen: msi already bound to pirq=%d\n", pirq
);
241 irq
= xen_bind_pirq_msi_to_irq(dev
, msidesc
, pirq
, 0,
242 (type
== PCI_CAP_ID_MSIX
) ?
248 "xen: msi --> pirq=%d --> irq=%d\n", pirq
, irq
);
254 "Xen PCI frontend has not registered MSI/MSI-X support!\n");
258 #ifdef CONFIG_XEN_DOM0
259 static int xen_initdom_setup_msi_irqs(struct pci_dev
*dev
, int nvec
, int type
)
262 struct msi_desc
*msidesc
;
264 list_for_each_entry(msidesc
, &dev
->msi_list
, list
) {
265 struct physdev_map_pirq map_irq
;
268 domid
= ret
= xen_find_device_domain_owner(dev
);
269 /* N.B. Casting int's -ENODEV to uint16_t results in 0xFFED,
270 * hence check ret value for < 0. */
274 memset(&map_irq
, 0, sizeof(map_irq
));
275 map_irq
.domid
= domid
;
276 map_irq
.type
= MAP_PIRQ_TYPE_MSI
;
279 map_irq
.bus
= dev
->bus
->number
;
280 map_irq
.devfn
= dev
->devfn
;
282 if (type
== PCI_CAP_ID_MSIX
) {
284 u32 table_offset
, bir
;
286 pos
= pci_find_capability(dev
, PCI_CAP_ID_MSIX
);
288 pci_read_config_dword(dev
, pos
+ PCI_MSIX_TABLE
,
290 bir
= (u8
)(table_offset
& PCI_MSIX_FLAGS_BIRMASK
);
292 map_irq
.table_base
= pci_resource_start(dev
, bir
);
293 map_irq
.entry_nr
= msidesc
->msi_attrib
.entry_nr
;
296 ret
= HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq
, &map_irq
);
298 dev_warn(&dev
->dev
, "xen map irq failed %d for %d domain\n",
303 ret
= xen_bind_pirq_msi_to_irq(dev
, msidesc
,
304 map_irq
.pirq
, map_irq
.index
,
305 (type
== PCI_CAP_ID_MSIX
) ?
317 static void xen_teardown_msi_irqs(struct pci_dev
*dev
)
319 struct msi_desc
*msidesc
;
321 msidesc
= list_entry(dev
->msi_list
.next
, struct msi_desc
, list
);
322 if (msidesc
->msi_attrib
.is_msix
)
323 xen_pci_frontend_disable_msix(dev
);
325 xen_pci_frontend_disable_msi(dev
);
327 /* Free the IRQ's and the msidesc using the generic code. */
328 default_teardown_msi_irqs(dev
);
331 static void xen_teardown_msi_irq(unsigned int irq
)
333 xen_destroy_irq(irq
);
338 int __init
pci_xen_init(void)
340 if (!xen_pv_domain() || xen_initial_domain())
343 printk(KERN_INFO
"PCI: setting up Xen PCI frontend stub\n");
345 pcibios_set_cache_line_size();
347 pcibios_enable_irq
= xen_pcifront_enable_irq
;
348 pcibios_disable_irq
= NULL
;
351 /* Keep ACPI out of the picture */
355 #ifdef CONFIG_PCI_MSI
356 x86_msi
.setup_msi_irqs
= xen_setup_msi_irqs
;
357 x86_msi
.teardown_msi_irq
= xen_teardown_msi_irq
;
358 x86_msi
.teardown_msi_irqs
= xen_teardown_msi_irqs
;
363 int __init
pci_xen_hvm_init(void)
365 if (!xen_feature(XENFEAT_hvm_pirqs
))
370 * We don't want to change the actual ACPI delivery model,
371 * just how GSIs get registered.
373 __acpi_register_gsi
= acpi_register_gsi_xen_hvm
;
376 #ifdef CONFIG_PCI_MSI
377 x86_msi
.setup_msi_irqs
= xen_hvm_setup_msi_irqs
;
378 x86_msi
.teardown_msi_irq
= xen_teardown_msi_irq
;
383 #ifdef CONFIG_XEN_DOM0
384 static __init
void xen_setup_acpi_sci(void)
387 int trigger
, polarity
;
388 int gsi
= acpi_sci_override_gsi
;
390 int gsi_override
= -1;
395 rc
= acpi_get_override_irq(gsi
, &trigger
, &polarity
);
397 printk(KERN_WARNING
"xen: acpi_get_override_irq failed for acpi"
398 " sci, rc=%d\n", rc
);
401 trigger
= trigger
? ACPI_LEVEL_SENSITIVE
: ACPI_EDGE_SENSITIVE
;
402 polarity
= polarity
? ACPI_ACTIVE_LOW
: ACPI_ACTIVE_HIGH
;
404 printk(KERN_INFO
"xen: sci override: global_irq=%d trigger=%d "
405 "polarity=%d\n", gsi
, trigger
, polarity
);
407 /* Before we bind the GSI to a Linux IRQ, check whether
408 * we need to override it with bus_irq (IRQ) value. Usually for
409 * IRQs below IRQ_LEGACY_IRQ this holds IRQ == GSI, as so:
410 * ACPI: INT_SRC_OVR (bus 0 bus_irq 9 global_irq 9 low level)
411 * but there are oddballs where the IRQ != GSI:
412 * ACPI: INT_SRC_OVR (bus 0 bus_irq 9 global_irq 20 low level)
413 * which ends up being: gsi_to_irq[9] == 20
414 * (which is what acpi_gsi_to_irq ends up calling when starting the
415 * the ACPI interpreter and keels over since IRQ 9 has not been
416 * setup as we had setup IRQ 20 for it).
418 /* Check whether the GSI != IRQ */
419 if (acpi_gsi_to_irq(gsi
, &irq
) == 0) {
420 if (irq
>= 0 && irq
!= gsi
)
421 /* Bugger, we MUST have that IRQ. */
425 gsi
= xen_register_gsi(gsi
, gsi_override
, trigger
, polarity
);
426 printk(KERN_INFO
"xen: acpi sci %d\n", gsi
);
431 int __init
pci_xen_initial_domain(void)
435 #ifdef CONFIG_PCI_MSI
436 x86_msi
.setup_msi_irqs
= xen_initdom_setup_msi_irqs
;
437 x86_msi
.teardown_msi_irq
= xen_teardown_msi_irq
;
439 xen_setup_acpi_sci();
440 __acpi_register_gsi
= acpi_register_gsi_xen
;
441 /* Pre-allocate legacy irqs */
442 for (irq
= 0; irq
< NR_IRQS_LEGACY
; irq
++) {
443 int trigger
, polarity
;
445 if (acpi_get_override_irq(irq
, &trigger
, &polarity
) == -1)
448 xen_register_pirq(irq
, -1 /* no GSI override */,
449 trigger
? ACPI_LEVEL_SENSITIVE
: ACPI_EDGE_SENSITIVE
,
450 true /* allocate IRQ */);
452 if (0 == nr_ioapics
) {
453 for (irq
= 0; irq
< NR_IRQS_LEGACY
; irq
++) {
454 pirq
= xen_allocate_pirq_gsi(irq
);
456 "Could not allocate PIRQ for legacy interrupt\n"))
458 irq
= xen_bind_pirq_gsi_to_irq(irq
, pirq
, 0, "xt-pic");
464 struct xen_device_domain_owner
{
467 struct list_head list
;
470 static DEFINE_SPINLOCK(dev_domain_list_spinlock
);
471 static struct list_head dev_domain_list
= LIST_HEAD_INIT(dev_domain_list
);
473 static struct xen_device_domain_owner
*find_device(struct pci_dev
*dev
)
475 struct xen_device_domain_owner
*owner
;
477 list_for_each_entry(owner
, &dev_domain_list
, list
) {
478 if (owner
->dev
== dev
)
484 int xen_find_device_domain_owner(struct pci_dev
*dev
)
486 struct xen_device_domain_owner
*owner
;
487 int domain
= -ENODEV
;
489 spin_lock(&dev_domain_list_spinlock
);
490 owner
= find_device(dev
);
492 domain
= owner
->domain
;
493 spin_unlock(&dev_domain_list_spinlock
);
496 EXPORT_SYMBOL_GPL(xen_find_device_domain_owner
);
498 int xen_register_device_domain_owner(struct pci_dev
*dev
, uint16_t domain
)
500 struct xen_device_domain_owner
*owner
;
502 owner
= kzalloc(sizeof(struct xen_device_domain_owner
), GFP_KERNEL
);
506 spin_lock(&dev_domain_list_spinlock
);
507 if (find_device(dev
)) {
508 spin_unlock(&dev_domain_list_spinlock
);
512 owner
->domain
= domain
;
514 list_add_tail(&owner
->list
, &dev_domain_list
);
515 spin_unlock(&dev_domain_list_spinlock
);
518 EXPORT_SYMBOL_GPL(xen_register_device_domain_owner
);
520 int xen_unregister_device_domain_owner(struct pci_dev
*dev
)
522 struct xen_device_domain_owner
*owner
;
524 spin_lock(&dev_domain_list_spinlock
);
525 owner
= find_device(dev
);
527 spin_unlock(&dev_domain_list_spinlock
);
530 list_del(&owner
->list
);
531 spin_unlock(&dev_domain_list_spinlock
);
535 EXPORT_SYMBOL_GPL(xen_unregister_device_domain_owner
);