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