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> | |
5a0e3ad6 | 24 | #include <linux/slab.h> |
fc59a3fc | 25 | #include <linux/debugfs.h> |
872e439a | 26 | #include <linux/spinlock.h> |
fc59a3fc | 27 | #include <asm/smp.h> |
fc59a3fc | 28 | #include <asm/uaccess.h> |
b71a0c29 | 29 | #include <asm/firmware.h> |
cf9efce0 | 30 | #include <asm/lppaca.h> |
ae3a197e | 31 | #include <asm/debug.h> |
fc59a3fc JK |
32 | |
33 | #include "plpar_wrappers.h" | |
34 | ||
fc59a3fc JK |
35 | struct dtl { |
36 | struct dtl_entry *buf; | |
37 | struct dentry *file; | |
38 | int cpu; | |
39 | int buf_entries; | |
40 | u64 last_idx; | |
872e439a | 41 | spinlock_t lock; |
fc59a3fc | 42 | }; |
6b7487fc | 43 | static DEFINE_PER_CPU(struct dtl, cpu_dtl); |
fc59a3fc JK |
44 | |
45 | /* | |
46 | * Dispatch trace log event mask: | |
47 | * 0x7: 0x1: voluntary virtual processor waits | |
48 | * 0x2: time-slice preempts | |
49 | * 0x4: virtual partition memory page faults | |
50 | */ | |
51 | static u8 dtl_event_mask = 0x7; | |
52 | ||
53 | ||
54 | /* | |
af442a1b NA |
55 | * Size of per-cpu log buffers. Firmware requires that the buffer does |
56 | * not cross a 4k boundary. | |
fc59a3fc | 57 | */ |
af442a1b | 58 | static int dtl_buf_entries = N_DISPATCH_LOG; |
fc59a3fc | 59 | |
872e439a PM |
60 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING |
61 | struct dtl_ring { | |
62 | u64 write_index; | |
63 | struct dtl_entry *write_ptr; | |
64 | struct dtl_entry *buf; | |
65 | struct dtl_entry *buf_end; | |
66 | u8 saved_dtl_mask; | |
67 | }; | |
68 | ||
69 | static DEFINE_PER_CPU(struct dtl_ring, dtl_rings); | |
70 | ||
71 | static atomic_t dtl_count; | |
72 | ||
73 | /* | |
74 | * The cpu accounting code controls the DTL ring buffer, and we get | |
75 | * given entries as they are processed. | |
76 | */ | |
77 | static void consume_dtle(struct dtl_entry *dtle, u64 index) | |
fc59a3fc | 78 | { |
872e439a PM |
79 | struct dtl_ring *dtlr = &__get_cpu_var(dtl_rings); |
80 | struct dtl_entry *wp = dtlr->write_ptr; | |
81 | struct lppaca *vpa = local_paca->lppaca_ptr; | |
fc59a3fc | 82 | |
872e439a PM |
83 | if (!wp) |
84 | return; | |
fc59a3fc | 85 | |
872e439a PM |
86 | *wp = *dtle; |
87 | barrier(); | |
fc59a3fc | 88 | |
872e439a PM |
89 | /* check for hypervisor ring buffer overflow, ignore this entry if so */ |
90 | if (index + N_DISPATCH_LOG < vpa->dtl_idx) | |
91 | return; | |
92 | ||
93 | ++wp; | |
94 | if (wp == dtlr->buf_end) | |
95 | wp = dtlr->buf; | |
96 | dtlr->write_ptr = wp; | |
97 | ||
98 | /* incrementing write_index makes the new entry visible */ | |
99 | smp_wmb(); | |
100 | ++dtlr->write_index; | |
101 | } | |
102 | ||
103 | static int dtl_start(struct dtl *dtl) | |
104 | { | |
105 | struct dtl_ring *dtlr = &per_cpu(dtl_rings, dtl->cpu); | |
106 | ||
107 | dtlr->buf = dtl->buf; | |
108 | dtlr->buf_end = dtl->buf + dtl->buf_entries; | |
109 | dtlr->write_index = 0; | |
110 | ||
111 | /* setting write_ptr enables logging into our buffer */ | |
112 | smp_wmb(); | |
113 | dtlr->write_ptr = dtl->buf; | |
114 | ||
115 | /* enable event logging */ | |
116 | dtlr->saved_dtl_mask = lppaca_of(dtl->cpu).dtl_enable_mask; | |
117 | lppaca_of(dtl->cpu).dtl_enable_mask |= dtl_event_mask; | |
118 | ||
119 | dtl_consumer = consume_dtle; | |
120 | atomic_inc(&dtl_count); | |
121 | return 0; | |
122 | } | |
123 | ||
124 | static void dtl_stop(struct dtl *dtl) | |
125 | { | |
126 | struct dtl_ring *dtlr = &per_cpu(dtl_rings, dtl->cpu); | |
127 | ||
128 | dtlr->write_ptr = NULL; | |
129 | smp_wmb(); | |
130 | ||
131 | dtlr->buf = NULL; | |
132 | ||
133 | /* restore dtl_enable_mask */ | |
134 | lppaca_of(dtl->cpu).dtl_enable_mask = dtlr->saved_dtl_mask; | |
135 | ||
136 | if (atomic_dec_and_test(&dtl_count)) | |
137 | dtl_consumer = NULL; | |
138 | } | |
139 | ||
140 | static u64 dtl_current_index(struct dtl *dtl) | |
141 | { | |
142 | return per_cpu(dtl_rings, dtl->cpu).write_index; | |
143 | } | |
144 | ||
145 | #else /* CONFIG_VIRT_CPU_ACCOUNTING */ | |
146 | ||
147 | static int dtl_start(struct dtl *dtl) | |
148 | { | |
149 | unsigned long addr; | |
150 | int ret, hwcpu; | |
fc59a3fc JK |
151 | |
152 | /* Register our dtl buffer with the hypervisor. The HV expects the | |
153 | * buffer size to be passed in the second word of the buffer */ | |
af442a1b | 154 | ((u32 *)dtl->buf)[1] = DISPATCH_LOG_BYTES; |
fc59a3fc JK |
155 | |
156 | hwcpu = get_hard_smp_processor_id(dtl->cpu); | |
157 | addr = __pa(dtl->buf); | |
158 | ret = register_dtl(hwcpu, addr); | |
159 | if (ret) { | |
160 | printk(KERN_WARNING "%s: DTL registration for cpu %d (hw %d) " | |
161 | "failed with %d\n", __func__, dtl->cpu, hwcpu, ret); | |
fc59a3fc JK |
162 | return -EIO; |
163 | } | |
164 | ||
165 | /* set our initial buffer indices */ | |
872e439a | 166 | lppaca_of(dtl->cpu).dtl_idx = 0; |
fc59a3fc | 167 | |
82631f5d JK |
168 | /* ensure that our updates to the lppaca fields have occurred before |
169 | * we actually enable the logging */ | |
170 | smp_wmb(); | |
171 | ||
fc59a3fc | 172 | /* enable event logging */ |
8154c5d2 | 173 | lppaca_of(dtl->cpu).dtl_enable_mask = dtl_event_mask; |
fc59a3fc JK |
174 | |
175 | return 0; | |
176 | } | |
177 | ||
872e439a | 178 | static void dtl_stop(struct dtl *dtl) |
fc59a3fc JK |
179 | { |
180 | int hwcpu = get_hard_smp_processor_id(dtl->cpu); | |
181 | ||
8154c5d2 | 182 | lppaca_of(dtl->cpu).dtl_enable_mask = 0x0; |
fc59a3fc | 183 | |
b1301797 | 184 | unregister_dtl(hwcpu); |
872e439a PM |
185 | } |
186 | ||
187 | static u64 dtl_current_index(struct dtl *dtl) | |
188 | { | |
189 | return lppaca_of(dtl->cpu).dtl_idx; | |
190 | } | |
191 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING */ | |
fc59a3fc | 192 | |
872e439a PM |
193 | static int dtl_enable(struct dtl *dtl) |
194 | { | |
195 | long int n_entries; | |
196 | long int rc; | |
197 | struct dtl_entry *buf = NULL; | |
198 | ||
af442a1b NA |
199 | if (!dtl_cache) |
200 | return -ENOMEM; | |
201 | ||
872e439a PM |
202 | /* only allow one reader */ |
203 | if (dtl->buf) | |
204 | return -EBUSY; | |
205 | ||
206 | n_entries = dtl_buf_entries; | |
af442a1b | 207 | buf = kmem_cache_alloc_node(dtl_cache, GFP_KERNEL, cpu_to_node(dtl->cpu)); |
872e439a PM |
208 | if (!buf) { |
209 | printk(KERN_WARNING "%s: buffer alloc failed for cpu %d\n", | |
210 | __func__, dtl->cpu); | |
211 | return -ENOMEM; | |
212 | } | |
213 | ||
214 | spin_lock(&dtl->lock); | |
215 | rc = -EBUSY; | |
216 | if (!dtl->buf) { | |
217 | /* store the original allocation size for use during read */ | |
218 | dtl->buf_entries = n_entries; | |
219 | dtl->buf = buf; | |
220 | dtl->last_idx = 0; | |
221 | rc = dtl_start(dtl); | |
222 | if (rc) | |
223 | dtl->buf = NULL; | |
224 | } | |
225 | spin_unlock(&dtl->lock); | |
226 | ||
227 | if (rc) | |
af442a1b | 228 | kmem_cache_free(dtl_cache, buf); |
872e439a PM |
229 | return rc; |
230 | } | |
231 | ||
232 | static void dtl_disable(struct dtl *dtl) | |
233 | { | |
234 | spin_lock(&dtl->lock); | |
235 | dtl_stop(dtl); | |
af442a1b | 236 | kmem_cache_free(dtl_cache, dtl->buf); |
fc59a3fc JK |
237 | dtl->buf = NULL; |
238 | dtl->buf_entries = 0; | |
872e439a | 239 | spin_unlock(&dtl->lock); |
fc59a3fc JK |
240 | } |
241 | ||
242 | /* file interface */ | |
243 | ||
244 | static int dtl_file_open(struct inode *inode, struct file *filp) | |
245 | { | |
246 | struct dtl *dtl = inode->i_private; | |
247 | int rc; | |
248 | ||
249 | rc = dtl_enable(dtl); | |
250 | if (rc) | |
251 | return rc; | |
252 | ||
253 | filp->private_data = dtl; | |
254 | return 0; | |
255 | } | |
256 | ||
257 | static int dtl_file_release(struct inode *inode, struct file *filp) | |
258 | { | |
259 | struct dtl *dtl = inode->i_private; | |
260 | dtl_disable(dtl); | |
261 | return 0; | |
262 | } | |
263 | ||
264 | static ssize_t dtl_file_read(struct file *filp, char __user *buf, size_t len, | |
265 | loff_t *pos) | |
266 | { | |
872e439a | 267 | long int rc, n_read, n_req, read_size; |
fc59a3fc | 268 | struct dtl *dtl; |
872e439a | 269 | u64 cur_idx, last_idx, i; |
fc59a3fc JK |
270 | |
271 | if ((len % sizeof(struct dtl_entry)) != 0) | |
272 | return -EINVAL; | |
273 | ||
274 | dtl = filp->private_data; | |
275 | ||
276 | /* requested number of entries to read */ | |
277 | n_req = len / sizeof(struct dtl_entry); | |
278 | ||
279 | /* actual number of entries read */ | |
280 | n_read = 0; | |
281 | ||
872e439a PM |
282 | spin_lock(&dtl->lock); |
283 | ||
284 | cur_idx = dtl_current_index(dtl); | |
fc59a3fc JK |
285 | last_idx = dtl->last_idx; |
286 | ||
872e439a PM |
287 | if (last_idx + dtl->buf_entries <= cur_idx) |
288 | last_idx = cur_idx - dtl->buf_entries + 1; | |
289 | ||
290 | if (last_idx + n_req > cur_idx) | |
291 | n_req = cur_idx - last_idx; | |
fc59a3fc | 292 | |
872e439a PM |
293 | if (n_req > 0) |
294 | dtl->last_idx = last_idx + n_req; | |
295 | ||
296 | spin_unlock(&dtl->lock); | |
297 | ||
298 | if (n_req <= 0) | |
299 | return 0; | |
300 | ||
301 | i = last_idx % dtl->buf_entries; | |
fc59a3fc JK |
302 | |
303 | /* read the tail of the buffer if we've wrapped */ | |
872e439a PM |
304 | if (i + n_req > dtl->buf_entries) { |
305 | read_size = dtl->buf_entries - i; | |
fc59a3fc | 306 | |
872e439a | 307 | rc = copy_to_user(buf, &dtl->buf[i], |
fc59a3fc JK |
308 | read_size * sizeof(struct dtl_entry)); |
309 | if (rc) | |
310 | return -EFAULT; | |
311 | ||
872e439a | 312 | i = 0; |
fc59a3fc JK |
313 | n_req -= read_size; |
314 | n_read += read_size; | |
315 | buf += read_size * sizeof(struct dtl_entry); | |
316 | } | |
317 | ||
318 | /* .. and now the head */ | |
872e439a | 319 | rc = copy_to_user(buf, &dtl->buf[i], n_req * sizeof(struct dtl_entry)); |
fc59a3fc JK |
320 | if (rc) |
321 | return -EFAULT; | |
322 | ||
872e439a | 323 | n_read += n_req; |
fc59a3fc JK |
324 | |
325 | return n_read * sizeof(struct dtl_entry); | |
326 | } | |
327 | ||
828c0950 | 328 | static const struct file_operations dtl_fops = { |
fc59a3fc JK |
329 | .open = dtl_file_open, |
330 | .release = dtl_file_release, | |
331 | .read = dtl_file_read, | |
332 | .llseek = no_llseek, | |
333 | }; | |
334 | ||
335 | static struct dentry *dtl_dir; | |
336 | ||
337 | static int dtl_setup_file(struct dtl *dtl) | |
338 | { | |
339 | char name[10]; | |
340 | ||
341 | sprintf(name, "cpu-%d", dtl->cpu); | |
342 | ||
343 | dtl->file = debugfs_create_file(name, 0400, dtl_dir, dtl, &dtl_fops); | |
344 | if (!dtl->file) | |
345 | return -ENOMEM; | |
346 | ||
347 | return 0; | |
348 | } | |
349 | ||
350 | static int dtl_init(void) | |
351 | { | |
352 | struct dentry *event_mask_file, *buf_entries_file; | |
353 | int rc, i; | |
354 | ||
355 | if (!firmware_has_feature(FW_FEATURE_SPLPAR)) | |
356 | return -ENODEV; | |
357 | ||
358 | /* set up common debugfs structure */ | |
359 | ||
360 | rc = -ENOMEM; | |
361 | dtl_dir = debugfs_create_dir("dtl", powerpc_debugfs_root); | |
362 | if (!dtl_dir) { | |
363 | printk(KERN_WARNING "%s: can't create dtl root dir\n", | |
364 | __func__); | |
365 | goto err; | |
366 | } | |
367 | ||
368 | event_mask_file = debugfs_create_x8("dtl_event_mask", 0600, | |
369 | dtl_dir, &dtl_event_mask); | |
af442a1b | 370 | buf_entries_file = debugfs_create_u32("dtl_buf_entries", 0400, |
fc59a3fc JK |
371 | dtl_dir, &dtl_buf_entries); |
372 | ||
373 | if (!event_mask_file || !buf_entries_file) { | |
374 | printk(KERN_WARNING "%s: can't create dtl files\n", __func__); | |
375 | goto err_remove_dir; | |
376 | } | |
377 | ||
378 | /* set up the per-cpu log structures */ | |
379 | for_each_possible_cpu(i) { | |
6b7487fc | 380 | struct dtl *dtl = &per_cpu(cpu_dtl, i); |
872e439a | 381 | spin_lock_init(&dtl->lock); |
fc59a3fc JK |
382 | dtl->cpu = i; |
383 | ||
384 | rc = dtl_setup_file(dtl); | |
385 | if (rc) | |
386 | goto err_remove_dir; | |
387 | } | |
388 | ||
389 | return 0; | |
390 | ||
391 | err_remove_dir: | |
392 | debugfs_remove_recursive(dtl_dir); | |
393 | err: | |
394 | return rc; | |
395 | } | |
396 | arch_initcall(dtl_init); |