vop_bus.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Intel Virtio Over PCIe (VOP) Bus driver.
  19. */
  20. #include <linux/slab.h>
  21. #include <linux/module.h>
  22. #include <linux/idr.h>
  23. #include <linux/dma-mapping.h>
  24. #include "vop_bus.h"
  25. static ssize_t device_show(struct device *d,
  26. struct device_attribute *attr, char *buf)
  27. {
  28. struct vop_device *dev = dev_to_vop(d);
  29. return sprintf(buf, "0x%04x\n", dev->id.device);
  30. }
  31. static DEVICE_ATTR_RO(device);
  32. static ssize_t vendor_show(struct device *d,
  33. struct device_attribute *attr, char *buf)
  34. {
  35. struct vop_device *dev = dev_to_vop(d);
  36. return sprintf(buf, "0x%04x\n", dev->id.vendor);
  37. }
  38. static DEVICE_ATTR_RO(vendor);
  39. static ssize_t modalias_show(struct device *d,
  40. struct device_attribute *attr, char *buf)
  41. {
  42. struct vop_device *dev = dev_to_vop(d);
  43. return sprintf(buf, "vop:d%08Xv%08X\n",
  44. dev->id.device, dev->id.vendor);
  45. }
  46. static DEVICE_ATTR_RO(modalias);
  47. static struct attribute *vop_dev_attrs[] = {
  48. &dev_attr_device.attr,
  49. &dev_attr_vendor.attr,
  50. &dev_attr_modalias.attr,
  51. NULL,
  52. };
  53. ATTRIBUTE_GROUPS(vop_dev);
  54. static inline int vop_id_match(const struct vop_device *dev,
  55. const struct vop_device_id *id)
  56. {
  57. if (id->device != dev->id.device && id->device != VOP_DEV_ANY_ID)
  58. return 0;
  59. return id->vendor == VOP_DEV_ANY_ID || id->vendor == dev->id.vendor;
  60. }
  61. /*
  62. * This looks through all the IDs a driver claims to support. If any of them
  63. * match, we return 1 and the kernel will call vop_dev_probe().
  64. */
  65. static int vop_dev_match(struct device *dv, struct device_driver *dr)
  66. {
  67. unsigned int i;
  68. struct vop_device *dev = dev_to_vop(dv);
  69. const struct vop_device_id *ids;
  70. ids = drv_to_vop(dr)->id_table;
  71. for (i = 0; ids[i].device; i++)
  72. if (vop_id_match(dev, &ids[i]))
  73. return 1;
  74. return 0;
  75. }
  76. static int vop_uevent(struct device *dv, struct kobj_uevent_env *env)
  77. {
  78. struct vop_device *dev = dev_to_vop(dv);
  79. return add_uevent_var(env, "MODALIAS=vop:d%08Xv%08X",
  80. dev->id.device, dev->id.vendor);
  81. }
  82. static int vop_dev_probe(struct device *d)
  83. {
  84. struct vop_device *dev = dev_to_vop(d);
  85. struct vop_driver *drv = drv_to_vop(dev->dev.driver);
  86. return drv->probe(dev);
  87. }
  88. static int vop_dev_remove(struct device *d)
  89. {
  90. struct vop_device *dev = dev_to_vop(d);
  91. struct vop_driver *drv = drv_to_vop(dev->dev.driver);
  92. drv->remove(dev);
  93. return 0;
  94. }
  95. static struct bus_type vop_bus = {
  96. .name = "vop_bus",
  97. .match = vop_dev_match,
  98. .dev_groups = vop_dev_groups,
  99. .uevent = vop_uevent,
  100. .probe = vop_dev_probe,
  101. .remove = vop_dev_remove,
  102. };
  103. int vop_register_driver(struct vop_driver *driver)
  104. {
  105. driver->driver.bus = &vop_bus;
  106. return driver_register(&driver->driver);
  107. }
  108. EXPORT_SYMBOL_GPL(vop_register_driver);
  109. void vop_unregister_driver(struct vop_driver *driver)
  110. {
  111. driver_unregister(&driver->driver);
  112. }
  113. EXPORT_SYMBOL_GPL(vop_unregister_driver);
  114. static void vop_release_dev(struct device *d)
  115. {
  116. put_device(d);
  117. }
  118. struct vop_device *
  119. vop_register_device(struct device *pdev, int id,
  120. const struct dma_map_ops *dma_ops,
  121. struct vop_hw_ops *hw_ops, u8 dnode, struct mic_mw *aper,
  122. struct dma_chan *chan)
  123. {
  124. int ret;
  125. struct vop_device *vdev;
  126. vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
  127. if (!vdev)
  128. return ERR_PTR(-ENOMEM);
  129. vdev->dev.parent = pdev;
  130. vdev->id.device = id;
  131. vdev->id.vendor = VOP_DEV_ANY_ID;
  132. vdev->dev.archdata.dma_ops = (struct dma_map_ops *)dma_ops;
  133. vdev->dev.dma_mask = &vdev->dev.coherent_dma_mask;
  134. dma_set_mask(&vdev->dev, DMA_BIT_MASK(64));
  135. vdev->dev.release = vop_release_dev;
  136. vdev->hw_ops = hw_ops;
  137. vdev->dev.bus = &vop_bus;
  138. vdev->dnode = dnode;
  139. vdev->aper = aper;
  140. vdev->dma_ch = chan;
  141. vdev->index = dnode - 1;
  142. dev_set_name(&vdev->dev, "vop-dev%u", vdev->index);
  143. /*
  144. * device_register() causes the bus infrastructure to look for a
  145. * matching driver.
  146. */
  147. ret = device_register(&vdev->dev);
  148. if (ret)
  149. goto free_vdev;
  150. return vdev;
  151. free_vdev:
  152. kfree(vdev);
  153. return ERR_PTR(ret);
  154. }
  155. EXPORT_SYMBOL_GPL(vop_register_device);
  156. void vop_unregister_device(struct vop_device *dev)
  157. {
  158. device_unregister(&dev->dev);
  159. }
  160. EXPORT_SYMBOL_GPL(vop_unregister_device);
  161. static int __init vop_init(void)
  162. {
  163. return bus_register(&vop_bus);
  164. }
  165. static void __exit vop_exit(void)
  166. {
  167. bus_unregister(&vop_bus);
  168. }
  169. core_initcall(vop_init);
  170. module_exit(vop_exit);
  171. MODULE_AUTHOR("Intel Corporation");
  172. MODULE_DESCRIPTION("Intel(R) VOP Bus driver");
  173. MODULE_LICENSE("GPL v2");