blob: ffd9dd1bf6660efab5507756e24ce63721ff2343 [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
520static void
521drmmode_load_cursor_argb(xf86CrtcPtr crtc, CARD32 *image)
522{
Dave Barnish2e998952013-06-11 16:31:10 +0100523 uint32_t *d;
524 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
525 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
526 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Rob Clark979add52012-02-21 18:35:24 -0600527 int visible;
Dave Barnishde45ed42013-06-05 13:47:56 +0100528 ScrnInfoPtr pScrn = crtc->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100529 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark687c6082012-01-08 19:33:18 -0600530
531 if (!cursor)
532 return;
533
Stéphane Marchesinb8b35202012-10-02 20:27:40 -0700534 visible = drmmode_crtc->cursor_visible;
Rob Clark979add52012-02-21 18:35:24 -0600535
536 if (visible)
Rob Clark687c6082012-01-08 19:33:18 -0600537 drmmode_hide_cursor(crtc);
538
Paul Geary8ffd91c2013-04-11 16:03:15 +0100539 d = armsoc_bo_map(cursor->bo);
Dave Barnish2e998952013-06-11 16:31:10 +0100540 if (!d) {
Paul Geary6fe52f32013-04-03 11:12:24 +0100541 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
542 "load_cursor_argb map failure\n");
543 if (visible)
Javier Martin4c572792013-09-11 15:08:19 +0100544 drmmode_show_cursor_image(crtc, TRUE);
Paul Geary6fe52f32013-04-03 11:12:24 +0100545 return;
546 }
John Rees292ae502013-03-21 16:24:35 +0000547
Dave Barnishde45ed42013-06-05 13:47:56 +0100548 /* set_cursor_image is a mandatory function */
Dave Barnish2e998952013-06-11 16:31:10 +0100549 assert(pARMSOC->drmmode_interface->set_cursor_image);
Dave Barnishde45ed42013-06-05 13:47:56 +0100550 pARMSOC->drmmode_interface->set_cursor_image(crtc, d, image);
Rob Clark687c6082012-01-08 19:33:18 -0600551
Rob Clark979add52012-02-21 18:35:24 -0600552 if (visible)
Javier Martin4c572792013-09-11 15:08:19 +0100553 drmmode_show_cursor_image(crtc, TRUE);
Rob Clark687c6082012-01-08 19:33:18 -0600554}
555
Javier Martin4c572792013-09-11 15:08:19 +0100556static Bool
557drmmode_cursor_init_plane(ScreenPtr pScreen)
Rob Clark687c6082012-01-08 19:33:18 -0600558{
Daniel Kurtz0361e002013-08-14 21:07:01 +0800559 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Dave Barnish2e998952013-06-11 16:31:10 +0100560 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
561 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
562 struct drmmode_cursor_rec *cursor;
Rob Clark687c6082012-01-08 19:33:18 -0600563 drmModePlaneRes *plane_resources;
564 drmModePlane *ovr;
Dave Barnishde45ed42013-06-05 13:47:56 +0100565 int w, h, pad;
Rob Clark687c6082012-01-08 19:33:18 -0600566 uint32_t handles[4], pitches[4], offsets[4]; /* we only use [0] */
567
568 if (drmmode->cursor) {
569 INFO_MSG("cursor already initialized");
570 return TRUE;
571 }
572
Dave Barnish2e998952013-06-11 16:31:10 +0100573 if (!xf86LoaderCheckSymbol("drmModeGetPlaneResources")) {
574 ERROR_MSG(
575 "HW cursor not supported (needs libdrm 2.4.30 or higher)");
Ray Smith003cf5e2013-03-19 10:44:07 +0000576 return FALSE;
577 }
Rob Clark687c6082012-01-08 19:33:18 -0600578
579 /* find an unused plane which can be used as a mouse cursor. Note
580 * that we cheat a bit, in order to not burn one overlay per crtc,
581 * and only show the mouse cursor on one crtc at a time
582 */
583 plane_resources = drmModeGetPlaneResources(drmmode->fd);
584 if (!plane_resources) {
Dave Barnish2e998952013-06-11 16:31:10 +0100585 ERROR_MSG("HW cursor: drmModeGetPlaneResources failed: %s",
586 strerror(errno));
Rob Clark687c6082012-01-08 19:33:18 -0600587 return FALSE;
588 }
589
590 if (plane_resources->count_planes < 1) {
591 ERROR_MSG("not enough planes for HW cursor");
Ray Smith003cf5e2013-03-19 10:44:07 +0000592 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600593 return FALSE;
594 }
595
596 ovr = drmModeGetPlane(drmmode->fd, plane_resources->planes[0]);
597 if (!ovr) {
Dave Barnish2e998952013-06-11 16:31:10 +0100598 ERROR_MSG("HW cursor: drmModeGetPlane failed: %s",
599 strerror(errno));
Ray Smith003cf5e2013-03-19 10:44:07 +0000600 drmModeFreePlaneResources(plane_resources);
601 return FALSE;
602 }
603
Dave Barnishde45ed42013-06-05 13:47:56 +0100604 if (pARMSOC->drmmode_interface->init_plane_for_cursor &&
Dave Barnish2e998952013-06-11 16:31:10 +0100605 pARMSOC->drmmode_interface->init_plane_for_cursor(
606 drmmode->fd, ovr->plane_id)) {
Ray Smithb93cd892013-04-03 10:06:18 +0100607 ERROR_MSG("Failed driver-specific cursor initialization");
608 drmModeFreePlaneResources(plane_resources);
609 return FALSE;
610 }
611
Dave Barnish2e998952013-06-11 16:31:10 +0100612 cursor = calloc(1, sizeof(struct drmmode_cursor_rec));
Ray Smith003cf5e2013-03-19 10:44:07 +0000613 if (!cursor) {
Dave Barnishfd50b132013-05-16 15:00:02 +0100614 ERROR_MSG("HW cursor: calloc failed");
Ray Smith003cf5e2013-03-19 10:44:07 +0000615 drmModeFreePlane(ovr);
616 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600617 return FALSE;
618 }
619
620 cursor->ovr = ovr;
Dave Barnishde45ed42013-06-05 13:47:56 +0100621
622 w = pARMSOC->drmmode_interface->cursor_width;
623 h = pARMSOC->drmmode_interface->cursor_height;
624 pad = pARMSOC->drmmode_interface->cursor_padding;
625
626 /* allow for cursor padding in the bo */
Dave Barnish2e998952013-06-11 16:31:10 +0100627 cursor->bo = armsoc_bo_new_with_dim(pARMSOC->dev,
628 w + 2 * pad, h,
629 0, 32, ARMSOC_BO_SCANOUT);
Rob Clark687c6082012-01-08 19:33:18 -0600630
Ray Smith003cf5e2013-03-19 10:44:07 +0000631 if (!cursor->bo) {
Dave Barnishfd50b132013-05-16 15:00:02 +0100632 ERROR_MSG("HW cursor: buffer allocation failed");
Ray Smith003cf5e2013-03-19 10:44:07 +0000633 free(cursor);
634 drmModeFreePlane(ovr);
635 drmModeFreePlaneResources(plane_resources);
636 return FALSE;
637 }
638
Paul Geary8ffd91c2013-04-11 16:03:15 +0100639 handles[0] = armsoc_bo_handle(cursor->bo);
640 pitches[0] = armsoc_bo_pitch(cursor->bo);
Rob Clark687c6082012-01-08 19:33:18 -0600641 offsets[0] = 0;
642
Dave Barnishde45ed42013-06-05 13:47:56 +0100643 /* allow for cursor padding in the fb */
644 if (drmModeAddFB2(drmmode->fd, w + 2 * pad, h, DRM_FORMAT_ARGB8888,
Rob Clark687c6082012-01-08 19:33:18 -0600645 handles, pitches, offsets, &cursor->fb_id, 0)) {
Dave Barnish2e998952013-06-11 16:31:10 +0100646 ERROR_MSG("HW cursor: drmModeAddFB2 failed: %s",
647 strerror(errno));
Paul Geary8ffd91c2013-04-11 16:03:15 +0100648 armsoc_bo_unreference(cursor->bo);
Ray Smith003cf5e2013-03-19 10:44:07 +0000649 free(cursor);
650 drmModeFreePlane(ovr);
651 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600652 return FALSE;
653 }
654
Ray Smith003cf5e2013-03-19 10:44:07 +0000655 if (!xf86_cursors_init(pScreen, w, h, HARDWARE_CURSOR_ARGB)) {
656 ERROR_MSG("xf86_cursors_init() failed");
Dave Barnish2e998952013-06-11 16:31:10 +0100657 if (drmModeRmFB(drmmode->fd, cursor->fb_id))
Ray Smith003cf5e2013-03-19 10:44:07 +0000658 ERROR_MSG("drmModeRmFB() failed");
Dave Barnish2e998952013-06-11 16:31:10 +0100659
Paul Geary8ffd91c2013-04-11 16:03:15 +0100660 armsoc_bo_unreference(cursor->bo);
Ray Smith003cf5e2013-03-19 10:44:07 +0000661 free(cursor);
662 drmModeFreePlane(ovr);
663 drmModeFreePlaneResources(plane_resources);
664 return FALSE;
Rob Clark687c6082012-01-08 19:33:18 -0600665 }
666
Ray Smith003cf5e2013-03-19 10:44:07 +0000667 INFO_MSG("HW cursor initialized");
668 drmmode->cursor = cursor;
Dave Barnishfd50b132013-05-16 15:00:02 +0100669 drmModeFreePlaneResources(plane_resources);
Ray Smith003cf5e2013-03-19 10:44:07 +0000670 return TRUE;
Rob Clark74210d52012-01-08 17:59:08 -0600671}
Rob Clark487687e2011-07-17 17:29:02 -0500672
Javier Martin4c572792013-09-11 15:08:19 +0100673static Bool
674drmmode_cursor_init_standard(ScreenPtr pScreen)
675{
676 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
677 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
678 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
679 struct drmmode_cursor_rec *cursor;
680 int w, h, pad;
681
682 if (drmmode->cursor) {
683 INFO_MSG("cursor already initialized");
684 return TRUE;
685 }
686
687 if (!xf86LoaderCheckSymbol("drmModeSetCursor") ||
688 !xf86LoaderCheckSymbol("drmModeMoveCursor")) {
Dave Barnish8e9ac7b2013-09-12 14:17:33 +0100689 ERROR_MSG(
690 "Standard HW cursor not supported (needs libdrm 2.4.3 or higher)");
Javier Martin4c572792013-09-11 15:08:19 +0100691 return FALSE;
692 }
693
694 cursor = calloc(1, sizeof(struct drmmode_cursor_rec));
695 if (!cursor) {
696 ERROR_MSG("HW cursor (standard): calloc failed");
697 return FALSE;
698 }
699
700 w = pARMSOC->drmmode_interface->cursor_width;
701 h = pARMSOC->drmmode_interface->cursor_height;
702 pad = pARMSOC->drmmode_interface->cursor_padding;
703
704 /* allow for cursor padding in the bo */
705 cursor->bo = armsoc_bo_new_with_dim(pARMSOC->dev,
706 w + 2 * pad, h,
707 0, 32, ARMSOC_BO_SCANOUT);
708
709 if (!cursor->bo) {
710 ERROR_MSG("HW cursor (standard): buffer allocation failed");
711 free(cursor);
712 return FALSE;
713 }
714
715 cursor->handle = armsoc_bo_handle(cursor->bo);
716
717 if (!xf86_cursors_init(pScreen, w, h, HARDWARE_CURSOR_ARGB)) {
718 ERROR_MSG("xf86_cursors_init() failed");
719 if (drmModeRmFB(drmmode->fd, cursor->fb_id))
720 ERROR_MSG("drmModeRmFB() failed");
721
722 armsoc_bo_unreference(cursor->bo);
723 free(cursor);
724 return FALSE;
725 }
726
727 INFO_MSG("HW cursor initialized");
728 drmmode->cursor = cursor;
729 return TRUE;
730}
731
732Bool drmmode_cursor_init(ScreenPtr pScreen)
733{
734 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
735 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
736
737 INFO_MSG("HW cursor init()");
738
739 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE)
740 return drmmode_cursor_init_plane(pScreen);
741 else /* HWCURSOR_API_STANDARD */
742 return drmmode_cursor_init_standard(pScreen);
743}
744
Dave Barnishfd50b132013-05-16 15:00:02 +0100745void drmmode_cursor_fini(ScreenPtr pScreen)
746{
Daniel Kurtz0361e002013-08-14 21:07:01 +0800747 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Dave Barnish2e998952013-06-11 16:31:10 +0100748 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
749 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Javier Martin4c572792013-09-11 15:08:19 +0100750 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Dave Barnishfd50b132013-05-16 15:00:02 +0100751
Dave Barnish2e998952013-06-11 16:31:10 +0100752 if (!cursor)
Dave Barnishfd50b132013-05-16 15:00:02 +0100753 return;
Dave Barnish2e998952013-06-11 16:31:10 +0100754
Dave Barnishfd50b132013-05-16 15:00:02 +0100755 drmmode->cursor = NULL;
756 xf86_cursors_fini(pScreen);
Javier Martin4c572792013-09-11 15:08:19 +0100757 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE)
758 drmModeRmFB(drmmode->fd, cursor->fb_id);
Dave Barnishfd50b132013-05-16 15:00:02 +0100759 armsoc_bo_unreference(cursor->bo);
Javier Martin4c572792013-09-11 15:08:19 +0100760 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE)
761 drmModeFreePlane(cursor->ovr);
Dave Barnishfd50b132013-05-16 15:00:02 +0100762 free(cursor);
763}
764
765
Dave Barnish2e998952013-06-11 16:31:10 +0100766#if 1 == ARMSOC_SUPPORT_GAMMA
Rob Clark487687e2011-07-17 17:29:02 -0500767static void
768drmmode_gamma_set(xf86CrtcPtr crtc, CARD16 *red, CARD16 *green, CARD16 *blue,
Rob Clark74210d52012-01-08 17:59:08 -0600769 int size)
Rob Clark487687e2011-07-17 17:29:02 -0500770{
Dave Barnish2e998952013-06-11 16:31:10 +0100771 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
772 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500773 int ret;
774
775 ret = drmModeCrtcSetGamma(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id,
Rob Clark74210d52012-01-08 17:59:08 -0600776 size, red, green, blue);
Rob Clark487687e2011-07-17 17:29:02 -0500777 if (ret != 0) {
778 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
Rob Clark74210d52012-01-08 17:59:08 -0600779 "failed to set gamma: %s\n", strerror(-ret));
Rob Clark487687e2011-07-17 17:29:02 -0500780 }
781}
Mandeep Singh Bainesc874fcb2012-09-13 15:30:00 +0200782#endif
Rob Clark487687e2011-07-17 17:29:02 -0500783
784static const xf86CrtcFuncsRec drmmode_crtc_funcs = {
Rob Clark74210d52012-01-08 17:59:08 -0600785 .dpms = drmmode_crtc_dpms,
786 .set_mode_major = drmmode_set_mode_major,
787 .set_cursor_position = drmmode_set_cursor_position,
788 .show_cursor = drmmode_show_cursor,
789 .hide_cursor = drmmode_hide_cursor,
790 .load_cursor_argb = drmmode_load_cursor_argb,
Dave Barnish2e998952013-06-11 16:31:10 +0100791#if 1 == ARMSOC_SUPPORT_GAMMA
Rob Clark74210d52012-01-08 17:59:08 -0600792 .gamma_set = drmmode_gamma_set,
Mandeep Singh Bainesc874fcb2012-09-13 15:30:00 +0200793#endif
Rob Clark487687e2011-07-17 17:29:02 -0500794};
795
796
797static void
Dave Barnish2e998952013-06-11 16:31:10 +0100798drmmode_crtc_init(ScrnInfoPtr pScrn, struct drmmode_rec *drmmode, int num)
Rob Clark487687e2011-07-17 17:29:02 -0500799{
Rob Clark74210d52012-01-08 17:59:08 -0600800 xf86CrtcPtr crtc;
Dave Barnish2e998952013-06-11 16:31:10 +0100801 struct drmmode_crtc_private_rec *drmmode_crtc;
Rob Clark487687e2011-07-17 17:29:02 -0500802
Rob Clark74210d52012-01-08 17:59:08 -0600803 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -0500804
Rob Clark74210d52012-01-08 17:59:08 -0600805 crtc = xf86CrtcCreate(pScrn, &drmmode_crtc_funcs);
806 if (crtc == NULL)
807 return;
808
Dave Barnish2e998952013-06-11 16:31:10 +0100809 drmmode_crtc = xnfcalloc(sizeof(struct drmmode_crtc_private_rec), 1);
Rob Clark74210d52012-01-08 17:59:08 -0600810 drmmode_crtc->mode_crtc = drmModeGetCrtc(drmmode->fd,
811 drmmode->mode_res->crtcs[num]);
812 drmmode_crtc->drmmode = drmmode;
Dave Barnishf245da32013-08-30 12:16:58 +0100813 drmmode_crtc->last_good_mode = NULL;
Rob Clark74210d52012-01-08 17:59:08 -0600814
Dave Barnishf245da32013-08-30 12:16:58 +0100815 INFO_MSG("Got CRTC: %d (id: %d)",
816 num, drmmode_crtc->mode_crtc->crtc_id);
Rob Clark74210d52012-01-08 17:59:08 -0600817 crtc->driver_private = drmmode_crtc;
818
819 TRACE_EXIT();
Rob Clark487687e2011-07-17 17:29:02 -0500820 return;
Rob Clark74210d52012-01-08 17:59:08 -0600821}
Rob Clark487687e2011-07-17 17:29:02 -0500822
823static xf86OutputStatus
824drmmode_output_detect(xf86OutputPtr output)
825{
826 /* go to the hw and retrieve a new output struct */
Dave Barnish2e998952013-06-11 16:31:10 +0100827 struct drmmode_output_priv *drmmode_output = output->driver_private;
828 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500829 xf86OutputStatus status;
830 drmModeFreeConnector(drmmode_output->mode_output);
831
832 drmmode_output->mode_output =
Dave Barnish2e998952013-06-11 16:31:10 +0100833 drmModeGetConnector(drmmode->fd,
834 drmmode_output->output_id);
Rob Clark487687e2011-07-17 17:29:02 -0500835
836 switch (drmmode_output->mode_output->connection) {
837 case DRM_MODE_CONNECTED:
838 status = XF86OutputStatusConnected;
839 break;
840 case DRM_MODE_DISCONNECTED:
841 status = XF86OutputStatusDisconnected;
842 break;
843 default:
844 case DRM_MODE_UNKNOWNCONNECTION:
845 status = XF86OutputStatusUnknown;
846 break;
847 }
848 return status;
849}
850
851static Bool
852drmmode_output_mode_valid(xf86OutputPtr output, DisplayModePtr mode)
853{
854 if (mode->type & M_T_DEFAULT)
855 /* Default modes are harmful here. */
856 return MODE_BAD;
857
858 return MODE_OK;
859}
860
861static DisplayModePtr
862drmmode_output_get_modes(xf86OutputPtr output)
863{
864 ScrnInfoPtr pScrn = output->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100865 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500866 drmModeConnectorPtr koutput = drmmode_output->mode_output;
Dave Barnish2e998952013-06-11 16:31:10 +0100867 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Dave Barnish0bdb7382013-06-06 12:04:04 +0100868 DisplayModePtr modes = NULL;
Daniel Kurtzb8131892012-10-17 01:39:54 +0800869 drmModePropertyPtr prop;
Rob Clark487687e2011-07-17 17:29:02 -0500870 xf86MonPtr ddc_mon = NULL;
871 int i;
872
873 /* look for an EDID property */
874 for (i = 0; i < koutput->count_props; i++) {
Daniel Kurtzb8131892012-10-17 01:39:54 +0800875 prop = drmModeGetProperty(drmmode->fd, koutput->props[i]);
876 if (!prop)
Rob Clark487687e2011-07-17 17:29:02 -0500877 continue;
878
Daniel Kurtzb8131892012-10-17 01:39:54 +0800879 if ((prop->flags & DRM_MODE_PROP_BLOB) &&
880 !strcmp(prop->name, "EDID")) {
Rob Clark487687e2011-07-17 17:29:02 -0500881 if (drmmode_output->edid_blob)
Dave Barnish2e998952013-06-11 16:31:10 +0100882 drmModeFreePropertyBlob(
883 drmmode_output->edid_blob);
Rob Clark487687e2011-07-17 17:29:02 -0500884 drmmode_output->edid_blob =
Rob Clark74210d52012-01-08 17:59:08 -0600885 drmModeGetPropertyBlob(drmmode->fd,
Dave Barnish2e998952013-06-11 16:31:10 +0100886 koutput->prop_values[i]);
Rob Clark487687e2011-07-17 17:29:02 -0500887 }
Daniel Kurtzb8131892012-10-17 01:39:54 +0800888 drmModeFreeProperty(prop);
Rob Clark487687e2011-07-17 17:29:02 -0500889 }
890
891 if (drmmode_output->edid_blob)
892 ddc_mon = xf86InterpretEDID(pScrn->scrnIndex,
Rob Clark74210d52012-01-08 17:59:08 -0600893 drmmode_output->edid_blob->data);
Rob Clark487687e2011-07-17 17:29:02 -0500894
895 if (ddc_mon) {
Rob Clark487687e2011-07-17 17:29:02 -0500896 xf86OutputSetEDID(output, ddc_mon);
897 xf86SetDDCproperties(pScrn, ddc_mon);
898 }
899
900 DEBUG_MSG("count_modes: %d", koutput->count_modes);
901
902 /* modes should already be available */
903 for (i = 0; i < koutput->count_modes; i++) {
Dave Barnish0bdb7382013-06-06 12:04:04 +0100904 DisplayModePtr mode = xnfalloc(sizeof(DisplayModeRec));
Rob Clark487687e2011-07-17 17:29:02 -0500905
Dave Barnish0bdb7382013-06-06 12:04:04 +0100906 drmmode_ConvertFromKMode(pScrn, &koutput->modes[i], mode);
907 modes = xf86ModesAdd(modes, mode);
Rob Clark487687e2011-07-17 17:29:02 -0500908 }
Dave Barnish0bdb7382013-06-06 12:04:04 +0100909 return modes;
Rob Clark487687e2011-07-17 17:29:02 -0500910}
911
912static void
913drmmode_output_destroy(xf86OutputPtr output)
914{
Dave Barnish2e998952013-06-11 16:31:10 +0100915 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500916 int i;
917
918 if (drmmode_output->edid_blob)
919 drmModeFreePropertyBlob(drmmode_output->edid_blob);
920 for (i = 0; i < drmmode_output->num_props; i++) {
921 drmModeFreeProperty(drmmode_output->props[i].mode_prop);
922 free(drmmode_output->props[i].atoms);
923 }
Daniel Kurtzf5a38ad2012-10-17 02:03:34 +0800924 free(drmmode_output->props);
Rob Clark487687e2011-07-17 17:29:02 -0500925 drmModeFreeConnector(drmmode_output->mode_output);
926 free(drmmode_output);
927 output->driver_private = NULL;
928}
929
930static void
931drmmode_output_dpms(xf86OutputPtr output, int mode)
932{
Dave Barnish2e998952013-06-11 16:31:10 +0100933 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500934 drmModeConnectorPtr koutput = drmmode_output->mode_output;
Daniel Kurtzb8131892012-10-17 01:39:54 +0800935 drmModePropertyPtr prop;
Dave Barnish2e998952013-06-11 16:31:10 +0100936 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500937 int mode_id = -1, i;
938
939 for (i = 0; i < koutput->count_props; i++) {
Daniel Kurtzb8131892012-10-17 01:39:54 +0800940 prop = drmModeGetProperty(drmmode->fd, koutput->props[i]);
941 if (!prop)
942 continue;
943 if ((prop->flags & DRM_MODE_PROP_ENUM) &&
944 !strcmp(prop->name, "DPMS")) {
945 mode_id = koutput->props[i];
946 drmModeFreeProperty(prop);
947 break;
Rob Clark487687e2011-07-17 17:29:02 -0500948 }
Daniel Kurtzb8131892012-10-17 01:39:54 +0800949 drmModeFreeProperty(prop);
Rob Clark487687e2011-07-17 17:29:02 -0500950 }
951
952 if (mode_id < 0)
953 return;
954
955 drmModeConnectorSetProperty(drmmode->fd, koutput->connector_id,
Rob Clark74210d52012-01-08 17:59:08 -0600956 mode_id, mode);
Rob Clark487687e2011-07-17 17:29:02 -0500957}
958
959static Bool
960drmmode_property_ignore(drmModePropertyPtr prop)
961{
962 if (!prop)
Rob Clark74210d52012-01-08 17:59:08 -0600963 return TRUE;
Rob Clark487687e2011-07-17 17:29:02 -0500964 /* ignore blob prop */
965 if (prop->flags & DRM_MODE_PROP_BLOB)
966 return TRUE;
967 /* ignore standard property */
968 if (!strcmp(prop->name, "EDID") ||
Rob Clark74210d52012-01-08 17:59:08 -0600969 !strcmp(prop->name, "DPMS"))
Rob Clark487687e2011-07-17 17:29:02 -0500970 return TRUE;
971
972 return FALSE;
973}
974
975static void
976drmmode_output_create_resources(xf86OutputPtr output)
977{
Dave Barnish2e998952013-06-11 16:31:10 +0100978 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500979 drmModeConnectorPtr mode_output = drmmode_output->mode_output;
Dave Barnish2e998952013-06-11 16:31:10 +0100980 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500981 drmModePropertyPtr drmmode_prop;
982 uint32_t value;
983 int i, j, err;
984
Dave Barnish2e998952013-06-11 16:31:10 +0100985 drmmode_output->props =
986 calloc(mode_output->count_props,
987 sizeof(struct drmmode_prop_rec));
Rob Clark487687e2011-07-17 17:29:02 -0500988 if (!drmmode_output->props)
989 return;
990
991 drmmode_output->num_props = 0;
Dave Barnish0bdb7382013-06-06 12:04:04 +0100992 for (i = 0; i < mode_output->count_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +0100993 drmmode_prop = drmModeGetProperty(drmmode->fd,
994 mode_output->props[i]);
Rob Clark487687e2011-07-17 17:29:02 -0500995 if (drmmode_property_ignore(drmmode_prop)) {
996 drmModeFreeProperty(drmmode_prop);
997 continue;
998 }
Dave Barnish2e998952013-06-11 16:31:10 +0100999 drmmode_output->props[drmmode_output->num_props].mode_prop =
1000 drmmode_prop;
Dave Barnish0bdb7382013-06-06 12:04:04 +01001001 drmmode_output->props[drmmode_output->num_props].index = i;
Rob Clark487687e2011-07-17 17:29:02 -05001002 drmmode_output->num_props++;
Rob Clark487687e2011-07-17 17:29:02 -05001003 }
1004
1005 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001006 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -05001007 drmmode_prop = p->mode_prop;
1008
1009 value = drmmode_output->mode_output->prop_values[p->index];
1010
1011 if (drmmode_prop->flags & DRM_MODE_PROP_RANGE) {
1012 INT32 range[2];
1013
1014 p->num_atoms = 1;
1015 p->atoms = calloc(p->num_atoms, sizeof(Atom));
1016 if (!p->atoms)
1017 continue;
Dave Barnish2e998952013-06-11 16:31:10 +01001018 p->atoms[0] = MakeAtom(drmmode_prop->name,
1019 strlen(drmmode_prop->name),
1020 TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001021 range[0] = drmmode_prop->values[0];
1022 range[1] = drmmode_prop->values[1];
Dave Barnish2e998952013-06-11 16:31:10 +01001023 err = RRConfigureOutputProperty(output->randr_output,
1024 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -06001025 FALSE, TRUE,
Dave Barnish2e998952013-06-11 16:31:10 +01001026 drmmode_prop->flags &
1027 DRM_MODE_PROP_IMMUTABLE ?
1028 TRUE : FALSE,
1029 2, range);
1030
1031 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001032 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001033 "RRConfigureOutputProperty error, %d\n",
1034 err);
1035
1036 err = RRChangeOutputProperty(output->randr_output,
1037 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -06001038 XA_INTEGER, 32, PropModeReplace, 1,
1039 &value, FALSE, FALSE);
Dave Barnish2e998952013-06-11 16:31:10 +01001040 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001041 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001042 "RRChangeOutputProperty error, %d\n",
1043 err);
1044
Rob Clark487687e2011-07-17 17:29:02 -05001045 } else if (drmmode_prop->flags & DRM_MODE_PROP_ENUM) {
1046 p->num_atoms = drmmode_prop->count_enums + 1;
1047 p->atoms = calloc(p->num_atoms, sizeof(Atom));
1048 if (!p->atoms)
1049 continue;
Dave Barnish2e998952013-06-11 16:31:10 +01001050 p->atoms[0] = MakeAtom(drmmode_prop->name,
1051 strlen(drmmode_prop->name),
1052 TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001053 for (j = 1; j <= drmmode_prop->count_enums; j++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001054 struct drm_mode_property_enum *e =
1055 &drmmode_prop->enums[j-1];
1056 p->atoms[j] = MakeAtom(e->name,
1057 strlen(e->name), TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001058 }
Dave Barnish2e998952013-06-11 16:31:10 +01001059 err = RRConfigureOutputProperty(output->randr_output,
1060 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -06001061 FALSE, FALSE,
Dave Barnish2e998952013-06-11 16:31:10 +01001062 drmmode_prop->flags &
1063 DRM_MODE_PROP_IMMUTABLE ?
1064 TRUE : FALSE,
1065 p->num_atoms - 1,
1066 (INT32 *)&p->atoms[1]);
1067
1068 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001069 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001070 "RRConfigureOutputProperty error, %d\n",
1071 err);
1072
Rob Clark487687e2011-07-17 17:29:02 -05001073 for (j = 0; j < drmmode_prop->count_enums; j++)
1074 if (drmmode_prop->enums[j].value == value)
1075 break;
1076 /* there's always a matching value */
Dave Barnish2e998952013-06-11 16:31:10 +01001077 err = RRChangeOutputProperty(output->randr_output,
1078 p->atoms[0],
1079 XA_ATOM, 32, PropModeReplace, 1,
1080 &p->atoms[j+1], FALSE, FALSE);
1081 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001082 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001083 "RRChangeOutputProperty error, %d\n",
1084 err);
Rob Clark487687e2011-07-17 17:29:02 -05001085 }
1086 }
1087}
1088
1089static Bool
1090drmmode_output_set_property(xf86OutputPtr output, Atom property,
Rob Clark74210d52012-01-08 17:59:08 -06001091 RRPropertyValuePtr value)
Rob Clark487687e2011-07-17 17:29:02 -05001092{
Dave Barnish2e998952013-06-11 16:31:10 +01001093 struct drmmode_output_priv *drmmode_output = output->driver_private;
1094 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001095 int i, ret;
1096
1097 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001098 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -05001099
1100 if (p->atoms[0] != property)
1101 continue;
1102
1103 if (p->mode_prop->flags & DRM_MODE_PROP_RANGE) {
1104 uint32_t val;
1105
1106 if (value->type != XA_INTEGER || value->format != 32 ||
Rob Clark74210d52012-01-08 17:59:08 -06001107 value->size != 1)
Rob Clark487687e2011-07-17 17:29:02 -05001108 return FALSE;
1109 val = *(uint32_t *)value->data;
1110
Dave Barnish2e998952013-06-11 16:31:10 +01001111 ret = drmModeConnectorSetProperty(drmmode->fd,
1112 drmmode_output->output_id,
Rob Clark74210d52012-01-08 17:59:08 -06001113 p->mode_prop->prop_id, (uint64_t)val);
Rob Clark487687e2011-07-17 17:29:02 -05001114
1115 if (ret)
1116 return FALSE;
1117
1118 return TRUE;
1119
1120 } else if (p->mode_prop->flags & DRM_MODE_PROP_ENUM) {
1121 Atom atom;
1122 const char *name;
1123 int j;
1124
Dave Barnish2e998952013-06-11 16:31:10 +01001125 if (value->type != XA_ATOM ||
1126 value->format != 32 ||
1127 value->size != 1)
Rob Clark487687e2011-07-17 17:29:02 -05001128 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +01001129
Rob Clark487687e2011-07-17 17:29:02 -05001130 memcpy(&atom, value->data, 4);
1131 name = NameForAtom(atom);
Daniel Kurtz23148372013-03-27 20:19:45 +08001132 if (name == NULL)
1133 return FALSE;
Rob Clark487687e2011-07-17 17:29:02 -05001134
Dave Barnish2e998952013-06-11 16:31:10 +01001135 /* search for matching name string, then
1136 * set its value down
1137 */
Rob Clark487687e2011-07-17 17:29:02 -05001138 for (j = 0; j < p->mode_prop->count_enums; j++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001139 if (!strcmp(p->mode_prop->enums[j].name,
1140 name)) {
1141 ret = drmModeConnectorSetProperty(
1142 drmmode->fd,
1143 drmmode_output->output_id,
1144 p->mode_prop->prop_id,
1145 p->mode_prop->enums[j].value);
Rob Clark487687e2011-07-17 17:29:02 -05001146
1147 if (ret)
1148 return FALSE;
1149
1150 return TRUE;
1151 }
1152 }
Rob Clark487687e2011-07-17 17:29:02 -05001153 return FALSE;
1154 }
1155 }
Rob Clark487687e2011-07-17 17:29:02 -05001156 return TRUE;
1157}
1158
1159static Bool
1160drmmode_output_get_property(xf86OutputPtr output, Atom property)
1161{
1162
Dave Barnish2e998952013-06-11 16:31:10 +01001163 struct drmmode_output_priv *drmmode_output = output->driver_private;
1164 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001165 uint32_t value;
1166 int err, i;
1167
1168 if (output->scrn->vtSema) {
1169 drmModeFreeConnector(drmmode_output->mode_output);
1170 drmmode_output->mode_output =
Dave Barnish2e998952013-06-11 16:31:10 +01001171 drmModeGetConnector(drmmode->fd,
1172 drmmode_output->output_id);
Rob Clark487687e2011-07-17 17:29:02 -05001173 }
1174
1175 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001176 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -05001177 if (p->atoms[0] != property)
1178 continue;
1179
1180 value = drmmode_output->mode_output->prop_values[p->index];
1181
1182 if (p->mode_prop->flags & DRM_MODE_PROP_RANGE) {
1183 err = RRChangeOutputProperty(output->randr_output,
Rob Clark74210d52012-01-08 17:59:08 -06001184 property, XA_INTEGER, 32,
1185 PropModeReplace, 1, &value,
1186 FALSE, FALSE);
Rob Clark487687e2011-07-17 17:29:02 -05001187
1188 return !err;
1189 } else if (p->mode_prop->flags & DRM_MODE_PROP_ENUM) {
1190 int j;
1191
Dave Barnish2e998952013-06-11 16:31:10 +01001192 /* search for matching name string, then set
1193 * its value down
1194 */
Rob Clark487687e2011-07-17 17:29:02 -05001195 for (j = 0; j < p->mode_prop->count_enums; j++) {
1196 if (p->mode_prop->enums[j].value == value)
1197 break;
1198 }
1199
Dave Barnish2e998952013-06-11 16:31:10 +01001200 err = RRChangeOutputProperty(output->randr_output,
1201 property,
1202 XA_ATOM, 32, PropModeReplace, 1,
1203 &p->atoms[j+1], FALSE, FALSE);
Rob Clark487687e2011-07-17 17:29:02 -05001204
1205 return !err;
1206 }
1207 }
1208
1209 return FALSE;
1210}
1211
1212static const xf86OutputFuncsRec drmmode_output_funcs = {
Rob Clark74210d52012-01-08 17:59:08 -06001213 .create_resources = drmmode_output_create_resources,
1214 .dpms = drmmode_output_dpms,
1215 .detect = drmmode_output_detect,
1216 .mode_valid = drmmode_output_mode_valid,
1217 .get_modes = drmmode_output_get_modes,
1218 .set_property = drmmode_output_set_property,
1219 .get_property = drmmode_output_get_property,
1220 .destroy = drmmode_output_destroy
Rob Clark487687e2011-07-17 17:29:02 -05001221};
1222
Rob Clark487687e2011-07-17 17:29:02 -05001223const char *output_names[] = { "None",
Rob Clark74210d52012-01-08 17:59:08 -06001224 "VGA",
1225 "DVI-I",
1226 "DVI-D",
1227 "DVI-A",
1228 "Composite",
1229 "SVIDEO",
1230 "LVDS",
1231 "CTV",
1232 "DIN",
1233 "DP",
1234 "HDMI",
1235 "HDMI",
1236 "TV",
1237 "eDP",
Rob Clark487687e2011-07-17 17:29:02 -05001238};
1239#define NUM_OUTPUT_NAMES (sizeof(output_names) / sizeof(output_names[0]))
1240
1241static void
Dave Barnish2e998952013-06-11 16:31:10 +01001242drmmode_output_init(ScrnInfoPtr pScrn, struct drmmode_rec *drmmode, int num)
Rob Clark487687e2011-07-17 17:29:02 -05001243{
Rob Clark74210d52012-01-08 17:59:08 -06001244 xf86OutputPtr output;
1245 drmModeConnectorPtr koutput;
1246 drmModeEncoderPtr kencoder;
Dave Barnish2e998952013-06-11 16:31:10 +01001247 struct drmmode_output_priv *drmmode_output;
Rob Clark74210d52012-01-08 17:59:08 -06001248 char name[32];
Rob Clark487687e2011-07-17 17:29:02 -05001249
Rob Clark74210d52012-01-08 17:59:08 -06001250 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001251
Rob Clark74210d52012-01-08 17:59:08 -06001252 koutput = drmModeGetConnector(drmmode->fd,
1253 drmmode->mode_res->connectors[num]);
1254 if (!koutput)
1255 return;
Rob Clark487687e2011-07-17 17:29:02 -05001256
Rob Clark74210d52012-01-08 17:59:08 -06001257 kencoder = drmModeGetEncoder(drmmode->fd, koutput->encoders[0]);
1258 if (!kencoder) {
1259 drmModeFreeConnector(koutput);
1260 return;
1261 }
Rob Clark487687e2011-07-17 17:29:02 -05001262
Rob Clark74210d52012-01-08 17:59:08 -06001263 if (koutput->connector_type >= NUM_OUTPUT_NAMES)
1264 snprintf(name, 32, "Unknown%d-%d", koutput->connector_type,
1265 koutput->connector_type_id);
1266 else
1267 snprintf(name, 32, "%s-%d",
1268 output_names[koutput->connector_type],
1269 koutput->connector_type_id);
Rob Clark487687e2011-07-17 17:29:02 -05001270
Rob Clark74210d52012-01-08 17:59:08 -06001271 output = xf86OutputCreate(pScrn, &drmmode_output_funcs, name);
1272 if (!output) {
1273 drmModeFreeEncoder(kencoder);
1274 drmModeFreeConnector(koutput);
1275 return;
1276 }
Rob Clark487687e2011-07-17 17:29:02 -05001277
Dave Barnish2e998952013-06-11 16:31:10 +01001278 drmmode_output = calloc(sizeof(struct drmmode_output_priv), 1);
Rob Clark74210d52012-01-08 17:59:08 -06001279 if (!drmmode_output) {
1280 xf86OutputDestroy(output);
1281 drmModeFreeConnector(koutput);
1282 drmModeFreeEncoder(kencoder);
1283 return;
1284 }
Rob Clark487687e2011-07-17 17:29:02 -05001285
Rob Clark74210d52012-01-08 17:59:08 -06001286 drmmode_output->output_id = drmmode->mode_res->connectors[num];
1287 drmmode_output->mode_output = koutput;
1288 drmmode_output->mode_encoder = kencoder;
1289 drmmode_output->drmmode = drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001290
Rob Clark74210d52012-01-08 17:59:08 -06001291 output->mm_width = koutput->mmWidth;
1292 output->mm_height = koutput->mmHeight;
1293 output->driver_private = drmmode_output;
Rob Clark487687e2011-07-17 17:29:02 -05001294
Dave Barnishf0952c12013-05-10 15:52:23 +01001295 if (ARMSOCPTR(pScrn)->crtcNum >= 0) {
1296 /* Only single crtc per screen - see if this output can use it*/
1297 output->possible_crtcs =
Dave Barnish2e998952013-06-11 16:31:10 +01001298 (kencoder->possible_crtcs >>
1299 (ARMSOCPTR(pScrn)->crtcNum)
1300 )&1;
1301 } else
Dave Barnishf0952c12013-05-10 15:52:23 +01001302 output->possible_crtcs = kencoder->possible_crtcs;
Dave Barnishf0952c12013-05-10 15:52:23 +01001303
Rob Clark74210d52012-01-08 17:59:08 -06001304 output->possible_clones = kencoder->possible_clones;
1305 output->interlaceAllowed = TRUE;
Rob Clark487687e2011-07-17 17:29:02 -05001306
Rob Clark74210d52012-01-08 17:59:08 -06001307 TRACE_EXIT();
1308 return;
1309}
Rob Clark487687e2011-07-17 17:29:02 -05001310
Paul Geary8ffd91c2013-04-11 16:03:15 +01001311void set_scanout_bo(ScrnInfoPtr pScrn, struct armsoc_bo *bo)
David Garbett3688b332012-05-11 12:17:34 +01001312{
Dave Barnish2e998952013-06-11 16:31:10 +01001313 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
David Garbett3688b332012-05-11 12:17:34 +01001314
1315 /* It had better have a framebuffer if we're scanning it out */
Paul Geary8ffd91c2013-04-11 16:03:15 +01001316 assert(armsoc_bo_get_fb(bo));
David Garbett3688b332012-05-11 12:17:34 +01001317
Paul Geary8ffd91c2013-04-11 16:03:15 +01001318 pARMSOC->scanout = bo;
David Garbett3688b332012-05-11 12:17:34 +01001319}
1320
Dave Barnishf245da32013-08-30 12:16:58 +01001321static Bool resize_scanout_bo(ScrnInfoPtr pScrn, int width, int height)
Rob Clark487687e2011-07-17 17:29:02 -05001322{
Dave Barnish2e998952013-06-11 16:31:10 +01001323 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001324 ScreenPtr pScreen = pScrn->pScreen;
Sean Paul3e107302012-08-30 12:08:12 -07001325 uint32_t pitch;
Rob Clark487687e2011-07-17 17:29:02 -05001326
1327 TRACE_ENTER();
Dave Barnish33866052013-03-18 10:57:40 +00001328 DEBUG_MSG("Resize: %dx%d", width, height);
Rob Clark487687e2011-07-17 17:29:02 -05001329
1330 pScrn->virtualX = width;
1331 pScrn->virtualY = height;
1332
Dave Barnish2e998952013-06-11 16:31:10 +01001333 if ((width != armsoc_bo_width(pARMSOC->scanout))
Paul Geary8ffd91c2013-04-11 16:03:15 +01001334 || (height != armsoc_bo_height(pARMSOC->scanout))
Dave Barnish2e998952013-06-11 16:31:10 +01001335 || (pScrn->bitsPerPixel != armsoc_bo_bpp(pARMSOC->scanout))) {
Dave Barnish33866052013-03-18 10:57:40 +00001336 struct armsoc_bo *new_scanout;
Rob Clark487687e2011-07-17 17:29:02 -05001337
1338 /* allocate new scanout buffer */
Dave Barnish2e998952013-06-11 16:31:10 +01001339 new_scanout = armsoc_bo_new_with_dim(pARMSOC->dev,
1340 width, height,
1341 pScrn->depth, pScrn->bitsPerPixel,
1342 ARMSOC_BO_SCANOUT);
1343 if (!new_scanout) {
1344 /* Try to use the previous buffer if the new resolution
1345 * is smaller than the one on buffer creation
1346 */
1347 DEBUG_MSG(
1348 "allocate new scanout buffer failed - resizing existing bo");
Dave Barnish33866052013-03-18 10:57:40 +00001349 /* Remove the old fb from the bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001350 if (armsoc_bo_rm_fb(pARMSOC->scanout))
Dave Barnish33866052013-03-18 10:57:40 +00001351 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +01001352
Dave Barnish33866052013-03-18 10:57:40 +00001353 /* Resize the bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001354 if (armsoc_bo_resize(pARMSOC->scanout, width, height)) {
Dave Barnish33866052013-03-18 10:57:40 +00001355 armsoc_bo_clear(pARMSOC->scanout);
Ray Smithb4299f82013-03-13 10:08:36 +00001356 if (armsoc_bo_add_fb(pARMSOC->scanout))
1357 ERROR_MSG(
1358 "Failed to add framebuffer to the existing scanout buffer");
Dave Barnish33866052013-03-18 10:57:40 +00001359 return FALSE;
1360 }
Ray Smithb4299f82013-03-13 10:08:36 +00001361
Dave Barnish33866052013-03-18 10:57:40 +00001362 /* Add new fb to the bo */
Ray Smithb4299f82013-03-13 10:08:36 +00001363 if (armsoc_bo_clear(pARMSOC->scanout))
Dave Barnish33866052013-03-18 10:57:40 +00001364 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +01001365
Ray Smithb4299f82013-03-13 10:08:36 +00001366 if (armsoc_bo_add_fb(pARMSOC->scanout)) {
1367 ERROR_MSG(
1368 "Failed to add framebuffer to the existing scanout buffer");
1369 return FALSE;
1370 }
1371
Dave Barnish33866052013-03-18 10:57:40 +00001372 pitch = armsoc_bo_pitch(pARMSOC->scanout);
Dave Barnish2e998952013-06-11 16:31:10 +01001373 } else {
Dave Barnish33866052013-03-18 10:57:40 +00001374 DEBUG_MSG("allocated new scanout buffer okay");
1375 pitch = armsoc_bo_pitch(new_scanout);
1376 /* clear new BO and add FB */
Ray Smithb4299f82013-03-13 10:08:36 +00001377 if (armsoc_bo_clear(new_scanout)) {
Paul Geary8ffd91c2013-04-11 16:03:15 +01001378 armsoc_bo_unreference(new_scanout);
David Garbettae5a6362012-07-02 10:15:47 +01001379 return FALSE;
1380 }
Ray Smithb4299f82013-03-13 10:08:36 +00001381
1382 if (armsoc_bo_add_fb(new_scanout)) {
1383 ERROR_MSG(
1384 "Failed to add framebuffer to the new scanout buffer");
1385 armsoc_bo_unreference(new_scanout);
1386 return FALSE;
1387 }
1388
Dave Barnish33866052013-03-18 10:57:40 +00001389 /* Handle dma_buf fd that may be attached to old bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001390 if (armsoc_bo_has_dmabuf(pARMSOC->scanout)) {
Dave Barnish33866052013-03-18 10:57:40 +00001391 int res;
1392
1393 armsoc_bo_clear_dmabuf(pARMSOC->scanout);
1394 res = armsoc_bo_set_dmabuf(new_scanout);
Dave Barnish2e998952013-06-11 16:31:10 +01001395 if (res) {
1396 ERROR_MSG(
1397 "Unable to attach dma_buf fd to new scanout buffer - %d (%s)\n",
1398 res, strerror(res));
Dave Barnish33866052013-03-18 10:57:40 +00001399 armsoc_bo_unreference(new_scanout);
1400 return FALSE;
1401 }
1402 }
1403 /* delete old scanout buffer */
1404 armsoc_bo_unreference(pARMSOC->scanout);
1405 /* use new scanout buffer */
1406 set_scanout_bo(pScrn, new_scanout);
David Garbettae5a6362012-07-02 10:15:47 +01001407 }
Daniel Kurtz34e72b02012-10-19 14:02:05 -07001408 pScrn->displayWidth = pitch / ((pScrn->bitsPerPixel + 7) / 8);
Dave Barnish2e998952013-06-11 16:31:10 +01001409 } else
Paul Geary8ffd91c2013-04-11 16:03:15 +01001410 pitch = armsoc_bo_pitch(pARMSOC->scanout);
Rob Clark487687e2011-07-17 17:29:02 -05001411
1412 if (pScreen && pScreen->ModifyPixmapHeader) {
1413 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
Dave Barnish33866052013-03-18 10:57:40 +00001414
Rob Clark487687e2011-07-17 17:29:02 -05001415 pScreen->ModifyPixmapHeader(rootPixmap,
1416 pScrn->virtualX, pScrn->virtualY,
1417 pScrn->depth, pScrn->bitsPerPixel, pitch,
Paul Geary8ffd91c2013-04-11 16:03:15 +01001418 armsoc_bo_map(pARMSOC->scanout));
Ray Smithedccfd82013-05-31 15:57:12 +01001419
Dave Barnishe762b122013-08-12 17:13:46 +01001420 /* Bump the serial number to ensure that all existing DRI2
1421 * buffers are invalidated.
Ray Smithedccfd82013-05-31 15:57:12 +01001422 *
Dave Barnishe762b122013-08-12 17:13:46 +01001423 * This is particularly required for when the resolution is
1424 * changed and then reverted to the original size without a
1425 * DRI2 client/s getting a new buffer. Without this, the
1426 * drawable is the same size and serial number so the old
1427 * DRI2Buffer will be returned, even though the backing buffer
1428 * has been deleted.
Ray Smithedccfd82013-05-31 15:57:12 +01001429 */
1430 rootPixmap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
Rob Clark487687e2011-07-17 17:29:02 -05001431 }
Dave Barnishf245da32013-08-30 12:16:58 +01001432 TRACE_EXIT();
1433 return TRUE;
1434}
1435
1436static Bool
1437drmmode_xf86crtc_resize(ScrnInfoPtr pScrn, int width, int height)
1438{
1439 int i;
1440 xf86CrtcConfigPtr xf86_config;
1441
1442 TRACE_ENTER();
Dave Barnishf245da32013-08-30 12:16:58 +01001443 if (!resize_scanout_bo(pScrn, width, height))
1444 return FALSE;
Rob Clark487687e2011-07-17 17:29:02 -05001445
Brian Starkeycd684422012-09-20 09:28:04 +01001446 /* Framebuffer needs to be reset on all CRTCs, not just
1447 * those that have repositioned */
1448 xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1449 for (i = 0; i < xf86_config->num_crtc; i++) {
1450 xf86CrtcPtr crtc = xf86_config->crtc[i];
1451
1452 if (!crtc->enabled)
1453 continue;
1454
1455 drmmode_set_mode_major(crtc, &crtc->mode,
1456 crtc->rotation, crtc->x, crtc->y);
1457 }
1458
Rob Clark487687e2011-07-17 17:29:02 -05001459 TRACE_EXIT();
1460 return TRUE;
Rob Clark74210d52012-01-08 17:59:08 -06001461}
Rob Clark487687e2011-07-17 17:29:02 -05001462
1463static const xf86CrtcConfigFuncsRec drmmode_xf86crtc_config_funcs = {
Daniel Kurtze81fdab2012-12-25 16:43:04 +08001464 .resize = drmmode_xf86crtc_resize
Rob Clark487687e2011-07-17 17:29:02 -05001465};
1466
1467
1468Bool drmmode_pre_init(ScrnInfoPtr pScrn, int fd, int cpp)
1469{
Dave Barnish2e998952013-06-11 16:31:10 +01001470 struct drmmode_rec *drmmode;
Rob Clark74210d52012-01-08 17:59:08 -06001471 int i;
Rob Clark487687e2011-07-17 17:29:02 -05001472
Rob Clark74210d52012-01-08 17:59:08 -06001473 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001474
Rob Clark74210d52012-01-08 17:59:08 -06001475 drmmode = calloc(1, sizeof *drmmode);
Dave Barnish2e998952013-06-11 16:31:10 +01001476 if (!drmmode)
Paul Geary6fe52f32013-04-03 11:12:24 +01001477 return FALSE;
Paul Geary6fe52f32013-04-03 11:12:24 +01001478
Rob Clark74210d52012-01-08 17:59:08 -06001479 drmmode->fd = fd;
Rob Clark487687e2011-07-17 17:29:02 -05001480
Rob Clark74210d52012-01-08 17:59:08 -06001481 xf86CrtcConfigInit(pScrn, &drmmode_xf86crtc_config_funcs);
Rob Clark487687e2011-07-17 17:29:02 -05001482
1483
Rob Clark74210d52012-01-08 17:59:08 -06001484 drmmode->cpp = cpp;
1485 drmmode->mode_res = drmModeGetResources(drmmode->fd);
1486 if (!drmmode->mode_res) {
Paul Geary6fe52f32013-04-03 11:12:24 +01001487 free(drmmode);
Rob Clark74210d52012-01-08 17:59:08 -06001488 return FALSE;
1489 } else {
1490 DEBUG_MSG("Got KMS resources");
1491 DEBUG_MSG(" %d connectors, %d encoders",
1492 drmmode->mode_res->count_connectors,
1493 drmmode->mode_res->count_encoders);
1494 DEBUG_MSG(" %d crtcs, %d fbs",
Dave Barnish2e998952013-06-11 16:31:10 +01001495 drmmode->mode_res->count_crtcs,
1496 drmmode->mode_res->count_fbs);
Rob Clark74210d52012-01-08 17:59:08 -06001497 DEBUG_MSG(" %dx%d minimum resolution",
Dave Barnish2e998952013-06-11 16:31:10 +01001498 drmmode->mode_res->min_width,
1499 drmmode->mode_res->min_height);
Rob Clark74210d52012-01-08 17:59:08 -06001500 DEBUG_MSG(" %dx%d maximum resolution",
Dave Barnish2e998952013-06-11 16:31:10 +01001501 drmmode->mode_res->max_width,
1502 drmmode->mode_res->max_height);
Rob Clark74210d52012-01-08 17:59:08 -06001503 }
1504 xf86CrtcSetSizeRange(pScrn, 320, 200, drmmode->mode_res->max_width,
1505 drmmode->mode_res->max_height);
Dave Barnishf0952c12013-05-10 15:52:23 +01001506
Dave Barnish2e998952013-06-11 16:31:10 +01001507 if (ARMSOCPTR(pScrn)->crtcNum == -1) {
Dave Barnishf0952c12013-05-10 15:52:23 +01001508 INFO_MSG("Adding all CRTCs");
1509 for (i = 0; i < drmmode->mode_res->count_crtcs; i++)
1510 drmmode_crtc_init(pScrn, drmmode, i);
Dave Barnish2e998952013-06-11 16:31:10 +01001511 } else if (ARMSOCPTR(pScrn)->crtcNum < drmmode->mode_res->count_crtcs) {
Dave Barnishf0952c12013-05-10 15:52:23 +01001512 drmmode_crtc_init(pScrn, drmmode, ARMSOCPTR(pScrn)->crtcNum);
1513 } else {
Dave Barnish2e998952013-06-11 16:31:10 +01001514 ERROR_MSG(
1515 "Specified more Screens in xorg.conf than there are DRM CRTCs");
Dave Barnishf0952c12013-05-10 15:52:23 +01001516 return FALSE;
1517 }
Rob Clark487687e2011-07-17 17:29:02 -05001518
Dave Barnish2e998952013-06-11 16:31:10 +01001519 if (ARMSOCPTR(pScrn)->crtcNum != -1) {
1520 if (ARMSOCPTR(pScrn)->crtcNum <
1521 drmmode->mode_res->count_connectors)
1522 drmmode_output_init(pScrn,
1523 drmmode, ARMSOCPTR(pScrn)->crtcNum);
Ray Smithc9c2faf2013-06-07 15:37:19 +01001524 else
1525 return FALSE;
1526 } else {
1527 for (i = 0; i < drmmode->mode_res->count_connectors; i++)
1528 drmmode_output_init(pScrn, drmmode, i);
1529 }
Rob Clark487687e2011-07-17 17:29:02 -05001530
Rob Clark74210d52012-01-08 17:59:08 -06001531 xf86InitialConfiguration(pScrn, TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001532
Rob Clark74210d52012-01-08 17:59:08 -06001533 TRACE_EXIT();
Rob Clark487687e2011-07-17 17:29:02 -05001534
Rob Clark74210d52012-01-08 17:59:08 -06001535 return TRUE;
1536}
Rob Clark487687e2011-07-17 17:29:02 -05001537
1538void
Cooper Yuana83caa62012-06-28 17:19:06 +02001539drmmode_adjust_frame(ScrnInfoPtr pScrn, int x, int y)
Rob Clark487687e2011-07-17 17:29:02 -05001540{
1541 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1542 xf86OutputPtr output = config->output[config->compat_output];
1543 xf86CrtcPtr crtc = output->crtc;
1544
1545 if (!crtc || !crtc->enabled)
1546 return;
1547
1548 drmmode_set_mode_major(crtc, &crtc->mode, crtc->rotation, x, y);
1549}
1550
Rob Clark4b8f30a2011-08-28 12:51:26 -05001551/*
1552 * Page Flipping
1553 */
1554
1555static void
1556page_flip_handler(int fd, unsigned int sequence, unsigned int tv_sec,
Rob Clark74210d52012-01-08 17:59:08 -06001557 unsigned int tv_usec, void *user_data)
Rob Clark4b8f30a2011-08-28 12:51:26 -05001558{
Paul Geary8ffd91c2013-04-11 16:03:15 +01001559 ARMSOCDRI2SwapComplete(user_data);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001560}
1561
1562static drmEventContext event_context = {
1563 .version = DRM_EVENT_CONTEXT_VERSION,
1564 .page_flip_handler = page_flip_handler,
1565};
1566
John Sheu022833e2012-08-15 11:40:11 -07001567int
Rob Clark4b8f30a2011-08-28 12:51:26 -05001568drmmode_page_flip(DrawablePtr draw, uint32_t fb_id, void *priv)
1569{
Daniel Kurtz0361e002013-08-14 21:07:01 +08001570 ScreenPtr pScreen = draw->pScreen;
1571 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Dave Barnish2e998952013-06-11 16:31:10 +01001572 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Ray Smith3c33c3d2013-03-26 16:06:37 +00001573 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
Dave Barnish2e998952013-06-11 16:31:10 +01001574 struct drmmode_crtc_private_rec *crtc = config->crtc[0]->driver_private;
1575 struct drmmode_rec *mode = crtc->drmmode;
John Sheu022833e2012-08-15 11:40:11 -07001576 int ret, i, failed = 0, num_flipped = 0;
Raymond Smith16a910e2012-05-09 13:04:51 +01001577 unsigned int flags = 0;
1578
Dave Barnishde45ed42013-06-05 13:47:56 +01001579 if (pARMSOC->drmmode_interface->use_page_flip_events)
Ray Smith3c33c3d2013-03-26 16:06:37 +00001580 flags |= DRM_MODE_PAGE_FLIP_EVENT;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001581
1582 /* if we can flip, we must be fullscreen.. so flip all CRTC's.. */
1583 for (i = 0; i < config->num_crtc; i++) {
1584 crtc = config->crtc[i]->driver_private;
1585
Ray Smithe20b3812013-04-08 12:53:53 +01001586 if (!config->crtc[i]->enabled)
1587 continue;
1588
Rob Clark4b8f30a2011-08-28 12:51:26 -05001589 ret = drmModePageFlip(mode->fd, crtc->mode_crtc->crtc_id,
Raymond Smith16a910e2012-05-09 13:04:51 +01001590 fb_id, flags, priv);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001591 if (ret) {
Ray Smith3c33c3d2013-03-26 16:06:37 +00001592 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
Dave Barnish2e998952013-06-11 16:31:10 +01001593 "flip queue failed: %s\n",
1594 strerror(errno));
John Sheu022833e2012-08-15 11:40:11 -07001595 failed = 1;
Dave Barnish2e998952013-06-11 16:31:10 +01001596 } else
John Sheu022833e2012-08-15 11:40:11 -07001597 num_flipped += 1;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001598 }
1599
John Sheu022833e2012-08-15 11:40:11 -07001600 if (failed)
1601 return -(num_flipped + 1);
1602 else
1603 return num_flipped;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001604}
Rob Clark487687e2011-07-17 17:29:02 -05001605
1606/*
1607 * Hot Plug Event handling:
David Garbettdbecfdd2013-08-28 10:45:03 +01001608 * TODO: MIDEGL-1441: Do we need to keep this handler, which
1609 * Rob originally wrote?
Rob Clark487687e2011-07-17 17:29:02 -05001610 */
Rob Clark487687e2011-07-17 17:29:02 -05001611static void
1612drmmode_handle_uevents(int fd, void *closure)
1613{
Rob Clark74210d52012-01-08 17:59:08 -06001614 ScrnInfoPtr pScrn = closure;
Dave Barnish2e998952013-06-11 16:31:10 +01001615 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
1616 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark74210d52012-01-08 17:59:08 -06001617 struct udev_device *dev;
1618 const char *hotplug;
1619 struct stat s;
1620 dev_t udev_devnum;
Rob Clark487687e2011-07-17 17:29:02 -05001621
Rob Clark74210d52012-01-08 17:59:08 -06001622 dev = udev_monitor_receive_device(drmmode->uevent_monitor);
1623 if (!dev)
1624 return;
Rob Clark487687e2011-07-17 17:29:02 -05001625
Rob Clark74210d52012-01-08 17:59:08 -06001626 /*
1627 * Check to make sure this event is directed at our
1628 * device (by comparing dev_t values), then make
1629 * sure it's a hotplug event (HOTPLUG=1)
1630 */
1631 udev_devnum = udev_device_get_devnum(dev);
David Garbettdbecfdd2013-08-28 10:45:03 +01001632 if (fstat(pARMSOC->drmFD, &s)) {
1633 ERROR_MSG("fstat failed: %s", strerror(errno));
1634 udev_device_unref(dev);
1635 return;
1636 }
Rob Clark487687e2011-07-17 17:29:02 -05001637
Rob Clark74210d52012-01-08 17:59:08 -06001638 hotplug = udev_device_get_property_value(dev, "HOTPLUG");
Rob Clark487687e2011-07-17 17:29:02 -05001639
Rob Clark74210d52012-01-08 17:59:08 -06001640 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "hotplug=%s, match=%d\n", hotplug,
Dave Barnish2e998952013-06-11 16:31:10 +01001641 !memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)));
Rob Clark487687e2011-07-17 17:29:02 -05001642
Dave Barnish2e998952013-06-11 16:31:10 +01001643 if (memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)) == 0 &&
Rob Clark74210d52012-01-08 17:59:08 -06001644 hotplug && atoi(hotplug) == 1) {
Daniel Kurtz0361e002013-08-14 21:07:01 +08001645 RRGetInfo(xf86ScrnToScreen(pScrn), TRUE);
Rob Clark74210d52012-01-08 17:59:08 -06001646 }
1647 udev_device_unref(dev);
1648}
Rob Clark487687e2011-07-17 17:29:02 -05001649
Rob Clark4b8f30a2011-08-28 12:51:26 -05001650static void
Rob Clark487687e2011-07-17 17:29:02 -05001651drmmode_uevent_init(ScrnInfoPtr pScrn)
1652{
Dave Barnish2e998952013-06-11 16:31:10 +01001653 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark74210d52012-01-08 17:59:08 -06001654 struct udev *u;
1655 struct udev_monitor *mon;
Rob Clark487687e2011-07-17 17:29:02 -05001656
Rob Clark74210d52012-01-08 17:59:08 -06001657 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001658
Rob Clark74210d52012-01-08 17:59:08 -06001659 u = udev_new();
1660 if (!u)
1661 return;
1662 mon = udev_monitor_new_from_netlink(u, "udev");
1663 if (!mon) {
1664 udev_unref(u);
1665 return;
1666 }
Rob Clark487687e2011-07-17 17:29:02 -05001667
Rob Clark74210d52012-01-08 17:59:08 -06001668 if (udev_monitor_filter_add_match_subsystem_devtype(mon,
1669 "drm",
1670 "drm_minor") < 0 ||
1671 udev_monitor_enable_receiving(mon) < 0) {
1672 udev_monitor_unref(mon);
1673 udev_unref(u);
1674 return;
1675 }
Rob Clark487687e2011-07-17 17:29:02 -05001676
Rob Clark74210d52012-01-08 17:59:08 -06001677 drmmode->uevent_handler =
1678 xf86AddGeneralHandler(udev_monitor_get_fd(mon),
1679 drmmode_handle_uevents, pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001680
Rob Clark74210d52012-01-08 17:59:08 -06001681 drmmode->uevent_monitor = mon;
Rob Clark487687e2011-07-17 17:29:02 -05001682
Rob Clark74210d52012-01-08 17:59:08 -06001683 TRACE_EXIT();
1684}
Rob Clark487687e2011-07-17 17:29:02 -05001685
Rob Clark4b8f30a2011-08-28 12:51:26 -05001686static void
Rob Clark487687e2011-07-17 17:29:02 -05001687drmmode_uevent_fini(ScrnInfoPtr pScrn)
1688{
Dave Barnish2e998952013-06-11 16:31:10 +01001689 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001690
Rob Clark74210d52012-01-08 17:59:08 -06001691 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001692
Rob Clark74210d52012-01-08 17:59:08 -06001693 if (drmmode->uevent_handler) {
1694 struct udev *u = udev_monitor_get_udev(drmmode->uevent_monitor);
1695 xf86RemoveGeneralHandler(drmmode->uevent_handler);
Rob Clark487687e2011-07-17 17:29:02 -05001696
Rob Clark74210d52012-01-08 17:59:08 -06001697 udev_monitor_unref(drmmode->uevent_monitor);
1698 udev_unref(u);
1699 }
Rob Clark487687e2011-07-17 17:29:02 -05001700
Rob Clark74210d52012-01-08 17:59:08 -06001701 TRACE_EXIT();
1702}
Rob Clark4b8f30a2011-08-28 12:51:26 -05001703
1704static void
1705drmmode_wakeup_handler(pointer data, int err, pointer p)
1706{
Ray Smith04af9992013-06-26 13:44:54 +01001707 int fd = (int)data;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001708 fd_set *read_mask = p;
1709
Ray Smith04af9992013-06-26 13:44:54 +01001710 if (err < 0)
Rob Clark4b8f30a2011-08-28 12:51:26 -05001711 return;
1712
Ray Smith04af9992013-06-26 13:44:54 +01001713 if (FD_ISSET(fd, read_mask))
1714 drmHandleEvent(fd, &event_context);
1715}
Paul Geary6fe52f32013-04-03 11:12:24 +01001716
Ray Smith04af9992013-06-26 13:44:54 +01001717void drmmode_init_wakeup_handler(int fd)
1718{
1719 AddGeneralSocket(fd);
1720 RegisterBlockAndWakeupHandlers((BlockHandlerProcPtr)NoopDDA,
1721 drmmode_wakeup_handler, (pointer)fd);
1722}
1723
1724void drmmode_fini_wakeup_handler(int fd)
1725{
1726 RemoveBlockAndWakeupHandlers((BlockHandlerProcPtr)NoopDDA,
1727 drmmode_wakeup_handler, (pointer)fd);
1728 RemoveGeneralSocket(fd);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001729}
1730
1731void
Rob Clark67b875f2012-04-20 19:13:57 -05001732drmmode_wait_for_event(ScrnInfoPtr pScrn)
1733{
Dave Barnish2e998952013-06-11 16:31:10 +01001734 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark67b875f2012-04-20 19:13:57 -05001735 drmHandleEvent(drmmode->fd, &event_context);
1736}
1737
1738void
Rob Clark4b8f30a2011-08-28 12:51:26 -05001739drmmode_screen_init(ScrnInfoPtr pScrn)
1740{
Rob Clark4b8f30a2011-08-28 12:51:26 -05001741 drmmode_uevent_init(pScrn);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001742}
1743
1744void
1745drmmode_screen_fini(ScrnInfoPtr pScrn)
1746{
1747 drmmode_uevent_fini(pScrn);
1748}