135c4bbdfSmrg/*
235c4bbdfSmrg * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
335c4bbdfSmrg * Copyright 2011 Dave Airlie
45a112b11Smrg * Copyright 2019 NVIDIA CORPORATION
535c4bbdfSmrg * All Rights Reserved.
635c4bbdfSmrg *
735c4bbdfSmrg * Permission is hereby granted, free of charge, to any person obtaining a
835c4bbdfSmrg * copy of this software and associated documentation files (the
935c4bbdfSmrg * "Software"), to deal in the Software without restriction, including
1035c4bbdfSmrg * without limitation the rights to use, copy, modify, merge, publish,
1135c4bbdfSmrg * distribute, sub license, and/or sell copies of the Software, and to
1235c4bbdfSmrg * permit persons to whom the Software is furnished to do so, subject to
1335c4bbdfSmrg * the following conditions:
1435c4bbdfSmrg *
1535c4bbdfSmrg * The above copyright notice and this permission notice (including the
1635c4bbdfSmrg * next paragraph) shall be included in all copies or substantial portions
1735c4bbdfSmrg * of the Software.
1835c4bbdfSmrg *
1935c4bbdfSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
2035c4bbdfSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2135c4bbdfSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
2235c4bbdfSmrg * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
2335c4bbdfSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2435c4bbdfSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2535c4bbdfSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2635c4bbdfSmrg *
2735c4bbdfSmrg *
2835c4bbdfSmrg * Original Author: Alan Hourihane <alanh@tungstengraphics.com>
2935c4bbdfSmrg * Rewrite: Dave Airlie <airlied@redhat.com>
305a112b11Smrg * Additional contributors:
315a112b11Smrg *   Aaron Plattner <aplattner@nvidia.com>
3235c4bbdfSmrg *
3335c4bbdfSmrg */
3435c4bbdfSmrg
3535c4bbdfSmrg#ifdef HAVE_DIX_CONFIG_H
3635c4bbdfSmrg#include "dix-config.h"
3735c4bbdfSmrg#endif
3835c4bbdfSmrg
3935c4bbdfSmrg#include <unistd.h>
4035c4bbdfSmrg#include <fcntl.h>
4135c4bbdfSmrg#include "xf86.h"
427e31ba66Smrg#include "xf86Priv.h"
4335c4bbdfSmrg#include "xf86_OSproc.h"
4435c4bbdfSmrg#include "compiler.h"
4535c4bbdfSmrg#include "xf86Pci.h"
4635c4bbdfSmrg#include "mipointer.h"
47806e81e9Smrg#include "mipointrst.h"
4835c4bbdfSmrg#include "micmap.h"
4935c4bbdfSmrg#include <X11/extensions/randr.h>
5035c4bbdfSmrg#include "fb.h"
5135c4bbdfSmrg#include "edid.h"
5235c4bbdfSmrg#include "xf86i2c.h"
5335c4bbdfSmrg#include "xf86Crtc.h"
5435c4bbdfSmrg#include "miscstruct.h"
5535c4bbdfSmrg#include "dixstruct.h"
5635c4bbdfSmrg#include "xf86xv.h"
5735c4bbdfSmrg#include <X11/extensions/Xv.h>
5835c4bbdfSmrg#include <xorg-config.h>
5935c4bbdfSmrg#ifdef XSERVER_PLATFORM_BUS
6035c4bbdfSmrg#include "xf86platformBus.h"
6135c4bbdfSmrg#endif
627e31ba66Smrg#ifdef XSERVER_LIBPCIACCESS
6335c4bbdfSmrg#include <pciaccess.h>
6435c4bbdfSmrg#endif
6535c4bbdfSmrg#include "driver.h"
6635c4bbdfSmrg
6735c4bbdfSmrgstatic void AdjustFrame(ScrnInfoPtr pScrn, int x, int y);
6835c4bbdfSmrgstatic Bool CloseScreen(ScreenPtr pScreen);
6935c4bbdfSmrgstatic Bool EnterVT(ScrnInfoPtr pScrn);
7035c4bbdfSmrgstatic void Identify(int flags);
7135c4bbdfSmrgstatic const OptionInfoRec *AvailableOptions(int chipid, int busid);
7235c4bbdfSmrgstatic ModeStatus ValidMode(ScrnInfoPtr pScrn, DisplayModePtr mode,
7335c4bbdfSmrg                            Bool verbose, int flags);
7435c4bbdfSmrgstatic void FreeScreen(ScrnInfoPtr pScrn);
7535c4bbdfSmrgstatic void LeaveVT(ScrnInfoPtr pScrn);
7635c4bbdfSmrgstatic Bool SwitchMode(ScrnInfoPtr pScrn, DisplayModePtr mode);
7735c4bbdfSmrgstatic Bool ScreenInit(ScreenPtr pScreen, int argc, char **argv);
7835c4bbdfSmrgstatic Bool PreInit(ScrnInfoPtr pScrn, int flags);
7935c4bbdfSmrg
8035c4bbdfSmrgstatic Bool Probe(DriverPtr drv, int flags);
8135c4bbdfSmrgstatic Bool ms_pci_probe(DriverPtr driver,
8235c4bbdfSmrg                         int entity_num, struct pci_device *device,
8335c4bbdfSmrg                         intptr_t match_data);
8435c4bbdfSmrgstatic Bool ms_driver_func(ScrnInfoPtr scrn, xorgDriverFuncOp op, void *data);
8535c4bbdfSmrg
865a112b11Smrg/* window wrapper functions used to get the notification when
875a112b11Smrg * the window property changes */
885a112b11Smrgstatic Atom vrr_atom;
895a112b11Smrgstatic Bool property_vectors_wrapped;
905a112b11Smrgstatic Bool restore_property_vector;
915a112b11Smrgstatic int (*saved_change_property) (ClientPtr client);
925a112b11Smrgstatic int (*saved_delete_property) (ClientPtr client);
935a112b11Smrg
9435c4bbdfSmrg#ifdef XSERVER_LIBPCIACCESS
9535c4bbdfSmrgstatic const struct pci_id_match ms_device_match[] = {
9635c4bbdfSmrg    {
9735c4bbdfSmrg     PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY,
9835c4bbdfSmrg     0x00030000, 0x00ff0000, 0},
9935c4bbdfSmrg
10035c4bbdfSmrg    {0, 0, 0},
10135c4bbdfSmrg};
10235c4bbdfSmrg#endif
10335c4bbdfSmrg
10435c4bbdfSmrg#ifndef XSERVER_PLATFORM_BUS
10535c4bbdfSmrgstruct xf86_platform_device;
10635c4bbdfSmrg#endif
10735c4bbdfSmrg
10835c4bbdfSmrg#ifdef XSERVER_PLATFORM_BUS
10935c4bbdfSmrgstatic Bool ms_platform_probe(DriverPtr driver,
11035c4bbdfSmrg                              int entity_num, int flags,
11135c4bbdfSmrg                              struct xf86_platform_device *device,
11235c4bbdfSmrg                              intptr_t match_data);
11335c4bbdfSmrg#endif
11435c4bbdfSmrg
11535c4bbdfSmrg_X_EXPORT DriverRec modesetting = {
11635c4bbdfSmrg    1,
11735c4bbdfSmrg    "modesetting",
11835c4bbdfSmrg    Identify,
11935c4bbdfSmrg    Probe,
12035c4bbdfSmrg    AvailableOptions,
12135c4bbdfSmrg    NULL,
12235c4bbdfSmrg    0,
12335c4bbdfSmrg    ms_driver_func,
12435c4bbdfSmrg    ms_device_match,
12535c4bbdfSmrg    ms_pci_probe,
12635c4bbdfSmrg#ifdef XSERVER_PLATFORM_BUS
12735c4bbdfSmrg    ms_platform_probe,
12835c4bbdfSmrg#endif
12935c4bbdfSmrg};
13035c4bbdfSmrg
13135c4bbdfSmrgstatic SymTabRec Chipsets[] = {
13235c4bbdfSmrg    {0, "kms"},
13335c4bbdfSmrg    {-1, NULL}
13435c4bbdfSmrg};
13535c4bbdfSmrg
13635c4bbdfSmrgstatic const OptionInfoRec Options[] = {
13735c4bbdfSmrg    {OPTION_SW_CURSOR, "SWcursor", OPTV_BOOLEAN, {0}, FALSE},
13835c4bbdfSmrg    {OPTION_DEVICE_PATH, "kmsdev", OPTV_STRING, {0}, FALSE},
13935c4bbdfSmrg    {OPTION_SHADOW_FB, "ShadowFB", OPTV_BOOLEAN, {0}, FALSE},
14035c4bbdfSmrg    {OPTION_ACCEL_METHOD, "AccelMethod", OPTV_STRING, {0}, FALSE},
14135c4bbdfSmrg    {OPTION_PAGEFLIP, "PageFlip", OPTV_BOOLEAN, {0}, FALSE},
14235c4bbdfSmrg    {OPTION_ZAPHOD_HEADS, "ZaphodHeads", OPTV_STRING, {0}, FALSE},
1437e31ba66Smrg    {OPTION_DOUBLE_SHADOW, "DoubleShadow", OPTV_BOOLEAN, {0}, FALSE},
144806e81e9Smrg    {OPTION_ATOMIC, "Atomic", OPTV_BOOLEAN, {0}, FALSE},
1455a112b11Smrg    {OPTION_VARIABLE_REFRESH, "VariableRefresh", OPTV_BOOLEAN, {0}, FALSE},
1465a112b11Smrg    {OPTION_USE_GAMMA_LUT, "UseGammaLUT", OPTV_BOOLEAN, {0}, FALSE},
1475a112b11Smrg    {OPTION_ASYNC_FLIP_SECONDARIES, "AsyncFlipSecondaries", OPTV_BOOLEAN, {0}, FALSE},
14835c4bbdfSmrg    {-1, NULL, OPTV_NONE, {0}, FALSE}
14935c4bbdfSmrg};
15035c4bbdfSmrg
15135c4bbdfSmrgint ms_entity_index = -1;
15235c4bbdfSmrg
15335c4bbdfSmrgstatic MODULESETUPPROTO(Setup);
15435c4bbdfSmrg
15535c4bbdfSmrgstatic XF86ModuleVersionInfo VersRec = {
15635c4bbdfSmrg    "modesetting",
15735c4bbdfSmrg    MODULEVENDORSTRING,
15835c4bbdfSmrg    MODINFOSTRING1,
15935c4bbdfSmrg    MODINFOSTRING2,
16035c4bbdfSmrg    XORG_VERSION_CURRENT,
16135c4bbdfSmrg    XORG_VERSION_MAJOR,
16235c4bbdfSmrg    XORG_VERSION_MINOR,
16335c4bbdfSmrg    XORG_VERSION_PATCH,
16435c4bbdfSmrg    ABI_CLASS_VIDEODRV,
16535c4bbdfSmrg    ABI_VIDEODRV_VERSION,
16635c4bbdfSmrg    MOD_CLASS_VIDEODRV,
16735c4bbdfSmrg    {0, 0, 0, 0}
16835c4bbdfSmrg};
16935c4bbdfSmrg
17035c4bbdfSmrg_X_EXPORT XF86ModuleData modesettingModuleData = { &VersRec, Setup, NULL };
17135c4bbdfSmrg
17235c4bbdfSmrgstatic void *
17335c4bbdfSmrgSetup(void *module, void *opts, int *errmaj, int *errmin)
17435c4bbdfSmrg{
17535c4bbdfSmrg    static Bool setupDone = 0;
17635c4bbdfSmrg
17735c4bbdfSmrg    /* This module should be loaded only once, but check to be sure.
17835c4bbdfSmrg     */
17935c4bbdfSmrg    if (!setupDone) {
18035c4bbdfSmrg        setupDone = 1;
18135c4bbdfSmrg        xf86AddDriver(&modesetting, module, HaveDriverFuncs);
18235c4bbdfSmrg
18335c4bbdfSmrg        /*
18435c4bbdfSmrg         * The return value must be non-NULL on success even though there
18535c4bbdfSmrg         * is no TearDownProc.
18635c4bbdfSmrg         */
18735c4bbdfSmrg        return (void *) 1;
18835c4bbdfSmrg    }
18935c4bbdfSmrg    else {
19035c4bbdfSmrg        if (errmaj)
19135c4bbdfSmrg            *errmaj = LDR_ONCEONLY;
19235c4bbdfSmrg        return NULL;
19335c4bbdfSmrg    }
19435c4bbdfSmrg}
19535c4bbdfSmrg
19635c4bbdfSmrgstatic void
19735c4bbdfSmrgIdentify(int flags)
19835c4bbdfSmrg{
19935c4bbdfSmrg    xf86PrintChipsets("modesetting", "Driver for Modesetting Kernel Drivers",
20035c4bbdfSmrg                      Chipsets);
20135c4bbdfSmrg}
20235c4bbdfSmrg
20335c4bbdfSmrgmodesettingEntPtr ms_ent_priv(ScrnInfoPtr scrn)
20435c4bbdfSmrg{
20535c4bbdfSmrg    DevUnion     *pPriv;
20635c4bbdfSmrg    modesettingPtr ms = modesettingPTR(scrn);
20735c4bbdfSmrg    pPriv = xf86GetEntityPrivate(ms->pEnt->index,
20835c4bbdfSmrg                                 ms_entity_index);
20935c4bbdfSmrg    return pPriv->ptr;
21035c4bbdfSmrg}
21135c4bbdfSmrg
2127e31ba66Smrgstatic int
2137e31ba66Smrgget_passed_fd(void)
2147e31ba66Smrg{
2157e31ba66Smrg    if (xf86DRMMasterFd >= 0) {
2167e31ba66Smrg        xf86DrvMsg(-1, X_INFO, "Using passed DRM master file descriptor %d\n", xf86DRMMasterFd);
2177e31ba66Smrg        return dup(xf86DRMMasterFd);
2187e31ba66Smrg    }
2197e31ba66Smrg    return -1;
2207e31ba66Smrg}
2217e31ba66Smrg
22235c4bbdfSmrgstatic int
22335c4bbdfSmrgopen_hw(const char *dev)
22435c4bbdfSmrg{
22535c4bbdfSmrg    int fd;
22635c4bbdfSmrg
2277e31ba66Smrg    if ((fd = get_passed_fd()) != -1)
2287e31ba66Smrg        return fd;
2297e31ba66Smrg
23035c4bbdfSmrg    if (dev)
2317e31ba66Smrg        fd = open(dev, O_RDWR | O_CLOEXEC, 0);
23235c4bbdfSmrg    else {
23335c4bbdfSmrg        dev = getenv("KMSDEVICE");
2347e31ba66Smrg        if ((NULL == dev) || ((fd = open(dev, O_RDWR | O_CLOEXEC, 0)) == -1)) {
23535c4bbdfSmrg            dev = "/dev/dri/card0";
2367e31ba66Smrg            fd = open(dev, O_RDWR | O_CLOEXEC, 0);
23735c4bbdfSmrg        }
23835c4bbdfSmrg    }
23935c4bbdfSmrg    if (fd == -1)
24035c4bbdfSmrg        xf86DrvMsg(-1, X_ERROR, "open %s: %s\n", dev, strerror(errno));
24135c4bbdfSmrg
24235c4bbdfSmrg    return fd;
24335c4bbdfSmrg}
24435c4bbdfSmrg
24535c4bbdfSmrgstatic int
2467e31ba66Smrgcheck_outputs(int fd, int *count)
24735c4bbdfSmrg{
24835c4bbdfSmrg    drmModeResPtr res = drmModeGetResources(fd);
24935c4bbdfSmrg    int ret;
25035c4bbdfSmrg
25135c4bbdfSmrg    if (!res)
25235c4bbdfSmrg        return FALSE;
2537e31ba66Smrg
2547e31ba66Smrg    if (count)
2557e31ba66Smrg        *count = res->count_connectors;
2567e31ba66Smrg
25735c4bbdfSmrg    ret = res->count_connectors > 0;
2587e31ba66Smrg#if defined(GLAMOR_HAS_GBM_LINEAR)
2597e31ba66Smrg    if (ret == FALSE) {
2607e31ba66Smrg        uint64_t value = 0;
2617e31ba66Smrg        if (drmGetCap(fd, DRM_CAP_PRIME, &value) == 0 &&
2627e31ba66Smrg                (value & DRM_PRIME_CAP_EXPORT))
2637e31ba66Smrg            ret = TRUE;
2647e31ba66Smrg    }
2657e31ba66Smrg#endif
26635c4bbdfSmrg    drmModeFreeResources(res);
26735c4bbdfSmrg    return ret;
26835c4bbdfSmrg}
26935c4bbdfSmrg
27035c4bbdfSmrgstatic Bool
27135c4bbdfSmrgprobe_hw(const char *dev, struct xf86_platform_device *platform_dev)
27235c4bbdfSmrg{
27335c4bbdfSmrg    int fd;
27435c4bbdfSmrg
2757e31ba66Smrg#ifdef XF86_PDEV_SERVER_FD
27635c4bbdfSmrg    if (platform_dev && (platform_dev->flags & XF86_PDEV_SERVER_FD)) {
27735c4bbdfSmrg        fd = xf86_platform_device_odev_attributes(platform_dev)->fd;
27835c4bbdfSmrg        if (fd == -1)
27935c4bbdfSmrg            return FALSE;
2807e31ba66Smrg        return check_outputs(fd, NULL);
28135c4bbdfSmrg    }
28235c4bbdfSmrg#endif
28335c4bbdfSmrg
28435c4bbdfSmrg    fd = open_hw(dev);
28535c4bbdfSmrg    if (fd != -1) {
2867e31ba66Smrg        int ret = check_outputs(fd, NULL);
28735c4bbdfSmrg
28835c4bbdfSmrg        close(fd);
28935c4bbdfSmrg        return ret;
29035c4bbdfSmrg    }
29135c4bbdfSmrg    return FALSE;
29235c4bbdfSmrg}
29335c4bbdfSmrg
29435c4bbdfSmrgstatic char *
29535c4bbdfSmrgms_DRICreatePCIBusID(const struct pci_device *dev)
29635c4bbdfSmrg{
29735c4bbdfSmrg    char *busID;
29835c4bbdfSmrg
29935c4bbdfSmrg    if (asprintf(&busID, "pci:%04x:%02x:%02x.%d",
30035c4bbdfSmrg                 dev->domain, dev->bus, dev->dev, dev->func) == -1)
30135c4bbdfSmrg        return NULL;
30235c4bbdfSmrg
30335c4bbdfSmrg    return busID;
30435c4bbdfSmrg}
30535c4bbdfSmrg
30635c4bbdfSmrgstatic Bool
30735c4bbdfSmrgprobe_hw_pci(const char *dev, struct pci_device *pdev)
30835c4bbdfSmrg{
30935c4bbdfSmrg    int ret = FALSE, fd = open_hw(dev);
31035c4bbdfSmrg    char *id, *devid;
31135c4bbdfSmrg    drmSetVersion sv;
31235c4bbdfSmrg
31335c4bbdfSmrg    if (fd == -1)
31435c4bbdfSmrg        return FALSE;
31535c4bbdfSmrg
31635c4bbdfSmrg    sv.drm_di_major = 1;
31735c4bbdfSmrg    sv.drm_di_minor = 4;
31835c4bbdfSmrg    sv.drm_dd_major = -1;
31935c4bbdfSmrg    sv.drm_dd_minor = -1;
32035c4bbdfSmrg    if (drmSetInterfaceVersion(fd, &sv)) {
32135c4bbdfSmrg        close(fd);
32235c4bbdfSmrg        return FALSE;
32335c4bbdfSmrg    }
32435c4bbdfSmrg
32535c4bbdfSmrg    id = drmGetBusid(fd);
32635c4bbdfSmrg    devid = ms_DRICreatePCIBusID(pdev);
32735c4bbdfSmrg
32835c4bbdfSmrg    if (id && devid && !strcmp(id, devid))
3297e31ba66Smrg        ret = check_outputs(fd, NULL);
33035c4bbdfSmrg
33135c4bbdfSmrg    close(fd);
33235c4bbdfSmrg    free(id);
33335c4bbdfSmrg    free(devid);
33435c4bbdfSmrg    return ret;
33535c4bbdfSmrg}
33635c4bbdfSmrg
33735c4bbdfSmrgstatic const OptionInfoRec *
33835c4bbdfSmrgAvailableOptions(int chipid, int busid)
33935c4bbdfSmrg{
34035c4bbdfSmrg    return Options;
34135c4bbdfSmrg}
34235c4bbdfSmrg
34335c4bbdfSmrgstatic Bool
34435c4bbdfSmrgms_driver_func(ScrnInfoPtr scrn, xorgDriverFuncOp op, void *data)
34535c4bbdfSmrg{
34635c4bbdfSmrg    xorgHWFlags *flag;
34735c4bbdfSmrg
34835c4bbdfSmrg    switch (op) {
34935c4bbdfSmrg    case GET_REQUIRED_HW_INTERFACES:
35035c4bbdfSmrg        flag = (CARD32 *) data;
35135c4bbdfSmrg        (*flag) = 0;
35235c4bbdfSmrg        return TRUE;
35335c4bbdfSmrg    case SUPPORTS_SERVER_FDS:
35435c4bbdfSmrg        return TRUE;
35535c4bbdfSmrg    default:
35635c4bbdfSmrg        return FALSE;
35735c4bbdfSmrg    }
35835c4bbdfSmrg}
35935c4bbdfSmrg
36035c4bbdfSmrgstatic void
36135c4bbdfSmrgms_setup_scrn_hooks(ScrnInfoPtr scrn)
36235c4bbdfSmrg{
36335c4bbdfSmrg    scrn->driverVersion = 1;
36435c4bbdfSmrg    scrn->driverName = "modesetting";
36535c4bbdfSmrg    scrn->name = "modeset";
36635c4bbdfSmrg
36735c4bbdfSmrg    scrn->Probe = NULL;
36835c4bbdfSmrg    scrn->PreInit = PreInit;
36935c4bbdfSmrg    scrn->ScreenInit = ScreenInit;
37035c4bbdfSmrg    scrn->SwitchMode = SwitchMode;
37135c4bbdfSmrg    scrn->AdjustFrame = AdjustFrame;
37235c4bbdfSmrg    scrn->EnterVT = EnterVT;
37335c4bbdfSmrg    scrn->LeaveVT = LeaveVT;
37435c4bbdfSmrg    scrn->FreeScreen = FreeScreen;
37535c4bbdfSmrg    scrn->ValidMode = ValidMode;
37635c4bbdfSmrg}
37735c4bbdfSmrg
37835c4bbdfSmrgstatic void
37935c4bbdfSmrgms_setup_entity(ScrnInfoPtr scrn, int entity_num)
38035c4bbdfSmrg{
38135c4bbdfSmrg    DevUnion *pPriv;
38235c4bbdfSmrg
38335c4bbdfSmrg    xf86SetEntitySharable(entity_num);
38435c4bbdfSmrg
38535c4bbdfSmrg    if (ms_entity_index == -1)
38635c4bbdfSmrg        ms_entity_index = xf86AllocateEntityPrivateIndex();
38735c4bbdfSmrg
38835c4bbdfSmrg    pPriv = xf86GetEntityPrivate(entity_num,
38935c4bbdfSmrg                                 ms_entity_index);
39035c4bbdfSmrg
39135c4bbdfSmrg    xf86SetEntityInstanceForScreen(scrn, entity_num, xf86GetNumEntityInstances(entity_num) - 1);
39235c4bbdfSmrg
39335c4bbdfSmrg    if (!pPriv->ptr)
39435c4bbdfSmrg        pPriv->ptr = xnfcalloc(sizeof(modesettingEntRec), 1);
39535c4bbdfSmrg}
39635c4bbdfSmrg
3977e31ba66Smrg#ifdef XSERVER_LIBPCIACCESS
39835c4bbdfSmrgstatic Bool
39935c4bbdfSmrgms_pci_probe(DriverPtr driver,
40035c4bbdfSmrg             int entity_num, struct pci_device *dev, intptr_t match_data)
40135c4bbdfSmrg{
40235c4bbdfSmrg    ScrnInfoPtr scrn = NULL;
40335c4bbdfSmrg
40435c4bbdfSmrg    scrn = xf86ConfigPciEntity(scrn, 0, entity_num, NULL,
40535c4bbdfSmrg                               NULL, NULL, NULL, NULL, NULL);
40635c4bbdfSmrg    if (scrn) {
40735c4bbdfSmrg        const char *devpath;
40835c4bbdfSmrg        GDevPtr devSection = xf86GetDevFromEntity(scrn->entityList[0],
40935c4bbdfSmrg                                                  scrn->entityInstanceList[0]);
41035c4bbdfSmrg
41135c4bbdfSmrg        devpath = xf86FindOptionValue(devSection->options, "kmsdev");
41235c4bbdfSmrg        if (probe_hw_pci(devpath, dev)) {
41335c4bbdfSmrg            ms_setup_scrn_hooks(scrn);
41435c4bbdfSmrg
41535c4bbdfSmrg            xf86DrvMsg(scrn->scrnIndex, X_CONFIG,
41635c4bbdfSmrg                       "claimed PCI slot %d@%d:%d:%d\n",
41735c4bbdfSmrg                       dev->bus, dev->domain, dev->dev, dev->func);
41835c4bbdfSmrg            xf86DrvMsg(scrn->scrnIndex, X_INFO,
41935c4bbdfSmrg                       "using %s\n", devpath ? devpath : "default device");
42035c4bbdfSmrg
42135c4bbdfSmrg            ms_setup_entity(scrn, entity_num);
42235c4bbdfSmrg        }
42335c4bbdfSmrg        else
42435c4bbdfSmrg            scrn = NULL;
42535c4bbdfSmrg    }
42635c4bbdfSmrg    return scrn != NULL;
42735c4bbdfSmrg}
42835c4bbdfSmrg#endif
42935c4bbdfSmrg
43035c4bbdfSmrg#ifdef XSERVER_PLATFORM_BUS
43135c4bbdfSmrgstatic Bool
43235c4bbdfSmrgms_platform_probe(DriverPtr driver,
43335c4bbdfSmrg                  int entity_num, int flags, struct xf86_platform_device *dev,
43435c4bbdfSmrg                  intptr_t match_data)
43535c4bbdfSmrg{
43635c4bbdfSmrg    ScrnInfoPtr scrn = NULL;
43735c4bbdfSmrg    const char *path = xf86_platform_device_odev_attributes(dev)->path;
43835c4bbdfSmrg    int scr_flags = 0;
43935c4bbdfSmrg
44035c4bbdfSmrg    if (flags & PLATFORM_PROBE_GPU_SCREEN)
44135c4bbdfSmrg        scr_flags = XF86_ALLOCATE_GPU_SCREEN;
44235c4bbdfSmrg
44335c4bbdfSmrg    if (probe_hw(path, dev)) {
44435c4bbdfSmrg        scrn = xf86AllocateScreen(driver, scr_flags);
44535c4bbdfSmrg        if (xf86IsEntitySharable(entity_num))
44635c4bbdfSmrg            xf86SetEntityShared(entity_num);
44735c4bbdfSmrg        xf86AddEntityToScreen(scrn, entity_num);
44835c4bbdfSmrg
44935c4bbdfSmrg        ms_setup_scrn_hooks(scrn);
45035c4bbdfSmrg
45135c4bbdfSmrg        xf86DrvMsg(scrn->scrnIndex, X_INFO,
45235c4bbdfSmrg                   "using drv %s\n", path ? path : "default device");
45335c4bbdfSmrg
45435c4bbdfSmrg        ms_setup_entity(scrn, entity_num);
45535c4bbdfSmrg    }
45635c4bbdfSmrg
45735c4bbdfSmrg    return scrn != NULL;
45835c4bbdfSmrg}
45935c4bbdfSmrg#endif
46035c4bbdfSmrg
46135c4bbdfSmrgstatic Bool
46235c4bbdfSmrgProbe(DriverPtr drv, int flags)
46335c4bbdfSmrg{
46435c4bbdfSmrg    int i, numDevSections;
46535c4bbdfSmrg    GDevPtr *devSections;
46635c4bbdfSmrg    Bool foundScreen = FALSE;
46735c4bbdfSmrg    const char *dev;
46835c4bbdfSmrg    ScrnInfoPtr scrn = NULL;
46935c4bbdfSmrg
47035c4bbdfSmrg    /* For now, just bail out for PROBE_DETECT. */
47135c4bbdfSmrg    if (flags & PROBE_DETECT)
47235c4bbdfSmrg        return FALSE;
47335c4bbdfSmrg
47435c4bbdfSmrg    /*
47535c4bbdfSmrg     * Find the config file Device sections that match this
47635c4bbdfSmrg     * driver, and return if there are none.
47735c4bbdfSmrg     */
47835c4bbdfSmrg    if ((numDevSections = xf86MatchDevice("modesetting", &devSections)) <= 0) {
47935c4bbdfSmrg        return FALSE;
48035c4bbdfSmrg    }
48135c4bbdfSmrg
48235c4bbdfSmrg    for (i = 0; i < numDevSections; i++) {
48335c4bbdfSmrg        int entity_num;
48435c4bbdfSmrg        dev = xf86FindOptionValue(devSections[i]->options, "kmsdev");
48535c4bbdfSmrg        if (probe_hw(dev, NULL)) {
48635c4bbdfSmrg
48735c4bbdfSmrg            entity_num = xf86ClaimFbSlot(drv, 0, devSections[i], TRUE);
48835c4bbdfSmrg            scrn = xf86ConfigFbEntity(scrn, 0, entity_num, NULL, NULL, NULL, NULL);
48935c4bbdfSmrg        }
49035c4bbdfSmrg
49135c4bbdfSmrg        if (scrn) {
49235c4bbdfSmrg            foundScreen = TRUE;
49335c4bbdfSmrg            ms_setup_scrn_hooks(scrn);
49435c4bbdfSmrg            scrn->Probe = Probe;
49535c4bbdfSmrg
49635c4bbdfSmrg            xf86DrvMsg(scrn->scrnIndex, X_INFO,
49735c4bbdfSmrg                       "using %s\n", dev ? dev : "default device");
49835c4bbdfSmrg            ms_setup_entity(scrn, entity_num);
49935c4bbdfSmrg        }
50035c4bbdfSmrg    }
50135c4bbdfSmrg
50235c4bbdfSmrg    free(devSections);
50335c4bbdfSmrg
50435c4bbdfSmrg    return foundScreen;
50535c4bbdfSmrg}
50635c4bbdfSmrg
50735c4bbdfSmrgstatic Bool
50835c4bbdfSmrgGetRec(ScrnInfoPtr pScrn)
50935c4bbdfSmrg{
51035c4bbdfSmrg    if (pScrn->driverPrivate)
51135c4bbdfSmrg        return TRUE;
51235c4bbdfSmrg
51335c4bbdfSmrg    pScrn->driverPrivate = xnfcalloc(sizeof(modesettingRec), 1);
51435c4bbdfSmrg
51535c4bbdfSmrg    return TRUE;
51635c4bbdfSmrg}
51735c4bbdfSmrg
518d566a54bSmrgstatic void
519d566a54bSmrgrotate_clip(PixmapPtr pixmap, BoxPtr rect, drmModeClip *clip, Rotation rotation)
520d566a54bSmrg{
521d566a54bSmrg    int w = pixmap->drawable.width;
522d566a54bSmrg    int h = pixmap->drawable.height;
523d566a54bSmrg
524d566a54bSmrg    if (rotation == RR_Rotate_90) {
525d566a54bSmrg	/* Rotate 90 degrees counter clockwise */
526d566a54bSmrg        clip->x1 = rect->y1;
527d566a54bSmrg	clip->x2 = rect->y2;
528d566a54bSmrg	clip->y1 = w - rect->x2;
529d566a54bSmrg	clip->y2 = w - rect->x1;
530d566a54bSmrg    } else if (rotation == RR_Rotate_180) {
531d566a54bSmrg	/* Rotate 180 degrees */
532d566a54bSmrg        clip->x1 = w - rect->x2;
533d566a54bSmrg	clip->x2 = w - rect->x1;
534d566a54bSmrg	clip->y1 = h - rect->y2;
535d566a54bSmrg	clip->y2 = h - rect->y1;
536d566a54bSmrg    } else if (rotation == RR_Rotate_270) {
537d566a54bSmrg	/* Rotate 90 degrees clockwise */
538d566a54bSmrg        clip->x1 = h - rect->y2;
539d566a54bSmrg	clip->x2 = h - rect->y1;
540d566a54bSmrg	clip->y1 = rect->x1;
541d566a54bSmrg	clip->y2 = rect->x2;
542d566a54bSmrg    } else {
543d566a54bSmrg	clip->x1 = rect->x1;
544d566a54bSmrg	clip->x2 = rect->x2;
545d566a54bSmrg	clip->y1 = rect->y1;
546d566a54bSmrg	clip->y2 = rect->y2;
547d566a54bSmrg    }
548d566a54bSmrg}
549d566a54bSmrg
55035c4bbdfSmrgstatic int
551d566a54bSmrgdispatch_dirty_region(ScrnInfoPtr scrn, xf86CrtcPtr crtc,
552d566a54bSmrg		      PixmapPtr pixmap, DamagePtr damage, int fb_id)
55335c4bbdfSmrg{
55435c4bbdfSmrg    modesettingPtr ms = modesettingPTR(scrn);
55535c4bbdfSmrg    RegionPtr dirty = DamageRegion(damage);
55635c4bbdfSmrg    unsigned num_cliprects = REGION_NUM_RECTS(dirty);
55735c4bbdfSmrg    int ret = 0;
55835c4bbdfSmrg
55935c4bbdfSmrg    if (num_cliprects) {
56035c4bbdfSmrg        drmModeClip *clip = xallocarray(num_cliprects, sizeof(drmModeClip));
56135c4bbdfSmrg        BoxPtr rect = REGION_RECTS(dirty);
56235c4bbdfSmrg        int i;
56335c4bbdfSmrg
56435c4bbdfSmrg        if (!clip)
56535c4bbdfSmrg            return -ENOMEM;
56635c4bbdfSmrg
567d566a54bSmrg        /* Rotate and copy rects into clips */
568d566a54bSmrg        for (i = 0; i < num_cliprects; i++, rect++)
569d566a54bSmrg	    rotate_clip(pixmap, rect, &clip[i], crtc->rotation);
57035c4bbdfSmrg
57135c4bbdfSmrg        /* TODO query connector property to see if this is needed */
57235c4bbdfSmrg        ret = drmModeDirtyFB(ms->fd, fb_id, clip, num_cliprects);
57335c4bbdfSmrg
57435c4bbdfSmrg        /* if we're swamping it with work, try one at a time */
57535c4bbdfSmrg        if (ret == -EINVAL) {
57635c4bbdfSmrg            for (i = 0; i < num_cliprects; i++) {
57735c4bbdfSmrg                if ((ret = drmModeDirtyFB(ms->fd, fb_id, &clip[i], 1)) < 0)
57835c4bbdfSmrg                    break;
57935c4bbdfSmrg            }
58035c4bbdfSmrg        }
58135c4bbdfSmrg
58235c4bbdfSmrg        free(clip);
58335c4bbdfSmrg        DamageEmpty(damage);
58435c4bbdfSmrg    }
58535c4bbdfSmrg    return ret;
58635c4bbdfSmrg}
58735c4bbdfSmrg
58835c4bbdfSmrgstatic void
58935c4bbdfSmrgdispatch_dirty(ScreenPtr pScreen)
59035c4bbdfSmrg{
59135c4bbdfSmrg    ScrnInfoPtr scrn = xf86ScreenToScrn(pScreen);
592d566a54bSmrg    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
59335c4bbdfSmrg    modesettingPtr ms = modesettingPTR(scrn);
59435c4bbdfSmrg    PixmapPtr pixmap = pScreen->GetScreenPixmap(pScreen);
595d566a54bSmrg    uint32_t fb_id;
596d566a54bSmrg    int ret, c, x, y ;
59735c4bbdfSmrg
598d566a54bSmrg    for (c = 0; c < xf86_config->num_crtc; c++) {
599d566a54bSmrg        xf86CrtcPtr crtc = xf86_config->crtc[c];
600d566a54bSmrg        drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
601d566a54bSmrg
602d566a54bSmrg        if (!drmmode_crtc)
603d566a54bSmrg            continue;
604d566a54bSmrg
605d566a54bSmrg	drmmode_crtc_get_fb_id(crtc, &fb_id, &x, &y);
606d566a54bSmrg
607d566a54bSmrg        ret = dispatch_dirty_region(scrn, crtc, pixmap, ms->damage, fb_id);
608d566a54bSmrg        if (ret == -EINVAL || ret == -ENOSYS) {
609d566a54bSmrg            ms->dirty_enabled = FALSE;
610d566a54bSmrg            DamageUnregister(ms->damage);
611d566a54bSmrg            DamageDestroy(ms->damage);
612d566a54bSmrg            ms->damage = NULL;
613d566a54bSmrg            xf86DrvMsg(scrn->scrnIndex, X_INFO,
614d566a54bSmrg                       "Disabling kernel dirty updates, not required.\n");
615d566a54bSmrg            return;
616d566a54bSmrg        }
61735c4bbdfSmrg    }
61835c4bbdfSmrg}
61935c4bbdfSmrg
62035c4bbdfSmrgstatic void
6217e31ba66Smrgdispatch_dirty_pixmap(ScrnInfoPtr scrn, xf86CrtcPtr crtc, PixmapPtr ppix)
62235c4bbdfSmrg{
62335c4bbdfSmrg    modesettingPtr ms = modesettingPTR(scrn);
6247e31ba66Smrg    msPixmapPrivPtr ppriv = msGetPixmapPriv(&ms->drmmode, ppix);
6255a112b11Smrg    DamagePtr damage = ppriv->secondary_damage;
62635c4bbdfSmrg    int fb_id = ppriv->fb_id;
62735c4bbdfSmrg
628d566a54bSmrg    dispatch_dirty_region(scrn, crtc, ppix, damage, fb_id);
62935c4bbdfSmrg}
63035c4bbdfSmrg
63135c4bbdfSmrgstatic void
6325a112b11Smrgdispatch_secondary_dirty(ScreenPtr pScreen)
63335c4bbdfSmrg{
63435c4bbdfSmrg    ScrnInfoPtr scrn = xf86ScreenToScrn(pScreen);
63535c4bbdfSmrg    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
63635c4bbdfSmrg    int c;
63735c4bbdfSmrg
63835c4bbdfSmrg    for (c = 0; c < xf86_config->num_crtc; c++) {
63935c4bbdfSmrg        xf86CrtcPtr crtc = xf86_config->crtc[c];
6407e31ba66Smrg        drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
64135c4bbdfSmrg
6427e31ba66Smrg        if (!drmmode_crtc)
64335c4bbdfSmrg            continue;
64435c4bbdfSmrg
6457e31ba66Smrg        if (drmmode_crtc->prime_pixmap)
6467e31ba66Smrg            dispatch_dirty_pixmap(scrn, crtc, drmmode_crtc->prime_pixmap);
6477e31ba66Smrg        if (drmmode_crtc->prime_pixmap_back)
6487e31ba66Smrg            dispatch_dirty_pixmap(scrn, crtc, drmmode_crtc->prime_pixmap_back);
64935c4bbdfSmrg    }
65035c4bbdfSmrg}
65135c4bbdfSmrg
65235c4bbdfSmrgstatic void
6537e31ba66Smrgredisplay_dirty(ScreenPtr screen, PixmapDirtyUpdatePtr dirty, int *timeout)
65435c4bbdfSmrg{
6557e31ba66Smrg    RegionRec pixregion;
65635c4bbdfSmrg
6575a112b11Smrg    PixmapRegionInit(&pixregion, dirty->secondary_dst);
6585a112b11Smrg    DamageRegionAppend(&dirty->secondary_dst->drawable, &pixregion);
6597e31ba66Smrg    PixmapSyncDirtyHelper(dirty);
66035c4bbdfSmrg
6617e31ba66Smrg    if (!screen->isGPU) {
6627e31ba66Smrg#ifdef GLAMOR_HAS_GBM
6637e31ba66Smrg        modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(screen));
6647e31ba66Smrg        /*
6655a112b11Smrg         * When copying from the primary framebuffer to the shared pixmap,
6665a112b11Smrg         * we must ensure the copy is complete before the secondary starts a
6675a112b11Smrg         * copy to its own framebuffer (some secondarys scanout directly from
6687e31ba66Smrg         * the shared pixmap, but not all).
6697e31ba66Smrg         */
6707e31ba66Smrg        if (ms->drmmode.glamor)
6715a112b11Smrg            ms->glamor.finish(screen);
6727e31ba66Smrg#endif
6735a112b11Smrg        /* Ensure the secondary processes the damage immediately */
6747e31ba66Smrg        if (timeout)
6757e31ba66Smrg            *timeout = 0;
6767e31ba66Smrg    }
67735c4bbdfSmrg
6785a112b11Smrg    DamageRegionProcessPending(&dirty->secondary_dst->drawable);
6797e31ba66Smrg    RegionUninit(&pixregion);
68035c4bbdfSmrg}
68135c4bbdfSmrg
68235c4bbdfSmrgstatic void
6837e31ba66Smrgms_dirty_update(ScreenPtr screen, int *timeout)
68435c4bbdfSmrg{
6857e31ba66Smrg    modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(screen));
6867e31ba66Smrg
6877e31ba66Smrg    RegionPtr region;
6887e31ba66Smrg    PixmapDirtyUpdatePtr ent;
6897e31ba66Smrg
6907e31ba66Smrg    if (xorg_list_is_empty(&screen->pixmap_dirty_list))
6917e31ba66Smrg        return;
6927e31ba66Smrg
6937e31ba66Smrg    xorg_list_for_each_entry(ent, &screen->pixmap_dirty_list, ent) {
6947e31ba66Smrg        region = DamageRegion(ent->damage);
6957e31ba66Smrg        if (RegionNotEmpty(region)) {
6967e31ba66Smrg            if (!screen->isGPU) {
6977e31ba66Smrg                   msPixmapPrivPtr ppriv =
6985a112b11Smrg                    msGetPixmapPriv(&ms->drmmode, ent->secondary_dst->primary_pixmap);
69935c4bbdfSmrg
7007e31ba66Smrg                if (ppriv->notify_on_damage) {
7017e31ba66Smrg                    ppriv->notify_on_damage = FALSE;
70235c4bbdfSmrg
7035a112b11Smrg                    ent->secondary_dst->drawable.pScreen->
7045a112b11Smrg                        SharedPixmapNotifyDamage(ent->secondary_dst);
70535c4bbdfSmrg                }
7067e31ba66Smrg
7077e31ba66Smrg                /* Requested manual updating */
7087e31ba66Smrg                if (ppriv->defer_dirty_update)
7097e31ba66Smrg                    continue;
7107e31ba66Smrg            }
7117e31ba66Smrg
7127e31ba66Smrg            redisplay_dirty(screen, ent, timeout);
7137e31ba66Smrg            DamageEmpty(ent->damage);
71435c4bbdfSmrg        }
7157e31ba66Smrg    }
7167e31ba66Smrg}
7177e31ba66Smrg
7187e31ba66Smrgstatic PixmapDirtyUpdatePtr
7195a112b11Smrgms_dirty_get_ent(ScreenPtr screen, PixmapPtr secondary_dst)
7207e31ba66Smrg{
7217e31ba66Smrg    PixmapDirtyUpdatePtr ent;
7227e31ba66Smrg
7237e31ba66Smrg    if (xorg_list_is_empty(&screen->pixmap_dirty_list))
7247e31ba66Smrg        return NULL;
7257e31ba66Smrg
7267e31ba66Smrg    xorg_list_for_each_entry(ent, &screen->pixmap_dirty_list, ent) {
7275a112b11Smrg        if (ent->secondary_dst == secondary_dst)
7287e31ba66Smrg            return ent;
7297e31ba66Smrg    }
7307e31ba66Smrg
7317e31ba66Smrg    return NULL;
73235c4bbdfSmrg}
73335c4bbdfSmrg
73435c4bbdfSmrgstatic void
7357e31ba66SmrgmsBlockHandler(ScreenPtr pScreen, void *timeout)
73635c4bbdfSmrg{
73735c4bbdfSmrg    modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(pScreen));
73835c4bbdfSmrg
73935c4bbdfSmrg    pScreen->BlockHandler = ms->BlockHandler;
7407e31ba66Smrg    pScreen->BlockHandler(pScreen, timeout);
74135c4bbdfSmrg    ms->BlockHandler = pScreen->BlockHandler;
74235c4bbdfSmrg    pScreen->BlockHandler = msBlockHandler;
74335c4bbdfSmrg    if (pScreen->isGPU && !ms->drmmode.reverse_prime_offload_mode)
7445a112b11Smrg        dispatch_secondary_dirty(pScreen);
74535c4bbdfSmrg    else if (ms->dirty_enabled)
74635c4bbdfSmrg        dispatch_dirty(pScreen);
74735c4bbdfSmrg
7487e31ba66Smrg    ms_dirty_update(pScreen, timeout);
7497e31ba66Smrg}
7507e31ba66Smrg
7517e31ba66Smrgstatic void
7527e31ba66SmrgmsBlockHandler_oneshot(ScreenPtr pScreen, void *pTimeout)
7537e31ba66Smrg{
7547e31ba66Smrg    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
7557e31ba66Smrg    modesettingPtr ms = modesettingPTR(pScrn);
7567e31ba66Smrg
7577e31ba66Smrg    msBlockHandler(pScreen, pTimeout);
7587e31ba66Smrg
759806e81e9Smrg    drmmode_set_desired_modes(pScrn, &ms->drmmode, TRUE, FALSE);
76035c4bbdfSmrg}
76135c4bbdfSmrg
7625a112b11SmrgBool
7635a112b11Smrgms_window_has_variable_refresh(modesettingPtr ms, WindowPtr win) {
7645a112b11Smrg	struct ms_vrr_priv *priv = dixLookupPrivate(&win->devPrivates, &ms->drmmode.vrrPrivateKeyRec);
7655a112b11Smrg
7665a112b11Smrg	return priv->variable_refresh;
7675a112b11Smrg}
7685a112b11Smrg
7695a112b11Smrgstatic void
7705a112b11Smrgms_vrr_property_update(WindowPtr window, Bool variable_refresh)
7715a112b11Smrg{
7725a112b11Smrg    ScrnInfoPtr scrn = xf86ScreenToScrn(window->drawable.pScreen);
7735a112b11Smrg    modesettingPtr ms = modesettingPTR(scrn);
7745a112b11Smrg
7755a112b11Smrg    struct ms_vrr_priv *priv = dixLookupPrivate(&window->devPrivates,
7765a112b11Smrg                                                &ms->drmmode.vrrPrivateKeyRec);
7775a112b11Smrg    priv->variable_refresh = variable_refresh;
7785a112b11Smrg
7795a112b11Smrg    if (ms->flip_window == window && ms->drmmode.present_flipping)
7805a112b11Smrg        ms_present_set_screen_vrr(scrn, variable_refresh);
7815a112b11Smrg}
7825a112b11Smrg
7835a112b11Smrg/* Wrapper for xserver/dix/property.c:ProcChangeProperty */
7845a112b11Smrgstatic int
7855a112b11Smrgms_change_property(ClientPtr client)
7865a112b11Smrg{
7875a112b11Smrg    WindowPtr window = NULL;
7885a112b11Smrg    int ret = 0;
7895a112b11Smrg
7905a112b11Smrg    REQUEST(xChangePropertyReq);
7915a112b11Smrg
7925a112b11Smrg    client->requestVector[X_ChangeProperty] = saved_change_property;
7935a112b11Smrg    ret = saved_change_property(client);
7945a112b11Smrg
7955a112b11Smrg    if (restore_property_vector)
7965a112b11Smrg        return ret;
7975a112b11Smrg
7985a112b11Smrg    client->requestVector[X_ChangeProperty] = ms_change_property;
7995a112b11Smrg
8005a112b11Smrg    if (ret != Success)
8015a112b11Smrg        return ret;
8025a112b11Smrg
8035a112b11Smrg    ret = dixLookupWindow(&window, stuff->window, client, DixSetPropAccess);
8045a112b11Smrg    if (ret != Success)
8055a112b11Smrg        return ret;
8065a112b11Smrg
8075a112b11Smrg    // Checking for the VRR property change on the window
8085a112b11Smrg    if (stuff->property == vrr_atom &&
8095a112b11Smrg        xf86ScreenToScrn(window->drawable.pScreen)->PreInit == PreInit &&
8105a112b11Smrg        stuff->format == 32 && stuff->nUnits == 1) {
8115a112b11Smrg        uint32_t *value = (uint32_t *)(stuff + 1);
8125a112b11Smrg        ms_vrr_property_update(window, *value != 0);
8135a112b11Smrg    }
8145a112b11Smrg
8155a112b11Smrg    return ret;
8165a112b11Smrg}
8175a112b11Smrg
8185a112b11Smrg/* Wrapper for xserver/dix/property.c:ProcDeleteProperty */
8195a112b11Smrgstatic int
8205a112b11Smrgms_delete_property(ClientPtr client)
8215a112b11Smrg{
8225a112b11Smrg    WindowPtr window;
8235a112b11Smrg    int ret;
8245a112b11Smrg
8255a112b11Smrg    REQUEST(xDeletePropertyReq);
8265a112b11Smrg
8275a112b11Smrg    client->requestVector[X_DeleteProperty] = saved_delete_property;
8285a112b11Smrg    ret = saved_delete_property(client);
8295a112b11Smrg
8305a112b11Smrg    if (restore_property_vector)
8315a112b11Smrg        return ret;
8325a112b11Smrg
8335a112b11Smrg    client->requestVector[X_DeleteProperty] = ms_delete_property;
8345a112b11Smrg
8355a112b11Smrg    if (ret != Success)
8365a112b11Smrg        return ret;
8375a112b11Smrg
8385a112b11Smrg    ret = dixLookupWindow(&window, stuff->window, client, DixSetPropAccess);
8395a112b11Smrg    if (ret != Success)
8405a112b11Smrg        return ret;
8415a112b11Smrg
8425a112b11Smrg    if (stuff->property == vrr_atom &&
8435a112b11Smrg        xf86ScreenToScrn(window->drawable.pScreen)->PreInit == PreInit)
8445a112b11Smrg        ms_vrr_property_update(window, FALSE);
8455a112b11Smrg
8465a112b11Smrg    return ret;
8475a112b11Smrg}
8485a112b11Smrg
8495a112b11Smrgstatic void
8505a112b11Smrgms_unwrap_property_requests(ScrnInfoPtr scrn)
8515a112b11Smrg{
8525a112b11Smrg    int i;
8535a112b11Smrg
8545a112b11Smrg    if (!property_vectors_wrapped)
8555a112b11Smrg        return;
8565a112b11Smrg
8575a112b11Smrg    if (ProcVector[X_ChangeProperty] == ms_change_property)
8585a112b11Smrg        ProcVector[X_ChangeProperty] = saved_change_property;
8595a112b11Smrg    else
8605a112b11Smrg        restore_property_vector = TRUE;
8615a112b11Smrg
8625a112b11Smrg    if (ProcVector[X_DeleteProperty] == ms_delete_property)
8635a112b11Smrg        ProcVector[X_DeleteProperty] = saved_delete_property;
8645a112b11Smrg    else
8655a112b11Smrg        restore_property_vector = TRUE;
8665a112b11Smrg
8675a112b11Smrg    for (i = 0; i < currentMaxClients; i++) {
8685a112b11Smrg        if (clients[i]->requestVector[X_ChangeProperty] == ms_change_property) {
8695a112b11Smrg            clients[i]->requestVector[X_ChangeProperty] = saved_change_property;
8705a112b11Smrg        } else {
8715a112b11Smrg            restore_property_vector = TRUE;
8725a112b11Smrg        }
8735a112b11Smrg
8745a112b11Smrg        if (clients[i]->requestVector[X_DeleteProperty] == ms_delete_property) {
8755a112b11Smrg            clients[i]->requestVector[X_DeleteProperty] = saved_delete_property;
8765a112b11Smrg        } else {
8775a112b11Smrg            restore_property_vector = TRUE;
8785a112b11Smrg        }
8795a112b11Smrg    }
8805a112b11Smrg
8815a112b11Smrg    if (restore_property_vector) {
8825a112b11Smrg        xf86DrvMsg(scrn->scrnIndex, X_WARNING,
8835a112b11Smrg                   "Couldn't unwrap some window property request vectors\n");
8845a112b11Smrg    }
8855a112b11Smrg
8865a112b11Smrg    property_vectors_wrapped = FALSE;
8875a112b11Smrg}
8885a112b11Smrg
88935c4bbdfSmrgstatic void
89035c4bbdfSmrgFreeRec(ScrnInfoPtr pScrn)
89135c4bbdfSmrg{
89235c4bbdfSmrg    modesettingPtr ms;
89335c4bbdfSmrg
89435c4bbdfSmrg    if (!pScrn)
89535c4bbdfSmrg        return;
89635c4bbdfSmrg
89735c4bbdfSmrg    ms = modesettingPTR(pScrn);
89835c4bbdfSmrg    if (!ms)
89935c4bbdfSmrg        return;
90035c4bbdfSmrg
90135c4bbdfSmrg    if (ms->fd > 0) {
90235c4bbdfSmrg        modesettingEntPtr ms_ent;
90335c4bbdfSmrg        int ret;
90435c4bbdfSmrg
90535c4bbdfSmrg        ms_ent = ms_ent_priv(pScrn);
90635c4bbdfSmrg        ms_ent->fd_ref--;
90735c4bbdfSmrg        if (!ms_ent->fd_ref) {
9085a112b11Smrg            ms_unwrap_property_requests(pScrn);
90935c4bbdfSmrg            if (ms->pEnt->location.type == BUS_PCI)
91035c4bbdfSmrg                ret = drmClose(ms->fd);
91135c4bbdfSmrg            else
91235c4bbdfSmrg#ifdef XF86_PDEV_SERVER_FD
91335c4bbdfSmrg                if (!(ms->pEnt->location.type == BUS_PLATFORM &&
91435c4bbdfSmrg                      (ms->pEnt->location.id.plat->flags & XF86_PDEV_SERVER_FD)))
91535c4bbdfSmrg#endif
91635c4bbdfSmrg                    ret = close(ms->fd);
91735c4bbdfSmrg            (void) ret;
91835c4bbdfSmrg            ms_ent->fd = 0;
91935c4bbdfSmrg        }
92035c4bbdfSmrg    }
92135c4bbdfSmrg    pScrn->driverPrivate = NULL;
92235c4bbdfSmrg    free(ms->drmmode.Options);
92335c4bbdfSmrg    free(ms);
92435c4bbdfSmrg
92535c4bbdfSmrg}
92635c4bbdfSmrg
9275a112b11Smrg#ifdef GLAMOR_HAS_GBM
9285a112b11Smrg
9295a112b11Smrgstatic Bool
9305a112b11Smrgload_glamor(ScrnInfoPtr pScrn)
9315a112b11Smrg{
9325a112b11Smrg    void *mod = xf86LoadSubModule(pScrn, GLAMOR_EGL_MODULE_NAME);
9335a112b11Smrg    modesettingPtr ms = modesettingPTR(pScrn);
9345a112b11Smrg
9355a112b11Smrg    if (!mod)
9365a112b11Smrg        return FALSE;
9375a112b11Smrg
9385a112b11Smrg    ms->glamor.back_pixmap_from_fd = LoaderSymbolFromModule(mod, "glamor_back_pixmap_from_fd");
9395a112b11Smrg    ms->glamor.block_handler = LoaderSymbolFromModule(mod, "glamor_block_handler");
9405a112b11Smrg    ms->glamor.clear_pixmap = LoaderSymbolFromModule(mod, "glamor_clear_pixmap");
9415a112b11Smrg    ms->glamor.egl_create_textured_pixmap = LoaderSymbolFromModule(mod, "glamor_egl_create_textured_pixmap");
9425a112b11Smrg    ms->glamor.egl_create_textured_pixmap_from_gbm_bo = LoaderSymbolFromModule(mod, "glamor_egl_create_textured_pixmap_from_gbm_bo");
9435a112b11Smrg    ms->glamor.egl_exchange_buffers = LoaderSymbolFromModule(mod, "glamor_egl_exchange_buffers");
9445a112b11Smrg    ms->glamor.egl_get_gbm_device = LoaderSymbolFromModule(mod, "glamor_egl_get_gbm_device");
9455a112b11Smrg    ms->glamor.egl_init = LoaderSymbolFromModule(mod, "glamor_egl_init");
9465a112b11Smrg    ms->glamor.finish = LoaderSymbolFromModule(mod, "glamor_finish");
9475a112b11Smrg    ms->glamor.gbm_bo_from_pixmap = LoaderSymbolFromModule(mod, "glamor_gbm_bo_from_pixmap");
9485a112b11Smrg    ms->glamor.init = LoaderSymbolFromModule(mod, "glamor_init");
9495a112b11Smrg    ms->glamor.name_from_pixmap = LoaderSymbolFromModule(mod, "glamor_name_from_pixmap");
9505a112b11Smrg    ms->glamor.set_drawable_modifiers_func = LoaderSymbolFromModule(mod, "glamor_set_drawable_modifiers_func");
9515a112b11Smrg    ms->glamor.shareable_fd_from_pixmap = LoaderSymbolFromModule(mod, "glamor_shareable_fd_from_pixmap");
9525a112b11Smrg    ms->glamor.supports_pixmap_import_export = LoaderSymbolFromModule(mod, "glamor_supports_pixmap_import_export");
9535a112b11Smrg    ms->glamor.xv_init = LoaderSymbolFromModule(mod, "glamor_xv_init");
9545a112b11Smrg    ms->glamor.egl_get_driver_name = LoaderSymbolFromModule(mod, "glamor_egl_get_driver_name");
9555a112b11Smrg
9565a112b11Smrg    return TRUE;
9575a112b11Smrg}
9585a112b11Smrg
9595a112b11Smrg#endif
9605a112b11Smrg
96135c4bbdfSmrgstatic void
96235c4bbdfSmrgtry_enable_glamor(ScrnInfoPtr pScrn)
96335c4bbdfSmrg{
96435c4bbdfSmrg    modesettingPtr ms = modesettingPTR(pScrn);
96535c4bbdfSmrg    const char *accel_method_str = xf86GetOptValString(ms->drmmode.Options,
96635c4bbdfSmrg                                                       OPTION_ACCEL_METHOD);
96735c4bbdfSmrg    Bool do_glamor = (!accel_method_str ||
96835c4bbdfSmrg                      strcmp(accel_method_str, "glamor") == 0);
96935c4bbdfSmrg
97035c4bbdfSmrg    ms->drmmode.glamor = FALSE;
97135c4bbdfSmrg
9727e31ba66Smrg#ifdef GLAMOR_HAS_GBM
97335c4bbdfSmrg    if (ms->drmmode.force_24_32) {
97435c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "Cannot use glamor with 24bpp packed fb\n");
97535c4bbdfSmrg        return;
97635c4bbdfSmrg    }
97735c4bbdfSmrg
97835c4bbdfSmrg    if (!do_glamor) {
97935c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "glamor disabled\n");
98035c4bbdfSmrg        return;
98135c4bbdfSmrg    }
98235c4bbdfSmrg
9835a112b11Smrg    if (load_glamor(pScrn)) {
9845a112b11Smrg        if (ms->glamor.egl_init(pScrn, ms->fd)) {
98535c4bbdfSmrg            xf86DrvMsg(pScrn->scrnIndex, X_INFO, "glamor initialized\n");
98635c4bbdfSmrg            ms->drmmode.glamor = TRUE;
98735c4bbdfSmrg        } else {
9885a112b11Smrg            xf86DrvMsg(pScrn->scrnIndex, X_INFO,
98935c4bbdfSmrg                       "glamor initialization failed\n");
99035c4bbdfSmrg        }
99135c4bbdfSmrg    } else {
99235c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
99335c4bbdfSmrg                   "Failed to load glamor module.\n");
99435c4bbdfSmrg    }
99535c4bbdfSmrg#else
99635c4bbdfSmrg    if (do_glamor) {
99735c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_INFO,
99835c4bbdfSmrg                   "No glamor support in the X Server\n");
99935c4bbdfSmrg    }
100035c4bbdfSmrg#endif
100135c4bbdfSmrg}
100235c4bbdfSmrg
10037e31ba66Smrgstatic Bool
10047e31ba66SmrgmsShouldDoubleShadow(ScrnInfoPtr pScrn, modesettingPtr ms)
10057e31ba66Smrg{
10067e31ba66Smrg    Bool ret = FALSE, asked;
10077e31ba66Smrg    int from;
10087e31ba66Smrg    drmVersionPtr v = drmGetVersion(ms->fd);
100935c4bbdfSmrg
10107e31ba66Smrg    if (!ms->drmmode.shadow_enable)
10117e31ba66Smrg        return FALSE;
10127e31ba66Smrg
10137e31ba66Smrg    if (!strcmp(v->name, "mgag200") ||
10147e31ba66Smrg        !strcmp(v->name, "ast")) /* XXX || rn50 */
10157e31ba66Smrg        ret = TRUE;
10167e31ba66Smrg
10177e31ba66Smrg    drmFreeVersion(v);
10187e31ba66Smrg
10197e31ba66Smrg    asked = xf86GetOptValBool(ms->drmmode.Options, OPTION_DOUBLE_SHADOW, &ret);
10207e31ba66Smrg
10217e31ba66Smrg    if (asked)
10227e31ba66Smrg        from = X_CONFIG;
10237e31ba66Smrg    else
10247e31ba66Smrg        from = X_INFO;
10257e31ba66Smrg
10267e31ba66Smrg    xf86DrvMsg(pScrn->scrnIndex, from,
10277e31ba66Smrg               "Double-buffered shadow updates: %s\n", ret ? "on" : "off");
10287e31ba66Smrg
10297e31ba66Smrg    return ret;
10307e31ba66Smrg}
103135c4bbdfSmrg
103235c4bbdfSmrgstatic Bool
103335c4bbdfSmrgms_get_drm_master_fd(ScrnInfoPtr pScrn)
103435c4bbdfSmrg{
103535c4bbdfSmrg    EntityInfoPtr pEnt;
103635c4bbdfSmrg    modesettingPtr ms;
103735c4bbdfSmrg    modesettingEntPtr ms_ent;
103835c4bbdfSmrg
103935c4bbdfSmrg    ms = modesettingPTR(pScrn);
104035c4bbdfSmrg    ms_ent = ms_ent_priv(pScrn);
104135c4bbdfSmrg
104235c4bbdfSmrg    pEnt = ms->pEnt;
104335c4bbdfSmrg
104435c4bbdfSmrg    if (ms_ent->fd) {
104535c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_INFO,
104635c4bbdfSmrg                   " reusing fd for second head\n");
104735c4bbdfSmrg        ms->fd = ms_ent->fd;
104835c4bbdfSmrg        ms_ent->fd_ref++;
104935c4bbdfSmrg        return TRUE;
105035c4bbdfSmrg    }
105135c4bbdfSmrg
10527e31ba66Smrg    ms->fd_passed = FALSE;
10537e31ba66Smrg    if ((ms->fd = get_passed_fd()) >= 0) {
10547e31ba66Smrg        ms->fd_passed = TRUE;
10557e31ba66Smrg        return TRUE;
10567e31ba66Smrg    }
10577e31ba66Smrg
10587e31ba66Smrg#ifdef XSERVER_PLATFORM_BUS
105935c4bbdfSmrg    if (pEnt->location.type == BUS_PLATFORM) {
106035c4bbdfSmrg#ifdef XF86_PDEV_SERVER_FD
106135c4bbdfSmrg        if (pEnt->location.id.plat->flags & XF86_PDEV_SERVER_FD)
106235c4bbdfSmrg            ms->fd =
106335c4bbdfSmrg                xf86_platform_device_odev_attributes(pEnt->location.id.plat)->
106435c4bbdfSmrg                fd;
106535c4bbdfSmrg        else
106635c4bbdfSmrg#endif
106735c4bbdfSmrg        {
106835c4bbdfSmrg            char *path =
106935c4bbdfSmrg                xf86_platform_device_odev_attributes(pEnt->location.id.plat)->
107035c4bbdfSmrg                path;
107135c4bbdfSmrg            ms->fd = open_hw(path);
107235c4bbdfSmrg        }
107335c4bbdfSmrg    }
107435c4bbdfSmrg    else
107535c4bbdfSmrg#endif
10767e31ba66Smrg#ifdef XSERVER_LIBPCIACCESS
107735c4bbdfSmrg    if (pEnt->location.type == BUS_PCI) {
10787e31ba66Smrg        char *BusID = NULL;
10797e31ba66Smrg        struct pci_device *PciInfo;
10807e31ba66Smrg
10817e31ba66Smrg        PciInfo = xf86GetPciInfoForEntity(ms->pEnt->index);
10827e31ba66Smrg        if (PciInfo) {
10837e31ba66Smrg            if ((BusID = ms_DRICreatePCIBusID(PciInfo)) != NULL) {
10847e31ba66Smrg                ms->fd = drmOpen(NULL, BusID);
10857e31ba66Smrg                free(BusID);
10867e31ba66Smrg            }
108735c4bbdfSmrg        }
108835c4bbdfSmrg    }
10897e31ba66Smrg    else
10907e31ba66Smrg#endif
10917e31ba66Smrg    {
109235c4bbdfSmrg        const char *devicename;
109335c4bbdfSmrg        devicename = xf86FindOptionValue(ms->pEnt->device->options, "kmsdev");
109435c4bbdfSmrg        ms->fd = open_hw(devicename);
109535c4bbdfSmrg    }
109635c4bbdfSmrg    if (ms->fd < 0)
109735c4bbdfSmrg        return FALSE;
109835c4bbdfSmrg
109935c4bbdfSmrg    ms_ent->fd = ms->fd;
110035c4bbdfSmrg    ms_ent->fd_ref = 1;
110135c4bbdfSmrg    return TRUE;
110235c4bbdfSmrg}
110335c4bbdfSmrg
110435c4bbdfSmrgstatic Bool
110535c4bbdfSmrgPreInit(ScrnInfoPtr pScrn, int flags)
110635c4bbdfSmrg{
110735c4bbdfSmrg    modesettingPtr ms;
110835c4bbdfSmrg    rgb defaultWeight = { 0, 0, 0 };
110935c4bbdfSmrg    EntityInfoPtr pEnt;
111035c4bbdfSmrg    uint64_t value = 0;
111135c4bbdfSmrg    int ret;
11127e31ba66Smrg    int bppflags, connector_count;
111335c4bbdfSmrg    int defaultdepth, defaultbpp;
111435c4bbdfSmrg
111535c4bbdfSmrg    if (pScrn->numEntities != 1)
111635c4bbdfSmrg        return FALSE;
111735c4bbdfSmrg
111835c4bbdfSmrg    if (flags & PROBE_DETECT) {
111935c4bbdfSmrg        return FALSE;
112035c4bbdfSmrg    }
112135c4bbdfSmrg
112235c4bbdfSmrg    /* Allocate driverPrivate */
112397cbb544Smaya    if (!GetRec(pScrn))
112435c4bbdfSmrg        return FALSE;
112535c4bbdfSmrg
11267e31ba66Smrg    pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
11277e31ba66Smrg
112835c4bbdfSmrg    ms = modesettingPTR(pScrn);
112935c4bbdfSmrg    ms->SaveGeneration = -1;
113035c4bbdfSmrg    ms->pEnt = pEnt;
113135c4bbdfSmrg    ms->drmmode.is_secondary = FALSE;
113235c4bbdfSmrg    pScrn->displayWidth = 640;  /* default it */
113335c4bbdfSmrg
113435c4bbdfSmrg    if (xf86IsEntityShared(pScrn->entityList[0])) {
113535c4bbdfSmrg        if (xf86IsPrimInitDone(pScrn->entityList[0]))
113635c4bbdfSmrg            ms->drmmode.is_secondary = TRUE;
113735c4bbdfSmrg        else
113835c4bbdfSmrg            xf86SetPrimInitDone(pScrn->entityList[0]);
113935c4bbdfSmrg    }
114035c4bbdfSmrg
114135c4bbdfSmrg    pScrn->monitor = pScrn->confScreen->monitor;
114235c4bbdfSmrg    pScrn->progClock = TRUE;
114335c4bbdfSmrg    pScrn->rgbBits = 8;
114435c4bbdfSmrg
114535c4bbdfSmrg    if (!ms_get_drm_master_fd(pScrn))
114635c4bbdfSmrg        return FALSE;
114735c4bbdfSmrg    ms->drmmode.fd = ms->fd;
114835c4bbdfSmrg
11497e31ba66Smrg    if (!check_outputs(ms->fd, &connector_count))
11507e31ba66Smrg        return FALSE;
11517e31ba66Smrg
115235c4bbdfSmrg    drmmode_get_default_bpp(pScrn, &ms->drmmode, &defaultdepth, &defaultbpp);
115335c4bbdfSmrg    if (defaultdepth == 24 && defaultbpp == 24) {
115435c4bbdfSmrg        ms->drmmode.force_24_32 = TRUE;
115535c4bbdfSmrg        ms->drmmode.kbpp = 24;
115635c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_INFO,
115735c4bbdfSmrg                   "Using 24bpp hw front buffer with 32bpp shadow\n");
115835c4bbdfSmrg        defaultbpp = 32;
115935c4bbdfSmrg    } else {
11607e31ba66Smrg        ms->drmmode.kbpp = 0;
116135c4bbdfSmrg    }
116235c4bbdfSmrg    bppflags = PreferConvert24to32 | SupportConvert24to32 | Support32bppFb;
116335c4bbdfSmrg
116435c4bbdfSmrg    if (!xf86SetDepthBpp
116535c4bbdfSmrg        (pScrn, defaultdepth, defaultdepth, defaultbpp, bppflags))
116635c4bbdfSmrg        return FALSE;
116735c4bbdfSmrg
116835c4bbdfSmrg    switch (pScrn->depth) {
116935c4bbdfSmrg    case 15:
117035c4bbdfSmrg    case 16:
117135c4bbdfSmrg    case 24:
11727e31ba66Smrg    case 30:
117335c4bbdfSmrg        break;
117435c4bbdfSmrg    default:
117535c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
117635c4bbdfSmrg                   "Given depth (%d) is not supported by the driver\n",
117735c4bbdfSmrg                   pScrn->depth);
117835c4bbdfSmrg        return FALSE;
117935c4bbdfSmrg    }
118035c4bbdfSmrg    xf86PrintDepthBpp(pScrn);
11817e31ba66Smrg    if (!ms->drmmode.kbpp)
11827e31ba66Smrg        ms->drmmode.kbpp = pScrn->bitsPerPixel;
118335c4bbdfSmrg
118435c4bbdfSmrg    /* Process the options */
118535c4bbdfSmrg    xf86CollectOptions(pScrn, NULL);
118635c4bbdfSmrg    if (!(ms->drmmode.Options = malloc(sizeof(Options))))
118735c4bbdfSmrg        return FALSE;
118835c4bbdfSmrg    memcpy(ms->drmmode.Options, Options, sizeof(Options));
118935c4bbdfSmrg    xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, ms->drmmode.Options);
119035c4bbdfSmrg
119135c4bbdfSmrg    if (!xf86SetWeight(pScrn, defaultWeight, defaultWeight))
119235c4bbdfSmrg        return FALSE;
119335c4bbdfSmrg    if (!xf86SetDefaultVisual(pScrn, -1))
119435c4bbdfSmrg        return FALSE;
119535c4bbdfSmrg
119635c4bbdfSmrg    if (xf86ReturnOptValBool(ms->drmmode.Options, OPTION_SW_CURSOR, FALSE)) {
119735c4bbdfSmrg        ms->drmmode.sw_cursor = TRUE;
119835c4bbdfSmrg    }
119935c4bbdfSmrg
120035c4bbdfSmrg    ms->cursor_width = 64;
120135c4bbdfSmrg    ms->cursor_height = 64;
120235c4bbdfSmrg    ret = drmGetCap(ms->fd, DRM_CAP_CURSOR_WIDTH, &value);
120335c4bbdfSmrg    if (!ret) {
120435c4bbdfSmrg        ms->cursor_width = value;
120535c4bbdfSmrg    }
120635c4bbdfSmrg    ret = drmGetCap(ms->fd, DRM_CAP_CURSOR_HEIGHT, &value);
120735c4bbdfSmrg    if (!ret) {
120835c4bbdfSmrg        ms->cursor_height = value;
120935c4bbdfSmrg    }
121035c4bbdfSmrg
121135c4bbdfSmrg    try_enable_glamor(pScrn);
121235c4bbdfSmrg
12137e31ba66Smrg    if (!ms->drmmode.glamor) {
121435c4bbdfSmrg        Bool prefer_shadow = TRUE;
121535c4bbdfSmrg
121635c4bbdfSmrg        if (ms->drmmode.force_24_32) {
121735c4bbdfSmrg            prefer_shadow = TRUE;
121835c4bbdfSmrg            ms->drmmode.shadow_enable = TRUE;
121935c4bbdfSmrg        } else {
122035c4bbdfSmrg            ret = drmGetCap(ms->fd, DRM_CAP_DUMB_PREFER_SHADOW, &value);
122135c4bbdfSmrg            if (!ret) {
122235c4bbdfSmrg                prefer_shadow = !!value;
122335c4bbdfSmrg            }
122435c4bbdfSmrg
122535c4bbdfSmrg            ms->drmmode.shadow_enable =
122635c4bbdfSmrg                xf86ReturnOptValBool(ms->drmmode.Options, OPTION_SHADOW_FB,
122735c4bbdfSmrg                                     prefer_shadow);
122835c4bbdfSmrg        }
122935c4bbdfSmrg
123035c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_INFO,
123135c4bbdfSmrg                   "ShadowFB: preferred %s, enabled %s\n",
123235c4bbdfSmrg                   prefer_shadow ? "YES" : "NO",
123335c4bbdfSmrg                   ms->drmmode.force_24_32 ? "FORCE" :
123435c4bbdfSmrg                   ms->drmmode.shadow_enable ? "YES" : "NO");
123535c4bbdfSmrg
12367e31ba66Smrg        ms->drmmode.shadow_enable2 = msShouldDoubleShadow(pScrn, ms);
12375a112b11Smrg    } else {
12385a112b11Smrg        if (!pScrn->is_gpu) {
12395a112b11Smrg            MessageType from = xf86GetOptValBool(ms->drmmode.Options, OPTION_VARIABLE_REFRESH,
12405a112b11Smrg                                                 &ms->vrr_support) ? X_CONFIG : X_DEFAULT;
12415a112b11Smrg            xf86DrvMsg(pScrn->scrnIndex, from, "VariableRefresh: %sabled\n",
12425a112b11Smrg                       ms->vrr_support ? "en" : "dis");
12435a112b11Smrg
12445a112b11Smrg            ms->drmmode.async_flip_secondaries = FALSE;
12455a112b11Smrg            from = xf86GetOptValBool(ms->drmmode.Options, OPTION_ASYNC_FLIP_SECONDARIES,
12465a112b11Smrg                                     &ms->drmmode.async_flip_secondaries) ? X_CONFIG : X_DEFAULT;
12475a112b11Smrg            xf86DrvMsg(pScrn->scrnIndex, from, "AsyncFlipSecondaries: %sabled\n",
12485a112b11Smrg                       ms->drmmode.async_flip_secondaries ? "en" : "dis");
12495a112b11Smrg        }
125035c4bbdfSmrg    }
125135c4bbdfSmrg
12527e31ba66Smrg    ms->drmmode.pageflip =
12537e31ba66Smrg        xf86ReturnOptValBool(ms->drmmode.Options, OPTION_PAGEFLIP, TRUE);
12547e31ba66Smrg
125535c4bbdfSmrg    pScrn->capabilities = 0;
125635c4bbdfSmrg    ret = drmGetCap(ms->fd, DRM_CAP_PRIME, &value);
125735c4bbdfSmrg    if (ret == 0) {
12587e31ba66Smrg        if (connector_count && (value & DRM_PRIME_CAP_IMPORT)) {
125935c4bbdfSmrg            pScrn->capabilities |= RR_Capability_SinkOutput;
126035c4bbdfSmrg            if (ms->drmmode.glamor)
126135c4bbdfSmrg                pScrn->capabilities |= RR_Capability_SinkOffload;
126235c4bbdfSmrg        }
12637e31ba66Smrg#ifdef GLAMOR_HAS_GBM_LINEAR
126435c4bbdfSmrg        if (value & DRM_PRIME_CAP_EXPORT && ms->drmmode.glamor)
126535c4bbdfSmrg            pScrn->capabilities |= RR_Capability_SourceOutput | RR_Capability_SourceOffload;
126635c4bbdfSmrg#endif
126735c4bbdfSmrg    }
12687e31ba66Smrg
12695a112b11Smrg    /*
12705a112b11Smrg     * Use "atomic modesetting disable" request to detect if the kms driver is
12715a112b11Smrg     * atomic capable, regardless if we will actually use atomic modesetting.
12725a112b11Smrg     * This is effectively a no-op, we only care about the return status code.
12735a112b11Smrg     */
12745a112b11Smrg    ret = drmSetClientCap(ms->fd, DRM_CLIENT_CAP_ATOMIC, 0);
12755a112b11Smrg    ms->atomic_modeset_capable = (ret == 0);
12765a112b11Smrg
1277806e81e9Smrg    if (xf86ReturnOptValBool(ms->drmmode.Options, OPTION_ATOMIC, FALSE)) {
1278806e81e9Smrg        ret = drmSetClientCap(ms->fd, DRM_CLIENT_CAP_ATOMIC, 1);
1279806e81e9Smrg        ms->atomic_modeset = (ret == 0);
1280806e81e9Smrg    } else {
1281806e81e9Smrg        ms->atomic_modeset = FALSE;
1282806e81e9Smrg    }
12837e31ba66Smrg
12847e31ba66Smrg    ms->kms_has_modifiers = FALSE;
12857e31ba66Smrg    ret = drmGetCap(ms->fd, DRM_CAP_ADDFB2_MODIFIERS, &value);
12867e31ba66Smrg    if (ret == 0 && value != 0)
12877e31ba66Smrg        ms->kms_has_modifiers = TRUE;
128835c4bbdfSmrg
128935c4bbdfSmrg    if (drmmode_pre_init(pScrn, &ms->drmmode, pScrn->bitsPerPixel / 8) == FALSE) {
129035c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "KMS setup failed\n");
129135c4bbdfSmrg        goto fail;
129235c4bbdfSmrg    }
129335c4bbdfSmrg
129435c4bbdfSmrg    /*
129535c4bbdfSmrg     * If the driver can do gamma correction, it should call xf86SetGamma() here.
129635c4bbdfSmrg     */
129735c4bbdfSmrg    {
129835c4bbdfSmrg        Gamma zeros = { 0.0, 0.0, 0.0 };
129935c4bbdfSmrg
130035c4bbdfSmrg        if (!xf86SetGamma(pScrn, zeros)) {
130135c4bbdfSmrg            return FALSE;
130235c4bbdfSmrg        }
130335c4bbdfSmrg    }
130435c4bbdfSmrg
13057e31ba66Smrg    if (!(pScrn->is_gpu && connector_count == 0) && pScrn->modes == NULL) {
130635c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No modes.\n");
130735c4bbdfSmrg        return FALSE;
130835c4bbdfSmrg    }
130935c4bbdfSmrg
131035c4bbdfSmrg    pScrn->currentMode = pScrn->modes;
131135c4bbdfSmrg
131235c4bbdfSmrg    /* Set display resolution */
131335c4bbdfSmrg    xf86SetDpi(pScrn, 0, 0);
131435c4bbdfSmrg
131535c4bbdfSmrg    /* Load the required sub modules */
131635c4bbdfSmrg    if (!xf86LoadSubModule(pScrn, "fb")) {
131735c4bbdfSmrg        return FALSE;
131835c4bbdfSmrg    }
131935c4bbdfSmrg
132035c4bbdfSmrg    if (ms->drmmode.shadow_enable) {
13215a112b11Smrg        void *mod = xf86LoadSubModule(pScrn, "shadow");
13225a112b11Smrg
13235a112b11Smrg        if (!mod)
132435c4bbdfSmrg            return FALSE;
13255a112b11Smrg
13265a112b11Smrg        ms->shadow.Setup        = LoaderSymbolFromModule(mod, "shadowSetup");
13275a112b11Smrg        ms->shadow.Add          = LoaderSymbolFromModule(mod, "shadowAdd");
13285a112b11Smrg        ms->shadow.Remove       = LoaderSymbolFromModule(mod, "shadowRemove");
13295a112b11Smrg        ms->shadow.Update32to24 = LoaderSymbolFromModule(mod, "shadowUpdate32to24");
13305a112b11Smrg        ms->shadow.UpdatePacked = LoaderSymbolFromModule(mod, "shadowUpdatePacked");
133135c4bbdfSmrg    }
133235c4bbdfSmrg
133335c4bbdfSmrg    return TRUE;
133435c4bbdfSmrg fail:
133535c4bbdfSmrg    return FALSE;
133635c4bbdfSmrg}
133735c4bbdfSmrg
133835c4bbdfSmrgstatic void *
133935c4bbdfSmrgmsShadowWindow(ScreenPtr screen, CARD32 row, CARD32 offset, int mode,
134035c4bbdfSmrg               CARD32 *size, void *closure)
134135c4bbdfSmrg{
134235c4bbdfSmrg    ScrnInfoPtr pScrn = xf86ScreenToScrn(screen);
134335c4bbdfSmrg    modesettingPtr ms = modesettingPTR(pScrn);
134435c4bbdfSmrg    int stride;
134535c4bbdfSmrg
134635c4bbdfSmrg    stride = (pScrn->displayWidth * ms->drmmode.kbpp) / 8;
134735c4bbdfSmrg    *size = stride;
134835c4bbdfSmrg
134935c4bbdfSmrg    return ((uint8_t *) ms->drmmode.front_bo.dumb->ptr + row * stride + offset);
135035c4bbdfSmrg}
135135c4bbdfSmrg
13527e31ba66Smrg/* somewhat arbitrary tile size, in pixels */
13537e31ba66Smrg#define TILE 16
13547e31ba66Smrg
13557e31ba66Smrgstatic int
13567e31ba66SmrgmsUpdateIntersect(modesettingPtr ms, shadowBufPtr pBuf, BoxPtr box,
13577e31ba66Smrg                  xRectangle *prect)
13587e31ba66Smrg{
13597e31ba66Smrg    int i, dirty = 0, stride = pBuf->pPixmap->devKind, cpp = ms->drmmode.cpp;
13607e31ba66Smrg    int width = (box->x2 - box->x1) * cpp;
13617e31ba66Smrg    unsigned char *old, *new;
13627e31ba66Smrg
13637e31ba66Smrg    old = ms->drmmode.shadow_fb2;
13647e31ba66Smrg    old += (box->y1 * stride) + (box->x1 * cpp);
13657e31ba66Smrg    new = ms->drmmode.shadow_fb;
13667e31ba66Smrg    new += (box->y1 * stride) + (box->x1 * cpp);
13677e31ba66Smrg
13687e31ba66Smrg    for (i = box->y2 - box->y1 - 1; i >= 0; i--) {
13697e31ba66Smrg        unsigned char *o = old + i * stride,
13707e31ba66Smrg                      *n = new + i * stride;
13717e31ba66Smrg        if (memcmp(o, n, width) != 0) {
13727e31ba66Smrg            dirty = 1;
13737e31ba66Smrg            memcpy(o, n, width);
13747e31ba66Smrg        }
13757e31ba66Smrg    }
13767e31ba66Smrg
13777e31ba66Smrg    if (dirty) {
13787e31ba66Smrg        prect->x = box->x1;
13797e31ba66Smrg        prect->y = box->y1;
13807e31ba66Smrg        prect->width = box->x2 - box->x1;
13817e31ba66Smrg        prect->height = box->y2 - box->y1;
13827e31ba66Smrg    }
13837e31ba66Smrg
13847e31ba66Smrg    return dirty;
13857e31ba66Smrg}
13867e31ba66Smrg
138735c4bbdfSmrgstatic void
138835c4bbdfSmrgmsUpdatePacked(ScreenPtr pScreen, shadowBufPtr pBuf)
138935c4bbdfSmrg{
13907e31ba66Smrg    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
13917e31ba66Smrg    modesettingPtr ms = modesettingPTR(pScrn);
13927e31ba66Smrg    Bool use_3224 = ms->drmmode.force_24_32 && pScrn->bitsPerPixel == 32;
13937e31ba66Smrg
13947e31ba66Smrg    if (ms->drmmode.shadow_enable2 && ms->drmmode.shadow_fb2) do {
13957e31ba66Smrg        RegionPtr damage = DamageRegion(pBuf->pDamage), tiles;
13967e31ba66Smrg        BoxPtr extents = RegionExtents(damage);
13977e31ba66Smrg        xRectangle *prect;
13987e31ba66Smrg        int nrects;
13997e31ba66Smrg        int i, j, tx1, tx2, ty1, ty2;
14007e31ba66Smrg
14017e31ba66Smrg        tx1 = extents->x1 / TILE;
14027e31ba66Smrg        tx2 = (extents->x2 + TILE - 1) / TILE;
14037e31ba66Smrg        ty1 = extents->y1 / TILE;
14047e31ba66Smrg        ty2 = (extents->y2 + TILE - 1) / TILE;
14057e31ba66Smrg
14067e31ba66Smrg        nrects = (tx2 - tx1) * (ty2 - ty1);
14077e31ba66Smrg        if (!(prect = calloc(nrects, sizeof(xRectangle))))
14087e31ba66Smrg            break;
14097e31ba66Smrg
14107e31ba66Smrg        nrects = 0;
14117e31ba66Smrg        for (j = ty2 - 1; j >= ty1; j--) {
14127e31ba66Smrg            for (i = tx2 - 1; i >= tx1; i--) {
14137e31ba66Smrg                BoxRec box;
14147e31ba66Smrg
14157e31ba66Smrg                box.x1 = max(i * TILE, extents->x1);
14167e31ba66Smrg                box.y1 = max(j * TILE, extents->y1);
14177e31ba66Smrg                box.x2 = min((i+1) * TILE, extents->x2);
14187e31ba66Smrg                box.y2 = min((j+1) * TILE, extents->y2);
14197e31ba66Smrg
14207e31ba66Smrg                if (RegionContainsRect(damage, &box) != rgnOUT) {
14217e31ba66Smrg                    if (msUpdateIntersect(ms, pBuf, &box, prect + nrects)) {
14227e31ba66Smrg                        nrects++;
14237e31ba66Smrg                    }
14247e31ba66Smrg                }
14257e31ba66Smrg            }
14267e31ba66Smrg        }
14277e31ba66Smrg
14287e31ba66Smrg        tiles = RegionFromRects(nrects, prect, CT_NONE);
14297e31ba66Smrg        RegionIntersect(damage, damage, tiles);
14307e31ba66Smrg        RegionDestroy(tiles);
14317e31ba66Smrg        free(prect);
14327e31ba66Smrg    } while (0);
14337e31ba66Smrg
14347e31ba66Smrg    if (use_3224)
14355a112b11Smrg        ms->shadow.Update32to24(pScreen, pBuf);
14367e31ba66Smrg    else
14375a112b11Smrg        ms->shadow.UpdatePacked(pScreen, pBuf);
14387e31ba66Smrg}
14397e31ba66Smrg
14407e31ba66Smrgstatic Bool
14417e31ba66SmrgmsEnableSharedPixmapFlipping(RRCrtcPtr crtc, PixmapPtr front, PixmapPtr back)
14427e31ba66Smrg{
14437e31ba66Smrg    ScreenPtr screen = crtc->pScreen;
14447e31ba66Smrg    ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
14457e31ba66Smrg    modesettingPtr ms = modesettingPTR(scrn);
14467e31ba66Smrg    EntityInfoPtr pEnt = ms->pEnt;
14477e31ba66Smrg    xf86CrtcPtr xf86Crtc = crtc->devPrivate;
14487e31ba66Smrg
14497e31ba66Smrg    if (!xf86Crtc)
14507e31ba66Smrg        return FALSE;
14517e31ba66Smrg
14527e31ba66Smrg    /* Not supported if we can't flip */
14537e31ba66Smrg    if (!ms->drmmode.pageflip)
14547e31ba66Smrg        return FALSE;
14557e31ba66Smrg
14567e31ba66Smrg    /* Not currently supported with reverse PRIME */
14577e31ba66Smrg    if (ms->drmmode.reverse_prime_offload_mode)
14587e31ba66Smrg        return FALSE;
14597e31ba66Smrg
14607e31ba66Smrg#ifdef XSERVER_PLATFORM_BUS
14617e31ba66Smrg    if (pEnt->location.type == BUS_PLATFORM) {
14627e31ba66Smrg        char *syspath =
14637e31ba66Smrg            xf86_platform_device_odev_attributes(pEnt->location.id.plat)->
14647e31ba66Smrg            syspath;
14657e31ba66Smrg
14667e31ba66Smrg        /* Not supported for devices using USB transport due to misbehaved
14677e31ba66Smrg         * vblank events */
14687e31ba66Smrg        if (syspath && strstr(syspath, "usb"))
14697e31ba66Smrg            return FALSE;
14707e31ba66Smrg
14717e31ba66Smrg        /* EVDI uses USB transport but is platform device, not usb.
14725a112b11Smrg         * Exclude it explicitly. */
14737e31ba66Smrg        if (syspath && strstr(syspath, "evdi"))
14747e31ba66Smrg            return FALSE;
14757e31ba66Smrg    }
14767e31ba66Smrg#endif
14777e31ba66Smrg
14787e31ba66Smrg    return drmmode_EnableSharedPixmapFlipping(xf86Crtc, &ms->drmmode,
14797e31ba66Smrg                                              front, back);
14807e31ba66Smrg}
14817e31ba66Smrg
14827e31ba66Smrgstatic void
14837e31ba66SmrgmsDisableSharedPixmapFlipping(RRCrtcPtr crtc)
14847e31ba66Smrg{
14857e31ba66Smrg    ScreenPtr screen = crtc->pScreen;
14867e31ba66Smrg    ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
14877e31ba66Smrg    modesettingPtr ms = modesettingPTR(scrn);
14887e31ba66Smrg    xf86CrtcPtr xf86Crtc = crtc->devPrivate;
14897e31ba66Smrg
14907e31ba66Smrg    if (xf86Crtc)
14917e31ba66Smrg        drmmode_DisableSharedPixmapFlipping(xf86Crtc, &ms->drmmode);
14927e31ba66Smrg}
14937e31ba66Smrg
14947e31ba66Smrgstatic Bool
14957e31ba66SmrgmsStartFlippingPixmapTracking(RRCrtcPtr crtc, DrawablePtr src,
14965a112b11Smrg                              PixmapPtr secondary_dst1, PixmapPtr secondary_dst2,
14977e31ba66Smrg                              int x, int y, int dst_x, int dst_y,
14987e31ba66Smrg                              Rotation rotation)
14997e31ba66Smrg{
15007e31ba66Smrg    ScreenPtr pScreen = src->pScreen;
15017e31ba66Smrg    modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(pScreen));
15027e31ba66Smrg
15035a112b11Smrg    msPixmapPrivPtr ppriv1 = msGetPixmapPriv(&ms->drmmode, secondary_dst1->primary_pixmap),
15045a112b11Smrg                    ppriv2 = msGetPixmapPriv(&ms->drmmode, secondary_dst2->primary_pixmap);
15057e31ba66Smrg
15065a112b11Smrg    if (!PixmapStartDirtyTracking(src, secondary_dst1, x, y,
15077e31ba66Smrg                                  dst_x, dst_y, rotation)) {
15087e31ba66Smrg        return FALSE;
15097e31ba66Smrg    }
15107e31ba66Smrg
15115a112b11Smrg    if (!PixmapStartDirtyTracking(src, secondary_dst2, x, y,
15127e31ba66Smrg                                  dst_x, dst_y, rotation)) {
15135a112b11Smrg        PixmapStopDirtyTracking(src, secondary_dst1);
15147e31ba66Smrg        return FALSE;
15157e31ba66Smrg    }
15167e31ba66Smrg
15175a112b11Smrg    ppriv1->secondary_src = src;
15185a112b11Smrg    ppriv2->secondary_src = src;
15197e31ba66Smrg
15205a112b11Smrg    ppriv1->dirty = ms_dirty_get_ent(pScreen, secondary_dst1);
15215a112b11Smrg    ppriv2->dirty = ms_dirty_get_ent(pScreen, secondary_dst2);
15227e31ba66Smrg
15237e31ba66Smrg    ppriv1->defer_dirty_update = TRUE;
15247e31ba66Smrg    ppriv2->defer_dirty_update = TRUE;
15257e31ba66Smrg
15267e31ba66Smrg    return TRUE;
15277e31ba66Smrg}
15287e31ba66Smrg
15297e31ba66Smrgstatic Bool
15305a112b11SmrgmsPresentSharedPixmap(PixmapPtr secondary_dst)
15317e31ba66Smrg{
15325a112b11Smrg    ScreenPtr pScreen = secondary_dst->primary_pixmap->drawable.pScreen;
15337e31ba66Smrg    modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(pScreen));
15347e31ba66Smrg
15355a112b11Smrg    msPixmapPrivPtr ppriv = msGetPixmapPriv(&ms->drmmode, secondary_dst->primary_pixmap);
15367e31ba66Smrg
15377e31ba66Smrg    RegionPtr region = DamageRegion(ppriv->dirty->damage);
15387e31ba66Smrg
15397e31ba66Smrg    if (RegionNotEmpty(region)) {
15405a112b11Smrg        redisplay_dirty(ppriv->secondary_src->pScreen, ppriv->dirty, NULL);
15417e31ba66Smrg        DamageEmpty(ppriv->dirty->damage);
15427e31ba66Smrg
15437e31ba66Smrg        return TRUE;
15447e31ba66Smrg    }
15457e31ba66Smrg
15467e31ba66Smrg    return FALSE;
15477e31ba66Smrg}
15487e31ba66Smrg
15497e31ba66Smrgstatic Bool
15507e31ba66SmrgmsStopFlippingPixmapTracking(DrawablePtr src,
15515a112b11Smrg                             PixmapPtr secondary_dst1, PixmapPtr secondary_dst2)
15527e31ba66Smrg{
15537e31ba66Smrg    ScreenPtr pScreen = src->pScreen;
15547e31ba66Smrg    modesettingPtr ms = modesettingPTR(xf86ScreenToScrn(pScreen));
15557e31ba66Smrg
15565a112b11Smrg    msPixmapPrivPtr ppriv1 = msGetPixmapPriv(&ms->drmmode, secondary_dst1->primary_pixmap),
15575a112b11Smrg                    ppriv2 = msGetPixmapPriv(&ms->drmmode, secondary_dst2->primary_pixmap);
15587e31ba66Smrg
15597e31ba66Smrg    Bool ret = TRUE;
15607e31ba66Smrg
15615a112b11Smrg    ret &= PixmapStopDirtyTracking(src, secondary_dst1);
15625a112b11Smrg    ret &= PixmapStopDirtyTracking(src, secondary_dst2);
15637e31ba66Smrg
15647e31ba66Smrg    if (ret) {
15655a112b11Smrg        ppriv1->secondary_src = NULL;
15665a112b11Smrg        ppriv2->secondary_src = NULL;
15677e31ba66Smrg
15687e31ba66Smrg        ppriv1->dirty = NULL;
15697e31ba66Smrg        ppriv2->dirty = NULL;
15707e31ba66Smrg
15717e31ba66Smrg        ppriv1->defer_dirty_update = FALSE;
15727e31ba66Smrg        ppriv2->defer_dirty_update = FALSE;
15737e31ba66Smrg    }
15747e31ba66Smrg
15757e31ba66Smrg    return ret;
157635c4bbdfSmrg}
157735c4bbdfSmrg
157835c4bbdfSmrgstatic Bool
157935c4bbdfSmrgCreateScreenResources(ScreenPtr pScreen)
158035c4bbdfSmrg{
158135c4bbdfSmrg    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
158235c4bbdfSmrg    modesettingPtr ms = modesettingPTR(pScrn);
158335c4bbdfSmrg    PixmapPtr rootPixmap;
158435c4bbdfSmrg    Bool ret;
158535c4bbdfSmrg    void *pixels = NULL;
158635c4bbdfSmrg    int err;
158735c4bbdfSmrg
158835c4bbdfSmrg    pScreen->CreateScreenResources = ms->createScreenResources;
158935c4bbdfSmrg    ret = pScreen->CreateScreenResources(pScreen);
159035c4bbdfSmrg    pScreen->CreateScreenResources = CreateScreenResources;
159135c4bbdfSmrg
1592806e81e9Smrg    if (!drmmode_set_desired_modes(pScrn, &ms->drmmode, pScrn->is_gpu, FALSE))
159335c4bbdfSmrg        return FALSE;
159435c4bbdfSmrg
159535c4bbdfSmrg    if (!drmmode_glamor_handle_new_screen_pixmap(&ms->drmmode))
159635c4bbdfSmrg        return FALSE;
159735c4bbdfSmrg
159835c4bbdfSmrg    drmmode_uevent_init(pScrn, &ms->drmmode);
159935c4bbdfSmrg
160035c4bbdfSmrg    if (!ms->drmmode.sw_cursor)
160135c4bbdfSmrg        drmmode_map_cursor_bos(pScrn, &ms->drmmode);
160235c4bbdfSmrg
160335c4bbdfSmrg    if (!ms->drmmode.gbm) {
160435c4bbdfSmrg        pixels = drmmode_map_front_bo(&ms->drmmode);
160535c4bbdfSmrg        if (!pixels)
160635c4bbdfSmrg            return FALSE;
160735c4bbdfSmrg    }
160835c4bbdfSmrg
160935c4bbdfSmrg    rootPixmap = pScreen->GetScreenPixmap(pScreen);
161035c4bbdfSmrg
161135c4bbdfSmrg    if (ms->drmmode.shadow_enable)
161235c4bbdfSmrg        pixels = ms->drmmode.shadow_fb;
161335c4bbdfSmrg
16147e31ba66Smrg    if (ms->drmmode.shadow_enable2) {
16157e31ba66Smrg        ms->drmmode.shadow_fb2 = calloc(1, pScrn->displayWidth * pScrn->virtualY * ((pScrn->bitsPerPixel + 7) >> 3));
16167e31ba66Smrg        if (!ms->drmmode.shadow_fb2)
16177e31ba66Smrg            ms->drmmode.shadow_enable2 = FALSE;
16187e31ba66Smrg    }
16197e31ba66Smrg
162035c4bbdfSmrg    if (!pScreen->ModifyPixmapHeader(rootPixmap, -1, -1, -1, -1, -1, pixels))
162135c4bbdfSmrg        FatalError("Couldn't adjust screen pixmap\n");
162235c4bbdfSmrg
162335c4bbdfSmrg    if (ms->drmmode.shadow_enable) {
16245a112b11Smrg        if (!ms->shadow.Add(pScreen, rootPixmap, msUpdatePacked, msShadowWindow,
16255a112b11Smrg                            0, 0))
162635c4bbdfSmrg            return FALSE;
162735c4bbdfSmrg    }
162835c4bbdfSmrg
162935c4bbdfSmrg    err = drmModeDirtyFB(ms->fd, ms->drmmode.fb_id, NULL, 0);
163035c4bbdfSmrg
163135c4bbdfSmrg    if (err != -EINVAL && err != -ENOSYS) {
163235c4bbdfSmrg        ms->damage = DamageCreate(NULL, NULL, DamageReportNone, TRUE,
163335c4bbdfSmrg                                  pScreen, rootPixmap);
163435c4bbdfSmrg
163535c4bbdfSmrg        if (ms->damage) {
163635c4bbdfSmrg            DamageRegister(&rootPixmap->drawable, ms->damage);
163735c4bbdfSmrg            ms->dirty_enabled = TRUE;
163835c4bbdfSmrg            xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Damage tracking initialized\n");
163935c4bbdfSmrg        }
164035c4bbdfSmrg        else {
164135c4bbdfSmrg            xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
164235c4bbdfSmrg                       "Failed to create screen damage record\n");
164335c4bbdfSmrg            return FALSE;
164435c4bbdfSmrg        }
164535c4bbdfSmrg    }
16467e31ba66Smrg
1647806e81e9Smrg    if (dixPrivateKeyRegistered(rrPrivKey)) {
1648806e81e9Smrg        rrScrPrivPtr pScrPriv = rrGetScrPriv(pScreen);
16497e31ba66Smrg
1650806e81e9Smrg        pScrPriv->rrEnableSharedPixmapFlipping = msEnableSharedPixmapFlipping;
1651806e81e9Smrg        pScrPriv->rrDisableSharedPixmapFlipping = msDisableSharedPixmapFlipping;
1652806e81e9Smrg
1653806e81e9Smrg        pScrPriv->rrStartFlippingPixmapTracking = msStartFlippingPixmapTracking;
1654806e81e9Smrg    }
16557e31ba66Smrg
16565a112b11Smrg    if (ms->vrr_support &&
16575a112b11Smrg        !dixRegisterPrivateKey(&ms->drmmode.vrrPrivateKeyRec,
16585a112b11Smrg                               PRIVATE_WINDOW,
16595a112b11Smrg                               sizeof(struct ms_vrr_priv)))
16605a112b11Smrg            return FALSE;
166135c4bbdfSmrg
16625a112b11Smrg    return ret;
166335c4bbdfSmrg}
166435c4bbdfSmrg
166535c4bbdfSmrgstatic Bool
16665a112b11SmrgmsSharePixmapBacking(PixmapPtr ppix, ScreenPtr secondary, void **handle)
166735c4bbdfSmrg{
166835c4bbdfSmrg#ifdef GLAMOR_HAS_GBM
16695a112b11Smrg    modesettingPtr ms =
16705a112b11Smrg        modesettingPTR(xf86ScreenToScrn(ppix->drawable.pScreen));
167135c4bbdfSmrg    int ret;
167235c4bbdfSmrg    CARD16 stride;
167335c4bbdfSmrg    CARD32 size;
16745a112b11Smrg    ret = ms->glamor.shareable_fd_from_pixmap(ppix->drawable.pScreen, ppix,
16755a112b11Smrg                                              &stride, &size);
167635c4bbdfSmrg    if (ret == -1)
167735c4bbdfSmrg        return FALSE;
167835c4bbdfSmrg
167935c4bbdfSmrg    *handle = (void *)(long)(ret);
168035c4bbdfSmrg    return TRUE;
168135c4bbdfSmrg#endif
168235c4bbdfSmrg    return FALSE;
168335c4bbdfSmrg}
168435c4bbdfSmrg
168535c4bbdfSmrgstatic Bool
168635c4bbdfSmrgmsSetSharedPixmapBacking(PixmapPtr ppix, void *fd_handle)
168735c4bbdfSmrg{
16887e31ba66Smrg#ifdef GLAMOR_HAS_GBM
168935c4bbdfSmrg    ScreenPtr screen = ppix->drawable.pScreen;
169035c4bbdfSmrg    ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
169135c4bbdfSmrg    modesettingPtr ms = modesettingPTR(scrn);
169235c4bbdfSmrg    Bool ret;
169335c4bbdfSmrg    int ihandle = (int) (long) fd_handle;
169435c4bbdfSmrg
16957e31ba66Smrg    if (ihandle == -1)
16967e31ba66Smrg        if (!ms->drmmode.reverse_prime_offload_mode)
16977e31ba66Smrg           return drmmode_SetSlaveBO(ppix, &ms->drmmode, ihandle, 0, 0);
16987e31ba66Smrg
169935c4bbdfSmrg    if (ms->drmmode.reverse_prime_offload_mode) {
17005a112b11Smrg        ret = ms->glamor.back_pixmap_from_fd(ppix, ihandle,
17015a112b11Smrg                                             ppix->drawable.width,
17025a112b11Smrg                                             ppix->drawable.height,
17035a112b11Smrg                                             ppix->devKind,
17045a112b11Smrg                                             ppix->drawable.depth,
17055a112b11Smrg                                             ppix->drawable.bitsPerPixel);
170635c4bbdfSmrg    } else {
170735c4bbdfSmrg        int size = ppix->devKind * ppix->drawable.height;
170835c4bbdfSmrg        ret = drmmode_SetSlaveBO(ppix, &ms->drmmode, ihandle, ppix->devKind, size);
170935c4bbdfSmrg    }
171035c4bbdfSmrg    if (ret == FALSE)
171135c4bbdfSmrg        return ret;
171235c4bbdfSmrg
171335c4bbdfSmrg    return TRUE;
171435c4bbdfSmrg#else
171535c4bbdfSmrg    return FALSE;
171635c4bbdfSmrg#endif
171735c4bbdfSmrg}
171835c4bbdfSmrg
17197e31ba66Smrgstatic Bool
17207e31ba66SmrgmsRequestSharedPixmapNotifyDamage(PixmapPtr ppix)
17217e31ba66Smrg{
17227e31ba66Smrg    ScreenPtr screen = ppix->drawable.pScreen;
17237e31ba66Smrg    ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
17247e31ba66Smrg    modesettingPtr ms = modesettingPTR(scrn);
17257e31ba66Smrg
17265a112b11Smrg    msPixmapPrivPtr ppriv = msGetPixmapPriv(&ms->drmmode, ppix->primary_pixmap);
17277e31ba66Smrg
17287e31ba66Smrg    ppriv->notify_on_damage = TRUE;
17297e31ba66Smrg
17307e31ba66Smrg    return TRUE;
17317e31ba66Smrg}
17327e31ba66Smrg
17337e31ba66Smrgstatic Bool
17347e31ba66SmrgmsSharedPixmapNotifyDamage(PixmapPtr ppix)
17357e31ba66Smrg{
17367e31ba66Smrg    Bool ret = FALSE;
17377e31ba66Smrg    int c;
17387e31ba66Smrg
17397e31ba66Smrg    ScreenPtr screen = ppix->drawable.pScreen;
17407e31ba66Smrg    ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
17417e31ba66Smrg    modesettingPtr ms = modesettingPTR(scrn);
17427e31ba66Smrg    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
17437e31ba66Smrg
17447e31ba66Smrg    msPixmapPrivPtr ppriv = msGetPixmapPriv(&ms->drmmode, ppix);
17457e31ba66Smrg
17467e31ba66Smrg    if (!ppriv->wait_for_damage)
17477e31ba66Smrg        return ret;
17487e31ba66Smrg    ppriv->wait_for_damage = FALSE;
17497e31ba66Smrg
17507e31ba66Smrg    for (c = 0; c < xf86_config->num_crtc; c++) {
17517e31ba66Smrg        xf86CrtcPtr crtc = xf86_config->crtc[c];
17527e31ba66Smrg        drmmode_crtc_private_ptr drmmode_crtc = crtc->driver_private;
17537e31ba66Smrg
17547e31ba66Smrg        if (!drmmode_crtc)
17557e31ba66Smrg            continue;
17567e31ba66Smrg        if (!(drmmode_crtc->prime_pixmap && drmmode_crtc->prime_pixmap_back))
17577e31ba66Smrg            continue;
17587e31ba66Smrg
17595a112b11Smrg        // Received damage on primary screen pixmap, schedule present on vblank
17607e31ba66Smrg        ret |= drmmode_SharedPixmapPresentOnVBlank(ppix, crtc, &ms->drmmode);
17617e31ba66Smrg    }
17627e31ba66Smrg
17637e31ba66Smrg    return ret;
17647e31ba66Smrg}
17657e31ba66Smrg
176635c4bbdfSmrgstatic Bool
176735c4bbdfSmrgSetMaster(ScrnInfoPtr pScrn)
176835c4bbdfSmrg{
176935c4bbdfSmrg    modesettingPtr ms = modesettingPTR(pScrn);
177035c4bbdfSmrg    int ret;
177135c4bbdfSmrg
177235c4bbdfSmrg#ifdef XF86_PDEV_SERVER_FD
177335c4bbdfSmrg    if (ms->pEnt->location.type == BUS_PLATFORM &&
177435c4bbdfSmrg        (ms->pEnt->location.id.plat->flags & XF86_PDEV_SERVER_FD))
177535c4bbdfSmrg        return TRUE;
177635c4bbdfSmrg#endif
177735c4bbdfSmrg
17787e31ba66Smrg    if (ms->fd_passed)
17797e31ba66Smrg        return TRUE;
17807e31ba66Smrg
178135c4bbdfSmrg    ret = drmSetMaster(ms->fd);
178235c4bbdfSmrg    if (ret)
178335c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "drmSetMaster failed: %s\n",
178435c4bbdfSmrg                   strerror(errno));
178535c4bbdfSmrg
178635c4bbdfSmrg    return ret == 0;
178735c4bbdfSmrg}
178835c4bbdfSmrg
17897e31ba66Smrg/* When the root window is created, initialize the screen contents from
17907e31ba66Smrg * console if -background none was specified on the command line
17917e31ba66Smrg */
17927e31ba66Smrgstatic Bool
17937e31ba66SmrgCreateWindow_oneshot(WindowPtr pWin)
17947e31ba66Smrg{
17957e31ba66Smrg    ScreenPtr pScreen = pWin->drawable.pScreen;
17967e31ba66Smrg    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
17977e31ba66Smrg    modesettingPtr ms = modesettingPTR(pScrn);
17987e31ba66Smrg    Bool ret;
17997e31ba66Smrg
18007e31ba66Smrg    pScreen->CreateWindow = ms->CreateWindow;
18017e31ba66Smrg    ret = pScreen->CreateWindow(pWin);
18027e31ba66Smrg
18037e31ba66Smrg    if (ret)
18047e31ba66Smrg        drmmode_copy_fb(pScrn, &ms->drmmode);
18057e31ba66Smrg    return ret;
18067e31ba66Smrg}
18077e31ba66Smrg
180835c4bbdfSmrgstatic Bool
180935c4bbdfSmrgScreenInit(ScreenPtr pScreen, int argc, char **argv)
181035c4bbdfSmrg{
181135c4bbdfSmrg    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
181235c4bbdfSmrg    modesettingPtr ms = modesettingPTR(pScrn);
181335c4bbdfSmrg    VisualPtr visual;
181435c4bbdfSmrg
181535c4bbdfSmrg    pScrn->pScreen = pScreen;
181635c4bbdfSmrg
181735c4bbdfSmrg    if (!SetMaster(pScrn))
181835c4bbdfSmrg        return FALSE;
181935c4bbdfSmrg
182035c4bbdfSmrg#ifdef GLAMOR_HAS_GBM
182135c4bbdfSmrg    if (ms->drmmode.glamor)
18225a112b11Smrg        ms->drmmode.gbm = ms->glamor.egl_get_gbm_device(pScreen);
182335c4bbdfSmrg#endif
182435c4bbdfSmrg
182535c4bbdfSmrg    /* HW dependent - FIXME */
182635c4bbdfSmrg    pScrn->displayWidth = pScrn->virtualX;
182735c4bbdfSmrg    if (!drmmode_create_initial_bos(pScrn, &ms->drmmode))
182835c4bbdfSmrg        return FALSE;
182935c4bbdfSmrg
183035c4bbdfSmrg    if (ms->drmmode.shadow_enable) {
183135c4bbdfSmrg        ms->drmmode.shadow_fb =
183235c4bbdfSmrg            calloc(1,
183335c4bbdfSmrg                   pScrn->displayWidth * pScrn->virtualY *
183435c4bbdfSmrg                   ((pScrn->bitsPerPixel + 7) >> 3));
183535c4bbdfSmrg        if (!ms->drmmode.shadow_fb)
183635c4bbdfSmrg            ms->drmmode.shadow_enable = FALSE;
183735c4bbdfSmrg    }
183835c4bbdfSmrg
183935c4bbdfSmrg    miClearVisualTypes();
184035c4bbdfSmrg
184135c4bbdfSmrg    if (!miSetVisualTypes(pScrn->depth,
184235c4bbdfSmrg                          miGetDefaultVisualMask(pScrn->depth),
184335c4bbdfSmrg                          pScrn->rgbBits, pScrn->defaultVisual))
184435c4bbdfSmrg        return FALSE;
184535c4bbdfSmrg
184635c4bbdfSmrg    if (!miSetPixmapDepths())
184735c4bbdfSmrg        return FALSE;
184835c4bbdfSmrg
184935c4bbdfSmrg    if (!dixRegisterScreenSpecificPrivateKey
185035c4bbdfSmrg        (pScreen, &ms->drmmode.pixmapPrivateKeyRec, PRIVATE_PIXMAP,
185135c4bbdfSmrg         sizeof(msPixmapPrivRec))) {
185235c4bbdfSmrg        return FALSE;
185335c4bbdfSmrg    }
185435c4bbdfSmrg
185535c4bbdfSmrg    pScrn->memPhysBase = 0;
185635c4bbdfSmrg    pScrn->fbOffset = 0;
185735c4bbdfSmrg
185835c4bbdfSmrg    if (!fbScreenInit(pScreen, NULL,
185935c4bbdfSmrg                      pScrn->virtualX, pScrn->virtualY,
186035c4bbdfSmrg                      pScrn->xDpi, pScrn->yDpi,
186135c4bbdfSmrg                      pScrn->displayWidth, pScrn->bitsPerPixel))
186235c4bbdfSmrg        return FALSE;
186335c4bbdfSmrg
186435c4bbdfSmrg    if (pScrn->bitsPerPixel > 8) {
186535c4bbdfSmrg        /* Fixup RGB ordering */
186635c4bbdfSmrg        visual = pScreen->visuals + pScreen->numVisuals;
186735c4bbdfSmrg        while (--visual >= pScreen->visuals) {
186835c4bbdfSmrg            if ((visual->class | DynamicClass) == DirectColor) {
186935c4bbdfSmrg                visual->offsetRed = pScrn->offset.red;
187035c4bbdfSmrg                visual->offsetGreen = pScrn->offset.green;
187135c4bbdfSmrg                visual->offsetBlue = pScrn->offset.blue;
187235c4bbdfSmrg                visual->redMask = pScrn->mask.red;
187335c4bbdfSmrg                visual->greenMask = pScrn->mask.green;
187435c4bbdfSmrg                visual->blueMask = pScrn->mask.blue;
187535c4bbdfSmrg            }
187635c4bbdfSmrg        }
187735c4bbdfSmrg    }
187835c4bbdfSmrg
187935c4bbdfSmrg    fbPictureInit(pScreen, NULL, 0);
188035c4bbdfSmrg
18817e31ba66Smrg    if (drmmode_init(pScrn, &ms->drmmode) == FALSE) {
18827e31ba66Smrg        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
18837e31ba66Smrg                   "Failed to initialize glamor at ScreenInit() time.\n");
18847e31ba66Smrg        return FALSE;
188535c4bbdfSmrg    }
188635c4bbdfSmrg
18875a112b11Smrg    if (ms->drmmode.shadow_enable && !ms->shadow.Setup(pScreen)) {
188835c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "shadow fb init failed\n");
188935c4bbdfSmrg        return FALSE;
189035c4bbdfSmrg    }
189135c4bbdfSmrg
189235c4bbdfSmrg    ms->createScreenResources = pScreen->CreateScreenResources;
189335c4bbdfSmrg    pScreen->CreateScreenResources = CreateScreenResources;
189435c4bbdfSmrg
189535c4bbdfSmrg    xf86SetBlackWhitePixels(pScreen);
189635c4bbdfSmrg
189735c4bbdfSmrg    xf86SetBackingStore(pScreen);
189835c4bbdfSmrg    xf86SetSilkenMouse(pScreen);
189935c4bbdfSmrg    miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
190035c4bbdfSmrg
1901806e81e9Smrg    /* If pageflip is enabled hook the screen's cursor-sprite (swcursor) funcs.
19025a112b11Smrg     * So that we can disable page-flipping on fallback to a swcursor. */
1903806e81e9Smrg    if (ms->drmmode.pageflip) {
1904806e81e9Smrg        miPointerScreenPtr PointPriv =
1905806e81e9Smrg            dixLookupPrivate(&pScreen->devPrivates, miPointerScreenKey);
1906806e81e9Smrg
1907806e81e9Smrg        if (!dixRegisterScreenPrivateKey(&ms->drmmode.spritePrivateKeyRec,
1908806e81e9Smrg                                         pScreen, PRIVATE_DEVICE,
1909806e81e9Smrg                                         sizeof(msSpritePrivRec)))
1910806e81e9Smrg            return FALSE;
1911806e81e9Smrg
1912806e81e9Smrg        ms->SpriteFuncs = PointPriv->spriteFuncs;
1913806e81e9Smrg        PointPriv->spriteFuncs = &drmmode_sprite_funcs;
1914806e81e9Smrg    }
1915806e81e9Smrg
191635c4bbdfSmrg    /* Need to extend HWcursor support to handle mask interleave */
191735c4bbdfSmrg    if (!ms->drmmode.sw_cursor)
191835c4bbdfSmrg        xf86_cursors_init(pScreen, ms->cursor_width, ms->cursor_height,
191935c4bbdfSmrg                          HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64 |
192035c4bbdfSmrg                          HARDWARE_CURSOR_UPDATE_UNHIDDEN |
192135c4bbdfSmrg                          HARDWARE_CURSOR_ARGB);
192235c4bbdfSmrg
192335c4bbdfSmrg    /* Must force it before EnterVT, so we are in control of VT and
192435c4bbdfSmrg     * later memory should be bound when allocating, e.g rotate_mem */
192535c4bbdfSmrg    pScrn->vtSema = TRUE;
192635c4bbdfSmrg
19277e31ba66Smrg    if (serverGeneration == 1 && bgNoneRoot && ms->drmmode.glamor) {
19287e31ba66Smrg        ms->CreateWindow = pScreen->CreateWindow;
19297e31ba66Smrg        pScreen->CreateWindow = CreateWindow_oneshot;
19307e31ba66Smrg    }
19317e31ba66Smrg
193235c4bbdfSmrg    pScreen->SaveScreen = xf86SaveScreen;
193335c4bbdfSmrg    ms->CloseScreen = pScreen->CloseScreen;
193435c4bbdfSmrg    pScreen->CloseScreen = CloseScreen;
193535c4bbdfSmrg
193635c4bbdfSmrg    ms->BlockHandler = pScreen->BlockHandler;
19377e31ba66Smrg    pScreen->BlockHandler = msBlockHandler_oneshot;
193835c4bbdfSmrg
193935c4bbdfSmrg    pScreen->SharePixmapBacking = msSharePixmapBacking;
194035c4bbdfSmrg    pScreen->SetSharedPixmapBacking = msSetSharedPixmapBacking;
194135c4bbdfSmrg    pScreen->StartPixmapTracking = PixmapStartDirtyTracking;
194235c4bbdfSmrg    pScreen->StopPixmapTracking = PixmapStopDirtyTracking;
194335c4bbdfSmrg
19447e31ba66Smrg    pScreen->SharedPixmapNotifyDamage = msSharedPixmapNotifyDamage;
19457e31ba66Smrg    pScreen->RequestSharedPixmapNotifyDamage =
19467e31ba66Smrg        msRequestSharedPixmapNotifyDamage;
19477e31ba66Smrg
19487e31ba66Smrg    pScreen->PresentSharedPixmap = msPresentSharedPixmap;
19497e31ba66Smrg    pScreen->StopFlippingPixmapTracking = msStopFlippingPixmapTracking;
19507e31ba66Smrg
195135c4bbdfSmrg    if (!xf86CrtcScreenInit(pScreen))
195235c4bbdfSmrg        return FALSE;
195335c4bbdfSmrg
19547e31ba66Smrg    if (!drmmode_setup_colormap(pScreen, pScrn))
195535c4bbdfSmrg        return FALSE;
195635c4bbdfSmrg
19577e31ba66Smrg    if (ms->atomic_modeset)
19587e31ba66Smrg        xf86DPMSInit(pScreen, drmmode_set_dpms, 0);
19597e31ba66Smrg    else
19607e31ba66Smrg        xf86DPMSInit(pScreen, xf86DPMSSet, 0);
196135c4bbdfSmrg
19627e31ba66Smrg#ifdef GLAMOR_HAS_GBM
196335c4bbdfSmrg    if (ms->drmmode.glamor) {
196435c4bbdfSmrg        XF86VideoAdaptorPtr     glamor_adaptor;
196535c4bbdfSmrg
19665a112b11Smrg        glamor_adaptor = ms->glamor.xv_init(pScreen, 16);
196735c4bbdfSmrg        if (glamor_adaptor != NULL)
196835c4bbdfSmrg            xf86XVScreenInit(pScreen, &glamor_adaptor, 1);
196935c4bbdfSmrg        else
197035c4bbdfSmrg            xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
197135c4bbdfSmrg                       "Failed to initialize XV support.\n");
197235c4bbdfSmrg    }
197335c4bbdfSmrg#endif
197435c4bbdfSmrg
197535c4bbdfSmrg    if (serverGeneration == 1)
197635c4bbdfSmrg        xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
197735c4bbdfSmrg
197835c4bbdfSmrg    if (!ms_vblank_screen_init(pScreen)) {
197935c4bbdfSmrg        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
198035c4bbdfSmrg                   "Failed to initialize vblank support.\n");
198135c4bbdfSmrg        return FALSE;
198235c4bbdfSmrg    }
198335c4bbdfSmrg
19847e31ba66Smrg#ifdef GLAMOR_HAS_GBM
198535c4bbdfSmrg    if (ms->drmmode.glamor) {
19867e31ba66Smrg        if (!(ms->drmmode.dri2_enable = ms_dri2_screen_init(pScreen))) {
198735c4bbdfSmrg            xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
198835c4bbdfSmrg                       "Failed to initialize the DRI2 extension.\n");
198935c4bbdfSmrg        }
199035c4bbdfSmrg
19917e31ba66Smrg        /* enable reverse prime if we are a GPU screen, and accelerated, and not
1992d44ca368Smrg         * i915, evdi or udl. i915 is happy scanning out from sysmem.
1993d44ca368Smrg         * evdi and udl are virtual drivers scanning out from sysmem
1994d44ca368Smrg         * backed dumb buffers.
1995d44ca368Smrg         */
19967e31ba66Smrg        if (pScreen->isGPU) {
19977e31ba66Smrg            drmVersionPtr version;
19987e31ba66Smrg
19997e31ba66Smrg            /* enable if we are an accelerated GPU screen */
200035c4bbdfSmrg            ms->drmmode.reverse_prime_offload_mode = TRUE;
20017e31ba66Smrg
20027e31ba66Smrg            if ((version = drmGetVersion(ms->drmmode.fd))) {
20037e31ba66Smrg                if (!strncmp("i915", version->name, version->name_len)) {
20047e31ba66Smrg                    ms->drmmode.reverse_prime_offload_mode = FALSE;
20057e31ba66Smrg                }
2006d44ca368Smrg                if (!strncmp("evdi", version->name, version->name_len)) {
2007d44ca368Smrg                    ms->drmmode.reverse_prime_offload_mode = FALSE;
2008d44ca368Smrg                }
2009d44ca368Smrg                if (!strncmp("udl", version->name, version->name_len)) {
2010d44ca368Smrg                    ms->drmmode.reverse_prime_offload_mode = FALSE;
2011d44ca368Smrg                }
2012d44ca368Smrg                if (!ms->drmmode.reverse_prime_offload_mode) {
2013d44ca368Smrg                    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2014d44ca368Smrg                       "Disable reverse prime offload mode for %s.\n", version->name);
2015d44ca368Smrg                }
20167e31ba66Smrg                drmFreeVersion(version);
20177e31ba66Smrg            }
20187e31ba66Smrg        }
201935c4bbdfSmrg    }
202035c4bbdfSmrg#endif
2021d44ca368Smrg    if (!(ms->drmmode.present_enable = ms_present_screen_init(pScreen))) {
2022d44ca368Smrg        xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2023d44ca368Smrg                   "Failed to initialize the Present extension.\n");
2024d44ca368Smrg    }
2025d44ca368Smrg
202635c4bbdfSmrg
20277e31ba66Smrg    pScrn->vtSema = TRUE;
20287e31ba66Smrg
20295a112b11Smrg    if (ms->vrr_support) {
20305a112b11Smrg        if (!property_vectors_wrapped) {
20315a112b11Smrg            saved_change_property = ProcVector[X_ChangeProperty];
20325a112b11Smrg            ProcVector[X_ChangeProperty] = ms_change_property;
20335a112b11Smrg            saved_delete_property = ProcVector[X_DeleteProperty];
20345a112b11Smrg            ProcVector[X_DeleteProperty] = ms_delete_property;
20355a112b11Smrg            property_vectors_wrapped = TRUE;
20365a112b11Smrg        }
20375a112b11Smrg        vrr_atom = MakeAtom("_VARIABLE_REFRESH",
20385a112b11Smrg                             strlen("_VARIABLE_REFRESH"), TRUE);
20395a112b11Smrg    }
20405a112b11Smrg
20417e31ba66Smrg    return TRUE;
204235c4bbdfSmrg}
204335c4bbdfSmrg
204435c4bbdfSmrgstatic void
204535c4bbdfSmrgAdjustFrame(ScrnInfoPtr pScrn, int x, int y)
204635c4bbdfSmrg{
204735c4bbdfSmrg    modesettingPtr ms = modesettingPTR(pScrn);
204835c4bbdfSmrg
204935c4bbdfSmrg    drmmode_adjust_frame(pScrn, &ms->drmmode, x, y);
205035c4bbdfSmrg}
205135c4bbdfSmrg
205235c4bbdfSmrgstatic void
205335c4bbdfSmrgFreeScreen(ScrnInfoPtr pScrn)
205435c4bbdfSmrg{
205535c4bbdfSmrg    FreeRec(pScrn);
205635c4bbdfSmrg}
205735c4bbdfSmrg
205835c4bbdfSmrgstatic void
205935c4bbdfSmrgLeaveVT(ScrnInfoPtr pScrn)
206035c4bbdfSmrg{
206135c4bbdfSmrg    modesettingPtr ms = modesettingPTR(pScrn);
206235c4bbdfSmrg
206335c4bbdfSmrg    xf86_hide_cursors(pScrn);
206435c4bbdfSmrg
206535c4bbdfSmrg    pScrn->vtSema = FALSE;
206635c4bbdfSmrg
206735c4bbdfSmrg#ifdef XF86_PDEV_SERVER_FD
206835c4bbdfSmrg    if (ms->pEnt->location.type == BUS_PLATFORM &&
206935c4bbdfSmrg        (ms->pEnt->location.id.plat->flags & XF86_PDEV_SERVER_FD))
207035c4bbdfSmrg        return;
207135c4bbdfSmrg#endif
207235c4bbdfSmrg
20737e31ba66Smrg    if (!ms->fd_passed)
20747e31ba66Smrg        drmDropMaster(ms->fd);
207535c4bbdfSmrg}
207635c4bbdfSmrg
207735c4bbdfSmrg/*
207835c4bbdfSmrg * This gets called when gaining control of the VT, and from ScreenInit().
207935c4bbdfSmrg */
208035c4bbdfSmrgstatic Bool
208135c4bbdfSmrgEnterVT(ScrnInfoPtr pScrn)
208235c4bbdfSmrg{
208335c4bbdfSmrg    modesettingPtr ms = modesettingPTR(pScrn);
208435c4bbdfSmrg
208535c4bbdfSmrg    pScrn->vtSema = TRUE;
208635c4bbdfSmrg
208735c4bbdfSmrg    SetMaster(pScrn);
208835c4bbdfSmrg
2089806e81e9Smrg    drmmode_update_kms_state(&ms->drmmode);
2090806e81e9Smrg
2091806e81e9Smrg    /* allow not all modes to be set successfully since some events might have
2092806e81e9Smrg     * happened while not being master that could prevent the previous
2093806e81e9Smrg     * configuration from being re-applied.
2094806e81e9Smrg     */
2095806e81e9Smrg    if (!drmmode_set_desired_modes(pScrn, &ms->drmmode, TRUE, TRUE)) {
2096806e81e9Smrg        xf86DisableUnusedFunctions(pScrn);
2097806e81e9Smrg
2098806e81e9Smrg        /* TODO: check that at least one screen is on, to allow the user to fix
2099806e81e9Smrg         * their setup if all modeset failed...
2100806e81e9Smrg         */
2101806e81e9Smrg
2102806e81e9Smrg        /* Tell the desktop environment that something changed, so that they
2103806e81e9Smrg         * can hopefully correct the situation
2104806e81e9Smrg         */
2105806e81e9Smrg        RRSetChanged(xf86ScrnToScreen(pScrn));
2106806e81e9Smrg        RRTellChanged(xf86ScrnToScreen(pScrn));
2107806e81e9Smrg    }
210835c4bbdfSmrg
210935c4bbdfSmrg    return TRUE;
211035c4bbdfSmrg}
211135c4bbdfSmrg
211235c4bbdfSmrgstatic Bool
211335c4bbdfSmrgSwitchMode(ScrnInfoPtr pScrn, DisplayModePtr mode)
211435c4bbdfSmrg{
211535c4bbdfSmrg    return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
211635c4bbdfSmrg}
211735c4bbdfSmrg
211835c4bbdfSmrgstatic Bool
211935c4bbdfSmrgCloseScreen(ScreenPtr pScreen)
212035c4bbdfSmrg{
212135c4bbdfSmrg    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
212235c4bbdfSmrg    modesettingPtr ms = modesettingPTR(pScrn);
212335c4bbdfSmrg    modesettingEntPtr ms_ent = ms_ent_priv(pScrn);
212435c4bbdfSmrg
212535c4bbdfSmrg    /* Clear mask of assigned crtc's in this generation */
212635c4bbdfSmrg    ms_ent->assigned_crtcs = 0;
212735c4bbdfSmrg
21287e31ba66Smrg#ifdef GLAMOR_HAS_GBM
21297e31ba66Smrg    if (ms->drmmode.dri2_enable) {
213035c4bbdfSmrg        ms_dri2_close_screen(pScreen);
213135c4bbdfSmrg    }
213235c4bbdfSmrg#endif
213335c4bbdfSmrg
213435c4bbdfSmrg    ms_vblank_close_screen(pScreen);
213535c4bbdfSmrg
213635c4bbdfSmrg    if (ms->damage) {
213735c4bbdfSmrg        DamageUnregister(ms->damage);
213835c4bbdfSmrg        DamageDestroy(ms->damage);
213935c4bbdfSmrg        ms->damage = NULL;
214035c4bbdfSmrg    }
214135c4bbdfSmrg
214235c4bbdfSmrg    if (ms->drmmode.shadow_enable) {
21435a112b11Smrg        ms->shadow.Remove(pScreen, pScreen->GetScreenPixmap(pScreen));
214435c4bbdfSmrg        free(ms->drmmode.shadow_fb);
214535c4bbdfSmrg        ms->drmmode.shadow_fb = NULL;
21467e31ba66Smrg        free(ms->drmmode.shadow_fb2);
21477e31ba66Smrg        ms->drmmode.shadow_fb2 = NULL;
214835c4bbdfSmrg    }
21497e31ba66Smrg
215035c4bbdfSmrg    drmmode_uevent_fini(pScrn, &ms->drmmode);
215135c4bbdfSmrg
215235c4bbdfSmrg    drmmode_free_bos(pScrn, &ms->drmmode);
215335c4bbdfSmrg
2154806e81e9Smrg    if (ms->drmmode.pageflip) {
2155806e81e9Smrg        miPointerScreenPtr PointPriv =
2156806e81e9Smrg            dixLookupPrivate(&pScreen->devPrivates, miPointerScreenKey);
2157806e81e9Smrg
2158806e81e9Smrg        if (PointPriv->spriteFuncs == &drmmode_sprite_funcs)
2159806e81e9Smrg            PointPriv->spriteFuncs = ms->SpriteFuncs;
2160806e81e9Smrg    }
2161806e81e9Smrg
216235c4bbdfSmrg    if (pScrn->vtSema) {
216335c4bbdfSmrg        LeaveVT(pScrn);
216435c4bbdfSmrg    }
216535c4bbdfSmrg
216635c4bbdfSmrg    pScreen->CreateScreenResources = ms->createScreenResources;
216735c4bbdfSmrg    pScreen->BlockHandler = ms->BlockHandler;
216835c4bbdfSmrg
216935c4bbdfSmrg    pScrn->vtSema = FALSE;
217035c4bbdfSmrg    pScreen->CloseScreen = ms->CloseScreen;
217135c4bbdfSmrg    return (*pScreen->CloseScreen) (pScreen);
217235c4bbdfSmrg}
217335c4bbdfSmrg
217435c4bbdfSmrgstatic ModeStatus
217535c4bbdfSmrgValidMode(ScrnInfoPtr arg, DisplayModePtr mode, Bool verbose, int flags)
217635c4bbdfSmrg{
217735c4bbdfSmrg    return MODE_OK;
217835c4bbdfSmrg}
2179