powerpc/pseries: Use macros for referring to the DTL enable mask
[linux-2.6-block.git] / arch / powerpc / platforms / pseries / dtl.c
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/slab.h>
24 #include <linux/spinlock.h>
25 #include <asm/smp.h>
26 #include <linux/uaccess.h>
27 #include <asm/firmware.h>
28 #include <asm/lppaca.h>
29 #include <asm/debugfs.h>
30 #include <asm/plpar_wrappers.h>
31 #include <asm/machdep.h>
32
33 struct dtl {
34         struct dtl_entry        *buf;
35         struct dentry           *file;
36         int                     cpu;
37         int                     buf_entries;
38         u64                     last_idx;
39         spinlock_t              lock;
40 };
41 static DEFINE_PER_CPU(struct dtl, cpu_dtl);
42
43 static u8 dtl_event_mask = DTL_LOG_ALL;
44
45
46 /*
47  * Size of per-cpu log buffers. Firmware requires that the buffer does
48  * not cross a 4k boundary.
49  */
50 static int dtl_buf_entries = N_DISPATCH_LOG;
51
52 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
53 struct 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
61 static DEFINE_PER_CPU(struct dtl_ring, dtl_rings);
62
63 static 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  */
69 static void consume_dtle(struct dtl_entry *dtle, u64 index)
70 {
71         struct dtl_ring *dtlr = this_cpu_ptr(&dtl_rings);
72         struct dtl_entry *wp = dtlr->write_ptr;
73         struct lppaca *vpa = local_paca->lppaca_ptr;
74
75         if (!wp)
76                 return;
77
78         *wp = *dtle;
79         barrier();
80
81         /* check for hypervisor ring buffer overflow, ignore this entry if so */
82         if (index + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx))
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
95 static 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
116 static 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
132 static u64 dtl_current_index(struct dtl *dtl)
133 {
134         return per_cpu(dtl_rings, dtl->cpu).write_index;
135 }
136
137 #else /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
138
139 static int dtl_start(struct dtl *dtl)
140 {
141         unsigned long addr;
142         int ret, hwcpu;
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 */
146         ((u32 *)dtl->buf)[1] = cpu_to_be32(DISPATCH_LOG_BYTES);
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);
154                 return -EIO;
155         }
156
157         /* set our initial buffer indices */
158         lppaca_of(dtl->cpu).dtl_idx = 0;
159
160         /* ensure that our updates to the lppaca fields have occurred before
161          * we actually enable the logging */
162         smp_wmb();
163
164         /* enable event logging */
165         lppaca_of(dtl->cpu).dtl_enable_mask = dtl_event_mask;
166
167         return 0;
168 }
169
170 static void dtl_stop(struct dtl *dtl)
171 {
172         int hwcpu = get_hard_smp_processor_id(dtl->cpu);
173
174         lppaca_of(dtl->cpu).dtl_enable_mask = 0x0;
175
176         unregister_dtl(hwcpu);
177 }
178
179 static u64 dtl_current_index(struct dtl *dtl)
180 {
181         return be64_to_cpu(lppaca_of(dtl->cpu).dtl_idx);
182 }
183 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
184
185 static int dtl_enable(struct dtl *dtl)
186 {
187         long int n_entries;
188         long int rc;
189         struct dtl_entry *buf = NULL;
190
191         if (!dtl_cache)
192                 return -ENOMEM;
193
194         /* only allow one reader */
195         if (dtl->buf)
196                 return -EBUSY;
197
198         n_entries = dtl_buf_entries;
199         buf = kmem_cache_alloc_node(dtl_cache, GFP_KERNEL, cpu_to_node(dtl->cpu));
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)
220                 kmem_cache_free(dtl_cache, buf);
221         return rc;
222 }
223
224 static void dtl_disable(struct dtl *dtl)
225 {
226         spin_lock(&dtl->lock);
227         dtl_stop(dtl);
228         kmem_cache_free(dtl_cache, dtl->buf);
229         dtl->buf = NULL;
230         dtl->buf_entries = 0;
231         spin_unlock(&dtl->lock);
232 }
233
234 /* file interface */
235
236 static 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
249 static 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
256 static ssize_t dtl_file_read(struct file *filp, char __user *buf, size_t len,
257                 loff_t *pos)
258 {
259         long int rc, n_read, n_req, read_size;
260         struct dtl *dtl;
261         u64 cur_idx, last_idx, i;
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
274         spin_lock(&dtl->lock);
275
276         cur_idx = dtl_current_index(dtl);
277         last_idx = dtl->last_idx;
278
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;
284
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;
294
295         /* read the tail of the buffer if we've wrapped */
296         if (i + n_req > dtl->buf_entries) {
297                 read_size = dtl->buf_entries - i;
298
299                 rc = copy_to_user(buf, &dtl->buf[i],
300                                 read_size * sizeof(struct dtl_entry));
301                 if (rc)
302                         return -EFAULT;
303
304                 i = 0;
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 */
311         rc = copy_to_user(buf, &dtl->buf[i], n_req * sizeof(struct dtl_entry));
312         if (rc)
313                 return -EFAULT;
314
315         n_read += n_req;
316
317         return n_read * sizeof(struct dtl_entry);
318 }
319
320 static const struct file_operations dtl_fops = {
321         .open           = dtl_file_open,
322         .release        = dtl_file_release,
323         .read           = dtl_file_read,
324         .llseek         = no_llseek,
325 };
326
327 static struct dentry *dtl_dir;
328
329 static 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
342 static 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);
362         buf_entries_file = debugfs_create_u32("dtl_buf_entries", 0400,
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) {
372                 struct dtl *dtl = &per_cpu(cpu_dtl, i);
373                 spin_lock_init(&dtl->lock);
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
383 err_remove_dir:
384         debugfs_remove_recursive(dtl_dir);
385 err:
386         return rc;
387 }
388 machine_arch_initcall(pseries, dtl_init);