xref: /linux/drivers/misc/sgi-gru/gruprocfs.c (revision 63307d015b91e626c97bb82e88054af3d0b74643)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * SN Platform GRU Driver
4  *
5  *              PROC INTERFACES
6  *
7  * This file supports the /proc interfaces for the GRU driver
8  *
9  *  Copyright (c) 2008 Silicon Graphics, Inc.  All Rights Reserved.
10  */
11 
12 #include <linux/proc_fs.h>
13 #include <linux/device.h>
14 #include <linux/seq_file.h>
15 #include <linux/uaccess.h>
16 #include "gru.h"
17 #include "grulib.h"
18 #include "grutables.h"
19 
20 #define printstat(s, f)		printstat_val(s, &gru_stats.f, #f)
21 
22 static void printstat_val(struct seq_file *s, atomic_long_t *v, char *id)
23 {
24 	unsigned long val = atomic_long_read(v);
25 
26 	seq_printf(s, "%16lu %s\n", val, id);
27 }
28 
29 static int statistics_show(struct seq_file *s, void *p)
30 {
31 	printstat(s, vdata_alloc);
32 	printstat(s, vdata_free);
33 	printstat(s, gts_alloc);
34 	printstat(s, gts_free);
35 	printstat(s, gms_alloc);
36 	printstat(s, gms_free);
37 	printstat(s, gts_double_allocate);
38 	printstat(s, assign_context);
39 	printstat(s, assign_context_failed);
40 	printstat(s, free_context);
41 	printstat(s, load_user_context);
42 	printstat(s, load_kernel_context);
43 	printstat(s, lock_kernel_context);
44 	printstat(s, unlock_kernel_context);
45 	printstat(s, steal_user_context);
46 	printstat(s, steal_kernel_context);
47 	printstat(s, steal_context_failed);
48 	printstat(s, nopfn);
49 	printstat(s, asid_new);
50 	printstat(s, asid_next);
51 	printstat(s, asid_wrap);
52 	printstat(s, asid_reuse);
53 	printstat(s, intr);
54 	printstat(s, intr_cbr);
55 	printstat(s, intr_tfh);
56 	printstat(s, intr_spurious);
57 	printstat(s, intr_mm_lock_failed);
58 	printstat(s, call_os);
59 	printstat(s, call_os_wait_queue);
60 	printstat(s, user_flush_tlb);
61 	printstat(s, user_unload_context);
62 	printstat(s, user_exception);
63 	printstat(s, set_context_option);
64 	printstat(s, check_context_retarget_intr);
65 	printstat(s, check_context_unload);
66 	printstat(s, tlb_dropin);
67 	printstat(s, tlb_preload_page);
68 	printstat(s, tlb_dropin_fail_no_asid);
69 	printstat(s, tlb_dropin_fail_upm);
70 	printstat(s, tlb_dropin_fail_invalid);
71 	printstat(s, tlb_dropin_fail_range_active);
72 	printstat(s, tlb_dropin_fail_idle);
73 	printstat(s, tlb_dropin_fail_fmm);
74 	printstat(s, tlb_dropin_fail_no_exception);
75 	printstat(s, tfh_stale_on_fault);
76 	printstat(s, mmu_invalidate_range);
77 	printstat(s, mmu_invalidate_page);
78 	printstat(s, flush_tlb);
79 	printstat(s, flush_tlb_gru);
80 	printstat(s, flush_tlb_gru_tgh);
81 	printstat(s, flush_tlb_gru_zero_asid);
82 	printstat(s, copy_gpa);
83 	printstat(s, read_gpa);
84 	printstat(s, mesq_receive);
85 	printstat(s, mesq_receive_none);
86 	printstat(s, mesq_send);
87 	printstat(s, mesq_send_failed);
88 	printstat(s, mesq_noop);
89 	printstat(s, mesq_send_unexpected_error);
90 	printstat(s, mesq_send_lb_overflow);
91 	printstat(s, mesq_send_qlimit_reached);
92 	printstat(s, mesq_send_amo_nacked);
93 	printstat(s, mesq_send_put_nacked);
94 	printstat(s, mesq_qf_locked);
95 	printstat(s, mesq_qf_noop_not_full);
96 	printstat(s, mesq_qf_switch_head_failed);
97 	printstat(s, mesq_qf_unexpected_error);
98 	printstat(s, mesq_noop_unexpected_error);
99 	printstat(s, mesq_noop_lb_overflow);
100 	printstat(s, mesq_noop_qlimit_reached);
101 	printstat(s, mesq_noop_amo_nacked);
102 	printstat(s, mesq_noop_put_nacked);
103 	printstat(s, mesq_noop_page_overflow);
104 	return 0;
105 }
106 
107 static ssize_t statistics_write(struct file *file, const char __user *userbuf,
108 				size_t count, loff_t *data)
109 {
110 	memset(&gru_stats, 0, sizeof(gru_stats));
111 	return count;
112 }
113 
114 static int mcs_statistics_show(struct seq_file *s, void *p)
115 {
116 	int op;
117 	unsigned long total, count, max;
118 	static char *id[] = {"cch_allocate", "cch_start", "cch_interrupt",
119 		"cch_interrupt_sync", "cch_deallocate", "tfh_write_only",
120 		"tfh_write_restart", "tgh_invalidate"};
121 
122 	seq_printf(s, "%-20s%12s%12s%12s\n", "#id", "count", "aver-clks", "max-clks");
123 	for (op = 0; op < mcsop_last; op++) {
124 		count = atomic_long_read(&mcs_op_statistics[op].count);
125 		total = atomic_long_read(&mcs_op_statistics[op].total);
126 		max = mcs_op_statistics[op].max;
127 		seq_printf(s, "%-20s%12ld%12ld%12ld\n", id[op], count,
128 			   count ? total / count : 0, max);
129 	}
130 	return 0;
131 }
132 
133 static ssize_t mcs_statistics_write(struct file *file,
134 			const char __user *userbuf, size_t count, loff_t *data)
135 {
136 	memset(mcs_op_statistics, 0, sizeof(mcs_op_statistics));
137 	return count;
138 }
139 
140 static int options_show(struct seq_file *s, void *p)
141 {
142 	seq_printf(s, "#bitmask: 1=trace, 2=statistics\n");
143 	seq_printf(s, "0x%lx\n", gru_options);
144 	return 0;
145 }
146 
147 static ssize_t options_write(struct file *file, const char __user *userbuf,
148 			     size_t count, loff_t *data)
149 {
150 	int ret;
151 
152 	ret = kstrtoul_from_user(userbuf, count, 0, &gru_options);
153 	if (ret)
154 		return ret;
155 
156 	return count;
157 }
158 
159 static int cch_seq_show(struct seq_file *file, void *data)
160 {
161 	long gid = *(long *)data;
162 	int i;
163 	struct gru_state *gru = GID_TO_GRU(gid);
164 	struct gru_thread_state *ts;
165 	const char *mode[] = { "??", "UPM", "INTR", "OS_POLL" };
166 
167 	if (gid == 0)
168 		seq_printf(file, "#%5s%5s%6s%7s%9s%6s%8s%8s\n", "gid", "bid",
169 			   "ctx#", "asid", "pid", "cbrs", "dsbytes", "mode");
170 	if (gru)
171 		for (i = 0; i < GRU_NUM_CCH; i++) {
172 			ts = gru->gs_gts[i];
173 			if (!ts)
174 				continue;
175 			seq_printf(file, " %5d%5d%6d%7d%9d%6d%8d%8s\n",
176 				   gru->gs_gid, gru->gs_blade_id, i,
177 				   is_kernel_context(ts) ? 0 : ts->ts_gms->ms_asids[gid].mt_asid,
178 				   is_kernel_context(ts) ? 0 : ts->ts_tgid_owner,
179 				   ts->ts_cbr_au_count * GRU_CBR_AU_SIZE,
180 				   ts->ts_cbr_au_count * GRU_DSR_AU_BYTES,
181 				   mode[ts->ts_user_options &
182 					GRU_OPT_MISS_MASK]);
183 		}
184 
185 	return 0;
186 }
187 
188 static int gru_seq_show(struct seq_file *file, void *data)
189 {
190 	long gid = *(long *)data, ctxfree, cbrfree, dsrfree;
191 	struct gru_state *gru = GID_TO_GRU(gid);
192 
193 	if (gid == 0) {
194 		seq_printf(file, "#%5s%5s%7s%6s%6s%8s%6s%6s\n", "gid", "nid",
195 			   "ctx", "cbr", "dsr", "ctx", "cbr", "dsr");
196 		seq_printf(file, "#%5s%5s%7s%6s%6s%8s%6s%6s\n", "", "", "busy",
197 			   "busy", "busy", "free", "free", "free");
198 	}
199 	if (gru) {
200 		ctxfree = GRU_NUM_CCH - gru->gs_active_contexts;
201 		cbrfree = hweight64(gru->gs_cbr_map) * GRU_CBR_AU_SIZE;
202 		dsrfree = hweight64(gru->gs_dsr_map) * GRU_DSR_AU_BYTES;
203 		seq_printf(file, " %5d%5d%7ld%6ld%6ld%8ld%6ld%6ld\n",
204 			   gru->gs_gid, gru->gs_blade_id, GRU_NUM_CCH - ctxfree,
205 			   GRU_NUM_CBE - cbrfree, GRU_NUM_DSR_BYTES - dsrfree,
206 			   ctxfree, cbrfree, dsrfree);
207 	}
208 
209 	return 0;
210 }
211 
212 static void seq_stop(struct seq_file *file, void *data)
213 {
214 }
215 
216 static void *seq_start(struct seq_file *file, loff_t *gid)
217 {
218 	if (*gid < gru_max_gids)
219 		return gid;
220 	return NULL;
221 }
222 
223 static void *seq_next(struct seq_file *file, void *data, loff_t *gid)
224 {
225 	(*gid)++;
226 	if (*gid < gru_max_gids)
227 		return gid;
228 	return NULL;
229 }
230 
231 static const struct seq_operations cch_seq_ops = {
232 	.start	= seq_start,
233 	.next	= seq_next,
234 	.stop	= seq_stop,
235 	.show	= cch_seq_show
236 };
237 
238 static const struct seq_operations gru_seq_ops = {
239 	.start	= seq_start,
240 	.next	= seq_next,
241 	.stop	= seq_stop,
242 	.show	= gru_seq_show
243 };
244 
245 static int statistics_open(struct inode *inode, struct file *file)
246 {
247 	return single_open(file, statistics_show, NULL);
248 }
249 
250 static int mcs_statistics_open(struct inode *inode, struct file *file)
251 {
252 	return single_open(file, mcs_statistics_show, NULL);
253 }
254 
255 static int options_open(struct inode *inode, struct file *file)
256 {
257 	return single_open(file, options_show, NULL);
258 }
259 
260 /* *INDENT-OFF* */
261 static const struct file_operations statistics_fops = {
262 	.open 		= statistics_open,
263 	.read 		= seq_read,
264 	.write 		= statistics_write,
265 	.llseek 	= seq_lseek,
266 	.release 	= single_release,
267 };
268 
269 static const struct file_operations mcs_statistics_fops = {
270 	.open 		= mcs_statistics_open,
271 	.read 		= seq_read,
272 	.write 		= mcs_statistics_write,
273 	.llseek 	= seq_lseek,
274 	.release 	= single_release,
275 };
276 
277 static const struct file_operations options_fops = {
278 	.open 		= options_open,
279 	.read 		= seq_read,
280 	.write 		= options_write,
281 	.llseek 	= seq_lseek,
282 	.release 	= single_release,
283 };
284 
285 static struct proc_dir_entry *proc_gru __read_mostly;
286 
287 int gru_proc_init(void)
288 {
289 	proc_gru = proc_mkdir("sgi_uv/gru", NULL);
290 	if (!proc_gru)
291 		return -1;
292 	if (!proc_create("statistics", 0644, proc_gru, &statistics_fops))
293 		goto err;
294 	if (!proc_create("mcs_statistics", 0644, proc_gru, &mcs_statistics_fops))
295 		goto err;
296 	if (!proc_create("debug_options", 0644, proc_gru, &options_fops))
297 		goto err;
298 	if (!proc_create_seq("cch_status", 0444, proc_gru, &cch_seq_ops))
299 		goto err;
300 	if (!proc_create_seq("gru_status", 0444, proc_gru, &gru_seq_ops))
301 		goto err;
302 	return 0;
303 err:
304 	remove_proc_subtree("sgi_uv/gru", NULL);
305 	return -1;
306 }
307 
308 void gru_proc_exit(void)
309 {
310 	remove_proc_subtree("sgi_uv/gru", NULL);
311 }
312