blob: c20259c171168b5c527c650f3b4ccb61e50a3abb [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
185static Bool
186drmmode_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode,
Rob Clark74210d52012-01-08 17:59:08 -0600187 Rotation rotation, int x, int y)
Rob Clark487687e2011-07-17 17:29:02 -0500188{
189 ScrnInfoPtr pScrn = crtc->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100190 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -0500191 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
Dave Barnish2e998952013-06-11 16:31:10 +0100192 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
193 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500194 uint32_t *output_ids = NULL;
195 int output_count = 0;
196 int ret = TRUE;
197 int i;
David Garbett7171c522012-05-11 13:12:50 +0100198 uint32_t fb_id;
Rob Clark487687e2011-07-17 17:29:02 -0500199 drmModeModeInfo kmode;
Dave Barnishf256ebe2013-08-22 15:05:58 +0100200 drmModeCrtcPtr newcrtc = NULL;
Rob Clark487687e2011-07-17 17:29:02 -0500201
202 TRACE_ENTER();
203
Paul Geary8ffd91c2013-04-11 16:03:15 +0100204 fb_id = armsoc_bo_get_fb(pARMSOC->scanout);
David Garbett7171c522012-05-11 13:12:50 +0100205
206 if (fb_id == 0) {
David Garbett7171c522012-05-11 13:12:50 +0100207 DEBUG_MSG("create framebuffer: %dx%d",
208 pScrn->virtualX, pScrn->virtualY);
209
Paul Geary8ffd91c2013-04-11 16:03:15 +0100210 ret = armsoc_bo_add_fb(pARMSOC->scanout);
Ray Smithb4299f82013-03-13 10:08:36 +0000211 if (ret) {
212 ERROR_MSG(
213 "Failed to add framebuffer to the scanout buffer");
David Garbett7171c522012-05-11 13:12:50 +0100214 return FALSE;
Ray Smithb4299f82013-03-13 10:08:36 +0000215 }
Dave Barnish523c9ff2013-03-12 10:59:03 +0000216
Paul Geary8ffd91c2013-04-11 16:03:15 +0100217 fb_id = armsoc_bo_get_fb(pARMSOC->scanout);
Paul Geary6fe52f32013-04-03 11:12:24 +0100218 if (0 == fb_id)
219 return FALSE;
David Garbett7171c522012-05-11 13:12:50 +0100220 }
Rob Clark487687e2011-07-17 17:29:02 -0500221
Rob Clark487687e2011-07-17 17:29:02 -0500222 /* Set the new mode: */
223 crtc->mode = *mode;
224 crtc->x = x;
225 crtc->y = y;
226 crtc->rotation = rotation;
227
228 output_ids = calloc(sizeof(uint32_t), xf86_config->num_output);
229 if (!output_ids) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100230 ERROR_MSG(
231 "memory allocation failed in drmmode_set_mode_major()");
Rob Clark487687e2011-07-17 17:29:02 -0500232 ret = FALSE;
233 goto done;
234 }
235
236 for (i = 0; i < xf86_config->num_output; i++) {
237 xf86OutputPtr output = xf86_config->output[i];
Dave Barnish2e998952013-06-11 16:31:10 +0100238 struct drmmode_output_priv *drmmode_output;
Rob Clark487687e2011-07-17 17:29:02 -0500239
240 if (output->crtc != crtc)
241 continue;
242
243 drmmode_output = output->driver_private;
244 output_ids[output_count] =
245 drmmode_output->mode_output->connector_id;
246 output_count++;
247 }
248
Dave Barnish2e998952013-06-11 16:31:10 +0100249 if (!xf86CrtcRotate(crtc)) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100250 ERROR_MSG(
251 "failed to assign rotation in drmmode_set_mode_major()");
Dave Barnish523c9ff2013-03-12 10:59:03 +0000252 ret = FALSE;
Rob Clark487687e2011-07-17 17:29:02 -0500253 goto done;
Dave Barnish523c9ff2013-03-12 10:59:03 +0000254 }
Rob Clark487687e2011-07-17 17:29:02 -0500255
Mandeep Singh Bainesc874fcb2012-09-13 15:30:00 +0200256 if (crtc->funcs->gamma_set)
257 crtc->funcs->gamma_set(crtc, crtc->gamma_red, crtc->gamma_green,
258 crtc->gamma_blue, crtc->gamma_size);
Rob Clark487687e2011-07-17 17:29:02 -0500259
260 drmmode_ConvertToKMode(crtc->scrn, &kmode, mode);
261
Rob Clark487687e2011-07-17 17:29:02 -0500262 ret = drmModeSetCrtc(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id,
263 fb_id, x, y, output_ids, output_count, &kmode);
264 if (ret) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100265 ERROR_MSG(
266 "failed to set mode: %s", strerror(-ret));
Dave Barnish523c9ff2013-03-12 10:59:03 +0000267 ret = FALSE;
268 goto done;
Dave Barnishf256ebe2013-08-22 15:05:58 +0100269 }
270
271 /* get the actual crtc info */
272 newcrtc = drmModeGetCrtc(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id);
273 if (!newcrtc) {
274 ERROR_MSG("couldn't get actual mode back");
275 ret = FALSE;
276 goto done;
277 }
278
279 if (kmode.hdisplay != newcrtc->mode.hdisplay ||
280 kmode.vdisplay != newcrtc->mode.vdisplay) {
281
Dave Barnishf256ebe2013-08-22 15:05:58 +0100282 ret = FALSE;
283 ERROR_MSG(
284 "drm did not set requested mode! (requested %dx%d, actual %dx%d)",
285 kmode.hdisplay, kmode.vdisplay,
286 newcrtc->mode.hdisplay,
287 newcrtc->mode.vdisplay);
288
Dave Barnishf245da32013-08-30 12:16:58 +0100289 if (!drmmode_crtc->last_good_mode) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100290 DEBUG_MSG("No last good values to use");
291 goto done;
292 }
293
Dave Barnishf256ebe2013-08-22 15:05:58 +0100294 /* revert to last good settings */
Dave Barnishf245da32013-08-30 12:16:58 +0100295 DEBUG_MSG("Reverting to last_good values");
296 if (!resize_scanout_bo(pScrn,
297 drmmode_crtc->last_good_mode->HDisplay,
298 drmmode_crtc->last_good_mode->VDisplay)) {
299 ERROR_MSG("Could not revert to last good mode");
300 goto done;
301 }
302
Dave Barnishf256ebe2013-08-22 15:05:58 +0100303 fb_id = armsoc_bo_get_fb(pARMSOC->scanout);
304 drmmode_ConvertToKMode(crtc->scrn, &kmode,
Dave Barnishf245da32013-08-30 12:16:58 +0100305 drmmode_crtc->last_good_mode);
Dave Barnishf256ebe2013-08-22 15:05:58 +0100306 drmModeSetCrtc(drmmode->fd,
307 drmmode_crtc->mode_crtc->crtc_id,
Dave Barnishf245da32013-08-30 12:16:58 +0100308 fb_id,
309 drmmode_crtc->last_good_x,
310 drmmode_crtc->last_good_y,
Dave Barnishf256ebe2013-08-22 15:05:58 +0100311 output_ids, output_count, &kmode);
Dave Barnishf256ebe2013-08-22 15:05:58 +0100312
313 /* let RandR know we changed things */
314 xf86RandR12TellChanged(pScrn->pScreen);
315
Rob Clark487687e2011-07-17 17:29:02 -0500316 } else {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100317 /* When called on a resize, crtc->mode already contains the
318 * resized values so we can't use this for recovery.
319 * We can't read it out of the crtc either as mode_valid is 0.
320 * Instead we save the last good mode set here & fallback to
321 * that on failure.
322 */
323 DEBUG_MSG("Setting last good values");
Dave Barnishf245da32013-08-30 12:16:58 +0100324 drmmode_crtc->last_good_x = crtc->x;
325 drmmode_crtc->last_good_y = crtc->y;
326 drmmode_crtc->last_good_rotation = crtc->rotation;
327 if (drmmode_crtc->last_good_mode) {
328 if (drmmode_crtc->last_good_mode->name) {
329 free(drmmode_crtc->last_good_mode->name);
330 }
331 free(drmmode_crtc->last_good_mode);
332 }
333 drmmode_crtc->last_good_mode = xf86DuplicateMode(&crtc->mode);
Dave Barnishf256ebe2013-08-22 15:05:58 +0100334
Rob Clark487687e2011-07-17 17:29:02 -0500335 ret = TRUE;
336 }
337
Rob Clark487687e2011-07-17 17:29:02 -0500338 /* Turn on any outputs on this crtc that may have been disabled: */
339 for (i = 0; i < xf86_config->num_output; i++) {
340 xf86OutputPtr output = xf86_config->output[i];
341
342 if (output->crtc != crtc)
343 continue;
344
345 drmmode_output_dpms(output, DPMSModeOn);
346 }
347
Dave Barnishaf046152013-05-31 09:12:44 +0100348 /* if hw cursor is initialized, reload it */
Dave Barnish2e998952013-06-11 16:31:10 +0100349 if (drmmode->cursor)
Dave Barnishaf046152013-05-31 09:12:44 +0100350 xf86_reload_cursors(pScrn->pScreen);
Rob Clark487687e2011-07-17 17:29:02 -0500351
352done:
Dave Barnishf256ebe2013-08-22 15:05:58 +0100353 if (newcrtc)
354 drmModeFreeCrtc(newcrtc);
355
Dave Barnish2e998952013-06-11 16:31:10 +0100356 if (output_ids)
Rob Clark487687e2011-07-17 17:29:02 -0500357 free(output_ids);
Dave Barnish2e998952013-06-11 16:31:10 +0100358
Dave Barnishf245da32013-08-30 12:16:58 +0100359 if (!ret && !drmmode_crtc->last_good_mode) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100360 /* If there was a problem, restore the last good mode: */
Dave Barnishf245da32013-08-30 12:16:58 +0100361 crtc->x = drmmode_crtc->last_good_x;
362 crtc->y = drmmode_crtc->last_good_y;
363 crtc->rotation = drmmode_crtc->last_good_rotation;
364 crtc->mode = *drmmode_crtc->last_good_mode;
Rob Clark487687e2011-07-17 17:29:02 -0500365 }
366
367 TRACE_EXIT();
368 return ret;
Rob Clark74210d52012-01-08 17:59:08 -0600369}
Rob Clark487687e2011-07-17 17:29:02 -0500370
371static void
Rob Clark487687e2011-07-17 17:29:02 -0500372drmmode_hide_cursor(xf86CrtcPtr crtc)
373{
Dave Barnish2e998952013-06-11 16:31:10 +0100374 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
375 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
376 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Javier Martin4c572792013-09-11 15:08:19 +0100377 ScrnInfoPtr pScrn = crtc->scrn;
378 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -0500379
Rob Clark687c6082012-01-08 19:33:18 -0600380 if (!cursor)
381 return;
382
Stéphane Marchesinb8b35202012-10-02 20:27:40 -0700383 drmmode_crtc->cursor_visible = FALSE;
Rob Clark687c6082012-01-08 19:33:18 -0600384
Javier Martin4c572792013-09-11 15:08:19 +0100385 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE) {
386 /* set plane's fb_id to 0 to disable it */
387 drmModeSetPlane(drmmode->fd, cursor->ovr->plane_id,
388 drmmode_crtc->mode_crtc->crtc_id, 0, 0,
389 0, 0, 0, 0, 0, 0, 0, 0);
390 } else { /* HWCURSOR_API_STANDARD */
391 /* set handle to 0 to disable the cursor */
392 drmModeSetCursor(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id,
393 0, 0, 0);
394 }
Rob Clark74210d52012-01-08 17:59:08 -0600395}
Rob Clark487687e2011-07-17 17:29:02 -0500396
Javier Martin4c572792013-09-11 15:08:19 +0100397/*
398 * The argument "update_image" controls whether the cursor image needs
399 * to be updated by the HW or not. This is ignored by HWCURSOR_API_PLANE
400 * which doesn't allow changing the cursor possition without updating
401 * the image too.
402 */
Rob Clark487687e2011-07-17 17:29:02 -0500403static void
Javier Martin4c572792013-09-11 15:08:19 +0100404drmmode_show_cursor_image(xf86CrtcPtr crtc, Bool update_image)
Rob Clark487687e2011-07-17 17:29:02 -0500405{
Dave Barnish2e998952013-06-11 16:31:10 +0100406 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
407 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
408 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Dave Barnishde45ed42013-06-05 13:47:56 +0100409 int crtc_x, crtc_y, src_x, src_y;
410 int w, h, pad;
411 ScrnInfoPtr pScrn = crtc->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100412 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -0500413
Rob Clark687c6082012-01-08 19:33:18 -0600414 if (!cursor)
415 return;
Rob Clark487687e2011-07-17 17:29:02 -0500416
Stéphane Marchesinb8b35202012-10-02 20:27:40 -0700417 drmmode_crtc->cursor_visible = TRUE;
Rob Clark687c6082012-01-08 19:33:18 -0600418
Dave Barnishde45ed42013-06-05 13:47:56 +0100419 w = pARMSOC->drmmode_interface->cursor_width;
420 h = pARMSOC->drmmode_interface->cursor_height;
421 pad = pARMSOC->drmmode_interface->cursor_padding;
422
Dave Barnish2e998952013-06-11 16:31:10 +0100423 /* get padded width */
424 w = w + 2 * pad;
425 /* get x of padded cursor */
426 crtc_x = cursor->x - pad;
Rob Clark60f5bad2012-01-22 18:35:28 -0600427 crtc_y = cursor->y;
Rob Clark60f5bad2012-01-22 18:35:28 -0600428
Javier Martin4c572792013-09-11 15:08:19 +0100429 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE) {
430 src_x = 0;
431 src_y = 0;
Rob Clark60f5bad2012-01-22 18:35:28 -0600432
Javier Martin4c572792013-09-11 15:08:19 +0100433 /* calculate clipped x, y, w & h if cursor is off edges */
434 if (crtc_x < 0) {
435 src_x += -crtc_x;
436 w -= -crtc_x;
437 crtc_x = 0;
438 }
Rob Clark60f5bad2012-01-22 18:35:28 -0600439
Javier Martin4c572792013-09-11 15:08:19 +0100440 if (crtc_y < 0) {
441 src_y += -crtc_y;
442 h -= -crtc_y;
443 crtc_y = 0;
444 }
Rob Clark60f5bad2012-01-22 18:35:28 -0600445
Javier Martin4c572792013-09-11 15:08:19 +0100446 if ((crtc_x + w) > crtc->mode.HDisplay)
447 w = crtc->mode.HDisplay - crtc_x;
Rob Clark60f5bad2012-01-22 18:35:28 -0600448
Javier Martin4c572792013-09-11 15:08:19 +0100449 if ((crtc_y + h) > crtc->mode.VDisplay)
450 h = crtc->mode.VDisplay - crtc_y;
451
452 /* note src coords (last 4 args) are in Q16 format */
453 drmModeSetPlane(drmmode->fd, cursor->ovr->plane_id,
Rob Clark687c6082012-01-08 19:33:18 -0600454 drmmode_crtc->mode_crtc->crtc_id, cursor->fb_id, 0,
Dave Barnish2e998952013-06-11 16:31:10 +0100455 crtc_x, crtc_y, w, h, src_x<<16, src_y<<16,
456 w<<16, h<<16);
Javier Martin4c572792013-09-11 15:08:19 +0100457 } else {
458 if (update_image)
459 drmModeSetCursor(drmmode->fd,
460 drmmode_crtc->mode_crtc->crtc_id,
461 cursor->handle, w, h);
462 drmModeMoveCursor(drmmode->fd,
463 drmmode_crtc->mode_crtc->crtc_id,
464 crtc_x, crtc_y);
465 }
466}
467
468static void
469drmmode_show_cursor(xf86CrtcPtr crtc)
470{
471 drmmode_show_cursor_image(crtc, TRUE);
Rob Clark687c6082012-01-08 19:33:18 -0600472}
473
474static void
475drmmode_set_cursor_position(xf86CrtcPtr crtc, int x, int y)
476{
Dave Barnish2e998952013-06-11 16:31:10 +0100477 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
478 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
479 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Rob Clark687c6082012-01-08 19:33:18 -0600480
481 if (!cursor)
482 return;
483
484 cursor->x = x;
485 cursor->y = y;
486
Javier Martin4c572792013-09-11 15:08:19 +0100487 /*
488 * Show the cursor at a different possition without updating the image
489 * when possible (HWCURSOR_API_PLANE doesn't have a way to update
490 * cursor position without updating the image too).
491 */
492 drmmode_show_cursor_image(crtc, FALSE);
Rob Clark687c6082012-01-08 19:33:18 -0600493}
494
495static void
496drmmode_load_cursor_argb(xf86CrtcPtr crtc, CARD32 *image)
497{
Dave Barnish2e998952013-06-11 16:31:10 +0100498 uint32_t *d;
499 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
500 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
501 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Rob Clark979add52012-02-21 18:35:24 -0600502 int visible;
Dave Barnishde45ed42013-06-05 13:47:56 +0100503 ScrnInfoPtr pScrn = crtc->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100504 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark687c6082012-01-08 19:33:18 -0600505
506 if (!cursor)
507 return;
508
Stéphane Marchesinb8b35202012-10-02 20:27:40 -0700509 visible = drmmode_crtc->cursor_visible;
Rob Clark979add52012-02-21 18:35:24 -0600510
511 if (visible)
Rob Clark687c6082012-01-08 19:33:18 -0600512 drmmode_hide_cursor(crtc);
513
Paul Geary8ffd91c2013-04-11 16:03:15 +0100514 d = armsoc_bo_map(cursor->bo);
Dave Barnish2e998952013-06-11 16:31:10 +0100515 if (!d) {
Paul Geary6fe52f32013-04-03 11:12:24 +0100516 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
517 "load_cursor_argb map failure\n");
518 if (visible)
Javier Martin4c572792013-09-11 15:08:19 +0100519 drmmode_show_cursor_image(crtc, TRUE);
Paul Geary6fe52f32013-04-03 11:12:24 +0100520 return;
521 }
John Rees292ae502013-03-21 16:24:35 +0000522
Dave Barnishde45ed42013-06-05 13:47:56 +0100523 /* set_cursor_image is a mandatory function */
Dave Barnish2e998952013-06-11 16:31:10 +0100524 assert(pARMSOC->drmmode_interface->set_cursor_image);
Dave Barnishde45ed42013-06-05 13:47:56 +0100525 pARMSOC->drmmode_interface->set_cursor_image(crtc, d, image);
Rob Clark687c6082012-01-08 19:33:18 -0600526
Rob Clark979add52012-02-21 18:35:24 -0600527 if (visible)
Javier Martin4c572792013-09-11 15:08:19 +0100528 drmmode_show_cursor_image(crtc, TRUE);
Rob Clark687c6082012-01-08 19:33:18 -0600529}
530
Javier Martin4c572792013-09-11 15:08:19 +0100531static Bool
532drmmode_cursor_init_plane(ScreenPtr pScreen)
Rob Clark687c6082012-01-08 19:33:18 -0600533{
Daniel Kurtz0361e002013-08-14 21:07:01 +0800534 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Dave Barnish2e998952013-06-11 16:31:10 +0100535 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
536 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
537 struct drmmode_cursor_rec *cursor;
Rob Clark687c6082012-01-08 19:33:18 -0600538 drmModePlaneRes *plane_resources;
539 drmModePlane *ovr;
Dave Barnishde45ed42013-06-05 13:47:56 +0100540 int w, h, pad;
Rob Clark687c6082012-01-08 19:33:18 -0600541 uint32_t handles[4], pitches[4], offsets[4]; /* we only use [0] */
542
543 if (drmmode->cursor) {
544 INFO_MSG("cursor already initialized");
545 return TRUE;
546 }
547
Dave Barnish2e998952013-06-11 16:31:10 +0100548 if (!xf86LoaderCheckSymbol("drmModeGetPlaneResources")) {
549 ERROR_MSG(
550 "HW cursor not supported (needs libdrm 2.4.30 or higher)");
Ray Smith003cf5e2013-03-19 10:44:07 +0000551 return FALSE;
552 }
Rob Clark687c6082012-01-08 19:33:18 -0600553
554 /* find an unused plane which can be used as a mouse cursor. Note
555 * that we cheat a bit, in order to not burn one overlay per crtc,
556 * and only show the mouse cursor on one crtc at a time
557 */
558 plane_resources = drmModeGetPlaneResources(drmmode->fd);
559 if (!plane_resources) {
Dave Barnish2e998952013-06-11 16:31:10 +0100560 ERROR_MSG("HW cursor: drmModeGetPlaneResources failed: %s",
561 strerror(errno));
Rob Clark687c6082012-01-08 19:33:18 -0600562 return FALSE;
563 }
564
565 if (plane_resources->count_planes < 1) {
566 ERROR_MSG("not enough planes for HW cursor");
Ray Smith003cf5e2013-03-19 10:44:07 +0000567 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600568 return FALSE;
569 }
570
571 ovr = drmModeGetPlane(drmmode->fd, plane_resources->planes[0]);
572 if (!ovr) {
Dave Barnish2e998952013-06-11 16:31:10 +0100573 ERROR_MSG("HW cursor: drmModeGetPlane failed: %s",
574 strerror(errno));
Ray Smith003cf5e2013-03-19 10:44:07 +0000575 drmModeFreePlaneResources(plane_resources);
576 return FALSE;
577 }
578
Dave Barnishde45ed42013-06-05 13:47:56 +0100579 if (pARMSOC->drmmode_interface->init_plane_for_cursor &&
Dave Barnish2e998952013-06-11 16:31:10 +0100580 pARMSOC->drmmode_interface->init_plane_for_cursor(
581 drmmode->fd, ovr->plane_id)) {
Ray Smithb93cd892013-04-03 10:06:18 +0100582 ERROR_MSG("Failed driver-specific cursor initialization");
583 drmModeFreePlaneResources(plane_resources);
584 return FALSE;
585 }
586
Dave Barnish2e998952013-06-11 16:31:10 +0100587 cursor = calloc(1, sizeof(struct drmmode_cursor_rec));
Ray Smith003cf5e2013-03-19 10:44:07 +0000588 if (!cursor) {
Dave Barnishfd50b132013-05-16 15:00:02 +0100589 ERROR_MSG("HW cursor: calloc failed");
Ray Smith003cf5e2013-03-19 10:44:07 +0000590 drmModeFreePlane(ovr);
591 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600592 return FALSE;
593 }
594
595 cursor->ovr = ovr;
Dave Barnishde45ed42013-06-05 13:47:56 +0100596
597 w = pARMSOC->drmmode_interface->cursor_width;
598 h = pARMSOC->drmmode_interface->cursor_height;
599 pad = pARMSOC->drmmode_interface->cursor_padding;
600
601 /* allow for cursor padding in the bo */
Dave Barnish2e998952013-06-11 16:31:10 +0100602 cursor->bo = armsoc_bo_new_with_dim(pARMSOC->dev,
603 w + 2 * pad, h,
604 0, 32, ARMSOC_BO_SCANOUT);
Rob Clark687c6082012-01-08 19:33:18 -0600605
Ray Smith003cf5e2013-03-19 10:44:07 +0000606 if (!cursor->bo) {
Dave Barnishfd50b132013-05-16 15:00:02 +0100607 ERROR_MSG("HW cursor: buffer allocation failed");
Ray Smith003cf5e2013-03-19 10:44:07 +0000608 free(cursor);
609 drmModeFreePlane(ovr);
610 drmModeFreePlaneResources(plane_resources);
611 return FALSE;
612 }
613
Paul Geary8ffd91c2013-04-11 16:03:15 +0100614 handles[0] = armsoc_bo_handle(cursor->bo);
615 pitches[0] = armsoc_bo_pitch(cursor->bo);
Rob Clark687c6082012-01-08 19:33:18 -0600616 offsets[0] = 0;
617
Dave Barnishde45ed42013-06-05 13:47:56 +0100618 /* allow for cursor padding in the fb */
619 if (drmModeAddFB2(drmmode->fd, w + 2 * pad, h, DRM_FORMAT_ARGB8888,
Rob Clark687c6082012-01-08 19:33:18 -0600620 handles, pitches, offsets, &cursor->fb_id, 0)) {
Dave Barnish2e998952013-06-11 16:31:10 +0100621 ERROR_MSG("HW cursor: drmModeAddFB2 failed: %s",
622 strerror(errno));
Paul Geary8ffd91c2013-04-11 16:03:15 +0100623 armsoc_bo_unreference(cursor->bo);
Ray Smith003cf5e2013-03-19 10:44:07 +0000624 free(cursor);
625 drmModeFreePlane(ovr);
626 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600627 return FALSE;
628 }
629
Ray Smith003cf5e2013-03-19 10:44:07 +0000630 if (!xf86_cursors_init(pScreen, w, h, HARDWARE_CURSOR_ARGB)) {
631 ERROR_MSG("xf86_cursors_init() failed");
Dave Barnish2e998952013-06-11 16:31:10 +0100632 if (drmModeRmFB(drmmode->fd, cursor->fb_id))
Ray Smith003cf5e2013-03-19 10:44:07 +0000633 ERROR_MSG("drmModeRmFB() failed");
Dave Barnish2e998952013-06-11 16:31:10 +0100634
Paul Geary8ffd91c2013-04-11 16:03:15 +0100635 armsoc_bo_unreference(cursor->bo);
Ray Smith003cf5e2013-03-19 10:44:07 +0000636 free(cursor);
637 drmModeFreePlane(ovr);
638 drmModeFreePlaneResources(plane_resources);
639 return FALSE;
Rob Clark687c6082012-01-08 19:33:18 -0600640 }
641
Ray Smith003cf5e2013-03-19 10:44:07 +0000642 INFO_MSG("HW cursor initialized");
643 drmmode->cursor = cursor;
Dave Barnishfd50b132013-05-16 15:00:02 +0100644 drmModeFreePlaneResources(plane_resources);
Ray Smith003cf5e2013-03-19 10:44:07 +0000645 return TRUE;
Rob Clark74210d52012-01-08 17:59:08 -0600646}
Rob Clark487687e2011-07-17 17:29:02 -0500647
Javier Martin4c572792013-09-11 15:08:19 +0100648static Bool
649drmmode_cursor_init_standard(ScreenPtr pScreen)
650{
651 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
652 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
653 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
654 struct drmmode_cursor_rec *cursor;
655 int w, h, pad;
656
657 if (drmmode->cursor) {
658 INFO_MSG("cursor already initialized");
659 return TRUE;
660 }
661
662 if (!xf86LoaderCheckSymbol("drmModeSetCursor") ||
663 !xf86LoaderCheckSymbol("drmModeMoveCursor")) {
664 ERROR_MSG("standard HW cursor not supported "
665 "(needs libdrm 2.4.3 or higher)");
666 return FALSE;
667 }
668
669 cursor = calloc(1, sizeof(struct drmmode_cursor_rec));
670 if (!cursor) {
671 ERROR_MSG("HW cursor (standard): calloc failed");
672 return FALSE;
673 }
674
675 w = pARMSOC->drmmode_interface->cursor_width;
676 h = pARMSOC->drmmode_interface->cursor_height;
677 pad = pARMSOC->drmmode_interface->cursor_padding;
678
679 /* allow for cursor padding in the bo */
680 cursor->bo = armsoc_bo_new_with_dim(pARMSOC->dev,
681 w + 2 * pad, h,
682 0, 32, ARMSOC_BO_SCANOUT);
683
684 if (!cursor->bo) {
685 ERROR_MSG("HW cursor (standard): buffer allocation failed");
686 free(cursor);
687 return FALSE;
688 }
689
690 cursor->handle = armsoc_bo_handle(cursor->bo);
691
692 if (!xf86_cursors_init(pScreen, w, h, HARDWARE_CURSOR_ARGB)) {
693 ERROR_MSG("xf86_cursors_init() failed");
694 if (drmModeRmFB(drmmode->fd, cursor->fb_id))
695 ERROR_MSG("drmModeRmFB() failed");
696
697 armsoc_bo_unreference(cursor->bo);
698 free(cursor);
699 return FALSE;
700 }
701
702 INFO_MSG("HW cursor initialized");
703 drmmode->cursor = cursor;
704 return TRUE;
705}
706
707Bool drmmode_cursor_init(ScreenPtr pScreen)
708{
709 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
710 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
711
712 INFO_MSG("HW cursor init()");
713
714 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE)
715 return drmmode_cursor_init_plane(pScreen);
716 else /* HWCURSOR_API_STANDARD */
717 return drmmode_cursor_init_standard(pScreen);
718}
719
Dave Barnishfd50b132013-05-16 15:00:02 +0100720void drmmode_cursor_fini(ScreenPtr pScreen)
721{
Daniel Kurtz0361e002013-08-14 21:07:01 +0800722 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Dave Barnish2e998952013-06-11 16:31:10 +0100723 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
724 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Javier Martin4c572792013-09-11 15:08:19 +0100725 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Dave Barnishfd50b132013-05-16 15:00:02 +0100726
Dave Barnish2e998952013-06-11 16:31:10 +0100727 if (!cursor)
Dave Barnishfd50b132013-05-16 15:00:02 +0100728 return;
Dave Barnish2e998952013-06-11 16:31:10 +0100729
Dave Barnishfd50b132013-05-16 15:00:02 +0100730 drmmode->cursor = NULL;
731 xf86_cursors_fini(pScreen);
Javier Martin4c572792013-09-11 15:08:19 +0100732 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE)
733 drmModeRmFB(drmmode->fd, cursor->fb_id);
Dave Barnishfd50b132013-05-16 15:00:02 +0100734 armsoc_bo_unreference(cursor->bo);
Javier Martin4c572792013-09-11 15:08:19 +0100735 if (pARMSOC->drmmode_interface->cursor_api == HWCURSOR_API_PLANE)
736 drmModeFreePlane(cursor->ovr);
Dave Barnishfd50b132013-05-16 15:00:02 +0100737 free(cursor);
738}
739
740
Dave Barnish2e998952013-06-11 16:31:10 +0100741#if 1 == ARMSOC_SUPPORT_GAMMA
Rob Clark487687e2011-07-17 17:29:02 -0500742static void
743drmmode_gamma_set(xf86CrtcPtr crtc, CARD16 *red, CARD16 *green, CARD16 *blue,
Rob Clark74210d52012-01-08 17:59:08 -0600744 int size)
Rob Clark487687e2011-07-17 17:29:02 -0500745{
Dave Barnish2e998952013-06-11 16:31:10 +0100746 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
747 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500748 int ret;
749
750 ret = drmModeCrtcSetGamma(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id,
Rob Clark74210d52012-01-08 17:59:08 -0600751 size, red, green, blue);
Rob Clark487687e2011-07-17 17:29:02 -0500752 if (ret != 0) {
753 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
Rob Clark74210d52012-01-08 17:59:08 -0600754 "failed to set gamma: %s\n", strerror(-ret));
Rob Clark487687e2011-07-17 17:29:02 -0500755 }
756}
Mandeep Singh Bainesc874fcb2012-09-13 15:30:00 +0200757#endif
Rob Clark487687e2011-07-17 17:29:02 -0500758
759static const xf86CrtcFuncsRec drmmode_crtc_funcs = {
Rob Clark74210d52012-01-08 17:59:08 -0600760 .dpms = drmmode_crtc_dpms,
761 .set_mode_major = drmmode_set_mode_major,
762 .set_cursor_position = drmmode_set_cursor_position,
763 .show_cursor = drmmode_show_cursor,
764 .hide_cursor = drmmode_hide_cursor,
765 .load_cursor_argb = drmmode_load_cursor_argb,
Dave Barnish2e998952013-06-11 16:31:10 +0100766#if 1 == ARMSOC_SUPPORT_GAMMA
Rob Clark74210d52012-01-08 17:59:08 -0600767 .gamma_set = drmmode_gamma_set,
Mandeep Singh Bainesc874fcb2012-09-13 15:30:00 +0200768#endif
Rob Clark487687e2011-07-17 17:29:02 -0500769};
770
771
772static void
Dave Barnish2e998952013-06-11 16:31:10 +0100773drmmode_crtc_init(ScrnInfoPtr pScrn, struct drmmode_rec *drmmode, int num)
Rob Clark487687e2011-07-17 17:29:02 -0500774{
Rob Clark74210d52012-01-08 17:59:08 -0600775 xf86CrtcPtr crtc;
Dave Barnish2e998952013-06-11 16:31:10 +0100776 struct drmmode_crtc_private_rec *drmmode_crtc;
Rob Clark487687e2011-07-17 17:29:02 -0500777
Rob Clark74210d52012-01-08 17:59:08 -0600778 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -0500779
Rob Clark74210d52012-01-08 17:59:08 -0600780 crtc = xf86CrtcCreate(pScrn, &drmmode_crtc_funcs);
781 if (crtc == NULL)
782 return;
783
Dave Barnish2e998952013-06-11 16:31:10 +0100784 drmmode_crtc = xnfcalloc(sizeof(struct drmmode_crtc_private_rec), 1);
Rob Clark74210d52012-01-08 17:59:08 -0600785 drmmode_crtc->mode_crtc = drmModeGetCrtc(drmmode->fd,
786 drmmode->mode_res->crtcs[num]);
787 drmmode_crtc->drmmode = drmmode;
Dave Barnishf245da32013-08-30 12:16:58 +0100788 drmmode_crtc->last_good_mode = NULL;
Rob Clark74210d52012-01-08 17:59:08 -0600789
Dave Barnishf245da32013-08-30 12:16:58 +0100790 INFO_MSG("Got CRTC: %d (id: %d)",
791 num, drmmode_crtc->mode_crtc->crtc_id);
Rob Clark74210d52012-01-08 17:59:08 -0600792 crtc->driver_private = drmmode_crtc;
793
794 TRACE_EXIT();
Rob Clark487687e2011-07-17 17:29:02 -0500795 return;
Rob Clark74210d52012-01-08 17:59:08 -0600796}
Rob Clark487687e2011-07-17 17:29:02 -0500797
798static xf86OutputStatus
799drmmode_output_detect(xf86OutputPtr output)
800{
801 /* go to the hw and retrieve a new output struct */
Dave Barnish2e998952013-06-11 16:31:10 +0100802 struct drmmode_output_priv *drmmode_output = output->driver_private;
803 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500804 xf86OutputStatus status;
805 drmModeFreeConnector(drmmode_output->mode_output);
806
807 drmmode_output->mode_output =
Dave Barnish2e998952013-06-11 16:31:10 +0100808 drmModeGetConnector(drmmode->fd,
809 drmmode_output->output_id);
Rob Clark487687e2011-07-17 17:29:02 -0500810
811 switch (drmmode_output->mode_output->connection) {
812 case DRM_MODE_CONNECTED:
813 status = XF86OutputStatusConnected;
814 break;
815 case DRM_MODE_DISCONNECTED:
816 status = XF86OutputStatusDisconnected;
817 break;
818 default:
819 case DRM_MODE_UNKNOWNCONNECTION:
820 status = XF86OutputStatusUnknown;
821 break;
822 }
823 return status;
824}
825
826static Bool
827drmmode_output_mode_valid(xf86OutputPtr output, DisplayModePtr mode)
828{
829 if (mode->type & M_T_DEFAULT)
830 /* Default modes are harmful here. */
831 return MODE_BAD;
832
833 return MODE_OK;
834}
835
836static DisplayModePtr
837drmmode_output_get_modes(xf86OutputPtr output)
838{
839 ScrnInfoPtr pScrn = output->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100840 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500841 drmModeConnectorPtr koutput = drmmode_output->mode_output;
Dave Barnish2e998952013-06-11 16:31:10 +0100842 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Dave Barnish0bdb7382013-06-06 12:04:04 +0100843 DisplayModePtr modes = NULL;
Daniel Kurtzb8131892012-10-17 01:39:54 +0800844 drmModePropertyPtr prop;
Rob Clark487687e2011-07-17 17:29:02 -0500845 xf86MonPtr ddc_mon = NULL;
846 int i;
847
848 /* look for an EDID property */
849 for (i = 0; i < koutput->count_props; i++) {
Daniel Kurtzb8131892012-10-17 01:39:54 +0800850 prop = drmModeGetProperty(drmmode->fd, koutput->props[i]);
851 if (!prop)
Rob Clark487687e2011-07-17 17:29:02 -0500852 continue;
853
Daniel Kurtzb8131892012-10-17 01:39:54 +0800854 if ((prop->flags & DRM_MODE_PROP_BLOB) &&
855 !strcmp(prop->name, "EDID")) {
Rob Clark487687e2011-07-17 17:29:02 -0500856 if (drmmode_output->edid_blob)
Dave Barnish2e998952013-06-11 16:31:10 +0100857 drmModeFreePropertyBlob(
858 drmmode_output->edid_blob);
Rob Clark487687e2011-07-17 17:29:02 -0500859 drmmode_output->edid_blob =
Rob Clark74210d52012-01-08 17:59:08 -0600860 drmModeGetPropertyBlob(drmmode->fd,
Dave Barnish2e998952013-06-11 16:31:10 +0100861 koutput->prop_values[i]);
Rob Clark487687e2011-07-17 17:29:02 -0500862 }
Daniel Kurtzb8131892012-10-17 01:39:54 +0800863 drmModeFreeProperty(prop);
Rob Clark487687e2011-07-17 17:29:02 -0500864 }
865
866 if (drmmode_output->edid_blob)
867 ddc_mon = xf86InterpretEDID(pScrn->scrnIndex,
Rob Clark74210d52012-01-08 17:59:08 -0600868 drmmode_output->edid_blob->data);
Rob Clark487687e2011-07-17 17:29:02 -0500869
870 if (ddc_mon) {
Rob Clark487687e2011-07-17 17:29:02 -0500871 xf86OutputSetEDID(output, ddc_mon);
872 xf86SetDDCproperties(pScrn, ddc_mon);
873 }
874
875 DEBUG_MSG("count_modes: %d", koutput->count_modes);
876
877 /* modes should already be available */
878 for (i = 0; i < koutput->count_modes; i++) {
Dave Barnish0bdb7382013-06-06 12:04:04 +0100879 DisplayModePtr mode = xnfalloc(sizeof(DisplayModeRec));
Rob Clark487687e2011-07-17 17:29:02 -0500880
Dave Barnish0bdb7382013-06-06 12:04:04 +0100881 drmmode_ConvertFromKMode(pScrn, &koutput->modes[i], mode);
882 modes = xf86ModesAdd(modes, mode);
Rob Clark487687e2011-07-17 17:29:02 -0500883 }
Dave Barnish0bdb7382013-06-06 12:04:04 +0100884 return modes;
Rob Clark487687e2011-07-17 17:29:02 -0500885}
886
887static void
888drmmode_output_destroy(xf86OutputPtr output)
889{
Dave Barnish2e998952013-06-11 16:31:10 +0100890 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500891 int i;
892
893 if (drmmode_output->edid_blob)
894 drmModeFreePropertyBlob(drmmode_output->edid_blob);
895 for (i = 0; i < drmmode_output->num_props; i++) {
896 drmModeFreeProperty(drmmode_output->props[i].mode_prop);
897 free(drmmode_output->props[i].atoms);
898 }
Daniel Kurtzf5a38ad2012-10-17 02:03:34 +0800899 free(drmmode_output->props);
Rob Clark487687e2011-07-17 17:29:02 -0500900 drmModeFreeConnector(drmmode_output->mode_output);
901 free(drmmode_output);
902 output->driver_private = NULL;
903}
904
905static void
906drmmode_output_dpms(xf86OutputPtr output, int mode)
907{
Dave Barnish2e998952013-06-11 16:31:10 +0100908 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500909 drmModeConnectorPtr koutput = drmmode_output->mode_output;
Daniel Kurtzb8131892012-10-17 01:39:54 +0800910 drmModePropertyPtr prop;
Dave Barnish2e998952013-06-11 16:31:10 +0100911 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500912 int mode_id = -1, i;
913
914 for (i = 0; i < koutput->count_props; i++) {
Daniel Kurtzb8131892012-10-17 01:39:54 +0800915 prop = drmModeGetProperty(drmmode->fd, koutput->props[i]);
916 if (!prop)
917 continue;
918 if ((prop->flags & DRM_MODE_PROP_ENUM) &&
919 !strcmp(prop->name, "DPMS")) {
920 mode_id = koutput->props[i];
921 drmModeFreeProperty(prop);
922 break;
Rob Clark487687e2011-07-17 17:29:02 -0500923 }
Daniel Kurtzb8131892012-10-17 01:39:54 +0800924 drmModeFreeProperty(prop);
Rob Clark487687e2011-07-17 17:29:02 -0500925 }
926
927 if (mode_id < 0)
928 return;
929
930 drmModeConnectorSetProperty(drmmode->fd, koutput->connector_id,
Rob Clark74210d52012-01-08 17:59:08 -0600931 mode_id, mode);
Rob Clark487687e2011-07-17 17:29:02 -0500932}
933
934static Bool
935drmmode_property_ignore(drmModePropertyPtr prop)
936{
937 if (!prop)
Rob Clark74210d52012-01-08 17:59:08 -0600938 return TRUE;
Rob Clark487687e2011-07-17 17:29:02 -0500939 /* ignore blob prop */
940 if (prop->flags & DRM_MODE_PROP_BLOB)
941 return TRUE;
942 /* ignore standard property */
943 if (!strcmp(prop->name, "EDID") ||
Rob Clark74210d52012-01-08 17:59:08 -0600944 !strcmp(prop->name, "DPMS"))
Rob Clark487687e2011-07-17 17:29:02 -0500945 return TRUE;
946
947 return FALSE;
948}
949
950static void
951drmmode_output_create_resources(xf86OutputPtr output)
952{
Dave Barnish2e998952013-06-11 16:31:10 +0100953 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500954 drmModeConnectorPtr mode_output = drmmode_output->mode_output;
Dave Barnish2e998952013-06-11 16:31:10 +0100955 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500956 drmModePropertyPtr drmmode_prop;
957 uint32_t value;
958 int i, j, err;
959
Dave Barnish2e998952013-06-11 16:31:10 +0100960 drmmode_output->props =
961 calloc(mode_output->count_props,
962 sizeof(struct drmmode_prop_rec));
Rob Clark487687e2011-07-17 17:29:02 -0500963 if (!drmmode_output->props)
964 return;
965
966 drmmode_output->num_props = 0;
Dave Barnish0bdb7382013-06-06 12:04:04 +0100967 for (i = 0; i < mode_output->count_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +0100968 drmmode_prop = drmModeGetProperty(drmmode->fd,
969 mode_output->props[i]);
Rob Clark487687e2011-07-17 17:29:02 -0500970 if (drmmode_property_ignore(drmmode_prop)) {
971 drmModeFreeProperty(drmmode_prop);
972 continue;
973 }
Dave Barnish2e998952013-06-11 16:31:10 +0100974 drmmode_output->props[drmmode_output->num_props].mode_prop =
975 drmmode_prop;
Dave Barnish0bdb7382013-06-06 12:04:04 +0100976 drmmode_output->props[drmmode_output->num_props].index = i;
Rob Clark487687e2011-07-17 17:29:02 -0500977 drmmode_output->num_props++;
Rob Clark487687e2011-07-17 17:29:02 -0500978 }
979
980 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +0100981 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -0500982 drmmode_prop = p->mode_prop;
983
984 value = drmmode_output->mode_output->prop_values[p->index];
985
986 if (drmmode_prop->flags & DRM_MODE_PROP_RANGE) {
987 INT32 range[2];
988
989 p->num_atoms = 1;
990 p->atoms = calloc(p->num_atoms, sizeof(Atom));
991 if (!p->atoms)
992 continue;
Dave Barnish2e998952013-06-11 16:31:10 +0100993 p->atoms[0] = MakeAtom(drmmode_prop->name,
994 strlen(drmmode_prop->name),
995 TRUE);
Rob Clark487687e2011-07-17 17:29:02 -0500996 range[0] = drmmode_prop->values[0];
997 range[1] = drmmode_prop->values[1];
Dave Barnish2e998952013-06-11 16:31:10 +0100998 err = RRConfigureOutputProperty(output->randr_output,
999 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -06001000 FALSE, TRUE,
Dave Barnish2e998952013-06-11 16:31:10 +01001001 drmmode_prop->flags &
1002 DRM_MODE_PROP_IMMUTABLE ?
1003 TRUE : FALSE,
1004 2, range);
1005
1006 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001007 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001008 "RRConfigureOutputProperty error, %d\n",
1009 err);
1010
1011 err = RRChangeOutputProperty(output->randr_output,
1012 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -06001013 XA_INTEGER, 32, PropModeReplace, 1,
1014 &value, FALSE, FALSE);
Dave Barnish2e998952013-06-11 16:31:10 +01001015 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001016 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001017 "RRChangeOutputProperty error, %d\n",
1018 err);
1019
Rob Clark487687e2011-07-17 17:29:02 -05001020 } else if (drmmode_prop->flags & DRM_MODE_PROP_ENUM) {
1021 p->num_atoms = drmmode_prop->count_enums + 1;
1022 p->atoms = calloc(p->num_atoms, sizeof(Atom));
1023 if (!p->atoms)
1024 continue;
Dave Barnish2e998952013-06-11 16:31:10 +01001025 p->atoms[0] = MakeAtom(drmmode_prop->name,
1026 strlen(drmmode_prop->name),
1027 TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001028 for (j = 1; j <= drmmode_prop->count_enums; j++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001029 struct drm_mode_property_enum *e =
1030 &drmmode_prop->enums[j-1];
1031 p->atoms[j] = MakeAtom(e->name,
1032 strlen(e->name), TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001033 }
Dave Barnish2e998952013-06-11 16:31:10 +01001034 err = RRConfigureOutputProperty(output->randr_output,
1035 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -06001036 FALSE, FALSE,
Dave Barnish2e998952013-06-11 16:31:10 +01001037 drmmode_prop->flags &
1038 DRM_MODE_PROP_IMMUTABLE ?
1039 TRUE : FALSE,
1040 p->num_atoms - 1,
1041 (INT32 *)&p->atoms[1]);
1042
1043 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001044 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001045 "RRConfigureOutputProperty error, %d\n",
1046 err);
1047
Rob Clark487687e2011-07-17 17:29:02 -05001048 for (j = 0; j < drmmode_prop->count_enums; j++)
1049 if (drmmode_prop->enums[j].value == value)
1050 break;
1051 /* there's always a matching value */
Dave Barnish2e998952013-06-11 16:31:10 +01001052 err = RRChangeOutputProperty(output->randr_output,
1053 p->atoms[0],
1054 XA_ATOM, 32, PropModeReplace, 1,
1055 &p->atoms[j+1], FALSE, FALSE);
1056 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -05001057 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +01001058 "RRChangeOutputProperty error, %d\n",
1059 err);
Rob Clark487687e2011-07-17 17:29:02 -05001060 }
1061 }
1062}
1063
1064static Bool
1065drmmode_output_set_property(xf86OutputPtr output, Atom property,
Rob Clark74210d52012-01-08 17:59:08 -06001066 RRPropertyValuePtr value)
Rob Clark487687e2011-07-17 17:29:02 -05001067{
Dave Barnish2e998952013-06-11 16:31:10 +01001068 struct drmmode_output_priv *drmmode_output = output->driver_private;
1069 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001070 int i, ret;
1071
1072 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001073 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -05001074
1075 if (p->atoms[0] != property)
1076 continue;
1077
1078 if (p->mode_prop->flags & DRM_MODE_PROP_RANGE) {
1079 uint32_t val;
1080
1081 if (value->type != XA_INTEGER || value->format != 32 ||
Rob Clark74210d52012-01-08 17:59:08 -06001082 value->size != 1)
Rob Clark487687e2011-07-17 17:29:02 -05001083 return FALSE;
1084 val = *(uint32_t *)value->data;
1085
Dave Barnish2e998952013-06-11 16:31:10 +01001086 ret = drmModeConnectorSetProperty(drmmode->fd,
1087 drmmode_output->output_id,
Rob Clark74210d52012-01-08 17:59:08 -06001088 p->mode_prop->prop_id, (uint64_t)val);
Rob Clark487687e2011-07-17 17:29:02 -05001089
1090 if (ret)
1091 return FALSE;
1092
1093 return TRUE;
1094
1095 } else if (p->mode_prop->flags & DRM_MODE_PROP_ENUM) {
1096 Atom atom;
1097 const char *name;
1098 int j;
1099
Dave Barnish2e998952013-06-11 16:31:10 +01001100 if (value->type != XA_ATOM ||
1101 value->format != 32 ||
1102 value->size != 1)
Rob Clark487687e2011-07-17 17:29:02 -05001103 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +01001104
Rob Clark487687e2011-07-17 17:29:02 -05001105 memcpy(&atom, value->data, 4);
1106 name = NameForAtom(atom);
Daniel Kurtz23148372013-03-27 20:19:45 +08001107 if (name == NULL)
1108 return FALSE;
Rob Clark487687e2011-07-17 17:29:02 -05001109
Dave Barnish2e998952013-06-11 16:31:10 +01001110 /* search for matching name string, then
1111 * set its value down
1112 */
Rob Clark487687e2011-07-17 17:29:02 -05001113 for (j = 0; j < p->mode_prop->count_enums; j++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001114 if (!strcmp(p->mode_prop->enums[j].name,
1115 name)) {
1116 ret = drmModeConnectorSetProperty(
1117 drmmode->fd,
1118 drmmode_output->output_id,
1119 p->mode_prop->prop_id,
1120 p->mode_prop->enums[j].value);
Rob Clark487687e2011-07-17 17:29:02 -05001121
1122 if (ret)
1123 return FALSE;
1124
1125 return TRUE;
1126 }
1127 }
Rob Clark487687e2011-07-17 17:29:02 -05001128 return FALSE;
1129 }
1130 }
Rob Clark487687e2011-07-17 17:29:02 -05001131 return TRUE;
1132}
1133
1134static Bool
1135drmmode_output_get_property(xf86OutputPtr output, Atom property)
1136{
1137
Dave Barnish2e998952013-06-11 16:31:10 +01001138 struct drmmode_output_priv *drmmode_output = output->driver_private;
1139 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001140 uint32_t value;
1141 int err, i;
1142
1143 if (output->scrn->vtSema) {
1144 drmModeFreeConnector(drmmode_output->mode_output);
1145 drmmode_output->mode_output =
Dave Barnish2e998952013-06-11 16:31:10 +01001146 drmModeGetConnector(drmmode->fd,
1147 drmmode_output->output_id);
Rob Clark487687e2011-07-17 17:29:02 -05001148 }
1149
1150 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001151 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -05001152 if (p->atoms[0] != property)
1153 continue;
1154
1155 value = drmmode_output->mode_output->prop_values[p->index];
1156
1157 if (p->mode_prop->flags & DRM_MODE_PROP_RANGE) {
1158 err = RRChangeOutputProperty(output->randr_output,
Rob Clark74210d52012-01-08 17:59:08 -06001159 property, XA_INTEGER, 32,
1160 PropModeReplace, 1, &value,
1161 FALSE, FALSE);
Rob Clark487687e2011-07-17 17:29:02 -05001162
1163 return !err;
1164 } else if (p->mode_prop->flags & DRM_MODE_PROP_ENUM) {
1165 int j;
1166
Dave Barnish2e998952013-06-11 16:31:10 +01001167 /* search for matching name string, then set
1168 * its value down
1169 */
Rob Clark487687e2011-07-17 17:29:02 -05001170 for (j = 0; j < p->mode_prop->count_enums; j++) {
1171 if (p->mode_prop->enums[j].value == value)
1172 break;
1173 }
1174
Dave Barnish2e998952013-06-11 16:31:10 +01001175 err = RRChangeOutputProperty(output->randr_output,
1176 property,
1177 XA_ATOM, 32, PropModeReplace, 1,
1178 &p->atoms[j+1], FALSE, FALSE);
Rob Clark487687e2011-07-17 17:29:02 -05001179
1180 return !err;
1181 }
1182 }
1183
1184 return FALSE;
1185}
1186
1187static const xf86OutputFuncsRec drmmode_output_funcs = {
Rob Clark74210d52012-01-08 17:59:08 -06001188 .create_resources = drmmode_output_create_resources,
1189 .dpms = drmmode_output_dpms,
1190 .detect = drmmode_output_detect,
1191 .mode_valid = drmmode_output_mode_valid,
1192 .get_modes = drmmode_output_get_modes,
1193 .set_property = drmmode_output_set_property,
1194 .get_property = drmmode_output_get_property,
1195 .destroy = drmmode_output_destroy
Rob Clark487687e2011-07-17 17:29:02 -05001196};
1197
Rob Clark487687e2011-07-17 17:29:02 -05001198const char *output_names[] = { "None",
Rob Clark74210d52012-01-08 17:59:08 -06001199 "VGA",
1200 "DVI-I",
1201 "DVI-D",
1202 "DVI-A",
1203 "Composite",
1204 "SVIDEO",
1205 "LVDS",
1206 "CTV",
1207 "DIN",
1208 "DP",
1209 "HDMI",
1210 "HDMI",
1211 "TV",
1212 "eDP",
Rob Clark487687e2011-07-17 17:29:02 -05001213};
1214#define NUM_OUTPUT_NAMES (sizeof(output_names) / sizeof(output_names[0]))
1215
1216static void
Dave Barnish2e998952013-06-11 16:31:10 +01001217drmmode_output_init(ScrnInfoPtr pScrn, struct drmmode_rec *drmmode, int num)
Rob Clark487687e2011-07-17 17:29:02 -05001218{
Rob Clark74210d52012-01-08 17:59:08 -06001219 xf86OutputPtr output;
1220 drmModeConnectorPtr koutput;
1221 drmModeEncoderPtr kencoder;
Dave Barnish2e998952013-06-11 16:31:10 +01001222 struct drmmode_output_priv *drmmode_output;
Rob Clark74210d52012-01-08 17:59:08 -06001223 char name[32];
Rob Clark487687e2011-07-17 17:29:02 -05001224
Rob Clark74210d52012-01-08 17:59:08 -06001225 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001226
Rob Clark74210d52012-01-08 17:59:08 -06001227 koutput = drmModeGetConnector(drmmode->fd,
1228 drmmode->mode_res->connectors[num]);
1229 if (!koutput)
1230 return;
Rob Clark487687e2011-07-17 17:29:02 -05001231
Rob Clark74210d52012-01-08 17:59:08 -06001232 kencoder = drmModeGetEncoder(drmmode->fd, koutput->encoders[0]);
1233 if (!kencoder) {
1234 drmModeFreeConnector(koutput);
1235 return;
1236 }
Rob Clark487687e2011-07-17 17:29:02 -05001237
Rob Clark74210d52012-01-08 17:59:08 -06001238 if (koutput->connector_type >= NUM_OUTPUT_NAMES)
1239 snprintf(name, 32, "Unknown%d-%d", koutput->connector_type,
1240 koutput->connector_type_id);
1241 else
1242 snprintf(name, 32, "%s-%d",
1243 output_names[koutput->connector_type],
1244 koutput->connector_type_id);
Rob Clark487687e2011-07-17 17:29:02 -05001245
Rob Clark74210d52012-01-08 17:59:08 -06001246 output = xf86OutputCreate(pScrn, &drmmode_output_funcs, name);
1247 if (!output) {
1248 drmModeFreeEncoder(kencoder);
1249 drmModeFreeConnector(koutput);
1250 return;
1251 }
Rob Clark487687e2011-07-17 17:29:02 -05001252
Dave Barnish2e998952013-06-11 16:31:10 +01001253 drmmode_output = calloc(sizeof(struct drmmode_output_priv), 1);
Rob Clark74210d52012-01-08 17:59:08 -06001254 if (!drmmode_output) {
1255 xf86OutputDestroy(output);
1256 drmModeFreeConnector(koutput);
1257 drmModeFreeEncoder(kencoder);
1258 return;
1259 }
Rob Clark487687e2011-07-17 17:29:02 -05001260
Rob Clark74210d52012-01-08 17:59:08 -06001261 drmmode_output->output_id = drmmode->mode_res->connectors[num];
1262 drmmode_output->mode_output = koutput;
1263 drmmode_output->mode_encoder = kencoder;
1264 drmmode_output->drmmode = drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001265
Rob Clark74210d52012-01-08 17:59:08 -06001266 output->mm_width = koutput->mmWidth;
1267 output->mm_height = koutput->mmHeight;
1268 output->driver_private = drmmode_output;
Rob Clark487687e2011-07-17 17:29:02 -05001269
Dave Barnishf0952c12013-05-10 15:52:23 +01001270 if (ARMSOCPTR(pScrn)->crtcNum >= 0) {
1271 /* Only single crtc per screen - see if this output can use it*/
1272 output->possible_crtcs =
Dave Barnish2e998952013-06-11 16:31:10 +01001273 (kencoder->possible_crtcs >>
1274 (ARMSOCPTR(pScrn)->crtcNum)
1275 )&1;
1276 } else
Dave Barnishf0952c12013-05-10 15:52:23 +01001277 output->possible_crtcs = kencoder->possible_crtcs;
Dave Barnishf0952c12013-05-10 15:52:23 +01001278
Rob Clark74210d52012-01-08 17:59:08 -06001279 output->possible_clones = kencoder->possible_clones;
1280 output->interlaceAllowed = TRUE;
Rob Clark487687e2011-07-17 17:29:02 -05001281
Rob Clark74210d52012-01-08 17:59:08 -06001282 TRACE_EXIT();
1283 return;
1284}
Rob Clark487687e2011-07-17 17:29:02 -05001285
Paul Geary8ffd91c2013-04-11 16:03:15 +01001286void set_scanout_bo(ScrnInfoPtr pScrn, struct armsoc_bo *bo)
David Garbett3688b332012-05-11 12:17:34 +01001287{
Dave Barnish2e998952013-06-11 16:31:10 +01001288 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
David Garbett3688b332012-05-11 12:17:34 +01001289
1290 /* It had better have a framebuffer if we're scanning it out */
Paul Geary8ffd91c2013-04-11 16:03:15 +01001291 assert(armsoc_bo_get_fb(bo));
David Garbett3688b332012-05-11 12:17:34 +01001292
Paul Geary8ffd91c2013-04-11 16:03:15 +01001293 pARMSOC->scanout = bo;
David Garbett3688b332012-05-11 12:17:34 +01001294}
1295
Dave Barnishf245da32013-08-30 12:16:58 +01001296static Bool resize_scanout_bo(ScrnInfoPtr pScrn, int width, int height)
Rob Clark487687e2011-07-17 17:29:02 -05001297{
Dave Barnish2e998952013-06-11 16:31:10 +01001298 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001299 ScreenPtr pScreen = pScrn->pScreen;
Sean Paul3e107302012-08-30 12:08:12 -07001300 uint32_t pitch;
Rob Clark487687e2011-07-17 17:29:02 -05001301
1302 TRACE_ENTER();
Dave Barnish33866052013-03-18 10:57:40 +00001303 DEBUG_MSG("Resize: %dx%d", width, height);
Rob Clark487687e2011-07-17 17:29:02 -05001304
1305 pScrn->virtualX = width;
1306 pScrn->virtualY = height;
1307
Dave Barnish2e998952013-06-11 16:31:10 +01001308 if ((width != armsoc_bo_width(pARMSOC->scanout))
Paul Geary8ffd91c2013-04-11 16:03:15 +01001309 || (height != armsoc_bo_height(pARMSOC->scanout))
Dave Barnish2e998952013-06-11 16:31:10 +01001310 || (pScrn->bitsPerPixel != armsoc_bo_bpp(pARMSOC->scanout))) {
Dave Barnish33866052013-03-18 10:57:40 +00001311 struct armsoc_bo *new_scanout;
Rob Clark487687e2011-07-17 17:29:02 -05001312
1313 /* allocate new scanout buffer */
Dave Barnish2e998952013-06-11 16:31:10 +01001314 new_scanout = armsoc_bo_new_with_dim(pARMSOC->dev,
1315 width, height,
1316 pScrn->depth, pScrn->bitsPerPixel,
1317 ARMSOC_BO_SCANOUT);
1318 if (!new_scanout) {
1319 /* Try to use the previous buffer if the new resolution
1320 * is smaller than the one on buffer creation
1321 */
1322 DEBUG_MSG(
1323 "allocate new scanout buffer failed - resizing existing bo");
Dave Barnish33866052013-03-18 10:57:40 +00001324 /* Remove the old fb from the bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001325 if (armsoc_bo_rm_fb(pARMSOC->scanout))
Dave Barnish33866052013-03-18 10:57:40 +00001326 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +01001327
Dave Barnish33866052013-03-18 10:57:40 +00001328 /* Resize the bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001329 if (armsoc_bo_resize(pARMSOC->scanout, width, height)) {
Dave Barnish33866052013-03-18 10:57:40 +00001330 armsoc_bo_clear(pARMSOC->scanout);
Ray Smithb4299f82013-03-13 10:08:36 +00001331 if (armsoc_bo_add_fb(pARMSOC->scanout))
1332 ERROR_MSG(
1333 "Failed to add framebuffer to the existing scanout buffer");
Dave Barnish33866052013-03-18 10:57:40 +00001334 return FALSE;
1335 }
Ray Smithb4299f82013-03-13 10:08:36 +00001336
Dave Barnish33866052013-03-18 10:57:40 +00001337 /* Add new fb to the bo */
Ray Smithb4299f82013-03-13 10:08:36 +00001338 if (armsoc_bo_clear(pARMSOC->scanout))
Dave Barnish33866052013-03-18 10:57:40 +00001339 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +01001340
Ray Smithb4299f82013-03-13 10:08:36 +00001341 if (armsoc_bo_add_fb(pARMSOC->scanout)) {
1342 ERROR_MSG(
1343 "Failed to add framebuffer to the existing scanout buffer");
1344 return FALSE;
1345 }
1346
Dave Barnish33866052013-03-18 10:57:40 +00001347 pitch = armsoc_bo_pitch(pARMSOC->scanout);
Dave Barnish2e998952013-06-11 16:31:10 +01001348 } else {
Dave Barnish33866052013-03-18 10:57:40 +00001349 DEBUG_MSG("allocated new scanout buffer okay");
1350 pitch = armsoc_bo_pitch(new_scanout);
1351 /* clear new BO and add FB */
Ray Smithb4299f82013-03-13 10:08:36 +00001352 if (armsoc_bo_clear(new_scanout)) {
Paul Geary8ffd91c2013-04-11 16:03:15 +01001353 armsoc_bo_unreference(new_scanout);
David Garbettae5a6362012-07-02 10:15:47 +01001354 return FALSE;
1355 }
Ray Smithb4299f82013-03-13 10:08:36 +00001356
1357 if (armsoc_bo_add_fb(new_scanout)) {
1358 ERROR_MSG(
1359 "Failed to add framebuffer to the new scanout buffer");
1360 armsoc_bo_unreference(new_scanout);
1361 return FALSE;
1362 }
1363
Dave Barnish33866052013-03-18 10:57:40 +00001364 /* Handle dma_buf fd that may be attached to old bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001365 if (armsoc_bo_has_dmabuf(pARMSOC->scanout)) {
Dave Barnish33866052013-03-18 10:57:40 +00001366 int res;
1367
1368 armsoc_bo_clear_dmabuf(pARMSOC->scanout);
1369 res = armsoc_bo_set_dmabuf(new_scanout);
Dave Barnish2e998952013-06-11 16:31:10 +01001370 if (res) {
1371 ERROR_MSG(
1372 "Unable to attach dma_buf fd to new scanout buffer - %d (%s)\n",
1373 res, strerror(res));
Dave Barnish33866052013-03-18 10:57:40 +00001374 armsoc_bo_unreference(new_scanout);
1375 return FALSE;
1376 }
1377 }
1378 /* delete old scanout buffer */
1379 armsoc_bo_unreference(pARMSOC->scanout);
1380 /* use new scanout buffer */
1381 set_scanout_bo(pScrn, new_scanout);
David Garbettae5a6362012-07-02 10:15:47 +01001382 }
Daniel Kurtz34e72b02012-10-19 14:02:05 -07001383 pScrn->displayWidth = pitch / ((pScrn->bitsPerPixel + 7) / 8);
Dave Barnish2e998952013-06-11 16:31:10 +01001384 } else
Paul Geary8ffd91c2013-04-11 16:03:15 +01001385 pitch = armsoc_bo_pitch(pARMSOC->scanout);
Rob Clark487687e2011-07-17 17:29:02 -05001386
1387 if (pScreen && pScreen->ModifyPixmapHeader) {
1388 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
Dave Barnish33866052013-03-18 10:57:40 +00001389
Rob Clark487687e2011-07-17 17:29:02 -05001390 pScreen->ModifyPixmapHeader(rootPixmap,
1391 pScrn->virtualX, pScrn->virtualY,
1392 pScrn->depth, pScrn->bitsPerPixel, pitch,
Paul Geary8ffd91c2013-04-11 16:03:15 +01001393 armsoc_bo_map(pARMSOC->scanout));
Ray Smithedccfd82013-05-31 15:57:12 +01001394
Dave Barnishe762b122013-08-12 17:13:46 +01001395 /* Bump the serial number to ensure that all existing DRI2
1396 * buffers are invalidated.
Ray Smithedccfd82013-05-31 15:57:12 +01001397 *
Dave Barnishe762b122013-08-12 17:13:46 +01001398 * This is particularly required for when the resolution is
1399 * changed and then reverted to the original size without a
1400 * DRI2 client/s getting a new buffer. Without this, the
1401 * drawable is the same size and serial number so the old
1402 * DRI2Buffer will be returned, even though the backing buffer
1403 * has been deleted.
Ray Smithedccfd82013-05-31 15:57:12 +01001404 */
1405 rootPixmap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
Rob Clark487687e2011-07-17 17:29:02 -05001406 }
Dave Barnishf245da32013-08-30 12:16:58 +01001407 TRACE_EXIT();
1408 return TRUE;
1409}
1410
1411static Bool
1412drmmode_xf86crtc_resize(ScrnInfoPtr pScrn, int width, int height)
1413{
1414 int i;
1415 xf86CrtcConfigPtr xf86_config;
1416
1417 TRACE_ENTER();
1418
1419 if (!resize_scanout_bo(pScrn, width, height))
1420 return FALSE;
Rob Clark487687e2011-07-17 17:29:02 -05001421
Brian Starkeycd684422012-09-20 09:28:04 +01001422 /* Framebuffer needs to be reset on all CRTCs, not just
1423 * those that have repositioned */
1424 xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1425 for (i = 0; i < xf86_config->num_crtc; i++) {
1426 xf86CrtcPtr crtc = xf86_config->crtc[i];
1427
1428 if (!crtc->enabled)
1429 continue;
1430
1431 drmmode_set_mode_major(crtc, &crtc->mode,
1432 crtc->rotation, crtc->x, crtc->y);
1433 }
1434
Rob Clark487687e2011-07-17 17:29:02 -05001435 TRACE_EXIT();
1436 return TRUE;
Rob Clark74210d52012-01-08 17:59:08 -06001437}
Rob Clark487687e2011-07-17 17:29:02 -05001438
1439static const xf86CrtcConfigFuncsRec drmmode_xf86crtc_config_funcs = {
Daniel Kurtze81fdab2012-12-25 16:43:04 +08001440 .resize = drmmode_xf86crtc_resize
Rob Clark487687e2011-07-17 17:29:02 -05001441};
1442
1443
1444Bool drmmode_pre_init(ScrnInfoPtr pScrn, int fd, int cpp)
1445{
Dave Barnish2e998952013-06-11 16:31:10 +01001446 struct drmmode_rec *drmmode;
Rob Clark74210d52012-01-08 17:59:08 -06001447 int i;
Rob Clark487687e2011-07-17 17:29:02 -05001448
Rob Clark74210d52012-01-08 17:59:08 -06001449 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001450
Rob Clark74210d52012-01-08 17:59:08 -06001451 drmmode = calloc(1, sizeof *drmmode);
Dave Barnish2e998952013-06-11 16:31:10 +01001452 if (!drmmode)
Paul Geary6fe52f32013-04-03 11:12:24 +01001453 return FALSE;
Paul Geary6fe52f32013-04-03 11:12:24 +01001454
Rob Clark74210d52012-01-08 17:59:08 -06001455 drmmode->fd = fd;
Rob Clark487687e2011-07-17 17:29:02 -05001456
Rob Clark74210d52012-01-08 17:59:08 -06001457 xf86CrtcConfigInit(pScrn, &drmmode_xf86crtc_config_funcs);
Rob Clark487687e2011-07-17 17:29:02 -05001458
1459
Rob Clark74210d52012-01-08 17:59:08 -06001460 drmmode->cpp = cpp;
1461 drmmode->mode_res = drmModeGetResources(drmmode->fd);
1462 if (!drmmode->mode_res) {
Paul Geary6fe52f32013-04-03 11:12:24 +01001463 free(drmmode);
Rob Clark74210d52012-01-08 17:59:08 -06001464 return FALSE;
1465 } else {
1466 DEBUG_MSG("Got KMS resources");
1467 DEBUG_MSG(" %d connectors, %d encoders",
1468 drmmode->mode_res->count_connectors,
1469 drmmode->mode_res->count_encoders);
1470 DEBUG_MSG(" %d crtcs, %d fbs",
Dave Barnish2e998952013-06-11 16:31:10 +01001471 drmmode->mode_res->count_crtcs,
1472 drmmode->mode_res->count_fbs);
Rob Clark74210d52012-01-08 17:59:08 -06001473 DEBUG_MSG(" %dx%d minimum resolution",
Dave Barnish2e998952013-06-11 16:31:10 +01001474 drmmode->mode_res->min_width,
1475 drmmode->mode_res->min_height);
Rob Clark74210d52012-01-08 17:59:08 -06001476 DEBUG_MSG(" %dx%d maximum resolution",
Dave Barnish2e998952013-06-11 16:31:10 +01001477 drmmode->mode_res->max_width,
1478 drmmode->mode_res->max_height);
Rob Clark74210d52012-01-08 17:59:08 -06001479 }
1480 xf86CrtcSetSizeRange(pScrn, 320, 200, drmmode->mode_res->max_width,
1481 drmmode->mode_res->max_height);
Dave Barnishf0952c12013-05-10 15:52:23 +01001482
Dave Barnish2e998952013-06-11 16:31:10 +01001483 if (ARMSOCPTR(pScrn)->crtcNum == -1) {
Dave Barnishf0952c12013-05-10 15:52:23 +01001484 INFO_MSG("Adding all CRTCs");
1485 for (i = 0; i < drmmode->mode_res->count_crtcs; i++)
1486 drmmode_crtc_init(pScrn, drmmode, i);
Dave Barnish2e998952013-06-11 16:31:10 +01001487 } else if (ARMSOCPTR(pScrn)->crtcNum < drmmode->mode_res->count_crtcs) {
Dave Barnishf0952c12013-05-10 15:52:23 +01001488 drmmode_crtc_init(pScrn, drmmode, ARMSOCPTR(pScrn)->crtcNum);
1489 } else {
Dave Barnish2e998952013-06-11 16:31:10 +01001490 ERROR_MSG(
1491 "Specified more Screens in xorg.conf than there are DRM CRTCs");
Dave Barnishf0952c12013-05-10 15:52:23 +01001492 return FALSE;
1493 }
Rob Clark487687e2011-07-17 17:29:02 -05001494
Dave Barnish2e998952013-06-11 16:31:10 +01001495 if (ARMSOCPTR(pScrn)->crtcNum != -1) {
1496 if (ARMSOCPTR(pScrn)->crtcNum <
1497 drmmode->mode_res->count_connectors)
1498 drmmode_output_init(pScrn,
1499 drmmode, ARMSOCPTR(pScrn)->crtcNum);
Ray Smithc9c2faf2013-06-07 15:37:19 +01001500 else
1501 return FALSE;
1502 } else {
1503 for (i = 0; i < drmmode->mode_res->count_connectors; i++)
1504 drmmode_output_init(pScrn, drmmode, i);
1505 }
Rob Clark487687e2011-07-17 17:29:02 -05001506
Rob Clark74210d52012-01-08 17:59:08 -06001507 xf86InitialConfiguration(pScrn, TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001508
Rob Clark74210d52012-01-08 17:59:08 -06001509 TRACE_EXIT();
Rob Clark487687e2011-07-17 17:29:02 -05001510
Rob Clark74210d52012-01-08 17:59:08 -06001511 return TRUE;
1512}
Rob Clark487687e2011-07-17 17:29:02 -05001513
1514void
Cooper Yuana83caa62012-06-28 17:19:06 +02001515drmmode_adjust_frame(ScrnInfoPtr pScrn, int x, int y)
Rob Clark487687e2011-07-17 17:29:02 -05001516{
1517 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1518 xf86OutputPtr output = config->output[config->compat_output];
1519 xf86CrtcPtr crtc = output->crtc;
1520
1521 if (!crtc || !crtc->enabled)
1522 return;
1523
1524 drmmode_set_mode_major(crtc, &crtc->mode, crtc->rotation, x, y);
1525}
1526
Rob Clark4b8f30a2011-08-28 12:51:26 -05001527/*
1528 * Page Flipping
1529 */
1530
1531static void
1532page_flip_handler(int fd, unsigned int sequence, unsigned int tv_sec,
Rob Clark74210d52012-01-08 17:59:08 -06001533 unsigned int tv_usec, void *user_data)
Rob Clark4b8f30a2011-08-28 12:51:26 -05001534{
Paul Geary8ffd91c2013-04-11 16:03:15 +01001535 ARMSOCDRI2SwapComplete(user_data);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001536}
1537
1538static drmEventContext event_context = {
1539 .version = DRM_EVENT_CONTEXT_VERSION,
1540 .page_flip_handler = page_flip_handler,
1541};
1542
John Sheu022833e2012-08-15 11:40:11 -07001543int
Rob Clark4b8f30a2011-08-28 12:51:26 -05001544drmmode_page_flip(DrawablePtr draw, uint32_t fb_id, void *priv)
1545{
Daniel Kurtz0361e002013-08-14 21:07:01 +08001546 ScreenPtr pScreen = draw->pScreen;
1547 ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Dave Barnish2e998952013-06-11 16:31:10 +01001548 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Ray Smith3c33c3d2013-03-26 16:06:37 +00001549 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
Dave Barnish2e998952013-06-11 16:31:10 +01001550 struct drmmode_crtc_private_rec *crtc = config->crtc[0]->driver_private;
1551 struct drmmode_rec *mode = crtc->drmmode;
John Sheu022833e2012-08-15 11:40:11 -07001552 int ret, i, failed = 0, num_flipped = 0;
Raymond Smith16a910e2012-05-09 13:04:51 +01001553 unsigned int flags = 0;
1554
Dave Barnishde45ed42013-06-05 13:47:56 +01001555 if (pARMSOC->drmmode_interface->use_page_flip_events)
Ray Smith3c33c3d2013-03-26 16:06:37 +00001556 flags |= DRM_MODE_PAGE_FLIP_EVENT;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001557
1558 /* if we can flip, we must be fullscreen.. so flip all CRTC's.. */
1559 for (i = 0; i < config->num_crtc; i++) {
1560 crtc = config->crtc[i]->driver_private;
1561
Ray Smithe20b3812013-04-08 12:53:53 +01001562 if (!config->crtc[i]->enabled)
1563 continue;
1564
Rob Clark4b8f30a2011-08-28 12:51:26 -05001565 ret = drmModePageFlip(mode->fd, crtc->mode_crtc->crtc_id,
Raymond Smith16a910e2012-05-09 13:04:51 +01001566 fb_id, flags, priv);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001567 if (ret) {
Ray Smith3c33c3d2013-03-26 16:06:37 +00001568 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
Dave Barnish2e998952013-06-11 16:31:10 +01001569 "flip queue failed: %s\n",
1570 strerror(errno));
John Sheu022833e2012-08-15 11:40:11 -07001571 failed = 1;
Dave Barnish2e998952013-06-11 16:31:10 +01001572 } else
John Sheu022833e2012-08-15 11:40:11 -07001573 num_flipped += 1;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001574 }
1575
John Sheu022833e2012-08-15 11:40:11 -07001576 if (failed)
1577 return -(num_flipped + 1);
1578 else
1579 return num_flipped;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001580}
Rob Clark487687e2011-07-17 17:29:02 -05001581
1582/*
1583 * Hot Plug Event handling:
David Garbettdbecfdd2013-08-28 10:45:03 +01001584 * TODO: MIDEGL-1441: Do we need to keep this handler, which
1585 * Rob originally wrote?
Rob Clark487687e2011-07-17 17:29:02 -05001586 */
Rob Clark487687e2011-07-17 17:29:02 -05001587static void
1588drmmode_handle_uevents(int fd, void *closure)
1589{
Rob Clark74210d52012-01-08 17:59:08 -06001590 ScrnInfoPtr pScrn = closure;
Dave Barnish2e998952013-06-11 16:31:10 +01001591 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
1592 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark74210d52012-01-08 17:59:08 -06001593 struct udev_device *dev;
1594 const char *hotplug;
1595 struct stat s;
1596 dev_t udev_devnum;
Rob Clark487687e2011-07-17 17:29:02 -05001597
Rob Clark74210d52012-01-08 17:59:08 -06001598 dev = udev_monitor_receive_device(drmmode->uevent_monitor);
1599 if (!dev)
1600 return;
Rob Clark487687e2011-07-17 17:29:02 -05001601
Rob Clark74210d52012-01-08 17:59:08 -06001602 /*
1603 * Check to make sure this event is directed at our
1604 * device (by comparing dev_t values), then make
1605 * sure it's a hotplug event (HOTPLUG=1)
1606 */
1607 udev_devnum = udev_device_get_devnum(dev);
David Garbettdbecfdd2013-08-28 10:45:03 +01001608 if (fstat(pARMSOC->drmFD, &s)) {
1609 ERROR_MSG("fstat failed: %s", strerror(errno));
1610 udev_device_unref(dev);
1611 return;
1612 }
Rob Clark487687e2011-07-17 17:29:02 -05001613
Rob Clark74210d52012-01-08 17:59:08 -06001614 hotplug = udev_device_get_property_value(dev, "HOTPLUG");
Rob Clark487687e2011-07-17 17:29:02 -05001615
Rob Clark74210d52012-01-08 17:59:08 -06001616 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "hotplug=%s, match=%d\n", hotplug,
Dave Barnish2e998952013-06-11 16:31:10 +01001617 !memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)));
Rob Clark487687e2011-07-17 17:29:02 -05001618
Dave Barnish2e998952013-06-11 16:31:10 +01001619 if (memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)) == 0 &&
Rob Clark74210d52012-01-08 17:59:08 -06001620 hotplug && atoi(hotplug) == 1) {
Daniel Kurtz0361e002013-08-14 21:07:01 +08001621 RRGetInfo(xf86ScrnToScreen(pScrn), TRUE);
Rob Clark74210d52012-01-08 17:59:08 -06001622 }
1623 udev_device_unref(dev);
1624}
Rob Clark487687e2011-07-17 17:29:02 -05001625
Rob Clark4b8f30a2011-08-28 12:51:26 -05001626static void
Rob Clark487687e2011-07-17 17:29:02 -05001627drmmode_uevent_init(ScrnInfoPtr pScrn)
1628{
Dave Barnish2e998952013-06-11 16:31:10 +01001629 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark74210d52012-01-08 17:59:08 -06001630 struct udev *u;
1631 struct udev_monitor *mon;
Rob Clark487687e2011-07-17 17:29:02 -05001632
Rob Clark74210d52012-01-08 17:59:08 -06001633 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001634
Rob Clark74210d52012-01-08 17:59:08 -06001635 u = udev_new();
1636 if (!u)
1637 return;
1638 mon = udev_monitor_new_from_netlink(u, "udev");
1639 if (!mon) {
1640 udev_unref(u);
1641 return;
1642 }
Rob Clark487687e2011-07-17 17:29:02 -05001643
Rob Clark74210d52012-01-08 17:59:08 -06001644 if (udev_monitor_filter_add_match_subsystem_devtype(mon,
1645 "drm",
1646 "drm_minor") < 0 ||
1647 udev_monitor_enable_receiving(mon) < 0) {
1648 udev_monitor_unref(mon);
1649 udev_unref(u);
1650 return;
1651 }
Rob Clark487687e2011-07-17 17:29:02 -05001652
Rob Clark74210d52012-01-08 17:59:08 -06001653 drmmode->uevent_handler =
1654 xf86AddGeneralHandler(udev_monitor_get_fd(mon),
1655 drmmode_handle_uevents, pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001656
Rob Clark74210d52012-01-08 17:59:08 -06001657 drmmode->uevent_monitor = mon;
Rob Clark487687e2011-07-17 17:29:02 -05001658
Rob Clark74210d52012-01-08 17:59:08 -06001659 TRACE_EXIT();
1660}
Rob Clark487687e2011-07-17 17:29:02 -05001661
Rob Clark4b8f30a2011-08-28 12:51:26 -05001662static void
Rob Clark487687e2011-07-17 17:29:02 -05001663drmmode_uevent_fini(ScrnInfoPtr pScrn)
1664{
Dave Barnish2e998952013-06-11 16:31:10 +01001665 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001666
Rob Clark74210d52012-01-08 17:59:08 -06001667 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001668
Rob Clark74210d52012-01-08 17:59:08 -06001669 if (drmmode->uevent_handler) {
1670 struct udev *u = udev_monitor_get_udev(drmmode->uevent_monitor);
1671 xf86RemoveGeneralHandler(drmmode->uevent_handler);
Rob Clark487687e2011-07-17 17:29:02 -05001672
Rob Clark74210d52012-01-08 17:59:08 -06001673 udev_monitor_unref(drmmode->uevent_monitor);
1674 udev_unref(u);
1675 }
Rob Clark487687e2011-07-17 17:29:02 -05001676
Rob Clark74210d52012-01-08 17:59:08 -06001677 TRACE_EXIT();
1678}
Rob Clark4b8f30a2011-08-28 12:51:26 -05001679
1680static void
1681drmmode_wakeup_handler(pointer data, int err, pointer p)
1682{
Ray Smith04af9992013-06-26 13:44:54 +01001683 int fd = (int)data;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001684 fd_set *read_mask = p;
1685
Ray Smith04af9992013-06-26 13:44:54 +01001686 if (err < 0)
Rob Clark4b8f30a2011-08-28 12:51:26 -05001687 return;
1688
Ray Smith04af9992013-06-26 13:44:54 +01001689 if (FD_ISSET(fd, read_mask))
1690 drmHandleEvent(fd, &event_context);
1691}
Paul Geary6fe52f32013-04-03 11:12:24 +01001692
Ray Smith04af9992013-06-26 13:44:54 +01001693void drmmode_init_wakeup_handler(int fd)
1694{
1695 AddGeneralSocket(fd);
1696 RegisterBlockAndWakeupHandlers((BlockHandlerProcPtr)NoopDDA,
1697 drmmode_wakeup_handler, (pointer)fd);
1698}
1699
1700void drmmode_fini_wakeup_handler(int fd)
1701{
1702 RemoveBlockAndWakeupHandlers((BlockHandlerProcPtr)NoopDDA,
1703 drmmode_wakeup_handler, (pointer)fd);
1704 RemoveGeneralSocket(fd);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001705}
1706
1707void
Rob Clark67b875f2012-04-20 19:13:57 -05001708drmmode_wait_for_event(ScrnInfoPtr pScrn)
1709{
Dave Barnish2e998952013-06-11 16:31:10 +01001710 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark67b875f2012-04-20 19:13:57 -05001711 drmHandleEvent(drmmode->fd, &event_context);
1712}
1713
1714void
Rob Clark4b8f30a2011-08-28 12:51:26 -05001715drmmode_screen_init(ScrnInfoPtr pScrn)
1716{
Rob Clark4b8f30a2011-08-28 12:51:26 -05001717 drmmode_uevent_init(pScrn);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001718}
1719
1720void
1721drmmode_screen_fini(ScrnInfoPtr pScrn)
1722{
1723 drmmode_uevent_fini(pScrn);
1724}