blob: c954e9769c74226aadbb4a24437f6d72bc52095a [file] [log] [blame]
Julius Wernerb624ae02017-06-09 15:17:15 -07001/*
Govindraj Rajaeee28e72023-08-01 15:52:40 -05002 * Copyright (c) 2017-2020, Arm Limited and Contributors. All rights reserved.
Julius Wernerb624ae02017-06-09 15:17:15 -07003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Julius Werner385176d2017-06-13 15:53:45 -07007#include <assert.h>
Julius Wernerb624ae02017-06-09 15:17:15 -07008#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
10#include <drivers/coreboot/cbmem_console.h>
11#include <common/debug.h>
12#include <lib/coreboot.h>
13#include <lib/mmio.h>
14#include <lib/xlat_tables/xlat_tables_v2.h>
Julius Wernerb624ae02017-06-09 15:17:15 -070015
16/*
17 * Structures describing coreboot's in-memory descriptor tables. See
18 * <coreboot>/src/commonlib/include/commonlib/coreboot_tables.h for
19 * canonical implementation.
20 */
21
22typedef struct {
23 char signature[4];
24 uint32_t header_bytes;
25 uint32_t header_checksum;
26 uint32_t table_bytes;
27 uint32_t table_checksum;
28 uint32_t table_entries;
29} cb_header_t;
30
31typedef enum {
Julius Werner8acc4932020-03-26 18:06:21 -070032 CB_TAG_MEMORY = 0x1,
Julius Wernerb624ae02017-06-09 15:17:15 -070033 CB_TAG_SERIAL = 0xf,
Julius Werner385176d2017-06-13 15:53:45 -070034 CB_TAG_CBMEM_CONSOLE = 0x17,
Julius Wernerb624ae02017-06-09 15:17:15 -070035} cb_tag_t;
36
37typedef struct {
38 uint32_t tag;
39 uint32_t size;
40 union {
Julius Werner8acc4932020-03-26 18:06:21 -070041 coreboot_memrange_t memranges[COREBOOT_MAX_MEMRANGES];
Julius Wernerb624ae02017-06-09 15:17:15 -070042 coreboot_serial_t serial;
Julius Werner385176d2017-06-13 15:53:45 -070043 uint64_t uint64;
Julius Wernerb624ae02017-06-09 15:17:15 -070044 };
45} cb_entry_t;
46
Julius Werner8acc4932020-03-26 18:06:21 -070047coreboot_memrange_t coreboot_memranges[COREBOOT_MAX_MEMRANGES];
Julius Wernerb624ae02017-06-09 15:17:15 -070048coreboot_serial_t coreboot_serial;
Jeffrey Kardatzke45521892023-02-09 10:45:35 -080049uint64_t coreboot_table_addr;
50uint32_t coreboot_table_size;
Julius Wernerb624ae02017-06-09 15:17:15 -070051
52/*
53 * The coreboot table is parsed before the MMU is enabled (i.e. with strongly
54 * ordered memory), so we cannot make unaligned accesses. The table entries
55 * immediately follow one another without padding, so nothing after the header
56 * is guaranteed to be naturally aligned. Therefore, we need to define safety
57 * functions that can read unaligned integers.
58 */
59static uint32_t read_le32(uint32_t *p)
60{
61 uintptr_t addr = (uintptr_t)p;
62 return mmio_read_8(addr) |
63 mmio_read_8(addr + 1) << 8 |
64 mmio_read_8(addr + 2) << 16 |
65 mmio_read_8(addr + 3) << 24;
66}
Julius Werner385176d2017-06-13 15:53:45 -070067static uint64_t read_le64(uint64_t *p)
68{
69 return read_le32((void *)p) | (uint64_t)read_le32((void *)p + 4) << 32;
70}
71
72static void expand_and_mmap(uintptr_t baseaddr, size_t size)
73{
74 uintptr_t pageaddr = round_down(baseaddr, PAGE_SIZE);
75 size_t expanded = round_up(baseaddr - pageaddr + size, PAGE_SIZE);
76 mmap_add_region(pageaddr, pageaddr, expanded,
77 MT_MEMORY | MT_RW | MT_NS | MT_EXECUTE_NEVER);
78}
79
80static void setup_cbmem_console(uintptr_t baseaddr)
81{
82 static console_cbmc_t console;
Andre Przywarac2f8d6d2020-01-25 01:07:19 +000083 assert(!console.console.base); /* should only have one CBMEM console */
Julius Werner385176d2017-06-13 15:53:45 -070084
85 /* CBMEM console structure stores its size in first header field. */
86 uint32_t size = *(uint32_t *)baseaddr;
87 expand_and_mmap(baseaddr, size);
88 console_cbmc_register(baseaddr, &console);
89 console_set_scope(&console.console, CONSOLE_FLAG_BOOT |
90 CONSOLE_FLAG_RUNTIME |
91 CONSOLE_FLAG_CRASH);
92}
Julius Wernerb624ae02017-06-09 15:17:15 -070093
Saurabh Gorecha86f7fb72020-10-15 00:05:36 +053094coreboot_memory_t coreboot_get_memory_type(uintptr_t start, size_t size)
Julius Werner8acc4932020-03-26 18:06:21 -070095{
96 int i;
97
98 for (i = 0; i < COREBOOT_MAX_MEMRANGES; i++) {
99 coreboot_memrange_t *range = &coreboot_memranges[i];
100
101 if (range->type == CB_MEM_NONE)
102 break; /* end of table reached */
Saurabh Gorecha86f7fb72020-10-15 00:05:36 +0530103 if ((start >= range->start) &&
104 (start - range->start < range->size) &&
105 (size <= range->size - (start - range->start))) {
Julius Werner8acc4932020-03-26 18:06:21 -0700106 return range->type;
Saurabh Gorecha86f7fb72020-10-15 00:05:36 +0530107 }
Julius Werner8acc4932020-03-26 18:06:21 -0700108 }
109
110 return CB_MEM_NONE;
111}
112
Jeffrey Kardatzke45521892023-02-09 10:45:35 -0800113void coreboot_get_table_location(uint64_t *address, uint32_t *size)
114{
115 *address = coreboot_table_addr;
116 *size = coreboot_table_size;
117}
118
Julius Wernerb624ae02017-06-09 15:17:15 -0700119void coreboot_table_setup(void *base)
120{
121 cb_header_t *header = base;
122 void *ptr;
123 int i;
124
125 if (strncmp(header->signature, "LBIO", 4)) {
126 ERROR("coreboot table signature corrupt!\n");
127 return;
128 }
Jeffrey Kardatzke45521892023-02-09 10:45:35 -0800129 coreboot_table_addr = (uint64_t) base;
130 coreboot_table_size = header->header_bytes + header->table_bytes;
Julius Wernerb624ae02017-06-09 15:17:15 -0700131
132 ptr = base + header->header_bytes;
133 for (i = 0; i < header->table_entries; i++) {
Julius Werner8acc4932020-03-26 18:06:21 -0700134 size_t size;
Julius Wernerb624ae02017-06-09 15:17:15 -0700135 cb_entry_t *entry = ptr;
136
137 if (ptr - base >= header->header_bytes + header->table_bytes) {
138 ERROR("coreboot table exceeds its bounds!\n");
139 break;
140 }
141
142 switch (read_le32(&entry->tag)) {
Julius Werner8acc4932020-03-26 18:06:21 -0700143 case CB_TAG_MEMORY:
144 size = read_le32(&entry->size) -
145 offsetof(cb_entry_t, memranges);
146 if (size > sizeof(coreboot_memranges)) {
147 ERROR("Need to truncate coreboot memranges!\n");
148 size = sizeof(coreboot_memranges);
149 }
150 memcpy(&coreboot_memranges, &entry->memranges, size);
151 break;
Julius Wernerb624ae02017-06-09 15:17:15 -0700152 case CB_TAG_SERIAL:
153 memcpy(&coreboot_serial, &entry->serial,
154 sizeof(coreboot_serial));
155 break;
Julius Werner385176d2017-06-13 15:53:45 -0700156 case CB_TAG_CBMEM_CONSOLE:
157 setup_cbmem_console(read_le64(&entry->uint64));
158 break;
Julius Wernerb624ae02017-06-09 15:17:15 -0700159 default:
160 /* There are many tags TF doesn't need to care about. */
161 break;
162 }
163
164 ptr += read_le32(&entry->size);
165 }
166}