blob: 7d61498e45c082fee52f370be623dd8f97114f62 [file] [log] [blame]
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +00001/*
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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +000024#include <linux/debugfs.h>
Paul Mackerras872e4392010-08-31 01:59:53 +000025#include <linux/spinlock.h>
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +000026#include <asm/smp.h>
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +000027#include <asm/uaccess.h>
Sachin Santb71a0c22009-04-14 14:35:55 +000028#include <asm/firmware.h>
Paul Mackerrascf9efce2010-08-26 19:56:43 +000029#include <asm/lppaca.h>
David Howellsae3a1972012-03-28 18:30:02 +010030#include <asm/debug.h>
Deepthi Dharwar212bebb2013-08-22 15:23:52 +053031#include <asm/plpar_wrappers.h>
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +000032
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +000033struct dtl {
34 struct dtl_entry *buf;
35 struct dentry *file;
36 int cpu;
37 int buf_entries;
38 u64 last_idx;
Paul Mackerras872e4392010-08-31 01:59:53 +000039 spinlock_t lock;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +000040};
Tejun Heo6b7487f2009-10-29 22:34:14 +090041static DEFINE_PER_CPU(struct dtl, cpu_dtl);
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +000042
43/*
44 * Dispatch trace log event mask:
45 * 0x7: 0x1: voluntary virtual processor waits
46 * 0x2: time-slice preempts
47 * 0x4: virtual partition memory page faults
48 */
49static u8 dtl_event_mask = 0x7;
50
51
52/*
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +000053 * Size of per-cpu log buffers. Firmware requires that the buffer does
54 * not cross a 4k boundary.
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +000055 */
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +000056static int dtl_buf_entries = N_DISPATCH_LOG;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +000057
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +020058#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
Paul Mackerras872e4392010-08-31 01:59:53 +000059struct dtl_ring {
60 u64 write_index;
61 struct dtl_entry *write_ptr;
62 struct dtl_entry *buf;
63 struct dtl_entry *buf_end;
64 u8 saved_dtl_mask;
65};
66
67static DEFINE_PER_CPU(struct dtl_ring, dtl_rings);
68
69static atomic_t dtl_count;
70
71/*
72 * The cpu accounting code controls the DTL ring buffer, and we get
73 * given entries as they are processed.
74 */
75static void consume_dtle(struct dtl_entry *dtle, u64 index)
76{
77 struct dtl_ring *dtlr = &__get_cpu_var(dtl_rings);
78 struct dtl_entry *wp = dtlr->write_ptr;
79 struct lppaca *vpa = local_paca->lppaca_ptr;
80
81 if (!wp)
82 return;
83
84 *wp = *dtle;
85 barrier();
86
87 /* check for hypervisor ring buffer overflow, ignore this entry if so */
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +100088 if (index + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx))
Paul Mackerras872e4392010-08-31 01:59:53 +000089 return;
90
91 ++wp;
92 if (wp == dtlr->buf_end)
93 wp = dtlr->buf;
94 dtlr->write_ptr = wp;
95
96 /* incrementing write_index makes the new entry visible */
97 smp_wmb();
98 ++dtlr->write_index;
99}
100
101static int dtl_start(struct dtl *dtl)
102{
103 struct dtl_ring *dtlr = &per_cpu(dtl_rings, dtl->cpu);
104
105 dtlr->buf = dtl->buf;
106 dtlr->buf_end = dtl->buf + dtl->buf_entries;
107 dtlr->write_index = 0;
108
109 /* setting write_ptr enables logging into our buffer */
110 smp_wmb();
111 dtlr->write_ptr = dtl->buf;
112
113 /* enable event logging */
114 dtlr->saved_dtl_mask = lppaca_of(dtl->cpu).dtl_enable_mask;
115 lppaca_of(dtl->cpu).dtl_enable_mask |= dtl_event_mask;
116
117 dtl_consumer = consume_dtle;
118 atomic_inc(&dtl_count);
119 return 0;
120}
121
122static void dtl_stop(struct dtl *dtl)
123{
124 struct dtl_ring *dtlr = &per_cpu(dtl_rings, dtl->cpu);
125
126 dtlr->write_ptr = NULL;
127 smp_wmb();
128
129 dtlr->buf = NULL;
130
131 /* restore dtl_enable_mask */
132 lppaca_of(dtl->cpu).dtl_enable_mask = dtlr->saved_dtl_mask;
133
134 if (atomic_dec_and_test(&dtl_count))
135 dtl_consumer = NULL;
136}
137
138static u64 dtl_current_index(struct dtl *dtl)
139{
140 return per_cpu(dtl_rings, dtl->cpu).write_index;
141}
142
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200143#else /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
Paul Mackerras872e4392010-08-31 01:59:53 +0000144
145static int dtl_start(struct dtl *dtl)
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000146{
147 unsigned long addr;
148 int ret, hwcpu;
149
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000150 /* Register our dtl buffer with the hypervisor. The HV expects the
151 * buffer size to be passed in the second word of the buffer */
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000152 ((u32 *)dtl->buf)[1] = DISPATCH_LOG_BYTES;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000153
154 hwcpu = get_hard_smp_processor_id(dtl->cpu);
155 addr = __pa(dtl->buf);
156 ret = register_dtl(hwcpu, addr);
157 if (ret) {
158 printk(KERN_WARNING "%s: DTL registration for cpu %d (hw %d) "
159 "failed with %d\n", __func__, dtl->cpu, hwcpu, ret);
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000160 return -EIO;
161 }
162
163 /* set our initial buffer indices */
Paul Mackerras872e4392010-08-31 01:59:53 +0000164 lppaca_of(dtl->cpu).dtl_idx = 0;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000165
Jeremy Kerr82631f52009-03-23 16:55:08 +0000166 /* ensure that our updates to the lppaca fields have occurred before
167 * we actually enable the logging */
168 smp_wmb();
169
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000170 /* enable event logging */
Paul Mackerras8154c5d2010-08-12 20:18:15 +0000171 lppaca_of(dtl->cpu).dtl_enable_mask = dtl_event_mask;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000172
173 return 0;
174}
175
Paul Mackerras872e4392010-08-31 01:59:53 +0000176static void dtl_stop(struct dtl *dtl)
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000177{
178 int hwcpu = get_hard_smp_processor_id(dtl->cpu);
179
Paul Mackerras8154c5d2010-08-12 20:18:15 +0000180 lppaca_of(dtl->cpu).dtl_enable_mask = 0x0;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000181
Anton Blanchardb1301792011-07-25 01:46:32 +0000182 unregister_dtl(hwcpu);
Paul Mackerras872e4392010-08-31 01:59:53 +0000183}
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000184
Paul Mackerras872e4392010-08-31 01:59:53 +0000185static u64 dtl_current_index(struct dtl *dtl)
186{
187 return lppaca_of(dtl->cpu).dtl_idx;
188}
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200189#endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
Paul Mackerras872e4392010-08-31 01:59:53 +0000190
191static int dtl_enable(struct dtl *dtl)
192{
193 long int n_entries;
194 long int rc;
195 struct dtl_entry *buf = NULL;
196
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000197 if (!dtl_cache)
198 return -ENOMEM;
199
Paul Mackerras872e4392010-08-31 01:59:53 +0000200 /* only allow one reader */
201 if (dtl->buf)
202 return -EBUSY;
203
204 n_entries = dtl_buf_entries;
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000205 buf = kmem_cache_alloc_node(dtl_cache, GFP_KERNEL, cpu_to_node(dtl->cpu));
Paul Mackerras872e4392010-08-31 01:59:53 +0000206 if (!buf) {
207 printk(KERN_WARNING "%s: buffer alloc failed for cpu %d\n",
208 __func__, dtl->cpu);
209 return -ENOMEM;
210 }
211
212 spin_lock(&dtl->lock);
213 rc = -EBUSY;
214 if (!dtl->buf) {
215 /* store the original allocation size for use during read */
216 dtl->buf_entries = n_entries;
217 dtl->buf = buf;
218 dtl->last_idx = 0;
219 rc = dtl_start(dtl);
220 if (rc)
221 dtl->buf = NULL;
222 }
223 spin_unlock(&dtl->lock);
224
225 if (rc)
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000226 kmem_cache_free(dtl_cache, buf);
Paul Mackerras872e4392010-08-31 01:59:53 +0000227 return rc;
228}
229
230static void dtl_disable(struct dtl *dtl)
231{
232 spin_lock(&dtl->lock);
233 dtl_stop(dtl);
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000234 kmem_cache_free(dtl_cache, dtl->buf);
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000235 dtl->buf = NULL;
236 dtl->buf_entries = 0;
Paul Mackerras872e4392010-08-31 01:59:53 +0000237 spin_unlock(&dtl->lock);
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000238}
239
240/* file interface */
241
242static int dtl_file_open(struct inode *inode, struct file *filp)
243{
244 struct dtl *dtl = inode->i_private;
245 int rc;
246
247 rc = dtl_enable(dtl);
248 if (rc)
249 return rc;
250
251 filp->private_data = dtl;
252 return 0;
253}
254
255static int dtl_file_release(struct inode *inode, struct file *filp)
256{
257 struct dtl *dtl = inode->i_private;
258 dtl_disable(dtl);
259 return 0;
260}
261
262static ssize_t dtl_file_read(struct file *filp, char __user *buf, size_t len,
263 loff_t *pos)
264{
Paul Mackerras872e4392010-08-31 01:59:53 +0000265 long int rc, n_read, n_req, read_size;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000266 struct dtl *dtl;
Paul Mackerras872e4392010-08-31 01:59:53 +0000267 u64 cur_idx, last_idx, i;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000268
269 if ((len % sizeof(struct dtl_entry)) != 0)
270 return -EINVAL;
271
272 dtl = filp->private_data;
273
274 /* requested number of entries to read */
275 n_req = len / sizeof(struct dtl_entry);
276
277 /* actual number of entries read */
278 n_read = 0;
279
Paul Mackerras872e4392010-08-31 01:59:53 +0000280 spin_lock(&dtl->lock);
281
282 cur_idx = dtl_current_index(dtl);
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000283 last_idx = dtl->last_idx;
284
Paul Mackerras872e4392010-08-31 01:59:53 +0000285 if (last_idx + dtl->buf_entries <= cur_idx)
286 last_idx = cur_idx - dtl->buf_entries + 1;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000287
Paul Mackerras872e4392010-08-31 01:59:53 +0000288 if (last_idx + n_req > cur_idx)
289 n_req = cur_idx - last_idx;
290
291 if (n_req > 0)
292 dtl->last_idx = last_idx + n_req;
293
294 spin_unlock(&dtl->lock);
295
296 if (n_req <= 0)
297 return 0;
298
299 i = last_idx % dtl->buf_entries;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000300
301 /* read the tail of the buffer if we've wrapped */
Paul Mackerras872e4392010-08-31 01:59:53 +0000302 if (i + n_req > dtl->buf_entries) {
303 read_size = dtl->buf_entries - i;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000304
Paul Mackerras872e4392010-08-31 01:59:53 +0000305 rc = copy_to_user(buf, &dtl->buf[i],
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000306 read_size * sizeof(struct dtl_entry));
307 if (rc)
308 return -EFAULT;
309
Paul Mackerras872e4392010-08-31 01:59:53 +0000310 i = 0;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000311 n_req -= read_size;
312 n_read += read_size;
313 buf += read_size * sizeof(struct dtl_entry);
314 }
315
316 /* .. and now the head */
Paul Mackerras872e4392010-08-31 01:59:53 +0000317 rc = copy_to_user(buf, &dtl->buf[i], n_req * sizeof(struct dtl_entry));
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000318 if (rc)
319 return -EFAULT;
320
Paul Mackerras872e4392010-08-31 01:59:53 +0000321 n_read += n_req;
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000322
323 return n_read * sizeof(struct dtl_entry);
324}
325
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700326static const struct file_operations dtl_fops = {
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000327 .open = dtl_file_open,
328 .release = dtl_file_release,
329 .read = dtl_file_read,
330 .llseek = no_llseek,
331};
332
333static struct dentry *dtl_dir;
334
335static int dtl_setup_file(struct dtl *dtl)
336{
337 char name[10];
338
339 sprintf(name, "cpu-%d", dtl->cpu);
340
341 dtl->file = debugfs_create_file(name, 0400, dtl_dir, dtl, &dtl_fops);
342 if (!dtl->file)
343 return -ENOMEM;
344
345 return 0;
346}
347
348static int dtl_init(void)
349{
350 struct dentry *event_mask_file, *buf_entries_file;
351 int rc, i;
352
353 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
354 return -ENODEV;
355
356 /* set up common debugfs structure */
357
358 rc = -ENOMEM;
359 dtl_dir = debugfs_create_dir("dtl", powerpc_debugfs_root);
360 if (!dtl_dir) {
361 printk(KERN_WARNING "%s: can't create dtl root dir\n",
362 __func__);
363 goto err;
364 }
365
366 event_mask_file = debugfs_create_x8("dtl_event_mask", 0600,
367 dtl_dir, &dtl_event_mask);
Nishanth Aravamudanaf442a12011-05-04 12:54:16 +0000368 buf_entries_file = debugfs_create_u32("dtl_buf_entries", 0400,
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000369 dtl_dir, &dtl_buf_entries);
370
371 if (!event_mask_file || !buf_entries_file) {
372 printk(KERN_WARNING "%s: can't create dtl files\n", __func__);
373 goto err_remove_dir;
374 }
375
376 /* set up the per-cpu log structures */
377 for_each_possible_cpu(i) {
Tejun Heo6b7487f2009-10-29 22:34:14 +0900378 struct dtl *dtl = &per_cpu(cpu_dtl, i);
Paul Mackerras872e4392010-08-31 01:59:53 +0000379 spin_lock_init(&dtl->lock);
Jeremy Kerrfc59a3f2009-03-11 17:55:52 +0000380 dtl->cpu = i;
381
382 rc = dtl_setup_file(dtl);
383 if (rc)
384 goto err_remove_dir;
385 }
386
387 return 0;
388
389err_remove_dir:
390 debugfs_remove_recursive(dtl_dir);
391err:
392 return rc;
393}
394arch_initcall(dtl_init);