]> git.sur5r.net Git - u-boot/blob - arch/x86/cpu/efi/payload.c
81fb8b5f72cb851c0fce3917c4290776f259885e
[u-boot] / arch / x86 / cpu / efi / payload.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #include <common.h>
8 #include <debug_uart.h>
9 #include <efi.h>
10 #include <errno.h>
11 #include <linux/err.h>
12 #include <linux/types.h>
13
14 DECLARE_GLOBAL_DATA_PTR;
15
16 /*
17  * This function looks for the highest region of memory lower than 4GB which
18  * has enough space for U-Boot where U-Boot is aligned on a page boundary.
19  * It overrides the default implementation found elsewhere which simply
20  * picks the end of ram, wherever that may be. The location of the stack,
21  * the relocation address, and how far U-Boot is moved by relocation are
22  * set in the global data structure.
23  */
24 ulong board_get_usable_ram_top(ulong total_size)
25 {
26         struct efi_mem_desc *desc, *end;
27         struct efi_entry_memmap *map;
28         int ret, size;
29         uintptr_t dest_addr = 0;
30         struct efi_mem_desc *largest = NULL;
31
32         /*
33          * Find largest area of memory below 4GB. We could
34          * call efi_build_mem_table() for a more accurate picture since it
35          * merges areas together where possible. But that function uses more
36          * pre-relocation memory, and it's not critical that we find the
37          * absolute largest region.
38          */
39         ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
40         if (ret) {
41                 /* We should have stopped in dram_init(), something is wrong */
42                 debug("%s: Missing memory map\n", __func__);
43                 goto err;
44         }
45
46         end = (struct efi_mem_desc *)((ulong)map + size);
47         desc = map->desc;
48         for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
49                 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
50                     desc->physical_start >= 1ULL << 32)
51                         continue;
52                 if (!largest || desc->num_pages > largest->num_pages)
53                         largest = desc;
54         }
55
56         /* If no suitable area was found, return an error. */
57         assert(largest);
58         if (!largest || (largest->num_pages << EFI_PAGE_SHIFT) < (2 << 20))
59                 goto err;
60
61         dest_addr = largest->physical_start + (largest->num_pages <<
62                         EFI_PAGE_SHIFT);
63
64         return (ulong)dest_addr;
65 err:
66         panic("No available memory found for relocation");
67         return 0;
68 }
69
70 int dram_init(void)
71 {
72         struct efi_mem_desc *desc, *end;
73         struct efi_entry_memmap *map;
74         int size, ret;
75
76         ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
77         if (ret) {
78                 printf("Cannot find EFI memory map tables, ret=%d\n", ret);
79
80                 return -ENODEV;
81         }
82
83         end = (struct efi_mem_desc *)((ulong)map + size);
84         gd->ram_size = 0;
85         desc = map->desc;
86         for (; desc < end; desc = efi_get_next_mem_desc(map, desc)) {
87                 if (desc->type < EFI_MMAP_IO)
88                         gd->ram_size += desc->num_pages << EFI_PAGE_SHIFT;
89         }
90
91         return 0;
92 }
93
94 int dram_init_banksize(void)
95 {
96         struct efi_mem_desc *desc, *end;
97         struct efi_entry_memmap *map;
98         int ret, size;
99         int num_banks;
100
101         ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size);
102         if (ret) {
103                 /* We should have stopped in dram_init(), something is wrong */
104                 debug("%s: Missing memory map\n", __func__);
105                 return -ENXIO;
106         }
107         end = (struct efi_mem_desc *)((ulong)map + size);
108         desc = map->desc;
109         for (num_banks = 0;
110              desc < end && num_banks < CONFIG_NR_DRAM_BANKS;
111              desc = efi_get_next_mem_desc(map, desc)) {
112                 /*
113                  * We only use conventional memory below 4GB, and ignore
114                  * anything less than 1MB.
115                  */
116                 if (desc->type != EFI_CONVENTIONAL_MEMORY ||
117                     desc->physical_start >= 1ULL << 32 ||
118                     (desc->num_pages << EFI_PAGE_SHIFT) < 1 << 20)
119                         continue;
120                 gd->bd->bi_dram[num_banks].start = desc->physical_start;
121                 gd->bd->bi_dram[num_banks].size = desc->num_pages <<
122                         EFI_PAGE_SHIFT;
123                 num_banks++;
124         }
125
126         return 0;
127 }
128
129 int checkcpu(void)
130 {
131         return 0;
132 }
133
134 int print_cpuinfo(void)
135 {
136         return default_print_cpuinfo();
137 }
138
139 /* Find any available tables and copy them to a safe place */
140 int reserve_arch(void)
141 {
142         struct efi_info_hdr *hdr;
143
144         debug("table=%lx\n", gd->arch.table);
145         if (!gd->arch.table)
146                 return 0;
147
148         hdr = (struct efi_info_hdr *)gd->arch.table;
149
150         gd->start_addr_sp -= hdr->total_size;
151         memcpy((void *)gd->start_addr_sp, hdr, hdr->total_size);
152         debug("Stashing EFI table at %lx to %lx, size %x\n",
153               gd->arch.table, gd->start_addr_sp, hdr->total_size);
154         gd->arch.table = gd->start_addr_sp;
155
156         return 0;
157 }