xen.c 13 KB

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