~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/soc/intel/skylake/skl-sst-utils.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  *  skl-sst-utils.c - SKL sst utils functions
  4  *
  5  *  Copyright (C) 2016 Intel Corp
  6  */
  7 
  8 #include <linux/device.h>
  9 #include <linux/slab.h>
 10 #include <linux/uuid.h>
 11 #include "../common/sst-dsp.h"
 12 #include "../common/sst-dsp-priv.h"
 13 #include "skl.h"
 14 
 15 #define DEFAULT_HASH_SHA256_LEN 32
 16 
 17 /* FW Extended Manifest Header id = $AE1 */
 18 #define SKL_EXT_MANIFEST_HEADER_MAGIC   0x31454124
 19 
 20 union seg_flags {
 21         u32 ul;
 22         struct {
 23                 u32 contents : 1;
 24                 u32 alloc    : 1;
 25                 u32 load     : 1;
 26                 u32 read_only : 1;
 27                 u32 code     : 1;
 28                 u32 data     : 1;
 29                 u32 _rsvd0   : 2;
 30                 u32 type     : 4;
 31                 u32 _rsvd1   : 4;
 32                 u32 length   : 16;
 33         } r;
 34 } __packed;
 35 
 36 struct segment_desc {
 37         union seg_flags flags;
 38         u32 v_base_addr;
 39         u32 file_offset;
 40 };
 41 
 42 struct module_type {
 43         u32 load_type  : 4;
 44         u32 auto_start : 1;
 45         u32 domain_ll  : 1;
 46         u32 domain_dp  : 1;
 47         u32 rsvd       : 25;
 48 } __packed;
 49 
 50 struct adsp_module_entry {
 51         u32 struct_id;
 52         u8  name[8];
 53         u8  uuid[16];
 54         struct module_type type;
 55         u8  hash1[DEFAULT_HASH_SHA256_LEN];
 56         u32 entry_point;
 57         u16 cfg_offset;
 58         u16 cfg_count;
 59         u32 affinity_mask;
 60         u16 instance_max_count;
 61         u16 instance_bss_size;
 62         struct segment_desc segments[3];
 63 } __packed;
 64 
 65 struct adsp_fw_hdr {
 66         u32 id;
 67         u32 len;
 68         u8  name[8];
 69         u32 preload_page_count;
 70         u32 fw_image_flags;
 71         u32 feature_mask;
 72         u16 major;
 73         u16 minor;
 74         u16 hotfix;
 75         u16 build;
 76         u32 num_modules;
 77         u32 hw_buf_base;
 78         u32 hw_buf_length;
 79         u32 load_offset;
 80 } __packed;
 81 
 82 struct skl_ext_manifest_hdr {
 83         u32 id;
 84         u32 len;
 85         u16 version_major;
 86         u16 version_minor;
 87         u32 entries;
 88 };
 89 
 90 static int skl_get_pvtid_map(struct uuid_module *module, int instance_id)
 91 {
 92         int pvt_id;
 93 
 94         for (pvt_id = 0; pvt_id < module->max_instance; pvt_id++) {
 95                 if (module->instance_id[pvt_id] == instance_id)
 96                         return pvt_id;
 97         }
 98         return -EINVAL;
 99 }
100 
101 int skl_get_pvt_instance_id_map(struct skl_dev *skl,
102                                 int module_id, int instance_id)
103 {
104         struct uuid_module *module;
105 
106         list_for_each_entry(module, &skl->uuid_list, list) {
107                 if (module->id == module_id)
108                         return skl_get_pvtid_map(module, instance_id);
109         }
110 
111         return -EINVAL;
112 }
113 EXPORT_SYMBOL_GPL(skl_get_pvt_instance_id_map);
114 
115 static inline int skl_getid_32(struct uuid_module *module, u64 *val,
116                                 int word1_mask, int word2_mask)
117 {
118         int index, max_inst, pvt_id;
119         u32 mask_val;
120 
121         max_inst =  module->max_instance;
122         mask_val = (u32)(*val >> word1_mask);
123 
124         if (mask_val != 0xffffffff) {
125                 index = ffz(mask_val);
126                 pvt_id = index + word1_mask + word2_mask;
127                 if (pvt_id <= (max_inst - 1)) {
128                         *val |= 1ULL << (index + word1_mask);
129                         return pvt_id;
130                 }
131         }
132 
133         return -EINVAL;
134 }
135 
136 static inline int skl_pvtid_128(struct uuid_module *module)
137 {
138         int j, i, word1_mask, word2_mask = 0, pvt_id;
139 
140         for (j = 0; j < MAX_INSTANCE_BUFF; j++) {
141                 word1_mask = 0;
142 
143                 for (i = 0; i < 2; i++) {
144                         pvt_id = skl_getid_32(module, &module->pvt_id[j],
145                                                 word1_mask, word2_mask);
146                         if (pvt_id >= 0)
147                                 return pvt_id;
148 
149                         word1_mask += 32;
150                         if ((word1_mask + word2_mask) >= module->max_instance)
151                                 return -EINVAL;
152                 }
153 
154                 word2_mask += 64;
155                 if (word2_mask >= module->max_instance)
156                         return -EINVAL;
157         }
158 
159         return -EINVAL;
160 }
161 
162 /**
163  * skl_get_pvt_id: generate a private id for use as module id
164  *
165  * @skl: driver context
166  * @uuid_mod: module's uuid
167  * @instance_id: module's instance id
168  *
169  * This generates a 128 bit private unique id for a module TYPE so that
170  * module instance is unique
171  */
172 int skl_get_pvt_id(struct skl_dev *skl, guid_t *uuid_mod, int instance_id)
173 {
174         struct uuid_module *module;
175         int pvt_id;
176 
177         list_for_each_entry(module, &skl->uuid_list, list) {
178                 if (guid_equal(uuid_mod, &module->uuid)) {
179 
180                         pvt_id = skl_pvtid_128(module);
181                         if (pvt_id >= 0) {
182                                 module->instance_id[pvt_id] = instance_id;
183 
184                                 return pvt_id;
185                         }
186                 }
187         }
188 
189         return -EINVAL;
190 }
191 EXPORT_SYMBOL_GPL(skl_get_pvt_id);
192 
193 /**
194  * skl_put_pvt_id: free up the private id allocated
195  *
196  * @skl: driver context
197  * @uuid_mod: module's uuid
198  * @pvt_id: module pvt id
199  *
200  * This frees a 128 bit private unique id previously generated
201  */
202 int skl_put_pvt_id(struct skl_dev *skl, guid_t *uuid_mod, int *pvt_id)
203 {
204         int i;
205         struct uuid_module *module;
206 
207         list_for_each_entry(module, &skl->uuid_list, list) {
208                 if (guid_equal(uuid_mod, &module->uuid)) {
209 
210                         if (*pvt_id != 0)
211                                 i = (*pvt_id) / 64;
212                         else
213                                 i = 0;
214 
215                         module->pvt_id[i] &= ~(1 << (*pvt_id));
216                         *pvt_id = -1;
217                         return 0;
218                 }
219         }
220 
221         return -EINVAL;
222 }
223 EXPORT_SYMBOL_GPL(skl_put_pvt_id);
224 
225 /*
226  * Parse the firmware binary to get the UUID, module id
227  * and loadable flags
228  */
229 int snd_skl_parse_uuids(struct sst_dsp *ctx, const struct firmware *fw,
230                         unsigned int offset, int index)
231 {
232         struct adsp_fw_hdr *adsp_hdr;
233         struct adsp_module_entry *mod_entry;
234         int i, num_entry, size;
235         const char *buf;
236         struct skl_dev *skl = ctx->thread_context;
237         struct uuid_module *module;
238         struct firmware stripped_fw;
239         unsigned int safe_file;
240         int ret;
241 
242         /* Get the FW pointer to derive ADSP header */
243         stripped_fw.data = fw->data;
244         stripped_fw.size = fw->size;
245 
246         skl_dsp_strip_extended_manifest(&stripped_fw);
247 
248         buf = stripped_fw.data;
249 
250         /* check if we have enough space in file to move to header */
251         safe_file = sizeof(*adsp_hdr) + offset;
252         if (stripped_fw.size <= safe_file) {
253                 dev_err(ctx->dev, "Small fw file size, No space for hdr\n");
254                 return -EINVAL;
255         }
256 
257         adsp_hdr = (struct adsp_fw_hdr *)(buf + offset);
258 
259         /* check 1st module entry is in file */
260         safe_file += adsp_hdr->len + sizeof(*mod_entry);
261         if (stripped_fw.size <= safe_file) {
262                 dev_err(ctx->dev, "Small fw file size, No module entry\n");
263                 return -EINVAL;
264         }
265 
266         mod_entry = (struct adsp_module_entry *)(buf + offset + adsp_hdr->len);
267 
268         num_entry = adsp_hdr->num_modules;
269 
270         /* check all entries are in file */
271         safe_file += num_entry * sizeof(*mod_entry);
272         if (stripped_fw.size <= safe_file) {
273                 dev_err(ctx->dev, "Small fw file size, No modules\n");
274                 return -EINVAL;
275         }
276 
277 
278         /*
279          * Read the UUID(GUID) from FW Manifest.
280          *
281          * The 16 byte UUID format is: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX
282          * Populate the UUID table to store module_id and loadable flags
283          * for the module.
284          */
285 
286         for (i = 0; i < num_entry; i++, mod_entry++) {
287                 module = kzalloc(sizeof(*module), GFP_KERNEL);
288                 if (!module) {
289                         ret = -ENOMEM;
290                         goto free_uuid_list;
291                 }
292 
293                 import_guid(&module->uuid, mod_entry->uuid);
294 
295                 module->id = (i | (index << 12));
296                 module->is_loadable = mod_entry->type.load_type;
297                 module->max_instance = mod_entry->instance_max_count;
298                 size = sizeof(int) * mod_entry->instance_max_count;
299                 module->instance_id = devm_kzalloc(ctx->dev, size, GFP_KERNEL);
300                 if (!module->instance_id) {
301                         ret = -ENOMEM;
302                         kfree(module);
303                         goto free_uuid_list;
304                 }
305 
306                 list_add_tail(&module->list, &skl->uuid_list);
307 
308                 dev_dbg(ctx->dev,
309                         "Adding uuid :%pUL   mod id: %d  Loadable: %d\n",
310                         &module->uuid, module->id, module->is_loadable);
311         }
312 
313         return 0;
314 
315 free_uuid_list:
316         skl_freeup_uuid_list(skl);
317         return ret;
318 }
319 
320 void skl_freeup_uuid_list(struct skl_dev *skl)
321 {
322         struct uuid_module *uuid, *_uuid;
323 
324         list_for_each_entry_safe(uuid, _uuid, &skl->uuid_list, list) {
325                 list_del(&uuid->list);
326                 kfree(uuid);
327         }
328 }
329 
330 /*
331  * some firmware binary contains some extended manifest. This needs
332  * to be stripped in that case before we load and use that image.
333  *
334  * Get the module id for the module by checking
335  * the table for the UUID for the module
336  */
337 int skl_dsp_strip_extended_manifest(struct firmware *fw)
338 {
339         struct skl_ext_manifest_hdr *hdr;
340 
341         /* check if fw file is greater than header we are looking */
342         if (fw->size < sizeof(hdr)) {
343                 pr_err("%s: Firmware file small, no hdr\n", __func__);
344                 return -EINVAL;
345         }
346 
347         hdr = (struct skl_ext_manifest_hdr *)fw->data;
348 
349         if (hdr->id == SKL_EXT_MANIFEST_HEADER_MAGIC) {
350                 fw->size -= hdr->len;
351                 fw->data += hdr->len;
352         }
353 
354         return 0;
355 }
356 
357 int skl_sst_ctx_init(struct device *dev, int irq, const char *fw_name,
358         struct skl_dsp_loader_ops dsp_ops, struct skl_dev **dsp,
359         struct sst_dsp_device *skl_dev)
360 {
361         struct skl_dev *skl = *dsp;
362         struct sst_dsp *sst;
363 
364         skl->dev = dev;
365         skl_dev->thread_context = skl;
366         INIT_LIST_HEAD(&skl->uuid_list);
367         skl->dsp = skl_dsp_ctx_init(dev, skl_dev, irq);
368         if (!skl->dsp) {
369                 dev_err(skl->dev, "%s: no device\n", __func__);
370                 return -ENODEV;
371         }
372 
373         sst = skl->dsp;
374         sst->fw_name = fw_name;
375         sst->dsp_ops = dsp_ops;
376         init_waitqueue_head(&skl->mod_load_wait);
377         INIT_LIST_HEAD(&sst->module_list);
378 
379         skl->is_first_boot = true;
380 
381         return 0;
382 }
383 
384 int skl_prepare_lib_load(struct skl_dev *skl, struct skl_lib_info *linfo,
385                 struct firmware *stripped_fw,
386                 unsigned int hdr_offset, int index)
387 {
388         int ret;
389         struct sst_dsp *dsp = skl->dsp;
390 
391         if (linfo->fw == NULL) {
392                 ret = request_firmware(&linfo->fw, linfo->name,
393                                         skl->dev);
394                 if (ret < 0) {
395                         dev_err(skl->dev, "Request lib %s failed:%d\n",
396                                 linfo->name, ret);
397                         return ret;
398                 }
399         }
400 
401         if (skl->is_first_boot) {
402                 ret = snd_skl_parse_uuids(dsp, linfo->fw, hdr_offset, index);
403                 if (ret < 0)
404                         return ret;
405         }
406 
407         stripped_fw->data = linfo->fw->data;
408         stripped_fw->size = linfo->fw->size;
409         skl_dsp_strip_extended_manifest(stripped_fw);
410 
411         return 0;
412 }
413 
414 void skl_release_library(struct skl_lib_info *linfo, int lib_count)
415 {
416         int i;
417 
418         /* library indices start from 1 to N. 0 represents base FW */
419         for (i = 1; i < lib_count; i++) {
420                 if (linfo[i].fw) {
421                         release_firmware(linfo[i].fw);
422                         linfo[i].fw = NULL;
423                 }
424         }
425 }
426 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php