1 /*
2  * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
3  * Copyright 2011 Dave Airlie
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  *
27  * Original Author: Alan Hourihane <alanh@tungstengraphics.com>
28  * Rewrite: Dave Airlie <airlied@redhat.com>
29  *
30  */
31 
32 #ifdef HAVE_DIX_CONFIG_H
33 #include "dix-config.h"
34 #endif
35 
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include "xf86.h"
39 #include "xf86Priv.h"
40 #include "xf86_OSproc.h"
41 #include "compiler.h"
42 #include "xf86Pci.h"
43 #include "mipointer.h"
44 #include "mipointrst.h"
45 #include "micmap.h"
46 #include <X11/extensions/randr.h>
47 #include "fb.h"
48 #include "edid.h"
49 #include "xf86i2c.h"
50 #include "xf86Crtc.h"
51 #include "miscstruct.h"
52 #include "dixstruct.h"
53 #include "shadow.h"
54 #include "xf86xv.h"
55 #include <X11/extensions/Xv.h>
56 #include <xorg-config.h>
57 #ifdef XSERVER_PLATFORM_BUS
58 #include "xf86platformBus.h"
59 #endif
60 #ifdef XSERVER_LIBPCIACCESS
61 #include <pciaccess.h>
62 #endif
63 
64 #include "driver.h"
65 
66 static void AdjustFrame(ScrnInfoPtr pScrn, int x, int y);
67 static Bool CloseScreen(ScreenPtr pScreen);
68 static Bool EnterVT(ScrnInfoPtr pScrn);
69 static void Identify(int flags);
70 static const OptionInfoRec *AvailableOptions(int chipid, int busid);
71 static ModeStatus ValidMode(ScrnInfoPtr pScrn, DisplayModePtr mode,
72                             Bool verbose, int flags);
73 static void FreeScreen(ScrnInfoPtr pScrn);
74 static void LeaveVT(ScrnInfoPtr pScrn);
75 static Bool SwitchMode(ScrnInfoPtr pScrn, DisplayModePtr mode);
76 static Bool ScreenInit(ScreenPtr pScreen, int argc, char **argv);
77 static Bool PreInit(ScrnInfoPtr pScrn, int flags);
78 
79 static Bool Probe(DriverPtr drv, int flags);
80 static Bool ms_pci_probe(DriverPtr driver,
81                          int entity_num, struct pci_device *device,
82                          intptr_t match_data);
83 static Bool ms_driver_func(ScrnInfoPtr scrn, xorgDriverFuncOp op, void *data);
84 
85 #ifdef XSERVER_LIBPCIACCESS
86 static const struct pci_id_match ms_device_match[] = {
87     {
88      PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY,
89      0x00030000, 0x00ff0000, 0},
90 
91     {0, 0, 0},
92 };
93 #endif
94 
95 #ifndef XSERVER_PLATFORM_BUS
96 struct xf86_platform_device;
97 #endif
98 
99 #ifdef XSERVER_PLATFORM_BUS
100 static Bool ms_platform_probe(DriverPtr driver,
101                               int entity_num, int flags,
102                               struct xf86_platform_device *device,
103                               intptr_t match_data);
104 #endif
105 
106 _X_EXPORT DriverRec modesetting = {
107     1,
108     "modesetting",
109     Identify,
110     Probe,
111     AvailableOptions,
112     NULL,
113     0,
114     ms_driver_func,
115     ms_device_match,
116     ms_pci_probe,
117 #ifdef XSERVER_PLATFORM_BUS
118     ms_platform_probe,
119 #endif
120 };
121 
122 static SymTabRec Chipsets[] = {
123     {0, "kms"},
124     {-1, NULL}
125 };
126 
127 static const OptionInfoRec Options[] = {
128     {OPTION_SW_CURSOR, "SWcursor", OPTV_BOOLEAN, {0}, FALSE},
129     {OPTION_DEVICE_PATH, "kmsdev", OPTV_STRING, {0}, FALSE},
130     {OPTION_SHADOW_FB, "ShadowFB", OPTV_BOOLEAN, {0}, FALSE},
131     {OPTION_ACCEL_METHOD, "AccelMethod", OPTV_STRING, {0}, FALSE},
132     {OPTION_PAGEFLIP, "PageFlip", OPTV_BOOLEAN, {0}, FALSE},
133     {OPTION_ZAPHOD_HEADS, "ZaphodHeads", OPTV_STRING, {0}, FALSE},
134     {OPTION_DOUBLE_SHADOW, "DoubleShadow", OPTV_BOOLEAN, {0}, FALSE},
135     {OPTION_ATOMIC, "Atomic", OPTV_BOOLEAN, {0}, FALSE},
136     {-1, NULL, OPTV_NONE, {0}, FALSE}
137 };
138 
139 int ms_entity_index = -1;
140 
141 static MODULESETUPPROTO(Setup);
142 
143 static XF86ModuleVersionInfo VersRec = {
144     "modesetting",
145     MODULEVENDORSTRING,
146     MODINFOSTRING1,
147     MODINFOSTRING2,
148     XORG_VERSION_CURRENT,
149     XORG_VERSION_MAJOR,
150     XORG_VERSION_MINOR,
151     XORG_VERSION_PATCH,
152     ABI_CLASS_VIDEODRV,
153     ABI_VIDEODRV_VERSION,
154     MOD_CLASS_VIDEODRV,
155     {0, 0, 0, 0}
156 };
157 
158 _X_EXPORT XF86ModuleData modesettingModuleData = { &VersRec, Setup, NULL };
159 
160 static void *
Setup(void * module,void * opts,int * errmaj,int * errmin)161 Setup(void *module, void *opts, int *errmaj, int *errmin)
162 {
163     static Bool setupDone = 0;
164 
165     /* This module should be loaded only once, but check to be sure.
166      */
167     if (!setupDone) {
168         setupDone = 1;
169         xf86AddDriver(&modesetting, module, HaveDriverFuncs);
170 
171         /*
172          * The return value must be non-NULL on success even though there
173          * is no TearDownProc.
174          */
175         return (void *) 1;
176     }
177     else {
178         if (errmaj)
179             *errmaj = LDR_ONCEONLY;
180         return NULL;
181     }
182 }
183 
184 static void
Identify(int flags)185 Identify(int flags)
186 {
187     xf86PrintChipsets("modesetting", "Driver for Modesetting Kernel Drivers",
188                       Chipsets);
189 }
190 
ms_ent_priv(ScrnInfoPtr scrn)191 modesettingEntPtr ms_ent_priv(ScrnInfoPtr scrn)
192 {
193     DevUnion     *pPriv;
194     modesettingPtr ms = modesettingPTR(scrn);
195     pPriv = xf86GetEntityPrivate(ms->pEnt->index,
196                                  ms_entity_index);
197     return pPriv->ptr;
198 }
199 
200 static int
get_passed_fd(void)201 get_passed_fd(void)
202 {
203     if (xf86DRMMasterFd >= 0) {
204         xf86DrvMsg(-1, X_INFO, "Using passed DRM master file descriptor %d\n", xf86DRMMasterFd);
205         return dup(xf86DRMMasterFd);
206     }
207     return -1;
208 }
209 
210 static int
open_hw(const char * dev)211 open_hw(const char *dev)
212 {
213     int fd;
214 
215     if ((fd = get_passed_fd()) != -1)
216         return fd;
217 
218     if (dev)
219         fd = open(dev, O_RDWR | O_CLOEXEC, 0);
220     else {
221         dev = getenv("KMSDEVICE");
222         if ((NULL == dev) || ((fd = open(dev, O_RDWR | O_CLOEXEC, 0)) == -1)) {
223             dev = "/dev/dri/card0";
224             fd = open(dev, O_RDWR | O_CLOEXEC, 0);
225         }
226     }
227     if (fd == -1)
228         xf86DrvMsg(-1, X_ERROR, "open %s: %s\n", dev, strerror(errno));
229 
230     return fd;
231 }
232 
233 static int
check_outputs(int fd,int * count)234 check_outputs(int fd, int *count)
235 {
236     drmModeResPtr res = drmModeGetResources(fd);
237     int ret;
238 
239     if (!res)
240         return FALSE;
241 
242     if (count)
243         *count = res->count_connectors;
244 
245     ret = res->count_connectors > 0;
246 #if defined(GLAMOR_HAS_GBM_LINEAR)
247     if (ret == FALSE) {
248         uint64_t value = 0;
249         if (drmGetCap(fd, DRM_CAP_PRIME, &value) == 0 &&
250                 (value & DRM_PRIME_CAP_EXPORT))
251             ret = TRUE;
252     }
253 #endif
254     drmModeFreeResources(res);
255     return ret;
256 }
257 
258 static Bool
probe_hw(const char * dev,struct xf86_platform_device * platform_dev)259 probe_hw(const char *dev, struct xf86_platform_device *platform_dev)
260 {
261     int fd;
262 
263 #ifdef XF86_PDEV_SERVER_FD
264     if (platform_dev && (platform_dev->flags & XF86_PDEV_SERVER_FD)) {
265         fd = xf86_platform_device_odev_attributes(platform_dev)->fd;
266         if (fd == -1)
267             return FALSE;
268         return check_outputs(fd, NULL);
269     }
270 #endif
271 
272     fd = open_hw(dev);
273     if (fd != -1) {
274         int ret = check_outputs(fd, NULL);
275 
276         close(fd);
277         return ret;
278     }
279     return FALSE;
280 }
281 
282 static char *
ms_DRICreatePCIBusID(const struct pci_device * dev)283 ms_DRICreatePCIBusID(const struct pci_device *dev)
284 {
285     char *busID;
286 
287     if (asprintf(&busID, "pci:%04x:%02x:%02x.%d",
288                  dev->domain, dev->bus, dev->dev, dev->func) == -1)
289         return NULL;
290 
291     return busID;
292 }
293 
294 static Bool
probe_hw_pci(const char * dev,struct pci_device * pdev)295 probe_hw_pci(const char *dev, struct pci_device *pdev)
296 {
297     int ret = FALSE, fd = open_hw(dev);
298     char *id, *devid;
299     drmSetVersion sv;
300 
301     if (fd == -1)
302         return FALSE;
303 
304     sv.drm_di_major = 1;
305     sv.drm_di_minor = 4;
306     sv.drm_dd_major = -1;
307     sv.drm_dd_minor = -1;
308     if (drmSetInterfaceVersion(fd, &sv)) {
309         close(fd);
310         return FALSE;
311     }
312 
313     id = drmGetBusid(fd);
314     devid = ms_DRICreatePCIBusID(pdev);
315 
316     if (id && devid && !strcmp(id, devid))
317         ret = check_outputs(fd, NULL);
318 
319     close(fd);
320     free(id);
321     free(devid);
322     return ret;
323 }
324 
325 static const OptionInfoRec *
AvailableOptions(int chipid,int busid)326 AvailableOptions(int chipid, int busid)
327 {
328     return Options;
329 }
330 
331 static Bool
ms_driver_func(ScrnInfoPtr scrn,xorgDriverFuncOp op,void * data)332 ms_driver_func(ScrnInfoPtr scrn, xorgDriverFuncOp op, void *data)
333 {
334     xorgHWFlags *flag;
335 
336     switch (op) {
337     case GET_REQUIRED_HW_INTERFACES:
338         flag = (CARD32 *) data;
339         (*flag) = 0;
340         return TRUE;
341     case SUPPORTS_SERVER_FDS:
342         return TRUE;
343     default:
344         return FALSE;
345     }
346 }
347 
348 static void
ms_setup_scrn_hooks(ScrnInfoPtr scrn)349 ms_setup_scrn_hooks(ScrnInfoPtr scrn)
350 {
351     scrn->driverVersion = 1;
352     scrn->driverName = "modesetting";
353     scrn->name = "modeset";
354 
355     scrn->Probe = NULL;
356     scrn->PreInit = PreInit;
357     scrn->ScreenInit = ScreenInit;
358     scrn->SwitchMode = SwitchMode;
359     scrn->AdjustFrame = AdjustFrame;
360     scrn->EnterVT = EnterVT;
361     scrn->LeaveVT = LeaveVT;
362     scrn->FreeScreen = FreeScreen;
363     scrn->ValidMode = ValidMode;
364 }
365 
366 static void
ms_setup_entity(ScrnInfoPtr scrn,int entity_num)367 ms_setup_entity(ScrnInfoPtr scrn, int entity_num)
368 {
369     DevUnion *pPriv;
370 
371     xf86SetEntitySharable(entity_num);
372 
373     if (ms_entity_index == -1)
374         ms_entity_index = xf86AllocateEntityPrivateIndex();
375 
376     pPriv = xf86GetEntityPrivate(entity_num,
377                                  ms_entity_index);
378 
379     xf86SetEntityInstanceForScreen(scrn, entity_num, xf86GetNumEntityInstances(entity_num) - 1);
380 
381     if (!pPriv->ptr)
382         pPriv->ptr = xnfcalloc(sizeof(modesettingEntRec), 1);
383 }
384 
385 #ifdef XSERVER_LIBPCIACCESS
386 static Bool
ms_pci_probe(DriverPtr driver,int entity_num,struct pci_device * dev,intptr_t match_data)387 ms_pci_probe(DriverPtr driver,
388              int entity_num, struct pci_device *dev, intptr_t match_data)
389 {
390     ScrnInfoPtr scrn = NULL;
391 
392     scrn = xf86ConfigPciEntity(scrn, 0, entity_num, NULL,
393                                NULL, NULL, NULL, NULL, NULL);
394     if (scrn) {
395         const char *devpath;
396         GDevPtr devSection = xf86GetDevFromEntity(scrn->entityList[0],
397                                                   scrn->entityInstanceList[0]);
398 
399         devpath = xf86FindOptionValue(devSection->options, "kmsdev");
400         if (probe_hw_pci(devpath, dev)) {
401             ms_setup_scrn_hooks(scrn);
402 
403             xf86DrvMsg(scrn->scrnIndex, X_CONFIG,
404                        "claimed PCI slot %d@%d:%d:%d\n",
405                        dev->bus, dev->domain, dev->dev, dev->func);
406             xf86DrvMsg(scrn->scrnIndex, X_INFO,
407                        "using %s\n", devpath ? devpath : "default device");
408 
409             ms_setup_entity(scrn, entity_num);
410         }
411         else
412             scrn = NULL;
413     }
414     return scrn != NULL;
415 }
416 #endif
417 
418 #ifdef XSERVER_PLATFORM_BUS
419 static Bool
ms_platform_probe(DriverPtr driver,int entity_num,int flags,struct xf86_platform_device * dev,intptr_t match_data)420 ms_platform_probe(DriverPtr driver,
421                   int entity_num, int flags, struct xf86_platform_device *dev,
422                   intptr_t match_data)
423 {
424     ScrnInfoPtr scrn = NULL;
425     const char *path = xf86_platform_device_odev_attributes(dev)->path;
426     int scr_flags = 0;
427 
428     if (flags & PLATFORM_PROBE_GPU_SCREEN)
429         scr_flags = XF86_ALLOCATE_GPU_SCREEN;
430 
431     if (probe_hw(path, dev)) {
432         scrn = xf86AllocateScreen(driver, scr_flags);
433         if (xf86IsEntitySharable(entity_num))
434             xf86SetEntityShared(entity_num);
435         xf86AddEntityToScreen(scrn, entity_num);
436 
437         ms_setup_scrn_hooks(scrn);
438 
439         xf86DrvMsg(scrn->scrnIndex, X_INFO,
440                    "using drv %s\n", path ? path : "default device");
441 
442         ms_setup_entity(scrn, entity_num);
443     }
444 
445     return scrn != NULL;
446 }
447 #endif
448 
449 static Bool
Probe(DriverPtr drv,int flags)450 Probe(DriverPtr drv, int flags)
451 {
452     int i, numDevSections;
453     GDevPtr *devSections;
454     Bool foundScreen = FALSE;
455     const char *dev;
456     ScrnInfoPtr scrn = NULL;
457 
458     /* For now, just bail out for PROBE_DETECT. */
459     if (flags & PROBE_DETECT)
460         return FALSE;
461 
462     /*
463      * Find the config file Device sections that match this
464      * driver, and return if there are none.
465      */
466     if ((numDevSections = xf86MatchDevice("modesetting", &devSections)) <= 0) {
467         return FALSE;
468     }
469 
470     for (i = 0; i < numDevSections; i++) {
471         int entity_num;
472         dev = xf86FindOptionValue(devSections[i]->options, "kmsdev");
473         if (probe_hw(dev, NULL)) {
474 
475             entity_num = xf86ClaimFbSlot(drv, 0, devSections[i], TRUE);
476             scrn = xf86ConfigFbEntity(scrn, 0, entity_num, NULL, NULL, NULL, NULL);
477         }
478 
479         if (scrn) {
480             foundScreen = TRUE;
481             ms_setup_scrn_hooks(scrn);
482             scrn->Probe = Probe;
483 
484             xf86DrvMsg(scrn->scrnIndex, X_INFO,
485                        "using %s\n", dev ? dev : "default device");
486             ms_setup_entity(scrn, entity_num);
487         }
488     }
489 
490     free(devSections);
491 
492     return foundScreen;
493 }
494 
495 static Bool
GetRec(ScrnInfoPtr pScrn)496 GetRec(ScrnInfoPtr pScrn)
497 {
498     if (pScrn->driverPrivate)
499         return TRUE;
500 
501     pScrn->driverPrivate = xnfcalloc(sizeof(modesettingRec), 1);
502 
503     return TRUE;
504 }
505 
506 static int
dispatch_dirty_region(ScrnInfoPtr scrn,PixmapPtr pixmap,DamagePtr damage,int fb_id)507 dispatch_dirty_region(ScrnInfoPtr scrn,
508                       PixmapPtr pixmap, DamagePtr damage, int fb_id)
509 {
510     modesettingPtr ms = modesettingPTR(scrn);
511     RegionPtr dirty = DamageRegion(damage);
512     unsigned num_cliprects = REGION_NUM_RECTS(dirty);
513     int ret = 0;
514 
515     if (num_cliprects) {
516         drmModeClip *clip = xallocarray(num_cliprects, sizeof(drmModeClip));
517         BoxPtr rect = REGION_RECTS(dirty);
518         int i;
519 
520         if (!clip)
521             return -ENOMEM;
522 
523         /* XXX no need for copy? */
524         for (i = 0; i < num_cliprects; i++, rect++) {
525             clip[i].x1 = rect->x1;
526             clip[i].y1 = rect->y1;
527             clip[i].x2 = rect->x2;
528             clip[i].y2 = rect->y2;
529         }
530 
531         /* TODO query connector property to see if this is needed */
532         ret = drmModeDirtyFB(ms->fd, fb_id, clip, num_cliprects);
533 
534         /* if we're swamping it with work, try one at a time */
535         if (ret == -EINVAL) {
536             for (i = 0; i < num_cliprects; i++) {
537                 if ((ret = drmModeDirtyFB(ms->fd, fb_id, &clip[i], 1)) < 0)
538                     break;
539             }
540         }
541 
542         free(clip);
543         DamageEmpty(damage);
544     }
545     return ret;
546 }
547 
548 static void
dispatch_dirty(ScreenPtr pScreen)549 dispatch_dirty(ScreenPtr pScreen)
550 {
551     ScrnInfoPtr scrn = xf86ScreenToScrn(pScreen);
552     modesettingPtr ms = modesettingPTR(scrn);
553     PixmapPtr pixmap = pScreen->GetScreenPixmap(pScreen);
554     int fb_id = ms->drmmode.fb_id;
555     int ret;
556 
557     ret = dispatch_dirty_region(scrn, pixmap, ms->damage, fb_id);
558     if (ret == -EINVAL || ret == -ENOSYS) {
559         ms->dirty_enabled = FALSE;
560         DamageUnregister(ms->damage);
561         DamageDestroy(ms->damage);
562         ms->damage = NULL;
563         xf86DrvMsg(scrn->scrnIndex, X_INFO,
564                    "Disabling kernel dirty updates, not required.\n");
565         return;
566     }
567 }
568 
569 static void
dispatch_dirty_pixmap(ScrnInfoPtr scrn,xf86CrtcPtr crtc,PixmapPtr ppix)570 dispatch_dirty_pixmap(ScrnInfoPtr scrn, xf86CrtcPtr crtc, PixmapPtr ppix)
571 {
572     modesettingPtr ms = modesettingPTR(scrn);
573     msPixmapPrivPtr ppriv = msGetPixmapPriv(&ms->drmmode, ppix);
574     DamagePtr damage = ppriv->slave_damage;
575     int fb_id = ppriv->fb_id;
576 
577     dispatch_dirty_region(scrn, ppix, damage, fb_id);
578 }
579 
580 static void
dispatch_slave_dirty(ScreenPtr pScreen)581 dispatch_slave_dirty(ScreenPtr pScreen)
582 {
583     ScrnInfoPtr scrn = xf86ScreenToScrn(pScreen);
584     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
585     int c;
586 
587     for (c = 0; c < xf86_config->num_crtc; c++) {
588         xf86CrtcPtr crtc = xf86_config->crtc[c];
589         drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
590 
591         if (!drmmode_crtc)
592             continue;
593 
594         if (drmmode_crtc->prime_pixmap)
595             dispatch_dirty_pixmap(scrn, crtc, drmmode_crtc->prime_pixmap);
596         if (drmmode_crtc->prime_pixmap_back)
597             dispatch_dirty_pixmap(scrn, crtc, drmmode_crtc->prime_pixmap_back);
598     }
599 }
600 
601 static void
redisplay_dirty(ScreenPtr screen,PixmapDirtyUpdatePtr dirty,int * timeout)602 redisplay_dirty(ScreenPtr screen, PixmapDirtyUpdatePtr dirty, int *timeout)
603 {
604     RegionRec pixregion;
605 
606     PixmapRegionInit(&pixregion, dirty->slave_dst);
607     DamageRegionAppend(&dirty->slave_dst->drawable, &pixregion);
608     PixmapSyncDirtyHelper(dirty);
609 
610     if (!screen->isGPU) {
611 #ifdef GLAMOR_HAS_GBM
612         modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(screen));
613         /*
614          * When copying from the master framebuffer to the shared pixmap,
615          * we must ensure the copy is complete before the slave starts a
616          * copy to its own framebuffer (some slaves scanout directly from
617          * the shared pixmap, but not all).
618          */
619         if (ms->drmmode.glamor)
620             glamor_finish(screen);
621 #endif
622         /* Ensure the slave processes the damage immediately */
623         if (timeout)
624             *timeout = 0;
625     }
626 
627     DamageRegionProcessPending(&dirty->slave_dst->drawable);
628     RegionUninit(&pixregion);
629 }
630 
631 static void
ms_dirty_update(ScreenPtr screen,int * timeout)632 ms_dirty_update(ScreenPtr screen, int *timeout)
633 {
634     modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(screen));
635 
636     RegionPtr region;
637     PixmapDirtyUpdatePtr ent;
638 
639     if (xorg_list_is_empty(&screen->pixmap_dirty_list))
640         return;
641 
642     xorg_list_for_each_entry(ent, &screen->pixmap_dirty_list, ent) {
643         region = DamageRegion(ent->damage);
644         if (RegionNotEmpty(region)) {
645             if (!screen->isGPU) {
646                    msPixmapPrivPtr ppriv =
647                     msGetPixmapPriv(&ms->drmmode, ent->slave_dst->master_pixmap);
648 
649                 if (ppriv->notify_on_damage) {
650                     ppriv->notify_on_damage = FALSE;
651 
652                     ent->slave_dst->drawable.pScreen->
653                         SharedPixmapNotifyDamage(ent->slave_dst);
654                 }
655 
656                 /* Requested manual updating */
657                 if (ppriv->defer_dirty_update)
658                     continue;
659             }
660 
661             redisplay_dirty(screen, ent, timeout);
662             DamageEmpty(ent->damage);
663         }
664     }
665 }
666 
667 static PixmapDirtyUpdatePtr
ms_dirty_get_ent(ScreenPtr screen,PixmapPtr slave_dst)668 ms_dirty_get_ent(ScreenPtr screen, PixmapPtr slave_dst)
669 {
670     PixmapDirtyUpdatePtr ent;
671 
672     if (xorg_list_is_empty(&screen->pixmap_dirty_list))
673         return NULL;
674 
675     xorg_list_for_each_entry(ent, &screen->pixmap_dirty_list, ent) {
676         if (ent->slave_dst == slave_dst)
677             return ent;
678     }
679 
680     return NULL;
681 }
682 
683 static void
msBlockHandler(ScreenPtr pScreen,void * timeout)684 msBlockHandler(ScreenPtr pScreen, void *timeout)
685 {
686     modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(pScreen));
687 
688     pScreen->BlockHandler = ms->BlockHandler;
689     pScreen->BlockHandler(pScreen, timeout);
690     ms->BlockHandler = pScreen->BlockHandler;
691     pScreen->BlockHandler = msBlockHandler;
692     if (pScreen->isGPU && !ms->drmmode.reverse_prime_offload_mode)
693         dispatch_slave_dirty(pScreen);
694     else if (ms->dirty_enabled)
695         dispatch_dirty(pScreen);
696 
697     ms_dirty_update(pScreen, timeout);
698 }
699 
700 static void
msBlockHandler_oneshot(ScreenPtr pScreen,void * pTimeout)701 msBlockHandler_oneshot(ScreenPtr pScreen, void *pTimeout)
702 {
703     ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
704     modesettingPtr ms = modesettingPTR(pScrn);
705 
706     msBlockHandler(pScreen, pTimeout);
707 
708     drmmode_set_desired_modes(pScrn, &ms->drmmode, TRUE, FALSE);
709 }
710 
711 static void
FreeRec(ScrnInfoPtr pScrn)712 FreeRec(ScrnInfoPtr pScrn)
713 {
714     modesettingPtr ms;
715 
716     if (!pScrn)
717         return;
718 
719     ms = modesettingPTR(pScrn);
720     if (!ms)
721         return;
722 
723     if (ms->fd > 0) {
724         modesettingEntPtr ms_ent;
725         int ret;
726 
727         ms_ent = ms_ent_priv(pScrn);
728         ms_ent->fd_ref--;
729         if (!ms_ent->fd_ref) {
730             if (ms->pEnt->location.type == BUS_PCI)
731                 ret = drmClose(ms->fd);
732             else
733 #ifdef XF86_PDEV_SERVER_FD
734                 if (!(ms->pEnt->location.type == BUS_PLATFORM &&
735                       (ms->pEnt->location.id.plat->flags & XF86_PDEV_SERVER_FD)))
736 #endif
737                     ret = close(ms->fd);
738             (void) ret;
739             ms_ent->fd = 0;
740         }
741     }
742     pScrn->driverPrivate = NULL;
743     free(ms->drmmode.Options);
744     free(ms);
745 
746 }
747 
748 static void
try_enable_glamor(ScrnInfoPtr pScrn)749 try_enable_glamor(ScrnInfoPtr pScrn)
750 {
751     modesettingPtr ms = modesettingPTR(pScrn);
752     const char *accel_method_str = xf86GetOptValString(ms->drmmode.Options,
753                                                        OPTION_ACCEL_METHOD);
754     Bool do_glamor = FALSE;
755 
756     ms->drmmode.glamor = FALSE;
757 
758 #ifdef GLAMOR_HAS_GBM
759     if (ms->drmmode.force_24_32) {
760         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Cannot use glamor with 24bpp packed fb\n");
761         return;
762     }
763 
764     if (!do_glamor) {
765         xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "glamor disabled\n");
766         return;
767     }
768 
769     if (xf86LoadSubModule(pScrn, GLAMOR_EGL_MODULE_NAME)) {
770         if (glamor_egl_init(pScrn, ms->fd)) {
771             xf86DrvMsg(pScrn->scrnIndex, X_INFO, "glamor initialized\n");
772             ms->drmmode.glamor = TRUE;
773         } else {
774             xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
775                        "glamor initialization failed\n");
776         }
777     } else {
778         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
779                    "Failed to load glamor module.\n");
780     }
781 #else
782     if (do_glamor) {
783         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
784                    "No glamor support in the X Server\n");
785     }
786 #endif
787 }
788 
789 static Bool
msShouldDoubleShadow(ScrnInfoPtr pScrn,modesettingPtr ms)790 msShouldDoubleShadow(ScrnInfoPtr pScrn, modesettingPtr ms)
791 {
792     Bool ret = FALSE, asked;
793     int from;
794     drmVersionPtr v = drmGetVersion(ms->fd);
795 
796     if (!ms->drmmode.shadow_enable)
797         return FALSE;
798 
799     if (!strcmp(v->name, "mgag200") ||
800         !strcmp(v->name, "ast")) /* XXX || rn50 */
801         ret = TRUE;
802 
803     drmFreeVersion(v);
804 
805     asked = xf86GetOptValBool(ms->drmmode.Options, OPTION_DOUBLE_SHADOW, &ret);
806 
807     if (asked)
808         from = X_CONFIG;
809     else
810         from = X_INFO;
811 
812     xf86DrvMsg(pScrn->scrnIndex, from,
813                "Double-buffered shadow updates: %s\n", ret ? "on" : "off");
814 
815     return ret;
816 }
817 
818 static Bool
ms_get_drm_master_fd(ScrnInfoPtr pScrn)819 ms_get_drm_master_fd(ScrnInfoPtr pScrn)
820 {
821     EntityInfoPtr pEnt;
822     modesettingPtr ms;
823     modesettingEntPtr ms_ent;
824 
825     ms = modesettingPTR(pScrn);
826     ms_ent = ms_ent_priv(pScrn);
827 
828     pEnt = ms->pEnt;
829 
830     if (ms_ent->fd) {
831         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
832                    " reusing fd for second head\n");
833         ms->fd = ms_ent->fd;
834         ms_ent->fd_ref++;
835         return TRUE;
836     }
837 
838     ms->fd_passed = FALSE;
839     if ((ms->fd = get_passed_fd()) >= 0) {
840         ms->fd_passed = TRUE;
841         return TRUE;
842     }
843 
844 #ifdef XSERVER_PLATFORM_BUS
845     if (pEnt->location.type == BUS_PLATFORM) {
846 #ifdef XF86_PDEV_SERVER_FD
847         if (pEnt->location.id.plat->flags & XF86_PDEV_SERVER_FD)
848             ms->fd =
849                 xf86_platform_device_odev_attributes(pEnt->location.id.plat)->
850                 fd;
851         else
852 #endif
853         {
854             char *path =
855                 xf86_platform_device_odev_attributes(pEnt->location.id.plat)->
856                 path;
857             ms->fd = open_hw(path);
858         }
859     }
860     else
861 #endif
862 #ifdef XSERVER_LIBPCIACCESS
863     if (pEnt->location.type == BUS_PCI) {
864         char *BusID = NULL;
865         struct pci_device *PciInfo;
866 
867         PciInfo = xf86GetPciInfoForEntity(ms->pEnt->index);
868         if (PciInfo) {
869             if ((BusID = ms_DRICreatePCIBusID(PciInfo)) != NULL) {
870                 ms->fd = drmOpen(NULL, BusID);
871                 free(BusID);
872             }
873         }
874     }
875     else
876 #endif
877     {
878         const char *devicename;
879         devicename = xf86FindOptionValue(ms->pEnt->device->options, "kmsdev");
880         ms->fd = open_hw(devicename);
881     }
882     if (ms->fd < 0)
883         return FALSE;
884 
885     ms_ent->fd = ms->fd;
886     ms_ent->fd_ref = 1;
887     return TRUE;
888 }
889 
890 static Bool
PreInit(ScrnInfoPtr pScrn,int flags)891 PreInit(ScrnInfoPtr pScrn, int flags)
892 {
893     modesettingPtr ms;
894     rgb defaultWeight = { 0, 0, 0 };
895     EntityInfoPtr pEnt;
896     uint64_t value = 0;
897     int ret;
898     int bppflags, connector_count;
899     int defaultdepth, defaultbpp;
900 
901     if (pScrn->numEntities != 1)
902         return FALSE;
903 
904     if (flags & PROBE_DETECT) {
905         return FALSE;
906     }
907 
908     /* Allocate driverPrivate */
909     if (!GetRec(pScrn))
910         return FALSE;
911 
912     pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
913 
914     ms = modesettingPTR(pScrn);
915     ms->SaveGeneration = -1;
916     ms->pEnt = pEnt;
917     ms->drmmode.is_secondary = FALSE;
918     pScrn->displayWidth = 640;  /* default it */
919 
920     if (xf86IsEntityShared(pScrn->entityList[0])) {
921         if (xf86IsPrimInitDone(pScrn->entityList[0]))
922             ms->drmmode.is_secondary = TRUE;
923         else
924             xf86SetPrimInitDone(pScrn->entityList[0]);
925     }
926 
927     pScrn->monitor = pScrn->confScreen->monitor;
928     pScrn->progClock = TRUE;
929     pScrn->rgbBits = 8;
930 
931     if (!ms_get_drm_master_fd(pScrn))
932         return FALSE;
933     ms->drmmode.fd = ms->fd;
934 
935     if (!check_outputs(ms->fd, &connector_count))
936         return FALSE;
937 
938     drmmode_get_default_bpp(pScrn, &ms->drmmode, &defaultdepth, &defaultbpp);
939     if (defaultdepth == 24 && defaultbpp == 24) {
940         ms->drmmode.force_24_32 = TRUE;
941         ms->drmmode.kbpp = 24;
942         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
943                    "Using 24bpp hw front buffer with 32bpp shadow\n");
944         defaultbpp = 32;
945     } else {
946         ms->drmmode.kbpp = 0;
947     }
948     bppflags = PreferConvert24to32 | SupportConvert24to32 | Support32bppFb;
949 
950     if (!xf86SetDepthBpp
951         (pScrn, defaultdepth, defaultdepth, defaultbpp, bppflags))
952         return FALSE;
953 
954     switch (pScrn->depth) {
955     case 15:
956     case 16:
957     case 24:
958     case 30:
959         break;
960     default:
961         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
962                    "Given depth (%d) is not supported by the driver\n",
963                    pScrn->depth);
964         return FALSE;
965     }
966     xf86PrintDepthBpp(pScrn);
967     if (!ms->drmmode.kbpp)
968         ms->drmmode.kbpp = pScrn->bitsPerPixel;
969 
970     /* Process the options */
971     xf86CollectOptions(pScrn, NULL);
972     if (!(ms->drmmode.Options = malloc(sizeof(Options))))
973         return FALSE;
974     memcpy(ms->drmmode.Options, Options, sizeof(Options));
975     xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, ms->drmmode.Options);
976 
977     if (!xf86SetWeight(pScrn, defaultWeight, defaultWeight))
978         return FALSE;
979     if (!xf86SetDefaultVisual(pScrn, -1))
980         return FALSE;
981 
982     if (xf86ReturnOptValBool(ms->drmmode.Options, OPTION_SW_CURSOR, FALSE)) {
983         ms->drmmode.sw_cursor = TRUE;
984     }
985 
986     ms->cursor_width = 64;
987     ms->cursor_height = 64;
988     ret = drmGetCap(ms->fd, DRM_CAP_CURSOR_WIDTH, &value);
989     if (!ret) {
990         ms->cursor_width = value;
991     }
992     ret = drmGetCap(ms->fd, DRM_CAP_CURSOR_HEIGHT, &value);
993     if (!ret) {
994         ms->cursor_height = value;
995     }
996 
997     try_enable_glamor(pScrn);
998 
999     if (!ms->drmmode.glamor) {
1000         Bool prefer_shadow = TRUE;
1001 
1002         if (ms->drmmode.force_24_32) {
1003             prefer_shadow = TRUE;
1004             ms->drmmode.shadow_enable = TRUE;
1005         } else {
1006             ret = drmGetCap(ms->fd, DRM_CAP_DUMB_PREFER_SHADOW, &value);
1007             if (!ret) {
1008                 prefer_shadow = !!value;
1009             }
1010 
1011             ms->drmmode.shadow_enable =
1012                 xf86ReturnOptValBool(ms->drmmode.Options, OPTION_SHADOW_FB,
1013                                      prefer_shadow);
1014         }
1015 
1016         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1017                    "ShadowFB: preferred %s, enabled %s\n",
1018                    prefer_shadow ? "YES" : "NO",
1019                    ms->drmmode.force_24_32 ? "FORCE" :
1020                    ms->drmmode.shadow_enable ? "YES" : "NO");
1021 
1022         ms->drmmode.shadow_enable2 = msShouldDoubleShadow(pScrn, ms);
1023     }
1024 
1025     ms->drmmode.pageflip =
1026         xf86ReturnOptValBool(ms->drmmode.Options, OPTION_PAGEFLIP, TRUE);
1027 
1028     pScrn->capabilities = 0;
1029     ret = drmGetCap(ms->fd, DRM_CAP_PRIME, &value);
1030     if (ret == 0) {
1031         if (connector_count && (value & DRM_PRIME_CAP_IMPORT)) {
1032             pScrn->capabilities |= RR_Capability_SinkOutput;
1033             if (ms->drmmode.glamor)
1034                 pScrn->capabilities |= RR_Capability_SinkOffload;
1035         }
1036 #ifdef GLAMOR_HAS_GBM_LINEAR
1037         if (value & DRM_PRIME_CAP_EXPORT && ms->drmmode.glamor)
1038             pScrn->capabilities |= RR_Capability_SourceOutput | RR_Capability_SourceOffload;
1039 #endif
1040     }
1041 
1042     if (xf86ReturnOptValBool(ms->drmmode.Options, OPTION_ATOMIC, FALSE)) {
1043         ret = drmSetClientCap(ms->fd, DRM_CLIENT_CAP_ATOMIC, 1);
1044         ms->atomic_modeset = (ret == 0);
1045     } else {
1046         ms->atomic_modeset = FALSE;
1047     }
1048 
1049     ms->kms_has_modifiers = FALSE;
1050     ret = drmGetCap(ms->fd, DRM_CAP_ADDFB2_MODIFIERS, &value);
1051     if (ret == 0 && value != 0)
1052         ms->kms_has_modifiers = TRUE;
1053 
1054     if (drmmode_pre_init(pScrn, &ms->drmmode, pScrn->bitsPerPixel / 8) == FALSE) {
1055         xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "KMS setup failed\n");
1056         goto fail;
1057     }
1058 
1059     /*
1060      * If the driver can do gamma correction, it should call xf86SetGamma() here.
1061      */
1062     {
1063         Gamma zeros = { 0.0, 0.0, 0.0 };
1064 
1065         if (!xf86SetGamma(pScrn, zeros)) {
1066             return FALSE;
1067         }
1068     }
1069 
1070     if (!(pScrn->is_gpu && connector_count == 0) && pScrn->modes == NULL) {
1071         xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No modes.\n");
1072         return FALSE;
1073     }
1074 
1075     pScrn->currentMode = pScrn->modes;
1076 
1077     /* Set display resolution */
1078     xf86SetDpi(pScrn, 0, 0);
1079 
1080     /* Load the required sub modules */
1081     if (!xf86LoadSubModule(pScrn, "fb")) {
1082         return FALSE;
1083     }
1084 
1085     if (ms->drmmode.shadow_enable) {
1086         if (!xf86LoadSubModule(pScrn, "shadow")) {
1087             return FALSE;
1088         }
1089     }
1090 
1091     return TRUE;
1092  fail:
1093     return FALSE;
1094 }
1095 
1096 static void *
msShadowWindow(ScreenPtr screen,CARD32 row,CARD32 offset,int mode,CARD32 * size,void * closure)1097 msShadowWindow(ScreenPtr screen, CARD32 row, CARD32 offset, int mode,
1098                CARD32 *size, void *closure)
1099 {
1100     ScrnInfoPtr pScrn = xf86ScreenToScrn(screen);
1101     modesettingPtr ms = modesettingPTR(pScrn);
1102     int stride;
1103 
1104     stride = (pScrn->displayWidth * ms->drmmode.kbpp) / 8;
1105     *size = stride;
1106 
1107     return ((uint8_t *) ms->drmmode.front_bo.dumb->ptr + row * stride + offset);
1108 }
1109 
1110 /* somewhat arbitrary tile size, in pixels */
1111 #define TILE 16
1112 
1113 static int
msUpdateIntersect(modesettingPtr ms,shadowBufPtr pBuf,BoxPtr box,xRectangle * prect)1114 msUpdateIntersect(modesettingPtr ms, shadowBufPtr pBuf, BoxPtr box,
1115                   xRectangle *prect)
1116 {
1117     int i, dirty = 0, stride = pBuf->pPixmap->devKind, cpp = ms->drmmode.cpp;
1118     int width = (box->x2 - box->x1) * cpp;
1119     unsigned char *old, *new;
1120 
1121     old = ms->drmmode.shadow_fb2;
1122     old += (box->y1 * stride) + (box->x1 * cpp);
1123     new = ms->drmmode.shadow_fb;
1124     new += (box->y1 * stride) + (box->x1 * cpp);
1125 
1126     for (i = box->y2 - box->y1 - 1; i >= 0; i--) {
1127         unsigned char *o = old + i * stride,
1128                       *n = new + i * stride;
1129         if (memcmp(o, n, width) != 0) {
1130             dirty = 1;
1131             memcpy(o, n, width);
1132         }
1133     }
1134 
1135     if (dirty) {
1136         prect->x = box->x1;
1137         prect->y = box->y1;
1138         prect->width = box->x2 - box->x1;
1139         prect->height = box->y2 - box->y1;
1140     }
1141 
1142     return dirty;
1143 }
1144 
1145 static void
msUpdatePacked(ScreenPtr pScreen,shadowBufPtr pBuf)1146 msUpdatePacked(ScreenPtr pScreen, shadowBufPtr pBuf)
1147 {
1148     ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1149     modesettingPtr ms = modesettingPTR(pScrn);
1150     Bool use_3224 = ms->drmmode.force_24_32 && pScrn->bitsPerPixel == 32;
1151 
1152     if (ms->drmmode.shadow_enable2 && ms->drmmode.shadow_fb2) do {
1153         RegionPtr damage = DamageRegion(pBuf->pDamage), tiles;
1154         BoxPtr extents = RegionExtents(damage);
1155         xRectangle *prect;
1156         int nrects;
1157         int i, j, tx1, tx2, ty1, ty2;
1158 
1159         tx1 = extents->x1 / TILE;
1160         tx2 = (extents->x2 + TILE - 1) / TILE;
1161         ty1 = extents->y1 / TILE;
1162         ty2 = (extents->y2 + TILE - 1) / TILE;
1163 
1164         nrects = (tx2 - tx1) * (ty2 - ty1);
1165         if (!(prect = calloc(nrects, sizeof(xRectangle))))
1166             break;
1167 
1168         nrects = 0;
1169         for (j = ty2 - 1; j >= ty1; j--) {
1170             for (i = tx2 - 1; i >= tx1; i--) {
1171                 BoxRec box;
1172 
1173                 box.x1 = max(i * TILE, extents->x1);
1174                 box.y1 = max(j * TILE, extents->y1);
1175                 box.x2 = min((i+1) * TILE, extents->x2);
1176                 box.y2 = min((j+1) * TILE, extents->y2);
1177 
1178                 if (RegionContainsRect(damage, &box) != rgnOUT) {
1179                     if (msUpdateIntersect(ms, pBuf, &box, prect + nrects)) {
1180                         nrects++;
1181                     }
1182                 }
1183             }
1184         }
1185 
1186         tiles = RegionFromRects(nrects, prect, CT_NONE);
1187         RegionIntersect(damage, damage, tiles);
1188         RegionDestroy(tiles);
1189         free(prect);
1190     } while (0);
1191 
1192     if (use_3224)
1193         shadowUpdate32to24(pScreen, pBuf);
1194     else
1195         shadowUpdatePacked(pScreen, pBuf);
1196 }
1197 
1198 static Bool
msEnableSharedPixmapFlipping(RRCrtcPtr crtc,PixmapPtr front,PixmapPtr back)1199 msEnableSharedPixmapFlipping(RRCrtcPtr crtc, PixmapPtr front, PixmapPtr back)
1200 {
1201     ScreenPtr screen = crtc->pScreen;
1202     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
1203     modesettingPtr ms = modesettingPTR(scrn);
1204     EntityInfoPtr pEnt = ms->pEnt;
1205     xf86CrtcPtr xf86Crtc = crtc->devPrivate;
1206 
1207     if (!xf86Crtc)
1208         return FALSE;
1209 
1210     /* Not supported if we can't flip */
1211     if (!ms->drmmode.pageflip)
1212         return FALSE;
1213 
1214     /* Not currently supported with reverse PRIME */
1215     if (ms->drmmode.reverse_prime_offload_mode)
1216         return FALSE;
1217 
1218 #ifdef XSERVER_PLATFORM_BUS
1219     if (pEnt->location.type == BUS_PLATFORM) {
1220         char *syspath =
1221             xf86_platform_device_odev_attributes(pEnt->location.id.plat)->
1222             syspath;
1223 
1224         /* Not supported for devices using USB transport due to misbehaved
1225          * vblank events */
1226         if (syspath && strstr(syspath, "usb"))
1227             return FALSE;
1228 
1229         /* EVDI uses USB transport but is platform device, not usb.
1230          * Blacklist it explicitly */
1231         if (syspath && strstr(syspath, "evdi"))
1232             return FALSE;
1233     }
1234 #endif
1235 
1236     return drmmode_EnableSharedPixmapFlipping(xf86Crtc, &ms->drmmode,
1237                                               front, back);
1238 }
1239 
1240 static void
msDisableSharedPixmapFlipping(RRCrtcPtr crtc)1241 msDisableSharedPixmapFlipping(RRCrtcPtr crtc)
1242 {
1243     ScreenPtr screen = crtc->pScreen;
1244     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
1245     modesettingPtr ms = modesettingPTR(scrn);
1246     xf86CrtcPtr xf86Crtc = crtc->devPrivate;
1247 
1248     if (xf86Crtc)
1249         drmmode_DisableSharedPixmapFlipping(xf86Crtc, &ms->drmmode);
1250 }
1251 
1252 static Bool
msStartFlippingPixmapTracking(RRCrtcPtr crtc,DrawablePtr src,PixmapPtr slave_dst1,PixmapPtr slave_dst2,int x,int y,int dst_x,int dst_y,Rotation rotation)1253 msStartFlippingPixmapTracking(RRCrtcPtr crtc, DrawablePtr src,
1254                               PixmapPtr slave_dst1, PixmapPtr slave_dst2,
1255                               int x, int y, int dst_x, int dst_y,
1256                               Rotation rotation)
1257 {
1258     ScreenPtr pScreen = src->pScreen;
1259     modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(pScreen));
1260 
1261     msPixmapPrivPtr ppriv1 = msGetPixmapPriv(&ms->drmmode, slave_dst1->master_pixmap),
1262                     ppriv2 = msGetPixmapPriv(&ms->drmmode, slave_dst2->master_pixmap);
1263 
1264     if (!PixmapStartDirtyTracking(src, slave_dst1, x, y,
1265                                   dst_x, dst_y, rotation)) {
1266         return FALSE;
1267     }
1268 
1269     if (!PixmapStartDirtyTracking(src, slave_dst2, x, y,
1270                                   dst_x, dst_y, rotation)) {
1271         PixmapStopDirtyTracking(src, slave_dst1);
1272         return FALSE;
1273     }
1274 
1275     ppriv1->slave_src = src;
1276     ppriv2->slave_src = src;
1277 
1278     ppriv1->dirty = ms_dirty_get_ent(pScreen, slave_dst1);
1279     ppriv2->dirty = ms_dirty_get_ent(pScreen, slave_dst2);
1280 
1281     ppriv1->defer_dirty_update = TRUE;
1282     ppriv2->defer_dirty_update = TRUE;
1283 
1284     return TRUE;
1285 }
1286 
1287 static Bool
msPresentSharedPixmap(PixmapPtr slave_dst)1288 msPresentSharedPixmap(PixmapPtr slave_dst)
1289 {
1290     ScreenPtr pScreen = slave_dst->master_pixmap->drawable.pScreen;
1291     modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(pScreen));
1292 
1293     msPixmapPrivPtr ppriv = msGetPixmapPriv(&ms->drmmode, slave_dst->master_pixmap);
1294 
1295     RegionPtr region = DamageRegion(ppriv->dirty->damage);
1296 
1297     if (RegionNotEmpty(region)) {
1298         redisplay_dirty(ppriv->slave_src->pScreen, ppriv->dirty, NULL);
1299         DamageEmpty(ppriv->dirty->damage);
1300 
1301         return TRUE;
1302     }
1303 
1304     return FALSE;
1305 }
1306 
1307 static Bool
msStopFlippingPixmapTracking(DrawablePtr src,PixmapPtr slave_dst1,PixmapPtr slave_dst2)1308 msStopFlippingPixmapTracking(DrawablePtr src,
1309                              PixmapPtr slave_dst1, PixmapPtr slave_dst2)
1310 {
1311     ScreenPtr pScreen = src->pScreen;
1312     modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(pScreen));
1313 
1314     msPixmapPrivPtr ppriv1 = msGetPixmapPriv(&ms->drmmode, slave_dst1->master_pixmap),
1315                     ppriv2 = msGetPixmapPriv(&ms->drmmode, slave_dst2->master_pixmap);
1316 
1317     Bool ret = TRUE;
1318 
1319     ret &= PixmapStopDirtyTracking(src, slave_dst1);
1320     ret &= PixmapStopDirtyTracking(src, slave_dst2);
1321 
1322     if (ret) {
1323         ppriv1->slave_src = NULL;
1324         ppriv2->slave_src = NULL;
1325 
1326         ppriv1->dirty = NULL;
1327         ppriv2->dirty = NULL;
1328 
1329         ppriv1->defer_dirty_update = FALSE;
1330         ppriv2->defer_dirty_update = FALSE;
1331     }
1332 
1333     return ret;
1334 }
1335 
1336 static Bool
CreateScreenResources(ScreenPtr pScreen)1337 CreateScreenResources(ScreenPtr pScreen)
1338 {
1339     ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1340     modesettingPtr ms = modesettingPTR(pScrn);
1341     PixmapPtr rootPixmap;
1342     Bool ret;
1343     void *pixels = NULL;
1344     int err;
1345 
1346     pScreen->CreateScreenResources = ms->createScreenResources;
1347     ret = pScreen->CreateScreenResources(pScreen);
1348     pScreen->CreateScreenResources = CreateScreenResources;
1349 
1350     if (!drmmode_set_desired_modes(pScrn, &ms->drmmode, pScrn->is_gpu, FALSE))
1351         return FALSE;
1352 
1353     if (!drmmode_glamor_handle_new_screen_pixmap(&ms->drmmode))
1354         return FALSE;
1355 
1356     drmmode_uevent_init(pScrn, &ms->drmmode);
1357 
1358     if (!ms->drmmode.sw_cursor)
1359         drmmode_map_cursor_bos(pScrn, &ms->drmmode);
1360 
1361     if (!ms->drmmode.gbm) {
1362         pixels = drmmode_map_front_bo(&ms->drmmode);
1363         if (!pixels)
1364             return FALSE;
1365     }
1366 
1367     rootPixmap = pScreen->GetScreenPixmap(pScreen);
1368 
1369     if (ms->drmmode.shadow_enable)
1370         pixels = ms->drmmode.shadow_fb;
1371 
1372     if (ms->drmmode.shadow_enable2) {
1373         ms->drmmode.shadow_fb2 = calloc(1, pScrn->displayWidth * pScrn->virtualY * ((pScrn->bitsPerPixel + 7) >> 3));
1374         if (!ms->drmmode.shadow_fb2)
1375             ms->drmmode.shadow_enable2 = FALSE;
1376     }
1377 
1378     if (!pScreen->ModifyPixmapHeader(rootPixmap, -1, -1, -1, -1, -1, pixels))
1379         FatalError("Couldn't adjust screen pixmap\n");
1380 
1381     if (ms->drmmode.shadow_enable) {
1382         if (!shadowAdd(pScreen, rootPixmap, msUpdatePacked, msShadowWindow,
1383                        0, 0))
1384             return FALSE;
1385     }
1386 
1387     err = drmModeDirtyFB(ms->fd, ms->drmmode.fb_id, NULL, 0);
1388 
1389     if (err != -EINVAL && err != -ENOSYS) {
1390         ms->damage = DamageCreate(NULL, NULL, DamageReportNone, TRUE,
1391                                   pScreen, rootPixmap);
1392 
1393         if (ms->damage) {
1394             DamageRegister(&rootPixmap->drawable, ms->damage);
1395             ms->dirty_enabled = TRUE;
1396             xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Damage tracking initialized\n");
1397         }
1398         else {
1399             xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1400                        "Failed to create screen damage record\n");
1401             return FALSE;
1402         }
1403     }
1404 
1405     if (dixPrivateKeyRegistered(rrPrivKey)) {
1406         rrScrPrivPtr pScrPriv = rrGetScrPriv(pScreen);
1407 
1408         pScrPriv->rrEnableSharedPixmapFlipping = msEnableSharedPixmapFlipping;
1409         pScrPriv->rrDisableSharedPixmapFlipping = msDisableSharedPixmapFlipping;
1410 
1411         pScrPriv->rrStartFlippingPixmapTracking = msStartFlippingPixmapTracking;
1412     }
1413 
1414     return ret;
1415 }
1416 
1417 static Bool
msShadowInit(ScreenPtr pScreen)1418 msShadowInit(ScreenPtr pScreen)
1419 {
1420     if (!shadowSetup(pScreen)) {
1421         return FALSE;
1422     }
1423     return TRUE;
1424 }
1425 
1426 static Bool
msSharePixmapBacking(PixmapPtr ppix,ScreenPtr screen,void ** handle)1427 msSharePixmapBacking(PixmapPtr ppix, ScreenPtr screen, void **handle)
1428 {
1429 #ifdef GLAMOR_HAS_GBM
1430     int ret;
1431     CARD16 stride;
1432     CARD32 size;
1433     ret = glamor_shareable_fd_from_pixmap(ppix->drawable.pScreen, ppix,
1434                                           &stride, &size);
1435     if (ret == -1)
1436         return FALSE;
1437 
1438     *handle = (void *)(long)(ret);
1439     return TRUE;
1440 #endif
1441     return FALSE;
1442 }
1443 
1444 static Bool
msSetSharedPixmapBacking(PixmapPtr ppix,void * fd_handle)1445 msSetSharedPixmapBacking(PixmapPtr ppix, void *fd_handle)
1446 {
1447 #ifdef GLAMOR_HAS_GBM
1448     ScreenPtr screen = ppix->drawable.pScreen;
1449     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
1450     modesettingPtr ms = modesettingPTR(scrn);
1451     Bool ret;
1452     int ihandle = (int) (long) fd_handle;
1453 
1454     if (ihandle == -1)
1455         if (!ms->drmmode.reverse_prime_offload_mode)
1456            return drmmode_SetSlaveBO(ppix, &ms->drmmode, ihandle, 0, 0);
1457 
1458     if (ms->drmmode.reverse_prime_offload_mode) {
1459         ret = glamor_back_pixmap_from_fd(ppix, ihandle,
1460                                          ppix->drawable.width,
1461                                          ppix->drawable.height,
1462                                          ppix->devKind, ppix->drawable.depth,
1463                                          ppix->drawable.bitsPerPixel);
1464     } else {
1465         int size = ppix->devKind * ppix->drawable.height;
1466         ret = drmmode_SetSlaveBO(ppix, &ms->drmmode, ihandle, ppix->devKind, size);
1467     }
1468     if (ret == FALSE)
1469         return ret;
1470 
1471     return TRUE;
1472 #else
1473     return FALSE;
1474 #endif
1475 }
1476 
1477 static Bool
msRequestSharedPixmapNotifyDamage(PixmapPtr ppix)1478 msRequestSharedPixmapNotifyDamage(PixmapPtr ppix)
1479 {
1480     ScreenPtr screen = ppix->drawable.pScreen;
1481     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
1482     modesettingPtr ms = modesettingPTR(scrn);
1483 
1484     msPixmapPrivPtr ppriv = msGetPixmapPriv(&ms->drmmode, ppix->master_pixmap);
1485 
1486     ppriv->notify_on_damage = TRUE;
1487 
1488     return TRUE;
1489 }
1490 
1491 static Bool
msSharedPixmapNotifyDamage(PixmapPtr ppix)1492 msSharedPixmapNotifyDamage(PixmapPtr ppix)
1493 {
1494     Bool ret = FALSE;
1495     int c;
1496 
1497     ScreenPtr screen = ppix->drawable.pScreen;
1498     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
1499     modesettingPtr ms = modesettingPTR(scrn);
1500     xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
1501 
1502     msPixmapPrivPtr ppriv = msGetPixmapPriv(&ms->drmmode, ppix);
1503 
1504     if (!ppriv->wait_for_damage)
1505         return ret;
1506     ppriv->wait_for_damage = FALSE;
1507 
1508     for (c = 0; c < xf86_config->num_crtc; c++) {
1509         xf86CrtcPtr crtc = xf86_config->crtc[c];
1510         drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
1511 
1512         if (!drmmode_crtc)
1513             continue;
1514         if (!(drmmode_crtc->prime_pixmap && drmmode_crtc->prime_pixmap_back))
1515             continue;
1516 
1517         // Received damage on master screen pixmap, schedule present on vblank
1518         ret |= drmmode_SharedPixmapPresentOnVBlank(ppix, crtc, &ms->drmmode);
1519     }
1520 
1521     return ret;
1522 }
1523 
1524 static Bool
SetMaster(ScrnInfoPtr pScrn)1525 SetMaster(ScrnInfoPtr pScrn)
1526 {
1527     modesettingPtr ms = modesettingPTR(pScrn);
1528     int ret;
1529 
1530 #ifdef XF86_PDEV_SERVER_FD
1531     if (ms->pEnt->location.type == BUS_PLATFORM &&
1532         (ms->pEnt->location.id.plat->flags & XF86_PDEV_SERVER_FD))
1533         return TRUE;
1534 #endif
1535 
1536     if (ms->fd_passed)
1537         return TRUE;
1538 
1539     ret = drmSetMaster(ms->fd);
1540     if (ret)
1541         xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "drmSetMaster failed: %s\n",
1542                    strerror(errno));
1543 
1544     return ret == 0;
1545 }
1546 
1547 /* When the root window is created, initialize the screen contents from
1548  * console if -background none was specified on the command line
1549  */
1550 static Bool
CreateWindow_oneshot(WindowPtr pWin)1551 CreateWindow_oneshot(WindowPtr pWin)
1552 {
1553     ScreenPtr pScreen = pWin->drawable.pScreen;
1554     ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1555     modesettingPtr ms = modesettingPTR(pScrn);
1556     Bool ret;
1557 
1558     pScreen->CreateWindow = ms->CreateWindow;
1559     ret = pScreen->CreateWindow(pWin);
1560 
1561     if (ret)
1562         drmmode_copy_fb(pScrn, &ms->drmmode);
1563     return ret;
1564 }
1565 
1566 static Bool
ScreenInit(ScreenPtr pScreen,int argc,char ** argv)1567 ScreenInit(ScreenPtr pScreen, int argc, char **argv)
1568 {
1569     ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1570     modesettingPtr ms = modesettingPTR(pScrn);
1571     VisualPtr visual;
1572 
1573     pScrn->pScreen = pScreen;
1574 
1575     if (!SetMaster(pScrn))
1576         return FALSE;
1577 
1578 #ifdef GLAMOR_HAS_GBM
1579     if (ms->drmmode.glamor)
1580         ms->drmmode.gbm = glamor_egl_get_gbm_device(pScreen);
1581 #endif
1582 
1583     /* HW dependent - FIXME */
1584     pScrn->displayWidth = pScrn->virtualX;
1585     if (!drmmode_create_initial_bos(pScrn, &ms->drmmode))
1586         return FALSE;
1587 
1588     if (ms->drmmode.shadow_enable) {
1589         ms->drmmode.shadow_fb =
1590             calloc(1,
1591                    pScrn->displayWidth * pScrn->virtualY *
1592                    ((pScrn->bitsPerPixel + 7) >> 3));
1593         if (!ms->drmmode.shadow_fb)
1594             ms->drmmode.shadow_enable = FALSE;
1595     }
1596 
1597     miClearVisualTypes();
1598 
1599     if (!miSetVisualTypes(pScrn->depth,
1600                           miGetDefaultVisualMask(pScrn->depth),
1601                           pScrn->rgbBits, pScrn->defaultVisual))
1602         return FALSE;
1603 
1604     if (!miSetPixmapDepths())
1605         return FALSE;
1606 
1607     if (!dixRegisterScreenSpecificPrivateKey
1608         (pScreen, &ms->drmmode.pixmapPrivateKeyRec, PRIVATE_PIXMAP,
1609          sizeof(msPixmapPrivRec))) {
1610         return FALSE;
1611     }
1612 
1613     pScrn->memPhysBase = 0;
1614     pScrn->fbOffset = 0;
1615 
1616     if (!fbScreenInit(pScreen, NULL,
1617                       pScrn->virtualX, pScrn->virtualY,
1618                       pScrn->xDpi, pScrn->yDpi,
1619                       pScrn->displayWidth, pScrn->bitsPerPixel))
1620         return FALSE;
1621 
1622     if (pScrn->bitsPerPixel > 8) {
1623         /* Fixup RGB ordering */
1624         visual = pScreen->visuals + pScreen->numVisuals;
1625         while (--visual >= pScreen->visuals) {
1626             if ((visual->class | DynamicClass) == DirectColor) {
1627                 visual->offsetRed = pScrn->offset.red;
1628                 visual->offsetGreen = pScrn->offset.green;
1629                 visual->offsetBlue = pScrn->offset.blue;
1630                 visual->redMask = pScrn->mask.red;
1631                 visual->greenMask = pScrn->mask.green;
1632                 visual->blueMask = pScrn->mask.blue;
1633             }
1634         }
1635     }
1636 
1637     fbPictureInit(pScreen, NULL, 0);
1638 
1639     if (drmmode_init(pScrn, &ms->drmmode) == FALSE) {
1640         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1641                    "Failed to initialize glamor at ScreenInit() time.\n");
1642         return FALSE;
1643     }
1644 
1645     if (ms->drmmode.shadow_enable && !msShadowInit(pScreen)) {
1646         xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "shadow fb init failed\n");
1647         return FALSE;
1648     }
1649 
1650     ms->createScreenResources = pScreen->CreateScreenResources;
1651     pScreen->CreateScreenResources = CreateScreenResources;
1652 
1653     xf86SetBlackWhitePixels(pScreen);
1654 
1655     xf86SetBackingStore(pScreen);
1656     xf86SetSilkenMouse(pScreen);
1657     miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
1658 
1659     /* If pageflip is enabled hook the screen's cursor-sprite (swcursor) funcs.
1660      * So that we can disabe page-flipping on fallback to a swcursor. */
1661     if (ms->drmmode.pageflip) {
1662         miPointerScreenPtr PointPriv =
1663             dixLookupPrivate(&pScreen->devPrivates, miPointerScreenKey);
1664 
1665         if (!dixRegisterScreenPrivateKey(&ms->drmmode.spritePrivateKeyRec,
1666                                          pScreen, PRIVATE_DEVICE,
1667                                          sizeof(msSpritePrivRec)))
1668             return FALSE;
1669 
1670         ms->SpriteFuncs = PointPriv->spriteFuncs;
1671         PointPriv->spriteFuncs = &drmmode_sprite_funcs;
1672     }
1673 
1674     /* Need to extend HWcursor support to handle mask interleave */
1675     if (!ms->drmmode.sw_cursor)
1676         xf86_cursors_init(pScreen, ms->cursor_width, ms->cursor_height,
1677                           HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64 |
1678                           HARDWARE_CURSOR_UPDATE_UNHIDDEN |
1679                           HARDWARE_CURSOR_ARGB);
1680 
1681     /* Must force it before EnterVT, so we are in control of VT and
1682      * later memory should be bound when allocating, e.g rotate_mem */
1683     pScrn->vtSema = TRUE;
1684 
1685     if (serverGeneration == 1 && bgNoneRoot && ms->drmmode.glamor) {
1686         ms->CreateWindow = pScreen->CreateWindow;
1687         pScreen->CreateWindow = CreateWindow_oneshot;
1688     }
1689 
1690     pScreen->SaveScreen = xf86SaveScreen;
1691     ms->CloseScreen = pScreen->CloseScreen;
1692     pScreen->CloseScreen = CloseScreen;
1693 
1694     ms->BlockHandler = pScreen->BlockHandler;
1695     pScreen->BlockHandler = msBlockHandler_oneshot;
1696 
1697     pScreen->SharePixmapBacking = msSharePixmapBacking;
1698     pScreen->SetSharedPixmapBacking = msSetSharedPixmapBacking;
1699     pScreen->StartPixmapTracking = PixmapStartDirtyTracking;
1700     pScreen->StopPixmapTracking = PixmapStopDirtyTracking;
1701 
1702     pScreen->SharedPixmapNotifyDamage = msSharedPixmapNotifyDamage;
1703     pScreen->RequestSharedPixmapNotifyDamage =
1704         msRequestSharedPixmapNotifyDamage;
1705 
1706     pScreen->PresentSharedPixmap = msPresentSharedPixmap;
1707     pScreen->StopFlippingPixmapTracking = msStopFlippingPixmapTracking;
1708 
1709     if (!xf86CrtcScreenInit(pScreen))
1710         return FALSE;
1711 
1712     if (!drmmode_setup_colormap(pScreen, pScrn))
1713         return FALSE;
1714 
1715     if (ms->atomic_modeset)
1716         xf86DPMSInit(pScreen, drmmode_set_dpms, 0);
1717     else
1718         xf86DPMSInit(pScreen, xf86DPMSSet, 0);
1719 
1720 #ifdef GLAMOR_HAS_GBM
1721     if (ms->drmmode.glamor) {
1722         XF86VideoAdaptorPtr     glamor_adaptor;
1723 
1724         glamor_adaptor = glamor_xv_init(pScreen, 16);
1725         if (glamor_adaptor != NULL)
1726             xf86XVScreenInit(pScreen, &glamor_adaptor, 1);
1727         else
1728             xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1729                        "Failed to initialize XV support.\n");
1730     }
1731 #endif
1732 
1733     if (serverGeneration == 1)
1734         xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
1735 
1736     if (!ms_vblank_screen_init(pScreen)) {
1737         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1738                    "Failed to initialize vblank support.\n");
1739         return FALSE;
1740     }
1741 
1742 #ifdef GLAMOR_HAS_GBM
1743     if (ms->drmmode.glamor) {
1744         if (!(ms->drmmode.dri2_enable = ms_dri2_screen_init(pScreen))) {
1745             xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1746                        "Failed to initialize the DRI2 extension.\n");
1747         }
1748 
1749         /* enable reverse prime if we are a GPU screen, and accelerated, and not
1750          * i915, evdi or udl. i915 is happy scanning out from sysmem.
1751          * evdi and udl are virtual drivers scanning out from sysmem
1752          * backed dumb buffers.
1753          */
1754         if (pScreen->isGPU) {
1755             drmVersionPtr version;
1756 
1757             /* enable if we are an accelerated GPU screen */
1758             ms->drmmode.reverse_prime_offload_mode = TRUE;
1759 
1760             if ((version = drmGetVersion(ms->drmmode.fd))) {
1761                 if (!strncmp("i915", version->name, version->name_len)) {
1762                     ms->drmmode.reverse_prime_offload_mode = FALSE;
1763                 }
1764                 if (!strncmp("evdi", version->name, version->name_len)) {
1765                     ms->drmmode.reverse_prime_offload_mode = FALSE;
1766                 }
1767                 if (!strncmp("udl", version->name, version->name_len)) {
1768                     ms->drmmode.reverse_prime_offload_mode = FALSE;
1769                 }
1770                 if (!ms->drmmode.reverse_prime_offload_mode) {
1771                     xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1772                        "Disable reverse prime offload mode for %s.\n", version->name);
1773                 }
1774                 drmFreeVersion(version);
1775             }
1776         }
1777     }
1778 #endif
1779     if (!(ms->drmmode.present_enable = ms_present_screen_init(pScreen))) {
1780         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1781                    "Failed to initialize the Present extension.\n");
1782     }
1783 
1784 
1785     pScrn->vtSema = TRUE;
1786 
1787     return TRUE;
1788 }
1789 
1790 static void
AdjustFrame(ScrnInfoPtr pScrn,int x,int y)1791 AdjustFrame(ScrnInfoPtr pScrn, int x, int y)
1792 {
1793     modesettingPtr ms = modesettingPTR(pScrn);
1794 
1795     drmmode_adjust_frame(pScrn, &ms->drmmode, x, y);
1796 }
1797 
1798 static void
FreeScreen(ScrnInfoPtr pScrn)1799 FreeScreen(ScrnInfoPtr pScrn)
1800 {
1801     FreeRec(pScrn);
1802 }
1803 
1804 static void
LeaveVT(ScrnInfoPtr pScrn)1805 LeaveVT(ScrnInfoPtr pScrn)
1806 {
1807     modesettingPtr ms = modesettingPTR(pScrn);
1808 
1809     xf86_hide_cursors(pScrn);
1810 
1811     pScrn->vtSema = FALSE;
1812 
1813 #ifdef XF86_PDEV_SERVER_FD
1814     if (ms->pEnt->location.type == BUS_PLATFORM &&
1815         (ms->pEnt->location.id.plat->flags & XF86_PDEV_SERVER_FD))
1816         return;
1817 #endif
1818 
1819     if (!ms->fd_passed)
1820         drmDropMaster(ms->fd);
1821 }
1822 
1823 /*
1824  * This gets called when gaining control of the VT, and from ScreenInit().
1825  */
1826 static Bool
EnterVT(ScrnInfoPtr pScrn)1827 EnterVT(ScrnInfoPtr pScrn)
1828 {
1829     modesettingPtr ms = modesettingPTR(pScrn);
1830 
1831     pScrn->vtSema = TRUE;
1832 
1833     SetMaster(pScrn);
1834 
1835     drmmode_update_kms_state(&ms->drmmode);
1836 
1837     /* allow not all modes to be set successfully since some events might have
1838      * happened while not being master that could prevent the previous
1839      * configuration from being re-applied.
1840      */
1841     if (!drmmode_set_desired_modes(pScrn, &ms->drmmode, TRUE, TRUE)) {
1842         xf86DisableUnusedFunctions(pScrn);
1843 
1844         /* TODO: check that at least one screen is on, to allow the user to fix
1845          * their setup if all modeset failed...
1846          */
1847 
1848         /* Tell the desktop environment that something changed, so that they
1849          * can hopefully correct the situation
1850          */
1851         RRSetChanged(xf86ScrnToScreen(pScrn));
1852         RRTellChanged(xf86ScrnToScreen(pScrn));
1853     }
1854 
1855     return TRUE;
1856 }
1857 
1858 static Bool
SwitchMode(ScrnInfoPtr pScrn,DisplayModePtr mode)1859 SwitchMode(ScrnInfoPtr pScrn, DisplayModePtr mode)
1860 {
1861     return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
1862 }
1863 
1864 static Bool
CloseScreen(ScreenPtr pScreen)1865 CloseScreen(ScreenPtr pScreen)
1866 {
1867     ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
1868     modesettingPtr ms = modesettingPTR(pScrn);
1869     modesettingEntPtr ms_ent = ms_ent_priv(pScrn);
1870 
1871     /* Clear mask of assigned crtc's in this generation */
1872     ms_ent->assigned_crtcs = 0;
1873 
1874 #ifdef GLAMOR_HAS_GBM
1875     if (ms->drmmode.dri2_enable) {
1876         ms_dri2_close_screen(pScreen);
1877     }
1878 #endif
1879 
1880     ms_vblank_close_screen(pScreen);
1881 
1882     if (ms->damage) {
1883         DamageUnregister(ms->damage);
1884         DamageDestroy(ms->damage);
1885         ms->damage = NULL;
1886     }
1887 
1888     if (ms->drmmode.shadow_enable) {
1889         shadowRemove(pScreen, pScreen->GetScreenPixmap(pScreen));
1890         free(ms->drmmode.shadow_fb);
1891         ms->drmmode.shadow_fb = NULL;
1892         free(ms->drmmode.shadow_fb2);
1893         ms->drmmode.shadow_fb2 = NULL;
1894     }
1895 
1896     drmmode_uevent_fini(pScrn, &ms->drmmode);
1897 
1898     drmmode_free_bos(pScrn, &ms->drmmode);
1899 
1900     if (ms->drmmode.pageflip) {
1901         miPointerScreenPtr PointPriv =
1902             dixLookupPrivate(&pScreen->devPrivates, miPointerScreenKey);
1903 
1904         if (PointPriv->spriteFuncs == &drmmode_sprite_funcs)
1905             PointPriv->spriteFuncs = ms->SpriteFuncs;
1906     }
1907 
1908     if (pScrn->vtSema) {
1909         LeaveVT(pScrn);
1910     }
1911 
1912     pScreen->CreateScreenResources = ms->createScreenResources;
1913     pScreen->BlockHandler = ms->BlockHandler;
1914 
1915     pScrn->vtSema = FALSE;
1916     pScreen->CloseScreen = ms->CloseScreen;
1917     return (*pScreen->CloseScreen) (pScreen);
1918 }
1919 
1920 static ModeStatus
ValidMode(ScrnInfoPtr arg,DisplayModePtr mode,Bool verbose,int flags)1921 ValidMode(ScrnInfoPtr arg, DisplayModePtr mode, Bool verbose, int flags)
1922 {
1923     return MODE_OK;
1924 }
1925