18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Sync File validation framework and debug information
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Google, Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
98c2ecf20Sopenharmony_ci#include "sync_debug.h"
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_cistatic struct dentry *dbgfs;
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistatic LIST_HEAD(sync_timeline_list_head);
148c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(sync_timeline_list_lock);
158c2ecf20Sopenharmony_cistatic LIST_HEAD(sync_file_list_head);
168c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(sync_file_list_lock);
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_civoid sync_timeline_debug_add(struct sync_timeline *obj)
198c2ecf20Sopenharmony_ci{
208c2ecf20Sopenharmony_ci	unsigned long flags;
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci	spin_lock_irqsave(&sync_timeline_list_lock, flags);
238c2ecf20Sopenharmony_ci	list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
248c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
258c2ecf20Sopenharmony_ci}
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_civoid sync_timeline_debug_remove(struct sync_timeline *obj)
288c2ecf20Sopenharmony_ci{
298c2ecf20Sopenharmony_ci	unsigned long flags;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	spin_lock_irqsave(&sync_timeline_list_lock, flags);
328c2ecf20Sopenharmony_ci	list_del(&obj->sync_timeline_list);
338c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
348c2ecf20Sopenharmony_ci}
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_civoid sync_file_debug_add(struct sync_file *sync_file)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	unsigned long flags;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	spin_lock_irqsave(&sync_file_list_lock, flags);
418c2ecf20Sopenharmony_ci	list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
428c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&sync_file_list_lock, flags);
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_civoid sync_file_debug_remove(struct sync_file *sync_file)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	unsigned long flags;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	spin_lock_irqsave(&sync_file_list_lock, flags);
508c2ecf20Sopenharmony_ci	list_del(&sync_file->sync_file_list);
518c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&sync_file_list_lock, flags);
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic const char *sync_status_str(int status)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	if (status < 0)
578c2ecf20Sopenharmony_ci		return "error";
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if (status > 0)
608c2ecf20Sopenharmony_ci		return "signaled";
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	return "active";
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic void sync_print_fence(struct seq_file *s,
668c2ecf20Sopenharmony_ci			     struct dma_fence *fence, bool show)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	struct sync_timeline *parent = dma_fence_parent(fence);
698c2ecf20Sopenharmony_ci	int status;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	status = dma_fence_get_status_locked(fence);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	seq_printf(s, "  %s%sfence %s",
748c2ecf20Sopenharmony_ci		   show ? parent->name : "",
758c2ecf20Sopenharmony_ci		   show ? "_" : "",
768c2ecf20Sopenharmony_ci		   sync_status_str(status));
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
798c2ecf20Sopenharmony_ci		struct timespec64 ts64 =
808c2ecf20Sopenharmony_ci			ktime_to_timespec64(fence->timestamp);
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci		seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
838c2ecf20Sopenharmony_ci	}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	if (fence->ops->timeline_value_str &&
868c2ecf20Sopenharmony_ci		fence->ops->fence_value_str) {
878c2ecf20Sopenharmony_ci		char value[64];
888c2ecf20Sopenharmony_ci		bool success;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci		fence->ops->fence_value_str(fence, value, sizeof(value));
918c2ecf20Sopenharmony_ci		success = strlen(value);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci		if (success) {
948c2ecf20Sopenharmony_ci			seq_printf(s, ": %s", value);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci			fence->ops->timeline_value_str(fence, value,
978c2ecf20Sopenharmony_ci						       sizeof(value));
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci			if (strlen(value))
1008c2ecf20Sopenharmony_ci				seq_printf(s, " / %s", value);
1018c2ecf20Sopenharmony_ci		}
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	seq_putc(s, '\n');
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	struct list_head *pos;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	seq_printf(s, "%s: %d\n", obj->name, obj->value);
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	spin_lock(&obj->lock); /* Caller already disabled IRQ. */
1148c2ecf20Sopenharmony_ci	list_for_each(pos, &obj->pt_list) {
1158c2ecf20Sopenharmony_ci		struct sync_pt *pt = container_of(pos, struct sync_pt, link);
1168c2ecf20Sopenharmony_ci		sync_print_fence(s, &pt->base, false);
1178c2ecf20Sopenharmony_ci	}
1188c2ecf20Sopenharmony_ci	spin_unlock(&obj->lock);
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic void sync_print_sync_file(struct seq_file *s,
1228c2ecf20Sopenharmony_ci				  struct sync_file *sync_file)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	char buf[128];
1258c2ecf20Sopenharmony_ci	int i;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	seq_printf(s, "[%p] %s: %s\n", sync_file,
1288c2ecf20Sopenharmony_ci		   sync_file_get_name(sync_file, buf, sizeof(buf)),
1298c2ecf20Sopenharmony_ci		   sync_status_str(dma_fence_get_status(sync_file->fence)));
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	if (dma_fence_is_array(sync_file->fence)) {
1328c2ecf20Sopenharmony_ci		struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci		for (i = 0; i < array->num_fences; ++i)
1358c2ecf20Sopenharmony_ci			sync_print_fence(s, array->fences[i], true);
1368c2ecf20Sopenharmony_ci	} else {
1378c2ecf20Sopenharmony_ci		sync_print_fence(s, sync_file->fence, true);
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic int sync_info_debugfs_show(struct seq_file *s, void *unused)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	struct list_head *pos;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	seq_puts(s, "objs:\n--------------\n");
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	spin_lock_irq(&sync_timeline_list_lock);
1488c2ecf20Sopenharmony_ci	list_for_each(pos, &sync_timeline_list_head) {
1498c2ecf20Sopenharmony_ci		struct sync_timeline *obj =
1508c2ecf20Sopenharmony_ci			container_of(pos, struct sync_timeline,
1518c2ecf20Sopenharmony_ci				     sync_timeline_list);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci		sync_print_obj(s, obj);
1548c2ecf20Sopenharmony_ci		seq_putc(s, '\n');
1558c2ecf20Sopenharmony_ci	}
1568c2ecf20Sopenharmony_ci	spin_unlock_irq(&sync_timeline_list_lock);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	seq_puts(s, "fences:\n--------------\n");
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	spin_lock_irq(&sync_file_list_lock);
1618c2ecf20Sopenharmony_ci	list_for_each(pos, &sync_file_list_head) {
1628c2ecf20Sopenharmony_ci		struct sync_file *sync_file =
1638c2ecf20Sopenharmony_ci			container_of(pos, struct sync_file, sync_file_list);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci		sync_print_sync_file(s, sync_file);
1668c2ecf20Sopenharmony_ci		seq_putc(s, '\n');
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci	spin_unlock_irq(&sync_file_list_lock);
1698c2ecf20Sopenharmony_ci	return 0;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic __init int sync_debugfs_init(void)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	dbgfs = debugfs_create_dir("sync", NULL);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	/*
1798c2ecf20Sopenharmony_ci	 * The debugfs files won't ever get removed and thus, there is
1808c2ecf20Sopenharmony_ci	 * no need to protect it against removal races. The use of
1818c2ecf20Sopenharmony_ci	 * debugfs_create_file_unsafe() is actually safe here.
1828c2ecf20Sopenharmony_ci	 */
1838c2ecf20Sopenharmony_ci	debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
1848c2ecf20Sopenharmony_ci				   &sync_info_debugfs_fops);
1858c2ecf20Sopenharmony_ci	debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
1868c2ecf20Sopenharmony_ci				   &sw_sync_debugfs_fops);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	return 0;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_cilate_initcall(sync_debugfs_init);
191