blob: fc37a68b3760eff97049d356f726723bf4beba69 [file] [log] [blame]
Bin Meng0f862b92018-06-12 08:36:22 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
4 *
5 * EFI framebuffer driver based on GOP
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <efi_api.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glassec86bc62022-07-30 15:52:04 -060012#include <vesa.h>
Bin Meng0f862b92018-06-12 08:36:22 -070013#include <video.h>
14
15struct pixel {
16 u8 pos;
17 u8 size;
18};
19
20static const struct efi_framebuffer {
21 struct pixel red;
22 struct pixel green;
23 struct pixel blue;
24 struct pixel rsvd;
25} efi_framebuffer_format_map[] = {
26 [EFI_GOT_RGBA8] = { {0, 8}, {8, 8}, {16, 8}, {24, 8} },
27 [EFI_GOT_BGRA8] = { {16, 8}, {8, 8}, {0, 8}, {24, 8} },
28};
29
30static void efi_find_pixel_bits(u32 mask, u8 *pos, u8 *size)
31{
32 u8 first, len;
33
34 first = 0;
35 len = 0;
36
37 if (mask) {
38 while (!(mask & 0x1)) {
39 mask = mask >> 1;
40 first++;
41 }
42
43 while (mask & 0x1) {
44 mask = mask >> 1;
45 len++;
46 }
47 }
48
49 *pos = first;
50 *size = len;
51}
52
Simon Glassf89af4e2023-03-10 12:47:11 -080053/**
54 * get_mode_info() - Ask EFI for the mode information
55 *
56 * Gets info from the graphics-output protocol
57 *
58 * @vesa: Place to put the mode information
59 * Returns: 0 if OK, -ENOSYS if boot services are not available, -ENOTSUPP if
60 * the protocol is not supported by EFI
61 */
Simon Glass04c26ff2021-11-03 21:09:10 -060062static int get_mode_info(struct vesa_mode_info *vesa)
63{
64 efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
65 struct efi_boot_services *boot = efi_get_boot();
66 struct efi_gop_mode *mode;
67 struct efi_gop *gop;
68 int ret;
69
70 if (!boot)
71 return log_msg_ret("sys", -ENOSYS);
72 ret = boot->locate_protocol(&efi_gop_guid, NULL, (void **)&gop);
73 if (ret)
74 return log_msg_ret("prot", -ENOTSUPP);
75
76 mode = gop->mode;
77 vesa->phys_base_ptr = mode->fb_base;
78 vesa->x_resolution = mode->info->width;
79 vesa->y_resolution = mode->info->height;
80
81 return 0;
82}
83
Simon Glassf89af4e2023-03-10 12:47:11 -080084/**
85 * get_mode_from_entry() - Obtain fb info from the EFIET_GOP_MODE payload entry
86 *
87 * This gets the mode information provided by the stub to the payload and puts
88 * it into a vesa structure. It also returns the mode information.
89 *
90 * @vesa: Place to put the mode information
91 * @infop: Returns a pointer to the mode info
92 * Returns: 0 if OK, -ve on error
93 */
94static int get_mode_from_entry(struct vesa_mode_info *vesa,
95 struct efi_gop_mode_info **infop)
96{
97 struct efi_gop_mode *mode;
98 int size;
99 int ret;
100
101 ret = efi_info_get(EFIET_GOP_MODE, (void **)&mode, &size);
102 if (ret) {
103 printf("EFI graphics output entry not found\n");
104 return ret;
105 }
106 vesa->phys_base_ptr = mode->fb_base;
107 vesa->x_resolution = mode->info->width;
108 vesa->y_resolution = mode->info->height;
109 *infop = mode->info;
110
111 return 0;
112}
113
Bin Meng0f862b92018-06-12 08:36:22 -0700114static int save_vesa_mode(struct vesa_mode_info *vesa)
115{
Bin Meng0f862b92018-06-12 08:36:22 -0700116 const struct efi_framebuffer *fbinfo;
Simon Glassf89af4e2023-03-10 12:47:11 -0800117 struct efi_gop_mode_info *info;
Bin Meng0f862b92018-06-12 08:36:22 -0700118 int ret;
119
Simon Glassf89af4e2023-03-10 12:47:11 -0800120 if (IS_ENABLED(CONFIG_EFI_APP))
Simon Glass04c26ff2021-11-03 21:09:10 -0600121 ret = get_mode_info(vesa);
Simon Glassf89af4e2023-03-10 12:47:11 -0800122 else
123 ret = get_mode_from_entry(vesa, &info);
124 if (ret) {
125 printf("EFI graphics output protocol not found (err=%dE)\n",
126 ret);
127 return ret;
Bin Meng0f862b92018-06-12 08:36:22 -0700128 }
129
Simon Glassf89af4e2023-03-10 12:47:11 -0800130 if (info->pixel_format < EFI_GOT_BITMASK) {
131 fbinfo = &efi_framebuffer_format_map[info->pixel_format];
Bin Meng0f862b92018-06-12 08:36:22 -0700132 vesa->red_mask_size = fbinfo->red.size;
133 vesa->red_mask_pos = fbinfo->red.pos;
134 vesa->green_mask_size = fbinfo->green.size;
135 vesa->green_mask_pos = fbinfo->green.pos;
136 vesa->blue_mask_size = fbinfo->blue.size;
137 vesa->blue_mask_pos = fbinfo->blue.pos;
138 vesa->reserved_mask_size = fbinfo->rsvd.size;
139 vesa->reserved_mask_pos = fbinfo->rsvd.pos;
140
141 vesa->bits_per_pixel = 32;
Simon Glassf89af4e2023-03-10 12:47:11 -0800142 vesa->bytes_per_scanline = info->pixels_per_scanline * 4;
143 } else if (info->pixel_format == EFI_GOT_BITMASK) {
144 efi_find_pixel_bits(info->pixel_bitmask[0],
Bin Meng0f862b92018-06-12 08:36:22 -0700145 &vesa->red_mask_pos,
146 &vesa->red_mask_size);
Simon Glassf89af4e2023-03-10 12:47:11 -0800147 efi_find_pixel_bits(info->pixel_bitmask[1],
Bin Meng0f862b92018-06-12 08:36:22 -0700148 &vesa->green_mask_pos,
149 &vesa->green_mask_size);
Simon Glassf89af4e2023-03-10 12:47:11 -0800150 efi_find_pixel_bits(info->pixel_bitmask[2],
Bin Meng0f862b92018-06-12 08:36:22 -0700151 &vesa->blue_mask_pos,
152 &vesa->blue_mask_size);
Simon Glassf89af4e2023-03-10 12:47:11 -0800153 efi_find_pixel_bits(info->pixel_bitmask[3],
Bin Meng0f862b92018-06-12 08:36:22 -0700154 &vesa->reserved_mask_pos,
155 &vesa->reserved_mask_size);
156 vesa->bits_per_pixel = vesa->red_mask_size +
157 vesa->green_mask_size +
158 vesa->blue_mask_size +
159 vesa->reserved_mask_size;
Simon Glassf89af4e2023-03-10 12:47:11 -0800160 vesa->bytes_per_scanline = (info->pixels_per_scanline *
Bin Meng0f862b92018-06-12 08:36:22 -0700161 vesa->bits_per_pixel) / 8;
162 } else {
163 debug("efi set unknown framebuffer format: %d\n",
Simon Glassf89af4e2023-03-10 12:47:11 -0800164 info->pixel_format);
Bin Meng0f862b92018-06-12 08:36:22 -0700165 return -EINVAL;
166 }
167
168 return 0;
169}
170
171static int efi_video_probe(struct udevice *dev)
172{
Simon Glassb75b15b2020-12-03 16:55:23 -0700173 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Bin Meng0f862b92018-06-12 08:36:22 -0700174 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
175 struct vesa_mode_info *vesa = &mode_info.vesa;
176 int ret;
177
178 /* Initialize vesa_mode_info structure */
179 ret = save_vesa_mode(vesa);
180 if (ret)
181 goto err;
182
Simon Glass5b925202022-07-30 15:52:05 -0600183 ret = vesa_setup_video_priv(vesa, uc_priv, plat);
Bin Meng0f862b92018-06-12 08:36:22 -0700184 if (ret)
185 goto err;
186
187 printf("Video: %dx%dx%d\n", uc_priv->xsize, uc_priv->ysize,
188 vesa->bits_per_pixel);
189
190 return 0;
191
192err:
193 printf("No video mode configured in EFI!\n");
194 return ret;
195}
196
197static const struct udevice_id efi_video_ids[] = {
198 { .compatible = "efi-fb" },
199 { }
200};
201
202U_BOOT_DRIVER(efi_video) = {
203 .name = "efi_video",
204 .id = UCLASS_VIDEO,
205 .of_match = efi_video_ids,
206 .probe = efi_video_probe,
207};