ISA Bus code scrub.
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Isa / IsaBusDxe / IsaBus.c
1 /**@file
2 ISA Bus UEFI driver.
3
4 Discovers all the ISA Controllers and their resources by using the ISA ACPI
5 Protocol, produces an instance of the ISA I/O Protocol for every ISA
6 Controller found. This driver is designed to manage a PCI-to-ISA bridge Device
7 such as LPC bridge.
8
9 Copyright (c) 2006 - 2009, Intel Corporation.<BR>
10 All rights reserved. This program and the accompanying materials
11 are licensed and made available under the terms and conditions of the BSD License
12 which accompanies this distribution. The full text of the license may be found at
13 http://opensource.org/licenses/bsd-license.php
14
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17
18 **/
19
20 #include "InternalIsaBus.h"
21
22 //
23 // ISA Bus Driver Global Variables
24 //
25 EFI_DRIVER_BINDING_PROTOCOL gIsaBusControllerDriver = {
26 IsaBusControllerDriverSupported,
27 IsaBusControllerDriverStart,
28 IsaBusControllerDriverStop,
29 0xa,
30 NULL,
31 NULL
32 };
33
34 /**
35 The main entry point for the ISA Bus driver.
36
37 @param[in] ImageHandle The firmware allocated handle for the EFI image.
38 @param[in] SystemTable A pointer to the EFI System Table.
39
40 @retval EFI_SUCCESS The entry point is executed successfully.
41 @retval EFI_OUT_OF_RESOURCES There was not enough memory in pool to install all the protocols.
42 **/
43 EFI_STATUS
44 EFIAPI
45 InitializeIsaBus(
46 IN EFI_HANDLE ImageHandle,
47 IN EFI_SYSTEM_TABLE *SystemTable
48 )
49 {
50 EFI_STATUS Status;
51
52 //
53 // Install driver model protocol(s).
54 //
55 Status = EfiLibInstallDriverBindingComponentName2 (
56 ImageHandle,
57 SystemTable,
58 &gIsaBusControllerDriver,
59 ImageHandle,
60 &gIsaBusComponentName,
61 &gIsaBusComponentName2
62 );
63 ASSERT_EFI_ERROR (Status);
64
65 return Status;
66 }
67
68 /**
69 Tests to see if a controller can be managed by the ISA Bus Driver. If a child device is provided,
70 it further tests to see if this driver supports creating a handle for the specified child device.
71
72 Note that the ISA Bus driver always creates all of its child handles on the first call to Start().
73 How the Start() function of a driver is implemented can affect how the Supported() function is implemented.
74
75 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
76 @param[in] Controller The handle of the controller to test.
77 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
78
79 @retval EFI_SUCCESS The device is supported by this driver.
80 @retval EFI_ALREADY_STARTED The device is already being managed by this driver.
81 @retval EFI_ACCESS_DENIED The device is already being managed by a different driver
82 or an application that requires exclusive access.
83 @retval EFI_UNSUPPORTED The device is is not supported by this driver.
84
85 **/
86 EFI_STATUS
87 EFIAPI
88 IsaBusControllerDriverSupported (
89 IN EFI_DRIVER_BINDING_PROTOCOL *This,
90 IN EFI_HANDLE Controller,
91 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
92 )
93 {
94 EFI_STATUS Status;
95 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
96 EFI_ISA_ACPI_PROTOCOL *IsaAcpi;
97
98 //
99 // If RemainingDevicePath is not NULL, it should verify that the first device
100 // path node in RemainingDevicePath is an ACPI Device path node which is a
101 // legal Device Path Node for this bus driver's children.
102 //
103 if (RemainingDevicePath != NULL) {
104 if (RemainingDevicePath->Type != ACPI_DEVICE_PATH) {
105 return EFI_UNSUPPORTED;
106 } else if (RemainingDevicePath->SubType == ACPI_DP) {
107 if (DevicePathNodeLength (RemainingDevicePath) != sizeof (ACPI_HID_DEVICE_PATH)) {
108 return EFI_UNSUPPORTED;
109 }
110 } else if (RemainingDevicePath->SubType == ACPI_EXTENDED_DP) {
111 if (DevicePathNodeLength (RemainingDevicePath) != sizeof (ACPI_EXTENDED_HID_DEVICE_PATH)) {
112 return EFI_UNSUPPORTED;
113 }
114 } else {
115 return EFI_UNSUPPORTED;
116 }
117 }
118 //
119 // Try to open EFI DEVICE PATH protocol on the controller
120 //
121 Status = gBS->OpenProtocol (
122 Controller,
123 &gEfiDevicePathProtocolGuid,
124 (VOID **) &ParentDevicePath,
125 This->DriverBindingHandle,
126 Controller,
127 EFI_OPEN_PROTOCOL_BY_DRIVER
128 );
129 //
130 // Although this driver creates all child handles at one time,
131 // but because all child handles may be not stopped at one time in EFI Driver Binding.Stop(),
132 // So it is allowed to create child handles again in successive calls to EFI Driver Binding.Start().
133 //
134 if (Status == EFI_ALREADY_STARTED) {
135 return EFI_SUCCESS;
136 }
137
138 if (EFI_ERROR (Status)) {
139 return Status;
140 }
141
142 gBS->CloseProtocol (
143 Controller,
144 &gEfiDevicePathProtocolGuid,
145 This->DriverBindingHandle,
146 Controller
147 );
148
149 //
150 // Try to get Pci IO Protocol because it is assumed
151 // to have been opened by ISA ACPI driver
152 //
153 Status = gBS->OpenProtocol (
154 Controller,
155 &gEfiPciIoProtocolGuid,
156 NULL,
157 This->DriverBindingHandle,
158 Controller,
159 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
160 );
161 if (EFI_ERROR (Status)) {
162 return Status;
163 }
164
165 //
166 // Try to open the Isa Acpi protocol on the controller
167 //
168 Status = gBS->OpenProtocol (
169 Controller,
170 &gEfiIsaAcpiProtocolGuid,
171 (VOID **) &IsaAcpi,
172 This->DriverBindingHandle,
173 Controller,
174 EFI_OPEN_PROTOCOL_BY_DRIVER
175 );
176 if (EFI_ERROR (Status)) {
177 return Status;
178 }
179
180 //
181 // Add more check to see if the child device is valid by calling IsaAcpi->DeviceEnumerate?
182 //
183
184 gBS->CloseProtocol (
185 Controller,
186 &gEfiIsaAcpiProtocolGuid,
187 This->DriverBindingHandle,
188 Controller
189 );
190
191 return Status;
192 }
193
194 /**
195 Start this driver on ControllerHandle.
196
197 Note that the ISA Bus driver always creates all of its child handles on the first call to Start().
198 The Start() function is designed to be invoked from the EFI boot service ConnectController().
199 As a result, much of the error checking on the parameters to Start() has been moved into this
200 common boot service. It is legal to call Start() from other locations, but the following calling
201 restrictions must be followed or the system behavior will not be deterministic.
202 1. ControllerHandle must be a valid EFI_HANDLE.
203 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
204 EFI_DEVICE_PATH_PROTOCOL.
205 3. Prior to calling Start(), the Supported() function for the driver specified by This must
206 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
207
208 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
209 @param[in] ControllerHandle The handle of the controller to start. This handle
210 must support a protocol interface that supplies
211 an I/O abstraction to the driver.
212 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path.
213 This parameter is ignored by device drivers, and is optional for bus drivers.
214
215 @retval EFI_SUCCESS The device was started.
216 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.
217 Currently not implemented.
218 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
219 @retval Others The driver failded to start the device.
220 **/
221 EFI_STATUS
222 EFIAPI
223 IsaBusControllerDriverStart (
224 IN EFI_DRIVER_BINDING_PROTOCOL *This,
225 IN EFI_HANDLE Controller,
226 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL
227 )
228 {
229 EFI_STATUS Status;
230 EFI_PCI_IO_PROTOCOL *PciIo;
231 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
232 EFI_ISA_ACPI_PROTOCOL *IsaAcpi;
233 EFI_ISA_ACPI_DEVICE_ID *IsaDevice;
234 EFI_ISA_ACPI_RESOURCE_LIST *ResourceList;
235 EFI_GENERIC_MEMORY_TEST_PROTOCOL *GenMemoryTest;
236
237 //
238 // Local variables declaration for StatusCode reporting
239 //
240 EFI_DEVICE_PATH_PROTOCOL *DevicePathData;
241
242 //
243 // Get Pci IO Protocol
244 //
245 Status = gBS->OpenProtocol (
246 Controller,
247 &gEfiPciIoProtocolGuid,
248 (VOID **) &PciIo,
249 This->DriverBindingHandle,
250 Controller,
251 EFI_OPEN_PROTOCOL_GET_PROTOCOL
252 );
253 if (EFI_ERROR (Status)) {
254 return Status;
255 }
256
257 //
258 // Open Device Path Protocol
259 //
260 Status = gBS->OpenProtocol (
261 Controller,
262 &gEfiDevicePathProtocolGuid,
263 (VOID **) &ParentDevicePath,
264 This->DriverBindingHandle,
265 Controller,
266 EFI_OPEN_PROTOCOL_BY_DRIVER
267 );
268 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
269 return Status;
270 }
271
272 //
273 // Open ISA Acpi Protocol
274 //
275 Status = gBS->OpenProtocol (
276 Controller,
277 &gEfiIsaAcpiProtocolGuid,
278 (VOID **) &IsaAcpi,
279 This->DriverBindingHandle,
280 Controller,
281 EFI_OPEN_PROTOCOL_BY_DRIVER
282 );
283 if (EFI_ERROR (Status) && Status != EFI_ALREADY_STARTED) {
284 //
285 // Close opened protocol
286 //
287 gBS->CloseProtocol (
288 Controller,
289 &gEfiDevicePathProtocolGuid,
290 This->DriverBindingHandle,
291 Controller
292 );
293 return Status;
294 }
295 //
296 // The IsaBus driver will use memory below 16M, which is not tested yet,
297 // so call CompatibleRangeTest to test them. Since memory below 1M should
298 // be reserved to CSM, and 15M~16M might be reserved for Isa hole, test 1M
299 // ~15M here
300 //
301 Status = gBS->LocateProtocol (
302 &gEfiGenericMemTestProtocolGuid,
303 NULL,
304 (VOID **) &GenMemoryTest
305 );
306
307 if (!EFI_ERROR (Status)) {
308 Status = GenMemoryTest->CompatibleRangeTest (
309 GenMemoryTest,
310 0x100000,
311 0xE00000
312 );
313 }
314 //
315 // Report Status Code here since we will initialize the host controller
316 //
317 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
318 EFI_PROGRESS_CODE,
319 (EFI_IO_BUS_LPC | EFI_IOB_PC_INIT),
320 ParentDevicePath
321 );
322
323 //
324 // first init ISA interface
325 //
326 IsaAcpi->InterfaceInit (IsaAcpi);
327
328 //
329 // Report Status Code here since we will enable the host controller
330 //
331 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
332 EFI_PROGRESS_CODE,
333 (EFI_IO_BUS_LPC | EFI_IOB_PC_ENABLE),
334 ParentDevicePath
335 );
336
337 //
338 // Create each ISA device handle in this ISA bus
339 //
340 IsaDevice = NULL;
341 do {
342 Status = IsaAcpi->DeviceEnumerate (IsaAcpi, &IsaDevice);
343 if (EFI_ERROR (Status)) {
344 break;
345 }
346 //
347 // Get current resource of this ISA device
348 //
349 ResourceList = NULL;
350 Status = IsaAcpi->GetCurResource (IsaAcpi, IsaDevice, &ResourceList);
351 if (EFI_ERROR (Status)) {
352 continue;
353 }
354
355 //
356 // Create handle for this ISA device
357 //
358 // If any child device handle was created in previous call to Start() and not stopped
359 // in previous call to Stop(), it will not be created again because the
360 // InstallMultipleProtocolInterfaces() boot service will reject same device path.
361 //
362 Status = IsaCreateDevice (
363 This,
364 Controller,
365 PciIo,
366 ParentDevicePath,
367 ResourceList,
368 &DevicePathData
369 );
370
371 if (EFI_ERROR (Status)) {
372 continue;
373 }
374 //
375 // Initialize ISA device
376 //
377 IsaAcpi->InitDevice (IsaAcpi, IsaDevice);
378
379 //
380 // Set resources for this ISA device
381 //
382 Status = IsaAcpi->SetResource (IsaAcpi, IsaDevice, ResourceList);
383
384 //
385 // Report Status Code here when failed to resource conflicts
386 //
387 if (EFI_ERROR (Status) && (Status != EFI_UNSUPPORTED)) {
388 //
389 // It's hard to tell which resource conflicts
390 //
391 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
392 EFI_ERROR_CODE,
393 (EFI_IO_BUS_LPC | EFI_IOB_EC_RESOURCE_CONFLICT),
394 DevicePathData
395 );
396
397 }
398 //
399 // Set power for this ISA device
400 //
401 IsaAcpi->SetPower (IsaAcpi, IsaDevice, TRUE);
402
403 //
404 // Enable this ISA device
405 //
406 IsaAcpi->EnableDevice (IsaAcpi, IsaDevice, TRUE);
407
408 } while (TRUE);
409
410 return EFI_SUCCESS;
411 }
412
413 /**
414 Stop this driver on ControllerHandle.
415
416 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
417 As a result, much of the error checking on the parameters to Stop() has been moved
418 into this common boot service. It is legal to call Stop() from other locations,
419 but the following calling restrictions must be followed or the system behavior will not be deterministic.
420 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
421 same driver's Start() function.
422 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
423 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
424 Start() function, and the Start() function must have called OpenProtocol() on
425 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
426
427 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
428 @param[in] ControllerHandle A handle to the device being stopped. The handle must
429 support a bus specific I/O protocol for the driver
430 to use to stop the device.
431 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
432 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
433 if NumberOfChildren is 0.
434
435 @retval EFI_SUCCESS The device was stopped.
436 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
437 **/
438 EFI_STATUS
439 EFIAPI
440 IsaBusControllerDriverStop (
441 IN EFI_DRIVER_BINDING_PROTOCOL * This,
442 IN EFI_HANDLE Controller,
443 IN UINTN NumberOfChildren,
444 IN EFI_HANDLE * ChildHandleBuffer OPTIONAL
445 )
446 {
447 EFI_STATUS Status;
448 UINTN Index;
449 BOOLEAN AllChildrenStopped;
450 ISA_IO_DEVICE *IsaIoDevice;
451 EFI_ISA_IO_PROTOCOL *IsaIo;
452 EFI_PCI_IO_PROTOCOL *PciIo;
453
454 if (NumberOfChildren == 0) {
455 //
456 // Close the bus driver
457 //
458 Status = gBS->CloseProtocol (
459 Controller,
460 &gEfiDevicePathProtocolGuid,
461 This->DriverBindingHandle,
462 Controller
463 );
464 if (EFI_ERROR (Status)) {
465 return Status;
466 }
467
468 Status = gBS->CloseProtocol (
469 Controller,
470 &gEfiIsaAcpiProtocolGuid,
471 This->DriverBindingHandle,
472 Controller
473 );
474 if (EFI_ERROR (Status)) {
475 return Status;
476 }
477
478 return EFI_SUCCESS;
479 }
480 //
481 // Complete all outstanding transactions to Controller.
482 // Don't allow any new transaction to Controller to be started.
483 //
484 //
485 // Stop all the children
486 // Find all the ISA devices that were discovered on this PCI to ISA Bridge
487 // with the Start() function.
488 //
489 AllChildrenStopped = TRUE;
490
491 for (Index = 0; Index < NumberOfChildren; Index++) {
492
493 Status = gBS->OpenProtocol (
494 ChildHandleBuffer[Index],
495 &gEfiIsaIoProtocolGuid,
496 (VOID **) &IsaIo,
497 This->DriverBindingHandle,
498 Controller,
499 EFI_OPEN_PROTOCOL_GET_PROTOCOL
500 );
501 if (!EFI_ERROR (Status)) {
502
503 IsaIoDevice = ISA_IO_DEVICE_FROM_ISA_IO_THIS (IsaIo);
504
505 //
506 // Close the child handle
507 //
508
509 Status = gBS->CloseProtocol (
510 Controller,
511 &gEfiPciIoProtocolGuid,
512 This->DriverBindingHandle,
513 ChildHandleBuffer[Index]
514 );
515 Status = gBS->UninstallMultipleProtocolInterfaces (
516 ChildHandleBuffer[Index],
517 &gEfiDevicePathProtocolGuid,
518 IsaIoDevice->DevicePath,
519 &gEfiIsaIoProtocolGuid,
520 &IsaIoDevice->IsaIo,
521 NULL
522 );
523
524 if (!EFI_ERROR (Status)) {
525 FreePool (IsaIoDevice->DevicePath);
526 FreePool (IsaIoDevice);
527 } else {
528 //
529 // Re-open PCI IO Protocol on behalf of the child device
530 // because of failure of destroying the child device handle
531 //
532 gBS->OpenProtocol (
533 Controller,
534 &gEfiPciIoProtocolGuid,
535 (VOID **) &PciIo,
536 This->DriverBindingHandle,
537 ChildHandleBuffer[Index],
538 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
539 );
540 }
541 }
542
543 if (EFI_ERROR (Status)) {
544 AllChildrenStopped = FALSE;
545 }
546 }
547
548 if (!AllChildrenStopped) {
549 return EFI_DEVICE_ERROR;
550 }
551
552 return EFI_SUCCESS;
553 }
554
555 //
556 // Internal Function
557 //
558
559 /**
560 Create EFI Handle for a ISA device found via ISA ACPI Protocol
561
562 @param[in] This The EFI_DRIVER_BINDING_PROTOCOL instance.
563 @param[in] Controller The handle of ISA bus controller(PCI to ISA bridge)
564 @param[in] PciIo The Pointer to the PCI protocol
565 @param[in] ParentDevicePath Device path of the ISA bus controller
566 @param[in] IsaDeviceResourceList The resource list of the ISA device
567 @param[in] ChildDevicePath The pointer to the child device.
568
569 @retval EFI_SUCCESS The handle for the child device was created.
570 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
571 @retval EFI_DEVICE_ERROR The handle for the child device can not be created.
572 **/
573 EFI_STATUS
574 IsaCreateDevice (
575 IN EFI_DRIVER_BINDING_PROTOCOL *This,
576 IN EFI_HANDLE Controller,
577 IN EFI_PCI_IO_PROTOCOL *PciIo,
578 IN EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath,
579 IN EFI_ISA_ACPI_RESOURCE_LIST *IsaDeviceResourceList,
580 OUT EFI_DEVICE_PATH_PROTOCOL **ChildDevicePath
581 )
582 {
583 EFI_STATUS Status;
584 ISA_IO_DEVICE *IsaIoDevice;
585 EFI_DEV_PATH Node;
586
587 //
588 // Initialize the PCI_IO_DEVICE structure
589 //
590 IsaIoDevice = AllocateZeroPool (sizeof (ISA_IO_DEVICE));
591 if (IsaIoDevice == NULL) {
592 return EFI_OUT_OF_RESOURCES;
593 }
594
595 IsaIoDevice->Signature = ISA_IO_DEVICE_SIGNATURE;
596 IsaIoDevice->Handle = NULL;
597 IsaIoDevice->PciIo = PciIo;
598
599 //
600 // Initialize the ISA I/O instance structure
601 //
602 InitializeIsaIoInstance (IsaIoDevice, IsaDeviceResourceList);
603
604 //
605 // Build the child device path
606 //
607 Node.DevPath.Type = ACPI_DEVICE_PATH;
608 Node.DevPath.SubType = ACPI_DP;
609 SetDevicePathNodeLength (&Node.DevPath, sizeof (ACPI_HID_DEVICE_PATH));
610 Node.Acpi.HID = IsaDeviceResourceList->Device.HID;
611 Node.Acpi.UID = IsaDeviceResourceList->Device.UID;
612
613 IsaIoDevice->DevicePath = AppendDevicePathNode (
614 ParentDevicePath,
615 &Node.DevPath
616 );
617
618 if (IsaIoDevice->DevicePath == NULL) {
619 Status = EFI_OUT_OF_RESOURCES;
620 goto Done;
621 }
622
623 *ChildDevicePath = IsaIoDevice->DevicePath;
624
625 //
626 // Create a child handle and install Device Path and ISA I/O protocols
627 //
628 Status = gBS->InstallMultipleProtocolInterfaces (
629 &IsaIoDevice->Handle,
630 &gEfiDevicePathProtocolGuid,
631 IsaIoDevice->DevicePath,
632 &gEfiIsaIoProtocolGuid,
633 &IsaIoDevice->IsaIo,
634 NULL
635 );
636 if (EFI_ERROR (Status)) {
637 goto Done;
638 }
639
640 Status = gBS->OpenProtocol (
641 Controller,
642 &gEfiPciIoProtocolGuid,
643 (VOID **) &PciIo,
644 This->DriverBindingHandle,
645 IsaIoDevice->Handle,
646 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
647 );
648 if (EFI_ERROR (Status)) {
649 gBS->UninstallMultipleProtocolInterfaces (
650 IsaIoDevice->Handle,
651 &gEfiDevicePathProtocolGuid,
652 IsaIoDevice->DevicePath,
653 &gEfiIsaIoProtocolGuid,
654 &IsaIoDevice->IsaIo,
655 NULL
656 );
657 }
658
659 Done:
660
661 if (EFI_ERROR (Status)) {
662 if (IsaIoDevice->DevicePath != NULL) {
663 FreePool (IsaIoDevice->DevicePath);
664 }
665
666 FreePool (IsaIoDevice);
667 }
668
669 return Status;
670 }
671