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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
// 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-common.h"
#include "dl/dsp-dl-engine.h"
#include "dl/dsp-lib-manager.h"
#include "dl/dsp-pm-manager.h"
#include "dl/dsp-gpt-manager.h"
#include "dl/dsp-dl-out-manager.h"
#include "dl/dsp-xml-parser.h"
static const char *dl_lib_path;
struct dsp_hash_tab *dsp_lib_hash;
void dsp_lib_init(struct dsp_lib *lib, struct dsp_dl_lib_info *info)
{
lib->name = (char *)dsp_dl_malloc(strlen(info->name) + 1,
"lib name");
strcpy(lib->name, info->name);
DL_DEBUG("lib name : %s\n", lib->name);
lib->elf = NULL;
lib->pm = NULL;
lib->gpt = NULL;
lib->dl_out = NULL;
lib->dl_out_mem = NULL;
lib->link_info = NULL;
lib->ref_cnt = 0;
lib->loaded = 0;
}
void dsp_lib_unload(struct dsp_lib *lib)
{
DL_DEBUG("DL lib unload\n");
if (lib->elf) {
dsp_elf32_free(lib->elf);
dsp_dl_free(lib->elf);
lib->elf = NULL;
}
if (lib->link_info) {
dsp_link_info_free(lib->link_info);
dsp_dl_free(lib->link_info);
lib->link_info = NULL;
}
}
void dsp_lib_free(struct dsp_lib *lib)
{
DL_DEBUG("DL lib free\n");
dsp_dl_free(lib->name);
dsp_lib_unload(lib);
dsp_pm_free(lib);
dsp_gpt_free(lib);
dsp_dl_out_free(lib);
}
void dsp_lib_print(struct dsp_lib *lib)
{
DL_INFO(DL_BORDER);
DL_INFO("Library name(%s) ref_cnt(%u) %s\n",
lib->name, lib->ref_cnt,
(lib->loaded) ? "loaded" : "unloaded");
if (lib->elf) {
DL_DEBUG("\n");
DL_DEBUG("ELF information\n");
dsp_elf32_print(lib->elf);
}
if (lib->pm) {
DL_INFO("\n");
DL_INFO("Program memory\n");
dsp_tlsf_mem_print(lib->pm);
DL_DEBUG("\n");
dsp_pm_print(lib);
}
if (lib->link_info) {
DL_INFO("\n");
DL_INFO("Linking information\n");
dsp_link_info_print(lib->link_info);
}
if (lib->gpt) {
DL_INFO("\n");
DL_INFO("Global pointer\n");
dsp_gpt_print(lib->gpt);
}
if (lib->dl_out_mem) {
DL_INFO("\n");
DL_INFO("Loader output\n");
dsp_tlsf_mem_print(lib->dl_out_mem);
dsp_dl_out_print(lib->dl_out);
}
}
void dsp_lib_manager_init(const char *lib_path)
{
DL_DEBUG("DL lib init\n");
dsp_lib_hash = (struct dsp_hash_tab *)dsp_dl_malloc(
sizeof(struct dsp_hash_tab),
"Dsp lib hash");
dl_lib_path = lib_path;
DL_DEBUG("%s\n", dl_lib_path);
dsp_hash_tab_init(dsp_lib_hash);
}
void dsp_lib_manager_free(void)
{
unsigned int idx;
DL_DEBUG("DL lib_manager free\n");
for (idx = 0; idx < DSP_HASH_MAX; idx++) {
struct dsp_list_node *cur, *next;
cur = (&dsp_lib_hash->list[idx])->next;
while (cur != NULL) {
struct dsp_hash_node *hash_node =
container_of(cur, struct dsp_hash_node, node);
struct dsp_lib *lib =
(struct dsp_lib *)hash_node->value;
next = cur->next;
dsp_lib_free(lib);
cur = next;
}
}
dsp_hash_free(dsp_lib_hash, 1);
dsp_dl_free(dsp_lib_hash);
}
void dsp_lib_manager_print(void)
{
unsigned int idx;
DL_INFO(DL_BORDER);
DL_INFO("Library manager\n");
for (idx = 0; idx < DSP_HASH_MAX; idx++) {
struct dsp_list_node *cur, *next;
cur = (&dsp_lib_hash->list[idx])->next;
while (cur != NULL) {
struct dsp_hash_node *hash_node =
container_of(cur, struct dsp_hash_node, node);
struct dsp_lib *lib =
(struct dsp_lib *)hash_node->value;
next = cur->next;
dsp_lib_print(lib);
cur = next;
}
}
}
struct dsp_lib **dsp_lib_manager_get_libs(struct dsp_dl_lib_info *lib_infos,
size_t lib_infos_size)
{
int ret;
unsigned int idx;
struct dsp_lib **libs = (struct dsp_lib **)dsp_dl_malloc(
sizeof(*libs) * lib_infos_size,
"struct dsp_lib list");
DL_DEBUG("DL lib_manager get libs\n");
for (idx = 0; idx < lib_infos_size; idx++) {
struct dsp_lib *lib;
const char *name = lib_infos[idx].name;
ret = dsp_hash_get(dsp_lib_hash, name, (void **)&lib);
if (ret == -1) {
DL_DEBUG("Create library %s\n", name);
lib = (struct dsp_lib *)dsp_dl_malloc(
sizeof(*lib), "Lib created");
dsp_lib_init(lib, &lib_infos[idx]);
dsp_hash_push(dsp_lib_hash, name, lib);
libs[idx] = lib;
} else {
DL_DEBUG("Check lib duplicate\n");
if (!lib->loaded) {
DL_ERROR("Library(%s) is duplicated\n",
lib->name);
dsp_lib_manager_delete_unloaded_libs(libs, idx);
dsp_dl_free(libs);
return NULL;
}
}
libs[idx] = lib;
}
return libs;
}
void dsp_lib_manager_inc_ref_cnt(struct dsp_lib **libs, size_t libs_size)
{
unsigned int idx;
DL_DEBUG("DL lib_manager inc_ref_cnt\n");
for (idx = 0; idx < libs_size; idx++)
libs[idx]->ref_cnt++;
}
void dsp_lib_manager_dec_ref_cnt(struct dsp_lib **libs, size_t libs_size)
{
unsigned int idx;
DL_DEBUG("DL lib_manager dec_ref_cnt\n");
for (idx = 0; idx < libs_size; idx++)
libs[idx]->ref_cnt--;
}
void dsp_lib_manager_delete_unloaded_libs(struct dsp_lib **libs,
size_t libs_size)
{
unsigned int idx;
DL_DEBUG("DL lib_manager delete unloaded libs\n");
for (idx = 0; idx < libs_size; idx++) {
if (!libs[idx]->loaded)
dsp_lib_manager_delete_lib(libs[idx]);
}
}
void dsp_lib_manager_delete_lib(struct dsp_lib *lib)
{
DL_DEBUG("DL lib-manager delete_lib\n");
dsp_hash_pop(dsp_lib_hash, lib->name, 0);
dsp_lib_free(lib);
dsp_dl_free(lib);
}
int dsp_lib_manager_delete_no_ref(void)
{
unsigned int idx;
int ret = 0;
DL_DEBUG("Delete no ref libs\n");
for (idx = 0; idx < DSP_HASH_MAX; idx++) {
struct dsp_list_node *cur, *next;
cur = (&dsp_lib_hash->list[idx])->next;
while (cur != NULL) {
struct dsp_hash_node *hash_node =
container_of(cur, struct dsp_hash_node, node);
struct dsp_lib *lib =
(struct dsp_lib *)hash_node->value;
if (lib->loaded && lib->ref_cnt == 0) {
DL_DEBUG("Delete lib(%s)\n", lib->name);
ret = 1;
dsp_lib_manager_delete_lib(lib);
}
next = cur->next;
cur = next;
}
}
return ret;
}
static int __dsp_lib_manager_load_kernel_table(struct dsp_lib *lib,
struct dsp_dl_out_section sec)
{
unsigned int ret;
struct dsp_xml_lib *xml_lib;
struct dsp_dl_kernel_table *kernel_table;
unsigned int idx;
ret = dsp_hash_get(&xml_libs->lib_hash, lib->name,
(void **)&xml_lib);
if (ret == -1U) {
DL_ERROR("No Library %s\n", lib->name);
return -1;
}
kernel_table = (struct dsp_dl_kernel_table *)(lib->dl_out->data +
sec.offset);
for (idx = 0; idx < xml_lib->kernel_cnt; idx++) {
struct dsp_xml_kernel_table *kernel = &xml_lib->kernels[idx];
ret = dsp_link_info_get_kernel_addr(lib->link_info,
kernel->pre);
if (ret == (unsigned int) -1) {
DL_ERROR("[%s] pre CHK_ERR\n", __func__);
return -1;
}
kernel_table[idx].pre = ret;
ret = dsp_link_info_get_kernel_addr(lib->link_info,
kernel->exe);
if (ret == (unsigned int) -1) {
DL_ERROR("[%s] exe CHK_ERR\n", __func__);
return -1;
}
kernel_table[idx].exe = ret;
ret = dsp_link_info_get_kernel_addr(lib->link_info,
kernel->post);
if (ret == (unsigned int) -1) {
DL_ERROR("[%s] post CHK_ERR\n", __func__);
return -1;
}
kernel_table[idx].post = ret;
}
return 0;
}
static void __dsp_lib_manager_load_gpt(struct dsp_lib *lib)
{
DL_DEBUG("load gpt\n");
lib->dl_out->gpt_addr = (unsigned int)lib->gpt->addr;
}
static void __dsp_lib_manager_load_pm(struct dsp_lib *lib)
{
struct dsp_elf32 *elf = lib->elf;
struct dsp_list_node *node;
DL_DEBUG("load pm\n");
dsp_list_for_each(node, &elf->text.text) {
struct dsp_elf32_idx_node *idx_node =
container_of(node, struct dsp_elf32_idx_node, node);
unsigned int ndx = idx_node->idx;
struct dsp_elf32_shdr *text_hdr = elf->shdr + ndx;
unsigned char *text = (unsigned char *)(elf->data
+ text_hdr->sh_offset);
unsigned char *text_end = text + text_hdr->sh_size;
unsigned char *dest = (unsigned char *)(dsp_pm_start_addr +
lib->link_info->sec[ndx]);
DL_DEBUG("Dest : %p, Src : %p, Src end : %p, size : %u\n",
dest, text, text_end, text_hdr->sh_size);
for (; text < text_end; text += 4, dest += 4) {
int idx;
for (idx = 0; idx < 4; idx++)
dest[idx] = text[3 - idx];
}
}
}
static void __dsp_lib_manager_load_bss_sec(struct dsp_lib *lib,
struct dsp_list_head *head, struct dsp_dl_out_section sec)
{
struct dsp_list_node *node;
struct dsp_elf32 *elf = lib->elf;
DL_DEBUG("load bss sec\n");
dsp_list_for_each(node, head) {
struct dsp_elf32_idx_node *idx_node =
container_of(node, struct dsp_elf32_idx_node, node);
unsigned int ndx = idx_node->idx;
struct dsp_elf32_shdr *mem_hdr = elf->shdr + ndx;
char *dest = lib->dl_out->data + sec.offset +
lib->link_info->sec[ndx];
unsigned long end = (unsigned long)(dest + mem_hdr->sh_size);
unsigned int *addr;
DL_DEBUG("load sec %u\n", ndx);
DL_DEBUG("Dest : %p, size : %u\n", dest, mem_hdr->sh_size);
for (addr = (unsigned int *)dest; (unsigned long)addr < end;
addr++)
*addr = 0;
}
}
static void __dsp_lib_manager_load_sec(struct dsp_lib *lib,
struct dsp_list_head *head, struct dsp_dl_out_section sec,
int rev_endian)
{
struct dsp_list_node *node;
struct dsp_elf32 *elf = lib->elf;
DL_DEBUG("load sec\n");
dsp_list_for_each(node, head) {
struct dsp_elf32_idx_node *idx_node =
container_of(node, struct dsp_elf32_idx_node, node);
unsigned int ndx = idx_node->idx;
struct dsp_elf32_shdr *mem_hdr = elf->shdr + ndx;
unsigned char *data = (unsigned char *)(elf->data +
mem_hdr->sh_offset);
unsigned char *data_end = data + mem_hdr->sh_size;
unsigned char *dest = lib->dl_out->data + sec.offset +
lib->link_info->sec[ndx];
DL_DEBUG("load sec %u\n", ndx);
DL_DEBUG("Dest : %p, Src : %p, size : %u\n",
dest, data, mem_hdr->sh_size);
if (rev_endian) {
for (; data < data_end; data += 4, dest += 4) {
int idx;
for (idx = 0; idx < 4; idx++)
dest[idx] = data[3 - idx];
}
} else
memcpy(dest, data, mem_hdr->sh_size);
}
}
static void __dsp_lib_manager_load_mem(struct dsp_lib *lib,
struct dsp_elf32_mem *mem, struct dsp_dl_out_section sec,
int rev_endian)
{
DL_DEBUG("load mem\n");
DL_DEBUG("Load robss\n");
__dsp_lib_manager_load_bss_sec(lib, &mem->robss, sec);
DL_DEBUG("Load rodata\n");
__dsp_lib_manager_load_sec(lib, &mem->rodata, sec, rev_endian);
DL_DEBUG("Load bss\n");
__dsp_lib_manager_load_bss_sec(lib, &mem->bss, sec);
DL_DEBUG("Load data\n");
__dsp_lib_manager_load_sec(lib, &mem->data, sec, rev_endian);
}
int dsp_lib_manager_load_libs(struct dsp_lib **libs, size_t libs_size)
{
int ret;
unsigned int idx;
DL_DEBUG(DL_BORDER);
DL_DEBUG("DL lib_manager load libs\n");
for (idx = 0; idx < libs_size; idx++) {
if (!libs[idx]->loaded) {
struct dsp_dl_out *dl_out = libs[idx]->dl_out;
if (dl_out) {
DL_DEBUG("DL out data addr : %p\n",
dl_out->data);
DL_DEBUG("Load Kernel table\n");
ret = __dsp_lib_manager_load_kernel_table(
libs[idx],
dl_out->kernel_table);
if (ret == -1) {
DL_ERROR("[%s] CHK_ERR\n", __func__);
return -1;
}
}
if (libs[idx]->pm) {
DL_DEBUG("Load PM\n");
__dsp_lib_manager_load_pm(libs[idx]);
}
if (libs[idx]->gpt) {
DL_DEBUG("Load GPT\n");
__dsp_lib_manager_load_gpt(libs[idx]);
}
if (libs[idx]->dl_out && libs[idx]->dl_out_mem) {
DL_DEBUG("Load DM\n");
__dsp_lib_manager_load_mem(libs[idx],
&libs[idx]->elf->DMb,
dl_out->DM_sh, 0);
DL_DEBUG("Load DM_local\n");
__dsp_lib_manager_load_mem(libs[idx],
&libs[idx]->elf->DMb_local,
dl_out->DM_local, 0);
DL_DEBUG("Load TCM\n");
__dsp_lib_manager_load_mem(libs[idx],
&libs[idx]->elf->TCMb,
dl_out->TCM_sh, 0);
DL_DEBUG("Load TCM_local\n");
__dsp_lib_manager_load_mem(libs[idx],
&libs[idx]->elf->TCMb_local,
dl_out->TCM_local, 0);
DL_DEBUG("Load Shared mem\n");
__dsp_lib_manager_load_mem(libs[idx],
&libs[idx]->elf->SFRw,
dl_out->sh_mem, 1);
}
libs[idx]->loaded = 1;
}
}
return 0;
}
void dsp_lib_manager_unload_libs(struct dsp_lib **libs, size_t libs_size)
{
unsigned int idx;
DL_DEBUG("DL lib_manager unload libs\n");
dsp_lib_manager_dec_ref_cnt(libs, libs_size);
for (idx = 0; idx < libs_size; idx++) {
if (libs[idx]->ref_cnt == 0)
dsp_lib_unload(libs[idx]);
}
}
|