Commit eadd7a1b authored by danh-arm's avatar danh-arm
Browse files

Merge pull request #240 from danh-arm/sm/rem_coh_mem

Remove coherent memory v2
parents 1c73ffbd ab8707e6
...@@ -63,6 +63,8 @@ ARM_GIC_ARCH := 2 ...@@ -63,6 +63,8 @@ ARM_GIC_ARCH := 2
# Flag used to indicate if ASM_ASSERTION should be enabled for the build. # Flag used to indicate if ASM_ASSERTION should be enabled for the build.
# This defaults to being present in DEBUG builds only. # This defaults to being present in DEBUG builds only.
ASM_ASSERTION := ${DEBUG} ASM_ASSERTION := ${DEBUG}
# Build option to choose whether Trusted firmware uses Coherent memory or not.
USE_COHERENT_MEM := 1
# Default FIP file name # Default FIP file name
FIP_NAME := fip.bin FIP_NAME := fip.bin
...@@ -230,6 +232,10 @@ $(eval $(call add_define,ASM_ASSERTION)) ...@@ -230,6 +232,10 @@ $(eval $(call add_define,ASM_ASSERTION))
# Process LOG_LEVEL flag # Process LOG_LEVEL flag
$(eval $(call add_define,LOG_LEVEL)) $(eval $(call add_define,LOG_LEVEL))
# Process USE_COHERENT_MEM flag
$(eval $(call assert_boolean,USE_COHERENT_MEM))
$(eval $(call add_define,USE_COHERENT_MEM))
ASFLAGS += -nostdinc -ffreestanding -Wa,--fatal-warnings \ ASFLAGS += -nostdinc -ffreestanding -Wa,--fatal-warnings \
-Werror -Wmissing-include-dirs \ -Werror -Wmissing-include-dirs \
-mgeneral-regs-only -D__ASSEMBLY__ \ -mgeneral-regs-only -D__ASSEMBLY__ \
......
...@@ -131,9 +131,11 @@ func bl1_entrypoint ...@@ -131,9 +131,11 @@ func bl1_entrypoint
ldr x1, =__BSS_SIZE__ ldr x1, =__BSS_SIZE__
bl zeromem16 bl zeromem16
#if USE_COHERENT_MEM
ldr x0, =__COHERENT_RAM_START__ ldr x0, =__COHERENT_RAM_START__
ldr x1, =__COHERENT_RAM_UNALIGNED_SIZE__ ldr x1, =__COHERENT_RAM_UNALIGNED_SIZE__
bl zeromem16 bl zeromem16
#endif
ldr x0, =__DATA_RAM_START__ ldr x0, =__DATA_RAM_START__
ldr x1, =__DATA_ROM_START__ ldr x1, =__DATA_ROM_START__
......
...@@ -107,6 +107,7 @@ SECTIONS ...@@ -107,6 +107,7 @@ SECTIONS
*(xlat_table) *(xlat_table)
} >RAM } >RAM
#if USE_COHERENT_MEM
/* /*
* The base address of the coherent memory section must be page-aligned (4K) * The base address of the coherent memory section must be page-aligned (4K)
* to guarantee that the coherent data are stored on their own pages and * to guarantee that the coherent data are stored on their own pages and
...@@ -125,6 +126,7 @@ SECTIONS ...@@ -125,6 +126,7 @@ SECTIONS
. = NEXT(4096); . = NEXT(4096);
__COHERENT_RAM_END__ = .; __COHERENT_RAM_END__ = .;
} >RAM } >RAM
#endif
__BL1_RAM_START__ = ADDR(.data); __BL1_RAM_START__ = ADDR(.data);
__BL1_RAM_END__ = .; __BL1_RAM_END__ = .;
...@@ -140,8 +142,10 @@ SECTIONS ...@@ -140,8 +142,10 @@ SECTIONS
__BSS_SIZE__ = SIZEOF(.bss); __BSS_SIZE__ = SIZEOF(.bss);
#if USE_COHERENT_MEM
__COHERENT_RAM_UNALIGNED_SIZE__ = __COHERENT_RAM_UNALIGNED_SIZE__ =
__COHERENT_RAM_END_UNALIGNED__ - __COHERENT_RAM_START__; __COHERENT_RAM_END_UNALIGNED__ - __COHERENT_RAM_START__;
#endif
ASSERT(. <= BL1_RW_LIMIT, "BL1's RW section has exceeded its limit.") ASSERT(. <= BL1_RW_LIMIT, "BL1's RW section has exceeded its limit.")
} }
...@@ -91,9 +91,11 @@ func bl2_entrypoint ...@@ -91,9 +91,11 @@ func bl2_entrypoint
ldr x1, =__BSS_SIZE__ ldr x1, =__BSS_SIZE__
bl zeromem16 bl zeromem16
#if USE_COHERENT_MEM
ldr x0, =__COHERENT_RAM_START__ ldr x0, =__COHERENT_RAM_START__
ldr x1, =__COHERENT_RAM_UNALIGNED_SIZE__ ldr x1, =__COHERENT_RAM_UNALIGNED_SIZE__
bl zeromem16 bl zeromem16
#endif
/* -------------------------------------------- /* --------------------------------------------
* Allocate a stack whose memory will be marked * Allocate a stack whose memory will be marked
......
...@@ -93,6 +93,7 @@ SECTIONS ...@@ -93,6 +93,7 @@ SECTIONS
*(xlat_table) *(xlat_table)
} >RAM } >RAM
#if USE_COHERENT_MEM
/* /*
* The base address of the coherent memory section must be page-aligned (4K) * The base address of the coherent memory section must be page-aligned (4K)
* to guarantee that the coherent data are stored on their own pages and * to guarantee that the coherent data are stored on their own pages and
...@@ -111,12 +112,16 @@ SECTIONS ...@@ -111,12 +112,16 @@ SECTIONS
. = NEXT(4096); . = NEXT(4096);
__COHERENT_RAM_END__ = .; __COHERENT_RAM_END__ = .;
} >RAM } >RAM
#endif
__BL2_END__ = .; __BL2_END__ = .;
__BSS_SIZE__ = SIZEOF(.bss); __BSS_SIZE__ = SIZEOF(.bss);
#if USE_COHERENT_MEM
__COHERENT_RAM_UNALIGNED_SIZE__ = __COHERENT_RAM_UNALIGNED_SIZE__ =
__COHERENT_RAM_END_UNALIGNED__ - __COHERENT_RAM_START__; __COHERENT_RAM_END_UNALIGNED__ - __COHERENT_RAM_START__;
#endif
ASSERT(. <= BL2_LIMIT, "BL2 image has exceeded its limit.") ASSERT(. <= BL2_LIMIT, "BL2 image has exceeded its limit.")
} }
...@@ -149,9 +149,11 @@ func bl31_entrypoint ...@@ -149,9 +149,11 @@ func bl31_entrypoint
ldr x1, =__BSS_SIZE__ ldr x1, =__BSS_SIZE__
bl zeromem16 bl zeromem16
#if USE_COHERENT_MEM
ldr x0, =__COHERENT_RAM_START__ ldr x0, =__COHERENT_RAM_START__
ldr x1, =__COHERENT_RAM_UNALIGNED_SIZE__ ldr x1, =__COHERENT_RAM_UNALIGNED_SIZE__
bl zeromem16 bl zeromem16
#endif
/* --------------------------------------------- /* ---------------------------------------------
* Initialize the cpu_ops pointer. * Initialize the cpu_ops pointer.
......
...@@ -117,6 +117,7 @@ SECTIONS ...@@ -117,6 +117,7 @@ SECTIONS
*(xlat_table) *(xlat_table)
} >RAM } >RAM
#if USE_COHERENT_MEM
/* /*
* The base address of the coherent memory section must be page-aligned (4K) * The base address of the coherent memory section must be page-aligned (4K)
* to guarantee that the coherent data are stored on their own pages and * to guarantee that the coherent data are stored on their own pages and
...@@ -135,12 +136,15 @@ SECTIONS ...@@ -135,12 +136,15 @@ SECTIONS
. = NEXT(4096); . = NEXT(4096);
__COHERENT_RAM_END__ = .; __COHERENT_RAM_END__ = .;
} >RAM } >RAM
#endif
__BL31_END__ = .; __BL31_END__ = .;
__BSS_SIZE__ = SIZEOF(.bss); __BSS_SIZE__ = SIZEOF(.bss);
#if USE_COHERENT_MEM
__COHERENT_RAM_UNALIGNED_SIZE__ = __COHERENT_RAM_UNALIGNED_SIZE__ =
__COHERENT_RAM_END_UNALIGNED__ - __COHERENT_RAM_START__; __COHERENT_RAM_END_UNALIGNED__ - __COHERENT_RAM_START__;
#endif
ASSERT(. <= BL31_LIMIT, "BL3-1 image has exceeded its limit.") ASSERT(. <= BL31_LIMIT, "BL3-1 image has exceeded its limit.")
} }
...@@ -40,7 +40,6 @@ BL31_SOURCES += bl31/bl31_main.c \ ...@@ -40,7 +40,6 @@ BL31_SOURCES += bl31/bl31_main.c \
bl31/aarch64/runtime_exceptions.S \ bl31/aarch64/runtime_exceptions.S \
bl31/aarch64/crash_reporting.S \ bl31/aarch64/crash_reporting.S \
lib/cpus/aarch64/cpu_helpers.S \ lib/cpus/aarch64/cpu_helpers.S \
lib/locks/bakery/bakery_lock.c \
lib/locks/exclusive/spinlock.S \ lib/locks/exclusive/spinlock.S \
services/std_svc/std_svc_setup.c \ services/std_svc/std_svc_setup.c \
services/std_svc/psci/psci_afflvl_off.c \ services/std_svc/psci/psci_afflvl_off.c \
...@@ -53,6 +52,12 @@ BL31_SOURCES += bl31/bl31_main.c \ ...@@ -53,6 +52,12 @@ BL31_SOURCES += bl31/bl31_main.c \
services/std_svc/psci/psci_setup.c \ services/std_svc/psci/psci_setup.c \
services/std_svc/psci/psci_system_off.c services/std_svc/psci/psci_system_off.c
ifeq (${USE_COHERENT_MEM}, 1)
BL31_SOURCES += lib/locks/bakery/bakery_lock_coherent.c
else
BL31_SOURCES += lib/locks/bakery/bakery_lock_normal.c
endif
BL31_LINKERFILE := bl31/bl31.ld.S BL31_LINKERFILE := bl31/bl31.ld.S
# Flag used by the generic interrupt management framework to determine if # Flag used by the generic interrupt management framework to determine if
......
...@@ -108,9 +108,11 @@ func tsp_entrypoint ...@@ -108,9 +108,11 @@ func tsp_entrypoint
ldr x1, =__BSS_SIZE__ ldr x1, =__BSS_SIZE__
bl zeromem16 bl zeromem16
#if USE_COHERENT_MEM
ldr x0, =__COHERENT_RAM_START__ ldr x0, =__COHERENT_RAM_START__
ldr x1, =__COHERENT_RAM_UNALIGNED_SIZE__ ldr x1, =__COHERENT_RAM_UNALIGNED_SIZE__
bl zeromem16 bl zeromem16
#endif
/* -------------------------------------------- /* --------------------------------------------
* Allocate a stack whose memory will be marked * Allocate a stack whose memory will be marked
......
...@@ -98,6 +98,7 @@ SECTIONS ...@@ -98,6 +98,7 @@ SECTIONS
*(xlat_table) *(xlat_table)
} >RAM } >RAM
#if USE_COHERENT_MEM
/* /*
* The base address of the coherent memory section must be page-aligned (4K) * The base address of the coherent memory section must be page-aligned (4K)
* to guarantee that the coherent data are stored on their own pages and * to guarantee that the coherent data are stored on their own pages and
...@@ -116,12 +117,15 @@ SECTIONS ...@@ -116,12 +117,15 @@ SECTIONS
. = NEXT(4096); . = NEXT(4096);
__COHERENT_RAM_END__ = .; __COHERENT_RAM_END__ = .;
} >RAM } >RAM
#endif
__BL32_END__ = .; __BL32_END__ = .;
__BSS_SIZE__ = SIZEOF(.bss); __BSS_SIZE__ = SIZEOF(.bss);
#if USE_COHERENT_MEM
__COHERENT_RAM_UNALIGNED_SIZE__ = __COHERENT_RAM_UNALIGNED_SIZE__ =
__COHERENT_RAM_END_UNALIGNED__ - __COHERENT_RAM_START__; __COHERENT_RAM_END_UNALIGNED__ - __COHERENT_RAM_START__;
#endif
ASSERT(. <= BL32_LIMIT, "BL3-2 image has exceeded its limit.") ASSERT(. <= BL32_LIMIT, "BL3-2 image has exceeded its limit.")
} }
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
* of trusted SRAM * of trusted SRAM
******************************************************************************/ ******************************************************************************/
extern unsigned long __RO_START__; extern unsigned long __RO_START__;
extern unsigned long __COHERENT_RAM_END__; extern unsigned long __BL32_END__;
/******************************************************************************* /*******************************************************************************
* Lock to control access to the console * Lock to control access to the console
...@@ -63,11 +63,11 @@ work_statistics_t tsp_stats[PLATFORM_CORE_COUNT]; ...@@ -63,11 +63,11 @@ work_statistics_t tsp_stats[PLATFORM_CORE_COUNT];
/******************************************************************************* /*******************************************************************************
* The BL32 memory footprint starts with an RO sections and ends * The BL32 memory footprint starts with an RO sections and ends
* with a section for coherent RAM. Use it to find the memory size * with the linker symbol __BL32_END__. Use it to find the memory size
******************************************************************************/ ******************************************************************************/
#define BL32_TOTAL_BASE (unsigned long)(&__RO_START__) #define BL32_TOTAL_BASE (unsigned long)(&__RO_START__)
#define BL32_TOTAL_LIMIT (unsigned long)(&__COHERENT_RAM_END__) #define BL32_TOTAL_LIMIT (unsigned long)(&__BL32_END__)
static tsp_args_t *set_smc_args(uint64_t arg0, static tsp_args_t *set_smc_args(uint64_t arg0,
uint64_t arg1, uint64_t arg1,
......
...@@ -12,8 +12,9 @@ Contents : ...@@ -12,8 +12,9 @@ Contents :
7. [CPU specific operations framework](#7--cpu-specific-operations-framework) 7. [CPU specific operations framework](#7--cpu-specific-operations-framework)
8. [Memory layout of BL images](#8-memory-layout-of-bl-images) 8. [Memory layout of BL images](#8-memory-layout-of-bl-images)
9. [Firmware Image Package (FIP)](#9--firmware-image-package-fip) 9. [Firmware Image Package (FIP)](#9--firmware-image-package-fip)
10. [Code Structure](#10--code-structure) 10. [Use of coherent memory in Trusted Firmware](#10--use-of-coherent-memory-in-trusted-firmware)
11. [References](#11--references) 11. [Code Structure](#11--code-structure)
12. [References](#12--references)
1. Introduction 1. Introduction
...@@ -368,10 +369,10 @@ level implementation of the generic timer through the memory mapped interface. ...@@ -368,10 +369,10 @@ level implementation of the generic timer through the memory mapped interface.
`ON`; any other cluster is `OFF`. BL3-1 initializes the data structures that `ON`; any other cluster is `OFF`. BL3-1 initializes the data structures that
implement the state machine, including the locks that protect them. BL3-1 implement the state machine, including the locks that protect them. BL3-1
accesses the state of a CPU or cluster immediately after reset and before accesses the state of a CPU or cluster immediately after reset and before
the MMU is enabled in the warm boot path. It is not currently possible to the data cache is enabled in the warm boot path. It is not currently
use 'exclusive' based spinlocks, therefore BL3-1 uses locks based on possible to use 'exclusive' based spinlocks, therefore BL3-1 uses locks
Lamport's Bakery algorithm instead. BL3-1 allocates these locks in device based on Lamport's Bakery algorithm instead. BL3-1 allocates these locks in
memory. They are accessible irrespective of MMU state. device memory by default.
* Runtime services initialization: * Runtime services initialization:
...@@ -1127,9 +1128,10 @@ this purpose: ...@@ -1127,9 +1128,10 @@ this purpose:
* `__BSS_START__` This address must be aligned on a 16-byte boundary. * `__BSS_START__` This address must be aligned on a 16-byte boundary.
* `__BSS_SIZE__` * `__BSS_SIZE__`
Similarly, the coherent memory section must be zero-initialised. Also, the MMU Similarly, the coherent memory section (if enabled) must be zero-initialised.
setup code needs to know the extents of this section to set the right memory Also, the MMU setup code needs to know the extents of this section to set the
attributes for it. The following linker symbols are defined for this purpose: right memory attributes for it. The following linker symbols are defined for
this purpose:
* `__COHERENT_RAM_START__` This address must be aligned on a page-size boundary. * `__COHERENT_RAM_START__` This address must be aligned on a page-size boundary.
* `__COHERENT_RAM_END__` This address must be aligned on a page-size boundary. * `__COHERENT_RAM_END__` This address must be aligned on a page-size boundary.
...@@ -1443,7 +1445,208 @@ Currently the FVP's policy only allows loading of a known set of images. The ...@@ -1443,7 +1445,208 @@ Currently the FVP's policy only allows loading of a known set of images. The
platform policy can be modified to allow additional images. platform policy can be modified to allow additional images.
10. Code Structure 10. Use of coherent memory in Trusted Firmware
----------------------------------------------
There might be loss of coherency when physical memory with mismatched
shareability, cacheability and memory attributes is accessed by multiple CPUs
(refer to section B2.9 of [ARM ARM] for more details). This possibility occurs
in Trusted Firmware during power up/down sequences when coherency, MMU and
caches are turned on/off incrementally.
Trusted Firmware defines coherent memory as a region of memory with Device
nGnRE attributes in the translation tables. The translation granule size in
Trusted Firmware is 4KB. This is the smallest possible size of the coherent
memory region.
By default, all data structures which are susceptible to accesses with
mismatched attributes from various CPUs are allocated in a coherent memory
region (refer to section 2.1 of [Porting Guide]). The coherent memory region
accesses are Outer Shareable, non-cacheable and they can be accessed
with the Device nGnRE attributes when the MMU is turned on. Hence, at the
expense of at least an extra page of memory, Trusted Firmware is able to work
around coherency issues due to mismatched memory attributes.
The alternative to the above approach is to allocate the susceptible data
structures in Normal WriteBack WriteAllocate Inner shareable memory. This
approach requires the data structures to be designed so that it is possible to
work around the issue of mismatched memory attributes by performing software
cache maintenance on them.
### Disabling the use of coherent memory in Trusted Firmware
It might be desirable to avoid the cost of allocating coherent memory on
platforms which are memory constrained. Trusted Firmware enables inclusion of
coherent memory in firmware images through the build flag `USE_COHERENT_MEM`.
This flag is enabled by default. It can be disabled to choose the second
approach described above.
The below sections analyze the data structures allocated in the coherent memory
region and the changes required to allocate them in normal memory.
### PSCI Affinity map nodes
The `psci_aff_map` data structure stores the hierarchial node information for
each affinity level in the system including the PSCI states associated with them.
By default, this data structure is allocated in the coherent memory region in
the Trusted Firmware because it can be accessed by multiple CPUs, either with
their caches enabled or disabled.
typedef struct aff_map_node {
unsigned long mpidr;
unsigned char ref_count;
unsigned char state;
unsigned char level;
#if USE_COHERENT_MEM
bakery_lock_t lock;
#else
unsigned char aff_map_index;
#endif
} aff_map_node_t;
In order to move this data structure to normal memory, the use of each of its
fields must be analyzed. Fields like `mpidr` and `level` are only written once
during cold boot. Hence removing them from coherent memory involves only doing
a clean and invalidate of the cache lines after these fields are written.
The fields `state` and `ref_count` can be concurrently accessed by multiple
CPUs in different cache states. A Lamport's Bakery lock is used to ensure mutual
exlusion to these fields. As a result, it is possible to move these fields out
of coherent memory by performing software cache maintenance on them. The field
`lock` is the bakery lock data structure when `USE_COHERENT_MEM` is enabled.
The `aff_map_index` is used to identify the bakery lock when `USE_COHERENT_MEM`
is disabled.
### Bakery lock data
The bakery lock data structure `bakery_lock_t` is allocated in coherent memory
and is accessed by multiple CPUs with mismatched attributes. `bakery_lock_t` is
defined as follows:
typedef struct bakery_lock {
int owner;
volatile char entering[BAKERY_LOCK_MAX_CPUS];
volatile unsigned number[BAKERY_LOCK_MAX_CPUS];
} bakery_lock_t;
It is a characteristic of Lamport's Bakery algorithm that the volatile per-CPU
fields can be read by all CPUs but only written to by the owning CPU.
Depending upon the data cache line size, the per-CPU fields of the
`bakery_lock_t` structure for multiple CPUs may exist on a single cache line.
These per-CPU fields can be read and written during lock contention by multiple
CPUs with mismatched memory attributes. Since these fields are a part of the
lock implementation, they do not have access to any other locking primitive to
safeguard against the resulting coherency issues. As a result, simple software
cache maintenance is not enough to allocate them in coherent memory. Consider
the following example.
CPU0 updates its per-CPU field with data cache enabled. This write updates a
local cache line which contains a copy of the fields for other CPUs as well. Now
CPU1 updates its per-CPU field of the `bakery_lock_t` structure with data cache
disabled. CPU1 then issues a DCIVAC operation to invalidate any stale copies of
its field in any other cache line in the system. This operation will invalidate
the update made by CPU0 as well.
To use bakery locks when `USE_COHERENT_MEM` is disabled, the lock data structure
has been redesigned. The changes utilise the characteristic of Lamport's Bakery
algorithm mentioned earlier. The per-CPU fields of the new lock structure are
aligned such that they are allocated on separate cache lines. The per-CPU data
framework in Trusted Firmware is used to achieve this. This enables software to
perform software cache maintenance on the lock data structure without running
into coherency issues associated with mismatched attributes.
The per-CPU data framework enables consolidation of data structures on the
fewest cache lines possible. This saves memory as compared to the scenario where
each data structure is separately aligned to the cache line boundary to achieve
the same effect.
The bakery lock data structure `bakery_info_t` is defined for use when
`USE_COHERENT_MEM` is disabled as follows:
typedef struct bakery_info {
/*
* The lock_data is a bit-field of 2 members:
* Bit[0] : choosing. This field is set when the CPU is
* choosing its bakery number.
* Bits[1 - 15] : number. This is the bakery number allocated.
*/
volatile uint16_t lock_data;
} bakery_info_t;
The `bakery_info_t` represents a single per-CPU field of one lock and
the combination of corresponding `bakery_info_t` structures for all CPUs in the
system represents the complete bakery lock. It is embedded in the per-CPU
data framework `cpu_data` as shown below:
CPU0 cpu_data
------------------
| .... |
|----------------|
| `bakery_info_t`| <-- Lock_0 per-CPU field
| Lock_0 | for CPU0
|----------------|
| `bakery_info_t`| <-- Lock_1 per-CPU field
| Lock_1 | for CPU0
|----------------|
| .... |
|----------------|
| `bakery_info_t`| <-- Lock_N per-CPU field
| Lock_N | for CPU0
------------------
CPU1 cpu_data
------------------
| .... |
|----------------|
| `bakery_info_t`| <-- Lock_0 per-CPU field
| Lock_0 | for CPU1
|----------------|
| `bakery_info_t`| <-- Lock_1 per-CPU field
| Lock_1 | for CPU1
|----------------|
| .... |
|----------------|
| `bakery_info_t`| <-- Lock_N per-CPU field
| Lock_N | for CPU1
------------------
Consider a system of 2 CPUs with 'N' bakery locks as shown above. For an
operation on Lock_N, the corresponding `bakery_info_t` in both CPU0 and CPU1
`cpu_data` need to be fetched and appropriate cache operations need to be
performed for each access.
For multiple bakery locks, an array of `bakery_info_t` is declared in `cpu_data`
and each lock is given an `id` to identify it in the array.
### Non Functional Impact of removing coherent memory
Removal of the coherent memory region leads to the additional software overhead
of performing cache maintenance for the affected data structures. However, since
the memory where the data structures are allocated is cacheable, the overhead is
mostly mitigated by an increase in performance.
There is however a performance impact for bakery locks, due to:
* Additional cache maintenance operations, and
* Multiple cache line reads for each lock operation, since the bakery locks
for each CPU are distributed across different cache lines.
The implementation has been optimized to mimimize this additional overhead.
Measurements indicate that when bakery locks are allocated in Normal memory, the
minimum latency of acquiring a lock is on an average 3-4 micro seconds whereas
in Device memory the same is 2 micro seconds. The measurements were done on the
Juno ARM development platform.
As mentioned earlier, almost a page of memory can be saved by disabling
`USE_COHERENT_MEM`. Each platform needs to consider these trade-offs to decide
whether coherent memory should be used. If a platform disables
`USE_COHERENT_MEM` and needs to use bakery locks in the porting layer, it should
reserve memory in `cpu_data` by defining the macro `PLAT_PCPU_DATA_SIZE` (see
the [Porting Guide]). Refer to the reference platform code for examples.
11. Code Structure
------------------- -------------------
Trusted Firmware code is logically divided between the three boot loader Trusted Firmware code is logically divided between the three boot loader
...@@ -1488,7 +1691,7 @@ FDTs provide a description of the hardware platform and are used by the Linux ...@@ -1488,7 +1691,7 @@ FDTs provide a description of the hardware platform and are used by the Linux
kernel at boot time. These can be found in the `fdts` directory. kernel at boot time. These can be found in the `fdts` directory.
11. References 12. References
--------------- ---------------
1. Trusted Board Boot Requirements CLIENT PDD (ARM DEN 0006B-5). Available 1. Trusted Board Boot Requirements CLIENT PDD (ARM DEN 0006B-5). Available
...@@ -1504,7 +1707,7 @@ kernel at boot time. These can be found in the `fdts` directory. ...@@ -1504,7 +1707,7 @@ kernel at boot time. These can be found in the `fdts` directory.
_Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved._ _Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved._
[ARM ARM]: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0487a.e/index.html "ARMv8-A Reference Manual (ARM DDI0487A.E)"
[PSCI]: http://infocenter.arm.com/help/topic/com.arm.doc.den0022b/index.html "Power State Coordination Interface PDD (ARM DEN 0022B.b)" [PSCI]: http://infocenter.arm.com/help/topic/com.arm.doc.den0022b/index.html "Power State Coordination Interface PDD (ARM DEN 0022B.b)"
[SMCCC]: http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html "SMC Calling Convention PDD (ARM DEN 0028A)" [SMCCC]: http://infocenter.arm.com/help/topic/com.arm.doc.den0028a/index.html "SMC Calling Convention PDD (ARM DEN 0028A)"
[UUID]: https://tools.ietf.org/rfc/rfc4122.txt "A Universally Unique IDentifier (UUID) URN Namespace" [UUID]: https://tools.ietf.org/rfc/rfc4122.txt "A Universally Unique IDentifier (UUID) URN Namespace"
......
...@@ -63,11 +63,11 @@ mapped page tables, and enable both the instruction and data caches for each BL ...@@ -63,11 +63,11 @@ mapped page tables, and enable both the instruction and data caches for each BL
stage. In the ARM FVP port, each BL stage configures the MMU in its platform- stage. In the ARM FVP port, each BL stage configures the MMU in its platform-
specific architecture setup function, for example `blX_plat_arch_setup()`. specific architecture setup function, for example `blX_plat_arch_setup()`.
Each platform must allocate a block of identity mapped secure memory with If the build option `USE_COHERENT_MEM` is enabled, each platform must allocate a
Device-nGnRE attributes aligned to page boundary (4K) for each BL stage. This block of identity mapped secure memory with Device-nGnRE attributes aligned to
memory is identified by the section name `tzfw_coherent_mem` so that its page boundary (4K) for each BL stage. This memory is identified by the section
possible for the firmware to place variables in it using the following C code name `tzfw_coherent_mem` so that its possible for the firmware to place
directive: variables in it using the following C code directive:
__attribute__ ((section("tzfw_coherent_mem"))) __attribute__ ((section("tzfw_coherent_mem")))
...@@ -246,6 +246,17 @@ must also be defined: ...@@ -246,6 +246,17 @@ must also be defined:
entities than this value using `io_open()` will fail with entities than this value using `io_open()` will fail with
IO_RESOURCES_EXHAUSTED. IO_RESOURCES_EXHAUSTED.
If the platform needs to allocate data within the per-cpu data framework in
BL3-1, it should define the following macro. Currently this is only required if
the platform decides not to use the coherent memory section by undefining the
USE_COHERENT_MEM build flag. In this case, the framework allocates the required
memory within the the per-cpu data to minimize wastage.
* **#define : PLAT_PCPU_DATA_SIZE**
Defines the memory (in bytes) to be reserved within the per-cpu data
structure for use by the platform layer.
The following constants are optional. They should be defined when the platform The following constants are optional. They should be defined when the platform
memory layout implies some image overlaying like on FVP. memory layout implies some image overlaying like on FVP.
......
...@@ -245,6 +245,12 @@ performed. ...@@ -245,6 +245,12 @@ performed.
synchronous method) or 1 (BL3-2 is initialized using asynchronous method). synchronous method) or 1 (BL3-2 is initialized using asynchronous method).
Default is 0. Default is 0.
* `USE_COHERENT_MEM`: This flag determines whether to include the coherent
memory region in the BL memory map or not (see "Use of Coherent memory in
Trusted Firmware" section in [Firmware Design]). It can take the value 1
(Coherent memory region is included) or 0 (Coherent memory region is
excluded). Default is 1.
#### FVP specific build options #### FVP specific build options
* `FVP_TSP_RAM_LOCATION`: location of the TSP binary. Options: * `FVP_TSP_RAM_LOCATION`: location of the TSP binary. Options:
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
#define __CPU_DATA_H__ #define __CPU_DATA_H__
/* Offsets for the cpu_data structure */ /* Offsets for the cpu_data structure */
#define CPU_DATA_CRASH_BUF_OFFSET 0x20 #define CPU_DATA_CRASH_BUF_OFFSET 0x18
#if CRASH_REPORTING #if CRASH_REPORTING
#define CPU_DATA_LOG2SIZE 7 #define CPU_DATA_LOG2SIZE 7
#else #else
...@@ -45,10 +45,20 @@ ...@@ -45,10 +45,20 @@
#ifndef __ASSEMBLY__ #ifndef __ASSEMBLY__
#include <arch_helpers.h> #include <arch_helpers.h>
#include <cassert.h>
#include <platform_def.h> #include <platform_def.h>
#include <psci.h> #include <psci.h>
#include <stdint.h> #include <stdint.h>
/* Offsets for the cpu_data structure */
#define CPU_DATA_PSCI_LOCK_OFFSET __builtin_offsetof\
(cpu_data_t, psci_svc_cpu_data.pcpu_bakery_info)
#if PLAT_PCPU_DATA_SIZE
#define CPU_DATA_PLAT_PCPU_OFFSET __builtin_offsetof\
(cpu_data_t, platform_cpu_data)
#endif
/******************************************************************************* /*******************************************************************************
* Function & variable prototypes * Function & variable prototypes
******************************************************************************/ ******************************************************************************/
...@@ -69,9 +79,12 @@ ...@@ -69,9 +79,12 @@
typedef struct cpu_data { typedef struct cpu_data {
void *cpu_context[2]; void *cpu_context[2];
uint64_t cpu_ops_ptr; uint64_t cpu_ops_ptr;
struct psci_cpu_data psci_svc_cpu_data;
#if CRASH_REPORTING #if CRASH_REPORTING
uint64_t crash_buf[CPU_DATA_CRASH_BUF_SIZE >> 3]; uint64_t crash_buf[CPU_DATA_CRASH_BUF_SIZE >> 3];
#endif
struct psci_cpu_data psci_svc_cpu_data;
#if PLAT_PCPU_DATA_SIZE
uint8_t platform_cpu_data[PLAT_PCPU_DATA_SIZE];
#endif #endif
} __aligned(CACHE_WRITEBACK_GRANULE) cpu_data_t; } __aligned(CACHE_WRITEBACK_GRANULE) cpu_data_t;
......
...@@ -31,6 +31,17 @@ ...@@ -31,6 +31,17 @@
#ifndef __PSCI_H__ #ifndef __PSCI_H__
#define __PSCI_H__ #define __PSCI_H__
#include <bakery_lock.h>
#include <platform_def.h> /* for PLATFORM_NUM_AFFS */
/*******************************************************************************
* Number of affinity instances whose state this psci imp. can track
******************************************************************************/
#ifdef PLATFORM_NUM_AFFS
#define PSCI_NUM_AFFS PLATFORM_NUM_AFFS
#else
#define PSCI_NUM_AFFS (2 * PLATFORM_CORE_COUNT)
#endif
/******************************************************************************* /*******************************************************************************
* Defines for runtime services func ids * Defines for runtime services func ids
...@@ -140,6 +151,9 @@ typedef struct psci_cpu_data { ...@@ -140,6 +151,9 @@ typedef struct psci_cpu_data {
uint32_t power_state; uint32_t power_state;
uint32_t max_phys_off_afflvl; /* Highest affinity level in physically uint32_t max_phys_off_afflvl; /* Highest affinity level in physically
powered off state */ powered off state */
#if !USE_COHERENT_MEM
bakery_info_t pcpu_bakery_info[PSCI_NUM_AFFS];
#endif
} psci_cpu_data_t; } psci_cpu_data_t;
/******************************************************************************* /*******************************************************************************
......
...@@ -175,6 +175,9 @@ DEFINE_SYSOP_FUNC(wfi) ...@@ -175,6 +175,9 @@ DEFINE_SYSOP_FUNC(wfi)
DEFINE_SYSOP_FUNC(wfe) DEFINE_SYSOP_FUNC(wfe)
DEFINE_SYSOP_FUNC(sev) DEFINE_SYSOP_FUNC(sev)
DEFINE_SYSOP_TYPE_FUNC(dsb, sy) DEFINE_SYSOP_TYPE_FUNC(dsb, sy)
DEFINE_SYSOP_TYPE_FUNC(dmb, sy)
DEFINE_SYSOP_TYPE_FUNC(dsb, ish)
DEFINE_SYSOP_TYPE_FUNC(dmb, ish)
DEFINE_SYSOP_FUNC(isb) DEFINE_SYSOP_FUNC(isb)
uint32_t get_afflvl_shift(uint32_t); uint32_t get_afflvl_shift(uint32_t);
......
...@@ -35,6 +35,11 @@ ...@@ -35,6 +35,11 @@
#define BAKERY_LOCK_MAX_CPUS PLATFORM_CORE_COUNT #define BAKERY_LOCK_MAX_CPUS PLATFORM_CORE_COUNT
#ifndef __ASSEMBLY__
#include <stdint.h>
#if USE_COHERENT_MEM
typedef struct bakery_lock { typedef struct bakery_lock {
int owner; int owner;
volatile char entering[BAKERY_LOCK_MAX_CPUS]; volatile char entering[BAKERY_LOCK_MAX_CPUS];
...@@ -48,4 +53,21 @@ void bakery_lock_get(bakery_lock_t *bakery); ...@@ -48,4 +53,21 @@ void bakery_lock_get(bakery_lock_t *bakery);
void bakery_lock_release(bakery_lock_t *bakery); void bakery_lock_release(bakery_lock_t *bakery);
int bakery_lock_try(bakery_lock_t *bakery); int bakery_lock_try(bakery_lock_t *bakery);
#else
typedef struct bakery_info {
/*
* The lock_data is a bit-field of 2 members:
* Bit[0] : choosing. This field is set when the CPU is
* choosing its bakery number.
* Bits[1 - 15] : number. This is the bakery number allocated.
*/
volatile uint16_t lock_data;
} bakery_info_t;
void bakery_lock_get(unsigned int id, unsigned int offset);
void bakery_lock_release(unsigned int id, unsigned int offset);
#endif /* __USE_COHERENT_MEM__ */
#endif /* __ASSEMBLY__ */
#endif /* __BAKERY_LOCK_H__ */ #endif /* __BAKERY_LOCK_H__ */
...@@ -31,11 +31,13 @@ ...@@ -31,11 +31,13 @@
#include <arch_helpers.h> #include <arch_helpers.h>
#include <assert.h> #include <assert.h>
#include <bakery_lock.h> #include <bakery_lock.h>
#include <cpu_data.h>
#include <platform.h> #include <platform.h>
#include <string.h> #include <string.h>
/* /*
* Functions in this file implement Bakery Algorithm for mutual exclusion. * Functions in this file implement Bakery Algorithm for mutual exclusion with the
* bakery lock data structures in coherent memory.
* *
* ARM architecture offers a family of exclusive access instructions to * ARM architecture offers a family of exclusive access instructions to
* efficiently implement mutual exclusion with hardware support. However, as * efficiently implement mutual exclusion with hardware support. However, as
...@@ -107,8 +109,6 @@ static unsigned int bakery_get_ticket(bakery_lock_t *bakery, unsigned int me) ...@@ -107,8 +109,6 @@ static unsigned int bakery_get_ticket(bakery_lock_t *bakery, unsigned int me)
++my_ticket; ++my_ticket;
bakery->number[me] = my_ticket; bakery->number[me] = my_ticket;
bakery->entering[me] = 0; bakery->entering[me] = 0;
dsb();
sev();
return my_ticket; return my_ticket;
} }
...@@ -151,7 +151,7 @@ void bakery_lock_get(bakery_lock_t *bakery) ...@@ -151,7 +151,7 @@ void bakery_lock_get(bakery_lock_t *bakery)
/* Wait for the contender to get their ticket */ /* Wait for the contender to get their ticket */
while (bakery->entering[they]) while (bakery->entering[they])
wfe(); ;
/* /*
* If the other party is a contender, they'll have non-zero * If the other party is a contender, they'll have non-zero
......
/*
* Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of ARM nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <arch_helpers.h>
#include <assert.h>
#include <bakery_lock.h>
#include <cpu_data.h>
#include <platform.h>
#include <string.h>
/*
* Functions in this file implement Bakery Algorithm for mutual exclusion with the
* bakery lock data structures in cacheable and Normal memory.
*
* ARM architecture offers a family of exclusive access instructions to
* efficiently implement mutual exclusion with hardware support. However, as
* well as depending on external hardware, these instructions have defined
* behavior only on certain memory types (cacheable and Normal memory in
* particular; see ARMv8 Architecture Reference Manual section B2.10). Use cases
* in trusted firmware are such that mutual exclusion implementation cannot
* expect that accesses to the lock have the specific type required by the
* architecture for these primitives to function (for example, not all
* contenders may have address translation enabled).
*
* This implementation does not use mutual exclusion primitives. It expects
* memory regions where the locks reside to be cacheable and Normal.
*
* Note that the ARM architecture guarantees single-copy atomicity for aligned
* accesses regardless of status of address translation.
*/
/* Convert a ticket to priority */
#define PRIORITY(t, pos) (((t) << 8) | (pos))
#define CHOOSING_TICKET 0x1
#define CHOOSING_DONE 0x0
#define bakery_is_choosing(info) (info & 0x1)
#define bakery_ticket_number(info) ((info >> 1) & 0x7FFF)
#define make_bakery_data(choosing, number) \
(((choosing & 0x1) | (number << 1)) & 0xFFFF)
/* This macro assumes that the bakery_info array is located at the offset specified */
#define get_my_bakery_info(offset, id) \
(((bakery_info_t *) (((uint8_t *)_cpu_data()) + offset)) + id)
#define get_bakery_info_by_index(offset, id, ix) \
(((bakery_info_t *) (((uint8_t *)_cpu_data_by_index(ix)) + offset)) + id)
#define write_cache_op(addr, cached) \
do { \
(cached ? dccvac((uint64_t)addr) :\
dcivac((uint64_t)addr));\
dsbish();\
} while (0)
#define read_cache_op(addr, cached) if (cached) \
dccivac((uint64_t)addr)
static unsigned int bakery_get_ticket(int id, unsigned int offset,
unsigned int me, int is_cached)
{
unsigned int my_ticket, their_ticket;
unsigned int they;
bakery_info_t *my_bakery_info, *their_bakery_info;
/*
* Obtain a reference to the bakery information for this cpu and ensure
* it is not NULL.
*/
my_bakery_info = get_my_bakery_info(offset, id);
assert(my_bakery_info);
/*
* Tell other contenders that we are through the bakery doorway i.e.
* going to allocate a ticket for this cpu.
*/
my_ticket = 0;
my_bakery_info->lock_data = make_bakery_data(CHOOSING_TICKET, my_ticket);
write_cache_op(my_bakery_info, is_cached);
/*
* Iterate through the bakery information of each contender to allocate
* the highest ticket number for this cpu.
*/
for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) {
if (me == they)
continue;
/*
* Get a reference to the other contender's bakery info and
* ensure that a stale copy is not read.
*/
their_bakery_info = get_bakery_info_by_index(offset, id, they);
assert(their_bakery_info);
read_cache_op(their_bakery_info, is_cached);
/*
* Update this cpu's ticket number if a higher ticket number is
* seen
*/
their_ticket = bakery_ticket_number(their_bakery_info->lock_data);
if (their_ticket > my_ticket)
my_ticket = their_ticket;
}
/*
* Compute ticket; then signal to other contenders waiting for us to
* finish calculating our ticket value that we're done
*/
++my_ticket;
my_bakery_info->lock_data = make_bakery_data(CHOOSING_DONE, my_ticket);
write_cache_op(my_bakery_info, is_cached);
return my_ticket;
}
void bakery_lock_get(unsigned int id, unsigned int offset)
{
unsigned int they, me, is_cached;
unsigned int my_ticket, my_prio, their_ticket;
bakery_info_t *their_bakery_info;
uint16_t their_bakery_data;
me = platform_get_core_pos(read_mpidr_el1());
is_cached = read_sctlr_el3() & SCTLR_C_BIT;
/* Get a ticket */
my_ticket = bakery_get_ticket(id, offset, me, is_cached);
/*
* Now that we got our ticket, compute our priority value, then compare
* with that of others, and proceed to acquire the lock
*/
my_prio = PRIORITY(my_ticket, me);
for (they = 0; they < BAKERY_LOCK_MAX_CPUS; they++) {
if (me == they)
continue;
/*
* Get a reference to the other contender's bakery info and
* ensure that a stale copy is not read.
*/
their_bakery_info = get_bakery_info_by_index(offset, id, they);
assert(their_bakery_info);
read_cache_op(their_bakery_info, is_cached);
their_bakery_data = their_bakery_info->lock_data;
/* Wait for the contender to get their ticket */
while (bakery_is_choosing(their_bakery_data)) {
read_cache_op(their_bakery_info, is_cached);
their_bakery_data = their_bakery_info->lock_data;
}
/*
* If the other party is a contender, they'll have non-zero
* (valid) ticket value. If they do, compare priorities
*/
their_ticket = bakery_ticket_number(their_bakery_data);
if (their_ticket && (PRIORITY(their_ticket, they) < my_prio)) {
/*
* They have higher priority (lower value). Wait for
* their ticket value to change (either release the lock
* to have it dropped to 0; or drop and probably content
* again for the same lock to have an even higher value)
*/
do {
wfe();
read_cache_op(their_bakery_info, is_cached);
} while (their_ticket
== bakery_ticket_number(their_bakery_info->lock_data));
}
}
}
void bakery_lock_release(unsigned int id, unsigned int offset)
{
bakery_info_t *my_bakery_info;
unsigned int is_cached = read_sctlr_el3() & SCTLR_C_BIT;
my_bakery_info = get_my_bakery_info(offset, id);
my_bakery_info->lock_data = 0;
write_cache_op(my_bakery_info, is_cached);
sev();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment