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.
86 * The firmware loading latency will be minimum if we can DMA the
87 * entire ISH firmware image in one go. This requires that we allocate
96 * struct loader_msg_hdr - Header for ISH Loader commands.
103 * between Host driver and ISH Shim firmware loader.
174 * struct response_info - Encapsulate firmware response related
177 * @data: Copy the data received from firmware here.
181 * @size: Actual size of data received from firmware.
184 * @received: Set to true on receiving a valid firmware
186 * @wait_queue: Wait queue for Host firmware loading where the
187 * client sends message to ISH firmware and waits
200 * struct ishtp_cl_data - Encapsulate per ISH-TP Client Data.
201 * @work_ishtp_reset: Work queue for reset handling.
202 * @work_fw_load: Work queue for host firmware loading.
203 * @flag_retry: Flag for indicating host firmware loading should
214 * Used for passing firmware response information between
223 * In certain failure scenrios, it makes sense to reset the ISH
224 * subsystem and retry Host firmware loading (e.g. bad message
229 * If set, the flag indicates that we should re-try the
239 #define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
242 * get_firmware_variant() - Gets the filename of firmware image to be
245 * @filename: Returns firmware filename.
247 * Queries the firmware-name device property string.
256 struct device *devc = ishtp_get_pci_device(client_data->cl_device); in get_firmware_variant()
258 rv = device_property_read_string(devc, "firmware-name", &val); in get_firmware_variant()
261 "Error: ISH firmware-name device property required\n"); in get_firmware_variant()
268 * loader_cl_send() Send message from host to firmware
270 * @out_msg: Message buffer to be sent to firmware
285 struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl; in loader_cl_send()
290 out_hdr->command & CMD_MASK, in loader_cl_send()
291 out_hdr->command & IS_RESPONSE ? 1 : 0, in loader_cl_send()
292 out_hdr->status); in loader_cl_send()
295 client_data->response.data = in_msg; in loader_cl_send()
296 client_data->response.max_size = in_size; in loader_cl_send()
297 client_data->response.error = 0; in loader_cl_send()
298 client_data->response.received = false; in loader_cl_send()
307 wait_event_interruptible_timeout(client_data->response.wait_queue, in loader_cl_send()
308 client_data->response.received, in loader_cl_send()
310 if (!client_data->response.received) { in loader_cl_send()
313 out_hdr->command & CMD_MASK); in loader_cl_send()
314 return -ETIMEDOUT; in loader_cl_send()
317 if (client_data->response.error < 0) in loader_cl_send()
318 return client_data->response.error; in loader_cl_send()
320 return client_data->response.size; in loader_cl_send()
324 * process_recv() - Receive and parse incoming packet
335 size_t data_len = rb_in_proc->buf_idx; in process_recv()
340 if (!client_data->response.data) { in process_recv()
343 client_data->response.error = -EINVAL; in process_recv()
347 if (client_data->response.received) { in process_recv()
349 "Previous firmware message not yet processed\n"); in process_recv()
350 client_data->response.error = -EINVAL; in process_recv()
354 * All firmware messages have a header. Check buffer size in process_recv()
357 if (!rb_in_proc->buffer.data) { in process_recv()
359 "rb_in_proc->buffer.data returned null"); in process_recv()
360 client_data->response.error = -EBADMSG; in process_recv()
368 client_data->response.error = -EMSGSIZE; in process_recv()
372 hdr = (struct loader_msg_hdr *)rb_in_proc->buffer.data; in process_recv()
377 hdr->command & CMD_MASK, in process_recv()
378 hdr->command & IS_RESPONSE ? 1 : 0, in process_recv()
379 hdr->status); in process_recv()
381 if (((hdr->command & CMD_MASK) != LOADER_CMD_XFER_QUERY) && in process_recv()
382 ((hdr->command & CMD_MASK) != LOADER_CMD_XFER_FRAGMENT) && in process_recv()
383 ((hdr->command & CMD_MASK) != LOADER_CMD_START)) { in process_recv()
386 hdr->command & CMD_MASK); in process_recv()
387 client_data->response.error = -EPROTO; in process_recv()
391 if (data_len > client_data->response.max_size) { in process_recv()
394 data_len, client_data->response.max_size); in process_recv()
395 client_data->response.error = -EMSGSIZE; in process_recv()
399 /* We expect only "response" messages from firmware */ in process_recv()
400 if (!(hdr->command & IS_RESPONSE)) { in process_recv()
403 client_data->response.error = -EIO; in process_recv()
407 if (hdr->status) { in process_recv()
410 hdr->status); in process_recv()
411 client_data->response.error = -EIO; in process_recv()
416 client_data->response.size = data_len; in process_recv()
419 * Copy the buffer received in firmware response for the in process_recv()
422 memcpy(client_data->response.data, in process_recv()
423 rb_in_proc->buffer.data, data_len); in process_recv()
426 client_data->response.received = true; in process_recv()
434 wake_up_interruptible(&client_data->response.wait_queue); in process_recv()
438 * loader_cl_event_cb() - bus driver callback for incoming message
451 /* Process the data packet from firmware */ in loader_cl_event_cb()
457 * ish_query_loader_prop() - Query ISH Shim firmware loader
459 * @fw: Pointer to firmware data struct in host memory
460 * @fw_info: Loader firmware properties
462 * This function queries the ISH Shim firmware loader for capabilities.
467 const struct firmware *fw, in ish_query_loader_prop()
476 ldr_xfer_query.image_size = fw->size; in ish_query_loader_prop()
483 client_data->flag_retry = true; in ish_query_loader_prop()
493 client_data->flag_retry = true; in ish_query_loader_prop()
495 return -EMSGSIZE; in ish_query_loader_prop()
501 /* Loader firmware properties */ in ish_query_loader_prop()
504 fw_info->ish_fw_version.major, in ish_query_loader_prop()
505 fw_info->ish_fw_version.minor, in ish_query_loader_prop()
506 fw_info->ish_fw_version.hotfix, in ish_query_loader_prop()
507 fw_info->ish_fw_version.build, in ish_query_loader_prop()
508 fw_info->protocol_version, in ish_query_loader_prop()
509 fw_info->ldr_version.value); in ish_query_loader_prop()
513 fw_info->ldr_capability.max_fw_image_size, in ish_query_loader_prop()
514 fw_info->ldr_capability.xfer_mode, in ish_query_loader_prop()
515 fw_info->ldr_capability.max_dma_buf_size, in ish_query_loader_prop()
519 if (fw_info->ldr_capability.max_fw_image_size < fw->size) { in ish_query_loader_prop()
521 "ISH firmware size %zu is greater than Shim firmware loader max supported %d\n", in ish_query_loader_prop()
522 fw->size, in ish_query_loader_prop()
523 fw_info->ldr_capability.max_fw_image_size); in ish_query_loader_prop()
524 return -ENOSPC; in ish_query_loader_prop()
528 if ((fw_info->ldr_capability.xfer_mode & LOADER_XFER_MODE_DIRECT_DMA) && in ish_query_loader_prop()
529 (fw_info->ldr_capability.max_dma_buf_size % L1_CACHE_BYTES)) { in ish_query_loader_prop()
531 "Shim firmware loader buffer size %d should be multiple of cacheline\n", in ish_query_loader_prop()
532 fw_info->ldr_capability.max_dma_buf_size); in ish_query_loader_prop()
533 return -EINVAL; in ish_query_loader_prop()
540 * ish_fw_xfer_ishtp() - Loads ISH firmware using ishtp interface
542 * @fw: Pointer to firmware data struct in host memory
544 * This function uses ISH-TP to transfer ISH firmware from host to
545 * ISH SRAM. Lower layers may use IPC or DMA depending on firmware
551 const struct firmware *fw) in ish_fw_xfer_ishtp()
559 LOADER_SHIM_IPC_BUF_SIZE - IPC_FRAGMENT_DATA_PREAMBLE; in ish_fw_xfer_ishtp()
563 client_data->flag_retry = true; in ish_fw_xfer_ishtp()
564 return -ENOMEM; in ish_fw_xfer_ishtp()
567 ldr_xfer_ipc_frag->fragment.hdr.command = LOADER_CMD_XFER_FRAGMENT; in ish_fw_xfer_ishtp()
568 ldr_xfer_ipc_frag->fragment.xfer_mode = LOADER_XFER_MODE_ISHTP; in ish_fw_xfer_ishtp()
570 /* Break the firmware image into fragments and send as ISH-TP payload */ in ish_fw_xfer_ishtp()
572 while (fragment_offset < fw->size) { in ish_fw_xfer_ishtp()
573 if (fragment_offset + payload_max_size < fw->size) { in ish_fw_xfer_ishtp()
575 ldr_xfer_ipc_frag->fragment.is_last = 0; in ish_fw_xfer_ishtp()
577 fragment_size = fw->size - fragment_offset; in ish_fw_xfer_ishtp()
578 ldr_xfer_ipc_frag->fragment.is_last = 1; in ish_fw_xfer_ishtp()
581 ldr_xfer_ipc_frag->fragment.offset = fragment_offset; in ish_fw_xfer_ishtp()
582 ldr_xfer_ipc_frag->fragment.size = fragment_size; in ish_fw_xfer_ishtp()
583 memcpy(ldr_xfer_ipc_frag->data, in ish_fw_xfer_ishtp()
584 &fw->data[fragment_offset], in ish_fw_xfer_ishtp()
589 ldr_xfer_ipc_frag->fragment.offset, in ish_fw_xfer_ishtp()
590 ldr_xfer_ipc_frag->fragment.size, in ish_fw_xfer_ishtp()
591 ldr_xfer_ipc_frag->fragment.is_last); in ish_fw_xfer_ishtp()
599 client_data->flag_retry = true; in ish_fw_xfer_ishtp()
616 * ish_fw_xfer_direct_dma() - Loads ISH firmware using direct dma
618 * @fw: Pointer to firmware data struct in host memory
619 * @fw_info: Loader firmware properties
621 * Host firmware load is a unique case where we need to download
622 * a large firmware image (200+ Kb). This function implements
623 * direct DMA transfer in kernel and ISH firmware. This allows
624 * us to overcome the ISH-TP 4 Kb limit, and allows us to DMA
626 * Function depends on corresponding support in ISH firmware.
631 const struct firmware *fw, in ish_fw_xfer_direct_dma()
640 struct device *devc = ishtp_get_pci_device(client_data->cl_device); in ish_fw_xfer_direct_dma()
646 * (1) Size of firmware to be loaded, in ish_fw_xfer_direct_dma()
647 * (2) Max DMA buffer size supported by Shim firmware, in ish_fw_xfer_direct_dma()
650 payload_max_size = min3(fw->size, in ish_fw_xfer_direct_dma()
658 payload_max_size &= ~(L1_CACHE_BYTES - 1); in ish_fw_xfer_direct_dma()
662 client_data->flag_retry = true; in ish_fw_xfer_direct_dma()
663 return -ENOMEM; in ish_fw_xfer_direct_dma()
670 client_data->flag_retry = true; in ish_fw_xfer_direct_dma()
671 rv = -ENOMEM; in ish_fw_xfer_direct_dma()
679 /* Send the firmware image in chucks of payload_max_size */ in ish_fw_xfer_direct_dma()
681 while (fragment_offset < fw->size) { in ish_fw_xfer_direct_dma()
682 if (fragment_offset + payload_max_size < fw->size) { in ish_fw_xfer_direct_dma()
686 fragment_size = fw->size - fragment_offset; in ish_fw_xfer_direct_dma()
692 memcpy(dma_buf, &fw->data[fragment_offset], fragment_size); in ish_fw_xfer_direct_dma()
717 client_data->flag_retry = true; in ish_fw_xfer_direct_dma()
737 * ish_fw_start() - Start executing ISH main firmware
740 * This function sends message to Shim firmware loader to start
741 * the execution of ISH main firmware.
760 * load_fw_from_host() - Loads ISH firmware from host
763 * This function loads the ISH firmware to ISH SRAM and starts execution
772 const struct firmware *fw; in load_fw_from_host()
774 struct ishtp_cl *loader_ishtp_cl = client_data->loader_ishtp_cl; in load_fw_from_host()
776 client_data->flag_retry = false; in load_fw_from_host()
780 client_data->flag_retry = true; in load_fw_from_host()
781 rv = -ENOMEM; in load_fw_from_host()
785 /* Get filename of the ISH firmware to be loaded */ in load_fw_from_host()
794 /* Step 1: Query Shim firmware loader properties */ in load_fw_from_host()
800 /* Step 2: Send the main firmware image to be loaded, to ISH SRAM */ in load_fw_from_host()
809 "No transfer mode selected in firmware\n"); in load_fw_from_host()
810 rv = -EINVAL; in load_fw_from_host()
815 /* Step 3: Start ISH main firmware exeuction */ in load_fw_from_host()
822 dev_info(cl_data_to_dev(client_data), "ISH firmware %s loaded\n", in load_fw_from_host()
833 if (client_data->flag_retry && in load_fw_from_host()
834 client_data->retry_count++ < MAX_LOAD_ATTEMPTS) { in load_fw_from_host()
836 "ISH host firmware load failed %d. Resetting ISH, and trying again..\n", in load_fw_from_host()
841 "ISH host firmware load failed %d\n", rv); in load_fw_from_host()
856 * loader_init() - Init function for ISH-TP client
857 * @loader_ishtp_cl: ISH-TP client instance
858 * @reset: true if called for init after reset
862 static int loader_init(struct ishtp_cl *loader_ishtp_cl, int reset) in loader_init() argument
869 dev_dbg(cl_data_to_dev(client_data), "reset flag: %d\n", reset); in loader_init()
877 /* Connect to firmware client */ in loader_init()
887 rv = -ENOENT; in loader_init()
903 ishtp_register_event_cb(client_data->cl_device, loader_cl_event_cb); in loader_init()
919 /* Disband and free all Tx and Rx client-level rings */ in loader_deinit()
933 loader_ishtp_cl = client_data->loader_ishtp_cl; in reset_handler()
934 cl_device = client_data->cl_device; in reset_handler()
947 client_data->loader_ishtp_cl = loader_ishtp_cl; in reset_handler()
948 client_data->cl_device = cl_device; in reset_handler()
952 dev_err(ishtp_device(cl_device), "Reset Failed\n"); in reset_handler()
956 /* ISH firmware loading from host */ in reset_handler()
961 * loader_ishtp_cl_probe() - ISH-TP client driver probe
962 * @cl_device: ISH-TP client device instance
964 * This function gets called on device create on ISH-TP bus
978 return -ENOMEM; in loader_ishtp_cl_probe()
982 return -ENOMEM; in loader_ishtp_cl_probe()
986 client_data->loader_ishtp_cl = loader_ishtp_cl; in loader_ishtp_cl_probe()
987 client_data->cl_device = cl_device; in loader_ishtp_cl_probe()
989 init_waitqueue_head(&client_data->response.wait_queue); in loader_ishtp_cl_probe()
991 INIT_WORK(&client_data->work_ishtp_reset, in loader_ishtp_cl_probe()
993 INIT_WORK(&client_data->work_fw_load, in loader_ishtp_cl_probe()
1003 client_data->retry_count = 0; in loader_ishtp_cl_probe()
1005 /* ISH firmware loading from host */ in loader_ishtp_cl_probe()
1006 schedule_work(&client_data->work_fw_load); in loader_ishtp_cl_probe()
1012 * loader_ishtp_cl_remove() - ISH-TP client driver remove
1013 * @cl_device: ISH-TP client device instance
1015 * This function gets called on device remove on ISH-TP bus
1032 cancel_work_sync(&client_data->work_fw_load); in loader_ishtp_cl_remove()
1033 cancel_work_sync(&client_data->work_ishtp_reset); in loader_ishtp_cl_remove()
1039 * loader_ishtp_cl_reset() - ISH-TP client driver reset
1040 * @cl_device: ISH-TP client device instance
1042 * This function gets called on device reset on ISH-TP bus
1053 schedule_work(&client_data->work_ishtp_reset); in loader_ishtp_cl_reset()
1059 .name = "ish-loader",
1063 .reset = loader_ishtp_cl_reset,
1082 MODULE_DESCRIPTION("ISH ISH-TP Host firmware Loader Client Driver");