19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
235538d78SGustavo Padovan /*
335538d78SGustavo Padovan * Sync File validation framework and debug information
435538d78SGustavo Padovan *
535538d78SGustavo Padovan * Copyright (C) 2012 Google, Inc.
635538d78SGustavo Padovan */
735538d78SGustavo Padovan
835538d78SGustavo Padovan #include <linux/debugfs.h>
935538d78SGustavo Padovan #include "sync_debug.h"
1035538d78SGustavo Padovan
1135538d78SGustavo Padovan static struct dentry *dbgfs;
1235538d78SGustavo Padovan
1335538d78SGustavo Padovan static LIST_HEAD(sync_timeline_list_head);
1435538d78SGustavo Padovan static DEFINE_SPINLOCK(sync_timeline_list_lock);
1535538d78SGustavo Padovan static LIST_HEAD(sync_file_list_head);
1635538d78SGustavo Padovan static DEFINE_SPINLOCK(sync_file_list_lock);
1735538d78SGustavo Padovan
sync_timeline_debug_add(struct sync_timeline * obj)1835538d78SGustavo Padovan void sync_timeline_debug_add(struct sync_timeline *obj)
1935538d78SGustavo Padovan {
2035538d78SGustavo Padovan unsigned long flags;
2135538d78SGustavo Padovan
2235538d78SGustavo Padovan spin_lock_irqsave(&sync_timeline_list_lock, flags);
2335538d78SGustavo Padovan list_add_tail(&obj->sync_timeline_list, &sync_timeline_list_head);
2435538d78SGustavo Padovan spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
2535538d78SGustavo Padovan }
2635538d78SGustavo Padovan
sync_timeline_debug_remove(struct sync_timeline * obj)2735538d78SGustavo Padovan void sync_timeline_debug_remove(struct sync_timeline *obj)
2835538d78SGustavo Padovan {
2935538d78SGustavo Padovan unsigned long flags;
3035538d78SGustavo Padovan
3135538d78SGustavo Padovan spin_lock_irqsave(&sync_timeline_list_lock, flags);
3235538d78SGustavo Padovan list_del(&obj->sync_timeline_list);
3335538d78SGustavo Padovan spin_unlock_irqrestore(&sync_timeline_list_lock, flags);
3435538d78SGustavo Padovan }
3535538d78SGustavo Padovan
sync_file_debug_add(struct sync_file * sync_file)3635538d78SGustavo Padovan void sync_file_debug_add(struct sync_file *sync_file)
3735538d78SGustavo Padovan {
3835538d78SGustavo Padovan unsigned long flags;
3935538d78SGustavo Padovan
4035538d78SGustavo Padovan spin_lock_irqsave(&sync_file_list_lock, flags);
4135538d78SGustavo Padovan list_add_tail(&sync_file->sync_file_list, &sync_file_list_head);
4235538d78SGustavo Padovan spin_unlock_irqrestore(&sync_file_list_lock, flags);
4335538d78SGustavo Padovan }
4435538d78SGustavo Padovan
sync_file_debug_remove(struct sync_file * sync_file)4535538d78SGustavo Padovan void sync_file_debug_remove(struct sync_file *sync_file)
4635538d78SGustavo Padovan {
4735538d78SGustavo Padovan unsigned long flags;
4835538d78SGustavo Padovan
4935538d78SGustavo Padovan spin_lock_irqsave(&sync_file_list_lock, flags);
5035538d78SGustavo Padovan list_del(&sync_file->sync_file_list);
5135538d78SGustavo Padovan spin_unlock_irqrestore(&sync_file_list_lock, flags);
5235538d78SGustavo Padovan }
5335538d78SGustavo Padovan
sync_status_str(int status)5435538d78SGustavo Padovan static const char *sync_status_str(int status)
5535538d78SGustavo Padovan {
56d6c99f4bSChris Wilson if (status < 0)
57d6c99f4bSChris Wilson return "error";
5835538d78SGustavo Padovan
5935538d78SGustavo Padovan if (status > 0)
60d6c99f4bSChris Wilson return "signaled";
6135538d78SGustavo Padovan
62d6c99f4bSChris Wilson return "active";
6335538d78SGustavo Padovan }
6435538d78SGustavo Padovan
sync_print_fence(struct seq_file * s,struct dma_fence * fence,bool show)65f54d1867SChris Wilson static void sync_print_fence(struct seq_file *s,
66f54d1867SChris Wilson struct dma_fence *fence, bool show)
6735538d78SGustavo Padovan {
68f54d1867SChris Wilson struct sync_timeline *parent = dma_fence_parent(fence);
69d6c99f4bSChris Wilson int status;
7035538d78SGustavo Padovan
71d6c99f4bSChris Wilson status = dma_fence_get_status_locked(fence);
7235538d78SGustavo Padovan
7335538d78SGustavo Padovan seq_printf(s, " %s%sfence %s",
7435538d78SGustavo Padovan show ? parent->name : "",
7535538d78SGustavo Padovan show ? "_" : "",
7635538d78SGustavo Padovan sync_status_str(status));
7735538d78SGustavo Padovan
7876250f2bSChris Wilson if (test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) {
7935538d78SGustavo Padovan struct timespec64 ts64 =
8035538d78SGustavo Padovan ktime_to_timespec64(fence->timestamp);
8135538d78SGustavo Padovan
8235538d78SGustavo Padovan seq_printf(s, "@%lld.%09ld", (s64)ts64.tv_sec, ts64.tv_nsec);
8335538d78SGustavo Padovan }
8435538d78SGustavo Padovan
8535538d78SGustavo Padovan if (fence->ops->timeline_value_str &&
8635538d78SGustavo Padovan fence->ops->fence_value_str) {
8735538d78SGustavo Padovan char value[64];
8835538d78SGustavo Padovan bool success;
8935538d78SGustavo Padovan
9035538d78SGustavo Padovan fence->ops->fence_value_str(fence, value, sizeof(value));
9135538d78SGustavo Padovan success = strlen(value);
9235538d78SGustavo Padovan
9335538d78SGustavo Padovan if (success) {
9435538d78SGustavo Padovan seq_printf(s, ": %s", value);
9535538d78SGustavo Padovan
9635538d78SGustavo Padovan fence->ops->timeline_value_str(fence, value,
9735538d78SGustavo Padovan sizeof(value));
9835538d78SGustavo Padovan
9935538d78SGustavo Padovan if (strlen(value))
10035538d78SGustavo Padovan seq_printf(s, " / %s", value);
10135538d78SGustavo Padovan }
10235538d78SGustavo Padovan }
10335538d78SGustavo Padovan
10447a369deSMarkus Elfring seq_putc(s, '\n');
10535538d78SGustavo Padovan }
10635538d78SGustavo Padovan
sync_print_obj(struct seq_file * s,struct sync_timeline * obj)10735538d78SGustavo Padovan static void sync_print_obj(struct seq_file *s, struct sync_timeline *obj)
10835538d78SGustavo Padovan {
10935538d78SGustavo Padovan struct list_head *pos;
11035538d78SGustavo Padovan
11135538d78SGustavo Padovan seq_printf(s, "%s: %d\n", obj->name, obj->value);
11235538d78SGustavo Padovan
113*b7949189STetsuo Handa spin_lock(&obj->lock); /* Caller already disabled IRQ. */
114d3862e44SChris Wilson list_for_each(pos, &obj->pt_list) {
115d3862e44SChris Wilson struct sync_pt *pt = container_of(pos, struct sync_pt, link);
11635538d78SGustavo Padovan sync_print_fence(s, &pt->base, false);
11735538d78SGustavo Padovan }
118*b7949189STetsuo Handa spin_unlock(&obj->lock);
11935538d78SGustavo Padovan }
12035538d78SGustavo Padovan
sync_print_sync_file(struct seq_file * s,struct sync_file * sync_file)12135538d78SGustavo Padovan static void sync_print_sync_file(struct seq_file *s,
12235538d78SGustavo Padovan struct sync_file *sync_file)
12335538d78SGustavo Padovan {
12471ebc9a3SChris Wilson char buf[128];
12535538d78SGustavo Padovan int i;
12635538d78SGustavo Padovan
12771ebc9a3SChris Wilson seq_printf(s, "[%p] %s: %s\n", sync_file,
12871ebc9a3SChris Wilson sync_file_get_name(sync_file, buf, sizeof(buf)),
129d6c99f4bSChris Wilson sync_status_str(dma_fence_get_status(sync_file->fence)));
13035538d78SGustavo Padovan
131f54d1867SChris Wilson if (dma_fence_is_array(sync_file->fence)) {
132f54d1867SChris Wilson struct dma_fence_array *array = to_dma_fence_array(sync_file->fence);
1336b25e21fSLinus Torvalds
1346b25e21fSLinus Torvalds for (i = 0; i < array->num_fences; ++i)
1356b25e21fSLinus Torvalds sync_print_fence(s, array->fences[i], true);
1366b25e21fSLinus Torvalds } else {
1376b25e21fSLinus Torvalds sync_print_fence(s, sync_file->fence, true);
1386b25e21fSLinus Torvalds }
13935538d78SGustavo Padovan }
14035538d78SGustavo Padovan
sync_info_debugfs_show(struct seq_file * s,void * unused)1412674305aSYangtao Li static int sync_info_debugfs_show(struct seq_file *s, void *unused)
14235538d78SGustavo Padovan {
14335538d78SGustavo Padovan struct list_head *pos;
14435538d78SGustavo Padovan
14535538d78SGustavo Padovan seq_puts(s, "objs:\n--------------\n");
14635538d78SGustavo Padovan
147a6aa8fcaSChris Wilson spin_lock_irq(&sync_timeline_list_lock);
14835538d78SGustavo Padovan list_for_each(pos, &sync_timeline_list_head) {
14935538d78SGustavo Padovan struct sync_timeline *obj =
15035538d78SGustavo Padovan container_of(pos, struct sync_timeline,
15135538d78SGustavo Padovan sync_timeline_list);
15235538d78SGustavo Padovan
15335538d78SGustavo Padovan sync_print_obj(s, obj);
15447a369deSMarkus Elfring seq_putc(s, '\n');
15535538d78SGustavo Padovan }
156a6aa8fcaSChris Wilson spin_unlock_irq(&sync_timeline_list_lock);
15735538d78SGustavo Padovan
15835538d78SGustavo Padovan seq_puts(s, "fences:\n--------------\n");
15935538d78SGustavo Padovan
160a6aa8fcaSChris Wilson spin_lock_irq(&sync_file_list_lock);
16135538d78SGustavo Padovan list_for_each(pos, &sync_file_list_head) {
16235538d78SGustavo Padovan struct sync_file *sync_file =
16335538d78SGustavo Padovan container_of(pos, struct sync_file, sync_file_list);
16435538d78SGustavo Padovan
16535538d78SGustavo Padovan sync_print_sync_file(s, sync_file);
16647a369deSMarkus Elfring seq_putc(s, '\n');
16735538d78SGustavo Padovan }
168a6aa8fcaSChris Wilson spin_unlock_irq(&sync_file_list_lock);
16935538d78SGustavo Padovan return 0;
17035538d78SGustavo Padovan }
17135538d78SGustavo Padovan
1722674305aSYangtao Li DEFINE_SHOW_ATTRIBUTE(sync_info_debugfs);
17335538d78SGustavo Padovan
sync_debugfs_init(void)17435538d78SGustavo Padovan static __init int sync_debugfs_init(void)
17535538d78SGustavo Padovan {
17635538d78SGustavo Padovan dbgfs = debugfs_create_dir("sync", NULL);
17735538d78SGustavo Padovan
17835538d78SGustavo Padovan /*
17935538d78SGustavo Padovan * The debugfs files won't ever get removed and thus, there is
18035538d78SGustavo Padovan * no need to protect it against removal races. The use of
18135538d78SGustavo Padovan * debugfs_create_file_unsafe() is actually safe here.
18235538d78SGustavo Padovan */
18335538d78SGustavo Padovan debugfs_create_file_unsafe("info", 0444, dbgfs, NULL,
18435538d78SGustavo Padovan &sync_info_debugfs_fops);
18535538d78SGustavo Padovan debugfs_create_file_unsafe("sw_sync", 0644, dbgfs, NULL,
18635538d78SGustavo Padovan &sw_sync_debugfs_fops);
18735538d78SGustavo Padovan
18835538d78SGustavo Padovan return 0;
18935538d78SGustavo Padovan }
19035538d78SGustavo Padovan late_initcall(sync_debugfs_init);
191