From patchwork Thu Aug 24 08:40:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brandon Ross Pollack X-Patchwork-Id: 13363769 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 79D24C27C40 for ; Thu, 24 Aug 2023 08:44:28 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D34DB10E4F6; Thu, 24 Aug 2023 08:44:27 +0000 (UTC) Received: from mail-oa1-x29.google.com (mail-oa1-x29.google.com [IPv6:2001:4860:4864:20::29]) by gabe.freedesktop.org (Postfix) with ESMTPS id 09A3510E4F2 for ; Thu, 24 Aug 2023 08:44:26 +0000 (UTC) Received: by mail-oa1-x29.google.com with SMTP id 586e51a60fabf-1cc87405650so2587254fac.2 for ; Thu, 24 Aug 2023 01:44:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1692866665; x=1693471465; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9xQIXi7bu5jPEvUoNFU5+d+V/znMEpTNbM7qk9XTFgo=; b=kFgLoYo0FaOPKhs1lmfd5DVoO9TE4PDNE/xQ+frAM88Hdn/OHiAcSwtXJ2oonkAZ5+ aLvg9qXf6lVzVfbvGHdhacDgjeNqj02nRCzW9NbdUA0kG335kP/AiB3B142z0WiyPcZi qL/RX+SXkjbuoowNNxcWj0/1YRMtuK/OGVxwU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692866665; x=1693471465; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9xQIXi7bu5jPEvUoNFU5+d+V/znMEpTNbM7qk9XTFgo=; b=cNRAjoa6mQdjlEkowTmxvoa0CJ0SMm9zcjWOvaVDwzvXpsnA97o5xP/VVMgo76iPaw g3t9ptVQwgxjXdtPiJ7mvq4+JELsWSaD2QUWBuSXrq9ROJDU7oSYr3b0GWvnv84j/urR 0WF541W/ugRKJscQwFnpf/PUPaSD40BqdojKGZOTt+UtqRbb4MjcMkLa5WryttNQTO2A KjiEAuYyQbv+fZln+JK7F6tJpRKfjeJIngqKEqAKLec4VzzihD81zRgNa5Y2RrgV3km1 JdFHic1/r5BBw61zAyjWffBZarVMaW+wv3DyO9DJ6GIJASjSa8FzBqBK24hEKcNZNdCB Q+nA== X-Gm-Message-State: AOJu0Yz6VCfumB9OF3eciIYSDlpc0SPoN5y1iqQpS1Zr6sanEs8EMGfX ur48QkydDKzK43AJk7TJ8cVlvw== X-Google-Smtp-Source: AGHT+IELrnL1C+ITWi0Dxq+NXA4mwtLfs4yEvI0u3Rk6eCKFUZHu/ZH0XBOzaVPkJU2vUPAjuqNFpw== X-Received: by 2002:a05:6870:64aa:b0:1bf:50e0:95d9 with SMTP id cz42-20020a05687064aa00b001bf50e095d9mr18745072oab.26.1692866665238; Thu, 24 Aug 2023 01:44:25 -0700 (PDT) Received: from datalore.c.googlers.com.com (148.175.199.104.bc.googleusercontent.com. [104.199.175.148]) by smtp.gmail.com with ESMTPSA id 12-20020a17090a194c00b00268188ea4b9sm1150216pjh.19.2023.08.24.01.44.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Aug 2023 01:44:24 -0700 (PDT) From: Brandon Pollack To: marius.vlad@collabora.com, jshargo@chromium.org Subject: [PATCH v4 1/7] drm/vkms: Back VKMS with DRM memory management instead of static objects Date: Thu, 24 Aug 2023 08:40:32 +0000 Message-ID: <20230824084350.1062579-2-brpol@chromium.org> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog In-Reply-To: <20230824084350.1062579-1-brpol@chromium.org> References: <20230824084350.1062579-1-brpol@chromium.org> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: hamohammed.sa@gmail.com, rodrigosiqueiramelo@gmail.com, linux-doc@vger.kernel.org, hirono@chromium.org, mduggan@chromium.org, corbet@lwn.net, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, melissa.srw@gmail.com, mairacanal@riseup.net, mripard@kernel.org, tzimmermann@suse.de, Brandon Pollack Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Jim Shargo This is a small refactor to make ConfigFS support easier. Once we support ConfigFS, there can be multiple devices instantiated by the driver, and so moving everything into managed memory makes things much easier. This should be a no-op refactor. Signed-off-by: Jim Shargo Signed-off-by: Brandon Pollack --- drivers/gpu/drm/vkms/vkms_drv.c | 128 +++++++++++++++-------------- drivers/gpu/drm/vkms/vkms_drv.h | 4 +- drivers/gpu/drm/vkms/vkms_output.c | 6 +- 3 files changed, 71 insertions(+), 67 deletions(-) diff --git a/drivers/gpu/drm/vkms/vkms_drv.c b/drivers/gpu/drm/vkms/vkms_drv.c index dd0af086e7fa..387c832f5dc9 100644 --- a/drivers/gpu/drm/vkms/vkms_drv.c +++ b/drivers/gpu/drm/vkms/vkms_drv.c @@ -9,10 +9,12 @@ * the GPU in DRM API tests. */ +#include #include #include #include +#include #include #include #include @@ -37,8 +39,6 @@ #define DRIVER_MAJOR 1 #define DRIVER_MINOR 0 -static struct vkms_config *default_config; - static bool enable_cursor = true; module_param_named(enable_cursor, enable_cursor, bool, 0444); MODULE_PARM_DESC(enable_cursor, "Enable/Disable cursor support"); @@ -96,9 +96,9 @@ static int vkms_config_show(struct seq_file *m, void *data) struct drm_device *dev = entry->dev; struct vkms_device *vkmsdev = drm_device_to_vkms_device(dev); - seq_printf(m, "writeback=%d\n", vkmsdev->config->writeback); - seq_printf(m, "cursor=%d\n", vkmsdev->config->cursor); - seq_printf(m, "overlay=%d\n", vkmsdev->config->overlay); + seq_printf(m, "writeback=%d\n", vkmsdev->config.writeback); + seq_printf(m, "cursor=%d\n", vkmsdev->config.cursor); + seq_printf(m, "overlay=%d\n", vkmsdev->config.overlay); return 0; } @@ -166,121 +166,127 @@ static int vkms_modeset_init(struct vkms_device *vkmsdev) dev->mode_config.cursor_height = 512; /* FIXME: There's a confusion between bpp and depth between this and * fbdev helpers. We have to go with 0, meaning "pick the default", - * which ix XRGB8888 in all cases. */ + * which ix XRGB8888 in all cases. + */ dev->mode_config.preferred_depth = 0; dev->mode_config.helper_private = &vkms_mode_config_helpers; return vkms_output_init(vkmsdev, 0); } -static int vkms_create(struct vkms_config *config) +static int vkms_platform_probe(struct platform_device *pdev) { int ret; - struct platform_device *pdev; struct vkms_device *vkms_device; + void *grp; - pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0); - if (IS_ERR(pdev)) - return PTR_ERR(pdev); - - if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) { - ret = -ENOMEM; - goto out_unregister; - } + grp = devres_open_group(&pdev->dev, NULL, GFP_KERNEL); + if (!grp) + return -ENOMEM; vkms_device = devm_drm_dev_alloc(&pdev->dev, &vkms_driver, struct vkms_device, drm); if (IS_ERR(vkms_device)) { ret = PTR_ERR(vkms_device); - goto out_devres; + goto out_release_group; } + vkms_device->platform = pdev; - vkms_device->config = config; - config->dev = vkms_device; + vkms_device->config.cursor = enable_cursor; + vkms_device->config.writeback = enable_writeback; + vkms_device->config.overlay = enable_overlay; ret = dma_coerce_mask_and_coherent(vkms_device->drm.dev, DMA_BIT_MASK(64)); - if (ret) { DRM_ERROR("Could not initialize DMA support\n"); - goto out_devres; + goto out_release_group; } ret = drm_vblank_init(&vkms_device->drm, 1); if (ret) { DRM_ERROR("Failed to vblank\n"); - goto out_devres; + goto out_release_group; } ret = vkms_modeset_init(vkms_device); - if (ret) - goto out_devres; + if (ret) { + DRM_ERROR("Unable to initialize modesetting\n"); + goto out_release_group; + } drm_debugfs_add_files(&vkms_device->drm, vkms_config_debugfs_list, ARRAY_SIZE(vkms_config_debugfs_list)); ret = drm_dev_register(&vkms_device->drm, 0); - if (ret) - goto out_devres; + if (ret) { + DRM_ERROR("Unable to register device with id %d\n", pdev->id); + goto out_release_group; + } drm_fbdev_generic_setup(&vkms_device->drm, 0); + platform_set_drvdata(pdev, vkms_device); + devres_close_group(&pdev->dev, grp); return 0; -out_devres: - devres_release_group(&pdev->dev, NULL); -out_unregister: - platform_device_unregister(pdev); +out_release_group: + devres_release_group(&pdev->dev, grp); return ret; } -static int __init vkms_init(void) +static int vkms_platform_remove(struct platform_device *pdev) { - int ret; - struct vkms_config *config; - - config = kmalloc(sizeof(*config), GFP_KERNEL); - if (!config) - return -ENOMEM; - - default_config = config; + struct vkms_device *vkms_device; - config->cursor = enable_cursor; - config->writeback = enable_writeback; - config->overlay = enable_overlay; + vkms_device = platform_get_drvdata(pdev); + if (!vkms_device) + return 0; - ret = vkms_create(config); - if (ret) - kfree(config); - - return ret; + drm_dev_unregister(&vkms_device->drm); + drm_atomic_helper_shutdown(&vkms_device->drm); + return 0; } -static void vkms_destroy(struct vkms_config *config) +static struct platform_driver vkms_platform_driver = { + .probe = vkms_platform_probe, + .remove = vkms_platform_remove, + .driver.name = DRIVER_NAME, +}; + +static int __init vkms_init(void) { + int ret; struct platform_device *pdev; - if (!config->dev) { - DRM_INFO("vkms_device is NULL.\n"); - return; + ret = platform_driver_register(&vkms_platform_driver); + if (ret) { + DRM_ERROR("Unable to register platform driver\n"); + return ret; } - pdev = config->dev->platform; - - drm_dev_unregister(&config->dev->drm); - drm_atomic_helper_shutdown(&config->dev->drm); - devres_release_group(&pdev->dev, NULL); - platform_device_unregister(pdev); + pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0); + if (IS_ERR(pdev)) { + platform_driver_unregister(&vkms_platform_driver); + return PTR_ERR(pdev); + } - config->dev = NULL; + return 0; } static void __exit vkms_exit(void) { - if (default_config->dev) - vkms_destroy(default_config); + struct device *dev; + + while ((dev = platform_find_device_by_driver( + NULL, &vkms_platform_driver.driver))) { + // platform_find_device_by_driver increments the refcount. Drop + // it so we don't leak memory. + put_device(dev); + platform_device_unregister(to_platform_device(dev)); + } - kfree(default_config); + platform_driver_unregister(&vkms_platform_driver); } module_init(vkms_init); diff --git a/drivers/gpu/drm/vkms/vkms_drv.h b/drivers/gpu/drm/vkms/vkms_drv.h index c7ae6c2ba1df..4c35d6305f2a 100644 --- a/drivers/gpu/drm/vkms/vkms_drv.h +++ b/drivers/gpu/drm/vkms/vkms_drv.h @@ -124,15 +124,13 @@ struct vkms_config { bool writeback; bool cursor; bool overlay; - /* only set when instantiated */ - struct vkms_device *dev; }; struct vkms_device { struct drm_device drm; struct platform_device *platform; struct vkms_output output; - const struct vkms_config *config; + struct vkms_config config; }; #define drm_crtc_to_vkms_output(target) \ diff --git a/drivers/gpu/drm/vkms/vkms_output.c b/drivers/gpu/drm/vkms/vkms_output.c index 5ce70dd946aa..963a64cf068b 100644 --- a/drivers/gpu/drm/vkms/vkms_output.c +++ b/drivers/gpu/drm/vkms/vkms_output.c @@ -62,7 +62,7 @@ int vkms_output_init(struct vkms_device *vkmsdev, int index) if (IS_ERR(primary)) return PTR_ERR(primary); - if (vkmsdev->config->overlay) { + if (vkmsdev->config.overlay) { for (n = 0; n < NUM_OVERLAY_PLANES; n++) { ret = vkms_add_overlay_plane(vkmsdev, index, crtc); if (ret) @@ -70,7 +70,7 @@ int vkms_output_init(struct vkms_device *vkmsdev, int index) } } - if (vkmsdev->config->cursor) { + if (vkmsdev->config.cursor) { cursor = vkms_plane_init(vkmsdev, DRM_PLANE_TYPE_CURSOR, index); if (IS_ERR(cursor)) return PTR_ERR(cursor); @@ -103,7 +103,7 @@ int vkms_output_init(struct vkms_device *vkmsdev, int index) goto err_attach; } - if (vkmsdev->config->writeback) { + if (vkmsdev->config.writeback) { writeback = vkms_enable_writeback_connector(vkmsdev); if (writeback) DRM_ERROR("Failed to init writeback connector\n");