xref: /openbmc/linux/arch/s390/pci/pci_debug.c (revision d0b73b48)
1 /*
2  *  Copyright IBM Corp. 2012
3  *
4  *  Author(s):
5  *    Jan Glauber <jang@linux.vnet.ibm.com>
6  */
7 
8 #define COMPONENT "zPCI"
9 #define pr_fmt(fmt) COMPONENT ": " fmt
10 
11 #include <linux/kernel.h>
12 #include <linux/seq_file.h>
13 #include <linux/debugfs.h>
14 #include <linux/pci.h>
15 #include <asm/debug.h>
16 
17 #include <asm/pci_dma.h>
18 
19 static struct dentry *debugfs_root;
20 
21 static char *pci_perf_names[] = {
22 	/* hardware counters */
23 	"Load operations",
24 	"Store operations",
25 	"Store block operations",
26 	"Refresh operations",
27 	"DMA read bytes",
28 	"DMA write bytes",
29 	/* software counters */
30 	"Allocated pages",
31 	"Mapped pages",
32 	"Unmapped pages",
33 };
34 
35 static int pci_perf_show(struct seq_file *m, void *v)
36 {
37 	struct zpci_dev *zdev = m->private;
38 	u64 *stat;
39 	int i;
40 
41 	if (!zdev)
42 		return 0;
43 	if (!zdev->fmb)
44 		return seq_printf(m, "FMB statistics disabled\n");
45 
46 	/* header */
47 	seq_printf(m, "FMB @ %p\n", zdev->fmb);
48 	seq_printf(m, "Update interval: %u ms\n", zdev->fmb_update);
49 	seq_printf(m, "Samples: %u\n", zdev->fmb->samples);
50 	seq_printf(m, "Last update TOD: %Lx\n", zdev->fmb->last_update);
51 
52 	/* hardware counters */
53 	stat = (u64 *) &zdev->fmb->ld_ops;
54 	for (i = 0; i < 4; i++)
55 		seq_printf(m, "%26s:\t%llu\n",
56 			   pci_perf_names[i], *(stat + i));
57 	if (zdev->fmb->dma_valid)
58 		for (i = 4; i < 6; i++)
59 			seq_printf(m, "%26s:\t%llu\n",
60 				   pci_perf_names[i], *(stat + i));
61 	/* software counters */
62 	for (i = 6; i < ARRAY_SIZE(pci_perf_names); i++)
63 		seq_printf(m, "%26s:\t%llu\n",
64 			   pci_perf_names[i],
65 			   atomic64_read((atomic64_t *) (stat + i)));
66 
67 	return 0;
68 }
69 
70 static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
71 				  size_t count, loff_t *off)
72 {
73 	struct zpci_dev *zdev = ((struct seq_file *) file->private_data)->private;
74 	unsigned long val;
75 	int rc;
76 
77 	if (!zdev)
78 		return 0;
79 
80 	rc = kstrtoul_from_user(ubuf, count, 10, &val);
81 	if (rc)
82 		return rc;
83 
84 	switch (val) {
85 	case 0:
86 		rc = zpci_fmb_disable_device(zdev);
87 		if (rc)
88 			return rc;
89 		break;
90 	case 1:
91 		rc = zpci_fmb_enable_device(zdev);
92 		if (rc)
93 			return rc;
94 		break;
95 	}
96 	return count;
97 }
98 
99 static int pci_perf_seq_open(struct inode *inode, struct file *filp)
100 {
101 	return single_open(filp, pci_perf_show,
102 			   filp->f_path.dentry->d_inode->i_private);
103 }
104 
105 static const struct file_operations debugfs_pci_perf_fops = {
106 	.open	 = pci_perf_seq_open,
107 	.read	 = seq_read,
108 	.write	 = pci_perf_seq_write,
109 	.llseek  = seq_lseek,
110 	.release = single_release,
111 };
112 
113 static int pci_debug_show(struct seq_file *m, void *v)
114 {
115 	struct zpci_dev *zdev = m->private;
116 
117 	zpci_debug_info(zdev, m);
118 	return 0;
119 }
120 
121 static int pci_debug_seq_open(struct inode *inode, struct file *filp)
122 {
123 	return single_open(filp, pci_debug_show,
124 			   filp->f_path.dentry->d_inode->i_private);
125 }
126 
127 static const struct file_operations debugfs_pci_debug_fops = {
128 	.open	 = pci_debug_seq_open,
129 	.read	 = seq_read,
130 	.llseek  = seq_lseek,
131 	.release = single_release,
132 };
133 
134 void zpci_debug_init_device(struct zpci_dev *zdev)
135 {
136 	zdev->debugfs_dev = debugfs_create_dir(dev_name(&zdev->pdev->dev),
137 					       debugfs_root);
138 	if (IS_ERR(zdev->debugfs_dev))
139 		zdev->debugfs_dev = NULL;
140 
141 	zdev->debugfs_perf = debugfs_create_file("statistics",
142 				S_IFREG | S_IRUGO | S_IWUSR,
143 				zdev->debugfs_dev, zdev,
144 				&debugfs_pci_perf_fops);
145 	if (IS_ERR(zdev->debugfs_perf))
146 		zdev->debugfs_perf = NULL;
147 
148 	zdev->debugfs_debug = debugfs_create_file("debug",
149 				S_IFREG | S_IRUGO | S_IWUSR,
150 				zdev->debugfs_dev, zdev,
151 				&debugfs_pci_debug_fops);
152 	if (IS_ERR(zdev->debugfs_debug))
153 		zdev->debugfs_debug = NULL;
154 }
155 
156 void zpci_debug_exit_device(struct zpci_dev *zdev)
157 {
158 	debugfs_remove(zdev->debugfs_perf);
159 	debugfs_remove(zdev->debugfs_debug);
160 	debugfs_remove(zdev->debugfs_dev);
161 }
162 
163 int __init zpci_debug_init(void)
164 {
165 	/* event trace buffer */
166 	pci_debug_msg_id = debug_register("pci_msg", 16, 1, 16 * sizeof(long));
167 	if (!pci_debug_msg_id)
168 		return -EINVAL;
169 	debug_register_view(pci_debug_msg_id, &debug_sprintf_view);
170 	debug_set_level(pci_debug_msg_id, 3);
171 	zpci_dbg("Debug view initialized\n");
172 
173 	/* error log */
174 	pci_debug_err_id = debug_register("pci_error", 2, 1, 16);
175 	if (!pci_debug_err_id)
176 		return -EINVAL;
177 	debug_register_view(pci_debug_err_id, &debug_hex_ascii_view);
178 	debug_set_level(pci_debug_err_id, 6);
179 	zpci_err("Debug view initialized\n");
180 
181 	debugfs_root = debugfs_create_dir("pci", NULL);
182 	return 0;
183 }
184 
185 void zpci_debug_exit(void)
186 {
187 	if (pci_debug_msg_id)
188 		debug_unregister(pci_debug_msg_id);
189 	if (pci_debug_err_id)
190 		debug_unregister(pci_debug_err_id);
191 
192 	debugfs_remove(debugfs_root);
193 }
194