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