platform-pci.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. * platform-pci.c
  4. *
  5. * Xen platform PCI device driver
  6. *
  7. * Authors: [email protected] and [email protected]
  8. *
  9. * Copyright (c) 2005, Intel Corporation.
  10. * Copyright (c) 2007, XenSource Inc.
  11. * Copyright (c) 2010, Citrix
  12. */
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/init.h>
  16. #include <linux/pci.h>
  17. #include <xen/platform_pci.h>
  18. #include <xen/grant_table.h>
  19. #include <xen/xenbus.h>
  20. #include <xen/events.h>
  21. #include <xen/hvm.h>
  22. #include <xen/xen-ops.h>
  23. #define DRV_NAME "xen-platform-pci"
  24. static unsigned long platform_mmio;
  25. static unsigned long platform_mmio_alloc;
  26. static unsigned long platform_mmiolen;
  27. static uint64_t callback_via;
  28. static unsigned long alloc_xen_mmio(unsigned long len)
  29. {
  30. unsigned long addr;
  31. addr = platform_mmio + platform_mmio_alloc;
  32. platform_mmio_alloc += len;
  33. BUG_ON(platform_mmio_alloc > platform_mmiolen);
  34. return addr;
  35. }
  36. static uint64_t get_callback_via(struct pci_dev *pdev)
  37. {
  38. u8 pin;
  39. int irq;
  40. irq = pdev->irq;
  41. if (irq < 16)
  42. return irq; /* ISA IRQ */
  43. pin = pdev->pin;
  44. /* We don't know the GSI. Specify the PCI INTx line instead. */
  45. return ((uint64_t)HVM_PARAM_CALLBACK_TYPE_PCI_INTX <<
  46. HVM_CALLBACK_VIA_TYPE_SHIFT) |
  47. ((uint64_t)pci_domain_nr(pdev->bus) << 32) |
  48. ((uint64_t)pdev->bus->number << 16) |
  49. ((uint64_t)(pdev->devfn & 0xff) << 8) |
  50. ((uint64_t)(pin - 1) & 3);
  51. }
  52. static irqreturn_t do_hvm_evtchn_intr(int irq, void *dev_id)
  53. {
  54. return xen_evtchn_do_upcall();
  55. }
  56. static int xen_allocate_irq(struct pci_dev *pdev)
  57. {
  58. return request_irq(pdev->irq, do_hvm_evtchn_intr,
  59. IRQF_NOBALANCING | IRQF_SHARED,
  60. "xen-platform-pci", pdev);
  61. }
  62. static int platform_pci_resume(struct device *dev)
  63. {
  64. int err;
  65. if (xen_have_vector_callback)
  66. return 0;
  67. err = xen_set_callback_via(callback_via);
  68. if (err) {
  69. dev_err(dev, "platform_pci_resume failure!\n");
  70. return err;
  71. }
  72. return 0;
  73. }
  74. static int platform_pci_probe(struct pci_dev *pdev,
  75. const struct pci_device_id *ent)
  76. {
  77. int i, ret;
  78. long ioaddr;
  79. long mmio_addr, mmio_len;
  80. unsigned int max_nr_gframes;
  81. unsigned long grant_frames;
  82. if (!xen_domain())
  83. return -ENODEV;
  84. i = pci_enable_device(pdev);
  85. if (i)
  86. return i;
  87. ioaddr = pci_resource_start(pdev, 0);
  88. mmio_addr = pci_resource_start(pdev, 1);
  89. mmio_len = pci_resource_len(pdev, 1);
  90. if (mmio_addr == 0 || ioaddr == 0) {
  91. dev_err(&pdev->dev, "no resources found\n");
  92. ret = -ENOENT;
  93. goto pci_out;
  94. }
  95. ret = pci_request_region(pdev, 1, DRV_NAME);
  96. if (ret < 0)
  97. goto pci_out;
  98. ret = pci_request_region(pdev, 0, DRV_NAME);
  99. if (ret < 0)
  100. goto mem_out;
  101. platform_mmio = mmio_addr;
  102. platform_mmiolen = mmio_len;
  103. if (!xen_have_vector_callback) {
  104. ret = xen_allocate_irq(pdev);
  105. if (ret) {
  106. dev_warn(&pdev->dev, "request_irq failed err=%d\n", ret);
  107. goto out;
  108. }
  109. /*
  110. * It doesn't strictly *have* to run on CPU0 but it sure
  111. * as hell better process the event channel ports delivered
  112. * to CPU0.
  113. */
  114. irq_set_affinity(pdev->irq, cpumask_of(0));
  115. callback_via = get_callback_via(pdev);
  116. ret = xen_set_callback_via(callback_via);
  117. if (ret) {
  118. dev_warn(&pdev->dev, "Unable to set the evtchn callback "
  119. "err=%d\n", ret);
  120. goto irq_out;
  121. }
  122. }
  123. max_nr_gframes = gnttab_max_grant_frames();
  124. grant_frames = alloc_xen_mmio(PAGE_SIZE * max_nr_gframes);
  125. ret = gnttab_setup_auto_xlat_frames(grant_frames);
  126. if (ret)
  127. goto irq_out;
  128. ret = gnttab_init();
  129. if (ret)
  130. goto grant_out;
  131. return 0;
  132. grant_out:
  133. gnttab_free_auto_xlat_frames();
  134. irq_out:
  135. if (!xen_have_vector_callback)
  136. free_irq(pdev->irq, pdev);
  137. out:
  138. pci_release_region(pdev, 0);
  139. mem_out:
  140. pci_release_region(pdev, 1);
  141. pci_out:
  142. pci_disable_device(pdev);
  143. return ret;
  144. }
  145. static const struct pci_device_id platform_pci_tbl[] = {
  146. {PCI_VENDOR_ID_XEN, PCI_DEVICE_ID_XEN_PLATFORM,
  147. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  148. {0,}
  149. };
  150. static const struct dev_pm_ops platform_pm_ops = {
  151. .resume_noirq = platform_pci_resume,
  152. };
  153. static struct pci_driver platform_driver = {
  154. .name = DRV_NAME,
  155. .probe = platform_pci_probe,
  156. .id_table = platform_pci_tbl,
  157. .driver = {
  158. .pm = &platform_pm_ops,
  159. },
  160. };
  161. builtin_pci_driver(platform_driver);