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