fence: ref could destroy the object it was referencing, fix that!
[nouveau] / src / nouveau_local.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_LOCAL_H__
24 #define __NOUVEAU_LOCAL_H__
25
26 #include "compiler.h"
27 #include "xf86_OSproc.h"
28
29 #define NOUVEAU_PRIVATE _X_HIDDEN
30 #define NOUVEAU_PUBLIC _X_EXPORT
31
32 struct nouveau_pixmap {
33         struct nouveau_bo *bo;
34         int mapped;
35 };
36
37 /* Debug output */
38 #define NOUVEAU_MSG(fmt,args...) ErrorF(fmt, ##args)
39 #define NOUVEAU_ERR(fmt,args...) \
40         ErrorF("%s:%d - "fmt, __func__, __LINE__, ##args)
41 #if 0
42 #define NOUVEAU_FALLBACK(fmt,args...) do {    \
43         NOUVEAU_ERR("FALLBACK: "fmt, ##args); \
44         return FALSE;                         \
45 } while(0)
46 #else
47 #define NOUVEAU_FALLBACK(fmt,args...) do {    \
48         return FALSE;                         \
49 } while(0)
50 #endif
51
52 #define NOUVEAU_TIME_MSEC() GetTimeInMillis()
53
54 #define NOUVEAU_ALIGN(x,bytes) (((x) + ((bytes) - 1)) & ~((bytes) - 1))
55
56 /* User FIFO control */
57 //#define NOUVEAU_DMA_TRACE
58 //#define NOUVEAU_DMA_DEBUG
59 //#define NOUVEAU_DMA_DUMP_POSTRELOC_PUSHBUF
60 #define NOUVEAU_DMA_BARRIER mem_barrier();
61 #define NOUVEAU_DMA_TIMEOUT 2000
62
63 #include "nouveau_channel.h"
64 #include "nouveau_pushbuf.h"
65 #include "nouveau_grobj.h"
66 #include "nouveau_bo.h"
67
68 NOUVEAU_PRIVATE int
69 nouveau_pushbuf_flush(struct nouveau_channel *, unsigned);
70 NOUVEAU_PRIVATE int
71 nouveau_pushbuf_emit_reloc(struct nouveau_channel *, void *ptr,
72                            struct nouveau_bo *, uint32_t data, uint32_t flags,
73                            uint32_t vor, uint32_t tor);
74 NOUVEAU_PRIVATE void
75 nouveau_grobj_autobind(struct nouveau_grobj *);
76
77 /* Push buffer access macros */
78 static __inline__ void
79 OUT_RING(struct nouveau_channel *chan, unsigned data)
80 {
81         *(chan->pushbuf->cur++) = (data);
82 }
83
84 static __inline__ void
85 OUT_RINGp(struct nouveau_channel *chan, const void *data, unsigned size)
86 {
87         memcpy(chan->pushbuf->cur, data, size * 4);
88         chan->pushbuf->cur += size;
89 }
90
91 static __inline__ void
92 OUT_RINGf(struct nouveau_channel *chan, float f)
93 {
94         union { uint32_t i; float f; } c;
95         c.f = f;
96         OUT_RING(chan, c.i);
97 }
98
99 static __inline__ void
100 RING_SPACE(struct nouveau_channel *chan, unsigned size)
101 {
102         if (chan->pushbuf->remaining < size)
103                 nouveau_pushbuf_flush(chan, size);
104 }
105
106 static __inline__ void
107 BEGIN_RING(struct nouveau_channel *chan, struct nouveau_grobj *gr,
108            unsigned mthd, unsigned size)
109 {
110         if (gr->bound == NOUVEAU_GROBJ_UNBOUND)
111                 nouveau_grobj_autobind(gr);
112         chan->subc[gr->subc].sequence = chan->subc_sequence++;
113
114         RING_SPACE(chan, size + 1);
115         OUT_RING(chan, (gr->subc << 13) | (size << 18) | mthd);
116         chan->pushbuf->remaining -= (size + 1);
117 }
118
119 static __inline__ void
120 FIRE_RING(struct nouveau_channel *chan)
121 {
122         nouveau_pushbuf_flush(chan, 0);
123 }
124
125 static __inline__ void
126 BIND_RING(struct nouveau_channel *chan, struct nouveau_grobj *gr, unsigned sc)
127 {
128         struct nouveau_subchannel *subc = &gr->channel->subc[sc];
129         
130         if (subc->gr) {
131                 if (subc->gr->bound == NOUVEAU_GROBJ_BOUND_EXPLICIT)
132                         assert(0);
133                 subc->gr->bound = NOUVEAU_GROBJ_UNBOUND;
134         }
135         subc->gr = gr;
136         subc->gr->subc = sc;
137         subc->gr->bound = NOUVEAU_GROBJ_BOUND_EXPLICIT;
138
139         BEGIN_RING(chan, gr, 0x0000, 1);
140         OUT_RING  (chan, gr->handle);
141 }
142
143 static __inline__ void
144 OUT_RELOC(struct nouveau_channel *chan, struct nouveau_bo *bo,
145           unsigned data, unsigned flags, unsigned vor, unsigned tor)
146 {
147         nouveau_pushbuf_emit_reloc(chan, chan->pushbuf->cur++, bo,
148                                    data, flags, vor, tor);
149 }
150
151 /* Raw data + flags depending on FB/TT buffer */
152 static __inline__ void
153 OUT_RELOCd(struct nouveau_channel *chan, struct nouveau_bo *bo,
154            unsigned data, unsigned flags, unsigned vor, unsigned tor)
155 {
156         OUT_RELOC(chan, bo, data, flags | NOUVEAU_BO_OR, vor, tor);
157 }
158
159 /* FB/TT object handle */
160 static __inline__ void
161 OUT_RELOCo(struct nouveau_channel *chan, struct nouveau_bo *bo,
162            unsigned flags)
163 {
164         OUT_RELOC(chan, bo, 0, flags | NOUVEAU_BO_OR,
165                   chan->vram->handle, chan->gart->handle);
166 }
167
168 /* Low 32-bits of offset */
169 static __inline__ void
170 OUT_RELOCl(struct nouveau_channel *chan, struct nouveau_bo *bo,
171            unsigned delta, unsigned flags)
172 {
173         OUT_RELOC(chan, bo, delta, flags | NOUVEAU_BO_LOW, 0, 0);
174 }
175
176 /* High 32-bits of offset */
177 static __inline__ void
178 OUT_RELOCh(struct nouveau_channel *chan, struct nouveau_bo *bo,
179            unsigned delta, unsigned flags)
180 {
181         OUT_RELOC(chan, bo, delta, flags | NOUVEAU_BO_HIGH, 0, 0);
182 }
183
184 /* Alternate versions of OUT_RELOCx above, takes pixmaps instead of BOs */
185 #define OUT_PIXMAPd(chan,pm,data,flags,vor,tor) do {                           \
186         struct nouveau_pixmap *nvpix = exaGetPixmapDriverPrivate((pm));        \
187         struct nouveau_bo *pmo = nvpix->bo;                                    \
188         OUT_RELOCd((chan), pmo, (data), (flags), (vor), (tor));                \
189 } while(0)
190 #define OUT_PIXMAPo(chan,pm,flags) do {                                        \
191         struct nouveau_pixmap *nvpix = exaGetPixmapDriverPrivate((pm));        \
192         struct nouveau_bo *pmo = nvpix->bo;                                    \
193         OUT_RELOCo((chan), pmo, (flags));                                      \
194 } while(0)
195 #define OUT_PIXMAPl(chan,pm,delta,flags) do {                                  \
196         struct nouveau_pixmap *nvpix = exaGetPixmapDriverPrivate((pm));        \
197         struct nouveau_bo *pmo = nvpix->bo;                                    \
198         OUT_RELOCl((chan), pmo, (delta), (flags));                             \
199 } while(0)
200 #define OUT_PIXMAPh(chan,pm,delta,flags) do {                                  \
201         struct nouveau_pixmap *nvpix = exaGetPixmapDriverPrivate((pm));        \
202         struct nouveau_bo *pmo = nvpix->bo;                                    \
203         OUT_RELOCh((chan), pmo, (delta), (flags));                             \
204 } while(0)
205
206 #endif