bo: make ref/del work like the rest
[nouveau] / src / nouveau_drmif.h
1 /*
2  * Copyright 2007 Nouveau Project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #ifndef __NOUVEAU_DRMIF_H__
24 #define __NOUVEAU_DRMIF_H__
25
26 #include <stdint.h>
27 #include <xf86drm.h>
28 #include <nouveau_drm.h>
29
30 #include "nouveau_device.h"
31 #include "nouveau_channel.h"
32 #include "nouveau_grobj.h"
33 #include "nouveau_notifier.h"
34 #include "nouveau_bo.h"
35 #include "nouveau_resource.h"
36 #include "nouveau_pushbuf.h"
37 #include "nouveau_local.h"
38
39 struct nouveau_device_priv {
40         struct nouveau_device base;
41
42         int fd;
43         drm_context_t ctx;
44         drmLock *lock;
45         int needs_close;
46 };
47 #define nouveau_device(n) ((struct nouveau_device_priv *)(n))
48
49 NOUVEAU_PRIVATE int
50 nouveau_device_open_existing(struct nouveau_device **, int close,
51                              int fd, drm_context_t ctx);
52
53 NOUVEAU_PRIVATE int
54 nouveau_device_open(struct nouveau_device **, const char *busid);
55
56 NOUVEAU_PRIVATE void
57 nouveau_device_close(struct nouveau_device **);
58
59 NOUVEAU_PRIVATE int
60 nouveau_device_get_param(struct nouveau_device *, uint64_t param, uint64_t *v);
61
62 NOUVEAU_PRIVATE int
63 nouveau_device_set_param(struct nouveau_device *, uint64_t param, uint64_t val);
64
65 struct nouveau_fence {
66         struct nouveau_channel *channel;
67 };
68
69 struct nouveau_fence_cb {
70         struct nouveau_fence_cb *next;
71         void (*func)(void *);
72         void *priv;
73 };
74
75 struct nouveau_fence_priv {
76         struct nouveau_fence base;
77         int refcount;
78
79         struct nouveau_fence *next;
80         struct nouveau_fence_cb *signal_cb;
81
82         uint32_t sequence;
83         int emitted;
84         int signalled;
85 };
86 #define nouveau_fence(n) ((struct nouveau_fence_priv *)(n))
87
88 NOUVEAU_PRIVATE int
89 nouveau_fence_new(struct nouveau_channel *, struct nouveau_fence **);
90
91 NOUVEAU_PRIVATE int
92 nouveau_fence_ref(struct nouveau_fence *, struct nouveau_fence **);
93
94 NOUVEAU_PRIVATE int
95 nouveau_fence_signal_cb(struct nouveau_fence *, void (*)(void *), void *);
96
97 NOUVEAU_PRIVATE void
98 nouveau_fence_emit(struct nouveau_fence *);
99
100 NOUVEAU_PRIVATE int
101 nouveau_fence_wait(struct nouveau_fence **);
102
103 NOUVEAU_PRIVATE void
104 nouveau_fence_flush(struct nouveau_channel *);
105
106 #define NOUVEAU_PUSHBUF_MAX_BUFFERS 1024
107 #define NOUVEAU_PUSHBUF_MAX_RELOCS 1024
108 struct nouveau_pushbuf_priv {
109         struct nouveau_pushbuf base;
110
111         unsigned *pushbuf;
112         unsigned  size;
113
114         struct drm_nouveau_gem_pushbuf_bo *buffers;
115         unsigned nr_buffers;
116         struct drm_nouveau_gem_pushbuf_reloc *relocs;
117         unsigned nr_relocs;
118 };
119 #define nouveau_pushbuf(n) ((struct nouveau_pushbuf_priv *)(n))
120
121 #define pbbo_to_ptr(o) ((uint64_t)(unsigned long)(o))
122 #define ptr_to_pbbo(h) ((struct nouveau_pushbuf_bo *)(unsigned long)(h))
123 #define pbrel_to_ptr(o) ((uint64_t)(unsigned long)(o))
124 #define ptr_to_pbrel(h) ((struct nouveau_pushbuf_reloc *)(unsigned long)(h))
125 #define bo_to_ptr(o) ((uint64_t)(unsigned long)(o))
126 #define ptr_to_bo(h) ((struct nouveau_bo_priv *)(unsigned long)(h))
127
128 NOUVEAU_PRIVATE int
129 nouveau_pushbuf_init(struct nouveau_channel *);
130
131 NOUVEAU_PRIVATE int
132 nouveau_pushbuf_flush(struct nouveau_channel *, unsigned min);
133
134 NOUVEAU_PRIVATE int
135 nouveau_pushbuf_emit_reloc(struct nouveau_channel *, void *ptr,
136                            struct nouveau_bo *, uint32_t data, uint32_t flags,
137                            uint32_t vor, uint32_t tor);
138
139 struct nouveau_channel_priv {
140         struct nouveau_channel base;
141
142         struct drm_nouveau_channel_alloc drm;
143
144         void     *notifier_block;
145
146         struct nouveau_fence *fence_head;
147         struct nouveau_fence *fence_tail;
148         uint32_t fence_sequence;
149         /* NV04 hackery */
150         struct nouveau_grobj *fence_grobj;
151         struct nouveau_notifier *fence_ntfy;
152
153         struct nouveau_pushbuf_priv pb;
154
155         unsigned user_charge;
156 };
157 #define nouveau_channel(n) ((struct nouveau_channel_priv *)(n))
158
159 NOUVEAU_PRIVATE int
160 nouveau_channel_alloc(struct nouveau_device *, uint32_t fb, uint32_t tt,
161                       struct nouveau_channel **);
162
163 NOUVEAU_PRIVATE void
164 nouveau_channel_free(struct nouveau_channel **);
165
166 struct nouveau_grobj_priv {
167         struct nouveau_grobj base;
168 };
169 #define nouveau_grobj(n) ((struct nouveau_grobj_priv *)(n))
170
171 NOUVEAU_PRIVATE int nouveau_grobj_alloc(struct nouveau_channel *, uint32_t handle,
172                                int class, struct nouveau_grobj **);
173 NOUVEAU_PRIVATE int nouveau_grobj_ref(struct nouveau_channel *, uint32_t handle,
174                              struct nouveau_grobj **);
175 NOUVEAU_PRIVATE void nouveau_grobj_free(struct nouveau_grobj **);
176
177
178 struct nouveau_notifier_priv {
179         struct nouveau_notifier base;
180
181         struct drm_nouveau_notifierobj_alloc drm;
182         volatile void *map;
183 };
184 #define nouveau_notifier(n) ((struct nouveau_notifier_priv *)(n))
185
186 NOUVEAU_PRIVATE int
187 nouveau_notifier_alloc(struct nouveau_channel *, uint32_t handle, int count,
188                        struct nouveau_notifier **);
189
190 NOUVEAU_PRIVATE void
191 nouveau_notifier_free(struct nouveau_notifier **);
192
193 NOUVEAU_PRIVATE void
194 nouveau_notifier_reset(struct nouveau_notifier *, int id);
195
196 NOUVEAU_PRIVATE uint32_t
197 nouveau_notifier_status(struct nouveau_notifier *, int id);
198
199 NOUVEAU_PRIVATE uint32_t
200 nouveau_notifier_return_val(struct nouveau_notifier *, int id);
201
202 NOUVEAU_PRIVATE int
203 nouveau_notifier_wait_status(struct nouveau_notifier *, int id, int status,
204                              int timeout);
205
206 struct nouveau_bo_priv {
207         struct nouveau_bo base;
208         int refcount;
209
210         /* Buffer configuration + usage hints */
211         unsigned flags;
212         unsigned size;
213         unsigned align;
214         int user;
215
216         /* Tracking */
217         struct drm_nouveau_gem_pushbuf_bo *pending;
218         struct nouveau_channel *pending_channel;
219         struct nouveau_fence *fence;
220         struct nouveau_fence *wr_fence;
221
222         /* Userspace object */
223         void *sysmem;
224
225         /* Kernel object */
226         uint32_t global_handle;
227         unsigned handle;
228         void *map;
229
230         /* Last known information from kernel on buffer status */
231         int pinned;
232         uint64_t offset;
233         uint32_t domain;
234 };
235 #define nouveau_bo(n) ((struct nouveau_bo_priv *)(n))
236
237 NOUVEAU_PRIVATE int
238 nouveau_bo_init(struct nouveau_device *);
239
240 NOUVEAU_PRIVATE void
241 nouveau_bo_takedown(struct nouveau_device *);
242
243 NOUVEAU_PRIVATE int
244 nouveau_bo_new(struct nouveau_device *, uint32_t flags, int align, int size,
245                struct nouveau_bo **);
246
247 NOUVEAU_PRIVATE int
248 nouveau_bo_user(struct nouveau_device *, void *ptr, int size,
249                 struct nouveau_bo **);
250
251 NOUVEAU_PRIVATE int
252 nouveau_bo_ref_handle(struct nouveau_device *, uint32_t handle,
253                       struct nouveau_bo **);
254
255 NOUVEAU_PRIVATE int
256 nouveau_bo_ref(struct nouveau_bo *, struct nouveau_bo **);
257
258 NOUVEAU_PRIVATE int
259 nouveau_bo_map(struct nouveau_bo *, uint32_t flags);
260
261 NOUVEAU_PRIVATE void
262 nouveau_bo_unmap(struct nouveau_bo *);
263
264 NOUVEAU_PRIVATE int
265 nouveau_bo_pin(struct nouveau_bo *, uint32_t flags);
266
267 NOUVEAU_PRIVATE void
268 nouveau_bo_unpin(struct nouveau_bo *);
269
270 NOUVEAU_PRIVATE uint64_t
271 nouveau_bo_get_drm_map(struct nouveau_bo *);
272
273 NOUVEAU_PRIVATE int
274 nouveau_resource_init(struct nouveau_resource **heap, unsigned start,
275                       unsigned size);
276
277 NOUVEAU_PRIVATE int
278 nouveau_resource_alloc(struct nouveau_resource *heap, int size, void *priv,
279                        struct nouveau_resource **);
280
281 NOUVEAU_PRIVATE void
282 nouveau_resource_free(struct nouveau_resource **);
283
284 NOUVEAU_PRIVATE struct drm_nouveau_gem_pushbuf_bo *
285 nouveau_bo_emit_buffer(struct nouveau_channel *, struct nouveau_bo *);
286
287 #endif