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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
/* $xfree86: xc/programs/Xserver/hw/xfree86/drivers/i810/i830_dri.c,v 1.15 2003/06/18 13:14:17 dawes Exp $ */
/**************************************************************************
Copyright 2001 VA Linux Systems Inc., Fremont, California.
Copyright © 2002 by David Dawes
All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
on the rights to use, copy, modify, merge, publish, distribute, sub
license, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
ATI, VA LINUX SYSTEMS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
**************************************************************************/
/*
* Reformatted with GNU indent (2.2.8), using the following options:
*
* -bad -bap -c41 -cd0 -ncdb -ci6 -cli0 -cp0 -ncs -d0 -di3 -i3 -ip3 -l78
* -lp -npcs -psl -sob -ss -br -ce -sc -hnl
*
* This provides a good match with the original i810 code and preferred
* XFree86 formatting conventions.
*
* When editing this driver, please follow the existing formatting, and edit
* with <TAB> characters expanded at 8-column intervals.
*/
/*
* Authors: Jeff Hartmann <jhartmann@valinux.com>
* David Dawes <dawes@xfree86.org>
* Keith Whitwell <keith@tungstengraphics.com>
*/
/*
* This driver does AGP memory allocation a little differently from most
* others. The 2D and 3D allocations have been unified (see i830_memory.c).
* The driver does the AGP allocations and binding directly, then passes
* on the mappings to the DRM module. The DRM's AGP interfaces are not used.
* The main difference with this is that the offsets need to include
* the AGP aperture base address because that won't be known or added on
* by the DRM module.
*
* DHD 07/2002
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86Priv.h"
#include "xf86PciInfo.h"
#include "xf86Pci.h"
#include "windowstr.h"
#include "shadow.h"
#include "GL/glxtokens.h"
#include "i830.h"
#include "i830_dri.h"
#include "i915_drm.h"
#include "dri2.h"
#ifdef DRI2
#if DRI2INFOREC_VERSION >= 1
#define USE_DRI2_1_1_0
#endif
extern XF86ModuleData dri2ModuleData;
#endif
typedef struct {
PixmapPtr pPixmap;
unsigned int attachment;
} I830DRI2BufferPrivateRec, *I830DRI2BufferPrivatePtr;
#ifndef USE_DRI2_1_1_0
static DRI2BufferPtr
I830DRI2CreateBuffers(DrawablePtr pDraw, unsigned int *attachments, int count)
{
ScreenPtr pScreen = pDraw->pScreen;
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
I830Ptr pI830 = I830PTR(pScrn);
DRI2BufferPtr buffers;
dri_bo *bo;
int i;
I830DRI2BufferPrivatePtr privates;
PixmapPtr pPixmap, pDepthPixmap;
buffers = xcalloc(count, sizeof *buffers);
if (buffers == NULL)
return NULL;
privates = xcalloc(count, sizeof *privates);
if (privates == NULL) {
xfree(buffers);
return NULL;
}
pDepthPixmap = NULL;
for (i = 0; i < count; i++) {
if (attachments[i] == DRI2BufferFrontLeft) {
if (pDraw->type == DRAWABLE_PIXMAP)
pPixmap = (PixmapPtr) pDraw;
else
pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr) pDraw);
pPixmap->refcnt++;
} else if (attachments[i] == DRI2BufferStencil && pDepthPixmap) {
pPixmap = pDepthPixmap;
pPixmap->refcnt++;
} else {
unsigned int hint = 0;
switch (attachments[i]) {
case DRI2BufferDepth:
if (SUPPORTS_YTILING(pI830))
hint = INTEL_CREATE_PIXMAP_TILING_Y;
else
hint = INTEL_CREATE_PIXMAP_TILING_X;
break;
case DRI2BufferFakeFrontLeft:
case DRI2BufferFakeFrontRight:
case DRI2BufferBackLeft:
case DRI2BufferBackRight:
hint = INTEL_CREATE_PIXMAP_TILING_X;
break;
}
if (!pI830->tiling ||
(!IS_I965G(pI830) && !pI830->kernel_exec_fencing))
hint = 0;
pPixmap = (*pScreen->CreatePixmap)(pScreen,
pDraw->width,
pDraw->height,
pDraw->depth,
hint);
}
if (attachments[i] == DRI2BufferDepth)
pDepthPixmap = pPixmap;
buffers[i].attachment = attachments[i];
buffers[i].pitch = pPixmap->devKind;
buffers[i].cpp = pPixmap->drawable.bitsPerPixel / 8;
buffers[i].driverPrivate = &privates[i];
buffers[i].flags = 0; /* not tiled */
privates[i].pPixmap = pPixmap;
privates[i].attachment = attachments[i];
bo = i830_get_pixmap_bo (pPixmap);
if (dri_bo_flink(bo, &buffers[i].name) != 0) {
/* failed to name buffer */
}
}
return buffers;
}
#else
static DRI2BufferPtr
I830DRI2CreateBuffer(DrawablePtr pDraw, unsigned int attachment,
unsigned int format)
{
ScreenPtr pScreen = pDraw->pScreen;
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
I830Ptr pI830 = I830PTR(pScrn);
DRI2BufferPtr buffers;
dri_bo *bo;
I830DRI2BufferPrivatePtr privates;
PixmapPtr pPixmap;
buffers = xcalloc(1, sizeof *buffers);
if (buffers == NULL)
return NULL;
privates = xcalloc(1, sizeof *privates);
if (privates == NULL) {
xfree(buffers);
return NULL;
}
if (attachment == DRI2BufferFrontLeft) {
if (pDraw->type == DRAWABLE_PIXMAP)
pPixmap = (PixmapPtr) pDraw;
else
pPixmap = (*pScreen->GetWindowPixmap)((WindowPtr) pDraw);
pPixmap->refcnt++;
} else {
unsigned int hint = 0;
switch (attachment) {
case DRI2BufferDepth:
case DRI2BufferDepthStencil:
if (SUPPORTS_YTILING(pI830))
hint = INTEL_CREATE_PIXMAP_TILING_Y;
else
hint = INTEL_CREATE_PIXMAP_TILING_X;
break;
case DRI2BufferFakeFrontLeft:
case DRI2BufferFakeFrontRight:
case DRI2BufferBackLeft:
case DRI2BufferBackRight:
hint = INTEL_CREATE_PIXMAP_TILING_X;
break;
}
if (!pI830->tiling ||
(!IS_I965G(pI830) && !pI830->kernel_exec_fencing))
hint = 0;
pPixmap = (*pScreen->CreatePixmap)(pScreen,
pDraw->width,
pDraw->height,
(format != 0)?format:pDraw->depth,
hint);
}
buffers->attachment = attachment;
buffers->pitch = pPixmap->devKind;
buffers->cpp = pPixmap->drawable.bitsPerPixel / 8;
buffers->driverPrivate = privates;
buffers->format = format;
buffers->flags = 0; /* not tiled */
privates->pPixmap = pPixmap;
privates->attachment = attachment;
bo = i830_get_pixmap_bo (pPixmap);
if (dri_bo_flink(bo, &buffers->name) != 0) {
/* failed to name buffer */
}
return buffers;
}
#endif
#ifndef USE_DRI2_1_1_0
static void
I830DRI2DestroyBuffers(DrawablePtr pDraw, DRI2BufferPtr buffers, int count)
{
ScreenPtr pScreen = pDraw->pScreen;
I830DRI2BufferPrivatePtr private;
int i;
for (i = 0; i < count; i++)
{
private = buffers[i].driverPrivate;
(*pScreen->DestroyPixmap)(private->pPixmap);
}
if (buffers)
{
xfree(buffers[0].driverPrivate);
xfree(buffers);
}
}
#else
static void
I830DRI2DestroyBuffer(DrawablePtr pDraw, DRI2BufferPtr buffer)
{
if (buffer) {
I830DRI2BufferPrivatePtr private = buffer->driverPrivate;
ScreenPtr pScreen = pDraw->pScreen;
(*pScreen->DestroyPixmap)(private->pPixmap);
xfree(private);
xfree(buffer);
}
}
#endif
static void
I830DRI2CopyRegion(DrawablePtr pDraw, RegionPtr pRegion,
DRI2BufferPtr pDstBuffer, DRI2BufferPtr pSrcBuffer)
{
I830DRI2BufferPrivatePtr srcPrivate = pSrcBuffer->driverPrivate;
I830DRI2BufferPrivatePtr dstPrivate = pDstBuffer->driverPrivate;
ScreenPtr pScreen = pDraw->pScreen;
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
I830Ptr pI830 = I830PTR(pScrn);
PixmapPtr pSrcPixmap = (srcPrivate->attachment == DRI2BufferFrontLeft)
? (PixmapPtr) pDraw : srcPrivate->pPixmap;
PixmapPtr pDstPixmap = (dstPrivate->attachment == DRI2BufferFrontLeft)
? (PixmapPtr) pDraw : dstPrivate->pPixmap;
RegionPtr pCopyClip;
GCPtr pGC;
pGC = GetScratchGC(pDraw->depth, pScreen);
pCopyClip = REGION_CREATE(pScreen, NULL, 0);
REGION_COPY(pScreen, pCopyClip, pRegion);
(*pGC->funcs->ChangeClip) (pGC, CT_REGION, pCopyClip, 0);
ValidateGC(&pDstPixmap->drawable, pGC);
(*pGC->ops->CopyArea)(&pSrcPixmap->drawable, &pDstPixmap->drawable,
pGC, 0, 0, pDraw->width, pDraw->height, 0, 0);
FreeScratchGC(pGC);
/* Emit a flush of the rendering cache, or on the 965 and beyond
* rendering results may not hit the framebuffer until significantly
* later.
*/
I830EmitFlush(pScrn);
pI830->need_mi_flush = FALSE;
/* We can't rely on getting into the block handler before the DRI
* client gets to run again so flush now. */
intel_batch_flush(pScrn, TRUE);
#if ALWAYS_SYNC
I830Sync(pScrn);
#endif
drmCommandNone(pI830->drmSubFD, DRM_I915_GEM_THROTTLE);
}
Bool I830DRI2ScreenInit(ScreenPtr pScreen)
{
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
I830Ptr pI830 = I830PTR(pScrn);
DRI2InfoRec info;
char *p, buf[64];
int i;
struct stat sbuf;
dev_t d;
#ifdef USE_DRI2_1_1_0
int dri2_major = 1;
int dri2_minor = 0;
#endif
if (pI830->accel != ACCEL_UXA) {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "DRI2 requires UXA\n");
return FALSE;
}
#ifdef USE_DRI2_1_1_0
if (xf86LoaderCheckSymbol("DRI2Version")) {
DRI2Version(& dri2_major, & dri2_minor);
}
if (dri2_minor < 1) {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"DRI2 requires DRI2 module version 1.1.0 or later\n");
return FALSE;
}
#endif
sprintf(buf, "pci:%04x:%02x:%02x.%d",
pI830->PciInfo->domain,
pI830->PciInfo->bus,
pI830->PciInfo->dev,
pI830->PciInfo->func);
/* Use the already opened (master) fd from modesetting */
if (pI830->use_drm_mode) {
info.fd = pI830->drmSubFD;
} else {
info.fd = drmOpen("i915", buf);
drmSetVersion sv;
int err;
/* Check that what we opened was a master or a master-capable FD,
* by setting the version of the interface we'll use to talk to it.
* (see DRIOpenDRMMaster() in DRI1)
*/
sv.drm_di_major = 1;
sv.drm_di_minor = 1;
sv.drm_dd_major = -1;
err = drmSetInterfaceVersion(info.fd, &sv);
if (err != 0)
return FALSE;
}
if (info.fd < 0) {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Failed to open DRM device\n");
return FALSE;
}
/* The whole drmOpen thing is a fiasco and we need to find a way
* back to just using open(2). For now, however, lets just make
* things worse with even more ad hoc directory walking code to
* discover the device file name. */
fstat(info.fd, &sbuf);
d = sbuf.st_rdev;
p = pI830->deviceName;
for (i = 0; i < DRM_MAX_MINOR; i++) {
sprintf(p, DRM_DEV_NAME, DRM_DIR_NAME, i);
if (stat(p, &sbuf) == 0 && sbuf.st_rdev == d)
break;
}
if (i == DRM_MAX_MINOR) {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"DRI2: failed to open drm device\n");
return FALSE;
}
info.driverName = IS_I965G(pI830) ? "i965" : "i915";
info.deviceName = p;
#ifdef USE_DRI2_1_1_0
info.version = 2;
info.CreateBuffers = NULL;
info.DestroyBuffers = NULL;
info.CreateBuffer = I830DRI2CreateBuffer;
info.DestroyBuffer = I830DRI2DestroyBuffer;
#else
info.version = 1;
info.CreateBuffers = I830DRI2CreateBuffers;
info.DestroyBuffers = I830DRI2DestroyBuffers;
#endif
info.CopyRegion = I830DRI2CopyRegion;
pI830->drmSubFD = info.fd;
return DRI2ScreenInit(pScreen, &info);
}
void I830DRI2CloseScreen(ScreenPtr pScreen)
{
ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
I830Ptr pI830 = I830PTR(pScrn);
DRI2CloseScreen(pScreen);
pI830->directRenderingType = DRI_NONE;
}
|