]>
Commit | Line | Data |
---|---|---|
263559b8 | 1 | /** @file\r |
2 | \r | |
3 | Utility functions used by virtio device drivers.\r | |
4 | \r | |
8bc951a2 | 5 | Copyright (C) 2012-2016, Red Hat, Inc.\r |
56f65ed8 | 6 | Portion of Copyright (C) 2013, ARM Ltd.\r |
0a78d754 | 7 | Copyright (C) 2017, AMD Inc, All rights reserved.<BR>\r |
263559b8 | 8 | \r |
b26f0cf9 | 9 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
263559b8 | 10 | \r |
11 | **/\r | |
12 | \r | |
e371e7e5 | 13 | #include <Library/BaseLib.h>\r |
263559b8 | 14 | #include <Library/BaseMemoryLib.h>\r |
15 | #include <Library/DebugLib.h>\r | |
e371e7e5 | 16 | #include <Library/UefiBootServicesTableLib.h>\r |
263559b8 | 17 | \r |
18 | #include <Library/VirtioLib.h>\r | |
19 | \r | |
20 | \r | |
263559b8 | 21 | /**\r |
22 | \r | |
23 | Configure a virtio ring.\r | |
24 | \r | |
25 | This function sets up internal storage (the guest-host communication area)\r | |
26 | and lays out several "navigation" (ie. no-ownership) pointers to parts of\r | |
27 | that storage.\r | |
28 | \r | |
29 | Relevant sections from the virtio-0.9.5 spec:\r | |
30 | - 1.1 Virtqueues,\r | |
31 | - 2.3 Virtqueue Configuration.\r | |
32 | \r | |
fc2c1543 BS |
33 | @param[in] VirtIo The virtio device which will use the ring.\r |
34 | \r | |
263559b8 | 35 | @param[in] The number of descriptors to allocate for the\r |
36 | virtio ring, as requested by the host.\r | |
37 | \r | |
38 | @param[out] Ring The virtio ring to set up.\r | |
39 | \r | |
b0338c53 BS |
40 | @return Status codes propagated from\r |
41 | VirtIo->AllocateSharedPages().\r | |
263559b8 | 42 | \r |
43 | @retval EFI_SUCCESS Allocation and setup successful. Ring->Base\r | |
44 | (and nothing else) is responsible for\r | |
45 | deallocation.\r | |
46 | \r | |
47 | **/\r | |
48 | EFI_STATUS\r | |
49 | EFIAPI\r | |
50 | VirtioRingInit (\r | |
fc2c1543 BS |
51 | IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r |
52 | IN UINT16 QueueSize,\r | |
53 | OUT VRING *Ring\r | |
263559b8 | 54 | )\r |
55 | {\r | |
b0338c53 | 56 | EFI_STATUS Status;\r |
263559b8 | 57 | UINTN RingSize;\r |
58 | volatile UINT8 *RingPagesPtr;\r | |
59 | \r | |
60 | RingSize = ALIGN_VALUE (\r | |
61 | sizeof *Ring->Desc * QueueSize +\r | |
62 | sizeof *Ring->Avail.Flags +\r | |
63 | sizeof *Ring->Avail.Idx +\r | |
64 | sizeof *Ring->Avail.Ring * QueueSize +\r | |
65 | sizeof *Ring->Avail.UsedEvent,\r | |
66 | EFI_PAGE_SIZE);\r | |
67 | \r | |
68 | RingSize += ALIGN_VALUE (\r | |
69 | sizeof *Ring->Used.Flags +\r | |
70 | sizeof *Ring->Used.Idx +\r | |
71 | sizeof *Ring->Used.UsedElem * QueueSize +\r | |
72 | sizeof *Ring->Used.AvailEvent,\r | |
73 | EFI_PAGE_SIZE);\r | |
74 | \r | |
b0338c53 BS |
75 | //\r |
76 | // Allocate a shared ring buffer\r | |
77 | //\r | |
263559b8 | 78 | Ring->NumPages = EFI_SIZE_TO_PAGES (RingSize);\r |
b0338c53 BS |
79 | Status = VirtIo->AllocateSharedPages (\r |
80 | VirtIo,\r | |
81 | Ring->NumPages,\r | |
82 | &Ring->Base\r | |
83 | );\r | |
84 | if (EFI_ERROR (Status)) {\r | |
85 | return Status;\r | |
263559b8 | 86 | }\r |
87 | SetMem (Ring->Base, RingSize, 0x00);\r | |
88 | RingPagesPtr = Ring->Base;\r | |
89 | \r | |
90 | Ring->Desc = (volatile VOID *) RingPagesPtr;\r | |
91 | RingPagesPtr += sizeof *Ring->Desc * QueueSize;\r | |
92 | \r | |
93 | Ring->Avail.Flags = (volatile VOID *) RingPagesPtr;\r | |
94 | RingPagesPtr += sizeof *Ring->Avail.Flags;\r | |
95 | \r | |
96 | Ring->Avail.Idx = (volatile VOID *) RingPagesPtr;\r | |
97 | RingPagesPtr += sizeof *Ring->Avail.Idx;\r | |
98 | \r | |
99 | Ring->Avail.Ring = (volatile VOID *) RingPagesPtr;\r | |
100 | RingPagesPtr += sizeof *Ring->Avail.Ring * QueueSize;\r | |
101 | \r | |
102 | Ring->Avail.UsedEvent = (volatile VOID *) RingPagesPtr;\r | |
103 | RingPagesPtr += sizeof *Ring->Avail.UsedEvent;\r | |
104 | \r | |
105 | RingPagesPtr = (volatile UINT8 *) Ring->Base +\r | |
106 | ALIGN_VALUE (RingPagesPtr - (volatile UINT8 *) Ring->Base,\r | |
107 | EFI_PAGE_SIZE);\r | |
108 | \r | |
109 | Ring->Used.Flags = (volatile VOID *) RingPagesPtr;\r | |
110 | RingPagesPtr += sizeof *Ring->Used.Flags;\r | |
111 | \r | |
112 | Ring->Used.Idx = (volatile VOID *) RingPagesPtr;\r | |
113 | RingPagesPtr += sizeof *Ring->Used.Idx;\r | |
114 | \r | |
115 | Ring->Used.UsedElem = (volatile VOID *) RingPagesPtr;\r | |
116 | RingPagesPtr += sizeof *Ring->Used.UsedElem * QueueSize;\r | |
117 | \r | |
118 | Ring->Used.AvailEvent = (volatile VOID *) RingPagesPtr;\r | |
119 | RingPagesPtr += sizeof *Ring->Used.AvailEvent;\r | |
120 | \r | |
121 | Ring->QueueSize = QueueSize;\r | |
122 | return EFI_SUCCESS;\r | |
123 | }\r | |
124 | \r | |
125 | \r | |
126 | /**\r | |
127 | \r | |
128 | Tear down the internal resources of a configured virtio ring.\r | |
129 | \r | |
130 | The caller is responsible to stop the host from using this ring before\r | |
131 | invoking this function: the VSTAT_DRIVER_OK bit must be clear in\r | |
132 | VhdrDeviceStatus.\r | |
133 | \r | |
fc2c1543 BS |
134 | @param[in] VirtIo The virtio device which was using the ring.\r |
135 | \r | |
136 | @param[out] Ring The virtio ring to clean up.\r | |
263559b8 | 137 | \r |
138 | **/\r | |
139 | VOID\r | |
140 | EFIAPI\r | |
141 | VirtioRingUninit (\r | |
fc2c1543 BS |
142 | IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r |
143 | IN OUT VRING *Ring\r | |
263559b8 | 144 | )\r |
145 | {\r | |
b0338c53 | 146 | VirtIo->FreeSharedPages (VirtIo, Ring->NumPages, Ring->Base);\r |
263559b8 | 147 | SetMem (Ring, sizeof *Ring, 0x00);\r |
148 | }\r | |
149 | \r | |
150 | \r | |
e371e7e5 | 151 | /**\r |
152 | \r | |
153 | Turn off interrupt notifications from the host, and prepare for appending\r | |
154 | multiple descriptors to the virtio ring.\r | |
155 | \r | |
156 | The calling driver must be in VSTAT_DRIVER_OK state.\r | |
157 | \r | |
f2965f4e | 158 | @param[in,out] Ring The virtio ring we intend to append descriptors to.\r |
e371e7e5 | 159 | \r |
160 | @param[out] Indices The DESC_INDICES structure to initialize.\r | |
161 | \r | |
162 | **/\r | |
163 | VOID\r | |
164 | EFIAPI\r | |
165 | VirtioPrepare (\r | |
166 | IN OUT VRING *Ring,\r | |
167 | OUT DESC_INDICES *Indices\r | |
168 | )\r | |
169 | {\r | |
170 | //\r | |
171 | // Prepare for virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device.\r | |
172 | // We're going to poll the answer, the host should not send an interrupt.\r | |
173 | //\r | |
174 | *Ring->Avail.Flags = (UINT16) VRING_AVAIL_F_NO_INTERRUPT;\r | |
175 | \r | |
176 | //\r | |
177 | // Prepare for virtio-0.9.5, 2.4.1 Supplying Buffers to the Device.\r | |
178 | //\r | |
635a3ca2 | 179 | // Since we support only one in-flight descriptor chain, we can always build\r |
180 | // that chain starting at entry #0 of the descriptor table.\r | |
181 | //\r | |
182 | Indices->HeadDescIdx = 0;\r | |
183 | Indices->NextDescIdx = Indices->HeadDescIdx;\r | |
e371e7e5 | 184 | }\r |
185 | \r | |
263559b8 | 186 | /**\r |
187 | \r | |
188 | Append a contiguous buffer for transmission / reception via the virtio ring.\r | |
189 | \r | |
635a3ca2 | 190 | This function implements the following section from virtio-0.9.5:\r |
263559b8 | 191 | - 2.4.1.1 Placing Buffers into the Descriptor Table\r |
263559b8 | 192 | \r |
193 | Free space is taken as granted, since the individual drivers support only\r | |
194 | synchronous requests and host side status is processed in lock-step with\r | |
195 | request submission. It is the calling driver's responsibility to verify the\r | |
196 | ring size in advance.\r | |
197 | \r | |
e371e7e5 | 198 | The caller is responsible for initializing *Indices with VirtioPrepare()\r |
199 | first.\r | |
200 | \r | |
4b725858 BS |
201 | @param[in,out] Ring The virtio ring to append the buffer to,\r |
202 | as a descriptor.\r | |
11a5fdf4 | 203 | \r |
4b725858 BS |
204 | @param[in] BufferDeviceAddress (Bus master device) start address of the\r |
205 | transmit / receive buffer.\r | |
11a5fdf4 | 206 | \r |
4b725858 | 207 | @param[in] BufferSize Number of bytes to transmit or receive.\r |
11a5fdf4 | 208 | \r |
4b725858 BS |
209 | @param[in] Flags A bitmask of VRING_DESC_F_* flags. The\r |
210 | caller computes this mask dependent on\r | |
211 | further buffers to append and transfer\r | |
212 | direction. VRING_DESC_F_INDIRECT is\r | |
213 | unsupported. The VRING_DESC.Next field is\r | |
214 | always set, but the host only interprets\r | |
215 | it dependent on VRING_DESC_F_NEXT.\r | |
11a5fdf4 | 216 | \r |
4b725858 BS |
217 | @param[in,out] Indices Indices->HeadDescIdx is not accessed.\r |
218 | On input, Indices->NextDescIdx identifies\r | |
219 | the next descriptor to carry the buffer.\r | |
220 | On output, Indices->NextDescIdx is\r | |
221 | incremented by one, modulo 2^16.\r | |
263559b8 | 222 | \r |
223 | **/\r | |
224 | VOID\r | |
225 | EFIAPI\r | |
7fcacd6c | 226 | VirtioAppendDesc (\r |
e371e7e5 | 227 | IN OUT VRING *Ring,\r |
4b725858 | 228 | IN UINT64 BufferDeviceAddress,\r |
e371e7e5 | 229 | IN UINT32 BufferSize,\r |
230 | IN UINT16 Flags,\r | |
231 | IN OUT DESC_INDICES *Indices\r | |
263559b8 | 232 | )\r |
233 | {\r | |
234 | volatile VRING_DESC *Desc;\r | |
235 | \r | |
635a3ca2 | 236 | Desc = &Ring->Desc[Indices->NextDescIdx++ % Ring->QueueSize];\r |
4b725858 | 237 | Desc->Addr = BufferDeviceAddress;\r |
263559b8 | 238 | Desc->Len = BufferSize;\r |
239 | Desc->Flags = Flags;\r | |
635a3ca2 | 240 | Desc->Next = Indices->NextDescIdx % Ring->QueueSize;\r |
e371e7e5 | 241 | }\r |
242 | \r | |
243 | \r | |
244 | /**\r | |
245 | \r | |
635a3ca2 | 246 | Notify the host about the descriptor chain just built, and wait until the\r |
247 | host processes it.\r | |
e371e7e5 | 248 | \r |
56f65ed8 | 249 | @param[in] VirtIo The target virtio device to notify.\r |
e371e7e5 | 250 | \r |
251 | @param[in] VirtQueueId Identifies the queue for the target device.\r | |
252 | \r | |
a7615fa8 | 253 | @param[in,out] Ring The virtio ring with descriptors to submit.\r |
e371e7e5 | 254 | \r |
a7615fa8 | 255 | @param[in] Indices Indices->NextDescIdx is not accessed.\r |
256 | Indices->HeadDescIdx identifies the head descriptor\r | |
257 | of the descriptor chain.\r | |
e371e7e5 | 258 | \r |
8bc951a2 LE |
259 | @param[out] UsedLen On success, the total number of bytes, consecutively\r |
260 | across the buffers linked by the descriptor chain,\r | |
261 | that the host wrote. May be NULL if the caller\r | |
262 | doesn't care, or can compute the same information\r | |
263 | from device-specific request structures linked by the\r | |
264 | descriptor chain.\r | |
e371e7e5 | 265 | \r |
56f65ed8 | 266 | @return Error code from VirtIo->SetQueueNotify() if it fails.\r |
e371e7e5 | 267 | \r |
268 | @retval EFI_SUCCESS Otherwise, the host processed all descriptors.\r | |
269 | \r | |
270 | **/\r | |
271 | EFI_STATUS\r | |
272 | EFIAPI\r | |
273 | VirtioFlush (\r | |
56f65ed8 OM |
274 | IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r |
275 | IN UINT16 VirtQueueId,\r | |
276 | IN OUT VRING *Ring,\r | |
8bc951a2 LE |
277 | IN DESC_INDICES *Indices,\r |
278 | OUT UINT32 *UsedLen OPTIONAL\r | |
e371e7e5 | 279 | )\r |
280 | {\r | |
635a3ca2 | 281 | UINT16 NextAvailIdx;\r |
8bc951a2 | 282 | UINT16 LastUsedIdx;\r |
e371e7e5 | 283 | EFI_STATUS Status;\r |
284 | UINTN PollPeriodUsecs;\r | |
285 | \r | |
635a3ca2 | 286 | //\r |
287 | // virtio-0.9.5, 2.4.1.2 Updating the Available Ring\r | |
288 | //\r | |
289 | // It is not exactly clear from the wording of the virtio-0.9.5\r | |
290 | // specification, but each entry in the Available Ring references only the\r | |
291 | // head descriptor of any given descriptor chain.\r | |
292 | //\r | |
293 | NextAvailIdx = *Ring->Avail.Idx;\r | |
8bc951a2 LE |
294 | //\r |
295 | // (Due to our lock-step progress, this is where the host will produce the\r | |
296 | // used element with the head descriptor's index in it.)\r | |
297 | //\r | |
298 | LastUsedIdx = NextAvailIdx;\r | |
635a3ca2 | 299 | Ring->Avail.Ring[NextAvailIdx++ % Ring->QueueSize] =\r |
300 | Indices->HeadDescIdx % Ring->QueueSize;\r | |
301 | \r | |
e371e7e5 | 302 | //\r |
303 | // virtio-0.9.5, 2.4.1.3 Updating the Index Field\r | |
304 | //\r | |
305 | MemoryFence();\r | |
635a3ca2 | 306 | *Ring->Avail.Idx = NextAvailIdx;\r |
e371e7e5 | 307 | \r |
308 | //\r | |
309 | // virtio-0.9.5, 2.4.1.4 Notifying the Device -- gratuitous notifications are\r | |
310 | // OK.\r | |
311 | //\r | |
312 | MemoryFence();\r | |
56f65ed8 | 313 | Status = VirtIo->SetQueueNotify (VirtIo, VirtQueueId);\r |
e371e7e5 | 314 | if (EFI_ERROR (Status)) {\r |
315 | return Status;\r | |
316 | }\r | |
317 | \r | |
318 | //\r | |
319 | // virtio-0.9.5, 2.4.2 Receiving Used Buffers From the Device\r | |
320 | // Wait until the host processes and acknowledges our descriptor chain. The\r | |
321 | // condition we use for polling is greatly simplified and relies on the\r | |
322 | // synchronous, lock-step progress.\r | |
323 | //\r | |
324 | // Keep slowing down until we reach a poll period of slightly above 1 ms.\r | |
325 | //\r | |
326 | PollPeriodUsecs = 1;\r | |
327 | MemoryFence();\r | |
635a3ca2 | 328 | while (*Ring->Used.Idx != NextAvailIdx) {\r |
e371e7e5 | 329 | gBS->Stall (PollPeriodUsecs); // calls AcpiTimerLib::MicroSecondDelay\r |
330 | \r | |
331 | if (PollPeriodUsecs < 1024) {\r | |
332 | PollPeriodUsecs *= 2;\r | |
333 | }\r | |
334 | MemoryFence();\r | |
335 | }\r | |
336 | \r | |
dc9447bd | 337 | MemoryFence();\r |
8bc951a2 LE |
338 | \r |
339 | if (UsedLen != NULL) {\r | |
340 | volatile CONST VRING_USED_ELEM *UsedElem;\r | |
341 | \r | |
342 | UsedElem = &Ring->Used.UsedElem[LastUsedIdx % Ring->QueueSize];\r | |
343 | ASSERT (UsedElem->Id == Indices->HeadDescIdx);\r | |
344 | *UsedLen = UsedElem->Len;\r | |
345 | }\r | |
346 | \r | |
e371e7e5 | 347 | return EFI_SUCCESS;\r |
263559b8 | 348 | }\r |
d0ece0d8 LE |
349 | \r |
350 | \r | |
351 | /**\r | |
352 | \r | |
353 | Report the feature bits to the VirtIo 1.0 device that the VirtIo 1.0 driver\r | |
354 | understands.\r | |
355 | \r | |
356 | In VirtIo 1.0, a device can reject a self-inconsistent feature bitmap through\r | |
357 | the new VSTAT_FEATURES_OK status bit. (For example if the driver requests a\r | |
358 | higher level feature but clears a prerequisite feature.) This function is a\r | |
359 | small wrapper around VIRTIO_DEVICE_PROTOCOL.SetGuestFeatures() that also\r | |
360 | verifies if the VirtIo 1.0 device accepts the feature bitmap.\r | |
361 | \r | |
362 | @param[in] VirtIo Report feature bits to this device.\r | |
363 | \r | |
364 | @param[in] Features The set of feature bits that the driver wishes\r | |
365 | to report. The caller is responsible to perform\r | |
366 | any masking before calling this function; the\r | |
367 | value is directly written with\r | |
368 | VIRTIO_DEVICE_PROTOCOL.SetGuestFeatures().\r | |
369 | \r | |
370 | @param[in,out] DeviceStatus On input, the status byte most recently written\r | |
371 | to the device's status register. On output (even\r | |
372 | on error), DeviceStatus will be updated so that\r | |
373 | it is suitable for further status bit\r | |
374 | manipulation and writing to the device's status\r | |
375 | register.\r | |
376 | \r | |
377 | @retval EFI_SUCCESS The device accepted the configuration in Features.\r | |
378 | \r | |
379 | @return EFI_UNSUPPORTED The device rejected the configuration in Features.\r | |
380 | \r | |
381 | @retval EFI_UNSUPPORTED VirtIo->Revision is smaller than 1.0.0.\r | |
382 | \r | |
383 | @return Error codes from the SetGuestFeatures(),\r | |
384 | SetDeviceStatus(), GetDeviceStatus() member\r | |
385 | functions.\r | |
386 | \r | |
387 | **/\r | |
388 | EFI_STATUS\r | |
389 | EFIAPI\r | |
390 | Virtio10WriteFeatures (\r | |
391 | IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r | |
392 | IN UINT64 Features,\r | |
393 | IN OUT UINT8 *DeviceStatus\r | |
394 | )\r | |
395 | {\r | |
396 | EFI_STATUS Status;\r | |
397 | \r | |
398 | if (VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0)) {\r | |
399 | return EFI_UNSUPPORTED;\r | |
400 | }\r | |
401 | \r | |
402 | Status = VirtIo->SetGuestFeatures (VirtIo, Features);\r | |
403 | if (EFI_ERROR (Status)) {\r | |
404 | return Status;\r | |
405 | }\r | |
406 | \r | |
407 | *DeviceStatus |= VSTAT_FEATURES_OK;\r | |
408 | Status = VirtIo->SetDeviceStatus (VirtIo, *DeviceStatus);\r | |
409 | if (EFI_ERROR (Status)) {\r | |
410 | return Status;\r | |
411 | }\r | |
412 | \r | |
413 | Status = VirtIo->GetDeviceStatus (VirtIo, DeviceStatus);\r | |
414 | if (EFI_ERROR (Status)) {\r | |
415 | return Status;\r | |
416 | }\r | |
417 | \r | |
418 | if ((*DeviceStatus & VSTAT_FEATURES_OK) == 0) {\r | |
419 | Status = EFI_UNSUPPORTED;\r | |
420 | }\r | |
421 | \r | |
422 | return Status;\r | |
423 | }\r | |
0a78d754 BS |
424 | \r |
425 | /**\r | |
426 | Provides the virtio device address required to access system memory from a\r | |
427 | DMA bus master.\r | |
428 | \r | |
429 | The interface follows the same usage pattern as defined in UEFI spec 2.6\r | |
430 | (Section 13.2 PCI Root Bridge I/O Protocol)\r | |
431 | \r | |
432 | The VirtioMapAllBytesInSharedBuffer() is similar to VIRTIO_MAP_SHARED\r | |
433 | with exception that NumberOfBytes is IN-only parameter. The function\r | |
434 | maps all the bytes specified in NumberOfBytes param in one consecutive\r | |
435 | range.\r | |
436 | \r | |
437 | @param[in] VirtIo The virtio device for which the mapping is\r | |
438 | requested.\r | |
439 | \r | |
440 | @param[in] Operation Indicates if the bus master is going to\r | |
441 | read or write to system memory.\r | |
442 | \r | |
443 | @param[in] HostAddress The system memory address to map to shared\r | |
444 | buffer address.\r | |
445 | \r | |
446 | @param[in] NumberOfBytes Number of bytes to map.\r | |
447 | \r | |
448 | @param[out] DeviceAddress The resulting shared map address for the\r | |
449 | bus master to access the hosts HostAddress.\r | |
450 | \r | |
451 | @param[out] Mapping A resulting token to pass to\r | |
452 | VIRTIO_UNMAP_SHARED.\r | |
453 | \r | |
454 | \r | |
455 | @retval EFI_SUCCESS The NumberOfBytes is succesfully mapped.\r | |
456 | @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a\r | |
457 | common buffer.\r | |
458 | @retval EFI_INVALID_PARAMETER One or more parameters are invalid.\r | |
459 | @retval EFI_OUT_OF_RESOURCES The request could not be completed due to\r | |
460 | a lack of resources. This includes the case\r | |
461 | when NumberOfBytes bytes cannot be mapped\r | |
462 | in one consecutive range.\r | |
463 | @retval EFI_DEVICE_ERROR The system hardware could not map the\r | |
464 | requested address.\r | |
465 | **/\r | |
466 | EFI_STATUS\r | |
467 | EFIAPI\r | |
468 | VirtioMapAllBytesInSharedBuffer (\r | |
469 | IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r | |
470 | IN VIRTIO_MAP_OPERATION Operation,\r | |
471 | IN VOID *HostAddress,\r | |
472 | IN UINTN NumberOfBytes,\r | |
473 | OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,\r | |
474 | OUT VOID **Mapping\r | |
475 | )\r | |
476 | {\r | |
477 | EFI_STATUS Status;\r | |
478 | VOID *MapInfo;\r | |
479 | UINTN Size;\r | |
480 | EFI_PHYSICAL_ADDRESS PhysicalAddress;\r | |
481 | \r | |
482 | Size = NumberOfBytes;\r | |
483 | Status = VirtIo->MapSharedBuffer (\r | |
484 | VirtIo,\r | |
485 | Operation,\r | |
486 | HostAddress,\r | |
487 | &Size,\r | |
488 | &PhysicalAddress,\r | |
489 | &MapInfo\r | |
490 | );\r | |
491 | if (EFI_ERROR (Status)) {\r | |
492 | return Status;\r | |
493 | }\r | |
494 | \r | |
495 | if (Size < NumberOfBytes) {\r | |
496 | goto Failed;\r | |
497 | }\r | |
498 | \r | |
499 | *Mapping = MapInfo;\r | |
500 | *DeviceAddress = PhysicalAddress;\r | |
501 | \r | |
502 | return EFI_SUCCESS;\r | |
503 | \r | |
504 | Failed:\r | |
505 | VirtIo->UnmapSharedBuffer (VirtIo, MapInfo);\r | |
506 | return EFI_OUT_OF_RESOURCES;\r | |
507 | }\r | |
fef6becb BS |
508 | \r |
509 | /**\r | |
510 | \r | |
511 | Map the ring buffer so that it can be accessed equally by both guest\r | |
512 | and hypervisor.\r | |
513 | \r | |
514 | @param[in] VirtIo The virtio device instance.\r | |
515 | \r | |
516 | @param[in] Ring The virtio ring to map.\r | |
517 | \r | |
518 | @param[out] RingBaseShift A resulting translation offset, to be\r | |
519 | passed to VirtIo->SetQueueAddress().\r | |
520 | \r | |
521 | @param[out] Mapping A resulting token to pass to\r | |
522 | VirtIo->UnmapSharedBuffer().\r | |
523 | \r | |
524 | @return Status code from VirtIo->MapSharedBuffer()\r | |
525 | **/\r | |
526 | EFI_STATUS\r | |
527 | EFIAPI\r | |
528 | VirtioRingMap (\r | |
529 | IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r | |
530 | IN VRING *Ring,\r | |
531 | OUT UINT64 *RingBaseShift,\r | |
532 | OUT VOID **Mapping\r | |
533 | )\r | |
534 | {\r | |
535 | EFI_STATUS Status;\r | |
536 | EFI_PHYSICAL_ADDRESS DeviceAddress;\r | |
537 | \r | |
538 | Status = VirtioMapAllBytesInSharedBuffer (\r | |
539 | VirtIo,\r | |
540 | VirtioOperationBusMasterCommonBuffer,\r | |
541 | Ring->Base,\r | |
542 | EFI_PAGES_TO_SIZE (Ring->NumPages),\r | |
543 | &DeviceAddress,\r | |
544 | Mapping\r | |
545 | );\r | |
546 | if (EFI_ERROR (Status)) {\r | |
547 | return Status;\r | |
548 | }\r | |
549 | \r | |
550 | *RingBaseShift = DeviceAddress - (UINT64)(UINTN)Ring->Base;\r | |
551 | return EFI_SUCCESS;\r | |
552 | }\r |