1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <device/mmio.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/pci_ops.h>
7 #include <console/console.h>
8 #include <device/pci_ids.h>
16 #include <vendorcode/google/chromeos/chromeos.h>
18 /* Path that the BIOS should take based on ME state */
19 static const char *const me_bios_path_values
[] = {
20 [ME_NORMAL_BIOS_PATH
] = "Normal",
21 [ME_S3WAKE_BIOS_PATH
] = "S3 Wake",
22 [ME_ERROR_BIOS_PATH
] = "Error",
23 [ME_RECOVERY_BIOS_PATH
] = "Recovery",
24 [ME_DISABLE_BIOS_PATH
] = "Disable",
25 [ME_FIRMWARE_UPDATE_BIOS_PATH
] = "Firmware Update",
28 const char *const me_get_bios_path_string(int path
)
30 return me_bios_path_values
[path
];
33 /* MMIO base address for MEI interface */
34 static u32
*mei_base_address
;
36 static void mei_dump(void *ptr
, int dword
, int offset
, const char *type
)
40 if (!CONFIG(DEBUG_INTEL_ME
))
43 printk(BIOS_SPEW
, "%-9s[%02x] : ", type
, offset
);
50 printk(BIOS_SPEW
, "ERROR: 0x%08x\n", dword
);
53 printk(BIOS_SPEW
, "cbd=%u cbrp=%02u cbwp=%02u ready=%u "
54 "reset=%u ig=%u is=%u ie=%u\n", csr
->buffer_depth
,
55 csr
->buffer_read_ptr
, csr
->buffer_write_ptr
,
56 csr
->ready
, csr
->reset
, csr
->interrupt_generate
,
57 csr
->interrupt_status
, csr
->interrupt_enable
);
61 printk(BIOS_SPEW
, "CB: 0x%08x\n", dword
);
64 printk(BIOS_SPEW
, "0x%08x\n", offset
);
70 * ME/MEI access helpers using memcpy to avoid aliasing.
73 void mei_read_dword_ptr(void *ptr
, int offset
)
75 u32 dword
= read32(mei_base_address
+ (offset
/ sizeof(u32
)));
76 memcpy(ptr
, &dword
, sizeof(dword
));
77 mei_dump(ptr
, dword
, offset
, "READ");
80 void mei_write_dword_ptr(void *ptr
, int offset
)
83 memcpy(&dword
, ptr
, sizeof(dword
));
84 write32(mei_base_address
+ (offset
/ sizeof(u32
)), dword
);
85 mei_dump(ptr
, dword
, offset
, "WRITE");
88 void read_host_csr(struct mei_csr
*csr
)
90 mei_read_dword_ptr(csr
, MEI_H_CSR
);
93 void write_host_csr(struct mei_csr
*csr
)
95 mei_write_dword_ptr(csr
, MEI_H_CSR
);
98 void read_me_csr(struct mei_csr
*csr
)
100 mei_read_dword_ptr(csr
, MEI_ME_CSR_HA
);
103 void write_cb(u32 dword
)
105 write32(mei_base_address
+ (MEI_H_CB_WW
/ sizeof(u32
)), dword
);
106 mei_dump(NULL
, dword
, MEI_H_CB_WW
, "WRITE");
111 u32 dword
= read32(mei_base_address
+ (MEI_ME_CB_RW
/ sizeof(u32
)));
112 mei_dump(NULL
, dword
, MEI_ME_CB_RW
, "READ");
116 /* Wait for ME ready bit to be asserted */
117 static int mei_wait_for_me_ready(void)
120 unsigned int try = ME_RETRY
;
129 printk(BIOS_ERR
, "ME: failed to become ready\n");
133 static void mei_reset(void)
137 if (mei_wait_for_me_ready() < 0)
140 /* Reset host and ME circular buffers for next message */
141 read_host_csr(&host
);
143 host
.interrupt_generate
= 1;
144 write_host_csr(&host
);
146 if (mei_wait_for_me_ready() < 0)
149 /* Re-init and indicate host is ready */
150 read_host_csr(&host
);
151 host
.interrupt_generate
= 1;
154 write_host_csr(&host
);
157 static int mei_send_msg(struct mei_header
*mei
, struct mkhi_header
*mkhi
, void *req_data
)
160 unsigned int ndata
, n
;
163 /* Number of dwords to write, ignoring MKHI */
164 ndata
= mei
->length
>> 2;
166 /* Pad non-dword aligned request message length */
171 printk(BIOS_DEBUG
, "ME: request does not include MKHI\n");
174 ndata
++; /* Add MEI header */
177 * Make sure there is still room left in the circular buffer.
178 * Reset the buffer pointers if the requested message will not fit.
180 read_host_csr(&host
);
181 if ((host
.buffer_depth
- host
.buffer_write_ptr
) < ndata
) {
182 printk(BIOS_ERR
, "ME: circular buffer full, resetting...\n");
184 read_host_csr(&host
);
188 * This implementation does not handle splitting large messages
189 * across multiple transactions. Ensure the requested length
190 * will fit in the available circular buffer depth.
192 if ((host
.buffer_depth
- host
.buffer_write_ptr
) < ndata
) {
193 printk(BIOS_ERR
, "ME: message (%u) too large for buffer (%u)\n",
194 ndata
+ 2, host
.buffer_depth
);
198 /* Write MEI header */
199 mei_write_dword_ptr(mei
, MEI_H_CB_WW
);
202 /* Write MKHI header */
203 mei_write_dword_ptr(mkhi
, MEI_H_CB_WW
);
206 /* Write message data */
208 for (n
= 0; n
< ndata
; ++n
)
211 /* Generate interrupt to the ME */
212 read_host_csr(&host
);
213 host
.interrupt_generate
= 1;
214 write_host_csr(&host
);
216 /* Make sure ME is ready after sending request data */
217 return mei_wait_for_me_ready();
220 static int mei_recv_msg(struct mkhi_header
*mkhi
, void *rsp_data
, int rsp_bytes
)
222 struct mei_header mei_rsp
;
223 struct mkhi_header mkhi_rsp
;
224 struct mei_csr me
, host
;
225 unsigned int ndata
, n
;
226 unsigned int expected
;
229 /* Total number of dwords to read from circular buffer */
230 expected
= (rsp_bytes
+ sizeof(mei_rsp
) + sizeof(mkhi_rsp
)) >> 2;
235 * The interrupt status bit does not appear to indicate that the
236 * message has actually been received. Instead we wait until the
237 * expected number of dwords are present in the circular buffer.
239 for (n
= ME_RETRY
; n
; --n
) {
241 if ((me
.buffer_write_ptr
- me
.buffer_read_ptr
) >= expected
)
247 printk(BIOS_ERR
, "ME: timeout waiting for data: expected %u, available %u\n",
248 expected
, me
.buffer_write_ptr
- me
.buffer_read_ptr
);
252 /* Read and verify MEI response header from the ME */
253 mei_read_dword_ptr(&mei_rsp
, MEI_ME_CB_RW
);
254 if (!mei_rsp
.is_complete
) {
255 printk(BIOS_ERR
, "ME: response is not complete\n");
259 /* Handle non-dword responses and expect at least MKHI header */
260 ndata
= mei_rsp
.length
>> 2;
261 if (mei_rsp
.length
& 3)
264 if (ndata
!= (expected
- 1)) {
265 printk(BIOS_ERR
, "ME: response is missing data %d != %d\n",
266 ndata
, (expected
- 1));
270 /* Read and verify MKHI response header from the ME */
271 mei_read_dword_ptr(&mkhi_rsp
, MEI_ME_CB_RW
);
272 if (!mkhi_rsp
.is_response
||
273 mkhi
->group_id
!= mkhi_rsp
.group_id
||
274 mkhi
->command
!= mkhi_rsp
.command
) {
275 printk(BIOS_ERR
, "ME: invalid response, group %u ?= %u, "
276 "command %u ?= %u, is_response %u\n", mkhi
->group_id
,
277 mkhi_rsp
.group_id
, mkhi
->command
, mkhi_rsp
.command
,
278 mkhi_rsp
.is_response
);
281 ndata
--; /* MKHI header has been read */
283 /* Make sure caller passed a buffer with enough space */
284 if (ndata
!= (rsp_bytes
>> 2)) {
285 printk(BIOS_ERR
, "ME: not enough room in response buffer: %u != %u\n",
286 ndata
, rsp_bytes
>> 2);
290 /* Read response data from the circular buffer */
292 for (n
= 0; n
< ndata
; ++n
)
295 /* Tell the ME that we have consumed the response */
296 read_host_csr(&host
);
297 host
.interrupt_status
= 1;
298 host
.interrupt_generate
= 1;
299 write_host_csr(&host
);
301 return mei_wait_for_me_ready();
304 int mei_sendrecv(struct mei_header
*mei
, struct mkhi_header
*mkhi
,
305 void *req_data
, void *rsp_data
, int rsp_bytes
)
307 if (mei_send_msg(mei
, mkhi
, req_data
) < 0)
309 if (mei_recv_msg(mkhi
, rsp_data
, rsp_bytes
) < 0)
314 #ifdef __SIMPLE_DEVICE__
316 void update_mei_base_address(void)
318 uint32_t reg32
= pci_read_config32(PCH_ME_DEV
, PCI_BASE_ADDRESS_0
) & ~0xf;
319 mei_base_address
= (u32
*)(uintptr_t)reg32
;
322 bool is_mei_base_address_valid(void)
324 return mei_base_address
&& mei_base_address
!= (u32
*)0xfffffff0;
329 /* Prepare ME for MEI messages */
330 int intel_mei_setup(struct device
*dev
)
332 struct resource
*res
;
335 /* Find the MMIO base for the ME interface */
336 res
= probe_resource(dev
, PCI_BASE_ADDRESS_0
);
337 if (!res
|| res
->base
== 0 || res
->size
== 0) {
338 printk(BIOS_DEBUG
, "ME: MEI resource not present!\n");
341 mei_base_address
= (u32
*)(uintptr_t)res
->base
;
343 /* Ensure Memory and Bus Master bits are set */
344 pci_or_config16(dev
, PCI_COMMAND
, PCI_COMMAND_MASTER
| PCI_COMMAND_MEMORY
);
346 /* Clean up status for next message */
347 read_host_csr(&host
);
348 host
.interrupt_generate
= 1;
351 write_host_csr(&host
);
356 /* Read the Extend register hash of ME firmware */
357 int intel_me_extend_valid(struct device
*dev
)
359 union me_heres status
;
363 status
.raw
= pci_read_config32(dev
, PCI_ME_HERES
);
364 if (!status
.extend_feature_present
) {
365 printk(BIOS_ERR
, "ME: Extend Feature not present\n");
369 if (!status
.extend_reg_valid
) {
370 printk(BIOS_ERR
, "ME: Extend Register not valid\n");
374 switch (status
.extend_reg_algorithm
) {
375 case PCI_ME_EXT_SHA1
:
377 printk(BIOS_DEBUG
, "ME: Extend SHA-1: ");
379 case PCI_ME_EXT_SHA256
:
381 printk(BIOS_DEBUG
, "ME: Extend SHA-256: ");
384 printk(BIOS_ERR
, "ME: Extend Algorithm %d unknown\n",
385 status
.extend_reg_algorithm
);
389 for (i
= 0; i
< count
; ++i
) {
390 extend
[i
] = pci_read_config32(dev
, PCI_ME_HER(i
));
391 printk(BIOS_DEBUG
, "%08x", extend
[i
]);
393 printk(BIOS_DEBUG
, "\n");
395 /* Save hash in NVS for the OS to verify */
396 if (CONFIG(CHROMEOS_NVS
))
397 chromeos_set_me_hash(extend
, count
);
402 /* Hide the ME virtual PCI devices */
403 void intel_me_hide(struct device
*dev
)
409 bool enter_soft_temp_disable(void)
411 /* The binary sequence for the disable command was found by PT in some vendor BIOS */
412 struct me_disable message
= {
413 .rule_id
= MKHI_DISABLE_RULE_ID
,
416 struct mkhi_header mkhi
= {
417 .group_id
= MKHI_GROUP_ID_FWCAPS
,
418 .command
= MKHI_FWCAPS_SET_RULE
,
420 struct mei_header mei
= {
422 .length
= sizeof(mkhi
) + sizeof(message
),
423 .host_address
= MEI_HOST_ADDRESS
,
424 .client_address
= MEI_ADDRESS_MKHI
,
428 if (mei_sendrecv(&mei
, &mkhi
, &message
, &resp
, sizeof(resp
)) < 0
429 || resp
!= MKHI_DISABLE_RULE_ID
) {
430 printk(BIOS_WARNING
, "ME: disable command failed\n");
437 void enter_soft_temp_disable_wait(void)
440 * TODO: Find smarter way to determine when we're ready to reboot.
442 * There has to be some bit in some register, or something, that indicates that ME has
443 * finished doing its thing and we're ready to reboot.
445 * It was not found yet, though, and waiting for a response after the disable command is
446 * not enough. If we reboot too early, ME will not be disabled on next boot. For now,
447 * let's just wait for 1 second here.
452 void exit_soft_temp_disable(struct device
*dev
)
454 /* To bring ME out of Soft Temporary Disable Mode, host writes 0x20000000 to H_GS */
455 pci_write_config32(dev
, PCI_ME_H_GS
, 0x2 << 28);
458 void exit_soft_temp_disable_wait(struct device
*dev
)
463 stopwatch_init_msecs_expire(&sw
, ME_ENABLE_TIMEOUT
);
466 * Wait for fw_init_complete. Check every 50 ms, give up after 20 sec.
467 * This is what vendor BIOS does. Usually it takes 1.5 seconds or so.
471 hfs
.raw
= pci_read_config32(dev
, PCI_ME_HFS
);
472 if (hfs
.fw_init_complete
)
474 } while (!stopwatch_expired(&sw
));
476 if (!hfs
.fw_init_complete
)
477 printk(BIOS_ERR
, "ME: giving up on waiting for fw_init_complete\n");
479 printk(BIOS_NOTICE
, "ME: took %lldms to complete initialization\n",
480 stopwatch_duration_msecs(&sw
));