blob: 9d0d242dab5291110382b9a8ae23b442d1720982 [file] [log] [blame]
Rob Clark487687e2011-07-17 17:29:02 -05001/*
2 * Copyright © 2007 Red Hat, Inc.
3 * Copyright © 2008 Maarten Maathuis
4 * Copyright © 2011 Texas Instruments, Inc
5 *
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 * Authors:
27 * Dave Airlie <airlied@redhat.com>
28 * Ian Elliott <ianelliottus@yahoo.com>
29 */
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
Rob Clark487687e2011-07-17 17:29:02 -050035#include "xf86DDC.h"
Dave Barnishf256ebe2013-08-22 15:05:58 +010036#include "xf86RandR12.h"
Rob Clark487687e2011-07-17 17:29:02 -050037
Rob Clark487687e2011-07-17 17:29:02 -050038#ifdef HAVE_XEXTPROTO_71
39#include <X11/extensions/dpmsconst.h>
40#else
41#define DPMS_SERVER
42#include <X11/extensions/dpms.h>
43#endif
44
Paul Gearyf8b99692013-04-15 10:55:17 +010045#include "armsoc_driver.h"
Rob Clark487687e2011-07-17 17:29:02 -050046
Rob Clark487687e2011-07-17 17:29:02 -050047#include "xf86drmMode.h"
Rob Clark687c6082012-01-08 19:33:18 -060048#include "drm_fourcc.h"
Rob Clark487687e2011-07-17 17:29:02 -050049#include "X11/Xatom.h"
50
Rob Clark487687e2011-07-17 17:29:02 -050051#include <libudev.h>
Ray Smith3c33c3d2013-03-26 16:06:37 +000052#include "drmmode_driver.h"
53
Dave Barnish2e998952013-06-11 16:31:10 +010054struct drmmode_cursor_rec {
Rob Clark687c6082012-01-08 19:33:18 -060055 /* hardware cursor: */
Paul Geary8ffd91c2013-04-11 16:03:15 +010056 struct armsoc_bo *bo;
Rob Clark687c6082012-01-08 19:33:18 -060057 int x, y;
Javier Martin4c572792013-09-11 15:08:19 +010058 /* These are used for HWCURSOR_API_PLANE */
59 drmModePlane *ovr;
60 uint32_t fb_id;
61 /* This is used for HWCURSOR_API_STANDARD */
62 uint32_t handle;
Dave Barnish2e998952013-06-11 16:31:10 +010063};
Rob Clark687c6082012-01-08 19:33:18 -060064
Dave Barnish2e998952013-06-11 16:31:10 +010065struct drmmode_rec {
Rob Clark74210d52012-01-08 17:59:08 -060066 int fd;
Rob Clark74210d52012-01-08 17:59:08 -060067 drmModeResPtr mode_res;
68 int cpp;
69 struct udev_monitor *uevent_monitor;
70 InputHandlerProc uevent_handler;
Dave Barnish2e998952013-06-11 16:31:10 +010071 struct drmmode_cursor_rec *cursor;
72};
Rob Clark487687e2011-07-17 17:29:02 -050073
Dave Barnish2e998952013-06-11 16:31:10 +010074struct drmmode_crtc_private_rec {
75 struct drmmode_rec *drmmode;
Rob Clark74210d52012-01-08 17:59:08 -060076 drmModeCrtcPtr mode_crtc;
Stéphane Marchesinb8b35202012-10-02 20:27:40 -070077 int cursor_visible;
Dave Barnishf245da32013-08-30 12:16:58 +010078 /* settings retained on last good modeset */
79 int last_good_x;
80 int last_good_y;
81 Rotation last_good_rotation;
82 DisplayModePtr last_good_mode;
Dave Barnish2e998952013-06-11 16:31:10 +010083};
Rob Clark487687e2011-07-17 17:29:02 -050084
Dave Barnish2e998952013-06-11 16:31:10 +010085struct drmmode_prop_rec {
Rob Clark487687e2011-07-17 17:29:02 -050086 drmModePropertyPtr mode_prop;
Dave Barnish2e998952013-06-11 16:31:10 +010087 /* Index within the kernel-side property arrays for this connector. */
88 int index;
89 /* if range prop, num_atoms == 1;
90 * if enum prop, num_atoms == num_enums + 1
91 */
92 int num_atoms;
Rob Clark487687e2011-07-17 17:29:02 -050093 Atom *atoms;
Dave Barnish2e998952013-06-11 16:31:10 +010094};
Rob Clark487687e2011-07-17 17:29:02 -050095
Dave Barnish2e998952013-06-11 16:31:10 +010096struct drmmode_output_priv {
97 struct drmmode_rec *drmmode;
Rob Clark74210d52012-01-08 17:59:08 -060098 int output_id;
99 drmModeConnectorPtr mode_output;
100 drmModeEncoderPtr mode_encoder;
101 drmModePropertyBlobPtr edid_blob;
102 int num_props;
Dave Barnish2e998952013-06-11 16:31:10 +0100103 struct drmmode_prop_rec *props;
104};
Rob Clark487687e2011-07-17 17:29:02 -0500105
106static void drmmode_output_dpms(xf86OutputPtr output, int mode);
Dave Barnishf245da32013-08-30 12:16:58 +0100107static Bool resize_scanout_bo(ScrnInfoPtr pScrn, int width, int height);
Rob Clark487687e2011-07-17 17:29:02 -0500108
Dave Barnish2e998952013-06-11 16:31:10 +0100109static struct drmmode_rec *
Rob Clark687c6082012-01-08 19:33:18 -0600110drmmode_from_scrn(ScrnInfoPtr pScrn)
111{
112 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
Dave Barnish2e998952013-06-11 16:31:10 +0100113 struct drmmode_crtc_private_rec *drmmode_crtc;
Rob Clark687c6082012-01-08 19:33:18 -0600114
115 drmmode_crtc = xf86_config->crtc[0]->driver_private;
116 return drmmode_crtc->drmmode;
117}
118
Rob Clark487687e2011-07-17 17:29:02 -0500119static void
120drmmode_ConvertFromKMode(ScrnInfoPtr pScrn, drmModeModeInfo *kmode,
Rob Clark74210d52012-01-08 17:59:08 -0600121 DisplayModePtr mode)
Rob Clark487687e2011-07-17 17:29:02 -0500122{
Rob Clark74210d52012-01-08 17:59:08 -0600123 memset(mode, 0, sizeof(DisplayModeRec));
124 mode->status = MODE_OK;
Rob Clark487687e2011-07-17 17:29:02 -0500125
Rob Clark74210d52012-01-08 17:59:08 -0600126 mode->Clock = kmode->clock;
Rob Clark487687e2011-07-17 17:29:02 -0500127
Rob Clark74210d52012-01-08 17:59:08 -0600128 mode->HDisplay = kmode->hdisplay;
129 mode->HSyncStart = kmode->hsync_start;
130 mode->HSyncEnd = kmode->hsync_end;
131 mode->HTotal = kmode->htotal;
132 mode->HSkew = kmode->hskew;
Rob Clark487687e2011-07-17 17:29:02 -0500133
Rob Clark74210d52012-01-08 17:59:08 -0600134 mode->VDisplay = kmode->vdisplay;
135 mode->VSyncStart = kmode->vsync_start;
136 mode->VSyncEnd = kmode->vsync_end;
137 mode->VTotal = kmode->vtotal;
138 mode->VScan = kmode->vscan;
Rob Clark487687e2011-07-17 17:29:02 -0500139
Dave Barnish01b5c172013-04-10 11:01:17 +0100140 mode->Flags = kmode->flags;
Rob Clark74210d52012-01-08 17:59:08 -0600141 mode->name = strdup(kmode->name);
Rob Clark487687e2011-07-17 17:29:02 -0500142
Rob Clark74210d52012-01-08 17:59:08 -0600143 DEBUG_MSG("copy mode %s (%p %p)", kmode->name, mode->name, mode);
Rob Clark487687e2011-07-17 17:29:02 -0500144
Rob Clark74210d52012-01-08 17:59:08 -0600145 if (kmode->type & DRM_MODE_TYPE_DRIVER)
146 mode->type = M_T_DRIVER;
Dave Barnish2e998952013-06-11 16:31:10 +0100147
Rob Clark74210d52012-01-08 17:59:08 -0600148 if (kmode->type & DRM_MODE_TYPE_PREFERRED)
149 mode->type |= M_T_PREFERRED;
Rob Clark487687e2011-07-17 17:29:02 -0500150
Dave Barnish2e998952013-06-11 16:31:10 +0100151 xf86SetModeCrtc(mode, pScrn->adjustFlags);
Rob Clark74210d52012-01-08 17:59:08 -0600152}
Rob Clark487687e2011-07-17 17:29:02 -0500153
154static void
155drmmode_ConvertToKMode(ScrnInfoPtr pScrn, drmModeModeInfo *kmode,
Rob Clark74210d52012-01-08 17:59:08 -0600156 DisplayModePtr mode)
Rob Clark487687e2011-07-17 17:29:02 -0500157{
Rob Clark74210d52012-01-08 17:59:08 -0600158 memset(kmode, 0, sizeof(*kmode));
Rob Clark487687e2011-07-17 17:29:02 -0500159
Rob Clark74210d52012-01-08 17:59:08 -0600160 kmode->clock = mode->Clock;
161 kmode->hdisplay = mode->HDisplay;
162 kmode->hsync_start = mode->HSyncStart;
163 kmode->hsync_end = mode->HSyncEnd;
164 kmode->htotal = mode->HTotal;
165 kmode->hskew = mode->HSkew;
Rob Clark487687e2011-07-17 17:29:02 -0500166
Rob Clark74210d52012-01-08 17:59:08 -0600167 kmode->vdisplay = mode->VDisplay;
168 kmode->vsync_start = mode->VSyncStart;
169 kmode->vsync_end = mode->VSyncEnd;
170 kmode->vtotal = mode->VTotal;
171 kmode->vscan = mode->VScan;
Rob Clark487687e2011-07-17 17:29:02 -0500172
Dave Barnish01b5c172013-04-10 11:01:17 +0100173 kmode->flags = mode->Flags;
Rob Clark74210d52012-01-08 17:59:08 -0600174 if (mode->name)
175 strncpy(kmode->name, mode->name, DRM_DISPLAY_MODE_LEN);
176 kmode->name[DRM_DISPLAY_MODE_LEN-1] = 0;
177}
Rob Clark487687e2011-07-17 17:29:02 -0500178
179static void
180drmmode_crtc_dpms(xf86CrtcPtr drmmode_crtc, int mode)
181{
Dave Barnish2e998952013-06-11 16:31:10 +0100182 /* TODO: MIDEGL-1431: Implement this function */
Rob Clark74210d52012-01-08 17:59:08 -0600183}
Rob Clark487687e2011-07-17 17:29:02 -0500184
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100185static int
186drmmode_revert_mode(xf86CrtcPtr crtc, uint32_t *output_ids, int output_count)
187{
188 ScrnInfoPtr pScrn = crtc->scrn;
189 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
190 uint32_t fb_id;
191 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
192 drmModeModeInfo kmode;
193
194 if (!drmmode_crtc->last_good_mode) {
195 DEBUG_MSG("No last good values to use");
196 return FALSE;
197 }
198
199 /* revert to last good settings */
200 DEBUG_MSG("Reverting to last_good values");
201 if (!resize_scanout_bo(pScrn,
202 drmmode_crtc->last_good_mode->HDisplay,
203 drmmode_crtc->last_good_mode->VDisplay)) {
204 ERROR_MSG("Could not revert to last good mode");
205 return FALSE;
206 }
207
208 fb_id = armsoc_bo_get_fb(pARMSOC->scanout);
209 drmmode_ConvertToKMode(crtc->scrn, &kmode,
210 drmmode_crtc->last_good_mode);
211 drmModeSetCrtc(drmmode_crtc->drmmode->fd,
212 drmmode_crtc->mode_crtc->crtc_id,
213 fb_id,
214 drmmode_crtc->last_good_x,
215 drmmode_crtc->last_good_y,
216 output_ids, output_count, &kmode);
217
218 /* let RandR know we changed things */
219 xf86RandR12TellChanged(pScrn->pScreen);
220
221 return TRUE;
222}
223
Rob Clark487687e2011-07-17 17:29:02 -0500224static Bool
225drmmode_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode,
Rob Clark74210d52012-01-08 17:59:08 -0600226 Rotation rotation, int x, int y)
Rob Clark487687e2011-07-17 17:29:02 -0500227{
228 ScrnInfoPtr pScrn = crtc->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100229 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -0500230 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
Dave Barnish2e998952013-06-11 16:31:10 +0100231 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
232 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500233 uint32_t *output_ids = NULL;
234 int output_count = 0;
235 int ret = TRUE;
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100236 int err;
Rob Clark487687e2011-07-17 17:29:02 -0500237 int i;
David Garbett7171c522012-05-11 13:12:50 +0100238 uint32_t fb_id;
Rob Clark487687e2011-07-17 17:29:02 -0500239 drmModeModeInfo kmode;
Dave Barnishf256ebe2013-08-22 15:05:58 +0100240 drmModeCrtcPtr newcrtc = NULL;
Rob Clark487687e2011-07-17 17:29:02 -0500241
242 TRACE_ENTER();
243
Paul Geary8ffd91c2013-04-11 16:03:15 +0100244 fb_id = armsoc_bo_get_fb(pARMSOC->scanout);
David Garbett7171c522012-05-11 13:12:50 +0100245
246 if (fb_id == 0) {
David Garbett7171c522012-05-11 13:12:50 +0100247 DEBUG_MSG("create framebuffer: %dx%d",
248 pScrn->virtualX, pScrn->virtualY);
249
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100250 err = armsoc_bo_add_fb(pARMSOC->scanout);
251 if (err) {
Ray Smithb4299f82013-03-13 10:08:36 +0000252 ERROR_MSG(
253 "Failed to add framebuffer to the scanout buffer");
David Garbett7171c522012-05-11 13:12:50 +0100254 return FALSE;
Ray Smithb4299f82013-03-13 10:08:36 +0000255 }
Dave Barnish523c9ff2013-03-12 10:59:03 +0000256
Paul Geary8ffd91c2013-04-11 16:03:15 +0100257 fb_id = armsoc_bo_get_fb(pARMSOC->scanout);
Paul Geary6fe52f32013-04-03 11:12:24 +0100258 if (0 == fb_id)
259 return FALSE;
David Garbett7171c522012-05-11 13:12:50 +0100260 }
Rob Clark487687e2011-07-17 17:29:02 -0500261
Rob Clark487687e2011-07-17 17:29:02 -0500262 /* Set the new mode: */
263 crtc->mode = *mode;
264 crtc->x = x;
265 crtc->y = y;
266 crtc->rotation = rotation;
267
268 output_ids = calloc(sizeof(uint32_t), xf86_config->num_output);
269 if (!output_ids) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100270 ERROR_MSG(
271 "memory allocation failed in drmmode_set_mode_major()");
Rob Clark487687e2011-07-17 17:29:02 -0500272 ret = FALSE;
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100273 goto cleanup;
Rob Clark487687e2011-07-17 17:29:02 -0500274 }
275
276 for (i = 0; i < xf86_config->num_output; i++) {
277 xf86OutputPtr output = xf86_config->output[i];
Dave Barnish2e998952013-06-11 16:31:10 +0100278 struct drmmode_output_priv *drmmode_output;
Rob Clark487687e2011-07-17 17:29:02 -0500279
280 if (output->crtc != crtc)
281 continue;
282
283 drmmode_output = output->driver_private;
284 output_ids[output_count] =
285 drmmode_output->mode_output->connector_id;
286 output_count++;
287 }
288
Dave Barnish2e998952013-06-11 16:31:10 +0100289 if (!xf86CrtcRotate(crtc)) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100290 ERROR_MSG(
291 "failed to assign rotation in drmmode_set_mode_major()");
Dave Barnish523c9ff2013-03-12 10:59:03 +0000292 ret = FALSE;
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100293 goto cleanup;
Dave Barnish523c9ff2013-03-12 10:59:03 +0000294 }
Rob Clark487687e2011-07-17 17:29:02 -0500295
Mandeep Singh Bainesc874fcb2012-09-13 15:30:00 +0200296 if (crtc->funcs->gamma_set)
297 crtc->funcs->gamma_set(crtc, crtc->gamma_red, crtc->gamma_green,
298 crtc->gamma_blue, crtc->gamma_size);
Rob Clark487687e2011-07-17 17:29:02 -0500299
300 drmmode_ConvertToKMode(crtc->scrn, &kmode, mode);
301
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100302 err = drmModeSetCrtc(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id,
Rob Clark487687e2011-07-17 17:29:02 -0500303 fb_id, x, y, output_ids, output_count, &kmode);
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100304 if (err) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100305 ERROR_MSG(
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100306 "drm failed to set mode: %s", strerror(-err));
307
Dave Barnish523c9ff2013-03-12 10:59:03 +0000308 ret = FALSE;
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100309 if (!drmmode_revert_mode(crtc, output_ids, output_count))
310 goto cleanup;
311 else
312 goto done_setting;
Dave Barnishf256ebe2013-08-22 15:05:58 +0100313 }
314
315 /* get the actual crtc info */
316 newcrtc = drmModeGetCrtc(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id);
317 if (!newcrtc) {
318 ERROR_MSG("couldn't get actual mode back");
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100319
Dave Barnishf256ebe2013-08-22 15:05:58 +0100320 ret = FALSE;
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100321 if (!drmmode_revert_mode(crtc, output_ids, output_count))
322 goto cleanup;
323 else
324 goto done_setting;
Dave Barnishf256ebe2013-08-22 15:05:58 +0100325 }
326
327 if (kmode.hdisplay != newcrtc->mode.hdisplay ||
328 kmode.vdisplay != newcrtc->mode.vdisplay) {
329
Dave Barnishf256ebe2013-08-22 15:05:58 +0100330 ERROR_MSG(
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100331 "drm did not set requested mode! (requested %dx%d, actual %dx%d)",
332 kmode.hdisplay, kmode.vdisplay,
333 newcrtc->mode.hdisplay,
334 newcrtc->mode.vdisplay);
Dave Barnishf256ebe2013-08-22 15:05:58 +0100335
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100336 ret = FALSE;
337 if (!drmmode_revert_mode(crtc, output_ids, output_count))
338 goto cleanup;
339 else
340 goto done_setting;
Rob Clark487687e2011-07-17 17:29:02 -0500341 }
342
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100343 /* When called on a resize, crtc->mode already contains the
344 * resized values so we can't use this for recovery.
345 * We can't read it out of the crtc either as mode_valid is 0.
346 * Instead we save the last good mode set here & fallback to
347 * that on failure.
348 */
349 DEBUG_MSG("Saving last good values");
350 drmmode_crtc->last_good_x = crtc->x;
351 drmmode_crtc->last_good_y = crtc->y;
352 drmmode_crtc->last_good_rotation = crtc->rotation;
353 if (drmmode_crtc->last_good_mode) {
354 if (drmmode_crtc->last_good_mode->name)
355 free(drmmode_crtc->last_good_mode->name);
356 free(drmmode_crtc->last_good_mode);
357 }
358 drmmode_crtc->last_good_mode = xf86DuplicateMode(&crtc->mode);
359
360 ret = TRUE;
361
362done_setting:
Rob Clark487687e2011-07-17 17:29:02 -0500363 /* Turn on any outputs on this crtc that may have been disabled: */
364 for (i = 0; i < xf86_config->num_output; i++) {
365 xf86OutputPtr output = xf86_config->output[i];
366
367 if (output->crtc != crtc)
368 continue;
369
370 drmmode_output_dpms(output, DPMSModeOn);
371 }
372
Dave Barnishaf046152013-05-31 09:12:44 +0100373 /* if hw cursor is initialized, reload it */
Dave Barnish2e998952013-06-11 16:31:10 +0100374 if (drmmode->cursor)
Dave Barnishaf046152013-05-31 09:12:44 +0100375 xf86_reload_cursors(pScrn->pScreen);
Rob Clark487687e2011-07-17 17:29:02 -0500376
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100377cleanup:
Dave Barnishf256ebe2013-08-22 15:05:58 +0100378 if (newcrtc)
379 drmModeFreeCrtc(newcrtc);
380
Dave Barnish2e998952013-06-11 16:31:10 +0100381 if (output_ids)
Rob Clark487687e2011-07-17 17:29:02 -0500382 free(output_ids);
Dave Barnish2e998952013-06-11 16:31:10 +0100383
Dave Barnishf245da32013-08-30 12:16:58 +0100384 if (!ret && !drmmode_crtc->last_good_mode) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100385 /* If there was a problem, restore the last good mode: */
Dave Barnishf245da32013-08-30 12:16:58 +0100386 crtc->x = drmmode_crtc->last_good_x;
387 crtc->y = drmmode_crtc->last_good_y;
388 crtc->rotation = drmmode_crtc->last_good_rotation;
389 crtc->mode = *drmmode_crtc->last_good_mode;
Rob Clark487687e2011-07-17 17:29:02 -0500390 }
391
392 TRACE_EXIT();
393 return ret;
Rob Clark74210d52012-01-08 17:59:08 -0600394}
Rob Clark487687e2011-07-17 17:29:02 -0500395
396static void
Rob Clark487687e2011-07-17 17:29:02 -0500397drmmode_hide_cursor(xf86CrtcPtr crtc)
398{
Dave Barnish2e998952013-06-11 16:31:10 +0100399 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
400 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
401 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Javier Martin4c572792013-09-11 15:08:19 +0100402 ScrnInfoPtr pScrn = crtc->scrn;
403 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -0500404
Rob Clark687c6082012-01-08 19:33:18 -0600405 if (!cursor)
406 return;
407
Stéphane Marchesinb8b35202012-10-02 20:27:40 -0700408 drmmode_crtc->cursor_visible = FALSE;
Rob Clark687c6082012-01-08 19:33:18 -0600409
Javier Martin4c572792013-09-11 15:08:19 +0100410 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE) {
411 /* set plane's fb_id to 0 to disable it */
412 drmModeSetPlane(drmmode->fd, cursor->ovr->plane_id,
413 drmmode_crtc->mode_crtc->crtc_id, 0, 0,
414 0, 0, 0, 0, 0, 0, 0, 0);
415 } else { /* HWCURSOR_API_STANDARD */
416 /* set handle to 0 to disable the cursor */
417 drmModeSetCursor(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id,
418 0, 0, 0);
419 }
Rob Clark74210d52012-01-08 17:59:08 -0600420}
Rob Clark487687e2011-07-17 17:29:02 -0500421
Javier Martin4c572792013-09-11 15:08:19 +0100422/*
423 * The argument "update_image" controls whether the cursor image needs
424 * to be updated by the HW or not. This is ignored by HWCURSOR_API_PLANE
425 * which doesn't allow changing the cursor possition without updating
426 * the image too.
427 */
Rob Clark487687e2011-07-17 17:29:02 -0500428static void
Javier Martin4c572792013-09-11 15:08:19 +0100429drmmode_show_cursor_image(xf86CrtcPtr crtc, Bool update_image)
Rob Clark487687e2011-07-17 17:29:02 -0500430{
Dave Barnish2e998952013-06-11 16:31:10 +0100431 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
432 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
433 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Dave Barnishde45ed42013-06-05 13:47:56 +0100434 int crtc_x, crtc_y, src_x, src_y;
435 int w, h, pad;
436 ScrnInfoPtr pScrn = crtc->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100437 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -0500438
Rob Clark687c6082012-01-08 19:33:18 -0600439 if (!cursor)
440 return;
Rob Clark487687e2011-07-17 17:29:02 -0500441
Stéphane Marchesinb8b35202012-10-02 20:27:40 -0700442 drmmode_crtc->cursor_visible = TRUE;
Rob Clark687c6082012-01-08 19:33:18 -0600443
Dave Barnishde45ed42013-06-05 13:47:56 +0100444 w = pARMSOC->drmmode_interface->cursor_width;
445 h = pARMSOC->drmmode_interface->cursor_height;
446 pad = pARMSOC->drmmode_interface->cursor_padding;
447
Dave Barnish2e998952013-06-11 16:31:10 +0100448 /* get padded width */
449 w = w + 2 * pad;
450 /* get x of padded cursor */
451 crtc_x = cursor->x - pad;
Rob Clark60f5bad2012-01-22 18:35:28 -0600452 crtc_y = cursor->y;
Rob Clark60f5bad2012-01-22 18:35:28 -0600453
Javier Martin4c572792013-09-11 15:08:19 +0100454 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE) {
455 src_x = 0;
456 src_y = 0;
Rob Clark60f5bad2012-01-22 18:35:28 -0600457
Javier Martin4c572792013-09-11 15:08:19 +0100458 /* calculate clipped x, y, w & h if cursor is off edges */
459 if (crtc_x < 0) {
460 src_x += -crtc_x;
461 w -= -crtc_x;
462 crtc_x = 0;
463 }
Rob Clark60f5bad2012-01-22 18:35:28 -0600464
Javier Martin4c572792013-09-11 15:08:19 +0100465 if (crtc_y < 0) {
466 src_y += -crtc_y;
467 h -= -crtc_y;
468 crtc_y = 0;
469 }
Rob Clark60f5bad2012-01-22 18:35:28 -0600470
Javier Martin4c572792013-09-11 15:08:19 +0100471 if ((crtc_x + w) > crtc->mode.HDisplay)
472 w = crtc->mode.HDisplay - crtc_x;
Rob Clark60f5bad2012-01-22 18:35:28 -0600473
Javier Martin4c572792013-09-11 15:08:19 +0100474 if ((crtc_y + h) > crtc->mode.VDisplay)
475 h = crtc->mode.VDisplay - crtc_y;
476
477 /* note src coords (last 4 args) are in Q16 format */
478 drmModeSetPlane(drmmode->fd, cursor->ovr->plane_id,
Rob Clark687c6082012-01-08 19:33:18 -0600479 drmmode_crtc->mode_crtc->crtc_id, cursor->fb_id, 0,
Dave Barnish2e998952013-06-11 16:31:10 +0100480 crtc_x, crtc_y, w, h, src_x<<16, src_y<<16,
481 w<<16, h<<16);
Javier Martin4c572792013-09-11 15:08:19 +0100482 } else {
483 if (update_image)
484 drmModeSetCursor(drmmode->fd,
485 drmmode_crtc->mode_crtc->crtc_id,
486 cursor->handle, w, h);
487 drmModeMoveCursor(drmmode->fd,
488 drmmode_crtc->mode_crtc->crtc_id,
489 crtc_x, crtc_y);
490 }
491}
492
493static void
494drmmode_show_cursor(xf86CrtcPtr crtc)
495{
496 drmmode_show_cursor_image(crtc, TRUE);
Rob Clark687c6082012-01-08 19:33:18 -0600497}
498
499static void
500drmmode_set_cursor_position(xf86CrtcPtr crtc, int x, int y)
501{
Dave Barnish2e998952013-06-11 16:31:10 +0100502 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
503 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
504 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Rob Clark687c6082012-01-08 19:33:18 -0600505
506 if (!cursor)
507 return;
508
509 cursor->x = x;
510 cursor->y = y;
511
Javier Martin4c572792013-09-11 15:08:19 +0100512 /*
513 * Show the cursor at a different possition without updating the image
514 * when possible (HWCURSOR_API_PLANE doesn't have a way to update
515 * cursor position without updating the image too).
516 */
517 drmmode_show_cursor_image(crtc, FALSE);
Rob Clark687c6082012-01-08 19:33:18 -0600518}
519
Javier Martina7e316f2013-09-16 15:22:41 +0100520/*
521 * The cursor format is ARGB so the image can be copied straight over.
522 * Columns of CURSORPAD blank pixels are maintained down either side
523 * of the destination image. This is a workaround for a bug causing
524 * corruption when the cursor reaches the screen edges in some DRM
525 * drivers.
526 */
527static void set_cursor_image(xf86CrtcPtr crtc, uint32_t *d, CARD32 *s)
528{
529 ScrnInfoPtr pScrn = crtc->scrn;
530 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
531 int row;
532 void *dst;
533 const char *src_row;
534 char *dst_row;
535 uint32_t cursorh = pARMSOC->drmmode_interface->cursor_height;
536 uint32_t cursorw = pARMSOC->drmmode_interface->cursor_width;
537 uint32_t cursorpad = pARMSOC->drmmode_interface->cursor_padding;
538
539 dst = d;
540 for (row = 0; row < cursorh; row += 1) {
541 /* we're operating with ARGB data (4 bytes per pixel) */
542 src_row = (const char *)s + row * 4 * cursorw;
543 dst_row = (char *)dst + row * 4 * (cursorw + 2 * cursorpad);
544
545 /* set first CURSORPAD pixels in row to 0 */
546 memset(dst_row, 0, (4 * cursorpad));
547 /* copy cursor image pixel row across */
548 memcpy(dst_row + (4 * cursorpad), src_row, 4 * cursorw);
549 /* set last CURSORPAD pixels in row to 0 */
550 memset(dst_row + 4 * (cursorpad + cursorw),
551 0, (4 * cursorpad));
552 }
553}
554
Rob Clark687c6082012-01-08 19:33:18 -0600555static void
556drmmode_load_cursor_argb(xf86CrtcPtr crtc, CARD32 *image)
557{
Dave Barnish2e998952013-06-11 16:31:10 +0100558 uint32_t *d;
559 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
560 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
561 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Rob Clark979add52012-02-21 18:35:24 -0600562 int visible;
Rob Clark687c6082012-01-08 19:33:18 -0600563
564 if (!cursor)
565 return;
566
Stéphane Marchesinb8b35202012-10-02 20:27:40 -0700567 visible = drmmode_crtc->cursor_visible;
Rob Clark979add52012-02-21 18:35:24 -0600568
569 if (visible)
Rob Clark687c6082012-01-08 19:33:18 -0600570 drmmode_hide_cursor(crtc);
571
Paul Geary8ffd91c2013-04-11 16:03:15 +0100572 d = armsoc_bo_map(cursor->bo);
Dave Barnish2e998952013-06-11 16:31:10 +0100573 if (!d) {
Paul Geary6fe52f32013-04-03 11:12:24 +0100574 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
575 "load_cursor_argb map failure\n");
576 if (visible)
Javier Martin4c572792013-09-11 15:08:19 +0100577 drmmode_show_cursor_image(crtc, TRUE);
Paul Geary6fe52f32013-04-03 11:12:24 +0100578 return;
579 }
John Rees292ae502013-03-21 16:24:35 +0000580
Javier Martina7e316f2013-09-16 15:22:41 +0100581 set_cursor_image(crtc, d, image);
Rob Clark687c6082012-01-08 19:33:18 -0600582
Rob Clark979add52012-02-21 18:35:24 -0600583 if (visible)
Javier Martin4c572792013-09-11 15:08:19 +0100584 drmmode_show_cursor_image(crtc, TRUE);
Rob Clark687c6082012-01-08 19:33:18 -0600585}
586
Javier Martin4c572792013-09-11 15:08:19 +0100587static Bool
588drmmode_cursor_init_plane(ScreenPtr pScreen)
Rob Clark687c6082012-01-08 19:33:18 -0600589{
Daniel Kurtz0361e002013-08-14 21:07:01 +0800590 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Dave Barnish2e998952013-06-11 16:31:10 +0100591 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
592 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
593 struct drmmode_cursor_rec *cursor;
Rob Clark687c6082012-01-08 19:33:18 -0600594 drmModePlaneRes *plane_resources;
595 drmModePlane *ovr;
Dave Barnishde45ed42013-06-05 13:47:56 +0100596 int w, h, pad;
Rob Clark687c6082012-01-08 19:33:18 -0600597 uint32_t handles[4], pitches[4], offsets[4]; /* we only use [0] */
598
599 if (drmmode->cursor) {
600 INFO_MSG("cursor already initialized");
601 return TRUE;
602 }
603
Dave Barnish2e998952013-06-11 16:31:10 +0100604 if (!xf86LoaderCheckSymbol("drmModeGetPlaneResources")) {
605 ERROR_MSG(
606 "HW cursor not supported (needs libdrm 2.4.30 or higher)");
Ray Smith003cf5e2013-03-19 10:44:07 +0000607 return FALSE;
608 }
Rob Clark687c6082012-01-08 19:33:18 -0600609
610 /* find an unused plane which can be used as a mouse cursor. Note
611 * that we cheat a bit, in order to not burn one overlay per crtc,
612 * and only show the mouse cursor on one crtc at a time
613 */
614 plane_resources = drmModeGetPlaneResources(drmmode->fd);
615 if (!plane_resources) {
Dave Barnish2e998952013-06-11 16:31:10 +0100616 ERROR_MSG("HW cursor: drmModeGetPlaneResources failed: %s",
617 strerror(errno));
Rob Clark687c6082012-01-08 19:33:18 -0600618 return FALSE;
619 }
620
621 if (plane_resources->count_planes < 1) {
622 ERROR_MSG("not enough planes for HW cursor");
Ray Smith003cf5e2013-03-19 10:44:07 +0000623 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600624 return FALSE;
625 }
626
627 ovr = drmModeGetPlane(drmmode->fd, plane_resources->planes[0]);
628 if (!ovr) {
Dave Barnish2e998952013-06-11 16:31:10 +0100629 ERROR_MSG("HW cursor: drmModeGetPlane failed: %s",
630 strerror(errno));
Ray Smith003cf5e2013-03-19 10:44:07 +0000631 drmModeFreePlaneResources(plane_resources);
632 return FALSE;
633 }
634
Dave Barnishde45ed42013-06-05 13:47:56 +0100635 if (pARMSOC->drmmode_interface->init_plane_for_cursor &&
Dave Barnish2e998952013-06-11 16:31:10 +0100636 pARMSOC->drmmode_interface->init_plane_for_cursor(
637 drmmode->fd, ovr->plane_id)) {
Ray Smithb93cd892013-04-03 10:06:18 +0100638 ERROR_MSG("Failed driver-specific cursor initialization");
639 drmModeFreePlaneResources(plane_resources);
640 return FALSE;
641 }
642
Dave Barnish2e998952013-06-11 16:31:10 +0100643 cursor = calloc(1, sizeof(struct drmmode_cursor_rec));
Ray Smith003cf5e2013-03-19 10:44:07 +0000644 if (!cursor) {
Dave Barnishfd50b132013-05-16 15:00:02 +0100645 ERROR_MSG("HW cursor: calloc failed");
Ray Smith003cf5e2013-03-19 10:44:07 +0000646 drmModeFreePlane(ovr);
647 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600648 return FALSE;
649 }
650
651 cursor->ovr = ovr;
Dave Barnishde45ed42013-06-05 13:47:56 +0100652
653 w = pARMSOC->drmmode_interface->cursor_width;
654 h = pARMSOC->drmmode_interface->cursor_height;
655 pad = pARMSOC->drmmode_interface->cursor_padding;
656
657 /* allow for cursor padding in the bo */
Dave Barnish2e998952013-06-11 16:31:10 +0100658 cursor->bo = armsoc_bo_new_with_dim(pARMSOC->dev,
659 w + 2 * pad, h,
660 0, 32, ARMSOC_BO_SCANOUT);
Rob Clark687c6082012-01-08 19:33:18 -0600661
Ray Smith003cf5e2013-03-19 10:44:07 +0000662 if (!cursor->bo) {
Dave Barnishfd50b132013-05-16 15:00:02 +0100663 ERROR_MSG("HW cursor: buffer allocation failed");
Ray Smith003cf5e2013-03-19 10:44:07 +0000664 free(cursor);
665 drmModeFreePlane(ovr);
666 drmModeFreePlaneResources(plane_resources);
667 return FALSE;
668 }
669
Paul Geary8ffd91c2013-04-11 16:03:15 +0100670 handles[0] = armsoc_bo_handle(cursor->bo);
671 pitches[0] = armsoc_bo_pitch(cursor->bo);
Rob Clark687c6082012-01-08 19:33:18 -0600672 offsets[0] = 0;
673
Dave Barnishde45ed42013-06-05 13:47:56 +0100674 /* allow for cursor padding in the fb */
675 if (drmModeAddFB2(drmmode->fd, w + 2 * pad, h, DRM_FORMAT_ARGB8888,
Rob Clark687c6082012-01-08 19:33:18 -0600676 handles, pitches, offsets, &cursor->fb_id, 0)) {
Dave Barnish2e998952013-06-11 16:31:10 +0100677 ERROR_MSG("HW cursor: drmModeAddFB2 failed: %s",
678 strerror(errno));
Paul Geary8ffd91c2013-04-11 16:03:15 +0100679 armsoc_bo_unreference(cursor->bo);
Ray Smith003cf5e2013-03-19 10:44:07 +0000680 free(cursor);
681 drmModeFreePlane(ovr);
682 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600683 return FALSE;
684 }
685
Ray Smith003cf5e2013-03-19 10:44:07 +0000686 if (!xf86_cursors_init(pScreen, w, h, HARDWARE_CURSOR_ARGB)) {
687 ERROR_MSG("xf86_cursors_init() failed");
Dave Barnish2e998952013-06-11 16:31:10 +0100688 if (drmModeRmFB(drmmode->fd, cursor->fb_id))
Ray Smith003cf5e2013-03-19 10:44:07 +0000689 ERROR_MSG("drmModeRmFB() failed");
Dave Barnish2e998952013-06-11 16:31:10 +0100690
Paul Geary8ffd91c2013-04-11 16:03:15 +0100691 armsoc_bo_unreference(cursor->bo);
Ray Smith003cf5e2013-03-19 10:44:07 +0000692 free(cursor);
693 drmModeFreePlane(ovr);
694 drmModeFreePlaneResources(plane_resources);
695 return FALSE;
Rob Clark687c6082012-01-08 19:33:18 -0600696 }
697
Ray Smith003cf5e2013-03-19 10:44:07 +0000698 INFO_MSG("HW cursor initialized");
699 drmmode->cursor = cursor;
Dave Barnishfd50b132013-05-16 15:00:02 +0100700 drmModeFreePlaneResources(plane_resources);
Ray Smith003cf5e2013-03-19 10:44:07 +0000701 return TRUE;
Rob Clark74210d52012-01-08 17:59:08 -0600702}
Rob Clark487687e2011-07-17 17:29:02 -0500703
Javier Martin4c572792013-09-11 15:08:19 +0100704static Bool
705drmmode_cursor_init_standard(ScreenPtr pScreen)
706{
707 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
708 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
709 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
710 struct drmmode_cursor_rec *cursor;
711 int w, h, pad;
712
713 if (drmmode->cursor) {
714 INFO_MSG("cursor already initialized");
715 return TRUE;
716 }
717
718 if (!xf86LoaderCheckSymbol("drmModeSetCursor") ||
719 !xf86LoaderCheckSymbol("drmModeMoveCursor")) {
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100720 ERROR_MSG(
721 "Standard HW cursor not supported (needs libdrm 2.4.3 or higher)");
Javier Martin4c572792013-09-11 15:08:19 +0100722 return FALSE;
723 }
724
725 cursor = calloc(1, sizeof(struct drmmode_cursor_rec));
726 if (!cursor) {
727 ERROR_MSG("HW cursor (standard): calloc failed");
728 return FALSE;
729 }
730
731 w = pARMSOC->drmmode_interface->cursor_width;
732 h = pARMSOC->drmmode_interface->cursor_height;
733 pad = pARMSOC->drmmode_interface->cursor_padding;
734
735 /* allow for cursor padding in the bo */
736 cursor->bo = armsoc_bo_new_with_dim(pARMSOC->dev,
737 w + 2 * pad, h,
738 0, 32, ARMSOC_BO_SCANOUT);
739
740 if (!cursor->bo) {
741 ERROR_MSG("HW cursor (standard): buffer allocation failed");
742 free(cursor);
743 return FALSE;
744 }
745
746 cursor->handle = armsoc_bo_handle(cursor->bo);
747
748 if (!xf86_cursors_init(pScreen, w, h, HARDWARE_CURSOR_ARGB)) {
749 ERROR_MSG("xf86_cursors_init() failed");
750 if (drmModeRmFB(drmmode->fd, cursor->fb_id))
751 ERROR_MSG("drmModeRmFB() failed");
752
753 armsoc_bo_unreference(cursor->bo);
754 free(cursor);
755 return FALSE;
756 }
757
758 INFO_MSG("HW cursor initialized");
759 drmmode->cursor = cursor;
760 return TRUE;
761}
762
763Bool drmmode_cursor_init(ScreenPtr pScreen)
764{
765 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
766 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
767
768 INFO_MSG("HW cursor init()");
769
770 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE)
771 return drmmode_cursor_init_plane(pScreen);
772 else /* HWCURSOR_API_STANDARD */
773 return drmmode_cursor_init_standard(pScreen);
774}
775
Dave Barnishfd50b132013-05-16 15:00:02 +0100776void drmmode_cursor_fini(ScreenPtr pScreen)
777{
Daniel Kurtz0361e002013-08-14 21:07:01 +0800778 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Dave Barnish2e998952013-06-11 16:31:10 +0100779 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
780 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Javier Martin4c572792013-09-11 15:08:19 +0100781 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Dave Barnishfd50b132013-05-16 15:00:02 +0100782
Dave Barnish2e998952013-06-11 16:31:10 +0100783 if (!cursor)
Dave Barnishfd50b132013-05-16 15:00:02 +0100784 return;
Dave Barnish2e998952013-06-11 16:31:10 +0100785
Dave Barnishfd50b132013-05-16 15:00:02 +0100786 drmmode->cursor = NULL;
787 xf86_cursors_fini(pScreen);
Javier Martin4c572792013-09-11 15:08:19 +0100788 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE)
789 drmModeRmFB(drmmode->fd, cursor->fb_id);
Dave Barnishfd50b132013-05-16 15:00:02 +0100790 armsoc_bo_unreference(cursor->bo);
Javier Martin4c572792013-09-11 15:08:19 +0100791 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE)
792 drmModeFreePlane(cursor->ovr);
Dave Barnishfd50b132013-05-16 15:00:02 +0100793 free(cursor);
794}
795
796
Dave Barnish2e998952013-06-11 16:31:10 +0100797#if 1 == ARMSOC_SUPPORT_GAMMA
Rob Clark487687e2011-07-17 17:29:02 -0500798static void
799drmmode_gamma_set(xf86CrtcPtr crtc, CARD16 *red, CARD16 *green, CARD16 *blue,
Rob Clark74210d52012-01-08 17:59:08 -0600800 int size)
Rob Clark487687e2011-07-17 17:29:02 -0500801{
Dave Barnish2e998952013-06-11 16:31:10 +0100802 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
803 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500804 int ret;
805
806 ret = drmModeCrtcSetGamma(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id,
Rob Clark74210d52012-01-08 17:59:08 -0600807 size, red, green, blue);
Rob Clark487687e2011-07-17 17:29:02 -0500808 if (ret != 0) {
809 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
Rob Clark74210d52012-01-08 17:59:08 -0600810 "failed to set gamma: %s\n", strerror(-ret));
Rob Clark487687e2011-07-17 17:29:02 -0500811 }
812}
Mandeep Singh Bainesc874fcb2012-09-13 15:30:00 +0200813#endif
Rob Clark487687e2011-07-17 17:29:02 -0500814
815static const xf86CrtcFuncsRec drmmode_crtc_funcs = {
Rob Clark74210d52012-01-08 17:59:08 -0600816 .dpms = drmmode_crtc_dpms,
817 .set_mode_major = drmmode_set_mode_major,
818 .set_cursor_position = drmmode_set_cursor_position,
819 .show_cursor = drmmode_show_cursor,
820 .hide_cursor = drmmode_hide_cursor,
821 .load_cursor_argb = drmmode_load_cursor_argb,
Dave Barnish2e998952013-06-11 16:31:10 +0100822#if 1 == ARMSOC_SUPPORT_GAMMA
Rob Clark74210d52012-01-08 17:59:08 -0600823 .gamma_set = drmmode_gamma_set,
Mandeep Singh Bainesc874fcb2012-09-13 15:30:00 +0200824#endif
Rob Clark487687e2011-07-17 17:29:02 -0500825};
826
827
828static void
Dave Barnish2e998952013-06-11 16:31:10 +0100829drmmode_crtc_init(ScrnInfoPtr pScrn, struct drmmode_rec *drmmode, int num)
Rob Clark487687e2011-07-17 17:29:02 -0500830{
Rob Clark74210d52012-01-08 17:59:08 -0600831 xf86CrtcPtr crtc;
Dave Barnish2e998952013-06-11 16:31:10 +0100832 struct drmmode_crtc_private_rec *drmmode_crtc;
Rob Clark487687e2011-07-17 17:29:02 -0500833
Rob Clark74210d52012-01-08 17:59:08 -0600834 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -0500835
Rob Clark74210d52012-01-08 17:59:08 -0600836 crtc = xf86CrtcCreate(pScrn, &drmmode_crtc_funcs);
837 if (crtc == NULL)
838 return;
839
Dave Barnish2e998952013-06-11 16:31:10 +0100840 drmmode_crtc = xnfcalloc(sizeof(struct drmmode_crtc_private_rec), 1);
Rob Clark74210d52012-01-08 17:59:08 -0600841 drmmode_crtc->mode_crtc = drmModeGetCrtc(drmmode->fd,
842 drmmode->mode_res->crtcs[num]);
843 drmmode_crtc->drmmode = drmmode;
Dave Barnishf245da32013-08-30 12:16:58 +0100844 drmmode_crtc->last_good_mode = NULL;
Rob Clark74210d52012-01-08 17:59:08 -0600845
Dave Barnishf245da32013-08-30 12:16:58 +0100846 INFO_MSG("Got CRTC: %d (id: %d)",
847 num, drmmode_crtc->mode_crtc->crtc_id);
Rob Clark74210d52012-01-08 17:59:08 -0600848 crtc->driver_private = drmmode_crtc;
849
850 TRACE_EXIT();
Rob Clark487687e2011-07-17 17:29:02 -0500851 return;
Rob Clark74210d52012-01-08 17:59:08 -0600852}
Rob Clark487687e2011-07-17 17:29:02 -0500853
854static xf86OutputStatus
855drmmode_output_detect(xf86OutputPtr output)
856{
857 /* go to the hw and retrieve a new output struct */
Dave Barnish2e998952013-06-11 16:31:10 +0100858 struct drmmode_output_priv *drmmode_output = output->driver_private;
859 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500860 xf86OutputStatus status;
861 drmModeFreeConnector(drmmode_output->mode_output);
862
863 drmmode_output->mode_output =
Dave Barnish2e998952013-06-11 16:31:10 +0100864 drmModeGetConnector(drmmode->fd,
865 drmmode_output->output_id);
Rob Clark487687e2011-07-17 17:29:02 -0500866
867 switch (drmmode_output->mode_output->connection) {
868 case DRM_MODE_CONNECTED:
869 status = XF86OutputStatusConnected;
870 break;
871 case DRM_MODE_DISCONNECTED:
872 status = XF86OutputStatusDisconnected;
873 break;
874 default:
875 case DRM_MODE_UNKNOWNCONNECTION:
876 status = XF86OutputStatusUnknown;
877 break;
878 }
879 return status;
880}
881
882static Bool
883drmmode_output_mode_valid(xf86OutputPtr output, DisplayModePtr mode)
884{
885 if (mode->type & M_T_DEFAULT)
886 /* Default modes are harmful here. */
887 return MODE_BAD;
888
889 return MODE_OK;
890}
891
892static DisplayModePtr
893drmmode_output_get_modes(xf86OutputPtr output)
894{
895 ScrnInfoPtr pScrn = output->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100896 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500897 drmModeConnectorPtr koutput = drmmode_output->mode_output;
Dave Barnish2e998952013-06-11 16:31:10 +0100898 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Dave Barnish0bdb7382013-06-06 12:04:04 +0100899 DisplayModePtr modes = NULL;
Daniel Kurtzb8131892012-10-17 01:39:54 +0800900 drmModePropertyPtr prop;
Rob Clark487687e2011-07-17 17:29:02 -0500901 xf86MonPtr ddc_mon = NULL;
902 int i;
903
904 /* look for an EDID property */
905 for (i = 0; i < koutput->count_props; i++) {
Daniel Kurtzb8131892012-10-17 01:39:54 +0800906 prop = drmModeGetProperty(drmmode->fd, koutput->props[i]);
907 if (!prop)
Rob Clark487687e2011-07-17 17:29:02 -0500908 continue;
909
Daniel Kurtzb8131892012-10-17 01:39:54 +0800910 if ((prop->flags & DRM_MODE_PROP_BLOB) &&
911 !strcmp(prop->name, "EDID")) {
Rob Clark487687e2011-07-17 17:29:02 -0500912 if (drmmode_output->edid_blob)
Dave Barnish2e998952013-06-11 16:31:10 +0100913 drmModeFreePropertyBlob(
914 drmmode_output->edid_blob);
Rob Clark487687e2011-07-17 17:29:02 -0500915 drmmode_output->edid_blob =
Rob Clark74210d52012-01-08 17:59:08 -0600916 drmModeGetPropertyBlob(drmmode->fd,
Dave Barnish2e998952013-06-11 16:31:10 +0100917 koutput->prop_values[i]);
Rob Clark487687e2011-07-17 17:29:02 -0500918 }
Daniel Kurtzb8131892012-10-17 01:39:54 +0800919 drmModeFreeProperty(prop);
Rob Clark487687e2011-07-17 17:29:02 -0500920 }
921
922 if (drmmode_output->edid_blob)
923 ddc_mon = xf86InterpretEDID(pScrn->scrnIndex,
Rob Clark74210d52012-01-08 17:59:08 -0600924 drmmode_output->edid_blob->data);
Rob Clark487687e2011-07-17 17:29:02 -0500925
926 if (ddc_mon) {
Rob Clark487687e2011-07-17 17:29:02 -0500927 xf86OutputSetEDID(output, ddc_mon);
928 xf86SetDDCproperties(pScrn, ddc_mon);
929 }
930
931 DEBUG_MSG("count_modes: %d", koutput->count_modes);
932
933 /* modes should already be available */
934 for (i = 0; i < koutput->count_modes; i++) {
Dave Barnish0bdb7382013-06-06 12:04:04 +0100935 DisplayModePtr mode = xnfalloc(sizeof(DisplayModeRec));
Rob Clark487687e2011-07-17 17:29:02 -0500936
Dave Barnish0bdb7382013-06-06 12:04:04 +0100937 drmmode_ConvertFromKMode(pScrn, &koutput->modes[i], mode);
938 modes = xf86ModesAdd(modes, mode);
Rob Clark487687e2011-07-17 17:29:02 -0500939 }
Dave Barnish0bdb7382013-06-06 12:04:04 +0100940 return modes;
Rob Clark487687e2011-07-17 17:29:02 -0500941}
942
943static void
944drmmode_output_destroy(xf86OutputPtr output)
945{
Dave Barnish2e998952013-06-11 16:31:10 +0100946 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500947 int i;
948
949 if (drmmode_output->edid_blob)
950 drmModeFreePropertyBlob(drmmode_output->edid_blob);
951 for (i = 0; i < drmmode_output->num_props; i++) {
952 drmModeFreeProperty(drmmode_output->props[i].mode_prop);
953 free(drmmode_output->props[i].atoms);
954 }
Daniel Kurtzf5a38ad2012-10-17 02:03:34 +0800955 free(drmmode_output->props);
Rob Clark487687e2011-07-17 17:29:02 -0500956 drmModeFreeConnector(drmmode_output->mode_output);
957 free(drmmode_output);
958 output->driver_private = NULL;
959}
960
961static void
962drmmode_output_dpms(xf86OutputPtr output, int mode)
963{
Dave Barnish2e998952013-06-11 16:31:10 +0100964 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500965 drmModeConnectorPtr koutput = drmmode_output->mode_output;
Daniel Kurtzb8131892012-10-17 01:39:54 +0800966 drmModePropertyPtr prop;
Dave Barnish2e998952013-06-11 16:31:10 +0100967 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500968 int mode_id = -1, i;
969
970 for (i = 0; i < koutput->count_props; i++) {
Daniel Kurtzb8131892012-10-17 01:39:54 +0800971 prop = drmModeGetProperty(drmmode->fd, koutput->props[i]);
972 if (!prop)
973 continue;
974 if ((prop->flags & DRM_MODE_PROP_ENUM) &&
975 !strcmp(prop->name, "DPMS")) {
976 mode_id = koutput->props[i];
977 drmModeFreeProperty(prop);
978 break;
Rob Clark487687e2011-07-17 17:29:02 -0500979 }
Daniel Kurtzb8131892012-10-17 01:39:54 +0800980 drmModeFreeProperty(prop);
Rob Clark487687e2011-07-17 17:29:02 -0500981 }
982
983 if (mode_id < 0)
984 return;
985
986 drmModeConnectorSetProperty(drmmode->fd, koutput->connector_id,
Rob Clark74210d52012-01-08 17:59:08 -0600987 mode_id, mode);
Rob Clark487687e2011-07-17 17:29:02 -0500988}
989
990static Bool
991drmmode_property_ignore(drmModePropertyPtr prop)
992{
993 if (!prop)
Rob Clark74210d52012-01-08 17:59:08 -0600994 return TRUE;
Rob Clark487687e2011-07-17 17:29:02 -0500995 /* ignore blob prop */
996 if (prop->flags & DRM_MODE_PROP_BLOB)
997 return TRUE;
998 /* ignore standard property */
999 if (!strcmp(prop->name, "EDID") ||
Rob Clark74210d52012-01-08 17:59:08 -06001000 !strcmp(prop->name, "DPMS"))
Rob Clark487687e2011-07-17 17:29:02 -05001001 return TRUE;
1002
1003 return FALSE;
1004}
1005
1006static void
1007drmmode_output_create_resources(xf86OutputPtr output)
1008{
Dave Barnish2e998952013-06-11 16:31:10 +01001009 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -05001010 drmModeConnectorPtr mode_output = drmmode_output->mode_output;
Dave Barnish2e998952013-06-11 16:31:10 +01001011 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001012 drmModePropertyPtr drmmode_prop;
1013 uint32_t value;
1014 int i, j, err;
1015
Dave Barnish2e998952013-06-11 16:31:10 +01001016 drmmode_output->props =
1017 calloc(mode_output->count_props,
1018 sizeof(struct drmmode_prop_rec));
Rob Clark487687e2011-07-17 17:29:02 -05001019 if (!drmmode_output->props)
1020 return;
1021
1022 drmmode_output->num_props = 0;
Dave Barnish0bdb7382013-06-06 12:04:04 +01001023 for (i = 0; i < mode_output->count_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001024 drmmode_prop = drmModeGetProperty(drmmode->fd,
1025 mode_output->props[i]);
Rob Clark487687e2011-07-17 17:29:02 -05001026 if (drmmode_property_ignore(drmmode_prop)) {
1027 drmModeFreeProperty(drmmode_prop);
1028 continue;
1029 }
Dave Barnish2e998952013-06-11 16:31:10 +01001030 drmmode_output->props[drmmode_output->num_props].mode_prop =
1031 drmmode_prop;
Dave Barnish0bdb7382013-06-06 12:04:04 +01001032 drmmode_output->props[drmmode_output->num_props].index = i;
Rob Clark487687e2011-07-17 17:29:02 -05001033 drmmode_output->num_props++;
Rob Clark487687e2011-07-17 17:29:02 -05001034 }
1035
1036 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001037 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -05001038 drmmode_prop = p->mode_prop;
1039
1040 value = drmmode_output->mode_output->prop_values[p->index];
1041
1042 if (drmmode_prop->flags & DRM_MODE_PROP_RANGE) {
1043 INT32 range[2];
1044
1045 p->num_atoms = 1;
1046 p->atoms = calloc(p->num_atoms, sizeof(Atom));
1047 if (!p->atoms)
1048 continue;
Dave Barnish2e998952013-06-11 16:31:10 +01001049 p->atoms[0] = MakeAtom(drmmode_prop->name,
1050 strlen(drmmode_prop->name),
1051 TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001052 range[0] = drmmode_prop->values[0];
1053 range[1] = drmmode_prop->values[1];
Dave Barnish2e998952013-06-11 16:31:10 +01001054 err = RRConfigureOutputProperty(output->randr_output,
1055 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -06001056 FALSE, TRUE,
Dave Barnish2e998952013-06-11 16:31:10 +01001057 drmmode_prop->flags &
1058 DRM_MODE_PROP_IMMUTABLE ?
1059 TRUE : FALSE,
1060 2, range);
1061
1062 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001063 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001064 "RRConfigureOutputProperty error, %d\n",
1065 err);
1066
1067 err = RRChangeOutputProperty(output->randr_output,
1068 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -06001069 XA_INTEGER, 32, PropModeReplace, 1,
1070 &value, FALSE, FALSE);
Dave Barnish2e998952013-06-11 16:31:10 +01001071 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001072 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001073 "RRChangeOutputProperty error, %d\n",
1074 err);
1075
Rob Clark487687e2011-07-17 17:29:02 -05001076 } else if (drmmode_prop->flags & DRM_MODE_PROP_ENUM) {
1077 p->num_atoms = drmmode_prop->count_enums + 1;
1078 p->atoms = calloc(p->num_atoms, sizeof(Atom));
1079 if (!p->atoms)
1080 continue;
Dave Barnish2e998952013-06-11 16:31:10 +01001081 p->atoms[0] = MakeAtom(drmmode_prop->name,
1082 strlen(drmmode_prop->name),
1083 TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001084 for (j = 1; j <= drmmode_prop->count_enums; j++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001085 struct drm_mode_property_enum *e =
1086 &drmmode_prop->enums[j-1];
1087 p->atoms[j] = MakeAtom(e->name,
1088 strlen(e->name), TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001089 }
Dave Barnish2e998952013-06-11 16:31:10 +01001090 err = RRConfigureOutputProperty(output->randr_output,
1091 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -06001092 FALSE, FALSE,
Dave Barnish2e998952013-06-11 16:31:10 +01001093 drmmode_prop->flags &
1094 DRM_MODE_PROP_IMMUTABLE ?
1095 TRUE : FALSE,
1096 p->num_atoms - 1,
1097 (INT32 *)&p->atoms[1]);
1098
1099 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001100 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001101 "RRConfigureOutputProperty error, %d\n",
1102 err);
1103
Rob Clark487687e2011-07-17 17:29:02 -05001104 for (j = 0; j < drmmode_prop->count_enums; j++)
1105 if (drmmode_prop->enums[j].value == value)
1106 break;
1107 /* there's always a matching value */
Dave Barnish2e998952013-06-11 16:31:10 +01001108 err = RRChangeOutputProperty(output->randr_output,
1109 p->atoms[0],
1110 XA_ATOM, 32, PropModeReplace, 1,
1111 &p->atoms[j+1], FALSE, FALSE);
1112 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001113 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001114 "RRChangeOutputProperty error, %d\n",
1115 err);
Rob Clark487687e2011-07-17 17:29:02 -05001116 }
1117 }
1118}
1119
1120static Bool
1121drmmode_output_set_property(xf86OutputPtr output, Atom property,
Rob Clark74210d52012-01-08 17:59:08 -06001122 RRPropertyValuePtr value)
Rob Clark487687e2011-07-17 17:29:02 -05001123{
Dave Barnish2e998952013-06-11 16:31:10 +01001124 struct drmmode_output_priv *drmmode_output = output->driver_private;
1125 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001126 int i, ret;
1127
1128 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001129 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -05001130
1131 if (p->atoms[0] != property)
1132 continue;
1133
1134 if (p->mode_prop->flags & DRM_MODE_PROP_RANGE) {
1135 uint32_t val;
1136
1137 if (value->type != XA_INTEGER || value->format != 32 ||
Rob Clark74210d52012-01-08 17:59:08 -06001138 value->size != 1)
Rob Clark487687e2011-07-17 17:29:02 -05001139 return FALSE;
1140 val = *(uint32_t *)value->data;
1141
Dave Barnish2e998952013-06-11 16:31:10 +01001142 ret = drmModeConnectorSetProperty(drmmode->fd,
1143 drmmode_output->output_id,
Rob Clark74210d52012-01-08 17:59:08 -06001144 p->mode_prop->prop_id, (uint64_t)val);
Rob Clark487687e2011-07-17 17:29:02 -05001145
1146 if (ret)
1147 return FALSE;
1148
1149 return TRUE;
1150
1151 } else if (p->mode_prop->flags & DRM_MODE_PROP_ENUM) {
1152 Atom atom;
1153 const char *name;
1154 int j;
1155
Dave Barnish2e998952013-06-11 16:31:10 +01001156 if (value->type != XA_ATOM ||
1157 value->format != 32 ||
1158 value->size != 1)
Rob Clark487687e2011-07-17 17:29:02 -05001159 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +01001160
Rob Clark487687e2011-07-17 17:29:02 -05001161 memcpy(&atom, value->data, 4);
1162 name = NameForAtom(atom);
Daniel Kurtz23148372013-03-27 20:19:45 +08001163 if (name == NULL)
1164 return FALSE;
Rob Clark487687e2011-07-17 17:29:02 -05001165
Dave Barnish2e998952013-06-11 16:31:10 +01001166 /* search for matching name string, then
1167 * set its value down
1168 */
Rob Clark487687e2011-07-17 17:29:02 -05001169 for (j = 0; j < p->mode_prop->count_enums; j++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001170 if (!strcmp(p->mode_prop->enums[j].name,
1171 name)) {
1172 ret = drmModeConnectorSetProperty(
1173 drmmode->fd,
1174 drmmode_output->output_id,
1175 p->mode_prop->prop_id,
1176 p->mode_prop->enums[j].value);
Rob Clark487687e2011-07-17 17:29:02 -05001177
1178 if (ret)
1179 return FALSE;
1180
1181 return TRUE;
1182 }
1183 }
Rob Clark487687e2011-07-17 17:29:02 -05001184 return FALSE;
1185 }
1186 }
Rob Clark487687e2011-07-17 17:29:02 -05001187 return TRUE;
1188}
1189
1190static Bool
1191drmmode_output_get_property(xf86OutputPtr output, Atom property)
1192{
1193
Dave Barnish2e998952013-06-11 16:31:10 +01001194 struct drmmode_output_priv *drmmode_output = output->driver_private;
1195 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001196 uint32_t value;
1197 int err, i;
1198
1199 if (output->scrn->vtSema) {
1200 drmModeFreeConnector(drmmode_output->mode_output);
1201 drmmode_output->mode_output =
Dave Barnish2e998952013-06-11 16:31:10 +01001202 drmModeGetConnector(drmmode->fd,
1203 drmmode_output->output_id);
Rob Clark487687e2011-07-17 17:29:02 -05001204 }
1205
1206 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001207 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -05001208 if (p->atoms[0] != property)
1209 continue;
1210
1211 value = drmmode_output->mode_output->prop_values[p->index];
1212
1213 if (p->mode_prop->flags & DRM_MODE_PROP_RANGE) {
1214 err = RRChangeOutputProperty(output->randr_output,
Rob Clark74210d52012-01-08 17:59:08 -06001215 property, XA_INTEGER, 32,
1216 PropModeReplace, 1, &value,
1217 FALSE, FALSE);
Rob Clark487687e2011-07-17 17:29:02 -05001218
1219 return !err;
1220 } else if (p->mode_prop->flags & DRM_MODE_PROP_ENUM) {
1221 int j;
1222
Dave Barnish2e998952013-06-11 16:31:10 +01001223 /* search for matching name string, then set
1224 * its value down
1225 */
Rob Clark487687e2011-07-17 17:29:02 -05001226 for (j = 0; j < p->mode_prop->count_enums; j++) {
1227 if (p->mode_prop->enums[j].value == value)
1228 break;
1229 }
1230
Dave Barnish2e998952013-06-11 16:31:10 +01001231 err = RRChangeOutputProperty(output->randr_output,
1232 property,
1233 XA_ATOM, 32, PropModeReplace, 1,
1234 &p->atoms[j+1], FALSE, FALSE);
Rob Clark487687e2011-07-17 17:29:02 -05001235
1236 return !err;
1237 }
1238 }
1239
1240 return FALSE;
1241}
1242
1243static const xf86OutputFuncsRec drmmode_output_funcs = {
Rob Clark74210d52012-01-08 17:59:08 -06001244 .create_resources = drmmode_output_create_resources,
1245 .dpms = drmmode_output_dpms,
1246 .detect = drmmode_output_detect,
1247 .mode_valid = drmmode_output_mode_valid,
1248 .get_modes = drmmode_output_get_modes,
1249 .set_property = drmmode_output_set_property,
1250 .get_property = drmmode_output_get_property,
1251 .destroy = drmmode_output_destroy
Rob Clark487687e2011-07-17 17:29:02 -05001252};
1253
Rob Clark487687e2011-07-17 17:29:02 -05001254const char *output_names[] = { "None",
Rob Clark74210d52012-01-08 17:59:08 -06001255 "VGA",
1256 "DVI-I",
1257 "DVI-D",
1258 "DVI-A",
1259 "Composite",
1260 "SVIDEO",
1261 "LVDS",
1262 "CTV",
1263 "DIN",
1264 "DP",
1265 "HDMI",
1266 "HDMI",
1267 "TV",
1268 "eDP",
Rob Clark487687e2011-07-17 17:29:02 -05001269};
1270#define NUM_OUTPUT_NAMES (sizeof(output_names) / sizeof(output_names[0]))
1271
1272static void
Dave Barnish2e998952013-06-11 16:31:10 +01001273drmmode_output_init(ScrnInfoPtr pScrn, struct drmmode_rec *drmmode, int num)
Rob Clark487687e2011-07-17 17:29:02 -05001274{
Rob Clark74210d52012-01-08 17:59:08 -06001275 xf86OutputPtr output;
1276 drmModeConnectorPtr koutput;
1277 drmModeEncoderPtr kencoder;
Dave Barnish2e998952013-06-11 16:31:10 +01001278 struct drmmode_output_priv *drmmode_output;
Rob Clark74210d52012-01-08 17:59:08 -06001279 char name[32];
Rob Clark487687e2011-07-17 17:29:02 -05001280
Rob Clark74210d52012-01-08 17:59:08 -06001281 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001282
Rob Clark74210d52012-01-08 17:59:08 -06001283 koutput = drmModeGetConnector(drmmode->fd,
1284 drmmode->mode_res->connectors[num]);
1285 if (!koutput)
1286 return;
Rob Clark487687e2011-07-17 17:29:02 -05001287
Rob Clark74210d52012-01-08 17:59:08 -06001288 kencoder = drmModeGetEncoder(drmmode->fd, koutput->encoders[0]);
1289 if (!kencoder) {
1290 drmModeFreeConnector(koutput);
1291 return;
1292 }
Rob Clark487687e2011-07-17 17:29:02 -05001293
Rob Clark74210d52012-01-08 17:59:08 -06001294 if (koutput->connector_type >= NUM_OUTPUT_NAMES)
1295 snprintf(name, 32, "Unknown%d-%d", koutput->connector_type,
1296 koutput->connector_type_id);
1297 else
1298 snprintf(name, 32, "%s-%d",
1299 output_names[koutput->connector_type],
1300 koutput->connector_type_id);
Rob Clark487687e2011-07-17 17:29:02 -05001301
Rob Clark74210d52012-01-08 17:59:08 -06001302 output = xf86OutputCreate(pScrn, &drmmode_output_funcs, name);
1303 if (!output) {
1304 drmModeFreeEncoder(kencoder);
1305 drmModeFreeConnector(koutput);
1306 return;
1307 }
Rob Clark487687e2011-07-17 17:29:02 -05001308
Dave Barnish2e998952013-06-11 16:31:10 +01001309 drmmode_output = calloc(sizeof(struct drmmode_output_priv), 1);
Rob Clark74210d52012-01-08 17:59:08 -06001310 if (!drmmode_output) {
1311 xf86OutputDestroy(output);
1312 drmModeFreeConnector(koutput);
1313 drmModeFreeEncoder(kencoder);
1314 return;
1315 }
Rob Clark487687e2011-07-17 17:29:02 -05001316
Rob Clark74210d52012-01-08 17:59:08 -06001317 drmmode_output->output_id = drmmode->mode_res->connectors[num];
1318 drmmode_output->mode_output = koutput;
1319 drmmode_output->mode_encoder = kencoder;
1320 drmmode_output->drmmode = drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001321
Rob Clark74210d52012-01-08 17:59:08 -06001322 output->mm_width = koutput->mmWidth;
1323 output->mm_height = koutput->mmHeight;
1324 output->driver_private = drmmode_output;
Rob Clark487687e2011-07-17 17:29:02 -05001325
Dave Barnishf0952c12013-05-10 15:52:23 +01001326 if (ARMSOCPTR(pScrn)->crtcNum >= 0) {
1327 /* Only single crtc per screen - see if this output can use it*/
1328 output->possible_crtcs =
Dave Barnish2e998952013-06-11 16:31:10 +01001329 (kencoder->possible_crtcs >>
1330 (ARMSOCPTR(pScrn)->crtcNum)
1331 )&1;
1332 } else
Dave Barnishf0952c12013-05-10 15:52:23 +01001333 output->possible_crtcs = kencoder->possible_crtcs;
Dave Barnishf0952c12013-05-10 15:52:23 +01001334
Rob Clark74210d52012-01-08 17:59:08 -06001335 output->possible_clones = kencoder->possible_clones;
1336 output->interlaceAllowed = TRUE;
Rob Clark487687e2011-07-17 17:29:02 -05001337
Rob Clark74210d52012-01-08 17:59:08 -06001338 TRACE_EXIT();
1339 return;
1340}
Rob Clark487687e2011-07-17 17:29:02 -05001341
Paul Geary8ffd91c2013-04-11 16:03:15 +01001342void set_scanout_bo(ScrnInfoPtr pScrn, struct armsoc_bo *bo)
David Garbett3688b332012-05-11 12:17:34 +01001343{
Dave Barnish2e998952013-06-11 16:31:10 +01001344 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
David Garbett3688b332012-05-11 12:17:34 +01001345
1346 /* It had better have a framebuffer if we're scanning it out */
Paul Geary8ffd91c2013-04-11 16:03:15 +01001347 assert(armsoc_bo_get_fb(bo));
David Garbett3688b332012-05-11 12:17:34 +01001348
Paul Geary8ffd91c2013-04-11 16:03:15 +01001349 pARMSOC->scanout = bo;
David Garbett3688b332012-05-11 12:17:34 +01001350}
1351
Dave Barnishf245da32013-08-30 12:16:58 +01001352static Bool resize_scanout_bo(ScrnInfoPtr pScrn, int width, int height)
Rob Clark487687e2011-07-17 17:29:02 -05001353{
Dave Barnish2e998952013-06-11 16:31:10 +01001354 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001355 ScreenPtr pScreen = pScrn->pScreen;
Sean Paul3e107302012-08-30 12:08:12 -07001356 uint32_t pitch;
Rob Clark487687e2011-07-17 17:29:02 -05001357
1358 TRACE_ENTER();
Dave Barnish33866052013-03-18 10:57:40 +00001359 DEBUG_MSG("Resize: %dx%d", width, height);
Rob Clark487687e2011-07-17 17:29:02 -05001360
1361 pScrn->virtualX = width;
1362 pScrn->virtualY = height;
1363
Dave Barnish2e998952013-06-11 16:31:10 +01001364 if ((width != armsoc_bo_width(pARMSOC->scanout))
Paul Geary8ffd91c2013-04-11 16:03:15 +01001365 || (height != armsoc_bo_height(pARMSOC->scanout))
Dave Barnish2e998952013-06-11 16:31:10 +01001366 || (pScrn->bitsPerPixel != armsoc_bo_bpp(pARMSOC->scanout))) {
Dave Barnish33866052013-03-18 10:57:40 +00001367 struct armsoc_bo *new_scanout;
Rob Clark487687e2011-07-17 17:29:02 -05001368
1369 /* allocate new scanout buffer */
Dave Barnish2e998952013-06-11 16:31:10 +01001370 new_scanout = armsoc_bo_new_with_dim(pARMSOC->dev,
1371 width, height,
1372 pScrn->depth, pScrn->bitsPerPixel,
1373 ARMSOC_BO_SCANOUT);
1374 if (!new_scanout) {
1375 /* Try to use the previous buffer if the new resolution
1376 * is smaller than the one on buffer creation
1377 */
1378 DEBUG_MSG(
1379 "allocate new scanout buffer failed - resizing existing bo");
Dave Barnish33866052013-03-18 10:57:40 +00001380 /* Remove the old fb from the bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001381 if (armsoc_bo_rm_fb(pARMSOC->scanout))
Dave Barnish33866052013-03-18 10:57:40 +00001382 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +01001383
Dave Barnish33866052013-03-18 10:57:40 +00001384 /* Resize the bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001385 if (armsoc_bo_resize(pARMSOC->scanout, width, height)) {
Dave Barnish33866052013-03-18 10:57:40 +00001386 armsoc_bo_clear(pARMSOC->scanout);
Ray Smithb4299f82013-03-13 10:08:36 +00001387 if (armsoc_bo_add_fb(pARMSOC->scanout))
1388 ERROR_MSG(
1389 "Failed to add framebuffer to the existing scanout buffer");
Dave Barnish33866052013-03-18 10:57:40 +00001390 return FALSE;
1391 }
Ray Smithb4299f82013-03-13 10:08:36 +00001392
Dave Barnish33866052013-03-18 10:57:40 +00001393 /* Add new fb to the bo */
Ray Smithb4299f82013-03-13 10:08:36 +00001394 if (armsoc_bo_clear(pARMSOC->scanout))
Dave Barnish33866052013-03-18 10:57:40 +00001395 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +01001396
Ray Smithb4299f82013-03-13 10:08:36 +00001397 if (armsoc_bo_add_fb(pARMSOC->scanout)) {
1398 ERROR_MSG(
1399 "Failed to add framebuffer to the existing scanout buffer");
1400 return FALSE;
1401 }
1402
Dave Barnish33866052013-03-18 10:57:40 +00001403 pitch = armsoc_bo_pitch(pARMSOC->scanout);
Dave Barnish2e998952013-06-11 16:31:10 +01001404 } else {
Dave Barnish33866052013-03-18 10:57:40 +00001405 DEBUG_MSG("allocated new scanout buffer okay");
1406 pitch = armsoc_bo_pitch(new_scanout);
1407 /* clear new BO and add FB */
Ray Smithb4299f82013-03-13 10:08:36 +00001408 if (armsoc_bo_clear(new_scanout)) {
Paul Geary8ffd91c2013-04-11 16:03:15 +01001409 armsoc_bo_unreference(new_scanout);
David Garbettae5a6362012-07-02 10:15:47 +01001410 return FALSE;
1411 }
Ray Smithb4299f82013-03-13 10:08:36 +00001412
1413 if (armsoc_bo_add_fb(new_scanout)) {
1414 ERROR_MSG(
1415 "Failed to add framebuffer to the new scanout buffer");
1416 armsoc_bo_unreference(new_scanout);
1417 return FALSE;
1418 }
1419
Dave Barnish33866052013-03-18 10:57:40 +00001420 /* Handle dma_buf fd that may be attached to old bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001421 if (armsoc_bo_has_dmabuf(pARMSOC->scanout)) {
Dave Barnish33866052013-03-18 10:57:40 +00001422 int res;
1423
1424 armsoc_bo_clear_dmabuf(pARMSOC->scanout);
1425 res = armsoc_bo_set_dmabuf(new_scanout);
Dave Barnish2e998952013-06-11 16:31:10 +01001426 if (res) {
1427 ERROR_MSG(
1428 "Unable to attach dma_buf fd to new scanout buffer - %d (%s)\n",
1429 res, strerror(res));
Dave Barnish33866052013-03-18 10:57:40 +00001430 armsoc_bo_unreference(new_scanout);
1431 return FALSE;
1432 }
1433 }
1434 /* delete old scanout buffer */
1435 armsoc_bo_unreference(pARMSOC->scanout);
1436 /* use new scanout buffer */
1437 set_scanout_bo(pScrn, new_scanout);
David Garbettae5a6362012-07-02 10:15:47 +01001438 }
Daniel Kurtz34e72b02012-10-19 14:02:05 -07001439 pScrn->displayWidth = pitch / ((pScrn->bitsPerPixel + 7) / 8);
Dave Barnish2e998952013-06-11 16:31:10 +01001440 } else
Paul Geary8ffd91c2013-04-11 16:03:15 +01001441 pitch = armsoc_bo_pitch(pARMSOC->scanout);
Rob Clark487687e2011-07-17 17:29:02 -05001442
1443 if (pScreen && pScreen->ModifyPixmapHeader) {
1444 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
Dave Barnish33866052013-03-18 10:57:40 +00001445
Rob Clark487687e2011-07-17 17:29:02 -05001446 pScreen->ModifyPixmapHeader(rootPixmap,
1447 pScrn->virtualX, pScrn->virtualY,
1448 pScrn->depth, pScrn->bitsPerPixel, pitch,
Paul Geary8ffd91c2013-04-11 16:03:15 +01001449 armsoc_bo_map(pARMSOC->scanout));
Ray Smithedccfd82013-05-31 15:57:12 +01001450
Dave Barnishe762b122013-08-12 17:13:46 +01001451 /* Bump the serial number to ensure that all existing DRI2
1452 * buffers are invalidated.
Ray Smithedccfd82013-05-31 15:57:12 +01001453 *
Dave Barnishe762b122013-08-12 17:13:46 +01001454 * This is particularly required for when the resolution is
1455 * changed and then reverted to the original size without a
1456 * DRI2 client/s getting a new buffer. Without this, the
1457 * drawable is the same size and serial number so the old
1458 * DRI2Buffer will be returned, even though the backing buffer
1459 * has been deleted.
Ray Smithedccfd82013-05-31 15:57:12 +01001460 */
1461 rootPixmap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
Rob Clark487687e2011-07-17 17:29:02 -05001462 }
Dave Barnishf245da32013-08-30 12:16:58 +01001463 TRACE_EXIT();
1464 return TRUE;
1465}
1466
1467static Bool
1468drmmode_xf86crtc_resize(ScrnInfoPtr pScrn, int width, int height)
1469{
1470 int i;
1471 xf86CrtcConfigPtr xf86_config;
1472
1473 TRACE_ENTER();
Dave Barnishf245da32013-08-30 12:16:58 +01001474 if (!resize_scanout_bo(pScrn, width, height))
1475 return FALSE;
Rob Clark487687e2011-07-17 17:29:02 -05001476
Brian Starkeycd684422012-09-20 09:28:04 +01001477 /* Framebuffer needs to be reset on all CRTCs, not just
1478 * those that have repositioned */
1479 xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1480 for (i = 0; i < xf86_config->num_crtc; i++) {
1481 xf86CrtcPtr crtc = xf86_config->crtc[i];
1482
1483 if (!crtc->enabled)
1484 continue;
1485
1486 drmmode_set_mode_major(crtc, &crtc->mode,
1487 crtc->rotation, crtc->x, crtc->y);
1488 }
1489
Rob Clark487687e2011-07-17 17:29:02 -05001490 TRACE_EXIT();
1491 return TRUE;
Rob Clark74210d52012-01-08 17:59:08 -06001492}
Rob Clark487687e2011-07-17 17:29:02 -05001493
1494static const xf86CrtcConfigFuncsRec drmmode_xf86crtc_config_funcs = {
Daniel Kurtze81fdab2012-12-25 16:43:04 +08001495 .resize = drmmode_xf86crtc_resize
Rob Clark487687e2011-07-17 17:29:02 -05001496};
1497
1498
1499Bool drmmode_pre_init(ScrnInfoPtr pScrn, int fd, int cpp)
1500{
Dave Barnish2e998952013-06-11 16:31:10 +01001501 struct drmmode_rec *drmmode;
Rob Clark74210d52012-01-08 17:59:08 -06001502 int i;
Rob Clark487687e2011-07-17 17:29:02 -05001503
Rob Clark74210d52012-01-08 17:59:08 -06001504 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001505
Rob Clark74210d52012-01-08 17:59:08 -06001506 drmmode = calloc(1, sizeof *drmmode);
Dave Barnish2e998952013-06-11 16:31:10 +01001507 if (!drmmode)
Paul Geary6fe52f32013-04-03 11:12:24 +01001508 return FALSE;
Paul Geary6fe52f32013-04-03 11:12:24 +01001509
Rob Clark74210d52012-01-08 17:59:08 -06001510 drmmode->fd = fd;
Rob Clark487687e2011-07-17 17:29:02 -05001511
Rob Clark74210d52012-01-08 17:59:08 -06001512 xf86CrtcConfigInit(pScrn, &drmmode_xf86crtc_config_funcs);
Rob Clark487687e2011-07-17 17:29:02 -05001513
1514
Rob Clark74210d52012-01-08 17:59:08 -06001515 drmmode->cpp = cpp;
1516 drmmode->mode_res = drmModeGetResources(drmmode->fd);
1517 if (!drmmode->mode_res) {
Paul Geary6fe52f32013-04-03 11:12:24 +01001518 free(drmmode);
Rob Clark74210d52012-01-08 17:59:08 -06001519 return FALSE;
1520 } else {
1521 DEBUG_MSG("Got KMS resources");
1522 DEBUG_MSG(" %d connectors, %d encoders",
1523 drmmode->mode_res->count_connectors,
1524 drmmode->mode_res->count_encoders);
1525 DEBUG_MSG(" %d crtcs, %d fbs",
Dave Barnish2e998952013-06-11 16:31:10 +01001526 drmmode->mode_res->count_crtcs,
1527 drmmode->mode_res->count_fbs);
Rob Clark74210d52012-01-08 17:59:08 -06001528 DEBUG_MSG(" %dx%d minimum resolution",
Dave Barnish2e998952013-06-11 16:31:10 +01001529 drmmode->mode_res->min_width,
1530 drmmode->mode_res->min_height);
Rob Clark74210d52012-01-08 17:59:08 -06001531 DEBUG_MSG(" %dx%d maximum resolution",
Dave Barnish2e998952013-06-11 16:31:10 +01001532 drmmode->mode_res->max_width,
1533 drmmode->mode_res->max_height);
Rob Clark74210d52012-01-08 17:59:08 -06001534 }
1535 xf86CrtcSetSizeRange(pScrn, 320, 200, drmmode->mode_res->max_width,
1536 drmmode->mode_res->max_height);
Dave Barnishf0952c12013-05-10 15:52:23 +01001537
Dave Barnish2e998952013-06-11 16:31:10 +01001538 if (ARMSOCPTR(pScrn)->crtcNum == -1) {
Dave Barnishf0952c12013-05-10 15:52:23 +01001539 INFO_MSG("Adding all CRTCs");
1540 for (i = 0; i < drmmode->mode_res->count_crtcs; i++)
1541 drmmode_crtc_init(pScrn, drmmode, i);
Dave Barnish2e998952013-06-11 16:31:10 +01001542 } else if (ARMSOCPTR(pScrn)->crtcNum < drmmode->mode_res->count_crtcs) {
Dave Barnishf0952c12013-05-10 15:52:23 +01001543 drmmode_crtc_init(pScrn, drmmode, ARMSOCPTR(pScrn)->crtcNum);
1544 } else {
Dave Barnish2e998952013-06-11 16:31:10 +01001545 ERROR_MSG(
1546 "Specified more Screens in xorg.conf than there are DRM CRTCs");
Dave Barnishf0952c12013-05-10 15:52:23 +01001547 return FALSE;
1548 }
Rob Clark487687e2011-07-17 17:29:02 -05001549
Dave Barnish2e998952013-06-11 16:31:10 +01001550 if (ARMSOCPTR(pScrn)->crtcNum != -1) {
1551 if (ARMSOCPTR(pScrn)->crtcNum <
1552 drmmode->mode_res->count_connectors)
1553 drmmode_output_init(pScrn,
1554 drmmode, ARMSOCPTR(pScrn)->crtcNum);
Ray Smithc9c2faf2013-06-07 15:37:19 +01001555 else
1556 return FALSE;
1557 } else {
1558 for (i = 0; i < drmmode->mode_res->count_connectors; i++)
1559 drmmode_output_init(pScrn, drmmode, i);
1560 }
Rob Clark487687e2011-07-17 17:29:02 -05001561
Rob Clark74210d52012-01-08 17:59:08 -06001562 xf86InitialConfiguration(pScrn, TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001563
Rob Clark74210d52012-01-08 17:59:08 -06001564 TRACE_EXIT();
Rob Clark487687e2011-07-17 17:29:02 -05001565
Rob Clark74210d52012-01-08 17:59:08 -06001566 return TRUE;
1567}
Rob Clark487687e2011-07-17 17:29:02 -05001568
1569void
Cooper Yuana83caa62012-06-28 17:19:06 +02001570drmmode_adjust_frame(ScrnInfoPtr pScrn, int x, int y)
Rob Clark487687e2011-07-17 17:29:02 -05001571{
1572 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1573 xf86OutputPtr output = config->output[config->compat_output];
1574 xf86CrtcPtr crtc = output->crtc;
1575
1576 if (!crtc || !crtc->enabled)
1577 return;
1578
1579 drmmode_set_mode_major(crtc, &crtc->mode, crtc->rotation, x, y);
1580}
1581
Rob Clark4b8f30a2011-08-28 12:51:26 -05001582/*
1583 * Page Flipping
1584 */
1585
1586static void
1587page_flip_handler(int fd, unsigned int sequence, unsigned int tv_sec,
Rob Clark74210d52012-01-08 17:59:08 -06001588 unsigned int tv_usec, void *user_data)
Rob Clark4b8f30a2011-08-28 12:51:26 -05001589{
Paul Geary8ffd91c2013-04-11 16:03:15 +01001590 ARMSOCDRI2SwapComplete(user_data);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001591}
1592
1593static drmEventContext event_context = {
1594 .version = DRM_EVENT_CONTEXT_VERSION,
1595 .page_flip_handler = page_flip_handler,
1596};
1597
John Sheu022833e2012-08-15 11:40:11 -07001598int
Rob Clark4b8f30a2011-08-28 12:51:26 -05001599drmmode_page_flip(DrawablePtr draw, uint32_t fb_id, void *priv)
1600{
Daniel Kurtz0361e002013-08-14 21:07:01 +08001601 ScreenPtr pScreen = draw->pScreen;
1602 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Dave Barnish2e998952013-06-11 16:31:10 +01001603 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Ray Smith3c33c3d2013-03-26 16:06:37 +00001604 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
Dave Barnish2e998952013-06-11 16:31:10 +01001605 struct drmmode_crtc_private_rec *crtc = config->crtc[0]->driver_private;
1606 struct drmmode_rec *mode = crtc->drmmode;
John Sheu022833e2012-08-15 11:40:11 -07001607 int ret, i, failed = 0, num_flipped = 0;
Raymond Smith16a910e2012-05-09 13:04:51 +01001608 unsigned int flags = 0;
1609
Dave Barnishde45ed42013-06-05 13:47:56 +01001610 if (pARMSOC->drmmode_interface->use_page_flip_events)
Ray Smith3c33c3d2013-03-26 16:06:37 +00001611 flags |= DRM_MODE_PAGE_FLIP_EVENT;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001612
1613 /* if we can flip, we must be fullscreen.. so flip all CRTC's.. */
1614 for (i = 0; i < config->num_crtc; i++) {
1615 crtc = config->crtc[i]->driver_private;
1616
Ray Smithe20b3812013-04-08 12:53:53 +01001617 if (!config->crtc[i]->enabled)
1618 continue;
1619
Rob Clark4b8f30a2011-08-28 12:51:26 -05001620 ret = drmModePageFlip(mode->fd, crtc->mode_crtc->crtc_id,
Raymond Smith16a910e2012-05-09 13:04:51 +01001621 fb_id, flags, priv);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001622 if (ret) {
Ray Smith3c33c3d2013-03-26 16:06:37 +00001623 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
Dave Barnish2e998952013-06-11 16:31:10 +01001624 "flip queue failed: %s\n",
1625 strerror(errno));
John Sheu022833e2012-08-15 11:40:11 -07001626 failed = 1;
Dave Barnish2e998952013-06-11 16:31:10 +01001627 } else
John Sheu022833e2012-08-15 11:40:11 -07001628 num_flipped += 1;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001629 }
1630
John Sheu022833e2012-08-15 11:40:11 -07001631 if (failed)
1632 return -(num_flipped + 1);
1633 else
1634 return num_flipped;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001635}
Rob Clark487687e2011-07-17 17:29:02 -05001636
1637/*
1638 * Hot Plug Event handling:
David Garbettdbecfdd2013-08-28 10:45:03 +01001639 * TODO: MIDEGL-1441: Do we need to keep this handler, which
1640 * Rob originally wrote?
Rob Clark487687e2011-07-17 17:29:02 -05001641 */
Rob Clark487687e2011-07-17 17:29:02 -05001642static void
1643drmmode_handle_uevents(int fd, void *closure)
1644{
Rob Clark74210d52012-01-08 17:59:08 -06001645 ScrnInfoPtr pScrn = closure;
Dave Barnish2e998952013-06-11 16:31:10 +01001646 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
1647 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark74210d52012-01-08 17:59:08 -06001648 struct udev_device *dev;
1649 const char *hotplug;
1650 struct stat s;
1651 dev_t udev_devnum;
Rob Clark487687e2011-07-17 17:29:02 -05001652
Rob Clark74210d52012-01-08 17:59:08 -06001653 dev = udev_monitor_receive_device(drmmode->uevent_monitor);
1654 if (!dev)
1655 return;
Rob Clark487687e2011-07-17 17:29:02 -05001656
Rob Clark74210d52012-01-08 17:59:08 -06001657 /*
1658 * Check to make sure this event is directed at our
1659 * device (by comparing dev_t values), then make
1660 * sure it's a hotplug event (HOTPLUG=1)
1661 */
1662 udev_devnum = udev_device_get_devnum(dev);
David Garbettdbecfdd2013-08-28 10:45:03 +01001663 if (fstat(pARMSOC->drmFD, &s)) {
1664 ERROR_MSG("fstat failed: %s", strerror(errno));
1665 udev_device_unref(dev);
1666 return;
1667 }
Rob Clark487687e2011-07-17 17:29:02 -05001668
Rob Clark74210d52012-01-08 17:59:08 -06001669 hotplug = udev_device_get_property_value(dev, "HOTPLUG");
Rob Clark487687e2011-07-17 17:29:02 -05001670
Rob Clark74210d52012-01-08 17:59:08 -06001671 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "hotplug=%s, match=%d\n", hotplug,
Dave Barnish2e998952013-06-11 16:31:10 +01001672 !memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)));
Rob Clark487687e2011-07-17 17:29:02 -05001673
Dave Barnish2e998952013-06-11 16:31:10 +01001674 if (memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)) == 0 &&
Rob Clark74210d52012-01-08 17:59:08 -06001675 hotplug && atoi(hotplug) == 1) {
Daniel Kurtz0361e002013-08-14 21:07:01 +08001676 RRGetInfo(xf86ScrnToScreen(pScrn), TRUE);
Rob Clark74210d52012-01-08 17:59:08 -06001677 }
1678 udev_device_unref(dev);
1679}
Rob Clark487687e2011-07-17 17:29:02 -05001680
Rob Clark4b8f30a2011-08-28 12:51:26 -05001681static void
Rob Clark487687e2011-07-17 17:29:02 -05001682drmmode_uevent_init(ScrnInfoPtr pScrn)
1683{
Dave Barnish2e998952013-06-11 16:31:10 +01001684 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark74210d52012-01-08 17:59:08 -06001685 struct udev *u;
1686 struct udev_monitor *mon;
Rob Clark487687e2011-07-17 17:29:02 -05001687
Rob Clark74210d52012-01-08 17:59:08 -06001688 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001689
Rob Clark74210d52012-01-08 17:59:08 -06001690 u = udev_new();
1691 if (!u)
1692 return;
1693 mon = udev_monitor_new_from_netlink(u, "udev");
1694 if (!mon) {
1695 udev_unref(u);
1696 return;
1697 }
Rob Clark487687e2011-07-17 17:29:02 -05001698
Rob Clark74210d52012-01-08 17:59:08 -06001699 if (udev_monitor_filter_add_match_subsystem_devtype(mon,
1700 "drm",
1701 "drm_minor") < 0 ||
1702 udev_monitor_enable_receiving(mon) < 0) {
1703 udev_monitor_unref(mon);
1704 udev_unref(u);
1705 return;
1706 }
Rob Clark487687e2011-07-17 17:29:02 -05001707
Rob Clark74210d52012-01-08 17:59:08 -06001708 drmmode->uevent_handler =
1709 xf86AddGeneralHandler(udev_monitor_get_fd(mon),
1710 drmmode_handle_uevents, pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001711
Rob Clark74210d52012-01-08 17:59:08 -06001712 drmmode->uevent_monitor = mon;
Rob Clark487687e2011-07-17 17:29:02 -05001713
Rob Clark74210d52012-01-08 17:59:08 -06001714 TRACE_EXIT();
1715}
Rob Clark487687e2011-07-17 17:29:02 -05001716
Rob Clark4b8f30a2011-08-28 12:51:26 -05001717static void
Rob Clark487687e2011-07-17 17:29:02 -05001718drmmode_uevent_fini(ScrnInfoPtr pScrn)
1719{
Dave Barnish2e998952013-06-11 16:31:10 +01001720 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001721
Rob Clark74210d52012-01-08 17:59:08 -06001722 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001723
Rob Clark74210d52012-01-08 17:59:08 -06001724 if (drmmode->uevent_handler) {
1725 struct udev *u = udev_monitor_get_udev(drmmode->uevent_monitor);
1726 xf86RemoveGeneralHandler(drmmode->uevent_handler);
Rob Clark487687e2011-07-17 17:29:02 -05001727
Rob Clark74210d52012-01-08 17:59:08 -06001728 udev_monitor_unref(drmmode->uevent_monitor);
1729 udev_unref(u);
1730 }
Rob Clark487687e2011-07-17 17:29:02 -05001731
Rob Clark74210d52012-01-08 17:59:08 -06001732 TRACE_EXIT();
1733}
Rob Clark4b8f30a2011-08-28 12:51:26 -05001734
1735static void
1736drmmode_wakeup_handler(pointer data, int err, pointer p)
1737{
Ray Smith04af9992013-06-26 13:44:54 +01001738 int fd = (int)data;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001739 fd_set *read_mask = p;
1740
Ray Smith04af9992013-06-26 13:44:54 +01001741 if (err < 0)
Rob Clark4b8f30a2011-08-28 12:51:26 -05001742 return;
1743
Ray Smith04af9992013-06-26 13:44:54 +01001744 if (FD_ISSET(fd, read_mask))
1745 drmHandleEvent(fd, &event_context);
1746}
Paul Geary6fe52f32013-04-03 11:12:24 +01001747
Ray Smith04af9992013-06-26 13:44:54 +01001748void drmmode_init_wakeup_handler(int fd)
1749{
1750 AddGeneralSocket(fd);
1751 RegisterBlockAndWakeupHandlers((BlockHandlerProcPtr)NoopDDA,
1752 drmmode_wakeup_handler, (pointer)fd);
1753}
1754
1755void drmmode_fini_wakeup_handler(int fd)
1756{
1757 RemoveBlockAndWakeupHandlers((BlockHandlerProcPtr)NoopDDA,
1758 drmmode_wakeup_handler, (pointer)fd);
1759 RemoveGeneralSocket(fd);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001760}
1761
1762void
Rob Clark67b875f2012-04-20 19:13:57 -05001763drmmode_wait_for_event(ScrnInfoPtr pScrn)
1764{
Dave Barnish2e998952013-06-11 16:31:10 +01001765 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark67b875f2012-04-20 19:13:57 -05001766 drmHandleEvent(drmmode->fd, &event_context);
1767}
1768
1769void
Rob Clark4b8f30a2011-08-28 12:51:26 -05001770drmmode_screen_init(ScrnInfoPtr pScrn)
1771{
Rob Clark4b8f30a2011-08-28 12:51:26 -05001772 drmmode_uevent_init(pScrn);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001773}
1774
1775void
1776drmmode_screen_fini(ScrnInfoPtr pScrn)
1777{
1778 drmmode_uevent_fini(pScrn);
1779}