Commit | Line | Data |
---|---|---|
fc59a3fc JK |
1 | /* |
2 | * Virtual Processor Dispatch Trace Log | |
3 | * | |
4 | * (C) Copyright IBM Corporation 2009 | |
5 | * | |
6 | * Author: Jeremy Kerr <jk@ozlabs.org> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2, or (at your option) | |
11 | * any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | */ | |
22 | ||
23 | #include <linux/init.h> | |
24 | #include <linux/debugfs.h> | |
25 | #include <asm/smp.h> | |
26 | #include <asm/system.h> | |
27 | #include <asm/uaccess.h> | |
b71a0c29 | 28 | #include <asm/firmware.h> |
fc59a3fc JK |
29 | |
30 | #include "plpar_wrappers.h" | |
31 | ||
32 | /* | |
33 | * Layout of entries in the hypervisor's DTL buffer. Although we don't | |
34 | * actually access the internals of an entry (we only need to know the size), | |
35 | * we might as well define it here for reference. | |
36 | */ | |
37 | struct dtl_entry { | |
38 | u8 dispatch_reason; | |
39 | u8 preempt_reason; | |
40 | u16 processor_id; | |
41 | u32 enqueue_to_dispatch_time; | |
42 | u32 ready_to_enqueue_time; | |
43 | u32 waiting_to_ready_time; | |
44 | u64 timebase; | |
45 | u64 fault_addr; | |
46 | u64 srr0; | |
47 | u64 srr1; | |
48 | }; | |
49 | ||
50 | struct dtl { | |
51 | struct dtl_entry *buf; | |
52 | struct dentry *file; | |
53 | int cpu; | |
54 | int buf_entries; | |
55 | u64 last_idx; | |
56 | }; | |
57 | static DEFINE_PER_CPU(struct dtl, dtl); | |
58 | ||
59 | /* | |
60 | * Dispatch trace log event mask: | |
61 | * 0x7: 0x1: voluntary virtual processor waits | |
62 | * 0x2: time-slice preempts | |
63 | * 0x4: virtual partition memory page faults | |
64 | */ | |
65 | static u8 dtl_event_mask = 0x7; | |
66 | ||
67 | ||
68 | /* | |
69 | * Size of per-cpu log buffers. Default is just under 16 pages worth. | |
70 | */ | |
71 | static int dtl_buf_entries = (16 * 85); | |
72 | ||
73 | ||
74 | static int dtl_enable(struct dtl *dtl) | |
75 | { | |
76 | unsigned long addr; | |
77 | int ret, hwcpu; | |
78 | ||
79 | /* only allow one reader */ | |
80 | if (dtl->buf) | |
81 | return -EBUSY; | |
82 | ||
83 | /* we need to store the original allocation size for use during read */ | |
84 | dtl->buf_entries = dtl_buf_entries; | |
85 | ||
86 | dtl->buf = kmalloc_node(dtl->buf_entries * sizeof(struct dtl_entry), | |
87 | GFP_KERNEL, cpu_to_node(dtl->cpu)); | |
88 | if (!dtl->buf) { | |
89 | printk(KERN_WARNING "%s: buffer alloc failed for cpu %d\n", | |
90 | __func__, dtl->cpu); | |
91 | return -ENOMEM; | |
92 | } | |
93 | ||
94 | /* Register our dtl buffer with the hypervisor. The HV expects the | |
95 | * buffer size to be passed in the second word of the buffer */ | |
96 | ((u32 *)dtl->buf)[1] = dtl->buf_entries * sizeof(struct dtl_entry); | |
97 | ||
98 | hwcpu = get_hard_smp_processor_id(dtl->cpu); | |
99 | addr = __pa(dtl->buf); | |
100 | ret = register_dtl(hwcpu, addr); | |
101 | if (ret) { | |
102 | printk(KERN_WARNING "%s: DTL registration for cpu %d (hw %d) " | |
103 | "failed with %d\n", __func__, dtl->cpu, hwcpu, ret); | |
104 | kfree(dtl->buf); | |
105 | return -EIO; | |
106 | } | |
107 | ||
108 | /* set our initial buffer indices */ | |
109 | dtl->last_idx = lppaca[dtl->cpu].dtl_idx = 0; | |
110 | ||
82631f5d JK |
111 | /* ensure that our updates to the lppaca fields have occurred before |
112 | * we actually enable the logging */ | |
113 | smp_wmb(); | |
114 | ||
fc59a3fc JK |
115 | /* enable event logging */ |
116 | lppaca[dtl->cpu].dtl_enable_mask = dtl_event_mask; | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
121 | static void dtl_disable(struct dtl *dtl) | |
122 | { | |
123 | int hwcpu = get_hard_smp_processor_id(dtl->cpu); | |
124 | ||
125 | lppaca[dtl->cpu].dtl_enable_mask = 0x0; | |
126 | ||
127 | unregister_dtl(hwcpu, __pa(dtl->buf)); | |
128 | ||
129 | kfree(dtl->buf); | |
130 | dtl->buf = NULL; | |
131 | dtl->buf_entries = 0; | |
132 | } | |
133 | ||
134 | /* file interface */ | |
135 | ||
136 | static int dtl_file_open(struct inode *inode, struct file *filp) | |
137 | { | |
138 | struct dtl *dtl = inode->i_private; | |
139 | int rc; | |
140 | ||
141 | rc = dtl_enable(dtl); | |
142 | if (rc) | |
143 | return rc; | |
144 | ||
145 | filp->private_data = dtl; | |
146 | return 0; | |
147 | } | |
148 | ||
149 | static int dtl_file_release(struct inode *inode, struct file *filp) | |
150 | { | |
151 | struct dtl *dtl = inode->i_private; | |
152 | dtl_disable(dtl); | |
153 | return 0; | |
154 | } | |
155 | ||
156 | static ssize_t dtl_file_read(struct file *filp, char __user *buf, size_t len, | |
157 | loff_t *pos) | |
158 | { | |
159 | int rc, cur_idx, last_idx, n_read, n_req, read_size; | |
160 | struct dtl *dtl; | |
161 | ||
162 | if ((len % sizeof(struct dtl_entry)) != 0) | |
163 | return -EINVAL; | |
164 | ||
165 | dtl = filp->private_data; | |
166 | ||
167 | /* requested number of entries to read */ | |
168 | n_req = len / sizeof(struct dtl_entry); | |
169 | ||
170 | /* actual number of entries read */ | |
171 | n_read = 0; | |
172 | ||
173 | cur_idx = lppaca[dtl->cpu].dtl_idx; | |
174 | last_idx = dtl->last_idx; | |
175 | ||
176 | if (cur_idx - last_idx > dtl->buf_entries) { | |
177 | pr_debug("%s: hv buffer overflow for cpu %d, samples lost\n", | |
178 | __func__, dtl->cpu); | |
179 | } | |
180 | ||
181 | cur_idx %= dtl->buf_entries; | |
182 | last_idx %= dtl->buf_entries; | |
183 | ||
184 | /* read the tail of the buffer if we've wrapped */ | |
185 | if (last_idx > cur_idx) { | |
186 | read_size = min(n_req, dtl->buf_entries - last_idx); | |
187 | ||
188 | rc = copy_to_user(buf, &dtl->buf[last_idx], | |
189 | read_size * sizeof(struct dtl_entry)); | |
190 | if (rc) | |
191 | return -EFAULT; | |
192 | ||
193 | last_idx = 0; | |
194 | n_req -= read_size; | |
195 | n_read += read_size; | |
196 | buf += read_size * sizeof(struct dtl_entry); | |
197 | } | |
198 | ||
199 | /* .. and now the head */ | |
200 | read_size = min(n_req, cur_idx - last_idx); | |
201 | rc = copy_to_user(buf, &dtl->buf[last_idx], | |
202 | read_size * sizeof(struct dtl_entry)); | |
203 | if (rc) | |
204 | return -EFAULT; | |
205 | ||
206 | n_read += read_size; | |
207 | dtl->last_idx += n_read; | |
208 | ||
209 | return n_read * sizeof(struct dtl_entry); | |
210 | } | |
211 | ||
212 | static struct file_operations dtl_fops = { | |
213 | .open = dtl_file_open, | |
214 | .release = dtl_file_release, | |
215 | .read = dtl_file_read, | |
216 | .llseek = no_llseek, | |
217 | }; | |
218 | ||
219 | static struct dentry *dtl_dir; | |
220 | ||
221 | static int dtl_setup_file(struct dtl *dtl) | |
222 | { | |
223 | char name[10]; | |
224 | ||
225 | sprintf(name, "cpu-%d", dtl->cpu); | |
226 | ||
227 | dtl->file = debugfs_create_file(name, 0400, dtl_dir, dtl, &dtl_fops); | |
228 | if (!dtl->file) | |
229 | return -ENOMEM; | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
234 | static int dtl_init(void) | |
235 | { | |
236 | struct dentry *event_mask_file, *buf_entries_file; | |
237 | int rc, i; | |
238 | ||
239 | if (!firmware_has_feature(FW_FEATURE_SPLPAR)) | |
240 | return -ENODEV; | |
241 | ||
242 | /* set up common debugfs structure */ | |
243 | ||
244 | rc = -ENOMEM; | |
245 | dtl_dir = debugfs_create_dir("dtl", powerpc_debugfs_root); | |
246 | if (!dtl_dir) { | |
247 | printk(KERN_WARNING "%s: can't create dtl root dir\n", | |
248 | __func__); | |
249 | goto err; | |
250 | } | |
251 | ||
252 | event_mask_file = debugfs_create_x8("dtl_event_mask", 0600, | |
253 | dtl_dir, &dtl_event_mask); | |
254 | buf_entries_file = debugfs_create_u32("dtl_buf_entries", 0600, | |
255 | dtl_dir, &dtl_buf_entries); | |
256 | ||
257 | if (!event_mask_file || !buf_entries_file) { | |
258 | printk(KERN_WARNING "%s: can't create dtl files\n", __func__); | |
259 | goto err_remove_dir; | |
260 | } | |
261 | ||
262 | /* set up the per-cpu log structures */ | |
263 | for_each_possible_cpu(i) { | |
264 | struct dtl *dtl = &per_cpu(dtl, i); | |
265 | dtl->cpu = i; | |
266 | ||
267 | rc = dtl_setup_file(dtl); | |
268 | if (rc) | |
269 | goto err_remove_dir; | |
270 | } | |
271 | ||
272 | return 0; | |
273 | ||
274 | err_remove_dir: | |
275 | debugfs_remove_recursive(dtl_dir); | |
276 | err: | |
277 | return rc; | |
278 | } | |
279 | arch_initcall(dtl_init); |