xref: /OK3568_Linux_fs/external/xserver/hw/xfree86/drivers/modesetting/present.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #ifdef HAVE_DIX_CONFIG_H
24 #include "dix-config.h"
25 #endif
26 
27 #include <assert.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <sys/ioctl.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <time.h>
38 
39 #include <xf86.h>
40 #include <xf86Crtc.h>
41 #include <xf86drm.h>
42 #include <xf86str.h>
43 #include <present.h>
44 
45 #include "driver.h"
46 #include "drmmode_display.h"
47 
48 #if 0
49 #define DebugPresent(x) ErrorF x
50 #else
51 #define DebugPresent(x)
52 #endif
53 
54 struct ms_present_vblank_event {
55     uint64_t        event_id;
56     Bool            unflip;
57 };
58 
59 static RRCrtcPtr
ms_present_get_crtc(WindowPtr window)60 ms_present_get_crtc(WindowPtr window)
61 {
62     return ms_randr_crtc_covering_drawable(&window->drawable);
63 }
64 
65 static int
ms_present_get_ust_msc(RRCrtcPtr crtc,CARD64 * ust,CARD64 * msc)66 ms_present_get_ust_msc(RRCrtcPtr crtc, CARD64 *ust, CARD64 *msc)
67 {
68     xf86CrtcPtr xf86_crtc = crtc->devPrivate;
69 
70     return ms_get_crtc_ust_msc(xf86_crtc, ust, msc);
71 }
72 
73 /*
74  * Called when the queued vblank event has occurred
75  */
76 static void
ms_present_vblank_handler(uint64_t msc,uint64_t usec,void * data)77 ms_present_vblank_handler(uint64_t msc, uint64_t usec, void *data)
78 {
79     struct ms_present_vblank_event *event = data;
80 
81     DebugPresent(("\t\tmh %lld msc %llu\n",
82                  (long long) event->event_id, (long long) msc));
83 
84     present_event_notify(event->event_id, usec, msc);
85     free(event);
86 }
87 
88 /*
89  * Called when the queued vblank is aborted
90  */
91 static void
ms_present_vblank_abort(void * data)92 ms_present_vblank_abort(void *data)
93 {
94     struct ms_present_vblank_event *event = data;
95 
96     DebugPresent(("\t\tma %lld\n", (long long) event->event_id));
97 
98     free(event);
99 }
100 
101 /*
102  * Queue an event to report back to the Present extension when the specified
103  * MSC has past
104  */
105 static int
ms_present_queue_vblank(RRCrtcPtr crtc,uint64_t event_id,uint64_t msc)106 ms_present_queue_vblank(RRCrtcPtr crtc,
107                         uint64_t event_id,
108                         uint64_t msc)
109 {
110     xf86CrtcPtr xf86_crtc = crtc->devPrivate;
111     struct ms_present_vblank_event *event;
112     uint32_t seq;
113 
114     event = calloc(sizeof(struct ms_present_vblank_event), 1);
115     if (!event)
116         return BadAlloc;
117     event->event_id = event_id;
118     seq = ms_drm_queue_alloc(xf86_crtc, event,
119                              ms_present_vblank_handler,
120                              ms_present_vblank_abort);
121     if (!seq) {
122         free(event);
123         return BadAlloc;
124     }
125 
126     if (!ms_queue_vblank(xf86_crtc, MS_QUEUE_ABSOLUTE, msc, NULL, seq))
127         return BadAlloc;
128 
129     DebugPresent(("\t\tmq %lld seq %u msc %llu (hw msc %u)\n",
130                  (long long) event_id, seq, (long long) msc,
131                  vbl.request.sequence));
132     return Success;
133 }
134 
135 static Bool
ms_present_event_match(void * data,void * match_data)136 ms_present_event_match(void *data, void *match_data)
137 {
138     struct ms_present_vblank_event *event = data;
139     uint64_t *match = match_data;
140 
141     return *match == event->event_id;
142 }
143 
144 /*
145  * Remove a pending vblank event from the DRM queue so that it is not reported
146  * to the extension
147  */
148 static void
ms_present_abort_vblank(RRCrtcPtr crtc,uint64_t event_id,uint64_t msc)149 ms_present_abort_vblank(RRCrtcPtr crtc, uint64_t event_id, uint64_t msc)
150 {
151     ScreenPtr screen = crtc->pScreen;
152     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
153 
154     ms_drm_abort(scrn, ms_present_event_match, &event_id);
155 }
156 
157 /*
158  * Flush our batch buffer when requested by the Present extension.
159  */
160 static void
ms_present_flush(WindowPtr window)161 ms_present_flush(WindowPtr window)
162 {
163 #ifdef GLAMOR_HAS_GBM
164     ScreenPtr screen = window->drawable.pScreen;
165     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
166     modesettingPtr ms = modesettingPTR(scrn);
167 
168     if (ms->drmmode.glamor)
169         glamor_block_handler(screen);
170 #endif
171 }
172 
173 /**
174  * Callback for the DRM event queue when a flip has completed on all pipes
175  *
176  * Notify the extension code
177  */
178 static void
ms_present_flip_handler(modesettingPtr ms,uint64_t msc,uint64_t ust,void * data)179 ms_present_flip_handler(modesettingPtr ms, uint64_t msc,
180                         uint64_t ust, void *data)
181 {
182     struct ms_present_vblank_event *event = data;
183 
184     DebugPresent(("\t\tms:fc %lld msc %llu ust %llu\n",
185                   (long long) event->event_id,
186                   (long long) msc, (long long) ust));
187 
188     if (event->unflip)
189         ms->drmmode.present_flipping = FALSE;
190 
191     ms_present_vblank_handler(msc, ust, event);
192 }
193 
194 /*
195  * Callback for the DRM queue abort code.  A flip has been aborted.
196  */
197 static void
ms_present_flip_abort(modesettingPtr ms,void * data)198 ms_present_flip_abort(modesettingPtr ms, void *data)
199 {
200     struct ms_present_vblank_event *event = data;
201 
202     DebugPresent(("\t\tms:fa %lld\n", (long long) event->event_id));
203 
204     free(event);
205 }
206 
207 /*
208  * Test to see if page flipping is possible on the target crtc
209  *
210  * We ignore sw-cursors when *disabling* flipping, we may very well be
211  * returning to scanning out the normal framebuffer *because* we just
212  * switched to sw-cursor mode and check_flip just failed because of that.
213  */
214 static Bool
ms_present_check_unflip(RRCrtcPtr crtc,WindowPtr window,PixmapPtr pixmap,Bool sync_flip,PresentFlipReason * reason)215 ms_present_check_unflip(RRCrtcPtr crtc,
216                         WindowPtr window,
217                         PixmapPtr pixmap,
218                         Bool sync_flip,
219                         PresentFlipReason *reason)
220 {
221     ScreenPtr screen = window->drawable.pScreen;
222     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
223     modesettingPtr ms = modesettingPTR(scrn);
224     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
225     int num_crtcs_on = 0;
226     int i;
227     struct gbm_bo *gbm;
228 
229     if (!ms->drmmode.pageflip)
230         return FALSE;
231 
232     if (ms->drmmode.dri2_flipping)
233         return FALSE;
234 
235     if (!scrn->vtSema)
236         return FALSE;
237 
238     for (i = 0; i < config->num_crtc; i++) {
239 #ifdef GLAMOR_HAS_GBM
240         drmmode_crtc_private_ptr drmmode_crtc = config->crtc[i]->driver_private;
241 
242         /* Don't do pageflipping if CRTCs are rotated. */
243         if (drmmode_crtc->rotate_bo.gbm)
244             return FALSE;
245 #endif
246 
247         if (ms_crtc_on(config->crtc[i]))
248             num_crtcs_on++;
249     }
250 
251     /* We can't do pageflipping if all the CRTCs are off. */
252     if (num_crtcs_on == 0)
253         return FALSE;
254 
255     /* Check stride, can't change that on flip */
256     if (!ms->atomic_modeset &&
257         pixmap->devKind != drmmode_bo_get_pitch(&ms->drmmode.front_bo))
258         return FALSE;
259 
260     if (ms->drmmode.exa)
261         return TRUE;
262 
263     if (!ms->drmmode.glamor)
264         return FALSE;
265 
266 #ifdef GBM_BO_WITH_MODIFIERS
267     /* Check if buffer format/modifier is supported by all active CRTCs */
268     gbm = glamor_gbm_bo_from_pixmap(screen, pixmap);
269     if (gbm) {
270         uint32_t format;
271         uint64_t modifier;
272 
273         format = gbm_bo_get_format(gbm);
274         modifier = gbm_bo_get_modifier(gbm);
275 
276         if (!drmmode_is_format_supported(scrn, format, modifier)) {
277             if (reason)
278                 *reason = PRESENT_FLIP_REASON_BUFFER_FORMAT;
279             return FALSE;
280         }
281     }
282 #endif
283 
284     /* Make sure there's a bo we can get to */
285     /* XXX: actually do this.  also...is it sufficient?
286      * if (!glamor_get_pixmap_private(pixmap))
287      *     return FALSE;
288      */
289 
290     return TRUE;
291 }
292 
293 static Bool
ms_present_check_flip(RRCrtcPtr crtc,WindowPtr window,PixmapPtr pixmap,Bool sync_flip,PresentFlipReason * reason)294 ms_present_check_flip(RRCrtcPtr crtc,
295                       WindowPtr window,
296                       PixmapPtr pixmap,
297                       Bool sync_flip,
298                       PresentFlipReason *reason)
299 {
300     ScreenPtr screen = window->drawable.pScreen;
301     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
302     modesettingPtr ms = modesettingPTR(scrn);
303 
304     if (ms->drmmode.sprites_visible > 0)
305         return FALSE;
306 
307     return ms_present_check_unflip(crtc, window, pixmap, sync_flip, reason);
308 }
309 
310 /*
311  * Queue a flip on 'crtc' to 'pixmap' at 'target_msc'. If 'sync_flip' is true,
312  * then wait for vblank. Otherwise, flip immediately
313  */
314 static Bool
ms_present_flip(RRCrtcPtr crtc,uint64_t event_id,uint64_t target_msc,PixmapPtr pixmap,Bool sync_flip)315 ms_present_flip(RRCrtcPtr crtc,
316                 uint64_t event_id,
317                 uint64_t target_msc,
318                 PixmapPtr pixmap,
319                 Bool sync_flip)
320 {
321     ScreenPtr screen = crtc->pScreen;
322     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
323     modesettingPtr ms = modesettingPTR(scrn);
324     xf86CrtcPtr xf86_crtc = crtc->devPrivate;
325     drmmode_crtc_private_ptr drmmode_crtc = xf86_crtc->driver_private;
326     Bool ret;
327     struct ms_present_vblank_event *event;
328 
329     if (!ms_present_check_flip(crtc, screen->root, pixmap, sync_flip, NULL))
330         return FALSE;
331 
332     event = calloc(1, sizeof(struct ms_present_vblank_event));
333     if (!event)
334         return FALSE;
335 
336     DebugPresent(("\t\tms:pf %lld msc %llu\n",
337                   (long long) event_id, (long long) target_msc));
338 
339     event->event_id = event_id;
340     event->unflip = FALSE;
341 
342     ret = ms_do_pageflip(screen, pixmap, event, drmmode_crtc->vblank_pipe, !sync_flip,
343                          ms_present_flip_handler, ms_present_flip_abort);
344     if (!ret) {
345         xf86DrvMsg(scrn->scrnIndex, X_ERROR, "present flip failed\n");
346     } else {
347         ms->drmmode.present_flipping = TRUE;
348         drmmode_crtc->external_flipped = TRUE;
349     }
350 
351     return ret;
352 }
353 
354 /*
355  * Queue a flip back to the normal frame buffer
356  */
357 static void
ms_present_unflip(ScreenPtr screen,uint64_t event_id)358 ms_present_unflip(ScreenPtr screen, uint64_t event_id)
359 {
360     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
361     modesettingPtr ms = modesettingPTR(scrn);
362     PixmapPtr pixmap = screen->GetScreenPixmap(screen);
363     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
364     int i;
365     struct ms_present_vblank_event *event;
366 
367     event = calloc(1, sizeof(struct ms_present_vblank_event));
368     if (!event)
369         return;
370 
371     event->event_id = event_id;
372     event->unflip = TRUE;
373 
374     if (ms_present_check_unflip(NULL, screen->root, pixmap, TRUE, NULL) &&
375         ms_do_pageflip(screen, pixmap, event, -1, FALSE,
376                        ms_present_flip_handler, ms_present_flip_abort)) {
377         return;
378     }
379 
380     for (i = 0; i < config->num_crtc; i++) {
381         xf86CrtcPtr crtc = config->crtc[i];
382 	drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
383 
384 	if (!crtc->enabled)
385 	    continue;
386 
387 	/* info->drmmode.fb_id still points to the FB for the last flipped BO.
388 	 * Clear it, drmmode_set_mode_major will re-create it
389 	 */
390 	if (drmmode_crtc->drmmode->fb_id) {
391 		drmModeRmFB(drmmode_crtc->drmmode->fd,
392 			    drmmode_crtc->drmmode->fb_id);
393 		drmmode_crtc->drmmode->fb_id = 0;
394 	}
395 
396 	if (drmmode_crtc->dpms_mode == DPMSModeOn)
397 	    crtc->funcs->set_mode_major(crtc, &crtc->mode, crtc->rotation,
398 					crtc->x, crtc->y);
399 	else
400 	    drmmode_crtc->need_modeset = TRUE;
401     }
402 
403     present_event_notify(event_id, 0, 0);
404     ms->drmmode.present_flipping = FALSE;
405 }
406 
407 static present_screen_info_rec ms_present_screen_info = {
408     .version = PRESENT_SCREEN_INFO_VERSION,
409 
410     .get_crtc = ms_present_get_crtc,
411     .get_ust_msc = ms_present_get_ust_msc,
412     .queue_vblank = ms_present_queue_vblank,
413     .abort_vblank = ms_present_abort_vblank,
414     .flush = ms_present_flush,
415 
416     .capabilities = PresentCapabilityNone,
417     .check_flip = NULL,
418     .check_flip2 = ms_present_check_flip,
419     .flip = ms_present_flip,
420     .unflip = ms_present_unflip,
421 };
422 
423 Bool
ms_present_screen_init(ScreenPtr screen)424 ms_present_screen_init(ScreenPtr screen)
425 {
426     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
427     modesettingPtr ms = modesettingPTR(scrn);
428     uint64_t value;
429     int ret;
430 
431     ret = drmGetCap(ms->fd, DRM_CAP_ASYNC_PAGE_FLIP, &value);
432     if (ret == 0 && value == 1)
433         ms_present_screen_info.capabilities |= PresentCapabilityAsync;
434 
435     return present_screen_init(screen, &ms_present_screen_info);
436 }
437