]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/tidspbridge/pmgr/cmm.c
Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux
[karo-tx-linux.git] / drivers / staging / tidspbridge / pmgr / cmm.c
1 /*
2  * cmm.c
3  *
4  * DSP-BIOS Bridge driver support functions for TI OMAP processors.
5  *
6  * The Communication(Shared) Memory Management(CMM) module provides
7  * shared memory management services for DSP/BIOS Bridge data streaming
8  * and messaging.
9  *
10  * Multiple shared memory segments can be registered with CMM.
11  * Each registered SM segment is represented by a SM "allocator" that
12  * describes a block of physically contiguous shared memory used for
13  * future allocations by CMM.
14  *
15  * Memory is coalesced back to the appropriate heap when a buffer is
16  * freed.
17  *
18  * Notes:
19  *   Va: Virtual address.
20  *   Pa: Physical or kernel system address.
21  *
22  * Copyright (C) 2005-2006 Texas Instruments, Inc.
23  *
24  * This package is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License version 2 as
26  * published by the Free Software Foundation.
27  *
28  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
29  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
30  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31  */
32 #include <linux/types.h>
33 #include <linux/list.h>
34
35 /*  ----------------------------------- DSP/BIOS Bridge */
36 #include <dspbridge/dbdefs.h>
37
38 /*  ----------------------------------- OS Adaptation Layer */
39 #include <dspbridge/sync.h>
40
41 /*  ----------------------------------- Platform Manager */
42 #include <dspbridge/dev.h>
43 #include <dspbridge/proc.h>
44
45 /*  ----------------------------------- This */
46 #include <dspbridge/cmm.h>
47
48 /*  ----------------------------------- Defines, Data Structures, Typedefs */
49 #define NEXT_PA(pnode)   (pnode->pa + pnode->size)
50
51 /* Other bus/platform translations */
52 #define DSPPA2GPPPA(base, x, y)  ((x)+(y))
53 #define GPPPA2DSPPA(base, x, y)  ((x)-(y))
54
55 /*
56  *  Allocators define a block of contiguous memory used for future allocations.
57  *
58  *      sma - shared memory allocator.
59  *      vma - virtual memory allocator.(not used).
60  */
61 struct cmm_allocator {          /* sma */
62         unsigned int shm_base;  /* Start of physical SM block */
63         u32 sm_size;            /* Size of SM block in bytes */
64         unsigned int vm_base;   /* Start of VM block. (Dev driver
65                                          * context for 'sma') */
66         u32 dsp_phys_addr_offset;       /* DSP PA to GPP PA offset for this
67                                          * SM space */
68         s8 c_factor;            /* DSPPa to GPPPa Conversion Factor */
69         unsigned int dsp_base;  /* DSP virt base byte address */
70         u32 dsp_size;   /* DSP seg size in bytes */
71         struct cmm_object *cmm_mgr;     /* back ref to parent mgr */
72         /* node list of available memory */
73         struct list_head free_list;
74         /* node list of memory in use */
75         struct list_head in_use_list;
76 };
77
78 struct cmm_xlator {             /* Pa<->Va translator object */
79         /* CMM object this translator associated */
80         struct cmm_object *cmm_mgr;
81         /*
82          *  Client process virtual base address that corresponds to phys SM
83          *  base address for translator's seg_id.
84          *  Only 1 segment ID currently supported.
85          */
86         unsigned int virt_base; /* virtual base address */
87         u32 virt_size;          /* size of virt space in bytes */
88         u32 seg_id;             /* Segment Id */
89 };
90
91 /* CMM Mgr */
92 struct cmm_object {
93         /*
94          * Cmm Lock is used to serialize access mem manager for multi-threads.
95          */
96         struct mutex cmm_lock;  /* Lock to access cmm mgr */
97         struct list_head node_free_list;        /* Free list of memory nodes */
98         u32 min_block_size;     /* Min SM block; default 16 bytes */
99         u32 page_size;  /* Memory Page size (1k/4k) */
100         /* GPP SM segment ptrs */
101         struct cmm_allocator *pa_gppsm_seg_tab[CMM_MAXGPPSEGS];
102 };
103
104 /* Default CMM Mgr attributes */
105 static struct cmm_mgrattrs cmm_dfltmgrattrs = {
106         /* min_block_size, min block size(bytes) allocated by cmm mgr */
107         16
108 };
109
110 /* Default allocation attributes */
111 static struct cmm_attrs cmm_dfltalctattrs = {
112         1               /* seg_id, default segment Id for allocator */
113 };
114
115 /* Address translator default attrs */
116 static struct cmm_xlatorattrs cmm_dfltxlatorattrs = {
117         /* seg_id, does not have to match cmm_dfltalctattrs ul_seg_id */
118         1,
119         0,                      /* dsp_bufs */
120         0,                      /* dsp_buf_size */
121         NULL,                   /* vm_base */
122         0,                      /* vm_size */
123 };
124
125 /* SM node representing a block of memory. */
126 struct cmm_mnode {
127         struct list_head link;  /* must be 1st element */
128         u32 pa;         /* Phys addr */
129         u32 va;                 /* Virtual address in device process context */
130         u32 size;               /* SM block size in bytes */
131         u32 client_proc;        /* Process that allocated this mem block */
132 };
133
134 /*  ----------------------------------- Function Prototypes */
135 static void add_to_free_list(struct cmm_allocator *allocator,
136                              struct cmm_mnode *pnode);
137 static struct cmm_allocator *get_allocator(struct cmm_object *cmm_mgr_obj,
138                                            u32 ul_seg_id);
139 static struct cmm_mnode *get_free_block(struct cmm_allocator *allocator,
140                                         u32 usize);
141 static struct cmm_mnode *get_node(struct cmm_object *cmm_mgr_obj, u32 dw_pa,
142                                   u32 dw_va, u32 ul_size);
143 /* get available slot for new allocator */
144 static s32 get_slot(struct cmm_object *cmm_mgr_obj);
145 static void un_register_gppsm_seg(struct cmm_allocator *psma);
146
147 /*
148  *  ======== cmm_calloc_buf ========
149  *  Purpose:
150  *      Allocate a SM buffer, zero contents, and return the physical address
151  *      and optional driver context virtual address(pp_buf_va).
152  *
153  *      The freelist is sorted in increasing size order. Get the first
154  *      block that satifies the request and sort the remaining back on
155  *      the freelist; if large enough. The kept block is placed on the
156  *      inUseList.
157  */
158 void *cmm_calloc_buf(struct cmm_object *hcmm_mgr, u32 usize,
159                      struct cmm_attrs *pattrs, void **pp_buf_va)
160 {
161         struct cmm_object *cmm_mgr_obj = (struct cmm_object *)hcmm_mgr;
162         void *buf_pa = NULL;
163         struct cmm_mnode *pnode = NULL;
164         struct cmm_mnode *new_node = NULL;
165         struct cmm_allocator *allocator = NULL;
166         u32 delta_size;
167         u8 *pbyte = NULL;
168         s32 cnt;
169
170         if (pattrs == NULL)
171                 pattrs = &cmm_dfltalctattrs;
172
173         if (pp_buf_va != NULL)
174                 *pp_buf_va = NULL;
175
176         if (cmm_mgr_obj && (usize != 0)) {
177                 if (pattrs->seg_id > 0) {
178                         /* SegId > 0 is SM */
179                         /* get the allocator object for this segment id */
180                         allocator =
181                             get_allocator(cmm_mgr_obj, pattrs->seg_id);
182                         /* keep block size a multiple of min_block_size */
183                         usize =
184                             ((usize - 1) & ~(cmm_mgr_obj->min_block_size -
185                                              1))
186                             + cmm_mgr_obj->min_block_size;
187                         mutex_lock(&cmm_mgr_obj->cmm_lock);
188                         pnode = get_free_block(allocator, usize);
189                 }
190                 if (pnode) {
191                         delta_size = (pnode->size - usize);
192                         if (delta_size >= cmm_mgr_obj->min_block_size) {
193                                 /* create a new block with the leftovers and
194                                  * add to freelist */
195                                 new_node =
196                                     get_node(cmm_mgr_obj, pnode->pa + usize,
197                                              pnode->va + usize,
198                                              (u32) delta_size);
199                                 /* leftovers go free */
200                                 add_to_free_list(allocator, new_node);
201                                 /* adjust our node's size */
202                                 pnode->size = usize;
203                         }
204                         /* Tag node with client process requesting allocation
205                          * We'll need to free up a process's alloc'd SM if the
206                          * client process goes away.
207                          */
208                         /* Return TGID instead of process handle */
209                         pnode->client_proc = current->tgid;
210
211                         /* put our node on InUse list */
212                         list_add_tail(&pnode->link, &allocator->in_use_list);
213                         buf_pa = (void *)pnode->pa;     /* physical address */
214                         /* clear mem */
215                         pbyte = (u8 *) pnode->va;
216                         for (cnt = 0; cnt < (s32) usize; cnt++, pbyte++)
217                                 *pbyte = 0;
218
219                         if (pp_buf_va != NULL) {
220                                 /* Virtual address */
221                                 *pp_buf_va = (void *)pnode->va;
222                         }
223                 }
224                 mutex_unlock(&cmm_mgr_obj->cmm_lock);
225         }
226         return buf_pa;
227 }
228
229 /*
230  *  ======== cmm_create ========
231  *  Purpose:
232  *      Create a communication memory manager object.
233  */
234 int cmm_create(struct cmm_object **ph_cmm_mgr,
235                       struct dev_object *hdev_obj,
236                       const struct cmm_mgrattrs *mgr_attrts)
237 {
238         struct cmm_object *cmm_obj = NULL;
239         int status = 0;
240
241         *ph_cmm_mgr = NULL;
242         /* create, zero, and tag a cmm mgr object */
243         cmm_obj = kzalloc(sizeof(struct cmm_object), GFP_KERNEL);
244         if (!cmm_obj)
245                 return -ENOMEM;
246
247         if (mgr_attrts == NULL)
248                 mgr_attrts = &cmm_dfltmgrattrs; /* set defaults */
249
250         /* save away smallest block allocation for this cmm mgr */
251         cmm_obj->min_block_size = mgr_attrts->min_block_size;
252         cmm_obj->page_size = PAGE_SIZE;
253
254         /* create node free list */
255         INIT_LIST_HEAD(&cmm_obj->node_free_list);
256         mutex_init(&cmm_obj->cmm_lock);
257         *ph_cmm_mgr = cmm_obj;
258
259         return status;
260 }
261
262 /*
263  *  ======== cmm_destroy ========
264  *  Purpose:
265  *      Release the communication memory manager resources.
266  */
267 int cmm_destroy(struct cmm_object *hcmm_mgr, bool force)
268 {
269         struct cmm_object *cmm_mgr_obj = (struct cmm_object *)hcmm_mgr;
270         struct cmm_info temp_info;
271         int status = 0;
272         s32 slot_seg;
273         struct cmm_mnode *node, *tmp;
274
275         if (!hcmm_mgr) {
276                 status = -EFAULT;
277                 return status;
278         }
279         mutex_lock(&cmm_mgr_obj->cmm_lock);
280         /* If not force then fail if outstanding allocations exist */
281         if (!force) {
282                 /* Check for outstanding memory allocations */
283                 status = cmm_get_info(hcmm_mgr, &temp_info);
284                 if (!status) {
285                         if (temp_info.total_in_use_cnt > 0) {
286                                 /* outstanding allocations */
287                                 status = -EPERM;
288                         }
289                 }
290         }
291         if (!status) {
292                 /* UnRegister SM allocator */
293                 for (slot_seg = 0; slot_seg < CMM_MAXGPPSEGS; slot_seg++) {
294                         if (cmm_mgr_obj->pa_gppsm_seg_tab[slot_seg] != NULL) {
295                                 un_register_gppsm_seg
296                                     (cmm_mgr_obj->pa_gppsm_seg_tab[slot_seg]);
297                                 /* Set slot to NULL for future reuse */
298                                 cmm_mgr_obj->pa_gppsm_seg_tab[slot_seg] = NULL;
299                         }
300                 }
301         }
302         list_for_each_entry_safe(node, tmp, &cmm_mgr_obj->node_free_list,
303                         link) {
304                 list_del(&node->link);
305                 kfree(node);
306         }
307         mutex_unlock(&cmm_mgr_obj->cmm_lock);
308         if (!status) {
309                 /* delete CS & cmm mgr object */
310                 mutex_destroy(&cmm_mgr_obj->cmm_lock);
311                 kfree(cmm_mgr_obj);
312         }
313         return status;
314 }
315
316 /*
317  *  ======== cmm_free_buf ========
318  *  Purpose:
319  *      Free the given buffer.
320  */
321 int cmm_free_buf(struct cmm_object *hcmm_mgr, void *buf_pa, u32 ul_seg_id)
322 {
323         struct cmm_object *cmm_mgr_obj = (struct cmm_object *)hcmm_mgr;
324         int status = -EFAULT;
325         struct cmm_mnode *curr, *tmp;
326         struct cmm_allocator *allocator;
327         struct cmm_attrs *pattrs;
328
329         if (ul_seg_id == 0) {
330                 pattrs = &cmm_dfltalctattrs;
331                 ul_seg_id = pattrs->seg_id;
332         }
333         if (!hcmm_mgr || !(ul_seg_id > 0)) {
334                 status = -EFAULT;
335                 return status;
336         }
337
338         allocator = get_allocator(cmm_mgr_obj, ul_seg_id);
339         if (!allocator)
340                 return status;
341
342         mutex_lock(&cmm_mgr_obj->cmm_lock);
343         list_for_each_entry_safe(curr, tmp, &allocator->in_use_list, link) {
344                 if (curr->pa == (u32) buf_pa) {
345                         list_del(&curr->link);
346                         add_to_free_list(allocator, curr);
347                         status = 0;
348                         break;
349                 }
350         }
351         mutex_unlock(&cmm_mgr_obj->cmm_lock);
352
353         return status;
354 }
355
356 /*
357  *  ======== cmm_get_handle ========
358  *  Purpose:
359  *      Return the communication memory manager object for this device.
360  *      This is typically called from the client process.
361  */
362 int cmm_get_handle(void *hprocessor, struct cmm_object **ph_cmm_mgr)
363 {
364         int status = 0;
365         struct dev_object *hdev_obj;
366
367         if (hprocessor != NULL)
368                 status = proc_get_dev_object(hprocessor, &hdev_obj);
369         else
370                 hdev_obj = dev_get_first();     /* default */
371
372         if (!status)
373                 status = dev_get_cmm_mgr(hdev_obj, ph_cmm_mgr);
374
375         return status;
376 }
377
378 /*
379  *  ======== cmm_get_info ========
380  *  Purpose:
381  *      Return the current memory utilization information.
382  */
383 int cmm_get_info(struct cmm_object *hcmm_mgr,
384                         struct cmm_info *cmm_info_obj)
385 {
386         struct cmm_object *cmm_mgr_obj = (struct cmm_object *)hcmm_mgr;
387         u32 ul_seg;
388         int status = 0;
389         struct cmm_allocator *altr;
390         struct cmm_mnode *curr;
391
392         if (!hcmm_mgr) {
393                 status = -EFAULT;
394                 return status;
395         }
396         mutex_lock(&cmm_mgr_obj->cmm_lock);
397         cmm_info_obj->num_gppsm_segs = 0;       /* # of SM segments */
398         /* Total # of outstanding alloc */
399         cmm_info_obj->total_in_use_cnt = 0;
400         /* min block size */
401         cmm_info_obj->min_block_size = cmm_mgr_obj->min_block_size;
402         /* check SM memory segments */
403         for (ul_seg = 1; ul_seg <= CMM_MAXGPPSEGS; ul_seg++) {
404                 /* get the allocator object for this segment id */
405                 altr = get_allocator(cmm_mgr_obj, ul_seg);
406                 if (!altr)
407                         continue;
408                 cmm_info_obj->num_gppsm_segs++;
409                 cmm_info_obj->seg_info[ul_seg - 1].seg_base_pa =
410                         altr->shm_base - altr->dsp_size;
411                 cmm_info_obj->seg_info[ul_seg - 1].total_seg_size =
412                         altr->dsp_size + altr->sm_size;
413                 cmm_info_obj->seg_info[ul_seg - 1].gpp_base_pa =
414                         altr->shm_base;
415                 cmm_info_obj->seg_info[ul_seg - 1].gpp_size =
416                         altr->sm_size;
417                 cmm_info_obj->seg_info[ul_seg - 1].dsp_base_va =
418                         altr->dsp_base;
419                 cmm_info_obj->seg_info[ul_seg - 1].dsp_size =
420                         altr->dsp_size;
421                 cmm_info_obj->seg_info[ul_seg - 1].seg_base_va =
422                         altr->vm_base - altr->dsp_size;
423                 cmm_info_obj->seg_info[ul_seg - 1].in_use_cnt = 0;
424
425                 list_for_each_entry(curr, &altr->in_use_list, link) {
426                         cmm_info_obj->total_in_use_cnt++;
427                         cmm_info_obj->seg_info[ul_seg - 1].in_use_cnt++;
428                 }
429         }
430         mutex_unlock(&cmm_mgr_obj->cmm_lock);
431         return status;
432 }
433
434 /*
435  *  ======== cmm_register_gppsm_seg ========
436  *  Purpose:
437  *      Register a block of SM with the CMM to be used for later GPP SM
438  *      allocations.
439  */
440 int cmm_register_gppsm_seg(struct cmm_object *hcmm_mgr,
441                                   u32 dw_gpp_base_pa, u32 ul_size,
442                                   u32 dsp_addr_offset, s8 c_factor,
443                                   u32 dw_dsp_base, u32 ul_dsp_size,
444                                   u32 *sgmt_id, u32 gpp_base_va)
445 {
446         struct cmm_object *cmm_mgr_obj = (struct cmm_object *)hcmm_mgr;
447         struct cmm_allocator *psma = NULL;
448         int status = 0;
449         struct cmm_mnode *new_node;
450         s32 slot_seg;
451
452         dev_dbg(bridge, "%s: dw_gpp_base_pa %x ul_size %x dsp_addr_offset %x dw_dsp_base %x ul_dsp_size %x gpp_base_va %x\n",
453                         __func__, dw_gpp_base_pa, ul_size, dsp_addr_offset,
454                         dw_dsp_base, ul_dsp_size, gpp_base_va);
455
456         if (!hcmm_mgr)
457                 return -EFAULT;
458
459         /* make sure we have room for another allocator */
460         mutex_lock(&cmm_mgr_obj->cmm_lock);
461
462         slot_seg = get_slot(cmm_mgr_obj);
463         if (slot_seg < 0) {
464                 status = -EPERM;
465                 goto func_end;
466         }
467
468         /* Check if input ul_size is big enough to alloc at least one block */
469         if (ul_size < cmm_mgr_obj->min_block_size) {
470                 status = -EINVAL;
471                 goto func_end;
472         }
473
474         /* create, zero, and tag an SM allocator object */
475         psma = kzalloc(sizeof(struct cmm_allocator), GFP_KERNEL);
476         if (!psma) {
477                 status = -ENOMEM;
478                 goto func_end;
479         }
480
481         psma->cmm_mgr = hcmm_mgr;       /* ref to parent */
482         psma->shm_base = dw_gpp_base_pa;        /* SM Base phys */
483         psma->sm_size = ul_size;        /* SM segment size in bytes */
484         psma->vm_base = gpp_base_va;
485         psma->dsp_phys_addr_offset = dsp_addr_offset;
486         psma->c_factor = c_factor;
487         psma->dsp_base = dw_dsp_base;
488         psma->dsp_size = ul_dsp_size;
489         if (psma->vm_base == 0) {
490                 status = -EPERM;
491                 goto func_end;
492         }
493         /* return the actual segment identifier */
494         *sgmt_id = (u32) slot_seg + 1;
495
496         INIT_LIST_HEAD(&psma->free_list);
497         INIT_LIST_HEAD(&psma->in_use_list);
498
499         /* Get a mem node for this hunk-o-memory */
500         new_node = get_node(cmm_mgr_obj, dw_gpp_base_pa,
501                         psma->vm_base, ul_size);
502         /* Place node on the SM allocator's free list */
503         if (new_node) {
504                 list_add_tail(&new_node->link, &psma->free_list);
505         } else {
506                 status = -ENOMEM;
507                 goto func_end;
508         }
509         /* make entry */
510         cmm_mgr_obj->pa_gppsm_seg_tab[slot_seg] = psma;
511
512 func_end:
513         /* Cleanup allocator */
514         if (status && psma)
515                 un_register_gppsm_seg(psma);
516         mutex_unlock(&cmm_mgr_obj->cmm_lock);
517
518         return status;
519 }
520
521 /*
522  *  ======== cmm_un_register_gppsm_seg ========
523  *  Purpose:
524  *      UnRegister GPP SM segments with the CMM.
525  */
526 int cmm_un_register_gppsm_seg(struct cmm_object *hcmm_mgr,
527                                      u32 ul_seg_id)
528 {
529         struct cmm_object *cmm_mgr_obj = (struct cmm_object *)hcmm_mgr;
530         int status = 0;
531         struct cmm_allocator *psma;
532         u32 ul_id = ul_seg_id;
533
534         if (!hcmm_mgr)
535                 return -EFAULT;
536
537         if (ul_seg_id == CMM_ALLSEGMENTS)
538                 ul_id = 1;
539
540         if ((ul_id <= 0) || (ul_id > CMM_MAXGPPSEGS))
541                 return -EINVAL;
542
543         /*
544          * FIXME: CMM_MAXGPPSEGS == 1. why use a while cycle? Seems to me like
545          * the ul_seg_id is not needed here. It must be always 1.
546          */
547         while (ul_id <= CMM_MAXGPPSEGS) {
548                 mutex_lock(&cmm_mgr_obj->cmm_lock);
549                 /* slot = seg_id-1 */
550                 psma = cmm_mgr_obj->pa_gppsm_seg_tab[ul_id - 1];
551                 if (psma != NULL) {
552                         un_register_gppsm_seg(psma);
553                         /* Set alctr ptr to NULL for future reuse */
554                         cmm_mgr_obj->pa_gppsm_seg_tab[ul_id - 1] = NULL;
555                 } else if (ul_seg_id != CMM_ALLSEGMENTS) {
556                         status = -EPERM;
557                 }
558                 mutex_unlock(&cmm_mgr_obj->cmm_lock);
559                 if (ul_seg_id != CMM_ALLSEGMENTS)
560                         break;
561
562                 ul_id++;
563         }       /* end while */
564         return status;
565 }
566
567 /*
568  *  ======== un_register_gppsm_seg ========
569  *  Purpose:
570  *      UnRegister the SM allocator by freeing all its resources and
571  *      nulling cmm mgr table entry.
572  *  Note:
573  *      This routine is always called within cmm lock crit sect.
574  */
575 static void un_register_gppsm_seg(struct cmm_allocator *psma)
576 {
577         struct cmm_mnode *curr, *tmp;
578
579         /* free nodes on free list */
580         list_for_each_entry_safe(curr, tmp, &psma->free_list, link) {
581                 list_del(&curr->link);
582                 kfree(curr);
583         }
584
585         /* free nodes on InUse list */
586         list_for_each_entry_safe(curr, tmp, &psma->in_use_list, link) {
587                 list_del(&curr->link);
588                 kfree(curr);
589         }
590
591         if ((void *)psma->vm_base != NULL)
592                 MEM_UNMAP_LINEAR_ADDRESS((void *)psma->vm_base);
593
594         /* Free allocator itself */
595         kfree(psma);
596 }
597
598 /*
599  *  ======== get_slot ========
600  *  Purpose:
601  *      An available slot # is returned. Returns negative on failure.
602  */
603 static s32 get_slot(struct cmm_object *cmm_mgr_obj)
604 {
605         s32 slot_seg = -1;      /* neg on failure */
606         /* get first available slot in cmm mgr SMSegTab[] */
607         for (slot_seg = 0; slot_seg < CMM_MAXGPPSEGS; slot_seg++) {
608                 if (cmm_mgr_obj->pa_gppsm_seg_tab[slot_seg] == NULL)
609                         break;
610
611         }
612         if (slot_seg == CMM_MAXGPPSEGS)
613                 slot_seg = -1;  /* failed */
614
615         return slot_seg;
616 }
617
618 /*
619  *  ======== get_node ========
620  *  Purpose:
621  *      Get a memory node from freelist or create a new one.
622  */
623 static struct cmm_mnode *get_node(struct cmm_object *cmm_mgr_obj, u32 dw_pa,
624                                   u32 dw_va, u32 ul_size)
625 {
626         struct cmm_mnode *pnode;
627
628         /* Check cmm mgr's node freelist */
629         if (list_empty(&cmm_mgr_obj->node_free_list)) {
630                 pnode = kzalloc(sizeof(struct cmm_mnode), GFP_KERNEL);
631                 if (!pnode)
632                         return NULL;
633         } else {
634                 /* surely a valid element */
635                 pnode = list_first_entry(&cmm_mgr_obj->node_free_list,
636                                 struct cmm_mnode, link);
637                 list_del_init(&pnode->link);
638         }
639
640         pnode->pa = dw_pa;
641         pnode->va = dw_va;
642         pnode->size = ul_size;
643
644         return pnode;
645 }
646
647 /*
648  *  ======== delete_node ========
649  *  Purpose:
650  *      Put a memory node on the cmm nodelist for later use.
651  *      Doesn't actually delete the node. Heap thrashing friendly.
652  */
653 static void delete_node(struct cmm_object *cmm_mgr_obj, struct cmm_mnode *pnode)
654 {
655         list_add_tail(&pnode->link, &cmm_mgr_obj->node_free_list);
656 }
657
658 /*
659  * ====== get_free_block ========
660  *  Purpose:
661  *      Scan the free block list and return the first block that satisfies
662  *      the size.
663  */
664 static struct cmm_mnode *get_free_block(struct cmm_allocator *allocator,
665                                         u32 usize)
666 {
667         struct cmm_mnode *node, *tmp;
668
669         if (!allocator)
670                 return NULL;
671
672         list_for_each_entry_safe(node, tmp, &allocator->free_list, link) {
673                 if (usize <= node->size) {
674                         list_del(&node->link);
675                         return node;
676                 }
677         }
678
679         return NULL;
680 }
681
682 /*
683  *  ======== add_to_free_list ========
684  *  Purpose:
685  *      Coalesce node into the freelist in ascending size order.
686  */
687 static void add_to_free_list(struct cmm_allocator *allocator,
688                              struct cmm_mnode *node)
689 {
690         struct cmm_mnode *curr;
691
692         if (!node) {
693                 pr_err("%s: failed - node is NULL\n", __func__);
694                 return;
695         }
696
697         list_for_each_entry(curr, &allocator->free_list, link) {
698                 if (NEXT_PA(curr) == node->pa) {
699                         curr->size += node->size;
700                         delete_node(allocator->cmm_mgr, node);
701                         return;
702                 }
703                 if (curr->pa == NEXT_PA(node)) {
704                         curr->pa = node->pa;
705                         curr->va = node->va;
706                         curr->size += node->size;
707                         delete_node(allocator->cmm_mgr, node);
708                         return;
709                 }
710         }
711         list_for_each_entry(curr, &allocator->free_list, link) {
712                 if (curr->size >= node->size) {
713                         list_add_tail(&node->link, &curr->link);
714                         return;
715                 }
716         }
717         list_add_tail(&node->link, &allocator->free_list);
718 }
719
720 /*
721  * ======== get_allocator ========
722  *  Purpose:
723  *      Return the allocator for the given SM Segid.
724  *      SegIds:  1,2,3..max.
725  */
726 static struct cmm_allocator *get_allocator(struct cmm_object *cmm_mgr_obj,
727                                            u32 ul_seg_id)
728 {
729         return cmm_mgr_obj->pa_gppsm_seg_tab[ul_seg_id - 1];
730 }
731
732 /*
733  *  The CMM_Xlator[xxx] routines below are used by Node and Stream
734  *  to perform SM address translation to the client process address space.
735  *  A "translator" object is created by a node/stream for each SM seg used.
736  */
737
738 /*
739  *  ======== cmm_xlator_create ========
740  *  Purpose:
741  *      Create an address translator object.
742  */
743 int cmm_xlator_create(struct cmm_xlatorobject **xlator,
744                              struct cmm_object *hcmm_mgr,
745                              struct cmm_xlatorattrs *xlator_attrs)
746 {
747         struct cmm_xlator *xlator_object = NULL;
748         int status = 0;
749
750         *xlator = NULL;
751         if (xlator_attrs == NULL)
752                 xlator_attrs = &cmm_dfltxlatorattrs;    /* set defaults */
753
754         xlator_object = kzalloc(sizeof(struct cmm_xlator), GFP_KERNEL);
755         if (xlator_object != NULL) {
756                 xlator_object->cmm_mgr = hcmm_mgr;      /* ref back to CMM */
757                 /* SM seg_id */
758                 xlator_object->seg_id = xlator_attrs->seg_id;
759         } else {
760                 status = -ENOMEM;
761         }
762         if (!status)
763                 *xlator = (struct cmm_xlatorobject *)xlator_object;
764
765         return status;
766 }
767
768 /*
769  *  ======== cmm_xlator_alloc_buf ========
770  */
771 void *cmm_xlator_alloc_buf(struct cmm_xlatorobject *xlator, void *va_buf,
772                            u32 pa_size)
773 {
774         struct cmm_xlator *xlator_obj = (struct cmm_xlator *)xlator;
775         void *pbuf = NULL;
776         void *tmp_va_buff;
777         struct cmm_attrs attrs;
778
779         if (xlator_obj) {
780                 attrs.seg_id = xlator_obj->seg_id;
781                 __raw_writel(0, va_buf);
782                 /* Alloc SM */
783                 pbuf =
784                     cmm_calloc_buf(xlator_obj->cmm_mgr, pa_size, &attrs, NULL);
785                 if (pbuf) {
786                         /* convert to translator(node/strm) process Virtual
787                          * address */
788                          tmp_va_buff = cmm_xlator_translate(xlator,
789                                                          pbuf, CMM_PA2VA);
790                         __raw_writel((u32)tmp_va_buff, va_buf);
791                 }
792         }
793         return pbuf;
794 }
795
796 /*
797  *  ======== cmm_xlator_free_buf ========
798  *  Purpose:
799  *      Free the given SM buffer and descriptor.
800  *      Does not free virtual memory.
801  */
802 int cmm_xlator_free_buf(struct cmm_xlatorobject *xlator, void *buf_va)
803 {
804         struct cmm_xlator *xlator_obj = (struct cmm_xlator *)xlator;
805         int status = -EPERM;
806         void *buf_pa = NULL;
807
808         if (xlator_obj) {
809                 /* convert Va to Pa so we can free it. */
810                 buf_pa = cmm_xlator_translate(xlator, buf_va, CMM_VA2PA);
811                 if (buf_pa) {
812                         status = cmm_free_buf(xlator_obj->cmm_mgr, buf_pa,
813                                               xlator_obj->seg_id);
814                         if (status) {
815                                 /* Uh oh, this shouldn't happen. Descriptor
816                                  * gone! */
817                                 pr_err("%s, line %d: Assertion failed\n",
818                                        __FILE__, __LINE__);
819                         }
820                 }
821         }
822         return status;
823 }
824
825 /*
826  *  ======== cmm_xlator_info ========
827  *  Purpose:
828  *      Set/Get translator info.
829  */
830 int cmm_xlator_info(struct cmm_xlatorobject *xlator, u8 **paddr,
831                            u32 ul_size, u32 segm_id, bool set_info)
832 {
833         struct cmm_xlator *xlator_obj = (struct cmm_xlator *)xlator;
834         int status = 0;
835
836         if (xlator_obj) {
837                 if (set_info) {
838                         /* set translators virtual address range */
839                         xlator_obj->virt_base = (u32) *paddr;
840                         xlator_obj->virt_size = ul_size;
841                 } else {        /* return virt base address */
842                         *paddr = (u8 *) xlator_obj->virt_base;
843                 }
844         } else {
845                 status = -EFAULT;
846         }
847         return status;
848 }
849
850 /*
851  *  ======== cmm_xlator_translate ========
852  */
853 void *cmm_xlator_translate(struct cmm_xlatorobject *xlator, void *paddr,
854                            enum cmm_xlatetype xtype)
855 {
856         u32 dw_addr_xlate = 0;
857         struct cmm_xlator *xlator_obj = (struct cmm_xlator *)xlator;
858         struct cmm_object *cmm_mgr_obj = NULL;
859         struct cmm_allocator *allocator = NULL;
860         u32 dw_offset = 0;
861
862         if (!xlator_obj)
863                 goto loop_cont;
864
865         cmm_mgr_obj = (struct cmm_object *)xlator_obj->cmm_mgr;
866         /* get this translator's default SM allocator */
867         allocator = cmm_mgr_obj->pa_gppsm_seg_tab[xlator_obj->seg_id - 1];
868         if (!allocator)
869                 goto loop_cont;
870
871         if ((xtype == CMM_VA2DSPPA) || (xtype == CMM_VA2PA) ||
872             (xtype == CMM_PA2VA)) {
873                 if (xtype == CMM_PA2VA) {
874                         /* Gpp Va = Va Base + offset */
875                         dw_offset = (u8 *) paddr - (u8 *) (allocator->shm_base -
876                                                            allocator->
877                                                            dsp_size);
878                         dw_addr_xlate = xlator_obj->virt_base + dw_offset;
879                         /* Check if translated Va base is in range */
880                         if ((dw_addr_xlate < xlator_obj->virt_base) ||
881                             (dw_addr_xlate >=
882                              (xlator_obj->virt_base +
883                               xlator_obj->virt_size))) {
884                                 dw_addr_xlate = 0;      /* bad address */
885                         }
886                 } else {
887                         /* Gpp PA =  Gpp Base + offset */
888                         dw_offset =
889                             (u8 *) paddr - (u8 *) xlator_obj->virt_base;
890                         dw_addr_xlate =
891                             allocator->shm_base - allocator->dsp_size +
892                             dw_offset;
893                 }
894         } else {
895                 dw_addr_xlate = (u32) paddr;
896         }
897         /*Now convert address to proper target physical address if needed */
898         if ((xtype == CMM_VA2DSPPA) || (xtype == CMM_PA2DSPPA)) {
899                 /* Got Gpp Pa now, convert to DSP Pa */
900                 dw_addr_xlate =
901                     GPPPA2DSPPA((allocator->shm_base - allocator->dsp_size),
902                                 dw_addr_xlate,
903                                 allocator->dsp_phys_addr_offset *
904                                 allocator->c_factor);
905         } else if (xtype == CMM_DSPPA2PA) {
906                 /* Got DSP Pa, convert to GPP Pa */
907                 dw_addr_xlate =
908                     DSPPA2GPPPA(allocator->shm_base - allocator->dsp_size,
909                                 dw_addr_xlate,
910                                 allocator->dsp_phys_addr_offset *
911                                 allocator->c_factor);
912         }
913 loop_cont:
914         return (void *)dw_addr_xlate;
915 }