drm/vc4: Add gem_info node via debugfs for vc5 81/285881/1
authorHoegeun Kwon <hoegeun.kwon@gmail.com>
Mon, 19 Dec 2022 06:57:39 +0000 (15:57 +0900)
committerHoegeun Kwon <hoegeun.kwon@samsung.com>
Thu, 22 Dec 2022 05:15:30 +0000 (14:15 +0900)
The memps requires gem_info with gem_names to analyze graphics shared
memory, so this patch adds gem_info node via common helpers for
debugfs.

Let's save pid/tgid in private file data only once when gem object is
created or prime_fd is imported and use them on gem_info. This is to
solve the wrong pid problem of gem_info created as different processes
share fd of drm device node.

The prime fd created in vc4, but handled in v3d, modified to hanle
this in vc4.

Change-Id: Ideab3ecf282b77247f9b3706ee7e46747b2d0762
Signed-off-by: Hoegeun Kwon <hoegeun.kwon@samsung.com>
drivers/gpu/drm/vc4/vc4_debugfs.c
drivers/gpu/drm/vc4/vc4_drv.c
drivers/gpu/drm/vc4/vc4_drv.h

index 19cda4f..f64eaee 100644 (file)
@@ -26,6 +26,10 @@ vc4_debugfs_init(struct drm_minor *minor)
 
        drm_WARN_ON(drm, vc4_hvs_debugfs_init(minor));
 
+       if (vc4->is_vc5) {
+               drm_WARN_ON(drm, vc5_bo_debugfs_init(minor));
+       }
+
        if (vc4->v3d) {
                drm_WARN_ON(drm, vc4_bo_debugfs_init(minor));
                drm_WARN_ON(drm, vc4_v3d_debugfs_init(minor));
@@ -50,6 +54,76 @@ static int vc4_debugfs_regset32(struct seq_file *m, void *unused)
        return 0;
 }
 
+struct vc5_gem_info_data {
+       struct drm_file *filp;
+       struct seq_file *m;
+};
+
+static int vc5_gem_one_info(int id, void *ptr, void *data)
+{
+
+       struct drm_gem_object *obj = (struct drm_gem_object *)ptr;
+       struct vc5_gem_info_data *gem_info_data = data;
+       struct vc4_file *vc4file = gem_info_data->filp->driver_priv;
+       struct drm_vc5_file_private *file_priv = &vc4file->priv;
+
+       if (!obj) {
+               DRM_ERROR("failed to get drm_gem_object\n");
+               return -EFAULT;
+       }
+
+       drm_gem_object_get(obj);
+
+       seq_printf(gem_info_data->m,
+                       "%5d\t%5d\t%4d\t%4d\t\t%4d\t0x%08zx\t0x%x\t%4d\t%4d\t\t"
+                       "%4d\t\t0x%p\t%6d\n",
+                       file_priv->pid,
+                       file_priv->tgid,
+                       id,
+                       kref_read(&obj->refcount) - 1,
+                       obj->handle_count,
+                       obj->size,
+                       0,
+                       0,
+                       obj->dma_buf ? 1 : 0,
+                       obj->import_attach ? 1 : 0,
+                       obj,
+                       obj->name);
+
+       drm_gem_object_put(obj);
+
+       return 0;
+}
+
+int vc5_debugfs_gem_info(struct seq_file *m, void *data)
+{
+       struct drm_info_node *node = (struct drm_info_node *)m->private;
+       struct drm_minor *minor = node->minor;
+       struct drm_device *drm_dev = minor->dev;
+       struct vc5_gem_info_data gem_info_data;
+       struct drm_file *filp;
+
+       gem_info_data.m = m;
+
+       seq_puts(gem_info_data.m,
+                       "pid\ttgid\thandle\trefcount\thcount\tsize\t\tflags\t"
+                       "pfnmap\texport_to_fd\timport_from_fd\tobj_addr\t\t"
+                       "name\n");
+
+       mutex_lock(&drm_dev->struct_mutex);
+       list_for_each_entry(filp, &drm_dev->filelist, lhead) {
+               gem_info_data.filp = filp;
+
+               spin_lock(&filp->table_lock);
+               idr_for_each(&filp->object_idr, vc5_gem_one_info,
+                               &gem_info_data);
+               spin_unlock(&filp->table_lock);
+       }
+       mutex_unlock(&drm_dev->struct_mutex);
+
+       return 0;
+}
+
 int vc4_debugfs_add_file(struct drm_minor *minor,
                         const char *name,
                         int (*show)(struct seq_file*, void*),
index 93a2cfa..9ada0ef 100644 (file)
@@ -78,6 +78,41 @@ int vc4_dumb_fixup_args(struct drm_mode_create_dumb *args)
        return 0;
 }
 
+static void vc5_gem_register_pid(struct drm_file *file_priv)
+{
+       struct vc4_file *vc4file = file_priv->driver_priv;
+       struct drm_vc5_file_private *driver_priv = &vc4file->priv;
+
+       if (!driver_priv->pid && !driver_priv->tgid) {
+               driver_priv->pid = task_pid_nr(current);
+               driver_priv->tgid = task_tgid_nr(current);
+       } else {
+               if (driver_priv->pid != task_pid_nr(current))
+                       DRM_DEBUG_KMS("wrong pid: %ld, %ld\n",
+                                       (unsigned long)driver_priv->pid,
+                                       (unsigned long)task_pid_nr(current));
+               if (driver_priv->tgid != task_tgid_nr(current))
+                       DRM_DEBUG_KMS("wrong tgid: %ld, %ld\n",
+                                       (unsigned long)driver_priv->tgid,
+                                       (unsigned long)task_tgid_nr(current));
+       }
+}
+
+int vc5_drm_gem_prime_fd_to_handle(struct drm_device *dev,
+                                  struct drm_file *file_priv, int prime_fd,
+                                  uint32_t *handle)
+{
+       int ret;
+
+       ret = drm_gem_prime_fd_to_handle(dev, file_priv, prime_fd, handle);
+       if (ret < 0)
+               return ret;
+
+       vc5_gem_register_pid(file_priv);
+
+       return ret;
+}
+
 static int vc4_dumb_create(struct drm_file *file_priv,
                           struct drm_device *dev,
                           struct drm_mode_create_dumb *args)
@@ -88,7 +123,11 @@ static int vc4_dumb_create(struct drm_file *file_priv,
        if (ret)
                return ret;
 
-       return drm_gem_cma_dumb_create_internal(file_priv, dev, args);
+       ret = drm_gem_cma_dumb_create_internal(file_priv, dev, args);
+       if (!ret)
+               vc5_gem_register_pid(file_priv);
+
+       return ret;
 }
 
 static int vc4_get_param_ioctl(struct drm_device *dev, void *data,
@@ -178,6 +217,27 @@ static void vc4_close(struct drm_device *dev, struct drm_file *file)
        kfree(vc4file);
 }
 
+static int vc5_open(struct drm_device *dev, struct drm_file *file)
+{
+       struct vc4_dev *vc4 = to_vc4_dev(dev);
+       struct vc4_file *vc4file;
+
+       vc4file = kzalloc(sizeof(*vc4file), GFP_KERNEL);
+       if (!vc4file)
+               return -ENOMEM;
+       vc4file->dev = vc4;
+
+       file->driver_priv = vc4file;
+       return 0;
+}
+
+static void vc5_close(struct drm_device *dev, struct drm_file *file)
+{
+       struct vc4_file *vc4file = file->driver_priv;
+
+       kfree(vc4file);
+}
+
 DEFINE_DRM_GEM_FOPS(vc4_drm_fops);
 
 static const struct drm_ioctl_desc vc4_drm_ioctls[] = {
@@ -233,12 +293,15 @@ static const struct drm_driver vc5_drm_driver = {
                            DRIVER_ATOMIC |
                            DRIVER_GEM),
 
+       .open = vc5_open,
+       .postclose = vc5_close,
 #if defined(CONFIG_DEBUG_FS)
        .debugfs_init = vc4_debugfs_init,
 #endif
 
        DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(vc4_dumb_create),
 
+       .prime_fd_to_handle = vc5_drm_gem_prime_fd_to_handle,
        .fops = &vc4_drm_fops,
 
        .name = DRIVER_NAME,
@@ -303,6 +366,23 @@ static bool firmware_kms(void)
               "raspberrypi,rpi-firmware-kms-2711"));
 }
 
+int vc5_bo_debugfs_init(struct drm_minor *minor)
+{
+       struct drm_device *drm = minor->dev;
+       struct vc4_dev *vc4 = to_vc4_dev(drm);
+       int ret;
+
+       if (!vc4->is_vc5)
+               return -ENODEV;
+
+       ret = vc4_debugfs_add_file(minor, "gem_info",
+                                  vc5_debugfs_gem_info, NULL);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+
 static int vc4_drm_bind(struct device *dev)
 {
        struct platform_device *pdev = to_platform_device(dev);
index 364f9c0..7304d74 100644 (file)
@@ -739,6 +739,11 @@ struct vc4_exec_info {
        bool bin_bo_used;
 };
 
+struct drm_vc5_file_private {
+       pid_t pid;
+       pid_t tgid;
+};
+
 /* Per-open file private data. Any driver-specific resource that has to be
  * released when the DRM file is closed should be placed here.
  */
@@ -751,6 +756,7 @@ struct vc4_file {
        } perfmon;
 
        bool bin_bo_used;
+       struct drm_vc5_file_private priv;
 };
 
 static inline struct vc4_exec_info *
@@ -881,6 +887,7 @@ void vc4_bo_dec_usecnt(struct vc4_bo *bo);
 void vc4_bo_add_to_purgeable_pool(struct vc4_bo *bo);
 void vc4_bo_remove_from_purgeable_pool(struct vc4_bo *bo);
 int vc4_bo_debugfs_init(struct drm_minor *minor);
+int vc5_bo_debugfs_init(struct drm_minor *minor);
 
 /* vc4_crtc.c */
 extern struct platform_driver vc4_crtc_driver;
@@ -1045,6 +1052,8 @@ bool vc4_check_tex_size(struct vc4_exec_info *exec,
 struct vc4_validated_shader_info *
 vc4_validate_shader(struct drm_gem_cma_object *shader_obj);
 
+int vc5_debugfs_gem_info(struct seq_file *m, void *data);
+
 /* vc4_perfmon.c */
 void vc4_perfmon_get(struct vc4_perfmon *perfmon);
 void vc4_perfmon_put(struct vc4_perfmon *perfmon);