platform-pci.c 4.3 KB

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