]>
Commit | Line | Data |
---|---|---|
6eee8726 RB |
1 | /* |
2 | * skl-sst-utils.c - SKL sst utils functions | |
3 | * | |
4 | * Copyright (C) 2016 Intel Corp | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as version 2, as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | */ | |
15 | ||
ea6b3e94 SN |
16 | #include <linux/device.h> |
17 | #include <linux/slab.h> | |
18 | #include <linux/uuid.h> | |
6eee8726 | 19 | #include "skl-sst-dsp.h" |
ea6b3e94 SN |
20 | #include "../common/sst-dsp.h" |
21 | #include "../common/sst-dsp-priv.h" | |
22 | #include "skl-sst-ipc.h" | |
23 | ||
24 | ||
25 | #define UUID_STR_SIZE 37 | |
26 | #define DEFAULT_HASH_SHA256_LEN 32 | |
6eee8726 RB |
27 | |
28 | /* FW Extended Manifest Header id = $AE1 */ | |
29 | #define SKL_EXT_MANIFEST_HEADER_MAGIC 0x31454124 | |
30 | ||
ea6b3e94 SN |
31 | struct UUID { |
32 | u8 id[16]; | |
33 | }; | |
34 | ||
35 | union seg_flags { | |
36 | u32 ul; | |
37 | struct { | |
38 | u32 contents : 1; | |
39 | u32 alloc : 1; | |
40 | u32 load : 1; | |
41 | u32 read_only : 1; | |
42 | u32 code : 1; | |
43 | u32 data : 1; | |
44 | u32 _rsvd0 : 2; | |
45 | u32 type : 4; | |
46 | u32 _rsvd1 : 4; | |
47 | u32 length : 16; | |
48 | } r; | |
49 | } __packed; | |
50 | ||
51 | struct segment_desc { | |
52 | union seg_flags flags; | |
53 | u32 v_base_addr; | |
54 | u32 file_offset; | |
55 | }; | |
56 | ||
57 | struct module_type { | |
58 | u32 load_type : 4; | |
59 | u32 auto_start : 1; | |
60 | u32 domain_ll : 1; | |
61 | u32 domain_dp : 1; | |
62 | u32 rsvd : 25; | |
63 | } __packed; | |
64 | ||
65 | struct adsp_module_entry { | |
66 | u32 struct_id; | |
67 | u8 name[8]; | |
68 | struct UUID uuid; | |
69 | struct module_type type; | |
70 | u8 hash1[DEFAULT_HASH_SHA256_LEN]; | |
71 | u32 entry_point; | |
72 | u16 cfg_offset; | |
73 | u16 cfg_count; | |
74 | u32 affinity_mask; | |
75 | u16 instance_max_count; | |
76 | u16 instance_bss_size; | |
77 | struct segment_desc segments[3]; | |
78 | } __packed; | |
79 | ||
80 | struct adsp_fw_hdr { | |
81 | u32 id; | |
82 | u32 len; | |
83 | u8 name[8]; | |
84 | u32 preload_page_count; | |
85 | u32 fw_image_flags; | |
86 | u32 feature_mask; | |
87 | u16 major; | |
88 | u16 minor; | |
89 | u16 hotfix; | |
90 | u16 build; | |
91 | u32 num_modules; | |
92 | u32 hw_buf_base; | |
93 | u32 hw_buf_length; | |
94 | u32 load_offset; | |
95 | } __packed; | |
96 | ||
700a9a63 D |
97 | #define MAX_INSTANCE_BUFF 2 |
98 | ||
ea6b3e94 SN |
99 | struct uuid_module { |
100 | uuid_le uuid; | |
101 | int id; | |
102 | int is_loadable; | |
700a9a63 D |
103 | int max_instance; |
104 | u64 pvt_id[MAX_INSTANCE_BUFF]; | |
ea6b3e94 SN |
105 | |
106 | struct list_head list; | |
107 | }; | |
108 | ||
6eee8726 RB |
109 | struct skl_ext_manifest_hdr { |
110 | u32 id; | |
111 | u32 len; | |
112 | u16 version_major; | |
113 | u16 version_minor; | |
114 | u32 entries; | |
115 | }; | |
116 | ||
0556ba46 D |
117 | int snd_skl_get_module_info(struct skl_sst *ctx, |
118 | struct skl_module_cfg *mconfig) | |
ea6b3e94 SN |
119 | { |
120 | struct uuid_module *module; | |
121 | uuid_le *uuid_mod; | |
122 | ||
0556ba46 | 123 | uuid_mod = (uuid_le *)mconfig->guid; |
ea6b3e94 SN |
124 | |
125 | list_for_each_entry(module, &ctx->uuid_list, list) { | |
126 | if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) { | |
0556ba46 D |
127 | mconfig->id.module_id = module->id; |
128 | mconfig->is_loadable = module->is_loadable; | |
ea6b3e94 SN |
129 | |
130 | return 0; | |
131 | } | |
132 | } | |
133 | ||
134 | return -EINVAL; | |
135 | } | |
136 | EXPORT_SYMBOL_GPL(snd_skl_get_module_info); | |
137 | ||
700a9a63 D |
138 | static inline int skl_getid_32(struct uuid_module *module, u64 *val, |
139 | int word1_mask, int word2_mask) | |
140 | { | |
141 | int index, max_inst, pvt_id; | |
142 | u32 mask_val; | |
143 | ||
144 | max_inst = module->max_instance; | |
145 | mask_val = (u32)(*val >> word1_mask); | |
146 | ||
147 | if (mask_val != 0xffffffff) { | |
148 | index = ffz(mask_val); | |
149 | pvt_id = index + word1_mask + word2_mask; | |
150 | if (pvt_id <= (max_inst - 1)) { | |
151 | *val |= 1 << (index + word1_mask); | |
152 | return pvt_id; | |
153 | } | |
154 | } | |
155 | ||
156 | return -EINVAL; | |
157 | } | |
158 | ||
159 | static inline int skl_pvtid_128(struct uuid_module *module) | |
160 | { | |
161 | int j, i, word1_mask, word2_mask = 0, pvt_id; | |
162 | ||
163 | for (j = 0; j < MAX_INSTANCE_BUFF; j++) { | |
164 | word1_mask = 0; | |
165 | ||
166 | for (i = 0; i < 2; i++) { | |
167 | pvt_id = skl_getid_32(module, &module->pvt_id[j], | |
168 | word1_mask, word2_mask); | |
169 | if (pvt_id >= 0) | |
170 | return pvt_id; | |
171 | ||
172 | word1_mask += 32; | |
173 | if ((word1_mask + word2_mask) >= module->max_instance) | |
174 | return -EINVAL; | |
175 | } | |
176 | ||
177 | word2_mask += 64; | |
178 | if (word2_mask >= module->max_instance) | |
179 | return -EINVAL; | |
180 | } | |
181 | ||
182 | return -EINVAL; | |
183 | } | |
184 | ||
185 | /** | |
186 | * skl_get_pvt_id: generate a private id for use as module id | |
187 | * | |
188 | * @ctx: driver context | |
189 | * @mconfig: module configuration data | |
190 | * | |
191 | * This generates a 128 bit private unique id for a module TYPE so that | |
192 | * module instance is unique | |
193 | */ | |
194 | int skl_get_pvt_id(struct skl_sst *ctx, struct skl_module_cfg *mconfig) | |
195 | { | |
196 | struct uuid_module *module; | |
197 | uuid_le *uuid_mod; | |
198 | int pvt_id; | |
199 | ||
200 | uuid_mod = (uuid_le *)mconfig->guid; | |
201 | ||
202 | list_for_each_entry(module, &ctx->uuid_list, list) { | |
203 | if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) { | |
204 | ||
205 | pvt_id = skl_pvtid_128(module); | |
206 | if (pvt_id >= 0) | |
207 | return pvt_id; | |
208 | } | |
209 | } | |
210 | ||
211 | return -EINVAL; | |
212 | } | |
213 | EXPORT_SYMBOL_GPL(skl_get_pvt_id); | |
214 | ||
215 | /** | |
216 | * skl_put_pvt_id: free up the private id allocated | |
217 | * | |
218 | * @ctx: driver context | |
219 | * @mconfig: module configuration data | |
220 | * | |
221 | * This frees a 128 bit private unique id previously generated | |
222 | */ | |
223 | int skl_put_pvt_id(struct skl_sst *ctx, struct skl_module_cfg *mconfig) | |
224 | { | |
225 | int i; | |
226 | uuid_le *uuid_mod; | |
227 | struct uuid_module *module; | |
228 | ||
229 | uuid_mod = (uuid_le *)mconfig->guid; | |
230 | list_for_each_entry(module, &ctx->uuid_list, list) { | |
231 | if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) { | |
232 | ||
233 | if (mconfig->id.pvt_id != 0) | |
234 | i = (mconfig->id.pvt_id) / 64; | |
235 | else | |
236 | i = 0; | |
237 | ||
238 | module->pvt_id[i] &= ~(1 << (mconfig->id.pvt_id)); | |
239 | mconfig->id.pvt_id = -1; | |
240 | return 0; | |
241 | } | |
242 | } | |
243 | ||
244 | return -EINVAL; | |
245 | } | |
246 | EXPORT_SYMBOL_GPL(skl_put_pvt_id); | |
247 | ||
ea6b3e94 SN |
248 | /* |
249 | * Parse the firmware binary to get the UUID, module id | |
250 | * and loadable flags | |
251 | */ | |
a8e2c19e SV |
252 | int snd_skl_parse_uuids(struct sst_dsp *ctx, const struct firmware *fw, |
253 | unsigned int offset, int index) | |
ea6b3e94 SN |
254 | { |
255 | struct adsp_fw_hdr *adsp_hdr; | |
256 | struct adsp_module_entry *mod_entry; | |
257 | int i, num_entry; | |
258 | uuid_le *uuid_bin; | |
259 | const char *buf; | |
260 | struct skl_sst *skl = ctx->thread_context; | |
261 | struct uuid_module *module; | |
262 | struct firmware stripped_fw; | |
263 | unsigned int safe_file; | |
264 | ||
265 | /* Get the FW pointer to derive ADSP header */ | |
a8e2c19e SV |
266 | stripped_fw.data = fw->data; |
267 | stripped_fw.size = fw->size; | |
ea6b3e94 SN |
268 | |
269 | skl_dsp_strip_extended_manifest(&stripped_fw); | |
270 | ||
271 | buf = stripped_fw.data; | |
272 | ||
273 | /* check if we have enough space in file to move to header */ | |
274 | safe_file = sizeof(*adsp_hdr) + offset; | |
275 | if (stripped_fw.size <= safe_file) { | |
276 | dev_err(ctx->dev, "Small fw file size, No space for hdr\n"); | |
277 | return -EINVAL; | |
278 | } | |
279 | ||
280 | adsp_hdr = (struct adsp_fw_hdr *)(buf + offset); | |
281 | ||
282 | /* check 1st module entry is in file */ | |
283 | safe_file += adsp_hdr->len + sizeof(*mod_entry); | |
284 | if (stripped_fw.size <= safe_file) { | |
285 | dev_err(ctx->dev, "Small fw file size, No module entry\n"); | |
286 | return -EINVAL; | |
287 | } | |
288 | ||
289 | mod_entry = (struct adsp_module_entry *) | |
290 | (buf + offset + adsp_hdr->len); | |
291 | ||
292 | num_entry = adsp_hdr->num_modules; | |
293 | ||
294 | /* check all entries are in file */ | |
295 | safe_file += num_entry * sizeof(*mod_entry); | |
296 | if (stripped_fw.size <= safe_file) { | |
297 | dev_err(ctx->dev, "Small fw file size, No modules\n"); | |
298 | return -EINVAL; | |
299 | } | |
300 | ||
301 | ||
302 | /* | |
303 | * Read the UUID(GUID) from FW Manifest. | |
304 | * | |
305 | * The 16 byte UUID format is: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX | |
306 | * Populate the UUID table to store module_id and loadable flags | |
307 | * for the module. | |
308 | */ | |
309 | ||
310 | for (i = 0; i < num_entry; i++, mod_entry++) { | |
311 | module = kzalloc(sizeof(*module), GFP_KERNEL); | |
312 | if (!module) | |
313 | return -ENOMEM; | |
314 | ||
315 | uuid_bin = (uuid_le *)mod_entry->uuid.id; | |
316 | memcpy(&module->uuid, uuid_bin, sizeof(module->uuid)); | |
317 | ||
a8e2c19e | 318 | module->id = (i | (index << 12)); |
ea6b3e94 | 319 | module->is_loadable = mod_entry->type.load_type; |
700a9a63 | 320 | module->max_instance = mod_entry->instance_max_count; |
ea6b3e94 SN |
321 | |
322 | list_add_tail(&module->list, &skl->uuid_list); | |
323 | ||
324 | dev_dbg(ctx->dev, | |
325 | "Adding uuid :%pUL mod id: %d Loadable: %d\n", | |
326 | &module->uuid, module->id, module->is_loadable); | |
327 | } | |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
332 | void skl_freeup_uuid_list(struct skl_sst *ctx) | |
333 | { | |
334 | struct uuid_module *uuid, *_uuid; | |
335 | ||
336 | list_for_each_entry_safe(uuid, _uuid, &ctx->uuid_list, list) { | |
337 | list_del(&uuid->list); | |
338 | kfree(uuid); | |
339 | } | |
340 | } | |
341 | ||
6eee8726 RB |
342 | /* |
343 | * some firmware binary contains some extended manifest. This needs | |
344 | * to be stripped in that case before we load and use that image. | |
345 | * | |
ea6b3e94 SN |
346 | * Get the module id for the module by checking |
347 | * the table for the UUID for the module | |
6eee8726 RB |
348 | */ |
349 | int skl_dsp_strip_extended_manifest(struct firmware *fw) | |
350 | { | |
351 | struct skl_ext_manifest_hdr *hdr; | |
352 | ||
353 | /* check if fw file is greater than header we are looking */ | |
354 | if (fw->size < sizeof(hdr)) { | |
355 | pr_err("%s: Firmware file small, no hdr\n", __func__); | |
356 | return -EINVAL; | |
357 | } | |
358 | ||
359 | hdr = (struct skl_ext_manifest_hdr *)fw->data; | |
360 | ||
361 | if (hdr->id == SKL_EXT_MANIFEST_HEADER_MAGIC) { | |
362 | fw->size -= hdr->len; | |
363 | fw->data += hdr->len; | |
364 | } | |
365 | ||
366 | return 0; | |
367 | } |