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