]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/Acpi/AcpiTableDxe/AcpiTableProtocol.c
MdeModulePkg AcpiTableDxe: Non-Boolean comparisons should use a compare operator.
[mirror_edk2.git] / MdeModulePkg / Universal / Acpi / AcpiTableDxe / AcpiTableProtocol.c
1 /** @file
2 ACPI Table Protocol Implementation
3
4 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 //
16 // Includes
17 //
18 #include "AcpiTable.h"
19 //
20 // The maximum number of tables that pre-allocated.
21 //
22 UINTN mEfiAcpiMaxNumTables = EFI_ACPI_MAX_NUM_TABLES;
23
24 /**
25 This function adds an ACPI table to the table list. It will detect FACS and
26 allocate the correct type of memory and properly align the table.
27
28 @param AcpiTableInstance Instance of the protocol.
29 @param Table Table to add.
30 @param Checksum Does the table require checksumming.
31 @param Version The version of the list to add the table to.
32 @param Handle Pointer for returning the handle.
33
34 @return EFI_SUCCESS The function completed successfully.
35 @return EFI_OUT_OF_RESOURCES Could not allocate a required resource.
36 @return EFI_ABORTED The table is a duplicate of a table that is required
37 to be unique.
38
39 **/
40 EFI_STATUS
41 AddTableToList (
42 IN EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance,
43 IN VOID *Table,
44 IN BOOLEAN Checksum,
45 IN EFI_ACPI_TABLE_VERSION Version,
46 OUT UINTN *Handle
47 );
48
49 /**
50 This function finds and removes the table specified by the handle.
51
52 @param AcpiTableInstance Instance of the protocol.
53 @param Version Bitmask of which versions to remove.
54 @param Handle Table to remove.
55
56 @return EFI_SUCCESS The function completed successfully.
57 @return EFI_ABORTED An error occurred.
58 @return EFI_NOT_FOUND Handle not found in table list.
59
60 **/
61 EFI_STATUS
62 RemoveTableFromList (
63 IN EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance,
64 IN EFI_ACPI_TABLE_VERSION Version,
65 IN UINTN Handle
66 );
67
68 /**
69 This function calculates and updates an UINT8 checksum.
70
71 @param Buffer Pointer to buffer to checksum
72 @param Size Number of bytes to checksum
73 @param ChecksumOffset Offset to place the checksum result in
74
75 @return EFI_SUCCESS The function completed successfully.
76 **/
77 EFI_STATUS
78 AcpiPlatformChecksum (
79 IN VOID *Buffer,
80 IN UINTN Size,
81 IN UINTN ChecksumOffset
82 );
83
84 /**
85 Checksum all versions of the common tables, RSDP, RSDT, XSDT.
86
87 @param AcpiTableInstance Protocol instance private data.
88
89 @return EFI_SUCCESS The function completed successfully.
90
91 **/
92 EFI_STATUS
93 ChecksumCommonTables (
94 IN OUT EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance
95 );
96
97 //
98 // Protocol function implementations.
99 //
100
101 /**
102 This function publishes the specified versions of the ACPI tables by
103 installing EFI configuration table entries for them. Any combination of
104 table versions can be published.
105
106 @param AcpiTableInstance Instance of the protocol.
107 @param Version Version(s) to publish.
108
109 @return EFI_SUCCESS The function completed successfully.
110 @return EFI_ABORTED The function could not complete successfully.
111
112 **/
113 EFI_STATUS
114 EFIAPI
115 PublishTables (
116 IN EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance,
117 IN EFI_ACPI_TABLE_VERSION Version
118 )
119 {
120 EFI_STATUS Status;
121 UINT32 *CurrentRsdtEntry;
122 VOID *CurrentXsdtEntry;
123 UINT64 Buffer64;
124
125 //
126 // Reorder tables as some operating systems don't seem to find the
127 // FADT correctly if it is not in the first few entries
128 //
129
130 //
131 // Add FADT as the first entry
132 //
133 if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
134 CurrentRsdtEntry = (UINT32 *) ((UINT8 *) AcpiTableInstance->Rsdt1 + sizeof (EFI_ACPI_DESCRIPTION_HEADER));
135 *CurrentRsdtEntry = (UINT32) (UINTN) AcpiTableInstance->Fadt1;
136 }
137 if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
138 CurrentRsdtEntry = (UINT32 *) ((UINT8 *) AcpiTableInstance->Rsdt3 + sizeof (EFI_ACPI_DESCRIPTION_HEADER));
139 *CurrentRsdtEntry = (UINT32) (UINTN) AcpiTableInstance->Fadt3;
140 CurrentXsdtEntry = (VOID *) ((UINT8 *) AcpiTableInstance->Xsdt + sizeof (EFI_ACPI_DESCRIPTION_HEADER));
141 //
142 // Add entry to XSDT, XSDT expects 64 bit pointers, but
143 // the table pointers in XSDT are not aligned on 8 byte boundary.
144 //
145 Buffer64 = (UINT64) (UINTN) AcpiTableInstance->Fadt3;
146 CopyMem (
147 CurrentXsdtEntry,
148 &Buffer64,
149 sizeof (UINT64)
150 );
151 }
152
153 //
154 // Do checksum again because Dsdt/Xsdt is updated.
155 //
156 ChecksumCommonTables (AcpiTableInstance);
157
158 //
159 // Add the RSD_PTR to the system table and store that we have installed the
160 // tables.
161 //
162 if (((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) &&
163 !AcpiTableInstance->TablesInstalled1) {
164 Status = gBS->InstallConfigurationTable (&gEfiAcpi10TableGuid, AcpiTableInstance->Rsdp1);
165 if (EFI_ERROR (Status)) {
166 return EFI_ABORTED;
167 }
168
169 AcpiTableInstance->TablesInstalled1 = TRUE;
170 }
171
172 if (((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) &&
173 !AcpiTableInstance->TablesInstalled3) {
174 Status = gBS->InstallConfigurationTable (&gEfiAcpiTableGuid, AcpiTableInstance->Rsdp3);
175 if (EFI_ERROR (Status)) {
176 return EFI_ABORTED;
177 }
178
179 AcpiTableInstance->TablesInstalled3= TRUE;
180 }
181
182 return EFI_SUCCESS;
183 }
184
185
186 /**
187 Installs an ACPI table into the RSDT/XSDT.
188 Note that the ACPI table should be checksumed before installing it.
189 Otherwise it will assert.
190
191 @param This Protocol instance pointer.
192 @param AcpiTableBuffer A pointer to a buffer containing the ACPI table to be installed.
193 @param AcpiTableBufferSize Specifies the size, in bytes, of the AcpiTableBuffer buffer.
194 @param TableKey Reurns a key to refer to the ACPI table.
195
196 @return EFI_SUCCESS The table was successfully inserted.
197 @return EFI_INVALID_PARAMETER Either AcpiTableBuffer is NULL, TableKey is NULL, or AcpiTableBufferSize
198 and the size field embedded in the ACPI table pointed to by AcpiTableBuffer
199 are not in sync.
200 @return EFI_OUT_OF_RESOURCES Insufficient resources exist to complete the request.
201 @retval EFI_ACCESS_DENIED The table signature matches a table already
202 present in the system and platform policy
203 does not allow duplicate tables of this type.
204
205 **/
206 EFI_STATUS
207 EFIAPI
208 InstallAcpiTable (
209 IN EFI_ACPI_TABLE_PROTOCOL *This,
210 IN VOID *AcpiTableBuffer,
211 IN UINTN AcpiTableBufferSize,
212 OUT UINTN *TableKey
213 )
214 {
215 EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance;
216 EFI_STATUS Status;
217 VOID *AcpiTableBufferConst;
218
219 //
220 // Check for invalid input parameters
221 //
222 if ((AcpiTableBuffer == NULL) || (TableKey == NULL)
223 || (((EFI_ACPI_DESCRIPTION_HEADER *) AcpiTableBuffer)->Length != AcpiTableBufferSize)) {
224 return EFI_INVALID_PARAMETER;
225 }
226
227 //
228 // Get the instance of the ACPI table protocol
229 //
230 AcpiTableInstance = EFI_ACPI_TABLE_INSTANCE_FROM_THIS (This);
231
232 //
233 // Install the ACPI table
234 //
235 AcpiTableBufferConst = AllocateCopyPool (AcpiTableBufferSize,AcpiTableBuffer);
236 *TableKey = 0;
237 Status = AddTableToList (
238 AcpiTableInstance,
239 AcpiTableBufferConst,
240 TRUE,
241 EFI_ACPI_TABLE_VERSION_1_0B | ACPI_TABLE_VERSION_GTE_2_0,
242 TableKey
243 );
244 if (!EFI_ERROR (Status)) {
245 Status = PublishTables (
246 AcpiTableInstance,
247 EFI_ACPI_TABLE_VERSION_1_0B | ACPI_TABLE_VERSION_GTE_2_0
248 );
249 }
250 FreePool (AcpiTableBufferConst);
251
252 //
253 // Add a new table successfully, notify registed callback
254 //
255 if (FeaturePcdGet (PcdInstallAcpiSdtProtocol)) {
256 if (!EFI_ERROR (Status)) {
257 SdtNotifyAcpiList (
258 AcpiTableInstance,
259 EFI_ACPI_TABLE_VERSION_1_0B | ACPI_TABLE_VERSION_GTE_2_0,
260 *TableKey
261 );
262 }
263 }
264
265 return Status;
266 }
267
268
269 /**
270 Removes an ACPI table from the RSDT/XSDT.
271
272 @param This Protocol instance pointer.
273 @param TableKey Specifies the table to uninstall. The key was returned from InstallAcpiTable().
274
275 @return EFI_SUCCESS The table was successfully uninstalled.
276 @return EFI_NOT_FOUND TableKey does not refer to a valid key for a table entry.
277
278 **/
279 EFI_STATUS
280 EFIAPI
281 UninstallAcpiTable (
282 IN EFI_ACPI_TABLE_PROTOCOL *This,
283 IN UINTN TableKey
284 )
285 {
286 EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance;
287 EFI_STATUS Status;
288
289 //
290 // Get the instance of the ACPI table protocol
291 //
292 AcpiTableInstance = EFI_ACPI_TABLE_INSTANCE_FROM_THIS (This);
293
294 //
295 // Uninstall the ACPI table
296 //
297 Status = RemoveTableFromList (
298 AcpiTableInstance,
299 EFI_ACPI_TABLE_VERSION_1_0B | ACPI_TABLE_VERSION_GTE_2_0,
300 TableKey
301 );
302 if (!EFI_ERROR (Status)) {
303 Status = PublishTables (
304 AcpiTableInstance,
305 EFI_ACPI_TABLE_VERSION_1_0B | ACPI_TABLE_VERSION_GTE_2_0
306 );
307 }
308
309 if (EFI_ERROR (Status)) {
310 return EFI_NOT_FOUND;
311 } else {
312 return EFI_SUCCESS;
313 }
314 }
315
316 /**
317 If the number of APCI tables exceeds the preallocated max table number, enlarge the table buffer.
318
319 @param AcpiTableInstance ACPI table protocol instance data structure.
320
321 @return EFI_SUCCESS reallocate the table beffer successfully.
322 @return EFI_OUT_OF_RESOURCES Unable to allocate required resources.
323
324 **/
325 EFI_STATUS
326 ReallocateAcpiTableBuffer (
327 IN EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance
328 )
329 {
330 UINTN NewMaxTableNumber;
331 UINTN TotalSize;
332 UINT8 *Pointer;
333 EFI_PHYSICAL_ADDRESS PageAddress;
334 EFI_ACPI_TABLE_INSTANCE TempPrivateData;
335 EFI_STATUS Status;
336 UINT64 CurrentData;
337
338 CopyMem (&TempPrivateData, AcpiTableInstance, sizeof (EFI_ACPI_TABLE_INSTANCE));
339 //
340 // Enlarge the max table number from mEfiAcpiMaxNumTables to mEfiAcpiMaxNumTables + EFI_ACPI_MAX_NUM_TABLES
341 //
342 NewMaxTableNumber = mEfiAcpiMaxNumTables + EFI_ACPI_MAX_NUM_TABLES;
343 //
344 // Create RSDT, XSDT structures and allocate buffers.
345 //
346 TotalSize = sizeof (EFI_ACPI_DESCRIPTION_HEADER) + // for ACPI 1.0 RSDT
347 NewMaxTableNumber * sizeof (UINT32) +
348 sizeof (EFI_ACPI_DESCRIPTION_HEADER) + // for ACPI 2.0/3.0 RSDT
349 NewMaxTableNumber * sizeof (UINT32) +
350 sizeof (EFI_ACPI_DESCRIPTION_HEADER) + // for ACPI 2.0/3.0 XSDT
351 NewMaxTableNumber * sizeof (UINT64);
352
353 //
354 // Allocate memory in the lower 32 bit of address range for
355 // compatibility with ACPI 1.0 OS.
356 //
357 // This is done because ACPI 1.0 pointers are 32 bit values.
358 // ACPI 2.0 OS and all 64 bit OS must use the 64 bit ACPI table addresses.
359 // There is no architectural reason these should be below 4GB, it is purely
360 // for convenience of implementation that we force memory below 4GB.
361 //
362 PageAddress = 0xFFFFFFFF;
363 Status = gBS->AllocatePages (
364 AllocateMaxAddress,
365 EfiACPIReclaimMemory,
366 EFI_SIZE_TO_PAGES (TotalSize),
367 &PageAddress
368 );
369
370 if (EFI_ERROR (Status)) {
371 return EFI_OUT_OF_RESOURCES;
372 }
373
374 Pointer = (UINT8 *) (UINTN) PageAddress;
375 ZeroMem (Pointer, TotalSize);
376
377 AcpiTableInstance->Rsdt1 = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
378 Pointer += (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + NewMaxTableNumber * sizeof (UINT32));
379 AcpiTableInstance->Rsdt3 = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
380 Pointer += (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + NewMaxTableNumber * sizeof (UINT32));
381 AcpiTableInstance->Xsdt = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
382
383 //
384 // Update RSDP to point to the new Rsdt and Xsdt address.
385 //
386 AcpiTableInstance->Rsdp1->RsdtAddress = (UINT32) (UINTN) AcpiTableInstance->Rsdt1;
387 AcpiTableInstance->Rsdp3->RsdtAddress = (UINT32) (UINTN) AcpiTableInstance->Rsdt3;
388 CurrentData = (UINT64) (UINTN) AcpiTableInstance->Xsdt;
389 CopyMem (&AcpiTableInstance->Rsdp3->XsdtAddress, &CurrentData, sizeof (UINT64));
390
391 //
392 // copy the original Rsdt1, Rsdt3 and Xsdt structure to new buffer
393 //
394 CopyMem (AcpiTableInstance->Rsdt1, TempPrivateData.Rsdt1, (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + mEfiAcpiMaxNumTables * sizeof (UINT32)));
395 CopyMem (AcpiTableInstance->Rsdt3, TempPrivateData.Rsdt3, (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + mEfiAcpiMaxNumTables * sizeof (UINT32)));
396 CopyMem (AcpiTableInstance->Xsdt, TempPrivateData.Xsdt, (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + mEfiAcpiMaxNumTables * sizeof (UINT64)));
397
398 //
399 // Calculate orignal ACPI table buffer size
400 //
401 TotalSize = sizeof (EFI_ACPI_DESCRIPTION_HEADER) + // for ACPI 1.0 RSDT
402 mEfiAcpiMaxNumTables * sizeof (UINT32) +
403 sizeof (EFI_ACPI_DESCRIPTION_HEADER) + // for ACPI 2.0/3.0 RSDT
404 mEfiAcpiMaxNumTables * sizeof (UINT32) +
405 sizeof (EFI_ACPI_DESCRIPTION_HEADER) + // for ACPI 2.0/3.0 XSDT
406 mEfiAcpiMaxNumTables * sizeof (UINT64);
407 gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)TempPrivateData.Rsdt1, EFI_SIZE_TO_PAGES (TotalSize));
408
409 //
410 // Update the Max ACPI table number
411 //
412 mEfiAcpiMaxNumTables = NewMaxTableNumber;
413 return EFI_SUCCESS;
414 }
415 /**
416 This function adds an ACPI table to the table list. It will detect FACS and
417 allocate the correct type of memory and properly align the table.
418
419 @param AcpiTableInstance Instance of the protocol.
420 @param Table Table to add.
421 @param Checksum Does the table require checksumming.
422 @param Version The version of the list to add the table to.
423 @param Handle Pointer for returning the handle.
424
425 @return EFI_SUCCESS The function completed successfully.
426 @return EFI_OUT_OF_RESOURCES Could not allocate a required resource.
427 @retval EFI_ACCESS_DENIED The table signature matches a table already
428 present in the system and platform policy
429 does not allow duplicate tables of this type.
430
431 **/
432 EFI_STATUS
433 AddTableToList (
434 IN EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance,
435 IN VOID *Table,
436 IN BOOLEAN Checksum,
437 IN EFI_ACPI_TABLE_VERSION Version,
438 OUT UINTN *Handle
439 )
440 {
441 EFI_STATUS Status;
442 EFI_ACPI_TABLE_LIST *CurrentTableList;
443 UINT32 CurrentTableSignature;
444 UINT32 CurrentTableSize;
445 UINT32 *CurrentRsdtEntry;
446 VOID *CurrentXsdtEntry;
447 UINT64 Buffer64;
448 BOOLEAN AddToRsdt;
449
450 //
451 // Check for invalid input parameters
452 //
453 ASSERT (AcpiTableInstance);
454 ASSERT (Table);
455 ASSERT (Handle);
456
457 //
458 // Init locals
459 //
460 AddToRsdt = TRUE;
461
462 //
463 // Create a new list entry
464 //
465 CurrentTableList = AllocatePool (sizeof (EFI_ACPI_TABLE_LIST));
466 ASSERT (CurrentTableList);
467
468 //
469 // Determine table type and size
470 //
471 CurrentTableSignature = ((EFI_ACPI_COMMON_HEADER *) Table)->Signature;
472 CurrentTableSize = ((EFI_ACPI_COMMON_HEADER *) Table)->Length;
473
474 //
475 // Allocate a buffer for the table. All tables are allocated in the lower 32 bits of address space
476 // for backwards compatibility with ACPI 1.0 OS.
477 //
478 // This is done because ACPI 1.0 pointers are 32 bit values.
479 // ACPI 2.0 OS and all 64 bit OS must use the 64 bit ACPI table addresses.
480 // There is no architectural reason these should be below 4GB, it is purely
481 // for convenience of implementation that we force memory below 4GB.
482 //
483 CurrentTableList->PageAddress = 0xFFFFFFFF;
484 CurrentTableList->NumberOfPages = EFI_SIZE_TO_PAGES (CurrentTableSize);
485
486 //
487 // Allocation memory type depends on the type of the table
488 //
489 if ((CurrentTableSignature == EFI_ACPI_2_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) ||
490 (CurrentTableSignature == EFI_ACPI_4_0_UEFI_ACPI_DATA_TABLE_SIGNATURE)) {
491 //
492 // Allocate memory for the FACS. This structure must be aligned
493 // on a 64 byte boundary and must be ACPI NVS memory.
494 // Using AllocatePages should ensure that it is always aligned.
495 // Do not change signature for new ACPI version because they are same.
496 //
497 // UEFI table also need to be in ACPI NVS memory, because some data field
498 // could be updated by OS present agent. For example, BufferPtrAddress in
499 // SMM communication ACPI table.
500 //
501 ASSERT ((EFI_PAGE_SIZE % 64) == 0);
502 Status = gBS->AllocatePages (
503 AllocateMaxAddress,
504 EfiACPIMemoryNVS,
505 CurrentTableList->NumberOfPages,
506 &CurrentTableList->PageAddress
507 );
508 } else {
509 //
510 // All other tables are ACPI reclaim memory, no alignment requirements.
511 //
512 Status = gBS->AllocatePages (
513 AllocateMaxAddress,
514 EfiACPIReclaimMemory,
515 CurrentTableList->NumberOfPages,
516 &CurrentTableList->PageAddress
517 );
518 }
519 //
520 // Check return value from memory alloc.
521 //
522 if (EFI_ERROR (Status)) {
523 gBS->FreePool (CurrentTableList);
524 return EFI_OUT_OF_RESOURCES;
525 }
526 //
527 // Update the table pointer with the allocated memory start
528 //
529 CurrentTableList->Table = (EFI_ACPI_COMMON_HEADER *) (UINTN) CurrentTableList->PageAddress;
530
531 //
532 // Initialize the table contents
533 //
534 CurrentTableList->Signature = EFI_ACPI_TABLE_LIST_SIGNATURE;
535 CopyMem (CurrentTableList->Table, Table, CurrentTableSize);
536 CurrentTableList->Handle = AcpiTableInstance->CurrentHandle++;
537 *Handle = CurrentTableList->Handle;
538 CurrentTableList->Version = Version;
539
540 //
541 // Update internal pointers if this is a required table. If it is a required
542 // table and a table of that type already exists, return an error.
543 //
544 // Calculate the checksum if the table is not FACS.
545 //
546 switch (CurrentTableSignature) {
547
548 case EFI_ACPI_1_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE:
549 //
550 // We don't add the FADT in the standard way because some
551 // OS expect the FADT to be early in the table list.
552 // So we always add it as the first element in the list.
553 //
554 AddToRsdt = FALSE;
555
556 //
557 // Check that the table has not been previously added.
558 //
559 if (((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0 && AcpiTableInstance->Fadt1 != NULL) ||
560 ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0 && AcpiTableInstance->Fadt3 != NULL)
561 ) {
562 gBS->FreePages (CurrentTableList->PageAddress, CurrentTableList->NumberOfPages);
563 gBS->FreePool (CurrentTableList);
564 return EFI_ACCESS_DENIED;
565 }
566 //
567 // Add the table to the appropriate table version
568 //
569 if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
570 //
571 // Save a pointer to the table
572 //
573 AcpiTableInstance->Fadt1 = (EFI_ACPI_1_0_FIXED_ACPI_DESCRIPTION_TABLE *) CurrentTableList->Table;
574
575 //
576 // Update pointers in FADT. If tables don't exist this will put NULL pointers there.
577 //
578 AcpiTableInstance->Fadt1->FirmwareCtrl = (UINT32) (UINTN) AcpiTableInstance->Facs1;
579 AcpiTableInstance->Fadt1->Dsdt = (UINT32) (UINTN) AcpiTableInstance->Dsdt1;
580
581 //
582 // RSDP OEM information is updated to match the FADT OEM information
583 //
584 CopyMem (
585 &AcpiTableInstance->Rsdp1->OemId,
586 &AcpiTableInstance->Fadt1->Header.OemId,
587 6
588 );
589
590 //
591 // RSDT OEM information is updated to match the FADT OEM information.
592 //
593 CopyMem (
594 &AcpiTableInstance->Rsdt1->OemId,
595 &AcpiTableInstance->Fadt1->Header.OemId,
596 6
597 );
598
599 CopyMem (
600 &AcpiTableInstance->Rsdt1->OemTableId,
601 &AcpiTableInstance->Fadt1->Header.OemTableId,
602 sizeof (UINT64)
603 );
604 AcpiTableInstance->Rsdt1->OemRevision = AcpiTableInstance->Fadt1->Header.OemRevision;
605 }
606
607 if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
608 //
609 // Save a pointer to the table
610 //
611 AcpiTableInstance->Fadt3 = (EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE *) CurrentTableList->Table;
612
613 //
614 // Update pointers in FADT. If tables don't exist this will put NULL pointers there.
615 // Note: If the FIRMWARE_CTRL is non-zero, then X_FIRMWARE_CTRL must be zero, and
616 // vice-versa.
617 //
618 if ((UINT64)(UINTN)AcpiTableInstance->Facs3 < BASE_4GB) {
619 AcpiTableInstance->Fadt3->FirmwareCtrl = (UINT32) (UINTN) AcpiTableInstance->Facs3;
620 ZeroMem (&AcpiTableInstance->Fadt3->XFirmwareCtrl, sizeof (UINT64));
621 } else {
622 Buffer64 = (UINT64) (UINTN) AcpiTableInstance->Facs3;
623 CopyMem (
624 &AcpiTableInstance->Fadt3->XFirmwareCtrl,
625 &Buffer64,
626 sizeof (UINT64)
627 );
628 AcpiTableInstance->Fadt3->FirmwareCtrl = 0;
629 }
630 AcpiTableInstance->Fadt3->Dsdt = (UINT32) (UINTN) AcpiTableInstance->Dsdt3;
631 Buffer64 = (UINT64) (UINTN) AcpiTableInstance->Dsdt3;
632 CopyMem (
633 &AcpiTableInstance->Fadt3->XDsdt,
634 &Buffer64,
635 sizeof (UINT64)
636 );
637
638 //
639 // RSDP OEM information is updated to match the FADT OEM information
640 //
641 CopyMem (
642 &AcpiTableInstance->Rsdp3->OemId,
643 &AcpiTableInstance->Fadt3->Header.OemId,
644 6
645 );
646
647 //
648 // RSDT OEM information is updated to match FADT OEM information.
649 //
650 CopyMem (
651 &AcpiTableInstance->Rsdt3->OemId,
652 &AcpiTableInstance->Fadt3->Header.OemId,
653 6
654 );
655 CopyMem (
656 &AcpiTableInstance->Rsdt3->OemTableId,
657 &AcpiTableInstance->Fadt3->Header.OemTableId,
658 sizeof (UINT64)
659 );
660 AcpiTableInstance->Rsdt3->OemRevision = AcpiTableInstance->Fadt3->Header.OemRevision;
661
662 //
663 // XSDT OEM information is updated to match FADT OEM information.
664 //
665 CopyMem (
666 &AcpiTableInstance->Xsdt->OemId,
667 &AcpiTableInstance->Fadt3->Header.OemId,
668 6
669 );
670 CopyMem (
671 &AcpiTableInstance->Xsdt->OemTableId,
672 &AcpiTableInstance->Fadt3->Header.OemTableId,
673 sizeof (UINT64)
674 );
675 AcpiTableInstance->Xsdt->OemRevision = AcpiTableInstance->Fadt3->Header.OemRevision;
676 }
677 //
678 // Checksum the table
679 //
680 if (Checksum) {
681 AcpiPlatformChecksum (
682 CurrentTableList->Table,
683 CurrentTableList->Table->Length,
684 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
685 Checksum)
686 );
687 }
688 break;
689
690 case EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE:
691 //
692 // Check that the table has not been previously added.
693 //
694 if (((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0 && AcpiTableInstance->Facs1 != NULL) ||
695 ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0 && AcpiTableInstance->Facs3 != NULL)
696 ) {
697 gBS->FreePages (CurrentTableList->PageAddress, CurrentTableList->NumberOfPages);
698 gBS->FreePool (CurrentTableList);
699 return EFI_ACCESS_DENIED;
700 }
701 //
702 // FACS is referenced by FADT and is not part of RSDT
703 //
704 AddToRsdt = FALSE;
705
706 //
707 // Add the table to the appropriate table version
708 //
709 if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
710 //
711 // Save a pointer to the table
712 //
713 AcpiTableInstance->Facs1 = (EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *) CurrentTableList->Table;
714
715 //
716 // If FADT already exists, update table pointers.
717 //
718 if (AcpiTableInstance->Fadt1 != NULL) {
719 AcpiTableInstance->Fadt1->FirmwareCtrl = (UINT32) (UINTN) AcpiTableInstance->Facs1;
720
721 //
722 // Checksum FADT table
723 //
724 AcpiPlatformChecksum (
725 AcpiTableInstance->Fadt1,
726 AcpiTableInstance->Fadt1->Header.Length,
727 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
728 Checksum)
729 );
730 }
731 }
732
733 if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
734 //
735 // Save a pointer to the table
736 //
737 AcpiTableInstance->Facs3 = (EFI_ACPI_3_0_FIRMWARE_ACPI_CONTROL_STRUCTURE *) CurrentTableList->Table;
738
739 //
740 // If FADT already exists, update table pointers.
741 //
742 if (AcpiTableInstance->Fadt3 != NULL) {
743 //
744 // Note: If the FIRMWARE_CTRL is non-zero, then X_FIRMWARE_CTRL must be zero, and
745 // vice-versa.
746 //
747 if ((UINT64)(UINTN)AcpiTableInstance->Facs3 < BASE_4GB) {
748 AcpiTableInstance->Fadt3->FirmwareCtrl = (UINT32) (UINTN) AcpiTableInstance->Facs3;
749 ZeroMem (&AcpiTableInstance->Fadt3->XFirmwareCtrl, sizeof (UINT64));
750 } else {
751 Buffer64 = (UINT64) (UINTN) AcpiTableInstance->Facs3;
752 CopyMem (
753 &AcpiTableInstance->Fadt3->XFirmwareCtrl,
754 &Buffer64,
755 sizeof (UINT64)
756 );
757 AcpiTableInstance->Fadt3->FirmwareCtrl = 0;
758 }
759
760 //
761 // Checksum FADT table
762 //
763 AcpiPlatformChecksum (
764 AcpiTableInstance->Fadt3,
765 AcpiTableInstance->Fadt3->Header.Length,
766 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
767 Checksum)
768 );
769 }
770 }
771
772 break;
773
774 case EFI_ACPI_1_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE:
775 //
776 // Check that the table has not been previously added.
777 //
778 if (((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0 && AcpiTableInstance->Dsdt1 != NULL) ||
779 ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0 && AcpiTableInstance->Dsdt3 != NULL)
780 ) {
781 gBS->FreePages (CurrentTableList->PageAddress, CurrentTableList->NumberOfPages);
782 gBS->FreePool (CurrentTableList);
783 return EFI_ACCESS_DENIED;
784 }
785 //
786 // DSDT is referenced by FADT and is not part of RSDT
787 //
788 AddToRsdt = FALSE;
789
790 //
791 // Add the table to the appropriate table version
792 //
793 if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
794 //
795 // Save a pointer to the table
796 //
797 AcpiTableInstance->Dsdt1 = (EFI_ACPI_DESCRIPTION_HEADER *) CurrentTableList->Table;
798
799 //
800 // If FADT already exists, update table pointers.
801 //
802 if (AcpiTableInstance->Fadt1 != NULL) {
803 AcpiTableInstance->Fadt1->Dsdt = (UINT32) (UINTN) AcpiTableInstance->Dsdt1;
804
805 //
806 // Checksum FADT table
807 //
808 AcpiPlatformChecksum (
809 AcpiTableInstance->Fadt1,
810 AcpiTableInstance->Fadt1->Header.Length,
811 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
812 Checksum)
813 );
814 }
815 }
816
817 if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
818 //
819 // Save a pointer to the table
820 //
821 AcpiTableInstance->Dsdt3 = (EFI_ACPI_DESCRIPTION_HEADER *) CurrentTableList->Table;
822
823 //
824 // If FADT already exists, update table pointers.
825 //
826 if (AcpiTableInstance->Fadt3 != NULL) {
827 AcpiTableInstance->Fadt3->Dsdt = (UINT32) (UINTN) AcpiTableInstance->Dsdt3;
828 Buffer64 = (UINT64) (UINTN) AcpiTableInstance->Dsdt3;
829 CopyMem (
830 &AcpiTableInstance->Fadt3->XDsdt,
831 &Buffer64,
832 sizeof (UINT64)
833 );
834
835 //
836 // Checksum FADT table
837 //
838 AcpiPlatformChecksum (
839 AcpiTableInstance->Fadt3,
840 AcpiTableInstance->Fadt3->Header.Length,
841 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
842 Checksum)
843 );
844 }
845 }
846 //
847 // Checksum the table
848 //
849 if (Checksum) {
850 AcpiPlatformChecksum (
851 CurrentTableList->Table,
852 CurrentTableList->Table->Length,
853 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
854 Checksum)
855 );
856 }
857 break;
858
859 default:
860 //
861 // Checksum the table
862 //
863 if (Checksum) {
864 AcpiPlatformChecksum (
865 CurrentTableList->Table,
866 CurrentTableList->Table->Length,
867 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
868 Checksum)
869 );
870 }
871 break;
872 }
873 //
874 // Add the table to the current list of tables
875 //
876 InsertTailList (&AcpiTableInstance->TableList, &CurrentTableList->Link);
877
878 //
879 // Add the table to RSDT and/or XSDT table entry lists.
880 //
881 //
882 // Add to ACPI 1.0b table tree
883 //
884 if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
885 if (AddToRsdt) {
886 //
887 // If the table number exceed the gEfiAcpiMaxNumTables, enlarge the table buffer
888 //
889 if (AcpiTableInstance->NumberOfTableEntries1 >= mEfiAcpiMaxNumTables) {
890 Status = ReallocateAcpiTableBuffer (AcpiTableInstance);
891 ASSERT_EFI_ERROR (Status);
892 }
893 CurrentRsdtEntry = (UINT32 *)
894 (
895 (UINT8 *) AcpiTableInstance->Rsdt1 +
896 sizeof (EFI_ACPI_DESCRIPTION_HEADER) +
897 AcpiTableInstance->NumberOfTableEntries1 *
898 sizeof (UINT32)
899 );
900
901 //
902 // Add entry to the RSDT unless its the FACS or DSDT
903 //
904 *CurrentRsdtEntry = (UINT32) (UINTN) CurrentTableList->Table;
905
906 //
907 // Update RSDT length
908 //
909 AcpiTableInstance->Rsdt1->Length = AcpiTableInstance->Rsdt1->Length + sizeof (UINT32);
910
911 AcpiTableInstance->NumberOfTableEntries1++;
912 }
913 }
914 //
915 // Add to ACPI 2.0/3.0 table tree
916 //
917 if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
918 if (AddToRsdt) {
919 //
920 // If the table number exceed the gEfiAcpiMaxNumTables, enlarge the table buffer
921 //
922 if (AcpiTableInstance->NumberOfTableEntries3 >= mEfiAcpiMaxNumTables) {
923 Status = ReallocateAcpiTableBuffer (AcpiTableInstance);
924 ASSERT_EFI_ERROR (Status);
925 }
926 //
927 // At this time, it is assumed that RSDT and XSDT maintain parallel lists of tables.
928 // If it becomes necessary to maintain separate table lists, changes will be required.
929 //
930 CurrentRsdtEntry = (UINT32 *)
931 (
932 (UINT8 *) AcpiTableInstance->Rsdt3 +
933 sizeof (EFI_ACPI_DESCRIPTION_HEADER) +
934 AcpiTableInstance->NumberOfTableEntries3 *
935 sizeof (UINT32)
936 );
937
938 //
939 // This pointer must not be directly dereferenced as the XSDT entries may not
940 // be 64 bit aligned resulting in a possible fault. Use CopyMem to update.
941 //
942 CurrentXsdtEntry = (VOID *)
943 (
944 (UINT8 *) AcpiTableInstance->Xsdt +
945 sizeof (EFI_ACPI_DESCRIPTION_HEADER) +
946 AcpiTableInstance->NumberOfTableEntries3 *
947 sizeof (UINT64)
948 );
949
950 //
951 // Add entry to the RSDT
952 //
953 *CurrentRsdtEntry = (UINT32) (UINTN) CurrentTableList->Table;
954
955 //
956 // Update RSDT length
957 //
958 AcpiTableInstance->Rsdt3->Length = AcpiTableInstance->Rsdt3->Length + sizeof (UINT32);
959
960 //
961 // Add entry to XSDT, XSDT expects 64 bit pointers, but
962 // the table pointers in XSDT are not aligned on 8 byte boundary.
963 //
964 Buffer64 = (UINT64) (UINTN) CurrentTableList->Table;
965 CopyMem (
966 CurrentXsdtEntry,
967 &Buffer64,
968 sizeof (UINT64)
969 );
970
971 //
972 // Update length
973 //
974 AcpiTableInstance->Xsdt->Length = AcpiTableInstance->Xsdt->Length + sizeof (UINT64);
975
976 AcpiTableInstance->NumberOfTableEntries3++;
977 }
978 }
979
980 ChecksumCommonTables (AcpiTableInstance);
981 return EFI_SUCCESS;
982 }
983
984
985 /**
986 This function finds the table specified by the handle and returns a pointer to it.
987 If the handle is not found, EFI_NOT_FOUND is returned and the contents of Table are
988 undefined.
989
990 @param Handle Table to find.
991 @param TableList Table list to search
992 @param Table Pointer to table found.
993
994 @return EFI_SUCCESS The function completed successfully.
995 @return EFI_NOT_FOUND No table found matching the handle specified.
996
997 **/
998 EFI_STATUS
999 FindTableByHandle (
1000 IN UINTN Handle,
1001 IN LIST_ENTRY *TableList,
1002 OUT EFI_ACPI_TABLE_LIST **Table
1003 )
1004 {
1005 LIST_ENTRY *CurrentLink;
1006 EFI_ACPI_TABLE_LIST *CurrentTable;
1007
1008 //
1009 // Check for invalid input parameters
1010 //
1011 ASSERT (Table);
1012
1013 //
1014 // Find the table
1015 //
1016 CurrentLink = TableList->ForwardLink;
1017
1018 while (CurrentLink != TableList) {
1019 CurrentTable = EFI_ACPI_TABLE_LIST_FROM_LINK (CurrentLink);
1020 if (CurrentTable->Handle == Handle) {
1021 //
1022 // Found handle, so return this table.
1023 //
1024 *Table = CurrentTable;
1025 return EFI_SUCCESS;
1026 }
1027
1028 CurrentLink = CurrentLink->ForwardLink;
1029 }
1030 //
1031 // Table not found
1032 //
1033 return EFI_NOT_FOUND;
1034 }
1035
1036
1037 /**
1038 This function removes a basic table from the RSDT and/or XSDT.
1039 For Acpi 1.0 tables, pass in the Rsdt.
1040 For Acpi 2.0 tables, pass in both Rsdt and Xsdt.
1041
1042 @param Table Pointer to table found.
1043 @param NumberOfTableEntries Current number of table entries in the RSDT/XSDT
1044 @param Rsdt Pointer to the RSDT to remove from
1045 @param Xsdt Pointer to the Xsdt to remove from
1046
1047 @return EFI_SUCCESS The function completed successfully.
1048 @return EFI_INVALID_PARAMETER The table was not found in both Rsdt and Xsdt.
1049
1050 **/
1051 EFI_STATUS
1052 RemoveTableFromRsdt (
1053 IN OUT EFI_ACPI_TABLE_LIST * Table,
1054 IN OUT UINTN *NumberOfTableEntries,
1055 IN OUT EFI_ACPI_DESCRIPTION_HEADER * Rsdt,
1056 IN OUT EFI_ACPI_DESCRIPTION_HEADER * Xsdt OPTIONAL
1057 )
1058 {
1059 UINT32 *CurrentRsdtEntry;
1060 VOID *CurrentXsdtEntry;
1061 UINT64 CurrentTablePointer64;
1062 UINTN Index;
1063
1064 //
1065 // Check for invalid input parameters
1066 //
1067 ASSERT (Table);
1068 ASSERT (NumberOfTableEntries);
1069 ASSERT (Rsdt);
1070
1071 //
1072 // Find the table entry in the RSDT and XSDT
1073 //
1074 for (Index = 0; Index < *NumberOfTableEntries; Index++) {
1075 //
1076 // At this time, it is assumed that RSDT and XSDT maintain parallel lists of tables.
1077 // If it becomes necessary to maintain separate table lists, changes will be required.
1078 //
1079 CurrentRsdtEntry = (UINT32 *) ((UINT8 *) Rsdt + sizeof (EFI_ACPI_DESCRIPTION_HEADER) + Index * sizeof (UINT32));
1080 if (Xsdt != NULL) {
1081 //
1082 // This pointer must not be directly dereferenced as the XSDT entries may not
1083 // be 64 bit aligned resulting in a possible fault. Use CopyMem to update.
1084 //
1085 CurrentXsdtEntry = (VOID *) ((UINT8 *) Xsdt + sizeof (EFI_ACPI_DESCRIPTION_HEADER) + Index * sizeof (UINT64));
1086
1087 //
1088 // Read the entry value out of the XSDT
1089 //
1090 CopyMem (&CurrentTablePointer64, CurrentXsdtEntry, sizeof (UINT64));
1091 } else {
1092 //
1093 // Initialize to NULL
1094 //
1095 CurrentXsdtEntry = 0;
1096 CurrentTablePointer64 = 0;
1097 }
1098 //
1099 // Check if we have found the corresponding entry in both RSDT and XSDT
1100 //
1101 if (*CurrentRsdtEntry == (UINT32) (UINTN) Table->Table &&
1102 ((Xsdt == NULL) || CurrentTablePointer64 == (UINT64) (UINTN) Table->Table)
1103 ) {
1104 //
1105 // Found entry, so copy all following entries and shrink table
1106 // We actually copy all + 1 to copy the initialized value of memory over
1107 // the last entry.
1108 //
1109 CopyMem (CurrentRsdtEntry, CurrentRsdtEntry + 1, (*NumberOfTableEntries - Index) * sizeof (UINT32));
1110 Rsdt->Length = Rsdt->Length - sizeof (UINT32);
1111 if (Xsdt != NULL) {
1112 CopyMem (CurrentXsdtEntry, ((UINT64 *) CurrentXsdtEntry) + 1, (*NumberOfTableEntries - Index) * sizeof (UINT64));
1113 Xsdt->Length = Xsdt->Length - sizeof (UINT64);
1114 }
1115 break;
1116 } else if (Index + 1 == *NumberOfTableEntries) {
1117 //
1118 // At the last entry, and table not found
1119 //
1120 return EFI_INVALID_PARAMETER;
1121 }
1122 }
1123 //
1124 // Checksum the tables
1125 //
1126 AcpiPlatformChecksum (
1127 Rsdt,
1128 Rsdt->Length,
1129 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1130 Checksum)
1131 );
1132
1133 if (Xsdt != NULL) {
1134 AcpiPlatformChecksum (
1135 Xsdt,
1136 Xsdt->Length,
1137 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1138 Checksum)
1139 );
1140 }
1141 //
1142 // Decrement the number of tables
1143 //
1144 (*NumberOfTableEntries)--;
1145
1146 return EFI_SUCCESS;
1147 }
1148
1149
1150 /**
1151 This function removes a table and frees any associated memory.
1152
1153 @param AcpiTableInstance Instance of the protocol.
1154 @param Version Version(s) to delete.
1155 @param Table Pointer to table found.
1156
1157 @return EFI_SUCCESS The function completed successfully.
1158
1159 **/
1160 EFI_STATUS
1161 DeleteTable (
1162 IN EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance,
1163 IN EFI_ACPI_TABLE_VERSION Version,
1164 IN OUT EFI_ACPI_TABLE_LIST *Table
1165 )
1166 {
1167 UINT32 CurrentTableSignature;
1168 BOOLEAN RemoveFromRsdt;
1169
1170 //
1171 // Check for invalid input parameters
1172 //
1173 ASSERT (AcpiTableInstance);
1174 ASSERT (Table);
1175
1176 //
1177 // Init locals
1178 //
1179 RemoveFromRsdt = TRUE;
1180 //
1181 // Check for Table->Table
1182 //
1183 ASSERT (Table->Table != NULL);
1184 CurrentTableSignature = ((EFI_ACPI_COMMON_HEADER *) Table->Table)->Signature;
1185
1186 //
1187 // Basic tasks to accomplish delete are:
1188 // Determine removal requirements (in RSDT/XSDT or not)
1189 // Remove entry from RSDT/XSDT
1190 // Remove any table references to the table
1191 // If no one is using the table
1192 // Free the table (removing pointers from private data and tables)
1193 // Remove from list
1194 // Free list structure
1195 //
1196 //
1197 // Determine if this table is in the RSDT or XSDT
1198 //
1199 if ((CurrentTableSignature == EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE) ||
1200 (CurrentTableSignature == EFI_ACPI_2_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE) ||
1201 (CurrentTableSignature == EFI_ACPI_3_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE)
1202 ) {
1203 RemoveFromRsdt = FALSE;
1204 }
1205 //
1206 // We don't remove the FADT in the standard way because some
1207 // OS expect the FADT to be early in the table list.
1208 // So we always put it as the first element in the list.
1209 //
1210 if (CurrentTableSignature == EFI_ACPI_1_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE) {
1211 RemoveFromRsdt = FALSE;
1212 }
1213
1214 //
1215 // Remove the table from RSDT and XSDT
1216 //
1217 if (Table->Table != NULL) {
1218 //
1219 // This is a basic table, remove it from any lists and the Rsdt and/or Xsdt
1220 //
1221 if (Version & EFI_ACPI_TABLE_VERSION_NONE & Table->Version) {
1222 //
1223 // Remove this version from the table
1224 //
1225 Table->Version = Table->Version &~EFI_ACPI_TABLE_VERSION_NONE;
1226 }
1227
1228 if (Version & EFI_ACPI_TABLE_VERSION_1_0B & Table->Version) {
1229 //
1230 // Remove this version from the table
1231 //
1232 Table->Version = Table->Version &~EFI_ACPI_TABLE_VERSION_1_0B;
1233
1234 //
1235 // Remove from Rsdt. We don't care about the return value because it is
1236 // acceptable for the table to not exist in Rsdt.
1237 // We didn't add some tables so we don't remove them.
1238 //
1239 if (RemoveFromRsdt) {
1240 RemoveTableFromRsdt (
1241 Table,
1242 &AcpiTableInstance->NumberOfTableEntries1,
1243 AcpiTableInstance->Rsdt1,
1244 NULL
1245 );
1246 }
1247 }
1248
1249 if (Version & ACPI_TABLE_VERSION_GTE_2_0 & Table->Version) {
1250 //
1251 // Remove this version from the table
1252 //
1253 Table->Version = Table->Version &~(Version & ACPI_TABLE_VERSION_GTE_2_0);
1254
1255 //
1256 // Remove from Rsdt and Xsdt. We don't care about the return value
1257 // because it is acceptable for the table to not exist in Rsdt/Xsdt.
1258 // We didn't add some tables so we don't remove them.
1259 //
1260 if (RemoveFromRsdt) {
1261 RemoveTableFromRsdt (
1262 Table,
1263 &AcpiTableInstance->NumberOfTableEntries3,
1264 AcpiTableInstance->Rsdt3,
1265 AcpiTableInstance->Xsdt
1266 );
1267 }
1268 }
1269 //
1270 // Free the table, clean up any dependent tables and our private data pointers.
1271 //
1272 switch (Table->Table->Signature) {
1273
1274 case EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE:
1275 if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1276 AcpiTableInstance->Fadt1 = NULL;
1277 }
1278
1279 if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
1280 AcpiTableInstance->Fadt3 = NULL;
1281 }
1282 break;
1283
1284 case EFI_ACPI_3_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE:
1285 if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1286 AcpiTableInstance->Facs1 = NULL;
1287
1288 //
1289 // Update FADT table pointers
1290 //
1291 if (AcpiTableInstance->Fadt1 != NULL) {
1292 AcpiTableInstance->Fadt1->FirmwareCtrl = 0;
1293
1294 //
1295 // Checksum table
1296 //
1297 AcpiPlatformChecksum (
1298 AcpiTableInstance->Fadt1,
1299 AcpiTableInstance->Fadt1->Header.Length,
1300 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1301 Checksum)
1302 );
1303 }
1304 }
1305
1306 if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
1307 AcpiTableInstance->Facs3 = NULL;
1308
1309 //
1310 // Update FADT table pointers
1311 //
1312 if (AcpiTableInstance->Fadt3 != NULL) {
1313 AcpiTableInstance->Fadt3->FirmwareCtrl = 0;
1314 ZeroMem (&AcpiTableInstance->Fadt3->XFirmwareCtrl, sizeof (UINT64));
1315
1316 //
1317 // Checksum table
1318 //
1319 AcpiPlatformChecksum (
1320 AcpiTableInstance->Fadt3,
1321 AcpiTableInstance->Fadt3->Header.Length,
1322 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1323 Checksum)
1324 );
1325 }
1326 }
1327 break;
1328
1329 case EFI_ACPI_3_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE:
1330 if ((Version & EFI_ACPI_TABLE_VERSION_1_0B) != 0) {
1331 AcpiTableInstance->Dsdt1 = NULL;
1332
1333 //
1334 // Update FADT table pointers
1335 //
1336 if (AcpiTableInstance->Fadt1 != NULL) {
1337 AcpiTableInstance->Fadt1->Dsdt = 0;
1338
1339 //
1340 // Checksum table
1341 //
1342 AcpiPlatformChecksum (
1343 AcpiTableInstance->Fadt1,
1344 AcpiTableInstance->Fadt1->Header.Length,
1345 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1346 Checksum)
1347 );
1348 }
1349 }
1350
1351
1352 if ((Version & ACPI_TABLE_VERSION_GTE_2_0) != 0) {
1353 AcpiTableInstance->Dsdt3 = NULL;
1354
1355 //
1356 // Update FADT table pointers
1357 //
1358 if (AcpiTableInstance->Fadt3 != NULL) {
1359 AcpiTableInstance->Fadt3->Dsdt = 0;
1360 ZeroMem (&AcpiTableInstance->Fadt3->XDsdt, sizeof (UINT64));
1361
1362 //
1363 // Checksum table
1364 //
1365 AcpiPlatformChecksum (
1366 AcpiTableInstance->Fadt3,
1367 AcpiTableInstance->Fadt3->Header.Length,
1368 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1369 Checksum)
1370 );
1371 }
1372 }
1373 break;
1374
1375 default:
1376 //
1377 // Do nothing
1378 //
1379 break;
1380 }
1381 }
1382 //
1383 // If no version is using this table anymore, remove and free list entry.
1384 //
1385 if (Table->Version == 0) {
1386 //
1387 // Free the Table
1388 //
1389 gBS->FreePages (Table->PageAddress, Table->NumberOfPages);
1390 RemoveEntryList (&(Table->Link));
1391 gBS->FreePool (Table);
1392 }
1393 //
1394 // Done
1395 //
1396 return EFI_SUCCESS;
1397 }
1398
1399
1400 /**
1401 This function finds and removes the table specified by the handle.
1402
1403 @param AcpiTableInstance Instance of the protocol.
1404 @param Version Bitmask of which versions to remove.
1405 @param Handle Table to remove.
1406
1407 @return EFI_SUCCESS The function completed successfully.
1408 @return EFI_ABORTED An error occurred.
1409 @return EFI_NOT_FOUND Handle not found in table list.
1410
1411 **/
1412 EFI_STATUS
1413 RemoveTableFromList (
1414 IN EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance,
1415 IN EFI_ACPI_TABLE_VERSION Version,
1416 IN UINTN Handle
1417 )
1418 {
1419 EFI_ACPI_TABLE_LIST *Table;
1420 EFI_STATUS Status;
1421
1422 Table = (EFI_ACPI_TABLE_LIST*) NULL;
1423
1424 //
1425 // Check for invalid input parameters
1426 //
1427 ASSERT (AcpiTableInstance);
1428
1429 //
1430 // Find the table
1431 //
1432 Status = FindTableByHandle (
1433 Handle,
1434 &AcpiTableInstance->TableList,
1435 &Table
1436 );
1437 if (EFI_ERROR (Status)) {
1438 return EFI_NOT_FOUND;
1439 }
1440 //
1441 // Remove the table
1442 //
1443 Status = DeleteTable (AcpiTableInstance, Version, Table);
1444 if (EFI_ERROR (Status)) {
1445 return EFI_ABORTED;
1446 }
1447 //
1448 // Completed successfully
1449 //
1450 return EFI_SUCCESS;
1451 }
1452
1453
1454 /**
1455 This function calculates and updates an UINT8 checksum.
1456
1457 @param Buffer Pointer to buffer to checksum
1458 @param Size Number of bytes to checksum
1459 @param ChecksumOffset Offset to place the checksum result in
1460
1461 @return EFI_SUCCESS The function completed successfully.
1462
1463 **/
1464 EFI_STATUS
1465 AcpiPlatformChecksum (
1466 IN VOID *Buffer,
1467 IN UINTN Size,
1468 IN UINTN ChecksumOffset
1469 )
1470 {
1471 UINT8 Sum;
1472 UINT8 *Ptr;
1473
1474 Sum = 0;
1475 //
1476 // Initialize pointer
1477 //
1478 Ptr = Buffer;
1479
1480 //
1481 // set checksum to 0 first
1482 //
1483 Ptr[ChecksumOffset] = 0;
1484
1485 //
1486 // add all content of buffer
1487 //
1488 while ((Size--) != 0) {
1489 Sum = (UINT8) (Sum + (*Ptr++));
1490 }
1491 //
1492 // set checksum
1493 //
1494 Ptr = Buffer;
1495 Ptr[ChecksumOffset] = (UINT8) (0xff - Sum + 1);
1496
1497 return EFI_SUCCESS;
1498 }
1499
1500
1501 /**
1502 Checksum all versions of the common tables, RSDP, RSDT, XSDT.
1503
1504 @param AcpiTableInstance Protocol instance private data.
1505
1506 @return EFI_SUCCESS The function completed successfully.
1507
1508 **/
1509 EFI_STATUS
1510 ChecksumCommonTables (
1511 IN OUT EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance
1512 )
1513 {
1514 //
1515 // RSDP ACPI 1.0 checksum for 1.0 table. This is only the first 20 bytes of the structure
1516 //
1517 AcpiPlatformChecksum (
1518 AcpiTableInstance->Rsdp1,
1519 sizeof (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER),
1520 OFFSET_OF (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER,
1521 Checksum)
1522 );
1523
1524 //
1525 // RSDP ACPI 1.0 checksum for 2.0/3.0 table. This is only the first 20 bytes of the structure
1526 //
1527 AcpiPlatformChecksum (
1528 AcpiTableInstance->Rsdp3,
1529 sizeof (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER),
1530 OFFSET_OF (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER,
1531 Checksum)
1532 );
1533
1534 //
1535 // RSDP ACPI 2.0/3.0 checksum, this is the entire table
1536 //
1537 AcpiPlatformChecksum (
1538 AcpiTableInstance->Rsdp3,
1539 sizeof (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER),
1540 OFFSET_OF (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER,
1541 ExtendedChecksum)
1542 );
1543
1544 //
1545 // RSDT checksums
1546 //
1547 AcpiPlatformChecksum (
1548 AcpiTableInstance->Rsdt1,
1549 AcpiTableInstance->Rsdt1->Length,
1550 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1551 Checksum)
1552 );
1553
1554 AcpiPlatformChecksum (
1555 AcpiTableInstance->Rsdt3,
1556 AcpiTableInstance->Rsdt3->Length,
1557 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1558 Checksum)
1559 );
1560
1561 //
1562 // XSDT checksum
1563 //
1564 AcpiPlatformChecksum (
1565 AcpiTableInstance->Xsdt,
1566 AcpiTableInstance->Xsdt->Length,
1567 OFFSET_OF (EFI_ACPI_DESCRIPTION_HEADER,
1568 Checksum)
1569 );
1570
1571 return EFI_SUCCESS;
1572 }
1573
1574
1575 /**
1576 Constructor for the ACPI table protocol. Initializes instance
1577 data.
1578
1579 @param AcpiTableInstance Instance to construct
1580
1581 @return EFI_SUCCESS Instance initialized.
1582 @return EFI_OUT_OF_RESOURCES Unable to allocate required resources.
1583
1584 **/
1585 EFI_STATUS
1586 AcpiTableAcpiTableConstructor (
1587 EFI_ACPI_TABLE_INSTANCE *AcpiTableInstance
1588 )
1589 {
1590 EFI_STATUS Status;
1591 UINT64 CurrentData;
1592 UINTN TotalSize;
1593 UINTN RsdpTableSize;
1594 UINT8 *Pointer;
1595 EFI_PHYSICAL_ADDRESS PageAddress;
1596
1597 //
1598 // Check for invalid input parameters
1599 //
1600 ASSERT (AcpiTableInstance);
1601
1602 InitializeListHead (&AcpiTableInstance->TableList);
1603 AcpiTableInstance->CurrentHandle = 1;
1604
1605 AcpiTableInstance->AcpiTableProtocol.InstallAcpiTable = InstallAcpiTable;
1606 AcpiTableInstance->AcpiTableProtocol.UninstallAcpiTable = UninstallAcpiTable;
1607
1608 if (FeaturePcdGet (PcdInstallAcpiSdtProtocol)) {
1609 SdtAcpiTableAcpiSdtConstructor (AcpiTableInstance);
1610 }
1611
1612 //
1613 // Create RSDP table
1614 //
1615 RsdpTableSize = sizeof (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER) +
1616 sizeof (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER);
1617
1618 PageAddress = 0xFFFFFFFF;
1619 Status = gBS->AllocatePages (
1620 AllocateMaxAddress,
1621 EfiACPIReclaimMemory,
1622 EFI_SIZE_TO_PAGES (RsdpTableSize),
1623 &PageAddress
1624 );
1625
1626 if (EFI_ERROR (Status)) {
1627 return EFI_OUT_OF_RESOURCES;
1628 }
1629
1630 Pointer = (UINT8 *) (UINTN) PageAddress;
1631 ZeroMem (Pointer, RsdpTableSize);
1632
1633 AcpiTableInstance->Rsdp1 = (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER *) Pointer;
1634 Pointer += sizeof (EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER);
1635 AcpiTableInstance->Rsdp3 = (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER *) Pointer;
1636
1637 //
1638 // Create RSDT, XSDT structures
1639 //
1640 TotalSize = sizeof (EFI_ACPI_DESCRIPTION_HEADER) + // for ACPI 1.0 RSDT
1641 mEfiAcpiMaxNumTables * sizeof (UINT32) +
1642 sizeof (EFI_ACPI_DESCRIPTION_HEADER) + // for ACPI 2.0/3.0 RSDT
1643 mEfiAcpiMaxNumTables * sizeof (UINT32) +
1644 sizeof (EFI_ACPI_DESCRIPTION_HEADER) + // for ACPI 2.0/3.0 XSDT
1645 mEfiAcpiMaxNumTables * sizeof (UINT64);
1646
1647 //
1648 // Allocate memory in the lower 32 bit of address range for
1649 // compatibility with ACPI 1.0 OS.
1650 //
1651 // This is done because ACPI 1.0 pointers are 32 bit values.
1652 // ACPI 2.0 OS and all 64 bit OS must use the 64 bit ACPI table addresses.
1653 // There is no architectural reason these should be below 4GB, it is purely
1654 // for convenience of implementation that we force memory below 4GB.
1655 //
1656 PageAddress = 0xFFFFFFFF;
1657 Status = gBS->AllocatePages (
1658 AllocateMaxAddress,
1659 EfiACPIReclaimMemory,
1660 EFI_SIZE_TO_PAGES (TotalSize),
1661 &PageAddress
1662 );
1663
1664 if (EFI_ERROR (Status)) {
1665 gBS->FreePages ((EFI_PHYSICAL_ADDRESS)(UINTN)AcpiTableInstance->Rsdp1, EFI_SIZE_TO_PAGES (RsdpTableSize));
1666 return EFI_OUT_OF_RESOURCES;
1667 }
1668
1669 Pointer = (UINT8 *) (UINTN) PageAddress;
1670 ZeroMem (Pointer, TotalSize);
1671
1672 AcpiTableInstance->Rsdt1 = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
1673 Pointer += (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + EFI_ACPI_MAX_NUM_TABLES * sizeof (UINT32));
1674 AcpiTableInstance->Rsdt3 = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
1675 Pointer += (sizeof (EFI_ACPI_DESCRIPTION_HEADER) + EFI_ACPI_MAX_NUM_TABLES * sizeof (UINT32));
1676 AcpiTableInstance->Xsdt = (EFI_ACPI_DESCRIPTION_HEADER *) Pointer;
1677
1678 //
1679 // Initialize RSDP
1680 //
1681 CurrentData = EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE;
1682 CopyMem (&AcpiTableInstance->Rsdp1->Signature, &CurrentData, sizeof (UINT64));
1683 CopyMem (AcpiTableInstance->Rsdp1->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (AcpiTableInstance->Rsdp1->OemId));
1684 AcpiTableInstance->Rsdp1->Reserved = EFI_ACPI_RESERVED_BYTE;
1685 AcpiTableInstance->Rsdp1->RsdtAddress = (UINT32) (UINTN) AcpiTableInstance->Rsdt1;
1686
1687 CurrentData = EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER_SIGNATURE;
1688 CopyMem (&AcpiTableInstance->Rsdp3->Signature, &CurrentData, sizeof (UINT64));
1689 CopyMem (AcpiTableInstance->Rsdp3->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (AcpiTableInstance->Rsdp3->OemId));
1690 AcpiTableInstance->Rsdp3->Revision = EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER_REVISION;
1691 AcpiTableInstance->Rsdp3->RsdtAddress = (UINT32) (UINTN) AcpiTableInstance->Rsdt3;
1692 AcpiTableInstance->Rsdp3->Length = sizeof (EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_POINTER);
1693 CurrentData = (UINT64) (UINTN) AcpiTableInstance->Xsdt;
1694 CopyMem (&AcpiTableInstance->Rsdp3->XsdtAddress, &CurrentData, sizeof (UINT64));
1695 SetMem (AcpiTableInstance->Rsdp3->Reserved, 3, EFI_ACPI_RESERVED_BYTE);
1696
1697 //
1698 // Initialize Rsdt
1699 //
1700 // Note that we "reserve" one entry for the FADT so it can always be
1701 // at the beginning of the list of tables. Some OS don't seem
1702 // to find it correctly if it is too far down the list.
1703 //
1704 AcpiTableInstance->Rsdt1->Signature = EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_TABLE_SIGNATURE;
1705 AcpiTableInstance->Rsdt1->Length = sizeof (EFI_ACPI_DESCRIPTION_HEADER);
1706 AcpiTableInstance->Rsdt1->Revision = EFI_ACPI_1_0_ROOT_SYSTEM_DESCRIPTION_TABLE_REVISION;
1707 CopyMem (AcpiTableInstance->Rsdt1->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (AcpiTableInstance->Rsdt1->OemId));
1708 CurrentData = PcdGet64 (PcdAcpiDefaultOemTableId);
1709 CopyMem (&AcpiTableInstance->Rsdt1->OemTableId, &CurrentData, sizeof (UINT64));
1710 AcpiTableInstance->Rsdt1->OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
1711 AcpiTableInstance->Rsdt1->CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
1712 AcpiTableInstance->Rsdt1->CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
1713 //
1714 // We always reserve first one for FADT
1715 //
1716 AcpiTableInstance->NumberOfTableEntries1 = 1;
1717 AcpiTableInstance->Rsdt1->Length = AcpiTableInstance->Rsdt1->Length + sizeof(UINT32);
1718
1719 AcpiTableInstance->Rsdt3->Signature = EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_TABLE_SIGNATURE;
1720 AcpiTableInstance->Rsdt3->Length = sizeof (EFI_ACPI_DESCRIPTION_HEADER);
1721 AcpiTableInstance->Rsdt3->Revision = EFI_ACPI_3_0_ROOT_SYSTEM_DESCRIPTION_TABLE_REVISION;
1722 CopyMem (AcpiTableInstance->Rsdt3->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (AcpiTableInstance->Rsdt3->OemId));
1723 CurrentData = PcdGet64 (PcdAcpiDefaultOemTableId);
1724 CopyMem (&AcpiTableInstance->Rsdt3->OemTableId, &CurrentData, sizeof (UINT64));
1725 AcpiTableInstance->Rsdt3->OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
1726 AcpiTableInstance->Rsdt3->CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
1727 AcpiTableInstance->Rsdt3->CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
1728 //
1729 // We always reserve first one for FADT
1730 //
1731 AcpiTableInstance->NumberOfTableEntries3 = 1;
1732 AcpiTableInstance->Rsdt3->Length = AcpiTableInstance->Rsdt3->Length + sizeof(UINT32);
1733
1734 //
1735 // Initialize Xsdt
1736 //
1737 AcpiTableInstance->Xsdt->Signature = EFI_ACPI_3_0_EXTENDED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE;
1738 AcpiTableInstance->Xsdt->Length = sizeof (EFI_ACPI_DESCRIPTION_HEADER);
1739 AcpiTableInstance->Xsdt->Revision = EFI_ACPI_3_0_EXTENDED_SYSTEM_DESCRIPTION_TABLE_REVISION;
1740 CopyMem (AcpiTableInstance->Xsdt->OemId, PcdGetPtr (PcdAcpiDefaultOemId), sizeof (AcpiTableInstance->Xsdt->OemId));
1741 CurrentData = PcdGet64 (PcdAcpiDefaultOemTableId);
1742 CopyMem (&AcpiTableInstance->Xsdt->OemTableId, &CurrentData, sizeof (UINT64));
1743 AcpiTableInstance->Xsdt->OemRevision = PcdGet32 (PcdAcpiDefaultOemRevision);
1744 AcpiTableInstance->Xsdt->CreatorId = PcdGet32 (PcdAcpiDefaultCreatorId);
1745 AcpiTableInstance->Xsdt->CreatorRevision = PcdGet32 (PcdAcpiDefaultCreatorRevision);
1746 //
1747 // We always reserve first one for FADT
1748 //
1749 AcpiTableInstance->Xsdt->Length = AcpiTableInstance->Xsdt->Length + sizeof(UINT64);
1750
1751 ChecksumCommonTables (AcpiTableInstance);
1752
1753 //
1754 // Completed successfully
1755 //
1756 return EFI_SUCCESS;
1757 }
1758