]> git.proxmox.com Git - mirror_edk2.git/blob - ArmPlatformPkg/Drivers/LcdGraphicsOutputDxe/LcdGraphicsOutputDxe.c
ArmPlatformPkg: Tidy Lcd code: Updated comments
[mirror_edk2.git] / ArmPlatformPkg / Drivers / LcdGraphicsOutputDxe / LcdGraphicsOutputDxe.c
1 /** @file
2 This file implements the Graphics Output protocol for Arm platforms
3
4 Copyright (c) 2011-2018, ARM Ltd. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include <PiDxe.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/DevicePathLib.h>
18 #include <Library/UefiBootServicesTableLib.h>
19 #include <Library/UefiRuntimeServicesTableLib.h>
20 #include <Library/MemoryAllocationLib.h>
21
22 #include <Guid/GlobalVariable.h>
23
24 #include "LcdGraphicsOutputDxe.h"
25
26 //
27 // Global variables
28 //
29
30 BOOLEAN mDisplayInitialized = FALSE;
31
32 LCD_INSTANCE mLcdTemplate = {
33 LCD_INSTANCE_SIGNATURE,
34 NULL, // Handle
35 { // ModeInfo
36 0, // Version
37 0, // HorizontalResolution
38 0, // VerticalResolution
39 PixelBltOnly, // PixelFormat
40 { 0 }, // PixelInformation
41 0, // PixelsPerScanLine
42 },
43 {
44 0, // MaxMode;
45 0, // Mode;
46 NULL, // Info;
47 0, // SizeOfInfo;
48 0, // FrameBufferBase;
49 0 // FrameBufferSize;
50 },
51 { // Gop
52 LcdGraphicsQueryMode, // QueryMode
53 LcdGraphicsSetMode, // SetMode
54 LcdGraphicsBlt, // Blt
55 NULL // *Mode
56 },
57 { // DevicePath
58 {
59 {
60 HARDWARE_DEVICE_PATH, HW_VENDOR_DP,
61 {
62 (UINT8)(sizeof (VENDOR_DEVICE_PATH)),
63 (UINT8)((sizeof (VENDOR_DEVICE_PATH)) >> 8)
64 },
65 },
66 // Hardware Device Path for Lcd
67 EFI_CALLER_ID_GUID // Use the driver's GUID
68 },
69
70 {
71 END_DEVICE_PATH_TYPE,
72 END_ENTIRE_DEVICE_PATH_SUBTYPE,
73 {
74 sizeof (EFI_DEVICE_PATH_PROTOCOL),
75 0
76 }
77 }
78 },
79 (EFI_EVENT)NULL // ExitBootServicesEvent
80 };
81
82 EFI_STATUS
83 LcdInstanceContructor (
84 OUT LCD_INSTANCE** NewInstance
85 )
86 {
87 LCD_INSTANCE* Instance;
88
89 Instance = AllocateCopyPool (sizeof (LCD_INSTANCE), &mLcdTemplate);
90 if (Instance == NULL) {
91 return EFI_OUT_OF_RESOURCES;
92 }
93
94 Instance->Gop.Mode = &Instance->Mode;
95 Instance->Gop.Mode->MaxMode = LcdPlatformGetMaxMode ();
96 Instance->Mode.Info = &Instance->ModeInfo;
97
98 *NewInstance = Instance;
99 return EFI_SUCCESS;
100 }
101
102 //
103 // Function Definitions
104 //
105
106 EFI_STATUS
107 InitializeDisplay (
108 IN LCD_INSTANCE* Instance
109 )
110 {
111 EFI_STATUS Status = EFI_SUCCESS;
112 EFI_PHYSICAL_ADDRESS VramBaseAddress;
113 UINTN VramSize;
114
115 Status = LcdPlatformGetVram (&VramBaseAddress, &VramSize);
116 if (EFI_ERROR (Status)) {
117 return Status;
118 }
119
120 // Setup the LCD
121 Status = LcdInitialize (VramBaseAddress);
122 if (EFI_ERROR (Status)) {
123 goto EXIT_ERROR_LCD_SHUTDOWN;
124 }
125
126 Status = LcdPlatformInitializeDisplay (Instance->Handle);
127 if (EFI_ERROR (Status)) {
128 goto EXIT_ERROR_LCD_SHUTDOWN;
129 }
130
131 // Setup all the relevant mode information
132 Instance->Gop.Mode->SizeOfInfo = sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);
133 Instance->Gop.Mode->FrameBufferBase = VramBaseAddress;
134
135 // Set the flag before changing the mode, to avoid infinite loops
136 mDisplayInitialized = TRUE;
137
138 // All is ok, so don't deal with any errors
139 goto EXIT;
140
141 EXIT_ERROR_LCD_SHUTDOWN:
142 DEBUG ((DEBUG_ERROR, "InitializeDisplay: ERROR - Can not initialise the display. Exit Status=%r\n", Status));
143
144 LcdShutdown ();
145
146 EXIT:
147 return Status;
148 }
149
150 EFI_STATUS
151 EFIAPI
152 LcdGraphicsOutputDxeInitialize (
153 IN EFI_HANDLE ImageHandle,
154 IN EFI_SYSTEM_TABLE *SystemTable
155 )
156 {
157 EFI_STATUS Status = EFI_SUCCESS;
158 LCD_INSTANCE* Instance;
159
160 Status = LcdIdentify ();
161 if (EFI_ERROR (Status)) {
162 goto EXIT;
163 }
164
165 Status = LcdInstanceContructor (&Instance);
166 if (EFI_ERROR (Status)) {
167 goto EXIT;
168 }
169
170 // Install the Graphics Output Protocol and the Device Path
171 Status = gBS->InstallMultipleProtocolInterfaces (
172 &Instance->Handle,
173 &gEfiGraphicsOutputProtocolGuid,
174 &Instance->Gop,
175 &gEfiDevicePathProtocolGuid,
176 &Instance->DevicePath,
177 NULL
178 );
179
180 if (EFI_ERROR (Status)) {
181 DEBUG ((DEBUG_ERROR, "LcdGraphicsOutputDxeInitialize: Can not install the protocol. Exit Status=%r\n", Status));
182 goto EXIT;
183 }
184
185 // Register for an ExitBootServicesEvent
186 // When ExitBootServices starts, this function will make sure that the
187 // graphics driver shuts down properly, i.e. it will free up all
188 // allocated memory and perform any necessary hardware re-configuration.
189 Status = gBS->CreateEvent (
190 EVT_SIGNAL_EXIT_BOOT_SERVICES,
191 TPL_NOTIFY,
192 LcdGraphicsExitBootServicesEvent,
193 NULL,
194 &Instance->ExitBootServicesEvent
195 );
196
197 if (EFI_ERROR (Status)) {
198 DEBUG ((DEBUG_ERROR, "LcdGraphicsOutputDxeInitialize: Can not install the ExitBootServicesEvent handler. Exit Status=%r\n", Status));
199 goto EXIT_ERROR_UNINSTALL_PROTOCOL;
200 }
201
202 // To get here, everything must be fine, so just exit
203 goto EXIT;
204
205 EXIT_ERROR_UNINSTALL_PROTOCOL:
206 // The following function could return an error message,
207 // however, to get here something must have gone wrong already,
208 // so preserve the original error, i.e. don't change
209 // the Status variable, even it fails to uninstall the protocol.
210 gBS->UninstallMultipleProtocolInterfaces (
211 Instance->Handle,
212 &gEfiGraphicsOutputProtocolGuid,
213 &Instance->Gop, // Uninstall Graphics Output protocol
214 &gEfiDevicePathProtocolGuid,
215 &Instance->DevicePath, // Uninstall device path
216 NULL
217 );
218
219 EXIT:
220 return Status;
221 }
222
223 /** This function should be called
224 on Event: ExitBootServices
225 to free up memory, stop the driver
226 and uninstall the protocols
227 **/
228 VOID
229 LcdGraphicsExitBootServicesEvent (
230 IN EFI_EVENT Event,
231 IN VOID *Context
232 )
233 {
234 // By default, this PCD is FALSE. But if a platform starts a predefined OS
235 // that does not use a framebuffer then we might want to disable the display
236 // controller to avoid to display corrupted information on the screen.
237 if (FeaturePcdGet (PcdGopDisableOnExitBootServices)) {
238 // Turn-off the Display controller
239 LcdShutdown ();
240 }
241 }
242
243 /** GraphicsOutput Protocol function, mapping to
244 EFI_GRAPHICS_OUTPUT_PROTOCOL.QueryMode
245 **/
246 EFI_STATUS
247 EFIAPI
248 LcdGraphicsQueryMode (
249 IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
250 IN UINT32 ModeNumber,
251 OUT UINTN *SizeOfInfo,
252 OUT EFI_GRAPHICS_OUTPUT_MODE_INFORMATION **Info
253 )
254 {
255 EFI_STATUS Status = EFI_SUCCESS;
256 LCD_INSTANCE *Instance;
257
258 Instance = LCD_INSTANCE_FROM_GOP_THIS (This);
259
260 // Setup the hardware if not already done
261 if (!mDisplayInitialized) {
262 Status = InitializeDisplay (Instance);
263 if (EFI_ERROR (Status)) {
264 goto EXIT;
265 }
266 }
267
268 // Error checking
269 if ((This == NULL) ||
270 (Info == NULL) ||
271 (SizeOfInfo == NULL) ||
272 (ModeNumber >= This->Mode->MaxMode)) {
273 DEBUG ((DEBUG_ERROR, "LcdGraphicsQueryMode: ERROR - For mode number %d : Invalid Parameter.\n", ModeNumber));
274 Status = EFI_INVALID_PARAMETER;
275 goto EXIT;
276 }
277
278 *Info = AllocatePool (sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION));
279 if (*Info == NULL) {
280 Status = EFI_OUT_OF_RESOURCES;
281 goto EXIT;
282 }
283
284 *SizeOfInfo = sizeof (EFI_GRAPHICS_OUTPUT_MODE_INFORMATION);
285
286 Status = LcdPlatformQueryMode (ModeNumber, *Info);
287 if (EFI_ERROR (Status)) {
288 FreePool (*Info);
289 }
290
291 EXIT:
292 return Status;
293 }
294
295 /** GraphicsOutput Protocol function, mapping to
296 EFI_GRAPHICS_OUTPUT_PROTOCOL.SetMode
297 **/
298 EFI_STATUS
299 EFIAPI
300 LcdGraphicsSetMode (
301 IN EFI_GRAPHICS_OUTPUT_PROTOCOL *This,
302 IN UINT32 ModeNumber
303 )
304 {
305 EFI_STATUS Status = EFI_SUCCESS;
306 EFI_GRAPHICS_OUTPUT_BLT_PIXEL FillColour;
307 LCD_INSTANCE* Instance;
308 LCD_BPP Bpp;
309
310 Instance = LCD_INSTANCE_FROM_GOP_THIS (This);
311
312 // Setup the hardware if not already done
313 if (!mDisplayInitialized) {
314 Status = InitializeDisplay (Instance);
315 if (EFI_ERROR (Status)) {
316 goto EXIT;
317 }
318 }
319
320 // Check if this mode is supported
321 if (ModeNumber >= This->Mode->MaxMode) {
322 DEBUG ((DEBUG_ERROR, "LcdGraphicsSetMode: ERROR - Unsupported mode number %d .\n", ModeNumber));
323 Status = EFI_UNSUPPORTED;
324 goto EXIT;
325 }
326
327 // Set the oscillator frequency to support the new mode
328 Status = LcdPlatformSetMode (ModeNumber);
329 if (EFI_ERROR (Status)) {
330 Status = EFI_DEVICE_ERROR;
331 goto EXIT;
332 }
333
334 // Update the UEFI mode information
335 This->Mode->Mode = ModeNumber;
336 LcdPlatformQueryMode (ModeNumber, &Instance->ModeInfo);
337 Status = LcdPlatformGetBpp (ModeNumber, &Bpp);
338 if (EFI_ERROR (Status)) {
339 DEBUG ((DEBUG_ERROR, "LcdGraphicsSetMode: ERROR - Couldn't get bytes per pixel, status: %r\n", Status));
340 goto EXIT;
341 }
342 This->Mode->FrameBufferSize = Instance->ModeInfo.VerticalResolution
343 * Instance->ModeInfo.PixelsPerScanLine
344 * GetBytesPerPixel (Bpp);
345
346 // Set the hardware to the new mode
347 Status = LcdSetMode (ModeNumber);
348 if (EFI_ERROR (Status)) {
349 Status = EFI_DEVICE_ERROR;
350 goto EXIT;
351 }
352
353 // The UEFI spec requires that we now clear the visible portions of the
354 // output display to black.
355
356 // Set the fill colour to black
357 SetMem (&FillColour, sizeof (EFI_GRAPHICS_OUTPUT_BLT_PIXEL), 0x0);
358
359 // Fill the entire visible area with the same colour.
360 Status = This->Blt (
361 This,
362 &FillColour,
363 EfiBltVideoFill,
364 0,
365 0,
366 0,
367 0,
368 This->Mode->Info->HorizontalResolution,
369 This->Mode->Info->VerticalResolution,
370 0
371 );
372
373 EXIT:
374 return Status;
375 }
376
377 UINTN
378 GetBytesPerPixel (
379 IN LCD_BPP Bpp
380 )
381 {
382 switch (Bpp) {
383 case LCD_BITS_PER_PIXEL_24:
384 return 4;
385
386 case LCD_BITS_PER_PIXEL_16_565:
387 case LCD_BITS_PER_PIXEL_16_555:
388 case LCD_BITS_PER_PIXEL_12_444:
389 return 2;
390
391 case LCD_BITS_PER_PIXEL_8:
392 case LCD_BITS_PER_PIXEL_4:
393 case LCD_BITS_PER_PIXEL_2:
394 case LCD_BITS_PER_PIXEL_1:
395 return 1;
396
397 default:
398 return 0;
399 }
400 }