1 /**************************************************************************
2  *
3  * Copyright 2006-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 #ifndef _WSBM_BUFPOOL_H_
34 #define _WSBM_BUFPOOL_H_
35 
36 #include <errno.h>
37 #include "wsbm_util.h"
38 #include "wsbm_driver.h"
39 #include "wsbm_atomic.h"
40 
41 struct _WsbmFenceObject;
42 
43 struct _WsbmBufStorage
44 {
45     struct _WsbmBufferPool *pool;
46     struct _WsbmMutex mutex;
47     struct _WsbmAtomic refCount;
48     struct _WsbmAtomic onList;
49     void *destroyArg;
50     void (*destroyContainer) (void *);
51 };
52 
53 struct _WsbmKernelBuf;
54 
55 struct _WsbmBufferPool
56 {
57     int fd;
58     int (*map) (struct _WsbmBufStorage * buf, unsigned mode, void **virtual);
59     void (*unmap) (struct _WsbmBufStorage * buf);
60     int (*syncforcpu) (struct _WsbmBufStorage * buf, unsigned mode);
61     void (*releasefromcpu) (struct _WsbmBufStorage * buf, unsigned mode);
62     void (*destroy) (struct _WsbmBufStorage ** buf);
63     unsigned long (*offset) (struct _WsbmBufStorage * buf);
64     unsigned long (*poolOffset) (struct _WsbmBufStorage * buf);
65         uint32_t(*placement) (struct _WsbmBufStorage * buf);
66     unsigned long (*size) (struct _WsbmBufStorage * buf);
67     struct _WsbmKernelBuf *(*kernel) (struct _WsbmBufStorage * buf);
68     struct _WsbmBufStorage *(*create) (struct _WsbmBufferPool * pool,
69 				       unsigned long size,
70 				       uint32_t placement,
71 				       unsigned alignment);
72     struct _WsbmBufStorage *(*createByReference) (struct _WsbmBufferPool *
73 						  pool, uint32_t handle);
74     void (*fence) (struct _WsbmBufStorage * buf,
75 		   struct _WsbmFenceObject * fence);
76     void (*unvalidate) (struct _WsbmBufStorage * buf);
77     int (*validate) (struct _WsbmBufStorage * buf, uint64_t set_flags,
78 		     uint64_t clr_flags);
79     int (*waitIdle) (struct _WsbmBufStorage * buf, int lazy);
80     int (*setStatus) (struct _WsbmBufStorage * buf,
81 		      uint32_t set_placement, uint32_t clr_placement);
82     void (*takeDown) (struct _WsbmBufferPool * pool);
83 };
84 
85 static inline int
wsbmBufStorageInit(struct _WsbmBufStorage * storage,struct _WsbmBufferPool * pool)86 wsbmBufStorageInit(struct _WsbmBufStorage *storage,
87 		   struct _WsbmBufferPool *pool)
88 {
89     int ret = WSBM_MUTEX_INIT(&storage->mutex);
90 
91     if (ret)
92 	return -ENOMEM;
93     storage->pool = pool;
94     wsbmAtomicSet(&storage->refCount, 1);
95     wsbmAtomicSet(&storage->onList, 0);
96     storage->destroyContainer = NULL;
97     return 0;
98 }
99 
100 static inline void
wsbmBufStorageTakedown(struct _WsbmBufStorage * storage)101 wsbmBufStorageTakedown(struct _WsbmBufStorage *storage)
102 {
103     WSBM_MUTEX_FREE(&storage->mutex);
104 }
105 
106 static inline void
wsbmBufStorageUnref(struct _WsbmBufStorage ** pStorage)107 wsbmBufStorageUnref(struct _WsbmBufStorage **pStorage)
108 {
109     struct _WsbmBufStorage *storage = *pStorage;
110 
111     *pStorage = NULL;
112     if (storage == NULL)
113 	return;
114 
115     if (wsbmAtomicDecZero(&storage->refCount)) {
116 	if (storage->destroyContainer)
117 	    storage->destroyContainer(storage->destroyArg);
118 	storage->pool->destroy(&storage);
119 	return;
120     }
121 }
122 
123 /*
124  * Builtin pools.
125  */
126 
127 /*
128  * Kernel buffer objects. Size in multiples of page size. Page size aligned.
129  */
130 
131 extern struct _WsbmBufferPool *wsbmTTMPoolInit(int fd,
132 					       unsigned int devOffset);
133 extern struct _WsbmBufferPool *wsbmMallocPoolInit(void);
134 
135 struct _WsbmSlabCache;
136 extern struct _WsbmBufferPool *wsbmSlabPoolInit(int fd, uint32_t devOffset,
137 						uint32_t placement,
138 						uint32_t validMask,
139 						uint32_t smallestSize,
140 						uint32_t numSizes,
141 						uint32_t desiredNumBuffers,
142 						uint32_t maxSlabSize,
143 						uint32_t pageAlignment,
144 						struct _WsbmSlabCache *cache);
145 extern struct _WsbmSlabCache *wsbmSlabCacheInit(uint32_t checkIntervalMsec,
146 						uint32_t slabTimeoutMsec);
147 extern void wsbmSlabCacheFinish(struct _WsbmSlabCache *cache);
148 
149 extern struct _WsbmBufferPool *wsbmUserPoolInit(void *vramAddr,
150 						unsigned long vramStart,
151 						unsigned long vramSize,
152 						void *agpAddr,
153 						unsigned long agpStart,
154 						unsigned long agpSize,
155 						uint32_t(*fenceTypes)
156 						    (uint64_t set_flags));
157 
158 extern void wsbmUserPoolClean(struct _WsbmBufferPool *pool,
159 			      int cleanVram, int cleanAgp);
160 
161 #endif
162