blob: 1bc0be2f94d51a2f22c305558e91cd4188d0ae8c [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: */
56 drmModePlane *ovr;
Paul Geary8ffd91c2013-04-11 16:03:15 +010057 struct armsoc_bo *bo;
Rob Clark687c6082012-01-08 19:33:18 -060058 uint32_t fb_id;
59 int x, y;
Dave Barnish2e998952013-06-11 16:31:10 +010060};
Rob Clark687c6082012-01-08 19:33:18 -060061
Dave Barnish2e998952013-06-11 16:31:10 +010062struct drmmode_rec {
Rob Clark74210d52012-01-08 17:59:08 -060063 int fd;
Rob Clark74210d52012-01-08 17:59:08 -060064 drmModeResPtr mode_res;
65 int cpp;
66 struct udev_monitor *uevent_monitor;
67 InputHandlerProc uevent_handler;
Dave Barnish2e998952013-06-11 16:31:10 +010068 struct drmmode_cursor_rec *cursor;
69};
Rob Clark487687e2011-07-17 17:29:02 -050070
Dave Barnish2e998952013-06-11 16:31:10 +010071struct drmmode_crtc_private_rec {
72 struct drmmode_rec *drmmode;
Rob Clark74210d52012-01-08 17:59:08 -060073 drmModeCrtcPtr mode_crtc;
Stéphane Marchesinb8b35202012-10-02 20:27:40 -070074 int cursor_visible;
Dave Barnish2e998952013-06-11 16:31:10 +010075};
Rob Clark487687e2011-07-17 17:29:02 -050076
Dave Barnish2e998952013-06-11 16:31:10 +010077struct drmmode_prop_rec {
Rob Clark487687e2011-07-17 17:29:02 -050078 drmModePropertyPtr mode_prop;
Dave Barnish2e998952013-06-11 16:31:10 +010079 /* Index within the kernel-side property arrays for this connector. */
80 int index;
81 /* if range prop, num_atoms == 1;
82 * if enum prop, num_atoms == num_enums + 1
83 */
84 int num_atoms;
Rob Clark487687e2011-07-17 17:29:02 -050085 Atom *atoms;
Dave Barnish2e998952013-06-11 16:31:10 +010086};
Rob Clark487687e2011-07-17 17:29:02 -050087
Dave Barnish2e998952013-06-11 16:31:10 +010088struct drmmode_output_priv {
89 struct drmmode_rec *drmmode;
Rob Clark74210d52012-01-08 17:59:08 -060090 int output_id;
91 drmModeConnectorPtr mode_output;
92 drmModeEncoderPtr mode_encoder;
93 drmModePropertyBlobPtr edid_blob;
94 int num_props;
Dave Barnish2e998952013-06-11 16:31:10 +010095 struct drmmode_prop_rec *props;
96};
Rob Clark487687e2011-07-17 17:29:02 -050097
98static void drmmode_output_dpms(xf86OutputPtr output, int mode);
Dave Barnishf256ebe2013-08-22 15:05:58 +010099static Bool drmmode_xf86crtc_resize(ScrnInfoPtr pScrn, int width, int height);
Rob Clark487687e2011-07-17 17:29:02 -0500100
Dave Barnish2e998952013-06-11 16:31:10 +0100101static struct drmmode_rec *
Rob Clark687c6082012-01-08 19:33:18 -0600102drmmode_from_scrn(ScrnInfoPtr pScrn)
103{
104 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
Dave Barnish2e998952013-06-11 16:31:10 +0100105 struct drmmode_crtc_private_rec *drmmode_crtc;
Rob Clark687c6082012-01-08 19:33:18 -0600106
107 drmmode_crtc = xf86_config->crtc[0]->driver_private;
108 return drmmode_crtc->drmmode;
109}
110
Rob Clark487687e2011-07-17 17:29:02 -0500111static void
112drmmode_ConvertFromKMode(ScrnInfoPtr pScrn, drmModeModeInfo *kmode,
Rob Clark74210d52012-01-08 17:59:08 -0600113 DisplayModePtr mode)
Rob Clark487687e2011-07-17 17:29:02 -0500114{
Rob Clark74210d52012-01-08 17:59:08 -0600115 memset(mode, 0, sizeof(DisplayModeRec));
116 mode->status = MODE_OK;
Rob Clark487687e2011-07-17 17:29:02 -0500117
Rob Clark74210d52012-01-08 17:59:08 -0600118 mode->Clock = kmode->clock;
Rob Clark487687e2011-07-17 17:29:02 -0500119
Rob Clark74210d52012-01-08 17:59:08 -0600120 mode->HDisplay = kmode->hdisplay;
121 mode->HSyncStart = kmode->hsync_start;
122 mode->HSyncEnd = kmode->hsync_end;
123 mode->HTotal = kmode->htotal;
124 mode->HSkew = kmode->hskew;
Rob Clark487687e2011-07-17 17:29:02 -0500125
Rob Clark74210d52012-01-08 17:59:08 -0600126 mode->VDisplay = kmode->vdisplay;
127 mode->VSyncStart = kmode->vsync_start;
128 mode->VSyncEnd = kmode->vsync_end;
129 mode->VTotal = kmode->vtotal;
130 mode->VScan = kmode->vscan;
Rob Clark487687e2011-07-17 17:29:02 -0500131
Dave Barnish01b5c172013-04-10 11:01:17 +0100132 mode->Flags = kmode->flags;
Rob Clark74210d52012-01-08 17:59:08 -0600133 mode->name = strdup(kmode->name);
Rob Clark487687e2011-07-17 17:29:02 -0500134
Rob Clark74210d52012-01-08 17:59:08 -0600135 DEBUG_MSG("copy mode %s (%p %p)", kmode->name, mode->name, mode);
Rob Clark487687e2011-07-17 17:29:02 -0500136
Rob Clark74210d52012-01-08 17:59:08 -0600137 if (kmode->type & DRM_MODE_TYPE_DRIVER)
138 mode->type = M_T_DRIVER;
Dave Barnish2e998952013-06-11 16:31:10 +0100139
Rob Clark74210d52012-01-08 17:59:08 -0600140 if (kmode->type & DRM_MODE_TYPE_PREFERRED)
141 mode->type |= M_T_PREFERRED;
Rob Clark487687e2011-07-17 17:29:02 -0500142
Dave Barnish2e998952013-06-11 16:31:10 +0100143 xf86SetModeCrtc(mode, pScrn->adjustFlags);
Rob Clark74210d52012-01-08 17:59:08 -0600144}
Rob Clark487687e2011-07-17 17:29:02 -0500145
146static void
147drmmode_ConvertToKMode(ScrnInfoPtr pScrn, drmModeModeInfo *kmode,
Rob Clark74210d52012-01-08 17:59:08 -0600148 DisplayModePtr mode)
Rob Clark487687e2011-07-17 17:29:02 -0500149{
Rob Clark74210d52012-01-08 17:59:08 -0600150 memset(kmode, 0, sizeof(*kmode));
Rob Clark487687e2011-07-17 17:29:02 -0500151
Rob Clark74210d52012-01-08 17:59:08 -0600152 kmode->clock = mode->Clock;
153 kmode->hdisplay = mode->HDisplay;
154 kmode->hsync_start = mode->HSyncStart;
155 kmode->hsync_end = mode->HSyncEnd;
156 kmode->htotal = mode->HTotal;
157 kmode->hskew = mode->HSkew;
Rob Clark487687e2011-07-17 17:29:02 -0500158
Rob Clark74210d52012-01-08 17:59:08 -0600159 kmode->vdisplay = mode->VDisplay;
160 kmode->vsync_start = mode->VSyncStart;
161 kmode->vsync_end = mode->VSyncEnd;
162 kmode->vtotal = mode->VTotal;
163 kmode->vscan = mode->VScan;
Rob Clark487687e2011-07-17 17:29:02 -0500164
Dave Barnish01b5c172013-04-10 11:01:17 +0100165 kmode->flags = mode->Flags;
Rob Clark74210d52012-01-08 17:59:08 -0600166 if (mode->name)
167 strncpy(kmode->name, mode->name, DRM_DISPLAY_MODE_LEN);
168 kmode->name[DRM_DISPLAY_MODE_LEN-1] = 0;
169}
Rob Clark487687e2011-07-17 17:29:02 -0500170
171static void
172drmmode_crtc_dpms(xf86CrtcPtr drmmode_crtc, int mode)
173{
Dave Barnish2e998952013-06-11 16:31:10 +0100174 /* TODO: MIDEGL-1431: Implement this function */
Rob Clark74210d52012-01-08 17:59:08 -0600175}
Rob Clark487687e2011-07-17 17:29:02 -0500176
177static Bool
178drmmode_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode,
Rob Clark74210d52012-01-08 17:59:08 -0600179 Rotation rotation, int x, int y)
Rob Clark487687e2011-07-17 17:29:02 -0500180{
181 ScrnInfoPtr pScrn = crtc->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100182 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -0500183 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(crtc->scrn);
Dave Barnish2e998952013-06-11 16:31:10 +0100184 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
185 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500186 uint32_t *output_ids = NULL;
187 int output_count = 0;
188 int ret = TRUE;
189 int i;
David Garbett7171c522012-05-11 13:12:50 +0100190 uint32_t fb_id;
Rob Clark487687e2011-07-17 17:29:02 -0500191 drmModeModeInfo kmode;
Dave Barnishf256ebe2013-08-22 15:05:58 +0100192 drmModeCrtcPtr newcrtc = NULL;
193 static int got_last_good = -1;
194 static int last_good_x;
195 static int last_good_y;
196 static Rotation last_good_rotation;
197 static DisplayModeRec last_good_mode;
198
Rob Clark487687e2011-07-17 17:29:02 -0500199
200 TRACE_ENTER();
201
Paul Geary8ffd91c2013-04-11 16:03:15 +0100202 fb_id = armsoc_bo_get_fb(pARMSOC->scanout);
David Garbett7171c522012-05-11 13:12:50 +0100203
204 if (fb_id == 0) {
David Garbett7171c522012-05-11 13:12:50 +0100205 DEBUG_MSG("create framebuffer: %dx%d",
206 pScrn->virtualX, pScrn->virtualY);
207
Paul Geary8ffd91c2013-04-11 16:03:15 +0100208 ret = armsoc_bo_add_fb(pARMSOC->scanout);
Ray Smithb4299f82013-03-13 10:08:36 +0000209 if (ret) {
210 ERROR_MSG(
211 "Failed to add framebuffer to the scanout buffer");
David Garbett7171c522012-05-11 13:12:50 +0100212 return FALSE;
Ray Smithb4299f82013-03-13 10:08:36 +0000213 }
Dave Barnish523c9ff2013-03-12 10:59:03 +0000214
Paul Geary8ffd91c2013-04-11 16:03:15 +0100215 fb_id = armsoc_bo_get_fb(pARMSOC->scanout);
Paul Geary6fe52f32013-04-03 11:12:24 +0100216 if (0 == fb_id)
217 return FALSE;
David Garbett7171c522012-05-11 13:12:50 +0100218 }
Rob Clark487687e2011-07-17 17:29:02 -0500219
Rob Clark487687e2011-07-17 17:29:02 -0500220 /* Set the new mode: */
221 crtc->mode = *mode;
222 crtc->x = x;
223 crtc->y = y;
224 crtc->rotation = rotation;
225
226 output_ids = calloc(sizeof(uint32_t), xf86_config->num_output);
227 if (!output_ids) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100228 ERROR_MSG(
229 "memory allocation failed in drmmode_set_mode_major()");
Rob Clark487687e2011-07-17 17:29:02 -0500230 ret = FALSE;
231 goto done;
232 }
233
234 for (i = 0; i < xf86_config->num_output; i++) {
235 xf86OutputPtr output = xf86_config->output[i];
Dave Barnish2e998952013-06-11 16:31:10 +0100236 struct drmmode_output_priv *drmmode_output;
Rob Clark487687e2011-07-17 17:29:02 -0500237
238 if (output->crtc != crtc)
239 continue;
240
241 drmmode_output = output->driver_private;
242 output_ids[output_count] =
243 drmmode_output->mode_output->connector_id;
244 output_count++;
245 }
246
Dave Barnish2e998952013-06-11 16:31:10 +0100247 if (!xf86CrtcRotate(crtc)) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100248 ERROR_MSG(
249 "failed to assign rotation in drmmode_set_mode_major()");
Dave Barnish523c9ff2013-03-12 10:59:03 +0000250 ret = FALSE;
Rob Clark487687e2011-07-17 17:29:02 -0500251 goto done;
Dave Barnish523c9ff2013-03-12 10:59:03 +0000252 }
Rob Clark487687e2011-07-17 17:29:02 -0500253
Mandeep Singh Bainesc874fcb2012-09-13 15:30:00 +0200254 if (crtc->funcs->gamma_set)
255 crtc->funcs->gamma_set(crtc, crtc->gamma_red, crtc->gamma_green,
256 crtc->gamma_blue, crtc->gamma_size);
Rob Clark487687e2011-07-17 17:29:02 -0500257
258 drmmode_ConvertToKMode(crtc->scrn, &kmode, mode);
259
Rob Clark487687e2011-07-17 17:29:02 -0500260 ret = drmModeSetCrtc(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id,
261 fb_id, x, y, output_ids, output_count, &kmode);
262 if (ret) {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100263 ERROR_MSG(
264 "failed to set mode: %s", strerror(-ret));
Dave Barnish523c9ff2013-03-12 10:59:03 +0000265 ret = FALSE;
266 goto done;
Dave Barnishf256ebe2013-08-22 15:05:58 +0100267 }
268
269 /* get the actual crtc info */
270 newcrtc = drmModeGetCrtc(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id);
271 if (!newcrtc) {
272 ERROR_MSG("couldn't get actual mode back");
273 ret = FALSE;
274 goto done;
275 }
276
277 if (kmode.hdisplay != newcrtc->mode.hdisplay ||
278 kmode.vdisplay != newcrtc->mode.vdisplay) {
279
280 xf86CrtcConfigPtr xf86_config;
281 int i;
282 Bool *enables;
283
284 ret = FALSE;
285 ERROR_MSG(
286 "drm did not set requested mode! (requested %dx%d, actual %dx%d)",
287 kmode.hdisplay, kmode.vdisplay,
288 newcrtc->mode.hdisplay,
289 newcrtc->mode.vdisplay);
290
291 if (got_last_good < 0) {
292 DEBUG_MSG("No last good values to use");
293 goto done;
294 }
295
296 DEBUG_MSG("Reverting to last_good values");
297 /* disable crtcs to prevent resize calling back here */
298 xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
299 enables = malloc(xf86_config->num_crtc * sizeof(Bool));
300 for (i = 0; i < xf86_config->num_crtc; i++) {
301 xf86CrtcPtr crtc = xf86_config->crtc[i];
302 enables[i] = crtc->enabled;
303 crtc->enabled = 0;
304 }
305 /* revert to last good settings */
306 drmmode_xf86crtc_resize(pScrn,
307 last_good_mode.HDisplay,
308 last_good_mode.VDisplay);
309 fb_id = armsoc_bo_get_fb(pARMSOC->scanout);
310 drmmode_ConvertToKMode(crtc->scrn, &kmode,
311 &last_good_mode);
312 drmModeSetCrtc(drmmode->fd,
313 drmmode_crtc->mode_crtc->crtc_id,
314 fb_id, last_good_x, last_good_y,
315 output_ids, output_count, &kmode);
316 /* re-enable crtcs */
317 for (i = 0; i < xf86_config->num_crtc; i++) {
318 xf86CrtcPtr crtc = xf86_config->crtc[i];
319 crtc->enabled = enables[i];
320 }
321 free(enables);
322
323 /* let RandR know we changed things */
324 xf86RandR12TellChanged(pScrn->pScreen);
325
Rob Clark487687e2011-07-17 17:29:02 -0500326 } else {
Dave Barnishf256ebe2013-08-22 15:05:58 +0100327 /* When called on a resize, crtc->mode already contains the
328 * resized values so we can't use this for recovery.
329 * We can't read it out of the crtc either as mode_valid is 0.
330 * Instead we save the last good mode set here & fallback to
331 * that on failure.
332 */
333 DEBUG_MSG("Setting last good values");
334 got_last_good = 1;
335 last_good_x = crtc->x;
336 last_good_y = crtc->y;
337 last_good_rotation = crtc->rotation;
338 last_good_mode = crtc->mode;
339
Rob Clark487687e2011-07-17 17:29:02 -0500340 ret = TRUE;
341 }
342
Rob Clark487687e2011-07-17 17:29:02 -0500343 /* Turn on any outputs on this crtc that may have been disabled: */
344 for (i = 0; i < xf86_config->num_output; i++) {
345 xf86OutputPtr output = xf86_config->output[i];
346
347 if (output->crtc != crtc)
348 continue;
349
350 drmmode_output_dpms(output, DPMSModeOn);
351 }
352
Dave Barnishaf046152013-05-31 09:12:44 +0100353 /* if hw cursor is initialized, reload it */
Dave Barnish2e998952013-06-11 16:31:10 +0100354 if (drmmode->cursor)
Dave Barnishaf046152013-05-31 09:12:44 +0100355 xf86_reload_cursors(pScrn->pScreen);
Rob Clark487687e2011-07-17 17:29:02 -0500356
357done:
Dave Barnishf256ebe2013-08-22 15:05:58 +0100358 if (newcrtc)
359 drmModeFreeCrtc(newcrtc);
360
Dave Barnish2e998952013-06-11 16:31:10 +0100361 if (output_ids)
Rob Clark487687e2011-07-17 17:29:02 -0500362 free(output_ids);
Dave Barnish2e998952013-06-11 16:31:10 +0100363
Dave Barnishf256ebe2013-08-22 15:05:58 +0100364 if (!ret && got_last_good > 0) {
365 /* If there was a problem, restore the last good mode: */
366 crtc->x = last_good_x;
367 crtc->y = last_good_y;
368 crtc->rotation = last_good_rotation;
369 crtc->mode = last_good_mode;
Rob Clark487687e2011-07-17 17:29:02 -0500370 }
371
372 TRACE_EXIT();
373 return ret;
Rob Clark74210d52012-01-08 17:59:08 -0600374}
Rob Clark487687e2011-07-17 17:29:02 -0500375
376static void
Rob Clark487687e2011-07-17 17:29:02 -0500377drmmode_hide_cursor(xf86CrtcPtr crtc)
378{
Dave Barnish2e998952013-06-11 16:31:10 +0100379 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
380 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
381 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Rob Clark487687e2011-07-17 17:29:02 -0500382
Rob Clark687c6082012-01-08 19:33:18 -0600383 if (!cursor)
384 return;
385
Stéphane Marchesinb8b35202012-10-02 20:27:40 -0700386 drmmode_crtc->cursor_visible = FALSE;
Rob Clark687c6082012-01-08 19:33:18 -0600387
388 /* set plane's fb_id to 0 to disable it */
389 drmModeSetPlane(drmmode->fd, cursor->ovr->plane_id,
390 drmmode_crtc->mode_crtc->crtc_id, 0, 0,
391 0, 0, 0, 0, 0, 0, 0, 0);
Rob Clark74210d52012-01-08 17:59:08 -0600392}
Rob Clark487687e2011-07-17 17:29:02 -0500393
394static void
395drmmode_show_cursor(xf86CrtcPtr crtc)
396{
Dave Barnish2e998952013-06-11 16:31:10 +0100397 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
398 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
399 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Dave Barnishde45ed42013-06-05 13:47:56 +0100400 int crtc_x, crtc_y, src_x, src_y;
401 int w, h, pad;
402 ScrnInfoPtr pScrn = crtc->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100403 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;
Rob Clark487687e2011-07-17 17:29:02 -0500407
Stéphane Marchesinb8b35202012-10-02 20:27:40 -0700408 drmmode_crtc->cursor_visible = TRUE;
Rob Clark687c6082012-01-08 19:33:18 -0600409
Dave Barnishde45ed42013-06-05 13:47:56 +0100410 w = pARMSOC->drmmode_interface->cursor_width;
411 h = pARMSOC->drmmode_interface->cursor_height;
412 pad = pARMSOC->drmmode_interface->cursor_padding;
413
Dave Barnish2e998952013-06-11 16:31:10 +0100414 /* get padded width */
415 w = w + 2 * pad;
416 /* get x of padded cursor */
417 crtc_x = cursor->x - pad;
Rob Clark60f5bad2012-01-22 18:35:28 -0600418 crtc_y = cursor->y;
419 src_x = 0;
420 src_y = 0;
421
Dave Barnishde45ed42013-06-05 13:47:56 +0100422 /* calculate clipped x, y, w & h if cursor is off edges */
Rob Clark60f5bad2012-01-22 18:35:28 -0600423 if (crtc_x < 0) {
424 src_x += -crtc_x;
425 w -= -crtc_x;
426 crtc_x = 0;
427 }
428
429 if (crtc_y < 0) {
430 src_y += -crtc_y;
431 h -= -crtc_y;
432 crtc_y = 0;
433 }
434
Dave Barnish2e998952013-06-11 16:31:10 +0100435 if ((crtc_x + w) > crtc->mode.HDisplay)
Rob Clark60f5bad2012-01-22 18:35:28 -0600436 w = crtc->mode.HDisplay - crtc_x;
Rob Clark60f5bad2012-01-22 18:35:28 -0600437
Dave Barnish2e998952013-06-11 16:31:10 +0100438 if ((crtc_y + h) > crtc->mode.VDisplay)
Rob Clark60f5bad2012-01-22 18:35:28 -0600439 h = crtc->mode.VDisplay - crtc_y;
Rob Clark60f5bad2012-01-22 18:35:28 -0600440
Rob Clark687c6082012-01-08 19:33:18 -0600441 /* note src coords (last 4 args) are in Q16 format */
442 drmModeSetPlane(drmmode->fd, cursor->ovr->plane_id,
443 drmmode_crtc->mode_crtc->crtc_id, cursor->fb_id, 0,
Dave Barnish2e998952013-06-11 16:31:10 +0100444 crtc_x, crtc_y, w, h, src_x<<16, src_y<<16,
445 w<<16, h<<16);
Rob Clark687c6082012-01-08 19:33:18 -0600446}
447
448static void
449drmmode_set_cursor_position(xf86CrtcPtr crtc, int x, int y)
450{
Dave Barnish2e998952013-06-11 16:31:10 +0100451 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
452 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
453 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Rob Clark687c6082012-01-08 19:33:18 -0600454
455 if (!cursor)
456 return;
457
458 cursor->x = x;
459 cursor->y = y;
460
Stéphane Marchesinb8b35202012-10-02 20:27:40 -0700461 if (drmmode_crtc->cursor_visible)
Rob Clark687c6082012-01-08 19:33:18 -0600462 drmmode_show_cursor(crtc);
463}
464
465static void
466drmmode_load_cursor_argb(xf86CrtcPtr crtc, CARD32 *image)
467{
Dave Barnish2e998952013-06-11 16:31:10 +0100468 uint32_t *d;
469 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
470 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
471 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Rob Clark979add52012-02-21 18:35:24 -0600472 int visible;
Dave Barnishde45ed42013-06-05 13:47:56 +0100473 ScrnInfoPtr pScrn = crtc->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100474 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark687c6082012-01-08 19:33:18 -0600475
476 if (!cursor)
477 return;
478
Stéphane Marchesinb8b35202012-10-02 20:27:40 -0700479 visible = drmmode_crtc->cursor_visible;
Rob Clark979add52012-02-21 18:35:24 -0600480
481 if (visible)
Rob Clark687c6082012-01-08 19:33:18 -0600482 drmmode_hide_cursor(crtc);
483
Paul Geary8ffd91c2013-04-11 16:03:15 +0100484 d = armsoc_bo_map(cursor->bo);
Dave Barnish2e998952013-06-11 16:31:10 +0100485 if (!d) {
Paul Geary6fe52f32013-04-03 11:12:24 +0100486 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
487 "load_cursor_argb map failure\n");
488 if (visible)
489 drmmode_show_cursor(crtc);
490 return;
491 }
John Rees292ae502013-03-21 16:24:35 +0000492
Dave Barnishde45ed42013-06-05 13:47:56 +0100493 /* set_cursor_image is a mandatory function */
Dave Barnish2e998952013-06-11 16:31:10 +0100494 assert(pARMSOC->drmmode_interface->set_cursor_image);
Dave Barnishde45ed42013-06-05 13:47:56 +0100495 pARMSOC->drmmode_interface->set_cursor_image(crtc, d, image);
Rob Clark687c6082012-01-08 19:33:18 -0600496
Rob Clark979add52012-02-21 18:35:24 -0600497 if (visible)
Rob Clark687c6082012-01-08 19:33:18 -0600498 drmmode_show_cursor(crtc);
499}
500
501Bool
502drmmode_cursor_init(ScreenPtr pScreen)
503{
504 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
Dave Barnish2e998952013-06-11 16:31:10 +0100505 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
506 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
507 struct drmmode_cursor_rec *cursor;
Rob Clark687c6082012-01-08 19:33:18 -0600508 drmModePlaneRes *plane_resources;
509 drmModePlane *ovr;
Dave Barnishde45ed42013-06-05 13:47:56 +0100510 int w, h, pad;
Rob Clark687c6082012-01-08 19:33:18 -0600511 uint32_t handles[4], pitches[4], offsets[4]; /* we only use [0] */
512
513 if (drmmode->cursor) {
514 INFO_MSG("cursor already initialized");
515 return TRUE;
516 }
517
Dave Barnish2e998952013-06-11 16:31:10 +0100518 if (!xf86LoaderCheckSymbol("drmModeGetPlaneResources")) {
519 ERROR_MSG(
520 "HW cursor not supported (needs libdrm 2.4.30 or higher)");
Ray Smith003cf5e2013-03-19 10:44:07 +0000521 return FALSE;
522 }
Rob Clark687c6082012-01-08 19:33:18 -0600523
524 /* find an unused plane which can be used as a mouse cursor. Note
525 * that we cheat a bit, in order to not burn one overlay per crtc,
526 * and only show the mouse cursor on one crtc at a time
527 */
528 plane_resources = drmModeGetPlaneResources(drmmode->fd);
529 if (!plane_resources) {
Dave Barnish2e998952013-06-11 16:31:10 +0100530 ERROR_MSG("HW cursor: drmModeGetPlaneResources failed: %s",
531 strerror(errno));
Rob Clark687c6082012-01-08 19:33:18 -0600532 return FALSE;
533 }
534
535 if (plane_resources->count_planes < 1) {
536 ERROR_MSG("not enough planes for HW cursor");
Ray Smith003cf5e2013-03-19 10:44:07 +0000537 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600538 return FALSE;
539 }
540
541 ovr = drmModeGetPlane(drmmode->fd, plane_resources->planes[0]);
542 if (!ovr) {
Dave Barnish2e998952013-06-11 16:31:10 +0100543 ERROR_MSG("HW cursor: drmModeGetPlane failed: %s",
544 strerror(errno));
Ray Smith003cf5e2013-03-19 10:44:07 +0000545 drmModeFreePlaneResources(plane_resources);
546 return FALSE;
547 }
548
Dave Barnishde45ed42013-06-05 13:47:56 +0100549 if (pARMSOC->drmmode_interface->init_plane_for_cursor &&
Dave Barnish2e998952013-06-11 16:31:10 +0100550 pARMSOC->drmmode_interface->init_plane_for_cursor(
551 drmmode->fd, ovr->plane_id)) {
Ray Smithb93cd892013-04-03 10:06:18 +0100552 ERROR_MSG("Failed driver-specific cursor initialization");
553 drmModeFreePlaneResources(plane_resources);
554 return FALSE;
555 }
556
Dave Barnish2e998952013-06-11 16:31:10 +0100557 cursor = calloc(1, sizeof(struct drmmode_cursor_rec));
Ray Smith003cf5e2013-03-19 10:44:07 +0000558 if (!cursor) {
Dave Barnishfd50b132013-05-16 15:00:02 +0100559 ERROR_MSG("HW cursor: calloc failed");
Ray Smith003cf5e2013-03-19 10:44:07 +0000560 drmModeFreePlane(ovr);
561 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600562 return FALSE;
563 }
564
565 cursor->ovr = ovr;
Dave Barnishde45ed42013-06-05 13:47:56 +0100566
567 w = pARMSOC->drmmode_interface->cursor_width;
568 h = pARMSOC->drmmode_interface->cursor_height;
569 pad = pARMSOC->drmmode_interface->cursor_padding;
570
571 /* allow for cursor padding in the bo */
Dave Barnish2e998952013-06-11 16:31:10 +0100572 cursor->bo = armsoc_bo_new_with_dim(pARMSOC->dev,
573 w + 2 * pad, h,
574 0, 32, ARMSOC_BO_SCANOUT);
Rob Clark687c6082012-01-08 19:33:18 -0600575
Ray Smith003cf5e2013-03-19 10:44:07 +0000576 if (!cursor->bo) {
Dave Barnishfd50b132013-05-16 15:00:02 +0100577 ERROR_MSG("HW cursor: buffer allocation failed");
Ray Smith003cf5e2013-03-19 10:44:07 +0000578 free(cursor);
579 drmModeFreePlane(ovr);
580 drmModeFreePlaneResources(plane_resources);
581 return FALSE;
582 }
583
Paul Geary8ffd91c2013-04-11 16:03:15 +0100584 handles[0] = armsoc_bo_handle(cursor->bo);
585 pitches[0] = armsoc_bo_pitch(cursor->bo);
Rob Clark687c6082012-01-08 19:33:18 -0600586 offsets[0] = 0;
587
Dave Barnishde45ed42013-06-05 13:47:56 +0100588 /* allow for cursor padding in the fb */
589 if (drmModeAddFB2(drmmode->fd, w + 2 * pad, h, DRM_FORMAT_ARGB8888,
Rob Clark687c6082012-01-08 19:33:18 -0600590 handles, pitches, offsets, &cursor->fb_id, 0)) {
Dave Barnish2e998952013-06-11 16:31:10 +0100591 ERROR_MSG("HW cursor: drmModeAddFB2 failed: %s",
592 strerror(errno));
Paul Geary8ffd91c2013-04-11 16:03:15 +0100593 armsoc_bo_unreference(cursor->bo);
Ray Smith003cf5e2013-03-19 10:44:07 +0000594 free(cursor);
595 drmModeFreePlane(ovr);
596 drmModeFreePlaneResources(plane_resources);
Rob Clark687c6082012-01-08 19:33:18 -0600597 return FALSE;
598 }
599
Ray Smith003cf5e2013-03-19 10:44:07 +0000600 if (!xf86_cursors_init(pScreen, w, h, HARDWARE_CURSOR_ARGB)) {
601 ERROR_MSG("xf86_cursors_init() failed");
Dave Barnish2e998952013-06-11 16:31:10 +0100602 if (drmModeRmFB(drmmode->fd, cursor->fb_id))
Ray Smith003cf5e2013-03-19 10:44:07 +0000603 ERROR_MSG("drmModeRmFB() failed");
Dave Barnish2e998952013-06-11 16:31:10 +0100604
Paul Geary8ffd91c2013-04-11 16:03:15 +0100605 armsoc_bo_unreference(cursor->bo);
Ray Smith003cf5e2013-03-19 10:44:07 +0000606 free(cursor);
607 drmModeFreePlane(ovr);
608 drmModeFreePlaneResources(plane_resources);
609 return FALSE;
Rob Clark687c6082012-01-08 19:33:18 -0600610 }
611
Ray Smith003cf5e2013-03-19 10:44:07 +0000612 INFO_MSG("HW cursor initialized");
613 drmmode->cursor = cursor;
Dave Barnishfd50b132013-05-16 15:00:02 +0100614 drmModeFreePlaneResources(plane_resources);
Ray Smith003cf5e2013-03-19 10:44:07 +0000615 return TRUE;
Rob Clark74210d52012-01-08 17:59:08 -0600616}
Rob Clark487687e2011-07-17 17:29:02 -0500617
Dave Barnishfd50b132013-05-16 15:00:02 +0100618void drmmode_cursor_fini(ScreenPtr pScreen)
619{
620 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
Dave Barnish2e998952013-06-11 16:31:10 +0100621 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
622 struct drmmode_cursor_rec *cursor = drmmode->cursor;
Dave Barnishfd50b132013-05-16 15:00:02 +0100623
Dave Barnish2e998952013-06-11 16:31:10 +0100624 if (!cursor)
Dave Barnishfd50b132013-05-16 15:00:02 +0100625 return;
Dave Barnish2e998952013-06-11 16:31:10 +0100626
Dave Barnishfd50b132013-05-16 15:00:02 +0100627 drmmode->cursor = NULL;
628 xf86_cursors_fini(pScreen);
629 drmModeRmFB(drmmode->fd, cursor->fb_id);
630 armsoc_bo_unreference(cursor->bo);
631 drmModeFreePlane(cursor->ovr);
632 free(cursor);
633}
634
635
Dave Barnish2e998952013-06-11 16:31:10 +0100636#if 1 == ARMSOC_SUPPORT_GAMMA
Rob Clark487687e2011-07-17 17:29:02 -0500637static void
638drmmode_gamma_set(xf86CrtcPtr crtc, CARD16 *red, CARD16 *green, CARD16 *blue,
Rob Clark74210d52012-01-08 17:59:08 -0600639 int size)
Rob Clark487687e2011-07-17 17:29:02 -0500640{
Dave Barnish2e998952013-06-11 16:31:10 +0100641 struct drmmode_crtc_private_rec *drmmode_crtc = crtc->driver_private;
642 struct drmmode_rec *drmmode = drmmode_crtc->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500643 int ret;
644
645 ret = drmModeCrtcSetGamma(drmmode->fd, drmmode_crtc->mode_crtc->crtc_id,
Rob Clark74210d52012-01-08 17:59:08 -0600646 size, red, green, blue);
Rob Clark487687e2011-07-17 17:29:02 -0500647 if (ret != 0) {
648 xf86DrvMsg(crtc->scrn->scrnIndex, X_ERROR,
Rob Clark74210d52012-01-08 17:59:08 -0600649 "failed to set gamma: %s\n", strerror(-ret));
Rob Clark487687e2011-07-17 17:29:02 -0500650 }
651}
Mandeep Singh Bainesc874fcb2012-09-13 15:30:00 +0200652#endif
Rob Clark487687e2011-07-17 17:29:02 -0500653
654static const xf86CrtcFuncsRec drmmode_crtc_funcs = {
Rob Clark74210d52012-01-08 17:59:08 -0600655 .dpms = drmmode_crtc_dpms,
656 .set_mode_major = drmmode_set_mode_major,
657 .set_cursor_position = drmmode_set_cursor_position,
658 .show_cursor = drmmode_show_cursor,
659 .hide_cursor = drmmode_hide_cursor,
660 .load_cursor_argb = drmmode_load_cursor_argb,
Dave Barnish2e998952013-06-11 16:31:10 +0100661#if 1 == ARMSOC_SUPPORT_GAMMA
Rob Clark74210d52012-01-08 17:59:08 -0600662 .gamma_set = drmmode_gamma_set,
Mandeep Singh Bainesc874fcb2012-09-13 15:30:00 +0200663#endif
Rob Clark487687e2011-07-17 17:29:02 -0500664};
665
666
667static void
Dave Barnish2e998952013-06-11 16:31:10 +0100668drmmode_crtc_init(ScrnInfoPtr pScrn, struct drmmode_rec *drmmode, int num)
Rob Clark487687e2011-07-17 17:29:02 -0500669{
Rob Clark74210d52012-01-08 17:59:08 -0600670 xf86CrtcPtr crtc;
Dave Barnish2e998952013-06-11 16:31:10 +0100671 struct drmmode_crtc_private_rec *drmmode_crtc;
Rob Clark487687e2011-07-17 17:29:02 -0500672
Rob Clark74210d52012-01-08 17:59:08 -0600673 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -0500674
Rob Clark74210d52012-01-08 17:59:08 -0600675 crtc = xf86CrtcCreate(pScrn, &drmmode_crtc_funcs);
676 if (crtc == NULL)
677 return;
678
Dave Barnish2e998952013-06-11 16:31:10 +0100679 drmmode_crtc = xnfcalloc(sizeof(struct drmmode_crtc_private_rec), 1);
Rob Clark74210d52012-01-08 17:59:08 -0600680 drmmode_crtc->mode_crtc = drmModeGetCrtc(drmmode->fd,
681 drmmode->mode_res->crtcs[num]);
682 drmmode_crtc->drmmode = drmmode;
Dave Barnish2e998952013-06-11 16:31:10 +0100683 INFO_MSG("Got CRTC: %d", num);
Rob Clark74210d52012-01-08 17:59:08 -0600684
Rob Clark74210d52012-01-08 17:59:08 -0600685 crtc->driver_private = drmmode_crtc;
686
687 TRACE_EXIT();
Rob Clark487687e2011-07-17 17:29:02 -0500688 return;
Rob Clark74210d52012-01-08 17:59:08 -0600689}
Rob Clark487687e2011-07-17 17:29:02 -0500690
691static xf86OutputStatus
692drmmode_output_detect(xf86OutputPtr output)
693{
694 /* go to the hw and retrieve a new output struct */
Dave Barnish2e998952013-06-11 16:31:10 +0100695 struct drmmode_output_priv *drmmode_output = output->driver_private;
696 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500697 xf86OutputStatus status;
698 drmModeFreeConnector(drmmode_output->mode_output);
699
700 drmmode_output->mode_output =
Dave Barnish2e998952013-06-11 16:31:10 +0100701 drmModeGetConnector(drmmode->fd,
702 drmmode_output->output_id);
Rob Clark487687e2011-07-17 17:29:02 -0500703
704 switch (drmmode_output->mode_output->connection) {
705 case DRM_MODE_CONNECTED:
706 status = XF86OutputStatusConnected;
707 break;
708 case DRM_MODE_DISCONNECTED:
709 status = XF86OutputStatusDisconnected;
710 break;
711 default:
712 case DRM_MODE_UNKNOWNCONNECTION:
713 status = XF86OutputStatusUnknown;
714 break;
715 }
716 return status;
717}
718
719static Bool
720drmmode_output_mode_valid(xf86OutputPtr output, DisplayModePtr mode)
721{
722 if (mode->type & M_T_DEFAULT)
723 /* Default modes are harmful here. */
724 return MODE_BAD;
725
726 return MODE_OK;
727}
728
729static DisplayModePtr
730drmmode_output_get_modes(xf86OutputPtr output)
731{
732 ScrnInfoPtr pScrn = output->scrn;
Dave Barnish2e998952013-06-11 16:31:10 +0100733 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500734 drmModeConnectorPtr koutput = drmmode_output->mode_output;
Dave Barnish2e998952013-06-11 16:31:10 +0100735 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Dave Barnish0bdb7382013-06-06 12:04:04 +0100736 DisplayModePtr modes = NULL;
Daniel Kurtzb8131892012-10-17 01:39:54 +0800737 drmModePropertyPtr prop;
Rob Clark487687e2011-07-17 17:29:02 -0500738 xf86MonPtr ddc_mon = NULL;
739 int i;
740
741 /* look for an EDID property */
742 for (i = 0; i < koutput->count_props; i++) {
Daniel Kurtzb8131892012-10-17 01:39:54 +0800743 prop = drmModeGetProperty(drmmode->fd, koutput->props[i]);
744 if (!prop)
Rob Clark487687e2011-07-17 17:29:02 -0500745 continue;
746
Daniel Kurtzb8131892012-10-17 01:39:54 +0800747 if ((prop->flags & DRM_MODE_PROP_BLOB) &&
748 !strcmp(prop->name, "EDID")) {
Rob Clark487687e2011-07-17 17:29:02 -0500749 if (drmmode_output->edid_blob)
Dave Barnish2e998952013-06-11 16:31:10 +0100750 drmModeFreePropertyBlob(
751 drmmode_output->edid_blob);
Rob Clark487687e2011-07-17 17:29:02 -0500752 drmmode_output->edid_blob =
Rob Clark74210d52012-01-08 17:59:08 -0600753 drmModeGetPropertyBlob(drmmode->fd,
Dave Barnish2e998952013-06-11 16:31:10 +0100754 koutput->prop_values[i]);
Rob Clark487687e2011-07-17 17:29:02 -0500755 }
Daniel Kurtzb8131892012-10-17 01:39:54 +0800756 drmModeFreeProperty(prop);
Rob Clark487687e2011-07-17 17:29:02 -0500757 }
758
759 if (drmmode_output->edid_blob)
760 ddc_mon = xf86InterpretEDID(pScrn->scrnIndex,
Rob Clark74210d52012-01-08 17:59:08 -0600761 drmmode_output->edid_blob->data);
Rob Clark487687e2011-07-17 17:29:02 -0500762
763 if (ddc_mon) {
Rob Clark487687e2011-07-17 17:29:02 -0500764 xf86OutputSetEDID(output, ddc_mon);
765 xf86SetDDCproperties(pScrn, ddc_mon);
766 }
767
768 DEBUG_MSG("count_modes: %d", koutput->count_modes);
769
770 /* modes should already be available */
771 for (i = 0; i < koutput->count_modes; i++) {
Dave Barnish0bdb7382013-06-06 12:04:04 +0100772 DisplayModePtr mode = xnfalloc(sizeof(DisplayModeRec));
Rob Clark487687e2011-07-17 17:29:02 -0500773
Dave Barnish0bdb7382013-06-06 12:04:04 +0100774 drmmode_ConvertFromKMode(pScrn, &koutput->modes[i], mode);
775 modes = xf86ModesAdd(modes, mode);
Rob Clark487687e2011-07-17 17:29:02 -0500776 }
Dave Barnish0bdb7382013-06-06 12:04:04 +0100777 return modes;
Rob Clark487687e2011-07-17 17:29:02 -0500778}
779
780static void
781drmmode_output_destroy(xf86OutputPtr output)
782{
Dave Barnish2e998952013-06-11 16:31:10 +0100783 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500784 int i;
785
786 if (drmmode_output->edid_blob)
787 drmModeFreePropertyBlob(drmmode_output->edid_blob);
788 for (i = 0; i < drmmode_output->num_props; i++) {
789 drmModeFreeProperty(drmmode_output->props[i].mode_prop);
790 free(drmmode_output->props[i].atoms);
791 }
Daniel Kurtzf5a38ad2012-10-17 02:03:34 +0800792 free(drmmode_output->props);
Rob Clark487687e2011-07-17 17:29:02 -0500793 drmModeFreeConnector(drmmode_output->mode_output);
794 free(drmmode_output);
795 output->driver_private = NULL;
796}
797
798static void
799drmmode_output_dpms(xf86OutputPtr output, int mode)
800{
Dave Barnish2e998952013-06-11 16:31:10 +0100801 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500802 drmModeConnectorPtr koutput = drmmode_output->mode_output;
Daniel Kurtzb8131892012-10-17 01:39:54 +0800803 drmModePropertyPtr prop;
Dave Barnish2e998952013-06-11 16:31:10 +0100804 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500805 int mode_id = -1, i;
806
807 for (i = 0; i < koutput->count_props; i++) {
Daniel Kurtzb8131892012-10-17 01:39:54 +0800808 prop = drmModeGetProperty(drmmode->fd, koutput->props[i]);
809 if (!prop)
810 continue;
811 if ((prop->flags & DRM_MODE_PROP_ENUM) &&
812 !strcmp(prop->name, "DPMS")) {
813 mode_id = koutput->props[i];
814 drmModeFreeProperty(prop);
815 break;
Rob Clark487687e2011-07-17 17:29:02 -0500816 }
Daniel Kurtzb8131892012-10-17 01:39:54 +0800817 drmModeFreeProperty(prop);
Rob Clark487687e2011-07-17 17:29:02 -0500818 }
819
820 if (mode_id < 0)
821 return;
822
823 drmModeConnectorSetProperty(drmmode->fd, koutput->connector_id,
Rob Clark74210d52012-01-08 17:59:08 -0600824 mode_id, mode);
Rob Clark487687e2011-07-17 17:29:02 -0500825}
826
827static Bool
828drmmode_property_ignore(drmModePropertyPtr prop)
829{
830 if (!prop)
Rob Clark74210d52012-01-08 17:59:08 -0600831 return TRUE;
Rob Clark487687e2011-07-17 17:29:02 -0500832 /* ignore blob prop */
833 if (prop->flags & DRM_MODE_PROP_BLOB)
834 return TRUE;
835 /* ignore standard property */
836 if (!strcmp(prop->name, "EDID") ||
Rob Clark74210d52012-01-08 17:59:08 -0600837 !strcmp(prop->name, "DPMS"))
Rob Clark487687e2011-07-17 17:29:02 -0500838 return TRUE;
839
840 return FALSE;
841}
842
843static void
844drmmode_output_create_resources(xf86OutputPtr output)
845{
Dave Barnish2e998952013-06-11 16:31:10 +0100846 struct drmmode_output_priv *drmmode_output = output->driver_private;
Rob Clark487687e2011-07-17 17:29:02 -0500847 drmModeConnectorPtr mode_output = drmmode_output->mode_output;
Dave Barnish2e998952013-06-11 16:31:10 +0100848 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500849 drmModePropertyPtr drmmode_prop;
850 uint32_t value;
851 int i, j, err;
852
Dave Barnish2e998952013-06-11 16:31:10 +0100853 drmmode_output->props =
854 calloc(mode_output->count_props,
855 sizeof(struct drmmode_prop_rec));
Rob Clark487687e2011-07-17 17:29:02 -0500856 if (!drmmode_output->props)
857 return;
858
859 drmmode_output->num_props = 0;
Dave Barnish0bdb7382013-06-06 12:04:04 +0100860 for (i = 0; i < mode_output->count_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +0100861 drmmode_prop = drmModeGetProperty(drmmode->fd,
862 mode_output->props[i]);
Rob Clark487687e2011-07-17 17:29:02 -0500863 if (drmmode_property_ignore(drmmode_prop)) {
864 drmModeFreeProperty(drmmode_prop);
865 continue;
866 }
Dave Barnish2e998952013-06-11 16:31:10 +0100867 drmmode_output->props[drmmode_output->num_props].mode_prop =
868 drmmode_prop;
Dave Barnish0bdb7382013-06-06 12:04:04 +0100869 drmmode_output->props[drmmode_output->num_props].index = i;
Rob Clark487687e2011-07-17 17:29:02 -0500870 drmmode_output->num_props++;
Rob Clark487687e2011-07-17 17:29:02 -0500871 }
872
873 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +0100874 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -0500875 drmmode_prop = p->mode_prop;
876
877 value = drmmode_output->mode_output->prop_values[p->index];
878
879 if (drmmode_prop->flags & DRM_MODE_PROP_RANGE) {
880 INT32 range[2];
881
882 p->num_atoms = 1;
883 p->atoms = calloc(p->num_atoms, sizeof(Atom));
884 if (!p->atoms)
885 continue;
Dave Barnish2e998952013-06-11 16:31:10 +0100886 p->atoms[0] = MakeAtom(drmmode_prop->name,
887 strlen(drmmode_prop->name),
888 TRUE);
Rob Clark487687e2011-07-17 17:29:02 -0500889 range[0] = drmmode_prop->values[0];
890 range[1] = drmmode_prop->values[1];
Dave Barnish2e998952013-06-11 16:31:10 +0100891 err = RRConfigureOutputProperty(output->randr_output,
892 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -0600893 FALSE, TRUE,
Dave Barnish2e998952013-06-11 16:31:10 +0100894 drmmode_prop->flags &
895 DRM_MODE_PROP_IMMUTABLE ?
896 TRUE : FALSE,
897 2, range);
898
899 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -0500900 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +0100901 "RRConfigureOutputProperty error, %d\n",
902 err);
903
904 err = RRChangeOutputProperty(output->randr_output,
905 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -0600906 XA_INTEGER, 32, PropModeReplace, 1,
907 &value, FALSE, FALSE);
Dave Barnish2e998952013-06-11 16:31:10 +0100908 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -0500909 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +0100910 "RRChangeOutputProperty error, %d\n",
911 err);
912
Rob Clark487687e2011-07-17 17:29:02 -0500913 } else if (drmmode_prop->flags & DRM_MODE_PROP_ENUM) {
914 p->num_atoms = drmmode_prop->count_enums + 1;
915 p->atoms = calloc(p->num_atoms, sizeof(Atom));
916 if (!p->atoms)
917 continue;
Dave Barnish2e998952013-06-11 16:31:10 +0100918 p->atoms[0] = MakeAtom(drmmode_prop->name,
919 strlen(drmmode_prop->name),
920 TRUE);
Rob Clark487687e2011-07-17 17:29:02 -0500921 for (j = 1; j <= drmmode_prop->count_enums; j++) {
Dave Barnish2e998952013-06-11 16:31:10 +0100922 struct drm_mode_property_enum *e =
923 &drmmode_prop->enums[j-1];
924 p->atoms[j] = MakeAtom(e->name,
925 strlen(e->name), TRUE);
Rob Clark487687e2011-07-17 17:29:02 -0500926 }
Dave Barnish2e998952013-06-11 16:31:10 +0100927 err = RRConfigureOutputProperty(output->randr_output,
928 p->atoms[0],
Rob Clark74210d52012-01-08 17:59:08 -0600929 FALSE, FALSE,
Dave Barnish2e998952013-06-11 16:31:10 +0100930 drmmode_prop->flags &
931 DRM_MODE_PROP_IMMUTABLE ?
932 TRUE : FALSE,
933 p->num_atoms - 1,
934 (INT32 *)&p->atoms[1]);
935
936 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -0500937 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +0100938 "RRConfigureOutputProperty error, %d\n",
939 err);
940
Rob Clark487687e2011-07-17 17:29:02 -0500941 for (j = 0; j < drmmode_prop->count_enums; j++)
942 if (drmmode_prop->enums[j].value == value)
943 break;
944 /* there's always a matching value */
Dave Barnish2e998952013-06-11 16:31:10 +0100945 err = RRChangeOutputProperty(output->randr_output,
946 p->atoms[0],
947 XA_ATOM, 32, PropModeReplace, 1,
948 &p->atoms[j+1], FALSE, FALSE);
949 if (err != 0)
Rob Clark487687e2011-07-17 17:29:02 -0500950 xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
Dave Barnish2e998952013-06-11 16:31:10 +0100951 "RRChangeOutputProperty error, %d\n",
952 err);
Rob Clark487687e2011-07-17 17:29:02 -0500953 }
954 }
955}
956
957static Bool
958drmmode_output_set_property(xf86OutputPtr output, Atom property,
Rob Clark74210d52012-01-08 17:59:08 -0600959 RRPropertyValuePtr value)
Rob Clark487687e2011-07-17 17:29:02 -0500960{
Dave Barnish2e998952013-06-11 16:31:10 +0100961 struct drmmode_output_priv *drmmode_output = output->driver_private;
962 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -0500963 int i, ret;
964
965 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +0100966 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -0500967
968 if (p->atoms[0] != property)
969 continue;
970
971 if (p->mode_prop->flags & DRM_MODE_PROP_RANGE) {
972 uint32_t val;
973
974 if (value->type != XA_INTEGER || value->format != 32 ||
Rob Clark74210d52012-01-08 17:59:08 -0600975 value->size != 1)
Rob Clark487687e2011-07-17 17:29:02 -0500976 return FALSE;
977 val = *(uint32_t *)value->data;
978
Dave Barnish2e998952013-06-11 16:31:10 +0100979 ret = drmModeConnectorSetProperty(drmmode->fd,
980 drmmode_output->output_id,
Rob Clark74210d52012-01-08 17:59:08 -0600981 p->mode_prop->prop_id, (uint64_t)val);
Rob Clark487687e2011-07-17 17:29:02 -0500982
983 if (ret)
984 return FALSE;
985
986 return TRUE;
987
988 } else if (p->mode_prop->flags & DRM_MODE_PROP_ENUM) {
989 Atom atom;
990 const char *name;
991 int j;
992
Dave Barnish2e998952013-06-11 16:31:10 +0100993 if (value->type != XA_ATOM ||
994 value->format != 32 ||
995 value->size != 1)
Rob Clark487687e2011-07-17 17:29:02 -0500996 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +0100997
Rob Clark487687e2011-07-17 17:29:02 -0500998 memcpy(&atom, value->data, 4);
999 name = NameForAtom(atom);
Daniel Kurtz23148372013-03-27 20:19:45 +08001000 if (name == NULL)
1001 return FALSE;
Rob Clark487687e2011-07-17 17:29:02 -05001002
Dave Barnish2e998952013-06-11 16:31:10 +01001003 /* search for matching name string, then
1004 * set its value down
1005 */
Rob Clark487687e2011-07-17 17:29:02 -05001006 for (j = 0; j < p->mode_prop->count_enums; j++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001007 if (!strcmp(p->mode_prop->enums[j].name,
1008 name)) {
1009 ret = drmModeConnectorSetProperty(
1010 drmmode->fd,
1011 drmmode_output->output_id,
1012 p->mode_prop->prop_id,
1013 p->mode_prop->enums[j].value);
Rob Clark487687e2011-07-17 17:29:02 -05001014
1015 if (ret)
1016 return FALSE;
1017
1018 return TRUE;
1019 }
1020 }
Rob Clark487687e2011-07-17 17:29:02 -05001021 return FALSE;
1022 }
1023 }
Rob Clark487687e2011-07-17 17:29:02 -05001024 return TRUE;
1025}
1026
1027static Bool
1028drmmode_output_get_property(xf86OutputPtr output, Atom property)
1029{
1030
Dave Barnish2e998952013-06-11 16:31:10 +01001031 struct drmmode_output_priv *drmmode_output = output->driver_private;
1032 struct drmmode_rec *drmmode = drmmode_output->drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001033 uint32_t value;
1034 int err, i;
1035
1036 if (output->scrn->vtSema) {
1037 drmModeFreeConnector(drmmode_output->mode_output);
1038 drmmode_output->mode_output =
Dave Barnish2e998952013-06-11 16:31:10 +01001039 drmModeGetConnector(drmmode->fd,
1040 drmmode_output->output_id);
Rob Clark487687e2011-07-17 17:29:02 -05001041 }
1042
1043 for (i = 0; i < drmmode_output->num_props; i++) {
Dave Barnish2e998952013-06-11 16:31:10 +01001044 struct drmmode_prop_rec *p = &drmmode_output->props[i];
Rob Clark487687e2011-07-17 17:29:02 -05001045 if (p->atoms[0] != property)
1046 continue;
1047
1048 value = drmmode_output->mode_output->prop_values[p->index];
1049
1050 if (p->mode_prop->flags & DRM_MODE_PROP_RANGE) {
1051 err = RRChangeOutputProperty(output->randr_output,
Rob Clark74210d52012-01-08 17:59:08 -06001052 property, XA_INTEGER, 32,
1053 PropModeReplace, 1, &value,
1054 FALSE, FALSE);
Rob Clark487687e2011-07-17 17:29:02 -05001055
1056 return !err;
1057 } else if (p->mode_prop->flags & DRM_MODE_PROP_ENUM) {
1058 int j;
1059
Dave Barnish2e998952013-06-11 16:31:10 +01001060 /* search for matching name string, then set
1061 * its value down
1062 */
Rob Clark487687e2011-07-17 17:29:02 -05001063 for (j = 0; j < p->mode_prop->count_enums; j++) {
1064 if (p->mode_prop->enums[j].value == value)
1065 break;
1066 }
1067
Dave Barnish2e998952013-06-11 16:31:10 +01001068 err = RRChangeOutputProperty(output->randr_output,
1069 property,
1070 XA_ATOM, 32, PropModeReplace, 1,
1071 &p->atoms[j+1], FALSE, FALSE);
Rob Clark487687e2011-07-17 17:29:02 -05001072
1073 return !err;
1074 }
1075 }
1076
1077 return FALSE;
1078}
1079
1080static const xf86OutputFuncsRec drmmode_output_funcs = {
Rob Clark74210d52012-01-08 17:59:08 -06001081 .create_resources = drmmode_output_create_resources,
1082 .dpms = drmmode_output_dpms,
1083 .detect = drmmode_output_detect,
1084 .mode_valid = drmmode_output_mode_valid,
1085 .get_modes = drmmode_output_get_modes,
1086 .set_property = drmmode_output_set_property,
1087 .get_property = drmmode_output_get_property,
1088 .destroy = drmmode_output_destroy
Rob Clark487687e2011-07-17 17:29:02 -05001089};
1090
Rob Clark487687e2011-07-17 17:29:02 -05001091const char *output_names[] = { "None",
Rob Clark74210d52012-01-08 17:59:08 -06001092 "VGA",
1093 "DVI-I",
1094 "DVI-D",
1095 "DVI-A",
1096 "Composite",
1097 "SVIDEO",
1098 "LVDS",
1099 "CTV",
1100 "DIN",
1101 "DP",
1102 "HDMI",
1103 "HDMI",
1104 "TV",
1105 "eDP",
Rob Clark487687e2011-07-17 17:29:02 -05001106};
1107#define NUM_OUTPUT_NAMES (sizeof(output_names) / sizeof(output_names[0]))
1108
1109static void
Dave Barnish2e998952013-06-11 16:31:10 +01001110drmmode_output_init(ScrnInfoPtr pScrn, struct drmmode_rec *drmmode, int num)
Rob Clark487687e2011-07-17 17:29:02 -05001111{
Rob Clark74210d52012-01-08 17:59:08 -06001112 xf86OutputPtr output;
1113 drmModeConnectorPtr koutput;
1114 drmModeEncoderPtr kencoder;
Dave Barnish2e998952013-06-11 16:31:10 +01001115 struct drmmode_output_priv *drmmode_output;
Rob Clark74210d52012-01-08 17:59:08 -06001116 char name[32];
Rob Clark487687e2011-07-17 17:29:02 -05001117
Rob Clark74210d52012-01-08 17:59:08 -06001118 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001119
Rob Clark74210d52012-01-08 17:59:08 -06001120 koutput = drmModeGetConnector(drmmode->fd,
1121 drmmode->mode_res->connectors[num]);
1122 if (!koutput)
1123 return;
Rob Clark487687e2011-07-17 17:29:02 -05001124
Rob Clark74210d52012-01-08 17:59:08 -06001125 kencoder = drmModeGetEncoder(drmmode->fd, koutput->encoders[0]);
1126 if (!kencoder) {
1127 drmModeFreeConnector(koutput);
1128 return;
1129 }
Rob Clark487687e2011-07-17 17:29:02 -05001130
Rob Clark74210d52012-01-08 17:59:08 -06001131 if (koutput->connector_type >= NUM_OUTPUT_NAMES)
1132 snprintf(name, 32, "Unknown%d-%d", koutput->connector_type,
1133 koutput->connector_type_id);
1134 else
1135 snprintf(name, 32, "%s-%d",
1136 output_names[koutput->connector_type],
1137 koutput->connector_type_id);
Rob Clark487687e2011-07-17 17:29:02 -05001138
Rob Clark74210d52012-01-08 17:59:08 -06001139 output = xf86OutputCreate(pScrn, &drmmode_output_funcs, name);
1140 if (!output) {
1141 drmModeFreeEncoder(kencoder);
1142 drmModeFreeConnector(koutput);
1143 return;
1144 }
Rob Clark487687e2011-07-17 17:29:02 -05001145
Dave Barnish2e998952013-06-11 16:31:10 +01001146 drmmode_output = calloc(sizeof(struct drmmode_output_priv), 1);
Rob Clark74210d52012-01-08 17:59:08 -06001147 if (!drmmode_output) {
1148 xf86OutputDestroy(output);
1149 drmModeFreeConnector(koutput);
1150 drmModeFreeEncoder(kencoder);
1151 return;
1152 }
Rob Clark487687e2011-07-17 17:29:02 -05001153
Rob Clark74210d52012-01-08 17:59:08 -06001154 drmmode_output->output_id = drmmode->mode_res->connectors[num];
1155 drmmode_output->mode_output = koutput;
1156 drmmode_output->mode_encoder = kencoder;
1157 drmmode_output->drmmode = drmmode;
Rob Clark487687e2011-07-17 17:29:02 -05001158
Rob Clark74210d52012-01-08 17:59:08 -06001159 output->mm_width = koutput->mmWidth;
1160 output->mm_height = koutput->mmHeight;
1161 output->driver_private = drmmode_output;
Rob Clark487687e2011-07-17 17:29:02 -05001162
Dave Barnishf0952c12013-05-10 15:52:23 +01001163 if (ARMSOCPTR(pScrn)->crtcNum >= 0) {
1164 /* Only single crtc per screen - see if this output can use it*/
1165 output->possible_crtcs =
Dave Barnish2e998952013-06-11 16:31:10 +01001166 (kencoder->possible_crtcs >>
1167 (ARMSOCPTR(pScrn)->crtcNum)
1168 )&1;
1169 } else
Dave Barnishf0952c12013-05-10 15:52:23 +01001170 output->possible_crtcs = kencoder->possible_crtcs;
Dave Barnishf0952c12013-05-10 15:52:23 +01001171
Rob Clark74210d52012-01-08 17:59:08 -06001172 output->possible_clones = kencoder->possible_clones;
1173 output->interlaceAllowed = TRUE;
Rob Clark487687e2011-07-17 17:29:02 -05001174
Rob Clark74210d52012-01-08 17:59:08 -06001175 TRACE_EXIT();
1176 return;
1177}
Rob Clark487687e2011-07-17 17:29:02 -05001178
Paul Geary8ffd91c2013-04-11 16:03:15 +01001179void set_scanout_bo(ScrnInfoPtr pScrn, struct armsoc_bo *bo)
David Garbett3688b332012-05-11 12:17:34 +01001180{
Dave Barnish2e998952013-06-11 16:31:10 +01001181 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
David Garbett3688b332012-05-11 12:17:34 +01001182
1183 /* It had better have a framebuffer if we're scanning it out */
Paul Geary8ffd91c2013-04-11 16:03:15 +01001184 assert(armsoc_bo_get_fb(bo));
David Garbett3688b332012-05-11 12:17:34 +01001185
Paul Geary8ffd91c2013-04-11 16:03:15 +01001186 pARMSOC->scanout = bo;
David Garbett3688b332012-05-11 12:17:34 +01001187}
1188
Rob Clark487687e2011-07-17 17:29:02 -05001189static Bool
1190drmmode_xf86crtc_resize(ScrnInfoPtr pScrn, int width, int height)
1191{
Dave Barnish2e998952013-06-11 16:31:10 +01001192 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001193 ScreenPtr pScreen = pScrn->pScreen;
Sean Paul3e107302012-08-30 12:08:12 -07001194 uint32_t pitch;
Brian Starkeycd684422012-09-20 09:28:04 +01001195 int i;
1196 xf86CrtcConfigPtr xf86_config;
Rob Clark487687e2011-07-17 17:29:02 -05001197
1198 TRACE_ENTER();
Dave Barnish33866052013-03-18 10:57:40 +00001199 DEBUG_MSG("Resize: %dx%d", width, height);
Rob Clark487687e2011-07-17 17:29:02 -05001200
1201 pScrn->virtualX = width;
1202 pScrn->virtualY = height;
1203
Dave Barnish2e998952013-06-11 16:31:10 +01001204 if ((width != armsoc_bo_width(pARMSOC->scanout))
Paul Geary8ffd91c2013-04-11 16:03:15 +01001205 || (height != armsoc_bo_height(pARMSOC->scanout))
Dave Barnish2e998952013-06-11 16:31:10 +01001206 || (pScrn->bitsPerPixel != armsoc_bo_bpp(pARMSOC->scanout))) {
Dave Barnish33866052013-03-18 10:57:40 +00001207 struct armsoc_bo *new_scanout;
Rob Clark487687e2011-07-17 17:29:02 -05001208
1209 /* allocate new scanout buffer */
Dave Barnish2e998952013-06-11 16:31:10 +01001210 new_scanout = armsoc_bo_new_with_dim(pARMSOC->dev,
1211 width, height,
1212 pScrn->depth, pScrn->bitsPerPixel,
1213 ARMSOC_BO_SCANOUT);
1214 if (!new_scanout) {
1215 /* Try to use the previous buffer if the new resolution
1216 * is smaller than the one on buffer creation
1217 */
1218 DEBUG_MSG(
1219 "allocate new scanout buffer failed - resizing existing bo");
Dave Barnish33866052013-03-18 10:57:40 +00001220 /* Remove the old fb from the bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001221 if (armsoc_bo_rm_fb(pARMSOC->scanout))
Dave Barnish33866052013-03-18 10:57:40 +00001222 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +01001223
Dave Barnish33866052013-03-18 10:57:40 +00001224 /* Resize the bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001225 if (armsoc_bo_resize(pARMSOC->scanout, width, height)) {
Dave Barnish33866052013-03-18 10:57:40 +00001226 armsoc_bo_clear(pARMSOC->scanout);
Ray Smithb4299f82013-03-13 10:08:36 +00001227 if (armsoc_bo_add_fb(pARMSOC->scanout))
1228 ERROR_MSG(
1229 "Failed to add framebuffer to the existing scanout buffer");
Dave Barnish33866052013-03-18 10:57:40 +00001230 return FALSE;
1231 }
Ray Smithb4299f82013-03-13 10:08:36 +00001232
Dave Barnish33866052013-03-18 10:57:40 +00001233 /* Add new fb to the bo */
Ray Smithb4299f82013-03-13 10:08:36 +00001234 if (armsoc_bo_clear(pARMSOC->scanout))
Dave Barnish33866052013-03-18 10:57:40 +00001235 return FALSE;
Dave Barnish2e998952013-06-11 16:31:10 +01001236
Ray Smithb4299f82013-03-13 10:08:36 +00001237 if (armsoc_bo_add_fb(pARMSOC->scanout)) {
1238 ERROR_MSG(
1239 "Failed to add framebuffer to the existing scanout buffer");
1240 return FALSE;
1241 }
1242
Dave Barnish33866052013-03-18 10:57:40 +00001243 pitch = armsoc_bo_pitch(pARMSOC->scanout);
Dave Barnish2e998952013-06-11 16:31:10 +01001244 } else {
Dave Barnish33866052013-03-18 10:57:40 +00001245 DEBUG_MSG("allocated new scanout buffer okay");
1246 pitch = armsoc_bo_pitch(new_scanout);
1247 /* clear new BO and add FB */
Ray Smithb4299f82013-03-13 10:08:36 +00001248 if (armsoc_bo_clear(new_scanout)) {
Paul Geary8ffd91c2013-04-11 16:03:15 +01001249 armsoc_bo_unreference(new_scanout);
David Garbettae5a6362012-07-02 10:15:47 +01001250 return FALSE;
1251 }
Ray Smithb4299f82013-03-13 10:08:36 +00001252
1253 if (armsoc_bo_add_fb(new_scanout)) {
1254 ERROR_MSG(
1255 "Failed to add framebuffer to the new scanout buffer");
1256 armsoc_bo_unreference(new_scanout);
1257 return FALSE;
1258 }
1259
Dave Barnish33866052013-03-18 10:57:40 +00001260 /* Handle dma_buf fd that may be attached to old bo */
Dave Barnish2e998952013-06-11 16:31:10 +01001261 if (armsoc_bo_has_dmabuf(pARMSOC->scanout)) {
Dave Barnish33866052013-03-18 10:57:40 +00001262 int res;
1263
1264 armsoc_bo_clear_dmabuf(pARMSOC->scanout);
1265 res = armsoc_bo_set_dmabuf(new_scanout);
Dave Barnish2e998952013-06-11 16:31:10 +01001266 if (res) {
1267 ERROR_MSG(
1268 "Unable to attach dma_buf fd to new scanout buffer - %d (%s)\n",
1269 res, strerror(res));
Dave Barnish33866052013-03-18 10:57:40 +00001270 armsoc_bo_unreference(new_scanout);
1271 return FALSE;
1272 }
1273 }
1274 /* delete old scanout buffer */
1275 armsoc_bo_unreference(pARMSOC->scanout);
1276 /* use new scanout buffer */
1277 set_scanout_bo(pScrn, new_scanout);
David Garbettae5a6362012-07-02 10:15:47 +01001278 }
Daniel Kurtz34e72b02012-10-19 14:02:05 -07001279 pScrn->displayWidth = pitch / ((pScrn->bitsPerPixel + 7) / 8);
Dave Barnish2e998952013-06-11 16:31:10 +01001280 } else
Paul Geary8ffd91c2013-04-11 16:03:15 +01001281 pitch = armsoc_bo_pitch(pARMSOC->scanout);
Rob Clark487687e2011-07-17 17:29:02 -05001282
1283 if (pScreen && pScreen->ModifyPixmapHeader) {
1284 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
Dave Barnish33866052013-03-18 10:57:40 +00001285
Rob Clark487687e2011-07-17 17:29:02 -05001286 pScreen->ModifyPixmapHeader(rootPixmap,
1287 pScrn->virtualX, pScrn->virtualY,
1288 pScrn->depth, pScrn->bitsPerPixel, pitch,
Paul Geary8ffd91c2013-04-11 16:03:15 +01001289 armsoc_bo_map(pARMSOC->scanout));
Ray Smithedccfd82013-05-31 15:57:12 +01001290
Dave Barnishe762b122013-08-12 17:13:46 +01001291 /* Bump the serial number to ensure that all existing DRI2
1292 * buffers are invalidated.
Ray Smithedccfd82013-05-31 15:57:12 +01001293 *
Dave Barnishe762b122013-08-12 17:13:46 +01001294 * This is particularly required for when the resolution is
1295 * changed and then reverted to the original size without a
1296 * DRI2 client/s getting a new buffer. Without this, the
1297 * drawable is the same size and serial number so the old
1298 * DRI2Buffer will be returned, even though the backing buffer
1299 * has been deleted.
Ray Smithedccfd82013-05-31 15:57:12 +01001300 */
1301 rootPixmap->drawable.serialNumber = NEXT_SERIAL_NUMBER;
Rob Clark487687e2011-07-17 17:29:02 -05001302 }
1303
Brian Starkeycd684422012-09-20 09:28:04 +01001304 /* Framebuffer needs to be reset on all CRTCs, not just
1305 * those that have repositioned */
1306 xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
1307 for (i = 0; i < xf86_config->num_crtc; i++) {
1308 xf86CrtcPtr crtc = xf86_config->crtc[i];
1309
1310 if (!crtc->enabled)
1311 continue;
1312
1313 drmmode_set_mode_major(crtc, &crtc->mode,
1314 crtc->rotation, crtc->x, crtc->y);
1315 }
1316
Rob Clark487687e2011-07-17 17:29:02 -05001317 TRACE_EXIT();
1318 return TRUE;
Rob Clark74210d52012-01-08 17:59:08 -06001319}
Rob Clark487687e2011-07-17 17:29:02 -05001320
1321static const xf86CrtcConfigFuncsRec drmmode_xf86crtc_config_funcs = {
Rob Clark74210d52012-01-08 17:59:08 -06001322 drmmode_xf86crtc_resize
Rob Clark487687e2011-07-17 17:29:02 -05001323};
1324
1325
1326Bool drmmode_pre_init(ScrnInfoPtr pScrn, int fd, int cpp)
1327{
Dave Barnish2e998952013-06-11 16:31:10 +01001328 struct drmmode_rec *drmmode;
Rob Clark74210d52012-01-08 17:59:08 -06001329 int i;
Rob Clark487687e2011-07-17 17:29:02 -05001330
Rob Clark74210d52012-01-08 17:59:08 -06001331 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001332
Rob Clark74210d52012-01-08 17:59:08 -06001333 drmmode = calloc(1, sizeof *drmmode);
Dave Barnish2e998952013-06-11 16:31:10 +01001334 if (!drmmode)
Paul Geary6fe52f32013-04-03 11:12:24 +01001335 return FALSE;
Paul Geary6fe52f32013-04-03 11:12:24 +01001336
Rob Clark74210d52012-01-08 17:59:08 -06001337 drmmode->fd = fd;
Rob Clark487687e2011-07-17 17:29:02 -05001338
Rob Clark74210d52012-01-08 17:59:08 -06001339 xf86CrtcConfigInit(pScrn, &drmmode_xf86crtc_config_funcs);
Rob Clark487687e2011-07-17 17:29:02 -05001340
1341
Rob Clark74210d52012-01-08 17:59:08 -06001342 drmmode->cpp = cpp;
1343 drmmode->mode_res = drmModeGetResources(drmmode->fd);
1344 if (!drmmode->mode_res) {
Paul Geary6fe52f32013-04-03 11:12:24 +01001345 free(drmmode);
Rob Clark74210d52012-01-08 17:59:08 -06001346 return FALSE;
1347 } else {
1348 DEBUG_MSG("Got KMS resources");
1349 DEBUG_MSG(" %d connectors, %d encoders",
1350 drmmode->mode_res->count_connectors,
1351 drmmode->mode_res->count_encoders);
1352 DEBUG_MSG(" %d crtcs, %d fbs",
Dave Barnish2e998952013-06-11 16:31:10 +01001353 drmmode->mode_res->count_crtcs,
1354 drmmode->mode_res->count_fbs);
Rob Clark74210d52012-01-08 17:59:08 -06001355 DEBUG_MSG(" %dx%d minimum resolution",
Dave Barnish2e998952013-06-11 16:31:10 +01001356 drmmode->mode_res->min_width,
1357 drmmode->mode_res->min_height);
Rob Clark74210d52012-01-08 17:59:08 -06001358 DEBUG_MSG(" %dx%d maximum resolution",
Dave Barnish2e998952013-06-11 16:31:10 +01001359 drmmode->mode_res->max_width,
1360 drmmode->mode_res->max_height);
Rob Clark74210d52012-01-08 17:59:08 -06001361 }
1362 xf86CrtcSetSizeRange(pScrn, 320, 200, drmmode->mode_res->max_width,
1363 drmmode->mode_res->max_height);
Dave Barnishf0952c12013-05-10 15:52:23 +01001364
Dave Barnish2e998952013-06-11 16:31:10 +01001365 if (ARMSOCPTR(pScrn)->crtcNum == -1) {
Dave Barnishf0952c12013-05-10 15:52:23 +01001366 INFO_MSG("Adding all CRTCs");
1367 for (i = 0; i < drmmode->mode_res->count_crtcs; i++)
1368 drmmode_crtc_init(pScrn, drmmode, i);
Dave Barnish2e998952013-06-11 16:31:10 +01001369 } else if (ARMSOCPTR(pScrn)->crtcNum < drmmode->mode_res->count_crtcs) {
Dave Barnishf0952c12013-05-10 15:52:23 +01001370 drmmode_crtc_init(pScrn, drmmode, ARMSOCPTR(pScrn)->crtcNum);
1371 } else {
Dave Barnish2e998952013-06-11 16:31:10 +01001372 ERROR_MSG(
1373 "Specified more Screens in xorg.conf than there are DRM CRTCs");
Dave Barnishf0952c12013-05-10 15:52:23 +01001374 return FALSE;
1375 }
Rob Clark487687e2011-07-17 17:29:02 -05001376
Dave Barnish2e998952013-06-11 16:31:10 +01001377 if (ARMSOCPTR(pScrn)->crtcNum != -1) {
1378 if (ARMSOCPTR(pScrn)->crtcNum <
1379 drmmode->mode_res->count_connectors)
1380 drmmode_output_init(pScrn,
1381 drmmode, ARMSOCPTR(pScrn)->crtcNum);
Ray Smithc9c2faf2013-06-07 15:37:19 +01001382 else
1383 return FALSE;
1384 } else {
1385 for (i = 0; i < drmmode->mode_res->count_connectors; i++)
1386 drmmode_output_init(pScrn, drmmode, i);
1387 }
Rob Clark487687e2011-07-17 17:29:02 -05001388
Rob Clark74210d52012-01-08 17:59:08 -06001389 xf86InitialConfiguration(pScrn, TRUE);
Rob Clark487687e2011-07-17 17:29:02 -05001390
Rob Clark74210d52012-01-08 17:59:08 -06001391 TRACE_EXIT();
Rob Clark487687e2011-07-17 17:29:02 -05001392
Rob Clark74210d52012-01-08 17:59:08 -06001393 return TRUE;
1394}
Rob Clark487687e2011-07-17 17:29:02 -05001395
1396void
Cooper Yuana83caa62012-06-28 17:19:06 +02001397drmmode_adjust_frame(ScrnInfoPtr pScrn, int x, int y)
Rob Clark487687e2011-07-17 17:29:02 -05001398{
1399 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
1400 xf86OutputPtr output = config->output[config->compat_output];
1401 xf86CrtcPtr crtc = output->crtc;
1402
1403 if (!crtc || !crtc->enabled)
1404 return;
1405
1406 drmmode_set_mode_major(crtc, &crtc->mode, crtc->rotation, x, y);
1407}
1408
Rob Clark4b8f30a2011-08-28 12:51:26 -05001409/*
1410 * Page Flipping
1411 */
1412
1413static void
1414page_flip_handler(int fd, unsigned int sequence, unsigned int tv_sec,
Rob Clark74210d52012-01-08 17:59:08 -06001415 unsigned int tv_usec, void *user_data)
Rob Clark4b8f30a2011-08-28 12:51:26 -05001416{
Paul Geary8ffd91c2013-04-11 16:03:15 +01001417 ARMSOCDRI2SwapComplete(user_data);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001418}
1419
1420static drmEventContext event_context = {
1421 .version = DRM_EVENT_CONTEXT_VERSION,
1422 .page_flip_handler = page_flip_handler,
1423};
1424
John Sheu022833e2012-08-15 11:40:11 -07001425int
Rob Clark4b8f30a2011-08-28 12:51:26 -05001426drmmode_page_flip(DrawablePtr draw, uint32_t fb_id, void *priv)
1427{
Ray Smith3c33c3d2013-03-26 16:06:37 +00001428 ScrnInfoPtr pScrn = xf86Screens[draw->pScreen->myNum];
Dave Barnish2e998952013-06-11 16:31:10 +01001429 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
Ray Smith3c33c3d2013-03-26 16:06:37 +00001430 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
Dave Barnish2e998952013-06-11 16:31:10 +01001431 struct drmmode_crtc_private_rec *crtc = config->crtc[0]->driver_private;
1432 struct drmmode_rec *mode = crtc->drmmode;
John Sheu022833e2012-08-15 11:40:11 -07001433 int ret, i, failed = 0, num_flipped = 0;
Raymond Smith16a910e2012-05-09 13:04:51 +01001434 unsigned int flags = 0;
1435
Dave Barnishde45ed42013-06-05 13:47:56 +01001436 if (pARMSOC->drmmode_interface->use_page_flip_events)
Ray Smith3c33c3d2013-03-26 16:06:37 +00001437 flags |= DRM_MODE_PAGE_FLIP_EVENT;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001438
1439 /* if we can flip, we must be fullscreen.. so flip all CRTC's.. */
1440 for (i = 0; i < config->num_crtc; i++) {
1441 crtc = config->crtc[i]->driver_private;
1442
Ray Smithe20b3812013-04-08 12:53:53 +01001443 if (!config->crtc[i]->enabled)
1444 continue;
1445
Rob Clark4b8f30a2011-08-28 12:51:26 -05001446 ret = drmModePageFlip(mode->fd, crtc->mode_crtc->crtc_id,
Raymond Smith16a910e2012-05-09 13:04:51 +01001447 fb_id, flags, priv);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001448 if (ret) {
Ray Smith3c33c3d2013-03-26 16:06:37 +00001449 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
Dave Barnish2e998952013-06-11 16:31:10 +01001450 "flip queue failed: %s\n",
1451 strerror(errno));
John Sheu022833e2012-08-15 11:40:11 -07001452 failed = 1;
Dave Barnish2e998952013-06-11 16:31:10 +01001453 } else
John Sheu022833e2012-08-15 11:40:11 -07001454 num_flipped += 1;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001455 }
1456
John Sheu022833e2012-08-15 11:40:11 -07001457 if (failed)
1458 return -(num_flipped + 1);
1459 else
1460 return num_flipped;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001461}
Rob Clark487687e2011-07-17 17:29:02 -05001462
1463/*
1464 * Hot Plug Event handling:
David Garbettdbecfdd2013-08-28 10:45:03 +01001465 * TODO: MIDEGL-1441: Do we need to keep this handler, which
1466 * Rob originally wrote?
Rob Clark487687e2011-07-17 17:29:02 -05001467 */
Rob Clark487687e2011-07-17 17:29:02 -05001468static void
1469drmmode_handle_uevents(int fd, void *closure)
1470{
Rob Clark74210d52012-01-08 17:59:08 -06001471 ScrnInfoPtr pScrn = closure;
Dave Barnish2e998952013-06-11 16:31:10 +01001472 struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn);
1473 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark74210d52012-01-08 17:59:08 -06001474 struct udev_device *dev;
1475 const char *hotplug;
1476 struct stat s;
1477 dev_t udev_devnum;
Rob Clark487687e2011-07-17 17:29:02 -05001478
Rob Clark74210d52012-01-08 17:59:08 -06001479 dev = udev_monitor_receive_device(drmmode->uevent_monitor);
1480 if (!dev)
1481 return;
Rob Clark487687e2011-07-17 17:29:02 -05001482
Rob Clark74210d52012-01-08 17:59:08 -06001483 /*
1484 * Check to make sure this event is directed at our
1485 * device (by comparing dev_t values), then make
1486 * sure it's a hotplug event (HOTPLUG=1)
1487 */
1488 udev_devnum = udev_device_get_devnum(dev);
David Garbettdbecfdd2013-08-28 10:45:03 +01001489 if (fstat(pARMSOC->drmFD, &s)) {
1490 ERROR_MSG("fstat failed: %s", strerror(errno));
1491 udev_device_unref(dev);
1492 return;
1493 }
Rob Clark487687e2011-07-17 17:29:02 -05001494
Rob Clark74210d52012-01-08 17:59:08 -06001495 hotplug = udev_device_get_property_value(dev, "HOTPLUG");
Rob Clark487687e2011-07-17 17:29:02 -05001496
Rob Clark74210d52012-01-08 17:59:08 -06001497 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "hotplug=%s, match=%d\n", hotplug,
Dave Barnish2e998952013-06-11 16:31:10 +01001498 !memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)));
Rob Clark487687e2011-07-17 17:29:02 -05001499
Dave Barnish2e998952013-06-11 16:31:10 +01001500 if (memcmp(&s.st_rdev, &udev_devnum, sizeof(dev_t)) == 0 &&
Rob Clark74210d52012-01-08 17:59:08 -06001501 hotplug && atoi(hotplug) == 1) {
1502 RRGetInfo(screenInfo.screens[pScrn->scrnIndex], TRUE);
1503 }
1504 udev_device_unref(dev);
1505}
Rob Clark487687e2011-07-17 17:29:02 -05001506
Rob Clark4b8f30a2011-08-28 12:51:26 -05001507static void
Rob Clark487687e2011-07-17 17:29:02 -05001508drmmode_uevent_init(ScrnInfoPtr pScrn)
1509{
Dave Barnish2e998952013-06-11 16:31:10 +01001510 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark74210d52012-01-08 17:59:08 -06001511 struct udev *u;
1512 struct udev_monitor *mon;
Rob Clark487687e2011-07-17 17:29:02 -05001513
Rob Clark74210d52012-01-08 17:59:08 -06001514 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001515
Rob Clark74210d52012-01-08 17:59:08 -06001516 u = udev_new();
1517 if (!u)
1518 return;
1519 mon = udev_monitor_new_from_netlink(u, "udev");
1520 if (!mon) {
1521 udev_unref(u);
1522 return;
1523 }
Rob Clark487687e2011-07-17 17:29:02 -05001524
Rob Clark74210d52012-01-08 17:59:08 -06001525 if (udev_monitor_filter_add_match_subsystem_devtype(mon,
1526 "drm",
1527 "drm_minor") < 0 ||
1528 udev_monitor_enable_receiving(mon) < 0) {
1529 udev_monitor_unref(mon);
1530 udev_unref(u);
1531 return;
1532 }
Rob Clark487687e2011-07-17 17:29:02 -05001533
Rob Clark74210d52012-01-08 17:59:08 -06001534 drmmode->uevent_handler =
1535 xf86AddGeneralHandler(udev_monitor_get_fd(mon),
1536 drmmode_handle_uevents, pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001537
Rob Clark74210d52012-01-08 17:59:08 -06001538 drmmode->uevent_monitor = mon;
Rob Clark487687e2011-07-17 17:29:02 -05001539
Rob Clark74210d52012-01-08 17:59:08 -06001540 TRACE_EXIT();
1541}
Rob Clark487687e2011-07-17 17:29:02 -05001542
Rob Clark4b8f30a2011-08-28 12:51:26 -05001543static void
Rob Clark487687e2011-07-17 17:29:02 -05001544drmmode_uevent_fini(ScrnInfoPtr pScrn)
1545{
Dave Barnish2e998952013-06-11 16:31:10 +01001546 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark487687e2011-07-17 17:29:02 -05001547
Rob Clark74210d52012-01-08 17:59:08 -06001548 TRACE_ENTER();
Rob Clark487687e2011-07-17 17:29:02 -05001549
Rob Clark74210d52012-01-08 17:59:08 -06001550 if (drmmode->uevent_handler) {
1551 struct udev *u = udev_monitor_get_udev(drmmode->uevent_monitor);
1552 xf86RemoveGeneralHandler(drmmode->uevent_handler);
Rob Clark487687e2011-07-17 17:29:02 -05001553
Rob Clark74210d52012-01-08 17:59:08 -06001554 udev_monitor_unref(drmmode->uevent_monitor);
1555 udev_unref(u);
1556 }
Rob Clark487687e2011-07-17 17:29:02 -05001557
Rob Clark74210d52012-01-08 17:59:08 -06001558 TRACE_EXIT();
1559}
Rob Clark4b8f30a2011-08-28 12:51:26 -05001560
1561static void
1562drmmode_wakeup_handler(pointer data, int err, pointer p)
1563{
Ray Smith04af9992013-06-26 13:44:54 +01001564 int fd = (int)data;
Rob Clark4b8f30a2011-08-28 12:51:26 -05001565 fd_set *read_mask = p;
1566
Ray Smith04af9992013-06-26 13:44:54 +01001567 if (err < 0)
Rob Clark4b8f30a2011-08-28 12:51:26 -05001568 return;
1569
Ray Smith04af9992013-06-26 13:44:54 +01001570 if (FD_ISSET(fd, read_mask))
1571 drmHandleEvent(fd, &event_context);
1572}
Paul Geary6fe52f32013-04-03 11:12:24 +01001573
Ray Smith04af9992013-06-26 13:44:54 +01001574void drmmode_init_wakeup_handler(int fd)
1575{
1576 AddGeneralSocket(fd);
1577 RegisterBlockAndWakeupHandlers((BlockHandlerProcPtr)NoopDDA,
1578 drmmode_wakeup_handler, (pointer)fd);
1579}
1580
1581void drmmode_fini_wakeup_handler(int fd)
1582{
1583 RemoveBlockAndWakeupHandlers((BlockHandlerProcPtr)NoopDDA,
1584 drmmode_wakeup_handler, (pointer)fd);
1585 RemoveGeneralSocket(fd);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001586}
1587
1588void
Rob Clark67b875f2012-04-20 19:13:57 -05001589drmmode_wait_for_event(ScrnInfoPtr pScrn)
1590{
Dave Barnish2e998952013-06-11 16:31:10 +01001591 struct drmmode_rec *drmmode = drmmode_from_scrn(pScrn);
Rob Clark67b875f2012-04-20 19:13:57 -05001592 drmHandleEvent(drmmode->fd, &event_context);
1593}
1594
1595void
Rob Clark4b8f30a2011-08-28 12:51:26 -05001596drmmode_screen_init(ScrnInfoPtr pScrn)
1597{
Rob Clark4b8f30a2011-08-28 12:51:26 -05001598 drmmode_uevent_init(pScrn);
Rob Clark4b8f30a2011-08-28 12:51:26 -05001599}
1600
1601void
1602drmmode_screen_fini(ScrnInfoPtr pScrn)
1603{
1604 drmmode_uevent_fini(pScrn);
1605}