mei_lb.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2025 Intel Corporation
  4. */
  5. #include <linux/component.h>
  6. #include <linux/mei_cl_bus.h>
  7. #include <linux/module.h>
  8. #include <linux/overflow.h>
  9. #include <linux/pci.h>
  10. #include <linux/slab.h>
  11. #include <linux/uuid.h>
  12. #include <drm/intel/i915_component.h>
  13. #include <drm/intel/intel_lb_mei_interface.h>
  14. #include "mkhi.h"
  15. /**
  16. * DOC: Late Binding Firmware Update/Upload
  17. *
  18. * Late Binding is a firmware update/upload mechanism that allows configuration
  19. * payloads to be securely delivered and applied at runtime, rather than
  20. * being embedded in the system firmware image (e.g., IFWI or SPI flash).
  21. *
  22. * This mechanism is used to update device-level configuration such as:
  23. * - Fan controller
  24. * - Voltage regulator (VR)
  25. *
  26. * Key Characteristics:
  27. * ---------------------
  28. * - Runtime Delivery:
  29. * Firmware blobs are loaded by the host driver (e.g., Xe KMD)
  30. * after the GPU or SoC has booted.
  31. *
  32. * - Secure and Authenticated:
  33. * All payloads are signed and verified by the authentication firmware.
  34. *
  35. * - No Firmware Flashing Required:
  36. * Updates are applied in volatile memory and do not require SPI flash
  37. * modification or system reboot.
  38. *
  39. * - Re-entrant:
  40. * Multiple updates of the same or different types can be applied
  41. * sequentially within a single boot session.
  42. *
  43. * - Version Controlled:
  44. * Each payload includes version and security version number (SVN)
  45. * metadata to support anti-rollback enforcement.
  46. *
  47. * Upload Flow:
  48. * ------------
  49. * 1. Host driver (KMD or user-space tool) loads the late binding firmware.
  50. * 2. Firmware is passed to the MEI interface and forwarded to
  51. * authentication firmware.
  52. * 3. Authentication firmware authenticates the payload and extracts
  53. * command and data arrays.
  54. * 4. Authentication firmware delivers the configuration to PUnit/PCODE.
  55. * 5. Status is returned back to the host via MEI.
  56. */
  57. #define INTEL_LB_CMD 0x12
  58. #define INTEL_LB_RSP (INTEL_LB_CMD | 0x80)
  59. #define INTEL_LB_SEND_TIMEOUT_MSEC 3000
  60. #define INTEL_LB_RECV_TIMEOUT_MSEC 3000
  61. /**
  62. * struct mei_lb_req - Late Binding request structure
  63. * @header: MKHI message header (see struct mkhi_msg_hdr)
  64. * @type: Type of the Late Binding payload
  65. * @flags: Flags to be passed to the authentication firmware (e.g. %INTEL_LB_FLAGS_IS_PERSISTENT)
  66. * @reserved: Reserved for future use by authentication firmware, must be set to 0
  67. * @payload_size: Size of the payload data in bytes
  68. * @payload: Payload data to be sent to the authentication firmware
  69. */
  70. struct mei_lb_req {
  71. struct mkhi_msg_hdr header;
  72. __le32 type;
  73. __le32 flags;
  74. __le32 reserved[2];
  75. __le32 payload_size;
  76. u8 payload[] __counted_by(payload_size);
  77. } __packed;
  78. /**
  79. * struct mei_lb_rsp - Late Binding response structure
  80. * @header: MKHI message header (see struct mkhi_msg_hdr)
  81. * @type: Type of the Late Binding payload
  82. * @reserved: Reserved for future use by authentication firmware, must be set to 0
  83. * @status: Status returned by authentication firmware (see &enum intel_lb_status)
  84. */
  85. struct mei_lb_rsp {
  86. struct mkhi_msg_hdr header;
  87. __le32 type;
  88. __le32 reserved[2];
  89. __le32 status;
  90. } __packed;
  91. static bool mei_lb_check_response(const struct device *dev, ssize_t bytes,
  92. struct mei_lb_rsp *rsp)
  93. {
  94. /*
  95. * Received message size may be smaller than the full message size when
  96. * reply contains only MKHI header with result field set to the error code.
  97. * Check the header size and content first to output exact error, if needed,
  98. * and then process to the whole message.
  99. */
  100. if (bytes < sizeof(rsp->header)) {
  101. dev_err(dev, "Received less than header size from the firmware: %zd < %zu\n",
  102. bytes, sizeof(rsp->header));
  103. return false;
  104. }
  105. if (rsp->header.group_id != MKHI_GROUP_ID_GFX) {
  106. dev_err(dev, "Mismatch group id: 0x%x instead of 0x%x\n",
  107. rsp->header.group_id, MKHI_GROUP_ID_GFX);
  108. return false;
  109. }
  110. if (rsp->header.command != INTEL_LB_RSP) {
  111. dev_err(dev, "Mismatch command: 0x%x instead of 0x%x\n",
  112. rsp->header.command, INTEL_LB_RSP);
  113. return false;
  114. }
  115. if (rsp->header.result) {
  116. dev_err(dev, "Error in result: 0x%x\n", rsp->header.result);
  117. return false;
  118. }
  119. if (bytes < sizeof(*rsp)) {
  120. dev_err(dev, "Received less than message size from the firmware: %zd < %zu\n",
  121. bytes, sizeof(*rsp));
  122. return false;
  123. }
  124. return true;
  125. }
  126. static int mei_lb_push_payload(struct device *dev, u32 type, u32 flags,
  127. const void *payload, size_t payload_size)
  128. {
  129. struct mei_cl_device *cldev;
  130. struct mei_lb_req *req = NULL;
  131. struct mei_lb_rsp rsp;
  132. size_t req_size;
  133. ssize_t bytes;
  134. int ret;
  135. cldev = to_mei_cl_device(dev);
  136. ret = mei_cldev_enable(cldev);
  137. if (ret) {
  138. dev_dbg(dev, "Failed to enable firmware client. %d\n", ret);
  139. return ret;
  140. }
  141. req_size = struct_size(req, payload, payload_size);
  142. if (req_size > mei_cldev_mtu(cldev)) {
  143. dev_err(dev, "Payload is too big: %zu\n", payload_size);
  144. ret = -EMSGSIZE;
  145. goto end;
  146. }
  147. req = kmalloc(req_size, GFP_KERNEL);
  148. if (!req) {
  149. ret = -ENOMEM;
  150. goto end;
  151. }
  152. req->header.group_id = MKHI_GROUP_ID_GFX;
  153. req->header.command = INTEL_LB_CMD;
  154. req->type = cpu_to_le32(type);
  155. req->flags = cpu_to_le32(flags);
  156. req->reserved[0] = 0;
  157. req->reserved[1] = 0;
  158. req->payload_size = cpu_to_le32(payload_size);
  159. memcpy(req->payload, payload, payload_size);
  160. bytes = mei_cldev_send_timeout(cldev, (u8 *)req, req_size,
  161. INTEL_LB_SEND_TIMEOUT_MSEC);
  162. if (bytes < 0) {
  163. dev_err(dev, "Failed to send late binding request to firmware. %zd\n", bytes);
  164. ret = bytes;
  165. goto end;
  166. }
  167. bytes = mei_cldev_recv_timeout(cldev, (u8 *)&rsp, sizeof(rsp),
  168. INTEL_LB_RECV_TIMEOUT_MSEC);
  169. if (bytes < 0) {
  170. dev_err(dev, "Failed to receive late binding reply from MEI firmware. %zd\n",
  171. bytes);
  172. ret = bytes;
  173. goto end;
  174. }
  175. if (!mei_lb_check_response(dev, bytes, &rsp)) {
  176. dev_err(dev, "Bad response from the firmware. header: %02x %02x %02x %02x\n",
  177. rsp.header.group_id, rsp.header.command,
  178. rsp.header.reserved, rsp.header.result);
  179. ret = -EPROTO;
  180. goto end;
  181. }
  182. dev_dbg(dev, "status = %u\n", le32_to_cpu(rsp.status));
  183. ret = (int)le32_to_cpu(rsp.status);
  184. end:
  185. mei_cldev_disable(cldev);
  186. kfree(req);
  187. return ret;
  188. }
  189. static const struct intel_lb_component_ops mei_lb_ops = {
  190. .push_payload = mei_lb_push_payload,
  191. };
  192. static int mei_lb_component_master_bind(struct device *dev)
  193. {
  194. return component_bind_all(dev, (void *)&mei_lb_ops);
  195. }
  196. static void mei_lb_component_master_unbind(struct device *dev)
  197. {
  198. component_unbind_all(dev, (void *)&mei_lb_ops);
  199. }
  200. static const struct component_master_ops mei_lb_component_master_ops = {
  201. .bind = mei_lb_component_master_bind,
  202. .unbind = mei_lb_component_master_unbind,
  203. };
  204. static int mei_lb_component_match(struct device *dev, int subcomponent,
  205. void *data)
  206. {
  207. /*
  208. * This function checks if requester is Intel %PCI_CLASS_DISPLAY_VGA or
  209. * %PCI_CLASS_DISPLAY_OTHER device, and checks if the requester is the
  210. * grand parent of mei_if i.e. late bind MEI device
  211. */
  212. struct device *base = data;
  213. struct pci_dev *pdev;
  214. if (!dev)
  215. return 0;
  216. if (!dev_is_pci(dev))
  217. return 0;
  218. pdev = to_pci_dev(dev);
  219. if (pdev->vendor != PCI_VENDOR_ID_INTEL)
  220. return 0;
  221. if (pdev->class != (PCI_CLASS_DISPLAY_VGA << 8) &&
  222. pdev->class != (PCI_CLASS_DISPLAY_OTHER << 8))
  223. return 0;
  224. if (subcomponent != INTEL_COMPONENT_LB)
  225. return 0;
  226. base = base->parent;
  227. if (!base) /* mei device */
  228. return 0;
  229. base = base->parent; /* pci device */
  230. return !!base && dev == base;
  231. }
  232. static int mei_lb_probe(struct mei_cl_device *cldev,
  233. const struct mei_cl_device_id *id)
  234. {
  235. struct component_match *master_match = NULL;
  236. int ret;
  237. component_match_add_typed(&cldev->dev, &master_match,
  238. mei_lb_component_match, &cldev->dev);
  239. if (IS_ERR_OR_NULL(master_match))
  240. return -ENOMEM;
  241. ret = component_master_add_with_match(&cldev->dev,
  242. &mei_lb_component_master_ops,
  243. master_match);
  244. if (ret < 0)
  245. dev_err(&cldev->dev, "Failed to add late binding master component. %d\n", ret);
  246. return ret;
  247. }
  248. static void mei_lb_remove(struct mei_cl_device *cldev)
  249. {
  250. component_master_del(&cldev->dev, &mei_lb_component_master_ops);
  251. }
  252. #define MEI_GUID_MKHI UUID_LE(0xe2c2afa2, 0x3817, 0x4d19, \
  253. 0x9d, 0x95, 0x6, 0xb1, 0x6b, 0x58, 0x8a, 0x5d)
  254. static const struct mei_cl_device_id mei_lb_tbl[] = {
  255. { .uuid = MEI_GUID_MKHI, .version = MEI_CL_VERSION_ANY },
  256. { }
  257. };
  258. MODULE_DEVICE_TABLE(mei, mei_lb_tbl);
  259. static struct mei_cl_driver mei_lb_driver = {
  260. .id_table = mei_lb_tbl,
  261. .name = "mei_lb",
  262. .probe = mei_lb_probe,
  263. .remove = mei_lb_remove,
  264. };
  265. module_mei_cl_driver(mei_lb_driver);
  266. MODULE_AUTHOR("Intel Corporation");
  267. MODULE_LICENSE("GPL");
  268. MODULE_DESCRIPTION("MEI Late Binding Firmware Update/Upload");