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