mic_bus.c 5.4 KB

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