uio_pci_generic.c 5.7 KB

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