]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/QemuVideoDxe/Qemu.h
fef648c967b2026f85d239ae51ccaf95a2f73259
[mirror_edk2.git] / OvmfPkg / QemuVideoDxe / Qemu.h
1 /** @file
2 QEMU Video Controller Driver
3
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 //
10 // QEMU Video Controller Driver
11 //
12
13 #ifndef _QEMU_H_
14 #define _QEMU_H_
15
16 #include <Uefi.h>
17 #include <Protocol/GraphicsOutput.h>
18 #include <Protocol/PciIo.h>
19 #include <Protocol/DriverSupportedEfiVersion.h>
20 #include <Protocol/DevicePath.h>
21
22 #include <Library/DebugLib.h>
23 #include <Library/UefiDriverEntryPoint.h>
24 #include <Library/UefiLib.h>
25 #include <Library/PcdLib.h>
26 #include <Library/MemoryAllocationLib.h>
27 #include <Library/UefiBootServicesTableLib.h>
28 #include <Library/BaseMemoryLib.h>
29 #include <Library/DevicePathLib.h>
30 #include <Library/TimerLib.h>
31 #include <Library/FrameBufferBltLib.h>
32
33 #include <IndustryStandard/Pci.h>
34 #include <IndustryStandard/Acpi.h>
35
36 //
37 // QEMU Video PCI Configuration Header values
38 //
39 #define CIRRUS_LOGIC_VENDOR_ID 0x1013
40 #define CIRRUS_LOGIC_5430_DEVICE_ID 0x00a8
41 #define CIRRUS_LOGIC_5430_ALTERNATE_DEVICE_ID 0x00a0
42 #define CIRRUS_LOGIC_5446_DEVICE_ID 0x00b8
43
44 //
45 // QEMU Vide Graphical Mode Data
46 //
47 typedef struct {
48 UINT32 InternalModeIndex; // points into card-specific mode table
49 UINT32 HorizontalResolution;
50 UINT32 VerticalResolution;
51 UINT32 ColorDepth;
52 } QEMU_VIDEO_MODE_DATA;
53
54 #define PIXEL_RED_SHIFT 0
55 #define PIXEL_GREEN_SHIFT 3
56 #define PIXEL_BLUE_SHIFT 6
57
58 #define PIXEL_RED_MASK (BIT7 | BIT6 | BIT5)
59 #define PIXEL_GREEN_MASK (BIT4 | BIT3 | BIT2)
60 #define PIXEL_BLUE_MASK (BIT1 | BIT0)
61
62 #define PIXEL_TO_COLOR_BYTE(pixel, mask, shift) ((UINT8) ((pixel & mask) << shift))
63 #define PIXEL_TO_RED_BYTE(pixel) PIXEL_TO_COLOR_BYTE(pixel, PIXEL_RED_MASK, PIXEL_RED_SHIFT)
64 #define PIXEL_TO_GREEN_BYTE(pixel) PIXEL_TO_COLOR_BYTE(pixel, PIXEL_GREEN_MASK, PIXEL_GREEN_SHIFT)
65 #define PIXEL_TO_BLUE_BYTE(pixel) PIXEL_TO_COLOR_BYTE(pixel, PIXEL_BLUE_MASK, PIXEL_BLUE_SHIFT)
66
67 #define RGB_BYTES_TO_PIXEL(Red, Green, Blue) \
68 (UINT8) ( (((Red) >> PIXEL_RED_SHIFT) & PIXEL_RED_MASK) | \
69 (((Green) >> PIXEL_GREEN_SHIFT) & PIXEL_GREEN_MASK) | \
70 (((Blue) >> PIXEL_BLUE_SHIFT) & PIXEL_BLUE_MASK) )
71
72 #define PIXEL24_RED_MASK 0x00ff0000
73 #define PIXEL24_GREEN_MASK 0x0000ff00
74 #define PIXEL24_BLUE_MASK 0x000000ff
75
76 #define GRAPHICS_OUTPUT_INVALIDE_MODE_NUMBER 0xffff
77
78 //
79 // QEMU Video Private Data Structure
80 //
81 #define QEMU_VIDEO_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('Q', 'V', 'I', 'D')
82
83 typedef enum {
84 QEMU_VIDEO_CIRRUS_5430 = 1,
85 QEMU_VIDEO_CIRRUS_5446,
86 QEMU_VIDEO_BOCHS,
87 QEMU_VIDEO_BOCHS_MMIO,
88 QEMU_VIDEO_VMWARE_SVGA,
89 } QEMU_VIDEO_VARIANT;
90
91 typedef struct {
92 UINT8 SubClass;
93 UINT16 VendorId;
94 UINT16 DeviceId;
95 QEMU_VIDEO_VARIANT Variant;
96 CHAR16 *Name;
97 } QEMU_VIDEO_CARD;
98
99 typedef struct {
100 UINT64 Signature;
101 EFI_HANDLE Handle;
102 EFI_PCI_IO_PROTOCOL *PciIo;
103 UINT64 OriginalPciAttributes;
104 EFI_GRAPHICS_OUTPUT_PROTOCOL GraphicsOutput;
105 EFI_DEVICE_PATH_PROTOCOL *GopDevicePath;
106
107 //
108 // The next two fields match the client-visible
109 // EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.MaxMode field.
110 //
111 UINTN MaxMode;
112 QEMU_VIDEO_MODE_DATA *ModeData;
113
114 QEMU_VIDEO_VARIANT Variant;
115 FRAME_BUFFER_CONFIGURE *FrameBufferBltConfigure;
116 UINTN FrameBufferBltConfigureSize;
117 UINT8 FrameBufferVramBarIndex;
118 } QEMU_VIDEO_PRIVATE_DATA;
119
120 ///
121 /// Card-specific Video Mode structures
122 ///
123 typedef struct {
124 UINT32 Width;
125 UINT32 Height;
126 UINT32 ColorDepth;
127 UINT8 *CrtcSettings;
128 UINT16 *SeqSettings;
129 UINT8 MiscSetting;
130 } QEMU_VIDEO_CIRRUS_MODES;
131
132 typedef struct {
133 UINT32 Width;
134 UINT32 Height;
135 UINT32 ColorDepth;
136 } QEMU_VIDEO_BOCHS_MODES;
137
138 #define QEMU_VIDEO_PRIVATE_DATA_FROM_GRAPHICS_OUTPUT_THIS(a) \
139 CR(a, QEMU_VIDEO_PRIVATE_DATA, GraphicsOutput, QEMU_VIDEO_PRIVATE_DATA_SIGNATURE)
140
141 //
142 // Global Variables
143 //
144 extern UINT8 AttributeController[];
145 extern UINT8 GraphicsController[];
146 extern UINT8 Crtc_640_480_256_60[];
147 extern UINT16 Seq_640_480_256_60[];
148 extern UINT8 Crtc_800_600_256_60[];
149 extern UINT16 Seq_800_600_256_60[];
150 extern UINT8 Crtc_1024_768_256_60[];
151 extern UINT16 Seq_1024_768_256_60[];
152 extern QEMU_VIDEO_CIRRUS_MODES QemuVideoCirrusModes[];
153 extern EFI_DRIVER_BINDING_PROTOCOL gQemuVideoDriverBinding;
154 extern EFI_COMPONENT_NAME_PROTOCOL gQemuVideoComponentName;
155 extern EFI_COMPONENT_NAME2_PROTOCOL gQemuVideoComponentName2;
156
157 //
158 // Io Registers defined by VGA
159 //
160 #define CRTC_ADDRESS_REGISTER 0x3d4
161 #define CRTC_DATA_REGISTER 0x3d5
162 #define SEQ_ADDRESS_REGISTER 0x3c4
163 #define SEQ_DATA_REGISTER 0x3c5
164 #define GRAPH_ADDRESS_REGISTER 0x3ce
165 #define GRAPH_DATA_REGISTER 0x3cf
166 #define ATT_ADDRESS_REGISTER 0x3c0
167 #define MISC_OUTPUT_REGISTER 0x3c2
168 #define INPUT_STATUS_1_REGISTER 0x3da
169 #define DAC_PIXEL_MASK_REGISTER 0x3c6
170 #define PALETTE_INDEX_REGISTER 0x3c8
171 #define PALETTE_DATA_REGISTER 0x3c9
172
173 #define VBE_DISPI_IOPORT_INDEX 0x01CE
174 #define VBE_DISPI_IOPORT_DATA 0x01D0
175
176 #define VBE_DISPI_INDEX_ID 0x0
177 #define VBE_DISPI_INDEX_XRES 0x1
178 #define VBE_DISPI_INDEX_YRES 0x2
179 #define VBE_DISPI_INDEX_BPP 0x3
180 #define VBE_DISPI_INDEX_ENABLE 0x4
181 #define VBE_DISPI_INDEX_BANK 0x5
182 #define VBE_DISPI_INDEX_VIRT_WIDTH 0x6
183 #define VBE_DISPI_INDEX_VIRT_HEIGHT 0x7
184 #define VBE_DISPI_INDEX_X_OFFSET 0x8
185 #define VBE_DISPI_INDEX_Y_OFFSET 0x9
186 #define VBE_DISPI_INDEX_VIDEO_MEMORY_64K 0xa
187
188 #define VBE_DISPI_ID0 0xB0C0
189 #define VBE_DISPI_ID1 0xB0C1
190 #define VBE_DISPI_ID2 0xB0C2
191 #define VBE_DISPI_ID3 0xB0C3
192 #define VBE_DISPI_ID4 0xB0C4
193 #define VBE_DISPI_ID5 0xB0C5
194
195 #define VBE_DISPI_DISABLED 0x00
196 #define VBE_DISPI_ENABLED 0x01
197 #define VBE_DISPI_GETCAPS 0x02
198 #define VBE_DISPI_8BIT_DAC 0x20
199 #define VBE_DISPI_LFB_ENABLED 0x40
200 #define VBE_DISPI_NOCLEARMEM 0x80
201
202 //
203 // Graphics Output Hardware abstraction internal worker functions
204 //
205 EFI_STATUS
206 QemuVideoGraphicsOutputConstructor (
207 QEMU_VIDEO_PRIVATE_DATA *Private
208 );
209
210 EFI_STATUS
211 QemuVideoGraphicsOutputDestructor (
212 QEMU_VIDEO_PRIVATE_DATA *Private
213 );
214
215 //
216 // EFI_DRIVER_BINDING_PROTOCOL Protocol Interface
217 //
218
219 /**
220 TODO: Add function description
221
222 @param This TODO: add argument description
223 @param Controller TODO: add argument description
224 @param RemainingDevicePath TODO: add argument description
225
226 TODO: add return values
227
228 **/
229 EFI_STATUS
230 EFIAPI
231 QemuVideoControllerDriverSupported (
232 IN EFI_DRIVER_BINDING_PROTOCOL *This,
233 IN EFI_HANDLE Controller,
234 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
235 );
236
237 /**
238 TODO: Add function description
239
240 @param This TODO: add argument description
241 @param Controller TODO: add argument description
242 @param RemainingDevicePath TODO: add argument description
243
244 TODO: add return values
245
246 **/
247 EFI_STATUS
248 EFIAPI
249 QemuVideoControllerDriverStart (
250 IN EFI_DRIVER_BINDING_PROTOCOL *This,
251 IN EFI_HANDLE Controller,
252 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
253 );
254
255 /**
256 TODO: Add function description
257
258 @param This TODO: add argument description
259 @param Controller TODO: add argument description
260 @param NumberOfChildren TODO: add argument description
261 @param ChildHandleBuffer TODO: add argument description
262
263 TODO: add return values
264
265 **/
266 EFI_STATUS
267 EFIAPI
268 QemuVideoControllerDriverStop (
269 IN EFI_DRIVER_BINDING_PROTOCOL *This,
270 IN EFI_HANDLE Controller,
271 IN UINTN NumberOfChildren,
272 IN EFI_HANDLE *ChildHandleBuffer
273 );
274
275 //
276 // EFI Component Name Functions
277 //
278
279 /**
280 Retrieves a Unicode string that is the user readable name of the driver.
281
282 This function retrieves the user readable name of a driver in the form of a
283 Unicode string. If the driver specified by This has a user readable name in
284 the language specified by Language, then a pointer to the driver name is
285 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
286 by This does not support the language specified by Language,
287 then EFI_UNSUPPORTED is returned.
288
289 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
290 EFI_COMPONENT_NAME_PROTOCOL instance.
291
292 @param Language[in] A pointer to a Null-terminated ASCII string
293 array indicating the language. This is the
294 language of the driver name that the caller is
295 requesting, and it must match one of the
296 languages specified in SupportedLanguages. The
297 number of languages supported by a driver is up
298 to the driver writer. Language is specified
299 in RFC 4646 or ISO 639-2 language code format.
300
301 @param DriverName[out] A pointer to the Unicode string to return.
302 This Unicode string is the name of the
303 driver specified by This in the language
304 specified by Language.
305
306 @retval EFI_SUCCESS The Unicode string for the Driver specified by
307 This and the language specified by Language was
308 returned in DriverName.
309
310 @retval EFI_INVALID_PARAMETER Language is NULL.
311
312 @retval EFI_INVALID_PARAMETER DriverName is NULL.
313
314 @retval EFI_UNSUPPORTED The driver specified by This does not support
315 the language specified by Language.
316
317 **/
318 EFI_STATUS
319 EFIAPI
320 QemuVideoComponentNameGetDriverName (
321 IN EFI_COMPONENT_NAME_PROTOCOL *This,
322 IN CHAR8 *Language,
323 OUT CHAR16 **DriverName
324 );
325
326 /**
327 Retrieves a Unicode string that is the user readable name of the controller
328 that is being managed by a driver.
329
330 This function retrieves the user readable name of the controller specified by
331 ControllerHandle and ChildHandle in the form of a Unicode string. If the
332 driver specified by This has a user readable name in the language specified by
333 Language, then a pointer to the controller name is returned in ControllerName,
334 and EFI_SUCCESS is returned. If the driver specified by This is not currently
335 managing the controller specified by ControllerHandle and ChildHandle,
336 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
337 support the language specified by Language, then EFI_UNSUPPORTED is returned.
338
339 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
340 EFI_COMPONENT_NAME_PROTOCOL instance.
341
342 @param ControllerHandle[in] The handle of a controller that the driver
343 specified by This is managing. This handle
344 specifies the controller whose name is to be
345 returned.
346
347 @param ChildHandle[in] The handle of the child controller to retrieve
348 the name of. This is an optional parameter that
349 may be NULL. It will be NULL for device
350 drivers. It will also be NULL for a bus drivers
351 that wish to retrieve the name of the bus
352 controller. It will not be NULL for a bus
353 driver that wishes to retrieve the name of a
354 child controller.
355
356 @param Language[in] A pointer to a Null-terminated ASCII string
357 array indicating the language. This is the
358 language of the driver name that the caller is
359 requesting, and it must match one of the
360 languages specified in SupportedLanguages. The
361 number of languages supported by a driver is up
362 to the driver writer. Language is specified in
363 RFC 4646 or ISO 639-2 language code format.
364
365 @param ControllerName[out] A pointer to the Unicode string to return.
366 This Unicode string is the name of the
367 controller specified by ControllerHandle and
368 ChildHandle in the language specified by
369 Language from the point of view of the driver
370 specified by This.
371
372 @retval EFI_SUCCESS The Unicode string for the user readable name in
373 the language specified by Language for the
374 driver specified by This was returned in
375 DriverName.
376
377 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
378
379 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
380 EFI_HANDLE.
381
382 @retval EFI_INVALID_PARAMETER Language is NULL.
383
384 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
385
386 @retval EFI_UNSUPPORTED The driver specified by This is not currently
387 managing the controller specified by
388 ControllerHandle and ChildHandle.
389
390 @retval EFI_UNSUPPORTED The driver specified by This does not support
391 the language specified by Language.
392
393 **/
394 EFI_STATUS
395 EFIAPI
396 QemuVideoComponentNameGetControllerName (
397 IN EFI_COMPONENT_NAME_PROTOCOL *This,
398 IN EFI_HANDLE ControllerHandle,
399 IN EFI_HANDLE ChildHandle OPTIONAL,
400 IN CHAR8 *Language,
401 OUT CHAR16 **ControllerName
402 );
403
404 //
405 // Local Function Prototypes
406 //
407 VOID
408 InitializeCirrusGraphicsMode (
409 QEMU_VIDEO_PRIVATE_DATA *Private,
410 QEMU_VIDEO_CIRRUS_MODES *ModeData
411 );
412
413 VOID
414 InitializeBochsGraphicsMode (
415 QEMU_VIDEO_PRIVATE_DATA *Private,
416 QEMU_VIDEO_MODE_DATA *ModeData
417 );
418
419 VOID
420 SetPaletteColor (
421 QEMU_VIDEO_PRIVATE_DATA *Private,
422 UINTN Index,
423 UINT8 Red,
424 UINT8 Green,
425 UINT8 Blue
426 );
427
428 VOID
429 SetDefaultPalette (
430 QEMU_VIDEO_PRIVATE_DATA *Private
431 );
432
433 VOID
434 DrawLogo (
435 QEMU_VIDEO_PRIVATE_DATA *Private,
436 UINTN ScreenWidth,
437 UINTN ScreenHeight
438 );
439
440 VOID
441 outb (
442 QEMU_VIDEO_PRIVATE_DATA *Private,
443 UINTN Address,
444 UINT8 Data
445 );
446
447 VOID
448 outw (
449 QEMU_VIDEO_PRIVATE_DATA *Private,
450 UINTN Address,
451 UINT16 Data
452 );
453
454 UINT8
455 inb (
456 QEMU_VIDEO_PRIVATE_DATA *Private,
457 UINTN Address
458 );
459
460 UINT16
461 inw (
462 QEMU_VIDEO_PRIVATE_DATA *Private,
463 UINTN Address
464 );
465
466 VOID
467 BochsWrite (
468 QEMU_VIDEO_PRIVATE_DATA *Private,
469 UINT16 Reg,
470 UINT16 Data
471 );
472
473 UINT16
474 BochsRead (
475 QEMU_VIDEO_PRIVATE_DATA *Private,
476 UINT16 Reg
477 );
478
479 VOID
480 VgaOutb (
481 QEMU_VIDEO_PRIVATE_DATA *Private,
482 UINTN Reg,
483 UINT8 Data
484 );
485
486 EFI_STATUS
487 QemuVideoCirrusModeSetup (
488 QEMU_VIDEO_PRIVATE_DATA *Private
489 );
490
491 EFI_STATUS
492 QemuVideoBochsModeSetup (
493 QEMU_VIDEO_PRIVATE_DATA *Private,
494 BOOLEAN IsQxl
495 );
496
497 VOID
498 InstallVbeShim (
499 IN CONST CHAR16 *CardName,
500 IN EFI_PHYSICAL_ADDRESS FrameBufferBase
501 );
502
503 #endif