Debian Codegraph @

src/soc/intel/common/block/fast_spi/mmap_boot.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /*
4 * This file provides a custom boot media device for the platforms that support additional
5 * window for BIOS regions greater than 16MiB. If the mainboard uses a smaller BIOS region, then
6 * the additional window is unused.
7 */
8
9 #include <boot_device.h>
10 #include <commonlib/region.h>
11 #include <console/console.h>
12 #include <fast_spi_dma.h>
13 #include <fmap.h>
14 #include <intelblocks/fast_spi.h>
15 #include <spi_flash.h>
16
17 enum window_type {
18 /* Fixed decode window of max 16MiB size just below 4G boundary */
19 FIXED_DECODE_WINDOW,
20 /* Additional decode window for mapping BIOS region greater than 16MiB */
21 EXT_BIOS_DECODE_WINDOW,
22 TOTAL_DECODE_WINDOWS,
23 };
24
25 static struct xlate_region_device real_dev;
26 static struct mem_region_device shadow_devs[TOTAL_DECODE_WINDOWS];
27 static struct xlate_window real_dev_windows[TOTAL_DECODE_WINDOWS];
28
29 static void initialize_window(enum window_type type, uintptr_t host_base,
30 uintptr_t flash_base, size_t size)
31 {
32 mem_region_device_ro_init(&shadow_devs[type], (void *)host_base, size);
33 xlate_window_init(&real_dev_windows[type], &shadow_devs[type].rdev,
34 flash_base, size);
35 printk(BIOS_INFO, "%s: ",
36 (type == FIXED_DECODE_WINDOW) ?
37 "Fixed Decode Window" : "Extended Decode Window");
38 printk(BIOS_INFO, "SPI flash base=0x%lx, Host base=0x%lx, Size=0x%zx\n",
39 flash_base, host_base, size);
40 }
41
42 /*
43 *
44 * +--------------+
45 * | |
46 * | |
47 * | |
48 * ^ +------------+--------------------------^--------------------------------+ 0xffffffff
49 * | | | | | |
50 * | | | | | |
51 * | | | + | FIXED |
52 * | | | fixed_win_size | DECODE |
53 * | | BIOS | + | WINDOW |
54 * + | region | | | |
55 * bios_size | (Region 1) | | | |
56 * + | | | | |
57 * | | | | | |
58 * | | | fixed_win_flash_base+----v--------------------------------+ fixed_win_host_base
59 * | | | | | |
60 * | | | | | |
61 * | | | | | Other MMIO |
62 * v | | | | |
63 * bios_start +------------+ ext_win_flash_base | | |
64 * | | + | | |
65 * | | | | | |
66 * | | | | | |
67 * | | | +-----^------------------------------------------------------------^
68 * 0 +------------+ | | | | |
69 * | + | EXT_BIOS | |
70 * SPI flash | ext_win_size | DECODE | |
71 * address | + | WINDOW | +
72 * space | | | | CONFIG_EXT_BIOS_WIN_SIZE
73 * +--------------v-------------------------------+ ext_win_host_base +
74 * | | |
75 * | Unused | |
76 * | | |
77 * +--------------+ CONFIG_EXT_BIOS_WIN_BASE+--v
78 * | |
79 * | |
80 * | |
81 * +--------------+
82 *
83 * Host address
84 * space
85 */
86 static void bios_mmap_init(void)
87 {
88 static bool init_done;
89
90 size_t bios_size, bios_start;
91
92 uintptr_t fixed_win_host_base, fixed_win_flash_base;
93 uintptr_t ext_win_host_base, ext_win_flash_base;
94 size_t fixed_win_size, ext_win_size;
95
96 size_t win_count = 0;
97
98 if (init_done)
99 return;
100
101 /* Read the offset and size of BIOS region in the SPI flash address space. */
102 bios_start = fast_spi_get_bios_region(&bios_size);
103
104 /*
105 * By default, fixed decode window (maximum size 16MiB) is mapped just below the 4G
106 * boundary. This window maps the top part of the BIOS region in the SPI flash address
107 * space to the host address space.
108 */
109 fixed_win_size = MIN(16 * MiB, bios_size);
110 fixed_win_host_base = 4ULL * GiB - fixed_win_size;
111 fixed_win_flash_base = bios_start + bios_size - fixed_win_size;
112
113 initialize_window(FIXED_DECODE_WINDOW, fixed_win_host_base, fixed_win_flash_base,
114 fixed_win_size);
115 win_count++;
116
117 _Static_assert(CONFIG_EXT_BIOS_WIN_BASE != 0, "Extended BIOS window base cannot be 0!");
118 _Static_assert(CONFIG_EXT_BIOS_WIN_SIZE != 0, "Extended BIOS window size cannot be 0!");
119
120 /*
121 * Remaining portion of the BIOS region up to a maximum of CONFIG_EXT_BIOS_WIN_SIZE is
122 * mapped at the top of the extended window if the BIOS region is greater than 16MiB.
123 *
124 * If the BIOS region is not greater than 16MiB, then the extended window is not
125 * enabled.
126 */
127 ext_win_size = MIN(CONFIG_EXT_BIOS_WIN_SIZE, bios_size - fixed_win_size);
128
129 if (ext_win_size) {
130 ext_win_host_base = CONFIG_EXT_BIOS_WIN_BASE + CONFIG_EXT_BIOS_WIN_SIZE -
131 ext_win_size;
132 ext_win_flash_base = fixed_win_flash_base - ext_win_size;
133 initialize_window(EXT_BIOS_DECODE_WINDOW, ext_win_host_base,
134 ext_win_flash_base, ext_win_size);
135 win_count++;
136 }
137
138 xlate_region_device_ro_init(&real_dev, win_count, real_dev_windows, CONFIG_ROM_SIZE);
139
140 init_done = true;
141 }
142
143 const struct region_device *boot_device_ro(void)
144 {
145 bios_mmap_init();
146
147 if (fast_spi_dma_is_supported())
148 spi_flash_dma_install_ops(&real_dev);
149
150 return &real_dev.rdev;
151 }
152
153 void fast_spi_get_ext_bios_window(uintptr_t *base, size_t *size)
154 {
155 const struct region_device *rd = &shadow_devs[EXT_BIOS_DECODE_WINDOW].rdev;
156
157 bios_mmap_init();
158
159 *size = region_device_sz(rd);
160
161 if (*size == 0) {
162 *base = 0;
163 } else {
164 /*
165 * This is a memory region device. So, mmap returns the base address of the
166 * device. Also, as this is a memory region device, unmap is a no-op.
167 */
168 *base = (uintptr_t)rdev_mmap_full(rd);
169 }
170 }
171
172 uint32_t spi_flash_get_mmap_windows(struct flash_mmap_window *table)
173 {
174 int i;
175 uint32_t count = 0;
176
177 bios_mmap_init();
178
179 for (i = 0; i < TOTAL_DECODE_WINDOWS; i++) {
180 if (region_sz(&real_dev_windows[i].sub_region) == 0)
181 continue;
182
183 count++;
184 table->flash_base = region_offset(&real_dev_windows[i].sub_region);
185 table->host_base = (uintptr_t)rdev_mmap_full(&shadow_devs[i].rdev);
186 table->size = region_sz(&real_dev_windows[i].sub_region);
187
188 table++;
189 }
190
191 return count;
192 }