1 | /** @file
|
---|
2 |
|
---|
3 | Implement the Driver Binding Protocol and the Component Name 2 Protocol for
|
---|
4 | the Virtio GPU hybrid driver.
|
---|
5 |
|
---|
6 | Copyright (C) 2016, Red Hat, Inc.
|
---|
7 |
|
---|
8 | This program and the accompanying materials are licensed and made available
|
---|
9 | under the terms and conditions of the BSD License which accompanies this
|
---|
10 | distribution. The full text of the license may be found at
|
---|
11 | http://opensource.org/licenses/bsd-license.php
|
---|
12 |
|
---|
13 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
|
---|
14 | WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
---|
15 |
|
---|
16 | **/
|
---|
17 |
|
---|
18 | #include <Library/DevicePathLib.h>
|
---|
19 | #include <Library/MemoryAllocationLib.h>
|
---|
20 | #include <Library/PrintLib.h>
|
---|
21 | #include <Library/UefiBootServicesTableLib.h>
|
---|
22 | #include <Library/UefiLib.h>
|
---|
23 | #include <Protocol/ComponentName2.h>
|
---|
24 | #include <Protocol/DevicePath.h>
|
---|
25 | #include <Protocol/DriverBinding.h>
|
---|
26 | #include <Protocol/PciIo.h>
|
---|
27 |
|
---|
28 | #include "VirtioGpu.h"
|
---|
29 |
|
---|
30 | //
|
---|
31 | // The device path node that describes the Video Output Device Attributes for
|
---|
32 | // the single head (UEFI child handle) that we support.
|
---|
33 | //
|
---|
34 | // The ACPI_DISPLAY_ADR() macro corresponds to Table B-2, section "B.4.2 _DOD"
|
---|
35 | // in the ACPI 3.0b spec, or more recently, to Table B-379, section "B.3.2
|
---|
36 | // _DOD" in the ACPI 6.0 spec.
|
---|
37 | //
|
---|
38 | STATIC CONST ACPI_ADR_DEVICE_PATH mAcpiAdr = {
|
---|
39 | { // Header
|
---|
40 | ACPI_DEVICE_PATH, // Type
|
---|
41 | ACPI_ADR_DP, // SubType
|
---|
42 | { sizeof mAcpiAdr, 0 }, // Length
|
---|
43 | },
|
---|
44 | ACPI_DISPLAY_ADR ( // ADR
|
---|
45 | 1, // DeviceIdScheme: use the ACPI
|
---|
46 | // bit-field definitions
|
---|
47 | 0, // HeadId
|
---|
48 | 0, // NonVgaOutput
|
---|
49 | 1, // BiosCanDetect
|
---|
50 | 0, // VendorInfo
|
---|
51 | ACPI_ADR_DISPLAY_TYPE_EXTERNAL_DIGITAL, // Type
|
---|
52 | 0, // Port
|
---|
53 | 0 // Index
|
---|
54 | )
|
---|
55 | };
|
---|
56 |
|
---|
57 | //
|
---|
58 | // Component Name 2 Protocol implementation.
|
---|
59 | //
|
---|
60 | STATIC CONST EFI_UNICODE_STRING_TABLE mDriverNameTable[] = {
|
---|
61 | { "en", L"Virtio GPU Driver" },
|
---|
62 | { NULL, NULL }
|
---|
63 | };
|
---|
64 |
|
---|
65 | STATIC
|
---|
66 | EFI_STATUS
|
---|
67 | EFIAPI
|
---|
68 | VirtioGpuGetDriverName (
|
---|
69 | IN EFI_COMPONENT_NAME2_PROTOCOL *This,
|
---|
70 | IN CHAR8 *Language,
|
---|
71 | OUT CHAR16 **DriverName
|
---|
72 | )
|
---|
73 | {
|
---|
74 | return LookupUnicodeString2 (Language, This->SupportedLanguages,
|
---|
75 | mDriverNameTable, DriverName, FALSE /* Iso639Language */);
|
---|
76 | }
|
---|
77 |
|
---|
78 | STATIC
|
---|
79 | EFI_STATUS
|
---|
80 | EFIAPI
|
---|
81 | VirtioGpuGetControllerName (
|
---|
82 | IN EFI_COMPONENT_NAME2_PROTOCOL *This,
|
---|
83 | IN EFI_HANDLE ControllerHandle,
|
---|
84 | IN EFI_HANDLE ChildHandle OPTIONAL,
|
---|
85 | IN CHAR8 *Language,
|
---|
86 | OUT CHAR16 **ControllerName
|
---|
87 | )
|
---|
88 | {
|
---|
89 | EFI_STATUS Status;
|
---|
90 | VGPU_DEV *VgpuDev;
|
---|
91 |
|
---|
92 | //
|
---|
93 | // Look up the VGPU_DEV "protocol interface" on ControllerHandle.
|
---|
94 | //
|
---|
95 | Status = gBS->OpenProtocol (ControllerHandle, &gEfiCallerIdGuid,
|
---|
96 | (VOID **)&VgpuDev, gImageHandle, ControllerHandle,
|
---|
97 | EFI_OPEN_PROTOCOL_GET_PROTOCOL);
|
---|
98 | if (EFI_ERROR (Status)) {
|
---|
99 | return Status;
|
---|
100 | }
|
---|
101 | //
|
---|
102 | // Sanity check: if we found gEfiCallerIdGuid on ControllerHandle, then we
|
---|
103 | // keep its Virtio Device Protocol interface open BY_DRIVER.
|
---|
104 | //
|
---|
105 | ASSERT_EFI_ERROR (EfiTestManagedDevice (ControllerHandle, gImageHandle,
|
---|
106 | &gVirtioDeviceProtocolGuid));
|
---|
107 |
|
---|
108 | if (ChildHandle == NULL) {
|
---|
109 | //
|
---|
110 | // The caller is querying the name of the VGPU_DEV controller.
|
---|
111 | //
|
---|
112 | return LookupUnicodeString2 (Language, This->SupportedLanguages,
|
---|
113 | VgpuDev->BusName, ControllerName, FALSE /* Iso639Language */);
|
---|
114 | }
|
---|
115 |
|
---|
116 | //
|
---|
117 | // Otherwise, the caller is looking for the name of the GOP child controller.
|
---|
118 | // Check if it is asking about the GOP child controller that we manage. (The
|
---|
119 | // condition below covers the case when we haven't produced the GOP child
|
---|
120 | // controller yet, or we've destroyed it since.)
|
---|
121 | //
|
---|
122 | if (VgpuDev->Child == NULL || ChildHandle != VgpuDev->Child->GopHandle) {
|
---|
123 | return EFI_UNSUPPORTED;
|
---|
124 | }
|
---|
125 | //
|
---|
126 | // Sanity check: our GOP child controller keeps the VGPU_DEV controller's
|
---|
127 | // Virtio Device Protocol interface open BY_CHILD_CONTROLLER.
|
---|
128 | //
|
---|
129 | ASSERT_EFI_ERROR (EfiTestChildHandle (ControllerHandle, ChildHandle,
|
---|
130 | &gVirtioDeviceProtocolGuid));
|
---|
131 |
|
---|
132 | return LookupUnicodeString2 (Language, This->SupportedLanguages,
|
---|
133 | VgpuDev->Child->GopName, ControllerName,
|
---|
134 | FALSE /* Iso639Language */);
|
---|
135 | }
|
---|
136 |
|
---|
137 | STATIC CONST EFI_COMPONENT_NAME2_PROTOCOL mComponentName2 = {
|
---|
138 | VirtioGpuGetDriverName,
|
---|
139 | VirtioGpuGetControllerName,
|
---|
140 | "en" // SupportedLanguages (RFC 4646)
|
---|
141 | };
|
---|
142 |
|
---|
143 | //
|
---|
144 | // Helper functions for the Driver Binding Protocol Implementation.
|
---|
145 | //
|
---|
146 | /**
|
---|
147 | Format the VGPU_DEV controller name, to be looked up and returned by
|
---|
148 | VirtioGpuGetControllerName().
|
---|
149 |
|
---|
150 | @param[in] ControllerHandle The handle that identifies the VGPU_DEV
|
---|
151 | controller.
|
---|
152 |
|
---|
153 | @param[in] AgentHandle The handle of the agent that will attempt to
|
---|
154 | temporarily open the PciIo protocol. This is the
|
---|
155 | DriverBindingHandle member of the
|
---|
156 | EFI_DRIVER_BINDING_PROTOCOL whose Start()
|
---|
157 | function is calling this function.
|
---|
158 |
|
---|
159 | @param[in] DevicePath The device path that is installed on
|
---|
160 | ControllerHandle.
|
---|
161 |
|
---|
162 | @param[out] ControllerName A dynamically allocated unicode string that
|
---|
163 | unconditionally says "Virtio GPU Device", with a
|
---|
164 | PCI Segment:Bus:Device.Function location
|
---|
165 | optionally appended. The latter part is only
|
---|
166 | produced if DevicePath contains at least one
|
---|
167 | PciIo node; in that case, the most specific such
|
---|
168 | node is used for retrieving the location info.
|
---|
169 | The caller is responsible for freeing
|
---|
170 | ControllerName after use.
|
---|
171 |
|
---|
172 | @retval EFI_SUCCESS ControllerName has been formatted.
|
---|
173 |
|
---|
174 | @retval EFI_OUT_OF_RESOURCES Failed to allocate memory for ControllerName.
|
---|
175 | **/
|
---|
176 | STATIC
|
---|
177 | EFI_STATUS
|
---|
178 | FormatVgpuDevName (
|
---|
179 | IN EFI_HANDLE ControllerHandle,
|
---|
180 | IN EFI_HANDLE AgentHandle,
|
---|
181 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
|
---|
182 | OUT CHAR16 **ControllerName
|
---|
183 | )
|
---|
184 | {
|
---|
185 | EFI_HANDLE PciIoHandle;
|
---|
186 | EFI_PCI_IO_PROTOCOL *PciIo;
|
---|
187 | UINTN Segment, Bus, Device, Function;
|
---|
188 | STATIC CONST CHAR16 ControllerNameStem[] = L"Virtio GPU Device";
|
---|
189 | UINTN ControllerNameSize;
|
---|
190 |
|
---|
191 | if (EFI_ERROR (gBS->LocateDevicePath (&gEfiPciIoProtocolGuid, &DevicePath,
|
---|
192 | &PciIoHandle)) ||
|
---|
193 | EFI_ERROR (gBS->OpenProtocol (PciIoHandle, &gEfiPciIoProtocolGuid,
|
---|
194 | (VOID **)&PciIo, AgentHandle, ControllerHandle,
|
---|
195 | EFI_OPEN_PROTOCOL_GET_PROTOCOL)) ||
|
---|
196 | EFI_ERROR (PciIo->GetLocation (PciIo, &Segment, &Bus, &Device,
|
---|
197 | &Function))) {
|
---|
198 | //
|
---|
199 | // Failed to retrieve location info, return verbatim copy of static string.
|
---|
200 | //
|
---|
201 | *ControllerName = AllocateCopyPool (sizeof ControllerNameStem,
|
---|
202 | ControllerNameStem);
|
---|
203 | return (*ControllerName == NULL) ? EFI_OUT_OF_RESOURCES : EFI_SUCCESS;
|
---|
204 | }
|
---|
205 | //
|
---|
206 | // Location info available, format ControllerName dynamically.
|
---|
207 | //
|
---|
208 | ControllerNameSize = sizeof ControllerNameStem + // includes L'\0'
|
---|
209 | sizeof (CHAR16) * (1 + 4 + // Segment
|
---|
210 | 1 + 2 + // Bus
|
---|
211 | 1 + 2 + // Device
|
---|
212 | 1 + 1 // Function
|
---|
213 | );
|
---|
214 | *ControllerName = AllocatePool (ControllerNameSize);
|
---|
215 | if (*ControllerName == NULL) {
|
---|
216 | return EFI_OUT_OF_RESOURCES;
|
---|
217 | }
|
---|
218 |
|
---|
219 | UnicodeSPrintAsciiFormat (*ControllerName, ControllerNameSize,
|
---|
220 | "%s %04x:%02x:%02x.%x", ControllerNameStem, (UINT32)Segment, (UINT32)Bus,
|
---|
221 | (UINT32)Device, (UINT32)Function);
|
---|
222 | return EFI_SUCCESS;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | Dynamically allocate and initialize the VGPU_GOP child object within an
|
---|
227 | otherwise configured parent VGPU_DEV object.
|
---|
228 |
|
---|
229 | This function adds a BY_CHILD_CONTROLLER reference to ParentBusController's
|
---|
230 | VIRTIO_DEVICE_PROTOCOL interface.
|
---|
231 |
|
---|
232 | @param[in,out] ParentBus The pre-initialized VGPU_DEV object that the
|
---|
233 | newly created VGPU_GOP object will be the
|
---|
234 | child of.
|
---|
235 |
|
---|
236 | @param[in] ParentDevicePath The device path protocol instance that is
|
---|
237 | installed on ParentBusController.
|
---|
238 |
|
---|
239 | @param[in] ParentBusController The UEFI controller handle on which the
|
---|
240 | ParentBus VGPU_DEV object and the
|
---|
241 | ParentDevicePath device path protocol are
|
---|
242 | installed.
|
---|
243 |
|
---|
244 | @param[in] DriverBindingHandle The DriverBindingHandle member of
|
---|
245 | EFI_DRIVER_BINDING_PROTOCOL whose Start()
|
---|
246 | function is calling this function. It is
|
---|
247 | passed as AgentHandle to gBS->OpenProtocol()
|
---|
248 | when creating the BY_CHILD_CONTROLLER
|
---|
249 | reference.
|
---|
250 |
|
---|
251 | @retval EFI_SUCCESS ParentBus->Child has been created and
|
---|
252 | populated, and ParentBus->Child->GopHandle now
|
---|
253 | references ParentBusController->VirtIo
|
---|
254 | BY_CHILD_CONTROLLER.
|
---|
255 |
|
---|
256 | @retval EFI_OUT_OF_RESOURCES Memory allocation failed.
|
---|
257 |
|
---|
258 | @return Error codes from underlying functions.
|
---|
259 | **/
|
---|
260 | STATIC
|
---|
261 | EFI_STATUS
|
---|
262 | InitVgpuGop (
|
---|
263 | IN OUT VGPU_DEV *ParentBus,
|
---|
264 | IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,
|
---|
265 | IN EFI_HANDLE ParentBusController,
|
---|
266 | IN EFI_HANDLE DriverBindingHandle
|
---|
267 | )
|
---|
268 | {
|
---|
269 | VGPU_GOP *VgpuGop;
|
---|
270 | EFI_STATUS Status;
|
---|
271 | CHAR16 *ParentBusName;
|
---|
272 | STATIC CONST CHAR16 NameSuffix[] = L" Head #0";
|
---|
273 | UINTN NameSize;
|
---|
274 | CHAR16 *Name;
|
---|
275 | EFI_TPL OldTpl;
|
---|
276 | VOID *ParentVirtIo;
|
---|
277 |
|
---|
278 | VgpuGop = AllocateZeroPool (sizeof *VgpuGop);
|
---|
279 | if (VgpuGop == NULL) {
|
---|
280 | return EFI_OUT_OF_RESOURCES;
|
---|
281 | }
|
---|
282 |
|
---|
283 | VgpuGop->Signature = VGPU_GOP_SIG;
|
---|
284 | VgpuGop->ParentBus = ParentBus;
|
---|
285 |
|
---|
286 | //
|
---|
287 | // Format a human-readable controller name for VGPU_GOP, and stash it for
|
---|
288 | // VirtioGpuGetControllerName() to look up. We simply append NameSuffix to
|
---|
289 | // ParentBus->BusName.
|
---|
290 | //
|
---|
291 | Status = LookupUnicodeString2 ("en", mComponentName2.SupportedLanguages,
|
---|
292 | ParentBus->BusName, &ParentBusName, FALSE /* Iso639Language */);
|
---|
293 | ASSERT_EFI_ERROR (Status);
|
---|
294 | NameSize = StrSize (ParentBusName) - sizeof (CHAR16) + sizeof NameSuffix;
|
---|
295 | Name = AllocatePool (NameSize);
|
---|
296 | if (Name == NULL) {
|
---|
297 | Status = EFI_OUT_OF_RESOURCES;
|
---|
298 | goto FreeVgpuGop;
|
---|
299 | }
|
---|
300 | UnicodeSPrintAsciiFormat (Name, NameSize, "%s%s", ParentBusName, NameSuffix);
|
---|
301 | Status = AddUnicodeString2 ("en", mComponentName2.SupportedLanguages,
|
---|
302 | &VgpuGop->GopName, Name, FALSE /* Iso639Language */);
|
---|
303 | FreePool (Name);
|
---|
304 | if (EFI_ERROR (Status)) {
|
---|
305 | goto FreeVgpuGop;
|
---|
306 | }
|
---|
307 |
|
---|
308 | //
|
---|
309 | // Create the child device path.
|
---|
310 | //
|
---|
311 | VgpuGop->GopDevicePath = AppendDevicePathNode (ParentDevicePath,
|
---|
312 | &mAcpiAdr.Header);
|
---|
313 | if (VgpuGop->GopDevicePath == NULL) {
|
---|
314 | Status = EFI_OUT_OF_RESOURCES;
|
---|
315 | goto FreeVgpuGopName;
|
---|
316 | }
|
---|
317 |
|
---|
318 | //
|
---|
319 | // Mask protocol notify callbacks until we're done.
|
---|
320 | //
|
---|
321 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
|
---|
322 |
|
---|
323 | //
|
---|
324 | // Create the child handle with the child device path.
|
---|
325 | //
|
---|
326 | Status = gBS->InstallProtocolInterface (&VgpuGop->GopHandle,
|
---|
327 | &gEfiDevicePathProtocolGuid, EFI_NATIVE_INTERFACE,
|
---|
328 | VgpuGop->GopDevicePath);
|
---|
329 | if (EFI_ERROR (Status)) {
|
---|
330 | goto FreeDevicePath;
|
---|
331 | }
|
---|
332 |
|
---|
333 | //
|
---|
334 | // The child handle must present a reference to the parent handle's Virtio
|
---|
335 | // Device Protocol interface.
|
---|
336 | //
|
---|
337 | Status = gBS->OpenProtocol (ParentBusController, &gVirtioDeviceProtocolGuid,
|
---|
338 | &ParentVirtIo, DriverBindingHandle, VgpuGop->GopHandle,
|
---|
339 | EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER);
|
---|
340 | if (EFI_ERROR (Status)) {
|
---|
341 | goto UninstallDevicePath;
|
---|
342 | }
|
---|
343 | ASSERT (ParentVirtIo == ParentBus->VirtIo);
|
---|
344 |
|
---|
345 | //
|
---|
346 | // Initialize our Graphics Output Protocol.
|
---|
347 | //
|
---|
348 | // Fill in the function members of VgpuGop->Gop from the template, then set
|
---|
349 | // up the rest of the GOP infrastructure by calling SetMode() right now.
|
---|
350 | //
|
---|
351 | CopyMem (&VgpuGop->Gop, &mGopTemplate, sizeof mGopTemplate);
|
---|
352 | Status = VgpuGop->Gop.SetMode (&VgpuGop->Gop, 0);
|
---|
353 | if (EFI_ERROR (Status)) {
|
---|
354 | goto CloseVirtIoByChild;
|
---|
355 | }
|
---|
356 |
|
---|
357 | //
|
---|
358 | // Install the Graphics Output Protocol on the child handle.
|
---|
359 | //
|
---|
360 | Status = gBS->InstallProtocolInterface (&VgpuGop->GopHandle,
|
---|
361 | &gEfiGraphicsOutputProtocolGuid, EFI_NATIVE_INTERFACE,
|
---|
362 | &VgpuGop->Gop);
|
---|
363 | if (EFI_ERROR (Status)) {
|
---|
364 | goto UninitGop;
|
---|
365 | }
|
---|
366 |
|
---|
367 | //
|
---|
368 | // We're done.
|
---|
369 | //
|
---|
370 | gBS->RestoreTPL (OldTpl);
|
---|
371 | ParentBus->Child = VgpuGop;
|
---|
372 | return EFI_SUCCESS;
|
---|
373 |
|
---|
374 | UninitGop:
|
---|
375 | ReleaseGopResources (VgpuGop, TRUE /* DisableHead */);
|
---|
376 |
|
---|
377 | CloseVirtIoByChild:
|
---|
378 | gBS->CloseProtocol (ParentBusController, &gVirtioDeviceProtocolGuid,
|
---|
379 | DriverBindingHandle, VgpuGop->GopHandle);
|
---|
380 |
|
---|
381 | UninstallDevicePath:
|
---|
382 | gBS->UninstallProtocolInterface (VgpuGop->GopHandle,
|
---|
383 | &gEfiDevicePathProtocolGuid, VgpuGop->GopDevicePath);
|
---|
384 |
|
---|
385 | FreeDevicePath:
|
---|
386 | gBS->RestoreTPL (OldTpl);
|
---|
387 | FreePool (VgpuGop->GopDevicePath);
|
---|
388 |
|
---|
389 | FreeVgpuGopName:
|
---|
390 | FreeUnicodeStringTable (VgpuGop->GopName);
|
---|
391 |
|
---|
392 | FreeVgpuGop:
|
---|
393 | FreePool (VgpuGop);
|
---|
394 |
|
---|
395 | return Status;
|
---|
396 | }
|
---|
397 |
|
---|
398 | /**
|
---|
399 | Tear down and release the VGPU_GOP child object within the VGPU_DEV parent
|
---|
400 | object.
|
---|
401 |
|
---|
402 | This function removes the BY_CHILD_CONTROLLER reference from
|
---|
403 | ParentBusController's VIRTIO_DEVICE_PROTOCOL interface.
|
---|
404 |
|
---|
405 | @param[in,out] ParentBus The VGPU_DEV object that the VGPU_GOP child
|
---|
406 | object will be removed from.
|
---|
407 |
|
---|
408 | @param[in] ParentBusController The UEFI controller handle on which the
|
---|
409 | ParentBus VGPU_DEV object is installed.
|
---|
410 |
|
---|
411 | @param[in] DriverBindingHandle The DriverBindingHandle member of
|
---|
412 | EFI_DRIVER_BINDING_PROTOCOL whose Stop()
|
---|
413 | function is calling this function. It is
|
---|
414 | passed as AgentHandle to gBS->CloseProtocol()
|
---|
415 | when removing the BY_CHILD_CONTROLLER
|
---|
416 | reference.
|
---|
417 | **/
|
---|
418 | STATIC
|
---|
419 | VOID
|
---|
420 | UninitVgpuGop (
|
---|
421 | IN OUT VGPU_DEV *ParentBus,
|
---|
422 | IN EFI_HANDLE ParentBusController,
|
---|
423 | IN EFI_HANDLE DriverBindingHandle
|
---|
424 | )
|
---|
425 | {
|
---|
426 | VGPU_GOP *VgpuGop;
|
---|
427 | EFI_STATUS Status;
|
---|
428 |
|
---|
429 | VgpuGop = ParentBus->Child;
|
---|
430 | Status = gBS->UninstallProtocolInterface (VgpuGop->GopHandle,
|
---|
431 | &gEfiGraphicsOutputProtocolGuid, &VgpuGop->Gop);
|
---|
432 | ASSERT_EFI_ERROR (Status);
|
---|
433 |
|
---|
434 | //
|
---|
435 | // Uninitialize VgpuGop->Gop.
|
---|
436 | //
|
---|
437 | ReleaseGopResources (VgpuGop, TRUE /* DisableHead */);
|
---|
438 |
|
---|
439 | Status = gBS->CloseProtocol (ParentBusController, &gVirtioDeviceProtocolGuid,
|
---|
440 | DriverBindingHandle, VgpuGop->GopHandle);
|
---|
441 | ASSERT_EFI_ERROR (Status);
|
---|
442 |
|
---|
443 | Status = gBS->UninstallProtocolInterface (VgpuGop->GopHandle,
|
---|
444 | &gEfiDevicePathProtocolGuid, VgpuGop->GopDevicePath);
|
---|
445 | ASSERT_EFI_ERROR (Status);
|
---|
446 |
|
---|
447 | FreePool (VgpuGop->GopDevicePath);
|
---|
448 | FreeUnicodeStringTable (VgpuGop->GopName);
|
---|
449 | FreePool (VgpuGop);
|
---|
450 |
|
---|
451 | ParentBus->Child = NULL;
|
---|
452 | }
|
---|
453 |
|
---|
454 | //
|
---|
455 | // Driver Binding Protocol Implementation.
|
---|
456 | //
|
---|
457 | STATIC
|
---|
458 | EFI_STATUS
|
---|
459 | EFIAPI
|
---|
460 | VirtioGpuDriverBindingSupported (
|
---|
461 | IN EFI_DRIVER_BINDING_PROTOCOL *This,
|
---|
462 | IN EFI_HANDLE ControllerHandle,
|
---|
463 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
|
---|
464 | )
|
---|
465 | {
|
---|
466 | EFI_STATUS Status;
|
---|
467 | VIRTIO_DEVICE_PROTOCOL *VirtIo;
|
---|
468 |
|
---|
469 | //
|
---|
470 | // - If RemainingDevicePath is NULL: the caller is interested in creating all
|
---|
471 | // child handles.
|
---|
472 | // - If RemainingDevicePath points to an end node: the caller is not
|
---|
473 | // interested in creating any child handle.
|
---|
474 | // - Otherwise, the caller would like to create the one child handle
|
---|
475 | // specified in RemainingDevicePath. In this case we have to see if the
|
---|
476 | // requested device path is supportable.
|
---|
477 | //
|
---|
478 | if (RemainingDevicePath != NULL &&
|
---|
479 | !IsDevicePathEnd (RemainingDevicePath) &&
|
---|
480 | (DevicePathNodeLength (RemainingDevicePath) != sizeof mAcpiAdr ||
|
---|
481 | CompareMem (RemainingDevicePath, &mAcpiAdr, sizeof mAcpiAdr) != 0)) {
|
---|
482 | return EFI_UNSUPPORTED;
|
---|
483 | }
|
---|
484 |
|
---|
485 | //
|
---|
486 | // Open the Virtio Device Protocol interface on the controller, BY_DRIVER.
|
---|
487 | //
|
---|
488 | Status = gBS->OpenProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,
|
---|
489 | (VOID **)&VirtIo, This->DriverBindingHandle,
|
---|
490 | ControllerHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);
|
---|
491 | if (EFI_ERROR (Status)) {
|
---|
492 | //
|
---|
493 | // If this fails, then by default we cannot support ControllerHandle. There
|
---|
494 | // is one exception: we've already bound the device, have not produced any
|
---|
495 | // GOP child controller, and now the caller wants us to produce the child
|
---|
496 | // controller (either specifically or as part of "all children"). That's
|
---|
497 | // allowed.
|
---|
498 | //
|
---|
499 | if (Status == EFI_ALREADY_STARTED) {
|
---|
500 | EFI_STATUS Status2;
|
---|
501 | VGPU_DEV *VgpuDev;
|
---|
502 |
|
---|
503 | Status2 = gBS->OpenProtocol (ControllerHandle, &gEfiCallerIdGuid,
|
---|
504 | (VOID **)&VgpuDev, This->DriverBindingHandle,
|
---|
505 | ControllerHandle, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
|
---|
506 | ASSERT_EFI_ERROR (Status2);
|
---|
507 |
|
---|
508 | if (VgpuDev->Child == NULL &&
|
---|
509 | (RemainingDevicePath == NULL ||
|
---|
510 | !IsDevicePathEnd (RemainingDevicePath))) {
|
---|
511 | Status = EFI_SUCCESS;
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 | return Status;
|
---|
516 | }
|
---|
517 |
|
---|
518 | //
|
---|
519 | // First BY_DRIVER open; check the VirtIo revision and subsystem.
|
---|
520 | //
|
---|
521 | if (VirtIo->Revision < VIRTIO_SPEC_REVISION (1, 0, 0) ||
|
---|
522 | VirtIo->SubSystemDeviceId != VIRTIO_SUBSYSTEM_GPU_DEVICE) {
|
---|
523 | Status = EFI_UNSUPPORTED;
|
---|
524 | goto CloseVirtIo;
|
---|
525 | }
|
---|
526 |
|
---|
527 | //
|
---|
528 | // We'll need the device path of the VirtIo device both for formatting
|
---|
529 | // VGPU_DEV.BusName and for populating VGPU_GOP.GopDevicePath.
|
---|
530 | //
|
---|
531 | Status = gBS->OpenProtocol (ControllerHandle, &gEfiDevicePathProtocolGuid,
|
---|
532 | NULL, This->DriverBindingHandle, ControllerHandle,
|
---|
533 | EFI_OPEN_PROTOCOL_TEST_PROTOCOL);
|
---|
534 |
|
---|
535 | CloseVirtIo:
|
---|
536 | gBS->CloseProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,
|
---|
537 | This->DriverBindingHandle, ControllerHandle);
|
---|
538 |
|
---|
539 | return Status;
|
---|
540 | }
|
---|
541 |
|
---|
542 | STATIC
|
---|
543 | EFI_STATUS
|
---|
544 | EFIAPI
|
---|
545 | VirtioGpuDriverBindingStart (
|
---|
546 | IN EFI_DRIVER_BINDING_PROTOCOL *This,
|
---|
547 | IN EFI_HANDLE ControllerHandle,
|
---|
548 | IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
|
---|
549 | )
|
---|
550 | {
|
---|
551 | EFI_STATUS Status;
|
---|
552 | VIRTIO_DEVICE_PROTOCOL *VirtIo;
|
---|
553 | BOOLEAN VirtIoBoundJustNow;
|
---|
554 | VGPU_DEV *VgpuDev;
|
---|
555 | EFI_DEVICE_PATH_PROTOCOL *DevicePath;
|
---|
556 |
|
---|
557 | //
|
---|
558 | // Open the Virtio Device Protocol.
|
---|
559 | //
|
---|
560 | // The result of this operation, combined with the checks in
|
---|
561 | // VirtioGpuDriverBindingSupported(), uniquely tells us whether we are
|
---|
562 | // binding the VirtIo controller on this call (with or without creating child
|
---|
563 | // controllers), or else we're *only* creating child controllers.
|
---|
564 | //
|
---|
565 | Status = gBS->OpenProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,
|
---|
566 | (VOID **)&VirtIo, This->DriverBindingHandle,
|
---|
567 | ControllerHandle, EFI_OPEN_PROTOCOL_BY_DRIVER);
|
---|
568 | if (EFI_ERROR (Status)) {
|
---|
569 | //
|
---|
570 | // The assertions below are based on the success of
|
---|
571 | // VirtioGpuDriverBindingSupported(): we bound ControllerHandle earlier,
|
---|
572 | // without producing child handles, and now we're producing the GOP child
|
---|
573 | // handle only.
|
---|
574 | //
|
---|
575 | ASSERT (Status == EFI_ALREADY_STARTED);
|
---|
576 |
|
---|
577 | Status = gBS->OpenProtocol (ControllerHandle, &gEfiCallerIdGuid,
|
---|
578 | (VOID **)&VgpuDev, This->DriverBindingHandle,
|
---|
579 | ControllerHandle, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
|
---|
580 | ASSERT_EFI_ERROR (Status);
|
---|
581 |
|
---|
582 | ASSERT (VgpuDev->Child == NULL);
|
---|
583 | ASSERT (
|
---|
584 | RemainingDevicePath == NULL || !IsDevicePathEnd (RemainingDevicePath));
|
---|
585 |
|
---|
586 | VirtIoBoundJustNow = FALSE;
|
---|
587 | } else {
|
---|
588 | VirtIoBoundJustNow = TRUE;
|
---|
589 |
|
---|
590 | //
|
---|
591 | // Allocate the private structure.
|
---|
592 | //
|
---|
593 | VgpuDev = AllocateZeroPool (sizeof *VgpuDev);
|
---|
594 | if (VgpuDev == NULL) {
|
---|
595 | Status = EFI_OUT_OF_RESOURCES;
|
---|
596 | goto CloseVirtIo;
|
---|
597 | }
|
---|
598 | VgpuDev->VirtIo = VirtIo;
|
---|
599 | }
|
---|
600 |
|
---|
601 | //
|
---|
602 | // Grab the VirtIo controller's device path. This is necessary regardless of
|
---|
603 | // VirtIoBoundJustNow.
|
---|
604 | //
|
---|
605 | Status = gBS->OpenProtocol (ControllerHandle, &gEfiDevicePathProtocolGuid,
|
---|
606 | (VOID **)&DevicePath, This->DriverBindingHandle,
|
---|
607 | ControllerHandle, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
|
---|
608 | if (EFI_ERROR (Status)) {
|
---|
609 | goto FreeVgpuDev;
|
---|
610 | }
|
---|
611 |
|
---|
612 | //
|
---|
613 | // Create VGPU_DEV if we've bound the VirtIo controller right now (that is,
|
---|
614 | // if we aren't *only* creating child handles).
|
---|
615 | //
|
---|
616 | if (VirtIoBoundJustNow) {
|
---|
617 | CHAR16 *VgpuDevName;
|
---|
618 |
|
---|
619 | //
|
---|
620 | // Format a human-readable controller name for VGPU_DEV, and stash it for
|
---|
621 | // VirtioGpuGetControllerName() to look up.
|
---|
622 | //
|
---|
623 | Status = FormatVgpuDevName (ControllerHandle, This->DriverBindingHandle,
|
---|
624 | DevicePath, &VgpuDevName);
|
---|
625 | if (EFI_ERROR (Status)) {
|
---|
626 | goto FreeVgpuDev;
|
---|
627 | }
|
---|
628 | Status = AddUnicodeString2 ("en", mComponentName2.SupportedLanguages,
|
---|
629 | &VgpuDev->BusName, VgpuDevName, FALSE /* Iso639Language */);
|
---|
630 | FreePool (VgpuDevName);
|
---|
631 | if (EFI_ERROR (Status)) {
|
---|
632 | goto FreeVgpuDev;
|
---|
633 | }
|
---|
634 |
|
---|
635 | Status = VirtioGpuInit (VgpuDev);
|
---|
636 | if (EFI_ERROR (Status)) {
|
---|
637 | goto FreeVgpuDevBusName;
|
---|
638 | }
|
---|
639 |
|
---|
640 | Status = gBS->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
|
---|
641 | VirtioGpuExitBoot, VgpuDev /* NotifyContext */,
|
---|
642 | &VgpuDev->ExitBoot);
|
---|
643 | if (EFI_ERROR (Status)) {
|
---|
644 | goto UninitGpu;
|
---|
645 | }
|
---|
646 |
|
---|
647 | //
|
---|
648 | // Install the VGPU_DEV "protocol interface" on ControllerHandle.
|
---|
649 | //
|
---|
650 | Status = gBS->InstallProtocolInterface (&ControllerHandle,
|
---|
651 | &gEfiCallerIdGuid, EFI_NATIVE_INTERFACE, VgpuDev);
|
---|
652 | if (EFI_ERROR (Status)) {
|
---|
653 | goto CloseExitBoot;
|
---|
654 | }
|
---|
655 |
|
---|
656 | if (RemainingDevicePath != NULL && IsDevicePathEnd (RemainingDevicePath)) {
|
---|
657 | //
|
---|
658 | // No child handle should be produced; we're done.
|
---|
659 | //
|
---|
660 | DEBUG ((EFI_D_INFO, "%a: bound VirtIo=%p without producing GOP\n",
|
---|
661 | __FUNCTION__, (VOID *)VgpuDev->VirtIo));
|
---|
662 | return EFI_SUCCESS;
|
---|
663 | }
|
---|
664 | }
|
---|
665 |
|
---|
666 | //
|
---|
667 | // Below we'll produce our single child handle: the caller requested it
|
---|
668 | // either specifically, or as part of all child handles.
|
---|
669 | //
|
---|
670 | ASSERT (VgpuDev->Child == NULL);
|
---|
671 | ASSERT (
|
---|
672 | RemainingDevicePath == NULL || !IsDevicePathEnd (RemainingDevicePath));
|
---|
673 |
|
---|
674 | Status = InitVgpuGop (VgpuDev, DevicePath, ControllerHandle,
|
---|
675 | This->DriverBindingHandle);
|
---|
676 | if (EFI_ERROR (Status)) {
|
---|
677 | goto UninstallVgpuDev;
|
---|
678 | }
|
---|
679 |
|
---|
680 | //
|
---|
681 | // We're done.
|
---|
682 | //
|
---|
683 | DEBUG ((EFI_D_INFO, "%a: produced GOP %a VirtIo=%p\n", __FUNCTION__,
|
---|
684 | VirtIoBoundJustNow ? "while binding" : "for pre-bound",
|
---|
685 | (VOID *)VgpuDev->VirtIo));
|
---|
686 | return EFI_SUCCESS;
|
---|
687 |
|
---|
688 | UninstallVgpuDev:
|
---|
689 | if (VirtIoBoundJustNow) {
|
---|
690 | gBS->UninstallProtocolInterface (ControllerHandle, &gEfiCallerIdGuid,
|
---|
691 | VgpuDev);
|
---|
692 | }
|
---|
693 |
|
---|
694 | CloseExitBoot:
|
---|
695 | if (VirtIoBoundJustNow) {
|
---|
696 | gBS->CloseEvent (VgpuDev->ExitBoot);
|
---|
697 | }
|
---|
698 |
|
---|
699 | UninitGpu:
|
---|
700 | if (VirtIoBoundJustNow) {
|
---|
701 | VirtioGpuUninit (VgpuDev);
|
---|
702 | }
|
---|
703 |
|
---|
704 | FreeVgpuDevBusName:
|
---|
705 | if (VirtIoBoundJustNow) {
|
---|
706 | FreeUnicodeStringTable (VgpuDev->BusName);
|
---|
707 | }
|
---|
708 |
|
---|
709 | FreeVgpuDev:
|
---|
710 | if (VirtIoBoundJustNow) {
|
---|
711 | FreePool (VgpuDev);
|
---|
712 | }
|
---|
713 |
|
---|
714 | CloseVirtIo:
|
---|
715 | if (VirtIoBoundJustNow) {
|
---|
716 | gBS->CloseProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,
|
---|
717 | This->DriverBindingHandle, ControllerHandle);
|
---|
718 | }
|
---|
719 |
|
---|
720 | return Status;
|
---|
721 | }
|
---|
722 |
|
---|
723 | STATIC
|
---|
724 | EFI_STATUS
|
---|
725 | EFIAPI
|
---|
726 | VirtioGpuDriverBindingStop (
|
---|
727 | IN EFI_DRIVER_BINDING_PROTOCOL *This,
|
---|
728 | IN EFI_HANDLE ControllerHandle,
|
---|
729 | IN UINTN NumberOfChildren,
|
---|
730 | IN EFI_HANDLE *ChildHandleBuffer OPTIONAL
|
---|
731 | )
|
---|
732 | {
|
---|
733 | EFI_STATUS Status;
|
---|
734 | VGPU_DEV *VgpuDev;
|
---|
735 |
|
---|
736 | //
|
---|
737 | // Look up the VGPU_DEV "protocol interface" on ControllerHandle.
|
---|
738 | //
|
---|
739 | Status = gBS->OpenProtocol (ControllerHandle, &gEfiCallerIdGuid,
|
---|
740 | (VOID **)&VgpuDev, This->DriverBindingHandle,
|
---|
741 | ControllerHandle, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
|
---|
742 | if (EFI_ERROR (Status)) {
|
---|
743 | return Status;
|
---|
744 | }
|
---|
745 | //
|
---|
746 | // Sanity check: if we found gEfiCallerIdGuid on ControllerHandle, then we
|
---|
747 | // keep its Virtio Device Protocol interface open BY_DRIVER.
|
---|
748 | //
|
---|
749 | ASSERT_EFI_ERROR (EfiTestManagedDevice (ControllerHandle,
|
---|
750 | This->DriverBindingHandle, &gVirtioDeviceProtocolGuid));
|
---|
751 |
|
---|
752 | switch (NumberOfChildren) {
|
---|
753 | case 0:
|
---|
754 | //
|
---|
755 | // The caller wants us to unbind the VirtIo controller.
|
---|
756 | //
|
---|
757 | if (VgpuDev->Child != NULL) {
|
---|
758 | //
|
---|
759 | // We still have the GOP child.
|
---|
760 | //
|
---|
761 | Status = EFI_DEVICE_ERROR;
|
---|
762 | break;
|
---|
763 | }
|
---|
764 |
|
---|
765 | DEBUG ((EFI_D_INFO, "%a: unbinding GOP-less VirtIo=%p\n", __FUNCTION__,
|
---|
766 | (VOID *)VgpuDev->VirtIo));
|
---|
767 |
|
---|
768 | Status = gBS->UninstallProtocolInterface (ControllerHandle,
|
---|
769 | &gEfiCallerIdGuid, VgpuDev);
|
---|
770 | ASSERT_EFI_ERROR (Status);
|
---|
771 |
|
---|
772 | Status = gBS->CloseEvent (VgpuDev->ExitBoot);
|
---|
773 | ASSERT_EFI_ERROR (Status);
|
---|
774 |
|
---|
775 | VirtioGpuUninit (VgpuDev);
|
---|
776 | FreeUnicodeStringTable (VgpuDev->BusName);
|
---|
777 | FreePool (VgpuDev);
|
---|
778 |
|
---|
779 | Status = gBS->CloseProtocol (ControllerHandle, &gVirtioDeviceProtocolGuid,
|
---|
780 | This->DriverBindingHandle, ControllerHandle);
|
---|
781 | ASSERT_EFI_ERROR (Status);
|
---|
782 | break;
|
---|
783 |
|
---|
784 | case 1:
|
---|
785 | //
|
---|
786 | // The caller wants us to destroy our child GOP controller.
|
---|
787 | //
|
---|
788 | if (VgpuDev->Child == NULL ||
|
---|
789 | ChildHandleBuffer[0] != VgpuDev->Child->GopHandle) {
|
---|
790 | //
|
---|
791 | // We have no child controller at the moment, or it differs from the one
|
---|
792 | // the caller wants us to destroy. I.e., we don't own the child
|
---|
793 | // controller passed in.
|
---|
794 | //
|
---|
795 | Status = EFI_DEVICE_ERROR;
|
---|
796 | break;
|
---|
797 | }
|
---|
798 | //
|
---|
799 | // Sanity check: our GOP child controller keeps the VGPU_DEV controller's
|
---|
800 | // Virtio Device Protocol interface open BY_CHILD_CONTROLLER.
|
---|
801 | //
|
---|
802 | ASSERT_EFI_ERROR (EfiTestChildHandle (ControllerHandle,
|
---|
803 | VgpuDev->Child->GopHandle,
|
---|
804 | &gVirtioDeviceProtocolGuid));
|
---|
805 |
|
---|
806 | DEBUG ((EFI_D_INFO, "%a: destroying GOP under VirtIo=%p\n", __FUNCTION__,
|
---|
807 | (VOID *)VgpuDev->VirtIo));
|
---|
808 | UninitVgpuGop (VgpuDev, ControllerHandle, This->DriverBindingHandle);
|
---|
809 | break;
|
---|
810 |
|
---|
811 | default:
|
---|
812 | //
|
---|
813 | // Impossible, we never produced more than one child.
|
---|
814 | //
|
---|
815 | Status = EFI_DEVICE_ERROR;
|
---|
816 | break;
|
---|
817 | }
|
---|
818 | return Status;
|
---|
819 | }
|
---|
820 |
|
---|
821 | STATIC EFI_DRIVER_BINDING_PROTOCOL mDriverBinding = {
|
---|
822 | VirtioGpuDriverBindingSupported,
|
---|
823 | VirtioGpuDriverBindingStart,
|
---|
824 | VirtioGpuDriverBindingStop,
|
---|
825 | 0x10, // Version
|
---|
826 | NULL, // ImageHandle, overwritten in entry point
|
---|
827 | NULL // DriverBindingHandle, ditto
|
---|
828 | };
|
---|
829 |
|
---|
830 | //
|
---|
831 | // Entry point of the driver.
|
---|
832 | //
|
---|
833 | EFI_STATUS
|
---|
834 | EFIAPI
|
---|
835 | VirtioGpuEntryPoint (
|
---|
836 | IN EFI_HANDLE ImageHandle,
|
---|
837 | IN EFI_SYSTEM_TABLE *SystemTable
|
---|
838 | )
|
---|
839 | {
|
---|
840 | return EfiLibInstallDriverBindingComponentName2 (ImageHandle, SystemTable,
|
---|
841 | &mDriverBinding, ImageHandle, NULL /* ComponentName */,
|
---|
842 | &mComponentName2);
|
---|
843 | }
|
---|