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