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