Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Andrey Petrov | 5672dcd | 2016-02-12 15:12:43 -0800 | [diff] [blame] | 2 | |
| 3 | #include <boot_device.h> |
Andrey Petrov | 5672dcd | 2016-02-12 15:12:43 -0800 | [diff] [blame] | 4 | #include <commonlib/region.h> |
| 5 | #include <console/console.h> |
| 6 | #include <fmap.h> |
Barnali Sarkar | e70142c | 2017-03-28 16:32:33 +0530 | [diff] [blame] | 7 | #include <intelblocks/fast_spi.h> |
Furquan Shaikh | 493937e | 2020-11-25 17:15:09 -0800 | [diff] [blame] | 8 | #include <spi_flash.h> |
Andrey Petrov | 5672dcd | 2016-02-12 15:12:43 -0800 | [diff] [blame] | 9 | |
| 10 | /* |
Furquan Shaikh | 0be3da5 | 2016-06-19 23:20:43 -0700 | [diff] [blame] | 11 | * BIOS region on the flash is mapped right below 4GiB in the address |
| 12 | * space. However, 256KiB right below 4GiB is decoded by read-only SRAM and not |
| 13 | * boot media. |
| 14 | * |
Lee Leahy | 07441b5 | 2017-03-09 10:59:25 -0800 | [diff] [blame] | 15 | * +-----------+ 0 |
| 16 | * | | |
| 17 | * | | |
| 18 | * | | |
| 19 | * | | |
| 20 | * | | |
| 21 | * | | |
| 22 | * | | |
| 23 | * | | |
| 24 | * +--------+ | | |
| 25 | * | IFD | | | |
| 26 | * bios_start +---> +--------+------------------> +-----------+ 4GiB - bios_size |
| 27 | * ^ | | ^ | | |
| 28 | * | | | | | | |
| 29 | * | | | bios_mapped_size | BIOS | |
| 30 | * | | BIOS | | | | |
| 31 | * bios_size | | | | | |
| 32 | * | | | v | | |
| 33 | * | | +------------------> +-----------+ 4GiB - 256KiB |
| 34 | * | | | | Read only | |
| 35 | * v | | | SRAM | |
| 36 | * bios_end +---> +--------+ +-----------+ 4GiB |
| 37 | * | Device | |
| 38 | * | ext | |
| 39 | * +--------+ |
Furquan Shaikh | 0be3da5 | 2016-06-19 23:20:43 -0700 | [diff] [blame] | 40 | * |
Andrey Petrov | 5672dcd | 2016-02-12 15:12:43 -0800 | [diff] [blame] | 41 | */ |
Andrey Petrov | 5672dcd | 2016-02-12 15:12:43 -0800 | [diff] [blame] | 42 | |
Arthur Heymans | 6d6945b | 2018-12-29 14:00:46 +0100 | [diff] [blame] | 43 | static size_t bios_size; |
Andrey Petrov | 5672dcd | 2016-02-12 15:12:43 -0800 | [diff] [blame] | 44 | |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 45 | static struct region_device shadow_dev; |
Arthur Heymans | 6d6945b | 2018-12-29 14:00:46 +0100 | [diff] [blame] | 46 | static struct xlate_region_device real_dev; |
Furquan Shaikh | f5b30ed | 2020-11-11 23:23:13 -0800 | [diff] [blame] | 47 | static struct xlate_window real_dev_window; |
Furquan Shaikh | 0be3da5 | 2016-06-19 23:20:43 -0700 | [diff] [blame] | 48 | |
| 49 | static void bios_mmap_init(void) |
| 50 | { |
Barnali Sarkar | e70142c | 2017-03-28 16:32:33 +0530 | [diff] [blame] | 51 | size_t size, start, bios_mapped_size; |
Subrata Banik | c790850 | 2017-03-23 11:52:59 +0530 | [diff] [blame] | 52 | uintptr_t base; |
Furquan Shaikh | 0be3da5 | 2016-06-19 23:20:43 -0700 | [diff] [blame] | 53 | |
Arthur Heymans | 6d6945b | 2018-12-29 14:00:46 +0100 | [diff] [blame] | 54 | size = bios_size; |
Furquan Shaikh | 0be3da5 | 2016-06-19 23:20:43 -0700 | [diff] [blame] | 55 | |
| 56 | /* If bios_size is initialized, then bail out. */ |
| 57 | if (size != 0) |
| 58 | return; |
Barnali Sarkar | e70142c | 2017-03-28 16:32:33 +0530 | [diff] [blame] | 59 | start = fast_spi_get_bios_region(&size); |
Furquan Shaikh | 0be3da5 | 2016-06-19 23:20:43 -0700 | [diff] [blame] | 60 | |
Furquan Shaikh | 0be3da5 | 2016-06-19 23:20:43 -0700 | [diff] [blame] | 61 | /* BIOS region is mapped right below 4G. */ |
| 62 | base = 4ULL * GiB - size; |
| 63 | |
| 64 | /* |
| 65 | * The 256 KiB right below 4G are decoded by readonly SRAM, |
| 66 | * not boot media. |
| 67 | */ |
| 68 | bios_mapped_size = size - 256 * KiB; |
| 69 | |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 70 | rdev_chain_mem(&shadow_dev, (void *)base, bios_mapped_size); |
Furquan Shaikh | 0be3da5 | 2016-06-19 23:20:43 -0700 | [diff] [blame] | 71 | |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 72 | xlate_window_init(&real_dev_window, &shadow_dev, start, bios_mapped_size); |
Furquan Shaikh | f5b30ed | 2020-11-11 23:23:13 -0800 | [diff] [blame] | 73 | xlate_region_device_ro_init(&real_dev, 1, &real_dev_window, CONFIG_ROM_SIZE); |
Furquan Shaikh | 0be3da5 | 2016-06-19 23:20:43 -0700 | [diff] [blame] | 74 | |
Arthur Heymans | 6d6945b | 2018-12-29 14:00:46 +0100 | [diff] [blame] | 75 | bios_size = size; |
Julius Werner | 029d672 | 2019-12-05 22:08:09 -0800 | [diff] [blame] | 76 | |
| 77 | /* Check that the CBFS lies within the memory mapped area. It's too |
| 78 | easy to forget the SRAM mapping when crafting an FMAP file. */ |
| 79 | struct region cbfs_region; |
| 80 | if (!fmap_locate_area("COREBOOT", &cbfs_region) && |
Furquan Shaikh | f5b30ed | 2020-11-11 23:23:13 -0800 | [diff] [blame] | 81 | !region_is_subregion(&real_dev_window.sub_region, &cbfs_region)) |
Julius Werner | 029d672 | 2019-12-05 22:08:09 -0800 | [diff] [blame] | 82 | printk(BIOS_CRIT, |
| 83 | "ERROR: CBFS @ %zx size %zx exceeds mem-mapped area @ %zx size %zx\n", |
| 84 | region_offset(&cbfs_region), region_sz(&cbfs_region), |
| 85 | start, bios_mapped_size); |
Furquan Shaikh | 0be3da5 | 2016-06-19 23:20:43 -0700 | [diff] [blame] | 86 | } |
Andrey Petrov | 5672dcd | 2016-02-12 15:12:43 -0800 | [diff] [blame] | 87 | |
| 88 | const struct region_device *boot_device_ro(void) |
| 89 | { |
Furquan Shaikh | 0be3da5 | 2016-06-19 23:20:43 -0700 | [diff] [blame] | 90 | bios_mmap_init(); |
| 91 | |
Arthur Heymans | 6d6945b | 2018-12-29 14:00:46 +0100 | [diff] [blame] | 92 | return &real_dev.rdev; |
Andrey Petrov | 5672dcd | 2016-02-12 15:12:43 -0800 | [diff] [blame] | 93 | } |
Furquan Shaikh | 493937e | 2020-11-25 17:15:09 -0800 | [diff] [blame] | 94 | |
| 95 | uint32_t spi_flash_get_mmap_windows(struct flash_mmap_window *table) |
| 96 | { |
| 97 | bios_mmap_init(); |
| 98 | |
| 99 | table->flash_base = region_offset(&real_dev_window.sub_region); |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 100 | table->host_base = (uintptr_t)rdev_mmap_full(&shadow_dev); |
Furquan Shaikh | 493937e | 2020-11-25 17:15:09 -0800 | [diff] [blame] | 101 | table->size = region_sz(&real_dev_window.sub_region); |
| 102 | |
| 103 | return 1; |
| 104 | } |