loader.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ISHTP firmware loader function
  4. *
  5. * Copyright (c) 2024, Intel Corporation.
  6. *
  7. * This module implements the functionality to load the main ISH firmware from the host, starting
  8. * with the Lunar Lake generation. It leverages a new method that enhances space optimization and
  9. * flexibility by dividing the ISH firmware into a bootloader and main firmware.
  10. *
  11. * Please refer to the [Documentation](Documentation/hid/intel-ish-hid.rst) for the details on
  12. * flows.
  13. *
  14. * Additionally, address potential error scenarios to ensure graceful failure handling.
  15. * - Firmware Image Not Found:
  16. * Occurs when `request_firmware()` cannot locate the firmware image. The ISH firmware will
  17. * remain in a state awaiting firmware loading from the host, with no further action from
  18. * the ISHTP driver.
  19. * Recovery: Re-insmod the ISH drivers allows for a retry of the firmware loading from the host.
  20. *
  21. * - DMA Buffer Allocation Failure:
  22. * This happens if allocating a DMA buffer during `prepare_dma_bufs()` fails. The ISH firmware
  23. * will stay in a waiting state, and the ISHTP driver will release any allocated DMA buffers and
  24. * firmware without further actions.
  25. * Recovery: Re-insmod the ISH drivers allows for a retry of the firmware loading from the host.
  26. *
  27. * - Incorrect Firmware Image:
  28. * Using an incorrect firmware image will initiate the firmware loading process but will
  29. * eventually be refused by the ISH firmware after three unsuccessful attempts, indicated by
  30. * returning an error code. The ISHTP driver will stop attempting after three tries.
  31. * Recovery: A platform reset is required to retry firmware loading from the host.
  32. */
  33. #define dev_fmt(fmt) "ISH loader: " fmt
  34. #include <linux/cacheflush.h>
  35. #include <linux/container_of.h>
  36. #include <linux/crc32.h>
  37. #include <linux/dev_printk.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/dmi.h>
  40. #include <linux/errno.h>
  41. #include <linux/firmware.h>
  42. #include <linux/gfp_types.h>
  43. #include <linux/math.h>
  44. #include <linux/module.h>
  45. #include <linux/pfn.h>
  46. #include <linux/sprintf.h>
  47. #include <linux/string.h>
  48. #include <linux/types.h>
  49. #include <linux/wait.h>
  50. #include "hbm.h"
  51. #include "loader.h"
  52. /**
  53. * loader_write_message() - Write a message to the ISHTP device
  54. * @dev: The ISHTP device
  55. * @buf: The buffer containing the message
  56. * @len: The length of the message
  57. *
  58. * Return: 0 on success, negative error code on failure
  59. */
  60. static int loader_write_message(struct ishtp_device *dev, void *buf, int len)
  61. {
  62. struct ishtp_msg_hdr ishtp_hdr = {
  63. .fw_addr = ISHTP_LOADER_CLIENT_ADDR,
  64. .length = len,
  65. .msg_complete = 1,
  66. };
  67. dev->fw_loader_received = false;
  68. return ishtp_write_message(dev, &ishtp_hdr, buf);
  69. }
  70. /**
  71. * loader_xfer_cmd() - Transfer a command to the ISHTP device
  72. * @dev: The ISHTP device
  73. * @req: The request buffer
  74. * @req_len: The length of the request
  75. * @resp: The response buffer
  76. * @resp_len: The length of the response
  77. *
  78. * Return: 0 on success, negative error code on failure
  79. */
  80. static int loader_xfer_cmd(struct ishtp_device *dev, void *req, int req_len,
  81. void *resp, int resp_len)
  82. {
  83. union loader_msg_header req_hdr;
  84. union loader_msg_header resp_hdr;
  85. struct device *devc = dev->devc;
  86. int rv;
  87. dev->fw_loader_rx_buf = resp;
  88. dev->fw_loader_rx_size = resp_len;
  89. rv = loader_write_message(dev, req, req_len);
  90. req_hdr.val32 = le32_to_cpup(req);
  91. if (rv < 0) {
  92. dev_err(devc, "write cmd %u failed:%d\n", req_hdr.command, rv);
  93. return rv;
  94. }
  95. /* Wait the ACK */
  96. wait_event_interruptible_timeout(dev->wait_loader_recvd_msg, dev->fw_loader_received,
  97. ISHTP_LOADER_TIMEOUT);
  98. resp_hdr.val32 = le32_to_cpup(resp);
  99. dev->fw_loader_rx_size = 0;
  100. dev->fw_loader_rx_buf = NULL;
  101. if (!dev->fw_loader_received) {
  102. dev_err(devc, "wait response of cmd %u timeout\n", req_hdr.command);
  103. return -ETIMEDOUT;
  104. }
  105. if (!resp_hdr.is_response) {
  106. dev_err(devc, "not a response for %u\n", req_hdr.command);
  107. return -EBADMSG;
  108. }
  109. if (req_hdr.command != resp_hdr.command) {
  110. dev_err(devc, "unexpected cmd response %u:%u\n", req_hdr.command,
  111. resp_hdr.command);
  112. return -EBADMSG;
  113. }
  114. if (resp_hdr.status) {
  115. dev_err(devc, "cmd %u failed %u\n", req_hdr.command, resp_hdr.status);
  116. return -EIO;
  117. }
  118. return 0;
  119. }
  120. /**
  121. * release_dma_bufs() - Release the DMA buffer for transferring firmware fragments
  122. * @dev: The ISHTP device
  123. * @fragment: The ISHTP firmware fragment descriptor
  124. * @dma_bufs: The array of DMA fragment buffers
  125. * @fragment_size: The size of a single DMA fragment
  126. */
  127. static void release_dma_bufs(struct ishtp_device *dev,
  128. struct loader_xfer_dma_fragment *fragment,
  129. void **dma_bufs, u32 fragment_size)
  130. {
  131. dma_addr_t dma_addr;
  132. int i;
  133. for (i = 0; i < FRAGMENT_MAX_NUM; i++) {
  134. if (dma_bufs[i]) {
  135. dma_addr = le64_to_cpu(fragment->fragment_tbl[i].ddr_adrs);
  136. dma_free_coherent(dev->devc, fragment_size, dma_bufs[i], dma_addr);
  137. dma_bufs[i] = NULL;
  138. }
  139. }
  140. }
  141. /**
  142. * prepare_dma_bufs() - Prepare the DMA buffer for transferring firmware fragments
  143. * @dev: The ISHTP device
  144. * @ish_fw: The ISH firmware
  145. * @fragment: The ISHTP firmware fragment descriptor
  146. * @dma_bufs: The array of DMA fragment buffers
  147. * @fragment_size: The size of a single DMA fragment
  148. * @fragment_count: Number of fragments
  149. *
  150. * Return: 0 on success, negative error code on failure
  151. */
  152. static int prepare_dma_bufs(struct ishtp_device *dev,
  153. const struct firmware *ish_fw,
  154. struct loader_xfer_dma_fragment *fragment,
  155. void **dma_bufs, u32 fragment_size, u32 fragment_count)
  156. {
  157. dma_addr_t dma_addr;
  158. u32 offset = 0;
  159. u32 length;
  160. int i;
  161. for (i = 0; i < fragment_count && offset < ish_fw->size; i++) {
  162. dma_bufs[i] = dma_alloc_coherent(dev->devc, fragment_size, &dma_addr, GFP_KERNEL);
  163. if (!dma_bufs[i])
  164. return -ENOMEM;
  165. fragment->fragment_tbl[i].ddr_adrs = cpu_to_le64(dma_addr);
  166. length = clamp(ish_fw->size - offset, 0, fragment_size);
  167. fragment->fragment_tbl[i].length = cpu_to_le32(length);
  168. fragment->fragment_tbl[i].fw_off = cpu_to_le32(offset);
  169. memcpy(dma_bufs[i], ish_fw->data + offset, length);
  170. clflush_cache_range(dma_bufs[i], fragment_size);
  171. offset += length;
  172. }
  173. return 0;
  174. }
  175. #define ISH_FW_FILE_VENDOR_NAME_SKU_FMT "intel/ish/ish_%s_%08x_%08x_%08x.bin"
  176. #define ISH_FW_FILE_VENDOR_SKU_FMT "intel/ish/ish_%s_%08x_%08x.bin"
  177. #define ISH_FW_FILE_VENDOR_NAME_FMT "intel/ish/ish_%s_%08x_%08x.bin"
  178. #define ISH_FW_FILE_VENDOR_FMT "intel/ish/ish_%s_%08x.bin"
  179. #define ISH_FW_FILE_DEFAULT_FMT "intel/ish/ish_%s.bin"
  180. #define ISH_FW_FILENAME_LEN_MAX 56
  181. #define ISH_CRC_INIT (~0u)
  182. #define ISH_CRC_XOROUT (~0u)
  183. static int _request_ish_firmware(const struct firmware **firmware_p,
  184. const char *name, struct device *dev)
  185. {
  186. int ret;
  187. dev_dbg(dev, "Try to load firmware: %s\n", name);
  188. ret = firmware_request_nowarn(firmware_p, name, dev);
  189. if (!ret)
  190. dev_info(dev, "load firmware: %s\n", name);
  191. return ret;
  192. }
  193. /**
  194. * request_ish_firmware() - Request and load the ISH firmware.
  195. * @firmware_p: Pointer to the firmware image.
  196. * @dev: Device for which firmware is being requested.
  197. *
  198. * This function attempts to load the Integrated Sensor Hub (ISH) firmware
  199. * for the given device in the following order, prioritizing custom firmware
  200. * with more precise matching patterns:
  201. *
  202. * ish_${fw_generation}_${SYS_VENDOR_CRC32}_$(PRODUCT_NAME_CRC32)_${PRODUCT_SKU_CRC32}.bin
  203. * ish_${fw_generation}_${SYS_VENDOR_CRC32}_${PRODUCT_SKU_CRC32}.bin
  204. * ish_${fw_generation}_${SYS_VENDOR_CRC32}_$(PRODUCT_NAME_CRC32).bin
  205. * ish_${fw_generation}_${SYS_VENDOR_CRC32}.bin
  206. * ish_${fw_generation}.bin
  207. *
  208. * The driver will load the first matching firmware and skip the rest. If no
  209. * matching firmware is found, it will proceed to the next pattern in the
  210. * specified order. If all searches fail, the default Intel firmware, listed
  211. * last in the order above, will be loaded.
  212. *
  213. * The firmware file name is constructed using CRC32 checksums of strings.
  214. * This is done to create a valid file name that does not contain spaces
  215. * or special characters which may be present in the original strings.
  216. *
  217. * The CRC-32 algorithm uses the following parameters:
  218. * Poly: 0x04C11DB7
  219. * Init: 0xFFFFFFFF
  220. * RefIn: true
  221. * RefOut: true
  222. * XorOut: 0xFFFFFFFF
  223. *
  224. * Return: 0 on success, negative error code on failure.
  225. */
  226. static int request_ish_firmware(const struct firmware **firmware_p,
  227. struct device *dev)
  228. {
  229. const char *gen, *sys_vendor, *product_name, *product_sku;
  230. struct ishtp_device *ishtp = dev_get_drvdata(dev);
  231. u32 vendor_crc, name_crc, sku_crc;
  232. char filename[ISH_FW_FILENAME_LEN_MAX];
  233. int ret;
  234. gen = ishtp->driver_data->fw_generation;
  235. sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
  236. product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
  237. product_sku = dmi_get_system_info(DMI_PRODUCT_SKU);
  238. if (sys_vendor)
  239. vendor_crc = crc32(ISH_CRC_INIT, sys_vendor, strlen(sys_vendor)) ^ ISH_CRC_XOROUT;
  240. if (product_name)
  241. name_crc = crc32(ISH_CRC_INIT, product_name, strlen(product_name)) ^ ISH_CRC_XOROUT;
  242. if (product_sku)
  243. sku_crc = crc32(ISH_CRC_INIT, product_sku, strlen(product_sku)) ^ ISH_CRC_XOROUT;
  244. if (sys_vendor && product_name && product_sku) {
  245. snprintf(filename, sizeof(filename), ISH_FW_FILE_VENDOR_NAME_SKU_FMT, gen,
  246. vendor_crc, name_crc, sku_crc);
  247. ret = _request_ish_firmware(firmware_p, filename, dev);
  248. if (!ret)
  249. return 0;
  250. }
  251. if (sys_vendor && product_sku) {
  252. snprintf(filename, sizeof(filename), ISH_FW_FILE_VENDOR_SKU_FMT, gen, vendor_crc,
  253. sku_crc);
  254. ret = _request_ish_firmware(firmware_p, filename, dev);
  255. if (!ret)
  256. return 0;
  257. }
  258. if (sys_vendor && product_name) {
  259. snprintf(filename, sizeof(filename), ISH_FW_FILE_VENDOR_NAME_FMT, gen, vendor_crc,
  260. name_crc);
  261. ret = _request_ish_firmware(firmware_p, filename, dev);
  262. if (!ret)
  263. return 0;
  264. }
  265. if (sys_vendor) {
  266. snprintf(filename, sizeof(filename), ISH_FW_FILE_VENDOR_FMT, gen, vendor_crc);
  267. ret = _request_ish_firmware(firmware_p, filename, dev);
  268. if (!ret)
  269. return 0;
  270. }
  271. snprintf(filename, sizeof(filename), ISH_FW_FILE_DEFAULT_FMT, gen);
  272. return _request_ish_firmware(firmware_p, filename, dev);
  273. }
  274. /**
  275. * ishtp_loader_work() - Load the ISHTP firmware
  276. * @work: The work structure
  277. *
  278. * The ISH Loader attempts to load firmware by sending a series of commands
  279. * to the ISH device. If a command fails to be acknowledged by the ISH device,
  280. * the loader will retry sending the command, up to a maximum of
  281. * ISHTP_LOADER_RETRY_TIMES.
  282. *
  283. * After the maximum number of retries has been reached without success, the
  284. * ISH bootloader will return an error status code and will no longer respond
  285. * to the driver's commands. This behavior indicates that the ISH Loader has
  286. * encountered a critical error during the firmware loading process.
  287. *
  288. * In such a case, where the ISH bootloader is unresponsive after all retries
  289. * have been exhausted, a platform reset is required to restore communication
  290. * with the ISH device and to recover from this error state.
  291. */
  292. void ishtp_loader_work(struct work_struct *work)
  293. {
  294. DEFINE_RAW_FLEX(struct loader_xfer_dma_fragment, fragment, fragment_tbl, FRAGMENT_MAX_NUM);
  295. struct ishtp_device *dev = container_of(work, struct ishtp_device, work_fw_loader);
  296. union loader_msg_header query_hdr = { .command = LOADER_CMD_XFER_QUERY, };
  297. union loader_msg_header start_hdr = { .command = LOADER_CMD_START, };
  298. union loader_msg_header fragment_hdr = { .command = LOADER_CMD_XFER_FRAGMENT, };
  299. struct loader_xfer_query query = { .header = cpu_to_le32(query_hdr.val32), };
  300. struct loader_start start = { .header = cpu_to_le32(start_hdr.val32), };
  301. union loader_recv_message recv_msg;
  302. const struct firmware *ish_fw;
  303. void *dma_bufs[FRAGMENT_MAX_NUM] = {};
  304. u32 fragment_size;
  305. u32 fragment_count;
  306. int retry = ISHTP_LOADER_RETRY_TIMES;
  307. int rv;
  308. rv = request_ish_firmware(&ish_fw, dev->devc);
  309. if (rv < 0) {
  310. dev_err(dev->devc, "request ISH firmware failed:%d\n", rv);
  311. return;
  312. }
  313. fragment->fragment.header = cpu_to_le32(fragment_hdr.val32);
  314. fragment->fragment.xfer_mode = cpu_to_le32(LOADER_XFER_MODE_DMA);
  315. fragment->fragment.is_last = cpu_to_le32(1);
  316. fragment->fragment.size = cpu_to_le32(ish_fw->size);
  317. /* Calculate the size of a single DMA fragment */
  318. fragment_size = PFN_ALIGN(DIV_ROUND_UP(ish_fw->size, FRAGMENT_MAX_NUM));
  319. /* Calculate the count of DMA fragments */
  320. fragment_count = DIV_ROUND_UP(ish_fw->size, fragment_size);
  321. fragment->fragment_cnt = cpu_to_le32(fragment_count);
  322. rv = prepare_dma_bufs(dev, ish_fw, fragment, dma_bufs, fragment_size, fragment_count);
  323. if (rv) {
  324. dev_err(dev->devc, "prepare DMA buffer failed.\n");
  325. goto out;
  326. }
  327. do {
  328. query.image_size = cpu_to_le32(ish_fw->size);
  329. rv = loader_xfer_cmd(dev, &query, sizeof(query), recv_msg.raw_data,
  330. sizeof(struct loader_xfer_query_ack));
  331. if (rv)
  332. continue; /* try again if failed */
  333. dev_dbg(dev->devc, "ISH Version %u.%u.%u.%u\n",
  334. recv_msg.query_ack.version_major,
  335. recv_msg.query_ack.version_minor,
  336. recv_msg.query_ack.version_hotfix,
  337. recv_msg.query_ack.version_build);
  338. rv = loader_xfer_cmd(dev, fragment,
  339. struct_size(fragment, fragment_tbl, fragment_count),
  340. recv_msg.raw_data, sizeof(struct loader_xfer_fragment_ack));
  341. if (rv)
  342. continue; /* try again if failed */
  343. rv = loader_xfer_cmd(dev, &start, sizeof(start), recv_msg.raw_data,
  344. sizeof(struct loader_start_ack));
  345. if (rv)
  346. continue; /* try again if failed */
  347. dev_info(dev->devc, "firmware loaded. size:%zu\n", ish_fw->size);
  348. break;
  349. } while (--retry);
  350. out:
  351. release_dma_bufs(dev, fragment, dma_bufs, fragment_size);
  352. release_firmware(ish_fw);
  353. }