xref: /OK3568_Linux_fs/external/xserver/hw/xfree86/drivers/modesetting/pageflip.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 <xserver_poll.h>
28 #include <xf86drm.h>
29 
30 #include <sys/time.h>
31 
32 #include "driver.h"
33 
34 /*
35  * Flush the DRM event queue when full; makes space for new events.
36  *
37  * Returns a negative value on error, 0 if there was nothing to process,
38  * or 1 if we handled any events.
39  */
40 int
ms_flush_drm_events(ScreenPtr screen)41 ms_flush_drm_events(ScreenPtr screen)
42 {
43     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
44     modesettingPtr ms = modesettingPTR(scrn);
45 
46     struct pollfd p = { .fd = ms->fd, .events = POLLIN };
47     int r;
48 
49     do {
50             r = xserver_poll(&p, 1, 0);
51     } while (r == -1 && (errno == EINTR || errno == EAGAIN));
52 
53     /* If there was an error, r will be < 0.  Return that.  If there was
54      * nothing to process, r == 0.  Return that.
55      */
56     if (r <= 0)
57         return r;
58 
59     /* Try to handle the event.  If there was an error, return it. */
60     r = drmHandleEvent(ms->fd, &ms->event_context);
61     if (r < 0)
62         return r;
63 
64     /* Otherwise return 1 to indicate that we handled an event. */
65     return 1;
66 }
67 
68 /*
69  * Event data for an in progress flip.
70  * This contains a pointer to the vblank event,
71  * and information about the flip in progress.
72  * a reference to this is stored in the per-crtc
73  * flips.
74  */
75 struct ms_flipdata {
76     ScreenPtr screen;
77     void *event;
78     ms_pageflip_handler_proc event_handler;
79     ms_pageflip_abort_proc abort_handler;
80     /* number of CRTC events referencing this */
81     int flip_count;
82     uint64_t fe_msc;
83     uint64_t fe_usec;
84     uint32_t old_fb_id;
85     uint32_t *fb_id;
86 };
87 
88 /*
89  * Per crtc pageflipping infomation,
90  * These are submitted to the queuing code
91  * one of them per crtc per flip.
92  */
93 struct ms_crtc_pageflip {
94     Bool on_reference_crtc;
95     /* reference to the ms_flipdata */
96     struct ms_flipdata *flipdata;
97 };
98 
99 /**
100  * Free an ms_crtc_pageflip.
101  *
102  * Drops the reference count on the flipdata.
103  */
104 static void
ms_pageflip_free(struct ms_crtc_pageflip * flip)105 ms_pageflip_free(struct ms_crtc_pageflip *flip)
106 {
107     struct ms_flipdata *flipdata = flip->flipdata;
108 
109     free(flip);
110     if (--flipdata->flip_count > 0)
111         return;
112     free(flipdata);
113 }
114 
115 /**
116  * Callback for the DRM event queue when a single flip has completed
117  *
118  * Once the flip has been completed on all pipes, notify the
119  * extension code telling it when that happened
120  */
121 static void
ms_pageflip_handler(uint64_t msc,uint64_t ust,void * data)122 ms_pageflip_handler(uint64_t msc, uint64_t ust, void *data)
123 {
124     struct ms_crtc_pageflip *flip = data;
125     struct ms_flipdata *flipdata = flip->flipdata;
126     ScreenPtr screen = flipdata->screen;
127     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
128     modesettingPtr ms = modesettingPTR(scrn);
129 
130     if (flip->on_reference_crtc) {
131         flipdata->fe_msc = msc;
132         flipdata->fe_usec = ust;
133     }
134 
135     if (flipdata->flip_count == 1) {
136         flipdata->event_handler(ms, flipdata->fe_msc,
137                                 flipdata->fe_usec,
138                                 flipdata->event);
139 
140         drmModeRmFB(ms->fd, flipdata->old_fb_id);
141     }
142     ms_pageflip_free(flip);
143 }
144 
145 /*
146  * Callback for the DRM queue abort code.  A flip has been aborted.
147  */
148 static void
ms_pageflip_abort(void * data)149 ms_pageflip_abort(void *data)
150 {
151     struct ms_crtc_pageflip *flip = data;
152     struct ms_flipdata *flipdata = flip->flipdata;
153     ScreenPtr screen = flipdata->screen;
154     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
155     modesettingPtr ms = modesettingPTR(scrn);
156 
157     if (flipdata->flip_count == 1) {
158         flipdata->abort_handler(ms, flipdata->event);
159 
160         drmModeRmFB(ms->fd, flipdata->old_fb_id);
161     }
162     ms_pageflip_free(flip);
163 }
164 
165 static Bool
do_queue_flip_on_crtc(modesettingPtr ms,xf86CrtcPtr crtc,uint32_t flags,uint32_t seq,unsigned fb_id)166 do_queue_flip_on_crtc(modesettingPtr ms, xf86CrtcPtr crtc,
167                       uint32_t flags, uint32_t seq, unsigned fb_id)
168 {
169     return drmmode_crtc_flip(crtc, fb_id, flags,
170                              (void *) (uintptr_t) seq);
171 }
172 
173 static Bool
queue_flip_on_crtc(ScreenPtr screen,xf86CrtcPtr crtc,struct ms_flipdata * flipdata,int ref_crtc_vblank_pipe,uint32_t flags)174 queue_flip_on_crtc(ScreenPtr screen, xf86CrtcPtr crtc,
175                    struct ms_flipdata *flipdata,
176                    int ref_crtc_vblank_pipe, uint32_t flags)
177 {
178     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
179     modesettingPtr ms = modesettingPTR(scrn);
180     drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
181     struct ms_crtc_pageflip *flip;
182     uint32_t seq;
183     int err;
184 
185     flip = calloc(1, sizeof(struct ms_crtc_pageflip));
186     if (flip == NULL) {
187         xf86DrvMsg(scrn->scrnIndex, X_WARNING,
188                    "flip queue: carrier alloc failed.\n");
189         return FALSE;
190     }
191 
192     /* Only the reference crtc will finally deliver its page flip
193      * completion event. All other crtc's events will be discarded.
194      */
195     flip->on_reference_crtc = (drmmode_crtc->vblank_pipe == ref_crtc_vblank_pipe);
196     flip->flipdata = flipdata;
197 
198     seq = ms_drm_queue_alloc(crtc, flip, ms_pageflip_handler, ms_pageflip_abort);
199     if (!seq) {
200         free(flip);
201         return FALSE;
202     }
203 
204     /* take a reference on flipdata for use in flip */
205     flipdata->flip_count++;
206 
207     while (do_queue_flip_on_crtc(ms, crtc, flags, seq, *flipdata->fb_id)) {
208         err = errno;
209         /* We may have failed because the event queue was full.  Flush it
210          * and retry.  If there was nothing to flush, then we failed for
211          * some other reason and should just return an error.
212          */
213         if (ms_flush_drm_events(screen) <= 0) {
214             xf86DrvMsg(scrn->scrnIndex, X_WARNING,
215                        "flip queue failed: %s\n", strerror(err));
216             /* Aborting will also decrement flip_count and free(flip). */
217             ms_drm_abort_seq(scrn, seq);
218             return FALSE;
219         }
220 
221         /* We flushed some events, so try again. */
222         xf86DrvMsg(scrn->scrnIndex, X_WARNING, "flip queue retry\n");
223     }
224 
225     /* The page flip succeded. */
226     return TRUE;
227 }
228 
229 Bool
ms_do_pageflip_bo(ScreenPtr screen,drmmode_bo * new_front_bo,void * event,int ref_crtc_vblank_pipe,xf86CrtcPtr target_crtc,Bool async,ms_pageflip_handler_proc pageflip_handler,ms_pageflip_abort_proc pageflip_abort)230 ms_do_pageflip_bo(ScreenPtr screen,
231                   drmmode_bo *new_front_bo,
232                   void *event,
233                   int ref_crtc_vblank_pipe,
234                   xf86CrtcPtr target_crtc,
235                   Bool async,
236                   ms_pageflip_handler_proc pageflip_handler,
237                   ms_pageflip_abort_proc pageflip_abort)
238 {
239     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
240     modesettingPtr ms = modesettingPTR(scrn);
241     xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
242     drmmode_crtc_private_ptr drmmode_crtc;
243     uint32_t flags;
244     int i;
245     struct ms_flipdata *flipdata;
246     struct timeval tv;
247 
248     flipdata = calloc(1, sizeof(struct ms_flipdata));
249     if (!flipdata) {
250         xf86DrvMsg(scrn->scrnIndex, X_ERROR,
251                    "Failed to allocate flipdata.\n");
252         return FALSE;
253     }
254 
255     if (target_crtc) {
256         drmmode_crtc = target_crtc->driver_private;
257         flipdata->fb_id = &drmmode_crtc->fb_id;
258     } else {
259         flipdata->fb_id = &ms->drmmode.fb_id;
260     }
261 
262     flipdata->event = event;
263     flipdata->screen = screen;
264     flipdata->event_handler = pageflip_handler;
265     flipdata->abort_handler = pageflip_abort;
266 
267     /*
268      * Take a local reference on flipdata.
269      * if the first flip fails, the sequence abort
270      * code will free the crtc flip data, and drop
271      * it's reference which would cause this to be
272      * freed when we still required it.
273      */
274     flipdata->flip_count++;
275 
276     /* Create a new handle for the back buffer */
277     flipdata->old_fb_id = *flipdata->fb_id;
278 
279     if (drmmode_bo_import(&ms->drmmode, new_front_bo,
280                           flipdata->fb_id))
281         goto error_out;
282 
283     flags = DRM_MODE_PAGE_FLIP_EVENT;
284     if (async && ms->async_pageflip)
285         flags |= DRM_MODE_PAGE_FLIP_ASYNC;
286 
287     /* Queue flips on all enabled CRTCs.
288      *
289      * Note that if/when we get per-CRTC buffers, we'll have to update this.
290      * Right now it assumes a single shared fb across all CRTCs, with the
291      * kernel fixing up the offset of each CRTC as necessary.
292      *
293      * Also, flips queued on disabled or incorrectly configured displays
294      * may never complete; this is a configuration error.
295      */
296     for (i = 0; i < config->num_crtc; i++) {
297         xf86CrtcPtr crtc = config->crtc[i];
298 
299         if (!ms_crtc_on(crtc))
300             continue;
301 
302         if (target_crtc && crtc != target_crtc)
303             continue;
304 
305         if (!queue_flip_on_crtc(screen, crtc, flipdata,
306                                 ref_crtc_vblank_pipe,
307                                 flags)) {
308             goto error_undo;
309         }
310 
311         gettimeofday(&tv, NULL);
312         drmmode_crtc = crtc->driver_private;
313         drmmode_crtc->flipping_time_ms = tv.tv_sec * 1000 + tv.tv_usec / 1000;
314     }
315 
316     /*
317      * Do we have more than our local reference,
318      * if so and no errors, then drop our local
319      * reference and return now.
320      */
321     if (flipdata->flip_count > 1) {
322         flipdata->flip_count--;
323         return TRUE;
324     }
325 
326 error_undo:
327 
328     /*
329      * Have we just got the local reference?
330      * free the framebuffer if so since nobody successfully
331      * submitted anything
332      */
333     if (flipdata->flip_count == 1) {
334         drmModeRmFB(ms->fd, *flipdata->fb_id);
335         *flipdata->fb_id = flipdata->old_fb_id;
336     }
337 
338 error_out:
339     xf86DrvMsg(scrn->scrnIndex, X_WARNING, "Page flip failed: %s\n",
340                strerror(errno));
341     /* if only the local reference - free the structure,
342      * else drop the local reference and return */
343     if (flipdata->flip_count == 1)
344         free(flipdata);
345     else
346         flipdata->flip_count--;
347 
348     return FALSE;
349 }
350 
351 Bool
ms_do_pageflip(ScreenPtr screen,PixmapPtr new_front,void * event,int ref_crtc_vblank_pipe,Bool async,ms_pageflip_handler_proc pageflip_handler,ms_pageflip_abort_proc pageflip_abort)352 ms_do_pageflip(ScreenPtr screen,
353                PixmapPtr new_front,
354                void *event,
355                int ref_crtc_vblank_pipe,
356                Bool async,
357                ms_pageflip_handler_proc pageflip_handler,
358                ms_pageflip_abort_proc pageflip_abort)
359 {
360     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
361     modesettingPtr ms = modesettingPTR(scrn);
362     drmmode_bo new_front_bo = {0};
363     Bool ret;
364 
365 #ifdef GLAMOR_HAS_GBM
366     if (ms->drmmode.glamor) {
367         new_front_bo.gbm = glamor_gbm_bo_from_pixmap(screen, new_front);
368         if (!new_front_bo.gbm) {
369             xf86DrvMsg(scrn->scrnIndex, X_ERROR,
370                        "Failed to get GBM bo for flip to new front.\n");
371             return FALSE;
372         }
373 
374         glamor_block_handler(screen);
375     } else
376 #endif
377     if (ms->drmmode.exa) {
378         new_front_bo.dumb = ms_exa_bo_from_pixmap(screen, new_front);
379         if (!new_front_bo.dumb) {
380             xf86DrvMsg(scrn->scrnIndex, X_ERROR,
381                        "Failed to get dumb bo for flip to new front.\n");
382             return FALSE;
383         }
384     } else {
385         return FALSE;
386     }
387 
388     new_front_bo.width = new_front->drawable.width;
389     new_front_bo.height = new_front->drawable.height;
390 
391     ret = ms_do_pageflip_bo(screen, &new_front_bo, event,
392                             ref_crtc_vblank_pipe, NULL, async,
393                             pageflip_handler, pageflip_abort);
394 
395 #ifdef GLAMOR_HAS_GBM
396     new_front_bo.gbm = NULL;
397 #endif
398     new_front_bo.dumb = NULL;
399 
400     drmmode_bo_destroy(&ms->drmmode, &new_front_bo);
401 
402     return ret;
403 }
404