ishtp-fw-loader.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ISH-TP client driver for ISH firmware loading
  4. *
  5. * Copyright (c) 2019, Intel Corporation.
  6. */
  7. #include <linux/firmware.h>
  8. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/intel-ish-client-if.h>
  11. #include <linux/property.h>
  12. #include <asm/cacheflush.h>
  13. /* Number of times we attempt to load the firmware before giving up */
  14. #define MAX_LOAD_ATTEMPTS 3
  15. /* ISH TX/RX ring buffer pool size */
  16. #define LOADER_CL_RX_RING_SIZE 1
  17. #define LOADER_CL_TX_RING_SIZE 1
  18. /*
  19. * ISH Shim firmware loader reserves 4 Kb buffer in SRAM. The buffer is
  20. * used to temporarily hold the data transferred from host to Shim
  21. * firmware loader. Reason for the odd size of 3968 bytes? Each IPC
  22. * transfer is 128 bytes (= 4 bytes header + 124 bytes payload). So the
  23. * 4 Kb buffer can hold maximum of 32 IPC transfers, which means we can
  24. * have a max payload of 3968 bytes (= 32 x 124 payload).
  25. */
  26. #define LOADER_SHIM_IPC_BUF_SIZE 3968
  27. /**
  28. * enum ish_loader_commands - ISH loader host commands.
  29. * @LOADER_CMD_XFER_QUERY: Query the Shim firmware loader for
  30. * capabilities
  31. * @LOADER_CMD_XFER_FRAGMENT: Transfer one firmware image fragment at a
  32. * time. The command may be executed
  33. * multiple times until the entire firmware
  34. * image is downloaded to SRAM.
  35. * @LOADER_CMD_START: Start executing the main firmware.
  36. */
  37. enum ish_loader_commands {
  38. LOADER_CMD_XFER_QUERY = 0,
  39. LOADER_CMD_XFER_FRAGMENT,
  40. LOADER_CMD_START,
  41. };
  42. /* Command bit mask */
  43. #define CMD_MASK GENMASK(6, 0)
  44. #define IS_RESPONSE BIT(7)
  45. /*
  46. * ISH firmware max delay for one transmit failure is 1 Hz,
  47. * and firmware will retry 2 times, so 3 Hz is used for timeout.
  48. */
  49. #define ISHTP_SEND_TIMEOUT (3 * HZ)
  50. /*
  51. * Loader transfer modes:
  52. *
  53. * LOADER_XFER_MODE_ISHTP mode uses the existing ISH-TP mechanism to
  54. * transfer data. This may use IPC or DMA if supported in firmware.
  55. * The buffer size is limited to 4 Kb by the IPC/ISH-TP protocol for
  56. * both IPC & DMA (legacy).
  57. *
  58. * LOADER_XFER_MODE_DIRECT_DMA - firmware loading is a bit different
  59. * from the sensor data streaming. Here we download a large (300+ Kb)
  60. * image directly to ISH SRAM memory. There is limited benefit of
  61. * DMA'ing 300 Kb image in 4 Kb chucks limit. Hence, we introduce
  62. * this "direct dma" mode, where we do not use ISH-TP for DMA, but
  63. * instead manage the DMA directly in kernel driver and Shim firmware
  64. * loader (allocate buffer, break in chucks and transfer). This allows
  65. * to overcome 4 Kb limit, and optimize the data flow path in firmware.
  66. */
  67. #define LOADER_XFER_MODE_DIRECT_DMA BIT(0)
  68. #define LOADER_XFER_MODE_ISHTP BIT(1)
  69. /* ISH Transport Loader client unique GUID */
  70. static const struct ishtp_device_id loader_ishtp_id_table[] = {
  71. { .guid = GUID_INIT(0xc804d06a, 0x55bd, 0x4ea7,
  72. 0xad, 0xed, 0x1e, 0x31, 0x22, 0x8c, 0x76, 0xdc) },
  73. { }
  74. };
  75. MODULE_DEVICE_TABLE(ishtp, loader_ishtp_id_table);
  76. #define FILENAME_SIZE 256
  77. /*
  78. * The firmware loading latency will be minimum if we can DMA the
  79. * entire ISH firmware image in one go. This requires that we allocate
  80. * a large DMA buffer in kernel, which could be problematic on some
  81. * platforms. So here we limit the DMA buffer size via a module_param.
  82. * We default to 4 pages, but a customer can set it to higher limit if
  83. * deemed appropriate for his platform.
  84. */
  85. static int dma_buf_size_limit = 4 * PAGE_SIZE;
  86. /**
  87. * struct loader_msg_hdr - Header for ISH Loader commands.
  88. * @command: LOADER_CMD* commands. Bit 7 is the response.
  89. * @reserved: Reserved space
  90. * @status: Command response status. Non 0, is error
  91. * condition.
  92. *
  93. * This structure is used as header for every command/data sent/received
  94. * between Host driver and ISH Shim firmware loader.
  95. */
  96. struct loader_msg_hdr {
  97. u8 command;
  98. u8 reserved[2];
  99. u8 status;
  100. } __packed;
  101. struct loader_xfer_query {
  102. struct loader_msg_hdr hdr;
  103. u32 image_size;
  104. } __packed;
  105. struct ish_fw_version {
  106. u16 major;
  107. u16 minor;
  108. u16 hotfix;
  109. u16 build;
  110. } __packed;
  111. union loader_version {
  112. u32 value;
  113. struct {
  114. u8 major;
  115. u8 minor;
  116. u8 hotfix;
  117. u8 build;
  118. };
  119. } __packed;
  120. struct loader_capability {
  121. u32 max_fw_image_size;
  122. u32 xfer_mode;
  123. u32 max_dma_buf_size; /* only for dma mode, multiples of cacheline */
  124. } __packed;
  125. struct shim_fw_info {
  126. struct ish_fw_version ish_fw_version;
  127. u32 protocol_version;
  128. union loader_version ldr_version;
  129. struct loader_capability ldr_capability;
  130. } __packed;
  131. struct loader_xfer_query_response {
  132. struct loader_msg_hdr hdr;
  133. struct shim_fw_info fw_info;
  134. } __packed;
  135. struct loader_xfer_fragment {
  136. struct loader_msg_hdr hdr;
  137. u32 xfer_mode;
  138. u32 offset;
  139. u32 size;
  140. u32 is_last;
  141. } __packed;
  142. struct loader_xfer_ipc_fragment {
  143. struct loader_xfer_fragment fragment;
  144. u8 data[] ____cacheline_aligned; /* variable length payload here */
  145. } __packed;
  146. struct loader_xfer_dma_fragment {
  147. struct loader_xfer_fragment fragment;
  148. u64 ddr_phys_addr;
  149. } __packed;
  150. struct loader_start {
  151. struct loader_msg_hdr hdr;
  152. } __packed;
  153. /**
  154. * struct response_info - Encapsulate firmware response related
  155. * information for passing between function
  156. * loader_cl_send() and process_recv() callback.
  157. * @data: Copy the data received from firmware here.
  158. * @max_size: Max size allocated for the @data buffer. If the
  159. * received data exceeds this value, we log an
  160. * error.
  161. * @size: Actual size of data received from firmware.
  162. * @error: Returns 0 for success, negative error code for a
  163. * failure in function process_recv().
  164. * @received: Set to true on receiving a valid firmware
  165. * response to host command
  166. * @wait_queue: Wait queue for Host firmware loading where the
  167. * client sends message to ISH firmware and waits
  168. * for response
  169. */
  170. struct response_info {
  171. void *data;
  172. size_t max_size;
  173. size_t size;
  174. int error;
  175. bool received;
  176. wait_queue_head_t wait_queue;
  177. };
  178. /*
  179. * struct ishtp_cl_data - Encapsulate per ISH-TP Client Data.
  180. * @work_ishtp_reset: Work queue for reset handling.
  181. * @work_fw_load: Work queue for host firmware loading.
  182. * @flag_retry: Flag for indicating host firmware loading should
  183. * be retried.
  184. * @retry_count: Count the number of retries.
  185. *
  186. * This structure is used to store data per client.
  187. */
  188. struct ishtp_cl_data {
  189. struct ishtp_cl *loader_ishtp_cl;
  190. struct ishtp_cl_device *cl_device;
  191. /*
  192. * Used for passing firmware response information between
  193. * loader_cl_send() and process_recv() callback.
  194. */
  195. struct response_info response;
  196. struct work_struct work_ishtp_reset;
  197. struct work_struct work_fw_load;
  198. /*
  199. * In certain failure scenrios, it makes sense to reset the ISH
  200. * subsystem and retry Host firmware loading (e.g. bad message
  201. * packet, ENOMEM, etc.). On the other hand, failures due to
  202. * protocol mismatch, etc., are not recoverable. We do not
  203. * retry them.
  204. *
  205. * If set, the flag indicates that we should re-try the
  206. * particular failure.
  207. */
  208. bool flag_retry;
  209. int retry_count;
  210. };
  211. #define IPC_FRAGMENT_DATA_PREAMBLE \
  212. offsetof(struct loader_xfer_ipc_fragment, data)
  213. #define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
  214. /**
  215. * get_firmware_variant() - Gets the filename of firmware image to be
  216. * loaded based on platform variant.
  217. * @client_data: Client data instance.
  218. * @filename: Returns firmware filename.
  219. *
  220. * Queries the firmware-name device property string.
  221. *
  222. * Return: 0 for success, negative error code for failure.
  223. */
  224. static int get_firmware_variant(struct ishtp_cl_data *client_data,
  225. char *filename)
  226. {
  227. int rv;
  228. const char *val;
  229. struct device *devc = ishtp_get_pci_device(client_data->cl_device);
  230. rv = device_property_read_string(devc, "firmware-name", &val);
  231. if (rv < 0) {
  232. dev_err(devc,
  233. "Error: ISH firmware-name device property required\n");
  234. return rv;
  235. }
  236. return snprintf(filename, FILENAME_SIZE, "intel/%s", val);
  237. }
  238. /**
  239. * loader_cl_send() - Send message from host to firmware
  240. *
  241. * @client_data: Client data instance
  242. * @out_msg: Message buffer to be sent to firmware
  243. * @out_size: Size of out going message
  244. * @in_msg: Message buffer where the incoming data copied.
  245. * This buffer is allocated by calling
  246. * @in_size: Max size of incoming message
  247. *
  248. * Return: Number of bytes copied in the in_msg on success, negative
  249. * error code on failure.
  250. */
  251. static int loader_cl_send(struct ishtp_cl_data *client_data,
  252. u8 *out_msg, size_t out_size,
  253. u8 *in_msg, size_t in_size)
  254. {
  255. int rv;
  256. struct loader_msg_hdr *out_hdr = (struct loader_msg_hdr *)out_msg;
  257. struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl;
  258. dev_dbg(cl_data_to_dev(client_data),
  259. "%s: command=%02lx is_response=%u status=%02x\n",
  260. __func__,
  261. out_hdr->command & CMD_MASK,
  262. out_hdr->command & IS_RESPONSE ? 1 : 0,
  263. out_hdr->status);
  264. /* Setup in coming buffer & size */
  265. client_data->response.data = in_msg;
  266. client_data->response.max_size = in_size;
  267. client_data->response.error = 0;
  268. client_data->response.received = false;
  269. rv = ishtp_cl_send(loader_ishtp_cl, out_msg, out_size);
  270. if (rv < 0) {
  271. dev_err(cl_data_to_dev(client_data),
  272. "ishtp_cl_send error %d\n", rv);
  273. return rv;
  274. }
  275. wait_event_interruptible_timeout(client_data->response.wait_queue,
  276. client_data->response.received,
  277. ISHTP_SEND_TIMEOUT);
  278. if (!client_data->response.received) {
  279. dev_err(cl_data_to_dev(client_data),
  280. "Timed out for response to command=%02lx",
  281. out_hdr->command & CMD_MASK);
  282. return -ETIMEDOUT;
  283. }
  284. if (client_data->response.error < 0)
  285. return client_data->response.error;
  286. return client_data->response.size;
  287. }
  288. /**
  289. * process_recv() - Receive and parse incoming packet
  290. * @loader_ishtp_cl: Client instance to get stats
  291. * @rb_in_proc: ISH received message buffer
  292. *
  293. * Parse the incoming packet. If it is a response packet then it will
  294. * update received and wake up the caller waiting to for the response.
  295. */
  296. static void process_recv(struct ishtp_cl *loader_ishtp_cl,
  297. struct ishtp_cl_rb *rb_in_proc)
  298. {
  299. struct loader_msg_hdr *hdr;
  300. size_t data_len = rb_in_proc->buf_idx;
  301. struct ishtp_cl_data *client_data =
  302. ishtp_get_client_data(loader_ishtp_cl);
  303. /* Sanity check */
  304. if (!client_data->response.data) {
  305. dev_err(cl_data_to_dev(client_data),
  306. "Receiving buffer is null. Should be allocated by calling function\n");
  307. client_data->response.error = -EINVAL;
  308. goto end;
  309. }
  310. if (client_data->response.received) {
  311. dev_err(cl_data_to_dev(client_data),
  312. "Previous firmware message not yet processed\n");
  313. client_data->response.error = -EINVAL;
  314. goto end;
  315. }
  316. /*
  317. * All firmware messages have a header. Check buffer size
  318. * before accessing elements inside.
  319. */
  320. if (!rb_in_proc->buffer.data) {
  321. dev_warn(cl_data_to_dev(client_data),
  322. "rb_in_proc->buffer.data returned null");
  323. client_data->response.error = -EBADMSG;
  324. goto end;
  325. }
  326. if (data_len < sizeof(struct loader_msg_hdr)) {
  327. dev_err(cl_data_to_dev(client_data),
  328. "data size %zu is less than header %zu\n",
  329. data_len, sizeof(struct loader_msg_hdr));
  330. client_data->response.error = -EMSGSIZE;
  331. goto end;
  332. }
  333. hdr = (struct loader_msg_hdr *)rb_in_proc->buffer.data;
  334. dev_dbg(cl_data_to_dev(client_data),
  335. "%s: command=%02lx is_response=%u status=%02x\n",
  336. __func__,
  337. hdr->command & CMD_MASK,
  338. hdr->command & IS_RESPONSE ? 1 : 0,
  339. hdr->status);
  340. if (((hdr->command & CMD_MASK) != LOADER_CMD_XFER_QUERY) &&
  341. ((hdr->command & CMD_MASK) != LOADER_CMD_XFER_FRAGMENT) &&
  342. ((hdr->command & CMD_MASK) != LOADER_CMD_START)) {
  343. dev_err(cl_data_to_dev(client_data),
  344. "Invalid command=%02lx\n",
  345. hdr->command & CMD_MASK);
  346. client_data->response.error = -EPROTO;
  347. goto end;
  348. }
  349. if (data_len > client_data->response.max_size) {
  350. dev_err(cl_data_to_dev(client_data),
  351. "Received buffer size %zu is larger than allocated buffer %zu\n",
  352. data_len, client_data->response.max_size);
  353. client_data->response.error = -EMSGSIZE;
  354. goto end;
  355. }
  356. /* We expect only "response" messages from firmware */
  357. if (!(hdr->command & IS_RESPONSE)) {
  358. dev_err(cl_data_to_dev(client_data),
  359. "Invalid response to command\n");
  360. client_data->response.error = -EIO;
  361. goto end;
  362. }
  363. if (hdr->status) {
  364. dev_err(cl_data_to_dev(client_data),
  365. "Loader returned status %d\n",
  366. hdr->status);
  367. client_data->response.error = -EIO;
  368. goto end;
  369. }
  370. /* Update the actual received buffer size */
  371. client_data->response.size = data_len;
  372. /*
  373. * Copy the buffer received in firmware response for the
  374. * calling thread.
  375. */
  376. memcpy(client_data->response.data,
  377. rb_in_proc->buffer.data, data_len);
  378. /* Set flag before waking up the caller */
  379. client_data->response.received = true;
  380. end:
  381. /* Free the buffer */
  382. ishtp_cl_io_rb_recycle(rb_in_proc);
  383. rb_in_proc = NULL;
  384. /* Wake the calling thread */
  385. wake_up_interruptible(&client_data->response.wait_queue);
  386. }
  387. /**
  388. * loader_cl_event_cb() - bus driver callback for incoming message
  389. * @cl_device: Pointer to the ishtp client device for which this
  390. * message is targeted
  391. *
  392. * Remove the packet from the list and process the message by calling
  393. * process_recv
  394. */
  395. static void loader_cl_event_cb(struct ishtp_cl_device *cl_device)
  396. {
  397. struct ishtp_cl_rb *rb_in_proc;
  398. struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
  399. while ((rb_in_proc = ishtp_cl_rx_get_rb(loader_ishtp_cl)) != NULL) {
  400. /* Process the data packet from firmware */
  401. process_recv(loader_ishtp_cl, rb_in_proc);
  402. }
  403. }
  404. /**
  405. * ish_query_loader_prop() - Query ISH Shim firmware loader
  406. * @client_data: Client data instance
  407. * @fw: Pointer to firmware data struct in host memory
  408. * @fw_info: Loader firmware properties
  409. *
  410. * This function queries the ISH Shim firmware loader for capabilities.
  411. *
  412. * Return: 0 for success, negative error code for failure.
  413. */
  414. static int ish_query_loader_prop(struct ishtp_cl_data *client_data,
  415. const struct firmware *fw,
  416. struct shim_fw_info *fw_info)
  417. {
  418. int rv;
  419. struct loader_xfer_query ldr_xfer_query;
  420. struct loader_xfer_query_response ldr_xfer_query_resp;
  421. memset(&ldr_xfer_query, 0, sizeof(ldr_xfer_query));
  422. ldr_xfer_query.hdr.command = LOADER_CMD_XFER_QUERY;
  423. ldr_xfer_query.image_size = fw->size;
  424. rv = loader_cl_send(client_data,
  425. (u8 *)&ldr_xfer_query,
  426. sizeof(ldr_xfer_query),
  427. (u8 *)&ldr_xfer_query_resp,
  428. sizeof(ldr_xfer_query_resp));
  429. if (rv < 0) {
  430. client_data->flag_retry = true;
  431. *fw_info = (struct shim_fw_info){};
  432. return rv;
  433. }
  434. /* On success, the return value is the received buffer size */
  435. if (rv != sizeof(struct loader_xfer_query_response)) {
  436. dev_err(cl_data_to_dev(client_data),
  437. "data size %d is not equal to size of loader_xfer_query_response %zu\n",
  438. rv, sizeof(struct loader_xfer_query_response));
  439. client_data->flag_retry = true;
  440. *fw_info = (struct shim_fw_info){};
  441. return -EMSGSIZE;
  442. }
  443. /* Save fw_info for use outside this function */
  444. *fw_info = ldr_xfer_query_resp.fw_info;
  445. /* Loader firmware properties */
  446. dev_dbg(cl_data_to_dev(client_data),
  447. "ish_fw_version: major=%d minor=%d hotfix=%d build=%d protocol_version=0x%x loader_version=%d\n",
  448. fw_info->ish_fw_version.major,
  449. fw_info->ish_fw_version.minor,
  450. fw_info->ish_fw_version.hotfix,
  451. fw_info->ish_fw_version.build,
  452. fw_info->protocol_version,
  453. fw_info->ldr_version.value);
  454. dev_dbg(cl_data_to_dev(client_data),
  455. "loader_capability: max_fw_image_size=0x%x xfer_mode=%d max_dma_buf_size=0x%x dma_buf_size_limit=0x%x\n",
  456. fw_info->ldr_capability.max_fw_image_size,
  457. fw_info->ldr_capability.xfer_mode,
  458. fw_info->ldr_capability.max_dma_buf_size,
  459. dma_buf_size_limit);
  460. /* Sanity checks */
  461. if (fw_info->ldr_capability.max_fw_image_size < fw->size) {
  462. dev_err(cl_data_to_dev(client_data),
  463. "ISH firmware size %zu is greater than Shim firmware loader max supported %d\n",
  464. fw->size,
  465. fw_info->ldr_capability.max_fw_image_size);
  466. return -ENOSPC;
  467. }
  468. /* For DMA the buffer size should be multiple of cacheline size */
  469. if ((fw_info->ldr_capability.xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) &&
  470. (fw_info->ldr_capability.max_dma_buf_size % L1_CACHE_BYTES)) {
  471. dev_err(cl_data_to_dev(client_data),
  472. "Shim firmware loader buffer size %d should be multiple of cacheline\n",
  473. fw_info->ldr_capability.max_dma_buf_size);
  474. return -EINVAL;
  475. }
  476. return 0;
  477. }
  478. /**
  479. * ish_fw_xfer_ishtp() - Loads ISH firmware using ishtp interface
  480. * @client_data: Client data instance
  481. * @fw: Pointer to firmware data struct in host memory
  482. *
  483. * This function uses ISH-TP to transfer ISH firmware from host to
  484. * ISH SRAM. Lower layers may use IPC or DMA depending on firmware
  485. * support.
  486. *
  487. * Return: 0 for success, negative error code for failure.
  488. */
  489. static int ish_fw_xfer_ishtp(struct ishtp_cl_data *client_data,
  490. const struct firmware *fw)
  491. {
  492. int rv;
  493. u32 fragment_offset, fragment_size, payload_max_size;
  494. struct loader_xfer_ipc_fragment *ldr_xfer_ipc_frag;
  495. struct loader_msg_hdr ldr_xfer_ipc_ack;
  496. payload_max_size =
  497. LOADER_SHIM_IPC_BUF_SIZE - IPC_FRAGMENT_DATA_PREAMBLE;
  498. ldr_xfer_ipc_frag = kzalloc(LOADER_SHIM_IPC_BUF_SIZE, GFP_KERNEL);
  499. if (!ldr_xfer_ipc_frag) {
  500. client_data->flag_retry = true;
  501. return -ENOMEM;
  502. }
  503. ldr_xfer_ipc_frag->fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT;
  504. ldr_xfer_ipc_frag->fragment.xfer_mode = LOADER_XFER_MODE_ISHTP;
  505. /* Break the firmware image into fragments and send as ISH-TP payload */
  506. fragment_offset = 0;
  507. while (fragment_offset < fw->size) {
  508. if (fragment_offset + payload_max_size < fw->size) {
  509. fragment_size = payload_max_size;
  510. ldr_xfer_ipc_frag->fragment.is_last = 0;
  511. } else {
  512. fragment_size = fw->size - fragment_offset;
  513. ldr_xfer_ipc_frag->fragment.is_last = 1;
  514. }
  515. ldr_xfer_ipc_frag->fragment.offset = fragment_offset;
  516. ldr_xfer_ipc_frag->fragment.size = fragment_size;
  517. memcpy(ldr_xfer_ipc_frag->data,
  518. &fw->data[fragment_offset],
  519. fragment_size);
  520. dev_dbg(cl_data_to_dev(client_data),
  521. "xfer_mode=ipc offset=0x%08x size=0x%08x is_last=%d\n",
  522. ldr_xfer_ipc_frag->fragment.offset,
  523. ldr_xfer_ipc_frag->fragment.size,
  524. ldr_xfer_ipc_frag->fragment.is_last);
  525. rv = loader_cl_send(client_data,
  526. (u8 *)ldr_xfer_ipc_frag,
  527. IPC_FRAGMENT_DATA_PREAMBLE + fragment_size,
  528. (u8 *)&ldr_xfer_ipc_ack,
  529. sizeof(ldr_xfer_ipc_ack));
  530. if (rv < 0) {
  531. client_data->flag_retry = true;
  532. goto end_err_resp_buf_release;
  533. }
  534. fragment_offset += fragment_size;
  535. }
  536. kfree(ldr_xfer_ipc_frag);
  537. return 0;
  538. end_err_resp_buf_release:
  539. /* Free ISH buffer if not done already, in error case */
  540. kfree(ldr_xfer_ipc_frag);
  541. return rv;
  542. }
  543. /**
  544. * ish_fw_xfer_direct_dma() - Loads ISH firmware using direct dma
  545. * @client_data: Client data instance
  546. * @fw: Pointer to firmware data struct in host memory
  547. * @fw_info: Loader firmware properties
  548. *
  549. * Host firmware load is a unique case where we need to download
  550. * a large firmware image (200+ Kb). This function implements
  551. * direct DMA transfer in kernel and ISH firmware. This allows
  552. * us to overcome the ISH-TP 4 Kb limit, and allows us to DMA
  553. * directly to ISH UMA at location of choice.
  554. * Function depends on corresponding support in ISH firmware.
  555. *
  556. * Return: 0 for success, negative error code for failure.
  557. */
  558. static int ish_fw_xfer_direct_dma(struct ishtp_cl_data *client_data,
  559. const struct firmware *fw,
  560. const struct shim_fw_info fw_info)
  561. {
  562. int rv;
  563. void *dma_buf;
  564. dma_addr_t dma_buf_phy;
  565. u32 fragment_offset, fragment_size, payload_max_size;
  566. struct loader_msg_hdr ldr_xfer_dma_frag_ack;
  567. struct loader_xfer_dma_fragment ldr_xfer_dma_frag;
  568. struct device *devc = ishtp_get_pci_device(client_data->cl_device);
  569. u32 shim_fw_buf_size =
  570. fw_info.ldr_capability.max_dma_buf_size;
  571. /*
  572. * payload_max_size should be set to minimum of
  573. * (1) Size of firmware to be loaded,
  574. * (2) Max DMA buffer size supported by Shim firmware,
  575. * (3) DMA buffer size limit set by boot_param dma_buf_size_limit.
  576. */
  577. payload_max_size = min3(fw->size,
  578. (size_t)shim_fw_buf_size,
  579. (size_t)dma_buf_size_limit);
  580. /*
  581. * Buffer size should be multiple of cacheline size
  582. * if it's not, select the previous cacheline boundary.
  583. */
  584. payload_max_size &= ~(L1_CACHE_BYTES - 1);
  585. dma_buf = dma_alloc_coherent(devc, payload_max_size, &dma_buf_phy, GFP_KERNEL);
  586. if (!dma_buf) {
  587. client_data->flag_retry = true;
  588. return -ENOMEM;
  589. }
  590. ldr_xfer_dma_frag.fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT;
  591. ldr_xfer_dma_frag.fragment.xfer_mode = LOADER_XFER_MODE_DIRECT_DMA;
  592. ldr_xfer_dma_frag.ddr_phys_addr = (u64)dma_buf_phy;
  593. /* Send the firmware image in chucks of payload_max_size */
  594. fragment_offset = 0;
  595. while (fragment_offset < fw->size) {
  596. if (fragment_offset + payload_max_size < fw->size) {
  597. fragment_size = payload_max_size;
  598. ldr_xfer_dma_frag.fragment.is_last = 0;
  599. } else {
  600. fragment_size = fw->size - fragment_offset;
  601. ldr_xfer_dma_frag.fragment.is_last = 1;
  602. }
  603. ldr_xfer_dma_frag.fragment.offset = fragment_offset;
  604. ldr_xfer_dma_frag.fragment.size = fragment_size;
  605. memcpy(dma_buf, &fw->data[fragment_offset], fragment_size);
  606. /* Flush cache to be sure the data is in main memory. */
  607. clflush_cache_range(dma_buf, payload_max_size);
  608. dev_dbg(cl_data_to_dev(client_data),
  609. "xfer_mode=dma offset=0x%08x size=0x%x is_last=%d ddr_phys_addr=0x%016llx\n",
  610. ldr_xfer_dma_frag.fragment.offset,
  611. ldr_xfer_dma_frag.fragment.size,
  612. ldr_xfer_dma_frag.fragment.is_last,
  613. ldr_xfer_dma_frag.ddr_phys_addr);
  614. rv = loader_cl_send(client_data,
  615. (u8 *)&ldr_xfer_dma_frag,
  616. sizeof(ldr_xfer_dma_frag),
  617. (u8 *)&ldr_xfer_dma_frag_ack,
  618. sizeof(ldr_xfer_dma_frag_ack));
  619. if (rv < 0) {
  620. client_data->flag_retry = true;
  621. goto end_err_resp_buf_release;
  622. }
  623. fragment_offset += fragment_size;
  624. }
  625. end_err_resp_buf_release:
  626. dma_free_coherent(devc, payload_max_size, dma_buf, dma_buf_phy);
  627. return rv;
  628. }
  629. /**
  630. * ish_fw_start() - Start executing ISH main firmware
  631. * @client_data: client data instance
  632. *
  633. * This function sends message to Shim firmware loader to start
  634. * the execution of ISH main firmware.
  635. *
  636. * Return: 0 for success, negative error code for failure.
  637. */
  638. static int ish_fw_start(struct ishtp_cl_data *client_data)
  639. {
  640. struct loader_start ldr_start;
  641. struct loader_msg_hdr ldr_start_ack;
  642. memset(&ldr_start, 0, sizeof(ldr_start));
  643. ldr_start.hdr.command = LOADER_CMD_START;
  644. return loader_cl_send(client_data,
  645. (u8 *)&ldr_start,
  646. sizeof(ldr_start),
  647. (u8 *)&ldr_start_ack,
  648. sizeof(ldr_start_ack));
  649. }
  650. /**
  651. * load_fw_from_host() - Loads ISH firmware from host
  652. * @client_data: Client data instance
  653. *
  654. * This function loads the ISH firmware to ISH SRAM and starts execution
  655. *
  656. * Return: 0 for success, negative error code for failure.
  657. */
  658. static int load_fw_from_host(struct ishtp_cl_data *client_data)
  659. {
  660. int rv;
  661. u32 xfer_mode;
  662. char *filename;
  663. const struct firmware *fw;
  664. struct shim_fw_info fw_info;
  665. struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl;
  666. client_data->flag_retry = false;
  667. filename = kzalloc(FILENAME_SIZE, GFP_KERNEL);
  668. if (!filename) {
  669. client_data->flag_retry = true;
  670. rv = -ENOMEM;
  671. goto end_error;
  672. }
  673. /* Get filename of the ISH firmware to be loaded */
  674. rv = get_firmware_variant(client_data, filename);
  675. if (rv < 0)
  676. goto end_err_filename_buf_release;
  677. rv = request_firmware(&fw, filename, cl_data_to_dev(client_data));
  678. if (rv < 0)
  679. goto end_err_filename_buf_release;
  680. /* Step 1: Query Shim firmware loader properties */
  681. rv = ish_query_loader_prop(client_data, fw, &fw_info);
  682. if (rv < 0)
  683. goto end_err_fw_release;
  684. /* Step 2: Send the main firmware image to be loaded, to ISH SRAM */
  685. xfer_mode = fw_info.ldr_capability.xfer_mode;
  686. if (xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) {
  687. rv = ish_fw_xfer_direct_dma(client_data, fw, fw_info);
  688. } else if (xfer_mode & LOADER_XFER_MODE_ISHTP) {
  689. rv = ish_fw_xfer_ishtp(client_data, fw);
  690. } else {
  691. dev_err(cl_data_to_dev(client_data),
  692. "No transfer mode selected in firmware\n");
  693. rv = -EINVAL;
  694. }
  695. if (rv < 0)
  696. goto end_err_fw_release;
  697. /* Step 3: Start ISH main firmware exeuction */
  698. rv = ish_fw_start(client_data);
  699. if (rv < 0)
  700. goto end_err_fw_release;
  701. release_firmware(fw);
  702. dev_info(cl_data_to_dev(client_data), "ISH firmware %s loaded\n",
  703. filename);
  704. kfree(filename);
  705. return 0;
  706. end_err_fw_release:
  707. release_firmware(fw);
  708. end_err_filename_buf_release:
  709. kfree(filename);
  710. end_error:
  711. /* Keep a count of retries, and give up after 3 attempts */
  712. if (client_data->flag_retry &&
  713. client_data->retry_count++ < MAX_LOAD_ATTEMPTS) {
  714. dev_warn(cl_data_to_dev(client_data),
  715. "ISH host firmware load failed %d. Resetting ISH, and trying again..\n",
  716. rv);
  717. ish_hw_reset(ishtp_get_ishtp_device(loader_ishtp_cl));
  718. } else {
  719. dev_err(cl_data_to_dev(client_data),
  720. "ISH host firmware load failed %d\n", rv);
  721. }
  722. return rv;
  723. }
  724. static void load_fw_from_host_handler(struct work_struct *work)
  725. {
  726. struct ishtp_cl_data *client_data;
  727. client_data = container_of(work, struct ishtp_cl_data,
  728. work_fw_load);
  729. load_fw_from_host(client_data);
  730. }
  731. /**
  732. * loader_init() - Init function for ISH-TP client
  733. * @loader_ishtp_cl: ISH-TP client instance
  734. * @reset: true if called for init after reset
  735. *
  736. * Return: 0 for success, negative error code for failure
  737. */
  738. static int loader_init(struct ishtp_cl *loader_ishtp_cl, int reset)
  739. {
  740. int rv;
  741. struct ishtp_fw_client *fw_client;
  742. struct ishtp_cl_data *client_data =
  743. ishtp_get_client_data(loader_ishtp_cl);
  744. dev_dbg(cl_data_to_dev(client_data), "reset flag: %d\n", reset);
  745. rv = ishtp_cl_link(loader_ishtp_cl);
  746. if (rv < 0) {
  747. dev_err(cl_data_to_dev(client_data), "ishtp_cl_link failed\n");
  748. return rv;
  749. }
  750. /* Connect to firmware client */
  751. ishtp_set_tx_ring_size(loader_ishtp_cl, LOADER_CL_TX_RING_SIZE);
  752. ishtp_set_rx_ring_size(loader_ishtp_cl, LOADER_CL_RX_RING_SIZE);
  753. fw_client =
  754. ishtp_fw_cl_get_client(ishtp_get_ishtp_device(loader_ishtp_cl),
  755. &loader_ishtp_id_table[0].guid);
  756. if (!fw_client) {
  757. dev_err(cl_data_to_dev(client_data),
  758. "ISH client uuid not found\n");
  759. rv = -ENOENT;
  760. goto err_cl_unlink;
  761. }
  762. ishtp_cl_set_fw_client_id(loader_ishtp_cl,
  763. ishtp_get_fw_client_id(fw_client));
  764. ishtp_set_connection_state(loader_ishtp_cl, ISHTP_CL_CONNECTING);
  765. rv = ishtp_cl_connect(loader_ishtp_cl);
  766. if (rv < 0) {
  767. dev_err(cl_data_to_dev(client_data), "Client connect fail\n");
  768. goto err_cl_unlink;
  769. }
  770. dev_dbg(cl_data_to_dev(client_data), "Client connected\n");
  771. ishtp_register_event_cb(client_data->cl_device, loader_cl_event_cb);
  772. return 0;
  773. err_cl_unlink:
  774. ishtp_cl_unlink(loader_ishtp_cl);
  775. return rv;
  776. }
  777. static void loader_deinit(struct ishtp_cl *loader_ishtp_cl)
  778. {
  779. ishtp_set_connection_state(loader_ishtp_cl, ISHTP_CL_DISCONNECTING);
  780. ishtp_cl_disconnect(loader_ishtp_cl);
  781. ishtp_cl_unlink(loader_ishtp_cl);
  782. ishtp_cl_flush_queues(loader_ishtp_cl);
  783. /* Disband and free all Tx and Rx client-level rings */
  784. ishtp_cl_free(loader_ishtp_cl);
  785. }
  786. static void reset_handler(struct work_struct *work)
  787. {
  788. int rv;
  789. struct ishtp_cl_data *client_data;
  790. struct ishtp_cl *loader_ishtp_cl;
  791. struct ishtp_cl_device *cl_device;
  792. client_data = container_of(work, struct ishtp_cl_data,
  793. work_ishtp_reset);
  794. loader_ishtp_cl = client_data->loader_ishtp_cl;
  795. cl_device = client_data->cl_device;
  796. /* Unlink, flush queues & start again */
  797. ishtp_cl_unlink(loader_ishtp_cl);
  798. ishtp_cl_flush_queues(loader_ishtp_cl);
  799. ishtp_cl_free(loader_ishtp_cl);
  800. loader_ishtp_cl = ishtp_cl_allocate(cl_device);
  801. if (!loader_ishtp_cl)
  802. return;
  803. ishtp_set_drvdata(cl_device, loader_ishtp_cl);
  804. ishtp_set_client_data(loader_ishtp_cl, client_data);
  805. client_data->loader_ishtp_cl = loader_ishtp_cl;
  806. client_data->cl_device = cl_device;
  807. rv = loader_init(loader_ishtp_cl, 1);
  808. if (rv < 0) {
  809. dev_err(ishtp_device(cl_device), "Reset Failed\n");
  810. return;
  811. }
  812. /* ISH firmware loading from host */
  813. load_fw_from_host(client_data);
  814. }
  815. /**
  816. * loader_ishtp_cl_probe() - ISH-TP client driver probe
  817. * @cl_device: ISH-TP client device instance
  818. *
  819. * This function gets called on device create on ISH-TP bus
  820. *
  821. * Return: 0 for success, negative error code for failure
  822. */
  823. static int loader_ishtp_cl_probe(struct ishtp_cl_device *cl_device)
  824. {
  825. struct ishtp_cl *loader_ishtp_cl;
  826. struct ishtp_cl_data *client_data;
  827. int rv;
  828. client_data = devm_kzalloc(ishtp_device(cl_device),
  829. sizeof(*client_data),
  830. GFP_KERNEL);
  831. if (!client_data)
  832. return -ENOMEM;
  833. loader_ishtp_cl = ishtp_cl_allocate(cl_device);
  834. if (!loader_ishtp_cl)
  835. return -ENOMEM;
  836. ishtp_set_drvdata(cl_device, loader_ishtp_cl);
  837. ishtp_set_client_data(loader_ishtp_cl, client_data);
  838. client_data->loader_ishtp_cl = loader_ishtp_cl;
  839. client_data->cl_device = cl_device;
  840. init_waitqueue_head(&client_data->response.wait_queue);
  841. INIT_WORK(&client_data->work_ishtp_reset,
  842. reset_handler);
  843. INIT_WORK(&client_data->work_fw_load,
  844. load_fw_from_host_handler);
  845. rv = loader_init(loader_ishtp_cl, 0);
  846. if (rv < 0) {
  847. ishtp_cl_free(loader_ishtp_cl);
  848. return rv;
  849. }
  850. ishtp_get_device(cl_device);
  851. client_data->retry_count = 0;
  852. /* ISH firmware loading from host */
  853. schedule_work(&client_data->work_fw_load);
  854. return 0;
  855. }
  856. /**
  857. * loader_ishtp_cl_remove() - ISH-TP client driver remove
  858. * @cl_device: ISH-TP client device instance
  859. *
  860. * This function gets called on device remove on ISH-TP bus
  861. *
  862. * Return: 0
  863. */
  864. static void loader_ishtp_cl_remove(struct ishtp_cl_device *cl_device)
  865. {
  866. struct ishtp_cl_data *client_data;
  867. struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
  868. client_data = ishtp_get_client_data(loader_ishtp_cl);
  869. /*
  870. * The sequence of the following two cancel_work_sync() is
  871. * important. The work_fw_load can in turn schedue
  872. * work_ishtp_reset, so first cancel work_fw_load then
  873. * cancel work_ishtp_reset.
  874. */
  875. cancel_work_sync(&client_data->work_fw_load);
  876. cancel_work_sync(&client_data->work_ishtp_reset);
  877. loader_deinit(loader_ishtp_cl);
  878. ishtp_put_device(cl_device);
  879. }
  880. /**
  881. * loader_ishtp_cl_reset() - ISH-TP client driver reset
  882. * @cl_device: ISH-TP client device instance
  883. *
  884. * This function gets called on device reset on ISH-TP bus
  885. *
  886. * Return: 0
  887. */
  888. static int loader_ishtp_cl_reset(struct ishtp_cl_device *cl_device)
  889. {
  890. struct ishtp_cl_data *client_data;
  891. struct ishtp_cl *loader_ishtp_cl = ishtp_get_drvdata(cl_device);
  892. client_data = ishtp_get_client_data(loader_ishtp_cl);
  893. schedule_work(&client_data->work_ishtp_reset);
  894. return 0;
  895. }
  896. static struct ishtp_cl_driver loader_ishtp_cl_driver = {
  897. .name = "ish-loader",
  898. .id = loader_ishtp_id_table,
  899. .probe = loader_ishtp_cl_probe,
  900. .remove = loader_ishtp_cl_remove,
  901. .reset = loader_ishtp_cl_reset,
  902. };
  903. static int __init ish_loader_init(void)
  904. {
  905. return ishtp_cl_driver_register(&loader_ishtp_cl_driver, THIS_MODULE);
  906. }
  907. static void __exit ish_loader_exit(void)
  908. {
  909. ishtp_cl_driver_unregister(&loader_ishtp_cl_driver);
  910. }
  911. late_initcall(ish_loader_init);
  912. module_exit(ish_loader_exit);
  913. module_param(dma_buf_size_limit, int, 0644);
  914. MODULE_PARM_DESC(dma_buf_size_limit, "Limit the DMA buf size to this value in bytes");
  915. MODULE_DESCRIPTION("ISH ISH-TP Host firmware Loader Client Driver");
  916. MODULE_AUTHOR("Rushikesh S Kadam <[email protected]>");
  917. MODULE_LICENSE("GPL v2");