Qemu.h 16 KB

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