1d9523678SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d384d6f4SThierry Escande /*
3d384d6f4SThierry Escande * memconsole-coreboot.c
4d384d6f4SThierry Escande *
5d384d6f4SThierry Escande * Memory based BIOS console accessed through coreboot table.
6d384d6f4SThierry Escande *
7d384d6f4SThierry Escande * Copyright 2017 Google Inc.
8d384d6f4SThierry Escande */
9d384d6f4SThierry Escande
10294b2a90SSamuel Holland #include <linux/device.h>
11381e9760SStephen Boyd #include <linux/io.h>
12d384d6f4SThierry Escande #include <linux/kernel.h>
13d384d6f4SThierry Escande #include <linux/module.h>
14d384d6f4SThierry Escande
15d384d6f4SThierry Escande #include "memconsole.h"
16d384d6f4SThierry Escande #include "coreboot_table.h"
17d384d6f4SThierry Escande
18d384d6f4SThierry Escande #define CB_TAG_CBMEM_CONSOLE 0x17
19d384d6f4SThierry Escande
20d384d6f4SThierry Escande /* CBMEM firmware console log descriptor. */
21d384d6f4SThierry Escande struct cbmem_cons {
2240fbb238SJulius Werner u32 size_dont_access_after_boot;
23a5061d02SJulius Werner u32 cursor;
24ea9ee997SGustavo A. R. Silva u8 body[];
25d384d6f4SThierry Escande } __packed;
26d384d6f4SThierry Escande
27a5061d02SJulius Werner #define CURSOR_MASK ((1 << 28) - 1)
28a5061d02SJulius Werner #define OVERFLOW (1 << 31)
29a5061d02SJulius Werner
30e07f1009SStephen Boyd static struct cbmem_cons *cbmem_console;
3140fbb238SJulius Werner static u32 cbmem_console_size;
32d384d6f4SThierry Escande
33a5061d02SJulius Werner /*
34a5061d02SJulius Werner * The cbmem_console structure is read again on every access because it may
35a5061d02SJulius Werner * change at any time if runtime firmware logs new messages. This may rarely
36a5061d02SJulius Werner * lead to race conditions where the firmware overwrites the beginning of the
37a5061d02SJulius Werner * ring buffer with more lines after we have already read |cursor|. It should be
38a5061d02SJulius Werner * rare and harmless enough that we don't spend extra effort working around it.
39a5061d02SJulius Werner */
memconsole_coreboot_read(char * buf,loff_t pos,size_t count)407918cfc4SJulius Werner static ssize_t memconsole_coreboot_read(char *buf, loff_t pos, size_t count)
417918cfc4SJulius Werner {
42a5061d02SJulius Werner u32 cursor = cbmem_console->cursor & CURSOR_MASK;
43a5061d02SJulius Werner u32 flags = cbmem_console->cursor & ~CURSOR_MASK;
4440fbb238SJulius Werner u32 size = cbmem_console_size;
45a5061d02SJulius Werner struct seg { /* describes ring buffer segments in logical order */
46a5061d02SJulius Werner u32 phys; /* physical offset from start of mem buffer */
47a5061d02SJulius Werner u32 len; /* length of segment */
48a5061d02SJulius Werner } seg[2] = { {0}, {0} };
49a5061d02SJulius Werner size_t done = 0;
50a5061d02SJulius Werner int i;
51a5061d02SJulius Werner
52a5061d02SJulius Werner if (flags & OVERFLOW) {
53a5061d02SJulius Werner if (cursor > size) /* Shouldn't really happen, but... */
54a5061d02SJulius Werner cursor = 0;
55a5061d02SJulius Werner seg[0] = (struct seg){.phys = cursor, .len = size - cursor};
56a5061d02SJulius Werner seg[1] = (struct seg){.phys = 0, .len = cursor};
57a5061d02SJulius Werner } else {
58a5061d02SJulius Werner seg[0] = (struct seg){.phys = 0, .len = min(cursor, size)};
59a5061d02SJulius Werner }
60a5061d02SJulius Werner
61a5061d02SJulius Werner for (i = 0; i < ARRAY_SIZE(seg) && count > done; i++) {
62a5061d02SJulius Werner done += memory_read_from_buffer(buf + done, count - done, &pos,
63a5061d02SJulius Werner cbmem_console->body + seg[i].phys, seg[i].len);
64a5061d02SJulius Werner pos -= seg[i].len;
65a5061d02SJulius Werner }
66a5061d02SJulius Werner return done;
677918cfc4SJulius Werner }
687918cfc4SJulius Werner
memconsole_probe(struct coreboot_device * dev)69294b2a90SSamuel Holland static int memconsole_probe(struct coreboot_device *dev)
70d384d6f4SThierry Escande {
71e07f1009SStephen Boyd struct cbmem_cons *tmp_cbmc;
72d384d6f4SThierry Escande
73294b2a90SSamuel Holland tmp_cbmc = memremap(dev->cbmem_ref.cbmem_addr,
74294b2a90SSamuel Holland sizeof(*tmp_cbmc), MEMREMAP_WB);
75d384d6f4SThierry Escande
76d384d6f4SThierry Escande if (!tmp_cbmc)
77d384d6f4SThierry Escande return -ENOMEM;
78d384d6f4SThierry Escande
7940fbb238SJulius Werner /* Read size only once to prevent overrun attack through /dev/mem. */
8040fbb238SJulius Werner cbmem_console_size = tmp_cbmc->size_dont_access_after_boot;
81b0503584SStephen Boyd cbmem_console = devm_memremap(&dev->dev, dev->cbmem_ref.cbmem_addr,
8240fbb238SJulius Werner cbmem_console_size + sizeof(*cbmem_console),
83d384d6f4SThierry Escande MEMREMAP_WB);
84d384d6f4SThierry Escande memunmap(tmp_cbmc);
85d384d6f4SThierry Escande
86b0503584SStephen Boyd if (IS_ERR(cbmem_console))
87b0503584SStephen Boyd return PTR_ERR(cbmem_console);
88d384d6f4SThierry Escande
897918cfc4SJulius Werner memconsole_setup(memconsole_coreboot_read);
90d384d6f4SThierry Escande
91d384d6f4SThierry Escande return memconsole_sysfs_init();
92d384d6f4SThierry Escande }
93d384d6f4SThierry Escande
memconsole_remove(struct coreboot_device * dev)94*5f680532SUwe Kleine-König static void memconsole_remove(struct coreboot_device *dev)
95d384d6f4SThierry Escande {
96d384d6f4SThierry Escande memconsole_exit();
97d384d6f4SThierry Escande }
98d384d6f4SThierry Escande
99294b2a90SSamuel Holland static struct coreboot_driver memconsole_driver = {
100d384d6f4SThierry Escande .probe = memconsole_probe,
101d384d6f4SThierry Escande .remove = memconsole_remove,
102294b2a90SSamuel Holland .drv = {
103d384d6f4SThierry Escande .name = "memconsole",
104d384d6f4SThierry Escande },
105294b2a90SSamuel Holland .tag = CB_TAG_CBMEM_CONSOLE,
106d384d6f4SThierry Escande };
10735463503SStephen Boyd module_coreboot_driver(memconsole_driver);
108d384d6f4SThierry Escande
109d384d6f4SThierry Escande MODULE_AUTHOR("Google, Inc.");
110d384d6f4SThierry Escande MODULE_LICENSE("GPL");
111