<commit_before>#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef XF86DRM_MODE
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include "qxl.h"

#include "qxl_surface.h"

Bool qxl_kms_check_cap(qxl_screen_t *qxl, int idx)
{
    int ret;
    struct drm_qxl_clientcap cap;

    cap.index = idx;
    ret = drmIoctl(qxl->drm_fd, DRM_IOCTL_QXL_CLIENTCAP, &cap);
    if (ret == 0)
	return TRUE;
    return FALSE;
}

#if 0
static Bool qxl_kms_getparam(qxl_screen_t *qxl, uint64_t param, uint64_t *value)
{
    int ret;
    struct drm_qxl_getparam args = {0};

    args.param = param;
    ret = drmIoctl(qxl->drm_fd, DRM_IOCTL_QXL_GETPARAM, &args);
    if (ret != 0)
	return FALSE;

    *value = args.value;
    return TRUE;
}
#endif

static Bool qxl_open_drm_master(ScrnInfoPtr pScrn)
{
    qxl_screen_t *qxl = pScrn->driverPrivate;
    struct pci_device *dev = qxl->pci;
    char *busid;
    drmSetVersion sv;
    int err;

#if defined(ODEV_ATTRIB_FD)
    if (qxl->platform_dev) {
        qxl->drm_fd = xf86_get_platform_device_int_attrib(qxl->platform_dev,
                                                          ODEV_ATTRIB_FD, -1);
        if (qxl->drm_fd != -1) {
            qxl->drmmode.fd = qxl->drm_fd;
            return TRUE;
        }
    }
#endif

#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,9,99,901,0)
    XNFasprintf(&busid, "pci:%04x:%02x:%02x.%d",
                dev->domain, dev->bus, dev->dev, dev->func);
#else
    busid = XNFprintf("pci:%04x:%02x:%02x.%d",
		      dev->domain, dev->bus, dev->dev, dev->func);
#endif

    qxl->drm_fd = drmOpen("qxl", busid);
    if (qxl->drm_fd == -1) {

	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "[drm] Failed to open DRM device for %s: %s\n",
		   busid, strerror(errno));
	free(busid);
	return FALSE;
    }
    free(busid);

    /* Check that what we opened was a master or a master-capable FD,
     * by setting the version of the interface we'll use to talk to it.
     * (see DRIOpenDRMMaster() in DRI1)
     */
    sv.drm_di_major = 1;
    sv.drm_di_minor = 1;
    sv.drm_dd_major = -1;
    sv.drm_dd_minor = -1;
    err = drmSetInterfaceVersion(qxl->drm_fd, &sv);
    if (err != 0) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "[drm] failed to set drm interface version.\n");
	drmClose(qxl->drm_fd);
	qxl->drm_fd = -1;

	return FALSE;
    }

    qxl->drmmode.fd = qxl->drm_fd;
    return TRUE;
}

static Bool
qxl_close_screen_kms (CLOSE_SCREEN_ARGS_DECL)
{
    ScrnInfoPtr pScrn = xf86ScreenToScrn (pScreen);
    qxl_screen_t *qxl = pScrn->driverPrivate;
    Bool result;

    qxl_drmmode_uevent_fini(pScrn, &qxl->drmmode);
    pScreen->CloseScreen = qxl->close_screen;

    result = pScreen->CloseScreen (CLOSE_SCREEN_ARGS);

    return result;
}


Bool qxl_pre_init_kms(ScrnInfoPtr pScrn, int flags)
{
    int           scrnIndex = pScrn->scrnIndex;
    qxl_screen_t *qxl = NULL;

    if (!pScrn->confScreen)
	return FALSE;

    /* zaphod mode is for suckers and i choose not to implement it */
    if (xf86IsEntityShared (pScrn->entityList[0]))
    {
	xf86DrvMsg (scrnIndex, X_ERROR, "No Zaphod mode for you\n");
	return FALSE;
    }
    
    if (!pScrn->driverPrivate)
	pScrn->driverPrivate = xnfcalloc (sizeof (qxl_screen_t), 1);

    qxl = pScrn->driverPrivate;
    qxl->device_primary = QXL_DEVICE_PRIMARY_UNDEFINED;
    qxl->pScrn = pScrn;
    qxl->x_modes = NULL;
    qxl->entity = xf86GetEntityInfo (pScrn->entityList[0]);
    qxl->kms_enabled = TRUE;
    xorg_list_init(&qxl->ums_bos);

    qxl_kms_setup_funcs(qxl);
    qxl->pci = xf86GetPciInfoForEntity (qxl->entity->index);

    pScrn->monitor = pScrn->confScreen->monitor;

    if (qxl_open_drm_master(pScrn) == FALSE) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Kernel modesetting setup failed\n");
	goto out;
    }

    if (!qxl_pre_init_common(pScrn))
	goto out;

    xf86SetDpi (pScrn, 0, 0);

    if (!xf86LoadSubModule (pScrn, "fb"))
	goto out;

    if (!xf86LoadSubModule (pScrn, "ramdac"))
	goto out;

    if (drmmode_pre_init(pScrn, &qxl->drmmode, pScrn->bitsPerPixel / 8) == FALSE)
      goto out;

    qxl->virtual_x = 1024;
    qxl->virtual_y = 768;
    
    pScrn->display->virtualX = qxl->virtual_x;
    pScrn->display->virtualY = qxl->virtual_y;

    xf86DrvMsg (scrnIndex, X_INFO, "PreInit complete\n");
#ifdef GIT_VERSION
    xf86DrvMsg (scrnIndex, X_INFO, "git commit %s\n", GIT_VERSION);
#endif

    return TRUE;

 out:
    if (qxl)
      free(qxl);
    return FALSE;
}

static Bool
qxl_create_screen_resources_kms(ScreenPtr pScreen)
{
    ScrnInfoPtr    pScrn = xf86ScreenToScrn (pScreen);
    qxl_screen_t * qxl = pScrn->driverPrivate;<commit_msg>kms: initialize primary surface to screen virtual size<commit_after>#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef XF86DRM_MODE
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include "qxl.h"

#include "qxl_surface.h"

Bool qxl_kms_check_cap(qxl_screen_t *qxl, int idx)
{
    int ret;
    struct drm_qxl_clientcap cap;

    cap.index = idx;
    ret = drmIoctl(qxl->drm_fd, DRM_IOCTL_QXL_CLIENTCAP, &cap);
    if (ret == 0)
	return TRUE;
    return FALSE;
}

#if 0
static Bool qxl_kms_getparam(qxl_screen_t *qxl, uint64_t param, uint64_t *value)
{
    int ret;
    struct drm_qxl_getparam args = {0};

    args.param = param;
    ret = drmIoctl(qxl->drm_fd, DRM_IOCTL_QXL_GETPARAM, &args);
    if (ret != 0)
	return FALSE;

    *value = args.value;
    return TRUE;
}
#endif

static Bool qxl_open_drm_master(ScrnInfoPtr pScrn)
{
    qxl_screen_t *qxl = pScrn->driverPrivate;
    struct pci_device *dev = qxl->pci;
    char *busid;
    drmSetVersion sv;
    int err;

#if defined(ODEV_ATTRIB_FD)
    if (qxl->platform_dev) {
        qxl->drm_fd = xf86_get_platform_device_int_attrib(qxl->platform_dev,
                                                          ODEV_ATTRIB_FD, -1);
        if (qxl->drm_fd != -1) {
            qxl->drmmode.fd = qxl->drm_fd;
            return TRUE;
        }
    }
#endif

#if XORG_VERSION_CURRENT >= XORG_VERSION_NUMERIC(1,9,99,901,0)
    XNFasprintf(&busid, "pci:%04x:%02x:%02x.%d",
                dev->domain, dev->bus, dev->dev, dev->func);
#else
    busid = XNFprintf("pci:%04x:%02x:%02x.%d",
		      dev->domain, dev->bus, dev->dev, dev->func);
#endif

    qxl->drm_fd = drmOpen("qxl", busid);
    if (qxl->drm_fd == -1) {

	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "[drm] Failed to open DRM device for %s: %s\n",
		   busid, strerror(errno));
	free(busid);
	return FALSE;
    }
    free(busid);

    /* Check that what we opened was a master or a master-capable FD,
     * by setting the version of the interface we'll use to talk to it.
     * (see DRIOpenDRMMaster() in DRI1)
     */
    sv.drm_di_major = 1;
    sv.drm_di_minor = 1;
    sv.drm_dd_major = -1;
    sv.drm_dd_minor = -1;
    err = drmSetInterfaceVersion(qxl->drm_fd, &sv);
    if (err != 0) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "[drm] failed to set drm interface version.\n");
	drmClose(qxl->drm_fd);
	qxl->drm_fd = -1;

	return FALSE;
    }

    qxl->drmmode.fd = qxl->drm_fd;
    return TRUE;
}

static Bool
qxl_close_screen_kms (CLOSE_SCREEN_ARGS_DECL)
{
    ScrnInfoPtr pScrn = xf86ScreenToScrn (pScreen);
    qxl_screen_t *qxl = pScrn->driverPrivate;
    Bool result;

    qxl_drmmode_uevent_fini(pScrn, &qxl->drmmode);
    pScreen->CloseScreen = qxl->close_screen;

    result = pScreen->CloseScreen (CLOSE_SCREEN_ARGS);

    return result;
}


Bool qxl_pre_init_kms(ScrnInfoPtr pScrn, int flags)
{
    int           scrnIndex = pScrn->scrnIndex;
    qxl_screen_t *qxl = NULL;

    if (!pScrn->confScreen)
	return FALSE;

    /* zaphod mode is for suckers and i choose not to implement it */
    if (xf86IsEntityShared (pScrn->entityList[0]))
    {
	xf86DrvMsg (scrnIndex, X_ERROR, "No Zaphod mode for you\n");
	return FALSE;
    }
    
    if (!pScrn->driverPrivate)
	pScrn->driverPrivate = xnfcalloc (sizeof (qxl_screen_t), 1);

    qxl = pScrn->driverPrivate;
    qxl->device_primary = QXL_DEVICE_PRIMARY_UNDEFINED;
    qxl->pScrn = pScrn;
    qxl->x_modes = NULL;
    qxl->entity = xf86GetEntityInfo (pScrn->entityList[0]);
    qxl->kms_enabled = TRUE;
    xorg_list_init(&qxl->ums_bos);

    qxl_kms_setup_funcs(qxl);
    qxl->pci = xf86GetPciInfoForEntity (qxl->entity->index);

    pScrn->monitor = pScrn->confScreen->monitor;

    if (qxl_open_drm_master(pScrn) == FALSE) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Kernel modesetting setup failed\n");
	goto out;
    }

    if (!qxl_pre_init_common(pScrn))
	goto out;

    xf86SetDpi (pScrn, 0, 0);

    if (!xf86LoadSubModule (pScrn, "fb"))
	goto out;

    if (!xf86LoadSubModule (pScrn, "ramdac"))
	goto out;

    if (drmmode_pre_init(pScrn, &qxl->drmmode, pScrn->bitsPerPixel / 8) == FALSE)
      goto out;

    qxl->virtual_x = pScrn->virtualX;
    qxl->virtual_y = pScrn->virtualY;
    
    pScrn->display->virtualX = qxl->virtual_x;
    pScrn->display->virtualY = qxl->virtual_y;

    xf86DrvMsg (scrnIndex, X_INFO, "PreInit complete\n");
#ifdef GIT_VERSION
    xf86DrvMsg (scrnIndex, X_INFO, "git commit %s\n", GIT_VERSION);
#endif

    return TRUE;

 out:
    if (qxl)
      free(qxl);
    return FALSE;
}

static Bool
qxl_create_screen_resources_kms(ScreenPtr pScreen)
{
    ScrnInfoPtr    pScrn = xf86ScreenToScrn (pScreen);
    qxl_screen_t * qxl = pScrn->driverPrivate;
{'fraction_non_alphanumeric': 0.1306170715975059, 'fraction_numerical': 0.017093098258439045, 'mean_word_length': 3.127329192546584, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 141, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3655740', 'n_tokens_mistral': 3963, 'n_tokens_neox': 3716, 'n_words': 758}