1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/debugfs.h>
26#include <linux/spinlock.h>
27#include <asm/smp.h>
28#include <asm/system.h>
29#include <asm/uaccess.h>
30#include <asm/firmware.h>
31#include <asm/lppaca.h>
32
33#include "plpar_wrappers.h"
34
35struct dtl {
36 struct dtl_entry *buf;
37 struct dentry *file;
38 int cpu;
39 int buf_entries;
40 u64 last_idx;
41 spinlock_t lock;
42};
43static DEFINE_PER_CPU(struct dtl, cpu_dtl);
44
45
46
47
48
49
50
51static u8 dtl_event_mask = 0x7;
52
53
54
55
56
57static int dtl_buf_entries = (16 * 85);
58
59
60#ifdef CONFIG_VIRT_CPU_ACCOUNTING
61struct 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
69static DEFINE_PER_CPU(struct dtl_ring, dtl_rings);
70
71static atomic_t dtl_count;
72
73
74
75
76
77static void consume_dtle(struct dtl_entry *dtle, u64 index)
78{
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;
82
83 if (!wp)
84 return;
85
86 *wp = *dtle;
87 barrier();
88
89
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
99 smp_wmb();
100 ++dtlr->write_index;
101}
102
103static 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
112 smp_wmb();
113 dtlr->write_ptr = dtl->buf;
114
115
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
124static 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
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
140static u64 dtl_current_index(struct dtl *dtl)
141{
142 return per_cpu(dtl_rings, dtl->cpu).write_index;
143}
144
145#else
146
147static int dtl_start(struct dtl *dtl)
148{
149 unsigned long addr;
150 int ret, hwcpu;
151
152
153
154 ((u32 *)dtl->buf)[1] = dtl->buf_entries * sizeof(struct dtl_entry);
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);
162 return -EIO;
163 }
164
165
166 lppaca_of(dtl->cpu).dtl_idx = 0;
167
168
169
170 smp_wmb();
171
172
173 lppaca_of(dtl->cpu).dtl_enable_mask = dtl_event_mask;
174
175 return 0;
176}
177
178static void dtl_stop(struct dtl *dtl)
179{
180 int hwcpu = get_hard_smp_processor_id(dtl->cpu);
181
182 lppaca_of(dtl->cpu).dtl_enable_mask = 0x0;
183
184 unregister_dtl(hwcpu, __pa(dtl->buf));
185}
186
187static u64 dtl_current_index(struct dtl *dtl)
188{
189 return lppaca_of(dtl->cpu).dtl_idx;
190}
191#endif
192
193static int dtl_enable(struct dtl *dtl)
194{
195 long int n_entries;
196 long int rc;
197 struct dtl_entry *buf = NULL;
198
199
200 if (dtl->buf)
201 return -EBUSY;
202
203 n_entries = dtl_buf_entries;
204 buf = kmalloc_node(n_entries * sizeof(struct dtl_entry),
205 GFP_KERNEL, cpu_to_node(dtl->cpu));
206 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
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)
226 kfree(buf);
227 return rc;
228}
229
230static void dtl_disable(struct dtl *dtl)
231{
232 spin_lock(&dtl->lock);
233 dtl_stop(dtl);
234 kfree(dtl->buf);
235 dtl->buf = NULL;
236 dtl->buf_entries = 0;
237 spin_unlock(&dtl->lock);
238}
239
240
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{
265 long int rc, n_read, n_req, read_size;
266 struct dtl *dtl;
267 u64 cur_idx, last_idx, i;
268
269 if ((len % sizeof(struct dtl_entry)) != 0)
270 return -EINVAL;
271
272 dtl = filp->private_data;
273
274
275 n_req = len / sizeof(struct dtl_entry);
276
277
278 n_read = 0;
279
280 spin_lock(&dtl->lock);
281
282 cur_idx = dtl_current_index(dtl);
283 last_idx = dtl->last_idx;
284
285 if (last_idx + dtl->buf_entries <= cur_idx)
286 last_idx = cur_idx - dtl->buf_entries + 1;
287
288 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;
300
301
302 if (i + n_req > dtl->buf_entries) {
303 read_size = dtl->buf_entries - i;
304
305 rc = copy_to_user(buf, &dtl->buf[i],
306 read_size * sizeof(struct dtl_entry));
307 if (rc)
308 return -EFAULT;
309
310 i = 0;
311 n_req -= read_size;
312 n_read += read_size;
313 buf += read_size * sizeof(struct dtl_entry);
314 }
315
316
317 rc = copy_to_user(buf, &dtl->buf[i], n_req * sizeof(struct dtl_entry));
318 if (rc)
319 return -EFAULT;
320
321 n_read += n_req;
322
323 return n_read * sizeof(struct dtl_entry);
324}
325
326static const struct file_operations dtl_fops = {
327 .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
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);
368 buf_entries_file = debugfs_create_u32("dtl_buf_entries", 0600,
369 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
377 for_each_possible_cpu(i) {
378 struct dtl *dtl = &per_cpu(cpu_dtl, i);
379 spin_lock_init(&dtl->lock);
380 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);
395