platform-pci.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. 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)0x01 << HVM_CALLBACK_VIA_TYPE_SHIFT) | /* PCI INTx identifier */
  46. ((uint64_t)pci_domain_nr(pdev->bus) << 32) |
  47. ((uint64_t)pdev->bus->number << 16) |
  48. ((uint64_t)(pdev->devfn & 0xff) << 8) |
  49. ((uint64_t)(pin - 1) & 3);
  50. }
  51. static irqreturn_t do_hvm_evtchn_intr(int irq, void *dev_id)
  52. {
  53. xen_hvm_evtchn_do_upcall();
  54. return IRQ_HANDLED;
  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_TRIGGER_RISING,
  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. callback_via = get_callback_via(pdev);
  110. ret = xen_set_callback_via(callback_via);
  111. if (ret) {
  112. dev_warn(&pdev->dev, "Unable to set the evtchn callback "
  113. "err=%d\n", ret);
  114. goto out;
  115. }
  116. }
  117. max_nr_gframes = gnttab_max_grant_frames();
  118. grant_frames = alloc_xen_mmio(PAGE_SIZE * max_nr_gframes);
  119. ret = gnttab_setup_auto_xlat_frames(grant_frames);
  120. if (ret)
  121. goto out;
  122. ret = gnttab_init();
  123. if (ret)
  124. goto grant_out;
  125. return 0;
  126. grant_out:
  127. gnttab_free_auto_xlat_frames();
  128. out:
  129. pci_release_region(pdev, 0);
  130. mem_out:
  131. pci_release_region(pdev, 1);
  132. pci_out:
  133. pci_disable_device(pdev);
  134. return ret;
  135. }
  136. static const struct pci_device_id platform_pci_tbl[] = {
  137. {PCI_VENDOR_ID_XEN, PCI_DEVICE_ID_XEN_PLATFORM,
  138. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  139. {0,}
  140. };
  141. static const struct dev_pm_ops platform_pm_ops = {
  142. .resume_noirq = platform_pci_resume,
  143. };
  144. static struct pci_driver platform_driver = {
  145. .name = DRV_NAME,
  146. .probe = platform_pci_probe,
  147. .id_table = platform_pci_tbl,
  148. .driver = {
  149. .pm = &platform_pm_ops,
  150. },
  151. };
  152. builtin_pci_driver(platform_driver);