1*54fd6939SJiyong ParkGranule Protection Tables Library 2*54fd6939SJiyong Park================================= 3*54fd6939SJiyong Park 4*54fd6939SJiyong ParkThis document describes the design of the granule protection tables (GPT) 5*54fd6939SJiyong Parklibrary used by Trusted Firmware-A (TF-A). This library provides the APIs needed 6*54fd6939SJiyong Parkto initialize the GPTs based on a data structure containing information about 7*54fd6939SJiyong Parkthe systems memory layout, configure the system registers to enable granule 8*54fd6939SJiyong Parkprotection checks based on these tables, and transition granules between 9*54fd6939SJiyong Parkdifferent PAS (physical address spaces) at runtime. 10*54fd6939SJiyong Park 11*54fd6939SJiyong ParkArm CCA adds two new security states for a total of four: root, realm, secure, and 12*54fd6939SJiyong Parknon-secure. In addition to new security states, corresponding physical address 13*54fd6939SJiyong Parkspaces have been added to control memory access for each state. The PAS access 14*54fd6939SJiyong Parkallowed to each security state can be seen in the table below. 15*54fd6939SJiyong Park 16*54fd6939SJiyong Park.. list-table:: Security states and PAS access rights 17*54fd6939SJiyong Park :widths: 25 25 25 25 25 18*54fd6939SJiyong Park :header-rows: 1 19*54fd6939SJiyong Park 20*54fd6939SJiyong Park * - 21*54fd6939SJiyong Park - Root state 22*54fd6939SJiyong Park - Realm state 23*54fd6939SJiyong Park - Secure state 24*54fd6939SJiyong Park - Non-secure state 25*54fd6939SJiyong Park * - Root PAS 26*54fd6939SJiyong Park - yes 27*54fd6939SJiyong Park - no 28*54fd6939SJiyong Park - no 29*54fd6939SJiyong Park - no 30*54fd6939SJiyong Park * - Realm PAS 31*54fd6939SJiyong Park - yes 32*54fd6939SJiyong Park - yes 33*54fd6939SJiyong Park - no 34*54fd6939SJiyong Park - no 35*54fd6939SJiyong Park * - Secure PAS 36*54fd6939SJiyong Park - yes 37*54fd6939SJiyong Park - no 38*54fd6939SJiyong Park - yes 39*54fd6939SJiyong Park - no 40*54fd6939SJiyong Park * - Non-secure PAS 41*54fd6939SJiyong Park - yes 42*54fd6939SJiyong Park - yes 43*54fd6939SJiyong Park - yes 44*54fd6939SJiyong Park - yes 45*54fd6939SJiyong Park 46*54fd6939SJiyong ParkThe GPT can function as either a 1 level or 2 level lookup depending on how a 47*54fd6939SJiyong ParkPAS region is configured. The first step is the level 0 table, each entry in the 48*54fd6939SJiyong Parklevel 0 table controls access to a relatively large region in memory (block 49*54fd6939SJiyong Parkdescriptor), and the entire region can belong to a single PAS when a one step 50*54fd6939SJiyong Parkmapping is used, or a level 0 entry can link to a level 1 table where relatively 51*54fd6939SJiyong Parksmall regions (granules) of memory can be assigned to different PAS with a 2 52*54fd6939SJiyong Parkstep mapping. The type of mapping used for each PAS is determined by the user 53*54fd6939SJiyong Parkwhen setting up the configuration structure. 54*54fd6939SJiyong Park 55*54fd6939SJiyong ParkDesign Concepts and Interfaces 56*54fd6939SJiyong Park------------------------------ 57*54fd6939SJiyong Park 58*54fd6939SJiyong ParkThis section covers some important concepts and data structures used in the GPT 59*54fd6939SJiyong Parklibrary. 60*54fd6939SJiyong Park 61*54fd6939SJiyong ParkThere are three main parameters that determine how the tables are organized and 62*54fd6939SJiyong Parkfunction: the PPS (protected physical space) which is the total amount of 63*54fd6939SJiyong Parkprotected physical address space in the system, PGS (physical granule size) 64*54fd6939SJiyong Parkwhich is how large each level 1 granule is, and L0GPTSZ (level 0 GPT size) which 65*54fd6939SJiyong Parkdetermines how much physical memory is governed by each level 0 entry. A granule 66*54fd6939SJiyong Parkis the smallest unit of memory that can be independently assigned to a PAS. 67*54fd6939SJiyong Park 68*54fd6939SJiyong ParkL0GPTSZ is determined by the hardware and is read from the GPCCR_EL3 register. 69*54fd6939SJiyong ParkPPS and PGS are passed into the APIs at runtime and can be determined in 70*54fd6939SJiyong Parkwhatever way is best for a given platform, either through some algorithm or hard 71*54fd6939SJiyong Parkcoded in the firmware. 72*54fd6939SJiyong Park 73*54fd6939SJiyong ParkGPT setup is split into two parts: table creation and runtime initialization. In 74*54fd6939SJiyong Parkthe table creation step, a data structure containing information about the 75*54fd6939SJiyong Parkdesired PAS regions is passed into the library which validates the mappings, 76*54fd6939SJiyong Parkcreates the tables in memory, and enables granule protection checks. In the 77*54fd6939SJiyong Parkruntime initialization step, the runtime firmware locates the existing tables in 78*54fd6939SJiyong Parkmemory using the GPT register configuration and saves important data to a 79*54fd6939SJiyong Parkstructure used by the granule transition service which will be covered more 80*54fd6939SJiyong Parkbelow. 81*54fd6939SJiyong Park 82*54fd6939SJiyong ParkIn the reference implementation for FVP models, you can find an example of PAS 83*54fd6939SJiyong Parkregion definitions in the file ``include/plat/arm/common/arm_pas_def.h``. Table 84*54fd6939SJiyong Parkcreation API calls can be found in ``plat/arm/common/arm_bl2_setup.c`` and 85*54fd6939SJiyong Parkruntime initialization API calls can be seen in 86*54fd6939SJiyong Park``plat/arm/common/arm_bl31_setup.c``. 87*54fd6939SJiyong Park 88*54fd6939SJiyong ParkDefining PAS regions 89*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~ 90*54fd6939SJiyong Park 91*54fd6939SJiyong ParkA ``pas_region_t`` structure is a way to represent a physical address space and 92*54fd6939SJiyong Parkits attributes that can be used by the GPT library to initialize the tables. 93*54fd6939SJiyong Park 94*54fd6939SJiyong ParkThis structure is composed of the following: 95*54fd6939SJiyong Park 96*54fd6939SJiyong Park#. The base physical address 97*54fd6939SJiyong Park#. The region size 98*54fd6939SJiyong Park#. The desired attributes of this memory region (mapping type, PAS type) 99*54fd6939SJiyong Park 100*54fd6939SJiyong ParkSee the ``pas_region_t`` type in ``include/lib/gpt_rme/gpt_rme.h``. 101*54fd6939SJiyong Park 102*54fd6939SJiyong ParkThe programmer should provide the API with an array containing ``pas_region_t`` 103*54fd6939SJiyong Parkstructures, then the library will check the desired memory access layout for 104*54fd6939SJiyong Parkvalidity and create tables to implement it. 105*54fd6939SJiyong Park 106*54fd6939SJiyong Park``pas_region_t`` is a public type, however it is recommended that the macros 107*54fd6939SJiyong Park``GPT_MAP_REGION_BLOCK`` and ``GPT_MAP_REGION_GRANULE`` be used to populate 108*54fd6939SJiyong Parkthese structures instead of doing it manually to reduce the risk of future 109*54fd6939SJiyong Parkcompatibility issues. These macros take the base physical address, region size, 110*54fd6939SJiyong Parkand PAS type as arguments to generate the pas_region_t structure. As the names 111*54fd6939SJiyong Parkimply, ``GPT_MAP_REGION_BLOCK`` creates a region using only L0 mapping while 112*54fd6939SJiyong Park``GPT_MAP_REGION_GRANULE`` creates a region using L0 and L1 mappings. 113*54fd6939SJiyong Park 114*54fd6939SJiyong ParkLevel 0 and Level 1 Tables 115*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~ 116*54fd6939SJiyong Park 117*54fd6939SJiyong ParkThe GPT initialization APIs require memory to be passed in for the tables to be 118*54fd6939SJiyong Parkconstructed, ``gpt_init_l0_tables`` takes a memory address and size for building 119*54fd6939SJiyong Parkthe level 0 tables and ``gpt_init_pas_l1_tables`` takes an address and size for 120*54fd6939SJiyong Parkbuilding the level 1 tables which are linked from level 0 descriptors. The 121*54fd6939SJiyong Parktables should have PAS type ``GPT_GPI_ROOT`` and a typical system might place 122*54fd6939SJiyong Parkits level 0 table in SRAM and its level 1 table(s) in DRAM. 123*54fd6939SJiyong Park 124*54fd6939SJiyong ParkGranule Transition Service 125*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~ 126*54fd6939SJiyong Park 127*54fd6939SJiyong ParkThe Granule Transition Service allows memory mapped with GPT_MAP_REGION_GRANULE 128*54fd6939SJiyong Parkownership to be changed using SMC calls. Non-secure granules can be transitioned 129*54fd6939SJiyong Parkto either realm or secure space, and realm and secure granules can be 130*54fd6939SJiyong Parktransitioned back to non-secure. This library only allows memory mapped as 131*54fd6939SJiyong Parkgranules to be transitioned, memory mapped as blocks have their GPIs fixed after 132*54fd6939SJiyong Parktable creation. 133*54fd6939SJiyong Park 134*54fd6939SJiyong ParkLibrary APIs 135*54fd6939SJiyong Park------------ 136*54fd6939SJiyong Park 137*54fd6939SJiyong ParkThe public APIs and types can be found in ``include/lib/gpt_rme/gpt_rme.h`` and this 138*54fd6939SJiyong Parksection is intended to provide additional details and clarifications. 139*54fd6939SJiyong Park 140*54fd6939SJiyong ParkTo create the GPTs and enable granule protection checks the APIs need to be 141*54fd6939SJiyong Parkcalled in the correct order and at the correct time during the system boot 142*54fd6939SJiyong Parkprocess. 143*54fd6939SJiyong Park 144*54fd6939SJiyong Park#. Firmware must enable the MMU. 145*54fd6939SJiyong Park#. Firmware must call ``gpt_init_l0_tables`` to initialize the level 0 tables to 146*54fd6939SJiyong Park a default state, that is, initializing all of the L0 descriptors to allow all 147*54fd6939SJiyong Park accesses to all memory. The PPS is provided to this function as an argument. 148*54fd6939SJiyong Park#. DDR discovery and initialization by the system, the discovered DDR region(s) 149*54fd6939SJiyong Park are then added to the L1 PAS regions to be initialized in the next step and 150*54fd6939SJiyong Park used by the GTSI at runtime. 151*54fd6939SJiyong Park#. Firmware must call ``gpt_init_pas_l1_tables`` with a pointer to an array of 152*54fd6939SJiyong Park ``pas_region_t`` structures containing the desired memory access layout. The 153*54fd6939SJiyong Park PGS is provided to this function as an argument. 154*54fd6939SJiyong Park#. Firmware must call ``gpt_enable`` to enable granule protection checks by 155*54fd6939SJiyong Park setting the correct register values. 156*54fd6939SJiyong Park#. In systems that make use of the granule transition service, runtime 157*54fd6939SJiyong Park firmware must call ``gpt_runtime_init`` to set up the data structures needed 158*54fd6939SJiyong Park by the GTSI to find the tables and transition granules between PAS types. 159*54fd6939SJiyong Park 160*54fd6939SJiyong ParkAPI Constraints 161*54fd6939SJiyong Park~~~~~~~~~~~~~~~ 162*54fd6939SJiyong Park 163*54fd6939SJiyong ParkThe values allowed by the API for PPS and PGS are enumerated types 164*54fd6939SJiyong Parkdefined in the file ``include/lib/gpt_rme/gpt_rme.h``. 165*54fd6939SJiyong Park 166*54fd6939SJiyong ParkAllowable values for PPS along with their corresponding size. 167*54fd6939SJiyong Park 168*54fd6939SJiyong Park* ``GPCCR_PPS_4GB`` (4GB protected space, 0x100000000 bytes) 169*54fd6939SJiyong Park* ``GPCCR_PPS_64GB`` (64GB protected space, 0x1000000000 bytes) 170*54fd6939SJiyong Park* ``GPCCR_PPS_1TB`` (1TB protected space, 0x10000000000 bytes) 171*54fd6939SJiyong Park* ``GPCCR_PPS_4TB`` (4TB protected space, 0x40000000000 bytes) 172*54fd6939SJiyong Park* ``GPCCR_PPS_16TB`` (16TB protected space, 0x100000000000 bytes) 173*54fd6939SJiyong Park* ``GPCCR_PPS_256TB`` (256TB protected space, 0x1000000000000 bytes) 174*54fd6939SJiyong Park* ``GPCCR_PPS_4PB`` (4PB protected space, 0x10000000000000 bytes) 175*54fd6939SJiyong Park 176*54fd6939SJiyong ParkAllowable values for PGS along with their corresponding size. 177*54fd6939SJiyong Park 178*54fd6939SJiyong Park* ``GPCCR_PGS_4K`` (4KB granules, 0x1000 bytes) 179*54fd6939SJiyong Park* ``GPCCR_PGS_16K`` (16KB granules, 0x4000 bytes) 180*54fd6939SJiyong Park* ``GPCCR_PGS_64K`` (64KB granules, 0x10000 bytes) 181*54fd6939SJiyong Park 182*54fd6939SJiyong ParkAllowable values for L0GPTSZ along with the corresponding size. 183*54fd6939SJiyong Park 184*54fd6939SJiyong Park* ``GPCCR_L0GPTSZ_30BITS`` (1GB regions, 0x40000000 bytes) 185*54fd6939SJiyong Park* ``GPCCR_L0GPTSZ_34BITS`` (16GB regions, 0x400000000 bytes) 186*54fd6939SJiyong Park* ``GPCCR_L0GPTSZ_36BITS`` (64GB regions, 0x1000000000 bytes) 187*54fd6939SJiyong Park* ``GPCCR_L0GPTSZ_39BITS`` (512GB regions, 0x8000000000 bytes) 188*54fd6939SJiyong Park 189*54fd6939SJiyong ParkNote that the value of the PPS, PGS, and L0GPTSZ definitions is an encoded value 190*54fd6939SJiyong Parkcorresponding to the size, not the size itself. The decoded hex representations 191*54fd6939SJiyong Parkof the sizes have been provided for convenience. 192*54fd6939SJiyong Park 193*54fd6939SJiyong ParkThe L0 table memory has some constraints that must be taken into account. 194*54fd6939SJiyong Park 195*54fd6939SJiyong Park* The L0 table must be aligned to either the table size or 4096 bytes, whichever 196*54fd6939SJiyong Park is greater. L0 table size is the total protected space (PPS) divided by the 197*54fd6939SJiyong Park size of each L0 region (L0GPTSZ) multiplied by the size of each L0 descriptor 198*54fd6939SJiyong Park (8 bytes). ((PPS / L0GPTSZ) * 8) 199*54fd6939SJiyong Park* The L0 memory size must be greater than or equal to the table size. 200*54fd6939SJiyong Park* The L0 memory must fall within a PAS of type GPT_GPI_ROOT. 201*54fd6939SJiyong Park 202*54fd6939SJiyong ParkThe L1 memory also has some constraints. 203*54fd6939SJiyong Park 204*54fd6939SJiyong Park* The L1 tables must be aligned to their size. The size of each L1 table is the 205*54fd6939SJiyong Park size of each L0 region (L0GPTSZ) divided by the granule size (PGS) divided by 206*54fd6939SJiyong Park the granules controlled in each byte (2). ((L0GPTSZ / PGS) / 2) 207*54fd6939SJiyong Park* There must be enough L1 memory supplied to build all requested L1 tables. 208*54fd6939SJiyong Park* The L1 memory must fall within a PAS of type GPT_GPI_ROOT. 209*54fd6939SJiyong Park 210*54fd6939SJiyong ParkIf an invalid combination of parameters is supplied, the APIs will print an 211*54fd6939SJiyong Parkerror message and return a negative value. The return values of APIs should be 212*54fd6939SJiyong Parkchecked to ensure successful configuration. 213*54fd6939SJiyong Park 214*54fd6939SJiyong ParkSample Calculation for L0 memory size and alignment 215*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 216*54fd6939SJiyong Park 217*54fd6939SJiyong ParkLet PPS=GPCCR_PPS_4GB and L0GPTSZ=GPCCR_L0GPTSZ_30BITS 218*54fd6939SJiyong Park 219*54fd6939SJiyong ParkWe can find the total L0 table size with ((PPS / L0GPTSZ) * 8) 220*54fd6939SJiyong Park 221*54fd6939SJiyong ParkSubstitute values to get this: ((0x100000000 / 0x40000000) * 8) 222*54fd6939SJiyong Park 223*54fd6939SJiyong ParkAnd solve to get 32 bytes. In this case, 4096 is greater than 32, so the L0 224*54fd6939SJiyong Parktables must be aligned to 4096 bytes. 225*54fd6939SJiyong Park 226*54fd6939SJiyong ParkSample calculation for L1 table size and alignment 227*54fd6939SJiyong Park~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 228*54fd6939SJiyong Park 229*54fd6939SJiyong ParkLet PGS=GPCCR_PGS_4K and L0GPTSZ=GPCCR_L0GPTSZ_30BITS 230*54fd6939SJiyong Park 231*54fd6939SJiyong ParkWe can find the size of each L1 table with ((L0GPTSZ / PGS) / 2). 232*54fd6939SJiyong Park 233*54fd6939SJiyong ParkSubstitute values: ((0x40000000 / 0x1000) / 2) 234*54fd6939SJiyong Park 235*54fd6939SJiyong ParkAnd solve to get 0x20000 bytes per L1 table. 236