2 QEMU Video Controller Driver
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 // QEMU Video Controller Driver
17 #include <Protocol/GraphicsOutput.h>
18 #include <Protocol/PciIo.h>
19 #include <Protocol/DriverSupportedEfiVersion.h>
20 #include <Protocol/DevicePath.h>
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>
33 #include <IndustryStandard/Pci.h>
34 #include <IndustryStandard/Acpi.h>
37 // QEMU Video PCI Configuration Header values
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
45 // QEMU Vide Graphical Mode Data
48 UINT32 InternalModeIndex
; // points into card-specific mode table
49 UINT32 HorizontalResolution
;
50 UINT32 VerticalResolution
;
52 } QEMU_VIDEO_MODE_DATA
;
54 #define PIXEL_RED_SHIFT 0
55 #define PIXEL_GREEN_SHIFT 3
56 #define PIXEL_BLUE_SHIFT 6
58 #define PIXEL_RED_MASK (BIT7 | BIT6 | BIT5)
59 #define PIXEL_GREEN_MASK (BIT4 | BIT3 | BIT2)
60 #define PIXEL_BLUE_MASK (BIT1 | BIT0)
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)
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) )
72 #define PIXEL24_RED_MASK 0x00ff0000
73 #define PIXEL24_GREEN_MASK 0x0000ff00
74 #define PIXEL24_BLUE_MASK 0x000000ff
76 #define GRAPHICS_OUTPUT_INVALIDE_MODE_NUMBER 0xffff
79 // QEMU Video Private Data Structure
81 #define QEMU_VIDEO_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('Q', 'V', 'I', 'D')
84 QEMU_VIDEO_CIRRUS_5430
= 1,
85 QEMU_VIDEO_CIRRUS_5446
,
87 QEMU_VIDEO_BOCHS_MMIO
,
88 QEMU_VIDEO_VMWARE_SVGA
,
95 QEMU_VIDEO_VARIANT Variant
;
102 EFI_PCI_IO_PROTOCOL
*PciIo
;
103 UINT64 OriginalPciAttributes
;
104 EFI_GRAPHICS_OUTPUT_PROTOCOL GraphicsOutput
;
105 EFI_DEVICE_PATH_PROTOCOL
*GopDevicePath
;
108 // The next two fields match the client-visible
109 // EFI_GRAPHICS_OUTPUT_PROTOCOL_MODE.MaxMode field.
112 QEMU_VIDEO_MODE_DATA
*ModeData
;
114 QEMU_VIDEO_VARIANT Variant
;
115 FRAME_BUFFER_CONFIGURE
*FrameBufferBltConfigure
;
116 UINTN FrameBufferBltConfigureSize
;
117 UINT8 FrameBufferVramBarIndex
;
118 } QEMU_VIDEO_PRIVATE_DATA
;
121 /// Card-specific Video Mode structures
130 } QEMU_VIDEO_CIRRUS_MODES
;
135 } QEMU_VIDEO_BOCHS_MODES
;
137 #define QEMU_VIDEO_PRIVATE_DATA_FROM_GRAPHICS_OUTPUT_THIS(a) \
138 CR(a, QEMU_VIDEO_PRIVATE_DATA, GraphicsOutput, QEMU_VIDEO_PRIVATE_DATA_SIGNATURE)
143 extern UINT8 AttributeController
[];
144 extern UINT8 GraphicsController
[];
145 extern UINT8 Crtc_640_480_256_60
[];
146 extern UINT16 Seq_640_480_256_60
[];
147 extern UINT8 Crtc_800_600_256_60
[];
148 extern UINT16 Seq_800_600_256_60
[];
149 extern UINT8 Crtc_1024_768_256_60
[];
150 extern UINT16 Seq_1024_768_256_60
[];
151 extern QEMU_VIDEO_CIRRUS_MODES QemuVideoCirrusModes
[];
152 extern EFI_DRIVER_BINDING_PROTOCOL gQemuVideoDriverBinding
;
153 extern EFI_COMPONENT_NAME_PROTOCOL gQemuVideoComponentName
;
154 extern EFI_COMPONENT_NAME2_PROTOCOL gQemuVideoComponentName2
;
157 // Io Registers defined by VGA
159 #define CRTC_ADDRESS_REGISTER 0x3d4
160 #define CRTC_DATA_REGISTER 0x3d5
161 #define SEQ_ADDRESS_REGISTER 0x3c4
162 #define SEQ_DATA_REGISTER 0x3c5
163 #define GRAPH_ADDRESS_REGISTER 0x3ce
164 #define GRAPH_DATA_REGISTER 0x3cf
165 #define ATT_ADDRESS_REGISTER 0x3c0
166 #define MISC_OUTPUT_REGISTER 0x3c2
167 #define INPUT_STATUS_1_REGISTER 0x3da
168 #define DAC_PIXEL_MASK_REGISTER 0x3c6
169 #define PALETTE_INDEX_REGISTER 0x3c8
170 #define PALETTE_DATA_REGISTER 0x3c9
172 #define VBE_DISPI_IOPORT_INDEX 0x01CE
173 #define VBE_DISPI_IOPORT_DATA 0x01D0
175 #define VBE_DISPI_INDEX_ID 0x0
176 #define VBE_DISPI_INDEX_XRES 0x1
177 #define VBE_DISPI_INDEX_YRES 0x2
178 #define VBE_DISPI_INDEX_BPP 0x3
179 #define VBE_DISPI_INDEX_ENABLE 0x4
180 #define VBE_DISPI_INDEX_BANK 0x5
181 #define VBE_DISPI_INDEX_VIRT_WIDTH 0x6
182 #define VBE_DISPI_INDEX_VIRT_HEIGHT 0x7
183 #define VBE_DISPI_INDEX_X_OFFSET 0x8
184 #define VBE_DISPI_INDEX_Y_OFFSET 0x9
185 #define VBE_DISPI_INDEX_VIDEO_MEMORY_64K 0xa
187 #define VBE_DISPI_ID0 0xB0C0
188 #define VBE_DISPI_ID1 0xB0C1
189 #define VBE_DISPI_ID2 0xB0C2
190 #define VBE_DISPI_ID3 0xB0C3
191 #define VBE_DISPI_ID4 0xB0C4
192 #define VBE_DISPI_ID5 0xB0C5
194 #define VBE_DISPI_DISABLED 0x00
195 #define VBE_DISPI_ENABLED 0x01
196 #define VBE_DISPI_GETCAPS 0x02
197 #define VBE_DISPI_8BIT_DAC 0x20
198 #define VBE_DISPI_LFB_ENABLED 0x40
199 #define VBE_DISPI_NOCLEARMEM 0x80
202 // Graphics Output Hardware abstraction internal worker functions
205 QemuVideoGraphicsOutputConstructor (
206 QEMU_VIDEO_PRIVATE_DATA
*Private
210 QemuVideoGraphicsOutputDestructor (
211 QEMU_VIDEO_PRIVATE_DATA
*Private
215 // EFI_DRIVER_BINDING_PROTOCOL Protocol Interface
219 TODO: Add function description
221 @param This TODO: add argument description
222 @param Controller TODO: add argument description
223 @param RemainingDevicePath TODO: add argument description
225 TODO: add return values
230 QemuVideoControllerDriverSupported (
231 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
232 IN EFI_HANDLE Controller
,
233 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
237 TODO: Add function description
239 @param This TODO: add argument description
240 @param Controller TODO: add argument description
241 @param RemainingDevicePath TODO: add argument description
243 TODO: add return values
248 QemuVideoControllerDriverStart (
249 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
250 IN EFI_HANDLE Controller
,
251 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
255 TODO: Add function description
257 @param This TODO: add argument description
258 @param Controller TODO: add argument description
259 @param NumberOfChildren TODO: add argument description
260 @param ChildHandleBuffer TODO: add argument description
262 TODO: add return values
267 QemuVideoControllerDriverStop (
268 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
269 IN EFI_HANDLE Controller
,
270 IN UINTN NumberOfChildren
,
271 IN EFI_HANDLE
*ChildHandleBuffer
275 // EFI Component Name Functions
279 Retrieves a Unicode string that is the user readable name of the driver.
281 This function retrieves the user readable name of a driver in the form of a
282 Unicode string. If the driver specified by This has a user readable name in
283 the language specified by Language, then a pointer to the driver name is
284 returned in DriverName, and EFI_SUCCESS is returned. If the driver specified
285 by This does not support the language specified by Language,
286 then EFI_UNSUPPORTED is returned.
288 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
289 EFI_COMPONENT_NAME_PROTOCOL instance.
291 @param Language[in] A pointer to a Null-terminated ASCII string
292 array indicating the language. This is the
293 language of the driver name that the caller is
294 requesting, and it must match one of the
295 languages specified in SupportedLanguages. The
296 number of languages supported by a driver is up
297 to the driver writer. Language is specified
298 in RFC 4646 or ISO 639-2 language code format.
300 @param DriverName[out] A pointer to the Unicode string to return.
301 This Unicode string is the name of the
302 driver specified by This in the language
303 specified by Language.
305 @retval EFI_SUCCESS The Unicode string for the Driver specified by
306 This and the language specified by Language was
307 returned in DriverName.
309 @retval EFI_INVALID_PARAMETER Language is NULL.
311 @retval EFI_INVALID_PARAMETER DriverName is NULL.
313 @retval EFI_UNSUPPORTED The driver specified by This does not support
314 the language specified by Language.
319 QemuVideoComponentNameGetDriverName (
320 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
322 OUT CHAR16
**DriverName
326 Retrieves a Unicode string that is the user readable name of the controller
327 that is being managed by a driver.
329 This function retrieves the user readable name of the controller specified by
330 ControllerHandle and ChildHandle in the form of a Unicode string. If the
331 driver specified by This has a user readable name in the language specified by
332 Language, then a pointer to the controller name is returned in ControllerName,
333 and EFI_SUCCESS is returned. If the driver specified by This is not currently
334 managing the controller specified by ControllerHandle and ChildHandle,
335 then EFI_UNSUPPORTED is returned. If the driver specified by This does not
336 support the language specified by Language, then EFI_UNSUPPORTED is returned.
338 @param This[in] A pointer to the EFI_COMPONENT_NAME2_PROTOCOL or
339 EFI_COMPONENT_NAME_PROTOCOL instance.
341 @param ControllerHandle[in] The handle of a controller that the driver
342 specified by This is managing. This handle
343 specifies the controller whose name is to be
346 @param ChildHandle[in] The handle of the child controller to retrieve
347 the name of. This is an optional parameter that
348 may be NULL. It will be NULL for device
349 drivers. It will also be NULL for a bus drivers
350 that wish to retrieve the name of the bus
351 controller. It will not be NULL for a bus
352 driver that wishes to retrieve the name of a
355 @param Language[in] A pointer to a Null-terminated ASCII string
356 array indicating the language. This is the
357 language of the driver name that the caller is
358 requesting, and it must match one of the
359 languages specified in SupportedLanguages. The
360 number of languages supported by a driver is up
361 to the driver writer. Language is specified in
362 RFC 4646 or ISO 639-2 language code format.
364 @param ControllerName[out] A pointer to the Unicode string to return.
365 This Unicode string is the name of the
366 controller specified by ControllerHandle and
367 ChildHandle in the language specified by
368 Language from the point of view of the driver
371 @retval EFI_SUCCESS The Unicode string for the user readable name in
372 the language specified by Language for the
373 driver specified by This was returned in
376 @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE.
378 @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid
381 @retval EFI_INVALID_PARAMETER Language is NULL.
383 @retval EFI_INVALID_PARAMETER ControllerName is NULL.
385 @retval EFI_UNSUPPORTED The driver specified by This is not currently
386 managing the controller specified by
387 ControllerHandle and ChildHandle.
389 @retval EFI_UNSUPPORTED The driver specified by This does not support
390 the language specified by Language.
395 QemuVideoComponentNameGetControllerName (
396 IN EFI_COMPONENT_NAME_PROTOCOL
*This
,
397 IN EFI_HANDLE ControllerHandle
,
398 IN EFI_HANDLE ChildHandle OPTIONAL
,
400 OUT CHAR16
**ControllerName
404 // Local Function Prototypes
407 InitializeCirrusGraphicsMode (
408 QEMU_VIDEO_PRIVATE_DATA
*Private
,
409 QEMU_VIDEO_CIRRUS_MODES
*ModeData
413 InitializeBochsGraphicsMode (
414 QEMU_VIDEO_PRIVATE_DATA
*Private
,
415 QEMU_VIDEO_MODE_DATA
*ModeData
420 QEMU_VIDEO_PRIVATE_DATA
*Private
,
429 QEMU_VIDEO_PRIVATE_DATA
*Private
434 QEMU_VIDEO_PRIVATE_DATA
*Private
,
441 QEMU_VIDEO_PRIVATE_DATA
*Private
,
448 QEMU_VIDEO_PRIVATE_DATA
*Private
,
455 QEMU_VIDEO_PRIVATE_DATA
*Private
,
461 QEMU_VIDEO_PRIVATE_DATA
*Private
,
467 QEMU_VIDEO_PRIVATE_DATA
*Private
,
474 QEMU_VIDEO_PRIVATE_DATA
*Private
,
480 QEMU_VIDEO_PRIVATE_DATA
*Private
,
486 QemuVideoCirrusModeSetup (
487 QEMU_VIDEO_PRIVATE_DATA
*Private
491 QemuVideoBochsModeSetup (
492 QEMU_VIDEO_PRIVATE_DATA
*Private
,
498 IN CONST CHAR16
*CardName
,
499 IN EFI_PHYSICAL_ADDRESS FrameBufferBase