1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2023 Cai Huoqing
4  * Synopsys DesignWare HDMA v0 debugfs
5  *
6  * Author: Cai Huoqing <cai.huoqing@linux.dev>
7  */
8 
9 #include <linux/debugfs.h>
10 #include <linux/bitfield.h>
11 
12 #include "dw-hdma-v0-debugfs.h"
13 #include "dw-hdma-v0-regs.h"
14 #include "dw-edma-core.h"
15 
16 #define REGS_ADDR(dw, name)						       \
17 	({								       \
18 		struct dw_hdma_v0_regs __iomem *__regs = (dw)->chip->reg_base; \
19 									       \
20 		(void __iomem *)&__regs->name;				       \
21 	})
22 
23 #define REGS_CH_ADDR(dw, name, _dir, _ch)				       \
24 	({								       \
25 		struct dw_hdma_v0_ch_regs __iomem *__ch_regs;		       \
26 									       \
27 		if (_dir == EDMA_DIR_READ)				       \
28 			__ch_regs = REGS_ADDR(dw, ch[_ch].rd);		       \
29 		else							       \
30 			__ch_regs = REGS_ADDR(dw, ch[_ch].wr);		       \
31 									       \
32 		(void __iomem *)&__ch_regs->name;			       \
33 	})
34 
35 #define CTX_REGISTER(dw, name, dir, ch) \
36 	{#name, REGS_CH_ADDR(dw, name, dir, ch)}
37 
38 #define WRITE_STR				"write"
39 #define READ_STR				"read"
40 #define CHANNEL_STR				"channel"
41 #define REGISTERS_STR				"registers"
42 
43 struct dw_hdma_debugfs_entry {
44 	const char				*name;
45 	void __iomem				*reg;
46 };
47 
dw_hdma_debugfs_u32_get(void * data,u64 * val)48 static int dw_hdma_debugfs_u32_get(void *data, u64 *val)
49 {
50 	struct dw_hdma_debugfs_entry *entry = data;
51 	void __iomem *reg = entry->reg;
52 
53 	*val = readl(reg);
54 
55 	return 0;
56 }
57 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, dw_hdma_debugfs_u32_get, NULL, "0x%08llx\n");
58 
dw_hdma_debugfs_create_x32(struct dw_edma * dw,const struct dw_hdma_debugfs_entry ini[],int nr_entries,struct dentry * dent)59 static void dw_hdma_debugfs_create_x32(struct dw_edma *dw,
60 				       const struct dw_hdma_debugfs_entry ini[],
61 				       int nr_entries, struct dentry *dent)
62 {
63 	struct dw_hdma_debugfs_entry *entries;
64 	int i;
65 
66 	entries = devm_kcalloc(dw->chip->dev, nr_entries, sizeof(*entries),
67 			       GFP_KERNEL);
68 	if (!entries)
69 		return;
70 
71 	for (i = 0; i < nr_entries; i++) {
72 		entries[i] = ini[i];
73 
74 		debugfs_create_file_unsafe(entries[i].name, 0444, dent,
75 					   &entries[i], &fops_x32);
76 	}
77 }
78 
dw_hdma_debugfs_regs_ch(struct dw_edma * dw,enum dw_edma_dir dir,u16 ch,struct dentry * dent)79 static void dw_hdma_debugfs_regs_ch(struct dw_edma *dw, enum dw_edma_dir dir,
80 				    u16 ch, struct dentry *dent)
81 {
82 	const struct dw_hdma_debugfs_entry debugfs_regs[] = {
83 		CTX_REGISTER(dw, ch_en, dir, ch),
84 		CTX_REGISTER(dw, doorbell, dir, ch),
85 		CTX_REGISTER(dw, prefetch, dir, ch),
86 		CTX_REGISTER(dw, handshake, dir, ch),
87 		CTX_REGISTER(dw, llp.lsb, dir, ch),
88 		CTX_REGISTER(dw, llp.msb, dir, ch),
89 		CTX_REGISTER(dw, cycle_sync, dir, ch),
90 		CTX_REGISTER(dw, transfer_size, dir, ch),
91 		CTX_REGISTER(dw, sar.lsb, dir, ch),
92 		CTX_REGISTER(dw, sar.msb, dir, ch),
93 		CTX_REGISTER(dw, dar.lsb, dir, ch),
94 		CTX_REGISTER(dw, dar.msb, dir, ch),
95 		CTX_REGISTER(dw, watermark_en, dir, ch),
96 		CTX_REGISTER(dw, control1, dir, ch),
97 		CTX_REGISTER(dw, func_num, dir, ch),
98 		CTX_REGISTER(dw, qos, dir, ch),
99 		CTX_REGISTER(dw, ch_stat, dir, ch),
100 		CTX_REGISTER(dw, int_stat, dir, ch),
101 		CTX_REGISTER(dw, int_setup, dir, ch),
102 		CTX_REGISTER(dw, int_clear, dir, ch),
103 		CTX_REGISTER(dw, msi_stop.lsb, dir, ch),
104 		CTX_REGISTER(dw, msi_stop.msb, dir, ch),
105 		CTX_REGISTER(dw, msi_watermark.lsb, dir, ch),
106 		CTX_REGISTER(dw, msi_watermark.msb, dir, ch),
107 		CTX_REGISTER(dw, msi_abort.lsb, dir, ch),
108 		CTX_REGISTER(dw, msi_abort.msb, dir, ch),
109 		CTX_REGISTER(dw, msi_msgdata, dir, ch),
110 	};
111 	int nr_entries = ARRAY_SIZE(debugfs_regs);
112 
113 	dw_hdma_debugfs_create_x32(dw, debugfs_regs, nr_entries, dent);
114 }
115 
dw_hdma_debugfs_regs_wr(struct dw_edma * dw,struct dentry * dent)116 static void dw_hdma_debugfs_regs_wr(struct dw_edma *dw, struct dentry *dent)
117 {
118 	struct dentry *regs_dent, *ch_dent;
119 	char name[32];
120 	int i;
121 
122 	regs_dent = debugfs_create_dir(WRITE_STR, dent);
123 
124 	for (i = 0; i < dw->wr_ch_cnt; i++) {
125 		snprintf(name, sizeof(name), "%s:%d", CHANNEL_STR, i);
126 
127 		ch_dent = debugfs_create_dir(name, regs_dent);
128 
129 		dw_hdma_debugfs_regs_ch(dw, EDMA_DIR_WRITE, i, ch_dent);
130 	}
131 }
132 
dw_hdma_debugfs_regs_rd(struct dw_edma * dw,struct dentry * dent)133 static void dw_hdma_debugfs_regs_rd(struct dw_edma *dw, struct dentry *dent)
134 {
135 	struct dentry *regs_dent, *ch_dent;
136 	char name[32];
137 	int i;
138 
139 	regs_dent = debugfs_create_dir(READ_STR, dent);
140 
141 	for (i = 0; i < dw->rd_ch_cnt; i++) {
142 		snprintf(name, sizeof(name), "%s:%d", CHANNEL_STR, i);
143 
144 		ch_dent = debugfs_create_dir(name, regs_dent);
145 
146 		dw_hdma_debugfs_regs_ch(dw, EDMA_DIR_READ, i, ch_dent);
147 	}
148 }
149 
dw_hdma_debugfs_regs(struct dw_edma * dw)150 static void dw_hdma_debugfs_regs(struct dw_edma *dw)
151 {
152 	struct dentry *regs_dent;
153 
154 	regs_dent = debugfs_create_dir(REGISTERS_STR, dw->dma.dbg_dev_root);
155 
156 	dw_hdma_debugfs_regs_wr(dw, regs_dent);
157 	dw_hdma_debugfs_regs_rd(dw, regs_dent);
158 }
159 
dw_hdma_v0_debugfs_on(struct dw_edma * dw)160 void dw_hdma_v0_debugfs_on(struct dw_edma *dw)
161 {
162 	if (!debugfs_initialized())
163 		return;
164 
165 	debugfs_create_u32("mf", 0444, dw->dma.dbg_dev_root, &dw->chip->mf);
166 	debugfs_create_u16("wr_ch_cnt", 0444, dw->dma.dbg_dev_root, &dw->wr_ch_cnt);
167 	debugfs_create_u16("rd_ch_cnt", 0444, dw->dma.dbg_dev_root, &dw->rd_ch_cnt);
168 
169 	dw_hdma_debugfs_regs(dw);
170 }
171