platform-pci.c 4.5 KB

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