1 // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only)
2 /* Copyright(c) 2014 - 2020 Intel Corporation */
3 #include <linux/mutex.h>
4 #include <linux/slab.h>
5 #include <linux/seq_file.h>
6 #include "adf_accel_devices.h"
7 #include "adf_transport_internal.h"
8 #include "adf_transport_access_macros.h"
9
10 static DEFINE_MUTEX(ring_read_lock);
11 static DEFINE_MUTEX(bank_read_lock);
12
adf_ring_start(struct seq_file * sfile,loff_t * pos)13 static void *adf_ring_start(struct seq_file *sfile, loff_t *pos)
14 {
15 struct adf_etr_ring_data *ring = sfile->private;
16
17 mutex_lock(&ring_read_lock);
18 if (*pos == 0)
19 return SEQ_START_TOKEN;
20
21 if (*pos >= (ADF_SIZE_TO_RING_SIZE_IN_BYTES(ring->ring_size) /
22 ADF_MSG_SIZE_TO_BYTES(ring->msg_size)))
23 return NULL;
24
25 return ring->base_addr +
26 (ADF_MSG_SIZE_TO_BYTES(ring->msg_size) * (*pos)++);
27 }
28
adf_ring_next(struct seq_file * sfile,void * v,loff_t * pos)29 static void *adf_ring_next(struct seq_file *sfile, void *v, loff_t *pos)
30 {
31 struct adf_etr_ring_data *ring = sfile->private;
32
33 if (*pos >= (ADF_SIZE_TO_RING_SIZE_IN_BYTES(ring->ring_size) /
34 ADF_MSG_SIZE_TO_BYTES(ring->msg_size)))
35 return NULL;
36
37 return ring->base_addr +
38 (ADF_MSG_SIZE_TO_BYTES(ring->msg_size) * (*pos)++);
39 }
40
adf_ring_show(struct seq_file * sfile,void * v)41 static int adf_ring_show(struct seq_file *sfile, void *v)
42 {
43 struct adf_etr_ring_data *ring = sfile->private;
44 struct adf_etr_bank_data *bank = ring->bank;
45 void __iomem *csr = ring->bank->csr_addr;
46
47 if (v == SEQ_START_TOKEN) {
48 int head, tail, empty;
49
50 head = READ_CSR_RING_HEAD(csr, bank->bank_number,
51 ring->ring_number);
52 tail = READ_CSR_RING_TAIL(csr, bank->bank_number,
53 ring->ring_number);
54 empty = READ_CSR_E_STAT(csr, bank->bank_number);
55
56 seq_puts(sfile, "------- Ring configuration -------\n");
57 seq_printf(sfile, "ring name: %s\n",
58 ring->ring_debug->ring_name);
59 seq_printf(sfile, "ring num %d, bank num %d\n",
60 ring->ring_number, ring->bank->bank_number);
61 seq_printf(sfile, "head %x, tail %x, empty: %d\n",
62 head, tail, (empty & 1 << ring->ring_number)
63 >> ring->ring_number);
64 seq_printf(sfile, "ring size %d, msg size %d\n",
65 ADF_SIZE_TO_RING_SIZE_IN_BYTES(ring->ring_size),
66 ADF_MSG_SIZE_TO_BYTES(ring->msg_size));
67 seq_puts(sfile, "----------- Ring data ------------\n");
68 return 0;
69 }
70 seq_hex_dump(sfile, "", DUMP_PREFIX_ADDRESS, 32, 4,
71 v, ADF_MSG_SIZE_TO_BYTES(ring->msg_size), false);
72 return 0;
73 }
74
adf_ring_stop(struct seq_file * sfile,void * v)75 static void adf_ring_stop(struct seq_file *sfile, void *v)
76 {
77 mutex_unlock(&ring_read_lock);
78 }
79
80 static const struct seq_operations adf_ring_debug_sops = {
81 .start = adf_ring_start,
82 .next = adf_ring_next,
83 .stop = adf_ring_stop,
84 .show = adf_ring_show
85 };
86
87 DEFINE_SEQ_ATTRIBUTE(adf_ring_debug);
88
adf_ring_debugfs_add(struct adf_etr_ring_data * ring,const char * name)89 int adf_ring_debugfs_add(struct adf_etr_ring_data *ring, const char *name)
90 {
91 struct adf_etr_ring_debug_entry *ring_debug;
92 char entry_name[8];
93
94 ring_debug = kzalloc(sizeof(*ring_debug), GFP_KERNEL);
95 if (!ring_debug)
96 return -ENOMEM;
97
98 strlcpy(ring_debug->ring_name, name, sizeof(ring_debug->ring_name));
99 snprintf(entry_name, sizeof(entry_name), "ring_%02d",
100 ring->ring_number);
101
102 ring_debug->debug = debugfs_create_file(entry_name, S_IRUSR,
103 ring->bank->bank_debug_dir,
104 ring, &adf_ring_debug_fops);
105 ring->ring_debug = ring_debug;
106 return 0;
107 }
108
adf_ring_debugfs_rm(struct adf_etr_ring_data * ring)109 void adf_ring_debugfs_rm(struct adf_etr_ring_data *ring)
110 {
111 if (ring->ring_debug) {
112 debugfs_remove(ring->ring_debug->debug);
113 kfree(ring->ring_debug);
114 ring->ring_debug = NULL;
115 }
116 }
117
adf_bank_start(struct seq_file * sfile,loff_t * pos)118 static void *adf_bank_start(struct seq_file *sfile, loff_t *pos)
119 {
120 mutex_lock(&bank_read_lock);
121 if (*pos == 0)
122 return SEQ_START_TOKEN;
123
124 if (*pos >= ADF_ETR_MAX_RINGS_PER_BANK)
125 return NULL;
126
127 return pos;
128 }
129
adf_bank_next(struct seq_file * sfile,void * v,loff_t * pos)130 static void *adf_bank_next(struct seq_file *sfile, void *v, loff_t *pos)
131 {
132 if (++(*pos) >= ADF_ETR_MAX_RINGS_PER_BANK)
133 return NULL;
134
135 return pos;
136 }
137
adf_bank_show(struct seq_file * sfile,void * v)138 static int adf_bank_show(struct seq_file *sfile, void *v)
139 {
140 struct adf_etr_bank_data *bank = sfile->private;
141
142 if (v == SEQ_START_TOKEN) {
143 seq_printf(sfile, "------- Bank %d configuration -------\n",
144 bank->bank_number);
145 } else {
146 int ring_id = *((int *)v) - 1;
147 struct adf_etr_ring_data *ring = &bank->rings[ring_id];
148 void __iomem *csr = bank->csr_addr;
149 int head, tail, empty;
150
151 if (!(bank->ring_mask & 1 << ring_id))
152 return 0;
153
154 head = READ_CSR_RING_HEAD(csr, bank->bank_number,
155 ring->ring_number);
156 tail = READ_CSR_RING_TAIL(csr, bank->bank_number,
157 ring->ring_number);
158 empty = READ_CSR_E_STAT(csr, bank->bank_number);
159
160 seq_printf(sfile,
161 "ring num %02d, head %04x, tail %04x, empty: %d\n",
162 ring->ring_number, head, tail,
163 (empty & 1 << ring->ring_number) >>
164 ring->ring_number);
165 }
166 return 0;
167 }
168
adf_bank_stop(struct seq_file * sfile,void * v)169 static void adf_bank_stop(struct seq_file *sfile, void *v)
170 {
171 mutex_unlock(&bank_read_lock);
172 }
173
174 static const struct seq_operations adf_bank_debug_sops = {
175 .start = adf_bank_start,
176 .next = adf_bank_next,
177 .stop = adf_bank_stop,
178 .show = adf_bank_show
179 };
180
181 DEFINE_SEQ_ATTRIBUTE(adf_bank_debug);
182
adf_bank_debugfs_add(struct adf_etr_bank_data * bank)183 int adf_bank_debugfs_add(struct adf_etr_bank_data *bank)
184 {
185 struct adf_accel_dev *accel_dev = bank->accel_dev;
186 struct dentry *parent = accel_dev->transport->debug;
187 char name[8];
188
189 snprintf(name, sizeof(name), "bank_%02d", bank->bank_number);
190 bank->bank_debug_dir = debugfs_create_dir(name, parent);
191 bank->bank_debug_cfg = debugfs_create_file("config", S_IRUSR,
192 bank->bank_debug_dir, bank,
193 &adf_bank_debug_fops);
194 return 0;
195 }
196
adf_bank_debugfs_rm(struct adf_etr_bank_data * bank)197 void adf_bank_debugfs_rm(struct adf_etr_bank_data *bank)
198 {
199 debugfs_remove(bank->bank_debug_cfg);
200 debugfs_remove(bank->bank_debug_dir);
201 }
202