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