Merge remote-tracking branch 'spi/for-next'
[deliverable/linux.git] / sound / soc / intel / skylake / skl-sst-utils.c
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
16 #include <linux/device.h>
17 #include <linux/slab.h>
18 #include <linux/uuid.h>
19 #include "skl-sst-dsp.h"
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
27
28 /* FW Extended Manifest Header id = $AE1 */
29 #define SKL_EXT_MANIFEST_HEADER_MAGIC 0x31454124
30
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
97 struct uuid_module {
98 uuid_le uuid;
99 int id;
100 int is_loadable;
101
102 struct list_head list;
103 };
104
105 struct skl_ext_manifest_hdr {
106 u32 id;
107 u32 len;
108 u16 version_major;
109 u16 version_minor;
110 u32 entries;
111 };
112
113 int snd_skl_get_module_info(struct skl_sst *ctx,
114 struct skl_module_cfg *mconfig)
115 {
116 struct uuid_module *module;
117 uuid_le *uuid_mod;
118
119 uuid_mod = (uuid_le *)mconfig->guid;
120
121 if (list_empty(&ctx->uuid_list)) {
122 dev_err(ctx->dev, "Module list is empty\n");
123 return -EINVAL;
124 }
125
126 list_for_each_entry(module, &ctx->uuid_list, list) {
127 if (uuid_le_cmp(*uuid_mod, module->uuid) == 0) {
128 mconfig->id.module_id = module->id;
129 mconfig->is_loadable = module->is_loadable;
130
131 return 0;
132 }
133 }
134
135 return -EINVAL;
136 }
137 EXPORT_SYMBOL_GPL(snd_skl_get_module_info);
138
139 /*
140 * Parse the firmware binary to get the UUID, module id
141 * and loadable flags
142 */
143 int snd_skl_parse_uuids(struct sst_dsp *ctx, const struct firmware *fw,
144 unsigned int offset, int index)
145 {
146 struct adsp_fw_hdr *adsp_hdr;
147 struct adsp_module_entry *mod_entry;
148 int i, num_entry;
149 uuid_le *uuid_bin;
150 const char *buf;
151 struct skl_sst *skl = ctx->thread_context;
152 struct uuid_module *module;
153 struct firmware stripped_fw;
154 unsigned int safe_file;
155
156 /* Get the FW pointer to derive ADSP header */
157 stripped_fw.data = fw->data;
158 stripped_fw.size = fw->size;
159
160 skl_dsp_strip_extended_manifest(&stripped_fw);
161
162 buf = stripped_fw.data;
163
164 /* check if we have enough space in file to move to header */
165 safe_file = sizeof(*adsp_hdr) + offset;
166 if (stripped_fw.size <= safe_file) {
167 dev_err(ctx->dev, "Small fw file size, No space for hdr\n");
168 return -EINVAL;
169 }
170
171 adsp_hdr = (struct adsp_fw_hdr *)(buf + offset);
172
173 /* check 1st module entry is in file */
174 safe_file += adsp_hdr->len + sizeof(*mod_entry);
175 if (stripped_fw.size <= safe_file) {
176 dev_err(ctx->dev, "Small fw file size, No module entry\n");
177 return -EINVAL;
178 }
179
180 mod_entry = (struct adsp_module_entry *)
181 (buf + offset + adsp_hdr->len);
182
183 num_entry = adsp_hdr->num_modules;
184
185 /* check all entries are in file */
186 safe_file += num_entry * sizeof(*mod_entry);
187 if (stripped_fw.size <= safe_file) {
188 dev_err(ctx->dev, "Small fw file size, No modules\n");
189 return -EINVAL;
190 }
191
192
193 /*
194 * Read the UUID(GUID) from FW Manifest.
195 *
196 * The 16 byte UUID format is: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX
197 * Populate the UUID table to store module_id and loadable flags
198 * for the module.
199 */
200
201 for (i = 0; i < num_entry; i++, mod_entry++) {
202 module = kzalloc(sizeof(*module), GFP_KERNEL);
203 if (!module)
204 return -ENOMEM;
205
206 uuid_bin = (uuid_le *)mod_entry->uuid.id;
207 memcpy(&module->uuid, uuid_bin, sizeof(module->uuid));
208
209 module->id = (i | (index << 12));
210 module->is_loadable = mod_entry->type.load_type;
211
212 list_add_tail(&module->list, &skl->uuid_list);
213
214 dev_dbg(ctx->dev,
215 "Adding uuid :%pUL mod id: %d Loadable: %d\n",
216 &module->uuid, module->id, module->is_loadable);
217 }
218
219 return 0;
220 }
221
222 void skl_freeup_uuid_list(struct skl_sst *ctx)
223 {
224 struct uuid_module *uuid, *_uuid;
225
226 list_for_each_entry_safe(uuid, _uuid, &ctx->uuid_list, list) {
227 list_del(&uuid->list);
228 kfree(uuid);
229 }
230 }
231
232 /*
233 * some firmware binary contains some extended manifest. This needs
234 * to be stripped in that case before we load and use that image.
235 *
236 * Get the module id for the module by checking
237 * the table for the UUID for the module
238 */
239 int skl_dsp_strip_extended_manifest(struct firmware *fw)
240 {
241 struct skl_ext_manifest_hdr *hdr;
242
243 /* check if fw file is greater than header we are looking */
244 if (fw->size < sizeof(hdr)) {
245 pr_err("%s: Firmware file small, no hdr\n", __func__);
246 return -EINVAL;
247 }
248
249 hdr = (struct skl_ext_manifest_hdr *)fw->data;
250
251 if (hdr->id == SKL_EXT_MANIFEST_HEADER_MAGIC) {
252 fw->size -= hdr->len;
253 fw->data += hdr->len;
254 }
255
256 return 0;
257 }
This page took 0.041941 seconds and 5 git commands to generate.