Debian Codegraph @

src/soc/amd/common/block/spi/mmap_boot_rom3.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <boot_device.h>
4 #include <spi_flash.h>
5 #include <stdint.h>
6 #include <amdblocks/lpc.h>
7
8 enum window_type {
9 /* Fixed decode window of max 16MiB size just below 4G boundary */
10 ROM2_DECODE_WINDOW,
11 /* Additional decode window for mapping BIOS region greater than 16MiB */
12 ROM3_DECODE_WINDOW,
13 TOTAL_DECODE_WINDOWS,
14 };
15
16 static struct xlate_region_device real_dev;
17 static struct mem_region_device shadow_devs[TOTAL_DECODE_WINDOWS];
18 static struct xlate_window real_dev_windows[TOTAL_DECODE_WINDOWS];
19
20 static void initialize_window(const size_t win_idx, const enum window_type type,
21 uintptr_t host_base, uintptr_t flash_base, size_t size)
22 {
23 mem_region_device_ro_init(&shadow_devs[win_idx], (void *)host_base, size);
24 xlate_window_init(&real_dev_windows[win_idx], &shadow_devs[win_idx].rdev,
25 flash_base, size);
26 printk(BIOS_INFO, "ROM%d Decode Window: ",
27 (type == ROM2_DECODE_WINDOW) ? 2 : 3);
28 printk(BIOS_INFO, "SPI flash base=0x%lx, Host base=0x%lx, Size=0x%zx\n",
29 flash_base, host_base, size);
30 }
31
32 /*
33 *
34 * Note: This configuration assumes no flash remapping (fch_spi_rom_remapping() = 0).
35 *
36 * +--------------+
37 * | |
38 * | |
39 * | |
40 * CONFIG_ROM_SIZE +------------+--------------------------+--------------------------------+- rom3_end
41 * | | ^ | |
42 * | | | | ROM3 |
43 * | | | | |
44 * | | | | |
45 * | | + | |
46 * | | rom3_size | |
47 * | | + | |
48 * | SPI ROM | | | |
49 * | | +----+-----------------+--------------+
50 * | | | | | |
51 * | | | | | |
52 * | | | V | |
53 * | | | +---0xfd00000000--+--------------+- rom3_start
54 * | | | | |
55 * | | | | |
56 * | | | | Other MMIO |
57 * | | | | |
58 * +------------+ --------------------+---------0x100000000--+--------------+- rom2_end
59 * | | ^ | |
60 * | | | | |
61 * | | rom2_size | ROM2 |
62 * | | | | |
63 * | | V | |
64 * 0 +------------+ ------------------------------0xFF0000000--+--------------+- rom2_start
65 * | |
66 * SPI flash | |
67 * address | |
68 * space | |
69 * +--------------+
70 *
71 * Host address
72 * space
73 */
74 static void bios_mmap_init(void)
75 {
76 static bool init_done;
77 size_t win_count = 0;
78 size_t map_win_size = 0;
79
80 if (init_done)
81 return;
82
83 /*
84 * By default, fixed decode window (maximum size 16MiB) is mapped just
85 * below the 4G boundary. This window maps the bottom part of the BIOS
86 * region in the SPI flash address space to the host address space.
87 */
88 size_t rom2_size = 0;
89 const uintptr_t rom2_start = lpc_get_rom2_region(&rom2_size);
90 if (rom2_start && rom2_size) {
91 initialize_window(win_count, ROM2_DECODE_WINDOW, rom2_start, 0, rom2_size);
92 win_count++;
93 map_win_size += rom2_size;
94 }
95
96 /*
97 * Remaining portion of the BIOS region up to a maximum of 64MiB is
98 * mapped at the bottom of the ROM3 if the BIOS region is greater than 16MiB.
99 * The ROM3 window is only used when it's inside the identity mapped
100 * page tables used by x86_64.
101 *
102 * If the BIOS region is not greater than 16MiB, then the ROM3 window is not
103 * enabled.
104 */
105 size_t rom3_size;
106 const uint64_t rom3_start = lpc_get_rom3_region(&rom3_size);
107 const uint64_t rom3_end = rom3_start + rom3_size;
108
109 if (CONFIG_ROM_SIZE > 16 * MiB) {
110 assert(rom3_start);
111 assert(rom3_size > 16 * MiB);
112 assert(DIV_ROUND_UP(rom3_end, GiB) < CONFIG_CPU_PT_ROM_MAP_GB);
113 }
114
115 if (CONFIG_ROM_SIZE > 16 * MiB &&
116 rom3_start > 0 &&
117 rom3_size > 16 * MiB &&
118 DIV_ROUND_UP(rom3_end, GiB) < CONFIG_CPU_PT_ROM_MAP_GB) {
119
120 const size_t ext_win_size = MIN(rom3_size, CONFIG_ROM_SIZE - rom2_size);
121
122 initialize_window(win_count, ROM3_DECODE_WINDOW,
123 rom3_start + rom2_size, rom2_size, ext_win_size);
124 win_count++;
125 map_win_size += ext_win_size;
126 }
127
128 assert(map_win_size == CONFIG_ROM_SIZE);
129 xlate_region_device_ro_init(&real_dev, win_count, real_dev_windows, map_win_size);
130
131 init_done = true;
132 }
133
134 const struct region_device *boot_device_ro(void)
135 {
136 bios_mmap_init();
137
138 return &real_dev.rdev;
139 }
140
141 uint32_t spi_flash_get_mmap_windows(struct flash_mmap_window *table)
142 {
143 int i;
144 uint32_t count = 0;
145
146 bios_mmap_init();
147
148 for (i = 0; i < TOTAL_DECODE_WINDOWS; i++) {
149 if (region_sz(&real_dev_windows[i].sub_region) == 0)
150 continue;
151
152 count++;
153 table->flash_base = region_offset(&real_dev_windows[i].sub_region);
154 /* FIXME: Allow SPI mmap in 64-bit address space */
155 if ((uintptr_t)rdev_mmap_full(&shadow_devs[i].rdev) < 4ULL * GiB)
156 table->host_base = (uintptr_t)rdev_mmap_full(&shadow_devs[i].rdev);
157 table->size = region_sz(&real_dev_windows[i].sub_region);
158
159 table++;
160 }
161
162 return count;
163 }