]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/powerpc/oprofile/cell/spu_task_sync.c
leds/acpi: Fix merge fallout from acpi_driver_data change
[linux-2.6-omap-h63xx.git] / arch / powerpc / oprofile / cell / spu_task_sync.c
1 /*
2  * Cell Broadband Engine OProfile Support
3  *
4  * (C) Copyright IBM Corporation 2006
5  *
6  * Author: Maynard Johnson <maynardj@us.ibm.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version
11  * 2 of the License, or (at your option) any later version.
12  */
13
14 /* The purpose of this file is to handle SPU event task switching
15  * and to record SPU context information into the OProfile
16  * event buffer.
17  *
18  * Additionally, the spu_sync_buffer function is provided as a helper
19  * for recoding actual SPU program counter samples to the event buffer.
20  */
21 #include <linux/dcookies.h>
22 #include <linux/kref.h>
23 #include <linux/mm.h>
24 #include <linux/fs.h>
25 #include <linux/module.h>
26 #include <linux/notifier.h>
27 #include <linux/numa.h>
28 #include <linux/oprofile.h>
29 #include <linux/spinlock.h>
30 #include "pr_util.h"
31
32 #define RELEASE_ALL 9999
33
34 static DEFINE_SPINLOCK(buffer_lock);
35 static DEFINE_SPINLOCK(cache_lock);
36 static int num_spu_nodes;
37 int spu_prof_num_nodes;
38 int last_guard_val[MAX_NUMNODES * 8];
39
40 /* Container for caching information about an active SPU task. */
41 struct cached_info {
42         struct vma_to_fileoffset_map *map;
43         struct spu *the_spu;    /* needed to access pointer to local_store */
44         struct kref cache_ref;
45 };
46
47 static struct cached_info *spu_info[MAX_NUMNODES * 8];
48
49 static void destroy_cached_info(struct kref *kref)
50 {
51         struct cached_info *info;
52
53         info = container_of(kref, struct cached_info, cache_ref);
54         vma_map_free(info->map);
55         kfree(info);
56         module_put(THIS_MODULE);
57 }
58
59 /* Return the cached_info for the passed SPU number.
60  * ATTENTION:  Callers are responsible for obtaining the
61  *             cache_lock if needed prior to invoking this function.
62  */
63 static struct cached_info *get_cached_info(struct spu *the_spu, int spu_num)
64 {
65         struct kref *ref;
66         struct cached_info *ret_info;
67
68         if (spu_num >= num_spu_nodes) {
69                 printk(KERN_ERR "SPU_PROF: "
70                        "%s, line %d: Invalid index %d into spu info cache\n",
71                        __func__, __LINE__, spu_num);
72                 ret_info = NULL;
73                 goto out;
74         }
75         if (!spu_info[spu_num] && the_spu) {
76                 ref = spu_get_profile_private_kref(the_spu->ctx);
77                 if (ref) {
78                         spu_info[spu_num] = container_of(ref, struct cached_info, cache_ref);
79                         kref_get(&spu_info[spu_num]->cache_ref);
80                 }
81         }
82
83         ret_info = spu_info[spu_num];
84  out:
85         return ret_info;
86 }
87
88
89 /* Looks for cached info for the passed spu.  If not found, the
90  * cached info is created for the passed spu.
91  * Returns 0 for success; otherwise, -1 for error.
92  */
93 static int
94 prepare_cached_spu_info(struct spu *spu, unsigned long objectId)
95 {
96         unsigned long flags;
97         struct vma_to_fileoffset_map *new_map;
98         int retval = 0;
99         struct cached_info *info;
100
101         /* We won't bother getting cache_lock here since
102          * don't do anything with the cached_info that's returned.
103          */
104         info = get_cached_info(spu, spu->number);
105
106         if (info) {
107                 pr_debug("Found cached SPU info.\n");
108                 goto out;
109         }
110
111         /* Create cached_info and set spu_info[spu->number] to point to it.
112          * spu->number is a system-wide value, not a per-node value.
113          */
114         info = kzalloc(sizeof(struct cached_info), GFP_KERNEL);
115         if (!info) {
116                 printk(KERN_ERR "SPU_PROF: "
117                        "%s, line %d: create vma_map failed\n",
118                        __func__, __LINE__);
119                 retval = -ENOMEM;
120                 goto err_alloc;
121         }
122         new_map = create_vma_map(spu, objectId);
123         if (!new_map) {
124                 printk(KERN_ERR "SPU_PROF: "
125                        "%s, line %d: create vma_map failed\n",
126                        __func__, __LINE__);
127                 retval = -ENOMEM;
128                 goto err_alloc;
129         }
130
131         pr_debug("Created vma_map\n");
132         info->map = new_map;
133         info->the_spu = spu;
134         kref_init(&info->cache_ref);
135         spin_lock_irqsave(&cache_lock, flags);
136         spu_info[spu->number] = info;
137         /* Increment count before passing off ref to SPUFS. */
138         kref_get(&info->cache_ref);
139
140         /* We increment the module refcount here since SPUFS is
141          * responsible for the final destruction of the cached_info,
142          * and it must be able to access the destroy_cached_info()
143          * function defined in the OProfile module.  We decrement
144          * the module refcount in destroy_cached_info.
145          */
146         try_module_get(THIS_MODULE);
147         spu_set_profile_private_kref(spu->ctx, &info->cache_ref,
148                                 destroy_cached_info);
149         spin_unlock_irqrestore(&cache_lock, flags);
150         goto out;
151
152 err_alloc:
153         kfree(info);
154 out:
155         return retval;
156 }
157
158 /*
159  * NOTE:  The caller is responsible for locking the
160  *        cache_lock prior to calling this function.
161  */
162 static int release_cached_info(int spu_index)
163 {
164         int index, end;
165
166         if (spu_index == RELEASE_ALL) {
167                 end = num_spu_nodes;
168                 index = 0;
169         } else {
170                 if (spu_index >= num_spu_nodes) {
171                         printk(KERN_ERR "SPU_PROF: "
172                                 "%s, line %d: "
173                                 "Invalid index %d into spu info cache\n",
174                                 __func__, __LINE__, spu_index);
175                         goto out;
176                 }
177                 end = spu_index + 1;
178                 index = spu_index;
179         }
180         for (; index < end; index++) {
181                 if (spu_info[index]) {
182                         kref_put(&spu_info[index]->cache_ref,
183                                  destroy_cached_info);
184                         spu_info[index] = NULL;
185                 }
186         }
187
188 out:
189         return 0;
190 }
191
192 /* The source code for fast_get_dcookie was "borrowed"
193  * from drivers/oprofile/buffer_sync.c.
194  */
195
196 /* Optimisation. We can manage without taking the dcookie sem
197  * because we cannot reach this code without at least one
198  * dcookie user still being registered (namely, the reader
199  * of the event buffer).
200  */
201 static inline unsigned long fast_get_dcookie(struct path *path)
202 {
203         unsigned long cookie;
204
205         if (path->dentry->d_cookie)
206                 return (unsigned long)path->dentry;
207         get_dcookie(path, &cookie);
208         return cookie;
209 }
210
211 /* Look up the dcookie for the task's first VM_EXECUTABLE mapping,
212  * which corresponds loosely to "application name". Also, determine
213  * the offset for the SPU ELF object.  If computed offset is
214  * non-zero, it implies an embedded SPU object; otherwise, it's a
215  * separate SPU binary, in which case we retrieve it's dcookie.
216  * For the embedded case, we must determine if SPU ELF is embedded
217  * in the executable application or another file (i.e., shared lib).
218  * If embedded in a shared lib, we must get the dcookie and return
219  * that to the caller.
220  */
221 static unsigned long
222 get_exec_dcookie_and_offset(struct spu *spu, unsigned int *offsetp,
223                             unsigned long *spu_bin_dcookie,
224                             unsigned long spu_ref)
225 {
226         unsigned long app_cookie = 0;
227         unsigned int my_offset = 0;
228         struct file *app = NULL;
229         struct vm_area_struct *vma;
230         struct mm_struct *mm = spu->mm;
231
232         if (!mm)
233                 goto out;
234
235         down_read(&mm->mmap_sem);
236
237         for (vma = mm->mmap; vma; vma = vma->vm_next) {
238                 if (!vma->vm_file)
239                         continue;
240                 if (!(vma->vm_flags & VM_EXECUTABLE))
241                         continue;
242                 app_cookie = fast_get_dcookie(&vma->vm_file->f_path);
243                 pr_debug("got dcookie for %s\n",
244                          vma->vm_file->f_dentry->d_name.name);
245                 app = vma->vm_file;
246                 break;
247         }
248
249         for (vma = mm->mmap; vma; vma = vma->vm_next) {
250                 if (vma->vm_start > spu_ref || vma->vm_end <= spu_ref)
251                         continue;
252                 my_offset = spu_ref - vma->vm_start;
253                 if (!vma->vm_file)
254                         goto fail_no_image_cookie;
255
256                 pr_debug("Found spu ELF at %X(object-id:%lx) for file %s\n",
257                          my_offset, spu_ref,
258                          vma->vm_file->f_dentry->d_name.name);
259                 *offsetp = my_offset;
260                 break;
261         }
262
263         *spu_bin_dcookie = fast_get_dcookie(&vma->vm_file->f_path);
264         pr_debug("got dcookie for %s\n", vma->vm_file->f_dentry->d_name.name);
265
266         up_read(&mm->mmap_sem);
267
268 out:
269         return app_cookie;
270
271 fail_no_image_cookie:
272         up_read(&mm->mmap_sem);
273
274         printk(KERN_ERR "SPU_PROF: "
275                 "%s, line %d: Cannot find dcookie for SPU binary\n",
276                 __func__, __LINE__);
277         goto out;
278 }
279
280
281
282 /* This function finds or creates cached context information for the
283  * passed SPU and records SPU context information into the OProfile
284  * event buffer.
285  */
286 static int process_context_switch(struct spu *spu, unsigned long objectId)
287 {
288         unsigned long flags;
289         int retval;
290         unsigned int offset = 0;
291         unsigned long spu_cookie = 0, app_dcookie;
292
293         retval = prepare_cached_spu_info(spu, objectId);
294         if (retval)
295                 goto out;
296
297         /* Get dcookie first because a mutex_lock is taken in that
298          * code path, so interrupts must not be disabled.
299          */
300         app_dcookie = get_exec_dcookie_and_offset(spu, &offset, &spu_cookie, objectId);
301         if (!app_dcookie || !spu_cookie) {
302                 retval  = -ENOENT;
303                 goto out;
304         }
305
306         /* Record context info in event buffer */
307         spin_lock_irqsave(&buffer_lock, flags);
308         add_event_entry(ESCAPE_CODE);
309         add_event_entry(SPU_CTX_SWITCH_CODE);
310         add_event_entry(spu->number);
311         add_event_entry(spu->pid);
312         add_event_entry(spu->tgid);
313         add_event_entry(app_dcookie);
314         add_event_entry(spu_cookie);
315         add_event_entry(offset);
316         spin_unlock_irqrestore(&buffer_lock, flags);
317         smp_wmb();      /* insure spu event buffer updates are written */
318                         /* don't want entries intermingled... */
319 out:
320         return retval;
321 }
322
323 /*
324  * This function is invoked on either a bind_context or unbind_context.
325  * If called for an unbind_context, the val arg is 0; otherwise,
326  * it is the object-id value for the spu context.
327  * The data arg is of type 'struct spu *'.
328  */
329 static int spu_active_notify(struct notifier_block *self, unsigned long val,
330                                 void *data)
331 {
332         int retval;
333         unsigned long flags;
334         struct spu *the_spu = data;
335
336         pr_debug("SPU event notification arrived\n");
337         if (!val) {
338                 spin_lock_irqsave(&cache_lock, flags);
339                 retval = release_cached_info(the_spu->number);
340                 spin_unlock_irqrestore(&cache_lock, flags);
341         } else {
342                 retval = process_context_switch(the_spu, val);
343         }
344         return retval;
345 }
346
347 static struct notifier_block spu_active = {
348         .notifier_call = spu_active_notify,
349 };
350
351 static int number_of_online_nodes(void)
352 {
353         u32 cpu; u32 tmp;
354         int nodes = 0;
355         for_each_online_cpu(cpu) {
356                 tmp = cbe_cpu_to_node(cpu) + 1;
357                 if (tmp > nodes)
358                         nodes++;
359         }
360         return nodes;
361 }
362
363 /* The main purpose of this function is to synchronize
364  * OProfile with SPUFS by registering to be notified of
365  * SPU task switches.
366  *
367  * NOTE: When profiling SPUs, we must ensure that only
368  * spu_sync_start is invoked and not the generic sync_start
369  * in drivers/oprofile/oprof.c.  A return value of
370  * SKIP_GENERIC_SYNC or SYNC_START_ERROR will
371  * accomplish this.
372  */
373 int spu_sync_start(void)
374 {
375         int k;
376         int ret = SKIP_GENERIC_SYNC;
377         int register_ret;
378         unsigned long flags = 0;
379
380         spu_prof_num_nodes = number_of_online_nodes();
381         num_spu_nodes = spu_prof_num_nodes * 8;
382
383         spin_lock_irqsave(&buffer_lock, flags);
384         add_event_entry(ESCAPE_CODE);
385         add_event_entry(SPU_PROFILING_CODE);
386         add_event_entry(num_spu_nodes);
387         spin_unlock_irqrestore(&buffer_lock, flags);
388
389         /* Register for SPU events  */
390         register_ret = spu_switch_event_register(&spu_active);
391         if (register_ret) {
392                 ret = SYNC_START_ERROR;
393                 goto out;
394         }
395
396         for (k = 0; k < (MAX_NUMNODES * 8); k++)
397                 last_guard_val[k] = 0;
398         pr_debug("spu_sync_start -- running.\n");
399 out:
400         return ret;
401 }
402
403 /* Record SPU program counter samples to the oprofile event buffer. */
404 void spu_sync_buffer(int spu_num, unsigned int *samples,
405                      int num_samples)
406 {
407         unsigned long long file_offset;
408         unsigned long flags;
409         int i;
410         struct vma_to_fileoffset_map *map;
411         struct spu *the_spu;
412         unsigned long long spu_num_ll = spu_num;
413         unsigned long long spu_num_shifted = spu_num_ll << 32;
414         struct cached_info *c_info;
415
416         /* We need to obtain the cache_lock here because it's
417          * possible that after getting the cached_info, the SPU job
418          * corresponding to this cached_info may end, thus resulting
419          * in the destruction of the cached_info.
420          */
421         spin_lock_irqsave(&cache_lock, flags);
422         c_info = get_cached_info(NULL, spu_num);
423         if (!c_info) {
424                 /* This legitimately happens when the SPU task ends before all
425                  * samples are recorded.
426                  * No big deal -- so we just drop a few samples.
427                  */
428                 pr_debug("SPU_PROF: No cached SPU contex "
429                           "for SPU #%d. Dropping samples.\n", spu_num);
430                 goto out;
431         }
432
433         map = c_info->map;
434         the_spu = c_info->the_spu;
435         spin_lock(&buffer_lock);
436         for (i = 0; i < num_samples; i++) {
437                 unsigned int sample = *(samples+i);
438                 int grd_val = 0;
439                 file_offset = 0;
440                 if (sample == 0)
441                         continue;
442                 file_offset = vma_map_lookup( map, sample, the_spu, &grd_val);
443
444                 /* If overlays are used by this SPU application, the guard
445                  * value is non-zero, indicating which overlay section is in
446                  * use.  We need to discard samples taken during the time
447                  * period which an overlay occurs (i.e., guard value changes).
448                  */
449                 if (grd_val && grd_val != last_guard_val[spu_num]) {
450                         last_guard_val[spu_num] = grd_val;
451                         /* Drop the rest of the samples. */
452                         break;
453                 }
454
455                 add_event_entry(file_offset | spu_num_shifted);
456         }
457         spin_unlock(&buffer_lock);
458 out:
459         spin_unlock_irqrestore(&cache_lock, flags);
460 }
461
462
463 int spu_sync_stop(void)
464 {
465         unsigned long flags = 0;
466         int ret = spu_switch_event_unregister(&spu_active);
467         if (ret) {
468                 printk(KERN_ERR "SPU_PROF: "
469                         "%s, line %d: spu_switch_event_unregister returned %d\n",
470                         __func__, __LINE__, ret);
471                 goto out;
472         }
473
474         spin_lock_irqsave(&cache_lock, flags);
475         ret = release_cached_info(RELEASE_ALL);
476         spin_unlock_irqrestore(&cache_lock, flags);
477 out:
478         pr_debug("spu_sync_stop -- done.\n");
479         return ret;
480 }
481
482