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
42struct _WsbmMallocBuffer
43{
44    struct _WsbmBufStorage buf;
45    size_t size;
46    void *mem;
47};
48
49static inline struct _WsbmMallocBuffer *
50mallocBuf(struct _WsbmBufStorage *buf)
51{
52    return containerOf(buf, struct _WsbmMallocBuffer, buf);
53}
54
55static struct _WsbmBufStorage *
56pool_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
73static void
74pool_destroy(struct _WsbmBufStorage **buf)
75{
76    free(mallocBuf(*buf));
77    *buf = NULL;
78}
79
80static int
81pool_waitIdle(struct _WsbmBufStorage *buf __attribute__ ((unused)), int lazy __attribute__ ((unused)))
82{
83    return 0;
84}
85
86static int
87pool_map(struct _WsbmBufStorage *buf, unsigned mode __attribute__ ((unused)), void **virtual __attribute__ ((unused)))
88{
89    *virtual = mallocBuf(buf)->mem;
90    return 0;
91}
92
93static void
94pool_unmap(struct _WsbmBufStorage *buf __attribute__ ((unused)))
95{
96    ;
97}
98
99static int
100pool_syncforcpu(struct _WsbmBufStorage *buf __attribute__ ((unused)), unsigned mode __attribute__ ((unused)))
101{
102    return 0;
103}
104
105static void
106pool_releasefromcpu(struct _WsbmBufStorage *buf __attribute__ ((unused)), unsigned mode __attribute__ ((unused)))
107{
108    ;
109}
110
111static unsigned long
112pool_offset(struct _WsbmBufStorage *buf __attribute__ ((unused)))
113{
114    /*
115     * BUG
116     */
117    abort();
118    return 0UL;
119}
120
121static unsigned long
122pool_poolOffset(struct _WsbmBufStorage *buf __attribute__ ((unused)))
123{
124    /*
125     * BUG
126     */
127    abort();
128}
129
130static uint32_t
131pool_placement(struct _WsbmBufStorage *buf __attribute__ ((unused)))
132{
133    return WSBM_PL_FLAG_SYSTEM | WSBM_PL_FLAG_CACHED;
134}
135
136static unsigned long
137pool_size(struct _WsbmBufStorage *buf)
138{
139    return mallocBuf(buf)->size;
140}
141
142static void
143pool_fence(struct _WsbmBufStorage *buf __attribute__ ((unused)), struct _WsbmFenceObject *fence __attribute__ ((unused)))
144{
145    abort();
146}
147
148static struct _WsbmKernelBuf *
149pool_kernel(struct _WsbmBufStorage *buf __attribute__ ((unused)))
150{
151    abort();
152    return NULL;
153}
154
155static void
156pool_takedown(struct _WsbmBufferPool *pool)
157{
158    free(pool);
159}
160
161struct _WsbmBufferPool *
162wsbmMallocPoolInit(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