xen.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  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.
  7. *
  8. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  9. * Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
  10. * Stefano Stabellini <stefano.stabellini@eu.citrix.com>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/pci.h>
  15. #include <linux/acpi.h>
  16. #include <linux/io.h>
  17. #include <asm/io_apic.h>
  18. #include <asm/pci_x86.h>
  19. #include <asm/xen/hypervisor.h>
  20. #include <xen/features.h>
  21. #include <xen/events.h>
  22. #include <asm/xen/pci.h>
  23. #include <asm/xen/cpuid.h>
  24. #include <asm/apic.h>
  25. #include <asm/i8259.h>
  26. static int xen_pcifront_enable_irq(struct pci_dev *dev)
  27. {
  28. int rc;
  29. int share = 1;
  30. int pirq;
  31. u8 gsi;
  32. rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi);
  33. if (rc < 0) {
  34. dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n",
  35. rc);
  36. return rc;
  37. }
  38. /* In PV DomU the Xen PCI backend puts the PIRQ in the interrupt line.*/
  39. pirq = gsi;
  40. if (gsi < nr_legacy_irqs())
  41. share = 0;
  42. rc = xen_bind_pirq_gsi_to_irq(gsi, pirq, share, "pcifront");
  43. if (rc < 0) {
  44. dev_warn(&dev->dev, "Xen PCI: failed to bind GSI%d (PIRQ%d) to IRQ: %d\n",
  45. gsi, pirq, rc);
  46. return rc;
  47. }
  48. dev->irq = rc;
  49. dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq);
  50. return 0;
  51. }
  52. #ifdef CONFIG_ACPI
  53. static int xen_register_pirq(u32 gsi, int gsi_override, int triggering,
  54. bool set_pirq)
  55. {
  56. int rc, pirq = -1, irq = -1;
  57. struct physdev_map_pirq map_irq;
  58. int shareable = 0;
  59. char *name;
  60. irq = xen_irq_from_gsi(gsi);
  61. if (irq > 0)
  62. return irq;
  63. if (set_pirq)
  64. pirq = gsi;
  65. map_irq.domid = DOMID_SELF;
  66. map_irq.type = MAP_PIRQ_TYPE_GSI;
  67. map_irq.index = gsi;
  68. map_irq.pirq = pirq;
  69. rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
  70. if (rc) {
  71. printk(KERN_WARNING "xen map irq failed %d\n", rc);
  72. return -1;
  73. }
  74. if (triggering == ACPI_EDGE_SENSITIVE) {
  75. shareable = 0;
  76. name = "ioapic-edge";
  77. } else {
  78. shareable = 1;
  79. name = "ioapic-level";
  80. }
  81. if (gsi_override >= 0)
  82. gsi = gsi_override;
  83. irq = xen_bind_pirq_gsi_to_irq(gsi, map_irq.pirq, shareable, name);
  84. if (irq < 0)
  85. goto out;
  86. printk(KERN_DEBUG "xen: --> pirq=%d -> irq=%d (gsi=%d)\n", map_irq.pirq, irq, gsi);
  87. out:
  88. return irq;
  89. }
  90. static int acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi,
  91. int trigger, int polarity)
  92. {
  93. if (!xen_hvm_domain())
  94. return -1;
  95. return xen_register_pirq(gsi, -1 /* no GSI override */, trigger,
  96. false /* no mapping of GSI to PIRQ */);
  97. }
  98. #ifdef CONFIG_XEN_DOM0
  99. static int xen_register_gsi(u32 gsi, int gsi_override, int triggering, int polarity)
  100. {
  101. int rc, irq;
  102. struct physdev_setup_gsi setup_gsi;
  103. if (!xen_pv_domain())
  104. return -1;
  105. printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n",
  106. gsi, triggering, polarity);
  107. irq = xen_register_pirq(gsi, gsi_override, triggering, true);
  108. setup_gsi.gsi = gsi;
  109. setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1);
  110. setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
  111. rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi);
  112. if (rc == -EEXIST)
  113. printk(KERN_INFO "Already setup the GSI :%d\n", gsi);
  114. else if (rc) {
  115. printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n",
  116. gsi, rc);
  117. }
  118. return irq;
  119. }
  120. static int acpi_register_gsi_xen(struct device *dev, u32 gsi,
  121. int trigger, int polarity)
  122. {
  123. return xen_register_gsi(gsi, -1 /* no GSI override */, trigger, polarity);
  124. }
  125. #endif
  126. #endif
  127. #if defined(CONFIG_PCI_MSI)
  128. #include <linux/msi.h>
  129. #include <asm/msidef.h>
  130. struct xen_pci_frontend_ops *xen_pci_frontend;
  131. EXPORT_SYMBOL_GPL(xen_pci_frontend);
  132. static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  133. {
  134. int irq, ret, i;
  135. struct msi_desc *msidesc;
  136. int *v;
  137. if (type == PCI_CAP_ID_MSI && nvec > 1)
  138. return 1;
  139. v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL);
  140. if (!v)
  141. return -ENOMEM;
  142. if (type == PCI_CAP_ID_MSIX)
  143. ret = xen_pci_frontend_enable_msix(dev, v, nvec);
  144. else
  145. ret = xen_pci_frontend_enable_msi(dev, v);
  146. if (ret)
  147. goto error;
  148. i = 0;
  149. for_each_pci_msi_entry(msidesc, dev) {
  150. irq = xen_bind_pirq_msi_to_irq(dev, msidesc, v[i],
  151. (type == PCI_CAP_ID_MSI) ? nvec : 1,
  152. (type == PCI_CAP_ID_MSIX) ?
  153. "pcifront-msi-x" :
  154. "pcifront-msi",
  155. DOMID_SELF);
  156. if (irq < 0) {
  157. ret = irq;
  158. goto free;
  159. }
  160. i++;
  161. }
  162. kfree(v);
  163. return 0;
  164. error:
  165. dev_err(&dev->dev, "Xen PCI frontend has not registered MSI/MSI-X support!\n");
  166. free:
  167. kfree(v);
  168. return ret;
  169. }
  170. #define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \
  171. MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0))
  172. static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq,
  173. struct msi_msg *msg)
  174. {
  175. /* We set vector == 0 to tell the hypervisor we don't care about it,
  176. * but we want a pirq setup instead.
  177. * We use the dest_id field to pass the pirq that we want. */
  178. msg->address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(pirq);
  179. msg->address_lo =
  180. MSI_ADDR_BASE_LO |
  181. MSI_ADDR_DEST_MODE_PHYSICAL |
  182. MSI_ADDR_REDIRECTION_CPU |
  183. MSI_ADDR_DEST_ID(pirq);
  184. msg->data = XEN_PIRQ_MSI_DATA;
  185. }
  186. static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  187. {
  188. int irq, pirq;
  189. struct msi_desc *msidesc;
  190. struct msi_msg msg;
  191. if (type == PCI_CAP_ID_MSI && nvec > 1)
  192. return 1;
  193. for_each_pci_msi_entry(msidesc, dev) {
  194. pirq = xen_allocate_pirq_msi(dev, msidesc);
  195. if (pirq < 0) {
  196. irq = -ENODEV;
  197. goto error;
  198. }
  199. xen_msi_compose_msg(dev, pirq, &msg);
  200. __pci_write_msi_msg(msidesc, &msg);
  201. dev_dbg(&dev->dev, "xen: msi bound to pirq=%d\n", pirq);
  202. irq = xen_bind_pirq_msi_to_irq(dev, msidesc, pirq,
  203. (type == PCI_CAP_ID_MSI) ? nvec : 1,
  204. (type == PCI_CAP_ID_MSIX) ?
  205. "msi-x" : "msi",
  206. DOMID_SELF);
  207. if (irq < 0)
  208. goto error;
  209. dev_dbg(&dev->dev,
  210. "xen: msi --> pirq=%d --> irq=%d\n", pirq, irq);
  211. }
  212. return 0;
  213. error:
  214. dev_err(&dev->dev,
  215. "Xen PCI frontend has not registered MSI/MSI-X support!\n");
  216. return irq;
  217. }
  218. #ifdef CONFIG_XEN_DOM0
  219. static bool __read_mostly pci_seg_supported = true;
  220. static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  221. {
  222. int ret = 0;
  223. struct msi_desc *msidesc;
  224. for_each_pci_msi_entry(msidesc, dev) {
  225. struct physdev_map_pirq map_irq;
  226. domid_t domid;
  227. domid = ret = xen_find_device_domain_owner(dev);
  228. /* N.B. Casting int's -ENODEV to uint16_t results in 0xFFED,
  229. * hence check ret value for < 0. */
  230. if (ret < 0)
  231. domid = DOMID_SELF;
  232. memset(&map_irq, 0, sizeof(map_irq));
  233. map_irq.domid = domid;
  234. map_irq.type = MAP_PIRQ_TYPE_MSI_SEG;
  235. map_irq.index = -1;
  236. map_irq.pirq = -1;
  237. map_irq.bus = dev->bus->number |
  238. (pci_domain_nr(dev->bus) << 16);
  239. map_irq.devfn = dev->devfn;
  240. if (type == PCI_CAP_ID_MSI && nvec > 1) {
  241. map_irq.type = MAP_PIRQ_TYPE_MULTI_MSI;
  242. map_irq.entry_nr = nvec;
  243. } else if (type == PCI_CAP_ID_MSIX) {
  244. int pos;
  245. unsigned long flags;
  246. u32 table_offset, bir;
  247. pos = dev->msix_cap;
  248. pci_read_config_dword(dev, pos + PCI_MSIX_TABLE,
  249. &table_offset);
  250. bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
  251. flags = pci_resource_flags(dev, bir);
  252. if (!flags || (flags & IORESOURCE_UNSET))
  253. return -EINVAL;
  254. map_irq.table_base = pci_resource_start(dev, bir);
  255. map_irq.entry_nr = msidesc->msi_attrib.entry_nr;
  256. }
  257. ret = -EINVAL;
  258. if (pci_seg_supported)
  259. ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq,
  260. &map_irq);
  261. if (type == PCI_CAP_ID_MSI && nvec > 1 && ret) {
  262. /*
  263. * If MAP_PIRQ_TYPE_MULTI_MSI is not available
  264. * there's nothing else we can do in this case.
  265. * Just set ret > 0 so driver can retry with
  266. * single MSI.
  267. */
  268. ret = 1;
  269. goto out;
  270. }
  271. if (ret == -EINVAL && !pci_domain_nr(dev->bus)) {
  272. map_irq.type = MAP_PIRQ_TYPE_MSI;
  273. map_irq.index = -1;
  274. map_irq.pirq = -1;
  275. map_irq.bus = dev->bus->number;
  276. ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq,
  277. &map_irq);
  278. if (ret != -EINVAL)
  279. pci_seg_supported = false;
  280. }
  281. if (ret) {
  282. dev_warn(&dev->dev, "xen map irq failed %d for %d domain\n",
  283. ret, domid);
  284. goto out;
  285. }
  286. ret = xen_bind_pirq_msi_to_irq(dev, msidesc, map_irq.pirq,
  287. (type == PCI_CAP_ID_MSI) ? nvec : 1,
  288. (type == PCI_CAP_ID_MSIX) ? "msi-x" : "msi",
  289. domid);
  290. if (ret < 0)
  291. goto out;
  292. }
  293. ret = 0;
  294. out:
  295. return ret;
  296. }
  297. static void xen_initdom_restore_msi_irqs(struct pci_dev *dev)
  298. {
  299. int ret = 0;
  300. if (pci_seg_supported) {
  301. struct physdev_pci_device restore_ext;
  302. restore_ext.seg = pci_domain_nr(dev->bus);
  303. restore_ext.bus = dev->bus->number;
  304. restore_ext.devfn = dev->devfn;
  305. ret = HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi_ext,
  306. &restore_ext);
  307. if (ret == -ENOSYS)
  308. pci_seg_supported = false;
  309. WARN(ret && ret != -ENOSYS, "restore_msi_ext -> %d\n", ret);
  310. }
  311. if (!pci_seg_supported) {
  312. struct physdev_restore_msi restore;
  313. restore.bus = dev->bus->number;
  314. restore.devfn = dev->devfn;
  315. ret = HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi, &restore);
  316. WARN(ret && ret != -ENOSYS, "restore_msi -> %d\n", ret);
  317. }
  318. }
  319. #endif
  320. static void xen_teardown_msi_irqs(struct pci_dev *dev)
  321. {
  322. struct msi_desc *msidesc;
  323. msidesc = first_pci_msi_entry(dev);
  324. if (msidesc->msi_attrib.is_msix)
  325. xen_pci_frontend_disable_msix(dev);
  326. else
  327. xen_pci_frontend_disable_msi(dev);
  328. /* Free the IRQ's and the msidesc using the generic code. */
  329. default_teardown_msi_irqs(dev);
  330. }
  331. static void xen_teardown_msi_irq(unsigned int irq)
  332. {
  333. xen_destroy_irq(irq);
  334. }
  335. #endif
  336. int __init pci_xen_init(void)
  337. {
  338. if (!xen_pv_domain() || xen_initial_domain())
  339. return -ENODEV;
  340. printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n");
  341. pcibios_set_cache_line_size();
  342. pcibios_enable_irq = xen_pcifront_enable_irq;
  343. pcibios_disable_irq = NULL;
  344. #ifdef CONFIG_ACPI
  345. /* Keep ACPI out of the picture */
  346. acpi_noirq = 1;
  347. #endif
  348. #ifdef CONFIG_PCI_MSI
  349. x86_msi.setup_msi_irqs = xen_setup_msi_irqs;
  350. x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
  351. x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs;
  352. pci_msi_ignore_mask = 1;
  353. #endif
  354. return 0;
  355. }
  356. #ifdef CONFIG_PCI_MSI
  357. void __init xen_msi_init(void)
  358. {
  359. if (!disable_apic) {
  360. /*
  361. * If hardware supports (x2)APIC virtualization (as indicated
  362. * by hypervisor's leaf 4) then we don't need to use pirqs/
  363. * event channels for MSI handling and instead use regular
  364. * APIC processing
  365. */
  366. uint32_t eax = cpuid_eax(xen_cpuid_base() + 4);
  367. if (((eax & XEN_HVM_CPUID_X2APIC_VIRT) && x2apic_mode) ||
  368. ((eax & XEN_HVM_CPUID_APIC_ACCESS_VIRT) && cpu_has_apic))
  369. return;
  370. }
  371. x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs;
  372. x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
  373. }
  374. #endif
  375. int __init pci_xen_hvm_init(void)
  376. {
  377. if (!xen_have_vector_callback || !xen_feature(XENFEAT_hvm_pirqs))
  378. return 0;
  379. #ifdef CONFIG_ACPI
  380. /*
  381. * We don't want to change the actual ACPI delivery model,
  382. * just how GSIs get registered.
  383. */
  384. __acpi_register_gsi = acpi_register_gsi_xen_hvm;
  385. __acpi_unregister_gsi = NULL;
  386. #endif
  387. #ifdef CONFIG_PCI_MSI
  388. /*
  389. * We need to wait until after x2apic is initialized
  390. * before we can set MSI IRQ ops.
  391. */
  392. x86_platform.apic_post_init = xen_msi_init;
  393. #endif
  394. return 0;
  395. }
  396. #ifdef CONFIG_XEN_DOM0
  397. int __init pci_xen_initial_domain(void)
  398. {
  399. int irq;
  400. #ifdef CONFIG_PCI_MSI
  401. x86_msi.setup_msi_irqs = xen_initdom_setup_msi_irqs;
  402. x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
  403. x86_msi.restore_msi_irqs = xen_initdom_restore_msi_irqs;
  404. pci_msi_ignore_mask = 1;
  405. #endif
  406. __acpi_register_gsi = acpi_register_gsi_xen;
  407. __acpi_unregister_gsi = NULL;
  408. /*
  409. * Pre-allocate the legacy IRQs. Use NR_LEGACY_IRQS here
  410. * because we don't have a PIC and thus nr_legacy_irqs() is zero.
  411. */
  412. for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
  413. int trigger, polarity;
  414. if (acpi_get_override_irq(irq, &trigger, &polarity) == -1)
  415. continue;
  416. xen_register_pirq(irq, -1 /* no GSI override */,
  417. trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE,
  418. true /* Map GSI to PIRQ */);
  419. }
  420. if (0 == nr_ioapics) {
  421. for (irq = 0; irq < nr_legacy_irqs(); irq++)
  422. xen_bind_pirq_gsi_to_irq(irq, irq, 0, "xt-pic");
  423. }
  424. return 0;
  425. }
  426. struct xen_device_domain_owner {
  427. domid_t domain;
  428. struct pci_dev *dev;
  429. struct list_head list;
  430. };
  431. static DEFINE_SPINLOCK(dev_domain_list_spinlock);
  432. static struct list_head dev_domain_list = LIST_HEAD_INIT(dev_domain_list);
  433. static struct xen_device_domain_owner *find_device(struct pci_dev *dev)
  434. {
  435. struct xen_device_domain_owner *owner;
  436. list_for_each_entry(owner, &dev_domain_list, list) {
  437. if (owner->dev == dev)
  438. return owner;
  439. }
  440. return NULL;
  441. }
  442. int xen_find_device_domain_owner(struct pci_dev *dev)
  443. {
  444. struct xen_device_domain_owner *owner;
  445. int domain = -ENODEV;
  446. spin_lock(&dev_domain_list_spinlock);
  447. owner = find_device(dev);
  448. if (owner)
  449. domain = owner->domain;
  450. spin_unlock(&dev_domain_list_spinlock);
  451. return domain;
  452. }
  453. EXPORT_SYMBOL_GPL(xen_find_device_domain_owner);
  454. int xen_register_device_domain_owner(struct pci_dev *dev, uint16_t domain)
  455. {
  456. struct xen_device_domain_owner *owner;
  457. owner = kzalloc(sizeof(struct xen_device_domain_owner), GFP_KERNEL);
  458. if (!owner)
  459. return -ENODEV;
  460. spin_lock(&dev_domain_list_spinlock);
  461. if (find_device(dev)) {
  462. spin_unlock(&dev_domain_list_spinlock);
  463. kfree(owner);
  464. return -EEXIST;
  465. }
  466. owner->domain = domain;
  467. owner->dev = dev;
  468. list_add_tail(&owner->list, &dev_domain_list);
  469. spin_unlock(&dev_domain_list_spinlock);
  470. return 0;
  471. }
  472. EXPORT_SYMBOL_GPL(xen_register_device_domain_owner);
  473. int xen_unregister_device_domain_owner(struct pci_dev *dev)
  474. {
  475. struct xen_device_domain_owner *owner;
  476. spin_lock(&dev_domain_list_spinlock);
  477. owner = find_device(dev);
  478. if (!owner) {
  479. spin_unlock(&dev_domain_list_spinlock);
  480. return -ENODEV;
  481. }
  482. list_del(&owner->list);
  483. spin_unlock(&dev_domain_list_spinlock);
  484. kfree(owner);
  485. return 0;
  486. }
  487. EXPORT_SYMBOL_GPL(xen_unregister_device_domain_owner);
  488. #endif