common: Move reset_cpu() to the CPU header
[oweals/u-boot.git] / lib / efi / efi_app.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  *
5  * EFI information obtained here:
6  * http://wiki.phoenix.com/wiki/index.php/EFI_BOOT_SERVICES
7  *
8  * This file implements U-Boot running as an EFI application.
9  */
10
11 #include <common.h>
12 #include <cpu_func.h>
13 #include <debug_uart.h>
14 #include <dm.h>
15 #include <errno.h>
16 #include <linux/err.h>
17 #include <linux/types.h>
18 #include <efi.h>
19 #include <efi_api.h>
20 #include <sysreset.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 static struct efi_priv *global_priv;
25
26 struct efi_system_table *efi_get_sys_table(void)
27 {
28         return global_priv->sys_table;
29 }
30
31 unsigned long efi_get_ram_base(void)
32 {
33         return global_priv->ram_base;
34 }
35
36 static efi_status_t setup_memory(struct efi_priv *priv)
37 {
38         struct efi_boot_services *boot = priv->boot;
39         efi_physical_addr_t addr;
40         efi_status_t ret;
41         int pages;
42
43         /*
44          * Use global_data_ptr instead of gd since it is an assignment. There
45          * are very few assignments to global_data in U-Boot and this makes
46          * it easier to find them.
47          */
48         global_data_ptr = efi_malloc(priv, sizeof(struct global_data), &ret);
49         if (!global_data_ptr)
50                 return ret;
51         memset(gd, '\0', sizeof(*gd));
52
53         gd->malloc_base = (ulong)efi_malloc(priv, CONFIG_VAL(SYS_MALLOC_F_LEN),
54                                             &ret);
55         if (!gd->malloc_base)
56                 return ret;
57         pages = CONFIG_EFI_RAM_SIZE >> 12;
58
59         /*
60          * Don't allocate any memory above 4GB. U-Boot is a 32-bit application
61          * so we want it to load below 4GB.
62          */
63         addr = 1ULL << 32;
64         ret = boot->allocate_pages(EFI_ALLOCATE_MAX_ADDRESS,
65                                    priv->image_data_type, pages, &addr);
66         if (ret) {
67                 printf("(using pool %lx) ", ret);
68                 priv->ram_base = (ulong)efi_malloc(priv, CONFIG_EFI_RAM_SIZE,
69                                                    &ret);
70                 if (!priv->ram_base)
71                         return ret;
72                 priv->use_pool_for_malloc = true;
73         } else {
74                 priv->ram_base = addr;
75         }
76         gd->ram_size = pages << 12;
77
78         return 0;
79 }
80
81 static void free_memory(struct efi_priv *priv)
82 {
83         struct efi_boot_services *boot = priv->boot;
84
85         if (priv->use_pool_for_malloc)
86                 efi_free(priv, (void *)priv->ram_base);
87         else
88                 boot->free_pages(priv->ram_base, gd->ram_size >> 12);
89
90         efi_free(priv, (void *)gd->malloc_base);
91         efi_free(priv, gd);
92         global_data_ptr = NULL;
93 }
94
95 /**
96  * efi_main() - Start an EFI image
97  *
98  * This function is called by our EFI start-up code. It handles running
99  * U-Boot. If it returns, EFI will continue. Another way to get back to EFI
100  * is via reset_cpu().
101  */
102 efi_status_t EFIAPI efi_main(efi_handle_t image,
103                              struct efi_system_table *sys_table)
104 {
105         struct efi_priv local_priv, *priv = &local_priv;
106         efi_status_t ret;
107
108         /* Set up access to EFI data structures */
109         efi_init(priv, "App", image, sys_table);
110
111         global_priv = priv;
112
113         /*
114          * Set up the EFI debug UART so that printf() works. This is
115          * implemented in the EFI serial driver, serial_efi.c. The application
116          * can use printf() freely.
117          */
118         debug_uart_init();
119
120         ret = setup_memory(priv);
121         if (ret) {
122                 printf("Failed to set up memory: ret=%lx\n", ret);
123                 return ret;
124         }
125
126         printf("starting\n");
127
128         board_init_f(GD_FLG_SKIP_RELOC);
129         board_init_r(NULL, 0);
130         free_memory(priv);
131
132         return EFI_SUCCESS;
133 }
134
135 static void efi_exit(void)
136 {
137         struct efi_priv *priv = global_priv;
138
139         free_memory(priv);
140         printf("U-Boot EFI exiting\n");
141         priv->boot->exit(priv->parent_image, EFI_SUCCESS, 0, NULL);
142 }
143
144 static int efi_sysreset_request(struct udevice *dev, enum sysreset_t type)
145 {
146         efi_exit();
147
148         return -EINPROGRESS;
149 }
150
151 static const struct udevice_id efi_sysreset_ids[] = {
152         { .compatible = "efi,reset" },
153         { }
154 };
155
156 static struct sysreset_ops efi_sysreset_ops = {
157         .request = efi_sysreset_request,
158 };
159
160 U_BOOT_DRIVER(efi_sysreset) = {
161         .name = "efi-sysreset",
162         .id = UCLASS_SYSRESET,
163         .of_match = efi_sysreset_ids,
164         .ops = &efi_sysreset_ops,
165 };