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