blob: 1aa0f371b381366e82befc55273f594574d0df54 [file] [log] [blame]
Ramesh Babu6eee8722016-05-30 17:42:55 +05301/*
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
Shreyas NCea6b3e92016-05-30 17:42:59 +053016#include <linux/device.h>
17#include <linux/slab.h>
18#include <linux/uuid.h>
Ramesh Babu6eee8722016-05-30 17:42:55 +053019#include "skl-sst-dsp.h"
Shreyas NCea6b3e92016-05-30 17:42:59 +053020#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
Ramesh Babu6eee8722016-05-30 17:42:55 +053027
28/* FW Extended Manifest Header id = $AE1 */
29#define SKL_EXT_MANIFEST_HEADER_MAGIC 0x31454124
30
Shreyas NCea6b3e92016-05-30 17:42:59 +053031struct UUID {
32 u8 id[16];
33};
34
35union 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
51struct segment_desc {
52 union seg_flags flags;
53 u32 v_base_addr;
54 u32 file_offset;
55};
56
57struct 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
65struct 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
80struct 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
Dharageswari R700a9a62016-09-22 14:00:37 +053097#define MAX_INSTANCE_BUFF 2
98
Shreyas NCea6b3e92016-05-30 17:42:59 +053099struct uuid_module {
100 uuid_le uuid;
101 int id;
102 int is_loadable;
Dharageswari R700a9a62016-09-22 14:00:37 +0530103 int max_instance;
104 u64 pvt_id[MAX_INSTANCE_BUFF];
Dharageswari R55a92ea2016-09-22 14:00:39 +0530105 int *instance_id;
Shreyas NCea6b3e92016-05-30 17:42:59 +0530106
107 struct list_head list;
108};
109
Ramesh Babu6eee8722016-05-30 17:42:55 +0530110struct skl_ext_manifest_hdr {
111 u32 id;
112 u32 len;
113 u16 version_major;
114 u16 version_minor;
115 u32 entries;
116};
117
Dharageswari R0556ba42016-08-10 09:40:48 +0530118int snd_skl_get_module_info(struct skl_sst *ctx,
119 struct skl_module_cfg *mconfig)
Shreyas NCea6b3e92016-05-30 17:42:59 +0530120{
121 struct uuid_module *module;
122 uuid_le *uuid_mod;
123
Dharageswari R0556ba42016-08-10 09:40:48 +0530124 uuid_mod = (uuid_le *)mconfig->guid;
Shreyas NCea6b3e92016-05-30 17:42:59 +0530125
126 list_for_each_entry(module, &ctx->uuid_list, list) {
127 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
Dharageswari R0556ba42016-08-10 09:40:48 +0530128 mconfig->id.module_id = module->id;
129 mconfig->is_loadable = module->is_loadable;
Shreyas NCea6b3e92016-05-30 17:42:59 +0530130
131 return 0;
132 }
133 }
134
135 return -EINVAL;
136}
137EXPORT_SYMBOL_GPL(snd_skl_get_module_info);
138
Dharageswari R55a92ea2016-09-22 14:00:39 +0530139static int skl_get_pvtid_map(struct uuid_module *module, int instance_id)
140{
141 int pvt_id;
142
143 for (pvt_id = 0; pvt_id < module->max_instance; pvt_id++) {
144 if (module->instance_id[pvt_id] == instance_id)
145 return pvt_id;
146 }
147 return -EINVAL;
148}
149
150int skl_get_pvt_instance_id_map(struct skl_sst *ctx,
151 int module_id, int instance_id)
152{
153 struct uuid_module *module;
154
155 list_for_each_entry(module, &ctx->uuid_list, list) {
156 if (module->id == module_id)
157 return skl_get_pvtid_map(module, instance_id);
158 }
159
160 return -EINVAL;
161}
162EXPORT_SYMBOL_GPL(skl_get_pvt_instance_id_map);
163
Dharageswari R700a9a62016-09-22 14:00:37 +0530164static inline int skl_getid_32(struct uuid_module *module, u64 *val,
Dharageswari Ref2a3522016-09-22 14:00:38 +0530165 int word1_mask, int word2_mask)
Dharageswari R700a9a62016-09-22 14:00:37 +0530166{
167 int index, max_inst, pvt_id;
168 u32 mask_val;
169
170 max_inst = module->max_instance;
171 mask_val = (u32)(*val >> word1_mask);
172
173 if (mask_val != 0xffffffff) {
174 index = ffz(mask_val);
175 pvt_id = index + word1_mask + word2_mask;
176 if (pvt_id <= (max_inst - 1)) {
177 *val |= 1 << (index + word1_mask);
178 return pvt_id;
179 }
180 }
181
182 return -EINVAL;
183}
184
185static inline int skl_pvtid_128(struct uuid_module *module)
186{
187 int j, i, word1_mask, word2_mask = 0, pvt_id;
188
189 for (j = 0; j < MAX_INSTANCE_BUFF; j++) {
190 word1_mask = 0;
191
192 for (i = 0; i < 2; i++) {
193 pvt_id = skl_getid_32(module, &module->pvt_id[j],
194 word1_mask, word2_mask);
195 if (pvt_id >= 0)
196 return pvt_id;
197
198 word1_mask += 32;
199 if ((word1_mask + word2_mask) >= module->max_instance)
200 return -EINVAL;
201 }
202
203 word2_mask += 64;
204 if (word2_mask >= module->max_instance)
205 return -EINVAL;
206 }
207
208 return -EINVAL;
209}
210
211/**
212 * skl_get_pvt_id: generate a private id for use as module id
213 *
214 * @ctx: driver context
215 * @mconfig: module configuration data
216 *
217 * This generates a 128 bit private unique id for a module TYPE so that
218 * module instance is unique
219 */
220int skl_get_pvt_id(struct skl_sst *ctx, struct skl_module_cfg *mconfig)
221{
222 struct uuid_module *module;
223 uuid_le *uuid_mod;
224 int pvt_id;
225
226 uuid_mod = (uuid_le *)mconfig->guid;
227
228 list_for_each_entry(module, &ctx->uuid_list, list) {
229 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
230
231 pvt_id = skl_pvtid_128(module);
Dharageswari R55a92ea2016-09-22 14:00:39 +0530232 if (pvt_id >= 0) {
233 module->instance_id[pvt_id] =
234 mconfig->id.instance_id;
Dharageswari R700a9a62016-09-22 14:00:37 +0530235 return pvt_id;
Dharageswari R55a92ea2016-09-22 14:00:39 +0530236 }
Dharageswari R700a9a62016-09-22 14:00:37 +0530237 }
238 }
239
240 return -EINVAL;
241}
242EXPORT_SYMBOL_GPL(skl_get_pvt_id);
243
244/**
245 * skl_put_pvt_id: free up the private id allocated
246 *
247 * @ctx: driver context
248 * @mconfig: module configuration data
249 *
250 * This frees a 128 bit private unique id previously generated
251 */
252int skl_put_pvt_id(struct skl_sst *ctx, struct skl_module_cfg *mconfig)
253{
254 int i;
255 uuid_le *uuid_mod;
256 struct uuid_module *module;
257
258 uuid_mod = (uuid_le *)mconfig->guid;
259 list_for_each_entry(module, &ctx->uuid_list, list) {
260 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
261
262 if (mconfig->id.pvt_id != 0)
263 i = (mconfig->id.pvt_id) / 64;
264 else
265 i = 0;
266
267 module->pvt_id[i] &= ~(1 << (mconfig->id.pvt_id));
268 mconfig->id.pvt_id = -1;
269 return 0;
270 }
271 }
272
273 return -EINVAL;
274}
275EXPORT_SYMBOL_GPL(skl_put_pvt_id);
276
Shreyas NCea6b3e92016-05-30 17:42:59 +0530277/*
278 * Parse the firmware binary to get the UUID, module id
279 * and loadable flags
280 */
Senthilnathan Veppura8e2c192016-07-26 18:06:44 +0530281int snd_skl_parse_uuids(struct sst_dsp *ctx, const struct firmware *fw,
282 unsigned int offset, int index)
Shreyas NCea6b3e92016-05-30 17:42:59 +0530283{
284 struct adsp_fw_hdr *adsp_hdr;
285 struct adsp_module_entry *mod_entry;
Dharageswari R55a92ea2016-09-22 14:00:39 +0530286 int i, num_entry, size;
Shreyas NCea6b3e92016-05-30 17:42:59 +0530287 uuid_le *uuid_bin;
288 const char *buf;
289 struct skl_sst *skl = ctx->thread_context;
290 struct uuid_module *module;
291 struct firmware stripped_fw;
292 unsigned int safe_file;
293
294 /* Get the FW pointer to derive ADSP header */
Senthilnathan Veppura8e2c192016-07-26 18:06:44 +0530295 stripped_fw.data = fw->data;
296 stripped_fw.size = fw->size;
Shreyas NCea6b3e92016-05-30 17:42:59 +0530297
298 skl_dsp_strip_extended_manifest(&stripped_fw);
299
300 buf = stripped_fw.data;
301
302 /* check if we have enough space in file to move to header */
303 safe_file = sizeof(*adsp_hdr) + offset;
304 if (stripped_fw.size <= safe_file) {
305 dev_err(ctx->dev, "Small fw file size, No space for hdr\n");
306 return -EINVAL;
307 }
308
309 adsp_hdr = (struct adsp_fw_hdr *)(buf + offset);
310
311 /* check 1st module entry is in file */
312 safe_file += adsp_hdr->len + sizeof(*mod_entry);
313 if (stripped_fw.size <= safe_file) {
314 dev_err(ctx->dev, "Small fw file size, No module entry\n");
315 return -EINVAL;
316 }
317
318 mod_entry = (struct adsp_module_entry *)
319 (buf + offset + adsp_hdr->len);
320
321 num_entry = adsp_hdr->num_modules;
322
323 /* check all entries are in file */
324 safe_file += num_entry * sizeof(*mod_entry);
325 if (stripped_fw.size <= safe_file) {
326 dev_err(ctx->dev, "Small fw file size, No modules\n");
327 return -EINVAL;
328 }
329
330
331 /*
332 * Read the UUID(GUID) from FW Manifest.
333 *
334 * The 16 byte UUID format is: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX
335 * Populate the UUID table to store module_id and loadable flags
336 * for the module.
337 */
338
339 for (i = 0; i < num_entry; i++, mod_entry++) {
340 module = kzalloc(sizeof(*module), GFP_KERNEL);
341 if (!module)
342 return -ENOMEM;
343
344 uuid_bin = (uuid_le *)mod_entry->uuid.id;
345 memcpy(&module->uuid, uuid_bin, sizeof(module->uuid));
346
Senthilnathan Veppura8e2c192016-07-26 18:06:44 +0530347 module->id = (i | (index << 12));
Shreyas NCea6b3e92016-05-30 17:42:59 +0530348 module->is_loadable = mod_entry->type.load_type;
Dharageswari R700a9a62016-09-22 14:00:37 +0530349 module->max_instance = mod_entry->instance_max_count;
Dharageswari R55a92ea2016-09-22 14:00:39 +0530350 size = sizeof(int) * mod_entry->instance_max_count;
351 module->instance_id = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
352 if (!module->instance_id)
353 return -ENOMEM;
Shreyas NCea6b3e92016-05-30 17:42:59 +0530354
355 list_add_tail(&module->list, &skl->uuid_list);
356
357 dev_dbg(ctx->dev,
358 "Adding uuid :%pUL mod id: %d Loadable: %d\n",
359 &module->uuid, module->id, module->is_loadable);
360 }
361
362 return 0;
363}
364
365void skl_freeup_uuid_list(struct skl_sst *ctx)
366{
367 struct uuid_module *uuid, *_uuid;
368
369 list_for_each_entry_safe(uuid, _uuid, &ctx->uuid_list, list) {
370 list_del(&uuid->list);
371 kfree(uuid);
372 }
373}
374
Ramesh Babu6eee8722016-05-30 17:42:55 +0530375/*
376 * some firmware binary contains some extended manifest. This needs
377 * to be stripped in that case before we load and use that image.
378 *
Shreyas NCea6b3e92016-05-30 17:42:59 +0530379 * Get the module id for the module by checking
380 * the table for the UUID for the module
Ramesh Babu6eee8722016-05-30 17:42:55 +0530381 */
382int skl_dsp_strip_extended_manifest(struct firmware *fw)
383{
384 struct skl_ext_manifest_hdr *hdr;
385
386 /* check if fw file is greater than header we are looking */
387 if (fw->size < sizeof(hdr)) {
388 pr_err("%s: Firmware file small, no hdr\n", __func__);
389 return -EINVAL;
390 }
391
392 hdr = (struct skl_ext_manifest_hdr *)fw->data;
393
394 if (hdr->id == SKL_EXT_MANIFEST_HEADER_MAGIC) {
395 fw->size -= hdr->len;
396 fw->data += hdr->len;
397 }
398
399 return 0;
400}