]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/SioBusDxe/SioService.c
OvmfPkg: Add an Super IO bus driver
[mirror_edk2.git] / OvmfPkg / SioBusDxe / SioService.c
1 /** @file
2 The SioBusDxe driver is used to create child devices on the ISA bus and
3 installs the Super I/O protocols on them.
4
5 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions
9 of the BSD License which accompanies this distribution. The
10 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,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 #include "SioBusDxe.h"
19
20 //
21 // Super I/O Protocol interfaces
22 //
23 EFI_SIO_PROTOCOL mSioInterface = {
24 SioRegisterAccess,
25 SioGetResources,
26 SioSetResources,
27 SioPossibleResources,
28 SioModify
29 };
30
31 //
32 // COM 1 UART Controller
33 //
34 GLOBAL_REMOVE_IF_UNREFERENCED
35 SIO_RESOURCES_IO mCom1Resources = {
36 { { ACPI_FIXED_LOCATION_IO_PORT_DESCRIPTOR }, 0x3F8, 8 },
37 { ACPI_END_TAG_DESCRIPTOR, 0 }
38 };
39
40 //
41 // COM 2 UART Controller
42 //
43 GLOBAL_REMOVE_IF_UNREFERENCED
44 SIO_RESOURCES_IO mCom2Resources = {
45 { { ACPI_FIXED_LOCATION_IO_PORT_DESCRIPTOR }, 0x2F8, 8 },
46 { ACPI_END_TAG_DESCRIPTOR, 0 }
47 };
48
49 //
50 // PS/2 Keyboard Controller
51 //
52 GLOBAL_REMOVE_IF_UNREFERENCED
53 SIO_RESOURCES_IO mPs2KeyboardDeviceResources = {
54 { { ACPI_FIXED_LOCATION_IO_PORT_DESCRIPTOR }, 0x60, 5 },
55 { ACPI_END_TAG_DESCRIPTOR, 0 }
56 };
57
58 //
59 // Table of SIO Controllers
60 //
61 GLOBAL_REMOVE_IF_UNREFERENCED
62 SIO_DEVICE_INFO mDevicesInfo[] = {
63 {
64 EISA_PNP_ID (0x501),
65 0,
66 { (ACPI_SMALL_RESOURCE_HEADER *) &mCom1Resources }
67 }, // COM 1 UART Controller
68 {
69 EISA_PNP_ID (0x501),
70 1,
71 { (ACPI_SMALL_RESOURCE_HEADER *) &mCom2Resources }
72 }, // COM 2 UART Controller
73 {
74 EISA_PNP_ID(0x303),
75 0,
76 { (ACPI_SMALL_RESOURCE_HEADER *) &mPs2KeyboardDeviceResources }
77 } // PS/2 Keyboard Controller
78 };
79
80 //
81 // ACPI Device Path Node template
82 //
83 GLOBAL_REMOVE_IF_UNREFERENCED
84 ACPI_HID_DEVICE_PATH mAcpiDeviceNodeTemplate = {
85 { // Header
86 ACPI_DEVICE_PATH,
87 ACPI_DP,
88 {
89 (UINT8) (sizeof (ACPI_HID_DEVICE_PATH)),
90 (UINT8) ((sizeof (ACPI_HID_DEVICE_PATH)) >> 8)
91 }
92 },
93 0x0, // HID
94 0x0 // UID
95 };
96
97
98 /**
99 Provides a low level access to the registers for the Super I/O.
100
101 @param[in] This Indicates a pointer to the calling context.
102 @param[in] Write Specifies the type of the register operation.
103 If this parameter is TRUE, Value is interpreted
104 as an input parameter and the operation is a
105 register write. If this parameter is FALSE,
106 Value is interpreted as an output parameter and
107 the operation is a register read.
108 @param[in] ExitCfgMode Exit Configuration Mode Indicator. If this
109 parameter is set to TRUE, the Super I/O driver
110 will turn off configuration mode of the Super
111 I/O prior to returning from this function. If
112 this parameter is set to FALSE, the Super I/O
113 driver will leave Super I/O in the
114 configuration mode. The Super I/O driver must
115 track the current state of the Super I/O and
116 enable the configuration mode of Super I/O if
117 necessary prior to register access.
118 @param[in] Register Register number.
119 @param[in,out] Value If Write is TRUE, Value is a pointer to the
120 buffer containing the byte of data to be
121 written to the Super I/O register. If Write is
122 FALSE, Value is a pointer to the destination
123 buffer for the byte of data to be read from the
124 Super I/O register.
125
126 @retval EFI_SUCCESS The operation completed successfully.
127 @retval EFI_INVALID_PARAMETER The Value is NULL.
128 @retval EFI_INVALID_PARAMETER Invalid Register number.
129
130 **/
131 EFI_STATUS
132 EFIAPI
133 SioRegisterAccess (
134 IN CONST EFI_SIO_PROTOCOL *This,
135 IN BOOLEAN Write,
136 IN BOOLEAN ExitCfgMode,
137 IN UINT8 Register,
138 IN OUT UINT8 *Value
139 )
140 {
141 return EFI_SUCCESS;
142 }
143
144 /**
145 Provides an interface to get a list of the current resources consumed by the
146 device in the ACPI Resource Descriptor format.
147
148 GetResources() returns a list of resources currently consumed by the device.
149 The ResourceList is a pointer to the buffer containing resource descriptors
150 for the device. The descriptors are in the format of Small or Large ACPI
151 resource descriptor as defined by ACPI specification (2.0 & 3.0). The buffer
152 of resource descriptors is terminated with the 'End tag' resource descriptor.
153
154 @param[in] This Indicates a pointer to the calling context.
155 @param[out] ResourceList A pointer to an ACPI resource descriptor list
156 that defines the current resources used by the
157 device.
158
159 @retval EFI_SUCCESS The operation completed successfully.
160 @retval EFI_INVALID_PARAMETER ResourceList is NULL.
161
162 **/
163 EFI_STATUS
164 EFIAPI
165 SioGetResources (
166 IN CONST EFI_SIO_PROTOCOL *This,
167 OUT ACPI_RESOURCE_HEADER_PTR *ResourceList
168 )
169 {
170 SIO_DEV *SioDevice;
171
172 if (ResourceList == NULL) {
173 return EFI_INVALID_PARAMETER;
174 }
175
176 SioDevice = SIO_DEV_FROM_SIO (This);
177 if (SioDevice->DeviceIndex < ARRAY_SIZE (mDevicesInfo)) {
178 *ResourceList = mDevicesInfo[SioDevice->DeviceIndex].Resources;
179 }
180
181 return EFI_SUCCESS;
182 }
183
184 /**
185 Sets the resources for the device.
186
187 @param[in] This Indicates a pointer to the calling context.
188 @param[in] ResourceList Pointer to the ACPI resource descriptor list.
189
190 @retval EFI_SUCCESS The operation completed successfully.
191 @retval EFI_INVALID_PARAMETER ResourceList is invalid.
192 @retval EFI_ACCESS_DENIED Some of the resources in ResourceList are in
193 use.
194
195 **/
196 EFI_STATUS
197 EFIAPI
198 SioSetResources (
199 IN CONST EFI_SIO_PROTOCOL *This,
200 IN ACPI_RESOURCE_HEADER_PTR ResourceList
201 )
202 {
203 return EFI_SUCCESS;
204 }
205
206 /**
207 Provides a collection of resource descriptor lists. Each resource descriptor
208 list in the collection defines a combination of resources that can
209 potentially be used by the device.
210
211 @param[in] This Indicates a pointer to the calling context.
212 @param[out] ResourceCollection Collection of the resource descriptor
213 lists.
214
215 @retval EFI_SUCCESS The operation completed successfully.
216 @retval EFI_INVALID_PARAMETER ResourceCollection is NULL.
217
218 **/
219 EFI_STATUS
220 EFIAPI
221 SioPossibleResources (
222 IN CONST EFI_SIO_PROTOCOL *This,
223 OUT ACPI_RESOURCE_HEADER_PTR *ResourceCollection
224 )
225 {
226 return EFI_SUCCESS;
227 }
228
229 /**
230 Provides an interface for a table based programming of the Super I/O
231 registers.
232
233 The Modify() function provides an interface for table based programming of
234 the Super I/O registers. This function can be used to perform programming of
235 multiple Super I/O registers with a single function call. For each table
236 entry, the Register is read, its content is bitwise ANDed with AndMask, and
237 then ORed with OrMask before being written back to the Register. The Super
238 I/O driver must track the current state of the Super I/O and enable the
239 configuration mode of Super I/O if necessary prior to table processing. Once
240 the table is processed, the Super I/O device has to be returned to the
241 original state.
242
243 @param[in] This Indicates a pointer to the calling context.
244 @param[in] Command A pointer to an array of NumberOfCommands
245 EFI_SIO_REGISTER_MODIFY structures. Each
246 structure specifies a single Super I/O register
247 modify operation.
248 @param[in] NumberOfCommands Number of elements in the Command array.
249
250 @retval EFI_SUCCESS The operation completed successfully.
251 @retval EFI_INVALID_PARAMETER Command is NULL.
252
253 **/
254 EFI_STATUS
255 EFIAPI
256 SioModify (
257 IN CONST EFI_SIO_PROTOCOL *This,
258 IN CONST EFI_SIO_REGISTER_MODIFY *Command,
259 IN UINTN NumberOfCommands
260 )
261 {
262 return EFI_SUCCESS;
263 }
264
265 /**
266 Create the child device with a given device index.
267
268 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL instance.
269 @param[in] Controller The handle of ISA bus controller.
270 @param[in] PciIo The pointer to the PCI protocol.
271 @param[in] ParentDevicePath Device path of the ISA bus controller.
272 @param[in] DeviceIndex Index of the device supported by this driver.
273
274 @retval EFI_SUCCESS The child device has been created successfully.
275 @retval Others Error occured during the child device creation.
276
277 **/
278 EFI_STATUS
279 SioCreateChildDevice (
280 IN EFI_DRIVER_BINDING_PROTOCOL *This,
281 IN EFI_HANDLE Controller,
282 IN EFI_PCI_IO_PROTOCOL *PciIo,
283 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,
284 IN UINT32 DeviceIndex
285 )
286 {
287 EFI_STATUS Status;
288 SIO_DEV *SioDevice;
289
290 //
291 // Initialize the SIO_DEV structure
292 //
293 SioDevice = AllocateZeroPool (sizeof (SIO_DEV));
294 if (SioDevice == NULL) {
295 return EFI_OUT_OF_RESOURCES;
296 }
297
298 SioDevice->Signature = SIO_DEV_SIGNATURE;
299 SioDevice->Handle = NULL;
300 SioDevice->PciIo = PciIo;
301
302 //
303 // Construct the child device path
304 //
305 mAcpiDeviceNodeTemplate.HID = mDevicesInfo[DeviceIndex].Hid;
306 mAcpiDeviceNodeTemplate.UID = mDevicesInfo[DeviceIndex].Uid;
307 SioDevice->DevicePath = AppendDevicePathNode (
308 ParentDevicePath,
309 (EFI_DEVICE_PATH_PROTOCOL *) &mAcpiDeviceNodeTemplate
310 );
311 if (SioDevice->DevicePath == NULL) {
312 Status = EFI_OUT_OF_RESOURCES;
313 goto Done;
314 }
315
316 CopyMem (&SioDevice->Sio, &mSioInterface, sizeof (EFI_SIO_PROTOCOL));
317 SioDevice->DeviceIndex = DeviceIndex;
318
319 //
320 // Create a child handle and install Device Path and Super I/O protocols
321 //
322 Status = gBS->InstallMultipleProtocolInterfaces (
323 &SioDevice->Handle,
324 &gEfiDevicePathProtocolGuid,
325 SioDevice->DevicePath,
326 &gEfiSioProtocolGuid,
327 &SioDevice->Sio,
328 NULL
329 );
330 if (EFI_ERROR (Status)) {
331 goto Done;
332 }
333
334 Status = gBS->OpenProtocol (
335 Controller,
336 &gEfiPciIoProtocolGuid,
337 (VOID **) &PciIo,
338 This->DriverBindingHandle,
339 SioDevice->Handle,
340 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
341 );
342 if (EFI_ERROR (Status)) {
343 gBS->UninstallMultipleProtocolInterfaces (
344 SioDevice->Handle,
345 &gEfiDevicePathProtocolGuid,
346 SioDevice->DevicePath,
347 &gEfiSioProtocolGuid,
348 &SioDevice->Sio,
349 NULL
350 );
351 }
352
353 Done:
354 if (EFI_ERROR (Status)) {
355 if (SioDevice->DevicePath != NULL) {
356 FreePool (SioDevice->DevicePath);
357 }
358
359 FreePool (SioDevice);
360 }
361
362 return Status;
363 }
364
365 /**
366 Create all the ISA child devices on the ISA bus controller (PCI to ISA
367 bridge).
368
369 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL instance.
370 @param[in] Controller The handle of ISA bus controller.
371 @param[in] PciIo The pointer to the PCI protocol.
372 @param[in] ParentDevicePath Device path of the ISA bus controller.
373
374 @retval The number of child device that is successfully created.
375
376 **/
377 UINT32
378 SioCreateAllChildDevices (
379 IN EFI_DRIVER_BINDING_PROTOCOL *This,
380 IN EFI_HANDLE Controller,
381 IN EFI_PCI_IO_PROTOCOL *PciIo,
382 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath
383 )
384 {
385 UINT32 Index;
386 UINT32 ChildDeviceNumber;
387 EFI_STATUS Status;
388
389 ChildDeviceNumber = 0;
390
391 for (Index = 0; Index < ARRAY_SIZE (mDevicesInfo); Index++) {
392 Status = SioCreateChildDevice (
393 This,
394 Controller,
395 PciIo,
396 ParentDevicePath,
397 Index
398 );
399 if (!EFI_ERROR (Status)) {
400 ChildDeviceNumber++;
401 }
402 }
403
404 return ChildDeviceNumber;
405 }