mei_gsc_proxy.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2022-2023 Intel Corporation
  4. */
  5. /**
  6. * DOC: MEI_GSC_PROXY Client Driver
  7. *
  8. * The mei_gsc_proxy driver acts as a translation layer between
  9. * proxy user (I915) and ME FW by proxying messages to ME FW
  10. */
  11. #include <linux/component.h>
  12. #include <linux/mei_cl_bus.h>
  13. #include <linux/module.h>
  14. #include <linux/pci.h>
  15. #include <linux/slab.h>
  16. #include <linux/uuid.h>
  17. #include <drm/drm_connector.h>
  18. #include <drm/intel/i915_component.h>
  19. #include <drm/intel/i915_gsc_proxy_mei_interface.h>
  20. /**
  21. * mei_gsc_proxy_send - Sends a proxy message to ME FW.
  22. * @dev: device corresponding to the mei_cl_device
  23. * @buf: a message buffer to send
  24. * @size: size of the message
  25. * Return: bytes sent on Success, <0 on Failure
  26. */
  27. static int mei_gsc_proxy_send(struct device *dev, const void *buf, size_t size)
  28. {
  29. ssize_t ret;
  30. if (!dev || !buf)
  31. return -EINVAL;
  32. ret = mei_cldev_send(to_mei_cl_device(dev), buf, size);
  33. if (ret < 0)
  34. dev_dbg(dev, "mei_cldev_send failed. %zd\n", ret);
  35. return ret;
  36. }
  37. /**
  38. * mei_gsc_proxy_recv - Receives a proxy message from ME FW.
  39. * @dev: device corresponding to the mei_cl_device
  40. * @buf: a message buffer to contain the received message
  41. * @size: size of the buffer
  42. * Return: bytes received on Success, <0 on Failure
  43. */
  44. static int mei_gsc_proxy_recv(struct device *dev, void *buf, size_t size)
  45. {
  46. ssize_t ret;
  47. if (!dev || !buf)
  48. return -EINVAL;
  49. ret = mei_cldev_recv(to_mei_cl_device(dev), buf, size);
  50. if (ret < 0)
  51. dev_dbg(dev, "mei_cldev_recv failed. %zd\n", ret);
  52. return ret;
  53. }
  54. static const struct i915_gsc_proxy_component_ops mei_gsc_proxy_ops = {
  55. .owner = THIS_MODULE,
  56. .send = mei_gsc_proxy_send,
  57. .recv = mei_gsc_proxy_recv,
  58. };
  59. static int mei_component_master_bind(struct device *dev)
  60. {
  61. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  62. struct i915_gsc_proxy_component *comp_master = mei_cldev_get_drvdata(cldev);
  63. comp_master->ops = &mei_gsc_proxy_ops;
  64. comp_master->mei_dev = dev;
  65. return component_bind_all(dev, comp_master);
  66. }
  67. static void mei_component_master_unbind(struct device *dev)
  68. {
  69. struct mei_cl_device *cldev = to_mei_cl_device(dev);
  70. struct i915_gsc_proxy_component *comp_master = mei_cldev_get_drvdata(cldev);
  71. component_unbind_all(dev, comp_master);
  72. }
  73. static const struct component_master_ops mei_component_master_ops = {
  74. .bind = mei_component_master_bind,
  75. .unbind = mei_component_master_unbind,
  76. };
  77. /**
  78. * mei_gsc_proxy_component_match - compare function for matching mei.
  79. *
  80. * The function checks if the device is pci device and
  81. * Intel VGA adapter, the subcomponent is SW Proxy
  82. * and the VGA is on the bus 0 reserved for built-in devices
  83. * to reject discrete GFX.
  84. *
  85. * @dev: master device
  86. * @subcomponent: subcomponent to match (I915_COMPONENT_SWPROXY)
  87. * @data: compare data (mei pci parent)
  88. *
  89. * Return:
  90. * * 1 - if components match
  91. * * 0 - otherwise
  92. */
  93. static int mei_gsc_proxy_component_match(struct device *dev, int subcomponent,
  94. void *data)
  95. {
  96. struct pci_dev *pdev;
  97. if (!dev_is_pci(dev))
  98. return 0;
  99. pdev = to_pci_dev(dev);
  100. if (pdev->class != (PCI_CLASS_DISPLAY_VGA << 8) ||
  101. pdev->vendor != PCI_VENDOR_ID_INTEL)
  102. return 0;
  103. if (subcomponent != I915_COMPONENT_GSC_PROXY)
  104. return 0;
  105. /* Only built-in GFX */
  106. return (pdev->bus->number == 0);
  107. }
  108. static int mei_gsc_proxy_probe(struct mei_cl_device *cldev,
  109. const struct mei_cl_device_id *id)
  110. {
  111. struct i915_gsc_proxy_component *comp_master;
  112. struct component_match *master_match = NULL;
  113. int ret;
  114. ret = mei_cldev_enable(cldev);
  115. if (ret < 0) {
  116. dev_err(&cldev->dev, "mei_cldev_enable Failed. %d\n", ret);
  117. goto enable_err_exit;
  118. }
  119. comp_master = kzalloc_obj(*comp_master);
  120. if (!comp_master) {
  121. ret = -ENOMEM;
  122. goto err_exit;
  123. }
  124. component_match_add_typed(&cldev->dev, &master_match,
  125. mei_gsc_proxy_component_match, NULL);
  126. if (IS_ERR_OR_NULL(master_match)) {
  127. ret = -ENOMEM;
  128. goto err_exit;
  129. }
  130. mei_cldev_set_drvdata(cldev, comp_master);
  131. ret = component_master_add_with_match(&cldev->dev,
  132. &mei_component_master_ops,
  133. master_match);
  134. if (ret < 0) {
  135. dev_err(&cldev->dev, "Master comp add failed %d\n", ret);
  136. goto err_exit;
  137. }
  138. return 0;
  139. err_exit:
  140. mei_cldev_set_drvdata(cldev, NULL);
  141. kfree(comp_master);
  142. mei_cldev_disable(cldev);
  143. enable_err_exit:
  144. return ret;
  145. }
  146. static void mei_gsc_proxy_remove(struct mei_cl_device *cldev)
  147. {
  148. struct i915_gsc_proxy_component *comp_master = mei_cldev_get_drvdata(cldev);
  149. int ret;
  150. component_master_del(&cldev->dev, &mei_component_master_ops);
  151. kfree(comp_master);
  152. mei_cldev_set_drvdata(cldev, NULL);
  153. ret = mei_cldev_disable(cldev);
  154. if (ret)
  155. dev_warn(&cldev->dev, "mei_cldev_disable() failed %d\n", ret);
  156. }
  157. #define MEI_UUID_GSC_PROXY UUID_LE(0xf73db04, 0x97ab, 0x4125, \
  158. 0xb8, 0x93, 0xe9, 0x4, 0xad, 0xd, 0x54, 0x64)
  159. static struct mei_cl_device_id mei_gsc_proxy_tbl[] = {
  160. { .uuid = MEI_UUID_GSC_PROXY, .version = MEI_CL_VERSION_ANY },
  161. { }
  162. };
  163. MODULE_DEVICE_TABLE(mei, mei_gsc_proxy_tbl);
  164. static struct mei_cl_driver mei_gsc_proxy_driver = {
  165. .id_table = mei_gsc_proxy_tbl,
  166. .name = KBUILD_MODNAME,
  167. .probe = mei_gsc_proxy_probe,
  168. .remove = mei_gsc_proxy_remove,
  169. };
  170. module_mei_cl_driver(mei_gsc_proxy_driver);
  171. MODULE_AUTHOR("Intel Corporation");
  172. MODULE_LICENSE("GPL");
  173. MODULE_DESCRIPTION("MEI GSC PROXY");