xref: /linux/sound/soc/intel/avs/avs.h (revision 2f1f570cd730c81807ae143a83766068dd82d577)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4  *
5  * Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6  *          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7  */
8 
9 #ifndef __SOUND_SOC_INTEL_AVS_H
10 #define __SOUND_SOC_INTEL_AVS_H
11 
12 #include <linux/device.h>
13 #include <linux/firmware.h>
14 #include <sound/hda_codec.h>
15 #include <sound/hda_register.h>
16 #include <sound/soc-component.h>
17 #include "messages.h"
18 #include "registers.h"
19 
20 struct avs_dev;
21 struct avs_tplg;
22 struct avs_tplg_library;
23 struct avs_soc_component;
24 
25 /*
26  * struct avs_dsp_ops - Platform-specific DSP operations
27  *
28  * @power: Power on or off DSP cores
29  * @reset: Enter or exit reset state on DSP cores
30  * @stall: Stall or run DSP cores
31  * @irq_handler: Top half of IPC servicing
32  * @irq_thread: Bottom half of IPC servicing
33  * @int_control: Enable or disable IPC interrupts
34  */
35 struct avs_dsp_ops {
36 	int (* const power)(struct avs_dev *, u32, bool);
37 	int (* const reset)(struct avs_dev *, u32, bool);
38 	int (* const stall)(struct avs_dev *, u32, bool);
39 	irqreturn_t (* const irq_handler)(int, void *);
40 	irqreturn_t (* const irq_thread)(int, void *);
41 	void (* const int_control)(struct avs_dev *, bool);
42 	int (* const load_basefw)(struct avs_dev *, struct firmware *);
43 	int (* const load_lib)(struct avs_dev *, struct firmware *, u32);
44 	int (* const transfer_mods)(struct avs_dev *, bool, struct avs_module_entry *, u32);
45 	int (* const coredump)(struct avs_dev *, union avs_notify_msg *);
46 };
47 
48 #define avs_dsp_op(adev, op, ...) \
49 	((adev)->spec->dsp_ops->op(adev, ## __VA_ARGS__))
50 
51 #define AVS_PLATATTR_CLDMA		BIT_ULL(0)
52 #define AVS_PLATATTR_IMR		BIT_ULL(1)
53 
54 #define avs_platattr_test(adev, attr) \
55 	((adev)->spec->attributes & AVS_PLATATTR_##attr)
56 
57 /* Platform specific descriptor */
58 struct avs_spec {
59 	const char *name;
60 
61 	const struct avs_dsp_ops *const dsp_ops;
62 	struct avs_fw_version min_fw_version; /* anything below is rejected */
63 
64 	const u32 core_init_mask;	/* used during DSP boot */
65 	const u64 attributes;		/* bitmask of AVS_PLATATTR_* */
66 	const u32 sram_base_offset;
67 	const u32 sram_window_size;
68 	const u32 rom_status;
69 };
70 
71 struct avs_fw_entry {
72 	char *name;
73 	const struct firmware *fw;
74 
75 	struct list_head node;
76 };
77 
78 /*
79  * struct avs_dev - Intel HD-Audio driver data
80  *
81  * @dev: PCI device
82  * @dsp_ba: DSP bar address
83  * @spec: platform-specific descriptor
84  * @fw_cfg: Firmware configuration, obtained through FW_CONFIG message
85  * @hw_cfg: Hardware configuration, obtained through HW_CONFIG message
86  * @mods_info: Available module-types, obtained through MODULES_INFO message
87  * @mod_idas: Module instance ID pool, one per module-type
88  * @modres_mutex: For synchronizing any @mods_info updates
89  * @ppl_ida: Pipeline instance ID pool
90  * @fw_list: List of libraries loaded, including base firmware
91  */
92 struct avs_dev {
93 	struct hda_bus base;
94 	struct device *dev;
95 
96 	void __iomem *dsp_ba;
97 	const struct avs_spec *spec;
98 	struct avs_ipc *ipc;
99 
100 	struct avs_fw_cfg fw_cfg;
101 	struct avs_hw_cfg hw_cfg;
102 	struct avs_mods_info *mods_info;
103 	struct ida **mod_idas;
104 	struct mutex modres_mutex;
105 	struct ida ppl_ida;
106 	struct list_head fw_list;
107 	int *core_refs;		/* reference count per core */
108 	char **lib_names;
109 
110 	struct completion fw_ready;
111 
112 	struct nhlt_acpi_table *nhlt;
113 	struct list_head comp_list;
114 	struct mutex comp_list_mutex;
115 	struct list_head path_list;
116 	spinlock_t path_list_lock;
117 	struct mutex path_mutex;
118 };
119 
120 /* from hda_bus to avs_dev */
121 #define hda_to_avs(hda) container_of(hda, struct avs_dev, base)
122 /* from hdac_bus to avs_dev */
123 #define hdac_to_avs(hdac) hda_to_avs(to_hda_bus(hdac))
124 /* from device to avs_dev */
125 #define to_avs_dev(dev) \
126 ({ \
127 	struct hdac_bus *__bus = dev_get_drvdata(dev); \
128 	hdac_to_avs(__bus); \
129 })
130 
131 int avs_dsp_core_power(struct avs_dev *adev, u32 core_mask, bool power);
132 int avs_dsp_core_reset(struct avs_dev *adev, u32 core_mask, bool reset);
133 int avs_dsp_core_stall(struct avs_dev *adev, u32 core_mask, bool stall);
134 int avs_dsp_core_enable(struct avs_dev *adev, u32 core_mask);
135 int avs_dsp_core_disable(struct avs_dev *adev, u32 core_mask);
136 
137 /* Inter Process Communication */
138 
139 struct avs_ipc_msg {
140 	union {
141 		u64 header;
142 		union avs_global_msg glb;
143 		union avs_reply_msg rsp;
144 	};
145 	void *data;
146 	size_t size;
147 };
148 
149 /*
150  * struct avs_ipc - DSP IPC context
151  *
152  * @dev: PCI device
153  * @rx: Reply message cache
154  * @default_timeout_ms: default message timeout in MS
155  * @ready: whether firmware is ready and communication is open
156  * @rx_completed: whether RX for previously sent TX has been received
157  * @rx_lock: for serializing manipulation of rx_* fields
158  * @msg_lock: for synchronizing request handling
159  * @done_completion: DONE-part of IPC i.e. ROM and ACKs from FW
160  * @busy_completion: BUSY-part of IPC i.e. receiving responses from FW
161  */
162 struct avs_ipc {
163 	struct device *dev;
164 
165 	struct avs_ipc_msg rx;
166 	u32 default_timeout_ms;
167 	bool ready;
168 	atomic_t recovering;
169 
170 	bool rx_completed;
171 	spinlock_t rx_lock;
172 	struct mutex msg_mutex;
173 	struct completion done_completion;
174 	struct completion busy_completion;
175 
176 	struct work_struct recovery_work;
177 };
178 
179 #define AVS_EIPC	EREMOTEIO
180 /*
181  * IPC handlers may return positive value (firmware error code) what denotes
182  * successful HOST <-> DSP communication yet failure to process specific request.
183  *
184  * Below macro converts returned value to linux kernel error code.
185  * All IPC callers MUST use it as soon as firmware error code is consumed.
186  */
187 #define AVS_IPC_RET(ret) \
188 	(((ret) <= 0) ? (ret) : -AVS_EIPC)
189 
190 static inline void avs_ipc_err(struct avs_dev *adev, struct avs_ipc_msg *tx,
191 			       const char *name, int error)
192 {
193 	/*
194 	 * If IPC channel is blocked e.g.: due to ongoing recovery,
195 	 * -EPERM error code is expected and thus it's not an actual error.
196 	 */
197 	if (error == -EPERM)
198 		dev_dbg(adev->dev, "%s 0x%08x 0x%08x failed: %d\n", name,
199 			tx->glb.primary, tx->glb.ext.val, error);
200 	else
201 		dev_err(adev->dev, "%s 0x%08x 0x%08x failed: %d\n", name,
202 			tx->glb.primary, tx->glb.ext.val, error);
203 }
204 
205 irqreturn_t avs_dsp_irq_handler(int irq, void *dev_id);
206 irqreturn_t avs_dsp_irq_thread(int irq, void *dev_id);
207 void avs_dsp_process_response(struct avs_dev *adev, u64 header);
208 int avs_dsp_send_msg_timeout(struct avs_dev *adev,
209 			     struct avs_ipc_msg *request,
210 			     struct avs_ipc_msg *reply, int timeout);
211 int avs_dsp_send_msg(struct avs_dev *adev,
212 		     struct avs_ipc_msg *request, struct avs_ipc_msg *reply);
213 int avs_dsp_send_rom_msg_timeout(struct avs_dev *adev,
214 				 struct avs_ipc_msg *request, int timeout);
215 int avs_dsp_send_rom_msg(struct avs_dev *adev, struct avs_ipc_msg *request);
216 void avs_dsp_interrupt_control(struct avs_dev *adev, bool enable);
217 int avs_ipc_init(struct avs_ipc *ipc, struct device *dev);
218 void avs_ipc_block(struct avs_ipc *ipc);
219 
220 /* Firmware resources management */
221 
222 int avs_get_module_entry(struct avs_dev *adev, const guid_t *uuid, struct avs_module_entry *entry);
223 int avs_get_module_id_entry(struct avs_dev *adev, u32 module_id, struct avs_module_entry *entry);
224 int avs_get_module_id(struct avs_dev *adev, const guid_t *uuid);
225 bool avs_is_module_ida_empty(struct avs_dev *adev, u32 module_id);
226 
227 int avs_module_info_init(struct avs_dev *adev, bool purge);
228 void avs_module_info_free(struct avs_dev *adev);
229 int avs_module_id_alloc(struct avs_dev *adev, u16 module_id);
230 void avs_module_id_free(struct avs_dev *adev, u16 module_id, u8 instance_id);
231 int avs_request_firmware(struct avs_dev *adev, const struct firmware **fw_p, const char *name);
232 void avs_release_last_firmware(struct avs_dev *adev);
233 void avs_release_firmwares(struct avs_dev *adev);
234 
235 int avs_dsp_init_module(struct avs_dev *adev, u16 module_id, u8 ppl_instance_id,
236 			u8 core_id, u8 domain, void *param, u32 param_size,
237 			u16 *instance_id);
238 void avs_dsp_delete_module(struct avs_dev *adev, u16 module_id, u16 instance_id,
239 			   u8 ppl_instance_id, u8 core_id);
240 int avs_dsp_create_pipeline(struct avs_dev *adev, u16 req_size, u8 priority,
241 			    bool lp, u16 attributes, u8 *instance_id);
242 int avs_dsp_delete_pipeline(struct avs_dev *adev, u8 instance_id);
243 
244 /* Firmware loading */
245 
246 void avs_hda_clock_gating_enable(struct avs_dev *adev, bool enable);
247 void avs_hda_power_gating_enable(struct avs_dev *adev, bool enable);
248 void avs_hda_l1sen_enable(struct avs_dev *adev, bool enable);
249 
250 int avs_dsp_load_libraries(struct avs_dev *adev, struct avs_tplg_library *libs, u32 num_libs);
251 int avs_dsp_boot_firmware(struct avs_dev *adev, bool purge);
252 int avs_dsp_first_boot_firmware(struct avs_dev *adev);
253 
254 int avs_cldma_load_basefw(struct avs_dev *adev, struct firmware *fw);
255 int avs_cldma_load_library(struct avs_dev *adev, struct firmware *lib, u32 id);
256 int avs_cldma_transfer_modules(struct avs_dev *adev, bool load,
257 			       struct avs_module_entry *mods, u32 num_mods);
258 int avs_hda_load_basefw(struct avs_dev *adev, struct firmware *fw);
259 int avs_hda_load_library(struct avs_dev *adev, struct firmware *lib, u32 id);
260 int avs_hda_transfer_modules(struct avs_dev *adev, bool load,
261 			     struct avs_module_entry *mods, u32 num_mods);
262 
263 /* Soc component members */
264 
265 struct avs_soc_component {
266 	struct snd_soc_component base;
267 	struct avs_tplg *tplg;
268 
269 	struct list_head node;
270 };
271 
272 #define to_avs_soc_component(comp) \
273 	container_of(comp, struct avs_soc_component, base)
274 
275 extern const struct snd_soc_dai_ops avs_dai_fe_ops;
276 
277 int avs_dmic_platform_register(struct avs_dev *adev, const char *name);
278 int avs_i2s_platform_register(struct avs_dev *adev, const char *name, unsigned long port_mask,
279 			      unsigned long *tdms);
280 int avs_hda_platform_register(struct avs_dev *adev, const char *name);
281 
282 #endif /* __SOUND_SOC_INTEL_AVS_H */
283