blob: 1c56e5ebd02060ecacdb6b29ffccbf799cfc3695 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer8e073822012-04-04 00:07:22 +02002
3/*
4 * This is a ramstage driver for the Intel Management Engine found in the
5 * 6-series chipset. It handles the required boot-time messages over the
6 * MMIO-based Management Engine Interface to tell the ME that the BIOS is
7 * finished with POST. Additional messages are defined for debug but are
8 * not used unless the console loglevel is high enough.
9 */
10
Furquan Shaikh76cedd22020-05-02 10:24:23 -070011#include <acpi/acpi.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +020012#include <device/mmio.h>
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020013#include <device/device.h>
14#include <device/pci.h>
Angel Pons7f32df32020-06-02 13:36:57 +020015#include <device/pci_ops.h>
16#include <console/console.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020017#include <device/pci_ids.h>
18#include <device/pci_def.h>
19#include <string.h>
20#include <delay.h>
Duncan Lauriec1c94352012-07-13 10:11:54 -070021#include <elog.h>
Stefan Reinauer8e073822012-04-04 00:07:22 +020022
Stefan Reinauer8e073822012-04-04 00:07:22 +020023#include "me.h"
24#include "pch.h"
25
Julius Wernercd49cce2019-03-05 16:53:33 -080026#if CONFIG(CHROMEOS)
Stefan Reinauer8e073822012-04-04 00:07:22 +020027#include <vendorcode/google/chromeos/gnvs.h>
28#endif
29
Stefan Reinauer8e073822012-04-04 00:07:22 +020030/* Path that the BIOS should take based on ME state */
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020031static const char *me_bios_path_values[] __unused = {
Stefan Reinauer8e073822012-04-04 00:07:22 +020032 [ME_NORMAL_BIOS_PATH] = "Normal",
33 [ME_S3WAKE_BIOS_PATH] = "S3 Wake",
34 [ME_ERROR_BIOS_PATH] = "Error",
35 [ME_RECOVERY_BIOS_PATH] = "Recovery",
36 [ME_DISABLE_BIOS_PATH] = "Disable",
37 [ME_FIRMWARE_UPDATE_BIOS_PATH] = "Firmware Update",
38};
Stefan Reinauer8e073822012-04-04 00:07:22 +020039
40/* MMIO base address for MEI interface */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080041static u32 *mei_base_address;
Stefan Reinauer8e073822012-04-04 00:07:22 +020042
Stefan Reinauer8e073822012-04-04 00:07:22 +020043static void mei_dump(void *ptr, int dword, int offset, const char *type)
44{
45 struct mei_csr *csr;
46
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +020047 if (!CONFIG(DEBUG_INTEL_ME))
48 return;
49
Stefan Reinauer8e073822012-04-04 00:07:22 +020050 printk(BIOS_SPEW, "%-9s[%02x] : ", type, offset);
51
52 switch (offset) {
53 case MEI_H_CSR:
54 case MEI_ME_CSR_HA:
55 csr = ptr;
56 if (!csr) {
57 printk(BIOS_SPEW, "ERROR: 0x%08x\n", dword);
58 break;
59 }
60 printk(BIOS_SPEW, "cbd=%u cbrp=%02u cbwp=%02u ready=%u "
61 "reset=%u ig=%u is=%u ie=%u\n", csr->buffer_depth,
62 csr->buffer_read_ptr, csr->buffer_write_ptr,
63 csr->ready, csr->reset, csr->interrupt_generate,
64 csr->interrupt_status, csr->interrupt_enable);
65 break;
66 case MEI_ME_CB_RW:
67 case MEI_H_CB_WW:
68 printk(BIOS_SPEW, "CB: 0x%08x\n", dword);
69 break;
70 default:
71 printk(BIOS_SPEW, "0x%08x\n", offset);
72 break;
73 }
74}
Stefan Reinauer8e073822012-04-04 00:07:22 +020075
76/*
77 * ME/MEI access helpers using memcpy to avoid aliasing.
78 */
79
80static inline void mei_read_dword_ptr(void *ptr, int offset)
81{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080082 u32 dword = read32(mei_base_address + (offset/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +020083 memcpy(ptr, &dword, sizeof(dword));
84 mei_dump(ptr, dword, offset, "READ");
85}
86
87static inline void mei_write_dword_ptr(void *ptr, int offset)
88{
89 u32 dword = 0;
90 memcpy(&dword, ptr, sizeof(dword));
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080091 write32(mei_base_address + (offset/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +020092 mei_dump(ptr, dword, offset, "WRITE");
93}
94
Kyösti Mälkki21d6a272019-11-05 18:50:38 +020095#ifndef __SIMPLE_DEVICE__
Elyes HAOUASdc035282018-09-18 13:28:49 +020096static inline void pci_read_dword_ptr(struct device *dev, void *ptr, int offset)
Stefan Reinauer8e073822012-04-04 00:07:22 +020097{
98 u32 dword = pci_read_config32(dev, offset);
99 memcpy(ptr, &dword, sizeof(dword));
100 mei_dump(ptr, dword, offset, "PCI READ");
101}
102#endif
103
104static inline void read_host_csr(struct mei_csr *csr)
105{
106 mei_read_dword_ptr(csr, MEI_H_CSR);
107}
108
109static inline void write_host_csr(struct mei_csr *csr)
110{
111 mei_write_dword_ptr(csr, MEI_H_CSR);
112}
113
114static inline void read_me_csr(struct mei_csr *csr)
115{
116 mei_read_dword_ptr(csr, MEI_ME_CSR_HA);
117}
118
119static inline void write_cb(u32 dword)
120{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800121 write32(mei_base_address + (MEI_H_CB_WW/sizeof(u32)), dword);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200122 mei_dump(NULL, dword, MEI_H_CB_WW, "WRITE");
123}
124
125static inline u32 read_cb(void)
126{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800127 u32 dword = read32(mei_base_address + (MEI_ME_CB_RW/sizeof(u32)));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200128 mei_dump(NULL, dword, MEI_ME_CB_RW, "READ");
129 return dword;
130}
131
132/* Wait for ME ready bit to be asserted */
133static int mei_wait_for_me_ready(void)
134{
135 struct mei_csr me;
Martin Rothff744bf2019-10-23 21:46:03 -0600136 unsigned int try = ME_RETRY;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200137
138 while (try--) {
139 read_me_csr(&me);
140 if (me.ready)
141 return 0;
142 udelay(ME_DELAY);
143 }
144
145 printk(BIOS_ERR, "ME: failed to become ready\n");
146 return -1;
147}
148
149static void mei_reset(void)
150{
151 struct mei_csr host;
152
153 if (mei_wait_for_me_ready() < 0)
154 return;
155
156 /* Reset host and ME circular buffers for next message */
157 read_host_csr(&host);
158 host.reset = 1;
159 host.interrupt_generate = 1;
160 write_host_csr(&host);
161
162 if (mei_wait_for_me_ready() < 0)
163 return;
164
165 /* Re-init and indicate host is ready */
166 read_host_csr(&host);
167 host.interrupt_generate = 1;
168 host.ready = 1;
169 host.reset = 0;
170 write_host_csr(&host);
171}
172
173static int mei_send_msg(struct mei_header *mei, struct mkhi_header *mkhi,
174 void *req_data)
175{
176 struct mei_csr host;
Martin Rothff744bf2019-10-23 21:46:03 -0600177 unsigned int ndata, n;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200178 u32 *data;
179
180 /* Number of dwords to write, ignoring MKHI */
181 ndata = mei->length >> 2;
182
183 /* Pad non-dword aligned request message length */
184 if (mei->length & 3)
185 ndata++;
186 if (!ndata) {
187 printk(BIOS_DEBUG, "ME: request does not include MKHI\n");
188 return -1;
189 }
190 ndata++; /* Add MEI header */
191
192 /*
193 * Make sure there is still room left in the circular buffer.
194 * Reset the buffer pointers if the requested message will not fit.
195 */
196 read_host_csr(&host);
197 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
198 printk(BIOS_ERR, "ME: circular buffer full, resetting...\n");
199 mei_reset();
200 read_host_csr(&host);
201 }
202
203 /*
204 * This implementation does not handle splitting large messages
205 * across multiple transactions. Ensure the requested length
206 * will fit in the available circular buffer depth.
207 */
208 if ((host.buffer_depth - host.buffer_write_ptr) < ndata) {
209 printk(BIOS_ERR, "ME: message (%u) too large for buffer (%u)\n",
210 ndata + 2, host.buffer_depth);
211 return -1;
212 }
213
214 /* Write MEI header */
215 mei_write_dword_ptr(mei, MEI_H_CB_WW);
216 ndata--;
217
218 /* Write MKHI header */
219 mei_write_dword_ptr(mkhi, MEI_H_CB_WW);
220 ndata--;
221
222 /* Write message data */
223 data = req_data;
224 for (n = 0; n < ndata; ++n)
225 write_cb(*data++);
226
227 /* Generate interrupt to the ME */
228 read_host_csr(&host);
229 host.interrupt_generate = 1;
230 write_host_csr(&host);
231
232 /* Make sure ME is ready after sending request data */
233 return mei_wait_for_me_ready();
234}
235
Angel Pons0623b012020-06-02 13:52:26 +0200236static int mei_recv_msg(struct mkhi_header *mkhi, void *rsp_data, int rsp_bytes)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200237{
238 struct mei_header mei_rsp;
239 struct mkhi_header mkhi_rsp;
240 struct mei_csr me, host;
Martin Rothff744bf2019-10-23 21:46:03 -0600241 unsigned int ndata, n;
242 unsigned int expected;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200243 u32 *data;
244
245 /* Total number of dwords to read from circular buffer */
246 expected = (rsp_bytes + sizeof(mei_rsp) + sizeof(mkhi_rsp)) >> 2;
247 if (rsp_bytes & 3)
248 expected++;
249
250 /*
251 * The interrupt status bit does not appear to indicate that the
252 * message has actually been received. Instead we wait until the
253 * expected number of dwords are present in the circular buffer.
254 */
255 for (n = ME_RETRY; n; --n) {
256 read_me_csr(&me);
257 if ((me.buffer_write_ptr - me.buffer_read_ptr) >= expected)
258 break;
259 udelay(ME_DELAY);
260 }
261 if (!n) {
262 printk(BIOS_ERR, "ME: timeout waiting for data: expected "
263 "%u, available %u\n", expected,
264 me.buffer_write_ptr - me.buffer_read_ptr);
265 return -1;
266 }
267
268 /* Read and verify MEI response header from the ME */
269 mei_read_dword_ptr(&mei_rsp, MEI_ME_CB_RW);
270 if (!mei_rsp.is_complete) {
271 printk(BIOS_ERR, "ME: response is not complete\n");
272 return -1;
273 }
274
275 /* Handle non-dword responses and expect at least MKHI header */
276 ndata = mei_rsp.length >> 2;
277 if (mei_rsp.length & 3)
278 ndata++;
279 if (ndata != (expected - 1)) {
Angel Pons0623b012020-06-02 13:52:26 +0200280 printk(BIOS_ERR, "ME: response is missing data %d != %d\n",
281 ndata, (expected - 1));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200282 return -1;
283 }
284
285 /* Read and verify MKHI response header from the ME */
286 mei_read_dword_ptr(&mkhi_rsp, MEI_ME_CB_RW);
287 if (!mkhi_rsp.is_response ||
288 mkhi->group_id != mkhi_rsp.group_id ||
289 mkhi->command != mkhi_rsp.command) {
290 printk(BIOS_ERR, "ME: invalid response, group %u ?= %u, "
291 "command %u ?= %u, is_response %u\n", mkhi->group_id,
292 mkhi_rsp.group_id, mkhi->command, mkhi_rsp.command,
293 mkhi_rsp.is_response);
294 return -1;
295 }
296 ndata--; /* MKHI header has been read */
297
298 /* Make sure caller passed a buffer with enough space */
299 if (ndata != (rsp_bytes >> 2)) {
300 printk(BIOS_ERR, "ME: not enough room in response buffer: "
301 "%u != %u\n", ndata, rsp_bytes >> 2);
302 return -1;
303 }
304
305 /* Read response data from the circular buffer */
306 data = rsp_data;
307 for (n = 0; n < ndata; ++n)
308 *data++ = read_cb();
309
310 /* Tell the ME that we have consumed the response */
311 read_host_csr(&host);
312 host.interrupt_status = 1;
313 host.interrupt_generate = 1;
314 write_host_csr(&host);
315
316 return mei_wait_for_me_ready();
317}
318
319static inline int mei_sendrecv(struct mei_header *mei, struct mkhi_header *mkhi,
320 void *req_data, void *rsp_data, int rsp_bytes)
321{
322 if (mei_send_msg(mei, mkhi, req_data) < 0)
323 return -1;
Angel Pons0623b012020-06-02 13:52:26 +0200324 if (mei_recv_msg(mkhi, rsp_data, rsp_bytes) < 0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200325 return -1;
326 return 0;
327}
328
329/* Send END OF POST message to the ME */
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200330static int __unused mkhi_end_of_post(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200331{
332 struct mkhi_header mkhi = {
333 .group_id = MKHI_GROUP_ID_GEN,
334 .command = MKHI_END_OF_POST,
335 };
336 struct mei_header mei = {
337 .is_complete = 1,
338 .host_address = MEI_HOST_ADDRESS,
339 .client_address = MEI_ADDRESS_MKHI,
340 .length = sizeof(mkhi),
341 };
342
343 /* Send request and wait for response */
344 if (mei_sendrecv(&mei, &mkhi, NULL, NULL, 0) < 0) {
345 printk(BIOS_ERR, "ME: END OF POST message failed\n");
346 return -1;
347 }
348
349 printk(BIOS_INFO, "ME: END OF POST message successful\n");
350 return 0;
351}
352
Stefan Reinauer8e073822012-04-04 00:07:22 +0200353/* Get ME firmware version */
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200354static int __unused mkhi_get_fw_version(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200355{
356 struct me_fw_version version;
357 struct mkhi_header mkhi = {
358 .group_id = MKHI_GROUP_ID_GEN,
359 .command = MKHI_GET_FW_VERSION,
360 };
361 struct mei_header mei = {
362 .is_complete = 1,
363 .host_address = MEI_HOST_ADDRESS,
364 .client_address = MEI_ADDRESS_MKHI,
365 .length = sizeof(mkhi),
366 };
367
368 /* Send request and wait for response */
369 if (mei_sendrecv(&mei, &mkhi, NULL, &version, sizeof(version)) < 0) {
370 printk(BIOS_ERR, "ME: GET FW VERSION message failed\n");
371 return -1;
372 }
373
374 printk(BIOS_INFO, "ME: Firmware Version %u.%u.%u.%u (code) "
375 "%u.%u.%u.%u (recovery)\n",
376 version.code_major, version.code_minor,
377 version.code_build_number, version.code_hot_fix,
378 version.recovery_major, version.recovery_minor,
379 version.recovery_build_number, version.recovery_hot_fix);
380
381 return 0;
382}
383
384static inline void print_cap(const char *name, int state)
385{
386 printk(BIOS_DEBUG, "ME Capability: %-30s : %sabled\n",
387 name, state ? "en" : "dis");
388}
389
390/* Get ME Firmware Capabilities */
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200391static int __unused mkhi_get_fwcaps(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200392{
393 u32 rule_id = 0;
394 struct me_fwcaps cap;
395 struct mkhi_header mkhi = {
396 .group_id = MKHI_GROUP_ID_FWCAPS,
397 .command = MKHI_FWCAPS_GET_RULE,
398 };
399 struct mei_header mei = {
400 .is_complete = 1,
401 .host_address = MEI_HOST_ADDRESS,
402 .client_address = MEI_ADDRESS_MKHI,
403 .length = sizeof(mkhi) + sizeof(rule_id),
404 };
405
406 /* Send request and wait for response */
407 if (mei_sendrecv(&mei, &mkhi, &rule_id, &cap, sizeof(cap)) < 0) {
408 printk(BIOS_ERR, "ME: GET FWCAPS message failed\n");
409 return -1;
410 }
411
412 print_cap("Full Network manageability", cap.caps_sku.full_net);
413 print_cap("Regular Network manageability", cap.caps_sku.std_net);
414 print_cap("Manageability", cap.caps_sku.manageability);
415 print_cap("Small business technology", cap.caps_sku.small_business);
416 print_cap("Level III manageability", cap.caps_sku.l3manageability);
417 print_cap("IntelR Anti-Theft (AT)", cap.caps_sku.intel_at);
418 print_cap("IntelR Capability Licensing Service (CLS)",
419 cap.caps_sku.intel_cls);
420 print_cap("IntelR Power Sharing Technology (MPC)",
421 cap.caps_sku.intel_mpc);
422 print_cap("ICC Over Clocking", cap.caps_sku.icc_over_clocking);
Elyes HAOUASba28e8d2016-08-31 19:22:16 +0200423 print_cap("Protected Audio Video Path (PAVP)", cap.caps_sku.pavp);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200424 print_cap("IPV6", cap.caps_sku.ipv6);
425 print_cap("KVM Remote Control (KVM)", cap.caps_sku.kvm);
426 print_cap("Outbreak Containment Heuristic (OCH)", cap.caps_sku.och);
427 print_cap("Virtual LAN (VLAN)", cap.caps_sku.vlan);
428 print_cap("TLS", cap.caps_sku.tls);
429 print_cap("Wireless LAN (WLAN)", cap.caps_sku.wlan);
430
431 return 0;
432}
Stefan Reinauer8e073822012-04-04 00:07:22 +0200433
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200434#ifdef __SIMPLE_DEVICE__
435
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700436static void intel_me7_finalize_smm(void)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200437{
438 struct me_hfs hfs;
439 u32 reg32;
440
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800441 mei_base_address = (u32 *)
442 (pci_read_config32(PCH_ME_DEV, PCI_BASE_ADDRESS_0) & ~0xf);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200443
444 /* S3 path will have hidden this device already */
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800445 if (!mei_base_address || mei_base_address == (u32 *)0xfffffff0)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200446 return;
447
448 /* Make sure ME is in a mode that expects EOP */
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300449 reg32 = pci_read_config32(PCH_ME_DEV, PCI_ME_HFS);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200450 memcpy(&hfs, &reg32, sizeof(u32));
451
452 /* Abort and leave device alone if not normal mode */
453 if (hfs.fpt_bad ||
454 hfs.working_state != ME_HFS_CWS_NORMAL ||
455 hfs.operation_mode != ME_HFS_MODE_NORMAL)
456 return;
457
458 /* Try to send EOP command so ME stops accepting other commands */
459 mkhi_end_of_post();
460
461 /* Make sure IO is disabled */
Angel Ponsc803f652020-06-07 22:09:01 +0200462 pci_and_config16(PCH_ME_DEV, PCI_COMMAND,
463 ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO));
Stefan Reinauer8e073822012-04-04 00:07:22 +0200464
465 /* Hide the PCI device */
466 RCBA32_OR(FD2, PCH_DISABLE_MEI1);
467}
468
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700469void intel_me_finalize_smm(void)
470{
Kyösti Mälkkifd98c652013-07-26 08:50:53 +0300471 u32 did = pci_read_config32(PCH_ME_DEV, PCI_VENDOR_ID);
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700472 switch (did) {
Duncan Laurie708f7312012-07-10 15:15:41 -0700473 case 0x1c3a8086:
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700474 intel_me7_finalize_smm();
475 break;
Duncan Laurie708f7312012-07-10 15:15:41 -0700476 case 0x1e3a8086:
Stefan Reinauer998f3a22012-06-11 15:15:46 -0700477 intel_me8_finalize_smm();
478 break;
479 default:
480 printk(BIOS_ERR, "No finalize handler for ME %08x.\n", did);
481 }
482}
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200483
484#else
Stefan Reinauer8e073822012-04-04 00:07:22 +0200485
486/* Determine the path that we should take based on ME status */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200487static me_bios_path intel_me_path(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200488{
489 me_bios_path path = ME_DISABLE_BIOS_PATH;
490 struct me_hfs hfs;
491 struct me_gmes gmes;
492
Stefan Reinauer8e073822012-04-04 00:07:22 +0200493 /* S3 wake skips all MKHI messages */
Kyösti Mälkkic3ed8862014-06-19 19:50:51 +0300494 if (acpi_is_wakeup_s3())
Stefan Reinauer8e073822012-04-04 00:07:22 +0200495 return ME_S3WAKE_BIOS_PATH;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200496
497 pci_read_dword_ptr(dev, &hfs, PCI_ME_HFS);
498 pci_read_dword_ptr(dev, &gmes, PCI_ME_GMES);
499
500 /* Check and dump status */
501 intel_me_status(&hfs, &gmes);
502
Stefan Reinauer8e073822012-04-04 00:07:22 +0200503 /* Check Current Working State */
504 switch (hfs.working_state) {
505 case ME_HFS_CWS_NORMAL:
506 path = ME_NORMAL_BIOS_PATH;
507 break;
508 case ME_HFS_CWS_REC:
509 path = ME_RECOVERY_BIOS_PATH;
510 break;
511 default:
512 path = ME_DISABLE_BIOS_PATH;
513 break;
514 }
515
516 /* Check Current Operation Mode */
517 switch (hfs.operation_mode) {
518 case ME_HFS_MODE_NORMAL:
519 break;
520 case ME_HFS_MODE_DEBUG:
521 case ME_HFS_MODE_DIS:
522 case ME_HFS_MODE_OVER_JMPR:
523 case ME_HFS_MODE_OVER_MEI:
524 default:
525 path = ME_DISABLE_BIOS_PATH;
526 break;
527 }
528
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700529 /* Check for any error code and valid firmware */
530 if (hfs.error_code || hfs.fpt_bad)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200531 path = ME_ERROR_BIOS_PATH;
532
Kyösti Mälkkibe5317f2019-11-06 12:07:21 +0200533 if (CONFIG(ELOG) && path != ME_NORMAL_BIOS_PATH) {
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700534 struct elog_event_data_me_extended data = {
535 .current_working_state = hfs.working_state,
536 .operation_state = hfs.operation_state,
537 .operation_mode = hfs.operation_mode,
538 .error_code = hfs.error_code,
539 .progress_code = gmes.progress_code,
540 .current_pmevent = gmes.current_pmevent,
541 .current_state = gmes.current_state,
542 };
543 elog_add_event_byte(ELOG_TYPE_MANAGEMENT_ENGINE, path);
544 elog_add_event_raw(ELOG_TYPE_MANAGEMENT_ENGINE_EXT,
545 &data, sizeof(data));
546 }
Duncan Laurie5c88c6f2012-09-01 14:00:23 -0700547
Stefan Reinauer8e073822012-04-04 00:07:22 +0200548 return path;
549}
550
551/* Prepare ME for MEI messages */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200552static int intel_mei_setup(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200553{
554 struct resource *res;
555 struct mei_csr host;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200556
557 /* Find the MMIO base for the ME interface */
558 res = find_resource(dev, PCI_BASE_ADDRESS_0);
559 if (!res || res->base == 0 || res->size == 0) {
560 printk(BIOS_DEBUG, "ME: MEI resource not present!\n");
561 return -1;
562 }
Angel Pons7f32df32020-06-02 13:36:57 +0200563 mei_base_address = (u32 *)(uintptr_t)res->base;
Stefan Reinauer8e073822012-04-04 00:07:22 +0200564
565 /* Ensure Memory and Bus Master bits are set */
Elyes HAOUAS729c0692020-04-28 19:50:44 +0200566 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
Stefan Reinauer8e073822012-04-04 00:07:22 +0200567
568 /* Clean up status for next message */
569 read_host_csr(&host);
570 host.interrupt_generate = 1;
571 host.ready = 1;
572 host.reset = 0;
573 write_host_csr(&host);
574
575 return 0;
576}
577
578/* Read the Extend register hash of ME firmware */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200579static int intel_me_extend_valid(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200580{
581 struct me_heres status;
Stefan Reinauer49058c02012-06-11 14:13:09 -0700582 u32 extend[8] = {0};
Stefan Reinauer8e073822012-04-04 00:07:22 +0200583 int i, count = 0;
584
585 pci_read_dword_ptr(dev, &status, PCI_ME_HERES);
586 if (!status.extend_feature_present) {
587 printk(BIOS_ERR, "ME: Extend Feature not present\n");
588 return -1;
589 }
590
591 if (!status.extend_reg_valid) {
592 printk(BIOS_ERR, "ME: Extend Register not valid\n");
593 return -1;
594 }
595
596 switch (status.extend_reg_algorithm) {
597 case PCI_ME_EXT_SHA1:
598 count = 5;
599 printk(BIOS_DEBUG, "ME: Extend SHA-1: ");
600 break;
601 case PCI_ME_EXT_SHA256:
602 count = 8;
603 printk(BIOS_DEBUG, "ME: Extend SHA-256: ");
604 break;
605 default:
606 printk(BIOS_ERR, "ME: Extend Algorithm %d unknown\n",
607 status.extend_reg_algorithm);
608 return -1;
609 }
610
611 for (i = 0; i < count; ++i) {
612 extend[i] = pci_read_config32(dev, PCI_ME_HER(i));
613 printk(BIOS_DEBUG, "%08x", extend[i]);
614 }
615 printk(BIOS_DEBUG, "\n");
616
Julius Wernercd49cce2019-03-05 16:53:33 -0800617#if CONFIG(CHROMEOS)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200618 /* Save hash in NVS for the OS to verify */
619 chromeos_set_me_hash(extend, count);
620#endif
621
622 return 0;
623}
624
625/* Hide the ME virtual PCI devices */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200626static void intel_me_hide(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200627{
628 dev->enabled = 0;
629 pch_enable(dev);
630}
631
632/* Check whether ME is present and do basic init */
Elyes HAOUASdc035282018-09-18 13:28:49 +0200633static void intel_me_init(struct device *dev)
Stefan Reinauer8e073822012-04-04 00:07:22 +0200634{
635 me_bios_path path = intel_me_path(dev);
636
637 /* Do initial setup and determine the BIOS path */
638 printk(BIOS_NOTICE, "ME: BIOS path: %s\n", me_bios_path_values[path]);
639
640 switch (path) {
641 case ME_S3WAKE_BIOS_PATH:
642 intel_me_hide(dev);
643 break;
644
645 case ME_NORMAL_BIOS_PATH:
646 /* Validate the extend register */
647 if (intel_me_extend_valid(dev) < 0)
648 break; /* TODO: force recovery mode */
649
650 /* Prepare MEI MMIO interface */
651 if (intel_mei_setup(dev) < 0)
652 break;
653
Kyösti Mälkkic86fc8e2019-11-06 06:32:27 +0200654 if (CONFIG_DEFAULT_CONSOLE_LOGLEVEL >= BIOS_DEBUG) {
655 /* Print ME firmware version */
656 mkhi_get_fw_version();
657 /* Print ME firmware capabilities */
658 mkhi_get_fwcaps();
659 }
Stefan Reinauer8e073822012-04-04 00:07:22 +0200660
661 /*
662 * Leave the ME unlocked in this path.
663 * It will be locked via SMI command later.
664 */
665 break;
666
667 case ME_ERROR_BIOS_PATH:
668 case ME_RECOVERY_BIOS_PATH:
669 case ME_DISABLE_BIOS_PATH:
670 case ME_FIRMWARE_UPDATE_BIOS_PATH:
Stefan Reinauer8e073822012-04-04 00:07:22 +0200671 break;
672 }
673}
674
Stefan Reinauer8e073822012-04-04 00:07:22 +0200675static struct device_operations device_ops = {
676 .read_resources = pci_dev_read_resources,
677 .set_resources = pci_dev_set_resources,
678 .enable_resources = pci_dev_enable_resources,
679 .init = intel_me_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200680 .ops_pci = &pci_dev_ops_pci,
Stefan Reinauer8e073822012-04-04 00:07:22 +0200681};
682
683static const struct pci_driver intel_me __pci_driver = {
684 .ops = &device_ops,
685 .vendor = PCI_VENDOR_ID_INTEL,
686 .device = 0x1c3a,
687};
688
Kyösti Mälkki21d6a272019-11-05 18:50:38 +0200689#endif /* __SIMPLE_DEVICE__ */