mei: fix KDoc documentation formatting
[linux/fpc-iii.git] / drivers / misc / mei / hw-txe.c
blob0a155228645b678ec3f37288653a152b2bf24c55
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2013-2014, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
17 #include <linux/pci.h>
18 #include <linux/jiffies.h>
19 #include <linux/delay.h>
20 #include <linux/kthread.h>
21 #include <linux/irqreturn.h>
23 #include <linux/mei.h>
25 #include "mei_dev.h"
26 #include "hw-txe.h"
27 #include "client.h"
28 #include "hbm.h"
30 /**
31 * mei_txe_reg_read - Reads 32bit data from the device
33 * @base_addr: registers base address
34 * @offset: register offset
37 static inline u32 mei_txe_reg_read(void __iomem *base_addr,
38 unsigned long offset)
40 return ioread32(base_addr + offset);
43 /**
44 * mei_txe_reg_write - Writes 32bit data to the device
46 * @base_addr: registers base address
47 * @offset: register offset
48 * @value: the value to write
50 static inline void mei_txe_reg_write(void __iomem *base_addr,
51 unsigned long offset, u32 value)
53 iowrite32(value, base_addr + offset);
56 /**
57 * mei_txe_sec_reg_read_silent - Reads 32bit data from the SeC BAR
59 * @dev: the device structure
60 * @offset: register offset
62 * Doesn't check for aliveness while Reads 32bit data from the SeC BAR
64 static inline u32 mei_txe_sec_reg_read_silent(struct mei_txe_hw *hw,
65 unsigned long offset)
67 return mei_txe_reg_read(hw->mem_addr[SEC_BAR], offset);
70 /**
71 * mei_txe_sec_reg_read - Reads 32bit data from the SeC BAR
73 * @dev: the device structure
74 * @offset: register offset
76 * Reads 32bit data from the SeC BAR and shout loud if aliveness is not set
78 static inline u32 mei_txe_sec_reg_read(struct mei_txe_hw *hw,
79 unsigned long offset)
81 WARN(!hw->aliveness, "sec read: aliveness not asserted\n");
82 return mei_txe_sec_reg_read_silent(hw, offset);
84 /**
85 * mei_txe_sec_reg_write_silent - Writes 32bit data to the SeC BAR
86 * doesn't check for aliveness
88 * @hw: the txe hardware structure
89 * @offset: register offset
90 * @value: value to write
92 * Doesn't check for aliveness while writes 32bit data from to the SeC BAR
94 static inline void mei_txe_sec_reg_write_silent(struct mei_txe_hw *hw,
95 unsigned long offset, u32 value)
97 mei_txe_reg_write(hw->mem_addr[SEC_BAR], offset, value);
101 * mei_txe_sec_reg_write - Writes 32bit data to the SeC BAR
103 * @hw: the txe hardware structure
104 * @offset: register offset
105 * @value: value to write
107 * Writes 32bit data from the SeC BAR and shout loud if aliveness is not set
109 static inline void mei_txe_sec_reg_write(struct mei_txe_hw *hw,
110 unsigned long offset, u32 value)
112 WARN(!hw->aliveness, "sec write: aliveness not asserted\n");
113 mei_txe_sec_reg_write_silent(hw, offset, value);
116 * mei_txe_br_reg_read - Reads 32bit data from the Bridge BAR
118 * @hw: the device structure
119 * @offset: offset from which to read the data
122 static inline u32 mei_txe_br_reg_read(struct mei_txe_hw *hw,
123 unsigned long offset)
125 return mei_txe_reg_read(hw->mem_addr[BRIDGE_BAR], offset);
129 * mei_txe_br_reg_write - Writes 32bit data to the Bridge BAR
131 * @hw: the txe hardware structure
132 * @offset: offset from which to write the data
133 * @value: the byte to write
135 static inline void mei_txe_br_reg_write(struct mei_txe_hw *hw,
136 unsigned long offset, u32 value)
138 mei_txe_reg_write(hw->mem_addr[BRIDGE_BAR], offset, value);
142 * mei_txe_aliveness_set - request for aliveness change
144 * @dev: the device structure
145 * @req: requested aliveness value
147 * Request for aliveness change and returns true if the change is
148 * really needed and false if aliveness is already
149 * in the requested state
150 * Requires device lock to be held
152 static bool mei_txe_aliveness_set(struct mei_device *dev, u32 req)
155 struct mei_txe_hw *hw = to_txe_hw(dev);
156 bool do_req = hw->aliveness != req;
158 dev_dbg(dev->dev, "Aliveness current=%d request=%d\n",
159 hw->aliveness, req);
160 if (do_req) {
161 dev->pg_event = MEI_PG_EVENT_WAIT;
162 mei_txe_br_reg_write(hw, SICR_HOST_ALIVENESS_REQ_REG, req);
164 return do_req;
169 * mei_txe_aliveness_req_get - get aliveness requested register value
171 * @dev: the device structure
173 * Extract HICR_HOST_ALIVENESS_RESP_ACK bit from
174 * from HICR_HOST_ALIVENESS_REQ register value
176 static u32 mei_txe_aliveness_req_get(struct mei_device *dev)
178 struct mei_txe_hw *hw = to_txe_hw(dev);
179 u32 reg;
181 reg = mei_txe_br_reg_read(hw, SICR_HOST_ALIVENESS_REQ_REG);
182 return reg & SICR_HOST_ALIVENESS_REQ_REQUESTED;
186 * mei_txe_aliveness_get - get aliveness response register value
187 * @dev: the device structure
189 * Extract HICR_HOST_ALIVENESS_RESP_ACK bit
190 * from HICR_HOST_ALIVENESS_RESP register value
192 static u32 mei_txe_aliveness_get(struct mei_device *dev)
194 struct mei_txe_hw *hw = to_txe_hw(dev);
195 u32 reg;
197 reg = mei_txe_br_reg_read(hw, HICR_HOST_ALIVENESS_RESP_REG);
198 return reg & HICR_HOST_ALIVENESS_RESP_ACK;
202 * mei_txe_aliveness_poll - waits for aliveness to settle
204 * @dev: the device structure
205 * @expected: expected aliveness value
207 * Polls for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
209 * Return: > 0 if the expected value was received, -ETIME otherwise
211 static int mei_txe_aliveness_poll(struct mei_device *dev, u32 expected)
213 struct mei_txe_hw *hw = to_txe_hw(dev);
214 int t = 0;
216 do {
217 hw->aliveness = mei_txe_aliveness_get(dev);
218 if (hw->aliveness == expected) {
219 dev->pg_event = MEI_PG_EVENT_IDLE;
220 dev_dbg(dev->dev,
221 "aliveness settled after %d msecs\n", t);
222 return t;
224 mutex_unlock(&dev->device_lock);
225 msleep(MSEC_PER_SEC / 5);
226 mutex_lock(&dev->device_lock);
227 t += MSEC_PER_SEC / 5;
228 } while (t < SEC_ALIVENESS_WAIT_TIMEOUT);
230 dev->pg_event = MEI_PG_EVENT_IDLE;
231 dev_err(dev->dev, "aliveness timed out\n");
232 return -ETIME;
236 * mei_txe_aliveness_wait - waits for aliveness to settle
238 * @dev: the device structure
239 * @expected: expected aliveness value
241 * Waits for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
243 * Return: 0 on success and < 0 otherwise
245 static int mei_txe_aliveness_wait(struct mei_device *dev, u32 expected)
247 struct mei_txe_hw *hw = to_txe_hw(dev);
248 const unsigned long timeout =
249 msecs_to_jiffies(SEC_ALIVENESS_WAIT_TIMEOUT);
250 long err;
251 int ret;
253 hw->aliveness = mei_txe_aliveness_get(dev);
254 if (hw->aliveness == expected)
255 return 0;
257 mutex_unlock(&dev->device_lock);
258 err = wait_event_timeout(hw->wait_aliveness_resp,
259 dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
260 mutex_lock(&dev->device_lock);
262 hw->aliveness = mei_txe_aliveness_get(dev);
263 ret = hw->aliveness == expected ? 0 : -ETIME;
265 if (ret)
266 dev_warn(dev->dev, "aliveness timed out = %ld aliveness = %d event = %d\n",
267 err, hw->aliveness, dev->pg_event);
268 else
269 dev_dbg(dev->dev, "aliveness settled after = %d msec aliveness = %d event = %d\n",
270 jiffies_to_msecs(timeout - err),
271 hw->aliveness, dev->pg_event);
273 dev->pg_event = MEI_PG_EVENT_IDLE;
274 return ret;
278 * mei_txe_aliveness_set_sync - sets an wait for aliveness to complete
280 * @dev: the device structure
282 * Return: 0 on success and < 0 otherwise
284 int mei_txe_aliveness_set_sync(struct mei_device *dev, u32 req)
286 if (mei_txe_aliveness_set(dev, req))
287 return mei_txe_aliveness_wait(dev, req);
288 return 0;
292 * mei_txe_pg_is_enabled - detect if PG is supported by HW
294 * @dev: the device structure
296 * Return: true is pg supported, false otherwise
298 static bool mei_txe_pg_is_enabled(struct mei_device *dev)
300 return true;
304 * mei_txe_pg_state - translate aliveness register value
305 * to the mei power gating state
307 * @dev: the device structure
309 * Return: MEI_PG_OFF if aliveness is on and MEI_PG_ON otherwise
311 static inline enum mei_pg_state mei_txe_pg_state(struct mei_device *dev)
313 struct mei_txe_hw *hw = to_txe_hw(dev);
315 return hw->aliveness ? MEI_PG_OFF : MEI_PG_ON;
319 * mei_txe_input_ready_interrupt_enable - sets the Input Ready Interrupt
321 * @dev: the device structure
323 static void mei_txe_input_ready_interrupt_enable(struct mei_device *dev)
325 struct mei_txe_hw *hw = to_txe_hw(dev);
326 u32 hintmsk;
327 /* Enable the SEC_IPC_HOST_INT_MASK_IN_RDY interrupt */
328 hintmsk = mei_txe_sec_reg_read(hw, SEC_IPC_HOST_INT_MASK_REG);
329 hintmsk |= SEC_IPC_HOST_INT_MASK_IN_RDY;
330 mei_txe_sec_reg_write(hw, SEC_IPC_HOST_INT_MASK_REG, hintmsk);
334 * mei_txe_input_doorbell_set - sets bit 0 in
335 * SEC_IPC_INPUT_DOORBELL.IPC_INPUT_DOORBELL.
337 * @hw: the txe hardware structure
339 static void mei_txe_input_doorbell_set(struct mei_txe_hw *hw)
341 /* Clear the interrupt cause */
342 clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause);
343 mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_DOORBELL_REG, 1);
347 * mei_txe_output_ready_set - Sets the SICR_SEC_IPC_OUTPUT_STATUS bit to 1
349 * @hw: the txe hardware structure
351 static void mei_txe_output_ready_set(struct mei_txe_hw *hw)
353 mei_txe_br_reg_write(hw,
354 SICR_SEC_IPC_OUTPUT_STATUS_REG,
355 SEC_IPC_OUTPUT_STATUS_RDY);
359 * mei_txe_is_input_ready - check if TXE is ready for receiving data
361 * @dev: the device structure
363 static bool mei_txe_is_input_ready(struct mei_device *dev)
365 struct mei_txe_hw *hw = to_txe_hw(dev);
366 u32 status;
368 status = mei_txe_sec_reg_read(hw, SEC_IPC_INPUT_STATUS_REG);
369 return !!(SEC_IPC_INPUT_STATUS_RDY & status);
373 * mei_txe_intr_clear - clear all interrupts
375 * @dev: the device structure
377 static inline void mei_txe_intr_clear(struct mei_device *dev)
379 struct mei_txe_hw *hw = to_txe_hw(dev);
381 mei_txe_sec_reg_write_silent(hw, SEC_IPC_HOST_INT_STATUS_REG,
382 SEC_IPC_HOST_INT_STATUS_PENDING);
383 mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_STS_MSK);
384 mei_txe_br_reg_write(hw, HHISR_REG, IPC_HHIER_MSK);
388 * mei_txe_intr_disable - disable all interrupts
390 * @dev: the device structure
392 static void mei_txe_intr_disable(struct mei_device *dev)
394 struct mei_txe_hw *hw = to_txe_hw(dev);
396 mei_txe_br_reg_write(hw, HHIER_REG, 0);
397 mei_txe_br_reg_write(hw, HIER_REG, 0);
400 * mei_txe_intr_disable - enable all interrupts
402 * @dev: the device structure
404 static void mei_txe_intr_enable(struct mei_device *dev)
406 struct mei_txe_hw *hw = to_txe_hw(dev);
408 mei_txe_br_reg_write(hw, HHIER_REG, IPC_HHIER_MSK);
409 mei_txe_br_reg_write(hw, HIER_REG, HIER_INT_EN_MSK);
413 * mei_txe_pending_interrupts - check if there are pending interrupts
414 * only Aliveness, Input ready, and output doorbell are of relevance
416 * @dev: the device structure
418 * Checks if there are pending interrupts
419 * only Aliveness, Readiness, Input ready, and Output doorbell are relevant
421 static bool mei_txe_pending_interrupts(struct mei_device *dev)
424 struct mei_txe_hw *hw = to_txe_hw(dev);
425 bool ret = (hw->intr_cause & (TXE_INTR_READINESS |
426 TXE_INTR_ALIVENESS |
427 TXE_INTR_IN_READY |
428 TXE_INTR_OUT_DB));
430 if (ret) {
431 dev_dbg(dev->dev,
432 "Pending Interrupts InReady=%01d Readiness=%01d, Aliveness=%01d, OutDoor=%01d\n",
433 !!(hw->intr_cause & TXE_INTR_IN_READY),
434 !!(hw->intr_cause & TXE_INTR_READINESS),
435 !!(hw->intr_cause & TXE_INTR_ALIVENESS),
436 !!(hw->intr_cause & TXE_INTR_OUT_DB));
438 return ret;
442 * mei_txe_input_payload_write - write a dword to the host buffer
443 * at offset idx
445 * @dev: the device structure
446 * @idx: index in the host buffer
447 * @value: value
449 static void mei_txe_input_payload_write(struct mei_device *dev,
450 unsigned long idx, u32 value)
452 struct mei_txe_hw *hw = to_txe_hw(dev);
454 mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_PAYLOAD_REG +
455 (idx * sizeof(u32)), value);
459 * mei_txe_out_data_read - read dword from the device buffer
460 * at offset idx
462 * @dev: the device structure
463 * @idx: index in the device buffer
465 * Return: register value at index
467 static u32 mei_txe_out_data_read(const struct mei_device *dev,
468 unsigned long idx)
470 struct mei_txe_hw *hw = to_txe_hw(dev);
472 return mei_txe_br_reg_read(hw,
473 BRIDGE_IPC_OUTPUT_PAYLOAD_REG + (idx * sizeof(u32)));
476 /* Readiness */
479 * mei_txe_readiness_set_host_rdy
481 * @dev: the device structure
483 static void mei_txe_readiness_set_host_rdy(struct mei_device *dev)
485 struct mei_txe_hw *hw = to_txe_hw(dev);
487 mei_txe_br_reg_write(hw,
488 SICR_HOST_IPC_READINESS_REQ_REG,
489 SICR_HOST_IPC_READINESS_HOST_RDY);
493 * mei_txe_readiness_clear
495 * @dev: the device structure
497 static void mei_txe_readiness_clear(struct mei_device *dev)
499 struct mei_txe_hw *hw = to_txe_hw(dev);
501 mei_txe_br_reg_write(hw, SICR_HOST_IPC_READINESS_REQ_REG,
502 SICR_HOST_IPC_READINESS_RDY_CLR);
505 * mei_txe_readiness_get - Reads and returns
506 * the HICR_SEC_IPC_READINESS register value
508 * @dev: the device structure
510 * Return: the HICR_SEC_IPC_READINESS register value
512 static u32 mei_txe_readiness_get(struct mei_device *dev)
514 struct mei_txe_hw *hw = to_txe_hw(dev);
516 return mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
521 * mei_txe_readiness_is_sec_rdy - check readiness
522 * for HICR_SEC_IPC_READINESS_SEC_RDY
524 * @readiness - cached readiness state
526 static inline bool mei_txe_readiness_is_sec_rdy(u32 readiness)
528 return !!(readiness & HICR_SEC_IPC_READINESS_SEC_RDY);
532 * mei_txe_hw_is_ready - check if the hw is ready
534 * @dev: the device structure
536 static bool mei_txe_hw_is_ready(struct mei_device *dev)
538 u32 readiness = mei_txe_readiness_get(dev);
540 return mei_txe_readiness_is_sec_rdy(readiness);
544 * mei_txe_host_is_ready - check if the host is ready
546 * @dev: the device structure
548 static inline bool mei_txe_host_is_ready(struct mei_device *dev)
550 struct mei_txe_hw *hw = to_txe_hw(dev);
551 u32 reg = mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
553 return !!(reg & HICR_SEC_IPC_READINESS_HOST_RDY);
557 * mei_txe_readiness_wait - wait till readiness settles
559 * @dev: the device structure
561 * Return: 0 on success and -ETIME on timeout
563 static int mei_txe_readiness_wait(struct mei_device *dev)
565 if (mei_txe_hw_is_ready(dev))
566 return 0;
568 mutex_unlock(&dev->device_lock);
569 wait_event_timeout(dev->wait_hw_ready, dev->recvd_hw_ready,
570 msecs_to_jiffies(SEC_RESET_WAIT_TIMEOUT));
571 mutex_lock(&dev->device_lock);
572 if (!dev->recvd_hw_ready) {
573 dev_err(dev->dev, "wait for readiness failed\n");
574 return -ETIME;
577 dev->recvd_hw_ready = false;
578 return 0;
581 const struct mei_fw_status mei_txe_fw_sts = {
582 .count = 2,
583 .status[0] = PCI_CFG_TXE_FW_STS0,
584 .status[1] = PCI_CFG_TXE_FW_STS1
588 * mei_txe_fw_status - read fw status register from pci config space
590 * @dev: mei device
591 * @fw_status: fw status register values
593 static int mei_txe_fw_status(struct mei_device *dev,
594 struct mei_fw_status *fw_status)
596 const struct mei_fw_status *fw_src = &mei_txe_fw_sts;
597 struct pci_dev *pdev = to_pci_dev(dev->dev);
598 int ret;
599 int i;
601 if (!fw_status)
602 return -EINVAL;
604 fw_status->count = fw_src->count;
605 for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
606 ret = pci_read_config_dword(pdev,
607 fw_src->status[i], &fw_status->status[i]);
608 if (ret)
609 return ret;
612 return 0;
616 * mei_txe_hw_config - configure hardware at the start of the devices
618 * @dev: the device structure
620 * Configure hardware at the start of the device should be done only
621 * once at the device probe time
623 static void mei_txe_hw_config(struct mei_device *dev)
626 struct mei_txe_hw *hw = to_txe_hw(dev);
628 /* Doesn't change in runtime */
629 dev->hbuf_depth = PAYLOAD_SIZE / 4;
631 hw->aliveness = mei_txe_aliveness_get(dev);
632 hw->readiness = mei_txe_readiness_get(dev);
634 dev_dbg(dev->dev, "aliveness_resp = 0x%08x, readiness = 0x%08x.\n",
635 hw->aliveness, hw->readiness);
640 * mei_txe_write - writes a message to device.
642 * @dev: the device structure
643 * @header: header of message
644 * @buf: message buffer will be written
646 * Return: if success, 0 - otherwise.
649 static int mei_txe_write(struct mei_device *dev,
650 struct mei_msg_hdr *header, unsigned char *buf)
652 struct mei_txe_hw *hw = to_txe_hw(dev);
653 unsigned long rem;
654 unsigned long length;
655 int slots = dev->hbuf_depth;
656 u32 *reg_buf = (u32 *)buf;
657 u32 dw_cnt;
658 int i;
660 if (WARN_ON(!header || !buf))
661 return -EINVAL;
663 length = header->length;
665 dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
667 dw_cnt = mei_data2slots(length);
668 if (dw_cnt > slots)
669 return -EMSGSIZE;
671 if (WARN(!hw->aliveness, "txe write: aliveness not asserted\n"))
672 return -EAGAIN;
674 /* Enable Input Ready Interrupt. */
675 mei_txe_input_ready_interrupt_enable(dev);
677 if (!mei_txe_is_input_ready(dev)) {
678 struct mei_fw_status fw_status;
680 mei_fw_status(dev, &fw_status);
681 dev_err(dev->dev, "Input is not ready " FW_STS_FMT "\n",
682 FW_STS_PRM(fw_status));
683 return -EAGAIN;
686 mei_txe_input_payload_write(dev, 0, *((u32 *)header));
688 for (i = 0; i < length / 4; i++)
689 mei_txe_input_payload_write(dev, i + 1, reg_buf[i]);
691 rem = length & 0x3;
692 if (rem > 0) {
693 u32 reg = 0;
695 memcpy(&reg, &buf[length - rem], rem);
696 mei_txe_input_payload_write(dev, i + 1, reg);
699 /* after each write the whole buffer is consumed */
700 hw->slots = 0;
702 /* Set Input-Doorbell */
703 mei_txe_input_doorbell_set(hw);
705 return 0;
709 * mei_txe_hbuf_max_len - mimics the me hbuf circular buffer
711 * @dev: the device structure
713 * Return: PAYLOAD_SIZE - 4
715 static size_t mei_txe_hbuf_max_len(const struct mei_device *dev)
717 return PAYLOAD_SIZE - sizeof(struct mei_msg_hdr);
721 * mei_txe_hbuf_empty_slots - mimics the me hbuf circular buffer
723 * @dev: the device structure
725 * Return: always hbuf_depth
727 static int mei_txe_hbuf_empty_slots(struct mei_device *dev)
729 struct mei_txe_hw *hw = to_txe_hw(dev);
731 return hw->slots;
735 * mei_txe_count_full_read_slots - mimics the me device circular buffer
737 * @dev: the device structure
739 * Return: always buffer size in dwords count
741 static int mei_txe_count_full_read_slots(struct mei_device *dev)
743 /* read buffers has static size */
744 return PAYLOAD_SIZE / 4;
748 * mei_txe_read_hdr - read message header which is always in 4 first bytes
750 * @dev: the device structure
752 * Return: mei message header
755 static u32 mei_txe_read_hdr(const struct mei_device *dev)
757 return mei_txe_out_data_read(dev, 0);
760 * mei_txe_read - reads a message from the txe device.
762 * @dev: the device structure
763 * @buf: message buffer will be written
764 * @len: message size will be read
766 * Return: -EINVAL on error wrong argument and 0 on success
768 static int mei_txe_read(struct mei_device *dev,
769 unsigned char *buf, unsigned long len)
772 struct mei_txe_hw *hw = to_txe_hw(dev);
773 u32 *reg_buf, reg;
774 u32 rem;
775 u32 i;
777 if (WARN_ON(!buf || !len))
778 return -EINVAL;
780 reg_buf = (u32 *)buf;
781 rem = len & 0x3;
783 dev_dbg(dev->dev, "buffer-length = %lu buf[0]0x%08X\n",
784 len, mei_txe_out_data_read(dev, 0));
786 for (i = 0; i < len / 4; i++) {
787 /* skip header: index starts from 1 */
788 reg = mei_txe_out_data_read(dev, i + 1);
789 dev_dbg(dev->dev, "buf[%d] = 0x%08X\n", i, reg);
790 *reg_buf++ = reg;
793 if (rem) {
794 reg = mei_txe_out_data_read(dev, i + 1);
795 memcpy(reg_buf, &reg, rem);
798 mei_txe_output_ready_set(hw);
799 return 0;
803 * mei_txe_hw_reset - resets host and fw.
805 * @dev: the device structure
806 * @intr_enable: if interrupt should be enabled after reset.
808 * Return: 0 on success and < 0 in case of error
810 static int mei_txe_hw_reset(struct mei_device *dev, bool intr_enable)
812 struct mei_txe_hw *hw = to_txe_hw(dev);
814 u32 aliveness_req;
816 * read input doorbell to ensure consistency between Bridge and SeC
817 * return value might be garbage return
819 (void)mei_txe_sec_reg_read_silent(hw, SEC_IPC_INPUT_DOORBELL_REG);
821 aliveness_req = mei_txe_aliveness_req_get(dev);
822 hw->aliveness = mei_txe_aliveness_get(dev);
824 /* Disable interrupts in this stage we will poll */
825 mei_txe_intr_disable(dev);
828 * If Aliveness Request and Aliveness Response are not equal then
829 * wait for them to be equal
830 * Since we might have interrupts disabled - poll for it
832 if (aliveness_req != hw->aliveness)
833 if (mei_txe_aliveness_poll(dev, aliveness_req) < 0) {
834 dev_err(dev->dev, "wait for aliveness settle failed ... bailing out\n");
835 return -EIO;
839 * If Aliveness Request and Aliveness Response are set then clear them
841 if (aliveness_req) {
842 mei_txe_aliveness_set(dev, 0);
843 if (mei_txe_aliveness_poll(dev, 0) < 0) {
844 dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
845 return -EIO;
850 * Set readiness RDY_CLR bit
852 mei_txe_readiness_clear(dev);
854 return 0;
858 * mei_txe_hw_start - start the hardware after reset
860 * @dev: the device structure
862 * Return: 0 on success and < 0 in case of error
864 static int mei_txe_hw_start(struct mei_device *dev)
866 struct mei_txe_hw *hw = to_txe_hw(dev);
867 int ret;
869 u32 hisr;
871 /* bring back interrupts */
872 mei_txe_intr_enable(dev);
874 ret = mei_txe_readiness_wait(dev);
875 if (ret < 0) {
876 dev_err(dev->dev, "waiting for readiness failed\n");
877 return ret;
881 * If HISR.INT2_STS interrupt status bit is set then clear it.
883 hisr = mei_txe_br_reg_read(hw, HISR_REG);
884 if (hisr & HISR_INT_2_STS)
885 mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_2_STS);
887 /* Clear the interrupt cause of OutputDoorbell */
888 clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause);
890 ret = mei_txe_aliveness_set_sync(dev, 1);
891 if (ret < 0) {
892 dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
893 return ret;
896 /* enable input ready interrupts:
897 * SEC_IPC_HOST_INT_MASK.IPC_INPUT_READY_INT_MASK
899 mei_txe_input_ready_interrupt_enable(dev);
902 /* Set the SICR_SEC_IPC_OUTPUT_STATUS.IPC_OUTPUT_READY bit */
903 mei_txe_output_ready_set(hw);
905 /* Set bit SICR_HOST_IPC_READINESS.HOST_RDY
907 mei_txe_readiness_set_host_rdy(dev);
909 return 0;
913 * mei_txe_check_and_ack_intrs - translate multi BAR interrupt into
914 * single bit mask and acknowledge the interrupts
916 * @dev: the device structure
917 * @do_ack: acknowledge interrupts
919 static bool mei_txe_check_and_ack_intrs(struct mei_device *dev, bool do_ack)
921 struct mei_txe_hw *hw = to_txe_hw(dev);
922 u32 hisr;
923 u32 hhisr;
924 u32 ipc_isr;
925 u32 aliveness;
926 bool generated;
928 /* read interrupt registers */
929 hhisr = mei_txe_br_reg_read(hw, HHISR_REG);
930 generated = (hhisr & IPC_HHIER_MSK);
931 if (!generated)
932 goto out;
934 hisr = mei_txe_br_reg_read(hw, HISR_REG);
936 aliveness = mei_txe_aliveness_get(dev);
937 if (hhisr & IPC_HHIER_SEC && aliveness)
938 ipc_isr = mei_txe_sec_reg_read_silent(hw,
939 SEC_IPC_HOST_INT_STATUS_REG);
940 else
941 ipc_isr = 0;
943 generated = generated ||
944 (hisr & HISR_INT_STS_MSK) ||
945 (ipc_isr & SEC_IPC_HOST_INT_STATUS_PENDING);
947 if (generated && do_ack) {
948 /* Save the interrupt causes */
949 hw->intr_cause |= hisr & HISR_INT_STS_MSK;
950 if (ipc_isr & SEC_IPC_HOST_INT_STATUS_IN_RDY)
951 hw->intr_cause |= TXE_INTR_IN_READY;
954 mei_txe_intr_disable(dev);
955 /* Clear the interrupts in hierarchy:
956 * IPC and Bridge, than the High Level */
957 mei_txe_sec_reg_write_silent(hw,
958 SEC_IPC_HOST_INT_STATUS_REG, ipc_isr);
959 mei_txe_br_reg_write(hw, HISR_REG, hisr);
960 mei_txe_br_reg_write(hw, HHISR_REG, hhisr);
963 out:
964 return generated;
968 * mei_txe_irq_quick_handler - The ISR of the MEI device
970 * @irq: The irq number
971 * @dev_id: pointer to the device structure
973 * Return: IRQ_WAKE_THREAD if interrupt is designed for the device
974 * IRQ_NONE otherwise
976 irqreturn_t mei_txe_irq_quick_handler(int irq, void *dev_id)
978 struct mei_device *dev = dev_id;
980 if (mei_txe_check_and_ack_intrs(dev, true))
981 return IRQ_WAKE_THREAD;
982 return IRQ_NONE;
987 * mei_txe_irq_thread_handler - txe interrupt thread
989 * @irq: The irq number
990 * @dev_id: pointer to the device structure
992 * Return: IRQ_HANDLED
995 irqreturn_t mei_txe_irq_thread_handler(int irq, void *dev_id)
997 struct mei_device *dev = (struct mei_device *) dev_id;
998 struct mei_txe_hw *hw = to_txe_hw(dev);
999 struct mei_cl_cb complete_list;
1000 s32 slots;
1001 int rets = 0;
1003 dev_dbg(dev->dev, "irq thread: Interrupt Registers HHISR|HISR|SEC=%02X|%04X|%02X\n",
1004 mei_txe_br_reg_read(hw, HHISR_REG),
1005 mei_txe_br_reg_read(hw, HISR_REG),
1006 mei_txe_sec_reg_read_silent(hw, SEC_IPC_HOST_INT_STATUS_REG));
1009 /* initialize our complete list */
1010 mutex_lock(&dev->device_lock);
1011 mei_io_list_init(&complete_list);
1013 if (pci_dev_msi_enabled(to_pci_dev(dev->dev)))
1014 mei_txe_check_and_ack_intrs(dev, true);
1016 /* show irq events */
1017 mei_txe_pending_interrupts(dev);
1019 hw->aliveness = mei_txe_aliveness_get(dev);
1020 hw->readiness = mei_txe_readiness_get(dev);
1022 /* Readiness:
1023 * Detection of TXE driver going through reset
1024 * or TXE driver resetting the HECI interface.
1026 if (test_and_clear_bit(TXE_INTR_READINESS_BIT, &hw->intr_cause)) {
1027 dev_dbg(dev->dev, "Readiness Interrupt was received...\n");
1029 /* Check if SeC is going through reset */
1030 if (mei_txe_readiness_is_sec_rdy(hw->readiness)) {
1031 dev_dbg(dev->dev, "we need to start the dev.\n");
1032 dev->recvd_hw_ready = true;
1033 } else {
1034 dev->recvd_hw_ready = false;
1035 if (dev->dev_state != MEI_DEV_RESETTING) {
1037 dev_warn(dev->dev, "FW not ready: resetting.\n");
1038 schedule_work(&dev->reset_work);
1039 goto end;
1043 wake_up(&dev->wait_hw_ready);
1046 /************************************************************/
1047 /* Check interrupt cause:
1048 * Aliveness: Detection of SeC acknowledge of host request that
1049 * it remain alive or host cancellation of that request.
1052 if (test_and_clear_bit(TXE_INTR_ALIVENESS_BIT, &hw->intr_cause)) {
1053 /* Clear the interrupt cause */
1054 dev_dbg(dev->dev,
1055 "Aliveness Interrupt: Status: %d\n", hw->aliveness);
1056 dev->pg_event = MEI_PG_EVENT_RECEIVED;
1057 if (waitqueue_active(&hw->wait_aliveness_resp))
1058 wake_up(&hw->wait_aliveness_resp);
1062 /* Output Doorbell:
1063 * Detection of SeC having sent output to host
1065 slots = mei_count_full_read_slots(dev);
1066 if (test_and_clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause)) {
1067 /* Read from TXE */
1068 rets = mei_irq_read_handler(dev, &complete_list, &slots);
1069 if (rets && dev->dev_state != MEI_DEV_RESETTING) {
1070 dev_err(dev->dev,
1071 "mei_irq_read_handler ret = %d.\n", rets);
1073 schedule_work(&dev->reset_work);
1074 goto end;
1077 /* Input Ready: Detection if host can write to SeC */
1078 if (test_and_clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause)) {
1079 dev->hbuf_is_ready = true;
1080 hw->slots = dev->hbuf_depth;
1083 if (hw->aliveness && dev->hbuf_is_ready) {
1084 /* get the real register value */
1085 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
1086 rets = mei_irq_write_handler(dev, &complete_list);
1087 if (rets && rets != -EMSGSIZE)
1088 dev_err(dev->dev, "mei_irq_write_handler ret = %d.\n",
1089 rets);
1090 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
1093 mei_irq_compl_handler(dev, &complete_list);
1095 end:
1096 dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
1098 mutex_unlock(&dev->device_lock);
1100 mei_enable_interrupts(dev);
1101 return IRQ_HANDLED;
1104 static const struct mei_hw_ops mei_txe_hw_ops = {
1106 .host_is_ready = mei_txe_host_is_ready,
1108 .fw_status = mei_txe_fw_status,
1109 .pg_state = mei_txe_pg_state,
1111 .hw_is_ready = mei_txe_hw_is_ready,
1112 .hw_reset = mei_txe_hw_reset,
1113 .hw_config = mei_txe_hw_config,
1114 .hw_start = mei_txe_hw_start,
1116 .pg_is_enabled = mei_txe_pg_is_enabled,
1118 .intr_clear = mei_txe_intr_clear,
1119 .intr_enable = mei_txe_intr_enable,
1120 .intr_disable = mei_txe_intr_disable,
1122 .hbuf_free_slots = mei_txe_hbuf_empty_slots,
1123 .hbuf_is_ready = mei_txe_is_input_ready,
1124 .hbuf_max_len = mei_txe_hbuf_max_len,
1126 .write = mei_txe_write,
1128 .rdbuf_full_slots = mei_txe_count_full_read_slots,
1129 .read_hdr = mei_txe_read_hdr,
1131 .read = mei_txe_read,
1136 * mei_txe_dev_init - allocates and initializes txe hardware specific structure
1138 * @pdev - pci device
1140 * Return: struct mei_device * on success or NULL;
1143 struct mei_device *mei_txe_dev_init(struct pci_dev *pdev)
1145 struct mei_device *dev;
1146 struct mei_txe_hw *hw;
1148 dev = kzalloc(sizeof(struct mei_device) +
1149 sizeof(struct mei_txe_hw), GFP_KERNEL);
1150 if (!dev)
1151 return NULL;
1153 mei_device_init(dev, &pdev->dev, &mei_txe_hw_ops);
1155 hw = to_txe_hw(dev);
1157 init_waitqueue_head(&hw->wait_aliveness_resp);
1159 return dev;
1163 * mei_txe_setup_satt2 - SATT2 configuration for DMA support.
1165 * @dev: the device structure
1166 * @addr: physical address start of the range
1167 * @range: physical range size
1169 int mei_txe_setup_satt2(struct mei_device *dev, phys_addr_t addr, u32 range)
1171 struct mei_txe_hw *hw = to_txe_hw(dev);
1173 u32 lo32 = lower_32_bits(addr);
1174 u32 hi32 = upper_32_bits(addr);
1175 u32 ctrl;
1177 /* SATT is limited to 36 Bits */
1178 if (hi32 & ~0xF)
1179 return -EINVAL;
1181 /* SATT has to be 16Byte aligned */
1182 if (lo32 & 0xF)
1183 return -EINVAL;
1185 /* SATT range has to be 4Bytes aligned */
1186 if (range & 0x4)
1187 return -EINVAL;
1189 /* SATT is limited to 32 MB range*/
1190 if (range > SATT_RANGE_MAX)
1191 return -EINVAL;
1193 ctrl = SATT2_CTRL_VALID_MSK;
1194 ctrl |= hi32 << SATT2_CTRL_BR_BASE_ADDR_REG_SHIFT;
1196 mei_txe_br_reg_write(hw, SATT2_SAP_SIZE_REG, range);
1197 mei_txe_br_reg_write(hw, SATT2_BRG_BA_LSB_REG, lo32);
1198 mei_txe_br_reg_write(hw, SATT2_CTRL_REG, ctrl);
1199 dev_dbg(dev->dev, "SATT2: SAP_SIZE_OFFSET=0x%08X, BRG_BA_LSB_OFFSET=0x%08X, CTRL_OFFSET=0x%08X\n",
1200 range, lo32, ctrl);
1202 return 0;