summaryrefslogtreecommitdiff
path: root/src/amdgpu_probe.c
blob: 2dc934ff656d755f5c57b72bdf00b964972c6fab (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
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
/*
 * Copyright 2000 ATI Technologies Inc., Markham, Ontario, and
 *                VA Linux Systems Inc., Fremont, California.
 *
 * 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, sublicense, 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include <string.h>
#include <stdlib.h>

/*
 * Authors:
 *   Kevin E. Martin <martin@xfree86.org>
 *   Rickard E. Faith <faith@valinux.com>
 * KMS support - Dave Airlie <airlied@redhat.com>
 */

#include "amdgpu_probe.h"
#include "amdgpu_version.h"
#include "amdgpu_drv.h"

#include "xf86.h"

#include "xf86drmMode.h"
#include "dri.h"

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#include <xf86_OSproc.h>
#endif

#include <xf86platformBus.h>

_X_EXPORT int gAMDGPUEntityIndex = -1;

/* Return the options for supported chipset 'n'; NULL otherwise */
static const OptionInfoRec *AMDGPUAvailableOptions(int chipid, int busid)
{
	return AMDGPUOptionsWeak();
}

static SymTabRec AMDGPUAny[] = {
	{ 0, "All GPUs supported by the amdgpu kernel driver" },
	{ -1, NULL }
};

/* Return the string name for supported chipset 'n'; NULL otherwise. */
static void AMDGPUIdentify(int flags)
{
	xf86PrintChipsets(AMDGPU_NAME, "Driver for AMD Radeon", AMDGPUAny);
}

static char *amdgpu_bus_id(ScrnInfoPtr pScrn, struct pci_device *dev)
{
	char *busid;

	XNFasprintf(&busid, "pci:%04x:%02x:%02x.%d",
		    dev->domain, dev->bus, dev->dev, dev->func);

	if (!busid)
		xf86DrvMsgVerb(pScrn->scrnIndex, X_ERROR, 0,
			       "AMDGPU: Failed to generate bus ID string\n");

	return busid;
}

static Bool amdgpu_kernel_mode_enabled(ScrnInfoPtr pScrn, char *busIdString)
{
	int ret = drmCheckModesettingSupported(busIdString);

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
	if (ret) {
		if (xf86LoadKernelModule("amdgpukms"))
			ret = drmCheckModesettingSupported(busIdString);
	}
#endif
	if (ret) {
		xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 0,
			       "[KMS] drm report modesetting isn't supported.\n");
		return FALSE;
	}

	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 0,
		       "[KMS] Kernel modesetting enabled.\n");
	return TRUE;
}

static int amdgpu_kernel_open_fd(ScrnInfoPtr pScrn,
				 struct pci_device *pci_dev,
				 struct xf86_platform_device *platform_dev)
{
	struct pci_device *dev;
	char *busid;
	int fd;

#ifdef ODEV_ATTRIB_FD
	if (platform_dev) {
		fd = xf86_get_platform_device_int_attrib(platform_dev,
							 ODEV_ATTRIB_FD, -1);
		if (fd != -1)
			return fd;
	}
#endif

	if (platform_dev)
		dev = platform_dev->pdev;
	else
		dev = pci_dev;

	busid = amdgpu_bus_id(pScrn, dev);
	if (!busid)
		return -1;

	if (!amdgpu_kernel_mode_enabled(pScrn, busid)) {
		free(busid);
		return -1;
	}

	fd = drmOpen(NULL, busid);
	if (fd == -1)
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "[drm] Failed to open DRM device for %s: %s\n",
			   busid, strerror(errno));
	free(busid);
	return fd;
}

void amdgpu_kernel_close_fd(AMDGPUEntPtr pAMDGPUEnt)
{
#if defined(XSERVER_PLATFORM_BUS) && defined(XF86_PDEV_SERVER_FD)
	if (!(pAMDGPUEnt->platform_dev &&
	      pAMDGPUEnt->platform_dev->flags & XF86_PDEV_SERVER_FD))
#endif
		drmClose(pAMDGPUEnt->fd);
	pAMDGPUEnt->fd = -1;
}

static Bool amdgpu_open_drm_master(ScrnInfoPtr pScrn, AMDGPUEntPtr pAMDGPUEnt,
				   struct pci_device *pci_dev)
{
	drmSetVersion sv;
	int err;

	pAMDGPUEnt->fd = amdgpu_kernel_open_fd(pScrn, pci_dev, NULL);
	if (pAMDGPUEnt->fd == -1)
		return FALSE;

	/* 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;
	sv.drm_dd_minor = -1;
	err = drmSetInterfaceVersion(pAMDGPUEnt->fd, &sv);
	if (err != 0) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "[drm] failed to set drm interface version.\n");
		amdgpu_kernel_close_fd(pAMDGPUEnt);
		return FALSE;
	}

	return TRUE;
}

static Bool amdgpu_get_scrninfo(int entity_num, struct pci_device *pci_dev)
{
	ScrnInfoPtr pScrn = NULL;
	EntityInfoPtr pEnt = NULL;
	DevUnion *pPriv;
	AMDGPUEntPtr pAMDGPUEnt;

	pScrn = xf86ConfigPciEntity(pScrn, 0, entity_num, NULL,
				    NULL, NULL, NULL, NULL, NULL);

	if (!pScrn)
		return FALSE;

	pScrn->driverVersion = AMDGPU_VERSION_CURRENT;
	pScrn->driverName = AMDGPU_DRIVER_NAME;
	pScrn->name = AMDGPU_NAME;
	pScrn->Probe = NULL;

	pScrn->PreInit = AMDGPUPreInit_KMS;
	pScrn->ScreenInit = AMDGPUScreenInit_KMS;
	pScrn->SwitchMode = AMDGPUSwitchMode_KMS;
	pScrn->AdjustFrame = AMDGPUAdjustFrame_KMS;
	pScrn->EnterVT = AMDGPUEnterVT_KMS;
	pScrn->LeaveVT = AMDGPULeaveVT_KMS;
	pScrn->FreeScreen = AMDGPUFreeScreen_KMS;
	pScrn->ValidMode = AMDGPUValidMode;

	pEnt = xf86GetEntityInfo(entity_num);

	/* Create a AMDGPUEntity for all chips, even with old single head
	 * Radeon, need to use pAMDGPUEnt for new monitor detection routines.
	 */
	xf86SetEntitySharable(entity_num);

	if (gAMDGPUEntityIndex == -1)
		gAMDGPUEntityIndex = xf86AllocateEntityPrivateIndex();

	pPriv = xf86GetEntityPrivate(pEnt->index, gAMDGPUEntityIndex);

	if (!pPriv->ptr) {
		uint32_t major_version;
		uint32_t minor_version;

		pPriv->ptr = xnfcalloc(sizeof(AMDGPUEntRec), 1);
		if (!pPriv->ptr)
			goto error;

		pAMDGPUEnt = pPriv->ptr;
		if (!amdgpu_open_drm_master(pScrn, pAMDGPUEnt, pci_dev))
			goto error;

		pAMDGPUEnt->fd_ref = 1;

		if (amdgpu_device_initialize(pAMDGPUEnt->fd,
					     &major_version,
					     &minor_version,
					     &pAMDGPUEnt->pDev)) {
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "amdgpu_device_initialize failed\n");
			goto error_amdgpu;
		}
	} else {
		pAMDGPUEnt = pPriv->ptr;

		if (pAMDGPUEnt->fd_ref == ARRAY_SIZE(pAMDGPUEnt->scrn)) {
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "Only up to %u Zaphod instances supported\n",
				   (unsigned)ARRAY_SIZE(pAMDGPUEnt->scrn));
			goto error;
		}

		pAMDGPUEnt->fd_ref++;
	}

	xf86SetEntityInstanceForScreen(pScrn, pEnt->index,
				       xf86GetNumEntityInstances(pEnt->
								 index)
				       - 1);
	free(pEnt);

	return TRUE;

error_amdgpu:
	amdgpu_kernel_close_fd(pAMDGPUEnt);
error:
	free(pEnt);
	return FALSE;
}

static Bool
amdgpu_pci_probe(DriverPtr pDriver,
		 int entity_num, struct pci_device *device, intptr_t match_data)
{
	return amdgpu_get_scrninfo(entity_num, device);
}

static Bool AMDGPUDriverFunc(ScrnInfoPtr scrn, xorgDriverFuncOp op, void *data)
{
	xorgHWFlags *flag;

	switch (op) {
	case GET_REQUIRED_HW_INTERFACES:
		flag = (CARD32 *) data;
		(*flag) = 0;
		return TRUE;
#if XORG_VERSION_CURRENT > XORG_VERSION_NUMERIC(1,15,99,0,0)
	case SUPPORTS_SERVER_FDS:
		return TRUE;
#endif
       default:
		return FALSE;
	}
}

#ifdef XSERVER_PLATFORM_BUS
static Bool
amdgpu_platform_probe(DriverPtr pDriver,
		      int entity_num, int flags,
		      struct xf86_platform_device *dev, intptr_t match_data)
{
	ScrnInfoPtr pScrn;
	int scr_flags = 0;
	EntityInfoPtr pEnt = NULL;
	DevUnion *pPriv;
	AMDGPUEntPtr pAMDGPUEnt;

	if (!dev->pdev)
		return FALSE;

	if (flags & PLATFORM_PROBE_GPU_SCREEN)
		scr_flags = XF86_ALLOCATE_GPU_SCREEN;

	pScrn = xf86AllocateScreen(pDriver, scr_flags);
	if (xf86IsEntitySharable(entity_num))
		xf86SetEntityShared(entity_num);
	xf86AddEntityToScreen(pScrn, entity_num);

	pScrn->driverVersion = AMDGPU_VERSION_CURRENT;
	pScrn->driverName = AMDGPU_DRIVER_NAME;
	pScrn->name = AMDGPU_NAME;
	pScrn->Probe = NULL;
	pScrn->PreInit = AMDGPUPreInit_KMS;
	pScrn->ScreenInit = AMDGPUScreenInit_KMS;
	pScrn->SwitchMode = AMDGPUSwitchMode_KMS;
	pScrn->AdjustFrame = AMDGPUAdjustFrame_KMS;
	pScrn->EnterVT = AMDGPUEnterVT_KMS;
	pScrn->LeaveVT = AMDGPULeaveVT_KMS;
	pScrn->FreeScreen = AMDGPUFreeScreen_KMS;
	pScrn->ValidMode = AMDGPUValidMode;

	pEnt = xf86GetEntityInfo(entity_num);

	/* Create a AMDGPUEntity for all chips, even with old single head
	 * Radeon, need to use pAMDGPUEnt for new monitor detection routines.
	 */
	xf86SetEntitySharable(entity_num);

	if (gAMDGPUEntityIndex == -1)
		gAMDGPUEntityIndex = xf86AllocateEntityPrivateIndex();

	pPriv = xf86GetEntityPrivate(pEnt->index, gAMDGPUEntityIndex);

	if (!pPriv->ptr) {
		uint32_t major_version;
		uint32_t minor_version;

		pPriv->ptr = xnfcalloc(sizeof(AMDGPUEntRec), 1);
		pAMDGPUEnt = pPriv->ptr;
		pAMDGPUEnt->platform_dev = dev;
		pAMDGPUEnt->fd = amdgpu_kernel_open_fd(pScrn, NULL, dev);
		if (pAMDGPUEnt->fd < 0)
			goto error;

		pAMDGPUEnt->fd_ref = 1;

		if (amdgpu_device_initialize(pAMDGPUEnt->fd,
					     &major_version,
					     &minor_version,
					     &pAMDGPUEnt->pDev)) {
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "amdgpu_device_initialize failed\n");
			goto error_amdgpu;
		}
	} else {
		pAMDGPUEnt = pPriv->ptr;

		if (pAMDGPUEnt->fd_ref == ARRAY_SIZE(pAMDGPUEnt->scrn)) {
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "Only up to %u Zaphod instances supported\n",
				   (unsigned)ARRAY_SIZE(pAMDGPUEnt->scrn));
			goto error;
		}

		pAMDGPUEnt->fd_ref++;
	}

	xf86SetEntityInstanceForScreen(pScrn, pEnt->index,
				       xf86GetNumEntityInstances(pEnt->
								 index)
				       - 1);
	free(pEnt);

	return TRUE;

error_amdgpu:
	amdgpu_kernel_close_fd(pAMDGPUEnt);
error:
	free(pEnt);
	return FALSE;
}
#endif

static const struct pci_id_match amdgpu_device_match[] = {
    {0x1002, PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, 0, 0, 0},
    {0, 0, 0},
};

DriverRec AMDGPU = {
	AMDGPU_VERSION_CURRENT,
	AMDGPU_DRIVER_NAME,
	AMDGPUIdentify,
	NULL,
	AMDGPUAvailableOptions,
	NULL,
	0,
	AMDGPUDriverFunc,
	amdgpu_device_match,
	amdgpu_pci_probe,
#ifdef XSERVER_PLATFORM_BUS
	amdgpu_platform_probe
#endif
};