2 This driver is a sample implementation of the Graphics Output Protocol for
3 the QEMU (Cirrus Logic 5446) video controller.
5 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include <IndustryStandard/Acpi.h>
20 EFI_DRIVER_BINDING_PROTOCOL gQemuVideoDriverBinding
= {
21 QemuVideoControllerDriverSupported
,
22 QemuVideoControllerDriverStart
,
23 QemuVideoControllerDriverStop
,
29 QEMU_VIDEO_CARD gQemuVideoCardList
[] = {
31 CIRRUS_LOGIC_VENDOR_ID
,
32 CIRRUS_LOGIC_5430_DEVICE_ID
,
33 QEMU_VIDEO_CIRRUS_5430
,
36 CIRRUS_LOGIC_VENDOR_ID
,
37 CIRRUS_LOGIC_5430_ALTERNATE_DEVICE_ID
,
38 QEMU_VIDEO_CIRRUS_5430
,
41 CIRRUS_LOGIC_VENDOR_ID
,
42 CIRRUS_LOGIC_5446_DEVICE_ID
,
43 QEMU_VIDEO_CIRRUS_5446
,
48 QEMU_VIDEO_BOCHS_MMIO
,
60 static QEMU_VIDEO_CARD
*
68 while (gQemuVideoCardList
[Index
].VendorId
!= 0) {
69 if (gQemuVideoCardList
[Index
].VendorId
== VendorId
&&
70 gQemuVideoCardList
[Index
].DeviceId
== DeviceId
) {
71 return gQemuVideoCardList
+ Index
;
79 Check if this device is supported.
81 @param This The driver binding protocol.
82 @param Controller The controller handle to check.
83 @param RemainingDevicePath The remaining device path.
85 @retval EFI_SUCCESS The bus supports this controller.
86 @retval EFI_UNSUPPORTED This device isn't supported.
91 QemuVideoControllerDriverSupported (
92 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
93 IN EFI_HANDLE Controller
,
94 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
98 EFI_PCI_IO_PROTOCOL
*PciIo
;
101 QEMU_VIDEO_CARD
*Card
;
104 // Open the PCI I/O Protocol
106 Status
= gBS
->OpenProtocol (
108 &gEfiPciIoProtocolGuid
,
110 This
->DriverBindingHandle
,
112 EFI_OPEN_PROTOCOL_BY_DRIVER
114 if (EFI_ERROR (Status
)) {
119 // Read the PCI Configuration Header from the PCI Device
121 Status
= PciIo
->Pci
.Read (
125 sizeof (Pci
) / sizeof (UINT32
),
128 if (EFI_ERROR (Status
)) {
132 Status
= EFI_UNSUPPORTED
;
134 // See if the I/O enable is on. Most systems only allow one VGA device to be turned on
135 // at a time, so see if this is one that is turned on.
137 // if (((Pci.Hdr.Command & 0x01) == 0x01)) {
139 // See if this is a Cirrus Logic PCI controller
141 Card
= QemuVideoDetect(Pci
.Hdr
.VendorId
, Pci
.Hdr
.DeviceId
);
143 DEBUG ((EFI_D_INFO
, "QemuVideo: %s detected\n", Card
->Name
));
144 Status
= EFI_SUCCESS
;
146 // If this is an Intel 945 graphics controller,
147 // go further check RemainingDevicePath validation
149 if (RemainingDevicePath
!= NULL
) {
150 Node
= (EFI_DEV_PATH
*) RemainingDevicePath
;
152 // Check if RemainingDevicePath is the End of Device Path Node,
153 // if yes, return EFI_SUCCESS
155 if (!IsDevicePathEnd (Node
)) {
157 // If RemainingDevicePath isn't the End of Device Path Node,
158 // check its validation
160 if (Node
->DevPath
.Type
!= ACPI_DEVICE_PATH
||
161 Node
->DevPath
.SubType
!= ACPI_ADR_DP
||
162 DevicePathNodeLength(&Node
->DevPath
) != sizeof(ACPI_ADR_DEVICE_PATH
)) {
163 Status
= EFI_UNSUPPORTED
;
171 // Close the PCI I/O Protocol
175 &gEfiPciIoProtocolGuid
,
176 This
->DriverBindingHandle
,
184 Start to process the controller.
186 @param This The USB bus driver binding instance.
187 @param Controller The controller to check.
188 @param RemainingDevicePath The remaining device patch.
190 @retval EFI_SUCCESS The controller is controlled by the usb bus.
191 @retval EFI_ALREADY_STARTED The controller is already controlled by the usb
193 @retval EFI_OUT_OF_RESOURCES Failed to allocate resources.
198 QemuVideoControllerDriverStart (
199 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
200 IN EFI_HANDLE Controller
,
201 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
205 QEMU_VIDEO_PRIVATE_DATA
*Private
;
206 BOOLEAN PciAttributesSaved
;
207 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
208 ACPI_ADR_DEVICE_PATH AcpiDeviceNode
;
210 QEMU_VIDEO_CARD
*Card
;
211 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*MmioDesc
;
213 PciAttributesSaved
= FALSE
;
215 // Allocate Private context data for GOP inteface.
217 Private
= AllocateZeroPool (sizeof (QEMU_VIDEO_PRIVATE_DATA
));
218 if (Private
== NULL
) {
219 Status
= EFI_OUT_OF_RESOURCES
;
224 // Set up context record
226 Private
->Signature
= QEMU_VIDEO_PRIVATE_DATA_SIGNATURE
;
227 Private
->Handle
= NULL
;
230 // Open PCI I/O Protocol
232 Status
= gBS
->OpenProtocol (
234 &gEfiPciIoProtocolGuid
,
235 (VOID
**) &Private
->PciIo
,
236 This
->DriverBindingHandle
,
238 EFI_OPEN_PROTOCOL_BY_DRIVER
240 if (EFI_ERROR (Status
)) {
245 // Read the PCI Configuration Header from the PCI Device
247 Status
= Private
->PciIo
->Pci
.Read (
251 sizeof (Pci
) / sizeof (UINT32
),
254 if (EFI_ERROR (Status
)) {
258 Card
= QemuVideoDetect(Pci
.Hdr
.VendorId
, Pci
.Hdr
.DeviceId
);
260 Status
= EFI_DEVICE_ERROR
;
263 Private
->Variant
= Card
->Variant
;
266 // Save original PCI attributes
268 Status
= Private
->PciIo
->Attributes (
270 EfiPciIoAttributeOperationGet
,
272 &Private
->OriginalPciAttributes
275 if (EFI_ERROR (Status
)) {
278 PciAttributesSaved
= TRUE
;
280 Status
= Private
->PciIo
->Attributes (
282 EfiPciIoAttributeOperationEnable
,
283 EFI_PCI_DEVICE_ENABLE
| EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY
| EFI_PCI_IO_ATTRIBUTE_VGA_IO
,
286 if (EFI_ERROR (Status
)) {
291 // Check whenever the qemu stdvga mmio bar is present (qemu 1.3+).
293 if (Private
->Variant
== QEMU_VIDEO_BOCHS_MMIO
) {
294 Status
= Private
->PciIo
->GetBarAttributes (
300 if (EFI_ERROR (Status
) ||
301 MmioDesc
->ResType
!= ACPI_ADDRESS_SPACE_TYPE_MEM
) {
302 DEBUG ((EFI_D_INFO
, "QemuVideo: No mmio bar, fallback to port io\n"));
303 Private
->Variant
= QEMU_VIDEO_BOCHS
;
305 DEBUG ((EFI_D_INFO
, "QemuVideo: Using mmio bar @ 0x%lx\n",
306 MmioDesc
->AddrRangeMin
));
311 // Check if accessing the bochs interface works.
313 if (Private
->Variant
== QEMU_VIDEO_BOCHS_MMIO
||
314 Private
->Variant
== QEMU_VIDEO_BOCHS
) {
316 BochsId
= BochsRead(Private
, VBE_DISPI_INDEX_ID
);
317 if ((BochsId
& 0xFFF0) != VBE_DISPI_ID0
) {
318 DEBUG ((EFI_D_INFO
, "QemuVideo: BochsID mismatch (got 0x%x)\n", BochsId
));
319 Status
= EFI_DEVICE_ERROR
;
325 // Get ParentDevicePath
327 Status
= gBS
->HandleProtocol (
329 &gEfiDevicePathProtocolGuid
,
330 (VOID
**) &ParentDevicePath
332 if (EFI_ERROR (Status
)) {
337 // Set Gop Device Path
339 if (RemainingDevicePath
== NULL
) {
340 ZeroMem (&AcpiDeviceNode
, sizeof (ACPI_ADR_DEVICE_PATH
));
341 AcpiDeviceNode
.Header
.Type
= ACPI_DEVICE_PATH
;
342 AcpiDeviceNode
.Header
.SubType
= ACPI_ADR_DP
;
343 AcpiDeviceNode
.ADR
= ACPI_DISPLAY_ADR (1, 0, 0, 1, 0, ACPI_ADR_DISPLAY_TYPE_VGA
, 0, 0);
344 SetDevicePathNodeLength (&AcpiDeviceNode
.Header
, sizeof (ACPI_ADR_DEVICE_PATH
));
346 Private
->GopDevicePath
= AppendDevicePathNode (
348 (EFI_DEVICE_PATH_PROTOCOL
*) &AcpiDeviceNode
350 } else if (!IsDevicePathEnd (RemainingDevicePath
)) {
352 // If RemainingDevicePath isn't the End of Device Path Node,
353 // only scan the specified device by RemainingDevicePath
355 Private
->GopDevicePath
= AppendDevicePathNode (ParentDevicePath
, RemainingDevicePath
);
358 // If RemainingDevicePath is the End of Device Path Node,
359 // don't create child device and return EFI_SUCCESS
361 Private
->GopDevicePath
= NULL
;
364 if (Private
->GopDevicePath
!= NULL
) {
366 // Creat child handle and device path protocol firstly
368 Private
->Handle
= NULL
;
369 Status
= gBS
->InstallMultipleProtocolInterfaces (
371 &gEfiDevicePathProtocolGuid
,
372 Private
->GopDevicePath
,
378 // Construct video mode buffer
380 switch (Private
->Variant
) {
381 case QEMU_VIDEO_CIRRUS_5430
:
382 case QEMU_VIDEO_CIRRUS_5446
:
383 Status
= QemuVideoCirrusModeSetup (Private
);
385 case QEMU_VIDEO_BOCHS_MMIO
:
386 case QEMU_VIDEO_BOCHS
:
387 Status
= QemuVideoBochsModeSetup (Private
);
391 Status
= EFI_DEVICE_ERROR
;
394 if (EFI_ERROR (Status
)) {
398 if (Private
->GopDevicePath
== NULL
) {
400 // If RemainingDevicePath is the End of Device Path Node,
401 // don't create child device and return EFI_SUCCESS
403 Status
= EFI_SUCCESS
;
407 // Start the GOP software stack.
409 Status
= QemuVideoGraphicsOutputConstructor (Private
);
410 ASSERT_EFI_ERROR (Status
);
412 Status
= gBS
->InstallMultipleProtocolInterfaces (
414 &gEfiGraphicsOutputProtocolGuid
,
415 &Private
->GraphicsOutput
,
421 if (EFI_ERROR (Status
)) {
423 if (Private
->PciIo
) {
424 if (PciAttributesSaved
== TRUE
) {
426 // Restore original PCI attributes
428 Private
->PciIo
->Attributes (
430 EfiPciIoAttributeOperationSet
,
431 Private
->OriginalPciAttributes
,
436 // Close the PCI I/O Protocol
440 &gEfiPciIoProtocolGuid
,
441 This
->DriverBindingHandle
,
446 gBS
->FreePool (Private
);
456 @param This The USB bus driver binding protocol.
457 @param Controller The controller to release.
458 @param NumberOfChildren The number of children of this device that
459 opened the controller BY_CHILD.
460 @param ChildHandleBuffer The array of child handle.
462 @retval EFI_SUCCESS The controller or children are stopped.
463 @retval EFI_DEVICE_ERROR Failed to stop the driver.
468 QemuVideoControllerDriverStop (
469 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
470 IN EFI_HANDLE Controller
,
471 IN UINTN NumberOfChildren
,
472 IN EFI_HANDLE
*ChildHandleBuffer
475 EFI_GRAPHICS_OUTPUT_PROTOCOL
*GraphicsOutput
;
478 QEMU_VIDEO_PRIVATE_DATA
*Private
;
480 Status
= gBS
->OpenProtocol (
482 &gEfiGraphicsOutputProtocolGuid
,
483 (VOID
**) &GraphicsOutput
,
484 This
->DriverBindingHandle
,
486 EFI_OPEN_PROTOCOL_GET_PROTOCOL
488 if (EFI_ERROR (Status
)) {
493 // Get our private context information
495 Private
= QEMU_VIDEO_PRIVATE_DATA_FROM_GRAPHICS_OUTPUT_THIS (GraphicsOutput
);
497 QemuVideoGraphicsOutputDestructor (Private
);
499 // Remove the GOP protocol interface from the system
501 Status
= gBS
->UninstallMultipleProtocolInterfaces (
503 &gEfiGraphicsOutputProtocolGuid
,
504 &Private
->GraphicsOutput
,
508 if (EFI_ERROR (Status
)) {
513 // Restore original PCI attributes
515 Private
->PciIo
->Attributes (
517 EfiPciIoAttributeOperationSet
,
518 Private
->OriginalPciAttributes
,
523 // Close the PCI I/O Protocol
527 &gEfiPciIoProtocolGuid
,
528 This
->DriverBindingHandle
,
533 // Free our instance data
535 gBS
->FreePool (Private
);
541 TODO: Add function description
543 @param Private TODO: add argument description
544 @param Address TODO: add argument description
545 @param Data TODO: add argument description
547 TODO: add return values
552 QEMU_VIDEO_PRIVATE_DATA
*Private
,
557 Private
->PciIo
->Io
.Write (
560 EFI_PCI_IO_PASS_THROUGH_BAR
,
568 TODO: Add function description
570 @param Private TODO: add argument description
571 @param Address TODO: add argument description
572 @param Data TODO: add argument description
574 TODO: add return values
579 QEMU_VIDEO_PRIVATE_DATA
*Private
,
584 Private
->PciIo
->Io
.Write (
587 EFI_PCI_IO_PASS_THROUGH_BAR
,
595 TODO: Add function description
597 @param Private TODO: add argument description
598 @param Address TODO: add argument description
600 TODO: add return values
605 QEMU_VIDEO_PRIVATE_DATA
*Private
,
611 Private
->PciIo
->Io
.Read (
614 EFI_PCI_IO_PASS_THROUGH_BAR
,
623 TODO: Add function description
625 @param Private TODO: add argument description
626 @param Address TODO: add argument description
628 TODO: add return values
633 QEMU_VIDEO_PRIVATE_DATA
*Private
,
639 Private
->PciIo
->Io
.Read (
642 EFI_PCI_IO_PASS_THROUGH_BAR
,
651 TODO: Add function description
653 @param Private TODO: add argument description
654 @param Index TODO: add argument description
655 @param Red TODO: add argument description
656 @param Green TODO: add argument description
657 @param Blue TODO: add argument description
659 TODO: add return values
664 QEMU_VIDEO_PRIVATE_DATA
*Private
,
671 VgaOutb (Private
, PALETTE_INDEX_REGISTER
, (UINT8
) Index
);
672 VgaOutb (Private
, PALETTE_DATA_REGISTER
, (UINT8
) (Red
>> 2));
673 VgaOutb (Private
, PALETTE_DATA_REGISTER
, (UINT8
) (Green
>> 2));
674 VgaOutb (Private
, PALETTE_DATA_REGISTER
, (UINT8
) (Blue
>> 2));
678 TODO: Add function description
680 @param Private TODO: add argument description
682 TODO: add return values
687 QEMU_VIDEO_PRIVATE_DATA
*Private
696 for (RedIndex
= 0; RedIndex
< 8; RedIndex
++) {
697 for (GreenIndex
= 0; GreenIndex
< 8; GreenIndex
++) {
698 for (BlueIndex
= 0; BlueIndex
< 4; BlueIndex
++) {
699 SetPaletteColor (Private
, Index
, (UINT8
) (RedIndex
<< 5), (UINT8
) (GreenIndex
<< 5), (UINT8
) (BlueIndex
<< 6));
707 TODO: Add function description
709 @param Private TODO: add argument description
711 TODO: add return values
716 QEMU_VIDEO_PRIVATE_DATA
*Private
722 Private
->PciIo
->Mem
.Write (
724 EfiPciIoWidthFillUint32
,
733 TODO: Add function description
735 @param Private TODO: add argument description
737 TODO: add return values
742 QEMU_VIDEO_PRIVATE_DATA
*Private
,
750 TODO: Add function description
752 @param Private TODO: add argument description
753 @param ModeData TODO: add argument description
755 TODO: add return values
759 InitializeCirrusGraphicsMode (
760 QEMU_VIDEO_PRIVATE_DATA
*Private
,
761 QEMU_VIDEO_CIRRUS_MODES
*ModeData
767 outw (Private
, SEQ_ADDRESS_REGISTER
, 0x1206);
768 outw (Private
, SEQ_ADDRESS_REGISTER
, 0x0012);
770 for (Index
= 0; Index
< 15; Index
++) {
771 outw (Private
, SEQ_ADDRESS_REGISTER
, ModeData
->SeqSettings
[Index
]);
774 if (Private
->Variant
== QEMU_VIDEO_CIRRUS_5430
) {
775 outb (Private
, SEQ_ADDRESS_REGISTER
, 0x0f);
776 Byte
= (UINT8
) ((inb (Private
, SEQ_DATA_REGISTER
) & 0xc7) ^ 0x30);
777 outb (Private
, SEQ_DATA_REGISTER
, Byte
);
780 outb (Private
, MISC_OUTPUT_REGISTER
, ModeData
->MiscSetting
);
781 outw (Private
, GRAPH_ADDRESS_REGISTER
, 0x0506);
782 outw (Private
, SEQ_ADDRESS_REGISTER
, 0x0300);
783 outw (Private
, CRTC_ADDRESS_REGISTER
, 0x2011);
785 for (Index
= 0; Index
< 28; Index
++) {
786 outw (Private
, CRTC_ADDRESS_REGISTER
, (UINT16
) ((ModeData
->CrtcSettings
[Index
] << 8) | Index
));
789 for (Index
= 0; Index
< 9; Index
++) {
790 outw (Private
, GRAPH_ADDRESS_REGISTER
, (UINT16
) ((GraphicsController
[Index
] << 8) | Index
));
793 inb (Private
, INPUT_STATUS_1_REGISTER
);
795 for (Index
= 0; Index
< 21; Index
++) {
796 outb (Private
, ATT_ADDRESS_REGISTER
, (UINT8
) Index
);
797 outb (Private
, ATT_ADDRESS_REGISTER
, AttributeController
[Index
]);
800 outb (Private
, ATT_ADDRESS_REGISTER
, 0x20);
802 outw (Private
, GRAPH_ADDRESS_REGISTER
, 0x0009);
803 outw (Private
, GRAPH_ADDRESS_REGISTER
, 0x000a);
804 outw (Private
, GRAPH_ADDRESS_REGISTER
, 0x000b);
805 outb (Private
, DAC_PIXEL_MASK_REGISTER
, 0xff);
807 SetDefaultPalette (Private
);
808 ClearScreen (Private
);
813 QEMU_VIDEO_PRIVATE_DATA
*Private
,
820 if (Private
->Variant
== QEMU_VIDEO_BOCHS_MMIO
) {
821 Status
= Private
->PciIo
->Mem
.Write (
829 ASSERT_EFI_ERROR (Status
);
831 outw (Private
, VBE_DISPI_IOPORT_INDEX
, Reg
);
832 outw (Private
, VBE_DISPI_IOPORT_DATA
, Data
);
838 QEMU_VIDEO_PRIVATE_DATA
*Private
,
845 if (Private
->Variant
== QEMU_VIDEO_BOCHS_MMIO
) {
846 Status
= Private
->PciIo
->Mem
.Read (
854 ASSERT_EFI_ERROR (Status
);
856 outw (Private
, VBE_DISPI_IOPORT_INDEX
, Reg
);
857 Data
= inw (Private
, VBE_DISPI_IOPORT_DATA
);
864 QEMU_VIDEO_PRIVATE_DATA
*Private
,
871 if (Private
->Variant
== QEMU_VIDEO_BOCHS_MMIO
) {
872 Status
= Private
->PciIo
->Mem
.Write (
880 ASSERT_EFI_ERROR (Status
);
882 outb (Private
, Reg
, Data
);
887 InitializeBochsGraphicsMode (
888 QEMU_VIDEO_PRIVATE_DATA
*Private
,
889 QEMU_VIDEO_BOCHS_MODES
*ModeData
892 DEBUG ((EFI_D_INFO
, "InitializeBochsGraphicsMode: %dx%d @ %d\n",
893 ModeData
->Width
, ModeData
->Height
, ModeData
->ColorDepth
));
896 VgaOutb (Private
, ATT_ADDRESS_REGISTER
, 0x20);
898 BochsWrite (Private
, VBE_DISPI_INDEX_ENABLE
, 0);
899 BochsWrite (Private
, VBE_DISPI_INDEX_BANK
, 0);
900 BochsWrite (Private
, VBE_DISPI_INDEX_X_OFFSET
, 0);
901 BochsWrite (Private
, VBE_DISPI_INDEX_Y_OFFSET
, 0);
903 BochsWrite (Private
, VBE_DISPI_INDEX_BPP
, (UINT16
) ModeData
->ColorDepth
);
904 BochsWrite (Private
, VBE_DISPI_INDEX_XRES
, (UINT16
) ModeData
->Width
);
905 BochsWrite (Private
, VBE_DISPI_INDEX_VIRT_WIDTH
, (UINT16
) ModeData
->Width
);
906 BochsWrite (Private
, VBE_DISPI_INDEX_YRES
, (UINT16
) ModeData
->Height
);
907 BochsWrite (Private
, VBE_DISPI_INDEX_VIRT_HEIGHT
, (UINT16
) ModeData
->Height
);
909 BochsWrite (Private
, VBE_DISPI_INDEX_ENABLE
,
910 VBE_DISPI_ENABLED
| VBE_DISPI_LFB_ENABLED
);
912 SetDefaultPalette (Private
);
913 ClearScreen (Private
);
918 InitializeQemuVideo (
919 IN EFI_HANDLE ImageHandle
,
920 IN EFI_SYSTEM_TABLE
*SystemTable
925 Status
= EfiLibInstallDriverBindingComponentName2 (
928 &gQemuVideoDriverBinding
,
930 &gQemuVideoComponentName
,
931 &gQemuVideoComponentName2
933 ASSERT_EFI_ERROR (Status
);
936 // Install EFI Driver Supported EFI Version Protocol required for
937 // EFI drivers that are on PCI and other plug in cards.
939 gQemuVideoDriverSupportedEfiVersion
.FirmwareVersion
= PcdGet32 (PcdDriverSupportedEfiVersion
);
940 Status
= gBS
->InstallMultipleProtocolInterfaces (
942 &gEfiDriverSupportedEfiVersionProtocolGuid
,
943 &gQemuVideoDriverSupportedEfiVersion
,
946 ASSERT_EFI_ERROR (Status
);