xen.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Xen PCI - handle PCI (INTx) and MSI infrastructure calls for PV, HVM and
  4. * initial domain support. We also handle the DSDT _PRT callbacks for GSI's
  5. * used in HVM and initial domain mode (PV does not parse ACPI, so it has no
  6. * concept of GSIs). Under PV we hook under the pnbbios API for IRQs and
  7. * 0xcf8 PCI configuration read/write.
  8. *
  9. * Author: Ryan Wilson <[email protected]>
  10. * Konrad Rzeszutek Wilk <[email protected]>
  11. * Stefano Stabellini <[email protected]>
  12. */
  13. #include <linux/export.h>
  14. #include <linux/init.h>
  15. #include <linux/pci.h>
  16. #include <linux/acpi.h>
  17. #include <linux/io.h>
  18. #include <asm/io_apic.h>
  19. #include <asm/pci_x86.h>
  20. #include <asm/xen/hypervisor.h>
  21. #include <xen/features.h>
  22. #include <xen/events.h>
  23. #include <xen/pci.h>
  24. #include <asm/xen/pci.h>
  25. #include <asm/xen/cpuid.h>
  26. #include <asm/apic.h>
  27. #include <asm/acpi.h>
  28. #include <asm/i8259.h>
  29. static int xen_pcifront_enable_irq(struct pci_dev *dev)
  30. {
  31. int rc;
  32. int share = 1;
  33. int pirq;
  34. u8 gsi;
  35. rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi);
  36. if (rc < 0) {
  37. dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n",
  38. rc);
  39. return rc;
  40. }
  41. /* In PV DomU the Xen PCI backend puts the PIRQ in the interrupt line.*/
  42. pirq = gsi;
  43. if (gsi < nr_legacy_irqs())
  44. share = 0;
  45. rc = xen_bind_pirq_gsi_to_irq(gsi, pirq, share, "pcifront");
  46. if (rc < 0) {
  47. dev_warn(&dev->dev, "Xen PCI: failed to bind GSI%d (PIRQ%d) to IRQ: %d\n",
  48. gsi, pirq, rc);
  49. return rc;
  50. }
  51. dev->irq = rc;
  52. dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq);
  53. return 0;
  54. }
  55. #ifdef CONFIG_ACPI
  56. static int xen_register_pirq(u32 gsi, int triggering, bool set_pirq)
  57. {
  58. int rc, pirq = -1, irq;
  59. struct physdev_map_pirq map_irq;
  60. int shareable = 0;
  61. char *name;
  62. irq = xen_irq_from_gsi(gsi);
  63. if (irq > 0)
  64. return irq;
  65. if (set_pirq)
  66. pirq = gsi;
  67. map_irq.domid = DOMID_SELF;
  68. map_irq.type = MAP_PIRQ_TYPE_GSI;
  69. map_irq.index = gsi;
  70. map_irq.pirq = pirq;
  71. rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
  72. if (rc) {
  73. printk(KERN_WARNING "xen map irq failed %d\n", rc);
  74. return -1;
  75. }
  76. if (triggering == ACPI_EDGE_SENSITIVE) {
  77. shareable = 0;
  78. name = "ioapic-edge";
  79. } else {
  80. shareable = 1;
  81. name = "ioapic-level";
  82. }
  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, trigger,
  96. false /* no mapping of GSI to PIRQ */);
  97. }
  98. #ifdef CONFIG_XEN_PV_DOM0
  99. static int xen_register_gsi(u32 gsi, 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, 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, trigger, polarity);
  124. }
  125. #endif
  126. #endif
  127. #if defined(CONFIG_PCI_MSI)
  128. #include <linux/msi.h>
  129. struct xen_pci_frontend_ops *xen_pci_frontend;
  130. EXPORT_SYMBOL_GPL(xen_pci_frontend);
  131. struct xen_msi_ops {
  132. int (*setup_msi_irqs)(struct pci_dev *dev, int nvec, int type);
  133. void (*teardown_msi_irqs)(struct pci_dev *dev);
  134. };
  135. static struct xen_msi_ops xen_msi_ops __ro_after_init;
  136. static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  137. {
  138. int irq, ret, i;
  139. struct msi_desc *msidesc;
  140. int *v;
  141. if (type == PCI_CAP_ID_MSI && nvec > 1)
  142. return 1;
  143. v = kcalloc(max(1, nvec), sizeof(int), GFP_KERNEL);
  144. if (!v)
  145. return -ENOMEM;
  146. if (type == PCI_CAP_ID_MSIX)
  147. ret = xen_pci_frontend_enable_msix(dev, v, nvec);
  148. else
  149. ret = xen_pci_frontend_enable_msi(dev, v);
  150. if (ret)
  151. goto error;
  152. i = 0;
  153. msi_for_each_desc(msidesc, &dev->dev, MSI_DESC_NOTASSOCIATED) {
  154. irq = xen_bind_pirq_msi_to_irq(dev, msidesc, v[i],
  155. (type == PCI_CAP_ID_MSI) ? nvec : 1,
  156. (type == PCI_CAP_ID_MSIX) ?
  157. "pcifront-msi-x" :
  158. "pcifront-msi",
  159. DOMID_SELF);
  160. if (irq < 0) {
  161. ret = irq;
  162. goto free;
  163. }
  164. i++;
  165. }
  166. kfree(v);
  167. return msi_device_populate_sysfs(&dev->dev);
  168. error:
  169. if (ret == -ENOSYS)
  170. dev_err(&dev->dev, "Xen PCI frontend has not registered MSI/MSI-X support!\n");
  171. else if (ret)
  172. dev_err(&dev->dev, "Xen PCI frontend error: %d!\n", ret);
  173. free:
  174. kfree(v);
  175. return ret;
  176. }
  177. static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq,
  178. struct msi_msg *msg)
  179. {
  180. /*
  181. * We set vector == 0 to tell the hypervisor we don't care about
  182. * it, but we want a pirq setup instead. We use the dest_id fields
  183. * to pass the pirq that we want.
  184. */
  185. memset(msg, 0, sizeof(*msg));
  186. msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
  187. msg->arch_addr_hi.destid_8_31 = pirq >> 8;
  188. msg->arch_addr_lo.destid_0_7 = pirq & 0xFF;
  189. msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
  190. msg->arch_data.delivery_mode = APIC_DELIVERY_MODE_EXTINT;
  191. }
  192. static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  193. {
  194. int irq, pirq;
  195. struct msi_desc *msidesc;
  196. struct msi_msg msg;
  197. if (type == PCI_CAP_ID_MSI && nvec > 1)
  198. return 1;
  199. msi_for_each_desc(msidesc, &dev->dev, MSI_DESC_NOTASSOCIATED) {
  200. pirq = xen_allocate_pirq_msi(dev, msidesc);
  201. if (pirq < 0) {
  202. irq = -ENODEV;
  203. goto error;
  204. }
  205. xen_msi_compose_msg(dev, pirq, &msg);
  206. __pci_write_msi_msg(msidesc, &msg);
  207. dev_dbg(&dev->dev, "xen: msi bound to pirq=%d\n", pirq);
  208. irq = xen_bind_pirq_msi_to_irq(dev, msidesc, pirq,
  209. (type == PCI_CAP_ID_MSI) ? nvec : 1,
  210. (type == PCI_CAP_ID_MSIX) ?
  211. "msi-x" : "msi",
  212. DOMID_SELF);
  213. if (irq < 0)
  214. goto error;
  215. dev_dbg(&dev->dev,
  216. "xen: msi --> pirq=%d --> irq=%d\n", pirq, irq);
  217. }
  218. return msi_device_populate_sysfs(&dev->dev);
  219. error:
  220. dev_err(&dev->dev, "Failed to create MSI%s! ret=%d!\n",
  221. type == PCI_CAP_ID_MSI ? "" : "-X", irq);
  222. return irq;
  223. }
  224. #ifdef CONFIG_XEN_PV_DOM0
  225. static bool __read_mostly pci_seg_supported = true;
  226. static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  227. {
  228. int ret = 0;
  229. struct msi_desc *msidesc;
  230. msi_for_each_desc(msidesc, &dev->dev, MSI_DESC_NOTASSOCIATED) {
  231. struct physdev_map_pirq map_irq;
  232. domid_t domid;
  233. domid = ret = xen_find_device_domain_owner(dev);
  234. /* N.B. Casting int's -ENODEV to uint16_t results in 0xFFED,
  235. * hence check ret value for < 0. */
  236. if (ret < 0)
  237. domid = DOMID_SELF;
  238. memset(&map_irq, 0, sizeof(map_irq));
  239. map_irq.domid = domid;
  240. map_irq.type = MAP_PIRQ_TYPE_MSI_SEG;
  241. map_irq.index = -1;
  242. map_irq.pirq = -1;
  243. map_irq.bus = dev->bus->number |
  244. (pci_domain_nr(dev->bus) << 16);
  245. map_irq.devfn = dev->devfn;
  246. if (type == PCI_CAP_ID_MSI && nvec > 1) {
  247. map_irq.type = MAP_PIRQ_TYPE_MULTI_MSI;
  248. map_irq.entry_nr = nvec;
  249. } else if (type == PCI_CAP_ID_MSIX) {
  250. int pos;
  251. unsigned long flags;
  252. u32 table_offset, bir;
  253. pos = dev->msix_cap;
  254. pci_read_config_dword(dev, pos + PCI_MSIX_TABLE,
  255. &table_offset);
  256. bir = (u8)(table_offset & PCI_MSIX_TABLE_BIR);
  257. flags = pci_resource_flags(dev, bir);
  258. if (!flags || (flags & IORESOURCE_UNSET))
  259. return -EINVAL;
  260. map_irq.table_base = pci_resource_start(dev, bir);
  261. map_irq.entry_nr = msidesc->msi_index;
  262. }
  263. ret = -EINVAL;
  264. if (pci_seg_supported)
  265. ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq,
  266. &map_irq);
  267. if (type == PCI_CAP_ID_MSI && nvec > 1 && ret) {
  268. /*
  269. * If MAP_PIRQ_TYPE_MULTI_MSI is not available
  270. * there's nothing else we can do in this case.
  271. * Just set ret > 0 so driver can retry with
  272. * single MSI.
  273. */
  274. ret = 1;
  275. goto out;
  276. }
  277. if (ret == -EINVAL && !pci_domain_nr(dev->bus)) {
  278. map_irq.type = MAP_PIRQ_TYPE_MSI;
  279. map_irq.index = -1;
  280. map_irq.pirq = -1;
  281. map_irq.bus = dev->bus->number;
  282. ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq,
  283. &map_irq);
  284. if (ret != -EINVAL)
  285. pci_seg_supported = false;
  286. }
  287. if (ret) {
  288. dev_warn(&dev->dev, "xen map irq failed %d for %d domain\n",
  289. ret, domid);
  290. goto out;
  291. }
  292. ret = xen_bind_pirq_msi_to_irq(dev, msidesc, map_irq.pirq,
  293. (type == PCI_CAP_ID_MSI) ? nvec : 1,
  294. (type == PCI_CAP_ID_MSIX) ? "msi-x" : "msi",
  295. domid);
  296. if (ret < 0)
  297. goto out;
  298. }
  299. ret = msi_device_populate_sysfs(&dev->dev);
  300. out:
  301. return ret;
  302. }
  303. bool xen_initdom_restore_msi(struct pci_dev *dev)
  304. {
  305. int ret = 0;
  306. if (!xen_initial_domain())
  307. return true;
  308. if (pci_seg_supported) {
  309. struct physdev_pci_device restore_ext;
  310. restore_ext.seg = pci_domain_nr(dev->bus);
  311. restore_ext.bus = dev->bus->number;
  312. restore_ext.devfn = dev->devfn;
  313. ret = HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi_ext,
  314. &restore_ext);
  315. if (ret == -ENOSYS)
  316. pci_seg_supported = false;
  317. WARN(ret && ret != -ENOSYS, "restore_msi_ext -> %d\n", ret);
  318. }
  319. if (!pci_seg_supported) {
  320. struct physdev_restore_msi restore;
  321. restore.bus = dev->bus->number;
  322. restore.devfn = dev->devfn;
  323. ret = HYPERVISOR_physdev_op(PHYSDEVOP_restore_msi, &restore);
  324. WARN(ret && ret != -ENOSYS, "restore_msi -> %d\n", ret);
  325. }
  326. return false;
  327. }
  328. #else /* CONFIG_XEN_PV_DOM0 */
  329. #define xen_initdom_setup_msi_irqs NULL
  330. #endif /* !CONFIG_XEN_PV_DOM0 */
  331. static void xen_teardown_msi_irqs(struct pci_dev *dev)
  332. {
  333. struct msi_desc *msidesc;
  334. int i;
  335. msi_for_each_desc(msidesc, &dev->dev, MSI_DESC_ASSOCIATED) {
  336. for (i = 0; i < msidesc->nvec_used; i++)
  337. xen_destroy_irq(msidesc->irq + i);
  338. }
  339. msi_device_destroy_sysfs(&dev->dev);
  340. }
  341. static void xen_pv_teardown_msi_irqs(struct pci_dev *dev)
  342. {
  343. if (dev->msix_enabled)
  344. xen_pci_frontend_disable_msix(dev);
  345. else
  346. xen_pci_frontend_disable_msi(dev);
  347. xen_teardown_msi_irqs(dev);
  348. }
  349. static int xen_msi_domain_alloc_irqs(struct irq_domain *domain,
  350. struct device *dev, int nvec)
  351. {
  352. int type;
  353. if (WARN_ON_ONCE(!dev_is_pci(dev)))
  354. return -EINVAL;
  355. type = to_pci_dev(dev)->msix_enabled ? PCI_CAP_ID_MSIX : PCI_CAP_ID_MSI;
  356. return xen_msi_ops.setup_msi_irqs(to_pci_dev(dev), nvec, type);
  357. }
  358. static void xen_msi_domain_free_irqs(struct irq_domain *domain,
  359. struct device *dev)
  360. {
  361. if (WARN_ON_ONCE(!dev_is_pci(dev)))
  362. return;
  363. xen_msi_ops.teardown_msi_irqs(to_pci_dev(dev));
  364. }
  365. static struct msi_domain_ops xen_pci_msi_domain_ops = {
  366. .domain_alloc_irqs = xen_msi_domain_alloc_irqs,
  367. .domain_free_irqs = xen_msi_domain_free_irqs,
  368. };
  369. static struct msi_domain_info xen_pci_msi_domain_info = {
  370. .ops = &xen_pci_msi_domain_ops,
  371. };
  372. /*
  373. * This irq domain is a blatant violation of the irq domain design, but
  374. * distangling XEN into real irq domains is not a job for mere mortals with
  375. * limited XENology. But it's the least dangerous way for a mere mortal to
  376. * get rid of the arch_*_msi_irqs() hackery in order to store the irq
  377. * domain pointer in struct device. This irq domain wrappery allows to do
  378. * that without breaking XEN terminally.
  379. */
  380. static __init struct irq_domain *xen_create_pci_msi_domain(void)
  381. {
  382. struct irq_domain *d = NULL;
  383. struct fwnode_handle *fn;
  384. fn = irq_domain_alloc_named_fwnode("XEN-MSI");
  385. if (fn)
  386. d = msi_create_irq_domain(fn, &xen_pci_msi_domain_info, NULL);
  387. /* FIXME: No idea how to survive if this fails */
  388. BUG_ON(!d);
  389. return d;
  390. }
  391. static __init void xen_setup_pci_msi(void)
  392. {
  393. if (xen_pv_domain()) {
  394. if (xen_initial_domain())
  395. xen_msi_ops.setup_msi_irqs = xen_initdom_setup_msi_irqs;
  396. else
  397. xen_msi_ops.setup_msi_irqs = xen_setup_msi_irqs;
  398. xen_msi_ops.teardown_msi_irqs = xen_pv_teardown_msi_irqs;
  399. } else if (xen_hvm_domain()) {
  400. xen_msi_ops.setup_msi_irqs = xen_hvm_setup_msi_irqs;
  401. xen_msi_ops.teardown_msi_irqs = xen_teardown_msi_irqs;
  402. } else {
  403. WARN_ON_ONCE(1);
  404. return;
  405. }
  406. /*
  407. * Override the PCI/MSI irq domain init function. No point
  408. * in allocating the native domain and never use it.
  409. */
  410. x86_init.irqs.create_pci_msi_domain = xen_create_pci_msi_domain;
  411. /*
  412. * With XEN PIRQ/Eventchannels in use PCI/MSI[-X] masking is solely
  413. * controlled by the hypervisor.
  414. */
  415. pci_msi_ignore_mask = 1;
  416. }
  417. #else /* CONFIG_PCI_MSI */
  418. static inline void xen_setup_pci_msi(void) { }
  419. #endif /* CONFIG_PCI_MSI */
  420. int __init pci_xen_init(void)
  421. {
  422. if (!xen_pv_domain() || xen_initial_domain())
  423. return -ENODEV;
  424. printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n");
  425. pcibios_set_cache_line_size();
  426. pcibios_enable_irq = xen_pcifront_enable_irq;
  427. pcibios_disable_irq = NULL;
  428. /* Keep ACPI out of the picture */
  429. acpi_noirq_set();
  430. xen_setup_pci_msi();
  431. return 0;
  432. }
  433. #ifdef CONFIG_PCI_MSI
  434. static void __init xen_hvm_msi_init(void)
  435. {
  436. if (!disable_apic) {
  437. /*
  438. * If hardware supports (x2)APIC virtualization (as indicated
  439. * by hypervisor's leaf 4) then we don't need to use pirqs/
  440. * event channels for MSI handling and instead use regular
  441. * APIC processing
  442. */
  443. uint32_t eax = cpuid_eax(xen_cpuid_base() + 4);
  444. if (((eax & XEN_HVM_CPUID_X2APIC_VIRT) && x2apic_mode) ||
  445. ((eax & XEN_HVM_CPUID_APIC_ACCESS_VIRT) && boot_cpu_has(X86_FEATURE_APIC)))
  446. return;
  447. }
  448. xen_setup_pci_msi();
  449. }
  450. #endif
  451. int __init pci_xen_hvm_init(void)
  452. {
  453. if (!xen_have_vector_callback || !xen_feature(XENFEAT_hvm_pirqs))
  454. return 0;
  455. #ifdef CONFIG_ACPI
  456. /*
  457. * We don't want to change the actual ACPI delivery model,
  458. * just how GSIs get registered.
  459. */
  460. __acpi_register_gsi = acpi_register_gsi_xen_hvm;
  461. __acpi_unregister_gsi = NULL;
  462. #endif
  463. #ifdef CONFIG_PCI_MSI
  464. /*
  465. * We need to wait until after x2apic is initialized
  466. * before we can set MSI IRQ ops.
  467. */
  468. x86_platform.apic_post_init = xen_hvm_msi_init;
  469. #endif
  470. return 0;
  471. }
  472. #ifdef CONFIG_XEN_PV_DOM0
  473. int __init pci_xen_initial_domain(void)
  474. {
  475. int irq;
  476. xen_setup_pci_msi();
  477. __acpi_register_gsi = acpi_register_gsi_xen;
  478. __acpi_unregister_gsi = NULL;
  479. /*
  480. * Pre-allocate the legacy IRQs. Use NR_LEGACY_IRQS here
  481. * because we don't have a PIC and thus nr_legacy_irqs() is zero.
  482. */
  483. for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
  484. int trigger, polarity;
  485. if (acpi_get_override_irq(irq, &trigger, &polarity) == -1)
  486. continue;
  487. xen_register_pirq(irq,
  488. trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE,
  489. true /* Map GSI to PIRQ */);
  490. }
  491. if (0 == nr_ioapics) {
  492. for (irq = 0; irq < nr_legacy_irqs(); irq++)
  493. xen_bind_pirq_gsi_to_irq(irq, irq, 0, "xt-pic");
  494. }
  495. return 0;
  496. }
  497. #endif