blob: 265fa8814ccdcabbc41f78dcad0d641343d89a47 [file] [log] [blame]
Alex Nixonb5401a92010-03-18 16:31:34 -04001/*
2 * Xen PCI Frontend Stub - puts some "dummy" functions in to the Linux
3 * x86 PCI core to support the Xen PCI Frontend
4 *
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
6 */
7#include <linux/module.h>
8#include <linux/init.h>
9#include <linux/pci.h>
10#include <linux/acpi.h>
11
12#include <linux/io.h>
Stefano Stabellini0e058e52010-10-21 17:40:08 +010013#include <asm/io_apic.h>
Alex Nixonb5401a92010-03-18 16:31:34 -040014#include <asm/pci_x86.h>
15
16#include <asm/xen/hypervisor.h>
17
Stefano Stabellini3942b742010-06-24 17:50:18 +010018#include <xen/features.h>
Alex Nixonb5401a92010-03-18 16:31:34 -040019#include <xen/events.h>
20#include <asm/xen/pci.h>
21
Stefano Stabellini42a1de52010-06-24 16:42:04 +010022#ifdef CONFIG_ACPI
Ian Campbell9a626612011-02-18 16:43:30 +000023static int acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi,
24 int trigger, int polarity)
Stefano Stabellini42a1de52010-06-24 16:42:04 +010025{
26 int rc, irq;
27 struct physdev_map_pirq map_irq;
28 int shareable = 0;
29 char *name;
30
31 if (!xen_hvm_domain())
32 return -1;
33
34 map_irq.domid = DOMID_SELF;
35 map_irq.type = MAP_PIRQ_TYPE_GSI;
36 map_irq.index = gsi;
37 map_irq.pirq = -1;
38
39 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
40 if (rc) {
41 printk(KERN_WARNING "xen map irq failed %d\n", rc);
42 return -1;
43 }
44
Ian Campbell9a626612011-02-18 16:43:30 +000045 if (trigger == ACPI_EDGE_SENSITIVE) {
Stefano Stabellini42a1de52010-06-24 16:42:04 +010046 shareable = 0;
47 name = "ioapic-edge";
48 } else {
49 shareable = 1;
50 name = "ioapic-level";
51 }
52
Ian Campbellf4d06352011-03-10 16:08:07 +000053 irq = xen_bind_pirq_gsi_to_irq(gsi, map_irq.pirq, shareable, name);
Stefano Stabellini42a1de52010-06-24 16:42:04 +010054
55 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
56
57 return irq;
58}
59#endif
60
Alex Nixonb5401a92010-03-18 16:31:34 -040061#if defined(CONFIG_PCI_MSI)
62#include <linux/msi.h>
Stefano Stabellini809f9262010-07-01 17:10:39 +010063#include <asm/msidef.h>
Alex Nixonb5401a92010-03-18 16:31:34 -040064
65struct xen_pci_frontend_ops *xen_pci_frontend;
66EXPORT_SYMBOL_GPL(xen_pci_frontend);
67
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +000068#define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \
69 MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0))
70
Stefano Stabellini809f9262010-07-01 17:10:39 +010071static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq,
72 struct msi_msg *msg)
73{
74 /* We set vector == 0 to tell the hypervisor we don't care about it,
75 * but we want a pirq setup instead.
76 * We use the dest_id field to pass the pirq that we want. */
77 msg->address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(pirq);
78 msg->address_lo =
79 MSI_ADDR_BASE_LO |
80 MSI_ADDR_DEST_MODE_PHYSICAL |
81 MSI_ADDR_REDIRECTION_CPU |
82 MSI_ADDR_DEST_ID(pirq);
83
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +000084 msg->data = XEN_PIRQ_MSI_DATA;
Stefano Stabellini809f9262010-07-01 17:10:39 +010085}
86
87static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
88{
Ian Campbellbf480d92011-02-18 16:43:32 +000089 int irq, pirq;
Stefano Stabellini809f9262010-07-01 17:10:39 +010090 struct msi_desc *msidesc;
91 struct msi_msg msg;
92
93 list_for_each_entry(msidesc, &dev->msi_list, list) {
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +000094 __read_msi_msg(msidesc, &msg);
95 pirq = MSI_ADDR_EXT_DEST_ID(msg.address_hi) |
96 ((msg.address_lo >> MSI_ADDR_DEST_ID_SHIFT) & 0xff);
Ian Campbellbf480d92011-02-18 16:43:32 +000097 if (msg.data != XEN_PIRQ_MSI_DATA ||
98 xen_irq_from_pirq(pirq) < 0) {
99 pirq = xen_allocate_pirq_msi(dev, msidesc);
100 if (pirq < 0)
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000101 goto error;
Ian Campbellbf480d92011-02-18 16:43:32 +0000102 xen_msi_compose_msg(dev, pirq, &msg);
103 __write_msi_msg(msidesc, &msg);
104 dev_dbg(&dev->dev, "xen: msi bound to pirq=%d\n", pirq);
105 } else {
106 dev_dbg(&dev->dev,
107 "xen: msi already bound to pirq=%d\n", pirq);
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000108 }
Ian Campbellca1d8fe2011-02-18 16:43:36 +0000109 irq = xen_bind_pirq_msi_to_irq(dev, msidesc, pirq, 0,
Ian Campbellbf480d92011-02-18 16:43:32 +0000110 (type == PCI_CAP_ID_MSIX) ?
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400111 "msi-x" : "msi",
112 DOMID_SELF);
Ian Campbellbf480d92011-02-18 16:43:32 +0000113 if (irq < 0)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100114 goto error;
Ian Campbellbf480d92011-02-18 16:43:32 +0000115 dev_dbg(&dev->dev,
116 "xen: msi --> pirq=%d --> irq=%d\n", pirq, irq);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100117 }
118 return 0;
119
Stefano Stabellini809f9262010-07-01 17:10:39 +0100120error:
Ian Campbellbf480d92011-02-18 16:43:32 +0000121 dev_err(&dev->dev,
122 "Xen PCI frontend has not registered MSI/MSI-X support!\n");
123 return -ENODEV;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100124}
125
Alex Nixonb5401a92010-03-18 16:31:34 -0400126/*
127 * For MSI interrupts we have to use drivers/xen/event.s functions to
128 * allocate an irq_desc and setup the right */
129
130
131static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
132{
133 int irq, ret, i;
134 struct msi_desc *msidesc;
135 int *v;
136
137 v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL);
138 if (!v)
139 return -ENOMEM;
140
Qing Hef731e3ef2010-10-11 15:30:09 +0100141 if (type == PCI_CAP_ID_MSIX)
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500142 ret = xen_pci_frontend_enable_msix(dev, v, nvec);
Qing Hef731e3ef2010-10-11 15:30:09 +0100143 else
Konrad Rzeszutek Wilkcc0f89c2011-02-17 12:02:23 -0500144 ret = xen_pci_frontend_enable_msi(dev, v);
Qing Hef731e3ef2010-10-11 15:30:09 +0100145 if (ret)
146 goto error;
Alex Nixonb5401a92010-03-18 16:31:34 -0400147 i = 0;
148 list_for_each_entry(msidesc, &dev->msi_list, list) {
Ian Campbellca1d8fe2011-02-18 16:43:36 +0000149 irq = xen_bind_pirq_msi_to_irq(dev, msidesc, v[i], 0,
Ian Campbellbf480d92011-02-18 16:43:32 +0000150 (type == PCI_CAP_ID_MSIX) ?
151 "pcifront-msi-x" :
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400152 "pcifront-msi",
153 DOMID_SELF);
Ian Campbellbf480d92011-02-18 16:43:32 +0000154 if (irq < 0)
Jiri Slaby07cf2a62010-11-06 10:06:49 +0100155 goto free;
Alex Nixonb5401a92010-03-18 16:31:34 -0400156 i++;
157 }
158 kfree(v);
159 return 0;
160
Alex Nixonb5401a92010-03-18 16:31:34 -0400161error:
Ian Campbellbf480d92011-02-18 16:43:32 +0000162 dev_err(&dev->dev, "Xen PCI frontend has not registered MSI/MSI-X support!\n");
Jiri Slaby07cf2a62010-11-06 10:06:49 +0100163free:
Alex Nixonb5401a92010-03-18 16:31:34 -0400164 kfree(v);
165 return ret;
166}
167
168static void xen_teardown_msi_irqs(struct pci_dev *dev)
169{
Qing Hef731e3ef2010-10-11 15:30:09 +0100170 struct msi_desc *msidesc;
Alex Nixonb5401a92010-03-18 16:31:34 -0400171
Qing Hef731e3ef2010-10-11 15:30:09 +0100172 msidesc = list_entry(dev->msi_list.next, struct msi_desc, list);
173 if (msidesc->msi_attrib.is_msix)
174 xen_pci_frontend_disable_msix(dev);
175 else
176 xen_pci_frontend_disable_msi(dev);
Konrad Rzeszutek Wilk3d74a532011-02-17 16:12:51 -0500177
178 /* Free the IRQ's and the msidesc using the generic code. */
179 default_teardown_msi_irqs(dev);
Alex Nixonb5401a92010-03-18 16:31:34 -0400180}
181
182static void xen_teardown_msi_irq(unsigned int irq)
183{
184 xen_destroy_irq(irq);
185}
Qing Hef731e3ef2010-10-11 15:30:09 +0100186
Ian Campbell260a7d42011-02-18 16:43:26 +0000187#ifdef CONFIG_XEN_DOM0
Jan Beulich55e901f2011-09-22 09:17:57 +0100188static bool __read_mostly pci_seg_supported = true;
189
Qing Hef731e3ef2010-10-11 15:30:09 +0100190static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
191{
Ian Campbell71eef7d2011-02-18 17:06:55 +0000192 int ret = 0;
Qing Hef731e3ef2010-10-11 15:30:09 +0100193 struct msi_desc *msidesc;
194
195 list_for_each_entry(msidesc, &dev->msi_list, list) {
Ian Campbell71eef7d2011-02-18 17:06:55 +0000196 struct physdev_map_pirq map_irq;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400197 domid_t domid;
198
199 domid = ret = xen_find_device_domain_owner(dev);
200 /* N.B. Casting int's -ENODEV to uint16_t results in 0xFFED,
201 * hence check ret value for < 0. */
202 if (ret < 0)
203 domid = DOMID_SELF;
Ian Campbell71eef7d2011-02-18 17:06:55 +0000204
205 memset(&map_irq, 0, sizeof(map_irq));
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400206 map_irq.domid = domid;
Jan Beulich55e901f2011-09-22 09:17:57 +0100207 map_irq.type = MAP_PIRQ_TYPE_MSI_SEG;
Ian Campbell71eef7d2011-02-18 17:06:55 +0000208 map_irq.index = -1;
209 map_irq.pirq = -1;
Jan Beulich55e901f2011-09-22 09:17:57 +0100210 map_irq.bus = dev->bus->number |
211 (pci_domain_nr(dev->bus) << 16);
Ian Campbell71eef7d2011-02-18 17:06:55 +0000212 map_irq.devfn = dev->devfn;
213
214 if (type == PCI_CAP_ID_MSIX) {
215 int pos;
216 u32 table_offset, bir;
217
218 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
219
220 pci_read_config_dword(dev, pos + PCI_MSIX_TABLE,
221 &table_offset);
222 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
223
224 map_irq.table_base = pci_resource_start(dev, bir);
225 map_irq.entry_nr = msidesc->msi_attrib.entry_nr;
226 }
227
Jan Beulich55e901f2011-09-22 09:17:57 +0100228 ret = -EINVAL;
229 if (pci_seg_supported)
230 ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq,
231 &map_irq);
232 if (ret == -EINVAL && !pci_domain_nr(dev->bus)) {
233 map_irq.type = MAP_PIRQ_TYPE_MSI;
234 map_irq.index = -1;
235 map_irq.pirq = -1;
236 map_irq.bus = dev->bus->number;
237 ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq,
238 &map_irq);
239 if (ret != -EINVAL)
240 pci_seg_supported = false;
241 }
Ian Campbell71eef7d2011-02-18 17:06:55 +0000242 if (ret) {
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400243 dev_warn(&dev->dev, "xen map irq failed %d for %d domain\n",
244 ret, domid);
Ian Campbell71eef7d2011-02-18 17:06:55 +0000245 goto out;
246 }
247
248 ret = xen_bind_pirq_msi_to_irq(dev, msidesc,
249 map_irq.pirq, map_irq.index,
250 (type == PCI_CAP_ID_MSIX) ?
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400251 "msi-x" : "msi",
252 domid);
Ian Campbell71eef7d2011-02-18 17:06:55 +0000253 if (ret < 0)
254 goto out;
Qing Hef731e3ef2010-10-11 15:30:09 +0100255 }
Ian Campbell71eef7d2011-02-18 17:06:55 +0000256 ret = 0;
257out:
258 return ret;
Qing Hef731e3ef2010-10-11 15:30:09 +0100259}
Alex Nixonb5401a92010-03-18 16:31:34 -0400260#endif
Ian Campbell260a7d42011-02-18 16:43:26 +0000261#endif
Alex Nixonb5401a92010-03-18 16:31:34 -0400262
263static int xen_pcifront_enable_irq(struct pci_dev *dev)
264{
265 int rc;
266 int share = 1;
Ian Campbellf4d06352011-03-10 16:08:07 +0000267 int pirq;
Ian Campbell3f2a2302011-01-11 17:20:13 +0000268 u8 gsi;
Alex Nixonb5401a92010-03-18 16:31:34 -0400269
Ian Campbell3f2a2302011-01-11 17:20:13 +0000270 rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi);
Alex Nixonb5401a92010-03-18 16:31:34 -0400271 if (rc < 0) {
Ian Campbell3f2a2302011-01-11 17:20:13 +0000272 dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n",
273 rc);
Alex Nixonb5401a92010-03-18 16:31:34 -0400274 return rc;
275 }
Ian Campbell3f2a2302011-01-11 17:20:13 +0000276
Ian Campbellf4d06352011-03-10 16:08:07 +0000277 rc = xen_allocate_pirq_gsi(gsi);
278 if (rc < 0) {
279 dev_warn(&dev->dev, "Xen PCI: failed to allocate a PIRQ for GSI%d: %d\n",
280 gsi, rc);
281 return rc;
282 }
283 pirq = rc;
284
Ian Campbell3f2a2302011-01-11 17:20:13 +0000285 if (gsi < NR_IRQS_LEGACY)
286 share = 0;
287
Ian Campbellf4d06352011-03-10 16:08:07 +0000288 rc = xen_bind_pirq_gsi_to_irq(gsi, pirq, share, "pcifront");
Ian Campbell3f2a2302011-01-11 17:20:13 +0000289 if (rc < 0) {
Ian Campbellf4d06352011-03-10 16:08:07 +0000290 dev_warn(&dev->dev, "Xen PCI: failed to bind GSI%d (PIRQ%d) to IRQ: %d\n",
291 gsi, pirq, rc);
Ian Campbell3f2a2302011-01-11 17:20:13 +0000292 return rc;
293 }
294
295 dev->irq = rc;
296 dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400297 return 0;
298}
299
300int __init pci_xen_init(void)
301{
302 if (!xen_pv_domain() || xen_initial_domain())
303 return -ENODEV;
304
305 printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n");
306
307 pcibios_set_cache_line_size();
308
309 pcibios_enable_irq = xen_pcifront_enable_irq;
310 pcibios_disable_irq = NULL;
311
312#ifdef CONFIG_ACPI
313 /* Keep ACPI out of the picture */
314 acpi_noirq = 1;
315#endif
316
Alex Nixonb5401a92010-03-18 16:31:34 -0400317#ifdef CONFIG_PCI_MSI
318 x86_msi.setup_msi_irqs = xen_setup_msi_irqs;
319 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
320 x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs;
321#endif
322 return 0;
323}
Stefano Stabellini3942b742010-06-24 17:50:18 +0100324
325int __init pci_xen_hvm_init(void)
326{
327 if (!xen_feature(XENFEAT_hvm_pirqs))
328 return 0;
329
330#ifdef CONFIG_ACPI
331 /*
332 * We don't want to change the actual ACPI delivery model,
333 * just how GSIs get registered.
334 */
335 __acpi_register_gsi = acpi_register_gsi_xen_hvm;
336#endif
Stefano Stabellini809f9262010-07-01 17:10:39 +0100337
338#ifdef CONFIG_PCI_MSI
339 x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs;
340 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
341#endif
Stefano Stabellini3942b742010-06-24 17:50:18 +0100342 return 0;
343}
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100344
345#ifdef CONFIG_XEN_DOM0
Konrad Rzeszutek Wilkee339fe2011-07-06 09:43:16 -0400346static int xen_register_pirq(u32 gsi, int gsi_override, int triggering)
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100347{
Ian Campbellf4d06352011-03-10 16:08:07 +0000348 int rc, pirq, irq = -1;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100349 struct physdev_map_pirq map_irq;
350 int shareable = 0;
351 char *name;
352
353 if (!xen_pv_domain())
354 return -1;
355
356 if (triggering == ACPI_EDGE_SENSITIVE) {
357 shareable = 0;
358 name = "ioapic-edge";
359 } else {
360 shareable = 1;
361 name = "ioapic-level";
362 }
Ian Campbellf4d06352011-03-10 16:08:07 +0000363 pirq = xen_allocate_pirq_gsi(gsi);
364 if (pirq < 0)
365 goto out;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100366
Konrad Rzeszutek Wilkee339fe2011-07-06 09:43:16 -0400367 if (gsi_override >= 0)
368 irq = xen_bind_pirq_gsi_to_irq(gsi_override, pirq, shareable, name);
Konrad Rzeszutek Wilk155a16f2011-06-30 09:18:27 -0400369 else
370 irq = xen_bind_pirq_gsi_to_irq(gsi, pirq, shareable, name);
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100371 if (irq < 0)
372 goto out;
373
Konrad Rzeszutek Wilk155a16f2011-06-30 09:18:27 -0400374 printk(KERN_DEBUG "xen: --> pirq=%d -> irq=%d (gsi=%d)\n", pirq, irq, gsi);
Ian Campbellf4d06352011-03-10 16:08:07 +0000375
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100376 map_irq.domid = DOMID_SELF;
377 map_irq.type = MAP_PIRQ_TYPE_GSI;
378 map_irq.index = gsi;
Ian Campbellf4d06352011-03-10 16:08:07 +0000379 map_irq.pirq = pirq;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100380
381 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
382 if (rc) {
383 printk(KERN_WARNING "xen map irq failed %d\n", rc);
384 return -1;
385 }
386
387out:
388 return irq;
389}
390
Konrad Rzeszutek Wilkee339fe2011-07-06 09:43:16 -0400391static int xen_register_gsi(u32 gsi, int gsi_override, int triggering, int polarity)
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100392{
393 int rc, irq;
394 struct physdev_setup_gsi setup_gsi;
395
396 if (!xen_pv_domain())
397 return -1;
398
399 printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n",
400 gsi, triggering, polarity);
401
Konrad Rzeszutek Wilkee339fe2011-07-06 09:43:16 -0400402 irq = xen_register_pirq(gsi, gsi_override, triggering);
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100403
404 setup_gsi.gsi = gsi;
405 setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1);
406 setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
407
408 rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi);
409 if (rc == -EEXIST)
410 printk(KERN_INFO "Already setup the GSI :%d\n", gsi);
411 else if (rc) {
412 printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n",
413 gsi, rc);
414 }
415
416 return irq;
417}
418
419static __init void xen_setup_acpi_sci(void)
420{
421 int rc;
422 int trigger, polarity;
423 int gsi = acpi_sci_override_gsi;
Konrad Rzeszutek Wilkee339fe2011-07-06 09:43:16 -0400424 int irq = -1;
425 int gsi_override = -1;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100426
427 if (!gsi)
428 return;
429
430 rc = acpi_get_override_irq(gsi, &trigger, &polarity);
431 if (rc) {
432 printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi"
433 " sci, rc=%d\n", rc);
434 return;
435 }
436 trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
437 polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
438
439 printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d "
440 "polarity=%d\n", gsi, trigger, polarity);
441
Konrad Rzeszutek Wilkee339fe2011-07-06 09:43:16 -0400442 /* Before we bind the GSI to a Linux IRQ, check whether
443 * we need to override it with bus_irq (IRQ) value. Usually for
444 * IRQs below IRQ_LEGACY_IRQ this holds IRQ == GSI, as so:
445 * ACPI: INT_SRC_OVR (bus 0 bus_irq 9 global_irq 9 low level)
446 * but there are oddballs where the IRQ != GSI:
447 * ACPI: INT_SRC_OVR (bus 0 bus_irq 9 global_irq 20 low level)
448 * which ends up being: gsi_to_irq[9] == 20
449 * (which is what acpi_gsi_to_irq ends up calling when starting the
450 * the ACPI interpreter and keels over since IRQ 9 has not been
451 * setup as we had setup IRQ 20 for it).
452 */
453 /* Check whether the GSI != IRQ */
454 if (acpi_gsi_to_irq(gsi, &irq) == 0) {
455 if (irq >= 0 && irq != gsi)
456 /* Bugger, we MUST have that IRQ. */
457 gsi_override = irq;
458 }
459
460 gsi = xen_register_gsi(gsi, gsi_override, trigger, polarity);
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100461 printk(KERN_INFO "xen: acpi sci %d\n", gsi);
462
463 return;
464}
465
466static int acpi_register_gsi_xen(struct device *dev, u32 gsi,
467 int trigger, int polarity)
468{
Konrad Rzeszutek Wilkee339fe2011-07-06 09:43:16 -0400469 return xen_register_gsi(gsi, -1 /* no GSI override */, trigger, polarity);
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100470}
471
472static int __init pci_xen_initial_domain(void)
473{
Qing Hef731e3ef2010-10-11 15:30:09 +0100474#ifdef CONFIG_PCI_MSI
475 x86_msi.setup_msi_irqs = xen_initdom_setup_msi_irqs;
476 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
477#endif
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100478 xen_setup_acpi_sci();
479 __acpi_register_gsi = acpi_register_gsi_xen;
480
481 return 0;
482}
483
484void __init xen_setup_pirqs(void)
485{
Ian Campbellf4d06352011-03-10 16:08:07 +0000486 int pirq, irq;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100487
488 pci_xen_initial_domain();
489
490 if (0 == nr_ioapics) {
Ian Campbellf4d06352011-03-10 16:08:07 +0000491 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
492 pirq = xen_allocate_pirq_gsi(irq);
493 if (WARN(pirq < 0,
494 "Could not allocate PIRQ for legacy interrupt\n"))
495 break;
496 irq = xen_bind_pirq_gsi_to_irq(irq, pirq, 0, "xt-pic");
497 }
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100498 return;
499 }
500
501 /* Pre-allocate legacy irqs */
502 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
503 int trigger, polarity;
504
505 if (acpi_get_override_irq(irq, &trigger, &polarity) == -1)
506 continue;
507
Konrad Rzeszutek Wilkee339fe2011-07-06 09:43:16 -0400508 xen_register_pirq(irq, -1 /* no GSI override */,
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100509 trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE);
510 }
511}
512#endif
Konrad Rzeszutek Wilkc55fa782010-11-08 14:13:35 -0500513
Konrad Rzeszutek Wilk7c1bfd62011-05-16 13:47:30 -0400514#ifdef CONFIG_XEN_DOM0
Konrad Rzeszutek Wilkc55fa782010-11-08 14:13:35 -0500515struct xen_device_domain_owner {
516 domid_t domain;
517 struct pci_dev *dev;
518 struct list_head list;
519};
520
521static DEFINE_SPINLOCK(dev_domain_list_spinlock);
522static struct list_head dev_domain_list = LIST_HEAD_INIT(dev_domain_list);
523
524static struct xen_device_domain_owner *find_device(struct pci_dev *dev)
525{
526 struct xen_device_domain_owner *owner;
527
528 list_for_each_entry(owner, &dev_domain_list, list) {
529 if (owner->dev == dev)
530 return owner;
531 }
532 return NULL;
533}
534
535int xen_find_device_domain_owner(struct pci_dev *dev)
536{
537 struct xen_device_domain_owner *owner;
538 int domain = -ENODEV;
539
540 spin_lock(&dev_domain_list_spinlock);
541 owner = find_device(dev);
542 if (owner)
543 domain = owner->domain;
544 spin_unlock(&dev_domain_list_spinlock);
545 return domain;
546}
547EXPORT_SYMBOL_GPL(xen_find_device_domain_owner);
548
549int xen_register_device_domain_owner(struct pci_dev *dev, uint16_t domain)
550{
551 struct xen_device_domain_owner *owner;
552
553 owner = kzalloc(sizeof(struct xen_device_domain_owner), GFP_KERNEL);
554 if (!owner)
555 return -ENODEV;
556
557 spin_lock(&dev_domain_list_spinlock);
558 if (find_device(dev)) {
559 spin_unlock(&dev_domain_list_spinlock);
560 kfree(owner);
561 return -EEXIST;
562 }
563 owner->domain = domain;
564 owner->dev = dev;
565 list_add_tail(&owner->list, &dev_domain_list);
566 spin_unlock(&dev_domain_list_spinlock);
567 return 0;
568}
569EXPORT_SYMBOL_GPL(xen_register_device_domain_owner);
570
571int xen_unregister_device_domain_owner(struct pci_dev *dev)
572{
573 struct xen_device_domain_owner *owner;
574
575 spin_lock(&dev_domain_list_spinlock);
576 owner = find_device(dev);
577 if (!owner) {
578 spin_unlock(&dev_domain_list_spinlock);
579 return -ENODEV;
580 }
581 list_del(&owner->list);
582 spin_unlock(&dev_domain_list_spinlock);
583 kfree(owner);
584 return 0;
585}
586EXPORT_SYMBOL_GPL(xen_unregister_device_domain_owner);
Konrad Rzeszutek Wilk7c1bfd62011-05-16 13:47:30 -0400587#endif