GopDriver.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*++ @file
  2. Copyright (c) 2020, Rebecca Cran <rebecca@bsdio.com>
  3. Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>
  4. Portions copyright (c) 2010,Apple Inc. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "Gop.h"
  8. #include <IndustryStandard/Acpi.h>
  9. STATIC VOID
  10. BhyveGetGraphicsMode (
  11. EFI_PCI_IO_PROTOCOL *PciIo,
  12. UINT16 *Width,
  13. UINT16 *Height,
  14. UINT16 *Depth
  15. );
  16. /**
  17. Tests to see if this driver supports a given controller. If a child device is provided,
  18. it further tests to see if this driver supports creating a handle for the specified child device.
  19. This function checks to see if the driver specified by This supports the device specified by
  20. ControllerHandle. Drivers will typically use the device path attached to
  21. ControllerHandle and/or the services from the bus I/O abstraction attached to
  22. ControllerHandle to determine if the driver supports ControllerHandle. This function
  23. may be called many times during platform initialization. In order to reduce boot times, the tests
  24. performed by this function must be very small, and take as little time as possible to execute. This
  25. function must not change the state of any hardware devices, and this function must be aware that the
  26. device specified by ControllerHandle may already be managed by the same driver or a
  27. different driver. This function must match its calls to AllocatePages() with FreePages(),
  28. AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
  29. Because ControllerHandle may have been previously started by the same driver, if a protocol is
  30. already in the opened state, then it must not be closed with CloseProtocol(). This is required
  31. to guarantee the state of ControllerHandle is not modified by this function.
  32. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  33. @param[in] ControllerHandle The handle of the controller to test. This handle
  34. must support a protocol interface that supplies
  35. an I/O abstraction to the driver.
  36. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  37. parameter is ignored by device drivers, and is optional for bus
  38. drivers. For bus drivers, if this parameter is not NULL, then
  39. the bus driver must determine if the bus controller specified
  40. by ControllerHandle and the child controller specified
  41. by RemainingDevicePath are both supported by this
  42. bus driver.
  43. @retval EFI_SUCCESS The device specified by ControllerHandle and
  44. RemainingDevicePath is supported by the driver specified by This.
  45. @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
  46. RemainingDevicePath is already being managed by the driver
  47. specified by This.
  48. @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
  49. RemainingDevicePath is already being managed by a different
  50. driver or an application that requires exclusive access.
  51. Currently not implemented.
  52. @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
  53. RemainingDevicePath is not supported by the driver specified by This.
  54. **/
  55. EFI_STATUS
  56. EFIAPI
  57. EmuGopDriverBindingSupported (
  58. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  59. IN EFI_HANDLE Handle,
  60. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  61. )
  62. {
  63. EFI_STATUS Status;
  64. EFI_PCI_IO_PROTOCOL *PciIo;
  65. PCI_TYPE00 Pci;
  66. UINT16 Width, Height, Depth;
  67. //
  68. // Open the IO Abstraction(s) needed to perform the supported test
  69. //
  70. Status = gBS->OpenProtocol (
  71. Handle,
  72. &gEfiPciIoProtocolGuid,
  73. (VOID **)&PciIo,
  74. This->DriverBindingHandle,
  75. Handle,
  76. EFI_OPEN_PROTOCOL_BY_DRIVER
  77. );
  78. if (EFI_ERROR (Status)) {
  79. return Status;
  80. }
  81. //
  82. // See if this is a PCI Framebuffer Controller by looking at the Command register and
  83. // Class Code Register
  84. //
  85. Status = PciIo->Pci.Read (
  86. PciIo,
  87. EfiPciIoWidthUint32,
  88. PCI_BAR_IDX0,
  89. sizeof (Pci) / sizeof (UINT32),
  90. &Pci
  91. );
  92. if (EFI_ERROR (Status)) {
  93. Status = EFI_UNSUPPORTED;
  94. goto Done;
  95. }
  96. Status = EFI_UNSUPPORTED;
  97. if ((Pci.Hdr.VendorId == 0xFB5D) && (Pci.Hdr.DeviceId == 0x40FB)) {
  98. DEBUG ((DEBUG_INFO, "BHYVE framebuffer device detected\n"));
  99. Status = EFI_SUCCESS;
  100. BhyveGetGraphicsMode (PciIo, &Width, &Height, &Depth);
  101. PcdSet32S (PcdVideoHorizontalResolution, Width);
  102. PcdSet32S (PcdVideoVerticalResolution, Height);
  103. }
  104. Done:
  105. //
  106. // Close the PCI I/O Protocol
  107. //
  108. gBS->CloseProtocol (
  109. Handle,
  110. &gEfiPciIoProtocolGuid,
  111. This->DriverBindingHandle,
  112. Handle
  113. );
  114. return Status;
  115. }
  116. /**
  117. Starts a device controller or a bus controller.
  118. The Start() function is designed to be invoked from the EFI boot service ConnectController().
  119. As a result, much of the error checking on the parameters to Start() has been moved into this
  120. common boot service. It is legal to call Start() from other locations,
  121. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  122. 1. ControllerHandle must be a valid EFI_HANDLE.
  123. 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
  124. EFI_DEVICE_PATH_PROTOCOL.
  125. 3. Prior to calling Start(), the Supported() function for the driver specified by This must
  126. have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
  127. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  128. @param[in] ControllerHandle The handle of the controller to start. This handle
  129. must support a protocol interface that supplies
  130. an I/O abstraction to the driver.
  131. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  132. parameter is ignored by device drivers, and is optional for bus
  133. drivers. For a bus driver, if this parameter is NULL, then handles
  134. for all the children of Controller are created by this driver.
  135. If this parameter is not NULL and the first Device Path Node is
  136. not the End of Device Path Node, then only the handle for the
  137. child device specified by the first Device Path Node of
  138. RemainingDevicePath is created by this driver.
  139. If the first Device Path Node of RemainingDevicePath is
  140. the End of Device Path Node, no child handle is created by this
  141. driver.
  142. @retval EFI_SUCCESS The device was started.
  143. @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
  144. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  145. @retval Others The driver failded to start the device.
  146. **/
  147. EFI_STATUS
  148. EFIAPI
  149. EmuGopDriverBindingStart (
  150. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  151. IN EFI_HANDLE Handle,
  152. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  153. )
  154. {
  155. BHYVE_FBUF_MEMREGS Memregs;
  156. GOP_PRIVATE_DATA *Private;
  157. EFI_STATUS Status;
  158. EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *MmioDesc;
  159. //
  160. // Allocate Private context data for SGO inteface.
  161. //
  162. Private = NULL;
  163. Status = gBS->AllocatePool (
  164. EfiBootServicesData,
  165. sizeof (GOP_PRIVATE_DATA),
  166. (VOID **)&Private
  167. );
  168. if (EFI_ERROR (Status)) {
  169. goto Done;
  170. }
  171. // Set up context record
  172. //
  173. Private->Signature = GOP_PRIVATE_DATA_SIGNATURE;
  174. Private->Handle = Handle;
  175. Private->ControllerNameTable = NULL;
  176. //
  177. // Open PCI I/O Protocol
  178. //
  179. Status = gBS->OpenProtocol (
  180. Handle,
  181. &gEfiPciIoProtocolGuid,
  182. (VOID **)&Private->PciIo,
  183. This->DriverBindingHandle,
  184. Handle,
  185. EFI_OPEN_PROTOCOL_BY_DRIVER
  186. );
  187. if (EFI_ERROR (Status)) {
  188. goto Done;
  189. }
  190. //
  191. // Check if fbuf mmio BAR is present
  192. //
  193. MmioDesc = NULL;
  194. Status = Private->PciIo->GetBarAttributes (
  195. Private->PciIo,
  196. PCI_BAR_IDX0,
  197. NULL,
  198. (VOID **)&MmioDesc
  199. );
  200. if (EFI_ERROR (Status) ||
  201. (MmioDesc->ResType != ACPI_ADDRESS_SPACE_TYPE_MEM))
  202. {
  203. DEBUG ((DEBUG_INFO, "BHYVE GOP: No mmio bar\n"));
  204. } else {
  205. DEBUG ((
  206. DEBUG_INFO,
  207. "BHYVE GOP: Using mmio bar @ 0x%lx\n",
  208. MmioDesc->AddrRangeMin
  209. ));
  210. BhyveGetMemregs (Private, &Memregs);
  211. Private->FbSize = Memregs.FbSize;
  212. }
  213. if (MmioDesc != NULL) {
  214. FreePool (MmioDesc);
  215. }
  216. if (EFI_ERROR (Status)) {
  217. goto Done;
  218. }
  219. //
  220. // Check if fbuf frame-buffer BAR is present
  221. //
  222. MmioDesc = NULL;
  223. Status = Private->PciIo->GetBarAttributes (
  224. Private->PciIo,
  225. PCI_BAR_IDX1,
  226. NULL,
  227. (VOID **)&MmioDesc
  228. );
  229. if (EFI_ERROR (Status) ||
  230. (MmioDesc->ResType != ACPI_ADDRESS_SPACE_TYPE_MEM))
  231. {
  232. DEBUG ((DEBUG_INFO, "BHYVE GOP: No frame-buffer bar\n"));
  233. } else {
  234. DEBUG ((
  235. DEBUG_INFO,
  236. "BHYVE GOP: Using frame-buffer bar @ 0x%lx\n",
  237. MmioDesc->AddrRangeMin
  238. ));
  239. Private->FbAddr = MmioDesc->AddrRangeMin;
  240. // XXX assert BAR is >= size
  241. }
  242. if (MmioDesc != NULL) {
  243. FreePool (MmioDesc);
  244. }
  245. if (EFI_ERROR (Status)) {
  246. goto Done;
  247. }
  248. DEBUG ((
  249. DEBUG_INFO,
  250. "BHYVE GOP: Framebuf addr 0x%lx, size %x\n",
  251. Private->FbAddr,
  252. Private->FbSize
  253. ));
  254. Status = EmuGopConstructor (Private);
  255. if (EFI_ERROR (Status)) {
  256. goto Done;
  257. }
  258. //
  259. // Publish the Gop interface to the world
  260. //
  261. Status = gBS->InstallMultipleProtocolInterfaces (
  262. &Private->Handle,
  263. &gEfiGraphicsOutputProtocolGuid,
  264. &Private->GraphicsOutput,
  265. NULL
  266. );
  267. DEBUG ((DEBUG_INFO, "BHYVE framebuffer device started\n"));
  268. //
  269. // Install int10 handler
  270. //
  271. #ifndef CSM_ENABLE
  272. InstallVbeShim (L"Framebuffer", Private->FbAddr);
  273. #endif
  274. Done:
  275. if (EFI_ERROR (Status)) {
  276. if (Private != NULL) {
  277. //
  278. // On Error Free back private data
  279. //
  280. if (Private->ControllerNameTable != NULL) {
  281. FreeUnicodeStringTable (Private->ControllerNameTable);
  282. }
  283. gBS->FreePool (Private);
  284. }
  285. }
  286. return Status;
  287. }
  288. /**
  289. Stops a device controller or a bus controller.
  290. The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
  291. As a result, much of the error checking on the parameters to Stop() has been moved
  292. into this common boot service. It is legal to call Stop() from other locations,
  293. but the following calling restrictions must be followed, or the system behavior will not be deterministic.
  294. 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
  295. same driver's Start() function.
  296. 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
  297. EFI_HANDLE. In addition, all of these handles must have been created in this driver's
  298. Start() function, and the Start() function must have called OpenProtocol() on
  299. ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  300. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  301. @param[in] ControllerHandle A handle to the device being stopped. The handle must
  302. support a bus specific I/O protocol for the driver
  303. to use to stop the device.
  304. @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
  305. @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
  306. if NumberOfChildren is 0.
  307. @retval EFI_SUCCESS The device was stopped.
  308. @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
  309. **/
  310. EFI_STATUS
  311. EFIAPI
  312. EmuGopDriverBindingStop (
  313. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  314. IN EFI_HANDLE Handle,
  315. IN UINTN NumberOfChildren,
  316. IN EFI_HANDLE *ChildHandleBuffer
  317. )
  318. {
  319. EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
  320. EFI_STATUS Status;
  321. GOP_PRIVATE_DATA *Private;
  322. DEBUG ((DEBUG_INFO, "BHYVE framebuffer device stopping\n"));
  323. Status = gBS->OpenProtocol (
  324. Handle,
  325. &gEfiGraphicsOutputProtocolGuid,
  326. (VOID **)&GraphicsOutput,
  327. This->DriverBindingHandle,
  328. Handle,
  329. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  330. );
  331. if (EFI_ERROR (Status)) {
  332. //
  333. // If the GOP interface does not exist the driver is not started
  334. //
  335. return EFI_NOT_STARTED;
  336. }
  337. //
  338. // Get our private context information
  339. //
  340. Private = GOP_PRIVATE_DATA_FROM_THIS (GraphicsOutput);
  341. //
  342. // Remove the SGO interface from the system
  343. //
  344. Status = gBS->UninstallMultipleProtocolInterfaces (
  345. Private->Handle,
  346. &gEfiGraphicsOutputProtocolGuid,
  347. &Private->GraphicsOutput,
  348. NULL
  349. );
  350. if (!EFI_ERROR (Status)) {
  351. //
  352. // Shutdown the hardware
  353. //
  354. Status = EmuGopDestructor (Private);
  355. if (EFI_ERROR (Status)) {
  356. return EFI_DEVICE_ERROR;
  357. }
  358. gBS->CloseProtocol (
  359. Handle,
  360. &gEfiPciIoProtocolGuid,
  361. This->DriverBindingHandle,
  362. Private->Handle
  363. );
  364. //
  365. // Free our instance data
  366. //
  367. FreeUnicodeStringTable (Private->ControllerNameTable);
  368. gBS->FreePool (Private);
  369. }
  370. return Status;
  371. }
  372. ///
  373. /// This protocol provides the services required to determine if a driver supports a given controller.
  374. /// If a controller is supported, then it also provides routines to start and stop the controller.
  375. ///
  376. EFI_DRIVER_BINDING_PROTOCOL gEmuGopDriverBinding = {
  377. EmuGopDriverBindingSupported,
  378. EmuGopDriverBindingStart,
  379. EmuGopDriverBindingStop,
  380. 0xa,
  381. NULL,
  382. NULL
  383. };
  384. /**
  385. The user Entry Point for module EmuGop. The user code starts with this function.
  386. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  387. @param[in] SystemTable A pointer to the EFI System Table.
  388. @retval EFI_SUCCESS The entry point is executed successfully.
  389. @retval other Some error occurs when executing this entry point.
  390. **/
  391. EFI_STATUS
  392. EFIAPI
  393. InitializeEmuGop (
  394. IN EFI_HANDLE ImageHandle,
  395. IN EFI_SYSTEM_TABLE *SystemTable
  396. )
  397. {
  398. EFI_STATUS Status;
  399. Status = EfiLibInstallDriverBindingComponentName2 (
  400. ImageHandle,
  401. SystemTable,
  402. &gEmuGopDriverBinding,
  403. ImageHandle,
  404. &gEmuGopComponentName,
  405. &gEmuGopComponentName2
  406. );
  407. ASSERT_EFI_ERROR (Status);
  408. return Status;
  409. }
  410. STATIC VOID
  411. BhyveGetGraphicsMode (
  412. EFI_PCI_IO_PROTOCOL *PciIo,
  413. UINT16 *Width,
  414. UINT16 *Height,
  415. UINT16 *Depth
  416. )
  417. {
  418. BHYVE_FBUF_MEMREGS BhyveRegs;
  419. UINT64 Offset;
  420. EFI_STATUS Status;
  421. Offset = (UINT64)&BhyveRegs.Width - (UINT64)&BhyveRegs;
  422. Status = PciIo->Mem.Read (
  423. PciIo,
  424. EfiPciIoWidthUint16,
  425. PCI_BAR_IDX0,
  426. Offset,
  427. 3,
  428. &BhyveRegs.Width
  429. );
  430. *Width = BhyveRegs.Width;
  431. *Height = BhyveRegs.Height;
  432. *Depth = BhyveRegs.Depth;
  433. DEBUG ((DEBUG_INFO, "BHYVE Get Graphics Mode: w %d, h %d\n", *Width, *Height));
  434. ASSERT_EFI_ERROR (Status);
  435. }
  436. VOID
  437. BhyveSetGraphicsMode (
  438. GOP_PRIVATE_DATA *Private,
  439. UINT16 Width,
  440. UINT16 Height,
  441. UINT16 Depth
  442. )
  443. {
  444. BHYVE_FBUF_MEMREGS BhyveRegs;
  445. UINT64 Offset;
  446. EFI_STATUS Status;
  447. DEBUG ((DEBUG_INFO, "BHYVE Set Graphics Mode: w %d, h %d\n", Width, Height));
  448. BhyveRegs.Width = Width;
  449. BhyveRegs.Height = Height;
  450. BhyveRegs.Depth = Depth;
  451. Offset = (UINT64)&BhyveRegs.Width - (UINT64)&BhyveRegs;
  452. Status = Private->PciIo->Mem.Write (
  453. Private->PciIo,
  454. EfiPciIoWidthUint16,
  455. PCI_BAR_IDX0,
  456. Offset,
  457. 3,
  458. &BhyveRegs.Width
  459. );
  460. ASSERT_EFI_ERROR (Status);
  461. }
  462. VOID
  463. BhyveGetMemregs (
  464. GOP_PRIVATE_DATA *Private,
  465. BHYVE_FBUF_MEMREGS *Memregs
  466. )
  467. {
  468. EFI_STATUS Status;
  469. Status = Private->PciIo->Mem.Read (
  470. Private->PciIo,
  471. EfiPciIoWidthUint32,
  472. PCI_BAR_IDX0,
  473. 0,
  474. 3,
  475. Memregs
  476. );
  477. ASSERT_EFI_ERROR (Status);
  478. DEBUG ((
  479. DEBUG_INFO,
  480. "BHYVE Get Memregs, size %d width %d height %d\n",
  481. Memregs->FbSize,
  482. Memregs->Width,
  483. Memregs->Height
  484. ));
  485. }