nouveau: add BEGIN_RING_NI
[profile/ivi/libdrm.git] / nouveau / nouveau_pushbuf.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_PUSHBUF_H__
24 #define __NOUVEAU_PUSHBUF_H__
25
26 #include <assert.h>
27 #include <string.h>
28
29 #include "nouveau_bo.h"
30 #include "nouveau_grobj.h"
31
32 struct nouveau_pushbuf {
33         struct nouveau_channel *channel;
34
35         unsigned remaining;
36         uint32_t *cur;
37 };
38
39 int
40 nouveau_pushbuf_flush(struct nouveau_channel *, unsigned min);
41
42 int
43 nouveau_pushbuf_marker_emit(struct nouveau_channel *chan,
44                             unsigned wait_dwords, unsigned wait_relocs);
45
46 void
47 nouveau_pushbuf_marker_undo(struct nouveau_channel *chan);
48
49 int
50 nouveau_pushbuf_emit_reloc(struct nouveau_channel *, void *ptr,
51                            struct nouveau_bo *, uint32_t data, uint32_t data2,
52                            uint32_t flags, uint32_t vor, uint32_t tor);
53
54 /* Push buffer access macros */
55 static __inline__ int
56 MARK_RING(struct nouveau_channel *chan, unsigned dwords, unsigned relocs)
57 {
58         return nouveau_pushbuf_marker_emit(chan, dwords, relocs);
59 }
60
61 static __inline__ void
62 MARK_UNDO(struct nouveau_channel *chan)
63 {
64         nouveau_pushbuf_marker_undo(chan);
65 }
66
67 static __inline__ void
68 OUT_RING(struct nouveau_channel *chan, unsigned data)
69 {
70         *(chan->pushbuf->cur++) = (data);
71 }
72
73 static __inline__ void
74 OUT_RINGp(struct nouveau_channel *chan, const void *data, unsigned size)
75 {
76         memcpy(chan->pushbuf->cur, data, size * 4);
77         chan->pushbuf->cur += size;
78 }
79
80 static __inline__ void
81 OUT_RINGf(struct nouveau_channel *chan, float f)
82 {
83         union { uint32_t i; float f; } c;
84         c.f = f;
85         OUT_RING(chan, c.i);
86 }
87
88 static __inline__ unsigned
89 AVAIL_RING(struct nouveau_channel *chan)
90 {
91         return chan->pushbuf->remaining;
92 }
93
94 static __inline__ void
95 WAIT_RING(struct nouveau_channel *chan, unsigned size)
96 {
97         if (chan->pushbuf->remaining < size)
98                 nouveau_pushbuf_flush(chan, size);
99 }
100
101 static __inline__ void
102 BEGIN_RING(struct nouveau_channel *chan, struct nouveau_grobj *gr,
103            unsigned mthd, unsigned size)
104 {
105         if (gr->bound == NOUVEAU_GROBJ_UNBOUND)
106                 nouveau_grobj_autobind(gr);
107         chan->subc[gr->subc].sequence = chan->subc_sequence++;
108
109         WAIT_RING(chan, size + 1);
110         OUT_RING(chan, (gr->subc << 13) | (size << 18) | mthd);
111         chan->pushbuf->remaining -= (size + 1);
112 }
113
114 /* non-incrementing BEGIN_RING */
115 static __inline__ void
116 BEGIN_RING_NI(struct nouveau_channel *chan, struct nouveau_grobj *gr,
117            unsigned mthd, unsigned size)
118 {
119         BEGIN_RING(chan, gr, mthd | 0x40000000, size);
120 }
121
122 static __inline__ void
123 FIRE_RING(struct nouveau_channel *chan)
124 {
125         nouveau_pushbuf_flush(chan, 0);
126 }
127
128 static __inline__ void
129 BIND_RING(struct nouveau_channel *chan, struct nouveau_grobj *gr, unsigned sc)
130 {
131         struct nouveau_subchannel *subc = &gr->channel->subc[sc];
132         
133         if (subc->gr) {
134                 if (subc->gr->bound == NOUVEAU_GROBJ_BOUND_EXPLICIT)
135                         assert(0);
136                 subc->gr->bound = NOUVEAU_GROBJ_UNBOUND;
137         }
138         subc->gr = gr;
139         subc->gr->subc = sc;
140         subc->gr->bound = NOUVEAU_GROBJ_BOUND_EXPLICIT;
141
142         BEGIN_RING(chan, gr, 0x0000, 1);
143         OUT_RING  (chan, gr->handle);
144 }
145
146 static __inline__ int
147 OUT_RELOC(struct nouveau_channel *chan, struct nouveau_bo *bo,
148           unsigned data, unsigned flags, unsigned vor, unsigned tor)
149 {
150         return nouveau_pushbuf_emit_reloc(chan, chan->pushbuf->cur++, bo,
151                                           data, 0, flags, vor, tor);
152 }
153
154 static __inline__ int
155 OUT_RELOC2(struct nouveau_channel *chan, struct nouveau_bo *bo,
156            unsigned data, unsigned data2, unsigned flags,
157            unsigned vor, unsigned tor)
158 {
159         return nouveau_pushbuf_emit_reloc(chan, chan->pushbuf->cur++, bo,
160                                           data, data2, flags, vor, tor);
161 }
162
163 /* Raw data + flags depending on FB/TT buffer */
164 static __inline__ int
165 OUT_RELOCd(struct nouveau_channel *chan, struct nouveau_bo *bo,
166            unsigned data, unsigned flags, unsigned vor, unsigned tor)
167 {
168         return OUT_RELOC(chan, bo, data, flags | NOUVEAU_BO_OR, vor, tor);
169 }
170
171 /* FB/TT object handle */
172 static __inline__ int
173 OUT_RELOCo(struct nouveau_channel *chan, struct nouveau_bo *bo,
174            unsigned flags)
175 {
176         return OUT_RELOC(chan, bo, 0, flags | NOUVEAU_BO_OR,
177                          chan->vram->handle, chan->gart->handle);
178 }
179
180 /* Low 32-bits of offset */
181 static __inline__ int
182 OUT_RELOCl(struct nouveau_channel *chan, struct nouveau_bo *bo,
183            unsigned delta, unsigned flags)
184 {
185         return OUT_RELOC(chan, bo, delta, flags | NOUVEAU_BO_LOW, 0, 0);
186 }
187
188 /* Low 32-bits of offset + GPU linear access range info */
189 static __inline__ int
190 OUT_RELOCr(struct nouveau_channel *chan, struct nouveau_bo *bo,
191            unsigned delta, unsigned size, unsigned flags)
192 {
193         return OUT_RELOC2(chan, bo, delta, size, flags | NOUVEAU_BO_LOW, 0, 0);
194 }
195
196 /* High 32-bits of offset */
197 static __inline__ int
198 OUT_RELOCh(struct nouveau_channel *chan, struct nouveau_bo *bo,
199            unsigned delta, unsigned flags)
200 {
201         return OUT_RELOC(chan, bo, delta, flags | NOUVEAU_BO_HIGH, 0, 0);
202 }
203
204 #endif