uio_pci_generic.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* uio_pci_generic - generic UIO driver for PCI 2.3 devices
  2. *
  3. * Copyright (C) 2009 Red Hat, Inc.
  4. * Author: Michael S. Tsirkin <mst@redhat.com>
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2.
  7. *
  8. * Since the driver does not declare any device ids, you must allocate
  9. * id and bind the device to the driver yourself. For example:
  10. *
  11. * # echo "8086 10f5" > /sys/bus/pci/drivers/uio_pci_generic/new_id
  12. * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/e1000e/unbind
  13. * # echo -n 0000:00:19.0 > /sys/bus/pci/drivers/uio_pci_generic/bind
  14. * # ls -l /sys/bus/pci/devices/0000:00:19.0/driver
  15. * .../0000:00:19.0/driver -> ../../../bus/pci/drivers/uio_pci_generic
  16. *
  17. * Driver won't bind to devices which do not support the Interrupt Disable Bit
  18. * in the command register. All devices compliant to PCI 2.3 (circa 2002) and
  19. * all compliant PCI Express devices should support this bit.
  20. */
  21. #include <linux/device.h>
  22. #include <linux/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/slab.h>
  25. #include <linux/uio_driver.h>
  26. #include <linux/spinlock.h>
  27. #define DRIVER_VERSION "0.01.0"
  28. #define DRIVER_AUTHOR "Michael S. Tsirkin <mst@redhat.com>"
  29. #define DRIVER_DESC "Generic UIO driver for PCI 2.3 devices"
  30. struct uio_pci_generic_dev {
  31. struct uio_info info;
  32. struct pci_dev *pdev;
  33. spinlock_t lock; /* guards command register accesses */
  34. };
  35. static inline struct uio_pci_generic_dev *
  36. to_uio_pci_generic_dev(struct uio_info *info)
  37. {
  38. return container_of(info, struct uio_pci_generic_dev, info);
  39. }
  40. /* Interrupt handler. Read/modify/write the command register to disable
  41. * the interrupt. */
  42. static irqreturn_t irqhandler(int irq, struct uio_info *info)
  43. {
  44. struct uio_pci_generic_dev *gdev = to_uio_pci_generic_dev(info);
  45. struct pci_dev *pdev = gdev->pdev;
  46. irqreturn_t ret = IRQ_NONE;
  47. u32 cmd_status_dword;
  48. u16 origcmd, newcmd, status;
  49. /* We do a single dword read to retrieve both command and status.
  50. * Document assumptions that make this possible. */
  51. BUILD_BUG_ON(PCI_COMMAND % 4);
  52. BUILD_BUG_ON(PCI_COMMAND + 2 != PCI_STATUS);
  53. spin_lock_irq(&gdev->lock);
  54. pci_block_user_cfg_access(pdev);
  55. /* Read both command and status registers in a single 32-bit operation.
  56. * Note: we could cache the value for command and move the status read
  57. * out of the lock if there was a way to get notified of user changes
  58. * to command register through sysfs. Should be good for shared irqs. */
  59. pci_read_config_dword(pdev, PCI_COMMAND, &cmd_status_dword);
  60. origcmd = cmd_status_dword;
  61. status = cmd_status_dword >> 16;
  62. /* Check interrupt status register to see whether our device
  63. * triggered the interrupt. */
  64. if (!(status & PCI_STATUS_INTERRUPT))
  65. goto done;
  66. /* We triggered the interrupt, disable it. */
  67. newcmd = origcmd | PCI_COMMAND_INTX_DISABLE;
  68. if (newcmd != origcmd)
  69. pci_write_config_word(pdev, PCI_COMMAND, newcmd);
  70. /* UIO core will signal the user process. */
  71. ret = IRQ_HANDLED;
  72. done:
  73. pci_unblock_user_cfg_access(pdev);
  74. spin_unlock_irq(&gdev->lock);
  75. return ret;
  76. }
  77. /* Verify that the device supports Interrupt Disable bit in command register,
  78. * per PCI 2.3, by flipping this bit and reading it back: this bit was readonly
  79. * in PCI 2.2. */
  80. static int __devinit verify_pci_2_3(struct pci_dev *pdev)
  81. {
  82. u16 orig, new;
  83. int err = 0;
  84. pci_block_user_cfg_access(pdev);
  85. pci_read_config_word(pdev, PCI_COMMAND, &orig);
  86. pci_write_config_word(pdev, PCI_COMMAND,
  87. orig ^ PCI_COMMAND_INTX_DISABLE);
  88. pci_read_config_word(pdev, PCI_COMMAND, &new);
  89. /* There's no way to protect against
  90. * hardware bugs or detect them reliably, but as long as we know
  91. * what the value should be, let's go ahead and check it. */
  92. if ((new ^ orig) & ~PCI_COMMAND_INTX_DISABLE) {
  93. err = -EBUSY;
  94. dev_err(&pdev->dev, "Command changed from 0x%x to 0x%x: "
  95. "driver or HW bug?\n", orig, new);
  96. goto err;
  97. }
  98. if (!((new ^ orig) & PCI_COMMAND_INTX_DISABLE)) {
  99. dev_warn(&pdev->dev, "Device does not support "
  100. "disabling interrupts: unable to bind.\n");
  101. err = -ENODEV;
  102. goto err;
  103. }
  104. /* Now restore the original value. */
  105. pci_write_config_word(pdev, PCI_COMMAND, orig);
  106. err:
  107. pci_unblock_user_cfg_access(pdev);
  108. return err;
  109. }
  110. static int __devinit probe(struct pci_dev *pdev,
  111. const struct pci_device_id *id)
  112. {
  113. struct uio_pci_generic_dev *gdev;
  114. int err;
  115. err = pci_enable_device(pdev);
  116. if (err) {
  117. dev_err(&pdev->dev, "%s: pci_enable_device failed: %d\n",
  118. __func__, err);
  119. return err;
  120. }
  121. if (!pdev->irq) {
  122. dev_warn(&pdev->dev, "No IRQ assigned to device: "
  123. "no support for interrupts?\n");
  124. pci_disable_device(pdev);
  125. return -ENODEV;
  126. }
  127. err = verify_pci_2_3(pdev);
  128. if (err)
  129. goto err_verify;
  130. gdev = kzalloc(sizeof(struct uio_pci_generic_dev), GFP_KERNEL);
  131. if (!gdev) {
  132. err = -ENOMEM;
  133. goto err_alloc;
  134. }
  135. gdev->info.name = "uio_pci_generic";
  136. gdev->info.version = DRIVER_VERSION;
  137. gdev->info.irq = pdev->irq;
  138. gdev->info.irq_flags = IRQF_SHARED;
  139. gdev->info.handler = irqhandler;
  140. gdev->pdev = pdev;
  141. spin_lock_init(&gdev->lock);
  142. if (uio_register_device(&pdev->dev, &gdev->info))
  143. goto err_register;
  144. pci_set_drvdata(pdev, gdev);
  145. return 0;
  146. err_register:
  147. kfree(gdev);
  148. err_alloc:
  149. err_verify:
  150. pci_disable_device(pdev);
  151. return err;
  152. }
  153. static void remove(struct pci_dev *pdev)
  154. {
  155. struct uio_pci_generic_dev *gdev = pci_get_drvdata(pdev);
  156. uio_unregister_device(&gdev->info);
  157. pci_disable_device(pdev);
  158. kfree(gdev);
  159. }
  160. static struct pci_driver driver = {
  161. .name = "uio_pci_generic",
  162. .id_table = NULL, /* only dynamic id's */
  163. .probe = probe,
  164. .remove = remove,
  165. };
  166. static int __init init(void)
  167. {
  168. pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  169. return pci_register_driver(&driver);
  170. }
  171. static void __exit cleanup(void)
  172. {
  173. pci_unregister_driver(&driver);
  174. }
  175. module_init(init);
  176. module_exit(cleanup);
  177. MODULE_VERSION(DRIVER_VERSION);
  178. MODULE_LICENSE("GPL v2");
  179. MODULE_AUTHOR(DRIVER_AUTHOR);
  180. MODULE_DESCRIPTION(DRIVER_DESC);