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
41struct radeon_bo_manager;
42
43struct radeon_bo {
44    uint32_t                    alignment;
45    uint32_t                    handle;
46    uint32_t                    size;
47    uint32_t                    domains;
48    uint32_t                    flags;
49    unsigned                    cref;
50#ifdef RADEON_BO_TRACK
51    struct radeon_track         *track;
52#endif
53    void                        *ptr;
54    struct radeon_bo_manager    *bom;
55    uint32_t                    space_accounted;
56};
57
58/* bo functions */
59struct radeon_bo_funcs {
60    struct radeon_bo *(*bo_open)(struct radeon_bo_manager *bom,
61                                 uint32_t handle,
62                                 uint32_t size,
63                                 uint32_t alignment,
64                                 uint32_t domains,
65                                 uint32_t flags);
66    void (*bo_ref)(struct radeon_bo *bo);
67    struct radeon_bo *(*bo_unref)(struct radeon_bo *bo);
68    int (*bo_map)(struct radeon_bo *bo, int write);
69    int (*bo_unmap)(struct radeon_bo *bo);
70    int (*bo_wait)(struct radeon_bo *bo);
71    int (*bo_is_static)(struct radeon_bo *bo);
72    int (*bo_set_tiling)(struct radeon_bo *bo, uint32_t tiling_flags,
73			  uint32_t pitch);
74    int (*bo_get_tiling)(struct radeon_bo *bo, uint32_t *tiling_flags,
75			  uint32_t *pitch);
76    int (*bo_is_busy)(struct radeon_bo *bo, uint32_t *domain);
77};
78
79struct radeon_bo_manager {
80    struct radeon_bo_funcs  *funcs;
81    int                     fd;
82    struct radeon_tracker   tracker;
83};
84
85static inline void _radeon_bo_debug(struct radeon_bo *bo,
86                                    const char *op,
87                                    const char *file,
88                                    const char *func,
89                                    int line)
90{
91    fprintf(stderr, "%s %p 0x%08X 0x%08X 0x%08X [%s %s %d]\n",
92            op, bo, bo->handle, bo->size, bo->cref, file, func, line);
93}
94
95static inline struct radeon_bo *_radeon_bo_open(struct radeon_bo_manager *bom,
96                                                uint32_t handle,
97                                                uint32_t size,
98                                                uint32_t alignment,
99                                                uint32_t domains,
100                                                uint32_t flags,
101                                                const char *file,
102                                                const char *func,
103                                                int line)
104{
105    struct radeon_bo *bo;
106
107    bo = bom->funcs->bo_open(bom, handle, size, alignment, domains, flags);
108#ifdef RADEON_BO_TRACK
109    if (bo) {
110        bo->track = radeon_tracker_add_track(&bom->tracker, bo->handle);
111        radeon_track_add_event(bo->track, file, func, "open", line);
112    }
113#endif
114    return bo;
115}
116
117static inline void _radeon_bo_ref(struct radeon_bo *bo,
118                                  const char *file,
119                                  const char *func,
120                                  int line)
121{
122    bo->cref++;
123#ifdef RADEON_BO_TRACK
124    radeon_track_add_event(bo->track, file, func, "ref", line);
125#endif
126    bo->bom->funcs->bo_ref(bo);
127}
128
129static inline struct radeon_bo *_radeon_bo_unref(struct radeon_bo *bo,
130                                                 const char *file,
131                                                 const char *func,
132                                                 int line)
133{
134    bo->cref--;
135#ifdef RADEON_BO_TRACK
136    radeon_track_add_event(bo->track, file, func, "unref", line);
137    if (bo->cref <= 0) {
138        radeon_tracker_remove_track(&bo->bom->tracker, bo->track);
139        bo->track = NULL;
140    }
141#endif
142    return bo->bom->funcs->bo_unref(bo);
143}
144
145static inline int _radeon_bo_map(struct radeon_bo *bo,
146                                 int write,
147                                 const char *file,
148                                 const char *func,
149                                 int line)
150{
151    return bo->bom->funcs->bo_map(bo, write);
152}
153
154static inline int _radeon_bo_unmap(struct radeon_bo *bo,
155                                   const char *file,
156                                   const char *func,
157                                   int line)
158{
159    return bo->bom->funcs->bo_unmap(bo);
160}
161
162static inline int _radeon_bo_wait(struct radeon_bo *bo,
163                                  const char *file,
164                                  const char *func,
165                                  int line)
166{
167    return bo->bom->funcs->bo_wait(bo);
168}
169
170static inline int _radeon_bo_is_busy(struct radeon_bo *bo,
171				     uint32_t *domain,
172                                     const char *file,
173                                     const char *func,
174                                     int line)
175{
176    return bo->bom->funcs->bo_is_busy(bo, domain);
177}
178
179static inline int radeon_bo_set_tiling(struct radeon_bo *bo,
180				       uint32_t tiling_flags, uint32_t pitch)
181{
182    return bo->bom->funcs->bo_set_tiling(bo, tiling_flags, pitch);
183}
184
185static inline int radeon_bo_get_tiling(struct radeon_bo *bo,
186				       uint32_t *tiling_flags, uint32_t *pitch)
187{
188    return bo->bom->funcs->bo_get_tiling(bo, tiling_flags, pitch);
189}
190
191static inline int radeon_bo_is_static(struct radeon_bo *bo)
192{
193    if (bo->bom->funcs->bo_is_static)
194	return bo->bom->funcs->bo_is_static(bo);
195    return 0;
196}
197
198#define radeon_bo_open(bom, h, s, a, d, f)\
199    _radeon_bo_open(bom, h, s, a, d, f, __FILE__, __FUNCTION__, __LINE__)
200#define radeon_bo_ref(bo)\
201    _radeon_bo_ref(bo, __FILE__, __FUNCTION__, __LINE__)
202#define radeon_bo_unref(bo)\
203    _radeon_bo_unref(bo, __FILE__, __FUNCTION__, __LINE__)
204#define radeon_bo_map(bo, w)\
205    _radeon_bo_map(bo, w, __FILE__, __FUNCTION__, __LINE__)
206#define radeon_bo_unmap(bo)\
207    _radeon_bo_unmap(bo, __FILE__, __FUNCTION__, __LINE__)
208#define radeon_bo_debug(bo, opcode)\
209    _radeon_bo_debug(bo, opcode, __FILE__, __FUNCTION__, __LINE__)
210#define radeon_bo_wait(bo) \
211    _radeon_bo_wait(bo, __FILE__, __func__, __LINE__)
212#define radeon_bo_is_busy(bo, domain) \
213    _radeon_bo_is_busy(bo, domain, __FILE__, __func__, __LINE__)
214
215#endif
216