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