Commands.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /** @file
  2. VirtIo GPU initialization, and commands (primitives) for the GPU device.
  3. Copyright (C) 2016, Red Hat, Inc.
  4. Copyright (c) 2017, AMD Inc, All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include <Library/VirtioLib.h>
  8. #include "VirtioGpu.h"
  9. /**
  10. Configure the VirtIo GPU device that underlies VgpuDev.
  11. @param[in,out] VgpuDev The VGPU_DEV object to set up VirtIo messaging for.
  12. On input, the caller is responsible for having
  13. initialized VgpuDev->VirtIo. On output, VgpuDev->Ring
  14. has been initialized, and synchronous VirtIo GPU
  15. commands (primitives) can be submitted to the device.
  16. @retval EFI_SUCCESS VirtIo GPU configuration successful.
  17. @retval EFI_UNSUPPORTED The host-side configuration of the VirtIo GPU is not
  18. supported by this driver.
  19. @retval Error codes from underlying functions.
  20. **/
  21. EFI_STATUS
  22. VirtioGpuInit (
  23. IN OUT VGPU_DEV *VgpuDev
  24. )
  25. {
  26. UINT8 NextDevStat;
  27. EFI_STATUS Status;
  28. UINT64 Features;
  29. UINT16 QueueSize;
  30. UINT64 RingBaseShift;
  31. //
  32. // Execute virtio-v1.0-cs04, 3.1.1 Driver Requirements: Device
  33. // Initialization.
  34. //
  35. // 1. Reset the device.
  36. //
  37. NextDevStat = 0;
  38. Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
  39. if (EFI_ERROR (Status)) {
  40. goto Failed;
  41. }
  42. //
  43. // 2. Set the ACKNOWLEDGE status bit [...]
  44. //
  45. NextDevStat |= VSTAT_ACK;
  46. Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
  47. if (EFI_ERROR (Status)) {
  48. goto Failed;
  49. }
  50. //
  51. // 3. Set the DRIVER status bit [...]
  52. //
  53. NextDevStat |= VSTAT_DRIVER;
  54. Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
  55. if (EFI_ERROR (Status)) {
  56. goto Failed;
  57. }
  58. //
  59. // 4. Read device feature bits...
  60. //
  61. Status = VgpuDev->VirtIo->GetDeviceFeatures (VgpuDev->VirtIo, &Features);
  62. if (EFI_ERROR (Status)) {
  63. goto Failed;
  64. }
  65. if ((Features & VIRTIO_F_VERSION_1) == 0) {
  66. Status = EFI_UNSUPPORTED;
  67. goto Failed;
  68. }
  69. //
  70. // We only want the most basic 2D features.
  71. //
  72. Features &= VIRTIO_F_VERSION_1 | VIRTIO_F_IOMMU_PLATFORM;
  73. //
  74. // ... and write the subset of feature bits understood by the [...] driver to
  75. // the device. [...]
  76. // 5. Set the FEATURES_OK status bit.
  77. // 6. Re-read device status to ensure the FEATURES_OK bit is still set [...]
  78. //
  79. Status = Virtio10WriteFeatures (VgpuDev->VirtIo, Features, &NextDevStat);
  80. if (EFI_ERROR (Status)) {
  81. goto Failed;
  82. }
  83. //
  84. // 7. Perform device-specific setup, including discovery of virtqueues for
  85. // the device [...]
  86. //
  87. Status = VgpuDev->VirtIo->SetQueueSel (
  88. VgpuDev->VirtIo,
  89. VIRTIO_GPU_CONTROL_QUEUE
  90. );
  91. if (EFI_ERROR (Status)) {
  92. goto Failed;
  93. }
  94. Status = VgpuDev->VirtIo->GetQueueNumMax (VgpuDev->VirtIo, &QueueSize);
  95. if (EFI_ERROR (Status)) {
  96. goto Failed;
  97. }
  98. //
  99. // We implement each VirtIo GPU command that we use with two descriptors:
  100. // request, response.
  101. //
  102. if (QueueSize < 2) {
  103. Status = EFI_UNSUPPORTED;
  104. goto Failed;
  105. }
  106. //
  107. // [...] population of virtqueues [...]
  108. //
  109. Status = VirtioRingInit (VgpuDev->VirtIo, QueueSize, &VgpuDev->Ring);
  110. if (EFI_ERROR (Status)) {
  111. goto Failed;
  112. }
  113. //
  114. // If anything fails from here on, we have to release the ring.
  115. //
  116. Status = VirtioRingMap (
  117. VgpuDev->VirtIo,
  118. &VgpuDev->Ring,
  119. &RingBaseShift,
  120. &VgpuDev->RingMap
  121. );
  122. if (EFI_ERROR (Status)) {
  123. goto ReleaseQueue;
  124. }
  125. //
  126. // If anything fails from here on, we have to unmap the ring.
  127. //
  128. Status = VgpuDev->VirtIo->SetQueueAddress (
  129. VgpuDev->VirtIo,
  130. &VgpuDev->Ring,
  131. RingBaseShift
  132. );
  133. if (EFI_ERROR (Status)) {
  134. goto UnmapQueue;
  135. }
  136. //
  137. // 8. Set the DRIVER_OK status bit.
  138. //
  139. NextDevStat |= VSTAT_DRIVER_OK;
  140. Status = VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
  141. if (EFI_ERROR (Status)) {
  142. goto UnmapQueue;
  143. }
  144. return EFI_SUCCESS;
  145. UnmapQueue:
  146. VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap);
  147. ReleaseQueue:
  148. VirtioRingUninit (VgpuDev->VirtIo, &VgpuDev->Ring);
  149. Failed:
  150. //
  151. // If any of these steps go irrecoverably wrong, the driver SHOULD set the
  152. // FAILED status bit to indicate that it has given up on the device (it can
  153. // reset the device later to restart if desired). [...]
  154. //
  155. // VirtIo access failure here should not mask the original error.
  156. //
  157. NextDevStat |= VSTAT_FAILED;
  158. VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, NextDevStat);
  159. return Status;
  160. }
  161. /**
  162. De-configure the VirtIo GPU device that underlies VgpuDev.
  163. @param[in,out] VgpuDev The VGPU_DEV object to tear down VirtIo messaging
  164. for. On input, the caller is responsible for having
  165. called VirtioGpuInit(). On output, VgpuDev->Ring has
  166. been uninitialized; VirtIo GPU commands (primitives)
  167. can no longer be submitted to the device.
  168. **/
  169. VOID
  170. VirtioGpuUninit (
  171. IN OUT VGPU_DEV *VgpuDev
  172. )
  173. {
  174. //
  175. // Resetting the VirtIo device makes it release its resources and forget its
  176. // configuration.
  177. //
  178. VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0);
  179. VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, VgpuDev->RingMap);
  180. VirtioRingUninit (VgpuDev->VirtIo, &VgpuDev->Ring);
  181. }
  182. /**
  183. Allocate, zero and map memory, for bus master common buffer operation, to be
  184. attached as backing store to a host-side VirtIo GPU resource.
  185. @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
  186. device.
  187. @param[in] NumberOfPages The number of whole pages to allocate and map.
  188. @param[out] HostAddress The system memory address of the allocated area.
  189. @param[out] DeviceAddress The bus master device address of the allocated
  190. area. The VirtIo GPU device may be programmed to
  191. access the allocated area through DeviceAddress;
  192. DeviceAddress is to be passed to the
  193. VirtioGpuResourceAttachBacking() function, as the
  194. BackingStoreDeviceAddress parameter.
  195. @param[out] Mapping A resulting token to pass to
  196. VirtioGpuUnmapAndFreeBackingStore().
  197. @retval EFI_SUCCESS The requested number of pages has been allocated, zeroed
  198. and mapped.
  199. @return Status codes propagated from
  200. VgpuDev->VirtIo->AllocateSharedPages() and
  201. VirtioMapAllBytesInSharedBuffer().
  202. **/
  203. EFI_STATUS
  204. VirtioGpuAllocateZeroAndMapBackingStore (
  205. IN VGPU_DEV *VgpuDev,
  206. IN UINTN NumberOfPages,
  207. OUT VOID **HostAddress,
  208. OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
  209. OUT VOID **Mapping
  210. )
  211. {
  212. EFI_STATUS Status;
  213. VOID *NewHostAddress;
  214. Status = VgpuDev->VirtIo->AllocateSharedPages (
  215. VgpuDev->VirtIo,
  216. NumberOfPages,
  217. &NewHostAddress
  218. );
  219. if (EFI_ERROR (Status)) {
  220. return Status;
  221. }
  222. //
  223. // Avoid exposing stale data to the device even temporarily: zero the area
  224. // before mapping it.
  225. //
  226. ZeroMem (NewHostAddress, EFI_PAGES_TO_SIZE (NumberOfPages));
  227. Status = VirtioMapAllBytesInSharedBuffer (
  228. VgpuDev->VirtIo, // VirtIo
  229. VirtioOperationBusMasterCommonBuffer, // Operation
  230. NewHostAddress, // HostAddress
  231. EFI_PAGES_TO_SIZE (NumberOfPages), // NumberOfBytes
  232. DeviceAddress, // DeviceAddress
  233. Mapping // Mapping
  234. );
  235. if (EFI_ERROR (Status)) {
  236. goto FreeSharedPages;
  237. }
  238. *HostAddress = NewHostAddress;
  239. return EFI_SUCCESS;
  240. FreeSharedPages:
  241. VgpuDev->VirtIo->FreeSharedPages (
  242. VgpuDev->VirtIo,
  243. NumberOfPages,
  244. NewHostAddress
  245. );
  246. return Status;
  247. }
  248. /**
  249. Unmap and free memory originally allocated and mapped with
  250. VirtioGpuAllocateZeroAndMapBackingStore().
  251. If the memory allocated and mapped with
  252. VirtioGpuAllocateZeroAndMapBackingStore() was attached to a host-side VirtIo
  253. GPU resource with VirtioGpuResourceAttachBacking(), then the caller is
  254. responsible for detaching the backing store from the same resource, with
  255. VirtioGpuResourceDetachBacking(), before calling this function.
  256. @param[in] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
  257. device.
  258. @param[in] NumberOfPages The NumberOfPages parameter originally passed to
  259. VirtioGpuAllocateZeroAndMapBackingStore().
  260. @param[in] HostAddress The HostAddress value originally output by
  261. VirtioGpuAllocateZeroAndMapBackingStore().
  262. @param[in] Mapping The token that was originally output by
  263. VirtioGpuAllocateZeroAndMapBackingStore().
  264. **/
  265. VOID
  266. VirtioGpuUnmapAndFreeBackingStore (
  267. IN VGPU_DEV *VgpuDev,
  268. IN UINTN NumberOfPages,
  269. IN VOID *HostAddress,
  270. IN VOID *Mapping
  271. )
  272. {
  273. VgpuDev->VirtIo->UnmapSharedBuffer (
  274. VgpuDev->VirtIo,
  275. Mapping
  276. );
  277. VgpuDev->VirtIo->FreeSharedPages (
  278. VgpuDev->VirtIo,
  279. NumberOfPages,
  280. HostAddress
  281. );
  282. }
  283. /**
  284. EFI_EVENT_NOTIFY function for the VGPU_DEV.ExitBoot event. It resets the
  285. VirtIo device, causing it to release its resources and to forget its
  286. configuration.
  287. This function may only be called (that is, VGPU_DEV.ExitBoot may only be
  288. signaled) after VirtioGpuInit() returns and before VirtioGpuUninit() is
  289. called.
  290. @param[in] Event Event whose notification function is being invoked.
  291. @param[in] Context Pointer to the associated VGPU_DEV object.
  292. **/
  293. VOID
  294. EFIAPI
  295. VirtioGpuExitBoot (
  296. IN EFI_EVENT Event,
  297. IN VOID *Context
  298. )
  299. {
  300. VGPU_DEV *VgpuDev;
  301. DEBUG ((DEBUG_VERBOSE, "%a: Context=0x%p\n", __FUNCTION__, Context));
  302. VgpuDev = Context;
  303. VgpuDev->VirtIo->SetDeviceStatus (VgpuDev->VirtIo, 0);
  304. }
  305. /**
  306. Internal utility function that sends a request to the VirtIo GPU device
  307. model, awaits the answer from the host, and returns a status.
  308. @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
  309. device. The caller is responsible to have
  310. successfully invoked VirtioGpuInit() on VgpuDev
  311. previously, while VirtioGpuUninit() must not have
  312. been called on VgpuDev.
  313. @param[in] RequestType The type of the request. The caller is responsible
  314. for providing a VirtioGpuCmd* RequestType which, on
  315. success, elicits a VirtioGpuRespOkNodata response
  316. from the host.
  317. @param[in] Fence Whether to enable fencing for this request. Fencing
  318. forces the host to complete the command before
  319. producing a response. If Fence is TRUE, then
  320. VgpuDev->FenceId is consumed, and incremented.
  321. @param[in,out] Header Pointer to the caller-allocated request object. The
  322. request must start with VIRTIO_GPU_CONTROL_HEADER.
  323. This function overwrites all fields of Header before
  324. submitting the request to the host:
  325. - it sets Type from RequestType,
  326. - it sets Flags and FenceId based on Fence,
  327. - it zeroes CtxId and Padding.
  328. @param[in] RequestSize Size of the entire caller-allocated request object,
  329. including the leading VIRTIO_GPU_CONTROL_HEADER.
  330. @param[in] ResponseType The type of the response (VirtioGpuResp*).
  331. @param[in,out] Response Pointer to the caller-allocated response object. The
  332. request must start with VIRTIO_GPU_CONTROL_HEADER.
  333. @param[in] ResponseSize Size of the entire caller-allocated response object,
  334. including the leading VIRTIO_GPU_CONTROL_HEADER.
  335. @retval EFI_SUCCESS Operation successful.
  336. @retval EFI_DEVICE_ERROR The host rejected the request. The host error
  337. code has been logged on the DEBUG_ERROR level.
  338. @return Codes for unexpected errors in VirtIo
  339. messaging, or request/response
  340. mapping/unmapping.
  341. **/
  342. STATIC
  343. EFI_STATUS
  344. VirtioGpuSendCommandWithReply (
  345. IN OUT VGPU_DEV *VgpuDev,
  346. IN VIRTIO_GPU_CONTROL_TYPE RequestType,
  347. IN BOOLEAN Fence,
  348. IN OUT volatile VIRTIO_GPU_CONTROL_HEADER *Header,
  349. IN UINTN RequestSize,
  350. IN VIRTIO_GPU_CONTROL_TYPE ResponseType,
  351. IN OUT volatile VIRTIO_GPU_CONTROL_HEADER *Response,
  352. IN UINTN ResponseSize
  353. )
  354. {
  355. DESC_INDICES Indices;
  356. EFI_STATUS Status;
  357. UINT32 ResponseSizeRet;
  358. EFI_PHYSICAL_ADDRESS RequestDeviceAddress;
  359. VOID *RequestMap;
  360. EFI_PHYSICAL_ADDRESS ResponseDeviceAddress;
  361. VOID *ResponseMap;
  362. //
  363. // Initialize Header.
  364. //
  365. Header->Type = RequestType;
  366. if (Fence) {
  367. Header->Flags = VIRTIO_GPU_FLAG_FENCE;
  368. Header->FenceId = VgpuDev->FenceId++;
  369. } else {
  370. Header->Flags = 0;
  371. Header->FenceId = 0;
  372. }
  373. Header->CtxId = 0;
  374. Header->Padding = 0;
  375. ASSERT (RequestSize >= sizeof *Header);
  376. ASSERT (RequestSize <= MAX_UINT32);
  377. //
  378. // Map request and response to bus master device addresses.
  379. //
  380. Status = VirtioMapAllBytesInSharedBuffer (
  381. VgpuDev->VirtIo,
  382. VirtioOperationBusMasterRead,
  383. (VOID *)Header,
  384. RequestSize,
  385. &RequestDeviceAddress,
  386. &RequestMap
  387. );
  388. if (EFI_ERROR (Status)) {
  389. return Status;
  390. }
  391. Status = VirtioMapAllBytesInSharedBuffer (
  392. VgpuDev->VirtIo,
  393. VirtioOperationBusMasterWrite,
  394. (VOID *)Response,
  395. ResponseSize,
  396. &ResponseDeviceAddress,
  397. &ResponseMap
  398. );
  399. if (EFI_ERROR (Status)) {
  400. goto UnmapRequest;
  401. }
  402. //
  403. // Compose the descriptor chain.
  404. //
  405. VirtioPrepare (&VgpuDev->Ring, &Indices);
  406. VirtioAppendDesc (
  407. &VgpuDev->Ring,
  408. RequestDeviceAddress,
  409. (UINT32)RequestSize,
  410. VRING_DESC_F_NEXT,
  411. &Indices
  412. );
  413. VirtioAppendDesc (
  414. &VgpuDev->Ring,
  415. ResponseDeviceAddress,
  416. (UINT32)ResponseSize,
  417. VRING_DESC_F_WRITE,
  418. &Indices
  419. );
  420. //
  421. // Send the command.
  422. //
  423. Status = VirtioFlush (
  424. VgpuDev->VirtIo,
  425. VIRTIO_GPU_CONTROL_QUEUE,
  426. &VgpuDev->Ring,
  427. &Indices,
  428. &ResponseSizeRet
  429. );
  430. if (EFI_ERROR (Status)) {
  431. goto UnmapResponse;
  432. }
  433. //
  434. // Verify response size.
  435. //
  436. if (ResponseSize != ResponseSizeRet) {
  437. DEBUG ((
  438. DEBUG_ERROR,
  439. "%a: malformed response to Request=0x%x\n",
  440. __FUNCTION__,
  441. (UINT32)RequestType
  442. ));
  443. Status = EFI_PROTOCOL_ERROR;
  444. goto UnmapResponse;
  445. }
  446. //
  447. // Unmap response and request, in reverse order of mapping. On error, the
  448. // respective mapping is invalidated anyway, only the data may not have been
  449. // committed to system memory (in case of VirtioOperationBusMasterWrite).
  450. //
  451. Status = VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, ResponseMap);
  452. if (EFI_ERROR (Status)) {
  453. goto UnmapRequest;
  454. }
  455. Status = VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, RequestMap);
  456. if (EFI_ERROR (Status)) {
  457. return Status;
  458. }
  459. //
  460. // Parse the response.
  461. //
  462. if (Response->Type == (UINT32)ResponseType) {
  463. return EFI_SUCCESS;
  464. }
  465. DEBUG ((
  466. DEBUG_ERROR,
  467. "%a: Request=0x%x Response=0x%x (expected 0x%x)\n",
  468. __FUNCTION__,
  469. (UINT32)RequestType,
  470. Response->Type,
  471. ResponseType
  472. ));
  473. return EFI_DEVICE_ERROR;
  474. UnmapResponse:
  475. VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, ResponseMap);
  476. UnmapRequest:
  477. VgpuDev->VirtIo->UnmapSharedBuffer (VgpuDev->VirtIo, RequestMap);
  478. return Status;
  479. }
  480. /**
  481. Simplified version of VirtioGpuSendCommandWithReply() for commands
  482. which do not send back any data.
  483. **/
  484. STATIC
  485. EFI_STATUS
  486. VirtioGpuSendCommand (
  487. IN OUT VGPU_DEV *VgpuDev,
  488. IN VIRTIO_GPU_CONTROL_TYPE RequestType,
  489. IN BOOLEAN Fence,
  490. IN OUT volatile VIRTIO_GPU_CONTROL_HEADER *Header,
  491. IN UINTN RequestSize
  492. )
  493. {
  494. volatile VIRTIO_GPU_CONTROL_HEADER Response;
  495. return VirtioGpuSendCommandWithReply (
  496. VgpuDev,
  497. RequestType,
  498. Fence,
  499. Header,
  500. RequestSize,
  501. VirtioGpuRespOkNodata,
  502. &Response,
  503. sizeof (Response)
  504. );
  505. }
  506. /**
  507. The following functions send requests to the VirtIo GPU device model, await
  508. the answer from the host, and return a status. They share the following
  509. interface details:
  510. @param[in,out] VgpuDev The VGPU_DEV object that represents the VirtIo GPU
  511. device. The caller is responsible to have
  512. successfully invoked VirtioGpuInit() on VgpuDev
  513. previously, while VirtioGpuUninit() must not have
  514. been called on VgpuDev.
  515. @retval EFI_INVALID_PARAMETER Invalid command-specific parameters were
  516. detected by this driver.
  517. @retval EFI_SUCCESS Operation successful.
  518. @retval EFI_DEVICE_ERROR The host rejected the request. The host error
  519. code has been logged on the DEBUG_ERROR level.
  520. @return Codes for unexpected errors in VirtIo
  521. messaging.
  522. For the command-specific parameters, please consult the GPU Device section of
  523. the VirtIo 1.0 specification (see references in
  524. "OvmfPkg/Include/IndustryStandard/VirtioGpu.h").
  525. **/
  526. EFI_STATUS
  527. VirtioGpuResourceCreate2d (
  528. IN OUT VGPU_DEV *VgpuDev,
  529. IN UINT32 ResourceId,
  530. IN VIRTIO_GPU_FORMATS Format,
  531. IN UINT32 Width,
  532. IN UINT32 Height
  533. )
  534. {
  535. volatile VIRTIO_GPU_RESOURCE_CREATE_2D Request;
  536. if (ResourceId == 0) {
  537. return EFI_INVALID_PARAMETER;
  538. }
  539. Request.ResourceId = ResourceId;
  540. Request.Format = (UINT32)Format;
  541. Request.Width = Width;
  542. Request.Height = Height;
  543. return VirtioGpuSendCommand (
  544. VgpuDev,
  545. VirtioGpuCmdResourceCreate2d,
  546. FALSE, // Fence
  547. &Request.Header,
  548. sizeof Request
  549. );
  550. }
  551. EFI_STATUS
  552. VirtioGpuResourceUnref (
  553. IN OUT VGPU_DEV *VgpuDev,
  554. IN UINT32 ResourceId
  555. )
  556. {
  557. volatile VIRTIO_GPU_RESOURCE_UNREF Request;
  558. if (ResourceId == 0) {
  559. return EFI_INVALID_PARAMETER;
  560. }
  561. Request.ResourceId = ResourceId;
  562. Request.Padding = 0;
  563. return VirtioGpuSendCommand (
  564. VgpuDev,
  565. VirtioGpuCmdResourceUnref,
  566. FALSE, // Fence
  567. &Request.Header,
  568. sizeof Request
  569. );
  570. }
  571. EFI_STATUS
  572. VirtioGpuResourceAttachBacking (
  573. IN OUT VGPU_DEV *VgpuDev,
  574. IN UINT32 ResourceId,
  575. IN EFI_PHYSICAL_ADDRESS BackingStoreDeviceAddress,
  576. IN UINTN NumberOfPages
  577. )
  578. {
  579. volatile VIRTIO_GPU_RESOURCE_ATTACH_BACKING Request;
  580. if (ResourceId == 0) {
  581. return EFI_INVALID_PARAMETER;
  582. }
  583. Request.ResourceId = ResourceId;
  584. Request.NrEntries = 1;
  585. Request.Entry.Addr = BackingStoreDeviceAddress;
  586. Request.Entry.Length = (UINT32)EFI_PAGES_TO_SIZE (NumberOfPages);
  587. Request.Entry.Padding = 0;
  588. return VirtioGpuSendCommand (
  589. VgpuDev,
  590. VirtioGpuCmdResourceAttachBacking,
  591. FALSE, // Fence
  592. &Request.Header,
  593. sizeof Request
  594. );
  595. }
  596. EFI_STATUS
  597. VirtioGpuResourceDetachBacking (
  598. IN OUT VGPU_DEV *VgpuDev,
  599. IN UINT32 ResourceId
  600. )
  601. {
  602. volatile VIRTIO_GPU_RESOURCE_DETACH_BACKING Request;
  603. if (ResourceId == 0) {
  604. return EFI_INVALID_PARAMETER;
  605. }
  606. Request.ResourceId = ResourceId;
  607. Request.Padding = 0;
  608. //
  609. // In this case, we set Fence to TRUE, because after this function returns,
  610. // the caller might reasonably want to repurpose the backing pages
  611. // immediately. Thus we should ensure that the host releases all references
  612. // to the backing pages before we return.
  613. //
  614. return VirtioGpuSendCommand (
  615. VgpuDev,
  616. VirtioGpuCmdResourceDetachBacking,
  617. TRUE, // Fence
  618. &Request.Header,
  619. sizeof Request
  620. );
  621. }
  622. EFI_STATUS
  623. VirtioGpuSetScanout (
  624. IN OUT VGPU_DEV *VgpuDev,
  625. IN UINT32 X,
  626. IN UINT32 Y,
  627. IN UINT32 Width,
  628. IN UINT32 Height,
  629. IN UINT32 ScanoutId,
  630. IN UINT32 ResourceId
  631. )
  632. {
  633. volatile VIRTIO_GPU_SET_SCANOUT Request;
  634. //
  635. // Unlike for most other commands, ResourceId=0 is valid; it
  636. // is used to disable a scanout.
  637. //
  638. Request.Rectangle.X = X;
  639. Request.Rectangle.Y = Y;
  640. Request.Rectangle.Width = Width;
  641. Request.Rectangle.Height = Height;
  642. Request.ScanoutId = ScanoutId;
  643. Request.ResourceId = ResourceId;
  644. return VirtioGpuSendCommand (
  645. VgpuDev,
  646. VirtioGpuCmdSetScanout,
  647. FALSE, // Fence
  648. &Request.Header,
  649. sizeof Request
  650. );
  651. }
  652. EFI_STATUS
  653. VirtioGpuTransferToHost2d (
  654. IN OUT VGPU_DEV *VgpuDev,
  655. IN UINT32 X,
  656. IN UINT32 Y,
  657. IN UINT32 Width,
  658. IN UINT32 Height,
  659. IN UINT64 Offset,
  660. IN UINT32 ResourceId
  661. )
  662. {
  663. volatile VIRTIO_GPU_CMD_TRANSFER_TO_HOST_2D Request;
  664. if (ResourceId == 0) {
  665. return EFI_INVALID_PARAMETER;
  666. }
  667. Request.Rectangle.X = X;
  668. Request.Rectangle.Y = Y;
  669. Request.Rectangle.Width = Width;
  670. Request.Rectangle.Height = Height;
  671. Request.Offset = Offset;
  672. Request.ResourceId = ResourceId;
  673. Request.Padding = 0;
  674. return VirtioGpuSendCommand (
  675. VgpuDev,
  676. VirtioGpuCmdTransferToHost2d,
  677. FALSE, // Fence
  678. &Request.Header,
  679. sizeof Request
  680. );
  681. }
  682. EFI_STATUS
  683. VirtioGpuResourceFlush (
  684. IN OUT VGPU_DEV *VgpuDev,
  685. IN UINT32 X,
  686. IN UINT32 Y,
  687. IN UINT32 Width,
  688. IN UINT32 Height,
  689. IN UINT32 ResourceId
  690. )
  691. {
  692. volatile VIRTIO_GPU_RESOURCE_FLUSH Request;
  693. if (ResourceId == 0) {
  694. return EFI_INVALID_PARAMETER;
  695. }
  696. Request.Rectangle.X = X;
  697. Request.Rectangle.Y = Y;
  698. Request.Rectangle.Width = Width;
  699. Request.Rectangle.Height = Height;
  700. Request.ResourceId = ResourceId;
  701. Request.Padding = 0;
  702. return VirtioGpuSendCommand (
  703. VgpuDev,
  704. VirtioGpuCmdResourceFlush,
  705. FALSE, // Fence
  706. &Request.Header,
  707. sizeof Request
  708. );
  709. }
  710. EFI_STATUS
  711. VirtioGpuGetDisplayInfo (
  712. IN OUT VGPU_DEV *VgpuDev,
  713. volatile VIRTIO_GPU_RESP_DISPLAY_INFO *Response
  714. )
  715. {
  716. volatile VIRTIO_GPU_CONTROL_HEADER Request;
  717. return VirtioGpuSendCommandWithReply (
  718. VgpuDev,
  719. VirtioGpuCmdGetDisplayInfo,
  720. FALSE, // Fence
  721. &Request,
  722. sizeof Request,
  723. VirtioGpuRespOkDisplayInfo,
  724. &Response->Header,
  725. sizeof *Response
  726. );
  727. }