nouveau: switch to libdrm_nouveau-2.0
[mesa.git] / src / gallium / drivers / nv50 / nv50_context.c
1 /*
2 * Copyright 2010 Christoph Bumiller
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 #include "draw/draw_context.h"
24 #include "pipe/p_defines.h"
25
26 #include "nv50_context.h"
27 #include "nv50_screen.h"
28 #include "nv50_resource.h"
29
30 #include "nouveau/nouveau_reloc.h"
31
32 static void
33 nv50_flush(struct pipe_context *pipe,
34 struct pipe_fence_handle **fence)
35 {
36 struct nouveau_screen *screen = nouveau_screen(pipe->screen);
37
38 if (fence)
39 nouveau_fence_ref(screen->fence.current, (struct nouveau_fence **)fence);
40
41 PUSH_KICK(screen->pushbuf);
42 }
43
44 static void
45 nv50_texture_barrier(struct pipe_context *pipe)
46 {
47 struct nouveau_pushbuf *push = nv50_context(pipe)->base.pushbuf;
48
49 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
50 PUSH_DATA (push, 0);
51 BEGIN_NV04(push, NV50_3D(TEX_CACHE_CTL), 1);
52 PUSH_DATA (push, 0x20);
53 }
54
55 void
56 nv50_default_kick_notify(struct nouveau_pushbuf *push)
57 {
58 struct nv50_screen *screen = push->user_priv;
59
60 if (screen) {
61 nouveau_fence_next(&screen->base);
62 nouveau_fence_update(&screen->base, TRUE);
63 if (screen->cur_ctx)
64 screen->cur_ctx->state.flushed = TRUE;
65 }
66 }
67
68 static void
69 nv50_context_unreference_resources(struct nv50_context *nv50)
70 {
71 unsigned s, i;
72
73 nouveau_bufctx_del(&nv50->bufctx_3d);
74 nouveau_bufctx_del(&nv50->bufctx);
75
76 for (i = 0; i < nv50->num_vtxbufs; ++i)
77 pipe_resource_reference(&nv50->vtxbuf[i].buffer, NULL);
78
79 pipe_resource_reference(&nv50->idxbuf.buffer, NULL);
80
81 for (s = 0; s < 3; ++s) {
82 for (i = 0; i < nv50->num_textures[s]; ++i)
83 pipe_sampler_view_reference(&nv50->textures[s][i], NULL);
84
85 for (i = 0; i < 16; ++i)
86 pipe_resource_reference(&nv50->constbuf[s][i], NULL);
87 }
88 }
89
90 static void
91 nv50_destroy(struct pipe_context *pipe)
92 {
93 struct nv50_context *nv50 = nv50_context(pipe);
94
95 if (nv50_context_screen(nv50)->cur_ctx == nv50) {
96 nv50->base.pushbuf->kick_notify = NULL;
97 nv50_context_screen(nv50)->cur_ctx = NULL;
98 nouveau_pushbuf_bufctx(nv50->base.pushbuf, NULL);
99 }
100 /* need to flush before destroying the bufctx */
101 nouveau_pushbuf_kick(nv50->base.pushbuf, nv50->base.pushbuf->channel);
102
103 nv50_context_unreference_resources(nv50);
104
105 draw_destroy(nv50->draw);
106
107 FREE(nv50);
108 }
109
110 struct pipe_context *
111 nv50_create(struct pipe_screen *pscreen, void *priv)
112 {
113 struct nv50_screen *screen = nv50_screen(pscreen);
114 struct nv50_context *nv50;
115 struct pipe_context *pipe;
116 int ret;
117 uint32_t flags;
118
119 nv50 = CALLOC_STRUCT(nv50_context);
120 if (!nv50)
121 return NULL;
122 pipe = &nv50->base.pipe;
123
124 nv50->base.pushbuf = screen->base.pushbuf;
125
126 ret = nouveau_bufctx_new(screen->base.client, NV50_BIND_COUNT,
127 &nv50->bufctx_3d);
128 if (!ret)
129 ret = nouveau_bufctx_new(screen->base.client, 2, &nv50->bufctx);
130 if (ret)
131 goto out_err;
132
133 nv50->base.screen = &screen->base;
134 nv50->base.copy_data = nv50_m2mf_copy_linear;
135 nv50->base.push_data = nv50_sifc_linear_u8;
136
137 nv50->screen = screen;
138 pipe->screen = pscreen;
139 pipe->priv = priv;
140
141 pipe->destroy = nv50_destroy;
142
143 pipe->draw_vbo = nv50_draw_vbo;
144 pipe->clear = nv50_clear;
145
146 pipe->flush = nv50_flush;
147 pipe->texture_barrier = nv50_texture_barrier;
148
149 if (!screen->cur_ctx) {
150 screen->cur_ctx = nv50;
151 nouveau_pushbuf_bufctx(screen->base.pushbuf, nv50->bufctx);
152 }
153
154 nv50_init_query_functions(nv50);
155 nv50_init_surface_functions(nv50);
156 nv50_init_state_functions(nv50);
157 nv50_init_resource_functions(pipe);
158
159 nv50->draw = draw_create(pipe);
160 assert(nv50->draw);
161 draw_set_rasterize_stage(nv50->draw, nv50_draw_render_stage(nv50));
162
163 nouveau_context_init_vdec(&nv50->base);
164
165 flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
166
167 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->code);
168 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->uniforms);
169 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->txc);
170 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->stack_bo);
171
172 flags = NOUVEAU_BO_GART | NOUVEAU_BO_WR;
173
174 BCTX_REFN_bo(nv50->bufctx_3d, SCREEN, flags, screen->fence.bo);
175 BCTX_REFN_bo(nv50->bufctx, FENCE, flags, screen->fence.bo);
176
177 return pipe;
178
179 out_err:
180 if (nv50) {
181 if (nv50->bufctx_3d)
182 nouveau_bufctx_del(&nv50->bufctx_3d);
183 if (nv50->bufctx)
184 nouveau_bufctx_del(&nv50->bufctx);
185 FREE(nv50);
186 }
187 return NULL;
188 }
189
190 void
191 nv50_bufctx_fence(struct nouveau_bufctx *bufctx, boolean on_flush)
192 {
193 struct nouveau_list *list = on_flush ? &bufctx->current : &bufctx->pending;
194 struct nouveau_list *it;
195
196 for (it = list->next; it != list; it = it->next) {
197 struct nouveau_bufref *ref = (struct nouveau_bufref *)it;
198 struct nv04_resource *res = ref->priv;
199 if (res)
200 nv50_resource_validate(res, (unsigned)ref->priv_data);
201 }
202 }