]> Pileus Git - ~andy/linux/blob - drivers/staging/tidspbridge/rmgr/node.c
staging: ti dspbridge: Rename words with camel case
[~andy/linux] / drivers / staging / tidspbridge / rmgr / node.c
1 /*
2  * node.c
3  *
4  * DSP-BIOS Bridge driver support functions for TI OMAP processors.
5  *
6  * DSP/BIOS Bridge Node Manager.
7  *
8  * Copyright (C) 2005-2006 Texas Instruments, Inc.
9  *
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.
13  *
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.
17  */
18
19 /*  ----------------------------------- Host OS */
20 #include <dspbridge/host_os.h>
21
22 /*  ----------------------------------- DSP/BIOS Bridge */
23 #include <dspbridge/std.h>
24 #include <dspbridge/dbdefs.h>
25
26 /*  ----------------------------------- Trace & Debug */
27 #include <dspbridge/dbc.h>
28
29 /*  ----------------------------------- OS Adaptation Layer */
30 #include <dspbridge/cfg.h>
31 #include <dspbridge/list.h>
32 #include <dspbridge/memdefs.h>
33 #include <dspbridge/proc.h>
34 #include <dspbridge/strm.h>
35 #include <dspbridge/sync.h>
36 #include <dspbridge/ntfy.h>
37
38 /*  ----------------------------------- Platform Manager */
39 #include <dspbridge/cmm.h>
40 #include <dspbridge/cod.h>
41 #include <dspbridge/dev.h>
42 #include <dspbridge/msg.h>
43
44 /*  ----------------------------------- Resource Manager */
45 #include <dspbridge/dbdcd.h>
46 #include <dspbridge/disp.h>
47 #include <dspbridge/rms_sh.h>
48
49 /*  ----------------------------------- Link Driver */
50 #include <dspbridge/dspdefs.h>
51 #include <dspbridge/dspioctl.h>
52
53 /*  ----------------------------------- Others */
54 #include <dspbridge/gb.h>
55 #include <dspbridge/uuidutil.h>
56
57 /*  ----------------------------------- This */
58 #include <dspbridge/nodepriv.h>
59 #include <dspbridge/node.h>
60 #include <dspbridge/dmm.h>
61
62 /* Static/Dynamic Loader includes */
63 #include <dspbridge/dbll.h>
64 #include <dspbridge/nldr.h>
65
66 #include <dspbridge/drv.h>
67 #include <dspbridge/drvdefs.h>
68 #include <dspbridge/resourcecleanup.h>
69 #include <_tiomap.h>
70
71 #include <dspbridge/dspdeh.h>
72
73 #define HOSTPREFIX        "/host"
74 #define PIPEPREFIX        "/dbpipe"
75
76 #define MAX_INPUTS(h)  \
77                 ((h)->dcd_props.obj_data.node_obj.ndb_props.num_input_streams)
78 #define MAX_OUTPUTS(h) \
79                 ((h)->dcd_props.obj_data.node_obj.ndb_props.num_output_streams)
80
81 #define NODE_GET_PRIORITY(h) ((h)->prio)
82 #define NODE_SET_PRIORITY(hnode, prio) ((hnode)->prio = prio)
83 #define NODE_SET_STATE(hnode, state) ((hnode)->node_state = state)
84
85 #define MAXPIPES        100     /* Max # of /pipe connections (CSL limit) */
86 #define MAXDEVSUFFIXLEN 2       /* Max(Log base 10 of MAXPIPES, MAXSTREAMS) */
87
88 #define PIPENAMELEN     (sizeof(PIPEPREFIX) + MAXDEVSUFFIXLEN)
89 #define HOSTNAMELEN     (sizeof(HOSTPREFIX) + MAXDEVSUFFIXLEN)
90
91 #define MAXDEVNAMELEN   32      /* dsp_ndbprops.ac_name size */
92 #define CREATEPHASE     1
93 #define EXECUTEPHASE    2
94 #define DELETEPHASE     3
95
96 /* Define default STRM parameters */
97 /*
98  *  TBD: Put in header file, make global DSP_STRMATTRS with defaults,
99  *  or make defaults configurable.
100  */
101 #define DEFAULTBUFSIZE          32
102 #define DEFAULTNBUFS            2
103 #define DEFAULTSEGID            0
104 #define DEFAULTALIGNMENT        0
105 #define DEFAULTTIMEOUT          10000
106
107 #define RMSQUERYSERVER          0
108 #define RMSCONFIGURESERVER      1
109 #define RMSCREATENODE           2
110 #define RMSEXECUTENODE          3
111 #define RMSDELETENODE           4
112 #define RMSCHANGENODEPRIORITY   5
113 #define RMSREADMEMORY           6
114 #define RMSWRITEMEMORY          7
115 #define RMSCOPY                 8
116 #define MAXTIMEOUT              2000
117
118 #define NUMRMSFXNS              9
119
120 #define PWR_TIMEOUT             500     /* default PWR timeout in msec */
121
122 #define STACKSEGLABEL "L1DSRAM_HEAP"    /* Label for DSP Stack Segment Addr */
123
124 /*
125  *  ======== node_mgr ========
126  */
127 struct node_mgr {
128         struct dev_object *hdev_obj;    /* Device object */
129         /* Function interface to Bridge driver */
130         struct bridge_drv_interface *intf_fxns;
131         struct dcd_manager *hdcd_mgr;   /* Proc/Node data manager */
132         struct disp_object *disp_obj;   /* Node dispatcher */
133         struct lst_list *node_list;     /* List of all allocated nodes */
134         u32 num_nodes;          /* Number of nodes in node_list */
135         u32 num_created;        /* Number of nodes *created* on DSP */
136         struct gb_t_map *pipe_map;      /* Pipe connection bit map */
137         struct gb_t_map *pipe_done_map; /* Pipes that are half free */
138         struct gb_t_map *chnl_map;      /* Channel allocation bit map */
139         struct gb_t_map *dma_chnl_map;  /* DMA Channel allocation bit map */
140         struct gb_t_map *zc_chnl_map;   /* Zero-Copy Channel alloc bit map */
141         struct ntfy_object *ntfy_obj;   /* Manages registered notifications */
142         struct mutex node_mgr_lock;     /* For critical sections */
143         u32 ul_fxn_addrs[NUMRMSFXNS];   /* RMS function addresses */
144         struct msg_mgr *msg_mgr_obj;
145
146         /* Processor properties needed by Node Dispatcher */
147         u32 ul_num_chnls;       /* Total number of channels */
148         u32 ul_chnl_offset;     /* Offset of chnl ids rsvd for RMS */
149         u32 ul_chnl_buf_size;   /* Buffer size for data to RMS */
150         int proc_family;        /* eg, 5000 */
151         int proc_type;          /* eg, 5510 */
152         u32 udsp_word_size;     /* Size of DSP word on host bytes */
153         u32 udsp_data_mau_size; /* Size of DSP data MAU */
154         u32 udsp_mau_size;      /* Size of MAU */
155         s32 min_pri;            /* Minimum runtime priority for node */
156         s32 max_pri;            /* Maximum runtime priority for node */
157
158         struct strm_mgr *strm_mgr_obj;  /* STRM manager */
159
160         /* Loader properties */
161         struct nldr_object *nldr_obj;   /* Handle to loader */
162         struct node_ldr_fxns nldr_fxns; /* Handle to loader functions */
163         bool loader_init;       /* Loader Init function succeeded? */
164 };
165
166 /*
167  *  ======== connecttype ========
168  */
169 enum connecttype {
170         NOTCONNECTED = 0,
171         NODECONNECT,
172         HOSTCONNECT,
173         DEVICECONNECT,
174 };
175
176 /*
177  *  ======== stream_chnl ========
178  */
179 struct stream_chnl {
180         enum connecttype type;  /* Type of stream connection */
181         u32 dev_id;             /* pipe or channel id */
182 };
183
184 /*
185  *  ======== node_object ========
186  */
187 struct node_object {
188         struct list_head list_elem;
189         struct node_mgr *hnode_mgr;     /* The manager of this node */
190         struct proc_object *hprocessor; /* Back pointer to processor */
191         struct dsp_uuid node_uuid;      /* Node's ID */
192         s32 prio;               /* Node's current priority */
193         u32 utimeout;           /* Timeout for blocking NODE calls */
194         u32 heap_size;          /* Heap Size */
195         u32 udsp_heap_virt_addr;        /* Heap Size */
196         u32 ugpp_heap_virt_addr;        /* Heap Size */
197         enum node_type ntype;   /* Type of node: message, task, etc */
198         enum node_state node_state;     /* NODE_ALLOCATED, NODE_CREATED, ... */
199         u32 num_inputs;         /* Current number of inputs */
200         u32 num_outputs;        /* Current number of outputs */
201         u32 max_input_index;    /* Current max input stream index */
202         u32 max_output_index;   /* Current max output stream index */
203         struct stream_chnl *inputs;     /* Node's input streams */
204         struct stream_chnl *outputs;    /* Node's output streams */
205         struct node_createargs create_args;     /* Args for node create func */
206         nodeenv node_env;       /* Environment returned by RMS */
207         struct dcd_genericobj dcd_props;        /* Node properties from DCD */
208         struct dsp_cbdata *pargs;       /* Optional args to pass to node */
209         struct ntfy_object *ntfy_obj;   /* Manages registered notifications */
210         char *pstr_dev_name;    /* device name, if device node */
211         struct sync_object *sync_done;  /* Synchronize node_terminate */
212         s32 exit_status;        /* execute function return status */
213
214         /* Information needed for node_get_attr() */
215         void *device_owner;     /* If dev node, task that owns it */
216         u32 num_gpp_inputs;     /* Current # of from GPP streams */
217         u32 num_gpp_outputs;    /* Current # of to GPP streams */
218         /* Current stream connections */
219         struct dsp_streamconnect *stream_connect;
220
221         /* Message queue */
222         struct msg_queue *msg_queue_obj;
223
224         /* These fields used for SM messaging */
225         struct cmm_xlatorobject *xlator;        /* Node's SM addr translator */
226
227         /* Handle to pass to dynamic loader */
228         struct nldr_nodeobject *nldr_node_obj;
229         bool loaded;            /* Code is (dynamically) loaded */
230         bool phase_split;       /* Phases split in many libs or ovly */
231
232 };
233
234 /* Default buffer attributes */
235 static struct dsp_bufferattr node_dfltbufattrs = {
236         0,                      /* cb_struct */
237         1,                      /* segment_id */
238         0,                      /* buf_alignment */
239 };
240
241 static void delete_node(struct node_object *hnode,
242                         struct process_context *pr_ctxt);
243 static void delete_node_mgr(struct node_mgr *hnode_mgr);
244 static void fill_stream_connect(struct node_object *node1,
245                                 struct node_object *node2, u32 uStream1,
246                                 u32 uStream2);
247 static void fill_stream_def(struct node_object *hnode,
248                             struct node_strmdef *pstrm_def,
249                             struct dsp_strmattr *pattrs);
250 static void free_stream(struct node_mgr *hnode_mgr, struct stream_chnl stream);
251 static int get_fxn_address(struct node_object *hnode, u32 * pulFxnAddr,
252                                   u32 uPhase);
253 static int get_node_props(struct dcd_manager *hdcd_mgr,
254                                  struct node_object *hnode,
255                                  CONST struct dsp_uuid *node_uuid,
256                                  struct dcd_genericobj *dcd_prop);
257 static int get_proc_props(struct node_mgr *hnode_mgr,
258                                  struct dev_object *hdev_obj);
259 static int get_rms_fxns(struct node_mgr *hnode_mgr);
260 static u32 ovly(void *priv_ref, u32 ulDspRunAddr, u32 ulDspLoadAddr,
261                 u32 ul_num_bytes, u32 mem_space);
262 static u32 mem_write(void *priv_ref, u32 ulDspAddr, void *pbuf,
263                      u32 ul_num_bytes, u32 mem_space);
264
265 static u32 refs;                /* module reference count */
266
267 /* Dynamic loader functions. */
268 static struct node_ldr_fxns nldr_fxns = {
269         nldr_allocate,
270         nldr_create,
271         nldr_delete,
272         nldr_exit,
273         nldr_get_fxn_addr,
274         nldr_init,
275         nldr_load,
276         nldr_unload,
277 };
278
279 enum node_state node_get_state(void *hnode)
280 {
281         struct node_object *pnode = (struct node_object *)hnode;
282         if (!pnode)
283                 return -1;
284         else
285                 return pnode->node_state;
286 }
287
288 /*
289  *  ======== node_allocate ========
290  *  Purpose:
291  *      Allocate GPP resources to manage a node on the DSP.
292  */
293 int node_allocate(struct proc_object *hprocessor,
294                          IN CONST struct dsp_uuid *node_uuid,
295                          OPTIONAL IN CONST struct dsp_cbdata *pargs,
296                          OPTIONAL IN CONST struct dsp_nodeattrin *attr_in,
297                          OUT struct node_object **ph_node,
298                          struct process_context *pr_ctxt)
299 {
300         struct node_mgr *hnode_mgr;
301         struct dev_object *hdev_obj;
302         struct node_object *pnode = NULL;
303         enum node_type node_type = NODE_TASK;
304         struct node_msgargs *pmsg_args;
305         struct node_taskargs *ptask_args;
306         u32 num_streams;
307         struct bridge_drv_interface *intf_fxns;
308         int status = 0;
309         struct cmm_object *hcmm_mgr = NULL;     /* Shared memory manager hndl */
310         u32 proc_id;
311         u32 pul_value;
312         u32 dynext_base;
313         u32 off_set = 0;
314         u32 ul_stack_seg_addr, ul_stack_seg_val;
315         u32 ul_gpp_mem_base;
316         struct cfg_hostres *host_res;
317         struct bridge_dev_context *pbridge_context;
318         u32 mapped_addr = 0;
319         u32 map_attrs = 0x0;
320         struct dsp_processorstate proc_state;
321 #ifdef DSP_DMM_DEBUG
322         struct dmm_object *dmm_mgr;
323         struct proc_object *p_proc_object = (struct proc_object *)hprocessor;
324 #endif
325
326         void *node_res;
327
328         DBC_REQUIRE(refs > 0);
329         DBC_REQUIRE(hprocessor != NULL);
330         DBC_REQUIRE(ph_node != NULL);
331         DBC_REQUIRE(node_uuid != NULL);
332
333         *ph_node = NULL;
334
335         status = proc_get_processor_id(hprocessor, &proc_id);
336
337         if (proc_id != DSP_UNIT)
338                 goto func_end;
339
340         status = proc_get_dev_object(hprocessor, &hdev_obj);
341         if (DSP_SUCCEEDED(status)) {
342                 status = dev_get_node_manager(hdev_obj, &hnode_mgr);
343                 if (hnode_mgr == NULL)
344                         status = -EPERM;
345
346         }
347
348         if (DSP_FAILED(status))
349                 goto func_end;
350
351         status = dev_get_bridge_context(hdev_obj, &pbridge_context);
352         if (!pbridge_context) {
353                 status = -EFAULT;
354                 goto func_end;
355         }
356
357         status = proc_get_state(hprocessor, &proc_state,
358                                 sizeof(struct dsp_processorstate));
359         if (DSP_FAILED(status))
360                 goto func_end;
361         /* If processor is in error state then don't attempt
362            to send the message */
363         if (proc_state.proc_state == PROC_ERROR) {
364                 status = -EPERM;
365                 goto func_end;
366         }
367
368         /* Assuming that 0 is not a valid function address */
369         if (hnode_mgr->ul_fxn_addrs[0] == 0) {
370                 /* No RMS on target - we currently can't handle this */
371                 pr_err("%s: Failed, no RMS in base image\n", __func__);
372                 status = -EPERM;
373         } else {
374                 /* Validate attr_in fields, if non-NULL */
375                 if (attr_in) {
376                         /* Check if attr_in->prio is within range */
377                         if (attr_in->prio < hnode_mgr->min_pri ||
378                             attr_in->prio > hnode_mgr->max_pri)
379                                 status = -EDOM;
380                 }
381         }
382         /* Allocate node object and fill in */
383         if (DSP_FAILED(status))
384                 goto func_end;
385
386         pnode = kzalloc(sizeof(struct node_object), GFP_KERNEL);
387         if (pnode == NULL) {
388                 status = -ENOMEM;
389                 goto func_end;
390         }
391         pnode->hnode_mgr = hnode_mgr;
392         /* This critical section protects get_node_props */
393         mutex_lock(&hnode_mgr->node_mgr_lock);
394
395         /* Get dsp_ndbprops from node database */
396         status = get_node_props(hnode_mgr->hdcd_mgr, pnode, node_uuid,
397                                 &(pnode->dcd_props));
398         if (DSP_FAILED(status))
399                 goto func_cont;
400
401         pnode->node_uuid = *node_uuid;
402         pnode->hprocessor = hprocessor;
403         pnode->ntype = pnode->dcd_props.obj_data.node_obj.ndb_props.ntype;
404         pnode->utimeout = pnode->dcd_props.obj_data.node_obj.ndb_props.utimeout;
405         pnode->prio = pnode->dcd_props.obj_data.node_obj.ndb_props.prio;
406
407         /* Currently only C64 DSP builds support Node Dynamic * heaps */
408         /* Allocate memory for node heap */
409         pnode->create_args.asa.task_arg_obj.heap_size = 0;
410         pnode->create_args.asa.task_arg_obj.udsp_heap_addr = 0;
411         pnode->create_args.asa.task_arg_obj.udsp_heap_res_addr = 0;
412         pnode->create_args.asa.task_arg_obj.ugpp_heap_addr = 0;
413         if (!attr_in)
414                 goto func_cont;
415
416         /* Check if we have a user allocated node heap */
417         if (!(attr_in->pgpp_virt_addr))
418                 goto func_cont;
419
420         /* check for page aligned Heap size */
421         if (((attr_in->heap_size) & (PG_SIZE4K - 1))) {
422                 pr_err("%s: node heap size not aligned to 4K, size = 0x%x \n",
423                        __func__, attr_in->heap_size);
424                 status = -EINVAL;
425         } else {
426                 pnode->create_args.asa.task_arg_obj.heap_size =
427                     attr_in->heap_size;
428                 pnode->create_args.asa.task_arg_obj.ugpp_heap_addr =
429                     (u32) attr_in->pgpp_virt_addr;
430         }
431         if (DSP_FAILED(status))
432                 goto func_cont;
433
434         status = proc_reserve_memory(hprocessor,
435                                      pnode->create_args.asa.task_arg_obj.
436                                      heap_size + PAGE_SIZE,
437                                      (void **)&(pnode->create_args.asa.
438                                         task_arg_obj.udsp_heap_res_addr),
439                                      pr_ctxt);
440         if (DSP_FAILED(status)) {
441                 pr_err("%s: Failed to reserve memory for heap: 0x%x\n",
442                        __func__, status);
443                 goto func_cont;
444         }
445 #ifdef DSP_DMM_DEBUG
446         status = dmm_get_handle(p_proc_object, &dmm_mgr);
447         if (!dmm_mgr) {
448                 status = DSP_EHANDLE;
449                 goto func_cont;
450         }
451
452         dmm_mem_map_dump(dmm_mgr);
453 #endif
454
455         map_attrs |= DSP_MAPLITTLEENDIAN;
456         map_attrs |= DSP_MAPELEMSIZE32;
457         map_attrs |= DSP_MAPVIRTUALADDR;
458         status = proc_map(hprocessor, (void *)attr_in->pgpp_virt_addr,
459                           pnode->create_args.asa.task_arg_obj.heap_size,
460                           (void *)pnode->create_args.asa.task_arg_obj.
461                           udsp_heap_res_addr, (void **)&mapped_addr, map_attrs,
462                           pr_ctxt);
463         if (DSP_FAILED(status))
464                 pr_err("%s: Failed to map memory for Heap: 0x%x\n",
465                        __func__, status);
466         else
467                 pnode->create_args.asa.task_arg_obj.udsp_heap_addr =
468                     (u32) mapped_addr;
469
470 func_cont:
471         mutex_unlock(&hnode_mgr->node_mgr_lock);
472         if (attr_in != NULL) {
473                 /* Overrides of NBD properties */
474                 pnode->utimeout = attr_in->utimeout;
475                 pnode->prio = attr_in->prio;
476         }
477         /* Create object to manage notifications */
478         if (DSP_SUCCEEDED(status)) {
479                 pnode->ntfy_obj = kmalloc(sizeof(struct ntfy_object),
480                                                         GFP_KERNEL);
481                 if (pnode->ntfy_obj)
482                         ntfy_init(pnode->ntfy_obj);
483                 else
484                         status = -ENOMEM;
485         }
486
487         if (DSP_SUCCEEDED(status)) {
488                 node_type = node_get_type(pnode);
489                 /*  Allocate dsp_streamconnect array for device, task, and
490                  *  dais socket nodes. */
491                 if (node_type != NODE_MESSAGE) {
492                         num_streams = MAX_INPUTS(pnode) + MAX_OUTPUTS(pnode);
493                         pnode->stream_connect = kzalloc(num_streams *
494                                         sizeof(struct dsp_streamconnect),
495                                         GFP_KERNEL);
496                         if (num_streams > 0 && pnode->stream_connect == NULL)
497                                 status = -ENOMEM;
498
499                 }
500                 if (DSP_SUCCEEDED(status) && (node_type == NODE_TASK ||
501                                               node_type == NODE_DAISSOCKET)) {
502                         /* Allocate arrays for maintainig stream connections */
503                         pnode->inputs = kzalloc(MAX_INPUTS(pnode) *
504                                         sizeof(struct stream_chnl), GFP_KERNEL);
505                         pnode->outputs = kzalloc(MAX_OUTPUTS(pnode) *
506                                         sizeof(struct stream_chnl), GFP_KERNEL);
507                         ptask_args = &(pnode->create_args.asa.task_arg_obj);
508                         ptask_args->strm_in_def = kzalloc(MAX_INPUTS(pnode) *
509                                                 sizeof(struct node_strmdef),
510                                                 GFP_KERNEL);
511                         ptask_args->strm_out_def = kzalloc(MAX_OUTPUTS(pnode) *
512                                                 sizeof(struct node_strmdef),
513                                                 GFP_KERNEL);
514                         if ((MAX_INPUTS(pnode) > 0 && (pnode->inputs == NULL ||
515                                                        ptask_args->strm_in_def
516                                                        == NULL))
517                             || (MAX_OUTPUTS(pnode) > 0
518                                 && (pnode->outputs == NULL
519                                     || ptask_args->strm_out_def == NULL)))
520                                 status = -ENOMEM;
521                 }
522         }
523         if (DSP_SUCCEEDED(status) && (node_type != NODE_DEVICE)) {
524                 /* Create an event that will be posted when RMS_EXIT is
525                  * received. */
526                 pnode->sync_done = kzalloc(sizeof(struct sync_object),
527                                                                 GFP_KERNEL);
528                 if (pnode->sync_done)
529                         sync_init_event(pnode->sync_done);
530                 else
531                         status = -ENOMEM;
532
533                 if (DSP_SUCCEEDED(status)) {
534                         /*Get the shared mem mgr for this nodes dev object */
535                         status = cmm_get_handle(hprocessor, &hcmm_mgr);
536                         if (DSP_SUCCEEDED(status)) {
537                                 /* Allocate a SM addr translator for this node
538                                  * w/ deflt attr */
539                                 status = cmm_xlator_create(&pnode->xlator,
540                                                            hcmm_mgr, NULL);
541                         }
542                 }
543                 if (DSP_SUCCEEDED(status)) {
544                         /* Fill in message args */
545                         if ((pargs != NULL) && (pargs->cb_data > 0)) {
546                                 pmsg_args =
547                                     &(pnode->create_args.asa.node_msg_args);
548                                 pmsg_args->pdata = kzalloc(pargs->cb_data,
549                                                                 GFP_KERNEL);
550                                 if (pmsg_args->pdata == NULL) {
551                                         status = -ENOMEM;
552                                 } else {
553                                         pmsg_args->arg_length = pargs->cb_data;
554                                         memcpy(pmsg_args->pdata,
555                                                pargs->node_data,
556                                                pargs->cb_data);
557                                 }
558                         }
559                 }
560         }
561
562         if (DSP_SUCCEEDED(status) && node_type != NODE_DEVICE) {
563                 /* Create a message queue for this node */
564                 intf_fxns = hnode_mgr->intf_fxns;
565                 status =
566                     (*intf_fxns->pfn_msg_create_queue) (hnode_mgr->msg_mgr_obj,
567                                                         &pnode->msg_queue_obj,
568                                                         0,
569                                                         pnode->create_args.asa.
570                                                         node_msg_args.max_msgs,
571                                                         pnode);
572         }
573
574         if (DSP_SUCCEEDED(status)) {
575                 /* Create object for dynamic loading */
576
577                 status = hnode_mgr->nldr_fxns.pfn_allocate(hnode_mgr->nldr_obj,
578                                                            (void *)pnode,
579                                                            &pnode->dcd_props.
580                                                            obj_data.node_obj,
581                                                            &pnode->
582                                                            nldr_node_obj,
583                                                            &pnode->phase_split);
584         }
585
586         /* Compare value read from Node Properties and check if it is same as
587          * STACKSEGLABEL, if yes read the Address of STACKSEGLABEL, calculate
588          * GPP Address, Read the value in that address and override the
589          * stack_seg value in task args */
590         if (DSP_SUCCEEDED(status) &&
591             (char *)pnode->dcd_props.obj_data.node_obj.ndb_props.
592             stack_seg_name != NULL) {
593                 if (strcmp((char *)
594                            pnode->dcd_props.obj_data.node_obj.ndb_props.
595                            stack_seg_name, STACKSEGLABEL) == 0) {
596                         status =
597                             hnode_mgr->nldr_fxns.
598                             pfn_get_fxn_addr(pnode->nldr_node_obj, "DYNEXT_BEG",
599                                              &dynext_base);
600                         if (DSP_FAILED(status))
601                                 pr_err("%s: Failed to get addr for DYNEXT_BEG"
602                                        " status = 0x%x\n", __func__, status);
603
604                         status =
605                             hnode_mgr->nldr_fxns.
606                             pfn_get_fxn_addr(pnode->nldr_node_obj,
607                                              "L1DSRAM_HEAP", &pul_value);
608
609                         if (DSP_FAILED(status))
610                                 pr_err("%s: Failed to get addr for L1DSRAM_HEAP"
611                                        " status = 0x%x\n", __func__, status);
612
613                         host_res = pbridge_context->resources;
614                         if (!host_res)
615                                 status = -EPERM;
616
617                         if (DSP_FAILED(status)) {
618                                 pr_err("%s: Failed to get host resource, status"
619                                        " = 0x%x\n", __func__, status);
620                                 goto func_end;
621                         }
622
623                         ul_gpp_mem_base = (u32) host_res->dw_mem_base[1];
624                         off_set = pul_value - dynext_base;
625                         ul_stack_seg_addr = ul_gpp_mem_base + off_set;
626                         ul_stack_seg_val = (u32) *((reg_uword32 *)
627                                                     ((u32)
628                                                      (ul_stack_seg_addr)));
629
630                         dev_dbg(bridge, "%s: StackSegVal = 0x%x, StackSegAddr ="
631                                 " 0x%x\n", __func__, ul_stack_seg_val,
632                                 ul_stack_seg_addr);
633
634                         pnode->create_args.asa.task_arg_obj.stack_seg =
635                             ul_stack_seg_val;
636
637                 }
638         }
639
640         if (DSP_SUCCEEDED(status)) {
641                 /* Add the node to the node manager's list of allocated
642                  * nodes. */
643                 lst_init_elem((struct list_head *)pnode);
644                 NODE_SET_STATE(pnode, NODE_ALLOCATED);
645
646                 mutex_lock(&hnode_mgr->node_mgr_lock);
647
648                 lst_put_tail(hnode_mgr->node_list, (struct list_head *) pnode);
649                         ++(hnode_mgr->num_nodes);
650
651                 /* Exit critical section */
652                 mutex_unlock(&hnode_mgr->node_mgr_lock);
653
654                 /* Preset this to assume phases are split
655                  * (for overlay and dll) */
656                 pnode->phase_split = true;
657
658                 if (DSP_SUCCEEDED(status))
659                         *ph_node = pnode;
660
661                 /* Notify all clients registered for DSP_NODESTATECHANGE. */
662                 proc_notify_all_clients(hprocessor, DSP_NODESTATECHANGE);
663         } else {
664                 /* Cleanup */
665                 if (pnode)
666                         delete_node(pnode, pr_ctxt);
667
668         }
669
670         if (DSP_SUCCEEDED(status)) {
671                 drv_insert_node_res_element(*ph_node, &node_res, pr_ctxt);
672                 drv_proc_node_update_heap_status(node_res, true);
673                 drv_proc_node_update_status(node_res, true);
674         }
675         DBC_ENSURE((DSP_FAILED(status) && (*ph_node == NULL)) ||
676                         (DSP_SUCCEEDED(status) && *ph_node));
677 func_end:
678         dev_dbg(bridge, "%s: hprocessor: %p node_uuid: %p pargs: %p attr_in:"
679                 " %p ph_node: %p status: 0x%x\n", __func__, hprocessor,
680                 node_uuid, pargs, attr_in, ph_node, status);
681         return status;
682 }
683
684 /*
685  *  ======== node_alloc_msg_buf ========
686  *  Purpose:
687  *      Allocates buffer for zero copy messaging.
688  */
689 DBAPI node_alloc_msg_buf(struct node_object *hnode, u32 usize,
690                          OPTIONAL IN OUT struct dsp_bufferattr *pattr,
691                          OUT u8 **pbuffer)
692 {
693         struct node_object *pnode = (struct node_object *)hnode;
694         int status = 0;
695         bool va_flag = false;
696         bool set_info;
697         u32 proc_id;
698
699         DBC_REQUIRE(refs > 0);
700         DBC_REQUIRE(pbuffer != NULL);
701
702         DBC_REQUIRE(usize > 0);
703
704         if (!pnode)
705                 status = -EFAULT;
706         else if (node_get_type(pnode) == NODE_DEVICE)
707                 status = -EPERM;
708
709         if (DSP_FAILED(status))
710                 goto func_end;
711
712         if (pattr == NULL)
713                 pattr = &node_dfltbufattrs;     /* set defaults */
714
715         status = proc_get_processor_id(pnode->hprocessor, &proc_id);
716         if (proc_id != DSP_UNIT) {
717                 DBC_ASSERT(NULL);
718                 goto func_end;
719         }
720         /*  If segment ID includes MEM_SETVIRTUALSEGID then pbuffer is a
721          *  virt  address, so set this info in this node's translator
722          *  object for  future ref. If MEM_GETVIRTUALSEGID then retrieve
723          *  virtual address  from node's translator. */
724         if ((pattr->segment_id & MEM_SETVIRTUALSEGID) ||
725             (pattr->segment_id & MEM_GETVIRTUALSEGID)) {
726                 va_flag = true;
727                 set_info = (pattr->segment_id & MEM_SETVIRTUALSEGID) ?
728                     true : false;
729                 /* Clear mask bits */
730                 pattr->segment_id &= ~MEM_MASKVIRTUALSEGID;
731                 /* Set/get this node's translators virtual address base/size */
732                 status = cmm_xlator_info(pnode->xlator, pbuffer, usize,
733                                          pattr->segment_id, set_info);
734         }
735         if (DSP_SUCCEEDED(status) && (!va_flag)) {
736                 if (pattr->segment_id != 1) {
737                         /* Node supports single SM segment only. */
738                         status = -EBADR;
739                 }
740                 /*  Arbitrary SM buffer alignment not supported for host side
741                  *  allocs, but guaranteed for the following alignment
742                  *  values. */
743                 switch (pattr->buf_alignment) {
744                 case 0:
745                 case 1:
746                 case 2:
747                 case 4:
748                         break;
749                 default:
750                         /* alignment value not suportted */
751                         status = -EPERM;
752                         break;
753                 }
754                 if (DSP_SUCCEEDED(status)) {
755                         /* allocate physical buffer from seg_id in node's
756                          * translator */
757                         (void)cmm_xlator_alloc_buf(pnode->xlator, pbuffer,
758                                                    usize);
759                         if (*pbuffer == NULL) {
760                                 pr_err("%s: error - Out of shared memory\n",
761                                        __func__);
762                                 status = -ENOMEM;
763                         }
764                 }
765         }
766 func_end:
767         return status;
768 }
769
770 /*
771  *  ======== node_change_priority ========
772  *  Purpose:
773  *      Change the priority of a node in the allocated state, or that is
774  *      currently running or paused on the target.
775  */
776 int node_change_priority(struct node_object *hnode, s32 prio)
777 {
778         struct node_object *pnode = (struct node_object *)hnode;
779         struct node_mgr *hnode_mgr = NULL;
780         enum node_type node_type;
781         enum node_state state;
782         int status = 0;
783         u32 proc_id;
784
785         DBC_REQUIRE(refs > 0);
786
787         if (!hnode || !hnode->hnode_mgr) {
788                 status = -EFAULT;
789         } else {
790                 hnode_mgr = hnode->hnode_mgr;
791                 node_type = node_get_type(hnode);
792                 if (node_type != NODE_TASK && node_type != NODE_DAISSOCKET)
793                         status = -EPERM;
794                 else if (prio < hnode_mgr->min_pri || prio > hnode_mgr->max_pri)
795                         status = -EDOM;
796         }
797         if (DSP_FAILED(status))
798                 goto func_end;
799
800         /* Enter critical section */
801         mutex_lock(&hnode_mgr->node_mgr_lock);
802
803         state = node_get_state(hnode);
804         if (state == NODE_ALLOCATED || state == NODE_PAUSED) {
805                 NODE_SET_PRIORITY(hnode, prio);
806         } else {
807                 if (state != NODE_RUNNING) {
808                         status = -EBADR;
809                         goto func_cont;
810                 }
811                 status = proc_get_processor_id(pnode->hprocessor, &proc_id);
812                 if (proc_id == DSP_UNIT) {
813                         status =
814                             disp_node_change_priority(hnode_mgr->disp_obj,
815                                                       hnode,
816                                                       hnode_mgr->ul_fxn_addrs
817                                                       [RMSCHANGENODEPRIORITY],
818                                                       hnode->node_env, prio);
819                 }
820                 if (DSP_SUCCEEDED(status))
821                         NODE_SET_PRIORITY(hnode, prio);
822
823         }
824 func_cont:
825         /* Leave critical section */
826         mutex_unlock(&hnode_mgr->node_mgr_lock);
827 func_end:
828         return status;
829 }
830
831 /*
832  *  ======== node_connect ========
833  *  Purpose:
834  *      Connect two nodes on the DSP, or a node on the DSP to the GPP.
835  */
836 int node_connect(struct node_object *node1, u32 uStream1,
837                         struct node_object *node2,
838                         u32 uStream2, OPTIONAL IN struct dsp_strmattr *pattrs,
839                         OPTIONAL IN struct dsp_cbdata *conn_param)
840 {
841         struct node_mgr *hnode_mgr;
842         char *pstr_dev_name = NULL;
843         enum node_type node1_type = NODE_TASK;
844         enum node_type node2_type = NODE_TASK;
845         struct node_strmdef *pstrm_def;
846         struct node_strmdef *input = NULL;
847         struct node_strmdef *output = NULL;
848         struct node_object *dev_node_obj;
849         struct node_object *hnode;
850         struct stream_chnl *pstream;
851         u32 pipe_id = GB_NOBITS;
852         u32 chnl_id = GB_NOBITS;
853         s8 chnl_mode;
854         u32 dw_length;
855         int status = 0;
856         DBC_REQUIRE(refs > 0);
857
858         if ((node1 != (struct node_object *)DSP_HGPPNODE && !node1) ||
859             (node2 != (struct node_object *)DSP_HGPPNODE && !node2))
860                 status = -EFAULT;
861
862         if (DSP_SUCCEEDED(status)) {
863                 /* The two nodes must be on the same processor */
864                 if (node1 != (struct node_object *)DSP_HGPPNODE &&
865                     node2 != (struct node_object *)DSP_HGPPNODE &&
866                     node1->hnode_mgr != node2->hnode_mgr)
867                         status = -EPERM;
868                 /* Cannot connect a node to itself */
869                 if (node1 == node2)
870                         status = -EPERM;
871
872         }
873         if (DSP_SUCCEEDED(status)) {
874                 /* node_get_type() will return NODE_GPP if hnode =
875                  * DSP_HGPPNODE. */
876                 node1_type = node_get_type(node1);
877                 node2_type = node_get_type(node2);
878                 /* Check stream indices ranges */
879                 if ((node1_type != NODE_GPP && node1_type != NODE_DEVICE &&
880                      uStream1 >= MAX_OUTPUTS(node1)) || (node2_type != NODE_GPP
881                                                           && node2_type !=
882                                                           NODE_DEVICE
883                                                           && uStream2 >=
884                                                           MAX_INPUTS(node2)))
885                         status = -EINVAL;
886         }
887         if (DSP_SUCCEEDED(status)) {
888                 /*
889                  *  Only the following types of connections are allowed:
890                  *      task/dais socket < == > task/dais socket
891                  *      task/dais socket < == > device
892                  *      task/dais socket < == > GPP
893                  *
894                  *  ie, no message nodes, and at least one task or dais
895                  *  socket node.
896                  */
897                 if (node1_type == NODE_MESSAGE || node2_type == NODE_MESSAGE ||
898                     (node1_type != NODE_TASK && node1_type != NODE_DAISSOCKET &&
899                      node2_type != NODE_TASK && node2_type != NODE_DAISSOCKET))
900                         status = -EPERM;
901         }
902         /*
903          * Check stream mode. Default is STRMMODE_PROCCOPY.
904          */
905         if (DSP_SUCCEEDED(status) && pattrs) {
906                 if (pattrs->strm_mode != STRMMODE_PROCCOPY)
907                         status = -EPERM;        /* illegal stream mode */
908
909         }
910         if (DSP_FAILED(status))
911                 goto func_end;
912
913         if (node1_type != NODE_GPP) {
914                 hnode_mgr = node1->hnode_mgr;
915         } else {
916                 DBC_ASSERT(node2 != (struct node_object *)DSP_HGPPNODE);
917                 hnode_mgr = node2->hnode_mgr;
918         }
919         /* Enter critical section */
920         mutex_lock(&hnode_mgr->node_mgr_lock);
921
922         /* Nodes must be in the allocated state */
923         if (node1_type != NODE_GPP && node_get_state(node1) != NODE_ALLOCATED)
924                 status = -EBADR;
925
926         if (node2_type != NODE_GPP && node_get_state(node2) != NODE_ALLOCATED)
927                 status = -EBADR;
928
929         if (DSP_SUCCEEDED(status)) {
930                 /*  Check that stream indices for task and dais socket nodes
931                  *  are not already be used. (Device nodes checked later) */
932                 if (node1_type == NODE_TASK || node1_type == NODE_DAISSOCKET) {
933                         output =
934                             &(node1->create_args.asa.
935                               task_arg_obj.strm_out_def[uStream1]);
936                         if (output->sz_device != NULL)
937                                 status = -EISCONN;
938
939                 }
940                 if (node2_type == NODE_TASK || node2_type == NODE_DAISSOCKET) {
941                         input =
942                             &(node2->create_args.asa.
943                               task_arg_obj.strm_in_def[uStream2]);
944                         if (input->sz_device != NULL)
945                                 status = -EISCONN;
946
947                 }
948         }
949         /* Connecting two task nodes? */
950         if (DSP_SUCCEEDED(status) && ((node1_type == NODE_TASK ||
951                                        node1_type == NODE_DAISSOCKET)
952                                       && (node2_type == NODE_TASK
953                                           || node2_type == NODE_DAISSOCKET))) {
954                 /* Find available pipe */
955                 pipe_id = gb_findandset(hnode_mgr->pipe_map);
956                 if (pipe_id == GB_NOBITS) {
957                         status = -ECONNREFUSED;
958                 } else {
959                         node1->outputs[uStream1].type = NODECONNECT;
960                         node2->inputs[uStream2].type = NODECONNECT;
961                         node1->outputs[uStream1].dev_id = pipe_id;
962                         node2->inputs[uStream2].dev_id = pipe_id;
963                         output->sz_device = kzalloc(PIPENAMELEN + 1,
964                                                         GFP_KERNEL);
965                         input->sz_device = kzalloc(PIPENAMELEN + 1, GFP_KERNEL);
966                         if (output->sz_device == NULL ||
967                             input->sz_device == NULL) {
968                                 /* Undo the connection */
969                                 kfree(output->sz_device);
970
971                                 kfree(input->sz_device);
972
973                                 output->sz_device = NULL;
974                                 input->sz_device = NULL;
975                                 gb_clear(hnode_mgr->pipe_map, pipe_id);
976                                 status = -ENOMEM;
977                         } else {
978                                 /* Copy "/dbpipe<pipId>" name to device names */
979                                 sprintf(output->sz_device, "%s%d",
980                                         PIPEPREFIX, pipe_id);
981                                 strcpy(input->sz_device, output->sz_device);
982                         }
983                 }
984         }
985         /* Connecting task node to host? */
986         if (DSP_SUCCEEDED(status) && (node1_type == NODE_GPP ||
987                                       node2_type == NODE_GPP)) {
988                 if (node1_type == NODE_GPP) {
989                         chnl_mode = CHNL_MODETODSP;
990                 } else {
991                         DBC_ASSERT(node2_type == NODE_GPP);
992                         chnl_mode = CHNL_MODEFROMDSP;
993                 }
994                 /*  Reserve a channel id. We need to put the name "/host<id>"
995                  *  in the node's create_args, but the host
996                  *  side channel will not be opened until DSPStream_Open is
997                  *  called for this node. */
998                 if (pattrs) {
999                         if (pattrs->strm_mode == STRMMODE_RDMA) {
1000                                 chnl_id =
1001                                     gb_findandset(hnode_mgr->dma_chnl_map);
1002                                 /* dma chans are 2nd transport chnl set
1003                                  * ids(e.g. 16-31) */
1004                                 (chnl_id != GB_NOBITS) ?
1005                                     (chnl_id =
1006                                      chnl_id +
1007                                      hnode_mgr->ul_num_chnls) : chnl_id;
1008                         } else if (pattrs->strm_mode == STRMMODE_ZEROCOPY) {
1009                                 chnl_id = gb_findandset(hnode_mgr->zc_chnl_map);
1010                                 /* zero-copy chans are 3nd transport set
1011                                  * (e.g. 32-47) */
1012                                 (chnl_id != GB_NOBITS) ? (chnl_id = chnl_id +
1013                                                           (2 *
1014                                                            hnode_mgr->
1015                                                            ul_num_chnls))
1016                                     : chnl_id;
1017                         } else {        /* must be PROCCOPY */
1018                                 DBC_ASSERT(pattrs->strm_mode ==
1019                                            STRMMODE_PROCCOPY);
1020                                 chnl_id = gb_findandset(hnode_mgr->chnl_map);
1021                                 /* e.g. 0-15 */
1022                         }
1023                 } else {
1024                         /* default to PROCCOPY */
1025                         chnl_id = gb_findandset(hnode_mgr->chnl_map);
1026                 }
1027                 if (chnl_id == GB_NOBITS) {
1028                         status = -ECONNREFUSED;
1029                         goto func_cont2;
1030                 }
1031                 pstr_dev_name = kzalloc(HOSTNAMELEN + 1, GFP_KERNEL);
1032                 if (pstr_dev_name != NULL)
1033                         goto func_cont2;
1034
1035                 if (pattrs) {
1036                         if (pattrs->strm_mode == STRMMODE_RDMA) {
1037                                 gb_clear(hnode_mgr->dma_chnl_map, chnl_id -
1038                                          hnode_mgr->ul_num_chnls);
1039                         } else if (pattrs->strm_mode == STRMMODE_ZEROCOPY) {
1040                                 gb_clear(hnode_mgr->zc_chnl_map, chnl_id -
1041                                          (2 * hnode_mgr->ul_num_chnls));
1042                         } else {
1043                                 DBC_ASSERT(pattrs->strm_mode ==
1044                                            STRMMODE_PROCCOPY);
1045                                 gb_clear(hnode_mgr->chnl_map, chnl_id);
1046                         }
1047                 } else {
1048                         gb_clear(hnode_mgr->chnl_map, chnl_id);
1049                 }
1050                 status = -ENOMEM;
1051 func_cont2:
1052                 if (DSP_SUCCEEDED(status)) {
1053                         if (node1 == (struct node_object *)DSP_HGPPNODE) {
1054                                 node2->inputs[uStream2].type = HOSTCONNECT;
1055                                 node2->inputs[uStream2].dev_id = chnl_id;
1056                                 input->sz_device = pstr_dev_name;
1057                         } else {
1058                                 node1->outputs[uStream1].type = HOSTCONNECT;
1059                                 node1->outputs[uStream1].dev_id = chnl_id;
1060                                 output->sz_device = pstr_dev_name;
1061                         }
1062                         sprintf(pstr_dev_name, "%s%d", HOSTPREFIX, chnl_id);
1063                 }
1064         }
1065         /* Connecting task node to device node? */
1066         if (DSP_SUCCEEDED(status) && ((node1_type == NODE_DEVICE) ||
1067                                       (node2_type == NODE_DEVICE))) {
1068                 if (node2_type == NODE_DEVICE) {
1069                         /* node1 == > device */
1070                         dev_node_obj = node2;
1071                         hnode = node1;
1072                         pstream = &(node1->outputs[uStream1]);
1073                         pstrm_def = output;
1074                 } else {
1075                         /* device == > node2 */
1076                         dev_node_obj = node1;
1077                         hnode = node2;
1078                         pstream = &(node2->inputs[uStream2]);
1079                         pstrm_def = input;
1080                 }
1081                 /* Set up create args */
1082                 pstream->type = DEVICECONNECT;
1083                 dw_length = strlen(dev_node_obj->pstr_dev_name);
1084                 if (conn_param != NULL) {
1085                         pstrm_def->sz_device = kzalloc(dw_length + 1 +
1086                                                         conn_param->cb_data,
1087                                                         GFP_KERNEL);
1088                 } else {
1089                         pstrm_def->sz_device = kzalloc(dw_length + 1,
1090                                                         GFP_KERNEL);
1091                 }
1092                 if (pstrm_def->sz_device == NULL) {
1093                         status = -ENOMEM;
1094                 } else {
1095                         /* Copy device name */
1096                         strncpy(pstrm_def->sz_device,
1097                                 dev_node_obj->pstr_dev_name, dw_length);
1098                         if (conn_param != NULL) {
1099                                 strncat(pstrm_def->sz_device,
1100                                         (char *)conn_param->node_data,
1101                                         (u32) conn_param->cb_data);
1102                         }
1103                         dev_node_obj->device_owner = hnode;
1104                 }
1105         }
1106         if (DSP_SUCCEEDED(status)) {
1107                 /* Fill in create args */
1108                 if (node1_type == NODE_TASK || node1_type == NODE_DAISSOCKET) {
1109                         node1->create_args.asa.task_arg_obj.num_outputs++;
1110                         fill_stream_def(node1, output, pattrs);
1111                 }
1112                 if (node2_type == NODE_TASK || node2_type == NODE_DAISSOCKET) {
1113                         node2->create_args.asa.task_arg_obj.num_inputs++;
1114                         fill_stream_def(node2, input, pattrs);
1115                 }
1116                 /* Update node1 and node2 stream_connect */
1117                 if (node1_type != NODE_GPP && node1_type != NODE_DEVICE) {
1118                         node1->num_outputs++;
1119                         if (uStream1 > node1->max_output_index)
1120                                 node1->max_output_index = uStream1;
1121
1122                 }
1123                 if (node2_type != NODE_GPP && node2_type != NODE_DEVICE) {
1124                         node2->num_inputs++;
1125                         if (uStream2 > node2->max_input_index)
1126                                 node2->max_input_index = uStream2;
1127
1128                 }
1129                 fill_stream_connect(node1, node2, uStream1, uStream2);
1130         }
1131         /* end of sync_enter_cs */
1132         /* Exit critical section */
1133         mutex_unlock(&hnode_mgr->node_mgr_lock);
1134 func_end:
1135         dev_dbg(bridge, "%s: node1: %p uStream1: %d node2: %p uStream2: %d"
1136                 "pattrs: %p status: 0x%x\n", __func__, node1,
1137                 uStream1, node2, uStream2, pattrs, status);
1138         return status;
1139 }
1140
1141 /*
1142  *  ======== node_create ========
1143  *  Purpose:
1144  *      Create a node on the DSP by remotely calling the node's create function.
1145  */
1146 int node_create(struct node_object *hnode)
1147 {
1148         struct node_object *pnode = (struct node_object *)hnode;
1149         struct node_mgr *hnode_mgr;
1150         struct bridge_drv_interface *intf_fxns;
1151         u32 ul_create_fxn;
1152         enum node_type node_type;
1153         int status = 0;
1154         int status1 = 0;
1155         struct dsp_cbdata cb_data;
1156         u32 proc_id = 255;
1157         struct dsp_processorstate proc_state;
1158         struct proc_object *hprocessor;
1159 #if defined(CONFIG_TIDSPBRIDGE_DVFS) && !defined(CONFIG_CPU_FREQ)
1160         struct dspbridge_platform_data *pdata =
1161             omap_dspbridge_dev->dev.platform_data;
1162 #endif
1163
1164         DBC_REQUIRE(refs > 0);
1165         if (!pnode) {
1166                 status = -EFAULT;
1167                 goto func_end;
1168         }
1169         hprocessor = hnode->hprocessor;
1170         status = proc_get_state(hprocessor, &proc_state,
1171                                 sizeof(struct dsp_processorstate));
1172         if (DSP_FAILED(status))
1173                 goto func_end;
1174         /* If processor is in error state then don't attempt to create
1175            new node */
1176         if (proc_state.proc_state == PROC_ERROR) {
1177                 status = -EPERM;
1178                 goto func_end;
1179         }
1180         /* create struct dsp_cbdata struct for PWR calls */
1181         cb_data.cb_data = PWR_TIMEOUT;
1182         node_type = node_get_type(hnode);
1183         hnode_mgr = hnode->hnode_mgr;
1184         intf_fxns = hnode_mgr->intf_fxns;
1185         /* Get access to node dispatcher */
1186         mutex_lock(&hnode_mgr->node_mgr_lock);
1187
1188         /* Check node state */
1189         if (node_get_state(hnode) != NODE_ALLOCATED)
1190                 status = -EBADR;
1191
1192         if (DSP_SUCCEEDED(status))
1193                 status = proc_get_processor_id(pnode->hprocessor, &proc_id);
1194
1195         if (DSP_FAILED(status))
1196                 goto func_cont2;
1197
1198         if (proc_id != DSP_UNIT)
1199                 goto func_cont2;
1200
1201         /* Make sure streams are properly connected */
1202         if ((hnode->num_inputs && hnode->max_input_index >
1203              hnode->num_inputs - 1) ||
1204             (hnode->num_outputs && hnode->max_output_index >
1205              hnode->num_outputs - 1))
1206                 status = -ENOTCONN;
1207
1208         if (DSP_SUCCEEDED(status)) {
1209                 /* If node's create function is not loaded, load it */
1210                 /* Boost the OPP level to max level that DSP can be requested */
1211 #if defined(CONFIG_TIDSPBRIDGE_DVFS) && !defined(CONFIG_CPU_FREQ)
1212                 if (pdata->cpu_set_freq)
1213                         (*pdata->cpu_set_freq) (pdata->mpu_speed[VDD1_OPP3]);
1214 #endif
1215                 status = hnode_mgr->nldr_fxns.pfn_load(hnode->nldr_node_obj,
1216                                                        NLDR_CREATE);
1217                 /* Get address of node's create function */
1218                 if (DSP_SUCCEEDED(status)) {
1219                         hnode->loaded = true;
1220                         if (node_type != NODE_DEVICE) {
1221                                 status = get_fxn_address(hnode, &ul_create_fxn,
1222                                                          CREATEPHASE);
1223                         }
1224                 } else {
1225                         pr_err("%s: failed to load create code: 0x%x\n",
1226                                __func__, status);
1227                 }
1228                 /* Request the lowest OPP level */
1229 #if defined(CONFIG_TIDSPBRIDGE_DVFS) && !defined(CONFIG_CPU_FREQ)
1230                 if (pdata->cpu_set_freq)
1231                         (*pdata->cpu_set_freq) (pdata->mpu_speed[VDD1_OPP1]);
1232 #endif
1233                 /* Get address of iAlg functions, if socket node */
1234                 if (DSP_SUCCEEDED(status)) {
1235                         if (node_type == NODE_DAISSOCKET) {
1236                                 status = hnode_mgr->nldr_fxns.pfn_get_fxn_addr
1237                                     (hnode->nldr_node_obj,
1238                                      hnode->dcd_props.obj_data.node_obj.
1239                                      pstr_i_alg_name,
1240                                      &hnode->create_args.asa.
1241                                      task_arg_obj.ul_dais_arg);
1242                         }
1243                 }
1244         }
1245         if (DSP_SUCCEEDED(status)) {
1246                 if (node_type != NODE_DEVICE) {
1247                         status = disp_node_create(hnode_mgr->disp_obj, hnode,
1248                                                   hnode_mgr->ul_fxn_addrs
1249                                                   [RMSCREATENODE],
1250                                                   ul_create_fxn,
1251                                                   &(hnode->create_args),
1252                                                   &(hnode->node_env));
1253                         if (DSP_SUCCEEDED(status)) {
1254                                 /* Set the message queue id to the node env
1255                                  * pointer */
1256                                 intf_fxns = hnode_mgr->intf_fxns;
1257                                 (*intf_fxns->pfn_msg_set_queue_id) (hnode->
1258                                                         msg_queue_obj,
1259                                                         hnode->node_env);
1260                         }
1261                 }
1262         }
1263         /*  Phase II/Overlays: Create, execute, delete phases  possibly in
1264          *  different files/sections. */
1265         if (hnode->loaded && hnode->phase_split) {
1266                 /* If create code was dynamically loaded, we can now unload
1267                  * it. */
1268                 status1 = hnode_mgr->nldr_fxns.pfn_unload(hnode->nldr_node_obj,
1269                                                           NLDR_CREATE);
1270                 hnode->loaded = false;
1271         }
1272         if (DSP_FAILED(status1))
1273                 pr_err("%s: Failed to unload create code: 0x%x\n",
1274                        __func__, status1);
1275 func_cont2:
1276         /* Update node state and node manager state */
1277         if (DSP_SUCCEEDED(status)) {
1278                 NODE_SET_STATE(hnode, NODE_CREATED);
1279                 hnode_mgr->num_created++;
1280                 goto func_cont;
1281         }
1282         if (status != -EBADR) {
1283                 /* Put back in NODE_ALLOCATED state if error occurred */
1284                 NODE_SET_STATE(hnode, NODE_ALLOCATED);
1285         }
1286 func_cont:
1287         /* Free access to node dispatcher */
1288         mutex_unlock(&hnode_mgr->node_mgr_lock);
1289 func_end:
1290         if (DSP_SUCCEEDED(status)) {
1291                 proc_notify_clients(hnode->hprocessor, DSP_NODESTATECHANGE);
1292                 ntfy_notify(hnode->ntfy_obj, DSP_NODESTATECHANGE);
1293         }
1294
1295         dev_dbg(bridge, "%s: hnode: %p status: 0x%x\n", __func__,
1296                 hnode, status);
1297         return status;
1298 }
1299
1300 /*
1301  *  ======== node_create_mgr ========
1302  *  Purpose:
1303  *      Create a NODE Manager object.
1304  */
1305 int node_create_mgr(OUT struct node_mgr **node_man,
1306                            struct dev_object *hdev_obj)
1307 {
1308         u32 i;
1309         struct node_mgr *node_mgr_obj = NULL;
1310         struct disp_attr disp_attr_obj;
1311         char *sz_zl_file = "";
1312         struct nldr_attrs nldr_attrs_obj;
1313         int status = 0;
1314         u8 dev_type;
1315         DBC_REQUIRE(refs > 0);
1316         DBC_REQUIRE(node_man != NULL);
1317         DBC_REQUIRE(hdev_obj != NULL);
1318
1319         *node_man = NULL;
1320         /* Allocate Node manager object */
1321         node_mgr_obj = kzalloc(sizeof(struct node_mgr), GFP_KERNEL);
1322         if (node_mgr_obj) {
1323                 node_mgr_obj->hdev_obj = hdev_obj;
1324                 node_mgr_obj->node_list = kzalloc(sizeof(struct lst_list),
1325                                                         GFP_KERNEL);
1326                 node_mgr_obj->pipe_map = gb_create(MAXPIPES);
1327                 node_mgr_obj->pipe_done_map = gb_create(MAXPIPES);
1328                 if (node_mgr_obj->node_list == NULL
1329                     || node_mgr_obj->pipe_map == NULL
1330                     || node_mgr_obj->pipe_done_map == NULL) {
1331                         status = -ENOMEM;
1332                 } else {
1333                         INIT_LIST_HEAD(&node_mgr_obj->node_list->head);
1334                         node_mgr_obj->ntfy_obj = kmalloc(
1335                                 sizeof(struct ntfy_object), GFP_KERNEL);
1336                         if (node_mgr_obj->ntfy_obj)
1337                                 ntfy_init(node_mgr_obj->ntfy_obj);
1338                         else
1339                                 status = -ENOMEM;
1340                 }
1341                 node_mgr_obj->num_created = 0;
1342         } else {
1343                 status = -ENOMEM;
1344         }
1345         /* get devNodeType */
1346         if (DSP_SUCCEEDED(status))
1347                 status = dev_get_dev_type(hdev_obj, &dev_type);
1348
1349         /* Create the DCD Manager */
1350         if (DSP_SUCCEEDED(status)) {
1351                 status =
1352                     dcd_create_manager(sz_zl_file, &node_mgr_obj->hdcd_mgr);
1353                 if (DSP_SUCCEEDED(status))
1354                         status = get_proc_props(node_mgr_obj, hdev_obj);
1355
1356         }
1357         /* Create NODE Dispatcher */
1358         if (DSP_SUCCEEDED(status)) {
1359                 disp_attr_obj.ul_chnl_offset = node_mgr_obj->ul_chnl_offset;
1360                 disp_attr_obj.ul_chnl_buf_size = node_mgr_obj->ul_chnl_buf_size;
1361                 disp_attr_obj.proc_family = node_mgr_obj->proc_family;
1362                 disp_attr_obj.proc_type = node_mgr_obj->proc_type;
1363                 status =
1364                     disp_create(&node_mgr_obj->disp_obj, hdev_obj,
1365                                 &disp_attr_obj);
1366         }
1367         /* Create a STRM Manager */
1368         if (DSP_SUCCEEDED(status))
1369                 status = strm_create(&node_mgr_obj->strm_mgr_obj, hdev_obj);
1370
1371         if (DSP_SUCCEEDED(status)) {
1372                 dev_get_intf_fxns(hdev_obj, &node_mgr_obj->intf_fxns);
1373                 /* Get msg_ctrl queue manager */
1374                 dev_get_msg_mgr(hdev_obj, &node_mgr_obj->msg_mgr_obj);
1375                 mutex_init(&node_mgr_obj->node_mgr_lock);
1376                 node_mgr_obj->chnl_map = gb_create(node_mgr_obj->ul_num_chnls);
1377                 /* dma chnl map. ul_num_chnls is # per transport */
1378                 node_mgr_obj->dma_chnl_map =
1379                     gb_create(node_mgr_obj->ul_num_chnls);
1380                 node_mgr_obj->zc_chnl_map =
1381                     gb_create(node_mgr_obj->ul_num_chnls);
1382                 if ((node_mgr_obj->chnl_map == NULL)
1383                     || (node_mgr_obj->dma_chnl_map == NULL)
1384                     || (node_mgr_obj->zc_chnl_map == NULL)) {
1385                         status = -ENOMEM;
1386                 } else {
1387                         /* Block out reserved channels */
1388                         for (i = 0; i < node_mgr_obj->ul_chnl_offset; i++)
1389                                 gb_set(node_mgr_obj->chnl_map, i);
1390
1391                         /* Block out channels reserved for RMS */
1392                         gb_set(node_mgr_obj->chnl_map,
1393                                node_mgr_obj->ul_chnl_offset);
1394                         gb_set(node_mgr_obj->chnl_map,
1395                                node_mgr_obj->ul_chnl_offset + 1);
1396                 }
1397         }
1398         if (DSP_SUCCEEDED(status)) {
1399                 /* NO RM Server on the IVA */
1400                 if (dev_type != IVA_UNIT) {
1401                         /* Get addresses of any RMS functions loaded */
1402                         status = get_rms_fxns(node_mgr_obj);
1403                 }
1404         }
1405
1406         /* Get loader functions and create loader */
1407         if (DSP_SUCCEEDED(status))
1408                 node_mgr_obj->nldr_fxns = nldr_fxns;    /* Dyn loader funcs */
1409
1410         if (DSP_SUCCEEDED(status)) {
1411                 nldr_attrs_obj.pfn_ovly = ovly;
1412                 nldr_attrs_obj.pfn_write = mem_write;
1413                 nldr_attrs_obj.us_dsp_word_size = node_mgr_obj->udsp_word_size;
1414                 nldr_attrs_obj.us_dsp_mau_size = node_mgr_obj->udsp_mau_size;
1415                 node_mgr_obj->loader_init = node_mgr_obj->nldr_fxns.pfn_init();
1416                 status =
1417                     node_mgr_obj->nldr_fxns.pfn_create(&node_mgr_obj->nldr_obj,
1418                                                        hdev_obj,
1419                                                        &nldr_attrs_obj);
1420         }
1421         if (DSP_SUCCEEDED(status))
1422                 *node_man = node_mgr_obj;
1423         else
1424                 delete_node_mgr(node_mgr_obj);
1425
1426         DBC_ENSURE((DSP_FAILED(status) && (*node_man == NULL)) ||
1427                         (DSP_SUCCEEDED(status) && *node_man));
1428
1429         return status;
1430 }
1431
1432 /*
1433  *  ======== node_delete ========
1434  *  Purpose:
1435  *      Delete a node on the DSP by remotely calling the node's delete function.
1436  *      Loads the node's delete function if necessary. Free GPP side resources
1437  *      after node's delete function returns.
1438  */
1439 int node_delete(struct node_object *hnode,
1440                        struct process_context *pr_ctxt)
1441 {
1442         struct node_object *pnode = (struct node_object *)hnode;
1443         struct node_mgr *hnode_mgr;
1444         struct proc_object *hprocessor;
1445         struct disp_object *disp_obj;
1446         u32 ul_delete_fxn;
1447         enum node_type node_type;
1448         enum node_state state;
1449         int status = 0;
1450         int status1 = 0;
1451         struct dsp_cbdata cb_data;
1452         u32 proc_id;
1453         struct bridge_drv_interface *intf_fxns;
1454
1455         void *node_res;
1456
1457         struct dsp_processorstate proc_state;
1458         DBC_REQUIRE(refs > 0);
1459
1460         if (!hnode) {
1461                 status = -EFAULT;
1462                 goto func_end;
1463         }
1464         /* create struct dsp_cbdata struct for PWR call */
1465         cb_data.cb_data = PWR_TIMEOUT;
1466         hnode_mgr = hnode->hnode_mgr;
1467         hprocessor = hnode->hprocessor;
1468         disp_obj = hnode_mgr->disp_obj;
1469         node_type = node_get_type(hnode);
1470         intf_fxns = hnode_mgr->intf_fxns;
1471         /* Enter critical section */
1472         mutex_lock(&hnode_mgr->node_mgr_lock);
1473
1474         state = node_get_state(hnode);
1475         /*  Execute delete phase code for non-device node in all cases
1476          *  except when the node was only allocated. Delete phase must be
1477          *  executed even if create phase was executed, but failed.
1478          *  If the node environment pointer is non-NULL, the delete phase
1479          *  code must be  executed. */
1480         if (!(state == NODE_ALLOCATED && hnode->node_env == (u32) NULL) &&
1481             node_type != NODE_DEVICE) {
1482                 status = proc_get_processor_id(pnode->hprocessor, &proc_id);
1483                 if (DSP_FAILED(status))
1484                         goto func_cont1;
1485
1486                 if (proc_id == DSP_UNIT || proc_id == IVA_UNIT) {
1487                         /*  If node has terminated, execute phase code will
1488                          *  have already been unloaded in node_on_exit(). If the
1489                          *  node is PAUSED, the execute phase is loaded, and it
1490                          *  is now ok to unload it. If the node is running, we
1491                          *  will unload the execute phase only after deleting
1492                          *  the node. */
1493                         if (state == NODE_PAUSED && hnode->loaded &&
1494                             hnode->phase_split) {
1495                                 /* Ok to unload execute code as long as node
1496                                  * is not * running */
1497                                 status1 =
1498                                     hnode_mgr->nldr_fxns.
1499                                     pfn_unload(hnode->nldr_node_obj,
1500                                                NLDR_EXECUTE);
1501                                 hnode->loaded = false;
1502                                 NODE_SET_STATE(hnode, NODE_DONE);
1503                         }
1504                         /* Load delete phase code if not loaded or if haven't
1505                          * * unloaded EXECUTE phase */
1506                         if ((!(hnode->loaded) || (state == NODE_RUNNING)) &&
1507                             hnode->phase_split) {
1508                                 status =
1509                                     hnode_mgr->nldr_fxns.
1510                                     pfn_load(hnode->nldr_node_obj, NLDR_DELETE);
1511                                 if (DSP_SUCCEEDED(status))
1512                                         hnode->loaded = true;
1513                                 else
1514                                         pr_err("%s: fail - load delete code:"
1515                                                " 0x%x\n", __func__, status);
1516                         }
1517                 }
1518 func_cont1:
1519                 if (DSP_SUCCEEDED(status)) {
1520                         /* Unblock a thread trying to terminate the node */
1521                         (void)sync_set_event(hnode->sync_done);
1522                         if (proc_id == DSP_UNIT) {
1523                                 /* ul_delete_fxn = address of node's delete
1524                                  * function */
1525                                 status = get_fxn_address(hnode, &ul_delete_fxn,
1526                                                          DELETEPHASE);
1527                         } else if (proc_id == IVA_UNIT)
1528                                 ul_delete_fxn = (u32) hnode->node_env;
1529                         if (DSP_SUCCEEDED(status)) {
1530                                 status = proc_get_state(hprocessor,
1531                                                 &proc_state,
1532                                                 sizeof(struct
1533                                                        dsp_processorstate));
1534                                 if (proc_state.proc_state != PROC_ERROR) {
1535                                         status =
1536                                             disp_node_delete(disp_obj, hnode,
1537                                                              hnode_mgr->
1538                                                              ul_fxn_addrs
1539                                                              [RMSDELETENODE],
1540                                                              ul_delete_fxn,
1541                                                              hnode->node_env);
1542                                 } else
1543                                         NODE_SET_STATE(hnode, NODE_DONE);
1544
1545                                 /* Unload execute, if not unloaded, and delete
1546                                  * function */
1547                                 if (state == NODE_RUNNING &&
1548                                     hnode->phase_split) {
1549                                         status1 =
1550                                             hnode_mgr->nldr_fxns.
1551                                             pfn_unload(hnode->nldr_node_obj,
1552                                                        NLDR_EXECUTE);
1553                                 }
1554                                 if (DSP_FAILED(status1))
1555                                         pr_err("%s: fail - unload execute code:"
1556                                                " 0x%x\n", __func__, status1);
1557
1558                                 status1 =
1559                                     hnode_mgr->nldr_fxns.pfn_unload(hnode->
1560                                                             nldr_node_obj,
1561                                                             NLDR_DELETE);
1562                                 hnode->loaded = false;
1563                                 if (DSP_FAILED(status1))
1564                                         pr_err("%s: fail - unload delete code: "
1565                                                "0x%x\n", __func__, status1);
1566                         }
1567                 }
1568         }
1569         /* Free host side resources even if a failure occurred */
1570         /* Remove node from hnode_mgr->node_list */
1571         lst_remove_elem(hnode_mgr->node_list, (struct list_head *)hnode);
1572         hnode_mgr->num_nodes--;
1573         /* Decrement count of nodes created on DSP */
1574         if ((state != NODE_ALLOCATED) || ((state == NODE_ALLOCATED) &&
1575                                           (hnode->node_env != (u32) NULL)))
1576                 hnode_mgr->num_created--;
1577         /*  Free host-side resources allocated by node_create()
1578          *  delete_node() fails if SM buffers not freed by client! */
1579         if (drv_get_node_res_element(hnode, &node_res, pr_ctxt) !=
1580             -ENOENT)
1581                 drv_proc_node_update_status(node_res, false);
1582         delete_node(hnode, pr_ctxt);
1583
1584         drv_remove_node_res_element(node_res, pr_ctxt);
1585         /* Exit critical section */
1586         mutex_unlock(&hnode_mgr->node_mgr_lock);
1587         proc_notify_clients(hprocessor, DSP_NODESTATECHANGE);
1588 func_end:
1589         dev_dbg(bridge, "%s: hnode: %p status 0x%x\n", __func__, hnode, status);
1590         return status;
1591 }
1592
1593 /*
1594  *  ======== node_delete_mgr ========
1595  *  Purpose:
1596  *      Delete the NODE Manager.
1597  */
1598 int node_delete_mgr(struct node_mgr *hnode_mgr)
1599 {
1600         int status = 0;
1601
1602         DBC_REQUIRE(refs > 0);
1603
1604         if (hnode_mgr)
1605                 delete_node_mgr(hnode_mgr);
1606         else
1607                 status = -EFAULT;
1608
1609         return status;
1610 }
1611
1612 /*
1613  *  ======== node_enum_nodes ========
1614  *  Purpose:
1615  *      Enumerate currently allocated nodes.
1616  */
1617 int node_enum_nodes(struct node_mgr *hnode_mgr, void **node_tab,
1618                            u32 node_tab_size, OUT u32 *pu_num_nodes,
1619                            OUT u32 *pu_allocated)
1620 {
1621         struct node_object *hnode;
1622         u32 i;
1623         int status = 0;
1624         DBC_REQUIRE(refs > 0);
1625         DBC_REQUIRE(node_tab != NULL || node_tab_size == 0);
1626         DBC_REQUIRE(pu_num_nodes != NULL);
1627         DBC_REQUIRE(pu_allocated != NULL);
1628
1629         if (!hnode_mgr) {
1630                 status = -EFAULT;
1631                 goto func_end;
1632         }
1633         /* Enter critical section */
1634         mutex_lock(&hnode_mgr->node_mgr_lock);
1635
1636         if (hnode_mgr->num_nodes > node_tab_size) {
1637                 *pu_allocated = hnode_mgr->num_nodes;
1638                 *pu_num_nodes = 0;
1639                 status = -EINVAL;
1640         } else {
1641                 hnode = (struct node_object *)lst_first(hnode_mgr->
1642                         node_list);
1643                 for (i = 0; i < hnode_mgr->num_nodes; i++) {
1644                         DBC_ASSERT(hnode);
1645                         node_tab[i] = hnode;
1646                         hnode = (struct node_object *)lst_next
1647                                 (hnode_mgr->node_list,
1648                                 (struct list_head *)hnode);
1649                 }
1650                 *pu_allocated = *pu_num_nodes = hnode_mgr->num_nodes;
1651         }
1652         /* end of sync_enter_cs */
1653         /* Exit critical section */
1654         mutex_unlock(&hnode_mgr->node_mgr_lock);
1655 func_end:
1656         return status;
1657 }
1658
1659 /*
1660  *  ======== node_exit ========
1661  *  Purpose:
1662  *      Discontinue usage of NODE module.
1663  */
1664 void node_exit(void)
1665 {
1666         DBC_REQUIRE(refs > 0);
1667
1668         refs--;
1669
1670         DBC_ENSURE(refs >= 0);
1671 }
1672
1673 /*
1674  *  ======== node_free_msg_buf ========
1675  *  Purpose:
1676  *      Frees the message buffer.
1677  */
1678 int node_free_msg_buf(struct node_object *hnode, IN u8 * pbuffer,
1679                              OPTIONAL struct dsp_bufferattr *pattr)
1680 {
1681         struct node_object *pnode = (struct node_object *)hnode;
1682         int status = 0;
1683         u32 proc_id;
1684         DBC_REQUIRE(refs > 0);
1685         DBC_REQUIRE(pbuffer != NULL);
1686         DBC_REQUIRE(pnode != NULL);
1687         DBC_REQUIRE(pnode->xlator != NULL);
1688
1689         if (!hnode) {
1690                 status = -EFAULT;
1691                 goto func_end;
1692         }
1693         status = proc_get_processor_id(pnode->hprocessor, &proc_id);
1694         if (proc_id == DSP_UNIT) {
1695                 if (DSP_SUCCEEDED(status)) {
1696                         if (pattr == NULL) {
1697                                 /* set defaults */
1698                                 pattr = &node_dfltbufattrs;
1699                         }
1700                         /* Node supports single SM segment only */
1701                         if (pattr->segment_id != 1)
1702                                 status = -EBADR;
1703
1704                         /* pbuffer is clients Va. */
1705                         status = cmm_xlator_free_buf(pnode->xlator, pbuffer);
1706                 }
1707         } else {
1708                 DBC_ASSERT(NULL);       /* BUG */
1709         }
1710 func_end:
1711         return status;
1712 }
1713
1714 /*
1715  *  ======== node_get_attr ========
1716  *  Purpose:
1717  *      Copy the current attributes of the specified node into a dsp_nodeattr
1718  *      structure.
1719  */
1720 int node_get_attr(struct node_object *hnode,
1721                          OUT struct dsp_nodeattr *pattr, u32 attr_size)
1722 {
1723         struct node_mgr *hnode_mgr;
1724         int status = 0;
1725         DBC_REQUIRE(refs > 0);
1726         DBC_REQUIRE(pattr != NULL);
1727         DBC_REQUIRE(attr_size >= sizeof(struct dsp_nodeattr));
1728
1729         if (!hnode) {
1730                 status = -EFAULT;
1731         } else {
1732                 hnode_mgr = hnode->hnode_mgr;
1733                 /* Enter hnode_mgr critical section (since we're accessing
1734                  * data that could be changed by node_change_priority() and
1735                  * node_connect(). */
1736                 mutex_lock(&hnode_mgr->node_mgr_lock);
1737                 pattr->cb_struct = sizeof(struct dsp_nodeattr);
1738                 /* dsp_nodeattrin */
1739                 pattr->in_node_attr_in.cb_struct =
1740                                  sizeof(struct dsp_nodeattrin);
1741                 pattr->in_node_attr_in.prio = hnode->prio;
1742                 pattr->in_node_attr_in.utimeout = hnode->utimeout;
1743                 pattr->in_node_attr_in.heap_size =
1744                         hnode->create_args.asa.task_arg_obj.heap_size;
1745                 pattr->in_node_attr_in.pgpp_virt_addr = (void *)
1746                         hnode->create_args.asa.task_arg_obj.ugpp_heap_addr;
1747                 pattr->node_attr_inputs = hnode->num_gpp_inputs;
1748                 pattr->node_attr_outputs = hnode->num_gpp_outputs;
1749                 /* dsp_nodeinfo */
1750                 get_node_info(hnode, &(pattr->node_info));
1751                 /* end of sync_enter_cs */
1752                 /* Exit critical section */
1753                 mutex_unlock(&hnode_mgr->node_mgr_lock);
1754         }
1755         return status;
1756 }
1757
1758 /*
1759  *  ======== node_get_channel_id ========
1760  *  Purpose:
1761  *      Get the channel index reserved for a stream connection between the
1762  *      host and a node.
1763  */
1764 int node_get_channel_id(struct node_object *hnode, u32 dir, u32 index,
1765                                OUT u32 *pulId)
1766 {
1767         enum node_type node_type;
1768         int status = -EINVAL;
1769         DBC_REQUIRE(refs > 0);
1770         DBC_REQUIRE(dir == DSP_TONODE || dir == DSP_FROMNODE);
1771         DBC_REQUIRE(pulId != NULL);
1772
1773         if (!hnode) {
1774                 status = -EFAULT;
1775                 return status;
1776         }
1777         node_type = node_get_type(hnode);
1778         if (node_type != NODE_TASK && node_type != NODE_DAISSOCKET) {
1779                 status = -EPERM;
1780                 return status;
1781         }
1782         if (dir == DSP_TONODE) {
1783                 if (index < MAX_INPUTS(hnode)) {
1784                         if (hnode->inputs[index].type == HOSTCONNECT) {
1785                                 *pulId = hnode->inputs[index].dev_id;
1786                                 status = 0;
1787                         }
1788                 }
1789         } else {
1790                 DBC_ASSERT(dir == DSP_FROMNODE);
1791                 if (index < MAX_OUTPUTS(hnode)) {
1792                         if (hnode->outputs[index].type == HOSTCONNECT) {
1793                                 *pulId = hnode->outputs[index].dev_id;
1794                                 status = 0;
1795                         }
1796                 }
1797         }
1798         return status;
1799 }
1800
1801 /*
1802  *  ======== node_get_message ========
1803  *  Purpose:
1804  *      Retrieve a message from a node on the DSP.
1805  */
1806 int node_get_message(struct node_object *hnode,
1807                             OUT struct dsp_msg *pmsg, u32 utimeout)
1808 {
1809         struct node_mgr *hnode_mgr;
1810         enum node_type node_type;
1811         struct bridge_drv_interface *intf_fxns;
1812         int status = 0;
1813         void *tmp_buf;
1814         struct dsp_processorstate proc_state;
1815         struct proc_object *hprocessor;
1816
1817         DBC_REQUIRE(refs > 0);
1818         DBC_REQUIRE(pmsg != NULL);
1819
1820         if (!hnode) {
1821                 status = -EFAULT;
1822                 goto func_end;
1823         }
1824         hprocessor = hnode->hprocessor;
1825         status = proc_get_state(hprocessor, &proc_state,
1826                                 sizeof(struct dsp_processorstate));
1827         if (DSP_FAILED(status))
1828                 goto func_end;
1829         /* If processor is in error state then don't attempt to get the
1830            message */
1831         if (proc_state.proc_state == PROC_ERROR) {
1832                 status = -EPERM;
1833                 goto func_end;
1834         }
1835         hnode_mgr = hnode->hnode_mgr;
1836         node_type = node_get_type(hnode);
1837         if (node_type != NODE_MESSAGE && node_type != NODE_TASK &&
1838             node_type != NODE_DAISSOCKET) {
1839                 status = -EPERM;
1840                 goto func_end;
1841         }
1842         /*  This function will block unless a message is available. Since
1843          *  DSPNode_RegisterNotify() allows notification when a message
1844          *  is available, the system can be designed so that
1845          *  DSPNode_GetMessage() is only called when a message is
1846          *  available. */
1847         intf_fxns = hnode_mgr->intf_fxns;
1848         status =
1849             (*intf_fxns->pfn_msg_get) (hnode->msg_queue_obj, pmsg, utimeout);
1850         /* Check if message contains SM descriptor */
1851         if (DSP_FAILED(status) || !(pmsg->dw_cmd & DSP_RMSBUFDESC))
1852                 goto func_end;
1853
1854         /* Translate DSP byte addr to GPP Va. */
1855         tmp_buf = cmm_xlator_translate(hnode->xlator,
1856                                        (void *)(pmsg->dw_arg1 *
1857                                                 hnode->hnode_mgr->
1858                                                 udsp_word_size), CMM_DSPPA2PA);
1859         if (tmp_buf != NULL) {
1860                 /* now convert this GPP Pa to Va */
1861                 tmp_buf = cmm_xlator_translate(hnode->xlator, tmp_buf,
1862                                                CMM_PA2VA);
1863                 if (tmp_buf != NULL) {
1864                         /* Adjust SM size in msg */
1865                         pmsg->dw_arg1 = (u32) tmp_buf;
1866                         pmsg->dw_arg2 *= hnode->hnode_mgr->udsp_word_size;
1867                 } else {
1868                         status = -ESRCH;
1869                 }
1870         } else {
1871                 status = -ESRCH;
1872         }
1873 func_end:
1874         dev_dbg(bridge, "%s: hnode: %p pmsg: %p utimeout: 0x%x\n", __func__,
1875                 hnode, pmsg, utimeout);
1876         return status;
1877 }
1878
1879 /*
1880  *   ======== node_get_nldr_obj ========
1881  */
1882 int node_get_nldr_obj(struct node_mgr *hnode_mgr,
1883                              struct nldr_object **nldr_ovlyobj)
1884 {
1885         int status = 0;
1886         struct node_mgr *node_mgr_obj = hnode_mgr;
1887         DBC_REQUIRE(nldr_ovlyobj != NULL);
1888
1889         if (!hnode_mgr)
1890                 status = -EFAULT;
1891         else
1892                 *nldr_ovlyobj = node_mgr_obj->nldr_obj;
1893
1894         DBC_ENSURE(DSP_SUCCEEDED(status) || ((nldr_ovlyobj != NULL) &&
1895                                              (*nldr_ovlyobj == NULL)));
1896         return status;
1897 }
1898
1899 /*
1900  *  ======== node_get_strm_mgr ========
1901  *  Purpose:
1902  *      Returns the Stream manager.
1903  */
1904 int node_get_strm_mgr(struct node_object *hnode,
1905                              struct strm_mgr **strm_man)
1906 {
1907         int status = 0;
1908
1909         DBC_REQUIRE(refs > 0);
1910
1911         if (!hnode)
1912                 status = -EFAULT;
1913         else
1914                 *strm_man = hnode->hnode_mgr->strm_mgr_obj;
1915
1916         return status;
1917 }
1918
1919 /*
1920  *  ======== node_get_load_type ========
1921  */
1922 enum nldr_loadtype node_get_load_type(struct node_object *hnode)
1923 {
1924         DBC_REQUIRE(refs > 0);
1925         DBC_REQUIRE(hnode);
1926         if (!hnode) {
1927                 dev_dbg(bridge, "%s: Failed. hnode: %p\n", __func__, hnode);
1928                 return -1;
1929         } else {
1930                 return hnode->dcd_props.obj_data.node_obj.us_load_type;
1931         }
1932 }
1933
1934 /*
1935  *  ======== node_get_timeout ========
1936  *  Purpose:
1937  *      Returns the timeout value for this node.
1938  */
1939 u32 node_get_timeout(struct node_object *hnode)
1940 {
1941         DBC_REQUIRE(refs > 0);
1942         DBC_REQUIRE(hnode);
1943         if (!hnode) {
1944                 dev_dbg(bridge, "%s: failed. hnode: %p\n", __func__, hnode);
1945                 return 0;
1946         } else {
1947                 return hnode->utimeout;
1948         }
1949 }
1950
1951 /*
1952  *  ======== node_get_type ========
1953  *  Purpose:
1954  *      Returns the node type.
1955  */
1956 enum node_type node_get_type(struct node_object *hnode)
1957 {
1958         enum node_type node_type;
1959
1960         if (hnode == (struct node_object *)DSP_HGPPNODE)
1961                 node_type = NODE_GPP;
1962         else {
1963                 if (!hnode)
1964                         node_type = -1;
1965                 else
1966                         node_type = hnode->ntype;
1967         }
1968         return node_type;
1969 }
1970
1971 /*
1972  *  ======== node_init ========
1973  *  Purpose:
1974  *      Initialize the NODE module.
1975  */
1976 bool node_init(void)
1977 {
1978         DBC_REQUIRE(refs >= 0);
1979
1980         refs++;
1981
1982         return true;
1983 }
1984
1985 /*
1986  *  ======== node_on_exit ========
1987  *  Purpose:
1988  *      Gets called when RMS_EXIT is received for a node.
1989  */
1990 void node_on_exit(struct node_object *hnode, s32 node_status)
1991 {
1992         if (!hnode)
1993                 return;
1994
1995         /* Set node state to done */
1996         NODE_SET_STATE(hnode, NODE_DONE);
1997         hnode->exit_status = node_status;
1998         if (hnode->loaded && hnode->phase_split) {
1999                 (void)hnode->hnode_mgr->nldr_fxns.pfn_unload(hnode->
2000                                                              nldr_node_obj,
2001                                                              NLDR_EXECUTE);
2002                 hnode->loaded = false;
2003         }
2004         /* Unblock call to node_terminate */
2005         (void)sync_set_event(hnode->sync_done);
2006         /* Notify clients */
2007         proc_notify_clients(hnode->hprocessor, DSP_NODESTATECHANGE);
2008         ntfy_notify(hnode->ntfy_obj, DSP_NODESTATECHANGE);
2009 }
2010
2011 /*
2012  *  ======== node_pause ========
2013  *  Purpose:
2014  *      Suspend execution of a node currently running on the DSP.
2015  */
2016 int node_pause(struct node_object *hnode)
2017 {
2018         struct node_object *pnode = (struct node_object *)hnode;
2019         enum node_type node_type;
2020         enum node_state state;
2021         struct node_mgr *hnode_mgr;
2022         int status = 0;
2023         u32 proc_id;
2024         struct dsp_processorstate proc_state;
2025         struct proc_object *hprocessor;
2026
2027         DBC_REQUIRE(refs > 0);
2028
2029         if (!hnode) {
2030                 status = -EFAULT;
2031         } else {
2032                 node_type = node_get_type(hnode);
2033                 if (node_type != NODE_TASK && node_type != NODE_DAISSOCKET)
2034                         status = -EPERM;
2035         }
2036         if (DSP_FAILED(status))
2037                 goto func_end;
2038
2039         status = proc_get_processor_id(pnode->hprocessor, &proc_id);
2040
2041         if (proc_id == IVA_UNIT)
2042                 status = -ENOSYS;
2043
2044         if (DSP_SUCCEEDED(status)) {
2045                 hnode_mgr = hnode->hnode_mgr;
2046
2047                 /* Enter critical section */
2048                 mutex_lock(&hnode_mgr->node_mgr_lock);
2049                 state = node_get_state(hnode);
2050                 /* Check node state */
2051                 if (state != NODE_RUNNING)
2052                         status = -EBADR;
2053
2054                 if (DSP_FAILED(status))
2055                         goto func_cont;
2056                 hprocessor = hnode->hprocessor;
2057                 status = proc_get_state(hprocessor, &proc_state,
2058                                 sizeof(struct dsp_processorstate));
2059                 if (DSP_FAILED(status))
2060                         goto func_cont;
2061                 /* If processor is in error state then don't attempt
2062                    to send the message */
2063                 if (proc_state.proc_state == PROC_ERROR) {
2064                         status = -EPERM;
2065                         goto func_cont;
2066                 }
2067
2068                 status = disp_node_change_priority(hnode_mgr->disp_obj, hnode,
2069                         hnode_mgr->ul_fxn_addrs[RMSCHANGENODEPRIORITY],
2070                         hnode->node_env, NODE_SUSPENDEDPRI);
2071
2072                 /* Update state */
2073                 if (DSP_SUCCEEDED(status))
2074                         NODE_SET_STATE(hnode, NODE_PAUSED);
2075
2076 func_cont:
2077                 /* End of sync_enter_cs */
2078                 /* Leave critical section */
2079                 mutex_unlock(&hnode_mgr->node_mgr_lock);
2080                 if (DSP_SUCCEEDED(status)) {
2081                         proc_notify_clients(hnode->hprocessor,
2082                                             DSP_NODESTATECHANGE);
2083                         ntfy_notify(hnode->ntfy_obj, DSP_NODESTATECHANGE);
2084                 }
2085         }
2086 func_end:
2087         dev_dbg(bridge, "%s: hnode: %p status 0x%x\n", __func__, hnode, status);
2088         return status;
2089 }
2090
2091 /*
2092  *  ======== node_put_message ========
2093  *  Purpose:
2094  *      Send a message to a message node, task node, or XDAIS socket node. This
2095  *      function will block until the message stream can accommodate the
2096  *      message, or a timeout occurs.
2097  */
2098 int node_put_message(struct node_object *hnode,
2099                             IN CONST struct dsp_msg *pmsg, u32 utimeout)
2100 {
2101         struct node_mgr *hnode_mgr = NULL;
2102         enum node_type node_type;
2103         struct bridge_drv_interface *intf_fxns;
2104         enum node_state state;
2105         int status = 0;
2106         void *tmp_buf;
2107         struct dsp_msg new_msg;
2108         struct dsp_processorstate proc_state;
2109         struct proc_object *hprocessor;
2110
2111         DBC_REQUIRE(refs > 0);
2112         DBC_REQUIRE(pmsg != NULL);
2113
2114         if (!hnode) {
2115                 status = -EFAULT;
2116                 goto func_end;
2117         }
2118         hprocessor = hnode->hprocessor;
2119         status = proc_get_state(hprocessor, &proc_state,
2120                                 sizeof(struct dsp_processorstate));
2121         if (DSP_FAILED(status))
2122                 goto func_end;
2123         /* If processor is in bad state then don't attempt sending the
2124            message */
2125         if (proc_state.proc_state == PROC_ERROR) {
2126                 status = -EPERM;
2127                 goto func_end;
2128         }
2129         hnode_mgr = hnode->hnode_mgr;
2130         node_type = node_get_type(hnode);
2131         if (node_type != NODE_MESSAGE && node_type != NODE_TASK &&
2132             node_type != NODE_DAISSOCKET)
2133                 status = -EPERM;
2134
2135         if (DSP_SUCCEEDED(status)) {
2136                 /*  Check node state. Can't send messages to a node after
2137                  *  we've sent the RMS_EXIT command. There is still the
2138                  *  possibility that node_terminate can be called after we've
2139                  *  checked the state. Could add another SYNC object to
2140                  *  prevent this (can't use node_mgr_lock, since we don't
2141                  *  want to block other NODE functions). However, the node may
2142                  *  still exit on its own, before this message is sent. */
2143                 mutex_lock(&hnode_mgr->node_mgr_lock);
2144                 state = node_get_state(hnode);
2145                 if (state == NODE_TERMINATING || state == NODE_DONE)
2146                         status = -EBADR;
2147
2148                 /* end of sync_enter_cs */
2149                 mutex_unlock(&hnode_mgr->node_mgr_lock);
2150         }
2151         if (DSP_FAILED(status))
2152                 goto func_end;
2153
2154         /* assign pmsg values to new msg */
2155         new_msg = *pmsg;
2156         /* Now, check if message contains a SM buffer descriptor */
2157         if (pmsg->dw_cmd & DSP_RMSBUFDESC) {
2158                 /* Translate GPP Va to DSP physical buf Ptr. */
2159                 tmp_buf = cmm_xlator_translate(hnode->xlator,
2160                                                (void *)new_msg.dw_arg1,
2161                                                CMM_VA2DSPPA);
2162                 if (tmp_buf != NULL) {
2163                         /* got translation, convert to MAUs in msg */
2164                         if (hnode->hnode_mgr->udsp_word_size != 0) {
2165                                 new_msg.dw_arg1 =
2166                                     (u32) tmp_buf /
2167                                     hnode->hnode_mgr->udsp_word_size;
2168                                 /* MAUs */
2169                                 new_msg.dw_arg2 /= hnode->hnode_mgr->
2170                                     udsp_word_size;
2171                         } else {
2172                                 pr_err("%s: udsp_word_size is zero!\n",
2173                                        __func__);
2174                                 status = -EPERM;        /* bad DSPWordSize */
2175                         }
2176                 } else {        /* failed to translate buffer address */
2177                         status = -ESRCH;
2178                 }
2179         }
2180         if (DSP_SUCCEEDED(status)) {
2181                 intf_fxns = hnode_mgr->intf_fxns;
2182                 status = (*intf_fxns->pfn_msg_put) (hnode->msg_queue_obj,
2183                                                     &new_msg, utimeout);
2184         }
2185 func_end:
2186         dev_dbg(bridge, "%s: hnode: %p pmsg: %p utimeout: 0x%x, "
2187                 "status 0x%x\n", __func__, hnode, pmsg, utimeout, status);
2188         return status;
2189 }
2190
2191 /*
2192  *  ======== node_register_notify ========
2193  *  Purpose:
2194  *      Register to be notified on specific events for this node.
2195  */
2196 int node_register_notify(struct node_object *hnode, u32 event_mask,
2197                                 u32 notify_type,
2198                                 struct dsp_notification *hnotification)
2199 {
2200         struct bridge_drv_interface *intf_fxns;
2201         int status = 0;
2202
2203         DBC_REQUIRE(refs > 0);
2204         DBC_REQUIRE(hnotification != NULL);
2205
2206         if (!hnode) {
2207                 status = -EFAULT;
2208         } else {
2209                 /* Check if event mask is a valid node related event */
2210                 if (event_mask & ~(DSP_NODESTATECHANGE | DSP_NODEMESSAGEREADY))
2211                         status = -EINVAL;
2212
2213                 /* Check if notify type is valid */
2214                 if (notify_type != DSP_SIGNALEVENT)
2215                         status = -EINVAL;
2216
2217                 /* Only one Notification can be registered at a
2218                  * time - Limitation */
2219                 if (event_mask == (DSP_NODESTATECHANGE | DSP_NODEMESSAGEREADY))
2220                         status = -EINVAL;
2221         }
2222         if (DSP_SUCCEEDED(status)) {
2223                 if (event_mask == DSP_NODESTATECHANGE) {
2224                         status = ntfy_register(hnode->ntfy_obj, hnotification,
2225                                                event_mask & DSP_NODESTATECHANGE,
2226                                                notify_type);
2227                 } else {
2228                         /* Send Message part of event mask to msg_ctrl */
2229                         intf_fxns = hnode->hnode_mgr->intf_fxns;
2230                         status = (*intf_fxns->pfn_msg_register_notify)
2231                             (hnode->msg_queue_obj,
2232                              event_mask & DSP_NODEMESSAGEREADY, notify_type,
2233                              hnotification);
2234                 }
2235
2236         }
2237         dev_dbg(bridge, "%s: hnode: %p event_mask: 0x%x notify_type: 0x%x "
2238                 "hnotification: %p status 0x%x\n", __func__, hnode,
2239                 event_mask, notify_type, hnotification, status);
2240         return status;
2241 }
2242
2243 /*
2244  *  ======== node_run ========
2245  *  Purpose:
2246  *      Start execution of a node's execute phase, or resume execution of a node
2247  *      that has been suspended (via NODE_NodePause()) on the DSP. Load the
2248  *      node's execute function if necessary.
2249  */
2250 int node_run(struct node_object *hnode)
2251 {
2252         struct node_object *pnode = (struct node_object *)hnode;
2253         struct node_mgr *hnode_mgr;
2254         enum node_type node_type;
2255         enum node_state state;
2256         u32 ul_execute_fxn;
2257         u32 ul_fxn_addr;
2258         int status = 0;
2259         u32 proc_id;
2260         struct bridge_drv_interface *intf_fxns;
2261         struct dsp_processorstate proc_state;
2262         struct proc_object *hprocessor;
2263
2264         DBC_REQUIRE(refs > 0);
2265
2266         if (!hnode) {
2267                 status = -EFAULT;
2268                 goto func_end;
2269         }
2270         hprocessor = hnode->hprocessor;
2271         status = proc_get_state(hprocessor, &proc_state,
2272                                 sizeof(struct dsp_processorstate));
2273         if (DSP_FAILED(status))
2274                 goto func_end;
2275         /* If processor is in error state then don't attempt to run the node */
2276         if (proc_state.proc_state == PROC_ERROR) {
2277                 status = -EPERM;
2278                 goto func_end;
2279         }
2280         node_type = node_get_type(hnode);
2281         if (node_type == NODE_DEVICE)
2282                 status = -EPERM;
2283         if (DSP_FAILED(status))
2284                 goto func_end;
2285
2286         hnode_mgr = hnode->hnode_mgr;
2287         if (!hnode_mgr) {
2288                 status = -EFAULT;
2289                 goto func_end;
2290         }
2291         intf_fxns = hnode_mgr->intf_fxns;
2292         /* Enter critical section */
2293         mutex_lock(&hnode_mgr->node_mgr_lock);
2294
2295         state = node_get_state(hnode);
2296         if (state != NODE_CREATED && state != NODE_PAUSED)
2297                 status = -EBADR;
2298
2299         if (DSP_SUCCEEDED(status))
2300                 status = proc_get_processor_id(pnode->hprocessor, &proc_id);
2301
2302         if (DSP_FAILED(status))
2303                 goto func_cont1;
2304
2305         if ((proc_id != DSP_UNIT) && (proc_id != IVA_UNIT))
2306                 goto func_cont1;
2307
2308         if (state == NODE_CREATED) {
2309                 /* If node's execute function is not loaded, load it */
2310                 if (!(hnode->loaded) && hnode->phase_split) {
2311                         status =
2312                             hnode_mgr->nldr_fxns.pfn_load(hnode->nldr_node_obj,
2313                                                           NLDR_EXECUTE);
2314                         if (DSP_SUCCEEDED(status)) {
2315                                 hnode->loaded = true;
2316                         } else {
2317                                 pr_err("%s: fail - load execute code: 0x%x\n",
2318                                        __func__, status);
2319                         }
2320                 }
2321                 if (DSP_SUCCEEDED(status)) {
2322                         /* Get address of node's execute function */
2323                         if (proc_id == IVA_UNIT)
2324                                 ul_execute_fxn = (u32) hnode->node_env;
2325                         else {
2326                                 status = get_fxn_address(hnode, &ul_execute_fxn,
2327                                                          EXECUTEPHASE);
2328                         }
2329                 }
2330                 if (DSP_SUCCEEDED(status)) {
2331                         ul_fxn_addr = hnode_mgr->ul_fxn_addrs[RMSEXECUTENODE];
2332                         status =
2333                             disp_node_run(hnode_mgr->disp_obj, hnode,
2334                                           ul_fxn_addr, ul_execute_fxn,
2335                                           hnode->node_env);
2336                 }
2337         } else if (state == NODE_PAUSED) {
2338                 ul_fxn_addr = hnode_mgr->ul_fxn_addrs[RMSCHANGENODEPRIORITY];
2339                 status = disp_node_change_priority(hnode_mgr->disp_obj, hnode,
2340                                                    ul_fxn_addr, hnode->node_env,
2341                                                    NODE_GET_PRIORITY(hnode));
2342         } else {
2343                 /* We should never get here */
2344                 DBC_ASSERT(false);
2345         }
2346 func_cont1:
2347         /* Update node state. */
2348         if (DSP_SUCCEEDED(status))
2349                 NODE_SET_STATE(hnode, NODE_RUNNING);
2350         else                    /* Set state back to previous value */
2351                 NODE_SET_STATE(hnode, state);
2352         /*End of sync_enter_cs */
2353         /* Exit critical section */
2354         mutex_unlock(&hnode_mgr->node_mgr_lock);
2355         if (DSP_SUCCEEDED(status)) {
2356                 proc_notify_clients(hnode->hprocessor, DSP_NODESTATECHANGE);
2357                 ntfy_notify(hnode->ntfy_obj, DSP_NODESTATECHANGE);
2358         }
2359 func_end:
2360         dev_dbg(bridge, "%s: hnode: %p status 0x%x\n", __func__, hnode, status);
2361         return status;
2362 }
2363
2364 /*
2365  *  ======== node_terminate ========
2366  *  Purpose:
2367  *      Signal a node running on the DSP that it should exit its execute phase
2368  *      function.
2369  */
2370 int node_terminate(struct node_object *hnode, OUT int *pstatus)
2371 {
2372         struct node_object *pnode = (struct node_object *)hnode;
2373         struct node_mgr *hnode_mgr = NULL;
2374         enum node_type node_type;
2375         struct bridge_drv_interface *intf_fxns;
2376         enum node_state state;
2377         struct dsp_msg msg, killmsg;
2378         int status = 0;
2379         u32 proc_id, kill_time_out;
2380         struct deh_mgr *hdeh_mgr;
2381         struct dsp_processorstate proc_state;
2382
2383         DBC_REQUIRE(refs > 0);
2384         DBC_REQUIRE(pstatus != NULL);
2385
2386         if (!hnode || !hnode->hnode_mgr) {
2387                 status = -EFAULT;
2388                 goto func_end;
2389         }
2390         if (pnode->hprocessor == NULL) {
2391                 status = -EFAULT;
2392                 goto func_end;
2393         }
2394         status = proc_get_processor_id(pnode->hprocessor, &proc_id);
2395
2396         if (DSP_SUCCEEDED(status)) {
2397                 hnode_mgr = hnode->hnode_mgr;
2398                 node_type = node_get_type(hnode);
2399                 if (node_type != NODE_TASK && node_type != NODE_DAISSOCKET)
2400                         status = -EPERM;
2401         }
2402         if (DSP_SUCCEEDED(status)) {
2403                 /* Check node state */
2404                 mutex_lock(&hnode_mgr->node_mgr_lock);
2405                 state = node_get_state(hnode);
2406                 if (state != NODE_RUNNING) {
2407                         status = -EBADR;
2408                         /* Set the exit status if node terminated on
2409                          * its own. */
2410                         if (state == NODE_DONE)
2411                                 *pstatus = hnode->exit_status;
2412
2413                 } else {
2414                         NODE_SET_STATE(hnode, NODE_TERMINATING);
2415                 }
2416                 /* end of sync_enter_cs */
2417                 mutex_unlock(&hnode_mgr->node_mgr_lock);
2418         }
2419         if (DSP_SUCCEEDED(status)) {
2420                 /*
2421                  *  Send exit message. Do not change state to NODE_DONE
2422                  *  here. That will be done in callback.
2423                  */
2424                 status = proc_get_state(pnode->hprocessor, &proc_state,
2425                                         sizeof(struct dsp_processorstate));
2426                 if (DSP_FAILED(status))
2427                         goto func_cont;
2428                 /* If processor is in error state then don't attempt to send
2429                  * A kill task command */
2430                 if (proc_state.proc_state == PROC_ERROR) {
2431                         status = -EPERM;
2432                         goto func_cont;
2433                 }
2434
2435                 msg.dw_cmd = RMS_EXIT;
2436                 msg.dw_arg1 = hnode->node_env;
2437                 killmsg.dw_cmd = RMS_KILLTASK;
2438                 killmsg.dw_arg1 = hnode->node_env;
2439                 intf_fxns = hnode_mgr->intf_fxns;
2440
2441                 if (hnode->utimeout > MAXTIMEOUT)
2442                         kill_time_out = MAXTIMEOUT;
2443                 else
2444                         kill_time_out = (hnode->utimeout) * 2;
2445
2446                 status = (*intf_fxns->pfn_msg_put) (hnode->msg_queue_obj, &msg,
2447                                                     hnode->utimeout);
2448                 if (DSP_FAILED(status))
2449                         goto func_cont;
2450
2451                 /*
2452                  * Wait on synchronization object that will be
2453                  * posted in the callback on receiving RMS_EXIT
2454                  * message, or by node_delete. Check for valid hnode,
2455                  * in case posted by node_delete().
2456                  */
2457                 status = sync_wait_on_event(hnode->sync_done,
2458                                             kill_time_out / 2);
2459                 if (status != ETIME)
2460                         goto func_cont;
2461
2462                 status = (*intf_fxns->pfn_msg_put)(hnode->msg_queue_obj,
2463                                                 &killmsg, hnode->utimeout);
2464                 if (DSP_FAILED(status))
2465                         goto func_cont;
2466                 status = sync_wait_on_event(hnode->sync_done,
2467                                              kill_time_out / 2);
2468                 if (DSP_FAILED(status)) {
2469                         /*
2470                          * Here it goes the part of the simulation of
2471                          * the DSP exception.
2472                          */
2473                         dev_get_deh_mgr(hnode_mgr->hdev_obj, &hdeh_mgr);
2474                         if (!hdeh_mgr)
2475                                 goto func_cont;
2476
2477                         bridge_deh_notify(hdeh_mgr, DSP_SYSERROR, DSP_EXCEPTIONABORT);
2478                 }
2479         }
2480 func_cont:
2481         if (DSP_SUCCEEDED(status)) {
2482                 /* Enter CS before getting exit status, in case node was
2483                  * deleted. */
2484                 mutex_lock(&hnode_mgr->node_mgr_lock);
2485                 /* Make sure node wasn't deleted while we blocked */
2486                 if (!hnode) {
2487                         status = -EPERM;
2488                 } else {
2489                         *pstatus = hnode->exit_status;
2490                         dev_dbg(bridge, "%s: hnode: %p env 0x%x status 0x%x\n",
2491                                 __func__, hnode, hnode->node_env, status);
2492                 }
2493                 mutex_unlock(&hnode_mgr->node_mgr_lock);
2494         }                       /*End of sync_enter_cs */
2495 func_end:
2496         return status;
2497 }
2498
2499 /*
2500  *  ======== delete_node ========
2501  *  Purpose:
2502  *      Free GPP resources allocated in node_allocate() or node_connect().
2503  */
2504 static void delete_node(struct node_object *hnode,
2505                         struct process_context *pr_ctxt)
2506 {
2507         struct node_mgr *hnode_mgr;
2508         struct cmm_xlatorobject *xlator;
2509         struct bridge_drv_interface *intf_fxns;
2510         u32 i;
2511         enum node_type node_type;
2512         struct stream_chnl stream;
2513         struct node_msgargs node_msg_args;
2514         struct node_taskargs task_arg_obj;
2515 #ifdef DSP_DMM_DEBUG
2516         struct dmm_object *dmm_mgr;
2517         struct proc_object *p_proc_object =
2518             (struct proc_object *)hnode->hprocessor;
2519 #endif
2520         int status;
2521         if (!hnode)
2522                 goto func_end;
2523         hnode_mgr = hnode->hnode_mgr;
2524         if (!hnode_mgr)
2525                 goto func_end;
2526         xlator = hnode->xlator;
2527         node_type = node_get_type(hnode);
2528         if (node_type != NODE_DEVICE) {
2529                 node_msg_args = hnode->create_args.asa.node_msg_args;
2530                 kfree(node_msg_args.pdata);
2531
2532                 /* Free msg_ctrl queue */
2533                 if (hnode->msg_queue_obj) {
2534                         intf_fxns = hnode_mgr->intf_fxns;
2535                         (*intf_fxns->pfn_msg_delete_queue) (hnode->
2536                                                             msg_queue_obj);
2537                         hnode->msg_queue_obj = NULL;
2538                 }
2539
2540                 kfree(hnode->sync_done);
2541
2542                 /* Free all stream info */
2543                 if (hnode->inputs) {
2544                         for (i = 0; i < MAX_INPUTS(hnode); i++) {
2545                                 stream = hnode->inputs[i];
2546                                 free_stream(hnode_mgr, stream);
2547                         }
2548                         kfree(hnode->inputs);
2549                         hnode->inputs = NULL;
2550                 }
2551                 if (hnode->outputs) {
2552                         for (i = 0; i < MAX_OUTPUTS(hnode); i++) {
2553                                 stream = hnode->outputs[i];
2554                                 free_stream(hnode_mgr, stream);
2555                         }
2556                         kfree(hnode->outputs);
2557                         hnode->outputs = NULL;
2558                 }
2559                 task_arg_obj = hnode->create_args.asa.task_arg_obj;
2560                 if (task_arg_obj.strm_in_def) {
2561                         for (i = 0; i < MAX_INPUTS(hnode); i++) {
2562                                 kfree(task_arg_obj.strm_in_def[i].sz_device);
2563                                 task_arg_obj.strm_in_def[i].sz_device = NULL;
2564                         }
2565                         kfree(task_arg_obj.strm_in_def);
2566                         task_arg_obj.strm_in_def = NULL;
2567                 }
2568                 if (task_arg_obj.strm_out_def) {
2569                         for (i = 0; i < MAX_OUTPUTS(hnode); i++) {
2570                                 kfree(task_arg_obj.strm_out_def[i].sz_device);
2571                                 task_arg_obj.strm_out_def[i].sz_device = NULL;
2572                         }
2573                         kfree(task_arg_obj.strm_out_def);
2574                         task_arg_obj.strm_out_def = NULL;
2575                 }
2576                 if (task_arg_obj.udsp_heap_res_addr) {
2577                         status = proc_un_map(hnode->hprocessor, (void *)
2578                                              task_arg_obj.udsp_heap_addr,
2579                                              pr_ctxt);
2580
2581                         status = proc_un_reserve_memory(hnode->hprocessor,
2582                                                         (void *)
2583                                                         task_arg_obj.
2584                                                         udsp_heap_res_addr,
2585                                                         pr_ctxt);
2586 #ifdef DSP_DMM_DEBUG
2587                         status = dmm_get_handle(p_proc_object, &dmm_mgr);
2588                         if (dmm_mgr)
2589                                 dmm_mem_map_dump(dmm_mgr);
2590                         else
2591                                 status = DSP_EHANDLE;
2592 #endif
2593                 }
2594         }
2595         if (node_type != NODE_MESSAGE) {
2596                 kfree(hnode->stream_connect);
2597                 hnode->stream_connect = NULL;
2598         }
2599         kfree(hnode->pstr_dev_name);
2600         hnode->pstr_dev_name = NULL;
2601
2602         if (hnode->ntfy_obj) {
2603                 ntfy_delete(hnode->ntfy_obj);
2604                 kfree(hnode->ntfy_obj);
2605                 hnode->ntfy_obj = NULL;
2606         }
2607
2608         /* These were allocated in dcd_get_object_def (via node_allocate) */
2609         kfree(hnode->dcd_props.obj_data.node_obj.pstr_create_phase_fxn);
2610         hnode->dcd_props.obj_data.node_obj.pstr_create_phase_fxn = NULL;
2611
2612         kfree(hnode->dcd_props.obj_data.node_obj.pstr_execute_phase_fxn);
2613         hnode->dcd_props.obj_data.node_obj.pstr_execute_phase_fxn = NULL;
2614
2615         kfree(hnode->dcd_props.obj_data.node_obj.pstr_delete_phase_fxn);
2616         hnode->dcd_props.obj_data.node_obj.pstr_delete_phase_fxn = NULL;
2617
2618         kfree(hnode->dcd_props.obj_data.node_obj.pstr_i_alg_name);
2619         hnode->dcd_props.obj_data.node_obj.pstr_i_alg_name = NULL;
2620
2621         /* Free all SM address translator resources */
2622         if (xlator) {
2623                 (void)cmm_xlator_delete(xlator, TRUE);  /* force free */
2624                 xlator = NULL;
2625         }
2626
2627         kfree(hnode->nldr_node_obj);
2628         hnode->nldr_node_obj = NULL;
2629         hnode->hnode_mgr = NULL;
2630         kfree(hnode);
2631         hnode = NULL;
2632 func_end:
2633         return;
2634 }
2635
2636 /*
2637  *  ======== delete_node_mgr ========
2638  *  Purpose:
2639  *      Frees the node manager.
2640  */
2641 static void delete_node_mgr(struct node_mgr *hnode_mgr)
2642 {
2643         struct node_object *hnode;
2644
2645         if (hnode_mgr) {
2646                 /* Free resources */
2647                 if (hnode_mgr->hdcd_mgr)
2648                         dcd_destroy_manager(hnode_mgr->hdcd_mgr);
2649
2650                 /* Remove any elements remaining in lists */
2651                 if (hnode_mgr->node_list) {
2652                         while ((hnode = (struct node_object *)
2653                                 lst_get_head(hnode_mgr->node_list)))
2654                                 delete_node(hnode, NULL);
2655
2656                         DBC_ASSERT(LST_IS_EMPTY(hnode_mgr->node_list));
2657                         kfree(hnode_mgr->node_list);
2658                 }
2659                 mutex_destroy(&hnode_mgr->node_mgr_lock);
2660                 if (hnode_mgr->ntfy_obj) {
2661                         ntfy_delete(hnode_mgr->ntfy_obj);
2662                         kfree(hnode_mgr->ntfy_obj);
2663                 }
2664
2665                 if (hnode_mgr->pipe_map)
2666                         gb_delete(hnode_mgr->pipe_map);
2667
2668                 if (hnode_mgr->pipe_done_map)
2669                         gb_delete(hnode_mgr->pipe_done_map);
2670
2671                 if (hnode_mgr->chnl_map)
2672                         gb_delete(hnode_mgr->chnl_map);
2673
2674                 if (hnode_mgr->dma_chnl_map)
2675                         gb_delete(hnode_mgr->dma_chnl_map);
2676
2677                 if (hnode_mgr->zc_chnl_map)
2678                         gb_delete(hnode_mgr->zc_chnl_map);
2679
2680                 if (hnode_mgr->disp_obj)
2681                         disp_delete(hnode_mgr->disp_obj);
2682
2683                 if (hnode_mgr->strm_mgr_obj)
2684                         strm_delete(hnode_mgr->strm_mgr_obj);
2685
2686                 /* Delete the loader */
2687                 if (hnode_mgr->nldr_obj)
2688                         hnode_mgr->nldr_fxns.pfn_delete(hnode_mgr->nldr_obj);
2689
2690                 if (hnode_mgr->loader_init)
2691                         hnode_mgr->nldr_fxns.pfn_exit();
2692
2693                 kfree(hnode_mgr);
2694         }
2695 }
2696
2697 /*
2698  *  ======== fill_stream_connect ========
2699  *  Purpose:
2700  *      Fills stream information.
2701  */
2702 static void fill_stream_connect(struct node_object *node1,
2703                                 struct node_object *node2,
2704                                 u32 uStream1, u32 uStream2)
2705 {
2706         u32 strm_index;
2707         struct dsp_streamconnect *strm1 = NULL;
2708         struct dsp_streamconnect *strm2 = NULL;
2709         enum node_type node1_type = NODE_TASK;
2710         enum node_type node2_type = NODE_TASK;
2711
2712         node1_type = node_get_type(node1);
2713         node2_type = node_get_type(node2);
2714         if (node1 != (struct node_object *)DSP_HGPPNODE) {
2715
2716                 if (node1_type != NODE_DEVICE) {
2717                         strm_index = node1->num_inputs +
2718                             node1->num_outputs - 1;
2719                         strm1 = &(node1->stream_connect[strm_index]);
2720                         strm1->cb_struct = sizeof(struct dsp_streamconnect);
2721                         strm1->this_node_stream_index = uStream1;
2722                 }
2723
2724                 if (node2 != (struct node_object *)DSP_HGPPNODE) {
2725                         /* NODE == > NODE */
2726                         if (node1_type != NODE_DEVICE) {
2727                                 strm1->connected_node = node2;
2728                                 strm1->ui_connected_node_id = node2->node_uuid;
2729                                 strm1->connected_node_stream_index = uStream2;
2730                                 strm1->connect_type = CONNECTTYPE_NODEOUTPUT;
2731                         }
2732                         if (node2_type != NODE_DEVICE) {
2733                                 strm_index = node2->num_inputs +
2734                                     node2->num_outputs - 1;
2735                                 strm2 = &(node2->stream_connect[strm_index]);
2736                                 strm2->cb_struct =
2737                                     sizeof(struct dsp_streamconnect);
2738                                 strm2->this_node_stream_index = uStream2;
2739                                 strm2->connected_node = node1;
2740                                 strm2->ui_connected_node_id = node1->node_uuid;
2741                                 strm2->connected_node_stream_index = uStream1;
2742                                 strm2->connect_type = CONNECTTYPE_NODEINPUT;
2743                         }
2744                 } else if (node1_type != NODE_DEVICE)
2745                         strm1->connect_type = CONNECTTYPE_GPPOUTPUT;
2746         } else {
2747                 /* GPP == > NODE */
2748                 DBC_ASSERT(node2 != (struct node_object *)DSP_HGPPNODE);
2749                 strm_index = node2->num_inputs + node2->num_outputs - 1;
2750                 strm2 = &(node2->stream_connect[strm_index]);
2751                 strm2->cb_struct = sizeof(struct dsp_streamconnect);
2752                 strm2->this_node_stream_index = uStream2;
2753                 strm2->connect_type = CONNECTTYPE_GPPINPUT;
2754         }
2755 }
2756
2757 /*
2758  *  ======== fill_stream_def ========
2759  *  Purpose:
2760  *      Fills Stream attributes.
2761  */
2762 static void fill_stream_def(struct node_object *hnode,
2763                             struct node_strmdef *pstrm_def,
2764                             struct dsp_strmattr *pattrs)
2765 {
2766         struct node_mgr *hnode_mgr = hnode->hnode_mgr;
2767
2768         if (pattrs != NULL) {
2769                 pstrm_def->num_bufs = pattrs->num_bufs;
2770                 pstrm_def->buf_size =
2771                     pattrs->buf_size / hnode_mgr->udsp_data_mau_size;
2772                 pstrm_def->seg_id = pattrs->seg_id;
2773                 pstrm_def->buf_alignment = pattrs->buf_alignment;
2774                 pstrm_def->utimeout = pattrs->utimeout;
2775         } else {
2776                 pstrm_def->num_bufs = DEFAULTNBUFS;
2777                 pstrm_def->buf_size =
2778                     DEFAULTBUFSIZE / hnode_mgr->udsp_data_mau_size;
2779                 pstrm_def->seg_id = DEFAULTSEGID;
2780                 pstrm_def->buf_alignment = DEFAULTALIGNMENT;
2781                 pstrm_def->utimeout = DEFAULTTIMEOUT;
2782         }
2783 }
2784
2785 /*
2786  *  ======== free_stream ========
2787  *  Purpose:
2788  *      Updates the channel mask and frees the pipe id.
2789  */
2790 static void free_stream(struct node_mgr *hnode_mgr, struct stream_chnl stream)
2791 {
2792         /* Free up the pipe id unless other node has not yet been deleted. */
2793         if (stream.type == NODECONNECT) {
2794                 if (gb_test(hnode_mgr->pipe_done_map, stream.dev_id)) {
2795                         /* The other node has already been deleted */
2796                         gb_clear(hnode_mgr->pipe_done_map, stream.dev_id);
2797                         gb_clear(hnode_mgr->pipe_map, stream.dev_id);
2798                 } else {
2799                         /* The other node has not been deleted yet */
2800                         gb_set(hnode_mgr->pipe_done_map, stream.dev_id);
2801                 }
2802         } else if (stream.type == HOSTCONNECT) {
2803                 if (stream.dev_id < hnode_mgr->ul_num_chnls) {
2804                         gb_clear(hnode_mgr->chnl_map, stream.dev_id);
2805                 } else if (stream.dev_id < (2 * hnode_mgr->ul_num_chnls)) {
2806                         /* dsp-dma */
2807                         gb_clear(hnode_mgr->dma_chnl_map, stream.dev_id -
2808                                  (1 * hnode_mgr->ul_num_chnls));
2809                 } else if (stream.dev_id < (3 * hnode_mgr->ul_num_chnls)) {
2810                         /* zero-copy */
2811                         gb_clear(hnode_mgr->zc_chnl_map, stream.dev_id -
2812                                  (2 * hnode_mgr->ul_num_chnls));
2813                 }
2814         }
2815 }
2816
2817 /*
2818  *  ======== get_fxn_address ========
2819  *  Purpose:
2820  *      Retrieves the address for create, execute or delete phase for a node.
2821  */
2822 static int get_fxn_address(struct node_object *hnode, u32 * pulFxnAddr,
2823                                   u32 uPhase)
2824 {
2825         char *pstr_fxn_name = NULL;
2826         struct node_mgr *hnode_mgr = hnode->hnode_mgr;
2827         int status = 0;
2828         DBC_REQUIRE(node_get_type(hnode) == NODE_TASK ||
2829                     node_get_type(hnode) == NODE_DAISSOCKET ||
2830                     node_get_type(hnode) == NODE_MESSAGE);
2831
2832         switch (uPhase) {
2833         case CREATEPHASE:
2834                 pstr_fxn_name =
2835                     hnode->dcd_props.obj_data.node_obj.pstr_create_phase_fxn;
2836                 break;
2837         case EXECUTEPHASE:
2838                 pstr_fxn_name =
2839                     hnode->dcd_props.obj_data.node_obj.pstr_execute_phase_fxn;
2840                 break;
2841         case DELETEPHASE:
2842                 pstr_fxn_name =
2843                     hnode->dcd_props.obj_data.node_obj.pstr_delete_phase_fxn;
2844                 break;
2845         default:
2846                 /* Should never get here */
2847                 DBC_ASSERT(false);
2848                 break;
2849         }
2850
2851         status =
2852             hnode_mgr->nldr_fxns.pfn_get_fxn_addr(hnode->nldr_node_obj,
2853                                                   pstr_fxn_name, pulFxnAddr);
2854
2855         return status;
2856 }
2857
2858 /*
2859  *  ======== get_node_info ========
2860  *  Purpose:
2861  *      Retrieves the node information.
2862  */
2863 void get_node_info(struct node_object *hnode, struct dsp_nodeinfo *node_info)
2864 {
2865         u32 i;
2866
2867         DBC_REQUIRE(hnode);
2868         DBC_REQUIRE(node_info != NULL);
2869
2870         node_info->cb_struct = sizeof(struct dsp_nodeinfo);
2871         node_info->nb_node_database_props =
2872             hnode->dcd_props.obj_data.node_obj.ndb_props;
2873         node_info->execution_priority = hnode->prio;
2874         node_info->device_owner = hnode->device_owner;
2875         node_info->number_streams = hnode->num_inputs + hnode->num_outputs;
2876         node_info->node_env = hnode->node_env;
2877
2878         node_info->ns_execution_state = node_get_state(hnode);
2879
2880         /* Copy stream connect data */
2881         for (i = 0; i < hnode->num_inputs + hnode->num_outputs; i++)
2882                 node_info->sc_stream_connection[i] = hnode->stream_connect[i];
2883
2884 }
2885
2886 /*
2887  *  ======== get_node_props ========
2888  *  Purpose:
2889  *      Retrieve node properties.
2890  */
2891 static int get_node_props(struct dcd_manager *hdcd_mgr,
2892                                  struct node_object *hnode,
2893                                  CONST struct dsp_uuid *node_uuid,
2894                                  struct dcd_genericobj *dcd_prop)
2895 {
2896         u32 len;
2897         struct node_msgargs *pmsg_args;
2898         struct node_taskargs *task_arg_obj;
2899         enum node_type node_type = NODE_TASK;
2900         struct dsp_ndbprops *pndb_props =
2901             &(dcd_prop->obj_data.node_obj.ndb_props);
2902         int status = 0;
2903         char sz_uuid[MAXUUIDLEN];
2904
2905         status = dcd_get_object_def(hdcd_mgr, (struct dsp_uuid *)node_uuid,
2906                                     DSP_DCDNODETYPE, dcd_prop);
2907
2908         if (DSP_SUCCEEDED(status)) {
2909                 hnode->ntype = node_type = pndb_props->ntype;
2910
2911                 /* Create UUID value to set in registry. */
2912                 uuid_uuid_to_string((struct dsp_uuid *)node_uuid, sz_uuid,
2913                                     MAXUUIDLEN);
2914                 dev_dbg(bridge, "(node) UUID: %s\n", sz_uuid);
2915
2916                 /* Fill in message args that come from NDB */
2917                 if (node_type != NODE_DEVICE) {
2918                         pmsg_args = &(hnode->create_args.asa.node_msg_args);
2919                         pmsg_args->seg_id =
2920                             dcd_prop->obj_data.node_obj.msg_segid;
2921                         pmsg_args->notify_type =
2922                             dcd_prop->obj_data.node_obj.msg_notify_type;
2923                         pmsg_args->max_msgs = pndb_props->message_depth;
2924                         dev_dbg(bridge, "(node) Max Number of Messages: 0x%x\n",
2925                                 pmsg_args->max_msgs);
2926                 } else {
2927                         /* Copy device name */
2928                         DBC_REQUIRE(pndb_props->ac_name);
2929                         len = strlen(pndb_props->ac_name);
2930                         DBC_ASSERT(len < MAXDEVNAMELEN);
2931                         hnode->pstr_dev_name = kzalloc(len + 1, GFP_KERNEL);
2932                         if (hnode->pstr_dev_name == NULL) {
2933                                 status = -ENOMEM;
2934                         } else {
2935                                 strncpy(hnode->pstr_dev_name,
2936                                         pndb_props->ac_name, len);
2937                         }
2938                 }
2939         }
2940         if (DSP_SUCCEEDED(status)) {
2941                 /* Fill in create args that come from NDB */
2942                 if (node_type == NODE_TASK || node_type == NODE_DAISSOCKET) {
2943                         task_arg_obj = &(hnode->create_args.asa.task_arg_obj);
2944                         task_arg_obj->prio = pndb_props->prio;
2945                         task_arg_obj->stack_size = pndb_props->stack_size;
2946                         task_arg_obj->sys_stack_size =
2947                             pndb_props->sys_stack_size;
2948                         task_arg_obj->stack_seg = pndb_props->stack_seg;
2949                         dev_dbg(bridge, "(node) Priority: 0x%x Stack Size: "
2950                                 "0x%x words System Stack Size: 0x%x words "
2951                                 "Stack Segment: 0x%x profile count : 0x%x\n",
2952                                 task_arg_obj->prio, task_arg_obj->stack_size,
2953                                 task_arg_obj->sys_stack_size,
2954                                 task_arg_obj->stack_seg,
2955                                 pndb_props->count_profiles);
2956                 }
2957         }
2958
2959         return status;
2960 }
2961
2962 /*
2963  *  ======== get_proc_props ========
2964  *  Purpose:
2965  *      Retrieve the processor properties.
2966  */
2967 static int get_proc_props(struct node_mgr *hnode_mgr,
2968                                  struct dev_object *hdev_obj)
2969 {
2970         struct cfg_hostres *host_res;
2971         struct bridge_dev_context *pbridge_context;
2972         int status = 0;
2973
2974         status = dev_get_bridge_context(hdev_obj, &pbridge_context);
2975         if (!pbridge_context)
2976                 status = -EFAULT;
2977
2978         if (DSP_SUCCEEDED(status)) {
2979                 host_res = pbridge_context->resources;
2980                 if (!host_res)
2981                         return -EPERM;
2982                 hnode_mgr->ul_chnl_offset = host_res->dw_chnl_offset;
2983                 hnode_mgr->ul_chnl_buf_size = host_res->dw_chnl_buf_size;
2984                 hnode_mgr->ul_num_chnls = host_res->dw_num_chnls;
2985
2986                 /*
2987                  *  PROC will add an API to get dsp_processorinfo.
2988                  *  Fill in default values for now.
2989                  */
2990                 /* TODO -- Instead of hard coding, take from registry */
2991                 hnode_mgr->proc_family = 6000;
2992                 hnode_mgr->proc_type = 6410;
2993                 hnode_mgr->min_pri = DSP_NODE_MIN_PRIORITY;
2994                 hnode_mgr->max_pri = DSP_NODE_MAX_PRIORITY;
2995                 hnode_mgr->udsp_word_size = DSPWORDSIZE;
2996                 hnode_mgr->udsp_data_mau_size = DSPWORDSIZE;
2997                 hnode_mgr->udsp_mau_size = 1;
2998
2999         }
3000         return status;
3001 }
3002
3003 /*
3004  *  ======== node_get_uuid_props ========
3005  *  Purpose:
3006  *      Fetch Node UUID properties from DCD/DOF file.
3007  */
3008 int node_get_uuid_props(void *hprocessor,
3009                                IN CONST struct dsp_uuid *node_uuid,
3010                                OUT struct dsp_ndbprops *node_props)
3011 {
3012         struct node_mgr *hnode_mgr = NULL;
3013         struct dev_object *hdev_obj;
3014         int status = 0;
3015         struct dcd_nodeprops dcd_node_props;
3016         struct dsp_processorstate proc_state;
3017
3018         DBC_REQUIRE(refs > 0);
3019         DBC_REQUIRE(hprocessor != NULL);
3020         DBC_REQUIRE(node_uuid != NULL);
3021
3022         if (hprocessor == NULL || node_uuid == NULL) {
3023                 status = -EFAULT;
3024                 goto func_end;
3025         }
3026         status = proc_get_state(hprocessor, &proc_state,
3027                                 sizeof(struct dsp_processorstate));
3028         if (DSP_FAILED(status))
3029                 goto func_end;
3030         /* If processor is in error state then don't attempt
3031            to send the message */
3032         if (proc_state.proc_state == PROC_ERROR) {
3033                 status = -EPERM;
3034                 goto func_end;
3035         }
3036
3037         status = proc_get_dev_object(hprocessor, &hdev_obj);
3038         if (hdev_obj) {
3039                 status = dev_get_node_manager(hdev_obj, &hnode_mgr);
3040                 if (hnode_mgr == NULL) {
3041                         status = -EFAULT;
3042                         goto func_end;
3043                 }
3044         }
3045
3046         /*
3047          * Enter the critical section. This is needed because
3048          * dcd_get_object_def will ultimately end up calling dbll_open/close,
3049          * which needs to be protected in order to not corrupt the zlib manager
3050          * (COD).
3051          */
3052         mutex_lock(&hnode_mgr->node_mgr_lock);
3053
3054         dcd_node_props.pstr_create_phase_fxn = NULL;
3055         dcd_node_props.pstr_execute_phase_fxn = NULL;
3056         dcd_node_props.pstr_delete_phase_fxn = NULL;
3057         dcd_node_props.pstr_i_alg_name = NULL;
3058
3059         status = dcd_get_object_def(hnode_mgr->hdcd_mgr,
3060                 (struct dsp_uuid *)node_uuid, DSP_DCDNODETYPE,
3061                 (struct dcd_genericobj *)&dcd_node_props);
3062
3063         if (DSP_SUCCEEDED(status)) {
3064                 *node_props = dcd_node_props.ndb_props;
3065                 kfree(dcd_node_props.pstr_create_phase_fxn);
3066
3067                 kfree(dcd_node_props.pstr_execute_phase_fxn);
3068
3069                 kfree(dcd_node_props.pstr_delete_phase_fxn);
3070
3071                 kfree(dcd_node_props.pstr_i_alg_name);
3072         }
3073         /*  Leave the critical section, we're done. */
3074         mutex_unlock(&hnode_mgr->node_mgr_lock);
3075 func_end:
3076         return status;
3077 }
3078
3079 /*
3080  *  ======== get_rms_fxns ========
3081  *  Purpose:
3082  *      Retrieve the RMS functions.
3083  */
3084 static int get_rms_fxns(struct node_mgr *hnode_mgr)
3085 {
3086         s32 i;
3087         struct dev_object *dev_obj = hnode_mgr->hdev_obj;
3088         int status = 0;
3089
3090         static char *psz_fxns[NUMRMSFXNS] = {
3091                 "RMS_queryServer",      /* RMSQUERYSERVER */
3092                 "RMS_configureServer",  /* RMSCONFIGURESERVER */
3093                 "RMS_createNode",       /* RMSCREATENODE */
3094                 "RMS_executeNode",      /* RMSEXECUTENODE */
3095                 "RMS_deleteNode",       /* RMSDELETENODE */
3096                 "RMS_changeNodePriority",       /* RMSCHANGENODEPRIORITY */
3097                 "RMS_readMemory",       /* RMSREADMEMORY */
3098                 "RMS_writeMemory",      /* RMSWRITEMEMORY */
3099                 "RMS_copy",     /* RMSCOPY */
3100         };
3101
3102         for (i = 0; i < NUMRMSFXNS; i++) {
3103                 status = dev_get_symbol(dev_obj, psz_fxns[i],
3104                                         &(hnode_mgr->ul_fxn_addrs[i]));
3105                 if (DSP_FAILED(status)) {
3106                         if (status == -ESPIPE) {
3107                                 /*
3108                                  *  May be loaded dynamically (in the future),
3109                                  *  but return an error for now.
3110                                  */
3111                                 dev_dbg(bridge, "%s: RMS function: %s currently"
3112                                         " not loaded\n", __func__, psz_fxns[i]);
3113                         } else {
3114                                 dev_dbg(bridge, "%s: Symbol not found: %s "
3115                                         "status = 0x%x\n", __func__,
3116                                         psz_fxns[i], status);
3117                                 break;
3118                         }
3119                 }
3120         }
3121
3122         return status;
3123 }
3124
3125 /*
3126  *  ======== ovly ========
3127  *  Purpose:
3128  *      Called during overlay.Sends command to RMS to copy a block of data.
3129  */
3130 static u32 ovly(void *priv_ref, u32 ulDspRunAddr, u32 ulDspLoadAddr,
3131                 u32 ul_num_bytes, u32 mem_space)
3132 {
3133         struct node_object *hnode = (struct node_object *)priv_ref;
3134         struct node_mgr *hnode_mgr;
3135         u32 ul_bytes = 0;
3136         u32 ul_size;
3137         u32 ul_timeout;
3138         int status = 0;
3139         struct bridge_dev_context *hbridge_context;
3140         /* Function interface to Bridge driver*/
3141         struct bridge_drv_interface *intf_fxns;
3142
3143         DBC_REQUIRE(hnode);
3144
3145         hnode_mgr = hnode->hnode_mgr;
3146
3147         ul_size = ul_num_bytes / hnode_mgr->udsp_word_size;
3148         ul_timeout = hnode->utimeout;
3149
3150         /* Call new MemCopy function */
3151         intf_fxns = hnode_mgr->intf_fxns;
3152         status = dev_get_bridge_context(hnode_mgr->hdev_obj, &hbridge_context);
3153         if (DSP_SUCCEEDED(status)) {
3154                 status =
3155                     (*intf_fxns->pfn_brd_mem_copy) (hbridge_context,
3156                                                 ulDspRunAddr, ulDspLoadAddr,
3157                                                 ul_num_bytes, (u32) mem_space);
3158                 if (DSP_SUCCEEDED(status))
3159                         ul_bytes = ul_num_bytes;
3160                 else
3161                         pr_debug("%s: failed to copy brd memory, status 0x%x\n",
3162                                  __func__, status);
3163         } else {
3164                 pr_debug("%s: failed to get Bridge context, status 0x%x\n",
3165                          __func__, status);
3166         }
3167
3168         return ul_bytes;
3169 }
3170
3171 /*
3172  *  ======== mem_write ========
3173  */
3174 static u32 mem_write(void *priv_ref, u32 ulDspAddr, void *pbuf,
3175                      u32 ul_num_bytes, u32 mem_space)
3176 {
3177         struct node_object *hnode = (struct node_object *)priv_ref;
3178         struct node_mgr *hnode_mgr;
3179         u16 mem_sect_type;
3180         u32 ul_timeout;
3181         int status = 0;
3182         struct bridge_dev_context *hbridge_context;
3183         /* Function interface to Bridge driver */
3184         struct bridge_drv_interface *intf_fxns;
3185
3186         DBC_REQUIRE(hnode);
3187         DBC_REQUIRE(mem_space & DBLL_CODE || mem_space & DBLL_DATA);
3188
3189         hnode_mgr = hnode->hnode_mgr;
3190
3191         ul_timeout = hnode->utimeout;
3192         mem_sect_type = (mem_space & DBLL_CODE) ? RMS_CODE : RMS_DATA;
3193
3194         /* Call new MemWrite function */
3195         intf_fxns = hnode_mgr->intf_fxns;
3196         status = dev_get_bridge_context(hnode_mgr->hdev_obj, &hbridge_context);
3197         status = (*intf_fxns->pfn_brd_mem_write) (hbridge_context, pbuf,
3198                                         ulDspAddr, ul_num_bytes, mem_sect_type);
3199
3200         return ul_num_bytes;
3201 }
3202
3203 #ifdef CONFIG_TIDSPBRIDGE_BACKTRACE
3204 /*
3205  *  ======== node_find_addr ========
3206  */
3207 int node_find_addr(struct node_mgr *node_mgr, u32 sym_addr,
3208                 u32 offset_range, void *sym_addr_output, char *sym_name)
3209 {
3210         struct node_object *node_obj;
3211         int status = -ENOENT;
3212         u32 n;
3213
3214         pr_debug("%s(0x%x, 0x%x, 0x%x, 0x%x,  %s)\n", __func__,
3215                         (unsigned int) node_mgr,
3216                         sym_addr, offset_range,
3217                         (unsigned int) sym_addr_output, sym_name);
3218
3219         node_obj = (struct node_object *)(node_mgr->node_list->head.next);
3220
3221         for (n = 0; n < node_mgr->num_nodes; n++) {
3222                 status = nldr_find_addr(node_obj->nldr_node_obj, sym_addr,
3223                         offset_range, sym_addr_output, sym_name);
3224
3225                 if (DSP_SUCCEEDED(status))
3226                         break;
3227
3228                 node_obj = (struct node_object *) (node_obj->list_elem.next);
3229         }
3230
3231         return status;
3232 }
3233 #endif