blob: 154f306540601d55933dbdff5f2f74672d214736 [file] [log] [blame]
Alexander Grafbe8d3242016-03-15 18:38:21 +01001/*
2 * EFI application disk support
3 *
4 * Copyright (c) 2016 Alexander Graf
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
Alexander Grafa8122412016-06-05 22:34:31 +020010#include <dm.h>
Alexander Grafbe8d3242016-03-15 18:38:21 +010011#include <efi_loader.h>
12#include <inttypes.h>
13#include <lcd.h>
14#include <malloc.h>
Alexander Grafa8122412016-06-05 22:34:31 +020015#include <video.h>
Alexander Grafbe8d3242016-03-15 18:38:21 +010016
17DECLARE_GLOBAL_DATA_PTR;
18
19static const efi_guid_t efi_gop_guid = EFI_GOP_GUID;
20
21struct efi_gop_obj {
22 /* Generic EFI object parent class data */
23 struct efi_object parent;
24 /* EFI Interface callback struct for gop */
25 struct efi_gop ops;
26 /* The only mode we support */
27 struct efi_gop_mode_info info;
28 struct efi_gop_mode mode;
Alexander Grafa8122412016-06-05 22:34:31 +020029 /* Fields we only have acces to during init */
30 u32 bpix;
Rob Clarkca9193d2017-07-21 15:00:27 -040031 void *fb;
Alexander Grafbe8d3242016-03-15 18:38:21 +010032};
33
34static efi_status_t EFIAPI gop_query_mode(struct efi_gop *this, u32 mode_number,
Heinrich Schuchardt1c38a772017-10-26 19:25:51 +020035 efi_uintn_t *size_of_info,
Alexander Grafbe8d3242016-03-15 18:38:21 +010036 struct efi_gop_mode_info **info)
37{
38 struct efi_gop_obj *gopobj;
39
40 EFI_ENTRY("%p, %x, %p, %p", this, mode_number, size_of_info, info);
41
42 gopobj = container_of(this, struct efi_gop_obj, ops);
43 *size_of_info = sizeof(gopobj->info);
44 *info = &gopobj->info;
45
46 return EFI_EXIT(EFI_SUCCESS);
47}
48
49static efi_status_t EFIAPI gop_set_mode(struct efi_gop *this, u32 mode_number)
50{
51 EFI_ENTRY("%p, %x", this, mode_number);
52
53 if (mode_number != 0)
54 return EFI_EXIT(EFI_INVALID_PARAMETER);
55
56 return EFI_EXIT(EFI_SUCCESS);
57}
58
Heinrich Schuchardt0e0a3ce2018-02-07 22:14:22 +010059static inline struct efi_gop_pixel efi_vid16_to_blt_col(u16 vid)
60{
61 struct efi_gop_pixel blt = {
62 .reserved = 0,
63 };
64
65 blt.blue = (vid & 0x1f) << 3;
66 vid >>= 5;
67 blt.green = (vid & 0x3f) << 2;
68 vid >>= 6;
69 blt.red = (vid & 0x1f) << 3;
70 return blt;
71}
72
73static inline u16 efi_blt_col_to_vid16(struct efi_gop_pixel *blt)
74{
75 return (u16)(blt->red >> 3) << 11 |
76 (u16)(blt->green >> 2) << 5 |
77 (u16)(blt->blue >> 3);
78}
79
80efi_status_t EFIAPI gop_blt(struct efi_gop *this, struct efi_gop_pixel *buffer,
Heinrich Schuchardt1c38a772017-10-26 19:25:51 +020081 u32 operation, efi_uintn_t sx,
82 efi_uintn_t sy, efi_uintn_t dx,
83 efi_uintn_t dy, efi_uintn_t width,
84 efi_uintn_t height, efi_uintn_t delta)
Alexander Grafbe8d3242016-03-15 18:38:21 +010085{
Alexander Grafa8122412016-06-05 22:34:31 +020086 struct efi_gop_obj *gopobj = container_of(this, struct efi_gop_obj, ops);
Heinrich Schuchardt0e0a3ce2018-02-07 22:14:22 +010087 efi_uintn_t i, j, linelen;
88 u32 *fb32 = gopobj->fb;
89 u16 *fb16 = gopobj->fb;
90
91 if (delta)
92 linelen = delta;
93 else
94 linelen = width;
Alexander Grafbe8d3242016-03-15 18:38:21 +010095
Heinrich Schuchardt1c38a772017-10-26 19:25:51 +020096 EFI_ENTRY("%p, %p, %u, %zu, %zu, %zu, %zu, %zu, %zu, %zu", this,
Alexander Grafbe8d3242016-03-15 18:38:21 +010097 buffer, operation, sx, sy, dx, dy, width, height, delta);
98
Heinrich Schuchardt0e0a3ce2018-02-07 22:14:22 +010099 /* Check source rectangle */
100 switch (operation) {
101 case EFI_BLT_VIDEO_FILL:
Alexander Grafbe8d3242016-03-15 18:38:21 +0100102 break;
Heinrich Schuchardt0e0a3ce2018-02-07 22:14:22 +0100103 case EFI_BLT_BUFFER_TO_VIDEO:
104 if (sx + width > linelen)
105 return EFI_EXIT(EFI_INVALID_PARAMETER);
106 break;
107 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
108 case EFI_BLT_VIDEO_TO_VIDEO:
109 if (sx + width > gopobj->info.width ||
110 sy + height > gopobj->info.height)
111 return EFI_EXIT(EFI_INVALID_PARAMETER);
112 break;
113 default:
114 return EFI_EXIT(EFI_INVALID_PARAMETER);
115 }
Alexander Grafbe8d3242016-03-15 18:38:21 +0100116
Heinrich Schuchardt0e0a3ce2018-02-07 22:14:22 +0100117 /* Check destination rectangle */
118 switch (operation) {
119 case EFI_BLT_VIDEO_FILL:
120 case EFI_BLT_BUFFER_TO_VIDEO:
121 case EFI_BLT_VIDEO_TO_VIDEO:
122 if (dx + width > gopobj->info.width ||
123 dy + height > gopobj->info.height)
124 return EFI_EXIT(EFI_INVALID_PARAMETER);
125 break;
126 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
127 if (dx + width > linelen)
128 return EFI_EXIT(EFI_INVALID_PARAMETER);
129 break;
130 }
131
132 for (i = 0; i < height; i++) {
133 for (j = 0; j < width; j++) {
134 struct efi_gop_pixel pix;
135
136 /* Read source pixel */
137 switch (operation) {
138 case EFI_BLT_VIDEO_FILL:
139 pix = *buffer;
140 break;
141 case EFI_BLT_BUFFER_TO_VIDEO:
142 pix = buffer[linelen * (i + sy) + j + sx];
143 break;
144 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
145 case EFI_BLT_VIDEO_TO_VIDEO:
146 switch (gopobj->bpix) {
147#ifdef CONFIG_DM_VIDEO
148 case VIDEO_BPP32:
149#else
150 case LCD_COLOR32:
151#endif
152 pix = *(struct efi_gop_pixel *)&fb32[
153 gopobj->info.width *
154 (i + sy) + j + sx];
155 break;
156#ifdef CONFIG_DM_VIDEO
157 case VIDEO_BPP16:
158#else
159 case LCD_COLOR16:
160#endif
161 pix = efi_vid16_to_blt_col(fb16[
162 gopobj->info.width *
163 (i + sy) + j + sx]);
164 break;
165 default:
166 return EFI_EXIT(EFI_UNSUPPORTED);
167 }
168 break;
169 }
170
171 /* Write destination pixel */
172 switch (operation) {
173 case EFI_BLT_VIDEO_TO_BLT_BUFFER:
174 buffer[linelen * (i + dy) + j + dx] = pix;
175 break;
176 case EFI_BLT_BUFFER_TO_VIDEO:
177 case EFI_BLT_VIDEO_FILL:
178 case EFI_BLT_VIDEO_TO_VIDEO:
179 switch (gopobj->bpix) {
180#ifdef CONFIG_DM_VIDEO
181 case VIDEO_BPP32:
182#else
183 case LCD_COLOR32:
184#endif
185 fb32[gopobj->info.width *
186 (i + dy) + j + dx] = *(u32 *)&pix;
187 break;
188#ifdef CONFIG_DM_VIDEO
189 case VIDEO_BPP16:
190#else
191 case LCD_COLOR16:
192#endif
193 fb16[gopobj->info.width *
194 (i + dy) + j + dx] =
195 efi_blt_col_to_vid16(&pix);
196 break;
197 default:
198 return EFI_EXIT(EFI_UNSUPPORTED);
199 }
200 break;
Alexander Grafbe8d3242016-03-15 18:38:21 +0100201 }
202 }
Alexander Grafbe8d3242016-03-15 18:38:21 +0100203 }
204
Alexander Grafa8122412016-06-05 22:34:31 +0200205#ifdef CONFIG_DM_VIDEO
206 video_sync_all();
207#else
Alexander Grafbe8d3242016-03-15 18:38:21 +0100208 lcd_sync();
Alexander Grafa8122412016-06-05 22:34:31 +0200209#endif
Alexander Grafbe8d3242016-03-15 18:38:21 +0100210
211 return EFI_EXIT(EFI_SUCCESS);
212}
213
Heinrich Schuchardt80ea9b02018-03-03 15:28:55 +0100214/*
215 * Install graphical output protocol.
216 *
217 * If no supported video device exists this is not considered as an
218 * error.
219 */
220efi_status_t efi_gop_register(void)
Alexander Grafbe8d3242016-03-15 18:38:21 +0100221{
222 struct efi_gop_obj *gopobj;
Alexander Grafa8122412016-06-05 22:34:31 +0200223 u32 bpix, col, row;
Alexander Graf8f661a52016-06-07 00:57:05 +0200224 u64 fb_base, fb_size;
Rob Clarkca9193d2017-07-21 15:00:27 -0400225 void *fb;
Heinrich Schuchardt94493582017-11-26 14:05:14 +0100226 efi_status_t ret;
Alexander Grafbe8d3242016-03-15 18:38:21 +0100227
Alexander Grafa8122412016-06-05 22:34:31 +0200228#ifdef CONFIG_DM_VIDEO
229 struct udevice *vdev;
Heinrich Schuchardt80ea9b02018-03-03 15:28:55 +0100230 struct video_priv *priv;
Alexander Grafa8122412016-06-05 22:34:31 +0200231
232 /* We only support a single video output device for now */
Heinrich Schuchardt80ea9b02018-03-03 15:28:55 +0100233 if (uclass_first_device(UCLASS_VIDEO, &vdev) || !vdev) {
234 debug("WARNING: No video device\n");
235 return EFI_SUCCESS;
236 }
Alexander Grafa8122412016-06-05 22:34:31 +0200237
Heinrich Schuchardt80ea9b02018-03-03 15:28:55 +0100238 priv = dev_get_uclass_priv(vdev);
Alexander Grafa8122412016-06-05 22:34:31 +0200239 bpix = priv->bpix;
240 col = video_get_xsize(vdev);
241 row = video_get_ysize(vdev);
Alexander Graf8f661a52016-06-07 00:57:05 +0200242 fb_base = (uintptr_t)priv->fb;
243 fb_size = priv->fb_size;
Rob Clarkca9193d2017-07-21 15:00:27 -0400244 fb = priv->fb;
Alexander Grafa8122412016-06-05 22:34:31 +0200245#else
Alexander Graf8f661a52016-06-07 00:57:05 +0200246 int line_len;
Alexander Grafa8122412016-06-05 22:34:31 +0200247
248 bpix = panel_info.vl_bpix;
249 col = panel_info.vl_col;
250 row = panel_info.vl_row;
Alexander Graf8f661a52016-06-07 00:57:05 +0200251 fb_base = gd->fb_base;
252 fb_size = lcd_get_size(&line_len);
Alexander Grafc1ae1a12017-07-31 09:15:57 +0200253 fb = (void*)gd->fb_base;
Alexander Grafa8122412016-06-05 22:34:31 +0200254#endif
255
256 switch (bpix) {
257#ifdef CONFIG_DM_VIDEO
258 case VIDEO_BPP16:
259 case VIDEO_BPP32:
260#else
Alexander Grafbe8d3242016-03-15 18:38:21 +0100261 case LCD_COLOR32:
262 case LCD_COLOR16:
Alexander Grafa8122412016-06-05 22:34:31 +0200263#endif
Alexander Grafbe8d3242016-03-15 18:38:21 +0100264 break;
265 default:
266 /* So far, we only work in 16 or 32 bit mode */
Heinrich Schuchardt80ea9b02018-03-03 15:28:55 +0100267 debug("WARNING: Unsupported video mode\n");
268 return EFI_SUCCESS;
Alexander Grafbe8d3242016-03-15 18:38:21 +0100269 }
270
271 gopobj = calloc(1, sizeof(*gopobj));
Heinrich Schuchardt753edb12017-10-26 19:25:45 +0200272 if (!gopobj) {
273 printf("ERROR: Out of memory\n");
Heinrich Schuchardt80ea9b02018-03-03 15:28:55 +0100274 return EFI_OUT_OF_RESOURCES;
Heinrich Schuchardt753edb12017-10-26 19:25:45 +0200275 }
Alexander Grafbe8d3242016-03-15 18:38:21 +0100276
Heinrich Schuchardt94493582017-11-26 14:05:14 +0100277 /* Hook up to the device list */
Heinrich Schuchardt44549d62017-11-26 14:05:23 +0100278 efi_add_handle(&gopobj->parent);
Heinrich Schuchardt94493582017-11-26 14:05:14 +0100279
Alexander Grafbe8d3242016-03-15 18:38:21 +0100280 /* Fill in object data */
Heinrich Schuchardt94493582017-11-26 14:05:14 +0100281 ret = efi_add_protocol(gopobj->parent.handle, &efi_gop_guid,
282 &gopobj->ops);
283 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt80ea9b02018-03-03 15:28:55 +0100284 printf("ERROR: Failure adding gop protocol\n");
285 return ret;
Heinrich Schuchardt94493582017-11-26 14:05:14 +0100286 }
Alexander Grafbe8d3242016-03-15 18:38:21 +0100287 gopobj->ops.query_mode = gop_query_mode;
288 gopobj->ops.set_mode = gop_set_mode;
289 gopobj->ops.blt = gop_blt;
290 gopobj->ops.mode = &gopobj->mode;
291
292 gopobj->mode.max_mode = 1;
293 gopobj->mode.info = &gopobj->info;
294 gopobj->mode.info_size = sizeof(gopobj->info);
Alexander Grafbe8d3242016-03-15 18:38:21 +0100295
Alexander Graf8f661a52016-06-07 00:57:05 +0200296#ifdef CONFIG_DM_VIDEO
Heinrich Schuchardt80ea9b02018-03-03 15:28:55 +0100297 if (bpix == VIDEO_BPP32)
Alexander Graf8f661a52016-06-07 00:57:05 +0200298#else
Heinrich Schuchardt80ea9b02018-03-03 15:28:55 +0100299 if (bpix == LCD_COLOR32)
Alexander Graf8f661a52016-06-07 00:57:05 +0200300#endif
Heinrich Schuchardt80ea9b02018-03-03 15:28:55 +0100301 {
Alexander Graf8f661a52016-06-07 00:57:05 +0200302 /* With 32bit color space we can directly expose the fb */
303 gopobj->mode.fb_base = fb_base;
304 gopobj->mode.fb_size = fb_size;
305 }
306
Alexander Grafbe8d3242016-03-15 18:38:21 +0100307 gopobj->info.version = 0;
Alexander Grafa8122412016-06-05 22:34:31 +0200308 gopobj->info.width = col;
309 gopobj->info.height = row;
Alexander Grafbe8d3242016-03-15 18:38:21 +0100310 gopobj->info.pixel_format = EFI_GOT_RGBA8;
Alexander Grafa8122412016-06-05 22:34:31 +0200311 gopobj->info.pixels_per_scanline = col;
312
313 gopobj->bpix = bpix;
Rob Clarkca9193d2017-07-21 15:00:27 -0400314 gopobj->fb = fb;
Alexander Grafbe8d3242016-03-15 18:38:21 +0100315
Heinrich Schuchardt80ea9b02018-03-03 15:28:55 +0100316 return EFI_SUCCESS;
Alexander Grafbe8d3242016-03-15 18:38:21 +0100317}