platform-pci.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /******************************************************************************
  2. * platform-pci.c
  3. *
  4. * Xen platform PCI device driver
  5. * Copyright (c) 2005, Intel Corporation.
  6. * Copyright (c) 2007, XenSource Inc.
  7. * Copyright (c) 2010, Citrix
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. * Place - Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. */
  23. #include <linux/interrupt.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <xen/platform_pci.h>
  28. #include <xen/grant_table.h>
  29. #include <xen/xenbus.h>
  30. #include <xen/events.h>
  31. #include <xen/hvm.h>
  32. #include <xen/xen-ops.h>
  33. #define DRV_NAME "xen-platform-pci"
  34. MODULE_AUTHOR("ssmith@xensource.com and stefano.stabellini@eu.citrix.com");
  35. MODULE_DESCRIPTION("Xen platform PCI device");
  36. MODULE_LICENSE("GPL");
  37. static unsigned long platform_mmio;
  38. static unsigned long platform_mmio_alloc;
  39. static unsigned long platform_mmiolen;
  40. static uint64_t callback_via;
  41. static unsigned long alloc_xen_mmio(unsigned long len)
  42. {
  43. unsigned long addr;
  44. addr = platform_mmio + platform_mmio_alloc;
  45. platform_mmio_alloc += len;
  46. BUG_ON(platform_mmio_alloc > platform_mmiolen);
  47. return addr;
  48. }
  49. static uint64_t get_callback_via(struct pci_dev *pdev)
  50. {
  51. u8 pin;
  52. int irq;
  53. irq = pdev->irq;
  54. if (irq < 16)
  55. return irq; /* ISA IRQ */
  56. pin = pdev->pin;
  57. /* We don't know the GSI. Specify the PCI INTx line instead. */
  58. return ((uint64_t)0x01 << 56) | /* PCI INTx identifier */
  59. ((uint64_t)pci_domain_nr(pdev->bus) << 32) |
  60. ((uint64_t)pdev->bus->number << 16) |
  61. ((uint64_t)(pdev->devfn & 0xff) << 8) |
  62. ((uint64_t)(pin - 1) & 3);
  63. }
  64. static irqreturn_t do_hvm_evtchn_intr(int irq, void *dev_id)
  65. {
  66. xen_hvm_evtchn_do_upcall();
  67. return IRQ_HANDLED;
  68. }
  69. static int xen_allocate_irq(struct pci_dev *pdev)
  70. {
  71. return request_irq(pdev->irq, do_hvm_evtchn_intr,
  72. IRQF_NOBALANCING | IRQF_TRIGGER_RISING,
  73. "xen-platform-pci", pdev);
  74. }
  75. static int platform_pci_resume(struct pci_dev *pdev)
  76. {
  77. int err;
  78. if (xen_have_vector_callback)
  79. return 0;
  80. err = xen_set_callback_via(callback_via);
  81. if (err) {
  82. dev_err(&pdev->dev, "platform_pci_resume failure!\n");
  83. return err;
  84. }
  85. return 0;
  86. }
  87. static int platform_pci_init(struct pci_dev *pdev,
  88. const struct pci_device_id *ent)
  89. {
  90. int i, ret;
  91. long ioaddr;
  92. long mmio_addr, mmio_len;
  93. unsigned int max_nr_gframes;
  94. unsigned long grant_frames;
  95. if (!xen_domain())
  96. return -ENODEV;
  97. i = pci_enable_device(pdev);
  98. if (i)
  99. return i;
  100. ioaddr = pci_resource_start(pdev, 0);
  101. mmio_addr = pci_resource_start(pdev, 1);
  102. mmio_len = pci_resource_len(pdev, 1);
  103. if (mmio_addr == 0 || ioaddr == 0) {
  104. dev_err(&pdev->dev, "no resources found\n");
  105. ret = -ENOENT;
  106. goto pci_out;
  107. }
  108. ret = pci_request_region(pdev, 1, DRV_NAME);
  109. if (ret < 0)
  110. goto pci_out;
  111. ret = pci_request_region(pdev, 0, DRV_NAME);
  112. if (ret < 0)
  113. goto mem_out;
  114. platform_mmio = mmio_addr;
  115. platform_mmiolen = mmio_len;
  116. if (!xen_have_vector_callback) {
  117. ret = xen_allocate_irq(pdev);
  118. if (ret) {
  119. dev_warn(&pdev->dev, "request_irq failed err=%d\n", ret);
  120. goto out;
  121. }
  122. callback_via = get_callback_via(pdev);
  123. ret = xen_set_callback_via(callback_via);
  124. if (ret) {
  125. dev_warn(&pdev->dev, "Unable to set the evtchn callback "
  126. "err=%d\n", ret);
  127. goto out;
  128. }
  129. }
  130. max_nr_gframes = gnttab_max_grant_frames();
  131. grant_frames = alloc_xen_mmio(PAGE_SIZE * max_nr_gframes);
  132. ret = gnttab_setup_auto_xlat_frames(grant_frames);
  133. if (ret)
  134. goto out;
  135. ret = gnttab_init();
  136. if (ret)
  137. goto grant_out;
  138. xenbus_probe(NULL);
  139. return 0;
  140. grant_out:
  141. gnttab_free_auto_xlat_frames();
  142. out:
  143. pci_release_region(pdev, 0);
  144. mem_out:
  145. pci_release_region(pdev, 1);
  146. pci_out:
  147. pci_disable_device(pdev);
  148. return ret;
  149. }
  150. static struct pci_device_id platform_pci_tbl[] = {
  151. {PCI_VENDOR_ID_XEN, PCI_DEVICE_ID_XEN_PLATFORM,
  152. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  153. {0,}
  154. };
  155. MODULE_DEVICE_TABLE(pci, platform_pci_tbl);
  156. static struct pci_driver platform_driver = {
  157. .name = DRV_NAME,
  158. .probe = platform_pci_init,
  159. .id_table = platform_pci_tbl,
  160. #ifdef CONFIG_PM
  161. .resume_early = platform_pci_resume,
  162. #endif
  163. };
  164. static int __init platform_pci_module_init(void)
  165. {
  166. return pci_register_driver(&platform_driver);
  167. }
  168. module_init(platform_pci_module_init);