blob: 18067df555ebfc2b53f72450aed14e3f1faf93eb [file] [log] [blame]
Edison Ai75afd372020-04-21 18:29:57 +08001##########################
2Hardware Abstraction Layer
3##########################
4
5:Organization: Arm Limited
6:Contact: tf-m@lists.trustedfirmware.org
7
8:API Version: 0.9
9
10************
11Introduction
12************
13:term:`TF-M` :term:`HAL` abstracts the hardware-oriented and platform specific
14operations on the :term:`SPE` side and provides a set of APIs to the upper
15layers such as :term:`SPM`, :term:`RoT Service`.
16The :term:`HAL` aims to cover the platform different aspects whereas common
17architecturally defined aspects are done generically within the common
18:term:`SPE`.
19In some cases, although the operations are defined architecturally,
20it may not be possible to generalize implementations because lots of information
21is only known to platforms.
22It is more efficient to define a :term:`HAL` API for those architectural
23operations as well.
24
25.. note::
26 :term:`TBSA-M` provides the hardware requirements for security purposes.
27 :term:`TF-M` :term:`HAL` tries to reference :term:`TBSA-M` recommendations in
28 the interfaces from the software perspective only. Please reference
29 :term:`TBSA-M` for your security hardware design.
30
31Design Goals
32============
33:term:`TF-M` :term:`HAL` is designed to simplify the integration efforts on
34different platforms.
35
36:term:`TF-M` :term:`HAL` is designed to make it easy to use the hardware and
37develop the :term:`SPM` and :term:`RoT Service` which need to access the
38devices.
39
40:term:`TF-M` :term:`HAL` is designed to make the structure clearer and let the
41:term:`TF-M` mainly focus on :term:`PSA` implementation.
42
43********
44Overview
45********
46This section provides an overview of the abstraction layer structure.
47
48.. figure:: media/hal_structure.png
49
50Here lists a minimal set of necessary functionalities:
51
52 - **Isolation API**: Provides the necessary isolation functionalities required
53 by the :term:`PSA-FF-M` and :term:`TBSA-M`, and provides APIs to :term:`SPM`
54 to check the permissions of memory access.
55 - **Platform API**: Provides the platform initialization, platform-specific
56 memory information, system reset, etc.
Edison Ai75afd372020-04-21 18:29:57 +080057 - **Log dev API**: Provides the log system functions.
58 - **Interrupt API**: Provides the interrupt functions.
59
60.. note::
61 - There is a non-secure :term:`HAL` that focuses on the mailbox operation API
62 for Dual-core topology. For more information about it, please refer to
63 :doc:`Mailbox Design in TF-M on Dual-core System
64 </docs/design_documents/dual-cpu/mailbox_design_on_dual_core_system>`.
65 - The minimal set of :term:`TF-M` :term:`HAL` is sufficient for Secure
66 Partitions by using customized peripheral interfaces. To provide easier
67 portability for the Secure Partitions, a Secure Partition :term:`HAL` is
68 provided in this design too.
69 - The debug mechanisms give the external entity the corresponding right to
70 access the system assets. :term:`TF-M` ensures that the external entity is
71 permitted access to those assets. Currently, :term:`TF-M` only needs the
72 debug authentication. The whole debug mechanism and related :term:`HAL` will
73 be enhanced in the future. Please refer to the :doc:`Debug authentication
74 settings section </platform/readme>` for more detail.
75
76*****************
77Design Principles
78*****************
79As :term:`TF-M` runs on resource-constrained devices, the :term:`HAL` tries to
80avoid multiple level abstractions which cost more resources.
81
82Part of the :term:`HAL` interfaces does not focus on exact hardware operations
83such as power on/off or PIN manipulation.
84Instead, the :term:`HAL` abstracts higher-level interfaces to reserve the
85implementation flexibility for the platform vendors.
86
87The :term:`TF-M` :term:`HAL` should be easy to deprecate APIs and provide
88compatibilities.
89Any API incompatibility should be detected during building.
90
91:term:`TF-M` relies on the :term:`HAL` APIs to be implemented correctly and
92trusts the :term:`HAL` APIs.
93:term:`TFM` can provide assertions to detect common programming errors but
94essentially no further extensive checks will be provided.
95
96************
97Source Files
98************
99This section describes the source file of the :term:`TF-M` :term:`HAL`,
100including the header and c files.
101
102tfm_hal_defs.h
103==============
104This header file contains the definitions of common macros and types used by all
105:term:`HAL` APIs. Please refer to `Status Codes`_ for detailed definitions.
106
107tfm_hal_[module].[h/c]
108======================
109All other headers and c files are classified by the modules, such as isolation,
110platform, interrupt, devices, etc.
111
112.. note::
113 There are common files in the platform folder include the implemented
114 :term:`HAL` APIs. The platform vendors can use them directly but need to
115 implement all the sub APIs.
116
117************
118Status Codes
119************
120These are common status and error codes for all :term:`HAL` APIs.
121
122Types
123=====
124tfm_hal_status_t
125----------------
126This is a status code to be used as the return type of :term:`HAL` APIs.
127
128.. code-block:: c
129
Summer Qin2ecfc9f2020-08-20 14:20:27 +0800130 enum tfm_hal_status_t {
Mingyang Sun45c6c202020-09-23 16:17:50 +0800131 TFM_HAL_ERROR_MEM_FAULT = SCHAR_MIN,
Summer Qin2ecfc9f2020-08-20 14:20:27 +0800132 TFM_HAL_ERROR_MAX_VALUE,
133 TFM_HAL_ERROR_BAD_STATE,
134 TFM_HAL_ERROR_NOT_SUPPORTED,
135 TFM_HAL_ERROR_INVALID_INPUT,
136 TFM_HAL_ERROR_NOT_INIT,
137 TFM_HAL_ERROR_GENERIC,
138 TFM_HAL_SUCCESS = 0
139 };
Edison Ai75afd372020-04-21 18:29:57 +0800140
141Error Codes
142===========
143Negative values indicate an error. Zero and positive values indicate success.
144
145Here is the general list. The detailed usages for each error code are described
146in the API introduction sections.
147
148TFM_HAL_SUCCESS
149---------------
150Status code to indicate general success.
151
Edison Ai75afd372020-04-21 18:29:57 +0800152TFM_HAL_ERROR_GENERIC
153---------------------
154Status code to indicate an error that does not correspond to any defined failure
155cause.
156
Edison Ai75afd372020-04-21 18:29:57 +0800157TFM_HAL_ERROR_NOT_INIT
158----------------------
159Status code to indicate that the module is not initialed.
160
Edison Ai75afd372020-04-21 18:29:57 +0800161TFM_HAL_ERROR_INVALID_INPUT
162---------------------------
163Status code to indicate that the input is invalid.
164
Edison Ai75afd372020-04-21 18:29:57 +0800165TFM_HAL_ERROR_NOT_SUPPORTED
166---------------------------
167Status code to indicate that the requested operation or a parameter is not
168supported.
169
Edison Ai75afd372020-04-21 18:29:57 +0800170TFM_HAL_ERROR_BAD_STATE
171-----------------------
172Status code to indicate that the requested action cannot be performed in the
173current state.
174
Edison Ai75afd372020-04-21 18:29:57 +0800175TFM_HAL_ERROR_MAX_VALUE
176-----------------------
177Status code to indicate that the current number has got the max value.
178
Edison Ai75afd372020-04-21 18:29:57 +0800179TFM_HAL_ERROR_MEM_FAULT
180-----------------------
181Status code to indicate that the memory check failed.
182
Edison Aif4b61132020-07-01 18:35:17 +0800183***************************
184API Definition for TF-M SPM
185***************************
186This section describes the APIs defined for :term:`TF-M` :term:`SPM`.
187
edison.ai37e0f602020-07-13 18:30:58 +0800188Isolation API
189=============
190The :term:`PSA-FF-M` defines three isolation levels and a memory access rule to
191provide diverse levels of securitiy. The isolation API provides the functions to
192implement these requirements.
193
194Memory Access Attributes
195------------------------
196The memory access attributes are encoded as bit fields, you can logic OR them to
197have a combination of the atrributes, for example
198``TFM_HAL_MEM_ATTR_UNPRIVILEGED | TFM_HAL_MEM_ATTR_READABLE`` is unprivileged
199readable. The data type is `uint32_t`.
200
201TFM_HAL_MEM_ATTR_EXECUTABLE
202^^^^^^^^^^^^^^^^^^^^^^^^^^^
203The memory is executable.
204
205.. code-block:: c
206
207 #define TFM_HAL_MEM_ATTR_EXECUTABLE (1UL << 0)
208
209TFM_HAL_MEM_ATTR_READABLE
210^^^^^^^^^^^^^^^^^^^^^^^^^
211The memory is readable.
212
213.. code-block:: c
214
215 #define TFM_HAL_MEM_ATTR_READABLE (1UL << 1)
216
217TFM_HAL_MEM_ATTR_WRITABLE
218^^^^^^^^^^^^^^^^^^^^^^^^^
219The memory is writable.
220
221.. code-block:: c
222
223 #define TFM_HAL_MEM_ATTR_WRITABLE (1UL << 2)
224
225TFM_HAL_MEM_ATTR_UNPRIVILEGED
226^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
227The memory is unprivileged mode accessible.
228
229.. code-block:: c
230
231 #define TFM_HAL_MEM_ATTR_UNPRIVILEGED (1UL << 3)
232
233TFM_HAL_MEM_ATTR_DEVICE
234^^^^^^^^^^^^^^^^^^^^^^^
235The memory is a MMIO device.
236
237.. code-block:: c
238
239 #define TFM_HAL_MEM_ATTR_DEVICE (1UL << 4)
240
241TFM_HAL_MEM_ATTR_NS
242^^^^^^^^^^^^^^^^^^^
243The memory is accessible from :term:`NSPE`
244
245.. code-block:: c
246
247 #define TFM_HAL_MEM_ATTR_NS (1UL << 5)
248
249APIs
250----
251tfm_hal_set_up_static_boundaries()
252^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
253**Prototype**
254
255.. code-block:: c
256
257 tfm_hal_status_t tfm_hal_set_up_static_boundaries(void)
258
259**Description**
260
261This API sets up the static isolation boundaries which are constant throughout
262the runtime of the system.
263
264The boundaries include:
265
266- The SPE boundary between the :term:`SPE` and the :term:`NSPE`
267- The PSA RoT isolation boundary between the PSA Root of Trust and the
268 Application Root of Trust which is for isolation level 2 and 3 only.
269
270Please refer to the :term:`PSA-FF-M` for the definitions of the isolation
271boundaries.
272
273**Return Values**
274
275- ``TFM_HAL_SUCCESS`` - the isolation boundaries have been set up.
276- ``TFM_HAL_ERROR_GENERIC`` - failed to set up the isolation boundaries.
277
278tfm_hal_mpu_update_partition_boundary
279^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
280**Prototype**
281
282.. code-block:: c
283
284 enum tfm_hal_status_t tfm_hal_mpu_update_partition_boundary(uintptr_t start,
285 uintptr_t end);
286
287**Description**
288
289This API updates the partition isolation boundary for isolation level 3.
290Inside the partition isolation boundary is the private data of the running
291Secure Partition.
292This boundary is updated dynamically when :term:`SPM` switches Partitions in
293isolation level 3.
294
295The access permissions of the boundary is all privileged mode read-write.
296
297Platforms decide which :term:`MPU` region the paritition boundary uses.
298
299**Parameter**
300
301- ``start`` - start address of the partition boundary.
302- ``end`` - end address of the partition boundary.
303
304**Return Values**
305
306- ``TFM_HAL_SUCCESS`` - the isolation boundary has been set up.
307- ``TFM_HAL_ERROR_GENERIC`` - failed to set upthe isolation boundary.
308
309**Note**
310
311This API is only for platforms using :term:`MPU` as isolation hardwares.
312A generic API for all platforms will be introduced in future versions.
313
314tfm_hal_memory_has_access()
315^^^^^^^^^^^^^^^^^^^^^^^^^^^
316**Prototype**
317
318.. code-block:: c
319
320 tfm_hal_status_t tfm_hal_memory_has_access(const uintptr_t base,
321 size_t size,
322 uint32_t attr)
323
324**Description**
325
326This API checks if the memory region defined by ``base`` and ``size`` has the
327given access atrributes - ``attr``.
328
329The Attributes include :term:`NSPE` access, privileged mode, and read-write
330permissions.
331
332**Parameter**
333
334- ``base`` - The base address of the region.
335- ``size`` - The size of the region.
336- ``attr`` - The `Memory Access Attributes`_.
337
338**Return Values**
339
340- ``TFM_HAL_SUCCESS`` - The memory region has the access permissions.
341- ``TFM_HAL_ERROR_MEM_FAULT`` - The memory region does not have the access
342 permissions.
343- ``TFM_HAL_ERROR_INVALID_INPUT`` - Invalid inputs.
344- ``TFM_HAL_ERROR_GENERIC`` - An error occurred.
345
Edison Aif4b61132020-07-01 18:35:17 +0800346Log API
347=======
348The log API is used by the :term:`TF-M` :doc:`log system <tfm_log_system_design_document>`.
349The log device could be uart, memory, usb, etc.
350
351APIs
352----
353tfm_hal_output_partition_log()
354^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
355**Prototype**
356
357.. code-block:: c
358
359 int32_t tfm_hal_output_partition_log(const unsigned char *str, uint32_t len)
360
361**Description**
362
363This API is called by Secure Partition to output logs.
364
365**Parameter**
366
367- ``str`` - The string to output.
368- ``len`` - Length of the string in bytes.
369
370**Return Values**
371
372- Positive values - Number of bytes output.
373- ``TFM_HAL_ERROR_NOT_INIT`` - The log device has not been initialized.
374- ``TFM_HAL_ERROR_INVALID_INPUT`` - Invalid inputs when ``str`` is ``NULL`` or
375 ``len`` is zero.
376
377**Note**
378
379None.
380
381tfm_hal_output_spm_log()
382^^^^^^^^^^^^^^^^^^^^^^^^
383**Prototype**
384
385.. code-block:: c
386
387 int32_t tfm_hal_output_spm_log(const unsigned char *str, uint32_t len)
388
389**Description**
390
391This API is called by :term:`SPM` to output logs.
392
393**Parameter**
394
395- ``str`` - The string to output.
396- ``len`` - Length of the string in bytes.
397
398**Return Values**
399
400- Positive numbers - Number of bytes output.
401- ``TFM_HAL_ERROR_NOT_INIT`` - The log device has not been initialized.
402- ``TFM_HAL_ERROR_INVALID_INPUT`` - Invalid inputs when ``str`` is ``NULL``
403 or ``len`` is zero.
404
405**Note**
406
407Please check :doc:`TF-M log system <tfm_log_system_design_document>` for more
408information.
409
Edison Ai75afd372020-04-21 18:29:57 +0800410--------------
411
412*Copyright (c) 2020, Arm Limited. All rights reserved.*