Tables: Fix the issues in handling virtual addressed tables.

When table is overridden or reloaded, AcpiTbDeleteTable() is called where
ACPI_TABLE_DESC.Pointer will be NULL.  It thus is impossible for virtual
addressed tables to obtain the .Pointer again in AcpiTbVerifyTable().

This patch stores virtual table addresses  (ACPI_TABLE_ORIGIN_ALLOCATED,
ACPI_TABLE_ORIGIN_UNKNOWN, ACPI_TABLE_ORIGIN_OVERRIDE) in the
ACPI_TABLE_DESC.Address field and refills the ACPI_TABLE_DESC.Pointer
using these addresses in AcpiTbVerifyTable().  Note that if a table with
ACPI_TABLE_ORIGIN_ALLOCATED set is actually freed, the .Address field
should be invalidated and thus must be replaced with NULL to avoid wrong
future validations occuring in AcpiTbVerifyTable().

Signed-off-by: Lv Zheng <lv.zheng@intel.com>
Tables: Fix the issue that multiple ACPI_FREE()s are invoked for same pointer around AcpiTbAddTable().

Currently there are following issues in AcpiTbAddTable():
Following logic is currently correct:
1. When a table is allocated in AcpiExLoadOp(), if a reloading happens,
   the allocated memory is freed by AcpiTbAddTable() and AE_OK is
   returned to the caller to avoid the caller to free it again.
Following logic is currently incorrect:
1. When a table is allocated in AcpiExLoadOp() or by the
   AcpiLoadTable() caller, if the table is already loaded, there will be
   twice ACPI_FREE() called for the same pointer when AcpiTbAddTable()
   returns AE_ALREADY_EXISTS.

This patch only fixes the above incorrect logic in AcpiTbAddTable():
1. Only invoke AcpiTbDeleteTable() if AE_OK is going to be returned.
2. After doing so, we do not invoke ACPI_FREE() when returning AE_OK;

Signed-off-by: Lv Zheng <lv.zheng@intel.com>
Tables: Cleanup split INSTALLED/VALIDATED table state logics.

This patch is mainly a naming cleanup to clarify hidden logics, no
functional changes.

AcpiInitializeTables() is used by Linux to install table addresses for
early boot steps.  During this stage, table addresses are mapped by
early_ioremap() mechanism which is different from the runtime IO mappings.
Thus it is not safe for ACPICA to keep mapped pointers in ACPI_TABLE_DESC
structure during this stage.

In order to support this in ACPICA, table states are divided into
1. "INSTALLED" (where ACPI_TABLE_DESC.Pointer is always NULL) and
2. "VALIDATED" (where ACPI_TABLE_DESC.Pointer is always not NULL).
During AcpiInitializeTables(), table state are ensured to be "INSTALLED"
but not "VALIDATED".  This logic is ensured by the original code in very
ambigious way.  For example, currently AcpiTb_DeleteTable() is invoked in
some place to perform an uninstallation while it is invoked in other place
to perform an invalidation.  They happen to work just because no one enters
the penalty where the 2 behaviours are not equivalent.

The naming cleanups are made in this patch:
A. For installation and validation:
   There is code setting ACPI_TABLE_DESC.Pointer first and delete it
   immediately to keep the descriptor's state as "INSTALLED" during the
   installation.  This patch implements this in more direct way.  After
   applying it, ACPI_TABLE_DESC.Pointer will never be set in
   AcpiTbInstallTable() and AcpiTbOverrideTable() as they are the only
   functions invoked during AcpiInitializeTables(). This is achieved by:
1. Rename AcpiTbVerifyTable() to AcpiTbValidateTable() to clarify this
   change.
2. Rename AcpiTbTableOverride() to AcpiTbOverrideTable() to keep nameing
   consistencies as other APIs (verb. Table).
3. Stops setting ACPI_TABLE_DESC.Pointer in AcpiTbInstallTable() and
   AcpiTbTableOverride().
4. Introduce AcpiTbAcquireTable() to acquire the table pointer that is not
   maintained in the ACPI_TABLE_DESC of the global root table list and
   rewrite AcpiTbValidateTable() using this new function to reduce
   redundancies.
5. Replace the table pointer using the overridden table pointer in
   AcpiTbAddTable().  As AcpiTbAddTable() is not invoked during early boot
   stage, tables returned from this functions should be "VALIDATED".  As
   AcpiTbOverrideTable() is modified by this patch to return a "INSTALLED"
   but not "VALIDATED" descriptor, to keep AcpiTbAddTable() unchanged,
   ACPI_TABLE_DESC.Pointer is filled in AcpiTbAddTable().
B. For invalidation and uninstallation:
   The original code invalidate table by invoking AcpiTbDeleteTable() here
   and there, but actually this function should only be used to uninstall
   tables.  This can work just because its invocations are equivalent to
   invalidation in some cases.
   This patch splits AcpiTbDeleteTable() into AcpiTbInvalidateTable() and
   AcpiTbUninstallTable() and cleans up the hidden logic using the new
   APIs.  This is achieved by:
1. Rename AcpiTbDeleteTable() to AcpiTbUninstallTable() as it is mainly
   called before resetting ACPI_TABLE_DESC.Address.  Thus the table
   descriptor is in "not INSTALLED" state.  This patch enforces this by
   setting ACPI_TABLE_DESC.Address to NULL in this function.
2. Introduce AcpiTbInvalidateTable() to be the reversal of
   AcpiTbValidateTable() and invoke it in AcpiTbUninstallTable().
3. Introduce AcpiTbReleaseTable() to release the table pointer that is not
   maintained in AcpiGbl_RootTableList and rewrite AcpiTbInvalidateTable()
   using this new function to reduce redundancies.

After cleaning up, the maintainability of the internal APIs are also
improved:
1. AcpiTbAcquireTable: Acquire ACPI_TABLE_HEADER according to
                       ACPI_TABLE_ORIGIN_xxx flags.
2. AcpiTbReleaseTable: Release ACPI_TABLE_HEADER according to
                       ACPI_TABLE_ORIGIN_xxx flags.
3. AcpiTbInstallTable: Make ACPI_TABLE_DESC.Address not NULL according to
                       ACPI_TABLE_ORIGIN_xxx flags.
4. AcpiTbUninstallTable: Make ACPI_TABLE_DESC.Address NULL according to
                         ACPI_TABLE_ORIGIN_xxx flags.
5. AcpiTbValidateTable: Make ACPI_TABLE_DESC.Pointer not NULL according to
                        ACPI_TABLE_ORIGIN_xxx flags.
6. AcpiTbInvalidateTable: Make ACPI_TABLE_DESC.Pointer NULL according to
                          ACPI_TABLE_ORIGIN_xxx flags.
7. AcpiTbOverrideTable: Replace ACPI_TABLE_DESC.Address and
                        ACPI_TABLE_DESC.Flags.  It only happens in
                        "INSTALLED" state.

The patch has been unit tested in AcpiExec by:
1. Initializing;
2. Executing exc_tbl ASLTS tests;
3. Executing "Load" command.
So that all original AcpiTbInstallTable() and AcpiTbOverrideTable()
invocations are covered.

Known Issues:
1. Cleanup AcpiTbAddTable() to Kill Code Redundancies
   Current implementation in AcpiTbAddTable() is not very clean, further
   patch can rewrite AcpiTbAddTable() with ordered AcpiTbInstallTable(),
   AcpiTbOverrideTable() and AcpiTbValidateTable().  It is not done in this
   patch so that it is easy for the reviewers to understand the changes in
   this patch.

Signed-off-by: Lv Zheng <lv.zheng@intel.com>
Tables: Fix the issue that table validations are not balanced.

As AcpiTbValidateTable() returns failure on checksum verification without
doing invalidatation, all its invocations that are not done to a descriptor
stored in AcpiGbl_RootTableList are checked and balanced.

But this is not a real issue as the descritors that have been passed to
AcpiTbAddTable() are all virtual address tables and the validations are in
fact no-op.  The cleanup can ensure that any future extensions made on
AcpiTbAddTable() to allow it to be invoked with physical address tables
will not trigger memory leakage regressions.

Signed-off-by: Lv Zheng <lv.zheng@intel.com>
Tables: Fix the issue that table checksums are not always verified before installation.

The original table handling code does not always verify checksums before
installing a table, this is because code to achieve this must be
implemented here and there in the redundant code blocks.

There are two stages during table initialization:
1. "INSTALLED" after AcpiTbInstallTable() and AcpiTbOverrideTable(),
   ACPI_TABLE_DESC.Pointer is ensured to be NULL.  This can be safely used
   during OSPM's early boot stage.
2. "VALIDATED" after AcpiTbValidateTable(), ACPI_TABLE_DESC.Pointer is
   ensured to be not NULL.  This must not be used during OSPM's early boot
   stage.

This patch changes AcpiTbAddTable() into an early boot safe API to reduce
code redundancies by changing the table state that is returned by this
function from "VALIDATED" to "INSTALLED".  Then the table verification
code can be done in a single place.  Originally, the AcpiTbAddTable() can
only be used by dynamic table loadings that are executed after early boot
stage, it cannot be used by static table loadings that are executed in
early boot stage as:
1.  The address of the table is a virtual address either maintained by
    OSPMs who call AcpiLoadTable() or by ACPICA whenever "Load" or
    "LoadTable" opcodes are executed, while during early boot stage,
    physical address of the table should be used for table loading.
2.  The API will ensure the state of the loaded table to be "VALIDATED"
    while during early boot stage, tables maintained by root table list
    should be kept as "INSTALLED".

To achieve this:
1. Rename AcpiTbInstallTable() to AcpiTbInstallFixedTable() as it only
   applies to DSDT/FACS installation.  Rename AcpiTbAddTable() to
   AcpiTbInstallNonFixedTable() as it will be applied to the installation
   of the rest kinds of tables.
2. Introduce AcpiTbInstallTable(), AcpiTbInstallAndOverrideTable to collect
   redudant code where their invocations actually have slight differences.
   1. AcpiTbInstallTable() is used to fill an ACPI_TABLE_DESC where the
      table length is known to the caller.
   2. AcpiTbInstallAndOverrideTable() is used to perform necessary
      overriding before installation.
3. Change a parameter of AcpiTbInstallNonFixedTable() from ACPI_TABLE_DESC
   to ACPI_PHYSICAL_ADDRESS to allow it to be invoked by static table
   loadings.  Also cleanup AcpiExLoadOp() and AcpiLoadTable() to accomodate
   to the parameter change.
4. Invoke AcpiTbInstallNonFixedTable() for all table loadings other than
   DSDT/FACS in AcpiTbParseRootTable() to improve code maintainability
   (logics are collected in the single function).  Also delete useless code
   from AcpiTbParseRootTable().
5. Remove all AcpiTbValidateTable() from AcpiTbInstallNonFixedTable() and
   AcpiTbInstallFixedTable() so that the table descriptor is kept in the
   state of "INSTALLED" but not "VALIDATED" after returning from these
   functions.
6. Introduce temporary ACPI_TABLE_DESC (NewTableDesc/OldTableDesc) into
   the functions to indicate a table descriptor that is not maintained by
   AcpiGbl_RootTableList.  Introduce AcpiTbAcquireTemporalTable() and
   AcpiTbReleaseTemporalTable() to handle the use cases of such temporal
   tables.  They are only used for verified installation.
7. Introduce AcpiTbVerifyTable() to validate table and verify table
   checksum, also remove table checksum verification from
   AcpiTbValidateTable().  Invoke AcpiTbValidateTable() in the functions
   that will convert a table into "LOADED" state or invoke it from
   AcpiGetTableXXX() APIs.  Invoke AcpiTbVerifyTable() on temporary
   ACPI_TABLE_DESC(s) that are going to be "INSTALLED".
8. Change AcpiTbOverrideTable() logic so that a temporary ACPI_TABLE_DESC
   will be overridden before installtion, this makes code simpler.

After applying the patch, tables are always installed after being
overridden and the table checksums are always verified before installation.

Signed-off-by: Lv Zheng <lv.zheng@intel.com>
Tables: Cleanup ACPI_TABLE_ORIGIN_xxx flags.

This patch refines ACPI_TABLE_ORIGIN_xxx flags.  No functional changes.

The previous commits have introduced the following internal APIs:
1. AcpiTbAcquireTable: Acquire ACPI_TABLE_HEADER according to
                       ACPI_TABLE_ORIGIN_xxx flags.
2. AcpiTbReleaseTable: Release ACPI_TABLE_HEADER according to
                       ACPI_TABLE_ORIGIN_xxx flags.
3. AcpiTbInstallTable: Make ACPI_TABLE_DESC.Address not NULL according to
                       ACPI_TABLE_ORIGIN_xxx flags.
4. AcpiTbUninstallTable: Make ACPI_TABLE_DESC.Address NULL according to
                         ACPI_TABLE_ORIGIN_xxx flags.
5. AcpiTbValidateTable: Make ACPI_TABLE_DESC.Pointer not NULL according to
                        ACPI_TABLE_ORIGIN_xxx flags.
6. AcpiTbInvalidateTable: Make ACPI_TABLE_DESC.Pointer NULL according to
                          ACPI_TABLE_ORIGIN_xxx flags.
It thus detects that the ACPI_TABLE_ORIGIN_UNKNOWN is redundant to
ACPI_TABLE_ORIGIN_OVERRIDE.

The ACPI_TABLE_ORIGIN_xxTERN_VIRTUAL flags are named as VIRTUAL in order
not to confuse with x86 logical address, this patch also renames all
"logical override" into "virtual override".

Signed-off-by: Lv Zheng <lv.zheng@intel.com>
Tables: Avoid SSDT installation when AcpiGbl_DisableSsdtTableLoad is specified.

It is reported that when AcpiGbl_DisableSsdtTableLoad is specified, user
still can see it installed into /sys/firmware/acpi/tables on Linux boxes.
This is because the option only stops table "loading", but doesn't stop
table "installing", thus it is still in the AcpiGbl_RootTableList. With
previous cleanups, it is possible to prevent SSDT installations to make
it not such confusing.  The global variable is also renamed.  Lv Zheng.

Signed-off-by: Lv Zheng <lv.zheng@intel.com>
Tables: Add AcpiInstallTable() API to be invoked by OSPM early table installation.

This patch adds a new API - AcpiInstallTable().  OSPMs can use this API
to install tables during early boot stage.  Lv Zheng.

Signed-off-by: Lv Zheng <lv.zheng@intel.com>
Merge pull request #21 from zetalog/linux-tables

Linux tables