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>
27 #include "hw-me-regs.h"
29 #include "mei-trace.h"
32 * mei_me_reg_read - Reads 32bit data from the mei device
34 * @hw: the me hardware structure
35 * @offset: offset from which to read the data
37 * Return: register value (u32)
39 static inline u32
mei_me_reg_read(const struct mei_me_hw
*hw
,
42 return ioread32(hw
->mem_addr
+ offset
);
47 * mei_me_reg_write - Writes 32bit data to the mei device
49 * @hw: the me hardware structure
50 * @offset: offset from which to write the data
51 * @value: register value to write (u32)
53 static inline void mei_me_reg_write(const struct mei_me_hw
*hw
,
54 unsigned long offset
, u32 value
)
56 iowrite32(value
, hw
->mem_addr
+ offset
);
60 * mei_me_mecbrw_read - Reads 32bit data from ME circular buffer
61 * read window register
63 * @dev: the device structure
65 * Return: ME_CB_RW register value (u32)
67 static inline u32
mei_me_mecbrw_read(const struct mei_device
*dev
)
69 return mei_me_reg_read(to_me_hw(dev
), ME_CB_RW
);
73 * mei_me_hcbww_write - write 32bit data to the host circular buffer
75 * @dev: the device structure
76 * @data: 32bit data to be written to the host circular buffer
78 static inline void mei_me_hcbww_write(struct mei_device
*dev
, u32 data
)
80 mei_me_reg_write(to_me_hw(dev
), H_CB_WW
, data
);
84 * mei_me_mecsr_read - Reads 32bit data from the ME CSR
86 * @dev: the device structure
88 * Return: ME_CSR_HA register value (u32)
90 static inline u32
mei_me_mecsr_read(const struct mei_device
*dev
)
94 reg
= mei_me_reg_read(to_me_hw(dev
), ME_CSR_HA
);
95 trace_mei_reg_read(dev
->dev
, "ME_CSR_HA", ME_CSR_HA
, reg
);
101 * mei_hcsr_read - Reads 32bit data from the host CSR
103 * @dev: the device structure
105 * Return: H_CSR register value (u32)
107 static inline u32
mei_hcsr_read(const struct mei_device
*dev
)
111 reg
= mei_me_reg_read(to_me_hw(dev
), H_CSR
);
112 trace_mei_reg_read(dev
->dev
, "H_CSR", H_CSR
, reg
);
118 * mei_hcsr_write - writes H_CSR register to the mei device
120 * @dev: the device structure
121 * @reg: new register value
123 static inline void mei_hcsr_write(struct mei_device
*dev
, u32 reg
)
125 trace_mei_reg_write(dev
->dev
, "H_CSR", H_CSR
, reg
);
126 mei_me_reg_write(to_me_hw(dev
), H_CSR
, reg
);
130 * mei_hcsr_set - writes H_CSR register to the mei device,
131 * and ignores the H_IS bit for it is write-one-to-zero.
133 * @dev: the device structure
134 * @reg: new register value
136 static inline void mei_hcsr_set(struct mei_device
*dev
, u32 reg
)
138 reg
&= ~H_CSR_IS_MASK
;
139 mei_hcsr_write(dev
, reg
);
143 * mei_me_d0i3c_read - Reads 32bit data from the D0I3C register
145 * @dev: the device structure
147 * Return: H_D0I3C register value (u32)
149 static inline u32
mei_me_d0i3c_read(const struct mei_device
*dev
)
153 reg
= mei_me_reg_read(to_me_hw(dev
), H_D0I3C
);
154 trace_mei_reg_read(dev
->dev
, "H_D0I3C", H_D0I3C
, reg
);
160 * mei_me_d0i3c_write - writes H_D0I3C register to device
162 * @dev: the device structure
163 * @reg: new register value
165 static inline void mei_me_d0i3c_write(struct mei_device
*dev
, u32 reg
)
167 trace_mei_reg_write(dev
->dev
, "H_D0I3C", H_D0I3C
, reg
);
168 mei_me_reg_write(to_me_hw(dev
), H_D0I3C
, reg
);
172 * mei_me_fw_status - read fw status register from pci config space
175 * @fw_status: fw status register values
177 * Return: 0 on success, error otherwise
179 static int mei_me_fw_status(struct mei_device
*dev
,
180 struct mei_fw_status
*fw_status
)
182 struct pci_dev
*pdev
= to_pci_dev(dev
->dev
);
183 struct mei_me_hw
*hw
= to_me_hw(dev
);
184 const struct mei_fw_status
*fw_src
= &hw
->cfg
->fw_status
;
191 fw_status
->count
= fw_src
->count
;
192 for (i
= 0; i
< fw_src
->count
&& i
< MEI_FW_STATUS_MAX
; i
++) {
193 ret
= pci_read_config_dword(pdev
, fw_src
->status
[i
],
194 &fw_status
->status
[i
]);
195 trace_mei_pci_cfg_read(dev
->dev
, "PCI_CFG_HSF_X",
197 fw_status
->status
[i
]);
206 * mei_me_hw_config - configure hw dependent settings
210 static void mei_me_hw_config(struct mei_device
*dev
)
212 struct pci_dev
*pdev
= to_pci_dev(dev
->dev
);
213 struct mei_me_hw
*hw
= to_me_hw(dev
);
216 /* Doesn't change in runtime */
217 hcsr
= mei_hcsr_read(dev
);
218 dev
->hbuf_depth
= (hcsr
& H_CBD
) >> 24;
221 pci_read_config_dword(pdev
, PCI_CFG_HFS_1
, ®
);
222 trace_mei_pci_cfg_read(dev
->dev
, "PCI_CFG_HFS_1", PCI_CFG_HFS_1
, reg
);
224 ((reg
& PCI_CFG_HFS_1_D0I3_MSK
) == PCI_CFG_HFS_1_D0I3_MSK
);
226 hw
->pg_state
= MEI_PG_OFF
;
227 if (hw
->d0i3_supported
) {
228 reg
= mei_me_d0i3c_read(dev
);
229 if (reg
& H_D0I3C_I3
)
230 hw
->pg_state
= MEI_PG_ON
;
235 * mei_me_pg_state - translate internal pg state
236 * to the mei power gating state
240 * Return: MEI_PG_OFF if aliveness is on and MEI_PG_ON otherwise
242 static inline enum mei_pg_state
mei_me_pg_state(struct mei_device
*dev
)
244 struct mei_me_hw
*hw
= to_me_hw(dev
);
249 static inline u32
me_intr_src(u32 hcsr
)
251 return hcsr
& H_CSR_IS_MASK
;
255 * me_intr_disable - disables mei device interrupts
256 * using supplied hcsr register value.
258 * @dev: the device structure
259 * @hcsr: supplied hcsr register value
261 static inline void me_intr_disable(struct mei_device
*dev
, u32 hcsr
)
263 hcsr
&= ~H_CSR_IE_MASK
;
264 mei_hcsr_set(dev
, hcsr
);
268 * mei_me_intr_clear - clear and stop interrupts
270 * @dev: the device structure
271 * @hcsr: supplied hcsr register value
273 static inline void me_intr_clear(struct mei_device
*dev
, u32 hcsr
)
275 if (me_intr_src(hcsr
))
276 mei_hcsr_write(dev
, hcsr
);
280 * mei_me_intr_clear - clear and stop interrupts
282 * @dev: the device structure
284 static void mei_me_intr_clear(struct mei_device
*dev
)
286 u32 hcsr
= mei_hcsr_read(dev
);
288 me_intr_clear(dev
, hcsr
);
291 * mei_me_intr_enable - enables mei device interrupts
293 * @dev: the device structure
295 static void mei_me_intr_enable(struct mei_device
*dev
)
297 u32 hcsr
= mei_hcsr_read(dev
);
299 hcsr
|= H_CSR_IE_MASK
;
300 mei_hcsr_set(dev
, hcsr
);
304 * mei_me_intr_disable - disables mei device interrupts
306 * @dev: the device structure
308 static void mei_me_intr_disable(struct mei_device
*dev
)
310 u32 hcsr
= mei_hcsr_read(dev
);
312 me_intr_disable(dev
, hcsr
);
316 * mei_me_synchronize_irq - wait for pending IRQ handlers
318 * @dev: the device structure
320 static void mei_me_synchronize_irq(struct mei_device
*dev
)
322 struct pci_dev
*pdev
= to_pci_dev(dev
->dev
);
324 synchronize_irq(pdev
->irq
);
328 * mei_me_hw_reset_release - release device from the reset
330 * @dev: the device structure
332 static void mei_me_hw_reset_release(struct mei_device
*dev
)
334 u32 hcsr
= mei_hcsr_read(dev
);
338 mei_hcsr_set(dev
, hcsr
);
340 /* complete this write before we set host ready on another CPU */
345 * mei_me_host_set_ready - enable device
349 static void mei_me_host_set_ready(struct mei_device
*dev
)
351 u32 hcsr
= mei_hcsr_read(dev
);
353 hcsr
|= H_CSR_IE_MASK
| H_IG
| H_RDY
;
354 mei_hcsr_set(dev
, hcsr
);
358 * mei_me_host_is_ready - check whether the host has turned ready
363 static bool mei_me_host_is_ready(struct mei_device
*dev
)
365 u32 hcsr
= mei_hcsr_read(dev
);
367 return (hcsr
& H_RDY
) == H_RDY
;
371 * mei_me_hw_is_ready - check whether the me(hw) has turned ready
376 static bool mei_me_hw_is_ready(struct mei_device
*dev
)
378 u32 mecsr
= mei_me_mecsr_read(dev
);
380 return (mecsr
& ME_RDY_HRA
) == ME_RDY_HRA
;
384 * mei_me_hw_ready_wait - wait until the me(hw) has turned ready
385 * or timeout is reached
388 * Return: 0 on success, error otherwise
390 static int mei_me_hw_ready_wait(struct mei_device
*dev
)
392 mutex_unlock(&dev
->device_lock
);
393 wait_event_timeout(dev
->wait_hw_ready
,
395 mei_secs_to_jiffies(MEI_HW_READY_TIMEOUT
));
396 mutex_lock(&dev
->device_lock
);
397 if (!dev
->recvd_hw_ready
) {
398 dev_err(dev
->dev
, "wait hw ready failed\n");
402 mei_me_hw_reset_release(dev
);
403 dev
->recvd_hw_ready
= false;
408 * mei_me_hw_start - hw start routine
411 * Return: 0 on success, error otherwise
413 static int mei_me_hw_start(struct mei_device
*dev
)
415 int ret
= mei_me_hw_ready_wait(dev
);
419 dev_dbg(dev
->dev
, "hw is ready\n");
421 mei_me_host_set_ready(dev
);
427 * mei_hbuf_filled_slots - gets number of device filled buffer slots
429 * @dev: the device structure
431 * Return: number of filled slots
433 static unsigned char mei_hbuf_filled_slots(struct mei_device
*dev
)
436 char read_ptr
, write_ptr
;
438 hcsr
= mei_hcsr_read(dev
);
440 read_ptr
= (char) ((hcsr
& H_CBRP
) >> 8);
441 write_ptr
= (char) ((hcsr
& H_CBWP
) >> 16);
443 return (unsigned char) (write_ptr
- read_ptr
);
447 * mei_me_hbuf_is_empty - checks if host buffer is empty.
449 * @dev: the device structure
451 * Return: true if empty, false - otherwise.
453 static bool mei_me_hbuf_is_empty(struct mei_device
*dev
)
455 return mei_hbuf_filled_slots(dev
) == 0;
459 * mei_me_hbuf_empty_slots - counts write empty slots.
461 * @dev: the device structure
463 * Return: -EOVERFLOW if overflow, otherwise empty slots count
465 static int mei_me_hbuf_empty_slots(struct mei_device
*dev
)
467 unsigned char filled_slots
, empty_slots
;
469 filled_slots
= mei_hbuf_filled_slots(dev
);
470 empty_slots
= dev
->hbuf_depth
- filled_slots
;
472 /* check for overflow */
473 if (filled_slots
> dev
->hbuf_depth
)
480 * mei_me_hbuf_max_len - returns size of hw buffer.
482 * @dev: the device structure
484 * Return: size of hw buffer in bytes
486 static size_t mei_me_hbuf_max_len(const struct mei_device
*dev
)
488 return dev
->hbuf_depth
* sizeof(u32
) - sizeof(struct mei_msg_hdr
);
493 * mei_me_hbuf_write - writes a message to host hw buffer.
495 * @dev: the device structure
496 * @header: mei HECI header of message
497 * @buf: message payload will be written
499 * Return: -EIO if write has failed
501 static int mei_me_hbuf_write(struct mei_device
*dev
,
502 struct mei_msg_hdr
*header
,
503 const unsigned char *buf
)
506 unsigned long length
= header
->length
;
507 u32
*reg_buf
= (u32
*)buf
;
513 dev_dbg(dev
->dev
, MEI_HDR_FMT
, MEI_HDR_PRM(header
));
515 empty_slots
= mei_hbuf_empty_slots(dev
);
516 dev_dbg(dev
->dev
, "empty slots = %hu.\n", empty_slots
);
518 dw_cnt
= mei_data2slots(length
);
519 if (empty_slots
< 0 || dw_cnt
> empty_slots
)
522 mei_me_hcbww_write(dev
, *((u32
*) header
));
524 for (i
= 0; i
< length
/ 4; i
++)
525 mei_me_hcbww_write(dev
, reg_buf
[i
]);
531 memcpy(®
, &buf
[length
- rem
], rem
);
532 mei_me_hcbww_write(dev
, reg
);
535 hcsr
= mei_hcsr_read(dev
) | H_IG
;
536 mei_hcsr_set(dev
, hcsr
);
537 if (!mei_me_hw_is_ready(dev
))
544 * mei_me_count_full_read_slots - counts read full slots.
546 * @dev: the device structure
548 * Return: -EOVERFLOW if overflow, otherwise filled slots count
550 static int mei_me_count_full_read_slots(struct mei_device
*dev
)
553 char read_ptr
, write_ptr
;
554 unsigned char buffer_depth
, filled_slots
;
556 me_csr
= mei_me_mecsr_read(dev
);
557 buffer_depth
= (unsigned char)((me_csr
& ME_CBD_HRA
) >> 24);
558 read_ptr
= (char) ((me_csr
& ME_CBRP_HRA
) >> 8);
559 write_ptr
= (char) ((me_csr
& ME_CBWP_HRA
) >> 16);
560 filled_slots
= (unsigned char) (write_ptr
- read_ptr
);
562 /* check for overflow */
563 if (filled_slots
> buffer_depth
)
566 dev_dbg(dev
->dev
, "filled_slots =%08x\n", filled_slots
);
567 return (int)filled_slots
;
571 * mei_me_read_slots - reads a message from mei device.
573 * @dev: the device structure
574 * @buffer: message buffer will be written
575 * @buffer_length: message size will be read
579 static int mei_me_read_slots(struct mei_device
*dev
, unsigned char *buffer
,
580 unsigned long buffer_length
)
582 u32
*reg_buf
= (u32
*)buffer
;
585 for (; buffer_length
>= sizeof(u32
); buffer_length
-= sizeof(u32
))
586 *reg_buf
++ = mei_me_mecbrw_read(dev
);
588 if (buffer_length
> 0) {
589 u32 reg
= mei_me_mecbrw_read(dev
);
591 memcpy(reg_buf
, ®
, buffer_length
);
594 hcsr
= mei_hcsr_read(dev
) | H_IG
;
595 mei_hcsr_set(dev
, hcsr
);
600 * mei_me_pg_set - write pg enter register
602 * @dev: the device structure
604 static void mei_me_pg_set(struct mei_device
*dev
)
606 struct mei_me_hw
*hw
= to_me_hw(dev
);
609 reg
= mei_me_reg_read(hw
, H_HPG_CSR
);
610 trace_mei_reg_read(dev
->dev
, "H_HPG_CSR", H_HPG_CSR
, reg
);
612 reg
|= H_HPG_CSR_PGI
;
614 trace_mei_reg_write(dev
->dev
, "H_HPG_CSR", H_HPG_CSR
, reg
);
615 mei_me_reg_write(hw
, H_HPG_CSR
, reg
);
619 * mei_me_pg_unset - write pg exit register
621 * @dev: the device structure
623 static void mei_me_pg_unset(struct mei_device
*dev
)
625 struct mei_me_hw
*hw
= to_me_hw(dev
);
628 reg
= mei_me_reg_read(hw
, H_HPG_CSR
);
629 trace_mei_reg_read(dev
->dev
, "H_HPG_CSR", H_HPG_CSR
, reg
);
631 WARN(!(reg
& H_HPG_CSR_PGI
), "PGI is not set\n");
633 reg
|= H_HPG_CSR_PGIHEXR
;
635 trace_mei_reg_write(dev
->dev
, "H_HPG_CSR", H_HPG_CSR
, reg
);
636 mei_me_reg_write(hw
, H_HPG_CSR
, reg
);
640 * mei_me_pg_legacy_enter_sync - perform legacy pg entry procedure
642 * @dev: the device structure
644 * Return: 0 on success an error code otherwise
646 static int mei_me_pg_legacy_enter_sync(struct mei_device
*dev
)
648 struct mei_me_hw
*hw
= to_me_hw(dev
);
649 unsigned long timeout
= mei_secs_to_jiffies(MEI_PGI_TIMEOUT
);
652 dev
->pg_event
= MEI_PG_EVENT_WAIT
;
654 ret
= mei_hbm_pg(dev
, MEI_PG_ISOLATION_ENTRY_REQ_CMD
);
658 mutex_unlock(&dev
->device_lock
);
659 wait_event_timeout(dev
->wait_pg
,
660 dev
->pg_event
== MEI_PG_EVENT_RECEIVED
, timeout
);
661 mutex_lock(&dev
->device_lock
);
663 if (dev
->pg_event
== MEI_PG_EVENT_RECEIVED
) {
670 dev
->pg_event
= MEI_PG_EVENT_IDLE
;
671 hw
->pg_state
= MEI_PG_ON
;
677 * mei_me_pg_legacy_exit_sync - perform legacy pg exit procedure
679 * @dev: the device structure
681 * Return: 0 on success an error code otherwise
683 static int mei_me_pg_legacy_exit_sync(struct mei_device
*dev
)
685 struct mei_me_hw
*hw
= to_me_hw(dev
);
686 unsigned long timeout
= mei_secs_to_jiffies(MEI_PGI_TIMEOUT
);
689 if (dev
->pg_event
== MEI_PG_EVENT_RECEIVED
)
692 dev
->pg_event
= MEI_PG_EVENT_WAIT
;
694 mei_me_pg_unset(dev
);
696 mutex_unlock(&dev
->device_lock
);
697 wait_event_timeout(dev
->wait_pg
,
698 dev
->pg_event
== MEI_PG_EVENT_RECEIVED
, timeout
);
699 mutex_lock(&dev
->device_lock
);
702 if (dev
->pg_event
!= MEI_PG_EVENT_RECEIVED
) {
707 dev
->pg_event
= MEI_PG_EVENT_INTR_WAIT
;
708 ret
= mei_hbm_pg(dev
, MEI_PG_ISOLATION_EXIT_RES_CMD
);
712 mutex_unlock(&dev
->device_lock
);
713 wait_event_timeout(dev
->wait_pg
,
714 dev
->pg_event
== MEI_PG_EVENT_INTR_RECEIVED
, timeout
);
715 mutex_lock(&dev
->device_lock
);
717 if (dev
->pg_event
== MEI_PG_EVENT_INTR_RECEIVED
)
723 dev
->pg_event
= MEI_PG_EVENT_IDLE
;
724 hw
->pg_state
= MEI_PG_OFF
;
730 * mei_me_pg_in_transition - is device now in pg transition
732 * @dev: the device structure
734 * Return: true if in pg transition, false otherwise
736 static bool mei_me_pg_in_transition(struct mei_device
*dev
)
738 return dev
->pg_event
>= MEI_PG_EVENT_WAIT
&&
739 dev
->pg_event
<= MEI_PG_EVENT_INTR_WAIT
;
743 * mei_me_pg_is_enabled - detect if PG is supported by HW
745 * @dev: the device structure
747 * Return: true is pg supported, false otherwise
749 static bool mei_me_pg_is_enabled(struct mei_device
*dev
)
751 struct mei_me_hw
*hw
= to_me_hw(dev
);
752 u32 reg
= mei_me_mecsr_read(dev
);
754 if (hw
->d0i3_supported
)
757 if ((reg
& ME_PGIC_HRA
) == 0)
760 if (!dev
->hbm_f_pg_supported
)
766 dev_dbg(dev
->dev
, "pg: not supported: d0i3 = %d HGP = %d hbm version %d.%d ?= %d.%d\n",
768 !!(reg
& ME_PGIC_HRA
),
769 dev
->version
.major_version
,
770 dev
->version
.minor_version
,
771 HBM_MAJOR_VERSION_PGI
,
772 HBM_MINOR_VERSION_PGI
);
778 * mei_me_d0i3_set - write d0i3 register bit on mei device.
780 * @dev: the device structure
781 * @intr: ask for interrupt
783 * Return: D0I3C register value
785 static u32
mei_me_d0i3_set(struct mei_device
*dev
, bool intr
)
787 u32 reg
= mei_me_d0i3c_read(dev
);
794 mei_me_d0i3c_write(dev
, reg
);
795 /* read it to ensure HW consistency */
796 reg
= mei_me_d0i3c_read(dev
);
801 * mei_me_d0i3_unset - clean d0i3 register bit on mei device.
803 * @dev: the device structure
805 * Return: D0I3C register value
807 static u32
mei_me_d0i3_unset(struct mei_device
*dev
)
809 u32 reg
= mei_me_d0i3c_read(dev
);
813 mei_me_d0i3c_write(dev
, reg
);
814 /* read it to ensure HW consistency */
815 reg
= mei_me_d0i3c_read(dev
);
820 * mei_me_d0i3_enter_sync - perform d0i3 entry procedure
822 * @dev: the device structure
824 * Return: 0 on success an error code otherwise
826 static int mei_me_d0i3_enter_sync(struct mei_device
*dev
)
828 struct mei_me_hw
*hw
= to_me_hw(dev
);
829 unsigned long d0i3_timeout
= mei_secs_to_jiffies(MEI_D0I3_TIMEOUT
);
830 unsigned long pgi_timeout
= mei_secs_to_jiffies(MEI_PGI_TIMEOUT
);
834 reg
= mei_me_d0i3c_read(dev
);
835 if (reg
& H_D0I3C_I3
) {
836 /* we are in d0i3, nothing to do */
837 dev_dbg(dev
->dev
, "d0i3 set not needed\n");
842 /* PGI entry procedure */
843 dev
->pg_event
= MEI_PG_EVENT_WAIT
;
845 ret
= mei_hbm_pg(dev
, MEI_PG_ISOLATION_ENTRY_REQ_CMD
);
847 /* FIXME: should we reset here? */
850 mutex_unlock(&dev
->device_lock
);
851 wait_event_timeout(dev
->wait_pg
,
852 dev
->pg_event
== MEI_PG_EVENT_RECEIVED
, pgi_timeout
);
853 mutex_lock(&dev
->device_lock
);
855 if (dev
->pg_event
!= MEI_PG_EVENT_RECEIVED
) {
859 /* end PGI entry procedure */
861 dev
->pg_event
= MEI_PG_EVENT_INTR_WAIT
;
863 reg
= mei_me_d0i3_set(dev
, true);
864 if (!(reg
& H_D0I3C_CIP
)) {
865 dev_dbg(dev
->dev
, "d0i3 enter wait not needed\n");
870 mutex_unlock(&dev
->device_lock
);
871 wait_event_timeout(dev
->wait_pg
,
872 dev
->pg_event
== MEI_PG_EVENT_INTR_RECEIVED
, d0i3_timeout
);
873 mutex_lock(&dev
->device_lock
);
875 if (dev
->pg_event
!= MEI_PG_EVENT_INTR_RECEIVED
) {
876 reg
= mei_me_d0i3c_read(dev
);
877 if (!(reg
& H_D0I3C_I3
)) {
885 hw
->pg_state
= MEI_PG_ON
;
887 dev
->pg_event
= MEI_PG_EVENT_IDLE
;
888 dev_dbg(dev
->dev
, "d0i3 enter ret = %d\n", ret
);
893 * mei_me_d0i3_enter - perform d0i3 entry procedure
894 * no hbm PG handshake
895 * no waiting for confirmation; runs with interrupts
898 * @dev: the device structure
900 * Return: 0 on success an error code otherwise
902 static int mei_me_d0i3_enter(struct mei_device
*dev
)
904 struct mei_me_hw
*hw
= to_me_hw(dev
);
907 reg
= mei_me_d0i3c_read(dev
);
908 if (reg
& H_D0I3C_I3
) {
909 /* we are in d0i3, nothing to do */
910 dev_dbg(dev
->dev
, "already d0i3 : set not needed\n");
914 mei_me_d0i3_set(dev
, false);
916 hw
->pg_state
= MEI_PG_ON
;
917 dev
->pg_event
= MEI_PG_EVENT_IDLE
;
918 dev_dbg(dev
->dev
, "d0i3 enter\n");
923 * mei_me_d0i3_exit_sync - perform d0i3 exit procedure
925 * @dev: the device structure
927 * Return: 0 on success an error code otherwise
929 static int mei_me_d0i3_exit_sync(struct mei_device
*dev
)
931 struct mei_me_hw
*hw
= to_me_hw(dev
);
932 unsigned long timeout
= mei_secs_to_jiffies(MEI_D0I3_TIMEOUT
);
936 dev
->pg_event
= MEI_PG_EVENT_INTR_WAIT
;
938 reg
= mei_me_d0i3c_read(dev
);
939 if (!(reg
& H_D0I3C_I3
)) {
940 /* we are not in d0i3, nothing to do */
941 dev_dbg(dev
->dev
, "d0i3 exit not needed\n");
946 reg
= mei_me_d0i3_unset(dev
);
947 if (!(reg
& H_D0I3C_CIP
)) {
948 dev_dbg(dev
->dev
, "d0i3 exit wait not needed\n");
953 mutex_unlock(&dev
->device_lock
);
954 wait_event_timeout(dev
->wait_pg
,
955 dev
->pg_event
== MEI_PG_EVENT_INTR_RECEIVED
, timeout
);
956 mutex_lock(&dev
->device_lock
);
958 if (dev
->pg_event
!= MEI_PG_EVENT_INTR_RECEIVED
) {
959 reg
= mei_me_d0i3c_read(dev
);
960 if (reg
& H_D0I3C_I3
) {
968 hw
->pg_state
= MEI_PG_OFF
;
970 dev
->pg_event
= MEI_PG_EVENT_IDLE
;
972 dev_dbg(dev
->dev
, "d0i3 exit ret = %d\n", ret
);
977 * mei_me_pg_legacy_intr - perform legacy pg processing
978 * in interrupt thread handler
980 * @dev: the device structure
982 static void mei_me_pg_legacy_intr(struct mei_device
*dev
)
984 struct mei_me_hw
*hw
= to_me_hw(dev
);
986 if (dev
->pg_event
!= MEI_PG_EVENT_INTR_WAIT
)
989 dev
->pg_event
= MEI_PG_EVENT_INTR_RECEIVED
;
990 hw
->pg_state
= MEI_PG_OFF
;
991 if (waitqueue_active(&dev
->wait_pg
))
992 wake_up(&dev
->wait_pg
);
996 * mei_me_d0i3_intr - perform d0i3 processing in interrupt thread handler
998 * @dev: the device structure
999 * @intr_source: interrupt source
1001 static void mei_me_d0i3_intr(struct mei_device
*dev
, u32 intr_source
)
1003 struct mei_me_hw
*hw
= to_me_hw(dev
);
1005 if (dev
->pg_event
== MEI_PG_EVENT_INTR_WAIT
&&
1006 (intr_source
& H_D0I3C_IS
)) {
1007 dev
->pg_event
= MEI_PG_EVENT_INTR_RECEIVED
;
1008 if (hw
->pg_state
== MEI_PG_ON
) {
1009 hw
->pg_state
= MEI_PG_OFF
;
1010 if (dev
->hbm_state
!= MEI_HBM_IDLE
) {
1012 * force H_RDY because it could be
1013 * wiped off during PG
1015 dev_dbg(dev
->dev
, "d0i3 set host ready\n");
1016 mei_me_host_set_ready(dev
);
1019 hw
->pg_state
= MEI_PG_ON
;
1022 wake_up(&dev
->wait_pg
);
1025 if (hw
->pg_state
== MEI_PG_ON
&& (intr_source
& H_IS
)) {
1027 * HW sent some data and we are in D0i3, so
1028 * we got here because of HW initiated exit from D0i3.
1029 * Start runtime pm resume sequence to exit low power state.
1031 dev_dbg(dev
->dev
, "d0i3 want resume\n");
1032 mei_hbm_pg_resume(dev
);
1037 * mei_me_pg_intr - perform pg processing in interrupt thread handler
1039 * @dev: the device structure
1040 * @intr_source: interrupt source
1042 static void mei_me_pg_intr(struct mei_device
*dev
, u32 intr_source
)
1044 struct mei_me_hw
*hw
= to_me_hw(dev
);
1046 if (hw
->d0i3_supported
)
1047 mei_me_d0i3_intr(dev
, intr_source
);
1049 mei_me_pg_legacy_intr(dev
);
1053 * mei_me_pg_enter_sync - perform runtime pm entry procedure
1055 * @dev: the device structure
1057 * Return: 0 on success an error code otherwise
1059 int mei_me_pg_enter_sync(struct mei_device
*dev
)
1061 struct mei_me_hw
*hw
= to_me_hw(dev
);
1063 if (hw
->d0i3_supported
)
1064 return mei_me_d0i3_enter_sync(dev
);
1066 return mei_me_pg_legacy_enter_sync(dev
);
1070 * mei_me_pg_exit_sync - perform runtime pm exit procedure
1072 * @dev: the device structure
1074 * Return: 0 on success an error code otherwise
1076 int mei_me_pg_exit_sync(struct mei_device
*dev
)
1078 struct mei_me_hw
*hw
= to_me_hw(dev
);
1080 if (hw
->d0i3_supported
)
1081 return mei_me_d0i3_exit_sync(dev
);
1083 return mei_me_pg_legacy_exit_sync(dev
);
1087 * mei_me_hw_reset - resets fw via mei csr register.
1089 * @dev: the device structure
1090 * @intr_enable: if interrupt should be enabled after reset.
1092 * Return: 0 on success an error code otherwise
1094 static int mei_me_hw_reset(struct mei_device
*dev
, bool intr_enable
)
1096 struct mei_me_hw
*hw
= to_me_hw(dev
);
1101 mei_me_intr_enable(dev
);
1102 if (hw
->d0i3_supported
) {
1103 ret
= mei_me_d0i3_exit_sync(dev
);
1109 pm_runtime_set_active(dev
->dev
);
1111 hcsr
= mei_hcsr_read(dev
);
1112 /* H_RST may be found lit before reset is started,
1113 * for example if preceding reset flow hasn't completed.
1114 * In that case asserting H_RST will be ignored, therefore
1115 * we need to clean H_RST bit to start a successful reset sequence.
1117 if ((hcsr
& H_RST
) == H_RST
) {
1118 dev_warn(dev
->dev
, "H_RST is set = 0x%08X", hcsr
);
1120 mei_hcsr_set(dev
, hcsr
);
1121 hcsr
= mei_hcsr_read(dev
);
1124 hcsr
|= H_RST
| H_IG
| H_CSR_IS_MASK
;
1127 hcsr
&= ~H_CSR_IE_MASK
;
1129 dev
->recvd_hw_ready
= false;
1130 mei_hcsr_write(dev
, hcsr
);
1133 * Host reads the H_CSR once to ensure that the
1134 * posted write to H_CSR completes.
1136 hcsr
= mei_hcsr_read(dev
);
1138 if ((hcsr
& H_RST
) == 0)
1139 dev_warn(dev
->dev
, "H_RST is not set = 0x%08X", hcsr
);
1141 if ((hcsr
& H_RDY
) == H_RDY
)
1142 dev_warn(dev
->dev
, "H_RDY is not cleared 0x%08X", hcsr
);
1145 mei_me_hw_reset_release(dev
);
1146 if (hw
->d0i3_supported
) {
1147 ret
= mei_me_d0i3_enter(dev
);
1156 * mei_me_irq_quick_handler - The ISR of the MEI device
1158 * @irq: The irq number
1159 * @dev_id: pointer to the device structure
1161 * Return: irqreturn_t
1163 irqreturn_t
mei_me_irq_quick_handler(int irq
, void *dev_id
)
1165 struct mei_device
*dev
= (struct mei_device
*)dev_id
;
1168 hcsr
= mei_hcsr_read(dev
);
1169 if (!me_intr_src(hcsr
))
1172 dev_dbg(dev
->dev
, "interrupt source 0x%08X\n", me_intr_src(hcsr
));
1174 /* disable interrupts on device */
1175 me_intr_disable(dev
, hcsr
);
1176 return IRQ_WAKE_THREAD
;
1180 * mei_me_irq_thread_handler - function called after ISR to handle the interrupt
1183 * @irq: The irq number
1184 * @dev_id: pointer to the device structure
1186 * Return: irqreturn_t
1189 irqreturn_t
mei_me_irq_thread_handler(int irq
, void *dev_id
)
1191 struct mei_device
*dev
= (struct mei_device
*) dev_id
;
1192 struct mei_cl_cb complete_list
;
1197 dev_dbg(dev
->dev
, "function called after ISR to handle the interrupt processing.\n");
1198 /* initialize our complete list */
1199 mutex_lock(&dev
->device_lock
);
1201 hcsr
= mei_hcsr_read(dev
);
1202 me_intr_clear(dev
, hcsr
);
1204 mei_io_list_init(&complete_list
);
1206 /* check if ME wants a reset */
1207 if (!mei_hw_is_ready(dev
) && dev
->dev_state
!= MEI_DEV_RESETTING
) {
1208 dev_warn(dev
->dev
, "FW not ready: resetting.\n");
1209 schedule_work(&dev
->reset_work
);
1213 mei_me_pg_intr(dev
, me_intr_src(hcsr
));
1215 /* check if we need to start the dev */
1216 if (!mei_host_is_ready(dev
)) {
1217 if (mei_hw_is_ready(dev
)) {
1218 dev_dbg(dev
->dev
, "we need to start the dev.\n");
1219 dev
->recvd_hw_ready
= true;
1220 wake_up(&dev
->wait_hw_ready
);
1222 dev_dbg(dev
->dev
, "Spurious Interrupt\n");
1226 /* check slots available for reading */
1227 slots
= mei_count_full_read_slots(dev
);
1229 dev_dbg(dev
->dev
, "slots to read = %08x\n", slots
);
1230 rets
= mei_irq_read_handler(dev
, &complete_list
, &slots
);
1231 /* There is a race between ME write and interrupt delivery:
1232 * Not all data is always available immediately after the
1233 * interrupt, so try to read again on the next interrupt.
1235 if (rets
== -ENODATA
)
1238 if (rets
&& dev
->dev_state
!= MEI_DEV_RESETTING
) {
1239 dev_err(dev
->dev
, "mei_irq_read_handler ret = %d.\n",
1241 schedule_work(&dev
->reset_work
);
1246 dev
->hbuf_is_ready
= mei_hbuf_is_ready(dev
);
1249 * During PG handshake only allowed write is the replay to the
1250 * PG exit message, so block calling write function
1251 * if the pg event is in PG handshake
1253 if (dev
->pg_event
!= MEI_PG_EVENT_WAIT
&&
1254 dev
->pg_event
!= MEI_PG_EVENT_RECEIVED
) {
1255 rets
= mei_irq_write_handler(dev
, &complete_list
);
1256 dev
->hbuf_is_ready
= mei_hbuf_is_ready(dev
);
1259 mei_irq_compl_handler(dev
, &complete_list
);
1262 dev_dbg(dev
->dev
, "interrupt thread end ret = %d\n", rets
);
1263 mei_me_intr_enable(dev
);
1264 mutex_unlock(&dev
->device_lock
);
1268 static const struct mei_hw_ops mei_me_hw_ops
= {
1270 .fw_status
= mei_me_fw_status
,
1271 .pg_state
= mei_me_pg_state
,
1273 .host_is_ready
= mei_me_host_is_ready
,
1275 .hw_is_ready
= mei_me_hw_is_ready
,
1276 .hw_reset
= mei_me_hw_reset
,
1277 .hw_config
= mei_me_hw_config
,
1278 .hw_start
= mei_me_hw_start
,
1280 .pg_in_transition
= mei_me_pg_in_transition
,
1281 .pg_is_enabled
= mei_me_pg_is_enabled
,
1283 .intr_clear
= mei_me_intr_clear
,
1284 .intr_enable
= mei_me_intr_enable
,
1285 .intr_disable
= mei_me_intr_disable
,
1286 .synchronize_irq
= mei_me_synchronize_irq
,
1288 .hbuf_free_slots
= mei_me_hbuf_empty_slots
,
1289 .hbuf_is_ready
= mei_me_hbuf_is_empty
,
1290 .hbuf_max_len
= mei_me_hbuf_max_len
,
1292 .write
= mei_me_hbuf_write
,
1294 .rdbuf_full_slots
= mei_me_count_full_read_slots
,
1295 .read_hdr
= mei_me_mecbrw_read
,
1296 .read
= mei_me_read_slots
1299 static bool mei_me_fw_type_nm(struct pci_dev
*pdev
)
1303 pci_read_config_dword(pdev
, PCI_CFG_HFS_2
, ®
);
1304 trace_mei_pci_cfg_read(&pdev
->dev
, "PCI_CFG_HFS_2", PCI_CFG_HFS_2
, reg
);
1305 /* make sure that bit 9 (NM) is up and bit 10 (DM) is down */
1306 return (reg
& 0x600) == 0x200;
1309 #define MEI_CFG_FW_NM \
1310 .quirk_probe = mei_me_fw_type_nm
1312 static bool mei_me_fw_type_sps(struct pci_dev
*pdev
)
1318 * Read ME FW Status register to check for SPS Firmware
1319 * The SPS FW is only signaled in pci function 0
1321 devfn
= PCI_DEVFN(PCI_SLOT(pdev
->devfn
), 0);
1322 pci_bus_read_config_dword(pdev
->bus
, devfn
, PCI_CFG_HFS_1
, ®
);
1323 trace_mei_pci_cfg_read(&pdev
->dev
, "PCI_CFG_HFS_1", PCI_CFG_HFS_1
, reg
);
1324 /* if bits [19:16] = 15, running SPS Firmware */
1325 return (reg
& 0xf0000) == 0xf0000;
1328 #define MEI_CFG_FW_SPS \
1329 .quirk_probe = mei_me_fw_type_sps
1332 #define MEI_CFG_LEGACY_HFS \
1333 .fw_status.count = 0
1335 #define MEI_CFG_ICH_HFS \
1336 .fw_status.count = 1, \
1337 .fw_status.status[0] = PCI_CFG_HFS_1
1339 #define MEI_CFG_PCH_HFS \
1340 .fw_status.count = 2, \
1341 .fw_status.status[0] = PCI_CFG_HFS_1, \
1342 .fw_status.status[1] = PCI_CFG_HFS_2
1344 #define MEI_CFG_PCH8_HFS \
1345 .fw_status.count = 6, \
1346 .fw_status.status[0] = PCI_CFG_HFS_1, \
1347 .fw_status.status[1] = PCI_CFG_HFS_2, \
1348 .fw_status.status[2] = PCI_CFG_HFS_3, \
1349 .fw_status.status[3] = PCI_CFG_HFS_4, \
1350 .fw_status.status[4] = PCI_CFG_HFS_5, \
1351 .fw_status.status[5] = PCI_CFG_HFS_6
1353 /* ICH Legacy devices */
1354 const struct mei_cfg mei_me_legacy_cfg
= {
1359 const struct mei_cfg mei_me_ich_cfg
= {
1364 const struct mei_cfg mei_me_pch_cfg
= {
1369 /* PCH Cougar Point and Patsburg with quirk for Node Manager exclusion */
1370 const struct mei_cfg mei_me_pch_cpt_pbg_cfg
= {
1375 /* PCH8 Lynx Point and newer devices */
1376 const struct mei_cfg mei_me_pch8_cfg
= {
1380 /* PCH8 Lynx Point with quirk for SPS Firmware exclusion */
1381 const struct mei_cfg mei_me_pch8_sps_cfg
= {
1387 * mei_me_dev_init - allocates and initializes the mei device structure
1389 * @pdev: The pci device structure
1390 * @cfg: per device generation config
1392 * Return: The mei_device_device pointer on success, NULL on failure.
1394 struct mei_device
*mei_me_dev_init(struct pci_dev
*pdev
,
1395 const struct mei_cfg
*cfg
)
1397 struct mei_device
*dev
;
1398 struct mei_me_hw
*hw
;
1400 dev
= kzalloc(sizeof(struct mei_device
) +
1401 sizeof(struct mei_me_hw
), GFP_KERNEL
);
1406 mei_device_init(dev
, &pdev
->dev
, &mei_me_hw_ops
);