2 * Virtual Processor Dispatch Trace Log
4 * (C) Copyright IBM Corporation 2009
6 * Author: Jeremy Kerr <jk@ozlabs.org>
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)
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.
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.
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/debugfs.h>
26 #include <linux/spinlock.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30 #include <asm/firmware.h>
31 #include <asm/lppaca.h>
33 #include "plpar_wrappers.h"
36 struct dtl_entry
*buf
;
43 static DEFINE_PER_CPU(struct dtl
, cpu_dtl
);
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
51 static u8 dtl_event_mask
= 0x7;
55 * Size of per-cpu log buffers. Default is just under 16 pages worth.
57 static int dtl_buf_entries
= (16 * 85);
60 #ifdef CONFIG_VIRT_CPU_ACCOUNTING
63 struct dtl_entry
*write_ptr
;
64 struct dtl_entry
*buf
;
65 struct dtl_entry
*buf_end
;
69 static DEFINE_PER_CPU(struct dtl_ring
, dtl_rings
);
71 static atomic_t dtl_count
;
74 * The cpu accounting code controls the DTL ring buffer, and we get
75 * given entries as they are processed.
77 static void consume_dtle(struct dtl_entry
*dtle
, u64 index
)
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
;
89 /* check for hypervisor ring buffer overflow, ignore this entry if so */
90 if (index
+ N_DISPATCH_LOG
< vpa
->dtl_idx
)
94 if (wp
== dtlr
->buf_end
)
98 /* incrementing write_index makes the new entry visible */
103 static int dtl_start(struct dtl
*dtl
)
105 struct dtl_ring
*dtlr
= &per_cpu(dtl_rings
, dtl
->cpu
);
107 dtlr
->buf
= dtl
->buf
;
108 dtlr
->buf_end
= dtl
->buf
+ dtl
->buf_entries
;
109 dtlr
->write_index
= 0;
111 /* setting write_ptr enables logging into our buffer */
113 dtlr
->write_ptr
= dtl
->buf
;
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
;
119 dtl_consumer
= consume_dtle
;
120 atomic_inc(&dtl_count
);
124 static void dtl_stop(struct dtl
*dtl
)
126 struct dtl_ring
*dtlr
= &per_cpu(dtl_rings
, dtl
->cpu
);
128 dtlr
->write_ptr
= NULL
;
133 /* restore dtl_enable_mask */
134 lppaca_of(dtl
->cpu
).dtl_enable_mask
= dtlr
->saved_dtl_mask
;
136 if (atomic_dec_and_test(&dtl_count
))
140 static u64
dtl_current_index(struct dtl
*dtl
)
142 return per_cpu(dtl_rings
, dtl
->cpu
).write_index
;
145 #else /* CONFIG_VIRT_CPU_ACCOUNTING */
147 static int dtl_start(struct dtl
*dtl
)
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 */
154 ((u32
*)dtl
->buf
)[1] = dtl
->buf_entries
* sizeof(struct dtl_entry
);
156 hwcpu
= get_hard_smp_processor_id(dtl
->cpu
);
157 addr
= __pa(dtl
->buf
);
158 ret
= register_dtl(hwcpu
, addr
);
160 printk(KERN_WARNING
"%s: DTL registration for cpu %d (hw %d) "
161 "failed with %d\n", __func__
, dtl
->cpu
, hwcpu
, ret
);
165 /* set our initial buffer indices */
166 lppaca_of(dtl
->cpu
).dtl_idx
= 0;
168 /* ensure that our updates to the lppaca fields have occurred before
169 * we actually enable the logging */
172 /* enable event logging */
173 lppaca_of(dtl
->cpu
).dtl_enable_mask
= dtl_event_mask
;
178 static void dtl_stop(struct dtl
*dtl
)
180 int hwcpu
= get_hard_smp_processor_id(dtl
->cpu
);
182 lppaca_of(dtl
->cpu
).dtl_enable_mask
= 0x0;
184 unregister_dtl(hwcpu
, __pa(dtl
->buf
));
187 static u64
dtl_current_index(struct dtl
*dtl
)
189 return lppaca_of(dtl
->cpu
).dtl_idx
;
191 #endif /* CONFIG_VIRT_CPU_ACCOUNTING */
193 static int dtl_enable(struct dtl
*dtl
)
197 struct dtl_entry
*buf
= NULL
;
199 /* only allow one reader */
203 n_entries
= dtl_buf_entries
;
204 buf
= kmalloc_node(n_entries
* sizeof(struct dtl_entry
),
205 GFP_KERNEL
, cpu_to_node(dtl
->cpu
));
207 printk(KERN_WARNING
"%s: buffer alloc failed for cpu %d\n",
212 spin_lock(&dtl
->lock
);
215 /* store the original allocation size for use during read */
216 dtl
->buf_entries
= n_entries
;
223 spin_unlock(&dtl
->lock
);
230 static void dtl_disable(struct dtl
*dtl
)
232 spin_lock(&dtl
->lock
);
236 dtl
->buf_entries
= 0;
237 spin_unlock(&dtl
->lock
);
242 static int dtl_file_open(struct inode
*inode
, struct file
*filp
)
244 struct dtl
*dtl
= inode
->i_private
;
247 rc
= dtl_enable(dtl
);
251 filp
->private_data
= dtl
;
255 static int dtl_file_release(struct inode
*inode
, struct file
*filp
)
257 struct dtl
*dtl
= inode
->i_private
;
262 static ssize_t
dtl_file_read(struct file
*filp
, char __user
*buf
, size_t len
,
265 long int rc
, n_read
, n_req
, read_size
;
267 u64 cur_idx
, last_idx
, i
;
269 if ((len
% sizeof(struct dtl_entry
)) != 0)
272 dtl
= filp
->private_data
;
274 /* requested number of entries to read */
275 n_req
= len
/ sizeof(struct dtl_entry
);
277 /* actual number of entries read */
280 spin_lock(&dtl
->lock
);
282 cur_idx
= dtl_current_index(dtl
);
283 last_idx
= dtl
->last_idx
;
285 if (last_idx
+ dtl
->buf_entries
<= cur_idx
)
286 last_idx
= cur_idx
- dtl
->buf_entries
+ 1;
288 if (last_idx
+ n_req
> cur_idx
)
289 n_req
= cur_idx
- last_idx
;
292 dtl
->last_idx
= last_idx
+ n_req
;
294 spin_unlock(&dtl
->lock
);
299 i
= last_idx
% dtl
->buf_entries
;
301 /* read the tail of the buffer if we've wrapped */
302 if (i
+ n_req
> dtl
->buf_entries
) {
303 read_size
= dtl
->buf_entries
- i
;
305 rc
= copy_to_user(buf
, &dtl
->buf
[i
],
306 read_size
* sizeof(struct dtl_entry
));
313 buf
+= read_size
* sizeof(struct dtl_entry
);
316 /* .. and now the head */
317 rc
= copy_to_user(buf
, &dtl
->buf
[i
], n_req
* sizeof(struct dtl_entry
));
323 return n_read
* sizeof(struct dtl_entry
);
326 static const struct file_operations dtl_fops
= {
327 .open
= dtl_file_open
,
328 .release
= dtl_file_release
,
329 .read
= dtl_file_read
,
333 static struct dentry
*dtl_dir
;
335 static int dtl_setup_file(struct dtl
*dtl
)
339 sprintf(name
, "cpu-%d", dtl
->cpu
);
341 dtl
->file
= debugfs_create_file(name
, 0400, dtl_dir
, dtl
, &dtl_fops
);
348 static int dtl_init(void)
350 struct dentry
*event_mask_file
, *buf_entries_file
;
353 if (!firmware_has_feature(FW_FEATURE_SPLPAR
))
356 /* set up common debugfs structure */
359 dtl_dir
= debugfs_create_dir("dtl", powerpc_debugfs_root
);
361 printk(KERN_WARNING
"%s: can't create dtl root dir\n",
366 event_mask_file
= debugfs_create_x8("dtl_event_mask", 0600,
367 dtl_dir
, &dtl_event_mask
);
368 buf_entries_file
= debugfs_create_u32("dtl_buf_entries", 0600,
369 dtl_dir
, &dtl_buf_entries
);
371 if (!event_mask_file
|| !buf_entries_file
) {
372 printk(KERN_WARNING
"%s: can't create dtl files\n", __func__
);
376 /* set up the per-cpu log structures */
377 for_each_possible_cpu(i
) {
378 struct dtl
*dtl
= &per_cpu(cpu_dtl
, i
);
379 spin_lock_init(&dtl
->lock
);
382 rc
= dtl_setup_file(dtl
);
390 debugfs_remove_recursive(dtl_dir
);
394 arch_initcall(dtl_init
);