ASoC: Intel: Make ADSP memory block allocation more generic
[firefly-linux-kernel-4.4.55.git] / sound / soc / intel / sst-dsp-priv.h
1 /*
2  * Intel Smart Sound Technology
3  *
4  * Copyright (C) 2013, Intel Corporation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version
8  * 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #ifndef __SOUND_SOC_SST_DSP_PRIV_H
18 #define __SOUND_SOC_SST_DSP_PRIV_H
19
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/interrupt.h>
23 #include <linux/firmware.h>
24
25 struct sst_mem_block;
26 struct sst_module;
27 struct sst_fw;
28
29 /* do we need to remove or keep */
30 #define DSP_DRAM_ADDR_OFFSET            0x400000
31
32 /*
33  * DSP Operations exported by platform Audio DSP driver.
34  */
35 struct sst_ops {
36         /* DSP core boot / reset */
37         void (*boot)(struct sst_dsp *);
38         void (*reset)(struct sst_dsp *);
39
40         /* Shim IO */
41         void (*write)(void __iomem *addr, u32 offset, u32 value);
42         u32 (*read)(void __iomem *addr, u32 offset);
43         void (*write64)(void __iomem *addr, u32 offset, u64 value);
44         u64 (*read64)(void __iomem *addr, u32 offset);
45
46         /* DSP I/DRAM IO */
47         void (*ram_read)(struct sst_dsp *sst, void  *dest, void __iomem *src,
48                 size_t bytes);
49         void (*ram_write)(struct sst_dsp *sst, void __iomem *dest, void *src,
50                 size_t bytes);
51
52         void (*dump)(struct sst_dsp *);
53
54         /* IRQ handlers */
55         irqreturn_t (*irq_handler)(int irq, void *context);
56
57         /* SST init and free */
58         int (*init)(struct sst_dsp *sst, struct sst_pdata *pdata);
59         void (*free)(struct sst_dsp *sst);
60
61         /* FW module parser/loader */
62         int (*parse_fw)(struct sst_fw *sst_fw);
63 };
64
65 /*
66  * Audio DSP memory offsets and addresses.
67  */
68 struct sst_addr {
69         u32 lpe_base;
70         u32 shim_offset;
71         u32 iram_offset;
72         u32 dram_offset;
73         u32 dsp_iram_offset;
74         u32 dsp_dram_offset;
75         void __iomem *lpe;
76         void __iomem *shim;
77         void __iomem *pci_cfg;
78         void __iomem *fw_ext;
79 };
80
81 /*
82  * Audio DSP Mailbox configuration.
83  */
84 struct sst_mailbox {
85         void __iomem *in_base;
86         void __iomem *out_base;
87         size_t in_size;
88         size_t out_size;
89 };
90
91 /*
92  * Audio DSP memory block types.
93  */
94 enum sst_mem_type {
95         SST_MEM_IRAM = 0,
96         SST_MEM_DRAM = 1,
97         SST_MEM_ANY  = 2,
98         SST_MEM_CACHE= 3,
99 };
100
101 /*
102  * Audio DSP Generic Firmware File.
103  *
104  * SST Firmware files can consist of 1..N modules. This generic structure is
105  * used to manage each firmware file and it's modules regardless of SST firmware
106  * type. A SST driver may load multiple FW files.
107  */
108 struct sst_fw {
109         struct sst_dsp *dsp;
110
111         /* base addresses of FW file data */
112         dma_addr_t dmable_fw_paddr;     /* physical address of fw data */
113         void *dma_buf;                  /* virtual address of fw data */
114         u32 size;                       /* size of fw data */
115
116         /* lists */
117         struct list_head list;          /* DSP list of FW */
118         struct list_head module_list;   /* FW list of modules */
119
120         void *private;                  /* core doesn't touch this */
121 };
122
123 /*
124  * Audio DSP Generic Module Template.
125  *
126  * Used to define and register a new FW module. This data is extracted from
127  * FW module header information.
128  */
129 struct sst_module_template {
130         u32 id;
131         u32 entry;                      /* entry point */
132         u32 scratch_size;
133         u32 persistent_size;
134 };
135
136 /*
137  * Block Allocator - Used to allocate blocks of DSP memory.
138  */
139 struct sst_block_allocator {
140         u32 id;
141         u32 offset;
142         int size;
143         enum sst_mem_type type;
144 };
145
146 /*
147  * Runtime Module Instance - A module object can be instanciated multiple
148  * times within the DSP FW.
149  */
150 struct sst_module_runtime {
151         struct sst_dsp *dsp;
152         int id;
153         struct sst_module *module;      /* parent module we belong too */
154
155         u32 persistent_offset;          /* private memory offset */
156         void *private;
157
158         struct list_head list;
159         struct list_head block_list;    /* list of blocks used */
160 };
161
162 /*
163  * Runtime Module Context - The runtime context must be manually stored by the
164  * driver prior to enter S3 and restored after leaving S3. This should really be
165  * part of the memory context saved by the enter D3 message IPC ???
166  */
167 struct sst_module_runtime_context {
168         dma_addr_t dma_buffer;
169         u32 *buffer;
170 };
171
172 /*
173  * Audio DSP Generic Module.
174  *
175  * Each Firmware file can consist of 1..N modules. A module can span multiple
176  * ADSP memory blocks. The simplest FW will be a file with 1 module. A module
177  * can be instanciated multiple times in the DSP.
178  */
179 struct sst_module {
180         struct sst_dsp *dsp;
181         struct sst_fw *sst_fw;          /* parent FW we belong too */
182
183         /* module configuration */
184         u32 id;
185         u32 entry;                      /* module entry point */
186         s32 offset;                     /* module offset in firmware file */
187         u32 size;                       /* module size */
188         u32 scratch_size;               /* global scratch memory required */
189         u32 persistent_size;            /* private memory required */
190         enum sst_mem_type type;         /* destination memory type */
191         u32 data_offset;                /* offset in ADSP memory space */
192         void *data;                     /* module data */
193
194         /* runtime */
195         u32 usage_count;                /* can be unloaded if count == 0 */
196         void *private;                  /* core doesn't touch this */
197
198         /* lists */
199         struct list_head block_list;    /* Module list of blocks in use */
200         struct list_head list;          /* DSP list of modules */
201         struct list_head list_fw;       /* FW list of modules */
202         struct list_head runtime_list;  /* list of runtime module objects*/
203 };
204
205 /*
206  * SST Memory Block operations.
207  */
208 struct sst_block_ops {
209         int (*enable)(struct sst_mem_block *block);
210         int (*disable)(struct sst_mem_block *block);
211 };
212
213 /*
214  * SST Generic Memory Block.
215  *
216  * SST ADP  memory has multiple IRAM and DRAM blocks. Some ADSP blocks can be
217  * power gated.
218  */
219 struct sst_mem_block {
220         struct sst_dsp *dsp;
221         struct sst_module *module;      /* module that uses this block */
222
223         /* block config */
224         u32 offset;                     /* offset from base */
225         u32 size;                       /* block size */
226         u32 index;                      /* block index 0..N */
227         enum sst_mem_type type;         /* block memory type IRAM/DRAM */
228         struct sst_block_ops *ops;      /* block operations, if any */
229
230         /* block status */
231         u32 bytes_used;                 /* bytes in use by modules */
232         void *private;                  /* generic core does not touch this */
233         int users;                      /* number of modules using this block */
234
235         /* block lists */
236         struct list_head module_list;   /* Module list of blocks */
237         struct list_head list;          /* Map list of free/used blocks */
238 };
239
240 /*
241  * Generic SST Shim Interface.
242  */
243 struct sst_dsp {
244
245         /* runtime */
246         struct sst_dsp_device *sst_dev;
247         spinlock_t spinlock;    /* IPC locking */
248         struct mutex mutex;     /* DSP FW lock */
249         struct device *dev;
250         struct device *dma_dev;
251         void *thread_context;
252         int irq;
253         u32 id;
254
255         /* list of free and used ADSP memory blocks */
256         struct list_head used_block_list;
257         struct list_head free_block_list;
258
259         /* operations */
260         struct sst_ops *ops;
261
262         /* debug FS */
263         struct dentry *debugfs_root;
264
265         /* base addresses */
266         struct sst_addr addr;
267
268         /* mailbox */
269         struct sst_mailbox mailbox;
270
271         /* SST FW files loaded and their modules */
272         struct list_head module_list;
273         struct list_head fw_list;
274
275         /* scratch buffer */
276         struct list_head scratch_block_list;
277         u32 scratch_offset;
278         u32 scratch_size;
279
280         /* platform data */
281         struct sst_pdata *pdata;
282
283         /* DMA FW loading */
284         struct sst_dma *dma;
285         bool fw_use_dma;
286 };
287
288 /* Size optimised DRAM/IRAM memcpy */
289 static inline void sst_dsp_write(struct sst_dsp *sst, void *src,
290         u32 dest_offset, size_t bytes)
291 {
292         sst->ops->ram_write(sst, sst->addr.lpe + dest_offset, src, bytes);
293 }
294
295 static inline void sst_dsp_read(struct sst_dsp *sst, void *dest,
296         u32 src_offset, size_t bytes)
297 {
298         sst->ops->ram_read(sst, dest, sst->addr.lpe + src_offset, bytes);
299 }
300
301 static inline void *sst_dsp_get_thread_context(struct sst_dsp *sst)
302 {
303         return sst->thread_context;
304 }
305
306 /* Create/Free FW files - can contain multiple modules */
307 struct sst_fw *sst_fw_new(struct sst_dsp *dsp,
308         const struct firmware *fw, void *private);
309 void sst_fw_free(struct sst_fw *sst_fw);
310 void sst_fw_free_all(struct sst_dsp *dsp);
311 int sst_fw_reload(struct sst_fw *sst_fw);
312 void sst_fw_unload(struct sst_fw *sst_fw);
313
314 /* Create/Free firmware modules */
315 struct sst_module *sst_module_new(struct sst_fw *sst_fw,
316         struct sst_module_template *template, void *private);
317 void sst_module_free(struct sst_module *module);
318 struct sst_module *sst_module_get_from_id(struct sst_dsp *dsp, u32 id);
319 int sst_module_alloc_blocks(struct sst_module *module);
320 int sst_module_free_blocks(struct sst_module *module);
321
322 /* Create/Free firmware module runtime instances */
323 struct sst_module_runtime *sst_module_runtime_new(struct sst_module *module,
324         int id, void *private);
325 void sst_module_runtime_free(struct sst_module_runtime *runtime);
326 struct sst_module_runtime *sst_module_runtime_get_from_id(
327         struct sst_module *module, u32 id);
328 int sst_module_runtime_alloc_blocks(struct sst_module_runtime *runtime,
329         int offset);
330 int sst_module_runtime_free_blocks(struct sst_module_runtime *runtime);
331 int sst_module_runtime_save(struct sst_module_runtime *runtime,
332         struct sst_module_runtime_context *context);
333 int sst_module_runtime_restore(struct sst_module_runtime *runtime,
334         struct sst_module_runtime_context *context);
335
336 /* generic block allocation */
337 int sst_alloc_blocks(struct sst_dsp *dsp, struct sst_block_allocator *ba,
338         struct list_head *block_list);
339 int sst_free_blocks(struct sst_dsp *dsp, struct list_head *block_list);
340
341 /* scratch allocation */
342 int sst_block_alloc_scratch(struct sst_dsp *dsp);
343 void sst_block_free_scratch(struct sst_dsp *dsp);
344
345 /* Register the DSPs memory blocks - would be nice to read from ACPI */
346 struct sst_mem_block *sst_mem_block_register(struct sst_dsp *dsp, u32 offset,
347         u32 size, enum sst_mem_type type, struct sst_block_ops *ops, u32 index,
348         void *private);
349 void sst_mem_block_unregister_all(struct sst_dsp *dsp);
350
351 /* Create/Free DMA resources */
352 int sst_dma_new(struct sst_dsp *sst);
353 void sst_dma_free(struct sst_dma *dma);
354
355 u32 sst_dsp_get_offset(struct sst_dsp *dsp, u32 offset,
356         enum sst_mem_type type);
357 #endif