Add the DCP function driver that binds the VirtIO device to MSM
DRM. Without a function driver, Linux cannot establish HFI queues,
discover firmware capabilities, or register the firmware-owned
display backend.

Set up the DCP DMA and IOMMU identity, initialize VirtIO and HFI
transport, register the MSM DRM component, and synchronize suspend,
resume, remove, and shutdown with in-flight HFI activity.

Assisted-by: LLM
Signed-off-by: Jayant Shekhar <[email protected]>
---
 drivers/gpu/drm/msm/Makefile         |   2 +
 drivers/gpu/drm/msm/dcp/dcp_drv.c    | 193 ++++++++++++++++++++++
 drivers/gpu/drm/msm/dcp/dcp_drv.h    |  15 ++
 drivers/gpu/drm/msm/dcp/dcp_virtio.c | 310 +++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/msm/dcp/dcp_virtio.h |  12 ++
 drivers/gpu/drm/msm/msm_drv.h        |   8 +
 6 files changed, 540 insertions(+)

diff --git a/drivers/gpu/drm/msm/Makefile b/drivers/gpu/drm/msm/Makefile
index 5910891d75f9..d6eeb5d5f681 100644
--- a/drivers/gpu/drm/msm/Makefile
+++ b/drivers/gpu/drm/msm/Makefile
@@ -84,6 +84,8 @@ msm-display-$(CONFIG_DRM_MSM_DCP) += \
        dcp/dcp_hfi_kms.o \
        dcp/dcp_hfi_plane.o \
        dcp/dcp_encoder.o \
+       dcp/dcp_drv.o \
+       dcp/dcp_virtio.o \
        dcp/hfi/hfi_packer.o \
        dcp/hfi/hfi_unpacker.o \
        dcp/hfi/hfi_utils.o
diff --git a/drivers/gpu/drm/msm/dcp/dcp_drv.c 
b/drivers/gpu/drm/msm/dcp/dcp_drv.c
new file mode 100644
index 000000000000..62f38dff06ed
--- /dev/null
+++ b/drivers/gpu/drm/msm/dcp/dcp_drv.c
@@ -0,0 +1,193 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * DCP VirtIO driver.
+ */
+
+#include <linux/component.h>
+#include <linux/device.h>
+#include <linux/dma-mapping.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/panic_notifier.h>
+#include <linux/pm_runtime.h>
+
+#define DCP_DPU_AUTOSUSPEND_DELAY_MS   5000
+
+#include "dcp_drv.h"
+#include "dcp_dpu_kms.h"
+#include "dcp_hfi.h"
+#include "dcp_virtio.h"
+#include "msm_drv.h"
+#include "msm_kms.h"
+
+int dcp_subsystem_dev_init(struct dcp_drv *drv)
+{
+       struct virtio_device *vdev = drv->vdev;
+       struct device_node *dcp_subsystem_node;
+       struct platform_device *pdev;
+       struct device *dev;
+       char busid[32];
+       bool iommu_mapped;
+       int ret;
+
+       dcp_subsystem_node = of_node_get(vdev->dev.of_node);
+       if (!dcp_subsystem_node) {
+               dev_err(&vdev->dev, "no DCP subsystem DT node\n");
+               return -ENODEV;
+       }
+
+       snprintf(busid, sizeof(busid), "display-subsystem.%s",
+                dev_name(vdev->dev.parent));
+
+       /*
+        * DMA APIs accept a generic device, but MSM's component-master 
callbacks
+        * still require a platform device and use its driver data. Create one 
for
+        * the display-subsystem node and configure it with the display 
function's
+        * IOMMU identity for DMA and msm_kms_init_vm().
+        */
+       pdev = of_platform_device_create(dcp_subsystem_node, busid, 
vdev->dev.parent);
+       if (!pdev) {
+               dev_err(&vdev->dev, "dcp_subsystem_dev creation failed\n");
+               of_node_put(dcp_subsystem_node);
+               return -ENODEV;
+       }
+       dev = &pdev->dev;
+
+       dev->dma_parms = &drv->dcp_subsystem_dma_parms;
+       dma_set_max_seg_size(dev, UINT_MAX);
+
+       ret = of_dma_configure_id(dev, dcp_subsystem_node, true, NULL);
+       if (ret) {
+               dev_err(dev, "of_dma_configure_id failed: %d\n", ret);
+               goto err_destroy;
+       }
+
+       iommu_mapped = device_iommu_mapped(dev);
+       if (!iommu_mapped) {
+               dev_err(dev, "no IOMMU group attached\n");
+               ret = -ENODEV;
+               goto err_destroy;
+       }
+
+       dev_dbg(dev, "display-subsystem: iommu_mapped=%d iommu_group=%d\n",
+               iommu_mapped,
+               dev->iommu_group ? iommu_group_id(dev->iommu_group) : -1);
+
+       pm_runtime_set_active(dev);
+       pm_runtime_enable(dev);
+       pm_runtime_set_autosuspend_delay(dev, DCP_DPU_AUTOSUSPEND_DELAY_MS);
+       pm_runtime_use_autosuspend(dev);
+
+       drv->dcp_subsystem_dev = pdev;
+       drv->dcp_subsystem_node = dcp_subsystem_node;
+
+       return 0;
+
+err_destroy:
+       of_platform_device_destroy(dev, NULL);
+       of_node_put(dcp_subsystem_node);
+       return ret;
+}
+
+void dcp_subsystem_dev_deinit(struct dcp_drv *drv)
+{
+       if (!drv->dcp_subsystem_dev)
+               return;
+
+       pm_runtime_disable(&drv->dcp_subsystem_dev->dev);
+       of_platform_device_destroy(&drv->dcp_subsystem_dev->dev, NULL);
+       of_node_put(drv->dcp_subsystem_node);
+       drv->dcp_subsystem_dev = NULL;
+}
+
+int dcp_pm_prepare(struct device *vdev_dev)
+{
+       struct dcp_drv *drv = vdev_dev ?
+               dev_to_virtio(vdev_dev)->priv : NULL;
+
+       if (!drv || !drv->dcp_subsystem_dev)
+               return 0;
+
+       return msm_kms_pm_prepare(&drv->dcp_subsystem_dev->dev);
+}
+
+void dcp_pm_complete(struct device *vdev_dev)
+{
+       struct dcp_drv *drv = vdev_dev ?
+               dev_to_virtio(vdev_dev)->priv : NULL;
+
+       if (!drv || !drv->dcp_subsystem_dev)
+               return;
+
+       msm_kms_pm_complete(&drv->dcp_subsystem_dev->dev);
+}
+
+int dcp_component_register(struct dcp_drv *drv)
+{
+       struct device *dev = &drv->dcp_subsystem_dev->dev;
+       struct component_match *match = NULL;
+       int ret;
+
+       ret = dcp_dpu_kms_init_dsi_bridges(drv->kms, &match);
+       if (ret) {
+               dev_err(dev, "DSI host initialization failed: %d\n", ret);
+               dcp_dpu_kms_cleanup_bridges(drv->kms);
+               return ret;
+       }
+
+       /* A GPU is optional for the DCP display-only DRM device. */
+       ret = msm_add_gpu_components(dev, &match);
+       if (ret)
+               dev_err(dev, "failed to add GPU components: %d\n", ret);
+
+       ret = component_master_add_with_match(dev, &msm_drm_ops, match);
+       if (ret) {
+               dev_err(dev, "component_master_add_with_match failed: %d\n",
+                       ret);
+               dcp_dpu_kms_cleanup_bridges(drv->kms);
+               return ret;
+       }
+
+       return 0;
+}
+
+void dcp_component_unregister(struct dcp_drv *drv)
+{
+       struct device *dev = &drv->dcp_subsystem_dev->dev;
+
+       component_master_del(dev, &msm_drm_ops);
+       dcp_dpu_kms_cleanup_bridges(drv->kms);
+}
+
+static int dcp_panic_notifier_cb(struct notifier_block *nb,
+                                unsigned long action, void *data)
+{
+       struct dcp_drv *drv = container_of(nb, struct dcp_drv, panic_notifier);
+
+       if (drv->hfi)
+               atomic_set(&drv->hfi->shutdown_in_progress, 1);
+
+       return NOTIFY_OK;
+}
+
+void dcp_panic_notifier_register(struct dcp_drv *drv)
+{
+       drv->panic_notifier.notifier_call = dcp_panic_notifier_cb;
+       atomic_notifier_chain_register(&panic_notifier_list, 
&drv->panic_notifier);
+}
+
+void dcp_panic_notifier_unregister(struct dcp_drv *drv)
+{
+       atomic_notifier_chain_unregister(&panic_notifier_list, 
&drv->panic_notifier);
+}
+
+void __init msm_dcp_register(void)
+{
+       dcp_virtio_register();
+}
+
+void __exit msm_dcp_unregister(void)
+{
+       dcp_virtio_unregister();
+}
diff --git a/drivers/gpu/drm/msm/dcp/dcp_drv.h 
b/drivers/gpu/drm/msm/dcp/dcp_drv.h
index 44d9b2cd5171..b19f8027aa3a 100644
--- a/drivers/gpu/drm/msm/dcp/dcp_drv.h
+++ b/drivers/gpu/drm/msm/dcp/dcp_drv.h
@@ -12,7 +12,9 @@
 
 #include "dcp_hfi.h"
 
+struct virtio_device;
 struct dcp_dpu_kms;
+struct component_match;
 
 struct dcp_drv {
        struct virtio_device    *vdev;
@@ -23,6 +25,19 @@ struct dcp_drv {
        struct dcp_dpu_kms      *kms;
        struct dcp_caps          caps;
        struct notifier_block    panic_notifier;
+       bool                     vqs_allocated;
 };
 
+int dcp_subsystem_dev_init(struct dcp_drv *drv);
+void dcp_subsystem_dev_deinit(struct dcp_drv *drv);
+
+int dcp_component_register(struct dcp_drv *drv);
+void dcp_component_unregister(struct dcp_drv *drv);
+
+int dcp_pm_prepare(struct device *vdev_dev);
+void dcp_pm_complete(struct device *vdev_dev);
+
+void dcp_panic_notifier_register(struct dcp_drv *drv);
+void dcp_panic_notifier_unregister(struct dcp_drv *drv);
+
 #endif /* __DCP_DRV_H__ */
diff --git a/drivers/gpu/drm/msm/dcp/dcp_virtio.c 
b/drivers/gpu/drm/msm/dcp/dcp_virtio.c
new file mode 100644
index 000000000000..595e0650103c
--- /dev/null
+++ b/drivers/gpu/drm/msm/dcp/dcp_virtio.c
@@ -0,0 +1,310 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ * DCP VirtIO device driver.
+ */
+
+#include <linux/err.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/pm_runtime.h>
+#include <linux/slab.h>
+#include <linux/virtio.h>
+#include <linux/virtio_config.h>
+#include <linux/virtio_ids.h>
+
+#include "dcp_drv.h"
+#include "dcp_dpu_kms.h"
+#include "dcp_hfi.h"
+#include "dcp_virtio.h"
+#include "msm_drv.h"
+#include "msm_kms.h"
+
+static void dcp_virtio_hfi_cleanup(struct dcp_drv *drv);
+
+static const unsigned int dcp_virtio_features[] = {
+       VIRTIO_F_VERSION_1,
+       VIRTIO_F_NOTIFICATION_DATA,
+       VIRTIO_F_RING_RESET,
+       VIRTIO_F_ACCESS_PLATFORM,
+};
+
+static int dcp_virtio_find_vqs(struct virtio_device *vdev,
+                              struct virtqueue *vqs[DCP_NUM_VQS])
+{
+       struct virtqueue_info vqs_info[DCP_NUM_VQS] = {
+               [DCP_VQ_CMD]  = { .name = "cmd",  .callback = 
dcp_hfi_cmd_callback },
+               [DCP_VQ_RESP] = { .name = "resp", .callback = 
dcp_hfi_resp_callback },
+       };
+
+       return virtio_find_vqs(vdev, DCP_NUM_VQS, vqs, vqs_info, NULL);
+}
+
+/*
+ * The VirtIO core owns transport discovery. This driver owns HFI setup and
+ * creates the DRM component only after firmware capabilities are available.
+ */
+static int dcp_virtio_probe(struct virtio_device *vdev)
+{
+       struct dcp_drv *drv;
+       struct virtqueue *vqs[DCP_NUM_VQS];
+       struct dcp_dpu_kms *dcp_kms;
+       struct msm_drm_private *priv;
+       int ret;
+
+       /* The VENDOR device ID is shared; this host DT node identifies DCP. */
+       if (!vdev->dev.parent ||
+           !of_device_is_compatible(vdev->dev.parent->of_node,
+                                    "qcom,dcp-virtio"))
+               return -ENODEV;
+
+       dev_dbg(&vdev->dev, "DCP VirtIO probe started\n");
+
+       drv = devm_kzalloc(&vdev->dev, sizeof(*drv), GFP_KERNEL);
+       if (!drv)
+               return -ENOMEM;
+
+       drv->vdev = vdev;
+       vdev->priv = drv;
+
+       if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
+               dev_err(&vdev->dev, "VIRTIO_F_VERSION_1 not negotiated\n");
+               return -ENODEV;
+       }
+
+       /* Access-platform is required for the firmware-visible IOVA mappings. 
*/
+       if (!virtio_has_feature(vdev, VIRTIO_F_ACCESS_PLATFORM)) {
+               dev_err(&vdev->dev, "VIRTIO_F_ACCESS_PLATFORM not 
negotiated\n");
+               return -ENODEV;
+       }
+
+       ret = dcp_subsystem_dev_init(drv);
+       if (ret)
+               return ret;
+
+       ret = dcp_virtio_find_vqs(vdev, vqs);
+       if (ret) {
+               dev_err(&vdev->dev, "virtqueue allocation failed: %d\n", ret);
+               goto err_fn_dev;
+       }
+
+       drv->vqs_allocated = true;
+       ret = dcp_hfi_init(drv, vdev, vqs[DCP_VQ_CMD], vqs[DCP_VQ_RESP],
+                          vdev->dev.parent);
+       if (ret) {
+               dev_err(&vdev->dev, "HFI init failed: %d\n", ret);
+               goto err_del_vqs;
+       }
+
+       /*
+        * Keep the device unready until HFI state and virtqueues are 
initialized.
+        * KMS registration below depends on synchronous firmware 
initialization.
+        */
+       virtio_device_ready(vdev);
+       virtqueue_kick(drv->hfi->resp_vq);
+
+       ret = dcp_hfi_send_device_init(drv->hfi);
+       if (ret) {
+               dev_err(&vdev->dev, "HFI DEVICE_INIT failed: %d\n", ret);
+               goto err_hfi_deinit;
+       }
+
+       ret = dcp_hfi_get_capabilities(drv->hfi, &drv->caps);
+       if (ret) {
+               dev_err(&vdev->dev, "HFI GET_CAPS failed: %d\n", ret);
+               goto err_hfi_deinit;
+       }
+
+       dev_dbg(&vdev->dev,
+               "DCP caps: %u displays, max %ux%u, %u planes\n",
+               drv->caps.num_displays, drv->caps.max_width, 
drv->caps.max_height,
+               drv->caps.max_planes);
+
+       dcp_kms = devm_kzalloc(&drv->dcp_subsystem_dev->dev, sizeof(*dcp_kms), 
GFP_KERNEL);
+       if (!dcp_kms) {
+               ret = -ENOMEM;
+               goto err_hfi_deinit;
+       }
+
+       dcp_kms->drv       = drv;
+       dcp_kms->pm_dev    = &drv->dcp_subsystem_dev->dev;
+       dcp_kms->hfi       = drv->hfi;
+       dcp_kms->caps      = &drv->caps;
+       dcp_kms->dpus_np   = drv->dcp_subsystem_node;
+       dcp_kms->iommu_dev = &drv->dcp_subsystem_dev->dev;
+       drv->kms        = dcp_kms;
+
+       ret = msm_kms_init(&dcp_kms->base, &dcp_dpu_kms_funcs);
+       if (ret) {
+               dev_err(&vdev->dev, "msm_kms_init failed: %d\n", ret);
+               goto err_hfi_deinit;
+       }
+
+       priv = devm_kzalloc(&drv->dcp_subsystem_dev->dev, sizeof(*priv), 
GFP_KERNEL);
+       if (!priv) {
+               ret = -ENOMEM;
+               goto err_hfi_deinit;
+       }
+
+       priv->kms      = &dcp_kms->base;
+       priv->kms_init = dcp_kms_init;
+       dev_set_drvdata(&drv->dcp_subsystem_dev->dev, priv);
+       dcp_kms->rpm_enabled = true;
+
+       /* Component binding now sees initialized HFI, capabilities, and IOMMU 
state. */
+       ret = dcp_component_register(drv);
+       if (ret) {
+               dev_err_probe(&vdev->dev, ret, "component register failed\n");
+               goto err_hfi_deinit;
+       }
+
+       dcp_panic_notifier_register(drv);
+
+       dev_dbg(&vdev->dev, "DCP VirtIO probe complete\n");
+       return 0;
+
+err_hfi_deinit:
+       dcp_virtio_hfi_cleanup(drv);
+       goto err_fn_dev;
+err_del_vqs:
+       virtio_reset_device(vdev);
+       vdev->config->del_vqs(vdev);
+       drv->vqs_allocated = false;
+err_fn_dev:
+       dcp_subsystem_dev_deinit(drv);
+       return ret;
+}
+
+static void dcp_virtio_hfi_cleanup(struct dcp_drv *drv)
+{
+       struct virtio_device *vdev = drv->vdev;
+
+       if (!drv->hfi)
+               return;
+
+       dcp_hfi_stop(drv->hfi);
+       virtio_reset_device(vdev);
+       virtio_synchronize_cbs(vdev);
+       if (drv->vqs_allocated) {
+               dcp_hfi_quiesce_queues(drv->hfi);
+               vdev->config->del_vqs(vdev);
+               drv->vqs_allocated = false;
+       }
+       drv->hfi->cmd_vq = NULL;
+       drv->hfi->resp_vq = NULL;
+       dcp_hfi_deinit(drv->hfi);
+}
+
+#ifdef CONFIG_PM_SLEEP
+static int dcp_virtio_freeze(struct virtio_device *vdev)
+{
+       struct dcp_drv *drv = vdev->priv;
+
+       dcp_hfi_stop(drv->hfi);
+       virtio_reset_device(vdev);
+       virtio_synchronize_cbs(vdev);
+       if (drv->vqs_allocated) {
+               dcp_hfi_quiesce_queues(drv->hfi);
+               vdev->config->del_vqs(vdev);
+               drv->vqs_allocated = false;
+       }
+       drv->hfi->cmd_vq = NULL;
+       drv->hfi->resp_vq = NULL;
+
+       return 0;
+}
+
+static int dcp_virtio_restore(struct virtio_device *vdev)
+{
+       struct dcp_drv *drv = vdev->priv;
+       struct virtqueue *vqs[DCP_NUM_VQS];
+       int ret;
+
+       ret = dcp_virtio_find_vqs(vdev, vqs);
+       if (ret)
+               return ret;
+       drv->vqs_allocated = true;
+
+       ret = dcp_hfi_restore_queues(drv->hfi, vqs[DCP_VQ_CMD],
+                                    vqs[DCP_VQ_RESP]);
+       if (ret) {
+               virtio_reset_device(vdev);
+               virtio_synchronize_cbs(vdev);
+               dcp_hfi_quiesce_queues(drv->hfi);
+               drv->vqs_allocated = false;
+               vdev->config->del_vqs(vdev);
+               drv->hfi->cmd_vq = NULL;
+               drv->hfi->resp_vq = NULL;
+               return ret;
+       }
+
+       virtio_device_ready(vdev);
+       virtqueue_kick(vqs[DCP_VQ_RESP]);
+
+       return 0;
+}
+#endif
+
+/* Remove DRM users before tearing down HFI and the VirtIO queues. */
+static void dcp_virtio_remove(struct virtio_device *vdev)
+{
+       struct dcp_drv *drv = vdev->priv;
+
+       dev_dbg(&vdev->dev, "DCP VirtIO remove\n");
+
+       dcp_component_unregister(drv);
+       dcp_panic_notifier_unregister(drv);
+
+       dcp_virtio_hfi_cleanup(drv);
+
+       dcp_subsystem_dev_deinit(drv);
+}
+
+/* KMS shutdown must complete while the firmware transport is available. */
+static void dcp_virtio_shutdown(struct virtio_device *vdev)
+{
+       struct dcp_drv *drv = vdev->priv;
+
+       pm_runtime_get_sync(&drv->dcp_subsystem_dev->dev);
+       msm_kms_shutdown(drv->dcp_subsystem_dev);
+       pm_runtime_put_noidle(&drv->dcp_subsystem_dev->dev);
+}
+
+static const struct virtio_device_id dcp_virtio_id_table[] = {
+       { VIRTIO_ID_VENDOR, VIRTIO_DEV_ANY_ID },
+       { 0 },
+};
+MODULE_DEVICE_TABLE(virtio, dcp_virtio_id_table);
+
+static const struct dev_pm_ops dcp_virtio_pm_ops = {
+       .prepare = dcp_pm_prepare,
+       .complete = dcp_pm_complete,
+};
+
+static struct virtio_driver dcp_virtio_driver = {
+       .driver = {
+               .name  = "qcom-dcp-hfi",
+               .owner = THIS_MODULE,
+               .pm    = &dcp_virtio_pm_ops,
+       },
+       .id_table           = dcp_virtio_id_table,
+       .feature_table      = dcp_virtio_features,
+       .feature_table_size = ARRAY_SIZE(dcp_virtio_features),
+       .probe              = dcp_virtio_probe,
+       .remove             = dcp_virtio_remove,
+       .shutdown           = dcp_virtio_shutdown,
+#ifdef CONFIG_PM_SLEEP
+       .freeze             = dcp_virtio_freeze,
+       .restore            = dcp_virtio_restore,
+#endif
+};
+
+int dcp_virtio_register(void)
+{
+       return register_virtio_driver(&dcp_virtio_driver);
+}
+
+void dcp_virtio_unregister(void)
+{
+       unregister_virtio_driver(&dcp_virtio_driver);
+}
diff --git a/drivers/gpu/drm/msm/dcp/dcp_virtio.h 
b/drivers/gpu/drm/msm/dcp/dcp_virtio.h
new file mode 100644
index 000000000000..c9cda98d4496
--- /dev/null
+++ b/drivers/gpu/drm/msm/dcp/dcp_virtio.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+ */
+
+#ifndef __DCP_VIRTIO_H__
+#define __DCP_VIRTIO_H__
+
+int dcp_virtio_register(void);
+void dcp_virtio_unregister(void);
+
+#endif /* __DCP_VIRTIO_H__ */
diff --git a/drivers/gpu/drm/msm/msm_drv.h b/drivers/gpu/drm/msm/msm_drv.h
index aa5dfd82ac6f..befaa9f2cb5c 100644
--- a/drivers/gpu/drm/msm/msm_drv.h
+++ b/drivers/gpu/drm/msm/msm_drv.h
@@ -417,6 +417,14 @@ static inline void msm_dpu_register(void) {}
 static inline void msm_dpu_unregister(void) {}
 #endif
 
+#ifdef CONFIG_DRM_MSM_DCP
+void msm_dcp_register(void);
+void msm_dcp_unregister(void);
+#else
+static inline void msm_dcp_register(void) {}
+static inline void msm_dcp_unregister(void) {}
+#endif
+
 #ifdef CONFIG_DRM_MSM_MDSS
 void msm_mdss_register(void);
 void msm_mdss_unregister(void);

-- 
2.43.0

Reply via email to