3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, 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
17 #include <linux/pci.h>
19 #include <linux/kthread.h>
20 #include <linux/interrupt.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/sizes.h>
28 #include "hw-me-regs.h"
30 #include "mei-trace.h"
33 * mei_me_reg_read - Reads 32bit data from the mei device
35 * @hw: the me hardware structure
36 * @offset: offset from which to read the data
38 * Return: register value (u32)
40 static inline u32 mei_me_reg_read(const struct mei_me_hw *hw,
43 return ioread32(hw->mem_addr + offset);
48 * mei_me_reg_write - Writes 32bit data to the mei device
50 * @hw: the me hardware structure
51 * @offset: offset from which to write the data
52 * @value: register value to write (u32)
54 static inline void mei_me_reg_write(const struct mei_me_hw *hw,
55 unsigned long offset, u32 value)
57 iowrite32(value, hw->mem_addr + offset);
61 * mei_me_mecbrw_read - Reads 32bit data from ME circular buffer
62 * read window register
64 * @dev: the device structure
66 * Return: ME_CB_RW register value (u32)
68 static inline u32 mei_me_mecbrw_read(const struct mei_device *dev)
70 return mei_me_reg_read(to_me_hw(dev), ME_CB_RW);
74 * mei_me_hcbww_write - write 32bit data to the host circular buffer
76 * @dev: the device structure
77 * @data: 32bit data to be written to the host circular buffer
79 static inline void mei_me_hcbww_write(struct mei_device *dev, u32 data)
81 mei_me_reg_write(to_me_hw(dev), H_CB_WW, data);
85 * mei_me_mecsr_read - Reads 32bit data from the ME CSR
87 * @dev: the device structure
89 * Return: ME_CSR_HA register value (u32)
91 static inline u32 mei_me_mecsr_read(const struct mei_device *dev)
95 reg = mei_me_reg_read(to_me_hw(dev), ME_CSR_HA);
96 trace_mei_reg_read(dev->dev, "ME_CSR_HA", ME_CSR_HA, reg);
102 * mei_hcsr_read - Reads 32bit data from the host CSR
104 * @dev: the device structure
106 * Return: H_CSR register value (u32)
108 static inline u32 mei_hcsr_read(const struct mei_device *dev)
112 reg = mei_me_reg_read(to_me_hw(dev), H_CSR);
113 trace_mei_reg_read(dev->dev, "H_CSR", H_CSR, reg);
119 * mei_hcsr_write - writes H_CSR register to the mei device
121 * @dev: the device structure
122 * @reg: new register value
124 static inline void mei_hcsr_write(struct mei_device *dev, u32 reg)
126 trace_mei_reg_write(dev->dev, "H_CSR", H_CSR, reg);
127 mei_me_reg_write(to_me_hw(dev), H_CSR, reg);
131 * mei_hcsr_set - writes H_CSR register to the mei device,
132 * and ignores the H_IS bit for it is write-one-to-zero.
134 * @dev: the device structure
135 * @reg: new register value
137 static inline void mei_hcsr_set(struct mei_device *dev, u32 reg)
139 reg &= ~H_CSR_IS_MASK;
140 mei_hcsr_write(dev, reg);
144 * mei_hcsr_set_hig - set host interrupt (set H_IG)
146 * @dev: the device structure
148 static inline void mei_hcsr_set_hig(struct mei_device *dev)
152 hcsr = mei_hcsr_read(dev) | H_IG;
153 mei_hcsr_set(dev, hcsr);
157 * mei_me_d0i3c_read - Reads 32bit data from the D0I3C register
159 * @dev: the device structure
161 * Return: H_D0I3C register value (u32)
163 static inline u32 mei_me_d0i3c_read(const struct mei_device *dev)
167 reg = mei_me_reg_read(to_me_hw(dev), H_D0I3C);
168 trace_mei_reg_read(dev->dev, "H_D0I3C", H_D0I3C, reg);
174 * mei_me_d0i3c_write - writes H_D0I3C register to device
176 * @dev: the device structure
177 * @reg: new register value
179 static inline void mei_me_d0i3c_write(struct mei_device *dev, u32 reg)
181 trace_mei_reg_write(dev->dev, "H_D0I3C", H_D0I3C, reg);
182 mei_me_reg_write(to_me_hw(dev), H_D0I3C, reg);
186 * mei_me_fw_status - read fw status register from pci config space
189 * @fw_status: fw status register values
191 * Return: 0 on success, error otherwise
193 static int mei_me_fw_status(struct mei_device *dev,
194 struct mei_fw_status *fw_status)
196 struct pci_dev *pdev = to_pci_dev(dev->dev);
197 struct mei_me_hw *hw = to_me_hw(dev);
198 const struct mei_fw_status *fw_src = &hw->cfg->fw_status;
205 fw_status->count = fw_src->count;
206 for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
207 ret = pci_read_config_dword(pdev, fw_src->status[i],
208 &fw_status->status[i]);
209 trace_mei_pci_cfg_read(dev->dev, "PCI_CFG_HSF_X",
211 fw_status->status[i]);
220 * mei_me_hw_config - configure hw dependent settings
224 static void mei_me_hw_config(struct mei_device *dev)
226 struct pci_dev *pdev = to_pci_dev(dev->dev);
227 struct mei_me_hw *hw = to_me_hw(dev);
230 /* Doesn't change in runtime */
231 hcsr = mei_hcsr_read(dev);
232 hw->hbuf_depth = (hcsr & H_CBD) >> 24;
235 pci_read_config_dword(pdev, PCI_CFG_HFS_1, ®);
236 trace_mei_pci_cfg_read(dev->dev, "PCI_CFG_HFS_1", PCI_CFG_HFS_1, reg);
238 ((reg & PCI_CFG_HFS_1_D0I3_MSK) == PCI_CFG_HFS_1_D0I3_MSK);
240 hw->pg_state = MEI_PG_OFF;
241 if (hw->d0i3_supported) {
242 reg = mei_me_d0i3c_read(dev);
243 if (reg & H_D0I3C_I3)
244 hw->pg_state = MEI_PG_ON;
249 * mei_me_pg_state - translate internal pg state
250 * to the mei power gating state
254 * Return: MEI_PG_OFF if aliveness is on and MEI_PG_ON otherwise
256 static inline enum mei_pg_state mei_me_pg_state(struct mei_device *dev)
258 struct mei_me_hw *hw = to_me_hw(dev);
263 static inline u32 me_intr_src(u32 hcsr)
265 return hcsr & H_CSR_IS_MASK;
269 * me_intr_disable - disables mei device interrupts
270 * using supplied hcsr register value.
272 * @dev: the device structure
273 * @hcsr: supplied hcsr register value
275 static inline void me_intr_disable(struct mei_device *dev, u32 hcsr)
277 hcsr &= ~H_CSR_IE_MASK;
278 mei_hcsr_set(dev, hcsr);
282 * mei_me_intr_clear - clear and stop interrupts
284 * @dev: the device structure
285 * @hcsr: supplied hcsr register value
287 static inline void me_intr_clear(struct mei_device *dev, u32 hcsr)
289 if (me_intr_src(hcsr))
290 mei_hcsr_write(dev, hcsr);
294 * mei_me_intr_clear - clear and stop interrupts
296 * @dev: the device structure
298 static void mei_me_intr_clear(struct mei_device *dev)
300 u32 hcsr = mei_hcsr_read(dev);
302 me_intr_clear(dev, hcsr);
305 * mei_me_intr_enable - enables mei device interrupts
307 * @dev: the device structure
309 static void mei_me_intr_enable(struct mei_device *dev)
311 u32 hcsr = mei_hcsr_read(dev);
313 hcsr |= H_CSR_IE_MASK;
314 mei_hcsr_set(dev, hcsr);
318 * mei_me_intr_disable - disables mei device interrupts
320 * @dev: the device structure
322 static void mei_me_intr_disable(struct mei_device *dev)
324 u32 hcsr = mei_hcsr_read(dev);
326 me_intr_disable(dev, hcsr);
330 * mei_me_synchronize_irq - wait for pending IRQ handlers
332 * @dev: the device structure
334 static void mei_me_synchronize_irq(struct mei_device *dev)
336 struct pci_dev *pdev = to_pci_dev(dev->dev);
338 synchronize_irq(pdev->irq);
342 * mei_me_hw_reset_release - release device from the reset
344 * @dev: the device structure
346 static void mei_me_hw_reset_release(struct mei_device *dev)
348 u32 hcsr = mei_hcsr_read(dev);
352 mei_hcsr_set(dev, hcsr);
356 * mei_me_host_set_ready - enable device
360 static void mei_me_host_set_ready(struct mei_device *dev)
362 u32 hcsr = mei_hcsr_read(dev);
364 hcsr |= H_CSR_IE_MASK | H_IG | H_RDY;
365 mei_hcsr_set(dev, hcsr);
369 * mei_me_host_is_ready - check whether the host has turned ready
374 static bool mei_me_host_is_ready(struct mei_device *dev)
376 u32 hcsr = mei_hcsr_read(dev);
378 return (hcsr & H_RDY) == H_RDY;
382 * mei_me_hw_is_ready - check whether the me(hw) has turned ready
387 static bool mei_me_hw_is_ready(struct mei_device *dev)
389 u32 mecsr = mei_me_mecsr_read(dev);
391 return (mecsr & ME_RDY_HRA) == ME_RDY_HRA;
395 * mei_me_hw_is_resetting - check whether the me(hw) is in reset
400 static bool mei_me_hw_is_resetting(struct mei_device *dev)
402 u32 mecsr = mei_me_mecsr_read(dev);
404 return (mecsr & ME_RST_HRA) == ME_RST_HRA;
408 * mei_me_hw_ready_wait - wait until the me(hw) has turned ready
409 * or timeout is reached
412 * Return: 0 on success, error otherwise
414 static int mei_me_hw_ready_wait(struct mei_device *dev)
416 mutex_unlock(&dev->device_lock);
417 wait_event_timeout(dev->wait_hw_ready,
419 mei_secs_to_jiffies(MEI_HW_READY_TIMEOUT));
420 mutex_lock(&dev->device_lock);
421 if (!dev->recvd_hw_ready) {
422 dev_err(dev->dev, "wait hw ready failed\n");
426 mei_me_hw_reset_release(dev);
427 dev->recvd_hw_ready = false;
432 * mei_me_hw_start - hw start routine
435 * Return: 0 on success, error otherwise
437 static int mei_me_hw_start(struct mei_device *dev)
439 int ret = mei_me_hw_ready_wait(dev);
443 dev_dbg(dev->dev, "hw is ready\n");
445 mei_me_host_set_ready(dev);
451 * mei_hbuf_filled_slots - gets number of device filled buffer slots
453 * @dev: the device structure
455 * Return: number of filled slots
457 static unsigned char mei_hbuf_filled_slots(struct mei_device *dev)
460 char read_ptr, write_ptr;
462 hcsr = mei_hcsr_read(dev);
464 read_ptr = (char) ((hcsr & H_CBRP) >> 8);
465 write_ptr = (char) ((hcsr & H_CBWP) >> 16);
467 return (unsigned char) (write_ptr - read_ptr);
471 * mei_me_hbuf_is_empty - checks if host buffer is empty.
473 * @dev: the device structure
475 * Return: true if empty, false - otherwise.
477 static bool mei_me_hbuf_is_empty(struct mei_device *dev)
479 return mei_hbuf_filled_slots(dev) == 0;
483 * mei_me_hbuf_empty_slots - counts write empty slots.
485 * @dev: the device structure
487 * Return: -EOVERFLOW if overflow, otherwise empty slots count
489 static int mei_me_hbuf_empty_slots(struct mei_device *dev)
491 struct mei_me_hw *hw = to_me_hw(dev);
492 unsigned char filled_slots, empty_slots;
494 filled_slots = mei_hbuf_filled_slots(dev);
495 empty_slots = hw->hbuf_depth - filled_slots;
497 /* check for overflow */
498 if (filled_slots > hw->hbuf_depth)
505 * mei_me_hbuf_depth - returns depth of the hw buffer.
507 * @dev: the device structure
509 * Return: size of hw buffer in slots
511 static u32 mei_me_hbuf_depth(const struct mei_device *dev)
513 struct mei_me_hw *hw = to_me_hw(dev);
515 return hw->hbuf_depth;
519 * mei_me_hbuf_write - writes a message to host hw buffer.
521 * @dev: the device structure
522 * @hdr: header of message
523 * @hdr_len: header length in bytes: must be multiplication of a slot (4bytes)
525 * @data_len: payload length in bytes
527 * Return: 0 if success, < 0 - otherwise.
529 static int mei_me_hbuf_write(struct mei_device *dev,
530 const void *hdr, size_t hdr_len,
531 const void *data, size_t data_len)
539 if (WARN_ON(!hdr || !data || hdr_len & 0x3))
542 dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM((struct mei_msg_hdr *)hdr));
544 empty_slots = mei_hbuf_empty_slots(dev);
545 dev_dbg(dev->dev, "empty slots = %hu.\n", empty_slots);
550 dw_cnt = mei_data2slots(hdr_len + data_len);
551 if (dw_cnt > (u32)empty_slots)
555 for (i = 0; i < hdr_len / MEI_SLOT_SIZE; i++)
556 mei_me_hcbww_write(dev, reg_buf[i]);
559 for (i = 0; i < data_len / MEI_SLOT_SIZE; i++)
560 mei_me_hcbww_write(dev, reg_buf[i]);
562 rem = data_len & 0x3;
566 memcpy(®, (const u8 *)data + data_len - rem, rem);
567 mei_me_hcbww_write(dev, reg);
570 mei_hcsr_set_hig(dev);
571 if (!mei_me_hw_is_ready(dev))
578 * mei_me_count_full_read_slots - counts read full slots.
580 * @dev: the device structure
582 * Return: -EOVERFLOW if overflow, otherwise filled slots count
584 static int mei_me_count_full_read_slots(struct mei_device *dev)
587 char read_ptr, write_ptr;
588 unsigned char buffer_depth, filled_slots;
590 me_csr = mei_me_mecsr_read(dev);
591 buffer_depth = (unsigned char)((me_csr & ME_CBD_HRA) >> 24);
592 read_ptr = (char) ((me_csr & ME_CBRP_HRA) >> 8);
593 write_ptr = (char) ((me_csr & ME_CBWP_HRA) >> 16);
594 filled_slots = (unsigned char) (write_ptr - read_ptr);
596 /* check for overflow */
597 if (filled_slots > buffer_depth)
600 dev_dbg(dev->dev, "filled_slots =%08x\n", filled_slots);
601 return (int)filled_slots;
605 * mei_me_read_slots - reads a message from mei device.
607 * @dev: the device structure
608 * @buffer: message buffer will be written
609 * @buffer_length: message size will be read
613 static int mei_me_read_slots(struct mei_device *dev, unsigned char *buffer,
614 unsigned long buffer_length)
616 u32 *reg_buf = (u32 *)buffer;
618 for (; buffer_length >= MEI_SLOT_SIZE; buffer_length -= MEI_SLOT_SIZE)
619 *reg_buf++ = mei_me_mecbrw_read(dev);
621 if (buffer_length > 0) {
622 u32 reg = mei_me_mecbrw_read(dev);
624 memcpy(reg_buf, ®, buffer_length);
627 mei_hcsr_set_hig(dev);
632 * mei_me_pg_set - write pg enter register
634 * @dev: the device structure
636 static void mei_me_pg_set(struct mei_device *dev)
638 struct mei_me_hw *hw = to_me_hw(dev);
641 reg = mei_me_reg_read(hw, H_HPG_CSR);
642 trace_mei_reg_read(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
644 reg |= H_HPG_CSR_PGI;
646 trace_mei_reg_write(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
647 mei_me_reg_write(hw, H_HPG_CSR, reg);
651 * mei_me_pg_unset - write pg exit register
653 * @dev: the device structure
655 static void mei_me_pg_unset(struct mei_device *dev)
657 struct mei_me_hw *hw = to_me_hw(dev);
660 reg = mei_me_reg_read(hw, H_HPG_CSR);
661 trace_mei_reg_read(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
663 WARN(!(reg & H_HPG_CSR_PGI), "PGI is not set\n");
665 reg |= H_HPG_CSR_PGIHEXR;
667 trace_mei_reg_write(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
668 mei_me_reg_write(hw, H_HPG_CSR, reg);
672 * mei_me_pg_legacy_enter_sync - perform legacy pg entry procedure
674 * @dev: the device structure
676 * Return: 0 on success an error code otherwise
678 static int mei_me_pg_legacy_enter_sync(struct mei_device *dev)
680 struct mei_me_hw *hw = to_me_hw(dev);
681 unsigned long timeout = mei_secs_to_jiffies(MEI_PGI_TIMEOUT);
684 dev->pg_event = MEI_PG_EVENT_WAIT;
686 ret = mei_hbm_pg(dev, MEI_PG_ISOLATION_ENTRY_REQ_CMD);
690 mutex_unlock(&dev->device_lock);
691 wait_event_timeout(dev->wait_pg,
692 dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
693 mutex_lock(&dev->device_lock);
695 if (dev->pg_event == MEI_PG_EVENT_RECEIVED) {
702 dev->pg_event = MEI_PG_EVENT_IDLE;
703 hw->pg_state = MEI_PG_ON;
709 * mei_me_pg_legacy_exit_sync - perform legacy pg exit procedure
711 * @dev: the device structure
713 * Return: 0 on success an error code otherwise
715 static int mei_me_pg_legacy_exit_sync(struct mei_device *dev)
717 struct mei_me_hw *hw = to_me_hw(dev);
718 unsigned long timeout = mei_secs_to_jiffies(MEI_PGI_TIMEOUT);
721 if (dev->pg_event == MEI_PG_EVENT_RECEIVED)
724 dev->pg_event = MEI_PG_EVENT_WAIT;
726 mei_me_pg_unset(dev);
728 mutex_unlock(&dev->device_lock);
729 wait_event_timeout(dev->wait_pg,
730 dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
731 mutex_lock(&dev->device_lock);
734 if (dev->pg_event != MEI_PG_EVENT_RECEIVED) {
739 dev->pg_event = MEI_PG_EVENT_INTR_WAIT;
740 ret = mei_hbm_pg(dev, MEI_PG_ISOLATION_EXIT_RES_CMD);
744 mutex_unlock(&dev->device_lock);
745 wait_event_timeout(dev->wait_pg,
746 dev->pg_event == MEI_PG_EVENT_INTR_RECEIVED, timeout);
747 mutex_lock(&dev->device_lock);
749 if (dev->pg_event == MEI_PG_EVENT_INTR_RECEIVED)
755 dev->pg_event = MEI_PG_EVENT_IDLE;
756 hw->pg_state = MEI_PG_OFF;
762 * mei_me_pg_in_transition - is device now in pg transition
764 * @dev: the device structure
766 * Return: true if in pg transition, false otherwise
768 static bool mei_me_pg_in_transition(struct mei_device *dev)
770 return dev->pg_event >= MEI_PG_EVENT_WAIT &&
771 dev->pg_event <= MEI_PG_EVENT_INTR_WAIT;
775 * mei_me_pg_is_enabled - detect if PG is supported by HW
777 * @dev: the device structure
779 * Return: true is pg supported, false otherwise
781 static bool mei_me_pg_is_enabled(struct mei_device *dev)
783 struct mei_me_hw *hw = to_me_hw(dev);
784 u32 reg = mei_me_mecsr_read(dev);
786 if (hw->d0i3_supported)
789 if ((reg & ME_PGIC_HRA) == 0)
792 if (!dev->hbm_f_pg_supported)
798 dev_dbg(dev->dev, "pg: not supported: d0i3 = %d HGP = %d hbm version %d.%d ?= %d.%d\n",
800 !!(reg & ME_PGIC_HRA),
801 dev->version.major_version,
802 dev->version.minor_version,
803 HBM_MAJOR_VERSION_PGI,
804 HBM_MINOR_VERSION_PGI);
810 * mei_me_d0i3_set - write d0i3 register bit on mei device.
812 * @dev: the device structure
813 * @intr: ask for interrupt
815 * Return: D0I3C register value
817 static u32 mei_me_d0i3_set(struct mei_device *dev, bool intr)
819 u32 reg = mei_me_d0i3c_read(dev);
826 mei_me_d0i3c_write(dev, reg);
827 /* read it to ensure HW consistency */
828 reg = mei_me_d0i3c_read(dev);
833 * mei_me_d0i3_unset - clean d0i3 register bit on mei device.
835 * @dev: the device structure
837 * Return: D0I3C register value
839 static u32 mei_me_d0i3_unset(struct mei_device *dev)
841 u32 reg = mei_me_d0i3c_read(dev);
845 mei_me_d0i3c_write(dev, reg);
846 /* read it to ensure HW consistency */
847 reg = mei_me_d0i3c_read(dev);
852 * mei_me_d0i3_enter_sync - perform d0i3 entry procedure
854 * @dev: the device structure
856 * Return: 0 on success an error code otherwise
858 static int mei_me_d0i3_enter_sync(struct mei_device *dev)
860 struct mei_me_hw *hw = to_me_hw(dev);
861 unsigned long d0i3_timeout = mei_secs_to_jiffies(MEI_D0I3_TIMEOUT);
862 unsigned long pgi_timeout = mei_secs_to_jiffies(MEI_PGI_TIMEOUT);
866 reg = mei_me_d0i3c_read(dev);
867 if (reg & H_D0I3C_I3) {
868 /* we are in d0i3, nothing to do */
869 dev_dbg(dev->dev, "d0i3 set not needed\n");
874 /* PGI entry procedure */
875 dev->pg_event = MEI_PG_EVENT_WAIT;
877 ret = mei_hbm_pg(dev, MEI_PG_ISOLATION_ENTRY_REQ_CMD);
879 /* FIXME: should we reset here? */
882 mutex_unlock(&dev->device_lock);
883 wait_event_timeout(dev->wait_pg,
884 dev->pg_event == MEI_PG_EVENT_RECEIVED, pgi_timeout);
885 mutex_lock(&dev->device_lock);
887 if (dev->pg_event != MEI_PG_EVENT_RECEIVED) {
891 /* end PGI entry procedure */
893 dev->pg_event = MEI_PG_EVENT_INTR_WAIT;
895 reg = mei_me_d0i3_set(dev, true);
896 if (!(reg & H_D0I3C_CIP)) {
897 dev_dbg(dev->dev, "d0i3 enter wait not needed\n");
902 mutex_unlock(&dev->device_lock);
903 wait_event_timeout(dev->wait_pg,
904 dev->pg_event == MEI_PG_EVENT_INTR_RECEIVED, d0i3_timeout);
905 mutex_lock(&dev->device_lock);
907 if (dev->pg_event != MEI_PG_EVENT_INTR_RECEIVED) {
908 reg = mei_me_d0i3c_read(dev);
909 if (!(reg & H_D0I3C_I3)) {
917 hw->pg_state = MEI_PG_ON;
919 dev->pg_event = MEI_PG_EVENT_IDLE;
920 dev_dbg(dev->dev, "d0i3 enter ret = %d\n", ret);
925 * mei_me_d0i3_enter - perform d0i3 entry procedure
926 * no hbm PG handshake
927 * no waiting for confirmation; runs with interrupts
930 * @dev: the device structure
932 * Return: 0 on success an error code otherwise
934 static int mei_me_d0i3_enter(struct mei_device *dev)
936 struct mei_me_hw *hw = to_me_hw(dev);
939 reg = mei_me_d0i3c_read(dev);
940 if (reg & H_D0I3C_I3) {
941 /* we are in d0i3, nothing to do */
942 dev_dbg(dev->dev, "already d0i3 : set not needed\n");
946 mei_me_d0i3_set(dev, false);
948 hw->pg_state = MEI_PG_ON;
949 dev->pg_event = MEI_PG_EVENT_IDLE;
950 dev_dbg(dev->dev, "d0i3 enter\n");
955 * mei_me_d0i3_exit_sync - perform d0i3 exit procedure
957 * @dev: the device structure
959 * Return: 0 on success an error code otherwise
961 static int mei_me_d0i3_exit_sync(struct mei_device *dev)
963 struct mei_me_hw *hw = to_me_hw(dev);
964 unsigned long timeout = mei_secs_to_jiffies(MEI_D0I3_TIMEOUT);
968 dev->pg_event = MEI_PG_EVENT_INTR_WAIT;
970 reg = mei_me_d0i3c_read(dev);
971 if (!(reg & H_D0I3C_I3)) {
972 /* we are not in d0i3, nothing to do */
973 dev_dbg(dev->dev, "d0i3 exit not needed\n");
978 reg = mei_me_d0i3_unset(dev);
979 if (!(reg & H_D0I3C_CIP)) {
980 dev_dbg(dev->dev, "d0i3 exit wait not needed\n");
985 mutex_unlock(&dev->device_lock);
986 wait_event_timeout(dev->wait_pg,
987 dev->pg_event == MEI_PG_EVENT_INTR_RECEIVED, timeout);
988 mutex_lock(&dev->device_lock);
990 if (dev->pg_event != MEI_PG_EVENT_INTR_RECEIVED) {
991 reg = mei_me_d0i3c_read(dev);
992 if (reg & H_D0I3C_I3) {
1000 hw->pg_state = MEI_PG_OFF;
1002 dev->pg_event = MEI_PG_EVENT_IDLE;
1004 dev_dbg(dev->dev, "d0i3 exit ret = %d\n", ret);
1009 * mei_me_pg_legacy_intr - perform legacy pg processing
1010 * in interrupt thread handler
1012 * @dev: the device structure
1014 static void mei_me_pg_legacy_intr(struct mei_device *dev)
1016 struct mei_me_hw *hw = to_me_hw(dev);
1018 if (dev->pg_event != MEI_PG_EVENT_INTR_WAIT)
1021 dev->pg_event = MEI_PG_EVENT_INTR_RECEIVED;
1022 hw->pg_state = MEI_PG_OFF;
1023 if (waitqueue_active(&dev->wait_pg))
1024 wake_up(&dev->wait_pg);
1028 * mei_me_d0i3_intr - perform d0i3 processing in interrupt thread handler
1030 * @dev: the device structure
1031 * @intr_source: interrupt source
1033 static void mei_me_d0i3_intr(struct mei_device *dev, u32 intr_source)
1035 struct mei_me_hw *hw = to_me_hw(dev);
1037 if (dev->pg_event == MEI_PG_EVENT_INTR_WAIT &&
1038 (intr_source & H_D0I3C_IS)) {
1039 dev->pg_event = MEI_PG_EVENT_INTR_RECEIVED;
1040 if (hw->pg_state == MEI_PG_ON) {
1041 hw->pg_state = MEI_PG_OFF;
1042 if (dev->hbm_state != MEI_HBM_IDLE) {
1044 * force H_RDY because it could be
1045 * wiped off during PG
1047 dev_dbg(dev->dev, "d0i3 set host ready\n");
1048 mei_me_host_set_ready(dev);
1051 hw->pg_state = MEI_PG_ON;
1054 wake_up(&dev->wait_pg);
1057 if (hw->pg_state == MEI_PG_ON && (intr_source & H_IS)) {
1059 * HW sent some data and we are in D0i3, so
1060 * we got here because of HW initiated exit from D0i3.
1061 * Start runtime pm resume sequence to exit low power state.
1063 dev_dbg(dev->dev, "d0i3 want resume\n");
1064 mei_hbm_pg_resume(dev);
1069 * mei_me_pg_intr - perform pg processing in interrupt thread handler
1071 * @dev: the device structure
1072 * @intr_source: interrupt source
1074 static void mei_me_pg_intr(struct mei_device *dev, u32 intr_source)
1076 struct mei_me_hw *hw = to_me_hw(dev);
1078 if (hw->d0i3_supported)
1079 mei_me_d0i3_intr(dev, intr_source);
1081 mei_me_pg_legacy_intr(dev);
1085 * mei_me_pg_enter_sync - perform runtime pm entry procedure
1087 * @dev: the device structure
1089 * Return: 0 on success an error code otherwise
1091 int mei_me_pg_enter_sync(struct mei_device *dev)
1093 struct mei_me_hw *hw = to_me_hw(dev);
1095 if (hw->d0i3_supported)
1096 return mei_me_d0i3_enter_sync(dev);
1098 return mei_me_pg_legacy_enter_sync(dev);
1102 * mei_me_pg_exit_sync - perform runtime pm exit procedure
1104 * @dev: the device structure
1106 * Return: 0 on success an error code otherwise
1108 int mei_me_pg_exit_sync(struct mei_device *dev)
1110 struct mei_me_hw *hw = to_me_hw(dev);
1112 if (hw->d0i3_supported)
1113 return mei_me_d0i3_exit_sync(dev);
1115 return mei_me_pg_legacy_exit_sync(dev);
1119 * mei_me_hw_reset - resets fw via mei csr register.
1121 * @dev: the device structure
1122 * @intr_enable: if interrupt should be enabled after reset.
1124 * Return: 0 on success an error code otherwise
1126 static int mei_me_hw_reset(struct mei_device *dev, bool intr_enable)
1128 struct mei_me_hw *hw = to_me_hw(dev);
1133 mei_me_intr_enable(dev);
1134 if (hw->d0i3_supported) {
1135 ret = mei_me_d0i3_exit_sync(dev);
1141 pm_runtime_set_active(dev->dev);
1143 hcsr = mei_hcsr_read(dev);
1144 /* H_RST may be found lit before reset is started,
1145 * for example if preceding reset flow hasn't completed.
1146 * In that case asserting H_RST will be ignored, therefore
1147 * we need to clean H_RST bit to start a successful reset sequence.
1149 if ((hcsr & H_RST) == H_RST) {
1150 dev_warn(dev->dev, "H_RST is set = 0x%08X", hcsr);
1152 mei_hcsr_set(dev, hcsr);
1153 hcsr = mei_hcsr_read(dev);
1156 hcsr |= H_RST | H_IG | H_CSR_IS_MASK;
1159 hcsr &= ~H_CSR_IE_MASK;
1161 dev->recvd_hw_ready = false;
1162 mei_hcsr_write(dev, hcsr);
1165 * Host reads the H_CSR once to ensure that the
1166 * posted write to H_CSR completes.
1168 hcsr = mei_hcsr_read(dev);
1170 if ((hcsr & H_RST) == 0)
1171 dev_warn(dev->dev, "H_RST is not set = 0x%08X", hcsr);
1173 if ((hcsr & H_RDY) == H_RDY)
1174 dev_warn(dev->dev, "H_RDY is not cleared 0x%08X", hcsr);
1177 mei_me_hw_reset_release(dev);
1178 if (hw->d0i3_supported) {
1179 ret = mei_me_d0i3_enter(dev);
1188 * mei_me_irq_quick_handler - The ISR of the MEI device
1190 * @irq: The irq number
1191 * @dev_id: pointer to the device structure
1193 * Return: irqreturn_t
1195 irqreturn_t mei_me_irq_quick_handler(int irq, void *dev_id)
1197 struct mei_device *dev = (struct mei_device *)dev_id;
1200 hcsr = mei_hcsr_read(dev);
1201 if (!me_intr_src(hcsr))
1204 dev_dbg(dev->dev, "interrupt source 0x%08X\n", me_intr_src(hcsr));
1206 /* disable interrupts on device */
1207 me_intr_disable(dev, hcsr);
1208 return IRQ_WAKE_THREAD;
1212 * mei_me_irq_thread_handler - function called after ISR to handle the interrupt
1215 * @irq: The irq number
1216 * @dev_id: pointer to the device structure
1218 * Return: irqreturn_t
1221 irqreturn_t mei_me_irq_thread_handler(int irq, void *dev_id)
1223 struct mei_device *dev = (struct mei_device *) dev_id;
1224 struct list_head cmpl_list;
1229 dev_dbg(dev->dev, "function called after ISR to handle the interrupt processing.\n");
1230 /* initialize our complete list */
1231 mutex_lock(&dev->device_lock);
1233 hcsr = mei_hcsr_read(dev);
1234 me_intr_clear(dev, hcsr);
1236 INIT_LIST_HEAD(&cmpl_list);
1238 /* check if ME wants a reset */
1239 if (!mei_hw_is_ready(dev) && dev->dev_state != MEI_DEV_RESETTING) {
1240 dev_warn(dev->dev, "FW not ready: resetting.\n");
1241 schedule_work(&dev->reset_work);
1245 if (mei_me_hw_is_resetting(dev))
1246 mei_hcsr_set_hig(dev);
1248 mei_me_pg_intr(dev, me_intr_src(hcsr));
1250 /* check if we need to start the dev */
1251 if (!mei_host_is_ready(dev)) {
1252 if (mei_hw_is_ready(dev)) {
1253 dev_dbg(dev->dev, "we need to start the dev.\n");
1254 dev->recvd_hw_ready = true;
1255 wake_up(&dev->wait_hw_ready);
1257 dev_dbg(dev->dev, "Spurious Interrupt\n");
1261 /* check slots available for reading */
1262 slots = mei_count_full_read_slots(dev);
1264 dev_dbg(dev->dev, "slots to read = %08x\n", slots);
1265 rets = mei_irq_read_handler(dev, &cmpl_list, &slots);
1266 /* There is a race between ME write and interrupt delivery:
1267 * Not all data is always available immediately after the
1268 * interrupt, so try to read again on the next interrupt.
1270 if (rets == -ENODATA)
1274 (dev->dev_state != MEI_DEV_RESETTING &&
1275 dev->dev_state != MEI_DEV_POWER_DOWN)) {
1276 dev_err(dev->dev, "mei_irq_read_handler ret = %d.\n",
1278 schedule_work(&dev->reset_work);
1283 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
1286 * During PG handshake only allowed write is the replay to the
1287 * PG exit message, so block calling write function
1288 * if the pg event is in PG handshake
1290 if (dev->pg_event != MEI_PG_EVENT_WAIT &&
1291 dev->pg_event != MEI_PG_EVENT_RECEIVED) {
1292 rets = mei_irq_write_handler(dev, &cmpl_list);
1293 dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
1296 mei_irq_compl_handler(dev, &cmpl_list);
1299 dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
1300 mei_me_intr_enable(dev);
1301 mutex_unlock(&dev->device_lock);
1305 static const struct mei_hw_ops mei_me_hw_ops = {
1307 .fw_status = mei_me_fw_status,
1308 .pg_state = mei_me_pg_state,
1310 .host_is_ready = mei_me_host_is_ready,
1312 .hw_is_ready = mei_me_hw_is_ready,
1313 .hw_reset = mei_me_hw_reset,
1314 .hw_config = mei_me_hw_config,
1315 .hw_start = mei_me_hw_start,
1317 .pg_in_transition = mei_me_pg_in_transition,
1318 .pg_is_enabled = mei_me_pg_is_enabled,
1320 .intr_clear = mei_me_intr_clear,
1321 .intr_enable = mei_me_intr_enable,
1322 .intr_disable = mei_me_intr_disable,
1323 .synchronize_irq = mei_me_synchronize_irq,
1325 .hbuf_free_slots = mei_me_hbuf_empty_slots,
1326 .hbuf_is_ready = mei_me_hbuf_is_empty,
1327 .hbuf_depth = mei_me_hbuf_depth,
1329 .write = mei_me_hbuf_write,
1331 .rdbuf_full_slots = mei_me_count_full_read_slots,
1332 .read_hdr = mei_me_mecbrw_read,
1333 .read = mei_me_read_slots
1336 static bool mei_me_fw_type_nm(struct pci_dev *pdev)
1340 pci_read_config_dword(pdev, PCI_CFG_HFS_2, ®);
1341 trace_mei_pci_cfg_read(&pdev->dev, "PCI_CFG_HFS_2", PCI_CFG_HFS_2, reg);
1342 /* make sure that bit 9 (NM) is up and bit 10 (DM) is down */
1343 return (reg & 0x600) == 0x200;
1346 #define MEI_CFG_FW_NM \
1347 .quirk_probe = mei_me_fw_type_nm
1349 static bool mei_me_fw_type_sps(struct pci_dev *pdev)
1355 * Read ME FW Status register to check for SPS Firmware
1356 * The SPS FW is only signaled in pci function 0
1358 devfn = PCI_DEVFN(PCI_SLOT(pdev->devfn), 0);
1359 pci_bus_read_config_dword(pdev->bus, devfn, PCI_CFG_HFS_1, ®);
1360 trace_mei_pci_cfg_read(&pdev->dev, "PCI_CFG_HFS_1", PCI_CFG_HFS_1, reg);
1361 /* if bits [19:16] = 15, running SPS Firmware */
1362 return (reg & 0xf0000) == 0xf0000;
1365 #define MEI_CFG_FW_SPS \
1366 .quirk_probe = mei_me_fw_type_sps
1369 #define MEI_CFG_ICH_HFS \
1370 .fw_status.count = 0
1372 #define MEI_CFG_ICH10_HFS \
1373 .fw_status.count = 1, \
1374 .fw_status.status[0] = PCI_CFG_HFS_1
1376 #define MEI_CFG_PCH_HFS \
1377 .fw_status.count = 2, \
1378 .fw_status.status[0] = PCI_CFG_HFS_1, \
1379 .fw_status.status[1] = PCI_CFG_HFS_2
1381 #define MEI_CFG_PCH8_HFS \
1382 .fw_status.count = 6, \
1383 .fw_status.status[0] = PCI_CFG_HFS_1, \
1384 .fw_status.status[1] = PCI_CFG_HFS_2, \
1385 .fw_status.status[2] = PCI_CFG_HFS_3, \
1386 .fw_status.status[3] = PCI_CFG_HFS_4, \
1387 .fw_status.status[4] = PCI_CFG_HFS_5, \
1388 .fw_status.status[5] = PCI_CFG_HFS_6
1390 #define MEI_CFG_DMA_128 \
1391 .dma_size[DMA_DSCR_HOST] = SZ_128K, \
1392 .dma_size[DMA_DSCR_DEVICE] = SZ_128K, \
1393 .dma_size[DMA_DSCR_CTRL] = PAGE_SIZE
1395 /* ICH Legacy devices */
1396 static const struct mei_cfg mei_me_ich_cfg = {
1401 static const struct mei_cfg mei_me_ich10_cfg = {
1406 static const struct mei_cfg mei_me_pch_cfg = {
1410 /* PCH Cougar Point and Patsburg with quirk for Node Manager exclusion */
1411 static const struct mei_cfg mei_me_pch_cpt_pbg_cfg = {
1416 /* PCH8 Lynx Point and newer devices */
1417 static const struct mei_cfg mei_me_pch8_cfg = {
1421 /* PCH8 Lynx Point with quirk for SPS Firmware exclusion */
1422 static const struct mei_cfg mei_me_pch8_sps_cfg = {
1427 /* Cannon Lake and newer devices */
1428 static const struct mei_cfg mei_me_pch12_cfg = {
1434 * mei_cfg_list - A list of platform platform specific configurations.
1435 * Note: has to be synchronized with enum mei_cfg_idx.
1437 static const struct mei_cfg *const mei_cfg_list[] = {
1438 [MEI_ME_UNDEF_CFG] = NULL,
1439 [MEI_ME_ICH_CFG] = &mei_me_ich_cfg,
1440 [MEI_ME_ICH10_CFG] = &mei_me_ich10_cfg,
1441 [MEI_ME_PCH_CFG] = &mei_me_pch_cfg,
1442 [MEI_ME_PCH_CPT_PBG_CFG] = &mei_me_pch_cpt_pbg_cfg,
1443 [MEI_ME_PCH8_CFG] = &mei_me_pch8_cfg,
1444 [MEI_ME_PCH8_SPS_CFG] = &mei_me_pch8_sps_cfg,
1445 [MEI_ME_PCH12_CFG] = &mei_me_pch12_cfg,
1448 const struct mei_cfg *mei_me_get_cfg(kernel_ulong_t idx)
1450 BUILD_BUG_ON(ARRAY_SIZE(mei_cfg_list) != MEI_ME_NUM_CFG);
1452 if (idx >= MEI_ME_NUM_CFG)
1455 return mei_cfg_list[idx];
1459 * mei_me_dev_init - allocates and initializes the mei device structure
1461 * @pdev: The pci device structure
1462 * @cfg: per device generation config
1464 * Return: The mei_device pointer on success, NULL on failure.
1466 struct mei_device *mei_me_dev_init(struct pci_dev *pdev,
1467 const struct mei_cfg *cfg)
1469 struct mei_device *dev;
1470 struct mei_me_hw *hw;
1473 dev = devm_kzalloc(&pdev->dev, sizeof(struct mei_device) +
1474 sizeof(struct mei_me_hw), GFP_KERNEL);
1480 for (i = 0; i < DMA_DSCR_NUM; i++)
1481 dev->dr_dscr[i].size = cfg->dma_size[i];
1483 mei_device_init(dev, &pdev->dev, &mei_me_hw_ops);