1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 *  sst.h - Intel SST Driver for audio engine
4 *
5 *  Copyright (C) 2008-14 Intel Corporation
6 *  Authors:	Vinod Koul <vinod.koul@intel.com>
7 *		Harsha Priya <priya.harsha@intel.com>
8 *		Dharageswari R <dharageswari.r@intel.com>
9 *		KP Jeeja <jeeja.kp@intel.com>
10 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 *
12 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 *
14 *  Common private declarations for SST
15 */
16#ifndef __SST_H__
17#define __SST_H__
18
19#include <linux/firmware.h>
20
21/* driver names */
22#define SST_DRV_NAME "intel_sst_driver"
23#define SST_MRFLD_PCI_ID 0x119A
24#define SST_BYT_ACPI_ID	0x80860F28
25#define SST_CHV_ACPI_ID	0x808622A8
26
27#define SST_SUSPEND_DELAY 2000
28#define FW_CONTEXT_MEM (64*1024)
29#define SST_ICCM_BOUNDARY 4
30#define SST_CONFIG_SSP_SIGN 0x7ffe8001
31
32#define MRFLD_FW_VIRTUAL_BASE 0xC0000000
33#define MRFLD_FW_DDR_BASE_OFFSET 0x0
34#define MRFLD_FW_FEATURE_BASE_OFFSET 0x4
35#define MRFLD_FW_BSS_RESET_BIT 0
36
37/* SST Shim register map */
38#define SST_CSR			0x00
39#define SST_ISRX		0x18
40#define SST_IMRX		0x28
41#define SST_IPCX		0x38 /* IPC IA -> SST */
42#define SST_IPCD		0x40 /* IPC SST -> IA */
43
44extern const struct dev_pm_ops intel_sst_pm;
45enum sst_states {
46	SST_FW_LOADING = 1,
47	SST_FW_RUNNING,
48	SST_RESET,
49	SST_SHUTDOWN,
50};
51
52enum sst_algo_ops {
53	SST_SET_ALGO = 0,
54	SST_GET_ALGO = 1,
55};
56
57#define SST_BLOCK_TIMEOUT	1000
58
59#define FW_SIGNATURE_SIZE	4
60#define FW_NAME_SIZE		32
61
62/* stream states */
63enum sst_stream_states {
64	STREAM_UN_INIT	= 0,	/* Freed/Not used stream */
65	STREAM_RUNNING	= 1,	/* Running */
66	STREAM_PAUSED	= 2,	/* Paused stream */
67	STREAM_INIT	= 3,	/* stream init, waiting for data */
68};
69
70enum sst_ram_type {
71	SST_IRAM	= 1,
72	SST_DRAM	= 2,
73	SST_DDR	= 5,
74	SST_CUSTOM_INFO	= 7,	/* consists of FW binary information */
75};
76
77/* SST shim registers to structure mapping */
78union interrupt_reg {
79	struct {
80		u64 done_interrupt:1;
81		u64 busy_interrupt:1;
82		u64 rsvd:62;
83	} part;
84	u64 full;
85};
86
87union sst_pisr_reg {
88	struct {
89		u32 pssp0:1;
90		u32 pssp1:1;
91		u32 rsvd0:3;
92		u32 dmac:1;
93		u32 rsvd1:26;
94	} part;
95	u32 full;
96};
97
98union sst_pimr_reg {
99	struct {
100		u32 ssp0:1;
101		u32 ssp1:1;
102		u32 rsvd0:3;
103		u32 dmac:1;
104		u32 rsvd1:10;
105		u32 ssp0_sc:1;
106		u32 ssp1_sc:1;
107		u32 rsvd2:3;
108		u32 dmac_sc:1;
109		u32 rsvd3:10;
110	} part;
111	u32 full;
112};
113
114union config_status_reg_mrfld {
115	struct {
116		u64 lpe_reset:1;
117		u64 lpe_reset_vector:1;
118		u64 runstall:1;
119		u64 pwaitmode:1;
120		u64 clk_sel:3;
121		u64 rsvd2:1;
122		u64 sst_clk:3;
123		u64 xt_snoop:1;
124		u64 rsvd3:4;
125		u64 clk_sel1:6;
126		u64 clk_enable:3;
127		u64 rsvd4:6;
128		u64 slim0baseclk:1;
129		u64 rsvd:32;
130	} part;
131	u64 full;
132};
133
134union interrupt_reg_mrfld {
135	struct {
136		u64 done_interrupt:1;
137		u64 busy_interrupt:1;
138		u64 rsvd:62;
139	} part;
140	u64 full;
141};
142
143union sst_imr_reg_mrfld {
144	struct {
145		u64 done_interrupt:1;
146		u64 busy_interrupt:1;
147		u64 rsvd:62;
148	} part;
149	u64 full;
150};
151
152/**
153 * struct sst_block - This structure is used to block a user/fw data call to another
154 * fw/user call
155 *
156 * @condition: condition for blocking check
157 * @ret_code: ret code when block is released
158 * @data: data ptr
159 * @size: size of data
160 * @on: block condition
161 * @msg_id: msg_id = msgid in mfld/ctp, mrfld = NULL
162 * @drv_id: str_id in mfld/ctp, = drv_id in mrfld
163 * @node: list head node
164 */
165struct sst_block {
166	bool	condition;
167	int	ret_code;
168	void	*data;
169	u32     size;
170	bool	on;
171	u32     msg_id;
172	u32     drv_id;
173	struct list_head node;
174};
175
176/**
177 * struct stream_info - structure that holds the stream information
178 *
179 * @status : stream current state
180 * @prev : stream prev state
181 * @resume_status : stream current state to restore on resume
182 * @resume_prev : stream prev state to restore on resume
183 * @lock : stream mutex for protecting state
184 * @alloc_param : parameters used for stream (re-)allocation
185 * @pcm_substream : PCM substream
186 * @period_elapsed : PCM period elapsed callback
187 * @sfreq : stream sampling freq
188 * @cumm_bytes : cummulative bytes decoded
189 */
190struct stream_info {
191	unsigned int		status;
192	unsigned int		prev;
193	unsigned int		resume_status;
194	unsigned int		resume_prev;
195	struct mutex		lock;
196	struct snd_sst_alloc_mrfld alloc_param;
197
198	void			*pcm_substream;
199	void (*period_elapsed)(void *pcm_substream);
200
201	unsigned int		sfreq;
202	u32			cumm_bytes;
203
204	void			*compr_cb_param;
205	void (*compr_cb)(void *compr_cb_param);
206
207	void			*drain_cb_param;
208	void (*drain_notify)(void *drain_cb_param);
209
210	unsigned int		num_ch;
211	unsigned int		pipe_id;
212	unsigned int		task_id;
213};
214
215#define SST_FW_SIGN "$SST"
216#define SST_FW_LIB_SIGN "$LIB"
217
218/**
219 * struct sst_fw_header - FW file headers
220 *
221 * @signature : FW signature
222 * @file_size: size of fw image
223 * @modules : # of modules
224 * @file_format : version of header format
225 * @reserved : reserved fields
226 */
227struct sst_fw_header {
228	unsigned char signature[FW_SIGNATURE_SIZE];
229	u32 file_size;
230	u32 modules;
231	u32 file_format;
232	u32 reserved[4];
233};
234
235/**
236 * struct fw_module_header - module header in FW
237 *
238 * @signature: module signature
239 * @mod_size: size of module
240 * @blocks: block count
241 * @type: block type
242 * @entry_point: module netry point
243 */
244struct fw_module_header {
245	unsigned char signature[FW_SIGNATURE_SIZE];
246	u32 mod_size;
247	u32 blocks;
248	u32 type;
249	u32 entry_point;
250};
251
252/**
253 * struct fw_block_info - block header for FW
254 *
255 * @type: block ram type I/D
256 * @size: size of block
257 * @ram_offset: offset in ram
258 */
259struct fw_block_info {
260	enum sst_ram_type	type;
261	u32			size;
262	u32			ram_offset;
263	u32			rsvd;
264};
265
266struct sst_runtime_param {
267	struct snd_sst_runtime_params param;
268};
269
270struct sst_sg_list {
271	struct scatterlist *src;
272	struct scatterlist *dst;
273	int list_len;
274	unsigned int sg_idx;
275};
276
277struct sst_memcpy_list {
278	struct list_head memcpylist;
279	void *dstn;
280	const void *src;
281	u32 size;
282	bool is_io;
283};
284
285/*Firmware Module Information*/
286enum sst_lib_dwnld_status {
287	SST_LIB_NOT_FOUND = 0,
288	SST_LIB_FOUND,
289	SST_LIB_DOWNLOADED,
290};
291
292struct sst_module_info {
293	const char *name; /*Library name*/
294	u32	id; /*Module ID*/
295	u32	entry_pt; /*Module entry point*/
296	u8	status; /*module status*/
297	u8	rsvd1;
298	u16	rsvd2;
299};
300
301/*
302 * Structure for managing the Library Region(1.5MB)
303 * in DDR in Merrifield
304 */
305struct sst_mem_mgr {
306	phys_addr_t current_base;
307	int avail;
308	unsigned int count;
309};
310
311struct sst_ipc_reg {
312	int ipcx;
313	int ipcd;
314};
315
316struct sst_fw_save {
317	void *iram;	/* allocated via kvmalloc() */
318	void *dram;	/* allocated via kvmalloc() */
319	void *sram;	/* allocated via kvmalloc() */
320	void *ddr;	/* allocated via kvmalloc() */
321};
322
323/**
324 * struct intel_sst_drv - driver ops
325 *
326 * @sst_state : current sst device state
327 * @dev_id : device identifier, pci_id for pci devices and acpi_id for acpi
328 * 	     devices
329 * @shim : SST shim pointer
330 * @mailbox : SST mailbox pointer
331 * @iram : SST IRAM pointer
332 * @dram : SST DRAM pointer
333 * @pdata : SST info passed as a part of pci platform data
334 * @shim_phy_add : SST shim phy addr
335 * @ipc_dispatch_list : ipc messages dispatched
336 * @rx_list : to copy the process_reply/process_msg from DSP
337 * @ipc_post_msg_wq : wq to post IPC messages context
338 * @mad_ops : MAD driver operations registered
339 * @mad_wq : MAD driver wq
340 * @post_msg_wq : wq to post IPC messages
341 * @streams : sst stream contexts
342 * @list_lock : sst driver list lock (deprecated)
343 * @ipc_spin_lock : spin lock to handle audio shim access and ipc queue
344 * @block_lock : spin lock to add block to block_list and assign pvt_id
345 * @rx_msg_lock : spin lock to handle the rx messages from the DSP
346 * @scard_ops : sst card ops
347 * @pci : sst pci device struture
348 * @dev : pointer to current device struct
349 * @sst_lock : sst device lock
350 * @pvt_id : sst private id
351 * @stream_cnt : total sst active stream count
352 * @pb_streams : total active pb streams
353 * @cp_streams : total active cp streams
354 * @audio_start : audio status
355 * @qos		: PM Qos struct
356 * firmware_name : Firmware / Library name
357 */
358struct intel_sst_drv {
359	int			sst_state;
360	int			irq_num;
361	unsigned int		dev_id;
362	void __iomem		*ddr;
363	void __iomem		*shim;
364	void __iomem		*mailbox;
365	void __iomem		*iram;
366	void __iomem		*dram;
367	unsigned int		mailbox_add;
368	unsigned int		iram_base;
369	unsigned int		dram_base;
370	unsigned int		shim_phy_add;
371	unsigned int		iram_end;
372	unsigned int		dram_end;
373	unsigned int		ddr_end;
374	unsigned int		ddr_base;
375	unsigned int		mailbox_recv_offset;
376	struct list_head        block_list;
377	struct list_head	ipc_dispatch_list;
378	struct sst_platform_info *pdata;
379	struct list_head	rx_list;
380	struct work_struct      ipc_post_msg_wq;
381	wait_queue_head_t	wait_queue;
382	struct workqueue_struct *post_msg_wq;
383	unsigned int		tstamp;
384	/* str_id 0 is not used */
385	struct stream_info	streams[MAX_NUM_STREAMS+1];
386	spinlock_t		ipc_spin_lock;
387	spinlock_t              block_lock;
388	spinlock_t		rx_msg_lock;
389	struct pci_dev		*pci;
390	struct device		*dev;
391	volatile long unsigned 		pvt_id;
392	struct mutex            sst_lock;
393	unsigned int		stream_cnt;
394	unsigned int		csr_value;
395	void			*fw_in_mem;
396	struct sst_sg_list	fw_sg_list, library_list;
397	struct intel_sst_ops	*ops;
398	struct sst_info		info;
399	struct pm_qos_request	*qos;
400	unsigned int		use_dma;
401	unsigned int		use_lli;
402	atomic_t		fw_clear_context;
403	bool			lib_dwnld_reqd;
404	struct list_head	memcpy_list;
405	struct sst_ipc_reg	ipc_reg;
406	struct sst_mem_mgr      lib_mem_mgr;
407	/*
408	 * Holder for firmware name. Due to async call it needs to be
409	 * persistent till worker thread gets called
410	 */
411	char firmware_name[FW_NAME_SIZE];
412
413	struct snd_sst_fw_version fw_version;
414	struct sst_fw_save	*fw_save;
415};
416
417/* misc definitions */
418#define FW_DWNL_ID 0x01
419
420struct intel_sst_ops {
421	irqreturn_t (*interrupt)(int, void *);
422	irqreturn_t (*irq_thread)(int, void *);
423	void (*clear_interrupt)(struct intel_sst_drv *ctx);
424	int (*start)(struct intel_sst_drv *ctx);
425	int (*reset)(struct intel_sst_drv *ctx);
426	void (*process_reply)(struct intel_sst_drv *ctx, struct ipc_post *msg);
427	int (*post_message)(struct intel_sst_drv *ctx,
428			struct ipc_post *msg, bool sync);
429	void (*process_message)(struct ipc_post *msg);
430	void (*set_bypass)(bool set);
431	int (*save_dsp_context)(struct intel_sst_drv *sst);
432	void (*restore_dsp_context)(void);
433	int (*alloc_stream)(struct intel_sst_drv *ctx, void *params);
434	void (*post_download)(struct intel_sst_drv *sst);
435};
436
437int sst_realloc_stream(struct intel_sst_drv *sst_drv_ctx, int str_id);
438int sst_pause_stream(struct intel_sst_drv *sst_drv_ctx, int str_id);
439int sst_resume_stream(struct intel_sst_drv *sst_drv_ctx, int str_id);
440int sst_drop_stream(struct intel_sst_drv *sst_drv_ctx, int str_id);
441int sst_free_stream(struct intel_sst_drv *sst_drv_ctx, int str_id);
442int sst_start_stream(struct intel_sst_drv *sst_drv_ctx, int str_id);
443int sst_send_byte_stream_mrfld(struct intel_sst_drv *sst_drv_ctx,
444			struct snd_sst_bytes_v2 *bytes);
445int sst_set_stream_param(int str_id, struct snd_sst_params *str_param);
446int sst_set_metadata(int str_id, char *params);
447int sst_get_stream(struct intel_sst_drv *ctx,
448		struct snd_sst_params *str_param);
449int sst_get_stream_allocated(struct intel_sst_drv *ctx,
450		struct snd_sst_params *str_param,
451		struct snd_sst_lib_download **lib_dnld);
452int sst_drain_stream(struct intel_sst_drv *sst_drv_ctx,
453		int str_id, bool partial_drain);
454int sst_post_message_mrfld(struct intel_sst_drv *sst_drv_ctx,
455		struct ipc_post *ipc_msg, bool sync);
456void sst_process_reply_mrfld(struct intel_sst_drv *sst_drv_ctx, struct ipc_post *msg);
457int sst_start_mrfld(struct intel_sst_drv *sst_drv_ctx);
458int intel_sst_reset_dsp_mrfld(struct intel_sst_drv *sst_drv_ctx);
459void intel_sst_clear_intr_mrfld(struct intel_sst_drv *sst_drv_ctx);
460
461int sst_load_fw(struct intel_sst_drv *sst_drv_ctx);
462int sst_load_library(struct snd_sst_lib_download *lib, u8 ops);
463void sst_post_download_mrfld(struct intel_sst_drv *ctx);
464int sst_get_block_stream(struct intel_sst_drv *sst_drv_ctx);
465void sst_memcpy_free_resources(struct intel_sst_drv *sst_drv_ctx);
466
467int sst_wait_interruptible(struct intel_sst_drv *sst_drv_ctx,
468				struct sst_block *block);
469int sst_wait_timeout(struct intel_sst_drv *sst_drv_ctx,
470			struct sst_block *block);
471int sst_create_ipc_msg(struct ipc_post **arg, bool large);
472int free_stream_context(struct intel_sst_drv *ctx, unsigned int str_id);
473void sst_clean_stream(struct stream_info *stream);
474int intel_sst_register_compress(struct intel_sst_drv *sst);
475int intel_sst_remove_compress(struct intel_sst_drv *sst);
476void sst_cdev_fragment_elapsed(struct intel_sst_drv *ctx, int str_id);
477int sst_send_sync_msg(int ipc, int str_id);
478int sst_get_num_channel(struct snd_sst_params *str_param);
479int sst_get_sfreq(struct snd_sst_params *str_param);
480int sst_alloc_stream_mrfld(struct intel_sst_drv *sst_drv_ctx, void *params);
481void sst_restore_fw_context(void);
482struct sst_block *sst_create_block(struct intel_sst_drv *ctx,
483				u32 msg_id, u32 drv_id);
484int sst_create_block_and_ipc_msg(struct ipc_post **arg, bool large,
485		struct intel_sst_drv *sst_drv_ctx, struct sst_block **block,
486		u32 msg_id, u32 drv_id);
487int sst_free_block(struct intel_sst_drv *ctx, struct sst_block *freed);
488int sst_wake_up_block(struct intel_sst_drv *ctx, int result,
489		u32 drv_id, u32 ipc, void *data, u32 size);
490int sst_request_firmware_async(struct intel_sst_drv *ctx);
491int sst_driver_ops(struct intel_sst_drv *sst);
492struct sst_platform_info *sst_get_acpi_driver_data(const char *hid);
493void sst_firmware_load_cb(const struct firmware *fw, void *context);
494int sst_prepare_and_post_msg(struct intel_sst_drv *sst,
495		int task_id, int ipc_msg, int cmd_id, int pipe_id,
496		size_t mbox_data_len, const void *mbox_data, void **data,
497		bool large, bool fill_dsp, bool sync, bool response);
498
499void sst_process_pending_msg(struct work_struct *work);
500int sst_assign_pvt_id(struct intel_sst_drv *drv);
501int sst_validate_strid(struct intel_sst_drv *sst_drv_ctx, int str_id);
502struct stream_info *get_stream_info(struct intel_sst_drv *sst_drv_ctx,
503		int str_id);
504int get_stream_id_mrfld(struct intel_sst_drv *sst_drv_ctx,
505		u32 pipe_id);
506u32 relocate_imr_addr_mrfld(u32 base_addr);
507void sst_add_to_dispatch_list_and_post(struct intel_sst_drv *sst,
508					struct ipc_post *msg);
509int sst_pm_runtime_put(struct intel_sst_drv *sst_drv);
510int sst_shim_write(void __iomem *addr, int offset, int value);
511u32 sst_shim_read(void __iomem *addr, int offset);
512u64 sst_reg_read64(void __iomem *addr, int offset);
513int sst_shim_write64(void __iomem *addr, int offset, u64 value);
514u64 sst_shim_read64(void __iomem *addr, int offset);
515void sst_set_fw_state_locked(
516		struct intel_sst_drv *sst_drv_ctx, int sst_state);
517void sst_fill_header_mrfld(union ipc_header_mrfld *header,
518				int msg, int task_id, int large, int drv_id);
519void sst_fill_header_dsp(struct ipc_dsp_hdr *dsp, int msg,
520					int pipe_id, int len);
521
522int sst_register(struct device *);
523int sst_unregister(struct device *);
524
525int sst_alloc_drv_context(struct intel_sst_drv **ctx,
526		struct device *dev, unsigned int dev_id);
527int sst_context_init(struct intel_sst_drv *ctx);
528void sst_context_cleanup(struct intel_sst_drv *ctx);
529void sst_configure_runtime_pm(struct intel_sst_drv *ctx);
530void memcpy32_toio(void __iomem *dst, const void *src, int count);
531void memcpy32_fromio(void *dst, const void __iomem *src, int count);
532
533#endif
534