CirrusLogic5430UgaDraw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /** @file
  2. This file produces the graphics abstration of UGA Draw. It is called by
  3. CirrusLogic5430.c file which deals with the EFI 1.1 driver model.
  4. This file just does graphics.
  5. Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include "CirrusLogic5430.h"
  9. //
  10. // UGA Draw Protocol Member Functions
  11. //
  12. EFI_STATUS
  13. EFIAPI
  14. CirrusLogic5430UgaDrawGetMode (
  15. IN EFI_UGA_DRAW_PROTOCOL *This,
  16. OUT UINT32 *HorizontalResolution,
  17. OUT UINT32 *VerticalResolution,
  18. OUT UINT32 *ColorDepth,
  19. OUT UINT32 *RefreshRate
  20. )
  21. {
  22. CIRRUS_LOGIC_5430_PRIVATE_DATA *Private;
  23. Private = CIRRUS_LOGIC_5430_PRIVATE_DATA_FROM_UGA_DRAW_THIS (This);
  24. if (Private->HardwareNeedsStarting) {
  25. return EFI_NOT_STARTED;
  26. }
  27. if ((HorizontalResolution == NULL) ||
  28. (VerticalResolution == NULL) ||
  29. (ColorDepth == NULL) ||
  30. (RefreshRate == NULL)) {
  31. return EFI_INVALID_PARAMETER;
  32. }
  33. *HorizontalResolution = Private->ModeData[Private->CurrentMode].HorizontalResolution;
  34. *VerticalResolution = Private->ModeData[Private->CurrentMode].VerticalResolution;
  35. *ColorDepth = Private->ModeData[Private->CurrentMode].ColorDepth;
  36. *RefreshRate = Private->ModeData[Private->CurrentMode].RefreshRate;
  37. return EFI_SUCCESS;
  38. }
  39. EFI_STATUS
  40. EFIAPI
  41. CirrusLogic5430UgaDrawSetMode (
  42. IN EFI_UGA_DRAW_PROTOCOL *This,
  43. IN UINT32 HorizontalResolution,
  44. IN UINT32 VerticalResolution,
  45. IN UINT32 ColorDepth,
  46. IN UINT32 RefreshRate
  47. )
  48. {
  49. CIRRUS_LOGIC_5430_PRIVATE_DATA *Private;
  50. UINTN Index;
  51. Private = CIRRUS_LOGIC_5430_PRIVATE_DATA_FROM_UGA_DRAW_THIS (This);
  52. for (Index = 0; Index < Private->MaxMode; Index++) {
  53. if (HorizontalResolution != Private->ModeData[Index].HorizontalResolution) {
  54. continue;
  55. }
  56. if (VerticalResolution != Private->ModeData[Index].VerticalResolution) {
  57. continue;
  58. }
  59. if (ColorDepth != Private->ModeData[Index].ColorDepth) {
  60. continue;
  61. }
  62. if (RefreshRate != Private->ModeData[Index].RefreshRate) {
  63. continue;
  64. }
  65. if (Private->LineBuffer) {
  66. gBS->FreePool (Private->LineBuffer);
  67. }
  68. Private->LineBuffer = NULL;
  69. Private->LineBuffer = AllocatePool (HorizontalResolution);
  70. if (Private->LineBuffer == NULL) {
  71. return EFI_OUT_OF_RESOURCES;
  72. }
  73. InitializeGraphicsMode (Private, &CirrusLogic5430VideoModes[Private->ModeData[Index].ModeNumber]);
  74. Private->CurrentMode = Index;
  75. Private->HardwareNeedsStarting = FALSE;
  76. return EFI_SUCCESS;
  77. }
  78. return EFI_NOT_FOUND;
  79. }
  80. EFI_STATUS
  81. EFIAPI
  82. CirrusLogic5430UgaDrawBlt (
  83. IN EFI_UGA_DRAW_PROTOCOL *This,
  84. IN EFI_UGA_PIXEL *BltBuffer, OPTIONAL
  85. IN EFI_UGA_BLT_OPERATION BltOperation,
  86. IN UINTN SourceX,
  87. IN UINTN SourceY,
  88. IN UINTN DestinationX,
  89. IN UINTN DestinationY,
  90. IN UINTN Width,
  91. IN UINTN Height,
  92. IN UINTN Delta
  93. )
  94. {
  95. CIRRUS_LOGIC_5430_PRIVATE_DATA *Private;
  96. EFI_TPL OriginalTPL;
  97. UINTN DstY;
  98. UINTN SrcY;
  99. EFI_UGA_PIXEL *Blt;
  100. UINTN X;
  101. UINT8 Pixel;
  102. UINT32 WidePixel;
  103. UINTN ScreenWidth;
  104. UINTN Offset;
  105. UINTN SourceOffset;
  106. Private = CIRRUS_LOGIC_5430_PRIVATE_DATA_FROM_UGA_DRAW_THIS (This);
  107. if ((UINT32)BltOperation >= EfiUgaBltMax) {
  108. return EFI_INVALID_PARAMETER;
  109. }
  110. if (Width == 0 || Height == 0) {
  111. return EFI_INVALID_PARAMETER;
  112. }
  113. //
  114. // If Delta is zero, then the entire BltBuffer is being used, so Delta
  115. // is the number of bytes in each row of BltBuffer. Since BltBuffer is Width pixels size,
  116. // the number of bytes in each row can be computed.
  117. //
  118. if (Delta == 0) {
  119. Delta = Width * sizeof (EFI_UGA_PIXEL);
  120. }
  121. //
  122. // We need to fill the Virtual Screen buffer with the blt data.
  123. // The virtual screen is upside down, as the first row is the bootom row of
  124. // the image.
  125. //
  126. //
  127. // Make sure the SourceX, SourceY, DestinationX, DestinationY, Width, and Height parameters
  128. // are valid for the operation and the current screen geometry.
  129. //
  130. if (BltOperation == EfiUgaVideoToBltBuffer) {
  131. //
  132. // Video to BltBuffer: Source is Video, destination is BltBuffer
  133. //
  134. if (SourceY + Height > Private->ModeData[Private->CurrentMode].VerticalResolution) {
  135. return EFI_INVALID_PARAMETER;
  136. }
  137. if (SourceX + Width > Private->ModeData[Private->CurrentMode].HorizontalResolution) {
  138. return EFI_INVALID_PARAMETER;
  139. }
  140. } else {
  141. //
  142. // BltBuffer to Video: Source is BltBuffer, destination is Video
  143. //
  144. if (DestinationY + Height > Private->ModeData[Private->CurrentMode].VerticalResolution) {
  145. return EFI_INVALID_PARAMETER;
  146. }
  147. if (DestinationX + Width > Private->ModeData[Private->CurrentMode].HorizontalResolution) {
  148. return EFI_INVALID_PARAMETER;
  149. }
  150. }
  151. //
  152. // We have to raise to TPL Notify, so we make an atomic write the frame buffer.
  153. // We would not want a timer based event (Cursor, ...) to come in while we are
  154. // doing this operation.
  155. //
  156. OriginalTPL = gBS->RaiseTPL (TPL_NOTIFY);
  157. switch (BltOperation) {
  158. case EfiUgaVideoToBltBuffer:
  159. //
  160. // Video to BltBuffer: Source is Video, destination is BltBuffer
  161. //
  162. for (SrcY = SourceY, DstY = DestinationY; DstY < (Height + DestinationY); SrcY++, DstY++) {
  163. Offset = (SrcY * Private->ModeData[Private->CurrentMode].HorizontalResolution) + SourceX;
  164. if (((Offset & 0x03) == 0) && ((Width & 0x03) == 0)) {
  165. Private->PciIo->Mem.Read (
  166. Private->PciIo,
  167. EfiPciIoWidthUint32,
  168. 0,
  169. Offset,
  170. Width >> 2,
  171. Private->LineBuffer
  172. );
  173. } else {
  174. Private->PciIo->Mem.Read (
  175. Private->PciIo,
  176. EfiPciIoWidthUint8,
  177. 0,
  178. Offset,
  179. Width,
  180. Private->LineBuffer
  181. );
  182. }
  183. for (X = 0; X < Width; X++) {
  184. Blt = (EFI_UGA_PIXEL *) ((UINT8 *) BltBuffer + (DstY * Delta) + (DestinationX + X) * sizeof (EFI_UGA_PIXEL));
  185. Blt->Red = (UINT8) (Private->LineBuffer[X] & 0xe0);
  186. Blt->Green = (UINT8) ((Private->LineBuffer[X] & 0x1c) << 3);
  187. Blt->Blue = (UINT8) ((Private->LineBuffer[X] & 0x03) << 6);
  188. }
  189. }
  190. break;
  191. case EfiUgaVideoToVideo:
  192. //
  193. // Perform hardware acceleration for Video to Video operations
  194. //
  195. ScreenWidth = Private->ModeData[Private->CurrentMode].HorizontalResolution;
  196. SourceOffset = (SourceY * Private->ModeData[Private->CurrentMode].HorizontalResolution) + (SourceX);
  197. Offset = (DestinationY * Private->ModeData[Private->CurrentMode].HorizontalResolution) + (DestinationX);
  198. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0000);
  199. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0010);
  200. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0012);
  201. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0014);
  202. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0001);
  203. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0011);
  204. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0013);
  205. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0015);
  206. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) (((Width << 8) & 0xff00) | 0x20));
  207. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) ((Width & 0xff00) | 0x21));
  208. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) (((Height << 8) & 0xff00) | 0x22));
  209. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) ((Height & 0xff00) | 0x23));
  210. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) (((ScreenWidth << 8) & 0xff00) | 0x24));
  211. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) ((ScreenWidth & 0xff00) | 0x25));
  212. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) (((ScreenWidth << 8) & 0xff00) | 0x26));
  213. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) ((ScreenWidth & 0xff00) | 0x27));
  214. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) ((((Offset) << 8) & 0xff00) | 0x28));
  215. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) ((((Offset) >> 0) & 0xff00) | 0x29));
  216. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) ((((Offset) >> 8) & 0xff00) | 0x2a));
  217. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) ((((SourceOffset) << 8) & 0xff00) | 0x2c));
  218. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) ((((SourceOffset) >> 0) & 0xff00) | 0x2d));
  219. outw (Private, GRAPH_ADDRESS_REGISTER, (UINT16) ((((SourceOffset) >> 8) & 0xff00) | 0x2e));
  220. outw (Private, GRAPH_ADDRESS_REGISTER, 0x002f);
  221. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0030);
  222. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0d32);
  223. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0033);
  224. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0034);
  225. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0035);
  226. outw (Private, GRAPH_ADDRESS_REGISTER, 0x0231);
  227. outb (Private, GRAPH_ADDRESS_REGISTER, 0x31);
  228. while ((inb (Private, GRAPH_DATA_REGISTER) & 0x01) == 0x01)
  229. ;
  230. break;
  231. case EfiUgaVideoFill:
  232. Blt = BltBuffer;
  233. Pixel = (UINT8) ((Blt->Red & 0xe0) | ((Blt->Green >> 3) & 0x1c) | ((Blt->Blue >> 6) & 0x03));
  234. WidePixel = (Pixel << 8) | Pixel;
  235. WidePixel = (WidePixel << 16) | WidePixel;
  236. if (DestinationX == 0 && Width == Private->ModeData[Private->CurrentMode].HorizontalResolution) {
  237. Offset = DestinationY * Private->ModeData[Private->CurrentMode].HorizontalResolution;
  238. if (((Offset & 0x03) == 0) && (((Width * Height) & 0x03) == 0)) {
  239. Private->PciIo->Mem.Write (
  240. Private->PciIo,
  241. EfiPciIoWidthFillUint32,
  242. 0,
  243. Offset,
  244. (Width * Height) >> 2,
  245. &WidePixel
  246. );
  247. } else {
  248. Private->PciIo->Mem.Write (
  249. Private->PciIo,
  250. EfiPciIoWidthFillUint8,
  251. 0,
  252. Offset,
  253. Width * Height,
  254. &Pixel
  255. );
  256. }
  257. } else {
  258. for (SrcY = SourceY, DstY = DestinationY; SrcY < (Height + SourceY); SrcY++, DstY++) {
  259. Offset = (DstY * Private->ModeData[Private->CurrentMode].HorizontalResolution) + DestinationX;
  260. if (((Offset & 0x03) == 0) && ((Width & 0x03) == 0)) {
  261. Private->PciIo->Mem.Write (
  262. Private->PciIo,
  263. EfiPciIoWidthFillUint32,
  264. 0,
  265. Offset,
  266. Width >> 2,
  267. &WidePixel
  268. );
  269. } else {
  270. Private->PciIo->Mem.Write (
  271. Private->PciIo,
  272. EfiPciIoWidthFillUint8,
  273. 0,
  274. Offset,
  275. Width,
  276. &Pixel
  277. );
  278. }
  279. }
  280. }
  281. break;
  282. case EfiUgaBltBufferToVideo:
  283. for (SrcY = SourceY, DstY = DestinationY; SrcY < (Height + SourceY); SrcY++, DstY++) {
  284. for (X = 0; X < Width; X++) {
  285. Blt = (EFI_UGA_PIXEL *) ((UINT8 *) BltBuffer + (SrcY * Delta) + (SourceX + X) * sizeof (EFI_UGA_PIXEL));
  286. Private->LineBuffer[X] = (UINT8) ((Blt->Red & 0xe0) | ((Blt->Green >> 3) & 0x1c) | ((Blt->Blue >> 6) & 0x03));
  287. }
  288. Offset = (DstY * Private->ModeData[Private->CurrentMode].HorizontalResolution) + DestinationX;
  289. if (((Offset & 0x03) == 0) && ((Width & 0x03) == 0)) {
  290. Private->PciIo->Mem.Write (
  291. Private->PciIo,
  292. EfiPciIoWidthUint32,
  293. 0,
  294. Offset,
  295. Width >> 2,
  296. Private->LineBuffer
  297. );
  298. } else {
  299. Private->PciIo->Mem.Write (
  300. Private->PciIo,
  301. EfiPciIoWidthUint8,
  302. 0,
  303. Offset,
  304. Width,
  305. Private->LineBuffer
  306. );
  307. }
  308. }
  309. break;
  310. default:
  311. break;
  312. }
  313. gBS->RestoreTPL (OriginalTPL);
  314. return EFI_SUCCESS;
  315. }
  316. //
  317. // Construction and Destruction functions
  318. //
  319. EFI_STATUS
  320. CirrusLogic5430UgaDrawConstructor (
  321. CIRRUS_LOGIC_5430_PRIVATE_DATA *Private
  322. )
  323. {
  324. EFI_UGA_DRAW_PROTOCOL *UgaDraw;
  325. //
  326. // Fill in Private->UgaDraw protocol
  327. //
  328. UgaDraw = &Private->UgaDraw;
  329. UgaDraw->GetMode = CirrusLogic5430UgaDrawGetMode;
  330. UgaDraw->SetMode = CirrusLogic5430UgaDrawSetMode;
  331. UgaDraw->Blt = CirrusLogic5430UgaDrawBlt;
  332. //
  333. // Initialize the private data
  334. //
  335. Private->CurrentMode = 0;
  336. Private->HardwareNeedsStarting = TRUE;
  337. Private->LineBuffer = NULL;
  338. //
  339. // Initialize the hardware
  340. //
  341. UgaDraw->SetMode (
  342. UgaDraw,
  343. Private->ModeData[Private->CurrentMode].HorizontalResolution,
  344. Private->ModeData[Private->CurrentMode].VerticalResolution,
  345. Private->ModeData[Private->CurrentMode].ColorDepth,
  346. Private->ModeData[Private->CurrentMode].RefreshRate
  347. );
  348. DrawLogo (
  349. Private,
  350. Private->ModeData[Private->CurrentMode].HorizontalResolution,
  351. Private->ModeData[Private->CurrentMode].VerticalResolution
  352. );
  353. return EFI_SUCCESS;
  354. }