Lines Matching +full:firmware +full:- +full:reset
1 // SPDX-License-Identifier: GPL-2.0
3 * ISH-TP client driver for ISH firmware loading
8 #include <linux/firmware.h>
11 #include <linux/intel-ish-client-if.h>
15 /* Number of times we attempt to load the firmware before giving up */
23 * ISH Shim firmware loader reserves 4 Kb buffer in SRAM. The buffer is
25 * firmware loader. Reason for the odd size of 3968 bytes? Each IPC
33 * enum ish_loader_commands - ISH loader host commands.
34 * @LOADER_CMD_XFER_QUERY: Query the Shim firmware loader for
36 * @LOADER_CMD_XFER_FRAGMENT: Transfer one firmware image fragment at a
38 * multiple times until the entire firmware
40 * @LOADER_CMD_START: Start executing the main firmware.
53 * ISH firmware max delay for one transmit failure is 1 Hz,
54 * and firmware will retry 2 times, so 3 Hz is used for timeout.
61 * LOADER_XFER_MODE_ISHTP mode uses the existing ISH-TP mechanism to
62 * transfer data. This may use IPC or DMA if supported in firmware.
63 * The buffer size is limited to 4 Kb by the IPC/ISH-TP protocol for
66 * LOADER_XFER_MODE_DIRECT_DMA - firmware loading is a bit different
70 * this "direct dma" mode, where we do not use ISH-TP for DMA, but
71 * instead manage the DMA directly in kernel driver and Shim firmware
73 * to overcome 4 Kb limit, and optimize the data flow path in firmware.
89 * The firmware loading latency will be minimum if we can DMA the
90 * entire ISH firmware image in one go. This requires that we allocate
99 * struct loader_msg_hdr - Header for ISH Loader commands.
106 * between Host driver and ISH Shim firmware loader.
177 * struct response_info - Encapsulate firmware response related
180 * @data: Copy the data received from firmware here.
184 * @size: Actual size of data received from firmware.
187 * @received: Set to true on receiving a valid firmware
189 * @wait_queue: Wait queue for Host firmware loading where the
190 * client sends message to ISH firmware and waits
203 * struct ishtp_cl_data - Encapsulate per ISH-TP Client Data.
204 * @work_ishtp_reset: Work queue for reset handling.
205 * @work_fw_load: Work queue for host firmware loading.
206 * @flag_retry: Flag for indicating host firmware loading should
217 * Used for passing firmware response information between
226 * In certain failure scenrios, it makes sense to reset the ISH
227 * subsystem and retry Host firmware loading (e.g. bad message
232 * If set, the flag indicates that we should re-try the
242 #define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
245 * get_firmware_variant() - Gets the filename of firmware image to be
248 * @filename: Returns firmware filename.
250 * Queries the firmware-name device property string.
259 struct device *devc = ishtp_get_pci_device(client_data->cl_device); in get_firmware_variant()
261 rv = device_property_read_string(devc, "firmware-name", &val); in get_firmware_variant()
264 "Error: ISH firmware-name device property required\n"); in get_firmware_variant()
271 * loader_cl_send() - Send message from host to firmware
274 * @out_msg: Message buffer to be sent to firmware
289 struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl; in loader_cl_send()
294 out_hdr->command & CMD_MASK, in loader_cl_send()
295 out_hdr->command & IS_RESPONSE ? 1 : 0, in loader_cl_send()
296 out_hdr->status); in loader_cl_send()
299 client_data->response.data = in_msg; in loader_cl_send()
300 client_data->response.max_size = in_size; in loader_cl_send()
301 client_data->response.error = 0; in loader_cl_send()
302 client_data->response.received = false; in loader_cl_send()
311 wait_event_interruptible_timeout(client_data->response.wait_queue, in loader_cl_send()
312 client_data->response.received, in loader_cl_send()
314 if (!client_data->response.received) { in loader_cl_send()
317 out_hdr->command & CMD_MASK); in loader_cl_send()
318 return -ETIMEDOUT; in loader_cl_send()
321 if (client_data->response.error < 0) in loader_cl_send()
322 return client_data->response.error; in loader_cl_send()
324 return client_data->response.size; in loader_cl_send()
328 * process_recv() - Receive and parse incoming packet
339 size_t data_len = rb_in_proc->buf_idx; in process_recv()
344 if (!client_data->response.data) { in process_recv()
347 client_data->response.error = -EINVAL; in process_recv()
351 if (client_data->response.received) { in process_recv()
353 "Previous firmware message not yet processed\n"); in process_recv()
354 client_data->response.error = -EINVAL; in process_recv()
358 * All firmware messages have a header. Check buffer size in process_recv()
361 if (!rb_in_proc->buffer.data) { in process_recv()
363 "rb_in_proc->buffer.data returned null"); in process_recv()
364 client_data->response.error = -EBADMSG; in process_recv()
372 client_data->response.error = -EMSGSIZE; in process_recv()
376 hdr = (struct loader_msg_hdr *)rb_in_proc->buffer.data; in process_recv()
381 hdr->command & CMD_MASK, in process_recv()
382 hdr->command & IS_RESPONSE ? 1 : 0, in process_recv()
383 hdr->status); in process_recv()
385 if (((hdr->command & CMD_MASK) != LOADER_CMD_XFER_QUERY) && in process_recv()
386 ((hdr->command & CMD_MASK) != LOADER_CMD_XFER_FRAGMENT) && in process_recv()
387 ((hdr->command & CMD_MASK) != LOADER_CMD_START)) { in process_recv()
390 hdr->command & CMD_MASK); in process_recv()
391 client_data->response.error = -EPROTO; in process_recv()
395 if (data_len > client_data->response.max_size) { in process_recv()
398 data_len, client_data->response.max_size); in process_recv()
399 client_data->response.error = -EMSGSIZE; in process_recv()
403 /* We expect only "response" messages from firmware */ in process_recv()
404 if (!(hdr->command & IS_RESPONSE)) { in process_recv()
407 client_data->response.error = -EIO; in process_recv()
411 if (hdr->status) { in process_recv()
414 hdr->status); in process_recv()
415 client_data->response.error = -EIO; in process_recv()
420 client_data->response.size = data_len; in process_recv()
423 * Copy the buffer received in firmware response for the in process_recv()
426 memcpy(client_data->response.data, in process_recv()
427 rb_in_proc->buffer.data, data_len); in process_recv()
430 client_data->response.received = true; in process_recv()
438 wake_up_interruptible(&client_data->response.wait_queue); in process_recv()
442 * loader_cl_event_cb() - bus driver callback for incoming message
455 /* Process the data packet from firmware */ in loader_cl_event_cb()
461 * ish_query_loader_prop() - Query ISH Shim firmware loader
463 * @fw: Pointer to firmware data struct in host memory
464 * @fw_info: Loader firmware properties
466 * This function queries the ISH Shim firmware loader for capabilities.
471 const struct firmware *fw, in ish_query_loader_prop()
480 ldr_xfer_query.image_size = fw->size; in ish_query_loader_prop()
487 client_data->flag_retry = true; in ish_query_loader_prop()
497 client_data->flag_retry = true; in ish_query_loader_prop()
499 return -EMSGSIZE; in ish_query_loader_prop()
505 /* Loader firmware properties */ in ish_query_loader_prop()
508 fw_info->ish_fw_version.major, in ish_query_loader_prop()
509 fw_info->ish_fw_version.minor, in ish_query_loader_prop()
510 fw_info->ish_fw_version.hotfix, in ish_query_loader_prop()
511 fw_info->ish_fw_version.build, in ish_query_loader_prop()
512 fw_info->protocol_version, in ish_query_loader_prop()
513 fw_info->ldr_version.value); in ish_query_loader_prop()
517 fw_info->ldr_capability.max_fw_image_size, in ish_query_loader_prop()
518 fw_info->ldr_capability.xfer_mode, in ish_query_loader_prop()
519 fw_info->ldr_capability.max_dma_buf_size, in ish_query_loader_prop()
523 if (fw_info->ldr_capability.max_fw_image_size < fw->size) { in ish_query_loader_prop()
525 "ISH firmware size %zu is greater than Shim firmware loader max supported %d\n", in ish_query_loader_prop()
526 fw->size, in ish_query_loader_prop()
527 fw_info->ldr_capability.max_fw_image_size); in ish_query_loader_prop()
528 return -ENOSPC; in ish_query_loader_prop()
532 if ((fw_info->ldr_capability.xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) && in ish_query_loader_prop()
533 (fw_info->ldr_capability.max_dma_buf_size % L1_CACHE_BYTES)) { in ish_query_loader_prop()
535 "Shim firmware loader buffer size %d should be multiple of cacheline\n", in ish_query_loader_prop()
536 fw_info->ldr_capability.max_dma_buf_size); in ish_query_loader_prop()
537 return -EINVAL; in ish_query_loader_prop()
544 * ish_fw_xfer_ishtp() - Loads ISH firmware using ishtp interface
546 * @fw: Pointer to firmware data struct in host memory
548 * This function uses ISH-TP to transfer ISH firmware from host to
549 * ISH SRAM. Lower layers may use IPC or DMA depending on firmware
555 const struct firmware *fw) in ish_fw_xfer_ishtp()
563 LOADER_SHIM_IPC_BUF_SIZE - IPC_FRAGMENT_DATA_PREAMBLE; in ish_fw_xfer_ishtp()
567 client_data->flag_retry = true; in ish_fw_xfer_ishtp()
568 return -ENOMEM; in ish_fw_xfer_ishtp()
571 ldr_xfer_ipc_frag->fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT; in ish_fw_xfer_ishtp()
572 ldr_xfer_ipc_frag->fragment.xfer_mode = LOADER_XFER_MODE_ISHTP; in ish_fw_xfer_ishtp()
574 /* Break the firmware image into fragments and send as ISH-TP payload */ in ish_fw_xfer_ishtp()
576 while (fragment_offset < fw->size) { in ish_fw_xfer_ishtp()
577 if (fragment_offset + payload_max_size < fw->size) { in ish_fw_xfer_ishtp()
579 ldr_xfer_ipc_frag->fragment.is_last = 0; in ish_fw_xfer_ishtp()
581 fragment_size = fw->size - fragment_offset; in ish_fw_xfer_ishtp()
582 ldr_xfer_ipc_frag->fragment.is_last = 1; in ish_fw_xfer_ishtp()
585 ldr_xfer_ipc_frag->fragment.offset = fragment_offset; in ish_fw_xfer_ishtp()
586 ldr_xfer_ipc_frag->fragment.size = fragment_size; in ish_fw_xfer_ishtp()
587 memcpy(ldr_xfer_ipc_frag->data, in ish_fw_xfer_ishtp()
588 &fw->data[fragment_offset], in ish_fw_xfer_ishtp()
593 ldr_xfer_ipc_frag->fragment.offset, in ish_fw_xfer_ishtp()
594 ldr_xfer_ipc_frag->fragment.size, in ish_fw_xfer_ishtp()
595 ldr_xfer_ipc_frag->fragment.is_last); in ish_fw_xfer_ishtp()
603 client_data->flag_retry = true; in ish_fw_xfer_ishtp()
620 * ish_fw_xfer_direct_dma() - Loads ISH firmware using direct dma
622 * @fw: Pointer to firmware data struct in host memory
623 * @fw_info: Loader firmware properties
625 * Host firmware load is a unique case where we need to download
626 * a large firmware image (200+ Kb). This function implements
627 * direct DMA transfer in kernel and ISH firmware. This allows
628 * us to overcome the ISH-TP 4 Kb limit, and allows us to DMA
630 * Function depends on corresponding support in ISH firmware.
635 const struct firmware *fw, in ish_fw_xfer_direct_dma()
644 struct device *devc = ishtp_get_pci_device(client_data->cl_device); in ish_fw_xfer_direct_dma()
650 * (1) Size of firmware to be loaded, in ish_fw_xfer_direct_dma()
651 * (2) Max DMA buffer size supported by Shim firmware, in ish_fw_xfer_direct_dma()
654 payload_max_size = min3(fw->size, in ish_fw_xfer_direct_dma()
662 payload_max_size &= ~(L1_CACHE_BYTES - 1); in ish_fw_xfer_direct_dma()
666 client_data->flag_retry = true; in ish_fw_xfer_direct_dma()
667 return -ENOMEM; in ish_fw_xfer_direct_dma()
674 /* Send the firmware image in chucks of payload_max_size */ in ish_fw_xfer_direct_dma()
676 while (fragment_offset < fw->size) { in ish_fw_xfer_direct_dma()
677 if (fragment_offset + payload_max_size < fw->size) { in ish_fw_xfer_direct_dma()
681 fragment_size = fw->size - fragment_offset; in ish_fw_xfer_direct_dma()
687 memcpy(dma_buf, &fw->data[fragment_offset], fragment_size); in ish_fw_xfer_direct_dma()
705 client_data->flag_retry = true; in ish_fw_xfer_direct_dma()
718 * ish_fw_start() - Start executing ISH main firmware
721 * This function sends message to Shim firmware loader to start
722 * the execution of ISH main firmware.
741 * load_fw_from_host() - Loads ISH firmware from host
744 * This function loads the ISH firmware to ISH SRAM and starts execution
753 const struct firmware *fw; in load_fw_from_host()
755 struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl; in load_fw_from_host()
757 client_data->flag_retry = false; in load_fw_from_host()
761 client_data->flag_retry = true; in load_fw_from_host()
762 rv = -ENOMEM; in load_fw_from_host()
766 /* Get filename of the ISH firmware to be loaded */ in load_fw_from_host()
775 /* Step 1: Query Shim firmware loader properties */ in load_fw_from_host()
781 /* Step 2: Send the main firmware image to be loaded, to ISH SRAM */ in load_fw_from_host()
790 "No transfer mode selected in firmware\n"); in load_fw_from_host()
791 rv = -EINVAL; in load_fw_from_host()
796 /* Step 3: Start ISH main firmware exeuction */ in load_fw_from_host()
803 dev_info(cl_data_to_dev(client_data), "ISH firmware %s loaded\n", in load_fw_from_host()
814 if (client_data->flag_retry && in load_fw_from_host()
815 client_data->retry_count++ < MAX_LOAD_ATTEMPTS) { in load_fw_from_host()
817 "ISH host firmware load failed %d. Resetting ISH, and trying again..\n", in load_fw_from_host()
822 "ISH host firmware load failed %d\n", rv); in load_fw_from_host()
837 * loader_init() - Init function for ISH-TP client
838 * @loader_ishtp_cl: ISH-TP client instance
839 * @reset: true if called for init after reset
843 static int loader_init(struct ishtp_cl *loader_ishtp_cl, bool reset) in loader_init() argument
849 dev_dbg(cl_data_to_dev(client_data), "reset flag: %d\n", reset); in loader_init()
855 reset); in loader_init()
863 ishtp_register_event_cb(client_data->cl_device, loader_cl_event_cb); in loader_init()
868 ishtp_cl_destroy_connection(loader_ishtp_cl, reset); in loader_init()
876 /* Disband and free all Tx and Rx client-level rings */ in loader_deinit()
890 loader_ishtp_cl = client_data->loader_ishtp_cl; in reset_handler()
891 cl_device = client_data->cl_device; in reset_handler()
897 dev_err(ishtp_device(cl_device), "Reset Failed\n"); in reset_handler()
901 /* ISH firmware loading from host */ in reset_handler()
906 * loader_ishtp_cl_probe() - ISH-TP client driver probe
907 * @cl_device: ISH-TP client device instance
909 * This function gets called on device create on ISH-TP bus
923 return -ENOMEM; in loader_ishtp_cl_probe()
927 return -ENOMEM; in loader_ishtp_cl_probe()
931 client_data->loader_ishtp_cl = loader_ishtp_cl; in loader_ishtp_cl_probe()
932 client_data->cl_device = cl_device; in loader_ishtp_cl_probe()
934 init_waitqueue_head(&client_data->response.wait_queue); in loader_ishtp_cl_probe()
936 INIT_WORK(&client_data->work_ishtp_reset, in loader_ishtp_cl_probe()
938 INIT_WORK(&client_data->work_fw_load, in loader_ishtp_cl_probe()
948 client_data->retry_count = 0; in loader_ishtp_cl_probe()
950 /* ISH firmware loading from host */ in loader_ishtp_cl_probe()
951 schedule_work(&client_data->work_fw_load); in loader_ishtp_cl_probe()
957 * loader_ishtp_cl_remove() - ISH-TP client driver remove
958 * @cl_device: ISH-TP client device instance
960 * This function gets called on device remove on ISH-TP bus
977 cancel_work_sync(&client_data->work_fw_load); in loader_ishtp_cl_remove()
978 cancel_work_sync(&client_data->work_ishtp_reset); in loader_ishtp_cl_remove()
984 * loader_ishtp_cl_reset() - ISH-TP client driver reset
985 * @cl_device: ISH-TP client device instance
987 * This function gets called on device reset on ISH-TP bus
998 schedule_work(&client_data->work_ishtp_reset); in loader_ishtp_cl_reset()
1004 .name = "ish-loader",
1008 .reset = loader_ishtp_cl_reset,
1027 MODULE_DESCRIPTION("ISH ISH-TP Host firmware Loader Client Driver");