Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 1 | // 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 | |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 10 | #include <common.h> |
| 11 | #include <dm.h> |
| 12 | #include <efi_api.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | cafe871 | 2022-07-30 15:52:04 -0600 | [diff] [blame] | 14 | #include <vesa.h> |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 15 | #include <video.h> |
| 16 | |
| 17 | struct pixel { |
| 18 | u8 pos; |
| 19 | u8 size; |
| 20 | }; |
| 21 | |
| 22 | static const struct efi_framebuffer { |
| 23 | struct pixel red; |
| 24 | struct pixel green; |
| 25 | struct pixel blue; |
| 26 | struct pixel rsvd; |
| 27 | } efi_framebuffer_format_map[] = { |
| 28 | [EFI_GOT_RGBA8] = { {0, 8}, {8, 8}, {16, 8}, {24, 8} }, |
| 29 | [EFI_GOT_BGRA8] = { {16, 8}, {8, 8}, {0, 8}, {24, 8} }, |
| 30 | }; |
| 31 | |
| 32 | static void efi_find_pixel_bits(u32 mask, u8 *pos, u8 *size) |
| 33 | { |
| 34 | u8 first, len; |
| 35 | |
| 36 | first = 0; |
| 37 | len = 0; |
| 38 | |
| 39 | if (mask) { |
| 40 | while (!(mask & 0x1)) { |
| 41 | mask = mask >> 1; |
| 42 | first++; |
| 43 | } |
| 44 | |
| 45 | while (mask & 0x1) { |
| 46 | mask = mask >> 1; |
| 47 | len++; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | *pos = first; |
| 52 | *size = len; |
| 53 | } |
| 54 | |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 55 | /** |
| 56 | * get_mode_info() - Ask EFI for the mode information |
| 57 | * |
| 58 | * Gets info from the graphics-output protocol |
| 59 | * |
| 60 | * @vesa: Place to put the mode information |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 61 | * @fbp: Returns the address of the frame buffer |
Simon Glass | 57fa418 | 2023-03-10 12:47:12 -0800 | [diff] [blame] | 62 | * @infop: Returns a pointer to the mode info |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 63 | * Returns: 0 if OK, -ENOSYS if boot services are not available, -ENOTSUPP if |
| 64 | * the protocol is not supported by EFI |
| 65 | */ |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 66 | static int get_mode_info(struct vesa_mode_info *vesa, u64 *fbp, |
Simon Glass | 57fa418 | 2023-03-10 12:47:12 -0800 | [diff] [blame] | 67 | struct efi_gop_mode_info **infop) |
Simon Glass | 1834c08 | 2021-11-03 21:09:10 -0600 | [diff] [blame] | 68 | { |
| 69 | efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; |
| 70 | struct efi_boot_services *boot = efi_get_boot(); |
| 71 | struct efi_gop_mode *mode; |
| 72 | struct efi_gop *gop; |
| 73 | int ret; |
| 74 | |
| 75 | if (!boot) |
| 76 | return log_msg_ret("sys", -ENOSYS); |
| 77 | ret = boot->locate_protocol(&efi_gop_guid, NULL, (void **)&gop); |
| 78 | if (ret) |
| 79 | return log_msg_ret("prot", -ENOTSUPP); |
Simon Glass | 1834c08 | 2021-11-03 21:09:10 -0600 | [diff] [blame] | 80 | mode = gop->mode; |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 81 | log_debug("maxmode %u, mode %u, info %p, size %lx, fb %lx, fb_size %lx\n", |
| 82 | mode->max_mode, mode->mode, mode->info, mode->info_size, |
| 83 | (ulong)mode->fb_base, (ulong)mode->fb_size); |
| 84 | |
Simon Glass | 1834c08 | 2021-11-03 21:09:10 -0600 | [diff] [blame] | 85 | vesa->phys_base_ptr = mode->fb_base; |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 86 | *fbp = mode->fb_base; |
Simon Glass | 1834c08 | 2021-11-03 21:09:10 -0600 | [diff] [blame] | 87 | vesa->x_resolution = mode->info->width; |
| 88 | vesa->y_resolution = mode->info->height; |
Simon Glass | 57fa418 | 2023-03-10 12:47:12 -0800 | [diff] [blame] | 89 | *infop = mode->info; |
Simon Glass | 1834c08 | 2021-11-03 21:09:10 -0600 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 94 | /** |
| 95 | * get_mode_from_entry() - Obtain fb info from the EFIET_GOP_MODE payload entry |
| 96 | * |
| 97 | * This gets the mode information provided by the stub to the payload and puts |
| 98 | * it into a vesa structure. It also returns the mode information. |
| 99 | * |
| 100 | * @vesa: Place to put the mode information |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 101 | * @fbp: Returns the address of the frame buffer |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 102 | * @infop: Returns a pointer to the mode info |
| 103 | * Returns: 0 if OK, -ve on error |
| 104 | */ |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 105 | static int get_mode_from_entry(struct vesa_mode_info *vesa, u64 *fbp, |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 106 | struct efi_gop_mode_info **infop) |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 107 | { |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 108 | struct efi_gop_mode *mode; |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 109 | int size; |
| 110 | int ret; |
| 111 | |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 112 | ret = efi_info_get(EFIET_GOP_MODE, (void **)&mode, &size); |
| 113 | if (ret) { |
| 114 | printf("EFI graphics output entry not found\n"); |
| 115 | return ret; |
| 116 | } |
| 117 | vesa->phys_base_ptr = mode->fb_base; |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 118 | *fbp = mode->fb_base; |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 119 | vesa->x_resolution = mode->info->width; |
| 120 | vesa->y_resolution = mode->info->height; |
| 121 | *infop = mode->info; |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 126 | static int save_vesa_mode(struct vesa_mode_info *vesa, u64 *fbp) |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 127 | { |
| 128 | const struct efi_framebuffer *fbinfo; |
| 129 | struct efi_gop_mode_info *info; |
| 130 | int ret; |
| 131 | |
| 132 | if (IS_ENABLED(CONFIG_EFI_APP)) |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 133 | ret = get_mode_info(vesa, fbp, &info); |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 134 | else |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 135 | ret = get_mode_from_entry(vesa, fbp, &info); |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 136 | if (ret) { |
| 137 | printf("EFI graphics output protocol not found (err=%dE)\n", |
| 138 | ret); |
| 139 | return ret; |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 142 | if (info->pixel_format < EFI_GOT_BITMASK) { |
| 143 | fbinfo = &efi_framebuffer_format_map[info->pixel_format]; |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 144 | vesa->red_mask_size = fbinfo->red.size; |
| 145 | vesa->red_mask_pos = fbinfo->red.pos; |
| 146 | vesa->green_mask_size = fbinfo->green.size; |
| 147 | vesa->green_mask_pos = fbinfo->green.pos; |
| 148 | vesa->blue_mask_size = fbinfo->blue.size; |
| 149 | vesa->blue_mask_pos = fbinfo->blue.pos; |
| 150 | vesa->reserved_mask_size = fbinfo->rsvd.size; |
| 151 | vesa->reserved_mask_pos = fbinfo->rsvd.pos; |
| 152 | |
| 153 | vesa->bits_per_pixel = 32; |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 154 | vesa->bytes_per_scanline = info->pixels_per_scanline * 4; |
| 155 | } else if (info->pixel_format == EFI_GOT_BITMASK) { |
| 156 | efi_find_pixel_bits(info->pixel_bitmask[0], |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 157 | &vesa->red_mask_pos, |
| 158 | &vesa->red_mask_size); |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 159 | efi_find_pixel_bits(info->pixel_bitmask[1], |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 160 | &vesa->green_mask_pos, |
| 161 | &vesa->green_mask_size); |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 162 | efi_find_pixel_bits(info->pixel_bitmask[2], |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 163 | &vesa->blue_mask_pos, |
| 164 | &vesa->blue_mask_size); |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 165 | efi_find_pixel_bits(info->pixel_bitmask[3], |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 166 | &vesa->reserved_mask_pos, |
| 167 | &vesa->reserved_mask_size); |
| 168 | vesa->bits_per_pixel = vesa->red_mask_size + |
| 169 | vesa->green_mask_size + |
| 170 | vesa->blue_mask_size + |
| 171 | vesa->reserved_mask_size; |
Simon Glass | 8875381 | 2023-03-10 12:47:11 -0800 | [diff] [blame] | 172 | vesa->bytes_per_scanline = (info->pixels_per_scanline * |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 173 | vesa->bits_per_pixel) / 8; |
| 174 | } else { |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 175 | log_err("Unknown framebuffer format: %d\n", info->pixel_format); |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 176 | return -EINVAL; |
| 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static int efi_video_probe(struct udevice *dev) |
| 183 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 184 | struct video_uc_plat *plat = dev_get_uclass_plat(dev); |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 185 | struct video_priv *uc_priv = dev_get_uclass_priv(dev); |
| 186 | struct vesa_mode_info *vesa = &mode_info.vesa; |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 187 | u64 fb; |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 188 | int ret; |
| 189 | |
| 190 | /* Initialize vesa_mode_info structure */ |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 191 | ret = save_vesa_mode(vesa, &fb); |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 192 | if (ret) |
| 193 | goto err; |
| 194 | |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 195 | ret = vesa_setup_video_priv(vesa, fb, uc_priv, plat); |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 196 | if (ret) |
| 197 | goto err; |
| 198 | |
Simon Glass | 644e614 | 2023-03-10 12:47:13 -0800 | [diff] [blame] | 199 | printf("Video: %dx%dx%d @ %lx\n", uc_priv->xsize, uc_priv->ysize, |
| 200 | vesa->bits_per_pixel, (ulong)fb); |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 201 | |
| 202 | return 0; |
| 203 | |
| 204 | err: |
| 205 | printf("No video mode configured in EFI!\n"); |
| 206 | return ret; |
| 207 | } |
| 208 | |
Simon Glass | f622292 | 2023-03-10 12:47:16 -0800 | [diff] [blame] | 209 | static int efi_video_bind(struct udevice *dev) |
| 210 | { |
| 211 | if (IS_ENABLED(CONFIG_VIDEO_COPY)) { |
| 212 | struct video_uc_plat *plat = dev_get_uclass_plat(dev); |
| 213 | struct vesa_mode_info vesa; |
| 214 | int ret; |
| 215 | u64 fb; |
| 216 | |
| 217 | /* |
| 218 | * Initialise vesa_mode_info structure so we can figure out the |
| 219 | * required framebuffer size. If something goes wrong, just do |
| 220 | * without a copy framebuffer |
| 221 | */ |
| 222 | ret = save_vesa_mode(&vesa, &fb); |
| 223 | if (!ret) { |
| 224 | /* this is not reached if the EFI call failed */ |
| 225 | plat->copy_size = vesa.bytes_per_scanline * |
| 226 | vesa.y_resolution; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 233 | static const struct udevice_id efi_video_ids[] = { |
| 234 | { .compatible = "efi-fb" }, |
| 235 | { } |
| 236 | }; |
| 237 | |
| 238 | U_BOOT_DRIVER(efi_video) = { |
| 239 | .name = "efi_video", |
| 240 | .id = UCLASS_VIDEO, |
| 241 | .of_match = efi_video_ids, |
Simon Glass | f622292 | 2023-03-10 12:47:16 -0800 | [diff] [blame] | 242 | .bind = efi_video_bind, |
Bin Meng | 4a08c74 | 2018-06-12 08:36:22 -0700 | [diff] [blame] | 243 | .probe = efi_video_probe, |
| 244 | }; |