platform-pci.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /******************************************************************************
  2. * platform-pci.c
  3. *
  4. * Xen platform PCI device driver
  5. *
  6. * Authors: ssmith@xensource.com and stefano.stabellini@eu.citrix.com
  7. *
  8. * Copyright (c) 2005, Intel Corporation.
  9. * Copyright (c) 2007, XenSource Inc.
  10. * Copyright (c) 2010, Citrix
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms and conditions of the GNU General Public License,
  14. * version 2, as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along with
  22. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  23. * Place - Suite 330, Boston, MA 02111-1307 USA.
  24. *
  25. */
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/init.h>
  29. #include <linux/pci.h>
  30. #include <xen/platform_pci.h>
  31. #include <xen/grant_table.h>
  32. #include <xen/xenbus.h>
  33. #include <xen/events.h>
  34. #include <xen/hvm.h>
  35. #include <xen/xen-ops.h>
  36. #define DRV_NAME "xen-platform-pci"
  37. static unsigned long platform_mmio;
  38. static unsigned long platform_mmio_alloc;
  39. static unsigned long platform_mmiolen;
  40. static 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 int platform_pci_probe(struct pci_dev *pdev,
  49. const struct pci_device_id *ent)
  50. {
  51. int i, ret;
  52. long ioaddr;
  53. long mmio_addr, mmio_len;
  54. unsigned int max_nr_gframes;
  55. unsigned long grant_frames;
  56. if (!xen_domain())
  57. return -ENODEV;
  58. i = pci_enable_device(pdev);
  59. if (i)
  60. return i;
  61. ioaddr = pci_resource_start(pdev, 0);
  62. mmio_addr = pci_resource_start(pdev, 1);
  63. mmio_len = pci_resource_len(pdev, 1);
  64. if (mmio_addr == 0 || ioaddr == 0) {
  65. dev_err(&pdev->dev, "no resources found\n");
  66. ret = -ENOENT;
  67. goto pci_out;
  68. }
  69. ret = pci_request_region(pdev, 1, DRV_NAME);
  70. if (ret < 0)
  71. goto pci_out;
  72. ret = pci_request_region(pdev, 0, DRV_NAME);
  73. if (ret < 0)
  74. goto mem_out;
  75. platform_mmio = mmio_addr;
  76. platform_mmiolen = mmio_len;
  77. max_nr_gframes = gnttab_max_grant_frames();
  78. grant_frames = alloc_xen_mmio(PAGE_SIZE * max_nr_gframes);
  79. ret = gnttab_setup_auto_xlat_frames(grant_frames);
  80. if (ret)
  81. goto out;
  82. ret = gnttab_init();
  83. if (ret)
  84. goto grant_out;
  85. xenbus_probe(NULL);
  86. return 0;
  87. grant_out:
  88. gnttab_free_auto_xlat_frames();
  89. out:
  90. pci_release_region(pdev, 0);
  91. mem_out:
  92. pci_release_region(pdev, 1);
  93. pci_out:
  94. pci_disable_device(pdev);
  95. return ret;
  96. }
  97. static struct pci_device_id platform_pci_tbl[] = {
  98. {PCI_VENDOR_ID_XEN, PCI_DEVICE_ID_XEN_PLATFORM,
  99. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  100. {0,}
  101. };
  102. static struct pci_driver platform_driver = {
  103. .name = DRV_NAME,
  104. .probe = platform_pci_probe,
  105. .id_table = platform_pci_tbl,
  106. };
  107. builtin_pci_driver(platform_driver);