Merge remote branch 'origin/master' into libdrm
[platform/upstream/libdrm.git] / radeon / radeon_bo.h
1 /* 
2  * Copyright © 2008 Jérôme Glisse
3  * All Rights Reserved.
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16  * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17  * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  */
26 /*
27  * Authors:
28  *      Jérôme Glisse <glisse@freedesktop.org>
29  */
30 #ifndef RADEON_BO_H
31 #define RADEON_BO_H
32
33 #include <stdio.h>
34 #include <stdint.h>
35 #include "radeon_track.h"
36
37 /* bo object */
38 #define RADEON_BO_FLAGS_MACRO_TILE  1
39 #define RADEON_BO_FLAGS_MICRO_TILE  2
40
41 struct radeon_bo_manager;
42 struct radeon_cs;
43
44 struct radeon_bo {
45     uint32_t                    alignment;
46     uint32_t                    handle;
47     uint32_t                    size;
48     uint32_t                    domains;
49     uint32_t                    flags;
50     unsigned                    cref;
51 #ifdef RADEON_BO_TRACK
52     struct radeon_track         *track;
53 #endif
54     void                        *ptr;
55     struct radeon_bo_manager    *bom;
56     uint32_t                    space_accounted;
57     uint32_t                    referenced_in_cs;
58 };
59
60 /* bo functions */
61 struct radeon_bo_funcs {
62     struct radeon_bo *(*bo_open)(struct radeon_bo_manager *bom,
63                                  uint32_t handle,
64                                  uint32_t size,
65                                  uint32_t alignment,
66                                  uint32_t domains,
67                                  uint32_t flags);
68     void (*bo_ref)(struct radeon_bo *bo);
69     struct radeon_bo *(*bo_unref)(struct radeon_bo *bo);
70     int (*bo_map)(struct radeon_bo *bo, int write);
71     int (*bo_unmap)(struct radeon_bo *bo);
72     int (*bo_wait)(struct radeon_bo *bo);
73     int (*bo_is_static)(struct radeon_bo *bo);
74     int (*bo_set_tiling)(struct radeon_bo *bo, uint32_t tiling_flags,
75                           uint32_t pitch);
76     int (*bo_get_tiling)(struct radeon_bo *bo, uint32_t *tiling_flags,
77                           uint32_t *pitch);
78     int (*bo_is_busy)(struct radeon_bo *bo, uint32_t *domain);
79     int (*bo_is_referenced_by_cs)(struct radeon_bo *bo, struct radeon_cs *cs);
80 };
81
82 struct radeon_bo_manager {
83     struct radeon_bo_funcs  *funcs;
84     int                     fd;
85     struct radeon_tracker   tracker;
86 };
87     
88 static inline void _radeon_bo_debug(struct radeon_bo *bo,
89                                     const char *op,
90                                     const char *file,
91                                     const char *func,
92                                     int line)
93 {
94     fprintf(stderr, "%s %p 0x%08X 0x%08X 0x%08X [%s %s %d]\n",
95             op, bo, bo->handle, bo->size, bo->cref, file, func, line);
96 }
97
98 static inline struct radeon_bo *_radeon_bo_open(struct radeon_bo_manager *bom,
99                                                 uint32_t handle,
100                                                 uint32_t size,
101                                                 uint32_t alignment,
102                                                 uint32_t domains,
103                                                 uint32_t flags,
104                                                 const char *file,
105                                                 const char *func,
106                                                 int line)
107 {
108     struct radeon_bo *bo;
109
110     bo = bom->funcs->bo_open(bom, handle, size, alignment, domains, flags);
111 #ifdef RADEON_BO_TRACK
112     if (bo) {
113         bo->track = radeon_tracker_add_track(&bom->tracker, bo->handle);
114         radeon_track_add_event(bo->track, file, func, "open", line);
115     }
116 #endif
117     return bo;
118 }
119
120 static inline void _radeon_bo_ref(struct radeon_bo *bo,
121                                   const char *file,
122                                   const char *func,
123                                   int line)
124 {
125     bo->cref++;
126 #ifdef RADEON_BO_TRACK
127     radeon_track_add_event(bo->track, file, func, "ref", line); 
128 #endif
129     bo->bom->funcs->bo_ref(bo);
130 }
131
132 static inline struct radeon_bo *_radeon_bo_unref(struct radeon_bo *bo,
133                                                  const char *file,
134                                                  const char *func,
135                                                  int line)
136 {
137     bo->cref--;
138 #ifdef RADEON_BO_TRACK
139     radeon_track_add_event(bo->track, file, func, "unref", line);
140     if (bo->cref <= 0) {
141         radeon_tracker_remove_track(&bo->bom->tracker, bo->track);
142         bo->track = NULL;
143     }
144 #endif
145     return bo->bom->funcs->bo_unref(bo);
146 }
147
148 static inline int _radeon_bo_map(struct radeon_bo *bo,
149                                  int write,
150                                  const char *file,
151                                  const char *func,
152                                  int line)
153 {
154     return bo->bom->funcs->bo_map(bo, write);
155 }
156
157 static inline int _radeon_bo_unmap(struct radeon_bo *bo,
158                                    const char *file,
159                                    const char *func,
160                                    int line)
161 {
162     return bo->bom->funcs->bo_unmap(bo);
163 }
164
165 static inline int _radeon_bo_wait(struct radeon_bo *bo,
166                                   const char *file,
167                                   const char *func,
168                                   int line)
169 {
170     return bo->bom->funcs->bo_wait(bo);
171 }
172
173 static inline int _radeon_bo_is_busy(struct radeon_bo *bo,
174                                      uint32_t *domain,
175                                      const char *file,
176                                      const char *func,
177                                      int line)
178 {
179     return bo->bom->funcs->bo_is_busy(bo, domain);
180 }
181
182 static inline int radeon_bo_set_tiling(struct radeon_bo *bo,
183                                        uint32_t tiling_flags, uint32_t pitch)
184 {
185     return bo->bom->funcs->bo_set_tiling(bo, tiling_flags, pitch);
186 }
187
188 static inline int radeon_bo_get_tiling(struct radeon_bo *bo,
189                                        uint32_t *tiling_flags, uint32_t *pitch)
190 {
191     return bo->bom->funcs->bo_get_tiling(bo, tiling_flags, pitch);
192 }
193
194 static inline int radeon_bo_is_static(struct radeon_bo *bo)
195 {
196     if (bo->bom->funcs->bo_is_static)
197         return bo->bom->funcs->bo_is_static(bo);
198     return 0;
199 }
200
201 static inline int _radeon_bo_is_referenced_by_cs(struct radeon_bo *bo,
202                                                  struct radeon_cs *cs,
203                                                  const char *file,
204                                                  const char *func,
205                                                  unsigned line)
206 {
207     return bo->cref > 1;
208 }
209
210 #define radeon_bo_open(bom, h, s, a, d, f)\
211     _radeon_bo_open(bom, h, s, a, d, f, __FILE__, __FUNCTION__, __LINE__)
212 #define radeon_bo_ref(bo)\
213     _radeon_bo_ref(bo, __FILE__, __FUNCTION__, __LINE__)
214 #define radeon_bo_unref(bo)\
215     _radeon_bo_unref(bo, __FILE__, __FUNCTION__, __LINE__)
216 #define radeon_bo_map(bo, w)\
217     _radeon_bo_map(bo, w, __FILE__, __FUNCTION__, __LINE__)
218 #define radeon_bo_unmap(bo)\
219     _radeon_bo_unmap(bo, __FILE__, __FUNCTION__, __LINE__)
220 #define radeon_bo_debug(bo, opcode)\
221     _radeon_bo_debug(bo, opcode, __FILE__, __FUNCTION__, __LINE__)
222 #define radeon_bo_wait(bo) \
223     _radeon_bo_wait(bo, __FILE__, __func__, __LINE__)
224 #define radeon_bo_is_busy(bo, domain) \
225     _radeon_bo_is_busy(bo, domain, __FILE__, __func__, __LINE__)
226 #define radeon_bo_is_referenced_by_cs(bo, cs) \
227     _radeon_bo_is_referenced_by_cs(bo, cs, __FILE__, __FUNCTION__, __LINE__)
228
229 #endif