kvm tools, setup: Create private directory
[linux-2.6/next.git] / arch / x86 / pci / xen.c
blob8214724ce54dafeef19290199e4a9657a6468766
1 /*
2 * Xen PCI Frontend Stub - puts some "dummy" functions in to the Linux
3 * x86 PCI core to support the Xen PCI Frontend
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>
12 #include <linux/io.h>
13 #include <asm/io_apic.h>
14 #include <asm/pci_x86.h>
16 #include <asm/xen/hypervisor.h>
18 #include <xen/features.h>
19 #include <xen/events.h>
20 #include <asm/xen/pci.h>
22 #ifdef CONFIG_ACPI
23 static int acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi,
24 int trigger, int polarity)
26 int rc, irq;
27 struct physdev_map_pirq map_irq;
28 int shareable = 0;
29 char *name;
31 if (!xen_hvm_domain())
32 return -1;
34 map_irq.domid = DOMID_SELF;
35 map_irq.type = MAP_PIRQ_TYPE_GSI;
36 map_irq.index = gsi;
37 map_irq.pirq = -1;
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;
45 if (trigger == ACPI_EDGE_SENSITIVE) {
46 shareable = 0;
47 name = "ioapic-edge";
48 } else {
49 shareable = 1;
50 name = "ioapic-level";
53 irq = xen_bind_pirq_gsi_to_irq(gsi, map_irq.pirq, shareable, name);
55 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
57 return irq;
59 #endif
61 #if defined(CONFIG_PCI_MSI)
62 #include <linux/msi.h>
63 #include <asm/msidef.h>
65 struct xen_pci_frontend_ops *xen_pci_frontend;
66 EXPORT_SYMBOL_GPL(xen_pci_frontend);
68 #define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \
69 MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0))
71 static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq,
72 struct msi_msg *msg)
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);
84 msg->data = XEN_PIRQ_MSI_DATA;
87 static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
89 int irq, pirq;
90 struct msi_desc *msidesc;
91 struct msi_msg msg;
93 list_for_each_entry(msidesc, &dev->msi_list, list) {
94 __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);
97 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)
101 goto error;
102 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);
109 irq = xen_bind_pirq_msi_to_irq(dev, msidesc, pirq, 0,
110 (type == PCI_CAP_ID_MSIX) ?
111 "msi-x" : "msi",
112 DOMID_SELF);
113 if (irq < 0)
114 goto error;
115 dev_dbg(&dev->dev,
116 "xen: msi --> pirq=%d --> irq=%d\n", pirq, irq);
118 return 0;
120 error:
121 dev_err(&dev->dev,
122 "Xen PCI frontend has not registered MSI/MSI-X support!\n");
123 return -ENODEV;
127 * For MSI interrupts we have to use drivers/xen/event.s functions to
128 * allocate an irq_desc and setup the right */
131 static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
133 int irq, ret, i;
134 struct msi_desc *msidesc;
135 int *v;
137 v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL);
138 if (!v)
139 return -ENOMEM;
141 if (type == PCI_CAP_ID_MSIX)
142 ret = xen_pci_frontend_enable_msix(dev, v, nvec);
143 else
144 ret = xen_pci_frontend_enable_msi(dev, v);
145 if (ret)
146 goto error;
147 i = 0;
148 list_for_each_entry(msidesc, &dev->msi_list, list) {
149 irq = xen_bind_pirq_msi_to_irq(dev, msidesc, v[i], 0,
150 (type == PCI_CAP_ID_MSIX) ?
151 "pcifront-msi-x" :
152 "pcifront-msi",
153 DOMID_SELF);
154 if (irq < 0)
155 goto free;
156 i++;
158 kfree(v);
159 return 0;
161 error:
162 dev_err(&dev->dev, "Xen PCI frontend has not registered MSI/MSI-X support!\n");
163 free:
164 kfree(v);
165 return ret;
168 static void xen_teardown_msi_irqs(struct pci_dev *dev)
170 struct msi_desc *msidesc;
172 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);
178 /* Free the IRQ's and the msidesc using the generic code. */
179 default_teardown_msi_irqs(dev);
182 static void xen_teardown_msi_irq(unsigned int irq)
184 xen_destroy_irq(irq);
187 #ifdef CONFIG_XEN_DOM0
188 static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
190 int ret = 0;
191 struct msi_desc *msidesc;
193 list_for_each_entry(msidesc, &dev->msi_list, list) {
194 struct physdev_map_pirq map_irq;
195 domid_t domid;
197 domid = ret = xen_find_device_domain_owner(dev);
198 /* N.B. Casting int's -ENODEV to uint16_t results in 0xFFED,
199 * hence check ret value for < 0. */
200 if (ret < 0)
201 domid = DOMID_SELF;
203 memset(&map_irq, 0, sizeof(map_irq));
204 map_irq.domid = domid;
205 map_irq.type = MAP_PIRQ_TYPE_MSI;
206 map_irq.index = -1;
207 map_irq.pirq = -1;
208 map_irq.bus = dev->bus->number;
209 map_irq.devfn = dev->devfn;
211 if (type == PCI_CAP_ID_MSIX) {
212 int pos;
213 u32 table_offset, bir;
215 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
217 pci_read_config_dword(dev, pos + PCI_MSIX_TABLE,
218 &table_offset);
219 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
221 map_irq.table_base = pci_resource_start(dev, bir);
222 map_irq.entry_nr = msidesc->msi_attrib.entry_nr;
225 ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
226 if (ret) {
227 dev_warn(&dev->dev, "xen map irq failed %d for %d domain\n",
228 ret, domid);
229 goto out;
232 ret = xen_bind_pirq_msi_to_irq(dev, msidesc,
233 map_irq.pirq, map_irq.index,
234 (type == PCI_CAP_ID_MSIX) ?
235 "msi-x" : "msi",
236 domid);
237 if (ret < 0)
238 goto out;
240 ret = 0;
241 out:
242 return ret;
244 #endif
245 #endif
247 static int xen_pcifront_enable_irq(struct pci_dev *dev)
249 int rc;
250 int share = 1;
251 int pirq;
252 u8 gsi;
254 rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi);
255 if (rc < 0) {
256 dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n",
257 rc);
258 return rc;
261 rc = xen_allocate_pirq_gsi(gsi);
262 if (rc < 0) {
263 dev_warn(&dev->dev, "Xen PCI: failed to allocate a PIRQ for GSI%d: %d\n",
264 gsi, rc);
265 return rc;
267 pirq = rc;
269 if (gsi < NR_IRQS_LEGACY)
270 share = 0;
272 rc = xen_bind_pirq_gsi_to_irq(gsi, pirq, share, "pcifront");
273 if (rc < 0) {
274 dev_warn(&dev->dev, "Xen PCI: failed to bind GSI%d (PIRQ%d) to IRQ: %d\n",
275 gsi, pirq, rc);
276 return rc;
279 dev->irq = rc;
280 dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq);
281 return 0;
284 int __init pci_xen_init(void)
286 if (!xen_pv_domain() || xen_initial_domain())
287 return -ENODEV;
289 printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n");
291 pcibios_set_cache_line_size();
293 pcibios_enable_irq = xen_pcifront_enable_irq;
294 pcibios_disable_irq = NULL;
296 #ifdef CONFIG_ACPI
297 /* Keep ACPI out of the picture */
298 acpi_noirq = 1;
299 #endif
301 #ifdef CONFIG_PCI_MSI
302 x86_msi.setup_msi_irqs = xen_setup_msi_irqs;
303 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
304 x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs;
305 #endif
306 return 0;
309 int __init pci_xen_hvm_init(void)
311 if (!xen_feature(XENFEAT_hvm_pirqs))
312 return 0;
314 #ifdef CONFIG_ACPI
316 * We don't want to change the actual ACPI delivery model,
317 * just how GSIs get registered.
319 __acpi_register_gsi = acpi_register_gsi_xen_hvm;
320 #endif
322 #ifdef CONFIG_PCI_MSI
323 x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs;
324 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
325 #endif
326 return 0;
329 #ifdef CONFIG_XEN_DOM0
330 static int xen_register_pirq(u32 gsi, int triggering)
332 int rc, pirq, irq = -1;
333 struct physdev_map_pirq map_irq;
334 int shareable = 0;
335 char *name;
337 if (!xen_pv_domain())
338 return -1;
340 if (triggering == ACPI_EDGE_SENSITIVE) {
341 shareable = 0;
342 name = "ioapic-edge";
343 } else {
344 shareable = 1;
345 name = "ioapic-level";
348 pirq = xen_allocate_pirq_gsi(gsi);
349 if (pirq < 0)
350 goto out;
352 irq = xen_bind_pirq_gsi_to_irq(gsi, pirq, shareable, name);
353 if (irq < 0)
354 goto out;
356 printk(KERN_DEBUG "xen: --> pirq=%d -> irq=%d\n", pirq, irq);
358 map_irq.domid = DOMID_SELF;
359 map_irq.type = MAP_PIRQ_TYPE_GSI;
360 map_irq.index = gsi;
361 map_irq.pirq = pirq;
363 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
364 if (rc) {
365 printk(KERN_WARNING "xen map irq failed %d\n", rc);
366 return -1;
369 out:
370 return irq;
373 static int xen_register_gsi(u32 gsi, int triggering, int polarity)
375 int rc, irq;
376 struct physdev_setup_gsi setup_gsi;
378 if (!xen_pv_domain())
379 return -1;
381 printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n",
382 gsi, triggering, polarity);
384 irq = xen_register_pirq(gsi, triggering);
386 setup_gsi.gsi = gsi;
387 setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1);
388 setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
390 rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi);
391 if (rc == -EEXIST)
392 printk(KERN_INFO "Already setup the GSI :%d\n", gsi);
393 else if (rc) {
394 printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n",
395 gsi, rc);
398 return irq;
401 static __init void xen_setup_acpi_sci(void)
403 int rc;
404 int trigger, polarity;
405 int gsi = acpi_sci_override_gsi;
407 if (!gsi)
408 return;
410 rc = acpi_get_override_irq(gsi, &trigger, &polarity);
411 if (rc) {
412 printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi"
413 " sci, rc=%d\n", rc);
414 return;
416 trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
417 polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
419 printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d "
420 "polarity=%d\n", gsi, trigger, polarity);
422 gsi = xen_register_gsi(gsi, trigger, polarity);
423 printk(KERN_INFO "xen: acpi sci %d\n", gsi);
425 return;
428 static int acpi_register_gsi_xen(struct device *dev, u32 gsi,
429 int trigger, int polarity)
431 return xen_register_gsi(gsi, trigger, polarity);
434 static int __init pci_xen_initial_domain(void)
436 #ifdef CONFIG_PCI_MSI
437 x86_msi.setup_msi_irqs = xen_initdom_setup_msi_irqs;
438 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
439 #endif
440 xen_setup_acpi_sci();
441 __acpi_register_gsi = acpi_register_gsi_xen;
443 return 0;
446 void __init xen_setup_pirqs(void)
448 int pirq, irq;
450 pci_xen_initial_domain();
452 if (0 == nr_ioapics) {
453 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
454 pirq = xen_allocate_pirq_gsi(irq);
455 if (WARN(pirq < 0,
456 "Could not allocate PIRQ for legacy interrupt\n"))
457 break;
458 irq = xen_bind_pirq_gsi_to_irq(irq, pirq, 0, "xt-pic");
460 return;
463 /* Pre-allocate legacy irqs */
464 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
465 int trigger, polarity;
467 if (acpi_get_override_irq(irq, &trigger, &polarity) == -1)
468 continue;
470 xen_register_pirq(irq,
471 trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE);
474 #endif
476 #ifdef CONFIG_XEN_DOM0
477 struct xen_device_domain_owner {
478 domid_t domain;
479 struct pci_dev *dev;
480 struct list_head list;
483 static DEFINE_SPINLOCK(dev_domain_list_spinlock);
484 static struct list_head dev_domain_list = LIST_HEAD_INIT(dev_domain_list);
486 static struct xen_device_domain_owner *find_device(struct pci_dev *dev)
488 struct xen_device_domain_owner *owner;
490 list_for_each_entry(owner, &dev_domain_list, list) {
491 if (owner->dev == dev)
492 return owner;
494 return NULL;
497 int xen_find_device_domain_owner(struct pci_dev *dev)
499 struct xen_device_domain_owner *owner;
500 int domain = -ENODEV;
502 spin_lock(&dev_domain_list_spinlock);
503 owner = find_device(dev);
504 if (owner)
505 domain = owner->domain;
506 spin_unlock(&dev_domain_list_spinlock);
507 return domain;
509 EXPORT_SYMBOL_GPL(xen_find_device_domain_owner);
511 int xen_register_device_domain_owner(struct pci_dev *dev, uint16_t domain)
513 struct xen_device_domain_owner *owner;
515 owner = kzalloc(sizeof(struct xen_device_domain_owner), GFP_KERNEL);
516 if (!owner)
517 return -ENODEV;
519 spin_lock(&dev_domain_list_spinlock);
520 if (find_device(dev)) {
521 spin_unlock(&dev_domain_list_spinlock);
522 kfree(owner);
523 return -EEXIST;
525 owner->domain = domain;
526 owner->dev = dev;
527 list_add_tail(&owner->list, &dev_domain_list);
528 spin_unlock(&dev_domain_list_spinlock);
529 return 0;
531 EXPORT_SYMBOL_GPL(xen_register_device_domain_owner);
533 int xen_unregister_device_domain_owner(struct pci_dev *dev)
535 struct xen_device_domain_owner *owner;
537 spin_lock(&dev_domain_list_spinlock);
538 owner = find_device(dev);
539 if (!owner) {
540 spin_unlock(&dev_domain_list_spinlock);
541 return -ENODEV;
543 list_del(&owner->list);
544 spin_unlock(&dev_domain_list_spinlock);
545 kfree(owner);
546 return 0;
548 EXPORT_SYMBOL_GPL(xen_unregister_device_domain_owner);
549 #endif