4 * DSP-BIOS Bridge driver support functions for TI OMAP processors.
6 * DSP/BIOS Bridge dynamic + overlay Node loader.
8 * Copyright (C) 2005-2006 Texas Instruments, Inc.
10 * This package is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
14 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 #include <linux/types.h>
21 #include <dspbridge/host_os.h>
23 #include <dspbridge/dbdefs.h>
25 #include <dspbridge/dbc.h>
27 /* Platform manager */
28 #include <dspbridge/cod.h>
29 #include <dspbridge/dev.h>
31 /* Resource manager */
32 #include <dspbridge/dbll.h>
33 #include <dspbridge/dbdcd.h>
34 #include <dspbridge/rmm.h>
35 #include <dspbridge/uuidutil.h>
37 #include <dspbridge/nldr.h>
38 #include <linux/lcm.h>
40 /* Name of section containing dynamic load mem */
41 #define DYNMEMSECT ".dspbridge_mem"
43 /* Name of section containing dependent library information */
44 #define DEPLIBSECT ".dspbridge_deplibs"
46 /* Max depth of recursion for loading node's dependent libraries */
49 /* Max number of persistent libraries kept by a node */
53 * Defines for extracting packed dynamic load memory requirements from two
55 * These defines must match node.cdb and dynm.cdb
56 * Format of data/code mask is:
57 * uuuuuuuu|fueeeeee|fudddddd|fucccccc|
60 * cccccc = prefered/required dynamic mem segid for create phase data/code
61 * dddddd = prefered/required dynamic mem segid for delete phase data/code
62 * eeeeee = prefered/req. dynamic mem segid for execute phase data/code
63 * f = flag indicating if memory is preferred or required:
64 * f = 1 if required, f = 0 if preferred.
66 * The 6 bits of the segid are interpreted as follows:
68 * If the 6th bit (bit 5) is not set, then this specifies a memory segment
69 * between 0 and 31 (a maximum of 32 dynamic loading memory segments).
70 * If the 6th bit (bit 5) is set, segid has the following interpretation:
71 * segid = 32 - Any internal memory segment can be used.
72 * segid = 33 - Any external memory segment can be used.
73 * segid = 63 - Any memory segment can be used (in this case the
74 * required/preferred flag is irrelevant).
77 /* Maximum allowed dynamic loading memory segments */
80 #define MAXSEGID 3 /* Largest possible (real) segid */
81 #define MEMINTERNALID 32 /* Segid meaning use internal mem */
82 #define MEMEXTERNALID 33 /* Segid meaning use external mem */
83 #define NULLID 63 /* Segid meaning no memory req/pref */
84 #define FLAGBIT 7 /* 7th bit is pref./req. flag */
85 #define SEGMASK 0x3f /* Bits 0 - 5 */
87 #define CREATEBIT 0 /* Create segid starts at bit 0 */
88 #define DELETEBIT 8 /* Delete segid starts at bit 8 */
89 #define EXECUTEBIT 16 /* Execute segid starts at bit 16 */
92 * Masks that define memory type. Must match defines in dynm.cdb.
96 #define DYNM_CODEDATA (DYNM_CODE | DYNM_DATA)
97 #define DYNM_INTERNAL 0x8
98 #define DYNM_EXTERNAL 0x10
101 * Defines for packing memory requirement/preference flags for code and
102 * data of each of the node's phases into one mask.
103 * The bit is set if the segid is required for loading code/data of the
104 * given phase. The bit is not set, if the segid is preferred only.
106 * These defines are also used as indeces into a segid array for the node.
107 * eg node's segid[CREATEDATAFLAGBIT] is the memory segment id that the
108 * create phase data is required or preferred to be loaded into.
110 #define CREATEDATAFLAGBIT 0
111 #define CREATECODEFLAGBIT 1
112 #define EXECUTEDATAFLAGBIT 2
113 #define EXECUTECODEFLAGBIT 3
114 #define DELETEDATAFLAGBIT 4
115 #define DELETECODEFLAGBIT 5
119 * These names may be embedded in overlay sections to identify which
120 * node phase the section should be overlayed.
122 #define PCREATE "create"
123 #define PDELETE "delete"
124 #define PEXECUTE "execute"
126 static inline bool is_equal_uuid(struct dsp_uuid *uuid1,
127 struct dsp_uuid *uuid2)
129 return !memcmp(uuid1, uuid2, sizeof(struct dsp_uuid));
133 * ======== mem_seg_info ========
134 * Format of dynamic loading memory segment info in coff file.
135 * Must match dynm.h55.
137 struct mem_seg_info {
138 u32 segid; /* Dynamic loading memory segment number */
141 u32 type; /* Mask of DYNM_CODE, DYNM_INTERNAL, etc. */
145 * ======== lib_node ========
146 * For maintaining a tree of library dependencies.
149 struct dbll_library_obj *lib; /* The library */
150 u16 dep_libs; /* Number of dependent libraries */
151 struct lib_node *dep_libs_tree; /* Dependent libraries of lib */
155 * ======== ovly_sect ========
156 * Information needed to overlay a section.
159 struct ovly_sect *next_sect;
160 u32 sect_load_addr; /* Load address of section */
161 u32 sect_run_addr; /* Run address of section */
162 u32 size; /* Size of section */
163 u16 page; /* DBL_CODE, DBL_DATA */
167 * ======== ovly_node ========
168 * For maintaining a list of overlay nodes, with sections that need to be
169 * overlayed for each of the nodes phases.
172 struct dsp_uuid uuid;
174 struct ovly_sect *create_sects_list;
175 struct ovly_sect *delete_sects_list;
176 struct ovly_sect *execute_sects_list;
177 struct ovly_sect *other_sects_list;
189 * ======== nldr_object ========
190 * Overlay loader object.
193 struct dev_object *hdev_obj; /* Device object */
194 struct dcd_manager *hdcd_mgr; /* Proc/Node data manager */
195 struct dbll_tar_obj *dbll; /* The DBL loader */
196 struct dbll_library_obj *base_lib; /* Base image library */
197 struct rmm_target_obj *rmm; /* Remote memory manager for DSP */
198 struct dbll_fxns ldr_fxns; /* Loader function table */
199 struct dbll_attrs ldr_attrs; /* attrs to pass to loader functions */
200 nldr_ovlyfxn ovly_fxn; /* "write" for overlay nodes */
201 nldr_writefxn write_fxn; /* "write" for dynamic nodes */
202 struct ovly_node *ovly_table; /* Table of overlay nodes */
203 u16 ovly_nodes; /* Number of overlay nodes in base */
204 u16 ovly_nid; /* Index for tracking overlay nodes */
205 u16 dload_segs; /* Number of dynamic load mem segs */
206 u32 *seg_table; /* memtypes of dynamic memory segs
209 u16 us_dsp_mau_size; /* Size of DSP MAU */
210 u16 us_dsp_word_size; /* Size of DSP word */
214 * ======== nldr_nodeobject ========
215 * Dynamic node object. This object is created when a node is allocated.
217 struct nldr_nodeobject {
218 struct nldr_object *nldr_obj; /* Dynamic loader handle */
219 void *priv_ref; /* Handle to pass to dbl_write_fxn */
220 struct dsp_uuid uuid; /* Node's UUID */
221 bool dynamic; /* Dynamically loaded node? */
222 bool overlay; /* Overlay node? */
223 bool *pf_phase_split; /* Multiple phase libraries? */
224 struct lib_node root; /* Library containing node phase */
225 struct lib_node create_lib; /* Library with create phase lib */
226 struct lib_node execute_lib; /* Library with execute phase lib */
227 struct lib_node delete_lib; /* Library with delete phase lib */
228 /* libs remain loaded until Delete */
229 struct lib_node pers_lib_table[MAXLIBS];
230 s32 pers_libs; /* Number of persistent libraries */
231 /* Path in lib dependency tree */
232 struct dbll_library_obj *lib_path[MAXDEPTH + 1];
233 enum nldr_phase phase; /* Node phase currently being loaded */
236 * Dynamic loading memory segments for data and code of each phase.
238 u16 seg_id[MAXFLAGS];
241 * Mask indicating whether each mem segment specified in seg_id[]
242 * is preferred or required.
244 * if (code_data_flag_mask & (1 << EXECUTEDATAFLAGBIT)) != 0,
245 * then it is required to load execute phase data into the memory
246 * specified by seg_id[EXECUTEDATAFLAGBIT].
248 u32 code_data_flag_mask;
251 /* Dynamic loader function table */
252 static struct dbll_fxns ldr_fxns = {
253 (dbll_close_fxn) dbll_close,
254 (dbll_create_fxn) dbll_create,
255 (dbll_delete_fxn) dbll_delete,
256 (dbll_exit_fxn) dbll_exit,
257 (dbll_get_attrs_fxn) dbll_get_attrs,
258 (dbll_get_addr_fxn) dbll_get_addr,
259 (dbll_get_c_addr_fxn) dbll_get_c_addr,
260 (dbll_get_sect_fxn) dbll_get_sect,
261 (dbll_init_fxn) dbll_init,
262 (dbll_load_fxn) dbll_load,
263 (dbll_load_sect_fxn) dbll_load_sect,
264 (dbll_open_fxn) dbll_open,
265 (dbll_read_sect_fxn) dbll_read_sect,
266 (dbll_set_attrs_fxn) dbll_set_attrs,
267 (dbll_unload_fxn) dbll_unload,
268 (dbll_unload_sect_fxn) dbll_unload_sect,
271 static u32 refs; /* module reference count */
273 static int add_ovly_info(void *handle, struct dbll_sect_info *sect_info,
274 u32 addr, u32 bytes);
275 static int add_ovly_node(struct dsp_uuid *uuid_obj,
276 enum dsp_dcdobjtype obj_type, void *handle);
277 static int add_ovly_sect(struct nldr_object *nldr_obj,
278 struct ovly_sect **lst,
279 struct dbll_sect_info *sect_inf,
280 bool *exists, u32 addr, u32 bytes);
281 static s32 fake_ovly_write(void *handle, u32 dsp_address, void *buf, u32 bytes,
283 static void free_sects(struct nldr_object *nldr_obj,
284 struct ovly_sect *phase_sects, u16 alloc_num);
285 static bool get_symbol_value(void *handle, void *parg, void *rmm_handle,
286 char *sym_name, struct dbll_sym_val **sym);
287 static int load_lib(struct nldr_nodeobject *nldr_node_obj,
288 struct lib_node *root, struct dsp_uuid uuid,
290 struct dbll_library_obj **lib_path,
291 enum nldr_phase phase, u16 depth);
292 static int load_ovly(struct nldr_nodeobject *nldr_node_obj,
293 enum nldr_phase phase);
294 static int remote_alloc(void **ref, u16 mem_sect, u32 size,
295 u32 align, u32 *dsp_address,
297 s32 req, bool reserve);
298 static int remote_free(void **ref, u16 space, u32 dsp_address, u32 size,
301 static void unload_lib(struct nldr_nodeobject *nldr_node_obj,
302 struct lib_node *root);
303 static void unload_ovly(struct nldr_nodeobject *nldr_node_obj,
304 enum nldr_phase phase);
305 static bool find_in_persistent_lib_array(struct nldr_nodeobject *nldr_node_obj,
306 struct dbll_library_obj *lib);
309 * ======== nldr_allocate ========
311 int nldr_allocate(struct nldr_object *nldr_obj, void *priv_ref,
312 const struct dcd_nodeprops *node_props,
313 struct nldr_nodeobject **nldr_nodeobj,
314 bool *pf_phase_split)
316 struct nldr_nodeobject *nldr_node_obj = NULL;
319 DBC_REQUIRE(refs > 0);
320 DBC_REQUIRE(node_props != NULL);
321 DBC_REQUIRE(nldr_nodeobj != NULL);
322 DBC_REQUIRE(nldr_obj);
324 /* Initialize handle in case of failure */
325 *nldr_nodeobj = NULL;
326 /* Allocate node object */
327 nldr_node_obj = kzalloc(sizeof(struct nldr_nodeobject), GFP_KERNEL);
329 if (nldr_node_obj == NULL) {
332 nldr_node_obj->pf_phase_split = pf_phase_split;
333 nldr_node_obj->pers_libs = 0;
334 nldr_node_obj->nldr_obj = nldr_obj;
335 nldr_node_obj->priv_ref = priv_ref;
336 /* Save node's UUID. */
337 nldr_node_obj->uuid = node_props->ndb_props.ui_node_id;
339 * Determine if node is a dynamically loaded node from
342 if (node_props->us_load_type == NLDR_DYNAMICLOAD) {
344 nldr_node_obj->dynamic = true;
346 * Extract memory requirements from ndb_props masks
349 nldr_node_obj->seg_id[CREATEDATAFLAGBIT] = (u16)
350 (node_props->ul_data_mem_seg_mask >> CREATEBIT) &
352 nldr_node_obj->code_data_flag_mask |=
353 ((node_props->ul_data_mem_seg_mask >>
354 (CREATEBIT + FLAGBIT)) & 1) << CREATEDATAFLAGBIT;
355 nldr_node_obj->seg_id[CREATECODEFLAGBIT] = (u16)
356 (node_props->ul_code_mem_seg_mask >>
357 CREATEBIT) & SEGMASK;
358 nldr_node_obj->code_data_flag_mask |=
359 ((node_props->ul_code_mem_seg_mask >>
360 (CREATEBIT + FLAGBIT)) & 1) << CREATECODEFLAGBIT;
362 nldr_node_obj->seg_id[EXECUTEDATAFLAGBIT] = (u16)
363 (node_props->ul_data_mem_seg_mask >>
364 EXECUTEBIT) & SEGMASK;
365 nldr_node_obj->code_data_flag_mask |=
366 ((node_props->ul_data_mem_seg_mask >>
367 (EXECUTEBIT + FLAGBIT)) & 1) <<
369 nldr_node_obj->seg_id[EXECUTECODEFLAGBIT] = (u16)
370 (node_props->ul_code_mem_seg_mask >>
371 EXECUTEBIT) & SEGMASK;
372 nldr_node_obj->code_data_flag_mask |=
373 ((node_props->ul_code_mem_seg_mask >>
374 (EXECUTEBIT + FLAGBIT)) & 1) <<
377 nldr_node_obj->seg_id[DELETEDATAFLAGBIT] = (u16)
378 (node_props->ul_data_mem_seg_mask >> DELETEBIT) &
380 nldr_node_obj->code_data_flag_mask |=
381 ((node_props->ul_data_mem_seg_mask >>
382 (DELETEBIT + FLAGBIT)) & 1) << DELETEDATAFLAGBIT;
383 nldr_node_obj->seg_id[DELETECODEFLAGBIT] = (u16)
384 (node_props->ul_code_mem_seg_mask >>
385 DELETEBIT) & SEGMASK;
386 nldr_node_obj->code_data_flag_mask |=
387 ((node_props->ul_code_mem_seg_mask >>
388 (DELETEBIT + FLAGBIT)) & 1) << DELETECODEFLAGBIT;
390 /* Non-dynamically loaded nodes are part of the
392 nldr_node_obj->root.lib = nldr_obj->base_lib;
393 /* Check for overlay node */
394 if (node_props->us_load_type == NLDR_OVLYLOAD)
395 nldr_node_obj->overlay = true;
398 *nldr_nodeobj = (struct nldr_nodeobject *)nldr_node_obj;
400 /* Cleanup on failure */
401 if (status && nldr_node_obj)
402 kfree(nldr_node_obj);
404 DBC_ENSURE((!status && *nldr_nodeobj)
405 || (status && *nldr_nodeobj == NULL));
410 * ======== nldr_create ========
412 int nldr_create(struct nldr_object **nldr,
413 struct dev_object *hdev_obj,
414 const struct nldr_attrs *pattrs)
416 struct cod_manager *cod_mgr; /* COD manager */
417 char *psz_coff_buf = NULL;
418 char sz_zl_file[COD_MAXPATHLENGTH];
419 struct nldr_object *nldr_obj = NULL;
420 struct dbll_attrs save_attrs;
421 struct dbll_attrs new_attrs;
425 struct mem_seg_info *mem_info_obj;
428 struct rmm_segment *rmm_segs = NULL;
431 DBC_REQUIRE(refs > 0);
432 DBC_REQUIRE(nldr != NULL);
433 DBC_REQUIRE(hdev_obj != NULL);
434 DBC_REQUIRE(pattrs != NULL);
435 DBC_REQUIRE(pattrs->pfn_ovly != NULL);
436 DBC_REQUIRE(pattrs->pfn_write != NULL);
438 /* Allocate dynamic loader object */
439 nldr_obj = kzalloc(sizeof(struct nldr_object), GFP_KERNEL);
441 nldr_obj->hdev_obj = hdev_obj;
442 /* warning, lazy status checking alert! */
443 dev_get_cod_mgr(hdev_obj, &cod_mgr);
445 status = cod_get_loader(cod_mgr, &nldr_obj->dbll);
447 status = cod_get_base_lib(cod_mgr, &nldr_obj->base_lib);
450 cod_get_base_name(cod_mgr, sz_zl_file,
455 /* end lazy status checking */
456 nldr_obj->us_dsp_mau_size = pattrs->us_dsp_mau_size;
457 nldr_obj->us_dsp_word_size = pattrs->us_dsp_word_size;
458 nldr_obj->ldr_fxns = ldr_fxns;
459 if (!(nldr_obj->ldr_fxns.init_fxn()))
465 /* Create the DCD Manager */
467 status = dcd_create_manager(NULL, &nldr_obj->hdcd_mgr);
469 /* Get dynamic loading memory sections from base lib */
472 nldr_obj->ldr_fxns.get_sect_fxn(nldr_obj->base_lib,
473 DYNMEMSECT, &ul_addr,
477 kzalloc(ul_len * nldr_obj->us_dsp_mau_size,
482 /* Ok to not have dynamic loading memory */
485 dev_dbg(bridge, "%s: failed - no dynamic loading mem "
486 "segments: 0x%x\n", __func__, status);
489 if (!status && ul_len > 0) {
490 /* Read section containing dynamic load mem segments */
492 nldr_obj->ldr_fxns.read_sect_fxn(nldr_obj->base_lib,
493 DYNMEMSECT, psz_coff_buf,
496 if (!status && ul_len > 0) {
497 /* Parse memory segment data */
498 dload_segs = (u16) (*((u32 *) psz_coff_buf));
499 if (dload_segs > MAXMEMSEGS)
502 /* Parse dynamic load memory segments */
503 if (!status && dload_segs > 0) {
504 rmm_segs = kzalloc(sizeof(struct rmm_segment) * dload_segs,
506 nldr_obj->seg_table =
507 kzalloc(sizeof(u32) * dload_segs, GFP_KERNEL);
508 if (rmm_segs == NULL || nldr_obj->seg_table == NULL) {
511 nldr_obj->dload_segs = dload_segs;
512 mem_info_obj = (struct mem_seg_info *)(psz_coff_buf +
514 for (i = 0; i < dload_segs; i++) {
515 rmm_segs[i].base = (mem_info_obj + i)->base;
516 rmm_segs[i].length = (mem_info_obj + i)->len;
517 rmm_segs[i].space = 0;
518 nldr_obj->seg_table[i] =
519 (mem_info_obj + i)->type;
521 "(proc) DLL MEMSEGMENT: %d, "
522 "Base: 0x%x, Length: 0x%x\n", i,
523 rmm_segs[i].base, rmm_segs[i].length);
527 /* Create Remote memory manager */
529 status = rmm_create(&nldr_obj->rmm, rmm_segs, dload_segs);
532 /* set the alloc, free, write functions for loader */
533 nldr_obj->ldr_fxns.get_attrs_fxn(nldr_obj->dbll, &save_attrs);
534 new_attrs = save_attrs;
535 new_attrs.alloc = (dbll_alloc_fxn) remote_alloc;
536 new_attrs.free = (dbll_free_fxn) remote_free;
537 new_attrs.sym_lookup = (dbll_sym_lookup) get_symbol_value;
538 new_attrs.sym_handle = nldr_obj;
539 new_attrs.write = (dbll_write_fxn) pattrs->pfn_write;
540 nldr_obj->ovly_fxn = pattrs->pfn_ovly;
541 nldr_obj->write_fxn = pattrs->pfn_write;
542 nldr_obj->ldr_attrs = new_attrs;
548 /* Get overlay nodes */
551 cod_get_base_name(cod_mgr, sz_zl_file, COD_MAXPATHLENGTH);
554 /* First count number of overlay nodes */
556 dcd_get_objects(nldr_obj->hdcd_mgr, sz_zl_file,
557 add_ovly_node, (void *)nldr_obj);
558 /* Now build table of overlay nodes */
559 if (!status && nldr_obj->ovly_nodes > 0) {
560 /* Allocate table for overlay nodes */
561 nldr_obj->ovly_table =
562 kzalloc(sizeof(struct ovly_node) *
563 nldr_obj->ovly_nodes, GFP_KERNEL);
564 /* Put overlay nodes in the table */
565 nldr_obj->ovly_nid = 0;
566 status = dcd_get_objects(nldr_obj->hdcd_mgr, sz_zl_file,
571 /* Do a fake reload of the base image to get overlay section info */
572 if (!status && nldr_obj->ovly_nodes > 0) {
573 save_attrs.write = fake_ovly_write;
574 save_attrs.log_write = add_ovly_info;
575 save_attrs.log_write_handle = nldr_obj;
576 flags = DBLL_CODE | DBLL_DATA | DBLL_SYMB;
577 status = nldr_obj->ldr_fxns.load_fxn(nldr_obj->base_lib, flags,
578 &save_attrs, &ul_entry);
581 *nldr = (struct nldr_object *)nldr_obj;
584 nldr_delete((struct nldr_object *)nldr_obj);
588 /* FIXME:Temp. Fix. Must be removed */
589 DBC_ENSURE((!status && *nldr) || (status && *nldr == NULL));
594 * ======== nldr_delete ========
596 void nldr_delete(struct nldr_object *nldr_obj)
598 struct ovly_sect *ovly_section;
599 struct ovly_sect *next;
601 DBC_REQUIRE(refs > 0);
602 DBC_REQUIRE(nldr_obj);
604 nldr_obj->ldr_fxns.exit_fxn();
606 rmm_delete(nldr_obj->rmm);
608 kfree(nldr_obj->seg_table);
610 if (nldr_obj->hdcd_mgr)
611 dcd_destroy_manager(nldr_obj->hdcd_mgr);
613 /* Free overlay node information */
614 if (nldr_obj->ovly_table) {
615 for (i = 0; i < nldr_obj->ovly_nodes; i++) {
617 nldr_obj->ovly_table[i].create_sects_list;
618 while (ovly_section) {
619 next = ovly_section->next_sect;
624 nldr_obj->ovly_table[i].delete_sects_list;
625 while (ovly_section) {
626 next = ovly_section->next_sect;
631 nldr_obj->ovly_table[i].execute_sects_list;
632 while (ovly_section) {
633 next = ovly_section->next_sect;
637 ovly_section = nldr_obj->ovly_table[i].other_sects_list;
638 while (ovly_section) {
639 next = ovly_section->next_sect;
644 kfree(nldr_obj->ovly_table);
650 * ======== nldr_exit ========
651 * Discontinue usage of NLDR module.
655 DBC_REQUIRE(refs > 0);
662 DBC_ENSURE(refs >= 0);
666 * ======== nldr_get_fxn_addr ========
668 int nldr_get_fxn_addr(struct nldr_nodeobject *nldr_node_obj,
669 char *str_fxn, u32 * addr)
671 struct dbll_sym_val *dbll_sym;
672 struct nldr_object *nldr_obj;
674 bool status1 = false;
676 struct lib_node root = { NULL, 0, NULL };
677 DBC_REQUIRE(refs > 0);
678 DBC_REQUIRE(nldr_node_obj);
679 DBC_REQUIRE(addr != NULL);
680 DBC_REQUIRE(str_fxn != NULL);
682 nldr_obj = nldr_node_obj->nldr_obj;
683 /* Called from node_create(), node_delete(), or node_run(). */
684 if (nldr_node_obj->dynamic && *nldr_node_obj->pf_phase_split) {
685 switch (nldr_node_obj->phase) {
687 root = nldr_node_obj->create_lib;
690 root = nldr_node_obj->execute_lib;
693 root = nldr_node_obj->delete_lib;
700 /* for Overlay nodes or non-split Dynamic nodes */
701 root = nldr_node_obj->root;
704 nldr_obj->ldr_fxns.get_c_addr_fxn(root.lib, str_fxn, &dbll_sym);
707 nldr_obj->ldr_fxns.get_addr_fxn(root.lib, str_fxn,
710 /* If symbol not found, check dependent libraries */
712 for (i = 0; i < root.dep_libs; i++) {
714 nldr_obj->ldr_fxns.get_addr_fxn(root.dep_libs_tree
720 get_c_addr_fxn(root.dep_libs_tree[i].lib,
729 /* Check persistent libraries */
731 for (i = 0; i < nldr_node_obj->pers_libs; i++) {
734 get_addr_fxn(nldr_node_obj->pers_lib_table[i].lib,
739 get_c_addr_fxn(nldr_node_obj->pers_lib_table
740 [i].lib, str_fxn, &dbll_sym);
750 *addr = dbll_sym->value;
758 * ======== nldr_get_rmm_manager ========
759 * Given a NLDR object, retrieve RMM Manager Handle
761 int nldr_get_rmm_manager(struct nldr_object *nldr,
762 struct rmm_target_obj **rmm_mgr)
765 struct nldr_object *nldr_obj = nldr;
766 DBC_REQUIRE(rmm_mgr != NULL);
769 *rmm_mgr = nldr_obj->rmm;
775 DBC_ENSURE(!status || (rmm_mgr != NULL && *rmm_mgr == NULL));
781 * ======== nldr_init ========
782 * Initialize the NLDR module.
786 DBC_REQUIRE(refs >= 0);
793 DBC_ENSURE(refs > 0);
798 * ======== nldr_load ========
800 int nldr_load(struct nldr_nodeobject *nldr_node_obj,
801 enum nldr_phase phase)
803 struct nldr_object *nldr_obj;
804 struct dsp_uuid lib_uuid;
807 DBC_REQUIRE(refs > 0);
808 DBC_REQUIRE(nldr_node_obj);
810 nldr_obj = nldr_node_obj->nldr_obj;
812 if (nldr_node_obj->dynamic) {
813 nldr_node_obj->phase = phase;
815 lib_uuid = nldr_node_obj->uuid;
817 /* At this point, we may not know if node is split into
818 * different libraries. So we'll go ahead and load the
819 * library, and then save the pointer to the appropriate
820 * location after we know. */
823 load_lib(nldr_node_obj, &nldr_node_obj->root, lib_uuid,
824 false, nldr_node_obj->lib_path, phase, 0);
827 if (*nldr_node_obj->pf_phase_split) {
830 nldr_node_obj->create_lib =
835 nldr_node_obj->execute_lib =
840 nldr_node_obj->delete_lib =
851 if (nldr_node_obj->overlay)
852 status = load_ovly(nldr_node_obj, phase);
860 * ======== nldr_unload ========
862 int nldr_unload(struct nldr_nodeobject *nldr_node_obj,
863 enum nldr_phase phase)
866 struct lib_node *root_lib = NULL;
869 DBC_REQUIRE(refs > 0);
870 DBC_REQUIRE(nldr_node_obj);
872 if (nldr_node_obj != NULL) {
873 if (nldr_node_obj->dynamic) {
874 if (*nldr_node_obj->pf_phase_split) {
877 root_lib = &nldr_node_obj->create_lib;
880 root_lib = &nldr_node_obj->execute_lib;
883 root_lib = &nldr_node_obj->delete_lib;
884 /* Unload persistent libraries */
886 i < nldr_node_obj->pers_libs;
888 unload_lib(nldr_node_obj,
892 nldr_node_obj->pers_libs = 0;
899 /* Unload main library */
900 root_lib = &nldr_node_obj->root;
903 unload_lib(nldr_node_obj, root_lib);
905 if (nldr_node_obj->overlay)
906 unload_ovly(nldr_node_obj, phase);
914 * ======== add_ovly_info ========
916 static int add_ovly_info(void *handle, struct dbll_sect_info *sect_info,
920 char *sect_name = (char *)sect_info->name;
921 bool sect_exists = false;
925 struct nldr_object *nldr_obj = (struct nldr_object *)handle;
928 /* Is this an overlay section (load address != run address)? */
929 if (sect_info->sect_load_addr == sect_info->sect_run_addr)
932 /* Find the node it belongs to */
933 for (i = 0; i < nldr_obj->ovly_nodes; i++) {
934 node_name = nldr_obj->ovly_table[i].node_name;
935 DBC_REQUIRE(node_name);
936 if (strncmp(node_name, sect_name + 1, strlen(node_name)) == 0) {
941 if (!(i < nldr_obj->ovly_nodes))
944 /* Determine which phase this section belongs to */
945 for (pch = sect_name + 1; *pch && *pch != seps; pch++)
949 pch++; /* Skip over the ':' */
950 if (strncmp(pch, PCREATE, strlen(PCREATE)) == 0) {
952 add_ovly_sect(nldr_obj,
954 ovly_table[i].create_sects_list,
955 sect_info, §_exists, addr, bytes);
956 if (!status && !sect_exists)
957 nldr_obj->ovly_table[i].create_sects++;
959 } else if (strncmp(pch, PDELETE, strlen(PDELETE)) == 0) {
961 add_ovly_sect(nldr_obj,
963 ovly_table[i].delete_sects_list,
964 sect_info, §_exists, addr, bytes);
965 if (!status && !sect_exists)
966 nldr_obj->ovly_table[i].delete_sects++;
968 } else if (strncmp(pch, PEXECUTE, strlen(PEXECUTE)) == 0) {
970 add_ovly_sect(nldr_obj,
972 ovly_table[i].execute_sects_list,
973 sect_info, §_exists, addr, bytes);
974 if (!status && !sect_exists)
975 nldr_obj->ovly_table[i].execute_sects++;
978 /* Put in "other" sectins */
980 add_ovly_sect(nldr_obj,
982 ovly_table[i].other_sects_list,
983 sect_info, §_exists, addr, bytes);
984 if (!status && !sect_exists)
985 nldr_obj->ovly_table[i].other_sects++;
994 * ======== add_ovly_node =========
995 * Callback function passed to dcd_get_objects.
997 static int add_ovly_node(struct dsp_uuid *uuid_obj,
998 enum dsp_dcdobjtype obj_type, void *handle)
1000 struct nldr_object *nldr_obj = (struct nldr_object *)handle;
1001 char *node_name = NULL;
1004 struct dcd_genericobj obj_def;
1007 if (obj_type != DSP_DCDNODETYPE)
1011 dcd_get_object_def(nldr_obj->hdcd_mgr, uuid_obj, obj_type,
1016 /* If overlay node, add to the list */
1017 if (obj_def.obj_data.node_obj.us_load_type == NLDR_OVLYLOAD) {
1018 if (nldr_obj->ovly_table == NULL) {
1019 nldr_obj->ovly_nodes++;
1021 /* Add node to table */
1022 nldr_obj->ovly_table[nldr_obj->ovly_nid].uuid =
1024 DBC_REQUIRE(obj_def.obj_data.node_obj.ndb_props.
1027 strlen(obj_def.obj_data.node_obj.ndb_props.ac_name);
1028 node_name = obj_def.obj_data.node_obj.ndb_props.ac_name;
1029 pbuf = kzalloc(len + 1, GFP_KERNEL);
1033 strncpy(pbuf, node_name, len);
1034 nldr_obj->ovly_table[nldr_obj->ovly_nid].
1036 nldr_obj->ovly_nid++;
1040 /* These were allocated in dcd_get_object_def */
1041 kfree(obj_def.obj_data.node_obj.pstr_create_phase_fxn);
1043 kfree(obj_def.obj_data.node_obj.pstr_execute_phase_fxn);
1045 kfree(obj_def.obj_data.node_obj.pstr_delete_phase_fxn);
1047 kfree(obj_def.obj_data.node_obj.pstr_i_alg_name);
1054 * ======== add_ovly_sect ========
1056 static int add_ovly_sect(struct nldr_object *nldr_obj,
1057 struct ovly_sect **lst,
1058 struct dbll_sect_info *sect_inf,
1059 bool *exists, u32 addr, u32 bytes)
1061 struct ovly_sect *new_sect = NULL;
1062 struct ovly_sect *last_sect;
1063 struct ovly_sect *ovly_section;
1066 ovly_section = last_sect = *lst;
1068 while (ovly_section) {
1070 * Make sure section has not already been added. Multiple
1071 * 'write' calls may be made to load the section.
1073 if (ovly_section->sect_load_addr == addr) {
1078 last_sect = ovly_section;
1079 ovly_section = ovly_section->next_sect;
1082 if (!ovly_section) {
1084 new_sect = kzalloc(sizeof(struct ovly_sect), GFP_KERNEL);
1085 if (new_sect == NULL) {
1088 new_sect->sect_load_addr = addr;
1089 new_sect->sect_run_addr = sect_inf->sect_run_addr +
1090 (addr - sect_inf->sect_load_addr);
1091 new_sect->size = bytes;
1092 new_sect->page = sect_inf->type;
1095 /* Add to the list */
1098 /* First in the list */
1101 last_sect->next_sect = new_sect;
1110 * ======== fake_ovly_write ========
1112 static s32 fake_ovly_write(void *handle, u32 dsp_address, void *buf, u32 bytes,
1119 * ======== free_sects ========
1121 static void free_sects(struct nldr_object *nldr_obj,
1122 struct ovly_sect *phase_sects, u16 alloc_num)
1124 struct ovly_sect *ovly_section = phase_sects;
1128 while (ovly_section && i < alloc_num) {
1130 /* segid - page not supported yet */
1131 /* Reserved memory */
1133 rmm_free(nldr_obj->rmm, 0, ovly_section->sect_run_addr,
1134 ovly_section->size, true);
1136 ovly_section = ovly_section->next_sect;
1142 * ======== get_symbol_value ========
1143 * Find symbol in library's base image. If not there, check dependent
1146 static bool get_symbol_value(void *handle, void *parg, void *rmm_handle,
1147 char *sym_name, struct dbll_sym_val **sym)
1149 struct nldr_object *nldr_obj = (struct nldr_object *)handle;
1150 struct nldr_nodeobject *nldr_node_obj =
1151 (struct nldr_nodeobject *)rmm_handle;
1152 struct lib_node *root = (struct lib_node *)parg;
1154 bool status = false;
1156 /* check the base image */
1157 status = nldr_obj->ldr_fxns.get_addr_fxn(nldr_obj->base_lib,
1161 nldr_obj->ldr_fxns.get_c_addr_fxn(nldr_obj->base_lib,
1165 * Check in root lib itself. If the library consists of
1166 * multiple object files linked together, some symbols in the
1167 * library may need to be resolved.
1170 status = nldr_obj->ldr_fxns.get_addr_fxn(root->lib, sym_name,
1174 nldr_obj->ldr_fxns.get_c_addr_fxn(root->lib,
1180 * Check in root lib's dependent libraries, but not dependent
1181 * libraries' dependents.
1184 for (i = 0; i < root->dep_libs; i++) {
1186 nldr_obj->ldr_fxns.get_addr_fxn(root->
1193 get_c_addr_fxn(root->dep_libs_tree[i].lib,
1203 * Check in persistent libraries
1206 for (i = 0; i < nldr_node_obj->pers_libs; i++) {
1209 get_addr_fxn(nldr_node_obj->pers_lib_table[i].lib,
1212 status = nldr_obj->ldr_fxns.get_c_addr_fxn
1213 (nldr_node_obj->pers_lib_table[i].lib,
1227 * ======== load_lib ========
1228 * Recursively load library and all its dependent libraries. The library
1229 * we're loading is specified by a uuid.
1231 static int load_lib(struct nldr_nodeobject *nldr_node_obj,
1232 struct lib_node *root, struct dsp_uuid uuid,
1234 struct dbll_library_obj **lib_path,
1235 enum nldr_phase phase, u16 depth)
1237 struct nldr_object *nldr_obj = nldr_node_obj->nldr_obj;
1238 u16 nd_libs = 0; /* Number of dependent libraries */
1239 u16 np_libs = 0; /* Number of persistent libraries */
1240 u16 nd_libs_loaded = 0; /* Number of dep. libraries loaded */
1243 u32 dw_buf_size = NLDR_MAXPATHLENGTH;
1244 dbll_flags flags = DBLL_SYMB | DBLL_CODE | DBLL_DATA | DBLL_DYNAMIC;
1245 struct dbll_attrs new_attrs;
1246 char *psz_file_name = NULL;
1247 struct dsp_uuid *dep_lib_uui_ds = NULL;
1248 bool *persistent_dep_libs = NULL;
1250 bool lib_status = false;
1251 struct lib_node *dep_lib;
1253 if (depth > MAXDEPTH) {
1258 /* Allocate a buffer for library file name of size DBL_MAXPATHLENGTH */
1259 psz_file_name = kzalloc(DBLL_MAXPATHLENGTH, GFP_KERNEL);
1260 if (psz_file_name == NULL)
1264 /* Get the name of the library */
1267 dcd_get_library_name(nldr_node_obj->nldr_obj->
1268 hdcd_mgr, &uuid, psz_file_name,
1269 &dw_buf_size, phase,
1270 nldr_node_obj->pf_phase_split);
1272 /* Dependent libraries are registered with a phase */
1274 dcd_get_library_name(nldr_node_obj->nldr_obj->
1275 hdcd_mgr, &uuid, psz_file_name,
1276 &dw_buf_size, NLDR_NOPHASE,
1281 /* Open the library, don't load symbols */
1283 nldr_obj->ldr_fxns.open_fxn(nldr_obj->dbll, psz_file_name,
1284 DBLL_NOLOAD, &root->lib);
1286 /* Done with file name */
1287 kfree(psz_file_name);
1289 /* Check to see if library not already loaded */
1290 if (!status && root_prstnt) {
1292 find_in_persistent_lib_array(nldr_node_obj, root->lib);
1295 nldr_obj->ldr_fxns.close_fxn(root->lib);
1300 /* Check for circular dependencies. */
1301 for (i = 0; i < depth; i++) {
1302 if (root->lib == lib_path[i]) {
1303 /* This condition could be checked by a
1304 * tool at build time. */
1310 /* Add library to current path in dependency tree */
1311 lib_path[depth] = root->lib;
1313 /* Get number of dependent libraries */
1315 dcd_get_num_dep_libs(nldr_node_obj->nldr_obj->hdcd_mgr,
1316 &uuid, &nd_libs, &np_libs, phase);
1318 DBC_ASSERT(nd_libs >= np_libs);
1320 if (!(*nldr_node_obj->pf_phase_split))
1323 /* nd_libs = #of dependent libraries */
1324 root->dep_libs = nd_libs - np_libs;
1326 dep_lib_uui_ds = kzalloc(sizeof(struct dsp_uuid) *
1327 nd_libs, GFP_KERNEL);
1328 persistent_dep_libs =
1329 kzalloc(sizeof(bool) * nd_libs, GFP_KERNEL);
1330 if (!dep_lib_uui_ds || !persistent_dep_libs)
1333 if (root->dep_libs > 0) {
1334 /* Allocate arrays for dependent lib UUIDs,
1336 root->dep_libs_tree = kzalloc
1337 (sizeof(struct lib_node) *
1338 (root->dep_libs), GFP_KERNEL);
1339 if (!(root->dep_libs_tree))
1345 /* Get the dependent library UUIDs */
1347 dcd_get_dep_libs(nldr_node_obj->
1348 nldr_obj->hdcd_mgr, &uuid,
1349 nd_libs, dep_lib_uui_ds,
1350 persistent_dep_libs,
1357 * Recursively load dependent libraries.
1360 for (i = 0; i < nd_libs; i++) {
1361 /* If root library is NOT persistent, and dep library
1362 * is, then record it. If root library IS persistent,
1363 * the deplib is already included */
1364 if (!root_prstnt && persistent_dep_libs[i] &&
1365 *nldr_node_obj->pf_phase_split) {
1366 if ((nldr_node_obj->pers_libs) >= MAXLIBS) {
1371 /* Allocate library outside of phase */
1373 &nldr_node_obj->pers_lib_table
1374 [nldr_node_obj->pers_libs];
1377 persistent_dep_libs[i] = true;
1379 /* Allocate library within phase */
1380 dep_lib = &root->dep_libs_tree[nd_libs_loaded];
1383 status = load_lib(nldr_node_obj, dep_lib,
1385 persistent_dep_libs[i], lib_path,
1389 if ((status != 0) &&
1390 !root_prstnt && persistent_dep_libs[i] &&
1391 *nldr_node_obj->pf_phase_split) {
1392 (nldr_node_obj->pers_libs)++;
1394 if (!persistent_dep_libs[i] ||
1395 !(*nldr_node_obj->pf_phase_split)) {
1405 /* Now we can load the root library */
1407 new_attrs = nldr_obj->ldr_attrs;
1408 new_attrs.sym_arg = root;
1409 new_attrs.rmm_handle = nldr_node_obj;
1410 new_attrs.input_params = nldr_node_obj->priv_ref;
1411 new_attrs.base_image = false;
1414 nldr_obj->ldr_fxns.load_fxn(root->lib, flags, &new_attrs,
1419 * In case of failure, unload any dependent libraries that
1420 * were loaded, and close the root library.
1421 * (Persistent libraries are unloaded from the very top)
1424 if (phase != NLDR_EXECUTE) {
1425 for (i = 0; i < nldr_node_obj->pers_libs; i++)
1426 unload_lib(nldr_node_obj,
1427 &nldr_node_obj->pers_lib_table[i]);
1429 nldr_node_obj->pers_libs = 0;
1431 for (i = 0; i < nd_libs_loaded; i++)
1432 unload_lib(nldr_node_obj, &root->dep_libs_tree[i]);
1435 nldr_obj->ldr_fxns.close_fxn(root->lib);
1439 /* Going up one node in the dependency tree */
1442 kfree(dep_lib_uui_ds);
1443 dep_lib_uui_ds = NULL;
1445 kfree(persistent_dep_libs);
1446 persistent_dep_libs = NULL;
1452 * ======== load_ovly ========
1454 static int load_ovly(struct nldr_nodeobject *nldr_node_obj,
1455 enum nldr_phase phase)
1457 struct nldr_object *nldr_obj = nldr_node_obj->nldr_obj;
1458 struct ovly_node *po_node = NULL;
1459 struct ovly_sect *phase_sects = NULL;
1460 struct ovly_sect *other_sects_list = NULL;
1463 u16 other_alloc = 0;
1464 u16 *ref_count = NULL;
1465 u16 *other_ref = NULL;
1467 struct ovly_sect *ovly_section;
1470 /* Find the node in the table */
1471 for (i = 0; i < nldr_obj->ovly_nodes; i++) {
1473 (&nldr_node_obj->uuid, &nldr_obj->ovly_table[i].uuid)) {
1475 po_node = &(nldr_obj->ovly_table[i]);
1480 DBC_ASSERT(i < nldr_obj->ovly_nodes);
1489 ref_count = &(po_node->create_ref);
1490 other_ref = &(po_node->other_ref);
1491 phase_sects = po_node->create_sects_list;
1492 other_sects_list = po_node->other_sects_list;
1496 ref_count = &(po_node->execute_ref);
1497 phase_sects = po_node->execute_sects_list;
1501 ref_count = &(po_node->delete_ref);
1502 phase_sects = po_node->delete_sects_list;
1510 if (ref_count == NULL)
1513 if (*ref_count != 0)
1516 /* 'Allocate' memory for overlay sections of this phase */
1517 ovly_section = phase_sects;
1518 while (ovly_section) {
1519 /* allocate *//* page not supported yet */
1520 /* reserve *//* align */
1521 status = rmm_alloc(nldr_obj->rmm, 0, ovly_section->size, 0,
1522 &(ovly_section->sect_run_addr), true);
1524 ovly_section = ovly_section->next_sect;
1530 if (other_ref && *other_ref == 0) {
1531 /* 'Allocate' memory for other overlay sections
1534 ovly_section = other_sects_list;
1535 while (ovly_section) {
1536 /* page not supported *//* align */
1539 rmm_alloc(nldr_obj->rmm, 0,
1540 ovly_section->size, 0,
1541 &(ovly_section->sect_run_addr),
1544 ovly_section = ovly_section->next_sect;
1552 if (*ref_count == 0) {
1554 /* Load sections for this phase */
1555 ovly_section = phase_sects;
1556 while (ovly_section && !status) {
1558 (*nldr_obj->ovly_fxn) (nldr_node_obj->
1565 ovly_section->page);
1566 if (bytes != ovly_section->size)
1569 ovly_section = ovly_section->next_sect;
1573 if (other_ref && *other_ref == 0) {
1575 /* Load other sections (create phase) */
1576 ovly_section = other_sects_list;
1577 while (ovly_section && !status) {
1579 (*nldr_obj->ovly_fxn) (nldr_node_obj->
1586 ovly_section->page);
1587 if (bytes != ovly_section->size)
1590 ovly_section = ovly_section->next_sect;
1595 /* 'Deallocate' memory */
1596 free_sects(nldr_obj, phase_sects, alloc_num);
1597 free_sects(nldr_obj, other_sects_list, other_alloc);
1600 if (!status && (ref_count != NULL)) {
1611 * ======== remote_alloc ========
1613 static int remote_alloc(void **ref, u16 mem_sect, u32 size,
1614 u32 align, u32 *dsp_address,
1615 s32 segmnt_id, s32 req,
1618 struct nldr_nodeobject *hnode = (struct nldr_nodeobject *)ref;
1619 struct nldr_object *nldr_obj;
1620 struct rmm_target_obj *rmm;
1621 u16 mem_phase_bit = MAXFLAGS;
1626 struct rmm_addr *rmm_addr_obj = (struct rmm_addr *)dsp_address;
1627 bool mem_load_req = false;
1628 int status = -ENOMEM; /* Set to fail */
1630 DBC_REQUIRE(mem_sect == DBLL_CODE || mem_sect == DBLL_DATA ||
1631 mem_sect == DBLL_BSS);
1632 nldr_obj = hnode->nldr_obj;
1633 rmm = nldr_obj->rmm;
1634 /* Convert size to DSP words */
1636 (size + nldr_obj->us_dsp_word_size -
1637 1) / nldr_obj->us_dsp_word_size;
1638 /* Modify memory 'align' to account for DSP cache line size */
1639 align = lcm(GEM_CACHE_LINE_SIZE, align);
1640 dev_dbg(bridge, "%s: memory align to 0x%x\n", __func__, align);
1641 if (segmnt_id != -1) {
1642 rmm_addr_obj->segid = segmnt_id;
1646 switch (hnode->phase) {
1648 mem_phase_bit = CREATEDATAFLAGBIT;
1651 mem_phase_bit = DELETEDATAFLAGBIT;
1654 mem_phase_bit = EXECUTEDATAFLAGBIT;
1660 if (mem_sect == DBLL_CODE)
1663 if (mem_phase_bit < MAXFLAGS)
1664 segid = hnode->seg_id[mem_phase_bit];
1666 /* Determine if there is a memory loading requirement */
1667 if ((hnode->code_data_flag_mask >> mem_phase_bit) & 0x1)
1668 mem_load_req = true;
1671 mem_sect_type = (mem_sect == DBLL_CODE) ? DYNM_CODE : DYNM_DATA;
1673 /* Find an appropriate segment based on mem_sect */
1674 if (segid == NULLID) {
1675 /* No memory requirements of preferences */
1676 DBC_ASSERT(!mem_load_req);
1679 if (segid <= MAXSEGID) {
1680 DBC_ASSERT(segid < nldr_obj->dload_segs);
1681 /* Attempt to allocate from segid first. */
1682 rmm_addr_obj->segid = segid;
1684 rmm_alloc(rmm, segid, word_size, align, dsp_address, false);
1686 dev_dbg(bridge, "%s: Unable allocate from segment %d\n",
1690 /* segid > MAXSEGID ==> Internal or external memory */
1691 DBC_ASSERT(segid == MEMINTERNALID || segid == MEMEXTERNALID);
1692 /* Check for any internal or external memory segment,
1693 * depending on segid. */
1694 mem_sect_type |= segid == MEMINTERNALID ?
1695 DYNM_INTERNAL : DYNM_EXTERNAL;
1696 for (i = 0; i < nldr_obj->dload_segs; i++) {
1697 if ((nldr_obj->seg_table[i] & mem_sect_type) !=
1701 status = rmm_alloc(rmm, i, word_size, align,
1702 dsp_address, false);
1704 /* Save segid for freeing later */
1705 rmm_addr_obj->segid = i;
1711 /* Haven't found memory yet, attempt to find any segment that works */
1712 if (status == -ENOMEM && !mem_load_req) {
1713 dev_dbg(bridge, "%s: Preferred segment unavailable, trying "
1714 "another\n", __func__);
1715 for (i = 0; i < nldr_obj->dload_segs; i++) {
1716 /* All bits of mem_sect_type must be set */
1717 if ((nldr_obj->seg_table[i] & mem_sect_type) !=
1721 status = rmm_alloc(rmm, i, word_size, align,
1722 dsp_address, false);
1725 rmm_addr_obj->segid = i;
1734 static int remote_free(void **ref, u16 space, u32 dsp_address,
1735 u32 size, bool reserve)
1737 struct nldr_object *nldr_obj = (struct nldr_object *)ref;
1738 struct rmm_target_obj *rmm;
1740 int status = -ENOMEM; /* Set to fail */
1742 DBC_REQUIRE(nldr_obj);
1744 rmm = nldr_obj->rmm;
1746 /* Convert size to DSP words */
1748 (size + nldr_obj->us_dsp_word_size -
1749 1) / nldr_obj->us_dsp_word_size;
1751 if (rmm_free(rmm, space, dsp_address, word_size, reserve))
1758 * ======== unload_lib ========
1760 static void unload_lib(struct nldr_nodeobject *nldr_node_obj,
1761 struct lib_node *root)
1763 struct dbll_attrs new_attrs;
1764 struct nldr_object *nldr_obj = nldr_node_obj->nldr_obj;
1767 DBC_ASSERT(root != NULL);
1769 /* Unload dependent libraries */
1770 for (i = 0; i < root->dep_libs; i++)
1771 unload_lib(nldr_node_obj, &root->dep_libs_tree[i]);
1775 new_attrs = nldr_obj->ldr_attrs;
1776 new_attrs.rmm_handle = nldr_obj->rmm;
1777 new_attrs.input_params = nldr_node_obj->priv_ref;
1778 new_attrs.base_image = false;
1779 new_attrs.sym_arg = root;
1782 /* Unload the root library */
1783 nldr_obj->ldr_fxns.unload_fxn(root->lib, &new_attrs);
1784 nldr_obj->ldr_fxns.close_fxn(root->lib);
1787 /* Free dependent library list */
1788 kfree(root->dep_libs_tree);
1789 root->dep_libs_tree = NULL;
1793 * ======== unload_ovly ========
1795 static void unload_ovly(struct nldr_nodeobject *nldr_node_obj,
1796 enum nldr_phase phase)
1798 struct nldr_object *nldr_obj = nldr_node_obj->nldr_obj;
1799 struct ovly_node *po_node = NULL;
1800 struct ovly_sect *phase_sects = NULL;
1801 struct ovly_sect *other_sects_list = NULL;
1804 u16 other_alloc = 0;
1805 u16 *ref_count = NULL;
1806 u16 *other_ref = NULL;
1808 /* Find the node in the table */
1809 for (i = 0; i < nldr_obj->ovly_nodes; i++) {
1811 (&nldr_node_obj->uuid, &nldr_obj->ovly_table[i].uuid)) {
1813 po_node = &(nldr_obj->ovly_table[i]);
1818 DBC_ASSERT(i < nldr_obj->ovly_nodes);
1821 /* TODO: Should we print warning here? */
1826 ref_count = &(po_node->create_ref);
1827 phase_sects = po_node->create_sects_list;
1828 alloc_num = po_node->create_sects;
1831 ref_count = &(po_node->execute_ref);
1832 phase_sects = po_node->execute_sects_list;
1833 alloc_num = po_node->execute_sects;
1836 ref_count = &(po_node->delete_ref);
1837 other_ref = &(po_node->other_ref);
1838 phase_sects = po_node->delete_sects_list;
1839 /* 'Other' overlay sections are unloaded in the delete phase */
1840 other_sects_list = po_node->other_sects_list;
1841 alloc_num = po_node->delete_sects;
1842 other_alloc = po_node->other_sects;
1848 DBC_ASSERT(ref_count && (*ref_count > 0));
1849 if (ref_count && (*ref_count > 0)) {
1852 DBC_ASSERT(*other_ref > 0);
1857 if (ref_count && *ref_count == 0) {
1858 /* 'Deallocate' memory */
1859 free_sects(nldr_obj, phase_sects, alloc_num);
1861 if (other_ref && *other_ref == 0)
1862 free_sects(nldr_obj, other_sects_list, other_alloc);
1866 * ======== find_in_persistent_lib_array ========
1868 static bool find_in_persistent_lib_array(struct nldr_nodeobject *nldr_node_obj,
1869 struct dbll_library_obj *lib)
1873 for (i = 0; i < nldr_node_obj->pers_libs; i++) {
1874 if (lib == nldr_node_obj->pers_lib_table[i].lib)
1882 #ifdef CONFIG_TIDSPBRIDGE_BACKTRACE
1884 * nldr_find_addr() - Find the closest symbol to the given address based on
1885 * dynamic node object.
1887 * @nldr_node: Dynamic node object
1888 * @sym_addr: Given address to find the dsp symbol
1889 * @offset_range: offset range to look for dsp symbol
1890 * @offset_output: Symbol Output address
1891 * @sym_name: String with the dsp symbol
1893 * This function finds the node library for a given address and
1894 * retrieves the dsp symbol by calling dbll_find_dsp_symbol.
1896 int nldr_find_addr(struct nldr_nodeobject *nldr_node, u32 sym_addr,
1897 u32 offset_range, void *offset_output, char *sym_name)
1900 bool status1 = false;
1902 struct lib_node root = { NULL, 0, NULL };
1903 DBC_REQUIRE(refs > 0);
1904 DBC_REQUIRE(offset_output != NULL);
1905 DBC_REQUIRE(sym_name != NULL);
1906 pr_debug("%s(0x%x, 0x%x, 0x%x, 0x%x, %s)\n", __func__, (u32) nldr_node,
1907 sym_addr, offset_range, (u32) offset_output, sym_name);
1909 if (nldr_node->dynamic && *nldr_node->pf_phase_split) {
1910 switch (nldr_node->phase) {
1912 root = nldr_node->create_lib;
1915 root = nldr_node->execute_lib;
1918 root = nldr_node->delete_lib;
1925 /* for Overlay nodes or non-split Dynamic nodes */
1926 root = nldr_node->root;
1929 status1 = dbll_find_dsp_symbol(root.lib, sym_addr,
1930 offset_range, offset_output, sym_name);
1932 /* If symbol not found, check dependent libraries */
1934 for (i = 0; i < root.dep_libs; i++) {
1935 status1 = dbll_find_dsp_symbol(
1936 root.dep_libs_tree[i].lib, sym_addr,
1937 offset_range, offset_output, sym_name);
1942 /* Check persistent libraries */
1944 for (i = 0; i < nldr_node->pers_libs; i++) {
1945 status1 = dbll_find_dsp_symbol(
1946 nldr_node->pers_lib_table[i].lib, sym_addr,
1947 offset_range, offset_output, sym_name);
1954 pr_debug("%s: Address 0x%x not found in range %d.\n",
1955 __func__, sym_addr, offset_range);