blob: 0461428ba59200720368f7b302677e87592e17f5 [file] [log] [blame]
Duncan Lauriec88c54c2014-04-30 16:36:13 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Duncan Lauriec88c54c2014-04-30 16:36:13 -070014 */
15
16/*
17 * This is a ramstage driver for the Intel Management Engine found in the
18 * southbridge. It handles the required boot-time messages over the
19 * MMIO-based Management Engine Interface to tell the ME that the BIOS is
20 * finished with POST. Additional messages are defined for debug but are
21 * not used unless the console loglevel is high enough.
22 */
23
24#include <arch/acpi.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020025#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020026#include <device/pci_ops.h>
Duncan Lauriec88c54c2014-04-30 16:36:13 -070027#include <console/console.h>
28#include <device/device.h>
29#include <device/pci.h>
30#include <device/pci_ids.h>
31#include <device/pci_def.h>
32#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
Julius Wernercd49cce2019-03-05 16:53:33 -0800703#if CONFIG(ELOG)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700704 if (path != ME_NORMAL_BIOS_PATH) {
705 struct elog_event_data_me_extended data = {
706 .current_working_state = hfs.working_state,
707 .operation_state = hfs.operation_state,
708 .operation_mode = hfs.operation_mode,
709 .error_code = hfs.error_code,
710 .progress_code = hfs2.progress_code,
711 .current_pmevent = hfs2.current_pmevent,
712 .current_state = hfs2.current_state,
713 };
714 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
715 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
716 &data, sizeof(data));
717 }
718#endif
719
720 return path;
721}
722
723/* Prepare ME for MEI messages */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200724static int intel_mei_setup(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700725{
726 struct resource *res;
727 struct mei_csr host;
728 u32 reg32;
729
730 /* Find the MMIO base for the ME interface */
731 res = find_resource(dev, PCI_BASE_ADDRESS_0);
732 if (!res || res->base == 0 || res->size == 0) {
733 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
734 return -1;
735 }
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800736 mei_base_address = res2mmio(res, 0, 0);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700737
738 /* Ensure Memory and Bus Master bits are set */
739 reg32 = pci_read_config32(dev, PCI_COMMAND);
740 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY;
741 pci_write_config32(dev, PCI_COMMAND, reg32);
742
743 /* Clean up status for next message */
744 read_host_csr(&host);
745 host.interrupt_generate = 1;
746 host.ready = 1;
747 host.reset = 0;
748 write_host_csr(&host);
749
750 return 0;
751}
752
753/* Read the Extend register hash of ME firmware */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200754static int intel_me_extend_valid(struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700755{
756 struct me_heres status;
757 u32 extend[8] = {0};
758 int i, count = 0;
759
760 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
761 if (!status.extend_feature_present) {
762 printk(BIOS_ERR, "ME: Extend Feature not present\n");
763 return -1;
764 }
765
766 if (!status.extend_reg_valid) {
767 printk(BIOS_ERR, "ME: Extend Register not valid\n");
768 return -1;
769 }
770
771 switch (status.extend_reg_algorithm) {
772 case PCI_ME_EXT_SHA1:
773 count = 5;
774 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
775 break;
776 case PCI_ME_EXT_SHA256:
777 count = 8;
778 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
779 break;
780 default:
781 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
782 status.extend_reg_algorithm);
783 return -1;
784 }
785
786 for (i = 0; i < count; ++i) {
787 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
788 printk(BIOS_DEBUG, "%08x", extend[i]);
789 }
790 printk(BIOS_DEBUG, "\n");
791
Julius Wernercd49cce2019-03-05 16:53:33 -0800792#if CONFIG(CHROMEOS)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700793 /* Save hash in NVS for the OS to verify */
794 chromeos_set_me_hash(extend, count);
795#endif
796
797 return 0;
798}
799
Duncan Lauriec99681f2014-12-10 08:11:09 -0800800static void intel_me_print_mbp(me_bios_payload *mbp_data)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700801{
Duncan Lauriec99681f2014-12-10 08:11:09 -0800802 me_print_fw_version(mbp_data->fw_version_name);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700803
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200804 if (CONFIG(DEBUG_INTEL_ME))
805 me_print_fwcaps(mbp_data->fw_capabilities);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700806
Duncan Lauriec99681f2014-12-10 08:11:09 -0800807 if (mbp_data->plat_time) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700808 printk(BIOS_DEBUG, "ME: Wake Event to ME Reset: %u ms\n",
Duncan Lauriec99681f2014-12-10 08:11:09 -0800809 mbp_data->plat_time->wake_event_mrst_time_ms);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700810 printk(BIOS_DEBUG, "ME: ME Reset to Platform Reset: %u ms\n",
Duncan Lauriec99681f2014-12-10 08:11:09 -0800811 mbp_data->plat_time->mrst_pltrst_time_ms);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700812 printk(BIOS_DEBUG, "ME: Platform Reset to CPU Reset: %u ms\n",
Duncan Lauriec99681f2014-12-10 08:11:09 -0800813 mbp_data->plat_time->pltrst_cpurst_time_ms);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700814 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700815}
816
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700817static u32 me_to_host_words_pending(void)
818{
819 struct mei_csr me;
820 read_me_csr(&me);
821 if (!me.ready)
822 return 0;
823 return (me.buffer_write_ptr - me.buffer_read_ptr) &
824 (me.buffer_depth - 1);
825}
826
827struct mbp_payload {
828 mbp_header header;
829 u32 data[0];
830};
831
832/*
Duncan Lauriec99681f2014-12-10 08:11:09 -0800833 * Read and print ME MBP data
834 *
835 * Return -1 to indicate a problem (give up)
836 * Return 0 to indicate success (send LOCK+EOP)
837 * Return 1 to indicate success (send LOCK+EOP with NOACK)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700838 */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200839static int intel_me_read_mbp(me_bios_payload *mbp_data, struct device *dev)
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700840{
841 mbp_header mbp_hdr;
842 u32 me2host_pending;
843 struct mei_csr host;
844 struct me_hfs2 hfs2;
845 struct mbp_payload *mbp;
846 int i;
Duncan Lauriec99681f2014-12-10 08:11:09 -0800847 int ret = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700848
849 pci_read_dword_ptr(dev, &hfs2, PCI_ME_HFS2);
850
851 if (!hfs2.mbp_rdy) {
852 printk(BIOS_ERR, "ME: MBP not ready\n");
Duncan Lauriec99681f2014-12-10 08:11:09 -0800853 intel_me_mbp_give_up(dev);
854 return -1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700855 }
856
857 me2host_pending = me_to_host_words_pending();
858 if (!me2host_pending) {
859 printk(BIOS_ERR, "ME: no mbp data!\n");
Duncan Lauriec99681f2014-12-10 08:11:09 -0800860 intel_me_mbp_give_up(dev);
861 return -1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700862 }
863
864 /* we know for sure that at least the header is there */
865 mei_read_dword_ptr(&mbp_hdr, MEI_ME_CB_RW);
866
867 if ((mbp_hdr.num_entries > (mbp_hdr.mbp_size / 2)) ||
868 (me2host_pending < mbp_hdr.mbp_size)) {
869 printk(BIOS_ERR, "ME: mbp of %d entries, total size %d words"
870 " buffer contains %d words\n",
871 mbp_hdr.num_entries, mbp_hdr.mbp_size,
872 me2host_pending);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800873 intel_me_mbp_give_up(dev);
874 return -1;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700875 }
876 mbp = malloc(mbp_hdr.mbp_size * sizeof(u32));
Duncan Lauriec99681f2014-12-10 08:11:09 -0800877 if (!mbp) {
878 intel_me_mbp_give_up(dev);
879 return -1;
880 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700881
882 mbp->header = mbp_hdr;
883 me2host_pending--;
884
885 i = 0;
886 while (i != me2host_pending) {
887 mei_read_dword_ptr(&mbp->data[i], MEI_ME_CB_RW);
888 i++;
889 }
890
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700891 read_host_csr(&host);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800892
893 /* Check that read and write pointers are equal. */
894 if (host.buffer_read_ptr != host.buffer_write_ptr) {
895 printk(BIOS_INFO, "ME: MBP Read/Write pointer mismatch\n");
896 printk(BIOS_INFO, "ME: MBP Waiting for MBP cleared flag\n");
897
898 /* Tell ME that the host has finished reading the MBP. */
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700899 host.interrupt_generate = 1;
Duncan Lauriec99681f2014-12-10 08:11:09 -0800900 host.reset = 0;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700901 write_host_csr(&host);
902
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700903 /* Wait for the mbp_cleared indicator. */
904 intel_me_mbp_clear(dev);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800905 } else {
906 /* Indicate NOACK messages should be used. */
907 ret = 1;
908 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700909
910 /* Dump out the MBP contents. */
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200911 if (CONFIG(DEBUG_INTEL_ME)) {
912 printk(BIOS_INFO, "ME MBP: Header: items: %d, size dw: %d\n",
913 mbp->header.num_entries, mbp->header.mbp_size);
914 for (i = 0; i < mbp->header.mbp_size - 1; i++)
915 printk(BIOS_INFO, "ME MBP: %04x: 0x%08x\n", i, mbp->data[i]);
916 }
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700917
Lee Leahy26b7cd02017-03-16 18:47:55 -0700918#define ASSIGN_FIELD_PTR(field_, val_) \
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700919 { \
920 mbp_data->field_ = (typeof(mbp_data->field_))(void *)val_; \
921 break; \
922 }
923
924 /* Setup the pointers in the me_bios_payload structure. */
925 for (i = 0; i < mbp->header.mbp_size - 1;) {
926 mbp_item_header *item = (void *)&mbp->data[i];
927
Lee Leahy26b7cd02017-03-16 18:47:55 -0700928 switch (MBP_MAKE_IDENT(item->app_id, item->item_id)) {
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700929 case MBP_IDENT(KERNEL, FW_VER):
930 ASSIGN_FIELD_PTR(fw_version_name, &mbp->data[i+1]);
931
932 case MBP_IDENT(ICC, PROFILE):
933 ASSIGN_FIELD_PTR(icc_profile, &mbp->data[i+1]);
934
935 case MBP_IDENT(INTEL_AT, STATE):
936 ASSIGN_FIELD_PTR(at_state, &mbp->data[i+1]);
937
938 case MBP_IDENT(KERNEL, FW_CAP):
939 ASSIGN_FIELD_PTR(fw_capabilities, &mbp->data[i+1]);
940
941 case MBP_IDENT(KERNEL, ROM_BIST):
942 ASSIGN_FIELD_PTR(rom_bist_data, &mbp->data[i+1]);
943
944 case MBP_IDENT(KERNEL, PLAT_KEY):
945 ASSIGN_FIELD_PTR(platform_key, &mbp->data[i+1]);
946
947 case MBP_IDENT(KERNEL, FW_TYPE):
948 ASSIGN_FIELD_PTR(fw_plat_type, &mbp->data[i+1]);
949
950 case MBP_IDENT(KERNEL, MFS_FAILURE):
951 ASSIGN_FIELD_PTR(mfsintegrity, &mbp->data[i+1]);
952
953 case MBP_IDENT(KERNEL, PLAT_TIME):
954 ASSIGN_FIELD_PTR(plat_time, &mbp->data[i+1]);
955
956 case MBP_IDENT(NFC, SUPPORT_DATA):
957 ASSIGN_FIELD_PTR(nfc_data, &mbp->data[i+1]);
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700958 }
959 i += item->length;
960 }
961 #undef ASSIGN_FIELD_PTR
962
Patrick Georgib753eeb2016-10-18 19:46:33 +0200963 free(mbp);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800964 return ret;
Duncan Lauriec88c54c2014-04-30 16:36:13 -0700965}
Duncan Lauriec99681f2014-12-10 08:11:09 -0800966
967/* Check whether ME is present and do basic init */
Elyes HAOUAS040aff22018-05-27 16:30:36 +0200968static void intel_me_init(struct device *dev)
Duncan Lauriec99681f2014-12-10 08:11:09 -0800969{
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +0300970 config_t *config = config_of(dev);
Duncan Lauriec99681f2014-12-10 08:11:09 -0800971 me_bios_path path = intel_me_path(dev);
972 me_bios_payload mbp_data;
973 int mbp_ret;
974 struct me_hfs hfs;
975 struct mei_csr csr;
976
977 /* Do initial setup and determine the BIOS path */
978 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
979
980 if (path == ME_NORMAL_BIOS_PATH) {
981 /* Validate the extend register */
982 intel_me_extend_valid(dev);
983}
984
985 memset(&mbp_data, 0, sizeof(mbp_data));
986
987 /*
988 * According to the ME9 BWG, BIOS is required to fetch MBP data in
989 * all boot flows except S3 Resume.
990 */
991
992 /* Prepare MEI MMIO interface */
993 if (intel_mei_setup(dev) < 0)
994 return;
995
996 /* Read ME MBP data */
997 mbp_ret = intel_me_read_mbp(&mbp_data, dev);
998 if (mbp_ret < 0)
999 return;
1000 intel_me_print_mbp(&mbp_data);
1001
1002 /* Set clock enables according to devicetree */
Kyösti Mälkki8950cfb2019-07-13 22:16:25 +03001003 if (config->icc_clock_disable)
Duncan Lauriec99681f2014-12-10 08:11:09 -08001004 me_icc_set_clock_enables(config->icc_clock_disable);
1005
1006 /* Make sure ME is in a mode that expects EOP */
1007 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
1008
1009 /* Abort and leave device alone if not normal mode */
1010 if (hfs.fpt_bad ||
1011 hfs.working_state != ME_HFS_CWS_NORMAL ||
1012 hfs.operation_mode != ME_HFS_MODE_NORMAL)
1013 return;
1014
1015 if (mbp_ret) {
1016 /*
1017 * MBP Cleared wait is skipped,
1018 * Do not expect ACK and reset when complete.
1019 */
1020
1021 /* Send HMRFPO Lock command, no response */
1022 mkhi_hmrfpo_lock_noack();
1023
1024 /* Send END OF POST command, no response */
1025 mkhi_end_of_post_noack();
1026
1027 /* Assert reset and interrupt */
1028 read_host_csr(&csr);
1029 csr.interrupt_generate = 1;
1030 csr.reset = 1;
1031 write_host_csr(&csr);
1032 } else {
1033 /*
1034 * MBP Cleared wait was not skipped
1035 */
1036
1037 /* Send HMRFPO LOCK command */
1038 mkhi_hmrfpo_lock();
1039
1040 /* Send EOP command so ME stops accepting other commands */
1041 mkhi_end_of_post();
1042 }
1043}
1044
Elyes HAOUAS040aff22018-05-27 16:30:36 +02001045static void intel_me_enable(struct device *dev)
Duncan Lauriec99681f2014-12-10 08:11:09 -08001046{
Duncan Lauriec99681f2014-12-10 08:11:09 -08001047 /* Avoid talking to the device in S3 path */
Kyösti Mälkki1ec23c92015-05-29 06:18:18 +03001048 if (acpi_is_wakeup_s3()) {
Duncan Lauriec99681f2014-12-10 08:11:09 -08001049 dev->enabled = 0;
1050 pch_disable_devfn(dev);
1051 }
Duncan Lauriec99681f2014-12-10 08:11:09 -08001052}
1053
1054static struct device_operations device_ops = {
1055 .read_resources = &pci_dev_read_resources,
1056 .set_resources = &pci_dev_set_resources,
1057 .enable_resources = &pci_dev_enable_resources,
1058 .enable = &intel_me_enable,
1059 .init = &intel_me_init,
1060 .final = &intel_me_finalize,
1061 .ops_pci = &broadwell_pci_ops,
1062};
1063
1064static const unsigned short pci_device_ids[] = {
1065 0x9c3a, /* Low Power */
1066 0x9cba, /* WildcatPoint */
1067 0
1068};
1069
1070static const struct pci_driver intel_me __pci_driver = {
1071 .ops = &device_ops,
1072 .vendor = PCI_VENDOR_ID_INTEL,
1073 .devices = pci_device_ids,
1074};