xsk: Add overflow check for u64 division, stored into u32
[linux/fpc-iii.git] / drivers / misc / mei / hw-txe.c
blob8449fe0367ff4d65b7b06905490dc2ab3090ac4e
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/ktime.h>
20 #include <linux/delay.h>
21 #include <linux/kthread.h>
22 #include <linux/interrupt.h>
23 #include <linux/pm_runtime.h>
25 #include <linux/mei.h>
27 #include "mei_dev.h"
28 #include "hw-txe.h"
29 #include "client.h"
30 #include "hbm.h"
32 #include "mei-trace.h"
34 #define TXE_HBUF_DEPTH (PAYLOAD_SIZE / MEI_SLOT_SIZE)
36 /**
37 * mei_txe_reg_read - Reads 32bit data from the txe device
39 * @base_addr: registers base address
40 * @offset: register offset
42 * Return: register value
44 static inline u32 mei_txe_reg_read(void __iomem *base_addr,
45 unsigned long offset)
47 return ioread32(base_addr + offset);
50 /**
51 * mei_txe_reg_write - Writes 32bit data to the txe device
53 * @base_addr: registers base address
54 * @offset: register offset
55 * @value: the value to write
57 static inline void mei_txe_reg_write(void __iomem *base_addr,
58 unsigned long offset, u32 value)
60 iowrite32(value, base_addr + offset);
63 /**
64 * mei_txe_sec_reg_read_silent - Reads 32bit data from the SeC BAR
66 * @hw: the txe hardware structure
67 * @offset: register offset
69 * Doesn't check for aliveness while Reads 32bit data from the SeC BAR
71 * Return: register value
73 static inline u32 mei_txe_sec_reg_read_silent(struct mei_txe_hw *hw,
74 unsigned long offset)
76 return mei_txe_reg_read(hw->mem_addr[SEC_BAR], offset);
79 /**
80 * mei_txe_sec_reg_read - Reads 32bit data from the SeC BAR
82 * @hw: the txe hardware structure
83 * @offset: register offset
85 * Reads 32bit data from the SeC BAR and shout loud if aliveness is not set
87 * Return: register value
89 static inline u32 mei_txe_sec_reg_read(struct mei_txe_hw *hw,
90 unsigned long offset)
92 WARN(!hw->aliveness, "sec read: aliveness not asserted\n");
93 return mei_txe_sec_reg_read_silent(hw, offset);
95 /**
96 * mei_txe_sec_reg_write_silent - Writes 32bit data to the SeC BAR
97 * doesn't check for aliveness
99 * @hw: the txe hardware structure
100 * @offset: register offset
101 * @value: value to write
103 * Doesn't check for aliveness while writes 32bit data from to the SeC BAR
105 static inline void mei_txe_sec_reg_write_silent(struct mei_txe_hw *hw,
106 unsigned long offset, u32 value)
108 mei_txe_reg_write(hw->mem_addr[SEC_BAR], offset, value);
112 * mei_txe_sec_reg_write - Writes 32bit data to the SeC BAR
114 * @hw: the txe hardware structure
115 * @offset: register offset
116 * @value: value to write
118 * Writes 32bit data from the SeC BAR and shout loud if aliveness is not set
120 static inline void mei_txe_sec_reg_write(struct mei_txe_hw *hw,
121 unsigned long offset, u32 value)
123 WARN(!hw->aliveness, "sec write: aliveness not asserted\n");
124 mei_txe_sec_reg_write_silent(hw, offset, value);
127 * mei_txe_br_reg_read - Reads 32bit data from the Bridge BAR
129 * @hw: the txe hardware structure
130 * @offset: offset from which to read the data
132 * Return: the byte read.
134 static inline u32 mei_txe_br_reg_read(struct mei_txe_hw *hw,
135 unsigned long offset)
137 return mei_txe_reg_read(hw->mem_addr[BRIDGE_BAR], offset);
141 * mei_txe_br_reg_write - Writes 32bit data to the Bridge BAR
143 * @hw: the txe hardware structure
144 * @offset: offset from which to write the data
145 * @value: the byte to write
147 static inline void mei_txe_br_reg_write(struct mei_txe_hw *hw,
148 unsigned long offset, u32 value)
150 mei_txe_reg_write(hw->mem_addr[BRIDGE_BAR], offset, value);
154 * mei_txe_aliveness_set - request for aliveness change
156 * @dev: the device structure
157 * @req: requested aliveness value
159 * Request for aliveness change and returns true if the change is
160 * really needed and false if aliveness is already
161 * in the requested state
163 * Locking: called under "dev->device_lock" lock
165 * Return: true if request was send
167 static bool mei_txe_aliveness_set(struct mei_device *dev, u32 req)
170 struct mei_txe_hw *hw = to_txe_hw(dev);
171 bool do_req = hw->aliveness != req;
173 dev_dbg(dev->dev, "Aliveness current=%d request=%d\n",
174 hw->aliveness, req);
175 if (do_req) {
176 dev->pg_event = MEI_PG_EVENT_WAIT;
177 mei_txe_br_reg_write(hw, SICR_HOST_ALIVENESS_REQ_REG, req);
179 return do_req;
184 * mei_txe_aliveness_req_get - get aliveness requested register value
186 * @dev: the device structure
188 * Extract HICR_HOST_ALIVENESS_RESP_ACK bit from
189 * from HICR_HOST_ALIVENESS_REQ register value
191 * Return: SICR_HOST_ALIVENESS_REQ_REQUESTED bit value
193 static u32 mei_txe_aliveness_req_get(struct mei_device *dev)
195 struct mei_txe_hw *hw = to_txe_hw(dev);
196 u32 reg;
198 reg = mei_txe_br_reg_read(hw, SICR_HOST_ALIVENESS_REQ_REG);
199 return reg & SICR_HOST_ALIVENESS_REQ_REQUESTED;
203 * mei_txe_aliveness_get - get aliveness response register value
205 * @dev: the device structure
207 * Return: HICR_HOST_ALIVENESS_RESP_ACK bit from HICR_HOST_ALIVENESS_RESP
208 * register
210 static u32 mei_txe_aliveness_get(struct mei_device *dev)
212 struct mei_txe_hw *hw = to_txe_hw(dev);
213 u32 reg;
215 reg = mei_txe_br_reg_read(hw, HICR_HOST_ALIVENESS_RESP_REG);
216 return reg & HICR_HOST_ALIVENESS_RESP_ACK;
220 * mei_txe_aliveness_poll - waits for aliveness to settle
222 * @dev: the device structure
223 * @expected: expected aliveness value
225 * Polls for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
227 * Return: 0 if the expected value was received, -ETIME otherwise
229 static int mei_txe_aliveness_poll(struct mei_device *dev, u32 expected)
231 struct mei_txe_hw *hw = to_txe_hw(dev);
232 ktime_t stop, start;
234 start = ktime_get();
235 stop = ktime_add(start, ms_to_ktime(SEC_ALIVENESS_WAIT_TIMEOUT));
236 do {
237 hw->aliveness = mei_txe_aliveness_get(dev);
238 if (hw->aliveness == expected) {
239 dev->pg_event = MEI_PG_EVENT_IDLE;
240 dev_dbg(dev->dev, "aliveness settled after %lld usecs\n",
241 ktime_to_us(ktime_sub(ktime_get(), start)));
242 return 0;
244 usleep_range(20, 50);
245 } while (ktime_before(ktime_get(), stop));
247 dev->pg_event = MEI_PG_EVENT_IDLE;
248 dev_err(dev->dev, "aliveness timed out\n");
249 return -ETIME;
253 * mei_txe_aliveness_wait - waits for aliveness to settle
255 * @dev: the device structure
256 * @expected: expected aliveness value
258 * Waits for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
260 * Return: 0 on success and < 0 otherwise
262 static int mei_txe_aliveness_wait(struct mei_device *dev, u32 expected)
264 struct mei_txe_hw *hw = to_txe_hw(dev);
265 const unsigned long timeout =
266 msecs_to_jiffies(SEC_ALIVENESS_WAIT_TIMEOUT);
267 long err;
268 int ret;
270 hw->aliveness = mei_txe_aliveness_get(dev);
271 if (hw->aliveness == expected)
272 return 0;
274 mutex_unlock(&dev->device_lock);
275 err = wait_event_timeout(hw->wait_aliveness_resp,
276 dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
277 mutex_lock(&dev->device_lock);
279 hw->aliveness = mei_txe_aliveness_get(dev);
280 ret = hw->aliveness == expected ? 0 : -ETIME;
282 if (ret)
283 dev_warn(dev->dev, "aliveness timed out = %ld aliveness = %d event = %d\n",
284 err, hw->aliveness, dev->pg_event);
285 else
286 dev_dbg(dev->dev, "aliveness settled after = %d msec aliveness = %d event = %d\n",
287 jiffies_to_msecs(timeout - err),
288 hw->aliveness, dev->pg_event);
290 dev->pg_event = MEI_PG_EVENT_IDLE;
291 return ret;
295 * mei_txe_aliveness_set_sync - sets an wait for aliveness to complete
297 * @dev: the device structure
298 * @req: requested aliveness value
300 * Return: 0 on success and < 0 otherwise
302 int mei_txe_aliveness_set_sync(struct mei_device *dev, u32 req)
304 if (mei_txe_aliveness_set(dev, req))
305 return mei_txe_aliveness_wait(dev, req);
306 return 0;
310 * mei_txe_pg_in_transition - is device now in pg transition
312 * @dev: the device structure
314 * Return: true if in pg transition, false otherwise
316 static bool mei_txe_pg_in_transition(struct mei_device *dev)
318 return dev->pg_event == MEI_PG_EVENT_WAIT;
322 * mei_txe_pg_is_enabled - detect if PG is supported by HW
324 * @dev: the device structure
326 * Return: true is pg supported, false otherwise
328 static bool mei_txe_pg_is_enabled(struct mei_device *dev)
330 return true;
334 * mei_txe_pg_state - translate aliveness register value
335 * to the mei power gating state
337 * @dev: the device structure
339 * Return: MEI_PG_OFF if aliveness is on and MEI_PG_ON otherwise
341 static inline enum mei_pg_state mei_txe_pg_state(struct mei_device *dev)
343 struct mei_txe_hw *hw = to_txe_hw(dev);
345 return hw->aliveness ? MEI_PG_OFF : MEI_PG_ON;
349 * mei_txe_input_ready_interrupt_enable - sets the Input Ready Interrupt
351 * @dev: the device structure
353 static void mei_txe_input_ready_interrupt_enable(struct mei_device *dev)
355 struct mei_txe_hw *hw = to_txe_hw(dev);
356 u32 hintmsk;
357 /* Enable the SEC_IPC_HOST_INT_MASK_IN_RDY interrupt */
358 hintmsk = mei_txe_sec_reg_read(hw, SEC_IPC_HOST_INT_MASK_REG);
359 hintmsk |= SEC_IPC_HOST_INT_MASK_IN_RDY;
360 mei_txe_sec_reg_write(hw, SEC_IPC_HOST_INT_MASK_REG, hintmsk);
364 * mei_txe_input_doorbell_set - sets bit 0 in
365 * SEC_IPC_INPUT_DOORBELL.IPC_INPUT_DOORBELL.
367 * @hw: the txe hardware structure
369 static void mei_txe_input_doorbell_set(struct mei_txe_hw *hw)
371 /* Clear the interrupt cause */
372 clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause);
373 mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_DOORBELL_REG, 1);
377 * mei_txe_output_ready_set - Sets the SICR_SEC_IPC_OUTPUT_STATUS bit to 1
379 * @hw: the txe hardware structure
381 static void mei_txe_output_ready_set(struct mei_txe_hw *hw)
383 mei_txe_br_reg_write(hw,
384 SICR_SEC_IPC_OUTPUT_STATUS_REG,
385 SEC_IPC_OUTPUT_STATUS_RDY);
389 * mei_txe_is_input_ready - check if TXE is ready for receiving data
391 * @dev: the device structure
393 * Return: true if INPUT STATUS READY bit is set
395 static bool mei_txe_is_input_ready(struct mei_device *dev)
397 struct mei_txe_hw *hw = to_txe_hw(dev);
398 u32 status;
400 status = mei_txe_sec_reg_read(hw, SEC_IPC_INPUT_STATUS_REG);
401 return !!(SEC_IPC_INPUT_STATUS_RDY & status);
405 * mei_txe_intr_clear - clear all interrupts
407 * @dev: the device structure
409 static inline void mei_txe_intr_clear(struct mei_device *dev)
411 struct mei_txe_hw *hw = to_txe_hw(dev);
413 mei_txe_sec_reg_write_silent(hw, SEC_IPC_HOST_INT_STATUS_REG,
414 SEC_IPC_HOST_INT_STATUS_PENDING);
415 mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_STS_MSK);
416 mei_txe_br_reg_write(hw, HHISR_REG, IPC_HHIER_MSK);
420 * mei_txe_intr_disable - disable all interrupts
422 * @dev: the device structure
424 static void mei_txe_intr_disable(struct mei_device *dev)
426 struct mei_txe_hw *hw = to_txe_hw(dev);
428 mei_txe_br_reg_write(hw, HHIER_REG, 0);
429 mei_txe_br_reg_write(hw, HIER_REG, 0);
432 * mei_txe_intr_enable - enable all interrupts
434 * @dev: the device structure
436 static void mei_txe_intr_enable(struct mei_device *dev)
438 struct mei_txe_hw *hw = to_txe_hw(dev);
440 mei_txe_br_reg_write(hw, HHIER_REG, IPC_HHIER_MSK);
441 mei_txe_br_reg_write(hw, HIER_REG, HIER_INT_EN_MSK);
445 * mei_txe_synchronize_irq - wait for pending IRQ handlers
447 * @dev: the device structure
449 static void mei_txe_synchronize_irq(struct mei_device *dev)
451 struct pci_dev *pdev = to_pci_dev(dev->dev);
453 synchronize_irq(pdev->irq);
457 * mei_txe_pending_interrupts - check if there are pending interrupts
458 * only Aliveness, Input ready, and output doorbell are of relevance
460 * @dev: the device structure
462 * Checks if there are pending interrupts
463 * only Aliveness, Readiness, Input ready, and Output doorbell are relevant
465 * Return: true if there are pending interrupts
467 static bool mei_txe_pending_interrupts(struct mei_device *dev)
470 struct mei_txe_hw *hw = to_txe_hw(dev);
471 bool ret = (hw->intr_cause & (TXE_INTR_READINESS |
472 TXE_INTR_ALIVENESS |
473 TXE_INTR_IN_READY |
474 TXE_INTR_OUT_DB));
476 if (ret) {
477 dev_dbg(dev->dev,
478 "Pending Interrupts InReady=%01d Readiness=%01d, Aliveness=%01d, OutDoor=%01d\n",
479 !!(hw->intr_cause & TXE_INTR_IN_READY),
480 !!(hw->intr_cause & TXE_INTR_READINESS),
481 !!(hw->intr_cause & TXE_INTR_ALIVENESS),
482 !!(hw->intr_cause & TXE_INTR_OUT_DB));
484 return ret;
488 * mei_txe_input_payload_write - write a dword to the host buffer
489 * at offset idx
491 * @dev: the device structure
492 * @idx: index in the host buffer
493 * @value: value
495 static void mei_txe_input_payload_write(struct mei_device *dev,
496 unsigned long idx, u32 value)
498 struct mei_txe_hw *hw = to_txe_hw(dev);
500 mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_PAYLOAD_REG +
501 (idx * sizeof(u32)), value);
505 * mei_txe_out_data_read - read dword from the device buffer
506 * at offset idx
508 * @dev: the device structure
509 * @idx: index in the device buffer
511 * Return: register value at index
513 static u32 mei_txe_out_data_read(const struct mei_device *dev,
514 unsigned long idx)
516 struct mei_txe_hw *hw = to_txe_hw(dev);
518 return mei_txe_br_reg_read(hw,
519 BRIDGE_IPC_OUTPUT_PAYLOAD_REG + (idx * sizeof(u32)));
522 /* Readiness */
525 * mei_txe_readiness_set_host_rdy - set host readiness bit
527 * @dev: the device structure
529 static void mei_txe_readiness_set_host_rdy(struct mei_device *dev)
531 struct mei_txe_hw *hw = to_txe_hw(dev);
533 mei_txe_br_reg_write(hw,
534 SICR_HOST_IPC_READINESS_REQ_REG,
535 SICR_HOST_IPC_READINESS_HOST_RDY);
539 * mei_txe_readiness_clear - clear host readiness bit
541 * @dev: the device structure
543 static void mei_txe_readiness_clear(struct mei_device *dev)
545 struct mei_txe_hw *hw = to_txe_hw(dev);
547 mei_txe_br_reg_write(hw, SICR_HOST_IPC_READINESS_REQ_REG,
548 SICR_HOST_IPC_READINESS_RDY_CLR);
551 * mei_txe_readiness_get - Reads and returns
552 * the HICR_SEC_IPC_READINESS register value
554 * @dev: the device structure
556 * Return: the HICR_SEC_IPC_READINESS register value
558 static u32 mei_txe_readiness_get(struct mei_device *dev)
560 struct mei_txe_hw *hw = to_txe_hw(dev);
562 return mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
567 * mei_txe_readiness_is_sec_rdy - check readiness
568 * for HICR_SEC_IPC_READINESS_SEC_RDY
570 * @readiness: cached readiness state
572 * Return: true if readiness bit is set
574 static inline bool mei_txe_readiness_is_sec_rdy(u32 readiness)
576 return !!(readiness & HICR_SEC_IPC_READINESS_SEC_RDY);
580 * mei_txe_hw_is_ready - check if the hw is ready
582 * @dev: the device structure
584 * Return: true if sec is ready
586 static bool mei_txe_hw_is_ready(struct mei_device *dev)
588 u32 readiness = mei_txe_readiness_get(dev);
590 return mei_txe_readiness_is_sec_rdy(readiness);
594 * mei_txe_host_is_ready - check if the host is ready
596 * @dev: the device structure
598 * Return: true if host is ready
600 static inline bool mei_txe_host_is_ready(struct mei_device *dev)
602 struct mei_txe_hw *hw = to_txe_hw(dev);
603 u32 reg = mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
605 return !!(reg & HICR_SEC_IPC_READINESS_HOST_RDY);
609 * mei_txe_readiness_wait - wait till readiness settles
611 * @dev: the device structure
613 * Return: 0 on success and -ETIME on timeout
615 static int mei_txe_readiness_wait(struct mei_device *dev)
617 if (mei_txe_hw_is_ready(dev))
618 return 0;
620 mutex_unlock(&dev->device_lock);
621 wait_event_timeout(dev->wait_hw_ready, dev->recvd_hw_ready,
622 msecs_to_jiffies(SEC_RESET_WAIT_TIMEOUT));
623 mutex_lock(&dev->device_lock);
624 if (!dev->recvd_hw_ready) {
625 dev_err(dev->dev, "wait for readiness failed\n");
626 return -ETIME;
629 dev->recvd_hw_ready = false;
630 return 0;
633 static const struct mei_fw_status mei_txe_fw_sts = {
634 .count = 2,
635 .status[0] = PCI_CFG_TXE_FW_STS0,
636 .status[1] = PCI_CFG_TXE_FW_STS1
640 * mei_txe_fw_status - read fw status register from pci config space
642 * @dev: mei device
643 * @fw_status: fw status register values
645 * Return: 0 on success, error otherwise
647 static int mei_txe_fw_status(struct mei_device *dev,
648 struct mei_fw_status *fw_status)
650 const struct mei_fw_status *fw_src = &mei_txe_fw_sts;
651 struct pci_dev *pdev = to_pci_dev(dev->dev);
652 int ret;
653 int i;
655 if (!fw_status)
656 return -EINVAL;
658 fw_status->count = fw_src->count;
659 for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
660 ret = pci_read_config_dword(pdev, fw_src->status[i],
661 &fw_status->status[i]);
662 trace_mei_pci_cfg_read(dev->dev, "PCI_CFG_HSF_X",
663 fw_src->status[i],
664 fw_status->status[i]);
665 if (ret)
666 return ret;
669 return 0;
673 * mei_txe_hw_config - configure hardware at the start of the devices
675 * @dev: the device structure
677 * Configure hardware at the start of the device should be done only
678 * once at the device probe time
680 static void mei_txe_hw_config(struct mei_device *dev)
683 struct mei_txe_hw *hw = to_txe_hw(dev);
685 hw->aliveness = mei_txe_aliveness_get(dev);
686 hw->readiness = mei_txe_readiness_get(dev);
688 dev_dbg(dev->dev, "aliveness_resp = 0x%08x, readiness = 0x%08x.\n",
689 hw->aliveness, hw->readiness);
693 * mei_txe_write - writes a message to device.
695 * @dev: the device structure
696 * @hdr: header of message
697 * @hdr_len: header length in bytes - must multiplication of a slot (4bytes)
698 * @data: payload
699 * @data_len: paylead length in bytes
701 * Return: 0 if success, < 0 - otherwise.
703 static int mei_txe_write(struct mei_device *dev,
704 const void *hdr, size_t hdr_len,
705 const void *data, size_t data_len)
707 struct mei_txe_hw *hw = to_txe_hw(dev);
708 unsigned long rem;
709 const u32 *reg_buf;
710 u32 slots = TXE_HBUF_DEPTH;
711 u32 dw_cnt;
712 unsigned long i, j;
714 if (WARN_ON(!hdr || !data || hdr_len & 0x3))
715 return -EINVAL;
717 dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM((struct mei_msg_hdr *)hdr));
719 dw_cnt = mei_data2slots(hdr_len + data_len);
720 if (dw_cnt > slots)
721 return -EMSGSIZE;
723 if (WARN(!hw->aliveness, "txe write: aliveness not asserted\n"))
724 return -EAGAIN;
726 /* Enable Input Ready Interrupt. */
727 mei_txe_input_ready_interrupt_enable(dev);
729 if (!mei_txe_is_input_ready(dev)) {
730 char fw_sts_str[MEI_FW_STATUS_STR_SZ];
732 mei_fw_status_str(dev, fw_sts_str, MEI_FW_STATUS_STR_SZ);
733 dev_err(dev->dev, "Input is not ready %s\n", fw_sts_str);
734 return -EAGAIN;
737 reg_buf = hdr;
738 for (i = 0; i < hdr_len / MEI_SLOT_SIZE; i++)
739 mei_txe_input_payload_write(dev, i, reg_buf[i]);
741 reg_buf = data;
742 for (j = 0; j < data_len / MEI_SLOT_SIZE; j++)
743 mei_txe_input_payload_write(dev, i + j, reg_buf[j]);
745 rem = data_len & 0x3;
746 if (rem > 0) {
747 u32 reg = 0;
749 memcpy(&reg, (const u8 *)data + data_len - rem, rem);
750 mei_txe_input_payload_write(dev, i + j, reg);
753 /* after each write the whole buffer is consumed */
754 hw->slots = 0;
756 /* Set Input-Doorbell */
757 mei_txe_input_doorbell_set(hw);
759 return 0;
763 * mei_txe_hbuf_depth - mimics the me hbuf circular buffer
765 * @dev: the device structure
767 * Return: the TXE_HBUF_DEPTH
769 static u32 mei_txe_hbuf_depth(const struct mei_device *dev)
771 return TXE_HBUF_DEPTH;
775 * mei_txe_hbuf_empty_slots - mimics the me hbuf circular buffer
777 * @dev: the device structure
779 * Return: always TXE_HBUF_DEPTH
781 static int mei_txe_hbuf_empty_slots(struct mei_device *dev)
783 struct mei_txe_hw *hw = to_txe_hw(dev);
785 return hw->slots;
789 * mei_txe_count_full_read_slots - mimics the me device circular buffer
791 * @dev: the device structure
793 * Return: always buffer size in dwords count
795 static int mei_txe_count_full_read_slots(struct mei_device *dev)
797 /* read buffers has static size */
798 return TXE_HBUF_DEPTH;
802 * mei_txe_read_hdr - read message header which is always in 4 first bytes
804 * @dev: the device structure
806 * Return: mei message header
809 static u32 mei_txe_read_hdr(const struct mei_device *dev)
811 return mei_txe_out_data_read(dev, 0);
814 * mei_txe_read - reads a message from the txe device.
816 * @dev: the device structure
817 * @buf: message buffer will be written
818 * @len: message size will be read
820 * Return: -EINVAL on error wrong argument and 0 on success
822 static int mei_txe_read(struct mei_device *dev,
823 unsigned char *buf, unsigned long len)
826 struct mei_txe_hw *hw = to_txe_hw(dev);
827 u32 *reg_buf, reg;
828 u32 rem;
829 u32 i;
831 if (WARN_ON(!buf || !len))
832 return -EINVAL;
834 reg_buf = (u32 *)buf;
835 rem = len & 0x3;
837 dev_dbg(dev->dev, "buffer-length = %lu buf[0]0x%08X\n",
838 len, mei_txe_out_data_read(dev, 0));
840 for (i = 0; i < len / MEI_SLOT_SIZE; i++) {
841 /* skip header: index starts from 1 */
842 reg = mei_txe_out_data_read(dev, i + 1);
843 dev_dbg(dev->dev, "buf[%d] = 0x%08X\n", i, reg);
844 *reg_buf++ = reg;
847 if (rem) {
848 reg = mei_txe_out_data_read(dev, i + 1);
849 memcpy(reg_buf, &reg, rem);
852 mei_txe_output_ready_set(hw);
853 return 0;
857 * mei_txe_hw_reset - resets host and fw.
859 * @dev: the device structure
860 * @intr_enable: if interrupt should be enabled after reset.
862 * Return: 0 on success and < 0 in case of error
864 static int mei_txe_hw_reset(struct mei_device *dev, bool intr_enable)
866 struct mei_txe_hw *hw = to_txe_hw(dev);
868 u32 aliveness_req;
870 * read input doorbell to ensure consistency between Bridge and SeC
871 * return value might be garbage return
873 (void)mei_txe_sec_reg_read_silent(hw, SEC_IPC_INPUT_DOORBELL_REG);
875 aliveness_req = mei_txe_aliveness_req_get(dev);
876 hw->aliveness = mei_txe_aliveness_get(dev);
878 /* Disable interrupts in this stage we will poll */
879 mei_txe_intr_disable(dev);
882 * If Aliveness Request and Aliveness Response are not equal then
883 * wait for them to be equal
884 * Since we might have interrupts disabled - poll for it
886 if (aliveness_req != hw->aliveness)
887 if (mei_txe_aliveness_poll(dev, aliveness_req) < 0) {
888 dev_err(dev->dev, "wait for aliveness settle failed ... bailing out\n");
889 return -EIO;
893 * If Aliveness Request and Aliveness Response are set then clear them
895 if (aliveness_req) {
896 mei_txe_aliveness_set(dev, 0);
897 if (mei_txe_aliveness_poll(dev, 0) < 0) {
898 dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
899 return -EIO;
904 * Set readiness RDY_CLR bit
906 mei_txe_readiness_clear(dev);
908 return 0;
912 * mei_txe_hw_start - start the hardware after reset
914 * @dev: the device structure
916 * Return: 0 on success an error code otherwise
918 static int mei_txe_hw_start(struct mei_device *dev)
920 struct mei_txe_hw *hw = to_txe_hw(dev);
921 int ret;
923 u32 hisr;
925 /* bring back interrupts */
926 mei_txe_intr_enable(dev);
928 ret = mei_txe_readiness_wait(dev);
929 if (ret < 0) {
930 dev_err(dev->dev, "waiting for readiness failed\n");
931 return ret;
935 * If HISR.INT2_STS interrupt status bit is set then clear it.
937 hisr = mei_txe_br_reg_read(hw, HISR_REG);
938 if (hisr & HISR_INT_2_STS)
939 mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_2_STS);
941 /* Clear the interrupt cause of OutputDoorbell */
942 clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause);
944 ret = mei_txe_aliveness_set_sync(dev, 1);
945 if (ret < 0) {
946 dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
947 return ret;
950 pm_runtime_set_active(dev->dev);
952 /* enable input ready interrupts:
953 * SEC_IPC_HOST_INT_MASK.IPC_INPUT_READY_INT_MASK
955 mei_txe_input_ready_interrupt_enable(dev);
958 /* Set the SICR_SEC_IPC_OUTPUT_STATUS.IPC_OUTPUT_READY bit */
959 mei_txe_output_ready_set(hw);
961 /* Set bit SICR_HOST_IPC_READINESS.HOST_RDY
963 mei_txe_readiness_set_host_rdy(dev);
965 return 0;
969 * mei_txe_check_and_ack_intrs - translate multi BAR interrupt into
970 * single bit mask and acknowledge the interrupts
972 * @dev: the device structure
973 * @do_ack: acknowledge interrupts
975 * Return: true if found interrupts to process.
977 static bool mei_txe_check_and_ack_intrs(struct mei_device *dev, bool do_ack)
979 struct mei_txe_hw *hw = to_txe_hw(dev);
980 u32 hisr;
981 u32 hhisr;
982 u32 ipc_isr;
983 u32 aliveness;
984 bool generated;
986 /* read interrupt registers */
987 hhisr = mei_txe_br_reg_read(hw, HHISR_REG);
988 generated = (hhisr & IPC_HHIER_MSK);
989 if (!generated)
990 goto out;
992 hisr = mei_txe_br_reg_read(hw, HISR_REG);
994 aliveness = mei_txe_aliveness_get(dev);
995 if (hhisr & IPC_HHIER_SEC && aliveness) {
996 ipc_isr = mei_txe_sec_reg_read_silent(hw,
997 SEC_IPC_HOST_INT_STATUS_REG);
998 } else {
999 ipc_isr = 0;
1000 hhisr &= ~IPC_HHIER_SEC;
1003 generated = generated ||
1004 (hisr & HISR_INT_STS_MSK) ||
1005 (ipc_isr & SEC_IPC_HOST_INT_STATUS_PENDING);
1007 if (generated && do_ack) {
1008 /* Save the interrupt causes */
1009 hw->intr_cause |= hisr & HISR_INT_STS_MSK;
1010 if (ipc_isr & SEC_IPC_HOST_INT_STATUS_IN_RDY)
1011 hw->intr_cause |= TXE_INTR_IN_READY;
1014 mei_txe_intr_disable(dev);
1015 /* Clear the interrupts in hierarchy:
1016 * IPC and Bridge, than the High Level */
1017 mei_txe_sec_reg_write_silent(hw,
1018 SEC_IPC_HOST_INT_STATUS_REG, ipc_isr);
1019 mei_txe_br_reg_write(hw, HISR_REG, hisr);
1020 mei_txe_br_reg_write(hw, HHISR_REG, hhisr);
1023 out:
1024 return generated;
1028 * mei_txe_irq_quick_handler - The ISR of the MEI device
1030 * @irq: The irq number
1031 * @dev_id: pointer to the device structure
1033 * Return: IRQ_WAKE_THREAD if interrupt is designed for the device
1034 * IRQ_NONE otherwise
1036 irqreturn_t mei_txe_irq_quick_handler(int irq, void *dev_id)
1038 struct mei_device *dev = dev_id;
1040 if (mei_txe_check_and_ack_intrs(dev, true))
1041 return IRQ_WAKE_THREAD;
1042 return IRQ_NONE;
1047 * mei_txe_irq_thread_handler - txe interrupt thread
1049 * @irq: The irq number
1050 * @dev_id: pointer to the device structure
1052 * Return: IRQ_HANDLED
1054 irqreturn_t mei_txe_irq_thread_handler(int irq, void *dev_id)
1056 struct mei_device *dev = (struct mei_device *) dev_id;
1057 struct mei_txe_hw *hw = to_txe_hw(dev);
1058 struct list_head cmpl_list;
1059 s32 slots;
1060 int rets = 0;
1062 dev_dbg(dev->dev, "irq thread: Interrupt Registers HHISR|HISR|SEC=%02X|%04X|%02X\n",
1063 mei_txe_br_reg_read(hw, HHISR_REG),
1064 mei_txe_br_reg_read(hw, HISR_REG),
1065 mei_txe_sec_reg_read_silent(hw, SEC_IPC_HOST_INT_STATUS_REG));
1068 /* initialize our complete list */
1069 mutex_lock(&dev->device_lock);
1070 INIT_LIST_HEAD(&cmpl_list);
1072 if (pci_dev_msi_enabled(to_pci_dev(dev->dev)))
1073 mei_txe_check_and_ack_intrs(dev, true);
1075 /* show irq events */
1076 mei_txe_pending_interrupts(dev);
1078 hw->aliveness = mei_txe_aliveness_get(dev);
1079 hw->readiness = mei_txe_readiness_get(dev);
1081 /* Readiness:
1082 * Detection of TXE driver going through reset
1083 * or TXE driver resetting the HECI interface.
1085 if (test_and_clear_bit(TXE_INTR_READINESS_BIT, &hw->intr_cause)) {
1086 dev_dbg(dev->dev, "Readiness Interrupt was received...\n");
1088 /* Check if SeC is going through reset */
1089 if (mei_txe_readiness_is_sec_rdy(hw->readiness)) {
1090 dev_dbg(dev->dev, "we need to start the dev.\n");
1091 dev->recvd_hw_ready = true;
1092 } else {
1093 dev->recvd_hw_ready = false;
1094 if (dev->dev_state != MEI_DEV_RESETTING) {
1096 dev_warn(dev->dev, "FW not ready: resetting.\n");
1097 schedule_work(&dev->reset_work);
1098 goto end;
1102 wake_up(&dev->wait_hw_ready);
1105 /************************************************************/
1106 /* Check interrupt cause:
1107 * Aliveness: Detection of SeC acknowledge of host request that
1108 * it remain alive or host cancellation of that request.
1111 if (test_and_clear_bit(TXE_INTR_ALIVENESS_BIT, &hw->intr_cause)) {
1112 /* Clear the interrupt cause */
1113 dev_dbg(dev->dev,
1114 "Aliveness Interrupt: Status: %d\n", hw->aliveness);
1115 dev->pg_event = MEI_PG_EVENT_RECEIVED;
1116 if (waitqueue_active(&hw->wait_aliveness_resp))
1117 wake_up(&hw->wait_aliveness_resp);
1121 /* Output Doorbell:
1122 * Detection of SeC having sent output to host
1124 slots = mei_count_full_read_slots(dev);
1125 if (test_and_clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause)) {
1126 /* Read from TXE */
1127 rets = mei_irq_read_handler(dev, &cmpl_list, &slots);
1128 if (rets &&
1129 (dev->dev_state != MEI_DEV_RESETTING &&
1130 dev->dev_state != MEI_DEV_POWER_DOWN)) {
1131 dev_err(dev->dev,
1132 "mei_irq_read_handler ret = %d.\n", rets);
1134 schedule_work(&dev->reset_work);
1135 goto end;
1138 /* Input Ready: Detection if host can write to SeC */
1139 if (test_and_clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause)) {
1140 dev->hbuf_is_ready = true;
1141 hw->slots = TXE_HBUF_DEPTH;
1144 if (hw->aliveness && dev->hbuf_is_ready) {
1145 /* get the real register value */
1146 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
1147 rets = mei_irq_write_handler(dev, &cmpl_list);
1148 if (rets && rets != -EMSGSIZE)
1149 dev_err(dev->dev, "mei_irq_write_handler ret = %d.\n",
1150 rets);
1151 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
1154 mei_irq_compl_handler(dev, &cmpl_list);
1156 end:
1157 dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
1159 mutex_unlock(&dev->device_lock);
1161 mei_enable_interrupts(dev);
1162 return IRQ_HANDLED;
1165 static const struct mei_hw_ops mei_txe_hw_ops = {
1167 .host_is_ready = mei_txe_host_is_ready,
1169 .fw_status = mei_txe_fw_status,
1170 .pg_state = mei_txe_pg_state,
1172 .hw_is_ready = mei_txe_hw_is_ready,
1173 .hw_reset = mei_txe_hw_reset,
1174 .hw_config = mei_txe_hw_config,
1175 .hw_start = mei_txe_hw_start,
1177 .pg_in_transition = mei_txe_pg_in_transition,
1178 .pg_is_enabled = mei_txe_pg_is_enabled,
1180 .intr_clear = mei_txe_intr_clear,
1181 .intr_enable = mei_txe_intr_enable,
1182 .intr_disable = mei_txe_intr_disable,
1183 .synchronize_irq = mei_txe_synchronize_irq,
1185 .hbuf_free_slots = mei_txe_hbuf_empty_slots,
1186 .hbuf_is_ready = mei_txe_is_input_ready,
1187 .hbuf_depth = mei_txe_hbuf_depth,
1189 .write = mei_txe_write,
1191 .rdbuf_full_slots = mei_txe_count_full_read_slots,
1192 .read_hdr = mei_txe_read_hdr,
1194 .read = mei_txe_read,
1199 * mei_txe_dev_init - allocates and initializes txe hardware specific structure
1201 * @pdev: pci device
1203 * Return: struct mei_device * on success or NULL
1205 struct mei_device *mei_txe_dev_init(struct pci_dev *pdev)
1207 struct mei_device *dev;
1208 struct mei_txe_hw *hw;
1210 dev = devm_kzalloc(&pdev->dev, sizeof(struct mei_device) +
1211 sizeof(struct mei_txe_hw), GFP_KERNEL);
1212 if (!dev)
1213 return NULL;
1215 mei_device_init(dev, &pdev->dev, &mei_txe_hw_ops);
1217 hw = to_txe_hw(dev);
1219 init_waitqueue_head(&hw->wait_aliveness_resp);
1221 return dev;
1225 * mei_txe_setup_satt2 - SATT2 configuration for DMA support.
1227 * @dev: the device structure
1228 * @addr: physical address start of the range
1229 * @range: physical range size
1231 * Return: 0 on success an error code otherwise
1233 int mei_txe_setup_satt2(struct mei_device *dev, phys_addr_t addr, u32 range)
1235 struct mei_txe_hw *hw = to_txe_hw(dev);
1237 u32 lo32 = lower_32_bits(addr);
1238 u32 hi32 = upper_32_bits(addr);
1239 u32 ctrl;
1241 /* SATT is limited to 36 Bits */
1242 if (hi32 & ~0xF)
1243 return -EINVAL;
1245 /* SATT has to be 16Byte aligned */
1246 if (lo32 & 0xF)
1247 return -EINVAL;
1249 /* SATT range has to be 4Bytes aligned */
1250 if (range & 0x4)
1251 return -EINVAL;
1253 /* SATT is limited to 32 MB range*/
1254 if (range > SATT_RANGE_MAX)
1255 return -EINVAL;
1257 ctrl = SATT2_CTRL_VALID_MSK;
1258 ctrl |= hi32 << SATT2_CTRL_BR_BASE_ADDR_REG_SHIFT;
1260 mei_txe_br_reg_write(hw, SATT2_SAP_SIZE_REG, range);
1261 mei_txe_br_reg_write(hw, SATT2_BRG_BA_LSB_REG, lo32);
1262 mei_txe_br_reg_write(hw, SATT2_CTRL_REG, ctrl);
1263 dev_dbg(dev->dev, "SATT2: SAP_SIZE_OFFSET=0x%08X, BRG_BA_LSB_OFFSET=0x%08X, CTRL_OFFSET=0x%08X\n",
1264 range, lo32, ctrl);
1266 return 0;