avs.h 13.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* SPDX-License-Identifier: GPL-2.0-only */
/*
 * Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
 *
 * Authors: Cezary Rojewski <cezary.rojewski@intel.com>
 *          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
 */

#ifndef __SOUND_SOC_INTEL_AVS_H
#define __SOUND_SOC_INTEL_AVS_H

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 25
struct avs_tplg_library;
struct avs_soc_component;
26
struct avs_ipc_msg;
27

28 29 30 31 32 33 34
#ifdef CONFIG_ACPI
#define AVS_S0IX_SUPPORTED \
	(acpi_gbl_FADT.flags & ACPI_FADT_LOW_POWER_S0)
#else
#define AVS_S0IX_SUPPORTED false
#endif

35 36 37 38 39 40 41 42 43 44
/*
 * struct avs_dsp_ops - Platform-specific DSP operations
 *
 * @power: Power on or off DSP cores
 * @reset: Enter or exit reset state on DSP cores
 * @stall: Stall or run DSP cores
 * @irq_handler: Top half of IPC servicing
 * @irq_thread: Bottom half of IPC servicing
 * @int_control: Enable or disable IPC interrupts
 */
45 46 47 48
struct avs_dsp_ops {
	int (* const power)(struct avs_dev *, u32, bool);
	int (* const reset)(struct avs_dev *, u32, bool);
	int (* const stall)(struct avs_dev *, u32, bool);
49 50 51
	irqreturn_t (* const irq_handler)(int, void *);
	irqreturn_t (* const irq_thread)(int, void *);
	void (* const int_control)(struct avs_dev *, bool);
52 53 54
	int (* const load_basefw)(struct avs_dev *, struct firmware *);
	int (* const load_lib)(struct avs_dev *, struct firmware *, u32);
	int (* const transfer_mods)(struct avs_dev *, bool, struct avs_module_entry *, u32);
55 56 57 58
	int (* const enable_logs)(struct avs_dev *, enum avs_log_enable, u32, u32, unsigned long,
				  u32 *);
	int (* const log_buffer_offset)(struct avs_dev *, u32);
	int (* const log_buffer_status)(struct avs_dev *, union avs_notify_msg *);
59
	int (* const coredump)(struct avs_dev *, union avs_notify_msg *);
60 61
	bool (* const d0ix_toggle)(struct avs_dev *, struct avs_ipc_msg *, bool);
	int (* const set_d0ix)(struct avs_dev *, bool);
62 63 64 65 66
};

#define avs_dsp_op(adev, op, ...) \
	((adev)->spec->dsp_ops->op(adev, ## __VA_ARGS__))

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 74 75 76 77 78 79 80
#define avs_platattr_test(adev, attr) \
	((adev)->spec->attributes & AVS_PLATATTR_##attr)

/* Platform specific descriptor */
struct avs_spec {
	const char *name;

	const struct avs_dsp_ops *const dsp_ops;
81
	struct avs_fw_version min_fw_version; /* anything below is rejected */
82 83 84

	const u32 core_init_mask;	/* used during DSP boot */
	const u64 attributes;		/* bitmask of AVS_PLATATTR_* */
85 86 87
	const u32 sram_base_offset;
	const u32 sram_window_size;
	const u32 rom_status;
88 89
};

90 91 92 93 94 95 96
struct avs_fw_entry {
	char *name;
	const struct firmware *fw;

	struct list_head node;
};

97 98 99 100 101 102
/*
 * struct avs_dev - Intel HD-Audio driver data
 *
 * @dev: PCI device
 * @dsp_ba: DSP bar address
 * @spec: platform-specific descriptor
103 104 105 106 107 108 109
 * @fw_cfg: Firmware configuration, obtained through FW_CONFIG message
 * @hw_cfg: Hardware configuration, obtained through HW_CONFIG message
 * @mods_info: Available module-types, obtained through MODULES_INFO message
 * @mod_idas: Module instance ID pool, one per module-type
 * @modres_mutex: For synchronizing any @mods_info updates
 * @ppl_ida: Pipeline instance ID pool
 * @fw_list: List of libraries loaded, including base firmware
110 111 112 113 114 115 116
 */
struct avs_dev {
	struct hda_bus base;
	struct device *dev;

	void __iomem *dsp_ba;
	const struct avs_spec *spec;
117 118
	struct avs_ipc *ipc;

119 120 121 122 123 124 125
	struct avs_fw_cfg fw_cfg;
	struct avs_hw_cfg hw_cfg;
	struct avs_mods_info *mods_info;
	struct ida **mod_idas;
	struct mutex modres_mutex;
	struct ida ppl_ida;
	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 135 136 137 138
	struct list_head comp_list;
	struct mutex comp_list_mutex;
	struct list_head path_list;
	spinlock_t path_list_lock;
	struct mutex path_mutex;
139

140 141 142 143 144 145 146
	spinlock_t trace_lock;	/* serialize debug window I/O between each LOG_BUFFER_STATUS */
#ifdef CONFIG_DEBUG_FS
	struct kfifo trace_fifo;
	wait_queue_head_t trace_waitq;
	u32 aging_timer_period;
	u32 fifo_full_timer_period;
	u32 logged_resources;	/* context dependent: core or library */
147
	struct dentry *debugfs_root;
148 149 150
	/* probes */
	struct hdac_ext_stream *extractor;
	unsigned int num_probe_streams;
151
#endif
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
};

/* from hda_bus to avs_dev */
#define hda_to_avs(hda) container_of(hda, struct avs_dev, base)
/* from hdac_bus to avs_dev */
#define hdac_to_avs(hdac) hda_to_avs(to_hda_bus(hdac))
/* from device to avs_dev */
#define to_avs_dev(dev) \
({ \
	struct hdac_bus *__bus = dev_get_drvdata(dev); \
	hdac_to_avs(__bus); \
})

int avs_dsp_core_power(struct avs_dev *adev, u32 core_mask, bool power);
int avs_dsp_core_reset(struct avs_dev *adev, u32 core_mask, bool reset);
int avs_dsp_core_stall(struct avs_dev *adev, u32 core_mask, bool stall);
int avs_dsp_core_enable(struct avs_dev *adev, u32 core_mask);
int avs_dsp_core_disable(struct avs_dev *adev, u32 core_mask);

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
/* Inter Process Communication */

struct avs_ipc_msg {
	union {
		u64 header;
		union avs_global_msg glb;
		union avs_reply_msg rsp;
	};
	void *data;
	size_t size;
};

/*
 * struct avs_ipc - DSP IPC context
 *
 * @dev: PCI device
 * @rx: Reply message cache
 * @default_timeout_ms: default message timeout in MS
 * @ready: whether firmware is ready and communication is open
 * @rx_completed: whether RX for previously sent TX has been received
 * @rx_lock: for serializing manipulation of rx_* fields
 * @msg_lock: for synchronizing request handling
 * @done_completion: DONE-part of IPC i.e. ROM and ACKs from FW
 * @busy_completion: BUSY-part of IPC i.e. receiving responses from FW
 */
struct avs_ipc {
	struct device *dev;

	struct avs_ipc_msg rx;
	u32 default_timeout_ms;
	bool ready;
202
	atomic_t recovering;
203 204 205 206 207 208

	bool rx_completed;
	spinlock_t rx_lock;
	struct mutex msg_mutex;
	struct completion done_completion;
	struct completion busy_completion;
209 210

	struct work_struct recovery_work;
211 212 213
	struct delayed_work d0ix_work;
	atomic_t d0ix_disable_depth;
	bool in_d0ix;
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
};

#define AVS_EIPC	EREMOTEIO
/*
 * IPC handlers may return positive value (firmware error code) what denotes
 * successful HOST <-> DSP communication yet failure to process specific request.
 *
 * Below macro converts returned value to linux kernel error code.
 * All IPC callers MUST use it as soon as firmware error code is consumed.
 */
#define AVS_IPC_RET(ret) \
	(((ret) <= 0) ? (ret) : -AVS_EIPC)

static inline void avs_ipc_err(struct avs_dev *adev, struct avs_ipc_msg *tx,
			       const char *name, int error)
{
	/*
	 * If IPC channel is blocked e.g.: due to ongoing recovery,
	 * -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 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		dev_dbg(adev->dev, "%s 0x%08x 0x%08x failed: %d\n", name,
			tx->glb.primary, tx->glb.ext.val, error);
	else
		dev_err(adev->dev, "%s 0x%08x 0x%08x failed: %d\n", name,
			tx->glb.primary, tx->glb.ext.val, error);
}

irqreturn_t avs_dsp_irq_handler(int irq, void *dev_id);
irqreturn_t avs_dsp_irq_thread(int irq, void *dev_id);
void avs_dsp_process_response(struct avs_dev *adev, u64 header);
int avs_dsp_send_msg_timeout(struct avs_dev *adev,
			     struct avs_ipc_msg *request,
			     struct avs_ipc_msg *reply, int timeout);
int avs_dsp_send_msg(struct avs_dev *adev,
		     struct avs_ipc_msg *request, struct avs_ipc_msg *reply);
252 253 254 255 256
/* Two variants below are for messages that control DSP power states. */
int avs_dsp_send_pm_msg_timeout(struct avs_dev *adev, struct avs_ipc_msg *request,
				struct avs_ipc_msg *reply, int timeout, bool wake_d0i0);
int avs_dsp_send_pm_msg(struct avs_dev *adev, struct avs_ipc_msg *request,
			struct avs_ipc_msg *reply, bool wake_d0i0);
257 258 259 260 261 262 263
int avs_dsp_send_rom_msg_timeout(struct avs_dev *adev,
				 struct avs_ipc_msg *request, int timeout);
int avs_dsp_send_rom_msg(struct avs_dev *adev, struct avs_ipc_msg *request);
void avs_dsp_interrupt_control(struct avs_dev *adev, bool enable);
int avs_ipc_init(struct avs_ipc *ipc, struct device *dev);
void avs_ipc_block(struct avs_ipc *ipc);

264 265 266
int avs_dsp_disable_d0ix(struct avs_dev *adev);
int avs_dsp_enable_d0ix(struct avs_dev *adev);

267 268
int skl_log_buffer_offset(struct avs_dev *adev, u32 core);

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
/* Firmware resources management */

int avs_get_module_entry(struct avs_dev *adev, const guid_t *uuid, struct avs_module_entry *entry);
int avs_get_module_id_entry(struct avs_dev *adev, u32 module_id, struct avs_module_entry *entry);
int avs_get_module_id(struct avs_dev *adev, const guid_t *uuid);
bool avs_is_module_ida_empty(struct avs_dev *adev, u32 module_id);

int avs_module_info_init(struct avs_dev *adev, bool purge);
void avs_module_info_free(struct avs_dev *adev);
int avs_module_id_alloc(struct avs_dev *adev, u16 module_id);
void avs_module_id_free(struct avs_dev *adev, u16 module_id, u8 instance_id);
int avs_request_firmware(struct avs_dev *adev, const struct firmware **fw_p, const char *name);
void avs_release_last_firmware(struct avs_dev *adev);
void avs_release_firmwares(struct avs_dev *adev);

284 285
int avs_dsp_init_module(struct avs_dev *adev, u16 module_id, u8 ppl_instance_id,
			u8 core_id, u8 domain, void *param, u32 param_size,
286 287
			u8 *instance_id);
void avs_dsp_delete_module(struct avs_dev *adev, u16 module_id, u8 instance_id,
288 289 290 291 292
			   u8 ppl_instance_id, u8 core_id);
int avs_dsp_create_pipeline(struct avs_dev *adev, u16 req_size, u8 priority,
			    bool lp, u16 attributes, u8 *instance_id);
int avs_dsp_delete_pipeline(struct avs_dev *adev, u8 instance_id);

293 294 295 296 297 298
/* Firmware loading */

void avs_hda_clock_gating_enable(struct avs_dev *adev, bool enable);
void avs_hda_power_gating_enable(struct avs_dev *adev, bool enable);
void avs_hda_l1sen_enable(struct avs_dev *adev, bool enable);

299
int avs_dsp_load_libraries(struct avs_dev *adev, struct avs_tplg_library *libs, u32 num_libs);
300 301 302
int avs_dsp_boot_firmware(struct avs_dev *adev, bool purge);
int avs_dsp_first_boot_firmware(struct avs_dev *adev);

303 304 305 306
int avs_cldma_load_basefw(struct avs_dev *adev, struct firmware *fw);
int avs_cldma_load_library(struct avs_dev *adev, struct firmware *lib, u32 id);
int avs_cldma_transfer_modules(struct avs_dev *adev, bool load,
			       struct avs_module_entry *mods, u32 num_mods);
307 308 309 310
int avs_hda_load_basefw(struct avs_dev *adev, struct firmware *fw);
int avs_hda_load_library(struct avs_dev *adev, struct firmware *lib, u32 id);
int avs_hda_transfer_modules(struct avs_dev *adev, bool load,
			     struct avs_module_entry *mods, u32 num_mods);
311

312 313 314 315 316 317 318 319 320 321 322 323
/* Soc component members */

struct avs_soc_component {
	struct snd_soc_component base;
	struct avs_tplg *tplg;

	struct list_head node;
};

#define to_avs_soc_component(comp) \
	container_of(comp, struct avs_soc_component, base)

324 325
extern const struct snd_soc_dai_ops avs_dai_fe_ops;

326 327 328
int avs_soc_component_register(struct device *dev, const char *name,
			       const struct snd_soc_component_driver *drv,
			       struct snd_soc_dai_driver *cpu_dais, int num_cpu_dais);
329 330 331
int avs_dmic_platform_register(struct avs_dev *adev, const char *name);
int avs_i2s_platform_register(struct avs_dev *adev, const char *name, unsigned long port_mask,
			      unsigned long *tdms);
332
int avs_hda_platform_register(struct avs_dev *adev, const char *name);
333

334 335 336
int avs_register_all_boards(struct avs_dev *adev);
void avs_unregister_all_boards(struct avs_dev *adev);

337 338 339 340 341 342 343 344 345 346 347 348
/* Firmware tracing helpers */

#define avs_log_buffer_size(adev) \
	((adev)->fw_cfg.trace_log_bytes / (adev)->hw_cfg.dsp_cores)

#define avs_log_buffer_addr(adev, core) \
({ \
	s32 __offset = avs_dsp_op(adev, log_buffer_offset, core); \
	(__offset < 0) ? NULL : \
			 (avs_sram_addr(adev, AVS_DEBUG_WINDOW) + __offset); \
})

349 350 351 352 353
static inline int avs_log_buffer_status_locked(struct avs_dev *adev, union avs_notify_msg *msg)
{
	unsigned long flags;
	int ret;

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 359 360

	return ret;
}

361 362 363 364 365 366 367 368 369 370 371 372
struct apl_log_buffer_layout {
	u32 read_ptr;
	u32 write_ptr;
	u8 buffer[];
} __packed;

#define apl_log_payload_size(adev) \
	(avs_log_buffer_size(adev) - sizeof(struct apl_log_buffer_layout))

#define apl_log_payload_addr(addr) \
	(addr + sizeof(struct apl_log_buffer_layout))

373
#ifdef CONFIG_DEBUG_FS
374 375 376
#define AVS_SET_ENABLE_LOGS_OP(name) \
	.enable_logs = name##_enable_logs

377 378 379
bool avs_logging_fw(struct avs_dev *adev);
void avs_dump_fw_log(struct avs_dev *adev, const void __iomem *src, unsigned int len);
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 384

void avs_debugfs_init(struct avs_dev *adev);
void avs_debugfs_exit(struct avs_dev *adev);
385
#else
386 387
#define AVS_SET_ENABLE_LOGS_OP(name)

388 389 390 391 392 393 394 395 396 397 398 399 400
static inline bool avs_logging_fw(struct avs_dev *adev)
{
	return false;
}

static inline void avs_dump_fw_log(struct avs_dev *adev, const void __iomem *src, unsigned int len)
{
}

static inline void
avs_dump_fw_log_wakeup(struct avs_dev *adev, const void __iomem *src, unsigned int len)
{
}
401 402 403 404 405

static inline int avs_probe_platform_register(struct avs_dev *adev, const char *name)
{
	return 0;
}
406 407 408

static inline void avs_debugfs_init(struct avs_dev *adev) { }
static inline void avs_debugfs_exit(struct avs_dev *adev) { }
409 410
#endif

411
#endif /* __SOUND_SOC_INTEL_AVS_H */