1 /**************************************************************************
2  *
3  * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, TX., USA
4  * All Rights Reserved.
5  * Copyright 2009 VMware, Inc., Palo Alto, CA., USA
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  **************************************************************************/
29 /*
30  * Authors: Thomas Hellstr�m <thomas-at-tungstengraphics-dot-com>
31  */
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include <stdlib.h>
38 #include <errno.h>
39 #include "wsbm_pool.h"
40 #include "wsbm_manager.h"
41 
42 struct _WsbmMallocBuffer
43 {
44     struct _WsbmBufStorage buf;
45     size_t size;
46     void *mem;
47 };
48 
49 static inline struct _WsbmMallocBuffer *
mallocBuf(struct _WsbmBufStorage * buf)50 mallocBuf(struct _WsbmBufStorage *buf)
51 {
52     return containerOf(buf, struct _WsbmMallocBuffer, buf);
53 }
54 
55 static struct _WsbmBufStorage *
pool_create(struct _WsbmBufferPool * pool,unsigned long size,uint32_t placement,unsigned alignment)56 pool_create(struct _WsbmBufferPool *pool,
57         unsigned long size, uint32_t placement, unsigned alignment __attribute__ ((unused)))
58 {
59     struct _WsbmMallocBuffer *mBuf = malloc(size + sizeof(*mBuf) + 16);
60 
61     if (!mBuf)
62 	return NULL;
63 
64     wsbmBufStorageInit(&mBuf->buf, pool);
65     mBuf->size = size;
66     mBuf->mem = (void *)((unsigned long)mBuf + sizeof(*mBuf));
67     if ((placement & WSBM_PL_MASK_MEM) != WSBM_PL_FLAG_SYSTEM)
68 	abort();
69 
70     return &mBuf->buf;
71 }
72 
73 static void
pool_destroy(struct _WsbmBufStorage ** buf)74 pool_destroy(struct _WsbmBufStorage **buf)
75 {
76     free(mallocBuf(*buf));
77     *buf = NULL;
78 }
79 
80 static int
pool_waitIdle(struct _WsbmBufStorage * buf,int lazy)81 pool_waitIdle(struct _WsbmBufStorage *buf __attribute__ ((unused)), int lazy __attribute__ ((unused)))
82 {
83     return 0;
84 }
85 
86 static int
pool_map(struct _WsbmBufStorage * buf,unsigned mode,void ** virtual)87 pool_map(struct _WsbmBufStorage *buf, unsigned mode __attribute__ ((unused)), void **virtual __attribute__ ((unused)))
88 {
89     *virtual = mallocBuf(buf)->mem;
90     return 0;
91 }
92 
93 static void
pool_unmap(struct _WsbmBufStorage * buf)94 pool_unmap(struct _WsbmBufStorage *buf __attribute__ ((unused)))
95 {
96     ;
97 }
98 
99 static int
pool_syncforcpu(struct _WsbmBufStorage * buf,unsigned mode)100 pool_syncforcpu(struct _WsbmBufStorage *buf __attribute__ ((unused)), unsigned mode __attribute__ ((unused)))
101 {
102     return 0;
103 }
104 
105 static void
pool_releasefromcpu(struct _WsbmBufStorage * buf,unsigned mode)106 pool_releasefromcpu(struct _WsbmBufStorage *buf __attribute__ ((unused)), unsigned mode __attribute__ ((unused)))
107 {
108     ;
109 }
110 
111 static unsigned long
pool_offset(struct _WsbmBufStorage * buf)112 pool_offset(struct _WsbmBufStorage *buf __attribute__ ((unused)))
113 {
114     /*
115      * BUG
116      */
117     abort();
118     return 0UL;
119 }
120 
121 static unsigned long
pool_poolOffset(struct _WsbmBufStorage * buf)122 pool_poolOffset(struct _WsbmBufStorage *buf __attribute__ ((unused)))
123 {
124     /*
125      * BUG
126      */
127     abort();
128 }
129 
130 static uint32_t
pool_placement(struct _WsbmBufStorage * buf)131 pool_placement(struct _WsbmBufStorage *buf __attribute__ ((unused)))
132 {
133     return WSBM_PL_FLAG_SYSTEM | WSBM_PL_FLAG_CACHED;
134 }
135 
136 static unsigned long
pool_size(struct _WsbmBufStorage * buf)137 pool_size(struct _WsbmBufStorage *buf)
138 {
139     return mallocBuf(buf)->size;
140 }
141 
142 static void
pool_fence(struct _WsbmBufStorage * buf,struct _WsbmFenceObject * fence)143 pool_fence(struct _WsbmBufStorage *buf __attribute__ ((unused)), struct _WsbmFenceObject *fence __attribute__ ((unused)))
144 {
145     abort();
146 }
147 
148 static struct _WsbmKernelBuf *
pool_kernel(struct _WsbmBufStorage * buf)149 pool_kernel(struct _WsbmBufStorage *buf __attribute__ ((unused)))
150 {
151     abort();
152     return NULL;
153 }
154 
155 static void
pool_takedown(struct _WsbmBufferPool * pool)156 pool_takedown(struct _WsbmBufferPool *pool)
157 {
158     free(pool);
159 }
160 
161 struct _WsbmBufferPool *
wsbmMallocPoolInit(void)162 wsbmMallocPoolInit(void)
163 {
164     struct _WsbmBufferPool *pool;
165 
166     pool = (struct _WsbmBufferPool *)calloc(1, sizeof(*pool));
167     if (!pool)
168 	return NULL;
169 
170     pool->fd = -1;
171     pool->map = &pool_map;
172     pool->unmap = &pool_unmap;
173     pool->syncforcpu = &pool_syncforcpu;
174     pool->releasefromcpu = &pool_releasefromcpu;
175     pool->destroy = &pool_destroy;
176     pool->offset = &pool_offset;
177     pool->poolOffset = &pool_poolOffset;
178     pool->placement = &pool_placement;
179     pool->size = &pool_size;
180     pool->create = &pool_create;
181     pool->fence = &pool_fence;
182     pool->kernel = &pool_kernel;
183     pool->validate = NULL;
184     pool->waitIdle = &pool_waitIdle;
185     pool->takeDown = &pool_takedown;
186     return pool;
187 }
188