platform-pci.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. 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_DISABLED | 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 __devinit platform_pci_init(struct pci_dev *pdev,
  88. const struct pci_device_id *ent)
  89. {
  90. int i, ret;
  91. long ioaddr, iolen;
  92. long mmio_addr, mmio_len;
  93. unsigned int max_nr_gframes;
  94. i = pci_enable_device(pdev);
  95. if (i)
  96. return i;
  97. ioaddr = pci_resource_start(pdev, 0);
  98. iolen = pci_resource_len(pdev, 0);
  99. mmio_addr = pci_resource_start(pdev, 1);
  100. mmio_len = pci_resource_len(pdev, 1);
  101. if (mmio_addr == 0 || ioaddr == 0) {
  102. dev_err(&pdev->dev, "no resources found\n");
  103. ret = -ENOENT;
  104. goto pci_out;
  105. }
  106. if (request_mem_region(mmio_addr, mmio_len, DRV_NAME) == NULL) {
  107. dev_err(&pdev->dev, "MEM I/O resource 0x%lx @ 0x%lx busy\n",
  108. mmio_addr, mmio_len);
  109. ret = -EBUSY;
  110. goto pci_out;
  111. }
  112. if (request_region(ioaddr, iolen, DRV_NAME) == NULL) {
  113. dev_err(&pdev->dev, "I/O resource 0x%lx @ 0x%lx busy\n",
  114. iolen, ioaddr);
  115. ret = -EBUSY;
  116. goto mem_out;
  117. }
  118. platform_mmio = mmio_addr;
  119. platform_mmiolen = mmio_len;
  120. if (!xen_have_vector_callback) {
  121. ret = xen_allocate_irq(pdev);
  122. if (ret) {
  123. dev_warn(&pdev->dev, "request_irq failed err=%d\n", ret);
  124. goto out;
  125. }
  126. callback_via = get_callback_via(pdev);
  127. ret = xen_set_callback_via(callback_via);
  128. if (ret) {
  129. dev_warn(&pdev->dev, "Unable to set the evtchn callback "
  130. "err=%d\n", ret);
  131. goto out;
  132. }
  133. }
  134. max_nr_gframes = gnttab_max_grant_frames();
  135. xen_hvm_resume_frames = alloc_xen_mmio(PAGE_SIZE * max_nr_gframes);
  136. ret = gnttab_init();
  137. if (ret)
  138. goto out;
  139. xenbus_probe(NULL);
  140. ret = xen_setup_shutdown_event();
  141. if (ret)
  142. goto out;
  143. return 0;
  144. out:
  145. release_region(ioaddr, iolen);
  146. mem_out:
  147. release_mem_region(mmio_addr, mmio_len);
  148. pci_out:
  149. pci_disable_device(pdev);
  150. return ret;
  151. }
  152. static struct pci_device_id platform_pci_tbl[] __devinitdata = {
  153. {PCI_VENDOR_ID_XEN, PCI_DEVICE_ID_XEN_PLATFORM,
  154. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  155. {0,}
  156. };
  157. MODULE_DEVICE_TABLE(pci, platform_pci_tbl);
  158. static struct pci_driver platform_driver = {
  159. .name = DRV_NAME,
  160. .probe = platform_pci_init,
  161. .id_table = platform_pci_tbl,
  162. #ifdef CONFIG_PM
  163. .resume_early = platform_pci_resume,
  164. #endif
  165. };
  166. static int __init platform_pci_module_init(void)
  167. {
  168. /* no unplug has been done, IGNORE hasn't been specified: just
  169. * return now */
  170. if (!xen_platform_pci_unplug)
  171. return -ENODEV;
  172. return pci_register_driver(&platform_driver);
  173. }
  174. module_init(platform_pci_module_init);