xref: /linux/sound/soc/intel/avs/avs.h (revision a9e6060bb2a6cae6d43a98ec0794844ad01273d3)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright(c) 2021-2022 Intel Corporation
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/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/firmware.h>
15 #include <linux/kfifo.h>
16 #include <sound/hda_codec.h>
17 #include <sound/hda_register.h>
18 #include <sound/soc-component.h>
19 #include "messages.h"
20 #include "registers.h"
21 
22 struct avs_dev;
23 struct avs_tplg;
24 struct avs_tplg_library;
25 struct avs_soc_component;
26 struct avs_ipc_msg;
27 
28 #ifdef CONFIG_ACPI
29 #define AVS_S0IX_SUPPORTED \
30 	(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)
31 #else
32 #define AVS_S0IX_SUPPORTED false
33 #endif
34 
35 /*
36  * struct avs_dsp_ops - Platform-specific DSP operations
37  *
38  * @power: Power on or off DSP cores
39  * @reset: Enter or exit reset state on DSP cores
40  * @stall: Stall or run DSP cores
41  * @irq_handler: Top half of IPC servicing
42  * @irq_thread: Bottom half of IPC servicing
43  * @int_control: Enable or disable IPC interrupts
44  */
45 struct avs_dsp_ops {
46 	int (* const power)(struct avs_dev *, u32, bool);
47 	int (* const reset)(struct avs_dev *, u32, bool);
48 	int (* const stall)(struct avs_dev *, u32, bool);
49 	irqreturn_t (* const dsp_interrupt)(struct avs_dev *);
50 	void (* const int_control)(struct avs_dev *, bool);
51 	int (* const load_basefw)(struct avs_dev *, struct firmware *);
52 	int (* const load_lib)(struct avs_dev *, struct firmware *, u32);
53 	int (* const transfer_mods)(struct avs_dev *, bool, struct avs_module_entry *, u32);
54 	int (* const config_basefw)(struct avs_dev *);
55 	int (* const enable_logs)(struct avs_dev *, enum avs_log_enable, u32, u32, unsigned long,
56 				  u32 *);
57 	int (* const log_buffer_offset)(struct avs_dev *, u32);
58 	int (* const log_buffer_status)(struct avs_dev *, union avs_notify_msg *);
59 	int (* const coredump)(struct avs_dev *, union avs_notify_msg *);
60 	bool (* const d0ix_toggle)(struct avs_dev *, struct avs_ipc_msg *, bool);
61 	int (* const set_d0ix)(struct avs_dev *, bool);
62 };
63 
64 #define avs_dsp_op(adev, op, ...) \
65 	((adev)->spec->dsp_ops->op(adev, ## __VA_ARGS__))
66 
67 extern const struct avs_dsp_ops avs_skl_dsp_ops;
68 extern const struct avs_dsp_ops avs_apl_dsp_ops;
69 extern const struct avs_dsp_ops avs_cnl_dsp_ops;
70 extern const struct avs_dsp_ops avs_icl_dsp_ops;
71 extern const struct avs_dsp_ops avs_tgl_dsp_ops;
72 extern const struct avs_dsp_ops avs_ptl_dsp_ops;
73 
74 #define AVS_PLATATTR_CLDMA		BIT_ULL(0)
75 #define AVS_PLATATTR_IMR		BIT_ULL(1)
76 #define AVS_PLATATTR_ACE		BIT_ULL(2)
77 #define AVS_PLATATTR_ALTHDA		BIT_ULL(3)
78 
79 #define avs_platattr_test(adev, attr) \
80 	((adev)->spec->attributes & AVS_PLATATTR_##attr)
81 
82 struct avs_sram_spec {
83 	const u32 base_offset;
84 	const u32 window_size;
85 };
86 
87 struct avs_hipc_spec {
88 	const u32 req_offset;
89 	const u32 req_ext_offset;
90 	const u32 req_busy_mask;
91 	const u32 ack_offset;
92 	const u32 ack_done_mask;
93 	const u32 rsp_offset;
94 	const u32 rsp_busy_mask;
95 	const u32 ctl_offset;
96 	const u32 sts_offset;
97 };
98 
99 /* Platform specific descriptor */
100 struct avs_spec {
101 	const char *name;
102 
103 	const struct avs_dsp_ops *const dsp_ops;
104 	struct avs_fw_version min_fw_version; /* anything below is rejected */
105 
106 	const u32 core_init_mask;	/* used during DSP boot */
107 	const u64 attributes;		/* bitmask of AVS_PLATATTR_* */
108 	const struct avs_sram_spec *sram;
109 	const struct avs_hipc_spec *hipc;
110 };
111 
112 struct avs_fw_entry {
113 	const char *name;
114 	const struct firmware *fw;
115 
116 	struct list_head node;
117 };
118 
119 /*
120  * struct avs_dev - Intel HD-Audio driver data
121  *
122  * @dev: PCI device
123  * @dsp_ba: DSP bar address
124  * @spec: platform-specific descriptor
125  * @fw_cfg: Firmware configuration, obtained through FW_CONFIG message
126  * @hw_cfg: Hardware configuration, obtained through HW_CONFIG message
127  * @mods_info: Available module-types, obtained through MODULES_INFO message
128  * @mod_idas: Module instance ID pool, one per module-type
129  * @modres_mutex: For synchronizing any @mods_info updates
130  * @ppl_ida: Pipeline instance ID pool
131  * @fw_list: List of libraries loaded, including base firmware
132  */
133 struct avs_dev {
134 	struct hda_bus base;
135 	struct device *dev;
136 
137 	void __iomem *dsp_ba;
138 	const struct avs_spec *spec;
139 	struct avs_ipc *ipc;
140 
141 	struct avs_fw_cfg fw_cfg;
142 	struct avs_hw_cfg hw_cfg;
143 	struct avs_mods_info *mods_info;
144 	struct ida **mod_idas;
145 	struct mutex modres_mutex;
146 	void *modcfg_buf;		/* module configuration buffer */
147 	struct ida ppl_ida;
148 	struct list_head fw_list;
149 	int *core_refs;		/* reference count per core */
150 	char **lib_names;
151 	int num_lp_paths;
152 	atomic_t l1sen_counter;	/* controls whether L1SEN should be disabled */
153 
154 	struct completion fw_ready;
155 	struct work_struct probe_work;
156 
157 	struct list_head comp_list;
158 	struct mutex comp_list_mutex;
159 	struct list_head path_list;
160 	spinlock_t path_list_lock;
161 	struct mutex path_mutex;
162 
163 	spinlock_t trace_lock;	/* serialize debug window I/O between each LOG_BUFFER_STATUS */
164 #ifdef CONFIG_DEBUG_FS
165 	struct kfifo trace_fifo;
166 	wait_queue_head_t trace_waitq;
167 	u32 aging_timer_period;
168 	u32 fifo_full_timer_period;
169 	u32 logged_resources;	/* context dependent: core or library */
170 	struct dentry *debugfs_root;
171 	/* probes */
172 	struct hdac_ext_stream *extractor;
173 	unsigned int num_probe_streams;
174 #endif
175 };
176 
177 /* from hda_bus to avs_dev */
178 #define hda_to_avs(hda) container_of(hda, struct avs_dev, base)
179 /* from hdac_bus to avs_dev */
180 #define hdac_to_avs(hdac) hda_to_avs(to_hda_bus(hdac))
181 /* from device to avs_dev */
182 #define to_avs_dev(dev) \
183 ({ \
184 	struct hdac_bus *__bus = dev_get_drvdata(dev); \
185 	hdac_to_avs(__bus); \
186 })
187 
188 int avs_dsp_core_power(struct avs_dev *adev, u32 core_mask, bool power);
189 int avs_dsp_core_reset(struct avs_dev *adev, u32 core_mask, bool reset);
190 int avs_dsp_core_stall(struct avs_dev *adev, u32 core_mask, bool stall);
191 int avs_dsp_core_enable(struct avs_dev *adev, u32 core_mask);
192 int avs_dsp_core_disable(struct avs_dev *adev, u32 core_mask);
193 
194 /* Inter Process Communication */
195 
196 struct avs_ipc_msg {
197 	union {
198 		u64 header;
199 		union avs_global_msg glb;
200 		union avs_reply_msg rsp;
201 	};
202 	void *data;
203 	size_t size;
204 };
205 
206 /*
207  * struct avs_ipc - DSP IPC context
208  *
209  * @dev: PCI device
210  * @rx: Reply message cache
211  * @default_timeout_ms: default message timeout in MS
212  * @ready: whether firmware is ready and communication is open
213  * @rx_completed: whether RX for previously sent TX has been received
214  * @rx_lock: for serializing manipulation of rx_* fields
215  * @msg_lock: for synchronizing request handling
216  * @done_completion: DONE-part of IPC i.e. ROM and ACKs from FW
217  * @busy_completion: BUSY-part of IPC i.e. receiving responses from FW
218  */
219 struct avs_ipc {
220 	struct device *dev;
221 
222 	struct avs_ipc_msg rx;
223 	u32 default_timeout_ms;
224 	bool ready;
225 	atomic_t recovering;
226 
227 	bool rx_completed;
228 	spinlock_t rx_lock;
229 	struct mutex msg_mutex;
230 	struct completion done_completion;
231 	struct completion busy_completion;
232 
233 	struct work_struct recovery_work;
234 	struct delayed_work d0ix_work;
235 	atomic_t d0ix_disable_depth;
236 	bool in_d0ix;
237 };
238 
239 #define AVS_EIPC	EREMOTEIO
240 /*
241  * IPC handlers may return positive value (firmware error code) what denotes
242  * successful HOST <-> DSP communication yet failure to process specific request.
243  *
244  * Below macro converts returned value to linux kernel error code.
245  * All IPC callers MUST use it as soon as firmware error code is consumed.
246  */
247 #define AVS_IPC_RET(ret) \
248 	(((ret) <= 0) ? (ret) : -AVS_EIPC)
249 
250 void avs_dsp_process_response(struct avs_dev *adev, u64 header);
251 int avs_dsp_send_msg_timeout(struct avs_dev *adev, struct avs_ipc_msg *request,
252 			     struct avs_ipc_msg *reply, int timeout, const char *name);
253 int avs_dsp_send_msg(struct avs_dev *adev, struct avs_ipc_msg *request,
254 		     struct avs_ipc_msg *reply, const char *name);
255 /* Two variants below are for messages that control DSP power states. */
256 int avs_dsp_send_pm_msg_timeout(struct avs_dev *adev, struct avs_ipc_msg *request,
257 				struct avs_ipc_msg *reply, int timeout, bool wake_d0i0,
258 				const char *name);
259 int avs_dsp_send_pm_msg(struct avs_dev *adev, struct avs_ipc_msg *request,
260 			struct avs_ipc_msg *reply, bool wake_d0i0, const char *name);
261 int avs_dsp_send_rom_msg_timeout(struct avs_dev *adev, struct avs_ipc_msg *request, int timeout,
262 				 const char *name);
263 int avs_dsp_send_rom_msg(struct avs_dev *adev, struct avs_ipc_msg *request, const char *name);
264 void avs_dsp_interrupt_control(struct avs_dev *adev, bool enable);
265 int avs_ipc_init(struct avs_ipc *ipc, struct device *dev);
266 void avs_ipc_block(struct avs_ipc *ipc);
267 
268 int avs_dsp_disable_d0ix(struct avs_dev *adev);
269 int avs_dsp_enable_d0ix(struct avs_dev *adev);
270 
271 int avs_mtl_core_power(struct avs_dev *adev, u32 core_mask, bool power);
272 int avs_mtl_core_reset(struct avs_dev *adev, u32 core_mask, bool power);
273 int avs_mtl_core_stall(struct avs_dev *adev, u32 core_mask, bool stall);
274 int avs_lnl_core_stall(struct avs_dev *adev, u32 core_mask, bool stall);
275 void avs_mtl_interrupt_control(struct avs_dev *adev, bool enable);
276 void avs_skl_ipc_interrupt(struct avs_dev *adev);
277 irqreturn_t avs_cnl_dsp_interrupt(struct avs_dev *adev);
278 irqreturn_t avs_mtl_dsp_interrupt(struct avs_dev *adev);
279 int avs_apl_enable_logs(struct avs_dev *adev, enum avs_log_enable enable, u32 aging_period,
280 			u32 fifo_full_period, unsigned long resource_mask, u32 *priorities);
281 int avs_icl_enable_logs(struct avs_dev *adev, enum avs_log_enable enable, u32 aging_period,
282 			u32 fifo_full_period, unsigned long resource_mask, u32 *priorities);
283 int avs_skl_log_buffer_offset(struct avs_dev *adev, u32 core);
284 int avs_icl_log_buffer_offset(struct avs_dev *adev, u32 core);
285 int avs_apl_log_buffer_status(struct avs_dev *adev, union avs_notify_msg *msg);
286 int avs_apl_coredump(struct avs_dev *adev, union avs_notify_msg *msg);
287 bool avs_apl_d0ix_toggle(struct avs_dev *adev, struct avs_ipc_msg *tx, bool wake);
288 bool avs_icl_d0ix_toggle(struct avs_dev *adev, struct avs_ipc_msg *tx, bool wake);
289 int avs_apl_set_d0ix(struct avs_dev *adev, bool enable);
290 int avs_icl_set_d0ix(struct avs_dev *adev, bool enable);
291 
292 /* Firmware resources management */
293 
294 int avs_get_module_entry(struct avs_dev *adev, const guid_t *uuid, struct avs_module_entry *entry);
295 int avs_get_module_id_entry(struct avs_dev *adev, u32 module_id, struct avs_module_entry *entry);
296 int avs_get_module_id(struct avs_dev *adev, const guid_t *uuid);
297 bool avs_is_module_ida_empty(struct avs_dev *adev, u32 module_id);
298 
299 int avs_module_info_init(struct avs_dev *adev, bool purge);
300 void avs_module_info_free(struct avs_dev *adev);
301 int avs_module_id_alloc(struct avs_dev *adev, u16 module_id);
302 void avs_module_id_free(struct avs_dev *adev, u16 module_id, u8 instance_id);
303 int avs_request_firmware(struct avs_dev *adev, const struct firmware **fw_p, const char *name);
304 void avs_release_last_firmware(struct avs_dev *adev);
305 void avs_release_firmwares(struct avs_dev *adev);
306 
307 int avs_dsp_init_module(struct avs_dev *adev, u16 module_id, u8 ppl_instance_id,
308 			u8 core_id, u8 domain, void *param, u32 param_size,
309 			u8 *instance_id);
310 void avs_dsp_delete_module(struct avs_dev *adev, u16 module_id, u8 instance_id,
311 			   u8 ppl_instance_id, u8 core_id);
312 int avs_dsp_create_pipeline(struct avs_dev *adev, u16 req_size, u8 priority,
313 			    bool lp, u16 attributes, u8 *instance_id);
314 int avs_dsp_delete_pipeline(struct avs_dev *adev, u8 instance_id);
315 
316 /* Firmware loading */
317 
318 void avs_hda_clock_gating_enable(struct avs_dev *adev, bool enable);
319 void avs_hda_power_gating_enable(struct avs_dev *adev, bool enable);
320 void avs_hda_l1sen_enable(struct avs_dev *adev, bool enable);
321 
322 int avs_dsp_load_libraries(struct avs_dev *adev, struct avs_tplg_library *libs, u32 num_libs);
323 int avs_dsp_boot_firmware(struct avs_dev *adev, bool purge);
324 int avs_dsp_first_boot_firmware(struct avs_dev *adev);
325 
326 int avs_cldma_load_basefw(struct avs_dev *adev, struct firmware *fw);
327 int avs_cldma_load_library(struct avs_dev *adev, struct firmware *lib, u32 id);
328 int avs_cldma_transfer_modules(struct avs_dev *adev, bool load,
329 			       struct avs_module_entry *mods, u32 num_mods);
330 int avs_hda_load_basefw(struct avs_dev *adev, struct firmware *fw);
331 int avs_hda_load_library(struct avs_dev *adev, struct firmware *lib, u32 id);
332 int avs_hda_transfer_modules(struct avs_dev *adev, bool load,
333 			     struct avs_module_entry *mods, u32 num_mods);
334 
335 int avs_icl_load_basefw(struct avs_dev *adev, struct firmware *fw);
336 
337 /* Soc component members */
338 
339 struct avs_soc_component {
340 	struct snd_soc_component base;
341 	struct avs_tplg *tplg;
342 
343 	struct list_head node;
344 };
345 
346 #define to_avs_soc_component(comp) \
347 	container_of(comp, struct avs_soc_component, base)
348 
349 extern const struct snd_soc_dai_ops avs_dai_fe_ops;
350 
351 int avs_soc_component_register(struct device *dev, const char *name,
352 			       struct snd_soc_component_driver *drv,
353 			       struct snd_soc_dai_driver *cpu_dais, int num_cpu_dais);
354 int avs_dmic_platform_register(struct avs_dev *adev, const char *name);
355 int avs_i2s_platform_register(struct avs_dev *adev, const char *name, unsigned long port_mask,
356 			      unsigned long *tdms);
357 int avs_hda_platform_register(struct avs_dev *adev, const char *name);
358 
359 int avs_register_all_boards(struct avs_dev *adev);
360 void avs_unregister_all_boards(struct avs_dev *adev);
361 
362 /* Firmware tracing helpers */
363 
364 #define avs_log_buffer_size(adev) \
365 	((adev)->fw_cfg.trace_log_bytes / (adev)->hw_cfg.dsp_cores)
366 
367 #define avs_log_buffer_addr(adev, core) \
368 ({ \
369 	s32 __offset = avs_dsp_op(adev, log_buffer_offset, core); \
370 	(__offset < 0) ? NULL : \
371 			 (avs_sram_addr(adev, AVS_DEBUG_WINDOW) + __offset); \
372 })
373 
avs_log_buffer_status_locked(struct avs_dev * adev,union avs_notify_msg * msg)374 static inline int avs_log_buffer_status_locked(struct avs_dev *adev, union avs_notify_msg *msg)
375 {
376 	unsigned long flags;
377 	int ret;
378 
379 	spin_lock_irqsave(&adev->trace_lock, flags);
380 	ret = avs_dsp_op(adev, log_buffer_status, msg);
381 	spin_unlock_irqrestore(&adev->trace_lock, flags);
382 
383 	return ret;
384 }
385 
386 struct avs_apl_log_buffer_layout {
387 	u32 read_ptr;
388 	u32 write_ptr;
389 	u8 buffer[];
390 } __packed;
391 static_assert(sizeof(struct avs_apl_log_buffer_layout) == 8);
392 
393 #define avs_apl_log_payload_size(adev) \
394 	(avs_log_buffer_size(adev) - sizeof(struct avs_apl_log_buffer_layout))
395 
396 #define avs_apl_log_payload_addr(addr) \
397 	(addr + sizeof(struct avs_apl_log_buffer_layout))
398 
399 #ifdef CONFIG_DEBUG_FS
400 #define AVS_SET_ENABLE_LOGS_OP(name) \
401 	.enable_logs = avs_##name##_enable_logs
402 
403 bool avs_logging_fw(struct avs_dev *adev);
404 void avs_dump_fw_log(struct avs_dev *adev, const void __iomem *src, unsigned int len);
405 void avs_dump_fw_log_wakeup(struct avs_dev *adev, const void __iomem *src, unsigned int len);
406 
407 int avs_probe_platform_register(struct avs_dev *adev, const char *name);
408 
409 void avs_debugfs_init(struct avs_dev *adev);
410 void avs_debugfs_exit(struct avs_dev *adev);
411 #else
412 #define AVS_SET_ENABLE_LOGS_OP(name)
413 
avs_logging_fw(struct avs_dev * adev)414 static inline bool avs_logging_fw(struct avs_dev *adev)
415 {
416 	return false;
417 }
418 
avs_dump_fw_log(struct avs_dev * adev,const void __iomem * src,unsigned int len)419 static inline void avs_dump_fw_log(struct avs_dev *adev, const void __iomem *src, unsigned int len)
420 {
421 }
422 
423 static inline void
avs_dump_fw_log_wakeup(struct avs_dev * adev,const void __iomem * src,unsigned int len)424 avs_dump_fw_log_wakeup(struct avs_dev *adev, const void __iomem *src, unsigned int len)
425 {
426 }
427 
avs_probe_platform_register(struct avs_dev * adev,const char * name)428 static inline int avs_probe_platform_register(struct avs_dev *adev, const char *name)
429 {
430 	return 0;
431 }
432 
avs_debugfs_init(struct avs_dev * adev)433 static inline void avs_debugfs_init(struct avs_dev *adev) { }
avs_debugfs_exit(struct avs_dev * adev)434 static inline void avs_debugfs_exit(struct avs_dev *adev) { }
435 #endif
436 
437 /* Filesystems integration */
438 
439 extern const struct attribute_group *avs_attr_groups[];
440 
441 #endif /* __SOUND_SOC_INTEL_AVS_H */
442