mic_bus.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2014 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 MIC Bus driver.
  19. *
  20. * This implementation is very similar to the the virtio bus driver
  21. * implementation @ drivers/virtio/virtio.c
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/idr.h>
  26. #include <linux/mic_bus.h>
  27. static ssize_t device_show(struct device *d,
  28. struct device_attribute *attr, char *buf)
  29. {
  30. struct mbus_device *dev = dev_to_mbus(d);
  31. return sprintf(buf, "0x%04x\n", dev->id.device);
  32. }
  33. static DEVICE_ATTR_RO(device);
  34. static ssize_t vendor_show(struct device *d,
  35. struct device_attribute *attr, char *buf)
  36. {
  37. struct mbus_device *dev = dev_to_mbus(d);
  38. return sprintf(buf, "0x%04x\n", dev->id.vendor);
  39. }
  40. static DEVICE_ATTR_RO(vendor);
  41. static ssize_t modalias_show(struct device *d,
  42. struct device_attribute *attr, char *buf)
  43. {
  44. struct mbus_device *dev = dev_to_mbus(d);
  45. return sprintf(buf, "mbus:d%08Xv%08X\n",
  46. dev->id.device, dev->id.vendor);
  47. }
  48. static DEVICE_ATTR_RO(modalias);
  49. static struct attribute *mbus_dev_attrs[] = {
  50. &dev_attr_device.attr,
  51. &dev_attr_vendor.attr,
  52. &dev_attr_modalias.attr,
  53. NULL,
  54. };
  55. ATTRIBUTE_GROUPS(mbus_dev);
  56. static inline int mbus_id_match(const struct mbus_device *dev,
  57. const struct mbus_device_id *id)
  58. {
  59. if (id->device != dev->id.device && id->device != MBUS_DEV_ANY_ID)
  60. return 0;
  61. return id->vendor == MBUS_DEV_ANY_ID || id->vendor == dev->id.vendor;
  62. }
  63. /*
  64. * This looks through all the IDs a driver claims to support. If any of them
  65. * match, we return 1 and the kernel will call mbus_dev_probe().
  66. */
  67. static int mbus_dev_match(struct device *dv, struct device_driver *dr)
  68. {
  69. unsigned int i;
  70. struct mbus_device *dev = dev_to_mbus(dv);
  71. const struct mbus_device_id *ids;
  72. ids = drv_to_mbus(dr)->id_table;
  73. for (i = 0; ids[i].device; i++)
  74. if (mbus_id_match(dev, &ids[i]))
  75. return 1;
  76. return 0;
  77. }
  78. static int mbus_uevent(struct device *dv, struct kobj_uevent_env *env)
  79. {
  80. struct mbus_device *dev = dev_to_mbus(dv);
  81. return add_uevent_var(env, "MODALIAS=mbus:d%08Xv%08X",
  82. dev->id.device, dev->id.vendor);
  83. }
  84. static int mbus_dev_probe(struct device *d)
  85. {
  86. int err;
  87. struct mbus_device *dev = dev_to_mbus(d);
  88. struct mbus_driver *drv = drv_to_mbus(dev->dev.driver);
  89. err = drv->probe(dev);
  90. if (!err)
  91. if (drv->scan)
  92. drv->scan(dev);
  93. return err;
  94. }
  95. static int mbus_dev_remove(struct device *d)
  96. {
  97. struct mbus_device *dev = dev_to_mbus(d);
  98. struct mbus_driver *drv = drv_to_mbus(dev->dev.driver);
  99. drv->remove(dev);
  100. return 0;
  101. }
  102. static struct bus_type mic_bus = {
  103. .name = "mic_bus",
  104. .match = mbus_dev_match,
  105. .dev_groups = mbus_dev_groups,
  106. .uevent = mbus_uevent,
  107. .probe = mbus_dev_probe,
  108. .remove = mbus_dev_remove,
  109. };
  110. int mbus_register_driver(struct mbus_driver *driver)
  111. {
  112. driver->driver.bus = &mic_bus;
  113. return driver_register(&driver->driver);
  114. }
  115. EXPORT_SYMBOL_GPL(mbus_register_driver);
  116. void mbus_unregister_driver(struct mbus_driver *driver)
  117. {
  118. driver_unregister(&driver->driver);
  119. }
  120. EXPORT_SYMBOL_GPL(mbus_unregister_driver);
  121. static void mbus_release_dev(struct device *d)
  122. {
  123. struct mbus_device *mbdev = dev_to_mbus(d);
  124. kfree(mbdev);
  125. }
  126. struct mbus_device *
  127. mbus_register_device(struct device *pdev, int id, const struct dma_map_ops *dma_ops,
  128. struct mbus_hw_ops *hw_ops, int index,
  129. void __iomem *mmio_va)
  130. {
  131. int ret;
  132. struct mbus_device *mbdev;
  133. mbdev = kzalloc(sizeof(*mbdev), GFP_KERNEL);
  134. if (!mbdev)
  135. return ERR_PTR(-ENOMEM);
  136. mbdev->mmio_va = mmio_va;
  137. mbdev->dev.parent = pdev;
  138. mbdev->id.device = id;
  139. mbdev->id.vendor = MBUS_DEV_ANY_ID;
  140. mbdev->dev.dma_ops = dma_ops;
  141. mbdev->dev.dma_mask = &mbdev->dev.coherent_dma_mask;
  142. dma_set_mask(&mbdev->dev, DMA_BIT_MASK(64));
  143. mbdev->dev.release = mbus_release_dev;
  144. mbdev->hw_ops = hw_ops;
  145. mbdev->dev.bus = &mic_bus;
  146. mbdev->index = index;
  147. dev_set_name(&mbdev->dev, "mbus-dev%u", mbdev->index);
  148. /*
  149. * device_register() causes the bus infrastructure to look for a
  150. * matching driver.
  151. */
  152. ret = device_register(&mbdev->dev);
  153. if (ret)
  154. goto free_mbdev;
  155. return mbdev;
  156. free_mbdev:
  157. put_device(&mbdev->dev);
  158. return ERR_PTR(ret);
  159. }
  160. EXPORT_SYMBOL_GPL(mbus_register_device);
  161. void mbus_unregister_device(struct mbus_device *mbdev)
  162. {
  163. device_unregister(&mbdev->dev);
  164. }
  165. EXPORT_SYMBOL_GPL(mbus_unregister_device);
  166. static int __init mbus_init(void)
  167. {
  168. return bus_register(&mic_bus);
  169. }
  170. static void __exit mbus_exit(void)
  171. {
  172. bus_unregister(&mic_bus);
  173. }
  174. core_initcall(mbus_init);
  175. module_exit(mbus_exit);
  176. MODULE_AUTHOR("Intel Corporation");
  177. MODULE_DESCRIPTION("Intel(R) MIC Bus driver");
  178. MODULE_LICENSE("GPL v2");