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