summaryrefslogtreecommitdiff
path: root/src/gallium/winsys/g3dvl/nouveau/nouveau_context.c
blob: 06a61fcda3e613b24460094f4ad2b03211cc5c4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "pipe/p_defines.h"
#include "pipe/p_context.h"
#include "pipe/p_screen.h"
#include "util/u_memory.h"

#include "nouveau_context.h"
#include "nouveau_dri.h"
#include "nouveau_local.h"
#include "nouveau_screen.h"
#include "nouveau_winsys_pipe.h"

/*
#ifdef DEBUG
static const struct dri_debug_control debug_control[] = {
	{ "bo", DEBUG_BO },
	{ NULL, 0 }
};
int __nouveau_debug = 0;
#endif
*/

/*
 * TODO: Re-examine dri_screen, dri_context, nouveau_screen, nouveau_context
 * relationships, seems like there is a lot of room for simplification there.
 */

static void
nouveau_channel_context_destroy(struct nouveau_channel_context *nvc)
{
	nouveau_grobj_free(&nvc->NvCtxSurf2D);
	nouveau_grobj_free(&nvc->NvImageBlit);
	nouveau_grobj_free(&nvc->NvGdiRect);
	nouveau_grobj_free(&nvc->NvM2MF);
	nouveau_grobj_free(&nvc->Nv2D);
	nouveau_grobj_free(&nvc->NvSwzSurf);
	nouveau_grobj_free(&nvc->NvSIFM);

	nouveau_notifier_free(&nvc->sync_notifier);

	nouveau_channel_free(&nvc->channel);

	FREE(nvc);
}

static struct nouveau_channel_context *
nouveau_channel_context_create(struct nouveau_device *dev)
{
	struct nouveau_channel_context *nvc;
	int ret;

	nvc = CALLOC_STRUCT(nouveau_channel_context);
	if (!nvc)
		return NULL;

	if ((ret = nouveau_channel_alloc(dev, 0x8003d001, 0x8003d002,
					 &nvc->channel))) {
		NOUVEAU_ERR("Error creating GPU channel: %d\n", ret);
		nouveau_channel_context_destroy(nvc);
		return NULL;
	}

	nvc->next_handle = 0x80000000;

	if ((ret = nouveau_notifier_alloc(nvc->channel, nvc->next_handle++, 1,
					  &nvc->sync_notifier))) {
		NOUVEAU_ERR("Error creating channel sync notifier: %d\n", ret);
		nouveau_channel_context_destroy(nvc);
		return NULL;
	}

	switch (dev->chipset & 0xf0) {
	case 0x50:
	case 0x80:
	case 0x90:
		ret = nouveau_surface_channel_create_nv50(nvc);
		break;
	default:
		ret = nouveau_surface_channel_create_nv04(nvc);
		break;
	}

	if (ret) {
		NOUVEAU_ERR("Error initialising surface objects: %d\n", ret);
		nouveau_channel_context_destroy(nvc);
		return NULL;
	}

	return nvc;
}

int
nouveau_context_create(dri_context_t *dri_context)
{
	dri_screen_t			*dri_screen = dri_context->dri_screen;
	struct nouveau_screen 		*nv_screen = dri_screen->private;
	struct nouveau_context		*nv = CALLOC_STRUCT(nouveau_context);
	struct pipe_context		*pipe = NULL;
	struct nouveau_channel_context	*nvc = NULL;
	struct nouveau_device		*dev = nv_screen->device;
	int				i;

	switch (dev->chipset & 0xf0) {
	case 0x10:
	case 0x20:
		/* NV10 */
	case 0x30:
		/* NV30 */
	case 0x40:
	case 0x60:
		/* NV40 */
	case 0x50:
	case 0x80:
	case 0x90:
		/* G80 */
		break;
	default:
		NOUVEAU_ERR("Unsupported chipset: NV%02x\n", dev->chipset);
		return 1;
	}

	dri_context->private = (void*)nv;
	nv->dri_context = dri_context;
	nv->nv_screen  = nv_screen;

	{
		struct nouveau_device_priv *nvdev = nouveau_device(dev);

		nvdev->ctx  = dri_context->drm_context;
		nvdev->lock = (drmLock*)&dri_screen->sarea->lock;
	}

	/*
	driParseConfigFiles(&nv->dri_option_cache, &nv_screen->option_cache,
			    nv->dri_screen->myNum, "nouveau");
#ifdef DEBUG
	__nouveau_debug = driParseDebugString(getenv("NOUVEAU_DEBUG"),
					      debug_control);
#endif
	*/

	/*XXX: Hack up a fake region and buffer object for front buffer.
	 *     This will go away with TTM, replaced with a simple reference
	 *     of the front buffer handle passed to us by the DDX.
	 */
	{
		struct pipe_surface *fb_surf;
		struct nouveau_pipe_buffer *fb_buf;
		struct nouveau_bo_priv *fb_bo;

		fb_bo = calloc(1, sizeof(struct nouveau_bo_priv));
		fb_bo->drm.offset = nv_screen->front_offset;
		fb_bo->drm.flags = NOUVEAU_MEM_FB;
		fb_bo->drm.size = nv_screen->front_pitch *
				  nv_screen->front_height;
		fb_bo->refcount = 1;
		fb_bo->base.flags = NOUVEAU_BO_PIN | NOUVEAU_BO_VRAM;
		fb_bo->base.offset = fb_bo->drm.offset;
		fb_bo->base.handle = (unsigned long)fb_bo;
		fb_bo->base.size = fb_bo->drm.size;
		fb_bo->base.device = nv_screen->device;

		fb_buf = calloc(1, sizeof(struct nouveau_pipe_buffer));
		fb_buf->bo = &fb_bo->base;

		fb_surf = calloc(1, sizeof(struct pipe_surface));
		if (nv_screen->front_cpp == 2)
			fb_surf->format = PIPE_FORMAT_R5G6B5_UNORM;
		else
			fb_surf->format = PIPE_FORMAT_A8R8G8B8_UNORM;
		pf_get_block(fb_surf->format, &fb_surf->block);
		fb_surf->width = nv_screen->front_pitch / nv_screen->front_cpp;
		fb_surf->height = nv_screen->front_height;
		fb_surf->stride = fb_surf->width * fb_surf->block.size;
		fb_surf->refcount = 1;
		fb_surf->buffer = &fb_buf->base;

		nv->frontbuffer = fb_surf;
	}

	nvc = nv_screen->nvc;

	if (!nvc) {
		nvc = nouveau_channel_context_create(dev);
		if (!nvc) {
			NOUVEAU_ERR("Failed initialising GPU context\n");
			return 1;
		}
		nv_screen->nvc = nvc;
	}

	nvc->refcount++;
	nv->nvc = nvc;

	/* Find a free slot for a pipe context, allocate a new one if needed */
	nv->pctx_id = -1;
	for (i = 0; i < nvc->nr_pctx; i++) {
		if (nvc->pctx[i] == NULL) {
			nv->pctx_id = i;
			break;
		}
	}

	if (nv->pctx_id < 0) {
		nv->pctx_id = nvc->nr_pctx++;
		nvc->pctx =
			realloc(nvc->pctx,
				sizeof(struct pipe_context *) * nvc->nr_pctx);
	}

	/* Create pipe */
	switch (dev->chipset & 0xf0) {
	case 0x50:
	case 0x80:
	case 0x90:
		if (nouveau_surface_init_nv50(nv))
			return 1;
		break;
	default:
		if (nouveau_surface_init_nv04(nv))
			return 1;
		break;
	}

	if (!getenv("NOUVEAU_FORCE_SOFTPIPE")) {
		struct pipe_screen *pscreen;

		pipe = nouveau_pipe_create(nv);
		if (!pipe)
			NOUVEAU_ERR("Couldn't create hw pipe\n");
		pscreen = nvc->pscreen;

		nv->cap.hw_vertex_buffer =
			pscreen->get_param(pscreen, NOUVEAU_CAP_HW_VTXBUF);
		nv->cap.hw_index_buffer =
			pscreen->get_param(pscreen, NOUVEAU_CAP_HW_IDXBUF);
	}

	/* XXX: nouveau_winsys_softpipe needs a mesa header removed before we can compile it. */
	/*
	if (!pipe) {
		NOUVEAU_MSG("Using softpipe\n");
		pipe = nouveau_create_softpipe(nv);
		if (!pipe) {
			NOUVEAU_ERR("Error creating pipe, bailing\n");
			return 1;
		}
	}
	*/
	if (!pipe) {
		NOUVEAU_ERR("Error creating pipe, bailing\n");
		return 1;
	}

	pipe->priv = nv;

	return 0;
}

void
nouveau_context_destroy(dri_context_t *dri_context)
{
	struct nouveau_context *nv = dri_context->private;
	struct nouveau_channel_context *nvc = nv->nvc;

	assert(nv);

	if (nv->pctx_id >= 0) {
		nvc->pctx[nv->pctx_id] = NULL;
		if (--nvc->refcount <= 0) {
			nouveau_channel_context_destroy(nvc);
			nv->nv_screen->nvc = NULL;
		}
	}

	free(nv);
}

int
nouveau_context_bind(struct nouveau_context *nv, dri_drawable_t *dri_drawable)
{
	assert(nv);
	assert(dri_drawable);

	if (nv->dri_drawable != dri_drawable)
	{
		nv->dri_drawable = dri_drawable;
		dri_drawable->private = nv;
	}

	return 0;
}

int
nouveau_context_unbind(struct nouveau_context *nv)
{
	assert(nv);

	nv->dri_drawable = NULL;

	return 0;
}

/* Show starts here */

int bind_pipe_drawable(struct pipe_context *pipe, Drawable drawable)
{
	struct nouveau_context	*nv;
	dri_drawable_t		*dri_drawable;

	nv = pipe->priv;

	driCreateDrawable(nv->nv_screen->dri_screen, drawable, &dri_drawable);

	nouveau_context_bind(nv, dri_drawable);

	return 0;
}

int unbind_pipe_drawable(struct pipe_context *pipe)
{
	nouveau_context_unbind(pipe->priv);

	return 0;
}

struct pipe_context* create_pipe_context(Display *display, int screen)
{
	dri_screen_t		*dri_screen;
	dri_framebuffer_t	dri_framebuf;
	dri_context_t		*dri_context;
	struct nouveau_context	*nv;

	driCreateScreen(display, screen, &dri_screen, &dri_framebuf);
	driCreateContext(dri_screen, XDefaultVisual(display, screen), &dri_context);

	nouveau_screen_create(dri_screen, &dri_framebuf);
	nouveau_context_create(dri_context);

	nv = dri_context->private;

	return nv->nvc->pctx[nv->pctx_id];
}

int destroy_pipe_context(struct pipe_context *pipe)
{
	struct pipe_screen	*screen;
	struct pipe_winsys	*winsys;
	struct nouveau_context	*nv;
	dri_screen_t		*dri_screen;
	dri_context_t		*dri_context;

	assert(pipe);

	screen = pipe->screen;
	winsys = pipe->winsys;
	nv = pipe->priv;
	dri_context = nv->dri_context;
	dri_screen = dri_context->dri_screen;

	pipe->destroy(pipe);
	screen->destroy(screen);
	free(winsys);

	nouveau_context_destroy(dri_context);
	nouveau_screen_destroy(dri_screen);
	driDestroyContext(dri_context);
	driDestroyScreen(dri_screen);

	return 0;
}