1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2020, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
9 #include <linux/kthread.h>
10 #include <linux/interrupt.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/sizes.h>
18 #include "hw-me-regs.h"
20 #include "mei-trace.h"
23 * mei_me_reg_read - Reads 32bit data from the mei device
25 * @hw: the me hardware structure
26 * @offset: offset from which to read the data
28 * Return: register value (u32)
30 static inline u32
mei_me_reg_read(const struct mei_me_hw
*hw
,
33 return ioread32(hw
->mem_addr
+ offset
);
38 * mei_me_reg_write - Writes 32bit data to the mei device
40 * @hw: the me hardware structure
41 * @offset: offset from which to write the data
42 * @value: register value to write (u32)
44 static inline void mei_me_reg_write(const struct mei_me_hw
*hw
,
45 unsigned long offset
, u32 value
)
47 iowrite32(value
, hw
->mem_addr
+ offset
);
51 * mei_me_mecbrw_read - Reads 32bit data from ME circular buffer
52 * read window register
54 * @dev: the device structure
56 * Return: ME_CB_RW register value (u32)
58 static inline u32
mei_me_mecbrw_read(const struct mei_device
*dev
)
60 return mei_me_reg_read(to_me_hw(dev
), ME_CB_RW
);
64 * mei_me_hcbww_write - write 32bit data to the host circular buffer
66 * @dev: the device structure
67 * @data: 32bit data to be written to the host circular buffer
69 static inline void mei_me_hcbww_write(struct mei_device
*dev
, u32 data
)
71 mei_me_reg_write(to_me_hw(dev
), H_CB_WW
, data
);
75 * mei_me_mecsr_read - Reads 32bit data from the ME CSR
77 * @dev: the device structure
79 * Return: ME_CSR_HA register value (u32)
81 static inline u32
mei_me_mecsr_read(const struct mei_device
*dev
)
85 reg
= mei_me_reg_read(to_me_hw(dev
), ME_CSR_HA
);
86 trace_mei_reg_read(dev
->dev
, "ME_CSR_HA", ME_CSR_HA
, reg
);
92 * mei_hcsr_read - Reads 32bit data from the host CSR
94 * @dev: the device structure
96 * Return: H_CSR register value (u32)
98 static inline u32
mei_hcsr_read(const struct mei_device
*dev
)
102 reg
= mei_me_reg_read(to_me_hw(dev
), H_CSR
);
103 trace_mei_reg_read(dev
->dev
, "H_CSR", H_CSR
, reg
);
109 * mei_hcsr_write - writes H_CSR register to the mei device
111 * @dev: the device structure
112 * @reg: new register value
114 static inline void mei_hcsr_write(struct mei_device
*dev
, u32 reg
)
116 trace_mei_reg_write(dev
->dev
, "H_CSR", H_CSR
, reg
);
117 mei_me_reg_write(to_me_hw(dev
), H_CSR
, reg
);
121 * mei_hcsr_set - writes H_CSR register to the mei device,
122 * and ignores the H_IS bit for it is write-one-to-zero.
124 * @dev: the device structure
125 * @reg: new register value
127 static inline void mei_hcsr_set(struct mei_device
*dev
, u32 reg
)
129 reg
&= ~H_CSR_IS_MASK
;
130 mei_hcsr_write(dev
, reg
);
134 * mei_hcsr_set_hig - set host interrupt (set H_IG)
136 * @dev: the device structure
138 static inline void mei_hcsr_set_hig(struct mei_device
*dev
)
142 hcsr
= mei_hcsr_read(dev
) | H_IG
;
143 mei_hcsr_set(dev
, hcsr
);
147 * mei_me_d0i3c_read - Reads 32bit data from the D0I3C register
149 * @dev: the device structure
151 * Return: H_D0I3C register value (u32)
153 static inline u32
mei_me_d0i3c_read(const struct mei_device
*dev
)
157 reg
= mei_me_reg_read(to_me_hw(dev
), H_D0I3C
);
158 trace_mei_reg_read(dev
->dev
, "H_D0I3C", H_D0I3C
, reg
);
164 * mei_me_d0i3c_write - writes H_D0I3C register to device
166 * @dev: the device structure
167 * @reg: new register value
169 static inline void mei_me_d0i3c_write(struct mei_device
*dev
, u32 reg
)
171 trace_mei_reg_write(dev
->dev
, "H_D0I3C", H_D0I3C
, reg
);
172 mei_me_reg_write(to_me_hw(dev
), H_D0I3C
, reg
);
176 * mei_me_trc_status - read trc status register
179 * @trc: trc status register value
181 * Return: 0 on success, error otherwise
183 static int mei_me_trc_status(struct mei_device
*dev
, u32
*trc
)
185 struct mei_me_hw
*hw
= to_me_hw(dev
);
187 if (!hw
->cfg
->hw_trc_supported
)
190 *trc
= mei_me_reg_read(hw
, ME_TRC
);
191 trace_mei_reg_read(dev
->dev
, "ME_TRC", ME_TRC
, *trc
);
197 * mei_me_fw_status - read fw status register from pci config space
200 * @fw_status: fw status register values
202 * Return: 0 on success, error otherwise
204 static int mei_me_fw_status(struct mei_device
*dev
,
205 struct mei_fw_status
*fw_status
)
207 struct mei_me_hw
*hw
= to_me_hw(dev
);
208 const struct mei_fw_status
*fw_src
= &hw
->cfg
->fw_status
;
212 if (!fw_status
|| !hw
->read_fws
)
215 fw_status
->count
= fw_src
->count
;
216 for (i
= 0; i
< fw_src
->count
&& i
< MEI_FW_STATUS_MAX
; i
++) {
217 ret
= hw
->read_fws(dev
, fw_src
->status
[i
],
218 &fw_status
->status
[i
]);
219 trace_mei_pci_cfg_read(dev
->dev
, "PCI_CFG_HFS_X",
221 fw_status
->status
[i
]);
230 * mei_me_hw_config - configure hw dependent settings
235 * * -EINVAL when read_fws is not set
239 static int mei_me_hw_config(struct mei_device
*dev
)
241 struct mei_me_hw
*hw
= to_me_hw(dev
);
244 if (WARN_ON(!hw
->read_fws
))
247 /* Doesn't change in runtime */
248 hcsr
= mei_hcsr_read(dev
);
249 hw
->hbuf_depth
= (hcsr
& H_CBD
) >> 24;
252 hw
->read_fws(dev
, PCI_CFG_HFS_1
, ®
);
253 trace_mei_pci_cfg_read(dev
->dev
, "PCI_CFG_HFS_1", PCI_CFG_HFS_1
, reg
);
255 ((reg
& PCI_CFG_HFS_1_D0I3_MSK
) == PCI_CFG_HFS_1_D0I3_MSK
);
257 hw
->pg_state
= MEI_PG_OFF
;
258 if (hw
->d0i3_supported
) {
259 reg
= mei_me_d0i3c_read(dev
);
260 if (reg
& H_D0I3C_I3
)
261 hw
->pg_state
= MEI_PG_ON
;
268 * mei_me_pg_state - translate internal pg state
269 * to the mei power gating state
273 * Return: MEI_PG_OFF if aliveness is on and MEI_PG_ON otherwise
275 static inline enum mei_pg_state
mei_me_pg_state(struct mei_device
*dev
)
277 struct mei_me_hw
*hw
= to_me_hw(dev
);
282 static inline u32
me_intr_src(u32 hcsr
)
284 return hcsr
& H_CSR_IS_MASK
;
288 * me_intr_disable - disables mei device interrupts
289 * using supplied hcsr register value.
291 * @dev: the device structure
292 * @hcsr: supplied hcsr register value
294 static inline void me_intr_disable(struct mei_device
*dev
, u32 hcsr
)
296 hcsr
&= ~H_CSR_IE_MASK
;
297 mei_hcsr_set(dev
, hcsr
);
301 * me_intr_clear - clear and stop interrupts
303 * @dev: the device structure
304 * @hcsr: supplied hcsr register value
306 static inline void me_intr_clear(struct mei_device
*dev
, u32 hcsr
)
308 if (me_intr_src(hcsr
))
309 mei_hcsr_write(dev
, hcsr
);
313 * mei_me_intr_clear - clear and stop interrupts
315 * @dev: the device structure
317 static void mei_me_intr_clear(struct mei_device
*dev
)
319 u32 hcsr
= mei_hcsr_read(dev
);
321 me_intr_clear(dev
, hcsr
);
324 * mei_me_intr_enable - enables mei device interrupts
326 * @dev: the device structure
328 static void mei_me_intr_enable(struct mei_device
*dev
)
330 u32 hcsr
= mei_hcsr_read(dev
);
332 hcsr
|= H_CSR_IE_MASK
;
333 mei_hcsr_set(dev
, hcsr
);
337 * mei_me_intr_disable - disables mei device interrupts
339 * @dev: the device structure
341 static void mei_me_intr_disable(struct mei_device
*dev
)
343 u32 hcsr
= mei_hcsr_read(dev
);
345 me_intr_disable(dev
, hcsr
);
349 * mei_me_synchronize_irq - wait for pending IRQ handlers
351 * @dev: the device structure
353 static void mei_me_synchronize_irq(struct mei_device
*dev
)
355 struct mei_me_hw
*hw
= to_me_hw(dev
);
357 synchronize_irq(hw
->irq
);
361 * mei_me_hw_reset_release - release device from the reset
363 * @dev: the device structure
365 static void mei_me_hw_reset_release(struct mei_device
*dev
)
367 u32 hcsr
= mei_hcsr_read(dev
);
371 mei_hcsr_set(dev
, hcsr
);
375 * mei_me_host_set_ready - enable device
379 static void mei_me_host_set_ready(struct mei_device
*dev
)
381 u32 hcsr
= mei_hcsr_read(dev
);
383 hcsr
|= H_CSR_IE_MASK
| H_IG
| H_RDY
;
384 mei_hcsr_set(dev
, hcsr
);
388 * mei_me_host_is_ready - check whether the host has turned ready
393 static bool mei_me_host_is_ready(struct mei_device
*dev
)
395 u32 hcsr
= mei_hcsr_read(dev
);
397 return (hcsr
& H_RDY
) == H_RDY
;
401 * mei_me_hw_is_ready - check whether the me(hw) has turned ready
406 static bool mei_me_hw_is_ready(struct mei_device
*dev
)
408 u32 mecsr
= mei_me_mecsr_read(dev
);
410 return (mecsr
& ME_RDY_HRA
) == ME_RDY_HRA
;
414 * mei_me_hw_is_resetting - check whether the me(hw) is in reset
419 static bool mei_me_hw_is_resetting(struct mei_device
*dev
)
421 u32 mecsr
= mei_me_mecsr_read(dev
);
423 return (mecsr
& ME_RST_HRA
) == ME_RST_HRA
;
427 * mei_me_hw_ready_wait - wait until the me(hw) has turned ready
428 * or timeout is reached
431 * Return: 0 on success, error otherwise
433 static int mei_me_hw_ready_wait(struct mei_device
*dev
)
435 mutex_unlock(&dev
->device_lock
);
436 wait_event_timeout(dev
->wait_hw_ready
,
438 mei_secs_to_jiffies(MEI_HW_READY_TIMEOUT
));
439 mutex_lock(&dev
->device_lock
);
440 if (!dev
->recvd_hw_ready
) {
441 dev_err(dev
->dev
, "wait hw ready failed\n");
445 mei_me_hw_reset_release(dev
);
446 dev
->recvd_hw_ready
= false;
451 * mei_me_hw_start - hw start routine
454 * Return: 0 on success, error otherwise
456 static int mei_me_hw_start(struct mei_device
*dev
)
458 int ret
= mei_me_hw_ready_wait(dev
);
462 dev_dbg(dev
->dev
, "hw is ready\n");
464 mei_me_host_set_ready(dev
);
470 * mei_hbuf_filled_slots - gets number of device filled buffer slots
472 * @dev: the device structure
474 * Return: number of filled slots
476 static unsigned char mei_hbuf_filled_slots(struct mei_device
*dev
)
479 char read_ptr
, write_ptr
;
481 hcsr
= mei_hcsr_read(dev
);
483 read_ptr
= (char) ((hcsr
& H_CBRP
) >> 8);
484 write_ptr
= (char) ((hcsr
& H_CBWP
) >> 16);
486 return (unsigned char) (write_ptr
- read_ptr
);
490 * mei_me_hbuf_is_empty - checks if host buffer is empty.
492 * @dev: the device structure
494 * Return: true if empty, false - otherwise.
496 static bool mei_me_hbuf_is_empty(struct mei_device
*dev
)
498 return mei_hbuf_filled_slots(dev
) == 0;
502 * mei_me_hbuf_empty_slots - counts write empty slots.
504 * @dev: the device structure
506 * Return: -EOVERFLOW if overflow, otherwise empty slots count
508 static int mei_me_hbuf_empty_slots(struct mei_device
*dev
)
510 struct mei_me_hw
*hw
= to_me_hw(dev
);
511 unsigned char filled_slots
, empty_slots
;
513 filled_slots
= mei_hbuf_filled_slots(dev
);
514 empty_slots
= hw
->hbuf_depth
- filled_slots
;
516 /* check for overflow */
517 if (filled_slots
> hw
->hbuf_depth
)
524 * mei_me_hbuf_depth - returns depth of the hw buffer.
526 * @dev: the device structure
528 * Return: size of hw buffer in slots
530 static u32
mei_me_hbuf_depth(const struct mei_device
*dev
)
532 struct mei_me_hw
*hw
= to_me_hw(dev
);
534 return hw
->hbuf_depth
;
538 * mei_me_hbuf_write - writes a message to host hw buffer.
540 * @dev: the device structure
541 * @hdr: header of message
542 * @hdr_len: header length in bytes: must be multiplication of a slot (4bytes)
544 * @data_len: payload length in bytes
546 * Return: 0 if success, < 0 - otherwise.
548 static int mei_me_hbuf_write(struct mei_device
*dev
,
549 const void *hdr
, size_t hdr_len
,
550 const void *data
, size_t data_len
)
558 if (WARN_ON(!hdr
|| !data
|| hdr_len
& 0x3))
561 dev_dbg(dev
->dev
, MEI_HDR_FMT
, MEI_HDR_PRM((struct mei_msg_hdr
*)hdr
));
563 empty_slots
= mei_hbuf_empty_slots(dev
);
564 dev_dbg(dev
->dev
, "empty slots = %hu.\n", empty_slots
);
569 dw_cnt
= mei_data2slots(hdr_len
+ data_len
);
570 if (dw_cnt
> (u32
)empty_slots
)
574 for (i
= 0; i
< hdr_len
/ MEI_SLOT_SIZE
; i
++)
575 mei_me_hcbww_write(dev
, reg_buf
[i
]);
578 for (i
= 0; i
< data_len
/ MEI_SLOT_SIZE
; i
++)
579 mei_me_hcbww_write(dev
, reg_buf
[i
]);
581 rem
= data_len
& 0x3;
585 memcpy(®
, (const u8
*)data
+ data_len
- rem
, rem
);
586 mei_me_hcbww_write(dev
, reg
);
589 mei_hcsr_set_hig(dev
);
590 if (!mei_me_hw_is_ready(dev
))
597 * mei_me_count_full_read_slots - counts read full slots.
599 * @dev: the device structure
601 * Return: -EOVERFLOW if overflow, otherwise filled slots count
603 static int mei_me_count_full_read_slots(struct mei_device
*dev
)
606 char read_ptr
, write_ptr
;
607 unsigned char buffer_depth
, filled_slots
;
609 me_csr
= mei_me_mecsr_read(dev
);
610 buffer_depth
= (unsigned char)((me_csr
& ME_CBD_HRA
) >> 24);
611 read_ptr
= (char) ((me_csr
& ME_CBRP_HRA
) >> 8);
612 write_ptr
= (char) ((me_csr
& ME_CBWP_HRA
) >> 16);
613 filled_slots
= (unsigned char) (write_ptr
- read_ptr
);
615 /* check for overflow */
616 if (filled_slots
> buffer_depth
)
619 dev_dbg(dev
->dev
, "filled_slots =%08x\n", filled_slots
);
620 return (int)filled_slots
;
624 * mei_me_read_slots - reads a message from mei device.
626 * @dev: the device structure
627 * @buffer: message buffer will be written
628 * @buffer_length: message size will be read
632 static int mei_me_read_slots(struct mei_device
*dev
, unsigned char *buffer
,
633 unsigned long buffer_length
)
635 u32
*reg_buf
= (u32
*)buffer
;
637 for (; buffer_length
>= MEI_SLOT_SIZE
; buffer_length
-= MEI_SLOT_SIZE
)
638 *reg_buf
++ = mei_me_mecbrw_read(dev
);
640 if (buffer_length
> 0) {
641 u32 reg
= mei_me_mecbrw_read(dev
);
643 memcpy(reg_buf
, ®
, buffer_length
);
646 mei_hcsr_set_hig(dev
);
651 * mei_me_pg_set - write pg enter register
653 * @dev: the device structure
655 static void mei_me_pg_set(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 reg
|= H_HPG_CSR_PGI
;
665 trace_mei_reg_write(dev
->dev
, "H_HPG_CSR", H_HPG_CSR
, reg
);
666 mei_me_reg_write(hw
, H_HPG_CSR
, reg
);
670 * mei_me_pg_unset - write pg exit register
672 * @dev: the device structure
674 static void mei_me_pg_unset(struct mei_device
*dev
)
676 struct mei_me_hw
*hw
= to_me_hw(dev
);
679 reg
= mei_me_reg_read(hw
, H_HPG_CSR
);
680 trace_mei_reg_read(dev
->dev
, "H_HPG_CSR", H_HPG_CSR
, reg
);
682 WARN(!(reg
& H_HPG_CSR_PGI
), "PGI is not set\n");
684 reg
|= H_HPG_CSR_PGIHEXR
;
686 trace_mei_reg_write(dev
->dev
, "H_HPG_CSR", H_HPG_CSR
, reg
);
687 mei_me_reg_write(hw
, H_HPG_CSR
, reg
);
691 * mei_me_pg_legacy_enter_sync - perform legacy pg entry procedure
693 * @dev: the device structure
695 * Return: 0 on success an error code otherwise
697 static int mei_me_pg_legacy_enter_sync(struct mei_device
*dev
)
699 struct mei_me_hw
*hw
= to_me_hw(dev
);
700 unsigned long timeout
= mei_secs_to_jiffies(MEI_PGI_TIMEOUT
);
703 dev
->pg_event
= MEI_PG_EVENT_WAIT
;
705 ret
= mei_hbm_pg(dev
, MEI_PG_ISOLATION_ENTRY_REQ_CMD
);
709 mutex_unlock(&dev
->device_lock
);
710 wait_event_timeout(dev
->wait_pg
,
711 dev
->pg_event
== MEI_PG_EVENT_RECEIVED
, timeout
);
712 mutex_lock(&dev
->device_lock
);
714 if (dev
->pg_event
== MEI_PG_EVENT_RECEIVED
) {
721 dev
->pg_event
= MEI_PG_EVENT_IDLE
;
722 hw
->pg_state
= MEI_PG_ON
;
728 * mei_me_pg_legacy_exit_sync - perform legacy pg exit procedure
730 * @dev: the device structure
732 * Return: 0 on success an error code otherwise
734 static int mei_me_pg_legacy_exit_sync(struct mei_device
*dev
)
736 struct mei_me_hw
*hw
= to_me_hw(dev
);
737 unsigned long timeout
= mei_secs_to_jiffies(MEI_PGI_TIMEOUT
);
740 if (dev
->pg_event
== MEI_PG_EVENT_RECEIVED
)
743 dev
->pg_event
= MEI_PG_EVENT_WAIT
;
745 mei_me_pg_unset(dev
);
747 mutex_unlock(&dev
->device_lock
);
748 wait_event_timeout(dev
->wait_pg
,
749 dev
->pg_event
== MEI_PG_EVENT_RECEIVED
, timeout
);
750 mutex_lock(&dev
->device_lock
);
753 if (dev
->pg_event
!= MEI_PG_EVENT_RECEIVED
) {
758 dev
->pg_event
= MEI_PG_EVENT_INTR_WAIT
;
759 ret
= mei_hbm_pg(dev
, MEI_PG_ISOLATION_EXIT_RES_CMD
);
763 mutex_unlock(&dev
->device_lock
);
764 wait_event_timeout(dev
->wait_pg
,
765 dev
->pg_event
== MEI_PG_EVENT_INTR_RECEIVED
, timeout
);
766 mutex_lock(&dev
->device_lock
);
768 if (dev
->pg_event
== MEI_PG_EVENT_INTR_RECEIVED
)
774 dev
->pg_event
= MEI_PG_EVENT_IDLE
;
775 hw
->pg_state
= MEI_PG_OFF
;
781 * mei_me_pg_in_transition - is device now in pg transition
783 * @dev: the device structure
785 * Return: true if in pg transition, false otherwise
787 static bool mei_me_pg_in_transition(struct mei_device
*dev
)
789 return dev
->pg_event
>= MEI_PG_EVENT_WAIT
&&
790 dev
->pg_event
<= MEI_PG_EVENT_INTR_WAIT
;
794 * mei_me_pg_is_enabled - detect if PG is supported by HW
796 * @dev: the device structure
798 * Return: true is pg supported, false otherwise
800 static bool mei_me_pg_is_enabled(struct mei_device
*dev
)
802 struct mei_me_hw
*hw
= to_me_hw(dev
);
803 u32 reg
= mei_me_mecsr_read(dev
);
805 if (hw
->d0i3_supported
)
808 if ((reg
& ME_PGIC_HRA
) == 0)
811 if (!dev
->hbm_f_pg_supported
)
817 dev_dbg(dev
->dev
, "pg: not supported: d0i3 = %d HGP = %d hbm version %d.%d ?= %d.%d\n",
819 !!(reg
& ME_PGIC_HRA
),
820 dev
->version
.major_version
,
821 dev
->version
.minor_version
,
822 HBM_MAJOR_VERSION_PGI
,
823 HBM_MINOR_VERSION_PGI
);
829 * mei_me_d0i3_set - write d0i3 register bit on mei device.
831 * @dev: the device structure
832 * @intr: ask for interrupt
834 * Return: D0I3C register value
836 static u32
mei_me_d0i3_set(struct mei_device
*dev
, bool intr
)
838 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_unset - clean d0i3 register bit on mei device.
854 * @dev: the device structure
856 * Return: D0I3C register value
858 static u32
mei_me_d0i3_unset(struct mei_device
*dev
)
860 u32 reg
= mei_me_d0i3c_read(dev
);
864 mei_me_d0i3c_write(dev
, reg
);
865 /* read it to ensure HW consistency */
866 reg
= mei_me_d0i3c_read(dev
);
871 * mei_me_d0i3_enter_sync - perform d0i3 entry procedure
873 * @dev: the device structure
875 * Return: 0 on success an error code otherwise
877 static int mei_me_d0i3_enter_sync(struct mei_device
*dev
)
879 struct mei_me_hw
*hw
= to_me_hw(dev
);
880 unsigned long d0i3_timeout
= mei_secs_to_jiffies(MEI_D0I3_TIMEOUT
);
881 unsigned long pgi_timeout
= mei_secs_to_jiffies(MEI_PGI_TIMEOUT
);
885 reg
= mei_me_d0i3c_read(dev
);
886 if (reg
& H_D0I3C_I3
) {
887 /* we are in d0i3, nothing to do */
888 dev_dbg(dev
->dev
, "d0i3 set not needed\n");
893 /* PGI entry procedure */
894 dev
->pg_event
= MEI_PG_EVENT_WAIT
;
896 ret
= mei_hbm_pg(dev
, MEI_PG_ISOLATION_ENTRY_REQ_CMD
);
898 /* FIXME: should we reset here? */
901 mutex_unlock(&dev
->device_lock
);
902 wait_event_timeout(dev
->wait_pg
,
903 dev
->pg_event
== MEI_PG_EVENT_RECEIVED
, pgi_timeout
);
904 mutex_lock(&dev
->device_lock
);
906 if (dev
->pg_event
!= MEI_PG_EVENT_RECEIVED
) {
910 /* end PGI entry procedure */
912 dev
->pg_event
= MEI_PG_EVENT_INTR_WAIT
;
914 reg
= mei_me_d0i3_set(dev
, true);
915 if (!(reg
& H_D0I3C_CIP
)) {
916 dev_dbg(dev
->dev
, "d0i3 enter wait not needed\n");
921 mutex_unlock(&dev
->device_lock
);
922 wait_event_timeout(dev
->wait_pg
,
923 dev
->pg_event
== MEI_PG_EVENT_INTR_RECEIVED
, d0i3_timeout
);
924 mutex_lock(&dev
->device_lock
);
926 if (dev
->pg_event
!= MEI_PG_EVENT_INTR_RECEIVED
) {
927 reg
= mei_me_d0i3c_read(dev
);
928 if (!(reg
& H_D0I3C_I3
)) {
936 hw
->pg_state
= MEI_PG_ON
;
938 dev
->pg_event
= MEI_PG_EVENT_IDLE
;
939 dev_dbg(dev
->dev
, "d0i3 enter ret = %d\n", ret
);
944 * mei_me_d0i3_enter - perform d0i3 entry procedure
945 * no hbm PG handshake
946 * no waiting for confirmation; runs with interrupts
949 * @dev: the device structure
951 * Return: 0 on success an error code otherwise
953 static int mei_me_d0i3_enter(struct mei_device
*dev
)
955 struct mei_me_hw
*hw
= to_me_hw(dev
);
958 reg
= mei_me_d0i3c_read(dev
);
959 if (reg
& H_D0I3C_I3
) {
960 /* we are in d0i3, nothing to do */
961 dev_dbg(dev
->dev
, "already d0i3 : set not needed\n");
965 mei_me_d0i3_set(dev
, false);
967 hw
->pg_state
= MEI_PG_ON
;
968 dev
->pg_event
= MEI_PG_EVENT_IDLE
;
969 dev_dbg(dev
->dev
, "d0i3 enter\n");
974 * mei_me_d0i3_exit_sync - perform d0i3 exit procedure
976 * @dev: the device structure
978 * Return: 0 on success an error code otherwise
980 static int mei_me_d0i3_exit_sync(struct mei_device
*dev
)
982 struct mei_me_hw
*hw
= to_me_hw(dev
);
983 unsigned long timeout
= mei_secs_to_jiffies(MEI_D0I3_TIMEOUT
);
987 dev
->pg_event
= MEI_PG_EVENT_INTR_WAIT
;
989 reg
= mei_me_d0i3c_read(dev
);
990 if (!(reg
& H_D0I3C_I3
)) {
991 /* we are not in d0i3, nothing to do */
992 dev_dbg(dev
->dev
, "d0i3 exit not needed\n");
997 reg
= mei_me_d0i3_unset(dev
);
998 if (!(reg
& H_D0I3C_CIP
)) {
999 dev_dbg(dev
->dev
, "d0i3 exit wait not needed\n");
1004 mutex_unlock(&dev
->device_lock
);
1005 wait_event_timeout(dev
->wait_pg
,
1006 dev
->pg_event
== MEI_PG_EVENT_INTR_RECEIVED
, timeout
);
1007 mutex_lock(&dev
->device_lock
);
1009 if (dev
->pg_event
!= MEI_PG_EVENT_INTR_RECEIVED
) {
1010 reg
= mei_me_d0i3c_read(dev
);
1011 if (reg
& H_D0I3C_I3
) {
1019 hw
->pg_state
= MEI_PG_OFF
;
1021 dev
->pg_event
= MEI_PG_EVENT_IDLE
;
1023 dev_dbg(dev
->dev
, "d0i3 exit ret = %d\n", ret
);
1028 * mei_me_pg_legacy_intr - perform legacy pg processing
1029 * in interrupt thread handler
1031 * @dev: the device structure
1033 static void mei_me_pg_legacy_intr(struct mei_device
*dev
)
1035 struct mei_me_hw
*hw
= to_me_hw(dev
);
1037 if (dev
->pg_event
!= MEI_PG_EVENT_INTR_WAIT
)
1040 dev
->pg_event
= MEI_PG_EVENT_INTR_RECEIVED
;
1041 hw
->pg_state
= MEI_PG_OFF
;
1042 if (waitqueue_active(&dev
->wait_pg
))
1043 wake_up(&dev
->wait_pg
);
1047 * mei_me_d0i3_intr - perform d0i3 processing in interrupt thread handler
1049 * @dev: the device structure
1050 * @intr_source: interrupt source
1052 static void mei_me_d0i3_intr(struct mei_device
*dev
, u32 intr_source
)
1054 struct mei_me_hw
*hw
= to_me_hw(dev
);
1056 if (dev
->pg_event
== MEI_PG_EVENT_INTR_WAIT
&&
1057 (intr_source
& H_D0I3C_IS
)) {
1058 dev
->pg_event
= MEI_PG_EVENT_INTR_RECEIVED
;
1059 if (hw
->pg_state
== MEI_PG_ON
) {
1060 hw
->pg_state
= MEI_PG_OFF
;
1061 if (dev
->hbm_state
!= MEI_HBM_IDLE
) {
1063 * force H_RDY because it could be
1064 * wiped off during PG
1066 dev_dbg(dev
->dev
, "d0i3 set host ready\n");
1067 mei_me_host_set_ready(dev
);
1070 hw
->pg_state
= MEI_PG_ON
;
1073 wake_up(&dev
->wait_pg
);
1076 if (hw
->pg_state
== MEI_PG_ON
&& (intr_source
& H_IS
)) {
1078 * HW sent some data and we are in D0i3, so
1079 * we got here because of HW initiated exit from D0i3.
1080 * Start runtime pm resume sequence to exit low power state.
1082 dev_dbg(dev
->dev
, "d0i3 want resume\n");
1083 mei_hbm_pg_resume(dev
);
1088 * mei_me_pg_intr - perform pg processing in interrupt thread handler
1090 * @dev: the device structure
1091 * @intr_source: interrupt source
1093 static void mei_me_pg_intr(struct mei_device
*dev
, u32 intr_source
)
1095 struct mei_me_hw
*hw
= to_me_hw(dev
);
1097 if (hw
->d0i3_supported
)
1098 mei_me_d0i3_intr(dev
, intr_source
);
1100 mei_me_pg_legacy_intr(dev
);
1104 * mei_me_pg_enter_sync - perform runtime pm entry procedure
1106 * @dev: the device structure
1108 * Return: 0 on success an error code otherwise
1110 int mei_me_pg_enter_sync(struct mei_device
*dev
)
1112 struct mei_me_hw
*hw
= to_me_hw(dev
);
1114 if (hw
->d0i3_supported
)
1115 return mei_me_d0i3_enter_sync(dev
);
1117 return mei_me_pg_legacy_enter_sync(dev
);
1121 * mei_me_pg_exit_sync - perform runtime pm exit procedure
1123 * @dev: the device structure
1125 * Return: 0 on success an error code otherwise
1127 int mei_me_pg_exit_sync(struct mei_device
*dev
)
1129 struct mei_me_hw
*hw
= to_me_hw(dev
);
1131 if (hw
->d0i3_supported
)
1132 return mei_me_d0i3_exit_sync(dev
);
1134 return mei_me_pg_legacy_exit_sync(dev
);
1138 * mei_me_hw_reset - resets fw via mei csr register.
1140 * @dev: the device structure
1141 * @intr_enable: if interrupt should be enabled after reset.
1143 * Return: 0 on success an error code otherwise
1145 static int mei_me_hw_reset(struct mei_device
*dev
, bool intr_enable
)
1147 struct mei_me_hw
*hw
= to_me_hw(dev
);
1152 mei_me_intr_enable(dev
);
1153 if (hw
->d0i3_supported
) {
1154 ret
= mei_me_d0i3_exit_sync(dev
);
1160 pm_runtime_set_active(dev
->dev
);
1162 hcsr
= mei_hcsr_read(dev
);
1163 /* H_RST may be found lit before reset is started,
1164 * for example if preceding reset flow hasn't completed.
1165 * In that case asserting H_RST will be ignored, therefore
1166 * we need to clean H_RST bit to start a successful reset sequence.
1168 if ((hcsr
& H_RST
) == H_RST
) {
1169 dev_warn(dev
->dev
, "H_RST is set = 0x%08X", hcsr
);
1171 mei_hcsr_set(dev
, hcsr
);
1172 hcsr
= mei_hcsr_read(dev
);
1175 hcsr
|= H_RST
| H_IG
| H_CSR_IS_MASK
;
1178 hcsr
&= ~H_CSR_IE_MASK
;
1180 dev
->recvd_hw_ready
= false;
1181 mei_hcsr_write(dev
, hcsr
);
1184 * Host reads the H_CSR once to ensure that the
1185 * posted write to H_CSR completes.
1187 hcsr
= mei_hcsr_read(dev
);
1189 if ((hcsr
& H_RST
) == 0)
1190 dev_warn(dev
->dev
, "H_RST is not set = 0x%08X", hcsr
);
1192 if ((hcsr
& H_RDY
) == H_RDY
)
1193 dev_warn(dev
->dev
, "H_RDY is not cleared 0x%08X", hcsr
);
1196 mei_me_hw_reset_release(dev
);
1197 if (hw
->d0i3_supported
) {
1198 ret
= mei_me_d0i3_enter(dev
);
1207 * mei_me_irq_quick_handler - The ISR of the MEI device
1209 * @irq: The irq number
1210 * @dev_id: pointer to the device structure
1212 * Return: irqreturn_t
1214 irqreturn_t
mei_me_irq_quick_handler(int irq
, void *dev_id
)
1216 struct mei_device
*dev
= (struct mei_device
*)dev_id
;
1219 hcsr
= mei_hcsr_read(dev
);
1220 if (!me_intr_src(hcsr
))
1223 dev_dbg(dev
->dev
, "interrupt source 0x%08X\n", me_intr_src(hcsr
));
1225 /* disable interrupts on device */
1226 me_intr_disable(dev
, hcsr
);
1227 return IRQ_WAKE_THREAD
;
1231 * mei_me_irq_thread_handler - function called after ISR to handle the interrupt
1234 * @irq: The irq number
1235 * @dev_id: pointer to the device structure
1237 * Return: irqreturn_t
1240 irqreturn_t
mei_me_irq_thread_handler(int irq
, void *dev_id
)
1242 struct mei_device
*dev
= (struct mei_device
*) dev_id
;
1243 struct list_head cmpl_list
;
1248 dev_dbg(dev
->dev
, "function called after ISR to handle the interrupt processing.\n");
1249 /* initialize our complete list */
1250 mutex_lock(&dev
->device_lock
);
1252 hcsr
= mei_hcsr_read(dev
);
1253 me_intr_clear(dev
, hcsr
);
1255 INIT_LIST_HEAD(&cmpl_list
);
1257 /* check if ME wants a reset */
1258 if (!mei_hw_is_ready(dev
) && dev
->dev_state
!= MEI_DEV_RESETTING
) {
1259 dev_warn(dev
->dev
, "FW not ready: resetting.\n");
1260 schedule_work(&dev
->reset_work
);
1264 if (mei_me_hw_is_resetting(dev
))
1265 mei_hcsr_set_hig(dev
);
1267 mei_me_pg_intr(dev
, me_intr_src(hcsr
));
1269 /* check if we need to start the dev */
1270 if (!mei_host_is_ready(dev
)) {
1271 if (mei_hw_is_ready(dev
)) {
1272 dev_dbg(dev
->dev
, "we need to start the dev.\n");
1273 dev
->recvd_hw_ready
= true;
1274 wake_up(&dev
->wait_hw_ready
);
1276 dev_dbg(dev
->dev
, "Spurious Interrupt\n");
1280 /* check slots available for reading */
1281 slots
= mei_count_full_read_slots(dev
);
1283 dev_dbg(dev
->dev
, "slots to read = %08x\n", slots
);
1284 rets
= mei_irq_read_handler(dev
, &cmpl_list
, &slots
);
1285 /* There is a race between ME write and interrupt delivery:
1286 * Not all data is always available immediately after the
1287 * interrupt, so try to read again on the next interrupt.
1289 if (rets
== -ENODATA
)
1293 (dev
->dev_state
!= MEI_DEV_RESETTING
&&
1294 dev
->dev_state
!= MEI_DEV_POWER_DOWN
)) {
1295 dev_err(dev
->dev
, "mei_irq_read_handler ret = %d.\n",
1297 schedule_work(&dev
->reset_work
);
1302 dev
->hbuf_is_ready
= mei_hbuf_is_ready(dev
);
1305 * During PG handshake only allowed write is the replay to the
1306 * PG exit message, so block calling write function
1307 * if the pg event is in PG handshake
1309 if (dev
->pg_event
!= MEI_PG_EVENT_WAIT
&&
1310 dev
->pg_event
!= MEI_PG_EVENT_RECEIVED
) {
1311 rets
= mei_irq_write_handler(dev
, &cmpl_list
);
1312 dev
->hbuf_is_ready
= mei_hbuf_is_ready(dev
);
1315 mei_irq_compl_handler(dev
, &cmpl_list
);
1318 dev_dbg(dev
->dev
, "interrupt thread end ret = %d\n", rets
);
1319 mei_me_intr_enable(dev
);
1320 mutex_unlock(&dev
->device_lock
);
1324 static const struct mei_hw_ops mei_me_hw_ops
= {
1326 .trc_status
= mei_me_trc_status
,
1327 .fw_status
= mei_me_fw_status
,
1328 .pg_state
= mei_me_pg_state
,
1330 .host_is_ready
= mei_me_host_is_ready
,
1332 .hw_is_ready
= mei_me_hw_is_ready
,
1333 .hw_reset
= mei_me_hw_reset
,
1334 .hw_config
= mei_me_hw_config
,
1335 .hw_start
= mei_me_hw_start
,
1337 .pg_in_transition
= mei_me_pg_in_transition
,
1338 .pg_is_enabled
= mei_me_pg_is_enabled
,
1340 .intr_clear
= mei_me_intr_clear
,
1341 .intr_enable
= mei_me_intr_enable
,
1342 .intr_disable
= mei_me_intr_disable
,
1343 .synchronize_irq
= mei_me_synchronize_irq
,
1345 .hbuf_free_slots
= mei_me_hbuf_empty_slots
,
1346 .hbuf_is_ready
= mei_me_hbuf_is_empty
,
1347 .hbuf_depth
= mei_me_hbuf_depth
,
1349 .write
= mei_me_hbuf_write
,
1351 .rdbuf_full_slots
= mei_me_count_full_read_slots
,
1352 .read_hdr
= mei_me_mecbrw_read
,
1353 .read
= mei_me_read_slots
1357 * mei_me_fw_type_nm() - check for nm sku
1359 * Read ME FW Status register to check for the Node Manager (NM) Firmware.
1360 * The NM FW is only signaled in PCI function 0.
1361 * __Note__: Deprecated by PCH8 and newer.
1365 * Return: true in case of NM firmware
1367 static bool mei_me_fw_type_nm(const struct pci_dev
*pdev
)
1372 devfn
= PCI_DEVFN(PCI_SLOT(pdev
->devfn
), 0);
1373 pci_bus_read_config_dword(pdev
->bus
, devfn
, PCI_CFG_HFS_2
, ®
);
1374 trace_mei_pci_cfg_read(&pdev
->dev
, "PCI_CFG_HFS_2", PCI_CFG_HFS_2
, reg
);
1375 /* make sure that bit 9 (NM) is up and bit 10 (DM) is down */
1376 return (reg
& 0x600) == 0x200;
1379 #define MEI_CFG_FW_NM \
1380 .quirk_probe = mei_me_fw_type_nm
1383 * mei_me_fw_sku_sps_4() - check for sps 4.0 sku
1385 * Read ME FW Status register to check for SPS Firmware.
1386 * The SPS FW is only signaled in the PCI function 0.
1387 * __Note__: Deprecated by SPS 5.0 and newer.
1391 * Return: true in case of SPS firmware
1393 static bool mei_me_fw_type_sps_4(const struct pci_dev
*pdev
)
1398 devfn
= PCI_DEVFN(PCI_SLOT(pdev
->devfn
), 0);
1399 pci_bus_read_config_dword(pdev
->bus
, devfn
, PCI_CFG_HFS_1
, ®
);
1400 trace_mei_pci_cfg_read(&pdev
->dev
, "PCI_CFG_HFS_1", PCI_CFG_HFS_1
, reg
);
1401 return (reg
& PCI_CFG_HFS_1_OPMODE_MSK
) == PCI_CFG_HFS_1_OPMODE_SPS
;
1404 #define MEI_CFG_FW_SPS_4 \
1405 .quirk_probe = mei_me_fw_type_sps_4
1408 * mei_me_fw_sku_sps() - check for sps sku
1410 * Read ME FW Status register to check for SPS Firmware.
1411 * The SPS FW is only signaled in pci function 0
1415 * Return: true in case of SPS firmware
1417 static bool mei_me_fw_type_sps(const struct pci_dev
*pdev
)
1423 devfn
= PCI_DEVFN(PCI_SLOT(pdev
->devfn
), 0);
1424 pci_bus_read_config_dword(pdev
->bus
, devfn
, PCI_CFG_HFS_3
, ®
);
1425 trace_mei_pci_cfg_read(&pdev
->dev
, "PCI_CFG_HFS_3", PCI_CFG_HFS_3
, reg
);
1426 fw_type
= (reg
& PCI_CFG_HFS_3_FW_SKU_MSK
);
1428 dev_dbg(&pdev
->dev
, "fw type is %d\n", fw_type
);
1430 return fw_type
== PCI_CFG_HFS_3_FW_SKU_SPS
;
1433 #define MEI_CFG_KIND_ITOUCH \
1436 #define MEI_CFG_FW_SPS \
1437 .quirk_probe = mei_me_fw_type_sps
1439 #define MEI_CFG_FW_VER_SUPP \
1440 .fw_ver_supported = 1
1442 #define MEI_CFG_ICH_HFS \
1443 .fw_status.count = 0
1445 #define MEI_CFG_ICH10_HFS \
1446 .fw_status.count = 1, \
1447 .fw_status.status[0] = PCI_CFG_HFS_1
1449 #define MEI_CFG_PCH_HFS \
1450 .fw_status.count = 2, \
1451 .fw_status.status[0] = PCI_CFG_HFS_1, \
1452 .fw_status.status[1] = PCI_CFG_HFS_2
1454 #define MEI_CFG_PCH8_HFS \
1455 .fw_status.count = 6, \
1456 .fw_status.status[0] = PCI_CFG_HFS_1, \
1457 .fw_status.status[1] = PCI_CFG_HFS_2, \
1458 .fw_status.status[2] = PCI_CFG_HFS_3, \
1459 .fw_status.status[3] = PCI_CFG_HFS_4, \
1460 .fw_status.status[4] = PCI_CFG_HFS_5, \
1461 .fw_status.status[5] = PCI_CFG_HFS_6
1463 #define MEI_CFG_DMA_128 \
1464 .dma_size[DMA_DSCR_HOST] = SZ_128K, \
1465 .dma_size[DMA_DSCR_DEVICE] = SZ_128K, \
1466 .dma_size[DMA_DSCR_CTRL] = PAGE_SIZE
1468 #define MEI_CFG_TRC \
1469 .hw_trc_supported = 1
1471 /* ICH Legacy devices */
1472 static const struct mei_cfg mei_me_ich_cfg
= {
1477 static const struct mei_cfg mei_me_ich10_cfg
= {
1482 static const struct mei_cfg mei_me_pch6_cfg
= {
1487 static const struct mei_cfg mei_me_pch7_cfg
= {
1489 MEI_CFG_FW_VER_SUPP
,
1492 /* PCH Cougar Point and Patsburg with quirk for Node Manager exclusion */
1493 static const struct mei_cfg mei_me_pch_cpt_pbg_cfg
= {
1495 MEI_CFG_FW_VER_SUPP
,
1499 /* PCH8 Lynx Point and newer devices */
1500 static const struct mei_cfg mei_me_pch8_cfg
= {
1502 MEI_CFG_FW_VER_SUPP
,
1505 /* PCH8 Lynx Point and newer devices - iTouch */
1506 static const struct mei_cfg mei_me_pch8_itouch_cfg
= {
1507 MEI_CFG_KIND_ITOUCH
,
1509 MEI_CFG_FW_VER_SUPP
,
1512 /* PCH8 Lynx Point with quirk for SPS Firmware exclusion */
1513 static const struct mei_cfg mei_me_pch8_sps_4_cfg
= {
1515 MEI_CFG_FW_VER_SUPP
,
1519 /* LBG with quirk for SPS (4.0) Firmware exclusion */
1520 static const struct mei_cfg mei_me_pch12_sps_4_cfg
= {
1522 MEI_CFG_FW_VER_SUPP
,
1526 /* Cannon Lake and newer devices */
1527 static const struct mei_cfg mei_me_pch12_cfg
= {
1529 MEI_CFG_FW_VER_SUPP
,
1533 /* Cannon Lake with quirk for SPS 5.0 and newer Firmware exclusion */
1534 static const struct mei_cfg mei_me_pch12_sps_cfg
= {
1536 MEI_CFG_FW_VER_SUPP
,
1541 /* Cannon Lake itouch with quirk for SPS 5.0 and newer Firmware exclusion
1544 static const struct mei_cfg mei_me_pch12_itouch_sps_cfg
= {
1545 MEI_CFG_KIND_ITOUCH
,
1547 MEI_CFG_FW_VER_SUPP
,
1551 /* Tiger Lake and newer devices */
1552 static const struct mei_cfg mei_me_pch15_cfg
= {
1554 MEI_CFG_FW_VER_SUPP
,
1559 /* Tiger Lake with quirk for SPS 5.0 and newer Firmware exclusion */
1560 static const struct mei_cfg mei_me_pch15_sps_cfg
= {
1562 MEI_CFG_FW_VER_SUPP
,
1569 * mei_cfg_list - A list of platform platform specific configurations.
1570 * Note: has to be synchronized with enum mei_cfg_idx.
1572 static const struct mei_cfg
*const mei_cfg_list
[] = {
1573 [MEI_ME_UNDEF_CFG
] = NULL
,
1574 [MEI_ME_ICH_CFG
] = &mei_me_ich_cfg
,
1575 [MEI_ME_ICH10_CFG
] = &mei_me_ich10_cfg
,
1576 [MEI_ME_PCH6_CFG
] = &mei_me_pch6_cfg
,
1577 [MEI_ME_PCH7_CFG
] = &mei_me_pch7_cfg
,
1578 [MEI_ME_PCH_CPT_PBG_CFG
] = &mei_me_pch_cpt_pbg_cfg
,
1579 [MEI_ME_PCH8_CFG
] = &mei_me_pch8_cfg
,
1580 [MEI_ME_PCH8_ITOUCH_CFG
] = &mei_me_pch8_itouch_cfg
,
1581 [MEI_ME_PCH8_SPS_4_CFG
] = &mei_me_pch8_sps_4_cfg
,
1582 [MEI_ME_PCH12_CFG
] = &mei_me_pch12_cfg
,
1583 [MEI_ME_PCH12_SPS_4_CFG
] = &mei_me_pch12_sps_4_cfg
,
1584 [MEI_ME_PCH12_SPS_CFG
] = &mei_me_pch12_sps_cfg
,
1585 [MEI_ME_PCH12_SPS_ITOUCH_CFG
] = &mei_me_pch12_itouch_sps_cfg
,
1586 [MEI_ME_PCH15_CFG
] = &mei_me_pch15_cfg
,
1587 [MEI_ME_PCH15_SPS_CFG
] = &mei_me_pch15_sps_cfg
,
1590 const struct mei_cfg
*mei_me_get_cfg(kernel_ulong_t idx
)
1592 BUILD_BUG_ON(ARRAY_SIZE(mei_cfg_list
) != MEI_ME_NUM_CFG
);
1594 if (idx
>= MEI_ME_NUM_CFG
)
1597 return mei_cfg_list
[idx
];
1601 * mei_me_dev_init - allocates and initializes the mei device structure
1603 * @parent: device associated with physical device (pci/platform)
1604 * @cfg: per device generation config
1606 * Return: The mei_device pointer on success, NULL on failure.
1608 struct mei_device
*mei_me_dev_init(struct device
*parent
,
1609 const struct mei_cfg
*cfg
)
1611 struct mei_device
*dev
;
1612 struct mei_me_hw
*hw
;
1615 dev
= devm_kzalloc(parent
, sizeof(*dev
) + sizeof(*hw
), GFP_KERNEL
);
1621 for (i
= 0; i
< DMA_DSCR_NUM
; i
++)
1622 dev
->dr_dscr
[i
].size
= cfg
->dma_size
[i
];
1624 mei_device_init(dev
, parent
, &mei_me_hw_ops
);
1627 dev
->fw_f_fw_ver_supported
= cfg
->fw_ver_supported
;
1629 dev
->kind
= cfg
->kind
;