blob: 3abb47d610e658b67ba15ed3dcc0317fb3f96c0a [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0+
Alexander Grafd65783d2016-03-15 18:38:21 +01002/*
3 * EFI application disk support
4 *
5 * Copyright (c) 2016 Alexander Graf
Alexander Grafd65783d2016-03-15 18:38:21 +01006 */
7
Heinrich Schuchardt955a3212025-01-16 20:26:59 +01008#define LOG_CATEGORY LOGC_EFI
9
Alexander Graf792284f2016-06-05 22:34:31 +020010#include <dm.h>
Alexander Grafd65783d2016-03-15 18:38:21 +010011#include <efi_loader.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Alexander Grafd65783d2016-03-15 18:38:21 +010013#include <malloc.h>
Simon Glass65ea46f2023-10-01 19:14:36 -060014#include <mapmem.h>
Alexander Graf792284f2016-06-05 22:34:31 +020015#include <video.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Alexander Grafd65783d2016-03-15 18:38:21 +010017
18DECLARE_GLOBAL_DATA_PTR;
19
Heinrich Schuchardt788ad412019-04-20 07:39:11 +020020static const efi_guid_t efi_gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
Alexander Grafd65783d2016-03-15 18:38:21 +010021
Heinrich Schuchardt72928722018-09-26 05:27:56 +020022/**
23 * struct efi_gop_obj - graphical output protocol object
24 *
25 * @header: EFI object header
26 * @ops: graphical output protocol interface
27 * @info: graphical output mode information
28 * @mode: graphical output mode
Alexander Graf5e5e5d92022-06-10 00:59:19 +020029 * @vdev: backing video device
Heinrich Schuchardt72928722018-09-26 05:27:56 +020030 * @bpix: bits per pixel
31 * @fb: frame buffer
32 */
Alexander Grafd65783d2016-03-15 18:38:21 +010033struct efi_gop_obj {
Heinrich Schuchardt72928722018-09-26 05:27:56 +020034 struct efi_object header;
Alexander Grafd65783d2016-03-15 18:38:21 +010035 struct efi_gop ops;
Alexander Grafd65783d2016-03-15 18:38:21 +010036 struct efi_gop_mode_info info;
37 struct efi_gop_mode mode;
Alexander Graf5e5e5d92022-06-10 00:59:19 +020038 struct udevice *vdev;
Heinrich Schuchardt72928722018-09-26 05:27:56 +020039 /* Fields we only have access to during init */
Alexander Graf792284f2016-06-05 22:34:31 +020040 u32 bpix;
Rob Clark92cec962017-07-21 15:00:27 -040041 void *fb;
Alexander Grafd65783d2016-03-15 18:38:21 +010042};
43
44static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
Heinrich Schuchardt8b28d5c2017-10-26 19:25:51 +020045 efi_uintn_t *size_of_info,
Alexander Grafd65783d2016-03-15 18:38:21 +010046 struct efi_gop_mode_info **info)
47{
48 struct efi_gop_obj *gopobj;
Heinrich Schuchardt5e1dbf72019-06-15 12:52:35 +020049 efi_status_t ret = EFI_SUCCESS;
Alexander Grafd65783d2016-03-15 18:38:21 +010050
51 EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
52
Heinrich Schuchardt5e1dbf72019-06-15 12:52:35 +020053 if (!this || !size_of_info || !info || mode_number) {
54 ret = EFI_INVALID_PARAMETER;
55 goto out;
56 }
57
Alexander Grafd65783d2016-03-15 18:38:21 +010058 gopobj = container_of(this, struct efi_gop_obj, ops);
Heinrich Schuchardt0301e362019-06-15 14:07:40 +020059 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sizeof(gopobj->info),
60 (void **)info);
61 if (ret != EFI_SUCCESS)
62 goto out;
Alexander Grafd65783d2016-03-15 18:38:21 +010063 *size_of_info = sizeof(gopobj->info);
Heinrich Schuchardt0301e362019-06-15 14:07:40 +020064 memcpy(*info, &gopobj->info, sizeof(gopobj->info));
Alexander Grafd65783d2016-03-15 18:38:21 +010065
Heinrich Schuchardt5e1dbf72019-06-15 12:52:35 +020066out:
67 return EFI_EXIT(ret);
Alexander Grafd65783d2016-03-15 18:38:21 +010068}
69
Mark Kettenisd8ab3342021-09-25 22:47:39 +020070static __always_inline struct efi_gop_pixel efi_vid30_to_blt_col(u32 vid)
71{
72 struct efi_gop_pixel blt = {
73 .reserved = 0,
74 };
75
76 blt.blue = (vid & 0x3ff) >> 2;
77 vid >>= 10;
78 blt.green = (vid & 0x3ff) >> 2;
79 vid >>= 10;
80 blt.red = (vid & 0x3ff) >> 2;
81 return blt;
82}
83
84static __always_inline u32 efi_blt_col_to_vid30(struct efi_gop_pixel *blt)
85{
86 return (u32)(blt->red << 2) << 20 |
87 (u32)(blt->green << 2) << 10 |
88 (u32)(blt->blue << 2);
89}
90
Heinrich Schuchardt9370d5a2018-03-16 19:59:06 +010091static __always_inline struct efi_gop_pixel efi_vid16_to_blt_col(u16 vid)
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +010092{
93 struct efi_gop_pixel blt = {
94 .reserved = 0,
95 };
96
97 blt.blue = (vid & 0x1f) << 3;
98 vid >>= 5;
99 blt.green = (vid & 0x3f) << 2;
100 vid >>= 6;
101 blt.red = (vid & 0x1f) << 3;
102 return blt;
103}
104
Heinrich Schuchardt9370d5a2018-03-16 19:59:06 +0100105static __always_inline u16 efi_blt_col_to_vid16(struct efi_gop_pixel *blt)
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100106{
107 return (u16)(blt->red >> 3) << 11 |
108 (u16)(blt->green >> 2) << 5 |
109 (u16)(blt->blue >> 3);
110}
111
Alexander Grafa3800bd2018-03-15 15:02:28 +0100112static __always_inline efi_status_t gop_blt_int(struct efi_gop *this,
Alexander Graf18f436c2018-03-15 15:02:29 +0100113 struct efi_gop_pixel *bufferp,
Alexander Grafa3800bd2018-03-15 15:02:28 +0100114 u32 operation, efi_uintn_t sx,
115 efi_uintn_t sy, efi_uintn_t dx,
116 efi_uintn_t dy,
117 efi_uintn_t width,
118 efi_uintn_t height,
Alexander Graf18f436c2018-03-15 15:02:29 +0100119 efi_uintn_t delta,
120 efi_uintn_t vid_bpp)
Alexander Grafd65783d2016-03-15 18:38:21 +0100121{
Alexander Graf792284f2016-06-05 22:34:31 +0200122 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
Alexander Graf18f436c2018-03-15 15:02:29 +0100123 efi_uintn_t i, j, linelen, slineoff = 0, dlineoff, swidth, dwidth;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100124 u32 *fb32 = gopobj->fb;
125 u16 *fb16 = gopobj->fb;
Alexander Graf18f436c2018-03-15 15:02:29 +0100126 struct efi_gop_pixel *buffer = __builtin_assume_aligned(bufferp, 4);
Alexander Graf5e5e5d92022-06-10 00:59:19 +0200127 bool blt_to_video = (operation != EFI_BLT_VIDEO_TO_BLT_BUFFER);
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100128
Heinrich Schuchardt5f86d332018-03-14 19:57:02 +0100129 if (delta) {
130 /* Check for 4 byte alignment */
131 if (delta & 3)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100132 return EFI_INVALID_PARAMETER;
Heinrich Schuchardt5f86d332018-03-14 19:57:02 +0100133 linelen = delta >> 2;
134 } else {
135 linelen = width;
136 }
137
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100138 /* Check source rectangle */
139 switch (operation) {
140 case EFI_BLT_VIDEO_FILL:
141 break;
142 case EFI_BLT_BUFFER_TO_VIDEO:
143 if (sx + width > linelen)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100144 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100145 break;
146 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
147 case EFI_BLT_VIDEO_TO_VIDEO:
148 if (sx + width > gopobj->info.width ||
149 sy + height > gopobj->info.height)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100150 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100151 break;
152 default:
Alexander Grafa3800bd2018-03-15 15:02:28 +0100153 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100154 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100155
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100156 /* Check destination rectangle */
157 switch (operation) {
158 case EFI_BLT_VIDEO_FILL:
159 case EFI_BLT_BUFFER_TO_VIDEO:
160 case EFI_BLT_VIDEO_TO_VIDEO:
161 if (dx + width > gopobj->info.width ||
162 dy + height > gopobj->info.height)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100163 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100164 break;
165 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
166 if (dx + width > linelen)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100167 return EFI_INVALID_PARAMETER;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100168 break;
169 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100170
Alexander Graf18f436c2018-03-15 15:02:29 +0100171 /* Calculate line width */
172 switch (operation) {
173 case EFI_BLT_BUFFER_TO_VIDEO:
174 swidth = linelen;
175 break;
176 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
177 case EFI_BLT_VIDEO_TO_VIDEO:
178 swidth = gopobj->info.width;
179 if (!vid_bpp)
180 return EFI_UNSUPPORTED;
181 break;
182 case EFI_BLT_VIDEO_FILL:
183 swidth = 0;
184 break;
185 }
186
187 switch (operation) {
188 case EFI_BLT_BUFFER_TO_VIDEO:
189 case EFI_BLT_VIDEO_FILL:
190 case EFI_BLT_VIDEO_TO_VIDEO:
191 dwidth = gopobj->info.width;
192 if (!vid_bpp)
193 return EFI_UNSUPPORTED;
194 break;
195 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
196 dwidth = linelen;
197 break;
198 }
199
200 slineoff = swidth * sy;
201 dlineoff = dwidth * dy;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100202 for (i = 0; i < height; i++) {
203 for (j = 0; j < width; j++) {
204 struct efi_gop_pixel pix;
Alexander Grafd65783d2016-03-15 18:38:21 +0100205
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100206 /* Read source pixel */
207 switch (operation) {
208 case EFI_BLT_VIDEO_FILL:
209 pix = *buffer;
210 break;
211 case EFI_BLT_BUFFER_TO_VIDEO:
Alexander Graf18f436c2018-03-15 15:02:29 +0100212 pix = buffer[slineoff + j + sx];
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100213 break;
214 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
215 case EFI_BLT_VIDEO_TO_VIDEO:
Alexander Graf18f436c2018-03-15 15:02:29 +0100216 if (vid_bpp == 32)
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100217 pix = *(struct efi_gop_pixel *)&fb32[
Alexander Graf18f436c2018-03-15 15:02:29 +0100218 slineoff + j + sx];
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200219 else if (vid_bpp == 30)
220 pix = efi_vid30_to_blt_col(fb32[
221 slineoff + j + sx]);
Alexander Graf18f436c2018-03-15 15:02:29 +0100222 else
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100223 pix = efi_vid16_to_blt_col(fb16[
Alexander Graf18f436c2018-03-15 15:02:29 +0100224 slineoff + j + sx]);
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100225 break;
226 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100227
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100228 /* Write destination pixel */
229 switch (operation) {
230 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
Alexander Graf18f436c2018-03-15 15:02:29 +0100231 buffer[dlineoff + j + dx] = pix;
Heinrich Schuchardta2c715f2018-02-07 22:14:22 +0100232 break;
233 case EFI_BLT_BUFFER_TO_VIDEO:
234 case EFI_BLT_VIDEO_FILL:
235 case EFI_BLT_VIDEO_TO_VIDEO:
Alexander Graf18f436c2018-03-15 15:02:29 +0100236 if (vid_bpp == 32)
237 fb32[dlineoff + j + dx] = *(u32 *)&pix;
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200238 else if (vid_bpp == 30)
239 fb32[dlineoff + j + dx] =
240 efi_blt_col_to_vid30(&pix);
Alexander Graf18f436c2018-03-15 15:02:29 +0100241 else
242 fb16[dlineoff + j + dx] =
243 efi_blt_col_to_vid16(&pix);
244 break;
245 }
246 }
247 slineoff += swidth;
248 dlineoff += dwidth;
249 }
250
Alexander Graf5e5e5d92022-06-10 00:59:19 +0200251 if (blt_to_video)
252 video_damage(gopobj->vdev, dx, dy, width, height);
253
Alexander Graf18f436c2018-03-15 15:02:29 +0100254 return EFI_SUCCESS;
255}
256
257static efi_uintn_t gop_get_bpp(struct efi_gop *this)
258{
259 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
260 efi_uintn_t vid_bpp = 0;
261
262 switch (gopobj->bpix) {
Alexander Graf18f436c2018-03-15 15:02:29 +0100263 case VIDEO_BPP32:
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200264 if (gopobj->info.pixel_format == EFI_GOT_BGRA8)
265 vid_bpp = 32;
266 else
267 vid_bpp = 30;
Alexander Graf18f436c2018-03-15 15:02:29 +0100268 break;
Alexander Graf18f436c2018-03-15 15:02:29 +0100269 case VIDEO_BPP16:
Alexander Graf18f436c2018-03-15 15:02:29 +0100270 vid_bpp = 16;
271 break;
Alexander Grafd65783d2016-03-15 18:38:21 +0100272 }
273
Alexander Graf18f436c2018-03-15 15:02:29 +0100274 return vid_bpp;
Alexander Grafa3800bd2018-03-15 15:02:28 +0100275}
276
277/*
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200278 * GCC can't optimize our BLT function well, but we need to make sure that
Alexander Grafa3800bd2018-03-15 15:02:28 +0100279 * our 2-dimensional loop gets executed very quickly, otherwise the system
280 * will feel slow.
281 *
282 * By manually putting all obvious branch targets into functions which call
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200283 * our generic BLT function with constants, the compiler can successfully
Alexander Grafa3800bd2018-03-15 15:02:28 +0100284 * optimize for speed.
285 */
286static efi_status_t gop_blt_video_fill(struct efi_gop *this,
287 struct efi_gop_pixel *buffer,
288 u32 foo, efi_uintn_t sx,
289 efi_uintn_t sy, efi_uintn_t dx,
290 efi_uintn_t dy, efi_uintn_t width,
Alexander Graf18f436c2018-03-15 15:02:29 +0100291 efi_uintn_t height, efi_uintn_t delta,
292 efi_uintn_t vid_bpp)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100293{
294 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_FILL, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100295 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100296}
297
Alexander Graf18f436c2018-03-15 15:02:29 +0100298static efi_status_t gop_blt_buf_to_vid16(struct efi_gop *this,
299 struct efi_gop_pixel *buffer,
300 u32 foo, efi_uintn_t sx,
301 efi_uintn_t sy, efi_uintn_t dx,
302 efi_uintn_t dy, efi_uintn_t width,
303 efi_uintn_t height, efi_uintn_t delta)
304{
305 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
306 dy, width, height, delta, 16);
307}
308
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200309static efi_status_t gop_blt_buf_to_vid30(struct efi_gop *this,
310 struct efi_gop_pixel *buffer,
311 u32 foo, efi_uintn_t sx,
312 efi_uintn_t sy, efi_uintn_t dx,
313 efi_uintn_t dy, efi_uintn_t width,
314 efi_uintn_t height, efi_uintn_t delta)
315{
316 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
317 dy, width, height, delta, 30);
318}
319
Alexander Graf18f436c2018-03-15 15:02:29 +0100320static efi_status_t gop_blt_buf_to_vid32(struct efi_gop *this,
321 struct efi_gop_pixel *buffer,
322 u32 foo, efi_uintn_t sx,
323 efi_uintn_t sy, efi_uintn_t dx,
324 efi_uintn_t dy, efi_uintn_t width,
325 efi_uintn_t height, efi_uintn_t delta)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100326{
327 return gop_blt_int(this, buffer, EFI_BLT_BUFFER_TO_VIDEO, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100328 dy, width, height, delta, 32);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100329}
330
331static efi_status_t gop_blt_vid_to_vid(struct efi_gop *this,
332 struct efi_gop_pixel *buffer,
333 u32 foo, efi_uintn_t sx,
334 efi_uintn_t sy, efi_uintn_t dx,
335 efi_uintn_t dy, efi_uintn_t width,
Alexander Graf18f436c2018-03-15 15:02:29 +0100336 efi_uintn_t height, efi_uintn_t delta,
337 efi_uintn_t vid_bpp)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100338{
339 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_VIDEO, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100340 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100341}
342
343static efi_status_t gop_blt_vid_to_buf(struct efi_gop *this,
344 struct efi_gop_pixel *buffer,
345 u32 foo, efi_uintn_t sx,
346 efi_uintn_t sy, efi_uintn_t dx,
347 efi_uintn_t dy, efi_uintn_t width,
Alexander Graf18f436c2018-03-15 15:02:29 +0100348 efi_uintn_t height, efi_uintn_t delta,
349 efi_uintn_t vid_bpp)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100350{
351 return gop_blt_int(this, buffer, EFI_BLT_VIDEO_TO_BLT_BUFFER, sx, sy,
Alexander Graf18f436c2018-03-15 15:02:29 +0100352 dx, dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100353}
354
Heinrich Schuchardte32595b2019-06-15 14:52:53 +0200355/**
356 * gop_set_mode() - set graphical output mode
357 *
358 * This function implements the SetMode() service.
359 *
360 * See the Unified Extensible Firmware Interface (UEFI) specification for
361 * details.
362 *
363 * @this: the graphical output protocol
Heinrich Schuchardtdc305ad2019-09-05 20:37:13 +0200364 * @mode_number: the mode to be set
Heinrich Schuchardte32595b2019-06-15 14:52:53 +0200365 * Return: status code
366 */
367static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
368{
369 struct efi_gop_obj *gopobj;
370 struct efi_gop_pixel buffer = {0, 0, 0, 0};
371 efi_uintn_t vid_bpp;
372 efi_status_t ret = EFI_SUCCESS;
373
374 EFI_ENTRY("%p, %x", this, mode_number);
375
376 if (!this) {
377 ret = EFI_INVALID_PARAMETER;
378 goto out;
379 }
380 if (mode_number) {
381 ret = EFI_UNSUPPORTED;
382 goto out;
383 }
384 gopobj = container_of(this, struct efi_gop_obj, ops);
385 vid_bpp = gop_get_bpp(this);
386 ret = gop_blt_video_fill(this, &buffer, EFI_BLT_VIDEO_FILL, 0, 0, 0, 0,
387 gopobj->info.width, gopobj->info.height, 0,
388 vid_bpp);
389out:
390 return EFI_EXIT(ret);
391}
392
Alexander Grafa3800bd2018-03-15 15:02:28 +0100393/*
394 * Copy rectangle.
395 *
396 * This function implements the Blt service of the EFI_GRAPHICS_OUTPUT_PROTOCOL.
397 * See the Unified Extensible Firmware Interface (UEFI) specification for
398 * details.
399 *
400 * @this: EFI_GRAPHICS_OUTPUT_PROTOCOL
401 * @buffer: pixel buffer
402 * @sx: source x-coordinate
403 * @sy: source y-coordinate
404 * @dx: destination x-coordinate
405 * @dy: destination y-coordinate
406 * @width: width of rectangle
407 * @height: height of rectangle
408 * @delta: length in bytes of a line in the pixel buffer (optional)
Heinrich Schuchardtfbe90212022-01-20 19:48:20 +0100409 * Return: status code
Alexander Grafa3800bd2018-03-15 15:02:28 +0100410 */
Heinrich Schuchardt64a44002023-02-10 09:05:03 +0100411static efi_status_t EFIAPI gop_blt(struct efi_gop *this,
412 struct efi_gop_pixel *buffer,
413 u32 operation, efi_uintn_t sx,
414 efi_uintn_t sy, efi_uintn_t dx,
415 efi_uintn_t dy, efi_uintn_t width,
416 efi_uintn_t height, efi_uintn_t delta)
Alexander Grafa3800bd2018-03-15 15:02:28 +0100417{
418 efi_status_t ret = EFI_INVALID_PARAMETER;
Alexander Graf18f436c2018-03-15 15:02:29 +0100419 efi_uintn_t vid_bpp;
Alexander Grafa3800bd2018-03-15 15:02:28 +0100420
421 EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
422 buffer, operation, sx, sy, dx, dy, width, height, delta);
423
Alexander Graf18f436c2018-03-15 15:02:29 +0100424 vid_bpp = gop_get_bpp(this);
425
Alexander Grafa3800bd2018-03-15 15:02:28 +0100426 /* Allow for compiler optimization */
427 switch (operation) {
428 case EFI_BLT_VIDEO_FILL:
429 ret = gop_blt_video_fill(this, buffer, operation, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100430 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100431 break;
432 case EFI_BLT_BUFFER_TO_VIDEO:
Alexander Graf18f436c2018-03-15 15:02:29 +0100433 /* This needs to be super-fast, so duplicate for 16/32bpp */
434 if (vid_bpp == 32)
435 ret = gop_blt_buf_to_vid32(this, buffer, operation, sx,
436 sy, dx, dy, width, height,
437 delta);
Mark Kettenisd8ab3342021-09-25 22:47:39 +0200438 else if (vid_bpp == 30)
439 ret = gop_blt_buf_to_vid30(this, buffer, operation, sx,
440 sy, dx, dy, width, height,
441 delta);
Alexander Graf18f436c2018-03-15 15:02:29 +0100442 else
443 ret = gop_blt_buf_to_vid16(this, buffer, operation, sx,
444 sy, dx, dy, width, height,
445 delta);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100446 break;
447 case EFI_BLT_VIDEO_TO_VIDEO:
448 ret = gop_blt_vid_to_vid(this, buffer, operation, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100449 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100450 break;
451 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
452 ret = gop_blt_vid_to_buf(this, buffer, operation, sx, sy, dx,
Alexander Graf18f436c2018-03-15 15:02:29 +0100453 dy, width, height, delta, vid_bpp);
Alexander Grafa3800bd2018-03-15 15:02:28 +0100454 break;
455 default:
Heinrich Schuchardt087c8c42019-06-15 12:42:46 +0200456 ret = EFI_INVALID_PARAMETER;
Alexander Grafa3800bd2018-03-15 15:02:28 +0100457 }
458
459 if (ret != EFI_SUCCESS)
460 return EFI_EXIT(ret);
461
Alexander Graf792284f2016-06-05 22:34:31 +0200462 video_sync_all();
Alexander Grafd65783d2016-03-15 18:38:21 +0100463
464 return EFI_EXIT(EFI_SUCCESS);
465}
466
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100467/*
468 * Install graphical output protocol.
469 *
470 * If no supported video device exists this is not considered as an
471 * error.
472 */
473efi_status_t efi_gop_register(void)
Alexander Grafd65783d2016-03-15 18:38:21 +0100474{
475 struct efi_gop_obj *gopobj;
Mark Kettenis3e4b3d52021-09-25 22:47:37 +0200476 u32 bpix, format, col, row;
Alexander Grafbc766392016-06-07 00:57:05 +0200477 u64 fb_base, fb_size;
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100478 efi_status_t ret;
Alexander Graf792284f2016-06-05 22:34:31 +0200479 struct udevice *vdev;
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100480 struct video_priv *priv;
Simon Glass65ea46f2023-10-01 19:14:36 -0600481 struct video_uc_plat *plat;
Alexander Graf792284f2016-06-05 22:34:31 +0200482
483 /* We only support a single video output device for now */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200484 if (uclass_first_device_err(UCLASS_VIDEO, &vdev)) {
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100485 debug("WARNING: No video device\n");
486 return EFI_SUCCESS;
487 }
Alexander Graf792284f2016-06-05 22:34:31 +0200488
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100489 priv = dev_get_uclass_priv(vdev);
Alexander Graf792284f2016-06-05 22:34:31 +0200490 bpix = priv->bpix;
Mark Kettenis3e4b3d52021-09-25 22:47:37 +0200491 format = priv->format;
Alexander Graf792284f2016-06-05 22:34:31 +0200492 col = video_get_xsize(vdev);
493 row = video_get_ysize(vdev);
Simon Glass65ea46f2023-10-01 19:14:36 -0600494
495 plat = dev_get_uclass_plat(vdev);
496 fb_base = IS_ENABLED(CONFIG_VIDEO_COPY) ? plat->copy_base : plat->base;
497 fb_size = plat->size;
Alexander Graf792284f2016-06-05 22:34:31 +0200498
499 switch (bpix) {
Alexander Graf792284f2016-06-05 22:34:31 +0200500 case VIDEO_BPP16:
501 case VIDEO_BPP32:
Alexander Grafd65783d2016-03-15 18:38:21 +0100502 break;
503 default:
504 /* So far, we only work in 16 or 32 bit mode */
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100505 debug("WARNING: Unsupported video mode\n");
506 return EFI_SUCCESS;
Alexander Grafd65783d2016-03-15 18:38:21 +0100507 }
508
509 gopobj = calloc(1, sizeof(*gopobj));
Heinrich Schuchardt9aaa35b2017-10-26 19:25:45 +0200510 if (!gopobj) {
511 printf("ERROR: Out of memory\n");
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100512 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt9aaa35b2017-10-26 19:25:45 +0200513 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100514
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100515 /* Hook up to the device list */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200516 efi_add_handle(&gopobj->header);
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100517
Alexander Grafd65783d2016-03-15 18:38:21 +0100518 /* Fill in object data */
Heinrich Schuchardt72928722018-09-26 05:27:56 +0200519 ret = efi_add_protocol(&gopobj->header, &efi_gop_guid,
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100520 &gopobj->ops);
521 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt5e96f422018-10-18 21:51:38 +0200522 printf("ERROR: Failure adding GOP protocol\n");
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100523 return ret;
Heinrich Schuchardtbde9a9b2017-11-26 14:05:14 +0100524 }
Alexander Grafd65783d2016-03-15 18:38:21 +0100525 gopobj->ops.query_mode = gop_query_mode;
526 gopobj->ops.set_mode = gop_set_mode;
527 gopobj->ops.blt = gop_blt;
528 gopobj->ops.mode = &gopobj->mode;
529
530 gopobj->mode.max_mode = 1;
531 gopobj->mode.info = &gopobj->info;
532 gopobj->mode.info_size = sizeof(gopobj->info);
Alexander Grafd65783d2016-03-15 18:38:21 +0100533
Heinrich Schuchardt6ea1b292019-06-15 14:52:53 +0200534 gopobj->mode.fb_base = fb_base;
535 gopobj->mode.fb_size = fb_size;
536
537 gopobj->info.version = 0;
538 gopobj->info.width = col;
539 gopobj->info.height = row;
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100540 if (bpix == VIDEO_BPP32)
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100541 {
Mark Kettenis3e4b3d52021-09-25 22:47:37 +0200542 if (format == VIDEO_X2R10G10B10) {
543 gopobj->info.pixel_format = EFI_GOT_BITMASK;
544 gopobj->info.pixel_bitmask[0] = 0x3ff00000; /* red */
545 gopobj->info.pixel_bitmask[1] = 0x000ffc00; /* green */
546 gopobj->info.pixel_bitmask[2] = 0x000003ff; /* blue */
547 gopobj->info.pixel_bitmask[3] = 0xc0000000; /* reserved */
548 } else {
549 gopobj->info.pixel_format = EFI_GOT_BGRA8;
550 }
Heinrich Schuchardt6ea1b292019-06-15 14:52:53 +0200551 } else {
552 gopobj->info.pixel_format = EFI_GOT_BITMASK;
553 gopobj->info.pixel_bitmask[0] = 0xf800; /* red */
554 gopobj->info.pixel_bitmask[1] = 0x07e0; /* green */
555 gopobj->info.pixel_bitmask[2] = 0x001f; /* blue */
Alexander Grafbc766392016-06-07 00:57:05 +0200556 }
Alexander Graf792284f2016-06-05 22:34:31 +0200557 gopobj->info.pixels_per_scanline = col;
Alexander Graf792284f2016-06-05 22:34:31 +0200558 gopobj->bpix = bpix;
Simon Glass65ea46f2023-10-01 19:14:36 -0600559 gopobj->fb = map_sysmem(fb_base, fb_size);
Alexander Graf5e5e5d92022-06-10 00:59:19 +0200560 gopobj->vdev = vdev;
Alexander Grafd65783d2016-03-15 18:38:21 +0100561
Heinrich Schuchardtb91c0842018-03-03 15:28:55 +0100562 return EFI_SUCCESS;
Alexander Grafd65783d2016-03-15 18:38:21 +0100563}