Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright IBM Corp. 2012,2015
4 *
5 * Author(s):
6 * Jan Glauber <jang@linux.vnet.ibm.com>
7 */
8
9#define KMSG_COMPONENT "zpci"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12#include <linux/kernel.h>
13#include <linux/seq_file.h>
14#include <linux/debugfs.h>
15#include <linux/export.h>
16#include <linux/pci.h>
17#include <asm/debug.h>
18
19#include <asm/pci_dma.h>
20
21static struct dentry *debugfs_root;
22debug_info_t *pci_debug_msg_id;
23EXPORT_SYMBOL_GPL(pci_debug_msg_id);
24debug_info_t *pci_debug_err_id;
25EXPORT_SYMBOL_GPL(pci_debug_err_id);
26
27static char *pci_common_names[] = {
28 "Load operations",
29 "Store operations",
30 "Store block operations",
31 "Refresh operations",
32};
33
34static char *pci_fmt0_names[] = {
35 "DMA read bytes",
36 "DMA write bytes",
37};
38
39static char *pci_fmt1_names[] = {
40 "Received bytes",
41 "Received packets",
42 "Transmitted bytes",
43 "Transmitted packets",
44};
45
46static char *pci_fmt2_names[] = {
47 "Consumed work units",
48 "Maximum work units",
49};
50
51static char *pci_fmt3_names[] = {
52 "Transmitted bytes",
53};
54
55static char *pci_sw_names[] = {
56 "Mapped pages",
57 "Unmapped pages",
58 "Global RPCITs",
59 "Sync Map RPCITs",
60 "Sync RPCITs",
61};
62
63static void pci_fmb_show(struct seq_file *m, char *name[], int length,
64 u64 *data)
65{
66 int i;
67
68 for (i = 0; i < length; i++, data++)
69 seq_printf(m, "%26s:\t%llu\n", name[i], *data);
70}
71
72static void pci_sw_counter_show(struct seq_file *m)
73{
74 struct zpci_dev *zdev = m->private;
75 struct zpci_iommu_ctrs *ctrs;
76 atomic64_t *counter;
77 unsigned long flags;
78 int i;
79
80 spin_lock_irqsave(&zdev->dom_lock, flags);
81 ctrs = zpci_get_iommu_ctrs(m->private);
82 if (!ctrs)
83 goto unlock;
84
85 counter = &ctrs->mapped_pages;
86 for (i = 0; i < ARRAY_SIZE(pci_sw_names); i++, counter++)
87 seq_printf(m, "%26s:\t%llu\n", pci_sw_names[i],
88 atomic64_read(counter));
89unlock:
90 spin_unlock_irqrestore(&zdev->dom_lock, flags);
91}
92
93static int pci_perf_show(struct seq_file *m, void *v)
94{
95 struct zpci_dev *zdev = m->private;
96
97 if (!zdev)
98 return 0;
99
100 mutex_lock(&zdev->fmb_lock);
101 if (!zdev->fmb) {
102 mutex_unlock(&zdev->fmb_lock);
103 seq_puts(m, "FMB statistics disabled\n");
104 return 0;
105 }
106
107 /* header */
108 seq_printf(m, "Update interval: %u ms\n", zdev->fmb_update);
109 seq_printf(m, "Samples: %u\n", zdev->fmb->samples);
110 seq_printf(m, "Last update TOD: %Lx\n", zdev->fmb->last_update);
111
112 pci_fmb_show(m, pci_common_names, ARRAY_SIZE(pci_common_names),
113 &zdev->fmb->ld_ops);
114
115 switch (zdev->fmb->format) {
116 case 0:
117 if (!(zdev->fmb->fmt_ind & ZPCI_FMB_DMA_COUNTER_VALID))
118 break;
119 pci_fmb_show(m, pci_fmt0_names, ARRAY_SIZE(pci_fmt0_names),
120 &zdev->fmb->fmt0.dma_rbytes);
121 break;
122 case 1:
123 pci_fmb_show(m, pci_fmt1_names, ARRAY_SIZE(pci_fmt1_names),
124 &zdev->fmb->fmt1.rx_bytes);
125 break;
126 case 2:
127 pci_fmb_show(m, pci_fmt2_names, ARRAY_SIZE(pci_fmt2_names),
128 &zdev->fmb->fmt2.consumed_work_units);
129 break;
130 case 3:
131 pci_fmb_show(m, pci_fmt3_names, ARRAY_SIZE(pci_fmt3_names),
132 &zdev->fmb->fmt3.tx_bytes);
133 break;
134 default:
135 seq_puts(m, "Unknown format\n");
136 }
137
138 pci_sw_counter_show(m);
139 mutex_unlock(&zdev->fmb_lock);
140 return 0;
141}
142
143static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
144 size_t count, loff_t *off)
145{
146 struct zpci_dev *zdev = ((struct seq_file *) file->private_data)->private;
147 unsigned long val;
148 int rc;
149
150 if (!zdev)
151 return 0;
152
153 rc = kstrtoul_from_user(ubuf, count, 10, &val);
154 if (rc)
155 return rc;
156
157 mutex_lock(&zdev->fmb_lock);
158 switch (val) {
159 case 0:
160 rc = zpci_fmb_disable_device(zdev);
161 break;
162 case 1:
163 rc = zpci_fmb_enable_device(zdev);
164 break;
165 }
166 mutex_unlock(&zdev->fmb_lock);
167 return rc ? rc : count;
168}
169
170static int pci_perf_seq_open(struct inode *inode, struct file *filp)
171{
172 return single_open(filp, pci_perf_show,
173 file_inode(filp)->i_private);
174}
175
176static const struct file_operations debugfs_pci_perf_fops = {
177 .open = pci_perf_seq_open,
178 .read = seq_read,
179 .write = pci_perf_seq_write,
180 .llseek = seq_lseek,
181 .release = single_release,
182};
183
184void zpci_debug_init_device(struct zpci_dev *zdev, const char *name)
185{
186 zdev->debugfs_dev = debugfs_create_dir(name, debugfs_root);
187
188 debugfs_create_file("statistics", S_IFREG | S_IRUGO | S_IWUSR,
189 zdev->debugfs_dev, zdev, &debugfs_pci_perf_fops);
190}
191
192void zpci_debug_exit_device(struct zpci_dev *zdev)
193{
194 debugfs_remove_recursive(zdev->debugfs_dev);
195}
196
197int __init zpci_debug_init(void)
198{
199 /* event trace buffer */
200 pci_debug_msg_id = debug_register("pci_msg", 8, 1, 8 * sizeof(long));
201 if (!pci_debug_msg_id)
202 return -EINVAL;
203 debug_register_view(pci_debug_msg_id, &debug_sprintf_view);
204 debug_set_level(pci_debug_msg_id, 3);
205
206 /* error log */
207 pci_debug_err_id = debug_register("pci_error", 2, 1, 16);
208 if (!pci_debug_err_id)
209 return -EINVAL;
210 debug_register_view(pci_debug_err_id, &debug_hex_ascii_view);
211 debug_set_level(pci_debug_err_id, 3);
212
213 debugfs_root = debugfs_create_dir("pci", NULL);
214 return 0;
215}
216
217void zpci_debug_exit(void)
218{
219 debug_unregister(pci_debug_msg_id);
220 debug_unregister(pci_debug_err_id);
221 debugfs_remove(debugfs_root);
222}
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/export.h>
15#include <linux/pci.h>
16#include <asm/debug.h>
17
18#include <asm/pci_dma.h>
19
20static struct dentry *debugfs_root;
21debug_info_t *pci_debug_msg_id;
22EXPORT_SYMBOL_GPL(pci_debug_msg_id);
23debug_info_t *pci_debug_err_id;
24EXPORT_SYMBOL_GPL(pci_debug_err_id);
25
26static char *pci_perf_names[] = {
27 /* hardware counters */
28 "Load operations",
29 "Store operations",
30 "Store block operations",
31 "Refresh operations",
32 "DMA read bytes",
33 "DMA write bytes",
34 /* software counters */
35 "Allocated pages",
36 "Mapped pages",
37 "Unmapped pages",
38};
39
40static int pci_perf_show(struct seq_file *m, void *v)
41{
42 struct zpci_dev *zdev = m->private;
43 u64 *stat;
44 int i;
45
46 if (!zdev)
47 return 0;
48 if (!zdev->fmb)
49 return seq_printf(m, "FMB statistics disabled\n");
50
51 /* header */
52 seq_printf(m, "FMB @ %p\n", zdev->fmb);
53 seq_printf(m, "Update interval: %u ms\n", zdev->fmb_update);
54 seq_printf(m, "Samples: %u\n", zdev->fmb->samples);
55 seq_printf(m, "Last update TOD: %Lx\n", zdev->fmb->last_update);
56
57 /* hardware counters */
58 stat = (u64 *) &zdev->fmb->ld_ops;
59 for (i = 0; i < 4; i++)
60 seq_printf(m, "%26s:\t%llu\n",
61 pci_perf_names[i], *(stat + i));
62 if (zdev->fmb->dma_valid)
63 for (i = 4; i < 6; i++)
64 seq_printf(m, "%26s:\t%llu\n",
65 pci_perf_names[i], *(stat + i));
66 /* software counters */
67 for (i = 6; i < ARRAY_SIZE(pci_perf_names); i++)
68 seq_printf(m, "%26s:\t%llu\n",
69 pci_perf_names[i],
70 atomic64_read((atomic64_t *) (stat + i)));
71
72 return 0;
73}
74
75static ssize_t pci_perf_seq_write(struct file *file, const char __user *ubuf,
76 size_t count, loff_t *off)
77{
78 struct zpci_dev *zdev = ((struct seq_file *) file->private_data)->private;
79 unsigned long val;
80 int rc;
81
82 if (!zdev)
83 return 0;
84
85 rc = kstrtoul_from_user(ubuf, count, 10, &val);
86 if (rc)
87 return rc;
88
89 switch (val) {
90 case 0:
91 rc = zpci_fmb_disable_device(zdev);
92 if (rc)
93 return rc;
94 break;
95 case 1:
96 rc = zpci_fmb_enable_device(zdev);
97 if (rc)
98 return rc;
99 break;
100 }
101 return count;
102}
103
104static int pci_perf_seq_open(struct inode *inode, struct file *filp)
105{
106 return single_open(filp, pci_perf_show,
107 file_inode(filp)->i_private);
108}
109
110static const struct file_operations debugfs_pci_perf_fops = {
111 .open = pci_perf_seq_open,
112 .read = seq_read,
113 .write = pci_perf_seq_write,
114 .llseek = seq_lseek,
115 .release = single_release,
116};
117
118void zpci_debug_init_device(struct zpci_dev *zdev)
119{
120 zdev->debugfs_dev = debugfs_create_dir(dev_name(&zdev->pdev->dev),
121 debugfs_root);
122 if (IS_ERR(zdev->debugfs_dev))
123 zdev->debugfs_dev = NULL;
124
125 zdev->debugfs_perf = debugfs_create_file("statistics",
126 S_IFREG | S_IRUGO | S_IWUSR,
127 zdev->debugfs_dev, zdev,
128 &debugfs_pci_perf_fops);
129 if (IS_ERR(zdev->debugfs_perf))
130 zdev->debugfs_perf = NULL;
131}
132
133void zpci_debug_exit_device(struct zpci_dev *zdev)
134{
135 debugfs_remove(zdev->debugfs_perf);
136 debugfs_remove(zdev->debugfs_dev);
137}
138
139int __init zpci_debug_init(void)
140{
141 /* event trace buffer */
142 pci_debug_msg_id = debug_register("pci_msg", 8, 1, 8 * sizeof(long));
143 if (!pci_debug_msg_id)
144 return -EINVAL;
145 debug_register_view(pci_debug_msg_id, &debug_sprintf_view);
146 debug_set_level(pci_debug_msg_id, 3);
147
148 /* error log */
149 pci_debug_err_id = debug_register("pci_error", 2, 1, 16);
150 if (!pci_debug_err_id)
151 return -EINVAL;
152 debug_register_view(pci_debug_err_id, &debug_hex_ascii_view);
153 debug_set_level(pci_debug_err_id, 6);
154
155 debugfs_root = debugfs_create_dir("pci", NULL);
156 return 0;
157}
158
159void zpci_debug_exit(void)
160{
161 if (pci_debug_msg_id)
162 debug_unregister(pci_debug_msg_id);
163 if (pci_debug_err_id)
164 debug_unregister(pci_debug_err_id);
165
166 debugfs_remove(debugfs_root);
167}