blob: 4b786c81d92f26b8aaa6a509a156d35ef00d5d41 [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * This file is part of the coreboot project.
3 *
Duncan Lauriec88c54c2014-04-30 16:36:13 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070013 */
14
15/*
16 * This is a ramstage driver for the Intel Management Engine found in the
17 * southbridge. It handles the required boot-time messages over the
18 * MMIO-based Management Engine Interface to tell the ME that the BIOS is
19 * finished with POST. Additional messages are defined for debug but are
20 * not used unless the console loglevel is high enough.
21 */
22
23#include <arch/acpi.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020024#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020025#include <device/pci_ops.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070026#include <console/console.h>
27#include <device/device.h>
28#include <device/pci.h>
29#include <device/pci_ids.h>
30#include <device/pci_def.h>
Elyes HAOUAS70a03dd2019-12-02 20:47:50 +010031#include <stdlib.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070032#include <string.h>
33#include <delay.h>
34#include <elog.h>
Julius Werner4ee4bd52014-10-20 13:46:39 -070035#include <soc/me.h>
36#include <soc/lpc.h>
37#include <soc/pch.h>
38#include <soc/pci_devs.h>
39#include <soc/ramstage.h>
40#include <soc/rcba.h>
41#include <soc/intel/broadwell/chip.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070042
Julius Wernercd49cce2019-03-05 16:53:33 -080043#if CONFIG(CHROMEOS)
Duncan Lauriec88c54c2014-04-30 16:36:13 -070044#include <vendorcode/google/chromeos/chromeos.h>
45#include <vendorcode/google/chromeos/gnvs.h>
46#endif
47
48/* Path that the BIOS should take based on ME state */
49static const char *me_bios_path_values[] = {
50 [ME_NORMAL_BIOS_PATH] = "Normal",
51 [ME_S3WAKE_BIOS_PATH] = "S3 Wake",
52 [ME_ERROR_BIOS_PATH] = "Error",
53 [ME_RECOVERY_BIOS_PATH] = "Recovery",
54 [ME_DISABLE_BIOS_PATH] = "Disable",
55 [ME_FIRMWARE_UPDATE_BIOS_PATH] = "Firmware Update",
56};
Duncan Lauriec88c54c2014-04-30 16:36:13 -070057
58/* MMIO base address for MEI interface */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080059static u8 *mei_base_address;
Duncan Lauriec88c54c2014-04-30 16:36:13 -070060
Duncan Lauriec88c54c2014-04-30 16:36:13 -070061static void mei_dump(void *ptr, int dword, int offset, const char *type)
62{
63 struct mei_csr *csr;
64
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +020065 if (!CONFIG(DEBUG_INTEL_ME))
66 return;
67
Duncan Lauriec88c54c2014-04-30 16:36:13 -070068 printk(BIOS_SPEW, "%-9s[%02x] : ", type, offset);
69
70 switch (offset) {
71 case MEI_H_CSR:
72 case MEI_ME_CSR_HA:
73 csr = ptr;
74 if (!csr) {
75 printk(BIOS_SPEW, "ERROR: 0x%08x\n", dword);
76 break;
77 }
78 printk(BIOS_SPEW, "cbd=%u cbrp=%02u cbwp=%02u ready=%u "
79 "reset=%u ig=%u is=%u ie=%u\n", csr->buffer_depth,
80 csr->buffer_read_ptr, csr->buffer_write_ptr,
81 csr->ready, csr->reset, csr->interrupt_generate,
82 csr->interrupt_status, csr->interrupt_enable);
83 break;
84 case MEI_ME_CB_RW:
85 case MEI_H_CB_WW:
86 printk(BIOS_SPEW, "CB: 0x%08x\n", dword);
87 break;
88 default:
89 printk(BIOS_SPEW, "0x%08x\n", offset);
90 break;
91 }
92}
Duncan Lauriec88c54c2014-04-30 16:36:13 -070093
94/*
95 * ME/MEI access helpers using memcpy to avoid aliasing.
96 */
97
98static inline void mei_read_dword_ptr(void *ptr, int offset)
99{
100 u32 dword = read32(mei_base_address + offset);
101 memcpy(ptr, &dword, sizeof(dword));
102 mei_dump(ptr, dword, offset, "READ");
103}
104
105static inline void mei_write_dword_ptr(void *ptr, int offset)
106{
107 u32 dword = 0;
108 memcpy(&dword, ptr, sizeof(dword));
109 write32(mei_base_address + offset, dword);
110 mei_dump(ptr, dword, offset, "WRITE");
111}
112
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200113static inline void pci_read_dword_ptr(struct device *dev, void *ptr, int offset)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700114{
115 u32 dword = pci_read_config32(dev, offset);
116 memcpy(ptr, &dword, sizeof(dword));
117 mei_dump(ptr, dword, offset, "PCI READ");
118}
119
120static inline void read_host_csr(struct mei_csr *csr)
121{
122 mei_read_dword_ptr(csr, MEI_H_CSR);
123}
124
125static inline void write_host_csr(struct mei_csr *csr)
126{
127 mei_write_dword_ptr(csr, MEI_H_CSR);
128}
129
130static inline void read_me_csr(struct mei_csr *csr)
131{
132 mei_read_dword_ptr(csr, MEI_ME_CSR_HA);
133}
134
135static inline void write_cb(u32 dword)
136{
137 write32(mei_base_address + MEI_H_CB_WW, dword);
138 mei_dump(NULL, dword, MEI_H_CB_WW, "WRITE");
139}
140
141static inline u32 read_cb(void)
142{
143 u32 dword = read32(mei_base_address + MEI_ME_CB_RW);
144 mei_dump(NULL, dword, MEI_ME_CB_RW, "READ");
145 return dword;
146}
147
148/* Wait for ME ready bit to be asserted */
149static int mei_wait_for_me_ready(void)
150{
151 struct mei_csr me;
Lee Leahy23602df2017-03-16 19:00:37 -0700152 unsigned int try = ME_RETRY;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700153
154 while (try--) {
155 read_me_csr(&me);
156 if (me.ready)
157 return 0;
158 udelay(ME_DELAY);
159 }
160
161 printk(BIOS_ERR, "ME: failed to become ready\n");
162 return -1;
163}
164
165static void mei_reset(void)
166{
167 struct mei_csr host;
168
169 if (mei_wait_for_me_ready() < 0)
170 return;
171
172 /* Reset host and ME circular buffers for next message */
173 read_host_csr(&host);
174 host.reset = 1;
175 host.interrupt_generate = 1;
176 write_host_csr(&host);
177
178 if (mei_wait_for_me_ready() < 0)
179 return;
180
181 /* Re-init and indicate host is ready */
182 read_host_csr(&host);
183 host.interrupt_generate = 1;
184 host.ready = 1;
185 host.reset = 0;
186 write_host_csr(&host);
187}
188
189static int mei_send_packet(struct mei_header *mei, void *req_data)
190{
191 struct mei_csr host;
Lee Leahy23602df2017-03-16 19:00:37 -0700192 unsigned int ndata, n;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700193 u32 *data;
194
195 /* Number of dwords to write */
196 ndata = mei->length >> 2;
197
198 /* Pad non-dword aligned request message length */
199 if (mei->length & 3)
200 ndata++;
201 if (!ndata) {
202 printk(BIOS_DEBUG, "ME: request has no data\n");
203 return -1;
204 }
205 ndata++; /* Add MEI header */
206
207 /*
208 * Make sure there is still room left in the circular buffer.
209 * Reset the buffer pointers if the requested message will not fit.
210 */
211 read_host_csr(&host);
212 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
213 printk(BIOS_ERR, "ME: circular buffer full, resetting...\n");
214 mei_reset();
215 read_host_csr(&host);
216 }
217
218 /* Ensure the requested length will fit in the circular buffer. */
219 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
220 printk(BIOS_ERR, "ME: message (%u) too large for buffer (%u)\n",
221 ndata + 2, host.buffer_depth);
222 return -1;
223 }
224
225 /* Write MEI header */
226 mei_write_dword_ptr(mei, MEI_H_CB_WW);
227 ndata--;
228
229 /* Write message data */
230 data = req_data;
231 for (n = 0; n < ndata; ++n)
232 write_cb(*data++);
233
234 /* Generate interrupt to the ME */
235 read_host_csr(&host);
236 host.interrupt_generate = 1;
237 write_host_csr(&host);
238
239 /* Make sure ME is ready after sending request data */
240 return mei_wait_for_me_ready();
241}
242
243static int mei_send_data(u8 me_address, u8 host_address,
244 void *req_data, int req_bytes)
245{
246 struct mei_header header = {
247 .client_address = me_address,
248 .host_address = host_address,
249 };
250 struct mei_csr host;
251 int current = 0;
252 u8 *req_ptr = req_data;
253
254 while (!header.is_complete) {
255 int remain = req_bytes - current;
256 int buf_len;
257
258 read_host_csr(&host);
259 buf_len = host.buffer_depth - host.buffer_write_ptr;
260
261 if (buf_len > remain) {
262 /* Send all remaining data as final message */
263 header.length = req_bytes - current;
264 header.is_complete = 1;
265 } else {
266 /* Send as much data as the buffer can hold */
267 header.length = buf_len;
268 }
269
270 mei_send_packet(&header, req_ptr);
271
272 req_ptr += header.length;
273 current += header.length;
274 }
275
276 return 0;
277}
278
279static int mei_send_header(u8 me_address, u8 host_address,
280 void *header, int header_len, int complete)
281{
282 struct mei_header mei = {
283 .client_address = me_address,
284 .host_address = host_address,
285 .length = header_len,
286 .is_complete = complete,
287 };
288 return mei_send_packet(&mei, header);
289}
290
291static int mei_recv_msg(void *header, int header_bytes,
292 void *rsp_data, int rsp_bytes)
293{
294 struct mei_header mei_rsp;
295 struct mei_csr me, host;
Lee Leahy23602df2017-03-16 19:00:37 -0700296 unsigned int ndata, n;
297 unsigned int expected;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700298 u32 *data;
299
300 /* Total number of dwords to read from circular buffer */
301 expected = (rsp_bytes + sizeof(mei_rsp) + header_bytes) >> 2;
302 if (rsp_bytes & 3)
303 expected++;
304
305 if (mei_wait_for_me_ready() < 0)
306 return -1;
307
308 /*
309 * The interrupt status bit does not appear to indicate that the
310 * message has actually been received. Instead we wait until the
311 * expected number of dwords are present in the circular buffer.
312 */
313 for (n = ME_RETRY; n; --n) {
314 read_me_csr(&me);
315 if ((me.buffer_write_ptr - me.buffer_read_ptr) >= expected)
316 break;
317 udelay(ME_DELAY);
318 }
319 if (!n) {
320 printk(BIOS_ERR, "ME: timeout waiting for data: expected "
321 "%u, available %u\n", expected,
322 me.buffer_write_ptr - me.buffer_read_ptr);
323 return -1;
324 }
325
326 /* Read and verify MEI response header from the ME */
327 mei_read_dword_ptr(&mei_rsp, MEI_ME_CB_RW);
328 if (!mei_rsp.is_complete) {
329 printk(BIOS_ERR, "ME: response is not complete\n");
330 return -1;
331 }
332
333 /* Handle non-dword responses and expect at least the header */
334 ndata = mei_rsp.length >> 2;
335 if (mei_rsp.length & 3)
336 ndata++;
337 if (ndata != (expected - 1)) {
338 printk(BIOS_ERR, "ME: response is missing data %d != %d\n",
339 ndata, (expected - 1));
340 return -1;
341 }
342
343 /* Read response header from the ME */
344 data = header;
345 for (n = 0; n < (header_bytes >> 2); ++n)
346 *data++ = read_cb();
347 ndata -= header_bytes >> 2;
348
349 /* Make sure caller passed a buffer with enough space */
350 if (ndata != (rsp_bytes >> 2)) {
351 printk(BIOS_ERR, "ME: not enough room in response buffer: "
352 "%u != %u\n", ndata, rsp_bytes >> 2);
353 return -1;
354 }
355
356 /* Read response data from the circular buffer */
357 data = rsp_data;
358 for (n = 0; n < ndata; ++n)
359 *data++ = read_cb();
360
361 /* Tell the ME that we have consumed the response */
362 read_host_csr(&host);
363 host.interrupt_status = 1;
364 host.interrupt_generate = 1;
365 write_host_csr(&host);
366
367 return mei_wait_for_me_ready();
368}
369
370static inline int mei_sendrecv_mkhi(struct mkhi_header *mkhi,
371 void *req_data, int req_bytes,
372 void *rsp_data, int rsp_bytes)
373{
374 struct mkhi_header mkhi_rsp;
375
376 /* Send header */
377 if (mei_send_header(MEI_ADDRESS_MKHI, MEI_HOST_ADDRESS,
378 mkhi, sizeof(*mkhi), req_bytes ? 0 : 1) < 0)
379 return -1;
380
381 /* Send data if available */
382 if (req_bytes && mei_send_data(MEI_ADDRESS_MKHI, MEI_HOST_ADDRESS,
383 req_data, req_bytes) < 0)
384 return -1;
385
386 /* Return now if no response expected */
387 if (!rsp_bytes)
388 return 0;
389
390 /* Read header and data */
391 if (mei_recv_msg(&mkhi_rsp, sizeof(mkhi_rsp),
392 rsp_data, rsp_bytes) < 0)
393 return -1;
394
395 if (!mkhi_rsp.is_response ||
396 mkhi->group_id != mkhi_rsp.group_id ||
397 mkhi->command != mkhi_rsp.command) {
398 printk(BIOS_ERR, "ME: invalid response, group %u ?= %u,"
399 "command %u ?= %u, is_response %u\n", mkhi->group_id,
400 mkhi_rsp.group_id, mkhi->command, mkhi_rsp.command,
401 mkhi_rsp.is_response);
402 return -1;
403 }
404
405 return 0;
406}
407
408static inline int mei_sendrecv_icc(struct icc_header *icc,
409 void *req_data, int req_bytes,
410 void *rsp_data, int rsp_bytes)
411{
412 struct icc_header icc_rsp;
413
414 /* Send header */
415 if (mei_send_header(MEI_ADDRESS_ICC, MEI_HOST_ADDRESS,
416 icc, sizeof(*icc), req_bytes ? 0 : 1) < 0)
417 return -1;
418
419 /* Send data if available */
420 if (req_bytes && mei_send_data(MEI_ADDRESS_ICC, MEI_HOST_ADDRESS,
421 req_data, req_bytes) < 0)
422 return -1;
423
424 /* Read header and data, if needed */
425 if (rsp_bytes && mei_recv_msg(&icc_rsp, sizeof(icc_rsp),
426 rsp_data, rsp_bytes) < 0)
427 return -1;
428
429 return 0;
430}
431
432/*
433 * mbp give up routine. This path is taken if hfs.mpb_rdy is 0 or the read
434 * state machine on the BIOS end doesn't match the ME's state machine.
435 */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200436static void intel_me_mbp_give_up(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700437{
438 struct mei_csr csr;
439
440 pci_write_config32(dev, PCI_ME_H_GS2, PCI_ME_MBP_GIVE_UP);
441
442 read_host_csr(&csr);
443 csr.reset = 1;
444 csr.interrupt_generate = 1;
445 write_host_csr(&csr);
446}
447
448/*
449 * mbp clear routine. This will wait for the ME to indicate that
450 * the MBP has been read and cleared.
451 */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200452static void intel_me_mbp_clear(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700453{
454 int count;
455 struct me_hfs2 hfs2;
456
457 /* Wait for the mbp_cleared indicator */
458 for (count = ME_RETRY; count > 0; --count) {
459 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
460 if (hfs2.mbp_cleared)
461 break;
462 udelay(ME_DELAY);
463 }
464
465 if (count == 0) {
466 printk(BIOS_WARNING, "ME: Timeout waiting for mbp_cleared\n");
467 intel_me_mbp_give_up(dev);
468 } else {
469 printk(BIOS_INFO, "ME: MBP cleared\n");
470 }
471}
472
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700473static void me_print_fw_version(mbp_fw_version_name *vers_name)
474{
475 if (!vers_name) {
476 printk(BIOS_ERR, "ME: mbp missing version report\n");
477 return;
478 }
479
480 printk(BIOS_DEBUG, "ME: found version %d.%d.%d.%d\n",
481 vers_name->major_version, vers_name->minor_version,
482 vers_name->hotfix_version, vers_name->build_version);
483}
484
Edward O'Callaghan8cc5dc12015-01-07 15:50:43 +1100485static inline void print_cap(const char *name, int state)
486{
487 printk(BIOS_DEBUG, "ME Capability: %-41s : %sabled\n",
488 name, state ? " en" : "dis");
489}
490
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700491/* Get ME Firmware Capabilities */
492static int mkhi_get_fwcaps(mbp_mefwcaps *cap)
493{
494 u32 rule_id = 0;
495 struct me_fwcaps cap_msg;
496 struct mkhi_header mkhi = {
497 .group_id = MKHI_GROUP_ID_FWCAPS,
498 .command = MKHI_FWCAPS_GET_RULE,
499 };
500
501 /* Send request and wait for response */
502 if (mei_sendrecv_mkhi(&mkhi, &rule_id, sizeof(u32),
503 &cap_msg, sizeof(cap_msg)) < 0) {
504 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
505 return -1;
Lee Leahy26b7cd02017-03-16 18:47:55 -0700506 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700507 *cap = cap_msg.caps_sku;
508 return 0;
509}
510
511/* Get ME Firmware Capabilities */
512static void me_print_fwcaps(mbp_mefwcaps *cap)
513{
514 mbp_mefwcaps local_caps;
515 if (!cap) {
516 cap = &local_caps;
517 printk(BIOS_ERR, "ME: mbp missing fwcaps report\n");
518 if (mkhi_get_fwcaps(cap))
519 return;
520 }
521
522 print_cap("Full Network manageability", cap->full_net);
523 print_cap("Regular Network manageability", cap->std_net);
524 print_cap("Manageability", cap->manageability);
525 print_cap("IntelR Anti-Theft (AT)", cap->intel_at);
526 print_cap("IntelR Capability Licensing Service (CLS)", cap->intel_cls);
527 print_cap("IntelR Power Sharing Technology (MPC)", cap->intel_mpc);
528 print_cap("ICC Over Clocking", cap->icc_over_clocking);
Edward O'Callaghan8cc5dc12015-01-07 15:50:43 +1100529 print_cap("Protected Audio Video Path (PAVP)", cap->pavp);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700530 print_cap("IPV6", cap->ipv6);
531 print_cap("KVM Remote Control (KVM)", cap->kvm);
532 print_cap("Outbreak Containment Heuristic (OCH)", cap->och);
533 print_cap("Virtual LAN (VLAN)", cap->vlan);
534 print_cap("TLS", cap->tls);
535 print_cap("Wireless LAN (WLAN)", cap->wlan);
536}
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700537
538/* Send END OF POST message to the ME */
539static int mkhi_end_of_post(void)
540{
541 struct mkhi_header mkhi = {
542 .group_id = MKHI_GROUP_ID_GEN,
543 .command = MKHI_END_OF_POST,
544 };
545 u32 eop_ack;
546
547 /* Send request and wait for response */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700548 if (mei_sendrecv_mkhi(&mkhi, NULL, 0, &eop_ack, sizeof(eop_ack)) < 0) {
549 printk(BIOS_ERR, "ME: END OF POST message failed\n");
550 return -1;
551 }
552
553 printk(BIOS_INFO, "ME: END OF POST message successful (%d)\n", eop_ack);
554 return 0;
555}
556
Duncan Lauriec99681f2014-12-10 08:11:09 -0800557/* Send END OF POST message to the ME */
558static int mkhi_end_of_post_noack(void)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700559{
Duncan Lauriec99681f2014-12-10 08:11:09 -0800560 struct mkhi_header mkhi = {
561 .group_id = MKHI_GROUP_ID_GEN,
562 .command = MKHI_END_OF_POST_NOACK,
563 };
564
565 /* Send request, do not wait for response */
566 if (mei_sendrecv_mkhi(&mkhi, NULL, 0, NULL, 0) < 0) {
567 printk(BIOS_ERR, "ME: END OF POST NOACK message failed\n");
568 return -1;
569 }
570
571 printk(BIOS_INFO, "ME: END OF POST NOACK message successful\n");
572 return 0;
573}
574
575/* Send HMRFPO LOCK message to the ME */
576static int mkhi_hmrfpo_lock(void)
577{
578 struct mkhi_header mkhi = {
579 .group_id = MKHI_GROUP_ID_HMRFPO,
580 .command = MKHI_HMRFPO_LOCK,
581 };
582 u32 ack;
583
584 /* Send request and wait for response */
585 if (mei_sendrecv_mkhi(&mkhi, NULL, 0, &ack, sizeof(ack)) < 0) {
586 printk(BIOS_ERR, "ME: HMRFPO LOCK message failed\n");
587 return -1;
588 }
589
Angel Ponscac22172018-10-01 09:56:32 +0200590 printk(BIOS_INFO, "ME: HMRFPO LOCK message successful (%d)\n", ack);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800591 return 0;
592}
593
594/* Send HMRFPO LOCK message to the ME, do not wait for response */
595static int mkhi_hmrfpo_lock_noack(void)
596{
597 struct mkhi_header mkhi = {
598 .group_id = MKHI_GROUP_ID_HMRFPO,
599 .command = MKHI_HMRFPO_LOCK_NOACK,
600 };
601
602 /* Send request, do not wait for response */
603 if (mei_sendrecv_mkhi(&mkhi, NULL, 0, NULL, 0) < 0) {
604 printk(BIOS_ERR, "ME: HMRFPO LOCK NOACK message failed\n");
605 return -1;
606 }
607
Angel Ponscac22172018-10-01 09:56:32 +0200608 printk(BIOS_INFO, "ME: HMRFPO LOCK NOACK message successful\n");
Duncan Lauriec99681f2014-12-10 08:11:09 -0800609 return 0;
610}
611
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200612static void intel_me_finalize(struct device *dev)
Duncan Lauriec99681f2014-12-10 08:11:09 -0800613{
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700614 u32 reg32;
615
616 /* S3 path will have hidden this device already */
Lee Leahy26b7cd02017-03-16 18:47:55 -0700617 if (!mei_base_address || mei_base_address == (u8 *) 0xfffffff0)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700618 return;
619
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700620 /* Make sure IO is disabled */
621 reg32 = pci_read_config32(dev, PCI_COMMAND);
622 reg32 &= ~(PCI_COMMAND_MASTER |
623 PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
624 pci_write_config32(dev, PCI_COMMAND, reg32);
625
626 /* Hide the PCI device */
627 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800628 RCBA32(FD2);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700629}
630
631static int me_icc_set_clock_enables(u32 mask)
632{
633 struct icc_clock_enables_msg clk = {
634 .clock_enables = 0, /* Turn off specified clocks */
635 .clock_mask = mask,
636 .no_response = 1, /* Do not expect response */
637 };
638 struct icc_header icc = {
639 .api_version = ICC_API_VERSION_LYNXPOINT,
640 .icc_command = ICC_SET_CLOCK_ENABLES,
641 .length = sizeof(clk),
642 };
643
644 /* Send request and wait for response */
645 if (mei_sendrecv_icc(&icc, &clk, sizeof(clk), NULL, 0) < 0) {
646 printk(BIOS_ERR, "ME: ICC SET CLOCK ENABLES message failed\n");
647 return -1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700648 }
Lee Leahy8a9c7dc2017-03-17 10:43:25 -0700649 printk(BIOS_INFO, "ME: ICC SET CLOCK ENABLES 0x%08x\n", mask);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700650 return 0;
651}
652
653/* Determine the path that we should take based on ME status */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200654static me_bios_path intel_me_path(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700655{
656 me_bios_path path = ME_DISABLE_BIOS_PATH;
657 struct me_hfs hfs;
658 struct me_hfs2 hfs2;
659
660 /* Check and dump status */
661 intel_me_status();
662
663 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
664 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
665
666 /* Check Current Working State */
667 switch (hfs.working_state) {
668 case ME_HFS_CWS_NORMAL:
669 path = ME_NORMAL_BIOS_PATH;
670 break;
671 case ME_HFS_CWS_REC:
672 path = ME_RECOVERY_BIOS_PATH;
673 break;
674 default:
675 path = ME_DISABLE_BIOS_PATH;
676 break;
677 }
678
679 /* Check Current Operation Mode */
680 switch (hfs.operation_mode) {
681 case ME_HFS_MODE_NORMAL:
682 break;
683 case ME_HFS_MODE_DEBUG:
684 case ME_HFS_MODE_DIS:
685 case ME_HFS_MODE_OVER_JMPR:
686 case ME_HFS_MODE_OVER_MEI:
687 default:
688 path = ME_DISABLE_BIOS_PATH;
689 break;
690 }
691
692 /* Check for any error code and valid firmware and MBP */
693 if (hfs.error_code || hfs.fpt_bad)
694 path = ME_ERROR_BIOS_PATH;
695
696 /* Check if the MBP is ready */
697 if (!hfs2.mbp_rdy) {
698 printk(BIOS_CRIT, "%s: mbp is not ready!\n",
Lee Leahy6ef51922017-03-17 10:56:08 -0700699 __func__);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700700 path = ME_ERROR_BIOS_PATH;
701 }
702
Kyösti Mälkkibe5317f2019-11-06 12:07:21 +0200703 if (CONFIG(ELOG) && path != ME_NORMAL_BIOS_PATH) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700704 struct elog_event_data_me_extended data = {
705 .current_working_state = hfs.working_state,
706 .operation_state = hfs.operation_state,
707 .operation_mode = hfs.operation_mode,
708 .error_code = hfs.error_code,
709 .progress_code = hfs2.progress_code,
710 .current_pmevent = hfs2.current_pmevent,
711 .current_state = hfs2.current_state,
712 };
713 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
714 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
715 &data, sizeof(data));
716 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700717
718 return path;
719}
720
721/* Prepare ME for MEI messages */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200722static int intel_mei_setup(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700723{
724 struct resource *res;
725 struct mei_csr host;
726 u32 reg32;
727
728 /* Find the MMIO base for the ME interface */
729 res = find_resource(dev, PCI_BASE_ADDRESS_0);
730 if (!res || res->base == 0 || res->size == 0) {
731 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
732 return -1;
733 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800734 mei_base_address = res2mmio(res, 0, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700735
736 /* Ensure Memory and Bus Master bits are set */
737 reg32 = pci_read_config32(dev, PCI_COMMAND);
738 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
739 pci_write_config32(dev, PCI_COMMAND, reg32);
740
741 /* Clean up status for next message */
742 read_host_csr(&host);
743 host.interrupt_generate = 1;
744 host.ready = 1;
745 host.reset = 0;
746 write_host_csr(&host);
747
748 return 0;
749}
750
751/* Read the Extend register hash of ME firmware */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200752static int intel_me_extend_valid(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700753{
754 struct me_heres status;
755 u32 extend[8] = {0};
756 int i, count = 0;
757
758 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
759 if (!status.extend_feature_present) {
760 printk(BIOS_ERR, "ME: Extend Feature not present\n");
761 return -1;
762 }
763
764 if (!status.extend_reg_valid) {
765 printk(BIOS_ERR, "ME: Extend Register not valid\n");
766 return -1;
767 }
768
769 switch (status.extend_reg_algorithm) {
770 case PCI_ME_EXT_SHA1:
771 count = 5;
772 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
773 break;
774 case PCI_ME_EXT_SHA256:
775 count = 8;
776 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
777 break;
778 default:
779 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
780 status.extend_reg_algorithm);
781 return -1;
782 }
783
784 for (i = 0; i < count; ++i) {
785 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
786 printk(BIOS_DEBUG, "%08x", extend[i]);
787 }
788 printk(BIOS_DEBUG, "\n");
789
Julius Wernercd49cce2019-03-05 16:53:33 -0800790#if CONFIG(CHROMEOS)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700791 /* Save hash in NVS for the OS to verify */
792 chromeos_set_me_hash(extend, count);
793#endif
794
795 return 0;
796}
797
Duncan Lauriec99681f2014-12-10 08:11:09 -0800798static void intel_me_print_mbp(me_bios_payload *mbp_data)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700799{
Duncan Lauriec99681f2014-12-10 08:11:09 -0800800 me_print_fw_version(mbp_data->fw_version_name);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700801
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200802 if (CONFIG(DEBUG_INTEL_ME))
803 me_print_fwcaps(mbp_data->fw_capabilities);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700804
Duncan Lauriec99681f2014-12-10 08:11:09 -0800805 if (mbp_data->plat_time) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700806 printk(BIOS_DEBUG, "ME: Wake Event to ME Reset: %u ms\n",
Duncan Lauriec99681f2014-12-10 08:11:09 -0800807 mbp_data->plat_time->wake_event_mrst_time_ms);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700808 printk(BIOS_DEBUG, "ME: ME Reset to Platform Reset: %u ms\n",
Duncan Lauriec99681f2014-12-10 08:11:09 -0800809 mbp_data->plat_time->mrst_pltrst_time_ms);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700810 printk(BIOS_DEBUG, "ME: Platform Reset to CPU Reset: %u ms\n",
Duncan Lauriec99681f2014-12-10 08:11:09 -0800811 mbp_data->plat_time->pltrst_cpurst_time_ms);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700812 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700813}
814
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700815static u32 me_to_host_words_pending(void)
816{
817 struct mei_csr me;
818 read_me_csr(&me);
819 if (!me.ready)
820 return 0;
821 return (me.buffer_write_ptr - me.buffer_read_ptr) &
822 (me.buffer_depth - 1);
823}
824
825struct mbp_payload {
826 mbp_header header;
827 u32 data[0];
828};
829
830/*
Duncan Lauriec99681f2014-12-10 08:11:09 -0800831 * Read and print ME MBP data
832 *
833 * Return -1 to indicate a problem (give up)
834 * Return 0 to indicate success (send LOCK+EOP)
835 * Return 1 to indicate success (send LOCK+EOP with NOACK)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700836 */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200837static int intel_me_read_mbp(me_bios_payload *mbp_data, struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700838{
839 mbp_header mbp_hdr;
840 u32 me2host_pending;
841 struct mei_csr host;
842 struct me_hfs2 hfs2;
843 struct mbp_payload *mbp;
844 int i;
Duncan Lauriec99681f2014-12-10 08:11:09 -0800845 int ret = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700846
847 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
848
849 if (!hfs2.mbp_rdy) {
850 printk(BIOS_ERR, "ME: MBP not ready\n");
Duncan Lauriec99681f2014-12-10 08:11:09 -0800851 intel_me_mbp_give_up(dev);
852 return -1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700853 }
854
855 me2host_pending = me_to_host_words_pending();
856 if (!me2host_pending) {
857 printk(BIOS_ERR, "ME: no mbp data!\n");
Duncan Lauriec99681f2014-12-10 08:11:09 -0800858 intel_me_mbp_give_up(dev);
859 return -1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700860 }
861
862 /* we know for sure that at least the header is there */
863 mei_read_dword_ptr(&mbp_hdr, MEI_ME_CB_RW);
864
865 if ((mbp_hdr.num_entries > (mbp_hdr.mbp_size / 2)) ||
866 (me2host_pending < mbp_hdr.mbp_size)) {
867 printk(BIOS_ERR, "ME: mbp of %d entries, total size %d words"
868 " buffer contains %d words\n",
869 mbp_hdr.num_entries, mbp_hdr.mbp_size,
870 me2host_pending);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800871 intel_me_mbp_give_up(dev);
872 return -1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700873 }
874 mbp = malloc(mbp_hdr.mbp_size * sizeof(u32));
Duncan Lauriec99681f2014-12-10 08:11:09 -0800875 if (!mbp) {
876 intel_me_mbp_give_up(dev);
877 return -1;
878 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700879
880 mbp->header = mbp_hdr;
881 me2host_pending--;
882
883 i = 0;
884 while (i != me2host_pending) {
885 mei_read_dword_ptr(&mbp->data[i], MEI_ME_CB_RW);
886 i++;
887 }
888
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700889 read_host_csr(&host);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800890
891 /* Check that read and write pointers are equal. */
892 if (host.buffer_read_ptr != host.buffer_write_ptr) {
893 printk(BIOS_INFO, "ME: MBP Read/Write pointer mismatch\n");
894 printk(BIOS_INFO, "ME: MBP Waiting for MBP cleared flag\n");
895
896 /* Tell ME that the host has finished reading the MBP. */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700897 host.interrupt_generate = 1;
Duncan Lauriec99681f2014-12-10 08:11:09 -0800898 host.reset = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700899 write_host_csr(&host);
900
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700901 /* Wait for the mbp_cleared indicator. */
902 intel_me_mbp_clear(dev);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800903 } else {
904 /* Indicate NOACK messages should be used. */
905 ret = 1;
906 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700907
908 /* Dump out the MBP contents. */
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200909 if (CONFIG(DEBUG_INTEL_ME)) {
910 printk(BIOS_INFO, "ME MBP: Header: items: %d, size dw: %d\n",
911 mbp->header.num_entries, mbp->header.mbp_size);
912 for (i = 0; i < mbp->header.mbp_size - 1; i++)
913 printk(BIOS_INFO, "ME MBP: %04x: 0x%08x\n", i, mbp->data[i]);
914 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700915
Lee Leahy26b7cd02017-03-16 18:47:55 -0700916#define ASSIGN_FIELD_PTR(field_, val_) \
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700917 { \
918 mbp_data->field_ = (typeof(mbp_data->field_))(void *)val_; \
919 break; \
920 }
921
922 /* Setup the pointers in the me_bios_payload structure. */
923 for (i = 0; i < mbp->header.mbp_size - 1;) {
924 mbp_item_header *item = (void *)&mbp->data[i];
925
Lee Leahy26b7cd02017-03-16 18:47:55 -0700926 switch (MBP_MAKE_IDENT(item->app_id, item->item_id)) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700927 case MBP_IDENT(KERNEL, FW_VER):
928 ASSIGN_FIELD_PTR(fw_version_name, &mbp->data[i+1]);
929
930 case MBP_IDENT(ICC, PROFILE):
931 ASSIGN_FIELD_PTR(icc_profile, &mbp->data[i+1]);
932
933 case MBP_IDENT(INTEL_AT, STATE):
934 ASSIGN_FIELD_PTR(at_state, &mbp->data[i+1]);
935
936 case MBP_IDENT(KERNEL, FW_CAP):
937 ASSIGN_FIELD_PTR(fw_capabilities, &mbp->data[i+1]);
938
939 case MBP_IDENT(KERNEL, ROM_BIST):
940 ASSIGN_FIELD_PTR(rom_bist_data, &mbp->data[i+1]);
941
942 case MBP_IDENT(KERNEL, PLAT_KEY):
943 ASSIGN_FIELD_PTR(platform_key, &mbp->data[i+1]);
944
945 case MBP_IDENT(KERNEL, FW_TYPE):
946 ASSIGN_FIELD_PTR(fw_plat_type, &mbp->data[i+1]);
947
948 case MBP_IDENT(KERNEL, MFS_FAILURE):
949 ASSIGN_FIELD_PTR(mfsintegrity, &mbp->data[i+1]);
950
951 case MBP_IDENT(KERNEL, PLAT_TIME):
952 ASSIGN_FIELD_PTR(plat_time, &mbp->data[i+1]);
953
954 case MBP_IDENT(NFC, SUPPORT_DATA):
955 ASSIGN_FIELD_PTR(nfc_data, &mbp->data[i+1]);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700956 }
957 i += item->length;
958 }
959 #undef ASSIGN_FIELD_PTR
960
Patrick Georgib753eeb2016-10-18 19:46:33 +0200961 free(mbp);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800962 return ret;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700963}
Duncan Lauriec99681f2014-12-10 08:11:09 -0800964
965/* Check whether ME is present and do basic init */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200966static void intel_me_init(struct device *dev)
Duncan Lauriec99681f2014-12-10 08:11:09 -0800967{
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300968 config_t *config = config_of(dev);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800969 me_bios_path path = intel_me_path(dev);
970 me_bios_payload mbp_data;
971 int mbp_ret;
972 struct me_hfs hfs;
973 struct mei_csr csr;
974
975 /* Do initial setup and determine the BIOS path */
976 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
977
978 if (path == ME_NORMAL_BIOS_PATH) {
979 /* Validate the extend register */
980 intel_me_extend_valid(dev);
981}
982
983 memset(&mbp_data, 0, sizeof(mbp_data));
984
985 /*
986 * According to the ME9 BWG, BIOS is required to fetch MBP data in
987 * all boot flows except S3 Resume.
988 */
989
990 /* Prepare MEI MMIO interface */
991 if (intel_mei_setup(dev) < 0)
992 return;
993
994 /* Read ME MBP data */
995 mbp_ret = intel_me_read_mbp(&mbp_data, dev);
996 if (mbp_ret < 0)
997 return;
998 intel_me_print_mbp(&mbp_data);
999
1000 /* Set clock enables according to devicetree */
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +03001001 if (config->icc_clock_disable)
Duncan Lauriec99681f2014-12-10 08:11:09 -08001002 me_icc_set_clock_enables(config->icc_clock_disable);
1003
1004 /* Make sure ME is in a mode that expects EOP */
1005 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
1006
1007 /* Abort and leave device alone if not normal mode */
1008 if (hfs.fpt_bad ||
1009 hfs.working_state != ME_HFS_CWS_NORMAL ||
1010 hfs.operation_mode != ME_HFS_MODE_NORMAL)
1011 return;
1012
1013 if (mbp_ret) {
1014 /*
1015 * MBP Cleared wait is skipped,
1016 * Do not expect ACK and reset when complete.
1017 */
1018
1019 /* Send HMRFPO Lock command, no response */
1020 mkhi_hmrfpo_lock_noack();
1021
1022 /* Send END OF POST command, no response */
1023 mkhi_end_of_post_noack();
1024
1025 /* Assert reset and interrupt */
1026 read_host_csr(&csr);
1027 csr.interrupt_generate = 1;
1028 csr.reset = 1;
1029 write_host_csr(&csr);
1030 } else {
1031 /*
1032 * MBP Cleared wait was not skipped
1033 */
1034
1035 /* Send HMRFPO LOCK command */
1036 mkhi_hmrfpo_lock();
1037
1038 /* Send EOP command so ME stops accepting other commands */
1039 mkhi_end_of_post();
1040 }
1041}
1042
Elyes HAOUAS040aff22018-05-27 16:30:36 +02001043static void intel_me_enable(struct device *dev)
Duncan Lauriec99681f2014-12-10 08:11:09 -08001044{
Duncan Lauriec99681f2014-12-10 08:11:09 -08001045 /* Avoid talking to the device in S3 path */
Kyösti Mälkki1ec23c92015-05-29 06:18:18 +03001046 if (acpi_is_wakeup_s3()) {
Duncan Lauriec99681f2014-12-10 08:11:09 -08001047 dev->enabled = 0;
1048 pch_disable_devfn(dev);
1049 }
Duncan Lauriec99681f2014-12-10 08:11:09 -08001050}
1051
1052static struct device_operations device_ops = {
1053 .read_resources = &pci_dev_read_resources,
1054 .set_resources = &pci_dev_set_resources,
1055 .enable_resources = &pci_dev_enable_resources,
1056 .enable = &intel_me_enable,
1057 .init = &intel_me_init,
1058 .final = &intel_me_finalize,
1059 .ops_pci = &broadwell_pci_ops,
1060};
1061
1062static const unsigned short pci_device_ids[] = {
1063 0x9c3a, /* Low Power */
1064 0x9cba, /* WildcatPoint */
1065 0
1066};
1067
1068static const struct pci_driver intel_me __pci_driver = {
1069 .ops = &device_ops,
1070 .vendor = PCI_VENDOR_ID_INTEL,
1071 .devices = pci_device_ids,
1072};