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