summaryrefslogtreecommitdiff
path: root/src/amdgpu_extension.c
blob: 6199c94519c36e1a8b8e3bf7d896ddaef3cacfc3 (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
/*
 * Copyright © 2016 Advanced Micro Devices, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

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

#include "xf86.h"
#include "xf86Crtc.h"
#include "xf86drm.h"
#include "dix.h"
#include "dixstruct.h"
#include "extnsionst.h"
#include "resource.h"
#include "scrnintstr.h"
#include "dixaccess.h"
#include "pixmap.h"

#include "amdgpu_drm.h"
#include "amdgpu_extension.h"
#include "amdgpu_drv.h"

static RESTYPE RT_AMDGPUCLIENT;

static int ProcAMDGPUFreesyncCapability(ClientPtr client)
{
	REQUEST(xAMDGPUFreesyncCapabilityReq);
	REQUEST_SIZE_MATCH(xAMDGPUFreesyncCapabilityReq);

	if (stuff->screen >= screenInfo.numScreens) {
		client->errorValue = stuff->screen;
		return BadValue;
	}

	ScreenPtr pScreen = screenInfo.screens[stuff->screen];
	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
	AMDGPUInfoPtr info = AMDGPUPTR(pScrn);
	XID cliResId;
	DrawablePtr pDrawable = NULL;
	int ret = -1;

	ret = dixLookupDrawable(&pDrawable, (Drawable)stuff->drawable,
				client, 0, DixReadAccess);

// 	if (!ret) {
// 		xf86DrvMsg(0, X_INFO, "Freesync Capability: %d,%d,%u,%u vs. %d,%d,%u,%u\n",
// 			pDrawable->x, pDrawable->y, pDrawable->width, pDrawable->height,
// 			pDrawable->pScreen->x, pDrawable->pScreen->y,
// 			pDrawable->pScreen->width, pDrawable->pScreen->height);
// 	}

	if (!ret &&
	    pDrawable->x == pDrawable->pScreen->x &&
	    pDrawable->y == pDrawable->pScreen->y &&
	    pDrawable->width == pDrawable->pScreen->width &&
	    pDrawable->height == pDrawable->pScreen->height) {
		if (!info->freesync_client) {
			info->freesync_client = client;
			cliResId = FakeClientID(client->index);
			if (AddResource(cliResId, RT_AMDGPUCLIENT, (void *)pScrn))
				info->freesync_client_id = cliResId;
		}
		info->freesync_drawable = pDrawable;
	}

	return client->noClientException;
}

static int AMDGPUDispatch(ClientPtr client)
{
	REQUEST(xReq);

	if (screenInfo.numGPUScreens)
		return client->noClientException;

	switch(stuff->data) {
	case X_AMDGPUFreesyncCapability:
		return ProcAMDGPUFreesyncCapability(client);
	default:
		return BadRequest;
	}
}

static int AMDGPUSwapDispatch(ClientPtr client __attribute__((unused)))
{
	return BadRequest;
}

static void AMDGPUResetExtension(ExtensionEntry *extEntry __attribute__((unused)))
{
	RT_AMDGPUCLIENT = RT_NONE;
}

static void AMDGPUExtensionInit (void)
{
	ExtensionEntry *extEntry = NULL;

	extEntry = AddExtension(AMDGPU_EXTENSION_NAME,
				AMDGPU_EXTENSION_EVENTS,
				AMDGPU_EXTENSION_ERRORS,
				AMDGPUDispatch,
				AMDGPUSwapDispatch,
				AMDGPUResetExtension,
				StandardMinorOpcode);
	if (!extEntry) {
		ErrorF("AddExtension failed\n");
		return;
	}

	RT_AMDGPUCLIENT = CreateNewResourceType(AMDGPUClientGone, "AMDGPUClient");
}

static ExtensionModule AMDGPU_Ext = {
	AMDGPUExtensionInit,
	AMDGPU_EXTENSION_NAME,
	NULL
};

void AMDGPUExtensionSetup(void)
{
#if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1,16,0,0,0)
	LoadExtension(&AMDGPU_Ext, FALSE);
#else
	int size = 1;
	LoadExtensionList(&AMDGPU_Ext, size, FALSE);
#endif
}

int AMDGPUFreesyncControl(int fd, Bool enable_freesync)
{
	int ret = -1;
	struct drm_amdgpu_freesync args;

	memset(&args, 0, sizeof(args));
	if (enable_freesync) {
		xf86DrvMsg(0, X_INFO, "Trying to enable freesync mode.\n");
		args.op = AMDGPU_FREESYNC_FULLSCREEN_ENTER;
                ret = drmCommandWriteRead(fd, DRM_AMDGPU_FREESYNC,
                                          &args, sizeof(args));
                if (ret)
                        ErrorF("Fail to enable freesync mode.\n");

	} else {
		xf86DrvMsg(0, X_INFO, "Trying to disable freesync mode.\n");
		args.op = AMDGPU_FREESYNC_FULLSCREEN_EXIT;
		ret = drmCommandWriteRead(fd, DRM_AMDGPU_FREESYNC,
					  &args, sizeof(args));
		if (ret)
			ErrorF("Fail to disable freesync mode.\n");
	}

	return ret;
}

void AMDGPUFreeResourceByType(ScreenPtr pScreen)
{
	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
	AMDGPUInfoPtr info = AMDGPUPTR(pScrn);

	if (info->freesync_client_id)
		FreeResourceByType(info->freesync_client_id,
				   RT_AMDGPUCLIENT, FALSE);
	return;
}

int AMDGPUClientGone(void *data, XID id)
{
	ScrnInfoPtr pScrn = (ScrnInfoPtr)data;
	AMDGPUInfoPtr info = AMDGPUPTR(pScrn);
	AMDGPUEntPtr pAMDGPUEnt = AMDGPUEntPriv(pScrn);

	if (screenInfo.numGPUScreens)
		return 1;

	if (id == info->freesync_client_id) {
		info->freesync_client_id = None;
		info->freesync_client = NULL;
		info->freesync_drawable = NULL;
	}

	if (info->freesync_enabled == TRUE) {
		if (!AMDGPUFreesyncControl(pAMDGPUEnt->fd, FALSE))
			info->freesync_enabled = FALSE;
	}

	info->allow_freesync = FALSE;

        return 1;
}