platform-pci.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/grant_table.h>
  28. #include <xen/xenbus.h>
  29. #include <xen/events.h>
  30. #include <xen/hvm.h>
  31. #include <xen/xen-ops.h>
  32. #define DRV_NAME "xen-platform-pci"
  33. MODULE_AUTHOR("ssmith@xensource.com and stefano.stabellini@eu.citrix.com");
  34. MODULE_DESCRIPTION("Xen platform PCI device");
  35. MODULE_LICENSE("GPL");
  36. static unsigned long platform_mmio;
  37. static unsigned long platform_mmio_alloc;
  38. static unsigned long platform_mmiolen;
  39. static uint64_t callback_via;
  40. unsigned long alloc_xen_mmio(unsigned long len)
  41. {
  42. unsigned long addr;
  43. addr = platform_mmio + platform_mmio_alloc;
  44. platform_mmio_alloc += len;
  45. BUG_ON(platform_mmio_alloc > platform_mmiolen);
  46. return addr;
  47. }
  48. static uint64_t get_callback_via(struct pci_dev *pdev)
  49. {
  50. u8 pin;
  51. int irq;
  52. irq = pdev->irq;
  53. if (irq < 16)
  54. return irq; /* ISA IRQ */
  55. pin = pdev->pin;
  56. /* We don't know the GSI. Specify the PCI INTx line instead. */
  57. return ((uint64_t)0x01 << 56) | /* PCI INTx identifier */
  58. ((uint64_t)pci_domain_nr(pdev->bus) << 32) |
  59. ((uint64_t)pdev->bus->number << 16) |
  60. ((uint64_t)(pdev->devfn & 0xff) << 8) |
  61. ((uint64_t)(pin - 1) & 3);
  62. }
  63. static irqreturn_t do_hvm_evtchn_intr(int irq, void *dev_id)
  64. {
  65. xen_hvm_evtchn_do_upcall();
  66. return IRQ_HANDLED;
  67. }
  68. static int xen_allocate_irq(struct pci_dev *pdev)
  69. {
  70. return request_irq(pdev->irq, do_hvm_evtchn_intr,
  71. IRQF_DISABLED | IRQF_NOBALANCING | IRQF_TRIGGER_RISING,
  72. "xen-platform-pci", pdev);
  73. }
  74. static int platform_pci_resume(struct pci_dev *pdev)
  75. {
  76. int err;
  77. if (xen_have_vector_callback)
  78. return 0;
  79. err = xen_set_callback_via(callback_via);
  80. if (err) {
  81. dev_err(&pdev->dev, "platform_pci_resume failure!\n");
  82. return err;
  83. }
  84. return 0;
  85. }
  86. static int __devinit platform_pci_init(struct pci_dev *pdev,
  87. const struct pci_device_id *ent)
  88. {
  89. int i, ret;
  90. long ioaddr, iolen;
  91. long mmio_addr, mmio_len;
  92. unsigned int max_nr_gframes;
  93. i = pci_enable_device(pdev);
  94. if (i)
  95. return i;
  96. ioaddr = pci_resource_start(pdev, 0);
  97. iolen = pci_resource_len(pdev, 0);
  98. mmio_addr = pci_resource_start(pdev, 1);
  99. mmio_len = pci_resource_len(pdev, 1);
  100. if (mmio_addr == 0 || ioaddr == 0) {
  101. dev_err(&pdev->dev, "no resources found\n");
  102. ret = -ENOENT;
  103. goto pci_out;
  104. }
  105. if (request_mem_region(mmio_addr, mmio_len, DRV_NAME) == NULL) {
  106. dev_err(&pdev->dev, "MEM I/O resource 0x%lx @ 0x%lx busy\n",
  107. mmio_addr, mmio_len);
  108. ret = -EBUSY;
  109. goto pci_out;
  110. }
  111. if (request_region(ioaddr, iolen, DRV_NAME) == NULL) {
  112. dev_err(&pdev->dev, "I/O resource 0x%lx @ 0x%lx busy\n",
  113. iolen, ioaddr);
  114. ret = -EBUSY;
  115. goto mem_out;
  116. }
  117. platform_mmio = mmio_addr;
  118. platform_mmiolen = mmio_len;
  119. if (!xen_have_vector_callback) {
  120. ret = xen_allocate_irq(pdev);
  121. if (ret) {
  122. dev_warn(&pdev->dev, "request_irq failed err=%d\n", ret);
  123. goto out;
  124. }
  125. callback_via = get_callback_via(pdev);
  126. ret = xen_set_callback_via(callback_via);
  127. if (ret) {
  128. dev_warn(&pdev->dev, "Unable to set the evtchn callback "
  129. "err=%d\n", ret);
  130. goto out;
  131. }
  132. }
  133. max_nr_gframes = gnttab_max_grant_frames();
  134. xen_hvm_resume_frames = alloc_xen_mmio(PAGE_SIZE * max_nr_gframes);
  135. ret = gnttab_init();
  136. if (ret)
  137. goto out;
  138. xenbus_probe(NULL);
  139. ret = xen_setup_shutdown_event();
  140. if (ret)
  141. goto out;
  142. return 0;
  143. out:
  144. release_region(ioaddr, iolen);
  145. mem_out:
  146. release_mem_region(mmio_addr, mmio_len);
  147. pci_out:
  148. pci_disable_device(pdev);
  149. return ret;
  150. }
  151. static struct pci_device_id platform_pci_tbl[] __devinitdata = {
  152. {PCI_VENDOR_ID_XEN, PCI_DEVICE_ID_XEN_PLATFORM,
  153. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  154. {0,}
  155. };
  156. MODULE_DEVICE_TABLE(pci, platform_pci_tbl);
  157. static struct pci_driver platform_driver = {
  158. .name = DRV_NAME,
  159. .probe = platform_pci_init,
  160. .id_table = platform_pci_tbl,
  161. #ifdef CONFIG_PM
  162. .resume_early = platform_pci_resume,
  163. #endif
  164. };
  165. static int __init platform_pci_module_init(void)
  166. {
  167. return pci_register_driver(&platform_driver);
  168. }
  169. module_init(platform_pci_module_init);