1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * H/W layer of ISHTP provider device (ISH) 4 * 5 * Copyright (c) 2014-2016, Intel Corporation. 6 */ 7 8 #include <linux/devm-helpers.h> 9 #include <linux/sched.h> 10 #include <linux/spinlock.h> 11 #include <linux/delay.h> 12 #include <linux/jiffies.h> 13 #include "client.h" 14 #include "hw-ish.h" 15 #include "hbm.h" 16 17 /* For FW reset flow */ 18 static struct work_struct fw_reset_work; 19 static struct ishtp_device *ishtp_dev; 20 21 /** 22 * ish_reg_read() - Read register 23 * @dev: ISHTP device pointer 24 * @offset: Register offset 25 * 26 * Read 32 bit register at a given offset 27 * 28 * Return: Read register value 29 */ 30 static inline uint32_t ish_reg_read(const struct ishtp_device *dev, 31 unsigned long offset) 32 { 33 struct ish_hw *hw = to_ish_hw(dev); 34 35 return readl(hw->mem_addr + offset); 36 } 37 38 /** 39 * ish_reg_write() - Write register 40 * @dev: ISHTP device pointer 41 * @offset: Register offset 42 * @value: Value to write 43 * 44 * Writes 32 bit register at a give offset 45 */ 46 static inline void ish_reg_write(struct ishtp_device *dev, 47 unsigned long offset, 48 uint32_t value) 49 { 50 struct ish_hw *hw = to_ish_hw(dev); 51 52 writel(value, hw->mem_addr + offset); 53 } 54 55 /** 56 * _ish_read_fw_sts_reg() - Read FW status register 57 * @dev: ISHTP device pointer 58 * 59 * Read FW status register 60 * 61 * Return: Read register value 62 */ 63 static inline uint32_t _ish_read_fw_sts_reg(struct ishtp_device *dev) 64 { 65 return ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS); 66 } 67 68 /** 69 * check_generated_interrupt() - Check if ISH interrupt 70 * @dev: ISHTP device pointer 71 * 72 * Check if an interrupt was generated for ISH 73 * 74 * Return: Read true or false 75 */ 76 static bool check_generated_interrupt(struct ishtp_device *dev) 77 { 78 bool interrupt_generated = true; 79 uint32_t pisr_val = 0; 80 81 if (dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_CHV) { 82 pisr_val = ish_reg_read(dev, IPC_REG_PISR_CHV_AB); 83 interrupt_generated = 84 IPC_INT_FROM_ISH_TO_HOST_CHV_AB(pisr_val); 85 } else { 86 pisr_val = ish_reg_read(dev, IPC_REG_PISR_BXT); 87 interrupt_generated = !!pisr_val; 88 /* only busy-clear bit is RW, others are RO */ 89 if (pisr_val) 90 ish_reg_write(dev, IPC_REG_PISR_BXT, pisr_val); 91 } 92 93 return interrupt_generated; 94 } 95 96 /** 97 * ish_is_input_ready() - Check if FW ready for RX 98 * @dev: ISHTP device pointer 99 * 100 * Check if ISH FW is ready for receiving data 101 * 102 * Return: Read true or false 103 */ 104 static bool ish_is_input_ready(struct ishtp_device *dev) 105 { 106 uint32_t doorbell_val; 107 108 doorbell_val = ish_reg_read(dev, IPC_REG_HOST2ISH_DRBL); 109 return !IPC_IS_BUSY(doorbell_val); 110 } 111 112 /** 113 * set_host_ready() - Indicate host ready 114 * @dev: ISHTP device pointer 115 * 116 * Set host ready indication to FW 117 */ 118 static void set_host_ready(struct ishtp_device *dev) 119 { 120 if (dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_CHV) { 121 if (dev->pdev->revision == REVISION_ID_CHT_A0 || 122 (dev->pdev->revision & REVISION_ID_SI_MASK) == 123 REVISION_ID_CHT_Ax_SI) 124 ish_reg_write(dev, IPC_REG_HOST_COMM, 0x81); 125 else if (dev->pdev->revision == REVISION_ID_CHT_B0 || 126 (dev->pdev->revision & REVISION_ID_SI_MASK) == 127 REVISION_ID_CHT_Bx_SI || 128 (dev->pdev->revision & REVISION_ID_SI_MASK) == 129 REVISION_ID_CHT_Kx_SI || 130 (dev->pdev->revision & REVISION_ID_SI_MASK) == 131 REVISION_ID_CHT_Dx_SI) { 132 uint32_t host_comm_val; 133 134 host_comm_val = ish_reg_read(dev, IPC_REG_HOST_COMM); 135 host_comm_val |= IPC_HOSTCOMM_INT_EN_BIT_CHV_AB | 0x81; 136 ish_reg_write(dev, IPC_REG_HOST_COMM, host_comm_val); 137 } 138 } else { 139 uint32_t host_pimr_val; 140 141 host_pimr_val = ish_reg_read(dev, IPC_REG_PIMR_BXT); 142 host_pimr_val |= IPC_PIMR_INT_EN_BIT_BXT; 143 /* 144 * disable interrupt generated instead of 145 * RX_complete_msg 146 */ 147 host_pimr_val &= ~IPC_HOST2ISH_BUSYCLEAR_MASK_BIT; 148 149 ish_reg_write(dev, IPC_REG_PIMR_BXT, host_pimr_val); 150 } 151 } 152 153 /** 154 * ishtp_fw_is_ready() - Check if FW ready 155 * @dev: ISHTP device pointer 156 * 157 * Check if ISH FW is ready 158 * 159 * Return: Read true or false 160 */ 161 static bool ishtp_fw_is_ready(struct ishtp_device *dev) 162 { 163 uint32_t ish_status = _ish_read_fw_sts_reg(dev); 164 165 return IPC_IS_ISH_ILUP(ish_status) && 166 IPC_IS_ISH_ISHTP_READY(ish_status); 167 } 168 169 /** 170 * ish_set_host_rdy() - Indicate host ready 171 * @dev: ISHTP device pointer 172 * 173 * Set host ready indication to FW 174 */ 175 static void ish_set_host_rdy(struct ishtp_device *dev) 176 { 177 uint32_t host_status = ish_reg_read(dev, IPC_REG_HOST_COMM); 178 179 IPC_SET_HOST_READY(host_status); 180 ish_reg_write(dev, IPC_REG_HOST_COMM, host_status); 181 } 182 183 /** 184 * ish_clr_host_rdy() - Indicate host not ready 185 * @dev: ISHTP device pointer 186 * 187 * Send host not ready indication to FW 188 */ 189 static void ish_clr_host_rdy(struct ishtp_device *dev) 190 { 191 uint32_t host_status = ish_reg_read(dev, IPC_REG_HOST_COMM); 192 193 IPC_CLEAR_HOST_READY(host_status); 194 ish_reg_write(dev, IPC_REG_HOST_COMM, host_status); 195 } 196 197 static bool ish_chk_host_rdy(struct ishtp_device *dev) 198 { 199 uint32_t host_status = ish_reg_read(dev, IPC_REG_HOST_COMM); 200 201 return (host_status & IPC_HOSTCOMM_READY_BIT); 202 } 203 204 /** 205 * ish_set_host_ready() - reconfig ipc host registers 206 * @dev: ishtp device pointer 207 * 208 * Set host to ready state 209 * This API is called in some case: 210 * fw is still on, but ipc is powered down. 211 * such as OOB case. 212 * 213 * Return: 0 for success else error fault code 214 */ 215 void ish_set_host_ready(struct ishtp_device *dev) 216 { 217 if (ish_chk_host_rdy(dev)) 218 return; 219 220 ish_set_host_rdy(dev); 221 set_host_ready(dev); 222 } 223 224 /** 225 * _ishtp_read_hdr() - Read message header 226 * @dev: ISHTP device pointer 227 * 228 * Read header of 32bit length 229 * 230 * Return: Read register value 231 */ 232 static uint32_t _ishtp_read_hdr(const struct ishtp_device *dev) 233 { 234 return ish_reg_read(dev, IPC_REG_ISH2HOST_MSG); 235 } 236 237 /** 238 * _ishtp_read - Read message 239 * @dev: ISHTP device pointer 240 * @buffer: message buffer 241 * @buffer_length: length of message buffer 242 * 243 * Read message from FW 244 * 245 * Return: Always 0 246 */ 247 static int _ishtp_read(struct ishtp_device *dev, unsigned char *buffer, 248 unsigned long buffer_length) 249 { 250 uint32_t i; 251 uint32_t *r_buf = (uint32_t *)buffer; 252 uint32_t msg_offs; 253 254 msg_offs = IPC_REG_ISH2HOST_MSG + sizeof(struct ishtp_msg_hdr); 255 for (i = 0; i < buffer_length; i += sizeof(uint32_t)) 256 *r_buf++ = ish_reg_read(dev, msg_offs + i); 257 258 return 0; 259 } 260 261 /** 262 * write_ipc_from_queue() - try to write ipc msg from Tx queue to device 263 * @dev: ishtp device pointer 264 * 265 * Check if DRBL is cleared. if it is - write the first IPC msg, then call 266 * the callback function (unless it's NULL) 267 * 268 * Return: 0 for success else failure code 269 */ 270 static int write_ipc_from_queue(struct ishtp_device *dev) 271 { 272 struct wr_msg_ctl_info *ipc_link; 273 unsigned long length; 274 unsigned long rem; 275 unsigned long flags; 276 uint32_t doorbell_val; 277 uint32_t *r_buf; 278 uint32_t reg_addr; 279 int i; 280 void (*ipc_send_compl)(void *); 281 void *ipc_send_compl_prm; 282 283 if (dev->dev_state == ISHTP_DEV_DISABLED) 284 return -EINVAL; 285 286 spin_lock_irqsave(&dev->wr_processing_spinlock, flags); 287 if (!ish_is_input_ready(dev)) { 288 spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags); 289 return -EBUSY; 290 } 291 292 /* 293 * if tx send list is empty - return 0; 294 * may happen, as RX_COMPLETE handler doesn't check list emptiness. 295 */ 296 if (list_empty(&dev->wr_processing_list)) { 297 spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags); 298 return 0; 299 } 300 301 ipc_link = list_first_entry(&dev->wr_processing_list, 302 struct wr_msg_ctl_info, link); 303 /* first 4 bytes of the data is the doorbell value (IPC header) */ 304 length = ipc_link->length - sizeof(uint32_t); 305 doorbell_val = *(uint32_t *)ipc_link->inline_data; 306 r_buf = (uint32_t *)(ipc_link->inline_data + sizeof(uint32_t)); 307 308 /* If sending MNG_SYNC_FW_CLOCK, update clock again */ 309 if (IPC_HEADER_GET_PROTOCOL(doorbell_val) == IPC_PROTOCOL_MNG && 310 IPC_HEADER_GET_MNG_CMD(doorbell_val) == MNG_SYNC_FW_CLOCK) { 311 uint64_t usec_system, usec_utc; 312 struct ipc_time_update_msg time_update; 313 struct time_sync_format ts_format; 314 315 usec_system = ktime_to_us(ktime_get_boottime()); 316 usec_utc = ktime_to_us(ktime_get_real()); 317 ts_format.ts1_source = HOST_SYSTEM_TIME_USEC; 318 ts_format.ts2_source = HOST_UTC_TIME_USEC; 319 ts_format.reserved = 0; 320 321 time_update.primary_host_time = usec_system; 322 time_update.secondary_host_time = usec_utc; 323 time_update.sync_info = ts_format; 324 325 memcpy(r_buf, &time_update, 326 sizeof(struct ipc_time_update_msg)); 327 } 328 329 for (i = 0, reg_addr = IPC_REG_HOST2ISH_MSG; i < length >> 2; i++, 330 reg_addr += 4) 331 ish_reg_write(dev, reg_addr, r_buf[i]); 332 333 rem = length & 0x3; 334 if (rem > 0) { 335 uint32_t reg = 0; 336 337 memcpy(®, &r_buf[length >> 2], rem); 338 ish_reg_write(dev, reg_addr, reg); 339 } 340 ish_reg_write(dev, IPC_REG_HOST2ISH_DRBL, doorbell_val); 341 342 /* Flush writes to msg registers and doorbell */ 343 ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS); 344 345 /* Update IPC counters */ 346 ++dev->ipc_tx_cnt; 347 dev->ipc_tx_bytes_cnt += IPC_HEADER_GET_LENGTH(doorbell_val); 348 349 ipc_send_compl = ipc_link->ipc_send_compl; 350 ipc_send_compl_prm = ipc_link->ipc_send_compl_prm; 351 list_del_init(&ipc_link->link); 352 list_add(&ipc_link->link, &dev->wr_free_list); 353 spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags); 354 355 /* 356 * callback will be called out of spinlock, 357 * after ipc_link returned to free list 358 */ 359 if (ipc_send_compl) 360 ipc_send_compl(ipc_send_compl_prm); 361 362 return 0; 363 } 364 365 /** 366 * write_ipc_to_queue() - write ipc msg to Tx queue 367 * @dev: ishtp device instance 368 * @ipc_send_compl: Send complete callback 369 * @ipc_send_compl_prm: Parameter to send in complete callback 370 * @msg: Pointer to message 371 * @length: Length of message 372 * 373 * Recived msg with IPC (and upper protocol) header and add it to the device 374 * Tx-to-write list then try to send the first IPC waiting msg 375 * (if DRBL is cleared) 376 * This function returns negative value for failure (means free list 377 * is empty, or msg too long) and 0 for success. 378 * 379 * Return: 0 for success else failure code 380 */ 381 static int write_ipc_to_queue(struct ishtp_device *dev, 382 void (*ipc_send_compl)(void *), void *ipc_send_compl_prm, 383 unsigned char *msg, int length) 384 { 385 struct wr_msg_ctl_info *ipc_link; 386 unsigned long flags; 387 388 if (length > IPC_FULL_MSG_SIZE) 389 return -EMSGSIZE; 390 391 spin_lock_irqsave(&dev->wr_processing_spinlock, flags); 392 if (list_empty(&dev->wr_free_list)) { 393 spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags); 394 return -ENOMEM; 395 } 396 ipc_link = list_first_entry(&dev->wr_free_list, 397 struct wr_msg_ctl_info, link); 398 list_del_init(&ipc_link->link); 399 400 ipc_link->ipc_send_compl = ipc_send_compl; 401 ipc_link->ipc_send_compl_prm = ipc_send_compl_prm; 402 ipc_link->length = length; 403 memcpy(ipc_link->inline_data, msg, length); 404 405 list_add_tail(&ipc_link->link, &dev->wr_processing_list); 406 spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags); 407 408 write_ipc_from_queue(dev); 409 410 return 0; 411 } 412 413 /** 414 * ipc_send_mng_msg() - Send management message 415 * @dev: ishtp device instance 416 * @msg_code: Message code 417 * @msg: Pointer to message 418 * @size: Length of message 419 * 420 * Send management message to FW 421 * 422 * Return: 0 for success else failure code 423 */ 424 static int ipc_send_mng_msg(struct ishtp_device *dev, uint32_t msg_code, 425 void *msg, size_t size) 426 { 427 unsigned char ipc_msg[IPC_FULL_MSG_SIZE]; 428 uint32_t drbl_val = IPC_BUILD_MNG_MSG(msg_code, size); 429 430 memcpy(ipc_msg, &drbl_val, sizeof(uint32_t)); 431 memcpy(ipc_msg + sizeof(uint32_t), msg, size); 432 return write_ipc_to_queue(dev, NULL, NULL, ipc_msg, 433 sizeof(uint32_t) + size); 434 } 435 436 #define WAIT_FOR_FW_RDY 0x1 437 #define WAIT_FOR_INPUT_RDY 0x2 438 439 /** 440 * timed_wait_for_timeout() - wait special event with timeout 441 * @dev: ISHTP device pointer 442 * @condition: indicate the condition for waiting 443 * @timeinc: time slice for every wait cycle, in ms 444 * @timeout: time in ms for timeout 445 * 446 * This function will check special event to be ready in a loop, the loop 447 * period is specificd in timeinc. Wait timeout will causes failure. 448 * 449 * Return: 0 for success else failure code 450 */ 451 static int timed_wait_for_timeout(struct ishtp_device *dev, int condition, 452 unsigned int timeinc, unsigned int timeout) 453 { 454 bool complete = false; 455 int ret; 456 457 do { 458 if (condition == WAIT_FOR_FW_RDY) { 459 complete = ishtp_fw_is_ready(dev); 460 } else if (condition == WAIT_FOR_INPUT_RDY) { 461 complete = ish_is_input_ready(dev); 462 } else { 463 ret = -EINVAL; 464 goto out; 465 } 466 467 if (!complete) { 468 unsigned long left_time; 469 470 left_time = msleep_interruptible(timeinc); 471 timeout -= (timeinc - left_time); 472 } 473 } while (!complete && timeout > 0); 474 475 if (complete) 476 ret = 0; 477 else 478 ret = -EBUSY; 479 480 out: 481 return ret; 482 } 483 484 #define TIME_SLICE_FOR_FW_RDY_MS 100 485 #define TIME_SLICE_FOR_INPUT_RDY_MS 100 486 #define TIMEOUT_FOR_FW_RDY_MS 2000 487 #define TIMEOUT_FOR_INPUT_RDY_MS 2000 488 489 /** 490 * ish_fw_reset_handler() - FW reset handler 491 * @dev: ishtp device pointer 492 * 493 * Handle FW reset 494 * 495 * Return: 0 for success else failure code 496 */ 497 static int ish_fw_reset_handler(struct ishtp_device *dev) 498 { 499 uint32_t reset_id; 500 unsigned long flags; 501 int ret; 502 503 /* Read reset ID */ 504 reset_id = ish_reg_read(dev, IPC_REG_ISH2HOST_MSG) & 0xFFFF; 505 506 /* Clear IPC output queue */ 507 spin_lock_irqsave(&dev->wr_processing_spinlock, flags); 508 list_splice_init(&dev->wr_processing_list, &dev->wr_free_list); 509 spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags); 510 511 /* ISHTP notification in IPC_RESET */ 512 ishtp_reset_handler(dev); 513 514 ret = timed_wait_for_timeout(dev, WAIT_FOR_INPUT_RDY, 515 TIME_SLICE_FOR_INPUT_RDY_MS, 516 TIMEOUT_FOR_INPUT_RDY_MS); 517 /* ISH FW is dead */ 518 if (ret) 519 return -EPIPE; 520 521 /* Send clock sync at once after reset */ 522 ishtp_dev->prev_sync = 0; 523 524 /* 525 * Set HOST2ISH.ILUP. Apparently we need this BEFORE sending 526 * RESET_NOTIFY_ACK - FW will be checking for it 527 */ 528 ish_set_host_rdy(dev); 529 /* Send RESET_NOTIFY_ACK (with reset_id) */ 530 ipc_send_mng_msg(dev, MNG_RESET_NOTIFY_ACK, &reset_id, 531 sizeof(uint32_t)); 532 533 /* Wait for ISH FW'es ILUP and ISHTP_READY */ 534 ret = timed_wait_for_timeout(dev, WAIT_FOR_FW_RDY, 535 TIME_SLICE_FOR_FW_RDY_MS, 536 TIMEOUT_FOR_FW_RDY_MS); 537 if (ret) { 538 /* ISH FW is dead */ 539 uint32_t ish_status; 540 541 ish_status = _ish_read_fw_sts_reg(dev); 542 dev_err(dev->devc, 543 "[ishtp-ish]: completed reset, ISH is dead (FWSTS = %08X)\n", 544 ish_status); 545 return -ENODEV; 546 } 547 return 0; 548 } 549 550 #define TIMEOUT_FOR_HW_RDY_MS 300 551 552 /** 553 * fw_reset_work_fn() - FW reset worker function 554 * @work: Work item 555 * 556 * Call ish_fw_reset_handler to complete FW reset 557 */ 558 static void fw_reset_work_fn(struct work_struct *work) 559 { 560 int rv; 561 562 rv = ish_fw_reset_handler(ishtp_dev); 563 if (!rv) { 564 /* ISH is ILUP & ISHTP-ready. Restart ISHTP */ 565 msleep_interruptible(TIMEOUT_FOR_HW_RDY_MS); 566 ishtp_dev->recvd_hw_ready = 1; 567 wake_up_interruptible(&ishtp_dev->wait_hw_ready); 568 569 /* ISHTP notification in IPC_RESET sequence completion */ 570 if (!work_pending(work)) 571 ishtp_reset_compl_handler(ishtp_dev); 572 } else 573 dev_err(ishtp_dev->devc, "[ishtp-ish]: FW reset failed (%d)\n", 574 rv); 575 } 576 577 /** 578 * _ish_sync_fw_clock() -Sync FW clock with the OS clock 579 * @dev: ishtp device pointer 580 * 581 * Sync FW and OS time 582 */ 583 static void _ish_sync_fw_clock(struct ishtp_device *dev) 584 { 585 struct ipc_time_update_msg time = {}; 586 587 if (dev->prev_sync && time_before(jiffies, dev->prev_sync + 20 * HZ)) 588 return; 589 590 dev->prev_sync = jiffies; 591 /* The fields of time would be updated while sending message */ 592 ipc_send_mng_msg(dev, MNG_SYNC_FW_CLOCK, &time, sizeof(time)); 593 } 594 595 /** 596 * recv_ipc() - Receive and process IPC management messages 597 * @dev: ishtp device instance 598 * @doorbell_val: doorbell value 599 * 600 * This function runs in ISR context. 601 * NOTE: Any other mng command than reset_notify and reset_notify_ack 602 * won't wake BH handler 603 */ 604 static void recv_ipc(struct ishtp_device *dev, uint32_t doorbell_val) 605 { 606 uint32_t mng_cmd; 607 608 mng_cmd = IPC_HEADER_GET_MNG_CMD(doorbell_val); 609 610 switch (mng_cmd) { 611 default: 612 break; 613 614 case MNG_RX_CMPL_INDICATION: 615 if (dev->suspend_flag) { 616 dev->suspend_flag = 0; 617 wake_up_interruptible(&dev->suspend_wait); 618 } 619 if (dev->resume_flag) { 620 dev->resume_flag = 0; 621 wake_up_interruptible(&dev->resume_wait); 622 } 623 624 write_ipc_from_queue(dev); 625 break; 626 627 case MNG_RESET_NOTIFY: 628 if (!ishtp_dev) { 629 ishtp_dev = dev; 630 } 631 schedule_work(&fw_reset_work); 632 break; 633 634 case MNG_RESET_NOTIFY_ACK: 635 dev->recvd_hw_ready = 1; 636 wake_up_interruptible(&dev->wait_hw_ready); 637 break; 638 } 639 } 640 641 /** 642 * ish_irq_handler() - ISH IRQ handler 643 * @irq: irq number 644 * @dev_id: ishtp device pointer 645 * 646 * ISH IRQ handler. If interrupt is generated and is for ISH it will process 647 * the interrupt. 648 */ 649 irqreturn_t ish_irq_handler(int irq, void *dev_id) 650 { 651 struct ishtp_device *dev = dev_id; 652 uint32_t doorbell_val; 653 bool interrupt_generated; 654 655 /* Check that it's interrupt from ISH (may be shared) */ 656 interrupt_generated = check_generated_interrupt(dev); 657 658 if (!interrupt_generated) 659 return IRQ_NONE; 660 661 doorbell_val = ish_reg_read(dev, IPC_REG_ISH2HOST_DRBL); 662 if (!IPC_IS_BUSY(doorbell_val)) 663 return IRQ_HANDLED; 664 665 if (dev->dev_state == ISHTP_DEV_DISABLED) 666 return IRQ_HANDLED; 667 668 /* Sanity check: IPC dgram length in header */ 669 if (IPC_HEADER_GET_LENGTH(doorbell_val) > IPC_PAYLOAD_SIZE) { 670 dev_err(dev->devc, 671 "IPC hdr - bad length: %u; dropped\n", 672 (unsigned int)IPC_HEADER_GET_LENGTH(doorbell_val)); 673 goto eoi; 674 } 675 676 switch (IPC_HEADER_GET_PROTOCOL(doorbell_val)) { 677 default: 678 break; 679 case IPC_PROTOCOL_MNG: 680 recv_ipc(dev, doorbell_val); 681 break; 682 case IPC_PROTOCOL_ISHTP: 683 ishtp_recv(dev); 684 break; 685 } 686 687 eoi: 688 /* Update IPC counters */ 689 ++dev->ipc_rx_cnt; 690 dev->ipc_rx_bytes_cnt += IPC_HEADER_GET_LENGTH(doorbell_val); 691 692 ish_reg_write(dev, IPC_REG_ISH2HOST_DRBL, 0); 693 /* Flush write to doorbell */ 694 ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS); 695 696 return IRQ_HANDLED; 697 } 698 699 /** 700 * ish_disable_dma() - disable dma communication between host and ISHFW 701 * @dev: ishtp device pointer 702 * 703 * Clear the dma enable bit and wait for dma inactive. 704 * 705 * Return: 0 for success else error code. 706 */ 707 int ish_disable_dma(struct ishtp_device *dev) 708 { 709 unsigned int dma_delay; 710 711 /* Clear the dma enable bit */ 712 ish_reg_write(dev, IPC_REG_ISH_RMP2, 0); 713 714 /* wait for dma inactive */ 715 for (dma_delay = 0; dma_delay < MAX_DMA_DELAY && 716 _ish_read_fw_sts_reg(dev) & (IPC_ISH_IN_DMA); 717 dma_delay += 5) 718 mdelay(5); 719 720 if (dma_delay >= MAX_DMA_DELAY) { 721 dev_err(dev->devc, 722 "Wait for DMA inactive timeout\n"); 723 return -EBUSY; 724 } 725 726 return 0; 727 } 728 729 /** 730 * ish_wakeup() - wakeup ishfw from waiting-for-host state 731 * @dev: ishtp device pointer 732 * 733 * Set the dma enable bit and send a void message to FW, 734 * it wil wakeup FW from waiting-for-host state. 735 */ 736 static void ish_wakeup(struct ishtp_device *dev) 737 { 738 /* Set dma enable bit */ 739 ish_reg_write(dev, IPC_REG_ISH_RMP2, IPC_RMP2_DMA_ENABLED); 740 741 /* 742 * Send 0 IPC message so that ISH FW wakes up if it was already 743 * asleep. 744 */ 745 ish_reg_write(dev, IPC_REG_HOST2ISH_DRBL, IPC_DRBL_BUSY_BIT); 746 747 /* Flush writes to doorbell and REMAP2 */ 748 ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS); 749 } 750 751 /** 752 * _ish_hw_reset() - HW reset 753 * @dev: ishtp device pointer 754 * 755 * Reset ISH HW to recover if any error 756 * 757 * Return: 0 for success else error fault code 758 */ 759 static int _ish_hw_reset(struct ishtp_device *dev) 760 { 761 struct pci_dev *pdev = dev->pdev; 762 int rv; 763 uint16_t csr; 764 765 if (!pdev) 766 return -ENODEV; 767 768 rv = pci_reset_function(pdev); 769 if (!rv) 770 dev->dev_state = ISHTP_DEV_RESETTING; 771 772 if (!pdev->pm_cap) { 773 dev_err(&pdev->dev, "Can't reset - no PM caps\n"); 774 return -EINVAL; 775 } 776 777 /* Disable dma communication between FW and host */ 778 if (ish_disable_dma(dev)) { 779 dev_err(&pdev->dev, 780 "Can't reset - stuck with DMA in-progress\n"); 781 return -EBUSY; 782 } 783 784 pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &csr); 785 786 csr &= ~PCI_PM_CTRL_STATE_MASK; 787 csr |= PCI_D3hot; 788 pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, csr); 789 790 mdelay(pdev->d3hot_delay); 791 792 csr &= ~PCI_PM_CTRL_STATE_MASK; 793 csr |= PCI_D0; 794 pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, csr); 795 796 /* Now we can enable ISH DMA operation and wakeup ISHFW */ 797 ish_wakeup(dev); 798 799 return 0; 800 } 801 802 /** 803 * _ish_ipc_reset() - IPC reset 804 * @dev: ishtp device pointer 805 * 806 * Resets host and fw IPC and upper layers 807 * 808 * Return: 0 for success else error fault code 809 */ 810 static int _ish_ipc_reset(struct ishtp_device *dev) 811 { 812 struct ipc_rst_payload_type ipc_mng_msg; 813 int rv = 0; 814 815 ipc_mng_msg.reset_id = 1; 816 ipc_mng_msg.reserved = 0; 817 818 set_host_ready(dev); 819 820 /* Clear the incoming doorbell */ 821 ish_reg_write(dev, IPC_REG_ISH2HOST_DRBL, 0); 822 /* Flush write to doorbell */ 823 ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS); 824 825 dev->recvd_hw_ready = 0; 826 827 /* send message */ 828 rv = ipc_send_mng_msg(dev, MNG_RESET_NOTIFY, &ipc_mng_msg, 829 sizeof(struct ipc_rst_payload_type)); 830 if (rv) { 831 dev_err(dev->devc, "Failed to send IPC MNG_RESET_NOTIFY\n"); 832 return rv; 833 } 834 835 wait_event_interruptible_timeout(dev->wait_hw_ready, 836 dev->recvd_hw_ready, 2 * HZ); 837 if (!dev->recvd_hw_ready) { 838 dev_err(dev->devc, "Timed out waiting for HW ready\n"); 839 rv = -ENODEV; 840 } 841 842 return rv; 843 } 844 845 /** 846 * ish_hw_start() -Start ISH HW 847 * @dev: ishtp device pointer 848 * 849 * Set host to ready state and wait for FW reset 850 * 851 * Return: 0 for success else error fault code 852 */ 853 int ish_hw_start(struct ishtp_device *dev) 854 { 855 ish_set_host_rdy(dev); 856 857 set_host_ready(dev); 858 859 /* After that we can enable ISH DMA operation and wakeup ISHFW */ 860 ish_wakeup(dev); 861 862 /* wait for FW-initiated reset flow */ 863 if (!dev->recvd_hw_ready) 864 wait_event_interruptible_timeout(dev->wait_hw_ready, 865 dev->recvd_hw_ready, 866 10 * HZ); 867 868 if (!dev->recvd_hw_ready) { 869 dev_err(dev->devc, 870 "[ishtp-ish]: Timed out waiting for FW-initiated reset\n"); 871 return -ENODEV; 872 } 873 874 return 0; 875 } 876 877 /** 878 * ish_ipc_get_header() -Get doorbell value 879 * @dev: ishtp device pointer 880 * @length: length of message 881 * @busy: busy status 882 * 883 * Get door bell value from message header 884 * 885 * Return: door bell value 886 */ 887 static uint32_t ish_ipc_get_header(struct ishtp_device *dev, int length, 888 int busy) 889 { 890 uint32_t drbl_val; 891 892 drbl_val = IPC_BUILD_HEADER(length, IPC_PROTOCOL_ISHTP, busy); 893 894 return drbl_val; 895 } 896 897 /** 898 * _dma_no_cache_snooping() 899 * 900 * Check on current platform, DMA supports cache snooping or not. 901 * This callback is used to notify uplayer driver if manully cache 902 * flush is needed when do DMA operation. 903 * 904 * Please pay attention to this callback implementation, if declare 905 * having cache snooping on a cache snooping not supported platform 906 * will cause uplayer driver receiving mismatched data; and if 907 * declare no cache snooping on a cache snooping supported platform 908 * will cause cache be flushed twice and performance hit. 909 * 910 * @dev: ishtp device pointer 911 * 912 * Return: false - has cache snooping capability 913 * true - no cache snooping, need manually cache flush 914 */ 915 static bool _dma_no_cache_snooping(struct ishtp_device *dev) 916 { 917 return (dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_EHL_Ax || 918 dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_TGL_LP || 919 dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_TGL_H || 920 dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_ADL_S || 921 dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_ADL_P); 922 } 923 924 static const struct ishtp_hw_ops ish_hw_ops = { 925 .hw_reset = _ish_hw_reset, 926 .ipc_reset = _ish_ipc_reset, 927 .ipc_get_header = ish_ipc_get_header, 928 .ishtp_read = _ishtp_read, 929 .write = write_ipc_to_queue, 930 .get_fw_status = _ish_read_fw_sts_reg, 931 .sync_fw_clock = _ish_sync_fw_clock, 932 .ishtp_read_hdr = _ishtp_read_hdr, 933 .dma_no_cache_snooping = _dma_no_cache_snooping 934 }; 935 936 /** 937 * ish_dev_init() -Initialize ISH devoce 938 * @pdev: PCI device 939 * 940 * Allocate ISHTP device and initialize IPC processing 941 * 942 * Return: ISHTP device instance on success else NULL 943 */ 944 struct ishtp_device *ish_dev_init(struct pci_dev *pdev) 945 { 946 struct ishtp_device *dev; 947 int i; 948 int ret; 949 950 dev = devm_kzalloc(&pdev->dev, 951 sizeof(struct ishtp_device) + sizeof(struct ish_hw), 952 GFP_KERNEL); 953 if (!dev) 954 return NULL; 955 956 dev->devc = &pdev->dev; 957 ishtp_device_init(dev); 958 959 init_waitqueue_head(&dev->wait_hw_ready); 960 961 spin_lock_init(&dev->wr_processing_spinlock); 962 963 /* Init IPC processing and free lists */ 964 INIT_LIST_HEAD(&dev->wr_processing_list); 965 INIT_LIST_HEAD(&dev->wr_free_list); 966 for (i = 0; i < IPC_TX_FIFO_SIZE; i++) { 967 struct wr_msg_ctl_info *tx_buf; 968 969 tx_buf = devm_kzalloc(&pdev->dev, 970 sizeof(struct wr_msg_ctl_info), 971 GFP_KERNEL); 972 if (!tx_buf) { 973 /* 974 * IPC buffers may be limited or not available 975 * at all - although this shouldn't happen 976 */ 977 dev_err(dev->devc, 978 "[ishtp-ish]: failure in Tx FIFO allocations (%d)\n", 979 i); 980 break; 981 } 982 list_add_tail(&tx_buf->link, &dev->wr_free_list); 983 } 984 985 ret = devm_work_autocancel(&pdev->dev, &fw_reset_work, fw_reset_work_fn); 986 if (ret) { 987 dev_err(dev->devc, "Failed to initialise FW reset work\n"); 988 return NULL; 989 } 990 991 dev->ops = &ish_hw_ops; 992 dev->mtu = IPC_PAYLOAD_SIZE - sizeof(struct ishtp_msg_hdr); 993 return dev; 994 } 995 996 /** 997 * ish_device_disable() - Disable ISH device 998 * @dev: ISHTP device pointer 999 * 1000 * Disable ISH by clearing host ready to inform firmware. 1001 */ 1002 void ish_device_disable(struct ishtp_device *dev) 1003 { 1004 struct pci_dev *pdev = dev->pdev; 1005 1006 if (!pdev) 1007 return; 1008 1009 /* Disable dma communication between FW and host */ 1010 if (ish_disable_dma(dev)) { 1011 dev_err(&pdev->dev, 1012 "Can't reset - stuck with DMA in-progress\n"); 1013 return; 1014 } 1015 1016 /* Put ISH to D3hot state for power saving */ 1017 pci_set_power_state(pdev, PCI_D3hot); 1018 1019 dev->dev_state = ISHTP_DEV_DISABLED; 1020 ish_clr_host_rdy(dev); 1021 } 1022