]>
Commit | Line | Data |
---|---|---|
57b6122f AB |
1 | /** @file\r |
2 | * Virtio FDT client protocol driver for virtio,mmio DT node\r | |
3 | *\r | |
4 | * Copyright (c) 2014 - 2016, Linaro Ltd. All rights reserved.<BR>\r | |
5 | *\r | |
9792fb0e | 6 | * SPDX-License-Identifier: BSD-2-Clause-Patent\r |
57b6122f AB |
7 | *\r |
8 | **/\r | |
9 | \r | |
10 | #include <Library/BaseLib.h>\r | |
11 | #include <Library/BaseMemoryLib.h>\r | |
12 | #include <Library/DebugLib.h>\r | |
13 | #include <Library/DevicePathLib.h>\r | |
14 | #include <Library/MemoryAllocationLib.h>\r | |
15 | #include <Library/UefiBootServicesTableLib.h>\r | |
16 | #include <Library/UefiDriverEntryPoint.h>\r | |
17 | #include <Library/VirtioMmioDeviceLib.h>\r | |
18 | \r | |
19 | #include <Guid/VirtioMmioTransport.h>\r | |
20 | \r | |
21 | #include <Protocol/FdtClient.h>\r | |
22 | \r | |
23 | #pragma pack (1)\r | |
24 | typedef struct {\r | |
ac0a286f MK |
25 | VENDOR_DEVICE_PATH Vendor;\r |
26 | UINT64 PhysBase;\r | |
27 | EFI_DEVICE_PATH_PROTOCOL End;\r | |
57b6122f AB |
28 | } VIRTIO_TRANSPORT_DEVICE_PATH;\r |
29 | #pragma pack ()\r | |
30 | \r | |
31 | EFI_STATUS\r | |
32 | EFIAPI\r | |
33 | InitializeVirtioFdtDxe (\r | |
ac0a286f MK |
34 | IN EFI_HANDLE ImageHandle,\r |
35 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
57b6122f AB |
36 | )\r |
37 | {\r | |
ac0a286f MK |
38 | EFI_STATUS Status, FindNodeStatus;\r |
39 | FDT_CLIENT_PROTOCOL *FdtClient;\r | |
40 | INT32 Node;\r | |
41 | CONST UINT64 *Reg;\r | |
42 | UINT32 RegSize;\r | |
43 | VIRTIO_TRANSPORT_DEVICE_PATH *DevicePath;\r | |
44 | EFI_HANDLE Handle;\r | |
45 | UINT64 RegBase;\r | |
46 | \r | |
47 | Status = gBS->LocateProtocol (\r | |
48 | &gFdtClientProtocolGuid,\r | |
49 | NULL,\r | |
50 | (VOID **)&FdtClient\r | |
51 | );\r | |
57b6122f AB |
52 | ASSERT_EFI_ERROR (Status);\r |
53 | \r | |
ac0a286f MK |
54 | for (FindNodeStatus = FdtClient->FindCompatibleNode (\r |
55 | FdtClient,\r | |
56 | "virtio,mmio",\r | |
57 | &Node\r | |
58 | );\r | |
57b6122f | 59 | !EFI_ERROR (FindNodeStatus);\r |
ac0a286f MK |
60 | FindNodeStatus = FdtClient->FindNextCompatibleNode (\r |
61 | FdtClient,\r | |
62 | "virtio,mmio",\r | |
63 | Node,\r | |
64 | &Node\r | |
65 | ))\r | |
66 | {\r | |
67 | Status = FdtClient->GetNodeProperty (\r | |
68 | FdtClient,\r | |
69 | Node,\r | |
70 | "reg",\r | |
71 | (CONST VOID **)&Reg,\r | |
72 | &RegSize\r | |
73 | );\r | |
57b6122f | 74 | if (EFI_ERROR (Status)) {\r |
ac0a286f MK |
75 | DEBUG ((\r |
76 | DEBUG_ERROR,\r | |
77 | "%a: GetNodeProperty () failed (Status == %r)\n",\r | |
78 | __FUNCTION__,\r | |
79 | Status\r | |
80 | ));\r | |
57b6122f AB |
81 | continue;\r |
82 | }\r | |
83 | \r | |
84 | ASSERT (RegSize == 16);\r | |
85 | \r | |
86 | //\r | |
87 | // Create a unique device path for this transport on the fly\r | |
88 | //\r | |
ac0a286f | 89 | RegBase = SwapBytes64 (*Reg);\r |
57b6122f | 90 | DevicePath = (VIRTIO_TRANSPORT_DEVICE_PATH *)CreateDeviceNode (\r |
ac0a286f MK |
91 | HARDWARE_DEVICE_PATH,\r |
92 | HW_VENDOR_DP,\r | |
93 | sizeof (VIRTIO_TRANSPORT_DEVICE_PATH)\r | |
94 | );\r | |
57b6122f | 95 | if (DevicePath == NULL) {\r |
47719926 | 96 | DEBUG ((DEBUG_ERROR, "%a: Out of memory\n", __FUNCTION__));\r |
57b6122f AB |
97 | continue;\r |
98 | }\r | |
99 | \r | |
100 | CopyGuid (&DevicePath->Vendor.Guid, &gVirtioMmioTransportGuid);\r | |
101 | DevicePath->PhysBase = RegBase;\r | |
ac0a286f MK |
102 | SetDevicePathNodeLength (\r |
103 | &DevicePath->Vendor,\r | |
104 | sizeof (*DevicePath) - sizeof (DevicePath->End)\r | |
105 | );\r | |
57b6122f AB |
106 | SetDevicePathEndNode (&DevicePath->End);\r |
107 | \r | |
108 | Handle = NULL;\r | |
ac0a286f MK |
109 | Status = gBS->InstallProtocolInterface (\r |
110 | &Handle,\r | |
111 | &gEfiDevicePathProtocolGuid,\r | |
112 | EFI_NATIVE_INTERFACE,\r | |
113 | DevicePath\r | |
114 | );\r | |
57b6122f | 115 | if (EFI_ERROR (Status)) {\r |
ac0a286f MK |
116 | DEBUG ((\r |
117 | DEBUG_ERROR,\r | |
118 | "%a: Failed to install the EFI_DEVICE_PATH "\r | |
57b6122f | 119 | "protocol on a new handle (Status == %r)\n",\r |
ac0a286f MK |
120 | __FUNCTION__,\r |
121 | Status\r | |
122 | ));\r | |
57b6122f AB |
123 | FreePool (DevicePath);\r |
124 | continue;\r | |
125 | }\r | |
126 | \r | |
127 | Status = VirtioMmioInstallDevice (RegBase, Handle);\r | |
128 | if (EFI_ERROR (Status)) {\r | |
ac0a286f MK |
129 | DEBUG ((\r |
130 | DEBUG_ERROR,\r | |
131 | "%a: Failed to install VirtIO transport @ 0x%Lx "\r | |
132 | "on handle %p (Status == %r)\n",\r | |
133 | __FUNCTION__,\r | |
134 | RegBase,\r | |
135 | Handle,\r | |
136 | Status\r | |
137 | ));\r | |
138 | \r | |
139 | Status = gBS->UninstallProtocolInterface (\r | |
140 | Handle,\r | |
141 | &gEfiDevicePathProtocolGuid,\r | |
142 | DevicePath\r | |
143 | );\r | |
57b6122f AB |
144 | ASSERT_EFI_ERROR (Status);\r |
145 | FreePool (DevicePath);\r | |
146 | continue;\r | |
147 | }\r | |
148 | }\r | |
149 | \r | |
ac0a286f MK |
150 | if (EFI_ERROR (FindNodeStatus) && (FindNodeStatus != EFI_NOT_FOUND)) {\r |
151 | DEBUG ((\r | |
152 | DEBUG_ERROR,\r | |
153 | "%a: Error occurred while iterating DT nodes "\r | |
154 | "(FindNodeStatus == %r)\n",\r | |
155 | __FUNCTION__,\r | |
156 | FindNodeStatus\r | |
157 | ));\r | |
57b6122f AB |
158 | }\r |
159 | \r | |
160 | return EFI_SUCCESS;\r | |
161 | }\r |