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