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 
7 /*
8  * Generic audio task.
9  */
10 
11 #include <sof/audio/component_ext.h>
12 #include <sof/audio/pipeline.h>
13 #include <sof/debug/panic.h>
14 #include <sof/ipc/msg.h>
15 #include <sof/lib/alloc.h>
16 #include <sof/lib/agent.h>
17 #include <sof/lib/cpu.h>
18 #include <sof/lib/memory.h>
19 #include <sof/lib/uuid.h>
20 #include <sof/lib/wait.h>
21 #include <sof/platform.h>
22 #include <sof/schedule/edf_schedule.h>
23 #include <sof/schedule/schedule.h>
24 #include <sof/schedule/task.h>
25 #include <sof/sof.h>
26 #include <ipc/topology.h>
27 #include <errno.h>
28 #include <stddef.h>
29 #include <stdint.h>
30 
31 typedef enum task_state (*task_main)(void *);
32 
33 /* 37f1d41f-252d-448d-b9c4-1e2bee8e1bf1 */
34 DECLARE_SOF_UUID("main-task", main_task_uuid, 0x37f1d41f, 0x252d, 0x448d,
35 		 0xb9, 0xc4, 0x1e, 0x2b, 0xee, 0x8e, 0x1b, 0xf1);
36 
sys_module_init(void)37 static void sys_module_init(void)
38 {
39 	intptr_t *module_init = (intptr_t *)(&_module_init_start);
40 
41 	for (; module_init < (intptr_t *)&_module_init_end; ++module_init)
42 		((void(*)(void))(*module_init))();
43 }
44 
task_main_deadline(void * data)45 static uint64_t task_main_deadline(void *data)
46 {
47 	return SOF_TASK_DEADLINE_IDLE;
48 }
49 
task_main_primary_core(void * data)50 enum task_state task_main_primary_core(void *data)
51 {
52 	struct ipc *ipc = ipc_get();
53 
54 	/* main audio processing loop */
55 	while (1) {
56 		/* sleep until next IPC or DMA */
57 		wait_for_interrupt(0);
58 
59 		if (!ipc->pm_prepare_D3)
60 			ipc_send_queued_msg();
61 
62 	}
63 
64 	return SOF_TASK_STATE_COMPLETED;
65 }
66 
task_main_init(void)67 void task_main_init(void)
68 {
69 	struct task **main_task = task_main_get();
70 	int cpu = cpu_get_id();
71 	int ret;
72 	task_main main_main = cpu == PLATFORM_PRIMARY_CORE_ID ?
73 		&task_main_primary_core : &task_main_secondary_core;
74 	struct task_ops ops = {
75 		.run = main_main,
76 		.get_deadline = task_main_deadline,
77 	};
78 
79 	*main_task = rzalloc(SOF_MEM_ZONE_SYS, 0, SOF_MEM_CAPS_RAM,
80 			     sizeof(**main_task));
81 
82 	ret = schedule_task_init_edf(*main_task, SOF_UUID(main_task_uuid),
83 				     &ops, NULL, cpu, 0);
84 	assert(!ret);
85 }
86 
task_main_free(void)87 void task_main_free(void)
88 {
89 	schedule_task_free(*task_main_get());
90 }
91 
task_main_start(struct sof * sof)92 int task_main_start(struct sof *sof)
93 {
94 	int ret;
95 
96 	/* init default audio components */
97 	sys_comp_init(sof);
98 
99 	/* init self-registered modules */
100 	sys_module_init();
101 
102 	/* init pipeline position offsets */
103 	pipeline_posn_init(sof);
104 
105 	/* let host know DSP boot is complete */
106 	ret = platform_boot_complete(0);
107 	if (ret < 0)
108 		return ret;
109 
110 	/* task initialized in edf_scheduler_init */
111 	schedule_task(*task_main_get(), 0, UINT64_MAX);
112 
113 	/* something bad happened */
114 	return -EIO;
115 }
116