]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/UfsPciHcDxe/UfsPciHcDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / UfsPciHcDxe / UfsPciHcDxe.c
1 /** @file
2 UfsHcDxe driver is used to provide platform-dependent info, mainly UFS host controller
3 MMIO base, to upper layer UFS drivers.
4
5 Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8 **/
9
10 #include "UfsPciHcDxe.h"
11
12 //
13 // NVM Express Driver Binding Protocol Instance
14 //
15 EFI_DRIVER_BINDING_PROTOCOL gUfsHcDriverBinding = {
16 UfsHcDriverBindingSupported,
17 UfsHcDriverBindingStart,
18 UfsHcDriverBindingStop,
19 0x10,
20 NULL,
21 NULL
22 };
23
24 //
25 // Template for Ufs host controller private data.
26 //
27 UFS_HOST_CONTROLLER_PRIVATE_DATA gUfsHcTemplate = {
28 UFS_HC_PRIVATE_DATA_SIGNATURE, // Signature
29 { // UfsHcProtocol
30 UfsHcGetMmioBar,
31 UfsHcAllocateBuffer,
32 UfsHcFreeBuffer,
33 UfsHcMap,
34 UfsHcUnmap,
35 UfsHcFlush,
36 UfsHcMmioRead,
37 UfsHcMmioWrite
38 },
39 NULL, // PciIo
40 0, // BarIndex
41 0 // PciAttributes
42 };
43
44 /**
45 Get the MMIO base of the UFS host controller.
46
47 @param[in] This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
48 @param[out] MmioBar The MMIO base address of UFS host controller.
49
50 @retval EFI_SUCCESS The operation succeeds.
51 @retval others The operation fails.
52 **/
53 EFI_STATUS
54 EFIAPI
55 UfsHcGetMmioBar (
56 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
57 OUT UINTN *MmioBar
58 )
59 {
60 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
61 EFI_PCI_IO_PROTOCOL *PciIo;
62 EFI_STATUS Status;
63 UINT8 BarIndex;
64 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *BarDesc;
65
66 if ((This == NULL) || (MmioBar == NULL)) {
67 return EFI_INVALID_PARAMETER;
68 }
69
70 BarDesc = NULL;
71 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
72 PciIo = Private->PciIo;
73 BarIndex = Private->BarIndex;
74
75 Status = PciIo->GetBarAttributes (
76 PciIo,
77 BarIndex,
78 NULL,
79 (VOID **)&BarDesc
80 );
81 if (EFI_ERROR (Status)) {
82 return Status;
83 }
84
85 *MmioBar = (UINTN)BarDesc->AddrRangeMin;
86
87 FreePool (BarDesc);
88
89 return Status;
90 }
91
92 /**
93 Provides the UFS controller-specific addresses needed to access system memory.
94
95 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
96 @param Operation Indicates if the bus master is going to read or write to system memory.
97 @param HostAddress The system memory address to map to the UFS controller.
98 @param NumberOfBytes On input the number of bytes to map. On output the number of bytes
99 that were mapped.
100 @param DeviceAddress The resulting map address for the bus master UFS controller to use to
101 access the hosts HostAddress.
102 @param Mapping A resulting value to pass to Unmap().
103
104 @retval EFI_SUCCESS The range was mapped for the returned NumberOfBytes.
105 @retval EFI_UNSUPPORTED The HostAddress cannot be mapped as a common buffer.
106 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
107 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
108 @retval EFI_DEVICE_ERROR The system hardware could not map the requested address.
109
110 **/
111 EFI_STATUS
112 EFIAPI
113 UfsHcMap (
114 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
115 IN EDKII_UFS_HOST_CONTROLLER_OPERATION Operation,
116 IN VOID *HostAddress,
117 IN OUT UINTN *NumberOfBytes,
118 OUT EFI_PHYSICAL_ADDRESS *DeviceAddress,
119 OUT VOID **Mapping
120 )
121 {
122 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
123 EFI_PCI_IO_PROTOCOL *PciIo;
124 EFI_STATUS Status;
125
126 if ((This == NULL) || (HostAddress == NULL) || (NumberOfBytes == NULL) || (DeviceAddress == NULL) || (Mapping == NULL)) {
127 return EFI_INVALID_PARAMETER;
128 }
129
130 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
131 PciIo = Private->PciIo;
132
133 Status = PciIo->Map (PciIo, (EFI_PCI_IO_PROTOCOL_OPERATION)Operation, HostAddress, NumberOfBytes, DeviceAddress, Mapping);
134 return Status;
135 }
136
137 /**
138 Completes the Map() operation and releases any corresponding resources.
139
140 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
141 @param Mapping The mapping value returned from Map().
142
143 @retval EFI_SUCCESS The range was unmapped.
144 @retval EFI_DEVICE_ERROR The data was not committed to the target system memory.
145
146 **/
147 EFI_STATUS
148 EFIAPI
149 UfsHcUnmap (
150 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
151 IN VOID *Mapping
152 )
153 {
154 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
155 EFI_PCI_IO_PROTOCOL *PciIo;
156 EFI_STATUS Status;
157
158 if ((This == NULL) || (Mapping == NULL)) {
159 return EFI_INVALID_PARAMETER;
160 }
161
162 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
163 PciIo = Private->PciIo;
164
165 Status = PciIo->Unmap (PciIo, Mapping);
166 return Status;
167 }
168
169 /**
170 Allocates pages that are suitable for an EfiUfsHcOperationBusMasterCommonBuffer
171 mapping.
172
173 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
174 @param Type This parameter is not used and must be ignored.
175 @param MemoryType The type of memory to allocate, EfiBootServicesData or
176 EfiRuntimeServicesData.
177 @param Pages The number of pages to allocate.
178 @param HostAddress A pointer to store the base system memory address of the
179 allocated range.
180 @param Attributes The requested bit mask of attributes for the allocated range.
181
182 @retval EFI_SUCCESS The requested memory pages were allocated.
183 @retval EFI_UNSUPPORTED Attributes is unsupported. The only legal attribute bits are
184 MEMORY_WRITE_COMBINE and MEMORY_CACHED.
185 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
186 @retval EFI_OUT_OF_RESOURCES The memory pages could not be allocated.
187
188 **/
189 EFI_STATUS
190 EFIAPI
191 UfsHcAllocateBuffer (
192 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
193 IN EFI_ALLOCATE_TYPE Type,
194 IN EFI_MEMORY_TYPE MemoryType,
195 IN UINTN Pages,
196 OUT VOID **HostAddress,
197 IN UINT64 Attributes
198 )
199 {
200 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
201 EFI_PCI_IO_PROTOCOL *PciIo;
202 EFI_STATUS Status;
203
204 if ((This == NULL) || (HostAddress == NULL)) {
205 return EFI_INVALID_PARAMETER;
206 }
207
208 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
209 PciIo = Private->PciIo;
210
211 Status = PciIo->AllocateBuffer (PciIo, Type, MemoryType, Pages, HostAddress, Attributes);
212 return Status;
213 }
214
215 /**
216 Frees memory that was allocated with AllocateBuffer().
217
218 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
219 @param Pages The number of pages to free.
220 @param HostAddress The base system memory address of the allocated range.
221
222 @retval EFI_SUCCESS The requested memory pages were freed.
223 @retval EFI_INVALID_PARAMETER The memory range specified by HostAddress and Pages
224 was not allocated with AllocateBuffer().
225
226 **/
227 EFI_STATUS
228 EFIAPI
229 UfsHcFreeBuffer (
230 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
231 IN UINTN Pages,
232 IN VOID *HostAddress
233 )
234 {
235 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
236 EFI_PCI_IO_PROTOCOL *PciIo;
237 EFI_STATUS Status;
238
239 if ((This == NULL) || (HostAddress == NULL)) {
240 return EFI_INVALID_PARAMETER;
241 }
242
243 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
244 PciIo = Private->PciIo;
245
246 Status = PciIo->FreeBuffer (PciIo, Pages, HostAddress);
247 return Status;
248 }
249
250 /**
251 Flushes all posted write transactions from the UFS bus to attached UFS device.
252
253 @param This A pointer to the EFI_UFS_HOST_CONTROLLER_PROTOCOL instance.
254
255 @retval EFI_SUCCESS The posted write transactions were flushed from the UFS bus
256 to attached UFS device.
257 @retval EFI_DEVICE_ERROR The posted write transactions were not flushed from the UFS
258 bus to attached UFS device due to a hardware error.
259
260 **/
261 EFI_STATUS
262 EFIAPI
263 UfsHcFlush (
264 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This
265 )
266 {
267 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
268 EFI_PCI_IO_PROTOCOL *PciIo;
269 EFI_STATUS Status;
270
271 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
272 PciIo = Private->PciIo;
273
274 Status = PciIo->Flush (PciIo);
275 return Status;
276 }
277
278 /**
279 Enable a UFS bus driver to access UFS MMIO registers in the UFS Host Controller memory space.
280
281 @param This A pointer to the EDKII_UFS_HOST_CONTROLLER_PROTOCOL instance.
282 @param Width Signifies the width of the memory operations.
283 @param Offset The offset within the UFS Host Controller MMIO space to start the
284 memory operation.
285 @param Count The number of memory operations to perform.
286 @param Buffer For read operations, the destination buffer to store the results.
287 For write operations, the source buffer to write data from.
288
289 @retval EFI_SUCCESS The data was read from or written to the UFS host controller.
290 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
291 valid for the UFS Host Controller memory space.
292 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
293 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
294
295 **/
296 EFI_STATUS
297 EFIAPI
298 UfsHcMmioRead (
299 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
300 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL_WIDTH Width,
301 IN UINT64 Offset,
302 IN UINTN Count,
303 IN OUT VOID *Buffer
304 )
305 {
306 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
307 EFI_PCI_IO_PROTOCOL *PciIo;
308 EFI_STATUS Status;
309 UINT8 BarIndex;
310
311 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
312 PciIo = Private->PciIo;
313 BarIndex = Private->BarIndex;
314
315 Status = PciIo->Mem.Read (PciIo, (EFI_PCI_IO_PROTOCOL_WIDTH)Width, BarIndex, Offset, Count, Buffer);
316
317 return Status;
318 }
319
320 /**
321 Enable a UFS bus driver to access UFS MMIO registers in the UFS Host Controller memory space.
322
323 @param This A pointer to the EDKII_UFS_HOST_CONTROLLER_PROTOCOL instance.
324 @param Width Signifies the width of the memory operations.
325 @param Offset The offset within the UFS Host Controller MMIO space to start the
326 memory operation.
327 @param Count The number of memory operations to perform.
328 @param Buffer For read operations, the destination buffer to store the results.
329 For write operations, the source buffer to write data from.
330
331 @retval EFI_SUCCESS The data was read from or written to the UFS host controller.
332 @retval EFI_UNSUPPORTED The address range specified by Offset, Width, and Count is not
333 valid for the UFS Host Controller memory space.
334 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
335 @retval EFI_INVALID_PARAMETER One or more parameters are invalid.
336
337 **/
338 EFI_STATUS
339 EFIAPI
340 UfsHcMmioWrite (
341 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL *This,
342 IN EDKII_UFS_HOST_CONTROLLER_PROTOCOL_WIDTH Width,
343 IN UINT64 Offset,
344 IN UINTN Count,
345 IN OUT VOID *Buffer
346 )
347 {
348 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
349 EFI_PCI_IO_PROTOCOL *PciIo;
350 EFI_STATUS Status;
351 UINT8 BarIndex;
352
353 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (This);
354 PciIo = Private->PciIo;
355 BarIndex = Private->BarIndex;
356
357 Status = PciIo->Mem.Write (PciIo, (EFI_PCI_IO_PROTOCOL_WIDTH)Width, BarIndex, Offset, Count, Buffer);
358
359 return Status;
360 }
361
362 /**
363 Tests to see if this driver supports a given controller. If a child device is provided,
364 it further tests to see if this driver supports creating a handle for the specified child device.
365
366 This function checks to see if the driver specified by This supports the device specified by
367 ControllerHandle. Drivers will typically use the device path attached to
368 ControllerHandle and/or the services from the bus I/O abstraction attached to
369 ControllerHandle to determine if the driver supports ControllerHandle. This function
370 may be called many times during platform initialization. In order to reduce boot times, the tests
371 performed by this function must be very small, and take as little time as possible to execute. This
372 function must not change the state of any hardware devices, and this function must be aware that the
373 device specified by ControllerHandle may already be managed by the same driver or a
374 different driver. This function must match its calls to AllocatePages() with FreePages(),
375 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
376 Since ControllerHandle may have been previously started by the same driver, if a protocol is
377 already in the opened state, then it must not be closed with CloseProtocol(). This is required
378 to guarantee the state of ControllerHandle is not modified by this function.
379
380 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
381 @param[in] ControllerHandle The handle of the controller to test. This handle
382 must support a protocol interface that supplies
383 an I/O abstraction to the driver.
384 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
385 parameter is ignored by device drivers, and is optional for bus
386 drivers. For bus drivers, if this parameter is not NULL, then
387 the bus driver must determine if the bus controller specified
388 by ControllerHandle and the child controller specified
389 by RemainingDevicePath are both supported by this
390 bus driver.
391
392 @retval EFI_SUCCESS The device specified by ControllerHandle and
393 RemainingDevicePath is supported by the driver specified by This.
394 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
395 RemainingDevicePath is already being managed by the driver
396 specified by This.
397 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
398 RemainingDevicePath is already being managed by a different
399 driver or an application that requires exclusive access.
400 Currently not implemented.
401 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
402 RemainingDevicePath is not supported by the driver specified by This.
403 **/
404 EFI_STATUS
405 EFIAPI
406 UfsHcDriverBindingSupported (
407 IN EFI_DRIVER_BINDING_PROTOCOL *This,
408 IN EFI_HANDLE Controller,
409 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
410 )
411 {
412 EFI_STATUS Status;
413 BOOLEAN UfsHcFound;
414 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
415 EFI_PCI_IO_PROTOCOL *PciIo;
416 PCI_TYPE00 PciData;
417
418 PciIo = NULL;
419 ParentDevicePath = NULL;
420 UfsHcFound = FALSE;
421
422 //
423 // UfsHcDxe is a device driver, and should ingore the
424 // "RemainingDevicePath" according to EFI spec
425 //
426 Status = gBS->OpenProtocol (
427 Controller,
428 &gEfiDevicePathProtocolGuid,
429 (VOID *)&ParentDevicePath,
430 This->DriverBindingHandle,
431 Controller,
432 EFI_OPEN_PROTOCOL_BY_DRIVER
433 );
434 if (EFI_ERROR (Status)) {
435 //
436 // EFI_ALREADY_STARTED is also an error
437 //
438 return Status;
439 }
440
441 //
442 // Close the protocol because we don't use it here
443 //
444 gBS->CloseProtocol (
445 Controller,
446 &gEfiDevicePathProtocolGuid,
447 This->DriverBindingHandle,
448 Controller
449 );
450
451 //
452 // Now test the EfiPciIoProtocol
453 //
454 Status = gBS->OpenProtocol (
455 Controller,
456 &gEfiPciIoProtocolGuid,
457 (VOID **)&PciIo,
458 This->DriverBindingHandle,
459 Controller,
460 EFI_OPEN_PROTOCOL_BY_DRIVER
461 );
462 if (EFI_ERROR (Status)) {
463 return Status;
464 }
465
466 //
467 // Now further check the PCI header: Base class (offset 0x0B) and
468 // Sub Class (offset 0x0A). This controller should be an UFS controller
469 //
470 Status = PciIo->Pci.Read (
471 PciIo,
472 EfiPciIoWidthUint8,
473 0,
474 sizeof (PciData),
475 &PciData
476 );
477 if (EFI_ERROR (Status)) {
478 gBS->CloseProtocol (
479 Controller,
480 &gEfiPciIoProtocolGuid,
481 This->DriverBindingHandle,
482 Controller
483 );
484 return EFI_UNSUPPORTED;
485 }
486
487 //
488 // Since we already got the PciData, we can close protocol to avoid to carry it on for multiple exit points.
489 //
490 gBS->CloseProtocol (
491 Controller,
492 &gEfiPciIoProtocolGuid,
493 This->DriverBindingHandle,
494 Controller
495 );
496
497 //
498 // Examine UFS Host Controller PCI Configuration table fields
499 //
500 if (PciData.Hdr.ClassCode[2] == PCI_CLASS_MASS_STORAGE) {
501 if (PciData.Hdr.ClassCode[1] == 0x09 ) {
502 // UFS Controller Subclass
503 UfsHcFound = TRUE;
504 }
505 }
506
507 if (!UfsHcFound) {
508 return EFI_UNSUPPORTED;
509 }
510
511 return Status;
512 }
513
514 /**
515 Starts a device controller or a bus controller.
516
517 The Start() function is designed to be invoked from the EFI boot service ConnectController().
518 As a result, much of the error checking on the parameters to Start() has been moved into this
519 common boot service. It is legal to call Start() from other locations,
520 but the following calling restrictions must be followed or the system behavior will not be deterministic.
521 1. ControllerHandle must be a valid EFI_HANDLE.
522 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
523 EFI_DEVICE_PATH_PROTOCOL.
524 3. Prior to calling Start(), the Supported() function for the driver specified by This must
525 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
526
527 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
528 @param[in] ControllerHandle The handle of the controller to start. This handle
529 must support a protocol interface that supplies
530 an I/O abstraction to the driver.
531 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
532 parameter is ignored by device drivers, and is optional for bus
533 drivers. For a bus driver, if this parameter is NULL, then handles
534 for all the children of Controller are created by this driver.
535 If this parameter is not NULL and the first Device Path Node is
536 not the End of Device Path Node, then only the handle for the
537 child device specified by the first Device Path Node of
538 RemainingDevicePath is created by this driver.
539 If the first Device Path Node of RemainingDevicePath is
540 the End of Device Path Node, no child handle is created by this
541 driver.
542
543 @retval EFI_SUCCESS The device was started.
544 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
545 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
546 @retval Others The driver failded to start the device.
547
548 **/
549 EFI_STATUS
550 EFIAPI
551 UfsHcDriverBindingStart (
552 IN EFI_DRIVER_BINDING_PROTOCOL *This,
553 IN EFI_HANDLE Controller,
554 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
555 )
556 {
557 EFI_STATUS Status;
558 EFI_PCI_IO_PROTOCOL *PciIo;
559 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
560 UINT64 Supports;
561 UINT8 BarIndex;
562 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *BarDesc;
563
564 PciIo = NULL;
565 Private = NULL;
566 Supports = 0;
567 BarDesc = NULL;
568
569 //
570 // Now test and open the EfiPciIoProtocol
571 //
572 Status = gBS->OpenProtocol (
573 Controller,
574 &gEfiPciIoProtocolGuid,
575 (VOID **)&PciIo,
576 This->DriverBindingHandle,
577 Controller,
578 EFI_OPEN_PROTOCOL_BY_DRIVER
579 );
580 //
581 // Status == 0 - A normal execution flow, SUCCESS and the program proceeds.
582 // Status == ALREADY_STARTED - A non-zero Status code returned. It indicates
583 // that the protocol has been opened and should be treated as a
584 // normal condition and the program proceeds. The Protocol will not
585 // opened 'again' by this call.
586 // Status != ALREADY_STARTED - Error status, terminate program execution
587 //
588 if (EFI_ERROR (Status)) {
589 //
590 // EFI_ALREADY_STARTED is also an error
591 //
592 return Status;
593 }
594
595 Private = AllocateCopyPool (sizeof (UFS_HOST_CONTROLLER_PRIVATE_DATA), &gUfsHcTemplate);
596 if (Private == NULL) {
597 Status = EFI_OUT_OF_RESOURCES;
598 goto Done;
599 }
600
601 Private->PciIo = PciIo;
602
603 for (BarIndex = 0; BarIndex < PCI_MAX_BAR; BarIndex++) {
604 Status = PciIo->GetBarAttributes (
605 PciIo,
606 BarIndex,
607 NULL,
608 (VOID **)&BarDesc
609 );
610 if (Status == EFI_UNSUPPORTED) {
611 continue;
612 } else if (EFI_ERROR (Status)) {
613 goto Done;
614 }
615
616 if (BarDesc->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) {
617 Private->BarIndex = BarIndex;
618 FreePool (BarDesc);
619 break;
620 }
621
622 FreePool (BarDesc);
623 }
624
625 Status = PciIo->Attributes (
626 PciIo,
627 EfiPciIoAttributeOperationGet,
628 0,
629 &Private->PciAttributes
630 );
631
632 if (EFI_ERROR (Status)) {
633 goto Done;
634 }
635
636 Status = PciIo->Attributes (
637 PciIo,
638 EfiPciIoAttributeOperationSupported,
639 0,
640 &Supports
641 );
642
643 if (!EFI_ERROR (Status)) {
644 Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;
645 Status = PciIo->Attributes (
646 PciIo,
647 EfiPciIoAttributeOperationEnable,
648 Supports,
649 NULL
650 );
651 } else {
652 goto Done;
653 }
654
655 ///
656 /// Install UFS_HOST_CONTROLLER protocol
657 ///
658 Status = gBS->InstallProtocolInterface (
659 &Controller,
660 &gEdkiiUfsHostControllerProtocolGuid,
661 EFI_NATIVE_INTERFACE,
662 (VOID *)&(Private->UfsHc)
663 );
664
665 Done:
666 if (EFI_ERROR (Status)) {
667 if ((Private != NULL) && (Private->PciAttributes != 0)) {
668 //
669 // Restore original PCI attributes
670 //
671 PciIo->Attributes (
672 PciIo,
673 EfiPciIoAttributeOperationSet,
674 Private->PciAttributes,
675 NULL
676 );
677 }
678
679 gBS->CloseProtocol (
680 Controller,
681 &gEfiPciIoProtocolGuid,
682 This->DriverBindingHandle,
683 Controller
684 );
685 if (Private != NULL) {
686 FreePool (Private);
687 }
688 }
689
690 return Status;
691 }
692
693 /**
694 Stops a device controller or a bus controller.
695
696 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
697 As a result, much of the error checking on the parameters to Stop() has been moved
698 into this common boot service. It is legal to call Stop() from other locations,
699 but the following calling restrictions must be followed or the system behavior will not be deterministic.
700 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
701 same driver's Start() function.
702 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
703 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
704 Start() function, and the Start() function must have called OpenProtocol() on
705 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
706
707 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
708 @param[in] ControllerHandle A handle to the device being stopped. The handle must
709 support a bus specific I/O protocol for the driver
710 to use to stop the device.
711 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
712 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
713 if NumberOfChildren is 0.
714
715 @retval EFI_SUCCESS The device was stopped.
716 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
717
718 **/
719 EFI_STATUS
720 EFIAPI
721 UfsHcDriverBindingStop (
722 IN EFI_DRIVER_BINDING_PROTOCOL *This,
723 IN EFI_HANDLE Controller,
724 IN UINTN NumberOfChildren,
725 IN EFI_HANDLE *ChildHandleBuffer
726 )
727 {
728 EFI_STATUS Status;
729 UFS_HOST_CONTROLLER_PRIVATE_DATA *Private;
730 EDKII_UFS_HOST_CONTROLLER_PROTOCOL *UfsHc;
731
732 ///
733 /// Get private data
734 ///
735 Status = gBS->OpenProtocol (
736 Controller,
737 &gEdkiiUfsHostControllerProtocolGuid,
738 (VOID **)&UfsHc,
739 This->DriverBindingHandle,
740 Controller,
741 EFI_OPEN_PROTOCOL_GET_PROTOCOL
742 );
743
744 if (EFI_ERROR (Status)) {
745 return EFI_DEVICE_ERROR;
746 }
747
748 Private = UFS_HOST_CONTROLLER_PRIVATE_DATA_FROM_UFSHC (UfsHc);
749
750 Status = gBS->UninstallProtocolInterface (
751 Controller,
752 &gEdkiiUfsHostControllerProtocolGuid,
753 &(Private->UfsHc)
754 );
755 if (!EFI_ERROR (Status)) {
756 //
757 // Restore original PCI attributes
758 //
759 Status = Private->PciIo->Attributes (
760 Private->PciIo,
761 EfiPciIoAttributeOperationSet,
762 Private->PciAttributes,
763 NULL
764 );
765 ASSERT_EFI_ERROR (Status);
766
767 //
768 // Close protocols opened by UFS host controller driver
769 //
770 gBS->CloseProtocol (
771 Controller,
772 &gEfiPciIoProtocolGuid,
773 This->DriverBindingHandle,
774 Controller
775 );
776
777 FreePool (Private);
778 }
779
780 return Status;
781 }
782
783 /**
784 The entry point for UFS host controller driver, used to install this driver on the ImageHandle.
785
786 @param[in] ImageHandle The firmware allocated handle for this driver image.
787 @param[in] SystemTable Pointer to the EFI system table.
788
789 @retval EFI_SUCCESS Driver loaded.
790 @retval other Driver not loaded.
791
792 **/
793 EFI_STATUS
794 EFIAPI
795 UfsHcDriverEntry (
796 IN EFI_HANDLE ImageHandle,
797 IN EFI_SYSTEM_TABLE *SystemTable
798 )
799 {
800 EFI_STATUS Status;
801
802 Status = EfiLibInstallDriverBindingComponentName2 (
803 ImageHandle,
804 SystemTable,
805 &gUfsHcDriverBinding,
806 ImageHandle,
807 &gUfsHcComponentName,
808 &gUfsHcComponentName2
809 );
810 ASSERT_EFI_ERROR (Status);
811
812 return Status;
813 }