Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

firmware: google: memconsole: Adapt to new coreboot ring buffer format

The upstream coreboot implementation of memconsole was enhanced from a
single-boot console to a persistent ring buffer
(https://review.coreboot.org/#/c/18301). This patch changes the kernel
memconsole driver to be able to read the new format in all cases.

Signed-off-by: Julius Werner <jwerner@chromium.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Julius Werner and committed by
Greg Kroah-Hartman
a5061d02 7918cfc4

+39 -8
+39 -8
drivers/firmware/google/memconsole-coreboot.c
··· 26 26 27 27 /* CBMEM firmware console log descriptor. */ 28 28 struct cbmem_cons { 29 - u32 buffer_size; 30 - u32 buffer_cursor; 31 - u8 buffer_body[0]; 29 + u32 size; 30 + u32 cursor; 31 + u8 body[0]; 32 32 } __packed; 33 + 34 + #define CURSOR_MASK ((1 << 28) - 1) 35 + #define OVERFLOW (1 << 31) 33 36 34 37 static struct cbmem_cons __iomem *cbmem_console; 35 38 39 + /* 40 + * The cbmem_console structure is read again on every access because it may 41 + * change at any time if runtime firmware logs new messages. This may rarely 42 + * lead to race conditions where the firmware overwrites the beginning of the 43 + * ring buffer with more lines after we have already read |cursor|. It should be 44 + * rare and harmless enough that we don't spend extra effort working around it. 45 + */ 36 46 static ssize_t memconsole_coreboot_read(char *buf, loff_t pos, size_t count) 37 47 { 38 - return memory_read_from_buffer(buf, count, &pos, 39 - cbmem_console->buffer_body, 40 - min(cbmem_console->buffer_cursor, 41 - cbmem_console->buffer_size)); 48 + u32 cursor = cbmem_console->cursor & CURSOR_MASK; 49 + u32 flags = cbmem_console->cursor & ~CURSOR_MASK; 50 + u32 size = cbmem_console->size; 51 + struct seg { /* describes ring buffer segments in logical order */ 52 + u32 phys; /* physical offset from start of mem buffer */ 53 + u32 len; /* length of segment */ 54 + } seg[2] = { {0}, {0} }; 55 + size_t done = 0; 56 + int i; 57 + 58 + if (flags & OVERFLOW) { 59 + if (cursor > size) /* Shouldn't really happen, but... */ 60 + cursor = 0; 61 + seg[0] = (struct seg){.phys = cursor, .len = size - cursor}; 62 + seg[1] = (struct seg){.phys = 0, .len = cursor}; 63 + } else { 64 + seg[0] = (struct seg){.phys = 0, .len = min(cursor, size)}; 65 + } 66 + 67 + for (i = 0; i < ARRAY_SIZE(seg) && count > done; i++) { 68 + done += memory_read_from_buffer(buf + done, count - done, &pos, 69 + cbmem_console->body + seg[i].phys, seg[i].len); 70 + pos -= seg[i].len; 71 + } 72 + return done; 42 73 } 43 74 44 75 static int memconsole_coreboot_init(phys_addr_t physaddr) ··· 82 51 return -ENOMEM; 83 52 84 53 cbmem_console = memremap(physaddr, 85 - tmp_cbmc->buffer_size + sizeof(*cbmem_console), 54 + tmp_cbmc->size + sizeof(*cbmem_console), 86 55 MEMREMAP_WB); 87 56 memunmap(tmp_cbmc); 88 57