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