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