|
| 1 | +.. _zephyr-api-integration: |
| 2 | + |
| 3 | +Zephyr API Integration |
| 4 | +###################### |
| 5 | + |
| 6 | +Most of the interfaces between the application (audio) layer and the kernel are |
| 7 | +aggregated inside the part of the legacy SOF architecture called "lib". The |
| 8 | +interfaces are exposed by the *lib*, declared in header files in |
| 9 | +*src/include/sof/lib* directory. Implementation is located in the *src/lib* |
| 10 | +except for platform and architecture specific functions that are delegated to |
| 11 | +*platform* and *arch* parts respectively. |
| 12 | + |
| 13 | +.. uml:: images/sof_lib.pu |
| 14 | + :caption: Legacy SOF Lib |
| 15 | + |
| 16 | +Zephyr replaces *lib* and other architecture and platform specific code, |
| 17 | +everything below the *app* & *mpp* layers. |
| 18 | + |
| 19 | +In order to unify the access to the lower parts from the *app* and *mpp*, the |
| 20 | +library header files provides now a definition of unified interface but some |
| 21 | +changes are introduced to the original set of APIs and/or the implementation. |
| 22 | + |
| 23 | +Let's have a look at possible cases. |
| 24 | + |
| 25 | +**Case #1: New Zephyr API replaces 1:1 legacy SOF lib API** |
| 26 | + |
| 27 | +If there is a Zephyr version of a SOF legacy API which provides exactly the same |
| 28 | +functionality as the original function but has a different name, the Zephyr |
| 29 | +function name is used as a replacement in the SOF *app* and *mpp* code. It |
| 30 | +causes direct linking and call into the Zephyr code optimizing FW size and |
| 31 | +performance when SOF is built with Zephyr. Building with legacy SOF *lib* |
| 32 | +requires an implementation or just a simple adapter for the new Zephyr API. It |
| 33 | +may or may not slightly increase the size and decrease the performance of the |
| 34 | +legacy SOF. |
| 35 | + |
| 36 | +.. code-block:: c |
| 37 | +
|
| 38 | + // src/include/sof/lib.cpu.h |
| 39 | + #ifdef __ZEPHYR__ |
| 40 | + #include <zephyr/arch/cpu.h> |
| 41 | + #else |
| 42 | + // was: static inline int cpu_is_core_enabled(int id) |
| 43 | + static inline bool arch_cpu_active(int id) |
| 44 | + { |
| 45 | + arch_cpu_is_core_enabled(id); |
| 46 | + } |
| 47 | + #endif /* __ZEPHYR__ */ |
| 48 | +
|
| 49 | +**Case #2: Legacy SOF lib API requires multi-step implementation for Zephyr |
| 50 | +configuration** |
| 51 | + |
| 52 | +There may be a case when SOF legacy API is implemented by a single function |
| 53 | +provided by the *arch* or another package and there is no 1:1 API available in |
| 54 | +Zephyr to replace that. In this case, the API is implemented in the *lib-zephyr* |
| 55 | +part based on the native Zephyr APIs. |
| 56 | + |
| 57 | +.. code-block:: c |
| 58 | +
|
| 59 | + // src/include/sof/lib/cpu.h |
| 60 | + #ifdef __ZEPHYR__ |
| 61 | + void cpu_disable_core(int id); |
| 62 | + #else |
| 63 | + static inline void cpu_disable_core(int id) |
| 64 | + { |
| 65 | + arch_cpu_disable_core(id); |
| 66 | + } |
| 67 | + #endif /* __ZEPHYR__ */ |
| 68 | +
|
| 69 | + // src/lib-zephyr/cpu.c |
| 70 | + void cpu_disable_core(int id) |
| 71 | + { |
| 72 | + // ... calls to Zephyr APIs |
| 73 | + } |
| 74 | +
|
| 75 | +**Case #3: Legacy SOF lib API is implemented completely inside the lib and does |
| 76 | +not have any replacement in Zephyr** |
| 77 | + |
| 78 | +The agent code might be an example of the library functions that are common and |
| 79 | +must be compiled and linked together with either legacy SOF *lib* or |
| 80 | +*lib-zephyr*. |
| 81 | + |
| 82 | +The dependencies between the SOF *lib*, *lib-zephyr*, and *zephyr* are |
| 83 | +illustrated in the below figure. |
| 84 | + |
| 85 | +.. uml:: images/sof_lib_zephyr.pu |
| 86 | + :caption: SOF Lib + Zephyr |
0 commit comments