pciback_ops.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI Backend Operations - respond to PCI requests from Frontend
  4. *
  5. * Author: Ryan Wilson <[email protected]>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #define dev_fmt pr_fmt
  9. #include <linux/moduleparam.h>
  10. #include <linux/wait.h>
  11. #include <linux/bitops.h>
  12. #include <xen/events.h>
  13. #include <linux/sched.h>
  14. #include "pciback.h"
  15. static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id);
  16. /* Ensure a device is has the fake IRQ handler "turned on/off" and is
  17. * ready to be exported. This MUST be run after xen_pcibk_reset_device
  18. * which does the actual PCI device enable/disable.
  19. */
  20. static void xen_pcibk_control_isr(struct pci_dev *dev, int reset)
  21. {
  22. struct xen_pcibk_dev_data *dev_data;
  23. int rc;
  24. int enable = 0;
  25. dev_data = pci_get_drvdata(dev);
  26. if (!dev_data)
  27. return;
  28. /* We don't deal with bridges */
  29. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL)
  30. return;
  31. if (reset) {
  32. dev_data->enable_intx = 0;
  33. dev_data->ack_intr = 0;
  34. }
  35. enable = dev_data->enable_intx;
  36. /* Asked to disable, but ISR isn't runnig */
  37. if (!enable && !dev_data->isr_on)
  38. return;
  39. /* Squirrel away the IRQs in the dev_data. We need this
  40. * b/c when device transitions to MSI, the dev->irq is
  41. * overwritten with the MSI vector.
  42. */
  43. if (enable)
  44. dev_data->irq = dev->irq;
  45. /*
  46. * SR-IOV devices in all use MSI-X and have no legacy
  47. * interrupts, so inhibit creating a fake IRQ handler for them.
  48. */
  49. if (dev_data->irq == 0)
  50. goto out;
  51. dev_dbg(&dev->dev, "%s: #%d %s %s%s %s-> %s\n",
  52. dev_data->irq_name,
  53. dev_data->irq,
  54. pci_is_enabled(dev) ? "on" : "off",
  55. dev->msi_enabled ? "MSI" : "",
  56. dev->msix_enabled ? "MSI/X" : "",
  57. dev_data->isr_on ? "enable" : "disable",
  58. enable ? "enable" : "disable");
  59. if (enable) {
  60. /*
  61. * The MSI or MSI-X should not have an IRQ handler. Otherwise
  62. * if the guest terminates we BUG_ON in free_msi_irqs.
  63. */
  64. if (dev->msi_enabled || dev->msix_enabled)
  65. goto out;
  66. rc = request_irq(dev_data->irq,
  67. xen_pcibk_guest_interrupt, IRQF_SHARED,
  68. dev_data->irq_name, dev);
  69. if (rc) {
  70. dev_err(&dev->dev, "%s: failed to install fake IRQ " \
  71. "handler for IRQ %d! (rc:%d)\n",
  72. dev_data->irq_name, dev_data->irq, rc);
  73. goto out;
  74. }
  75. } else {
  76. free_irq(dev_data->irq, dev);
  77. dev_data->irq = 0;
  78. }
  79. dev_data->isr_on = enable;
  80. dev_data->ack_intr = enable;
  81. out:
  82. dev_dbg(&dev->dev, "%s: #%d %s %s%s %s\n",
  83. dev_data->irq_name,
  84. dev_data->irq,
  85. pci_is_enabled(dev) ? "on" : "off",
  86. dev->msi_enabled ? "MSI" : "",
  87. dev->msix_enabled ? "MSI/X" : "",
  88. enable ? (dev_data->isr_on ? "enabled" : "failed to enable") :
  89. (dev_data->isr_on ? "failed to disable" : "disabled"));
  90. }
  91. /* Ensure a device is "turned off" and ready to be exported.
  92. * (Also see xen_pcibk_config_reset to ensure virtual configuration space is
  93. * ready to be re-exported)
  94. */
  95. void xen_pcibk_reset_device(struct pci_dev *dev)
  96. {
  97. u16 cmd;
  98. xen_pcibk_control_isr(dev, 1 /* reset device */);
  99. /* Disable devices (but not bridges) */
  100. if (dev->hdr_type == PCI_HEADER_TYPE_NORMAL) {
  101. #ifdef CONFIG_PCI_MSI
  102. /* The guest could have been abruptly killed without
  103. * disabling MSI/MSI-X interrupts.*/
  104. if (dev->msix_enabled)
  105. pci_disable_msix(dev);
  106. if (dev->msi_enabled)
  107. pci_disable_msi(dev);
  108. #endif
  109. if (pci_is_enabled(dev))
  110. pci_disable_device(dev);
  111. dev->is_busmaster = 0;
  112. } else {
  113. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  114. if (cmd & (PCI_COMMAND_INVALIDATE)) {
  115. cmd &= ~(PCI_COMMAND_INVALIDATE);
  116. pci_write_config_word(dev, PCI_COMMAND, cmd);
  117. dev->is_busmaster = 0;
  118. }
  119. }
  120. }
  121. #ifdef CONFIG_PCI_MSI
  122. static
  123. int xen_pcibk_enable_msi(struct xen_pcibk_device *pdev,
  124. struct pci_dev *dev, struct xen_pci_op *op)
  125. {
  126. struct xen_pcibk_dev_data *dev_data;
  127. int status;
  128. if (dev->msi_enabled)
  129. status = -EALREADY;
  130. else if (dev->msix_enabled)
  131. status = -ENXIO;
  132. else
  133. status = pci_enable_msi(dev);
  134. if (status) {
  135. dev_warn_ratelimited(&dev->dev, "error enabling MSI for guest %u: err %d\n",
  136. pdev->xdev->otherend_id, status);
  137. op->value = 0;
  138. return XEN_PCI_ERR_op_failed;
  139. }
  140. /* The value the guest needs is actually the IDT vector, not
  141. * the local domain's IRQ number. */
  142. op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
  143. dev_dbg(&dev->dev, "MSI: %d\n", op->value);
  144. dev_data = pci_get_drvdata(dev);
  145. if (dev_data)
  146. dev_data->ack_intr = 0;
  147. return 0;
  148. }
  149. static
  150. int xen_pcibk_disable_msi(struct xen_pcibk_device *pdev,
  151. struct pci_dev *dev, struct xen_pci_op *op)
  152. {
  153. if (dev->msi_enabled) {
  154. struct xen_pcibk_dev_data *dev_data;
  155. pci_disable_msi(dev);
  156. dev_data = pci_get_drvdata(dev);
  157. if (dev_data)
  158. dev_data->ack_intr = 1;
  159. }
  160. op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
  161. dev_dbg(&dev->dev, "MSI: %d\n", op->value);
  162. return 0;
  163. }
  164. static
  165. int xen_pcibk_enable_msix(struct xen_pcibk_device *pdev,
  166. struct pci_dev *dev, struct xen_pci_op *op)
  167. {
  168. struct xen_pcibk_dev_data *dev_data;
  169. int i, result;
  170. struct msix_entry *entries;
  171. u16 cmd;
  172. dev_dbg(&dev->dev, "enable MSI-X\n");
  173. if (op->value > SH_INFO_MAX_VEC)
  174. return -EINVAL;
  175. if (dev->msix_enabled)
  176. return -EALREADY;
  177. /*
  178. * PCI_COMMAND_MEMORY must be enabled, otherwise we may not be able
  179. * to access the BARs where the MSI-X entries reside.
  180. * But VF devices are unique in which the PF needs to be checked.
  181. */
  182. pci_read_config_word(pci_physfn(dev), PCI_COMMAND, &cmd);
  183. if (dev->msi_enabled || !(cmd & PCI_COMMAND_MEMORY))
  184. return -ENXIO;
  185. entries = kmalloc_array(op->value, sizeof(*entries), GFP_KERNEL);
  186. if (entries == NULL)
  187. return -ENOMEM;
  188. for (i = 0; i < op->value; i++) {
  189. entries[i].entry = op->msix_entries[i].entry;
  190. entries[i].vector = op->msix_entries[i].vector;
  191. }
  192. result = pci_enable_msix_exact(dev, entries, op->value);
  193. if (result == 0) {
  194. for (i = 0; i < op->value; i++) {
  195. op->msix_entries[i].entry = entries[i].entry;
  196. if (entries[i].vector) {
  197. op->msix_entries[i].vector =
  198. xen_pirq_from_irq(entries[i].vector);
  199. dev_dbg(&dev->dev, "MSI-X[%d]: %d\n", i,
  200. op->msix_entries[i].vector);
  201. }
  202. }
  203. } else
  204. dev_warn_ratelimited(&dev->dev, "error enabling MSI-X for guest %u: err %d!\n",
  205. pdev->xdev->otherend_id, result);
  206. kfree(entries);
  207. op->value = result;
  208. dev_data = pci_get_drvdata(dev);
  209. if (dev_data)
  210. dev_data->ack_intr = 0;
  211. return result > 0 ? 0 : result;
  212. }
  213. static
  214. int xen_pcibk_disable_msix(struct xen_pcibk_device *pdev,
  215. struct pci_dev *dev, struct xen_pci_op *op)
  216. {
  217. if (dev->msix_enabled) {
  218. struct xen_pcibk_dev_data *dev_data;
  219. pci_disable_msix(dev);
  220. dev_data = pci_get_drvdata(dev);
  221. if (dev_data)
  222. dev_data->ack_intr = 1;
  223. }
  224. /*
  225. * SR-IOV devices (which don't have any legacy IRQ) have
  226. * an undefined IRQ value of zero.
  227. */
  228. op->value = dev->irq ? xen_pirq_from_irq(dev->irq) : 0;
  229. dev_dbg(&dev->dev, "MSI-X: %d\n", op->value);
  230. return 0;
  231. }
  232. #endif
  233. static inline bool xen_pcibk_test_op_pending(struct xen_pcibk_device *pdev)
  234. {
  235. return test_bit(_XEN_PCIF_active,
  236. (unsigned long *)&pdev->sh_info->flags) &&
  237. !test_and_set_bit(_PDEVF_op_active, &pdev->flags);
  238. }
  239. /*
  240. * Now the same evtchn is used for both pcifront conf_read_write request
  241. * as well as pcie aer front end ack. We use a new work_queue to schedule
  242. * xen_pcibk conf_read_write service for avoiding confict with aer_core
  243. * do_recovery job which also use the system default work_queue
  244. */
  245. static void xen_pcibk_test_and_schedule_op(struct xen_pcibk_device *pdev)
  246. {
  247. bool eoi = true;
  248. /* Check that frontend is requesting an operation and that we are not
  249. * already processing a request */
  250. if (xen_pcibk_test_op_pending(pdev)) {
  251. schedule_work(&pdev->op_work);
  252. eoi = false;
  253. }
  254. /*_XEN_PCIB_active should have been cleared by pcifront. And also make
  255. sure xen_pcibk is waiting for ack by checking _PCIB_op_pending*/
  256. if (!test_bit(_XEN_PCIB_active, (unsigned long *)&pdev->sh_info->flags)
  257. && test_bit(_PCIB_op_pending, &pdev->flags)) {
  258. wake_up(&xen_pcibk_aer_wait_queue);
  259. eoi = false;
  260. }
  261. /* EOI if there was nothing to do. */
  262. if (eoi)
  263. xen_pcibk_lateeoi(pdev, XEN_EOI_FLAG_SPURIOUS);
  264. }
  265. /* Performing the configuration space reads/writes must not be done in atomic
  266. * context because some of the pci_* functions can sleep (mostly due to ACPI
  267. * use of semaphores). This function is intended to be called from a work
  268. * queue in process context taking a struct xen_pcibk_device as a parameter */
  269. static void xen_pcibk_do_one_op(struct xen_pcibk_device *pdev)
  270. {
  271. struct pci_dev *dev;
  272. struct xen_pcibk_dev_data *dev_data = NULL;
  273. struct xen_pci_op *op = &pdev->op;
  274. int test_intx = 0;
  275. #ifdef CONFIG_PCI_MSI
  276. unsigned int nr = 0;
  277. #endif
  278. *op = pdev->sh_info->op;
  279. barrier();
  280. dev = xen_pcibk_get_pci_dev(pdev, op->domain, op->bus, op->devfn);
  281. if (dev == NULL)
  282. op->err = XEN_PCI_ERR_dev_not_found;
  283. else {
  284. dev_data = pci_get_drvdata(dev);
  285. if (dev_data)
  286. test_intx = dev_data->enable_intx;
  287. switch (op->cmd) {
  288. case XEN_PCI_OP_conf_read:
  289. op->err = xen_pcibk_config_read(dev,
  290. op->offset, op->size, &op->value);
  291. break;
  292. case XEN_PCI_OP_conf_write:
  293. op->err = xen_pcibk_config_write(dev,
  294. op->offset, op->size, op->value);
  295. break;
  296. #ifdef CONFIG_PCI_MSI
  297. case XEN_PCI_OP_enable_msi:
  298. op->err = xen_pcibk_enable_msi(pdev, dev, op);
  299. break;
  300. case XEN_PCI_OP_disable_msi:
  301. op->err = xen_pcibk_disable_msi(pdev, dev, op);
  302. break;
  303. case XEN_PCI_OP_enable_msix:
  304. nr = op->value;
  305. op->err = xen_pcibk_enable_msix(pdev, dev, op);
  306. break;
  307. case XEN_PCI_OP_disable_msix:
  308. op->err = xen_pcibk_disable_msix(pdev, dev, op);
  309. break;
  310. #endif
  311. default:
  312. op->err = XEN_PCI_ERR_not_implemented;
  313. break;
  314. }
  315. }
  316. if (!op->err && dev && dev_data) {
  317. /* Transition detected */
  318. if ((dev_data->enable_intx != test_intx))
  319. xen_pcibk_control_isr(dev, 0 /* no reset */);
  320. }
  321. pdev->sh_info->op.err = op->err;
  322. pdev->sh_info->op.value = op->value;
  323. #ifdef CONFIG_PCI_MSI
  324. if (op->cmd == XEN_PCI_OP_enable_msix && op->err == 0) {
  325. unsigned int i;
  326. for (i = 0; i < nr; i++)
  327. pdev->sh_info->op.msix_entries[i].vector =
  328. op->msix_entries[i].vector;
  329. }
  330. #endif
  331. /* Tell the driver domain that we're done. */
  332. wmb();
  333. clear_bit(_XEN_PCIF_active, (unsigned long *)&pdev->sh_info->flags);
  334. notify_remote_via_irq(pdev->evtchn_irq);
  335. /* Mark that we're done. */
  336. smp_mb__before_atomic(); /* /after/ clearing PCIF_active */
  337. clear_bit(_PDEVF_op_active, &pdev->flags);
  338. smp_mb__after_atomic(); /* /before/ final check for work */
  339. }
  340. void xen_pcibk_do_op(struct work_struct *data)
  341. {
  342. struct xen_pcibk_device *pdev =
  343. container_of(data, struct xen_pcibk_device, op_work);
  344. do {
  345. xen_pcibk_do_one_op(pdev);
  346. } while (xen_pcibk_test_op_pending(pdev));
  347. xen_pcibk_lateeoi(pdev, 0);
  348. }
  349. irqreturn_t xen_pcibk_handle_event(int irq, void *dev_id)
  350. {
  351. struct xen_pcibk_device *pdev = dev_id;
  352. bool eoi;
  353. /* IRQs might come in before pdev->evtchn_irq is written. */
  354. if (unlikely(pdev->evtchn_irq != irq))
  355. pdev->evtchn_irq = irq;
  356. eoi = test_and_set_bit(_EOI_pending, &pdev->flags);
  357. WARN(eoi, "IRQ while EOI pending\n");
  358. xen_pcibk_test_and_schedule_op(pdev);
  359. return IRQ_HANDLED;
  360. }
  361. static irqreturn_t xen_pcibk_guest_interrupt(int irq, void *dev_id)
  362. {
  363. struct pci_dev *dev = (struct pci_dev *)dev_id;
  364. struct xen_pcibk_dev_data *dev_data = pci_get_drvdata(dev);
  365. if (dev_data->isr_on && dev_data->ack_intr) {
  366. dev_data->handled++;
  367. if ((dev_data->handled % 1000) == 0) {
  368. if (xen_test_irq_shared(irq)) {
  369. dev_info(&dev->dev, "%s IRQ line is not shared "
  370. "with other domains. Turning ISR off\n",
  371. dev_data->irq_name);
  372. dev_data->ack_intr = 0;
  373. }
  374. }
  375. return IRQ_HANDLED;
  376. }
  377. return IRQ_NONE;
  378. }