1 /* SPDX-License-Identifier: BSD-3-Clause
2 *
3 * Copyright(c) 2016 Intel Corporation. All rights reserved.
4 *
5 * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
6 * Keyon Jie <yang.jie@linux.intel.com>
7 */
8
9 /**
10 * \file include/sof/audio/component.h
11 * \brief Component API definition
12 * \author Liam Girdwood <liam.r.girdwood@linux.intel.com>
13 * \author Keyon Jie <yang.jie@linux.intel.com>
14 */
15
16 #ifndef __SOF_AUDIO_COMPONENT_H__
17 #define __SOF_AUDIO_COMPONENT_H__
18
19 #include <sof/audio/buffer.h>
20 #include <sof/audio/format.h>
21 #include <sof/audio/pipeline.h>
22 #include <rtos/panic.h>
23 #include <rtos/idc.h>
24 #include <sof/list.h>
25 #include <rtos/alloc.h>
26 #include <sof/lib/cpu.h>
27 #include <sof/lib/dai.h>
28 #include <sof/lib/memory.h>
29 #include <sof/lib/perf_cnt.h>
30 #include <sof/math/numbers.h>
31 #include <sof/schedule/schedule.h>
32 #include <rtos/sof.h>
33 #include <sof/trace/trace.h>
34 #include <ipc/control.h>
35 #include <ipc/stream.h>
36 #include <sof/ipc/topology.h>
37 #include <kernel/abi.h>
38 #include <user/trace.h>
39
40 #include <errno.h>
41 #include <stdbool.h>
42 #include <stddef.h>
43 #include <stdint.h>
44
45 struct comp_dev;
46 struct sof_ipc_stream_posn;
47 struct dai_hw_params;
48 struct timestamp_data;
49
50 /** \addtogroup component_api Component API
51 * @{
52 */
53
54 /* NOTE: Keep the component state diagram up to date:
55 * sof-docs/developer_guides/firmware/components/images/comp-dev-states.pu
56 */
57
58 /** \name Audio Component States
59 * @{
60 */
61 #define COMP_STATE_INIT 0 /**< Component being initialised */
62 #define COMP_STATE_READY 1 /**< Component inactive, but ready */
63 #define COMP_STATE_SUSPEND 2 /**< Component suspended */
64 #define COMP_STATE_PREPARE 3 /**< Component prepared */
65 #define COMP_STATE_PAUSED 4 /**< Component paused */
66 #define COMP_STATE_ACTIVE 5 /**< Component active */
67 #define COMP_STATE_PRE_ACTIVE 6 /**< Component after early initialisation */
68 /** @}*/
69
70 /** \name Standard Component Stream Commands
71 * TODO: use IPC versions after 1.1
72 *
73 * Most component stream commands match one-to-one IPC stream trigger commands.
74 * However we add two PRE_ and two POST_ commands to the set. They are issued
75 * internally without matching IPC commands. A single START IPC command is
76 * translated into a sequence of PRE_START and START component commands, etc.
77 * POST_* commands aren't used so far.
78 *
79 * @{
80 */
81 enum {
82 COMP_TRIGGER_STOP, /**< Stop component stream */
83 COMP_TRIGGER_START, /**< Start component stream */
84 COMP_TRIGGER_PAUSE, /**< Pause the component stream */
85 COMP_TRIGGER_RELEASE, /**< Release paused component stream */
86 COMP_TRIGGER_RESET, /**< Reset component */
87 COMP_TRIGGER_PREPARE, /**< Prepare component */
88 COMP_TRIGGER_XRUN, /**< XRUN component */
89 COMP_TRIGGER_PRE_START, /**< Prepare to start component stream */
90 COMP_TRIGGER_PRE_RELEASE, /**< Prepare to release paused component stream */
91 COMP_TRIGGER_POST_STOP, /**< Finalize stop component stream */
92 COMP_TRIGGER_POST_PAUSE, /**< Finalize pause component stream */
93 COMP_TRIGGER_NO_ACTION, /**< No action required */
94 };
95 /** @}*/
96
97 /** \name Standard Component Control Commands
98 * "Value" commands are standard ones, known to the driver while
99 * "Data" commands are opaque blobs transferred by the driver.
100 *
101 * TODO: see also: ref to ipc data structures for commands.
102 * @{
103 */
104 #define COMP_CMD_SET_VALUE 100 /**< Set value to component */
105 #define COMP_CMD_GET_VALUE 101 /**< Get value from component */
106 #define COMP_CMD_SET_DATA 102 /**< Set data to component */
107 #define COMP_CMD_GET_DATA 103 /**< Get data from component */
108 /** @}*/
109
110 /** \name MMAP IPC status
111 * @{
112 */
113 #define COMP_CMD_IPC_MMAP_RPOS 200 /**< Host read position */
114 #define COMP_CMD_IPC_MMAP_PPOS 201 /**< DAI presentation position */
115
116 #define COMP_CMD_IPC_MMAP_VOL(chan) (216 + chan) /**< Volume */
117 /** @}*/
118
119 /** \name Component status
120 * @{
121 */
122 #define COMP_STATUS_STATE_ALREADY_SET 1 /**< Comp set state status */
123 /** @}*/
124
125 /** \name Component attribute types
126 * @{
127 */
128 #define COMP_ATTR_COPY_TYPE 0 /**< Comp copy type attribute */
129 #define COMP_ATTR_HOST_BUFFER 1 /**< Comp host buffer attribute */
130 #define COMP_ATTR_COPY_DIR 2 /**< Comp copy direction */
131 #define COMP_ATTR_VDMA_INDEX 3 /**< Comp index of the virtual DMA at the gateway. */
132 #define COMP_ATTR_BASE_CONFIG 4 /**< Component base config */
133 /** @}*/
134
135 /** \name Trace macros
136 * @{
137 */
138
139 /** \brief Retrieves trace context from the component driver */
140 #define trace_comp_drv_get_tr_ctx(drv_p) ((drv_p)->tctx)
141
142 /** \brief Retrieves id (-1 = undefined) from the component driver */
143 #define trace_comp_drv_get_id(drv_p) (-1)
144
145 /** \brief Retrieves subid (-1 = undefined) from the component driver */
146 #define trace_comp_drv_get_subid(drv_p) (-1)
147
148 /** \brief Retrieves trace context from the component device */
149 #define trace_comp_get_tr_ctx(comp_p) (&(comp_p)->tctx)
150
151 /** \brief Retrieves id (pipe id) from the component device */
152 #define trace_comp_get_id(comp_p) ((comp_p)->ipc_config.pipeline_id)
153
154 /** \brief Retrieves subid (comp id) from the component device */
155 #define trace_comp_get_subid(comp_p) ((comp_p)->ipc_config.id)
156
157 #if defined(__ZEPHYR__) && defined(CONFIG_ZEPHYR_LOG)
158 /* class (driver) level (no device object) tracing */
159 #define comp_cl_err(drv_p, __e, ...) LOG_ERR(__e, ##__VA_ARGS__)
160
161 #define comp_cl_warn(drv_p, __e, ...) LOG_WRN(__e, ##__VA_ARGS__)
162
163 #define comp_cl_info(drv_p, __e, ...) LOG_INF(__e, ##__VA_ARGS__)
164
165 #define comp_cl_dbg(drv_p, __e, ...) LOG_DBG(__e, ##__VA_ARGS__)
166
167 /* device level tracing */
168
169 #if CONFIG_IPC_MAJOR_4
170 #define __COMP_FMT "comp:%u %#x "
171 #else
172 #define __COMP_FMT "comp:%u.%u "
173 #endif
174
175 #define comp_err(comp_p, __e, ...) LOG_ERR(__COMP_FMT __e, trace_comp_get_id(comp_p), \
176 trace_comp_get_subid(comp_p), ##__VA_ARGS__)
177
178 #define comp_warn(comp_p, __e, ...) LOG_WRN(__COMP_FMT __e, trace_comp_get_id(comp_p), \
179 trace_comp_get_subid(comp_p), ##__VA_ARGS__)
180
181 #define comp_info(comp_p, __e, ...) LOG_INF(__COMP_FMT __e, trace_comp_get_id(comp_p), \
182 trace_comp_get_subid(comp_p), ##__VA_ARGS__)
183
184 #define comp_dbg(comp_p, __e, ...) LOG_DBG(__COMP_FMT __e, trace_comp_get_id(comp_p), \
185 trace_comp_get_subid(comp_p), ##__VA_ARGS__)
186
187 #else
188 /* class (driver) level (no device object) tracing */
189
190 /** \brief Trace error message from component driver (no comp instance) */
191 #define comp_cl_err(drv_p, __e, ...) \
192 trace_dev_err(trace_comp_drv_get_tr_ctx, \
193 trace_comp_drv_get_id, \
194 trace_comp_drv_get_subid, \
195 drv_p, \
196 __e, ##__VA_ARGS__)
197
198 /** \brief Trace warning message from component driver (no comp instance) */
199 #define comp_cl_warn(drv_p, __e, ...) \
200 trace_dev_warn(trace_comp_drv_get_tr_ctx, \
201 trace_comp_drv_get_id, \
202 trace_comp_drv_get_subid, \
203 drv_p, \
204 __e, ##__VA_ARGS__)
205
206 /** \brief Trace info message from component driver (no comp instance) */
207 #define comp_cl_info(drv_p, __e, ...) \
208 trace_dev_info(trace_comp_drv_get_tr_ctx, \
209 trace_comp_drv_get_id, \
210 trace_comp_drv_get_subid, \
211 drv_p, \
212 __e, ##__VA_ARGS__)
213
214 /** \brief Trace debug message from component driver (no comp instance) */
215 #define comp_cl_dbg(drv_p, __e, ...) \
216 trace_dev_dbg(trace_comp_drv_get_tr_ctx, \
217 trace_comp_drv_get_id, \
218 trace_comp_drv_get_subid, \
219 drv_p, \
220 __e, ##__VA_ARGS__)
221
222 /* device tracing */
223
224 /** \brief Trace error message from component device */
225 #define comp_err(comp_p, __e, ...) \
226 trace_dev_err(trace_comp_get_tr_ctx, trace_comp_get_id, \
227 trace_comp_get_subid, comp_p, __e, ##__VA_ARGS__)
228
229 /** \brief Trace warning message from component device */
230 #define comp_warn(comp_p, __e, ...) \
231 trace_dev_warn(trace_comp_get_tr_ctx, trace_comp_get_id, \
232 trace_comp_get_subid, comp_p, __e, ##__VA_ARGS__)
233
234 /** \brief Trace info message from component device */
235 #define comp_info(comp_p, __e, ...) \
236 trace_dev_info(trace_comp_get_tr_ctx, trace_comp_get_id, \
237 trace_comp_get_subid, comp_p, __e, ##__VA_ARGS__)
238
239 /** \brief Trace debug message from component device */
240 #define comp_dbg(comp_p, __e, ...) \
241 trace_dev_dbg(trace_comp_get_tr_ctx, trace_comp_get_id, \
242 trace_comp_get_subid, comp_p, __e, ##__VA_ARGS__)
243
244 #endif /* #if defined(__ZEPHYR__) && defined(CONFIG_ZEPHYR_LOG) */
245
246 #define comp_perf_info(pcd, comp_p) \
247 comp_info(comp_p, "perf comp_copy peak plat %u cpu %u", \
248 (uint32_t)((pcd)->plat_delta_peak), \
249 (uint32_t)((pcd)->cpu_delta_peak))
250
251 #define comp_perf_avg_info(pcd, comp_p) \
252 comp_info(comp_p, "perf comp_copy samples %u period %u cpu avg %u peak %u",\
253 (uint32_t)((comp_p)->frames), \
254 (uint32_t)((comp_p)->period), \
255 (uint32_t)((pcd)->cpu_delta_sum), \
256 (uint32_t)((pcd)->cpu_delta_peak))
257
258 /** @}*/
259
260 /** \brief Type of endpoint this component is connected to in a pipeline */
261 enum comp_endpoint_type {
262 COMP_ENDPOINT_HOST, /**< Connected to host dma */
263 COMP_ENDPOINT_DAI, /**< Connected to dai dma */
264 COMP_ENDPOINT_NODE /**< No dma connection */
265 };
266
267 /**
268 * \brief Type of next dma copy mode, changed on runtime.
269 *
270 * Supported by host as COMP_ATTR_COPY_TYPE parameter
271 * to comp_set_attribute().
272 */
273 enum comp_copy_type {
274 COMP_COPY_INVALID = -1, /**< Invalid */
275 COMP_COPY_NORMAL = 0, /**< Normal */
276 COMP_COPY_BLOCKING, /**< Blocking */
277 COMP_COPY_ONE_SHOT, /**< One-shot */
278 };
279
280 struct comp_driver;
281 struct comp_ipc_config;
282 union ipc_config_specific;
283
284 /**
285 * Audio component operations.
286 *
287 * All component operations must return 0 for success, negative values for
288 * errors and 1 to stop the pipeline walk operation unless specified otherwise
289 * in the operation documentation.
290 */
291 struct comp_ops {
292 /**
293 * Creates a new component device.
294 * @param drv Parent component driver.
295 * @param comp Component parameters.
296 * @return Pointer to the new component device.
297 *
298 * All required data objects should be allocated from the run-time
299 * heap (SOF_MEM_ZONE_RUNTIME).
300 * Any component-specific private data is allocated separately and
301 * pointer is connected to the comp_dev::private by using
302 * comp_set_drvdata() and later retrieved by comp_get_drvdata().
303 *
304 * All parameters should be initialized to their default values.
305 */
306 struct comp_dev *(*create)(const struct comp_driver *drv,
307 const struct comp_ipc_config *ipc_config,
308 const void *ipc_specific_config);
309
310 /**
311 * Called to delete the specified component device.
312 * @param dev Component device to be deleted.
313 *
314 * All data structures previously allocated on the run-time heap
315 * must be freed by the implementation of <code>free()</code>.
316 */
317 void (*free)(struct comp_dev *dev);
318
319 /**
320 * Sets component audio stream parameters.
321 * @param dev Component device.
322 * @param params Audio (PCM) stream parameters to be set.
323 *
324 * Infrastructure calls comp_verify_params() if this handler is not
325 * defined, therefore it should be left NULL if no extra steps are
326 * required.
327 */
328 int (*params)(struct comp_dev *dev,
329 struct sof_ipc_stream_params *params);
330
331 /**
332 * Fetches hardware stream parameters.
333 * @param dev Component device.
334 * @param params Receives copy of stream parameters retrieved from
335 * DAI hw settings.
336 * @param dir Stream direction (see enum sof_ipc_stream_direction).
337 *
338 * Mandatory for components that allocate DAI.
339 */
340 int (*dai_get_hw_params)(struct comp_dev *dev,
341 struct sof_ipc_stream_params *params, int dir);
342
343 /**
344 * Configures attached DAI.
345 * @param dev Component device.
346 * @param dai_config DAI configuration.
347 *
348 * Mandatory for components that allocate DAI.
349 */
350 int (*dai_config)(struct comp_dev *dev, struct ipc_config_dai *dai_config,
351 const void *dai_spec_config);
352
353 /**
354 * Used to pass standard and bespoke commands (with optional data).
355 * @param dev Component device.
356 * @param cmd Command.
357 * @param data Command data.
358 * @param max_data_size Max size of returned data acceptable by the
359 * caller in case of 'get' commands.
360 */
361 int (*cmd)(struct comp_dev *dev, int cmd, void *data,
362 int max_data_size);
363
364 /**
365 * Trigger, atomic - used to start/stop/pause stream operations.
366 * @param dev Component device.
367 * @param cmd Command, one of COMP_TRIGGER_...
368 */
369 int (*trigger)(struct comp_dev *dev, int cmd);
370
371 /**
372 * Prepares component after params are set.
373 * @param dev Component device.
374 *
375 * Prepare should be used to get the component ready for starting
376 * processing after it's hw_params are known or after an XRUN.
377 */
378 int (*prepare)(struct comp_dev *dev);
379
380 /**
381 * Resets component.
382 * @param dev Component device.
383 *
384 * Resets the component state and any hw_params to default component
385 * state. Should also free any resources acquired during hw_params.
386 */
387 int (*reset)(struct comp_dev *dev);
388
389 /**
390 * Copy and process stream data from source to sink buffers.
391 * @param dev Component device.
392 * @return Number of copied frames.
393 */
394 int (*copy)(struct comp_dev *dev);
395
396 /**
397 * Retrieves component rendering position.
398 * @param dev Component device.
399 * @param posn Receives reported position.
400 */
401 int (*position)(struct comp_dev *dev,
402 struct sof_ipc_stream_posn *posn);
403
404 /**
405 * Gets attribute in component.
406 * @param dev Component device.
407 * @param type Attribute type.
408 * @param value Attribute value.
409 * @return 0 if succeeded, error code otherwise.
410 */
411 int (*get_attribute)(struct comp_dev *dev, uint32_t type,
412 void *value);
413
414 /**
415 * Sets attribute in component.
416 * @param dev Component device.
417 * @param type Attribute type.
418 * @param value Attribute value.
419 * @return 0 if succeeded, error code otherwise.
420 */
421 int (*set_attribute)(struct comp_dev *dev, uint32_t type,
422 void *value);
423
424 /**
425 * Configures timestamping in attached DAI.
426 * @param dev Component device.
427 *
428 * Mandatory for components that allocate DAI.
429 */
430 int (*dai_ts_config)(struct comp_dev *dev);
431
432 /**
433 * Starts timestamping.
434 * @param dev Component device.
435 *
436 * Mandatory for components that allocate DAI.
437 */
438 int (*dai_ts_start)(struct comp_dev *dev);
439
440 /**
441 * Stops timestamping.
442 * @param dev Component device.
443 *
444 * Mandatory for components that allocate DAI.
445 */
446 int (*dai_ts_stop)(struct comp_dev *dev);
447
448 /**
449 * Gets timestamp.
450 * @param dev Component device.
451 * @param tsd Receives timestamp data.
452 *
453 * Mandatory for components that allocate DAI.
454 */
455 int (*dai_ts_get)(struct comp_dev *dev,
456 struct timestamp_data *tsd);
457
458 /**
459 * Bind, atomic - used to notify component of bind event.
460 * @param dev Component device.
461 * @param data Bind info
462 */
463 int (*bind)(struct comp_dev *dev, void *data);
464
465 /**
466 * Unbind, atomic - used to notify component of unbind event.
467 * @param dev Component device.
468 * @param data unBind info
469 */
470 int (*unbind)(struct comp_dev *dev, void *data);
471
472 /**
473 * Gets config in component.
474 * @param dev Component device
475 * @param param_id param id for each component
476 * @param first_block first block of large block.
477 * @param last_block last block of large block.
478 * @param data_offset block offset filled by callee.
479 * @param data block data.
480 * @return 0 if succeeded, error code otherwise.
481 *
482 * Callee fills up *data with config data and save the config
483 * size in *data_offset for host to reconstruct the config
484 */
485 int (*get_large_config)(struct comp_dev *dev, uint32_t param_id,
486 bool first_block,
487 bool last_block,
488 uint32_t *data_offset,
489 char *data);
490
491 /**
492 * Set config in component.
493 * @param dev Component device
494 * @param param_id param id for each component
495 * @param first_block first block of large block.
496 * @param last_block last block of large block.
497 * @param data_offset block offset in large block.
498 * @param data block data.
499 * @return 0 if succeeded, error code otherwise.
500 *
501 * Host divides large block into small blocks and sends them
502 * to fw. The data_offset indicates the offset in the large
503 * block data.
504 */
505 int (*set_large_config)(struct comp_dev *dev, uint32_t param_id,
506 bool first_block,
507 bool last_block,
508 uint32_t data_offset,
509 const char *data);
510
511 /**
512 * Returns total data processed in number bytes.
513 * @param dev Component device
514 * @param stream_no Index of input/output stream
515 * @param input Selects between input (true) or output (false) stream direction
516 * @return total data processed if succeeded, 0 otherwise.
517 */
518 uint64_t (*get_total_data_processed)(struct comp_dev *dev, uint32_t stream_no, bool input);
519 };
520
521 /**
522 * Audio component base driver "class"
523 * - used by all other component types.
524 */
525 struct comp_driver {
526 uint32_t type; /**< SOF_COMP_ for driver */
527 const struct sof_uuid *uid; /**< Address to UUID value */
528 struct tr_ctx *tctx; /**< Pointer to trace context */
529 struct comp_ops ops; /**< component operations */
530 };
531
532 /** \brief Holds constant pointer to component driver */
533 struct comp_driver_info {
534 const struct comp_driver *drv; /**< pointer to component driver */
535 struct list_item list; /**< list of component drivers */
536 };
537
538 #define COMP_PROCESSING_DOMAIN_LL 0
539 #define COMP_PROCESSING_DOMAIN_DP 1
540
541 /**
542 * Audio component base configuration from IPC at creation.
543 */
544 struct comp_ipc_config {
545 uint32_t core; /**< core we run on */
546 uint32_t id; /**< component id */
547 uint32_t pipeline_id; /**< component pipeline id */
548 uint32_t proc_domain; /**< processing domain - LL or DP */
549 enum sof_comp_type type; /**< component type */
550 uint32_t periods_sink; /**< 0 means variable */
551 uint32_t periods_source;/**< 0 means variable */
552 uint32_t frame_fmt; /**< SOF_IPC_FRAME_ */
553 uint32_t xrun_action; /**< action we should take on XRUN */
554 };
555
556 /**
557 * Audio component base device "class"
558 * - used by other component types.
559 */
560 struct comp_dev {
561
562 /* runtime */
563 uint16_t state; /**< COMP_STATE_ */
564 uint32_t frames; /**< number of frames we copy to sink */
565 struct pipeline *pipeline; /**< pipeline we belong to */
566
567 uint32_t min_sink_bytes; /**< min free sink buffer size measured in
568 * bytes required to run component's
569 * processing
570 */
571 uint32_t min_source_bytes; /**< amount of data measured in bytes
572 * available at source buffer required
573 * to run component's processing
574 */
575
576 struct task *task; /**< component's processing task used
577 * 1) for components running on different core
578 * than the rest of the pipeline
579 * 2) for all DP tasks
580 */
581 uint32_t size; /**< component's allocated size */
582 uint32_t period; /**< component's processing period */
583 uint32_t priority; /**< component's processing priority */
584 bool is_shared; /**< indicates whether component is shared
585 * across cores
586 */
587 struct comp_ipc_config ipc_config; /**< Component IPC configuration */
588 struct tr_ctx tctx; /**< trace settings */
589
590 /* common runtime configuration for downstream/upstream */
591 uint32_t direction; /**< enum sof_ipc_stream_direction */
592 bool direction_set; /**< flag indicating that the direction has been set */
593
594 const struct comp_driver *drv; /**< driver */
595
596 /* lists */
597 struct list_item bsource_list; /**< list of source buffers */
598 struct list_item bsink_list; /**< list of sink buffers */
599
600 /* private data - core does not touch this */
601 void *priv_data; /**< private data */
602
603 #if CONFIG_PERFORMANCE_COUNTERS
604 struct perf_cnt_data pcd;
605 #endif
606 };
607
608 /** @}*/
609
610 /* Common helper function used internally by the component implementations
611 * begin here.
612 */
613
614 /** \addtogroup component_common_int Component's Common Helpers
615 * @{
616 */
617
618 /** \brief Struct for use with comp_get_copy_limits() function. */
619 struct comp_copy_limits {
620 int frames;
621 int source_bytes;
622 int sink_bytes;
623 int source_frame_bytes;
624 int sink_frame_bytes;
625 };
626
627 /**
628 * Retrieves Component id from device.
629 * @param dev Device.
630 * @return Component id.
631 */
dev_comp_id(const struct comp_dev * dev)632 static inline uint32_t dev_comp_id(const struct comp_dev *dev)
633 {
634 return dev->ipc_config.id;
635 }
636
637 /**
638 * Retrieves Component pipeline id from device.
639 * @param dev Device.
640 * @return Component pipeline id.
641 */
dev_comp_pipe_id(const struct comp_dev * dev)642 static inline uint32_t dev_comp_pipe_id(const struct comp_dev *dev)
643 {
644 return dev->ipc_config.pipeline_id;
645 }
646
647 /**
648 * Retrieves component type from device.
649 * @param dev Device.
650 * @return Component type.
651 */
dev_comp_type(const struct comp_dev * dev)652 static inline enum sof_comp_type dev_comp_type(const struct comp_dev *dev)
653 {
654 return dev->ipc_config.type;
655 }
656
657 /**
658 * Allocates memory for the component device and initializes common part.
659 * @param drv Parent component driver.
660 * @param bytes Size of the component device in bytes.
661 * @return Pointer to the component device.
662 */
comp_alloc(const struct comp_driver * drv,size_t bytes)663 static inline struct comp_dev *comp_alloc(const struct comp_driver *drv,
664 size_t bytes)
665 {
666 struct comp_dev *dev = NULL;
667
668 /*
669 * Use uncached address everywhere to access components to rule out
670 * multi-core failures. In the future we might decide to switch over to
671 * the latest coherence API for performance. In that case components
672 * will be acquired for cached access and released afterwards.
673 */
674 dev = rzalloc(SOF_MEM_ZONE_RUNTIME_SHARED, 0, SOF_MEM_CAPS_RAM, bytes);
675 if (!dev)
676 return NULL;
677 dev->size = bytes;
678 dev->drv = drv;
679 dev->state = COMP_STATE_INIT;
680 memcpy_s(&dev->tctx, sizeof(struct tr_ctx),
681 trace_comp_drv_get_tr_ctx(dev->drv), sizeof(struct tr_ctx));
682
683 return dev;
684 }
685
686 /**
687 * \brief Assigns private data to component device.
688 * @param c Component device.
689 * @param data Private data.
690 */
691 #define comp_set_drvdata(c, data) \
692 (c->priv_data = data)
693
694 /**
695 * \brief Retrieves driver private data from component device.
696 * @param c Component device.
697 * @return Private data.
698 */
699 #define comp_get_drvdata(c) \
700 (c->priv_data)
701
702 #if defined UNIT_TEST || defined __ZEPHYR__ || CONFIG_LIBRARY_STATIC
703 #define DECLARE_MODULE(init)
704
705 /* declared modules */
706 void sys_comp_asrc_init(void);
707 void sys_comp_crossover_init(void);
708 void sys_comp_dai_init(void);
709 void sys_comp_dcblock_init(void);
710 void sys_comp_drc_init(void);
711 void sys_comp_eq_iir_init(void);
712 void sys_comp_host_init(void);
713 void sys_comp_kpb_init(void);
714 void sys_comp_mixer_init(void);
715 void sys_comp_multiband_drc_init(void);
716 void sys_comp_selector_init(void);
717 void sys_comp_src_init(void);
718
719 void sys_comp_module_demux_interface_init(void);
720 void sys_comp_module_eq_fir_interface_init(void);
721 void sys_comp_module_eq_iir_interface_init(void);
722 void sys_comp_module_mfcc_interface_init(void);
723 void sys_comp_module_mixer_interface_init(void);
724 void sys_comp_module_mux_interface_init(void);
725 void sys_comp_module_tdfb_interface_init(void);
726 void sys_comp_module_volume_interface_init(void);
727
728 #elif CONFIG_LIBRARY
729 /* In case of shared libs components are initialised in dlopen */
730 #define DECLARE_MODULE(init) __attribute__((constructor)) \
731 static void _module_##init(void) { init(); }
732 #else
733 /** \brief Usage at the end of an independent module file:
734 * DECLARE_MODULE(sys_*_init);
735 */
736 #define DECLARE_MODULE(init) __attribute__((__used__)) \
737 __section(".initcall") static void(*f##init)(void) = init
738 #endif
739
740 /** \name Component registration
741 * @{
742 */
743
744 /**
745 * Registers the component driver on the list of available components.
746 * @param drv Component driver to be registered.
747 * @return 0 if succeeded, error code otherwise.
748 */
749 int comp_register(struct comp_driver_info *drv);
750
751 /**
752 * Unregisters the component driver from the list of available components.
753 * @param drv Component driver to be unregistered.
754 */
755 void comp_unregister(struct comp_driver_info *drv);
756
757 /** @}*/
758
759 /**
760 * Component state set.
761 * @param dev Component device.
762 * @param cmd Command, one of <i>COMP_TRIGGER_...</i>.
763 * @return 0 if succeeded, error code otherwise.
764 *
765 * This function should be called by a component implementation at the beginning
766 * of its state transition to verify whether the trigger is valid in the
767 * current state and abort the transition otherwise.
768 *
769 * Typically the COMP_STATE_READY as the initial state is set directly
770 * by the component's implementation of _new().
771 *
772 * COMP_TRIGGER_PREPARE is called from the component's prepare().
773 *
774 * COMP_TRIGGER_START/STOP is called from trigger().
775 *
776 * COMP_TRIGGER_RESET is called from reset().
777 */
778 int comp_set_state(struct comp_dev *dev, int cmd);
779
780 /* \brief Set component period frames */
component_set_nearest_period_frames(struct comp_dev * current,uint32_t rate)781 static inline void component_set_nearest_period_frames(struct comp_dev *current,
782 uint32_t rate)
783 {
784 /* Sample rate is in Hz and period in microseconds.
785 * As we don't have floats use scale divider 1000000.
786 * Also integer round up the result.
787 * dma buffer size should align with 32bytes which can't be
788 * compatible with current 45K adjustment. 48K is a suitable
789 * adjustment.
790 */
791 switch (rate) {
792 case 44100:
793 current->frames = 48000 * current->period / 1000000;
794 break;
795 case 88200:
796 current->frames = 96000 * current->period / 1000000;
797 break;
798 case 176400:
799 current->frames = 192000 * current->period / 1000000;
800 break;
801 default:
802 current->frames = ceil_divide(rate * current->period, 1000000);
803 break;
804 }
805 }
806
807 /** \name XRUN handling.
808 * @{
809 */
810
811 /**
812 * Called by the component device when underrun is detected.
813 * @param dev Component device.
814 * @param source Source buffer.
815 * @param copy_bytes Requested size of data to be available.
816 */
comp_underrun(struct comp_dev * dev,struct comp_buffer __sparse_cache * source,uint32_t copy_bytes)817 static inline void comp_underrun(struct comp_dev *dev,
818 struct comp_buffer __sparse_cache *source,
819 uint32_t copy_bytes)
820 {
821 LOG_MODULE_DECLARE(component, CONFIG_SOF_LOG_LEVEL);
822
823 int32_t bytes = (int32_t)audio_stream_get_avail_bytes(&source->stream) -
824 copy_bytes;
825
826 comp_err(dev, "comp_underrun(): dev->comp.id = %u, source->avail = %u, copy_bytes = %u",
827 dev_comp_id(dev),
828 audio_stream_get_avail_bytes(&source->stream),
829 copy_bytes);
830
831 pipeline_xrun(dev->pipeline, dev, bytes);
832 }
833
834 /**
835 * Called by component device when overrun is detected.
836 * @param dev Component device.
837 * @param sink Sink buffer.
838 * @param copy_bytes Requested size of free space to be available.
839 */
comp_overrun(struct comp_dev * dev,struct comp_buffer __sparse_cache * sink,uint32_t copy_bytes)840 static inline void comp_overrun(struct comp_dev *dev, struct comp_buffer __sparse_cache *sink,
841 uint32_t copy_bytes)
842 {
843 LOG_MODULE_DECLARE(component, CONFIG_SOF_LOG_LEVEL);
844
845 int32_t bytes = (int32_t)copy_bytes -
846 audio_stream_get_free_bytes(&sink->stream);
847
848 comp_err(dev, "comp_overrun(): sink->free = %u, copy_bytes = %u",
849 audio_stream_get_free_bytes(&sink->stream), copy_bytes);
850
851 pipeline_xrun(dev->pipeline, dev, bytes);
852 }
853
854 /** @}*/
855
856 /**
857 * Computes source to sink copy operation boundaries including maximum number
858 * of frames that can be transferred (data available in source vs. free space
859 * available in sink).
860 *
861 * @param[in] source Source buffer.
862 * @param[in] sink Sink buffer.
863 * @param[out] cl Current copy limits.
864 */
865 void comp_get_copy_limits(struct comp_buffer __sparse_cache *source,
866 struct comp_buffer __sparse_cache *sink,
867 struct comp_copy_limits *cl);
868
869 /**
870 * Computes source to sink copy operation boundaries including maximum number
871 * of frames aligned with requirement that can be transferred (data available in
872 * source vs. free space available in sink).
873 *
874 * @param[in] source Buffer of source.
875 * @param[in] sink Buffer of sink.
876 * @param[out] cl Current copy limits.
877 */
878 void comp_get_copy_limits_frame_aligned(const struct comp_buffer __sparse_cache *source,
879 const struct comp_buffer __sparse_cache *sink,
880 struct comp_copy_limits *cl);
881
882 /**
883 * Version of comp_get_copy_limits that locks both buffers to guarantee
884 * consistent state readings.
885 *
886 * @param[in] source Source buffer.
887 * @param[in] sink Sink buffer
888 * @param[out] cl Current copy limits.
889 */
890 static inline
comp_get_copy_limits_with_lock(struct comp_buffer * source,struct comp_buffer * sink,struct comp_copy_limits * cl)891 void comp_get_copy_limits_with_lock(struct comp_buffer *source,
892 struct comp_buffer *sink,
893 struct comp_copy_limits *cl)
894 {
895 struct comp_buffer __sparse_cache *source_c, *sink_c;
896
897 source_c = buffer_acquire(source);
898 sink_c = buffer_acquire(sink);
899
900 comp_get_copy_limits(source_c, sink_c, cl);
901
902 buffer_release(sink_c);
903 buffer_release(source_c);
904 }
905
906 /**
907 * Version of comp_get_copy_limits_with_lock_frame_aligned that locks both
908 * buffers to guarantee consistent state readings and the frames aligned with
909 * the requirement.
910 *
911 * @param[in] source Buffer of source.
912 * @param[in] sink Buffer of sink
913 * @param[out] cl Current copy limits.
914 */
915 static inline
comp_get_copy_limits_with_lock_frame_aligned(struct comp_buffer * source,struct comp_buffer * sink,struct comp_copy_limits * cl)916 void comp_get_copy_limits_with_lock_frame_aligned(struct comp_buffer *source,
917 struct comp_buffer *sink,
918 struct comp_copy_limits *cl)
919 {
920 struct comp_buffer __sparse_cache *source_c, *sink_c;
921
922 source_c = buffer_acquire(source);
923 sink_c = buffer_acquire(sink);
924
925 comp_get_copy_limits_frame_aligned(source_c, sink_c, cl);
926
927 buffer_release(sink_c);
928 buffer_release(source_c);
929 }
930
931 /**
932 * Get component state.
933 *
934 * @param req_dev Requesting component
935 * @param dev Component from which user wants to read status.
936 */
comp_get_state(struct comp_dev * req_dev,struct comp_dev * dev)937 static inline int comp_get_state(struct comp_dev *req_dev, struct comp_dev *dev)
938 {
939 return dev->state;
940 }
941
942 /**
943 * Warning: duplicate declaration in topology.h
944 *
945 * Called by component in params() function in order to set and update some of
946 * downstream (playback) or upstream (capture) buffer parameters with pcm
947 * parameters. There is a possibility to specify which of parameters won't be
948 * overwritten (e.g. SRC component should not overwrite rate parameter, because
949 * it is able to change it).
950 *
951 * @param dev Component device
952 * @param flag Specifies which parameter should not be updated
953 * @param params pcm params
954 */
955 int comp_verify_params(struct comp_dev *dev, uint32_t flag,
956 struct sof_ipc_stream_params *params);
957
958 /** @}*/
959
960 #endif /* __SOF_AUDIO_COMPONENT_H__ */
961