Simics.h 17 KB

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