1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
// SPDX-License-Identifier: GPL-2.0
/*
* Samsung Exynos SoC series dsp driver
*
* Copyright (c) 2019 Samsung Electronics Co., Ltd.
* http://www.samsung.com/
*/
#include "dl/dsp-dl-out-manager.h"
#include "dl/dsp-elf-loader.h"
#include "dl/dsp-lib-manager.h"
#include "dl/dsp-xml-parser.h"
#define DL_DL_OUT_ALIGN (4)
#define DL_HASH_SIZE (300)
#define DL_HASH_END (0xFFFFFFFF)
static unsigned int *dl_hash;
struct dsp_tlsf *out_manager;
unsigned int dsp_dl_hash_get_key(char *data)
{
int len;
unsigned int hash, tmp;
int rem;
if (data == NULL)
return 0;
len = strlen(data);
if (len <= 0)
return 0;
hash = len;
rem = len & 3;
len >>= 2;
for (; len > 0; len--) {
hash += get16bits(data);
tmp = (get16bits(data + 2) << 11) ^ hash;
hash = (hash << 16) ^ tmp;
data += 2 * sizeof(unsigned short);
hash += hash >> 11;
}
switch (rem) {
case 3:
hash += get16bits(data);
hash ^= hash << 16;
hash ^= ((signed char)data[sizeof(unsigned short)]) << 18;
hash += hash >> 11;
break;
case 2:
hash += get16bits(data);
hash ^= hash << 11;
hash += hash >> 17;
break;
case 1:
hash += (signed char)(*data);
hash ^= hash << 10;
hash += hash >> 1;
}
hash ^= hash << 3;
hash += hash >> 5;
hash ^= hash << 4;
hash += hash >> 17;
hash ^= hash << 25;
hash += hash >> 6;
return hash % DL_HASH_SIZE;
}
void dsp_dl_hash_init(void)
{
int idx;
unsigned int *node;
for (idx = 0; idx < DL_HASH_SIZE; idx++) {
node = dl_hash + idx;
*node = DL_HASH_END;
}
}
void dsp_dl_hash_push(struct dsp_dl_out *dl_out)
{
unsigned int key = dsp_dl_hash_get_key(dl_out->data);
dl_out->hash_next = dl_hash[key];
dl_hash[key] = (unsigned long)dl_out - (unsigned long)dl_hash;
DL_DEBUG("key : %u, head : %u\n", key, dl_hash[key]);
}
void dsp_dl_hash_pop(char *k)
{
unsigned int key = dsp_dl_hash_get_key(k);
unsigned int *node = dl_hash + key;
struct dsp_dl_out *next;
while (*node != DL_HASH_END) {
next = (struct dsp_dl_out *)((unsigned long)dl_hash + *node);
if (strcmp(next->data, k) == 0) {
*node = next->hash_next;
return;
}
node = &next->hash_next;
}
DL_ERROR("No hash node for %s\n", k);
}
void dsp_dl_hash_print(void)
{
int idx;
unsigned int *node;
struct dsp_dl_out *next;
for (idx = 0; idx < DL_HASH_SIZE; idx++) {
node = dl_hash + idx;
while (*node != DL_HASH_END) {
DL_INFO("\n");
DL_INFO("Hash node: key(%u) offset(%u)\n", idx, *node);
next = (struct dsp_dl_out *)(
(unsigned long)dl_hash + *node);
DL_INFO("Library name: %s\n", next->data);
DL_INFO("Hash next offset: 0x%x\n", next->hash_next);
node = &next->hash_next;
}
}
}
static unsigned int __dsp_dl_out_offset_align(unsigned int offset)
{
return (offset + 0x3) & ~0x3;
}
int dsp_dl_out_create(struct dsp_lib *lib)
{
int ret;
unsigned int offset = 0;
struct dsp_xml_lib *xml_lib;
DL_DEBUG("DL out create\n");
lib->dl_out = (struct dsp_dl_out *)dsp_dl_malloc(
sizeof(*lib->dl_out), "lib dl_out");
lib->dl_out->hash_next = DL_HASH_END;
offset += strlen(lib->name) + 1;
offset = __dsp_dl_out_offset_align(offset);
lib->dl_out->kernel_table.offset = offset;
ret = dsp_hash_get(&xml_libs->lib_hash, lib->name,
(void **)&xml_lib);
if (ret == -1) {
DL_ERROR("No global table for %s\n", lib->name);
dsp_dl_free(lib->dl_out);
lib->dl_out = NULL;
return -1;
}
lib->dl_out->kernel_table.size = xml_lib->kernel_cnt *
sizeof(struct dsp_dl_kernel_table);
offset += lib->dl_out->kernel_table.size;
offset = __dsp_dl_out_offset_align(offset);
lib->dl_out->DM_sh.offset = offset;
lib->dl_out->DM_sh.size = dsp_elf32_get_mem_size(&lib->elf->DMb,
lib->elf);
offset += lib->dl_out->DM_sh.size;
offset = __dsp_dl_out_offset_align(offset);
lib->dl_out->DM_local.offset = offset;
lib->dl_out->DM_local.size = dsp_elf32_get_mem_size(
&lib->elf->DMb_local, lib->elf);
offset += lib->dl_out->DM_local.size;
offset = __dsp_dl_out_offset_align(offset);
lib->dl_out->TCM_sh.offset = offset;
lib->dl_out->TCM_sh.size = dsp_elf32_get_mem_size(&lib->elf->TCMb,
lib->elf);
offset += lib->dl_out->TCM_sh.size;
offset = __dsp_dl_out_offset_align(offset);
lib->dl_out->TCM_local.offset = offset;
lib->dl_out->TCM_local.size = dsp_elf32_get_mem_size(
&lib->elf->TCMb_local, lib->elf);
offset += lib->dl_out->TCM_local.size;
offset = __dsp_dl_out_offset_align(offset);
lib->dl_out->sh_mem.offset = offset;
lib->dl_out->sh_mem.size = dsp_elf32_get_mem_size(&lib->elf->SFRw,
lib->elf);
return 0;
}
size_t dsp_dl_out_get_size(struct dsp_dl_out *dl_out)
{
return sizeof(*dl_out) + dl_out->sh_mem.offset + dl_out->sh_mem.size;
}
static void __dsp_dl_out_cpy_metadata(struct dsp_dl_out *op1,
struct dsp_dl_out *op2)
{
memcpy(op1, op2, sizeof(*op1));
}
static void __dsp_dl_out_print_sec_data(struct dsp_dl_out *dl_out,
struct dsp_dl_out_section sec)
{
unsigned int idx;
unsigned int *data = (unsigned int *)(dl_out->data + sec.offset);
unsigned int sec_end = sec.size / sizeof(unsigned int);
for (idx = 0; idx < sec_end; idx++) {
DL_BUF_STR("0x%08x ", data[idx]);
if ((idx + 1) % 4 == 0 || idx == sec_end - 1) {
DL_BUF_STR("\n");
DL_PRINT_BUF(DEBUG);
}
}
}
static void __dsp_dl_out_print_kernel_table(struct dsp_dl_out *dl_out)
{
struct dsp_dl_out_section sec = dl_out->kernel_table;
unsigned int idx;
unsigned int *data = (unsigned int *)(dl_out->data + sec.offset);
unsigned int sec_end = sec.size / sizeof(unsigned int);
for (idx = 0; idx < sec_end; idx++) {
DL_BUF_STR("0x%08x ", data[idx]);
if ((idx + 1) % 3 == 0 || idx == sec_end - 1) {
DL_BUF_STR("\n");
DL_PRINT_BUF(DEBUG);
}
}
}
static void __dsp_dl_out_print_data(struct dsp_dl_out *dl_out)
{
DL_DEBUG("Kernel address table\n");
DL_DEBUG(" Pre Exe Post\n");
__dsp_dl_out_print_kernel_table(dl_out);
DL_DEBUG("DM shared data\n");
__dsp_dl_out_print_sec_data(dl_out, dl_out->DM_sh);
DL_DEBUG("DM thread local data\n");
__dsp_dl_out_print_sec_data(dl_out, dl_out->DM_local);
DL_DEBUG("TCM shared data\n");
__dsp_dl_out_print_sec_data(dl_out, dl_out->TCM_sh);
DL_DEBUG("TCM thread local data\n");
__dsp_dl_out_print_sec_data(dl_out, dl_out->TCM_local);
DL_DEBUG("Shared memory data\n");
__dsp_dl_out_print_sec_data(dl_out, dl_out->sh_mem);
}
void dsp_dl_out_print(struct dsp_dl_out *dl_out)
{
DL_INFO("Library name: %s\n", dl_out->data);
DL_INFO("Hash next offset: 0x%x\n", dl_out->hash_next);
DL_INFO("Gpt address: 0x%x\n", dl_out->gpt_addr);
DL_INFO("\n");
DL_INFO("Data offsets\n");
DL_INFO("Kernel table: offset(%u) size(%u)\n",
dl_out->kernel_table.offset, dl_out->kernel_table.size);
DL_INFO("DM shared data: offset(%u) size(%u\n",
dl_out->DM_sh.offset, dl_out->DM_sh.size);
DL_INFO("DM thread local data: offset(%u) size(%u)\n",
dl_out->DM_local.offset, dl_out->DM_local.size);
DL_INFO("TCM shared data: offset(%u) size(%u)\n",
dl_out->TCM_sh.offset, dl_out->TCM_sh.size);
DL_INFO("TCM thread local data: offset(%u) size(%u)\n",
dl_out->TCM_local.offset, dl_out->TCM_local.size);
DL_INFO("Shared memory data: offset(%u) size(%u)\n",
dl_out->sh_mem.offset, dl_out->sh_mem.size);
DL_INFO("\n");
DL_DEBUG("Data loaded\n");
__dsp_dl_out_print_data(dl_out);
}
int dsp_dl_out_manager_init(unsigned long start_addr, size_t size)
{
unsigned long mem_str;
size_t mem_size;
DL_DEBUG("DL out manager init\n");
out_manager = (struct dsp_tlsf *)dsp_dl_malloc(
sizeof(struct dsp_tlsf), "Out manager");
if (size <= sizeof(unsigned int) * DL_HASH_SIZE) {
DL_ERROR("Size(%zu) is not enough for DL_out\n", size);
dsp_dl_free(out_manager);
return -1;
}
dl_hash = (unsigned int *)start_addr;
DL_DEBUG("DL hash : 0x%p\n", dl_hash);
dsp_dl_hash_init();
mem_str = (unsigned long)((unsigned int *)start_addr + DL_HASH_SIZE);
mem_size = size - sizeof(unsigned int) * DL_HASH_SIZE;
dsp_tlsf_init(out_manager, mem_str, mem_size, DL_DL_OUT_ALIGN);
return 0;
}
int dsp_dl_out_manager_free(void)
{
dsp_tlsf_delete(out_manager);
dsp_dl_free(out_manager);
return 0;
}
void dsp_dl_out_manager_print(void)
{
DL_INFO(DL_BORDER);
DL_INFO("Dynamic loader output manager\n");
DL_INFO("\n");
dsp_tlsf_print(out_manager);
DL_INFO("\n");
DL_INFO("Output hash table\n");
dsp_dl_hash_print();
}
int dsp_dl_out_manager_alloc_libs(struct dsp_lib **libs, int libs_size,
int *pm_inv)
{
int ret, idx;
DL_DEBUG("Alloc DL out\n");
for (idx = 0; idx < libs_size; idx++) {
if (!libs[idx]->dl_out) {
DL_DEBUG("Alloc DL out for library %s\n",
libs[idx]->name);
ret = dsp_dl_out_alloc(libs[idx], pm_inv);
if (ret == -1) {
DL_ERROR("Alloc DL out failed\n");
return -1;
}
}
}
return 0;
}
static int __dsp_dl_out_alloc_mem(size_t size, struct dsp_lib *lib,
int *pm_inv)
{
int ret;
ret = dsp_tlsf_malloc(size, &lib->dl_out_mem,
out_manager);
if (ret == -1) {
if (dsp_tlsf_can_be_loaded(out_manager, size)) {
dsp_lib_manager_delete_no_ref();
*pm_inv = 1;
__dsp_dl_out_alloc_mem(size, lib, pm_inv);
} else {
DL_ERROR("Can not alloc DL out memory for library %s\n",
lib->name);
return -1;
}
}
return 0;
}
int dsp_dl_out_alloc(struct dsp_lib *lib, int *pm_inv)
{
int ret;
size_t dl_out_size;
int alloc_ret;
unsigned long mem_addr;
DL_DEBUG("DL out alloc\n");
ret = dsp_dl_out_create(lib);
if (ret == -1) {
DL_ERROR("[%s] CHK_ERR\n", __func__);
return -1;
}
dl_out_size = dsp_dl_out_get_size(lib->dl_out);
DL_DEBUG("DL_out_size : %zu\n", dl_out_size);
alloc_ret = __dsp_dl_out_alloc_mem(dl_out_size, lib,
pm_inv);
if (alloc_ret == -1) {
dsp_dl_free(lib->dl_out);
lib->dl_out = NULL;
lib->dl_out_mem = NULL;
return -1;
}
lib->dl_out_mem->lib = lib;
mem_addr = lib->dl_out_mem->start_addr;
__dsp_dl_out_cpy_metadata((struct dsp_dl_out *)mem_addr, lib->dl_out);
dsp_dl_free(lib->dl_out);
lib->dl_out = (struct dsp_dl_out *)mem_addr;
strcpy(lib->dl_out->data, lib->name);
DL_DEBUG("lib name : %s\n", lib->dl_out->data);
dsp_dl_hash_push(lib->dl_out);
return 0;
}
void dsp_dl_out_free(struct dsp_lib *lib)
{
if (lib->dl_out && lib->dl_out_mem) {
dsp_dl_hash_pop(lib->dl_out->data);
dsp_tlsf_free(lib->dl_out_mem, out_manager);
lib->dl_out = NULL;
lib->dl_out_mem = NULL;
}
}
|