d21eb53ba19986348e926f8f1ebfcb5e3a53197f
[firefly-linux-kernel-4.4.55.git] / drivers / hwtracing / stm / core.c
1 /*
2  * System Trace Module (STM) infrastructure
3  * Copyright (c) 2014, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * STM class implements generic infrastructure for  System Trace Module devices
15  * as defined in MIPI STPv2 specification.
16  */
17
18 #include <linux/uaccess.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/device.h>
22 #include <linux/compat.h>
23 #include <linux/kdev_t.h>
24 #include <linux/srcu.h>
25 #include <linux/slab.h>
26 #include <linux/stm.h>
27 #include <linux/fs.h>
28 #include <linux/mm.h>
29 #include "stm.h"
30
31 #include <uapi/linux/stm.h>
32
33 static unsigned int stm_core_up;
34
35 /*
36  * The SRCU here makes sure that STM device doesn't disappear from under a
37  * stm_source_write() caller, which may want to have as little overhead as
38  * possible.
39  */
40 static struct srcu_struct stm_source_srcu;
41
42 static ssize_t masters_show(struct device *dev,
43                             struct device_attribute *attr,
44                             char *buf)
45 {
46         struct stm_device *stm = to_stm_device(dev);
47         int ret;
48
49         ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
50
51         return ret;
52 }
53
54 static DEVICE_ATTR_RO(masters);
55
56 static ssize_t channels_show(struct device *dev,
57                              struct device_attribute *attr,
58                              char *buf)
59 {
60         struct stm_device *stm = to_stm_device(dev);
61         int ret;
62
63         ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
64
65         return ret;
66 }
67
68 static DEVICE_ATTR_RO(channels);
69
70 static struct attribute *stm_attrs[] = {
71         &dev_attr_masters.attr,
72         &dev_attr_channels.attr,
73         NULL,
74 };
75
76 ATTRIBUTE_GROUPS(stm);
77
78 static struct class stm_class = {
79         .name           = "stm",
80         .dev_groups     = stm_groups,
81 };
82
83 static int stm_dev_match(struct device *dev, const void *data)
84 {
85         const char *name = data;
86
87         return sysfs_streq(name, dev_name(dev));
88 }
89
90 /**
91  * stm_find_device() - find stm device by name
92  * @buf:        character buffer containing the name
93  *
94  * This is called when either policy gets assigned to an stm device or an
95  * stm_source device gets linked to an stm device.
96  *
97  * This grabs device's reference (get_device()) and module reference, both
98  * of which the calling path needs to make sure to drop with stm_put_device().
99  *
100  * Return:      stm device pointer or null if lookup failed.
101  */
102 struct stm_device *stm_find_device(const char *buf)
103 {
104         struct stm_device *stm;
105         struct device *dev;
106
107         if (!stm_core_up)
108                 return NULL;
109
110         dev = class_find_device(&stm_class, NULL, buf, stm_dev_match);
111         if (!dev)
112                 return NULL;
113
114         stm = to_stm_device(dev);
115         if (!try_module_get(stm->owner)) {
116                 /* matches class_find_device() above */
117                 put_device(dev);
118                 return NULL;
119         }
120
121         return stm;
122 }
123
124 /**
125  * stm_put_device() - drop references on the stm device
126  * @stm:        stm device, previously acquired by stm_find_device()
127  *
128  * This drops the module reference and device reference taken by
129  * stm_find_device() or stm_char_open().
130  */
131 void stm_put_device(struct stm_device *stm)
132 {
133         module_put(stm->owner);
134         put_device(&stm->dev);
135 }
136
137 /*
138  * Internally we only care about software-writable masters here, that is the
139  * ones in the range [stm_data->sw_start..stm_data..sw_end], however we need
140  * original master numbers to be visible externally, since they are the ones
141  * that will appear in the STP stream. Thus, the internal bookkeeping uses
142  * $master - stm_data->sw_start to reference master descriptors and such.
143  */
144
145 #define __stm_master(_s, _m)                            \
146         ((_s)->masters[(_m) - (_s)->data->sw_start])
147
148 static inline struct stp_master *
149 stm_master(struct stm_device *stm, unsigned int idx)
150 {
151         if (idx < stm->data->sw_start || idx > stm->data->sw_end)
152                 return NULL;
153
154         return __stm_master(stm, idx);
155 }
156
157 static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
158 {
159         struct stp_master *master;
160         size_t size;
161
162         size = ALIGN(stm->data->sw_nchannels, 8) / 8;
163         size += sizeof(struct stp_master);
164         master = kzalloc(size, GFP_ATOMIC);
165         if (!master)
166                 return -ENOMEM;
167
168         master->nr_free = stm->data->sw_nchannels;
169         __stm_master(stm, idx) = master;
170
171         return 0;
172 }
173
174 static void stp_master_free(struct stm_device *stm, unsigned int idx)
175 {
176         struct stp_master *master = stm_master(stm, idx);
177
178         if (!master)
179                 return;
180
181         __stm_master(stm, idx) = NULL;
182         kfree(master);
183 }
184
185 static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
186 {
187         struct stp_master *master = stm_master(stm, output->master);
188
189         lockdep_assert_held(&stm->mc_lock);
190         lockdep_assert_held(&output->lock);
191
192         if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
193                 return;
194
195         bitmap_allocate_region(&master->chan_map[0], output->channel,
196                                ilog2(output->nr_chans));
197
198         master->nr_free -= output->nr_chans;
199 }
200
201 static void
202 stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
203 {
204         struct stp_master *master = stm_master(stm, output->master);
205
206         lockdep_assert_held(&stm->mc_lock);
207         lockdep_assert_held(&output->lock);
208
209         bitmap_release_region(&master->chan_map[0], output->channel,
210                               ilog2(output->nr_chans));
211
212         output->nr_chans = 0;
213         master->nr_free += output->nr_chans;
214 }
215
216 /*
217  * This is like bitmap_find_free_region(), except it can ignore @start bits
218  * at the beginning.
219  */
220 static int find_free_channels(unsigned long *bitmap, unsigned int start,
221                               unsigned int end, unsigned int width)
222 {
223         unsigned int pos;
224         int i;
225
226         for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
227                 pos = find_next_zero_bit(bitmap, end + 1, pos);
228                 if (pos + width > end + 1)
229                         break;
230
231                 if (pos & (width - 1))
232                         continue;
233
234                 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
235                         ;
236                 if (i == width)
237                         return pos;
238         }
239
240         return -1;
241 }
242
243 static unsigned int
244 stm_find_master_chan(struct stm_device *stm, unsigned int width,
245                      unsigned int *mstart, unsigned int mend,
246                      unsigned int *cstart, unsigned int cend)
247 {
248         struct stp_master *master;
249         unsigned int midx;
250         int pos, err;
251
252         for (midx = *mstart; midx <= mend; midx++) {
253                 if (!stm_master(stm, midx)) {
254                         err = stp_master_alloc(stm, midx);
255                         if (err)
256                                 return err;
257                 }
258
259                 master = stm_master(stm, midx);
260
261                 if (!master->nr_free)
262                         continue;
263
264                 pos = find_free_channels(master->chan_map, *cstart, cend,
265                                          width);
266                 if (pos < 0)
267                         continue;
268
269                 *mstart = midx;
270                 *cstart = pos;
271                 return 0;
272         }
273
274         return -ENOSPC;
275 }
276
277 static int stm_output_assign(struct stm_device *stm, unsigned int width,
278                              struct stp_policy_node *policy_node,
279                              struct stm_output *output)
280 {
281         unsigned int midx, cidx, mend, cend;
282         int ret = -EINVAL;
283
284         if (width > stm->data->sw_nchannels)
285                 return -EINVAL;
286
287         if (policy_node) {
288                 stp_policy_node_get_ranges(policy_node,
289                                            &midx, &mend, &cidx, &cend);
290         } else {
291                 midx = stm->data->sw_start;
292                 cidx = 0;
293                 mend = stm->data->sw_end;
294                 cend = stm->data->sw_nchannels - 1;
295         }
296
297         spin_lock(&stm->mc_lock);
298         spin_lock(&output->lock);
299         /* output is already assigned -- shouldn't happen */
300         if (WARN_ON_ONCE(output->nr_chans))
301                 goto unlock;
302
303         ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
304         if (ret)
305                 goto unlock;
306
307         output->master = midx;
308         output->channel = cidx;
309         output->nr_chans = width;
310         stm_output_claim(stm, output);
311         dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
312
313         ret = 0;
314 unlock:
315         spin_unlock(&output->lock);
316         spin_unlock(&stm->mc_lock);
317
318         return ret;
319 }
320
321 static void stm_output_free(struct stm_device *stm, struct stm_output *output)
322 {
323         spin_lock(&stm->mc_lock);
324         spin_lock(&output->lock);
325         if (output->nr_chans)
326                 stm_output_disclaim(stm, output);
327         spin_unlock(&output->lock);
328         spin_unlock(&stm->mc_lock);
329 }
330
331 static void stm_output_init(struct stm_output *output)
332 {
333         spin_lock_init(&output->lock);
334 }
335
336 static int major_match(struct device *dev, const void *data)
337 {
338         unsigned int major = *(unsigned int *)data;
339
340         return MAJOR(dev->devt) == major;
341 }
342
343 static int stm_char_open(struct inode *inode, struct file *file)
344 {
345         struct stm_file *stmf;
346         struct device *dev;
347         unsigned int major = imajor(inode);
348         int err = -ENODEV;
349
350         dev = class_find_device(&stm_class, NULL, &major, major_match);
351         if (!dev)
352                 return -ENODEV;
353
354         stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
355         if (!stmf)
356                 return -ENOMEM;
357
358         stm_output_init(&stmf->output);
359         stmf->stm = to_stm_device(dev);
360
361         if (!try_module_get(stmf->stm->owner))
362                 goto err_free;
363
364         file->private_data = stmf;
365
366         return nonseekable_open(inode, file);
367
368 err_free:
369         /* matches class_find_device() above */
370         put_device(dev);
371         kfree(stmf);
372
373         return err;
374 }
375
376 static int stm_char_release(struct inode *inode, struct file *file)
377 {
378         struct stm_file *stmf = file->private_data;
379         struct stm_device *stm = stmf->stm;
380
381         if (stm->data->unlink)
382                 stm->data->unlink(stm->data, stmf->output.master,
383                                   stmf->output.channel);
384
385         stm_output_free(stm, &stmf->output);
386
387         /*
388          * matches the stm_char_open()'s
389          * class_find_device() + try_module_get()
390          */
391         stm_put_device(stm);
392         kfree(stmf);
393
394         return 0;
395 }
396
397 static int stm_file_assign(struct stm_file *stmf, char *id, unsigned int width)
398 {
399         struct stm_device *stm = stmf->stm;
400         int ret;
401
402         stmf->policy_node = stp_policy_node_lookup(stm, id);
403
404         ret = stm_output_assign(stm, width, stmf->policy_node, &stmf->output);
405
406         if (stmf->policy_node)
407                 stp_policy_node_put(stmf->policy_node);
408
409         return ret;
410 }
411
412 static ssize_t stm_write(struct stm_data *data, unsigned int master,
413                           unsigned int channel, const char *buf, size_t count)
414 {
415         unsigned int flags = STP_PACKET_TIMESTAMPED;
416         const unsigned char *p = buf, nil = 0;
417         size_t pos;
418         ssize_t sz;
419
420         for (pos = 0, p = buf; count > pos; pos += sz, p += sz) {
421                 sz = min_t(unsigned int, count - pos, 8);
422                 sz = data->packet(data, master, channel, STP_PACKET_DATA, flags,
423                                   sz, p);
424                 flags = 0;
425
426                 if (sz < 0)
427                         break;
428         }
429
430         data->packet(data, master, channel, STP_PACKET_FLAG, 0, 0, &nil);
431
432         return pos;
433 }
434
435 static ssize_t stm_char_write(struct file *file, const char __user *buf,
436                               size_t count, loff_t *ppos)
437 {
438         struct stm_file *stmf = file->private_data;
439         struct stm_device *stm = stmf->stm;
440         char *kbuf;
441         int err;
442
443         if (count + 1 > PAGE_SIZE)
444                 count = PAGE_SIZE - 1;
445
446         /*
447          * if no m/c have been assigned to this writer up to this
448          * point, use "default" policy entry
449          */
450         if (!stmf->output.nr_chans) {
451                 err = stm_file_assign(stmf, "default", 1);
452                 /*
453                  * EBUSY means that somebody else just assigned this
454                  * output, which is just fine for write()
455                  */
456                 if (err && err != -EBUSY)
457                         return err;
458         }
459
460         kbuf = kmalloc(count + 1, GFP_KERNEL);
461         if (!kbuf)
462                 return -ENOMEM;
463
464         err = copy_from_user(kbuf, buf, count);
465         if (err) {
466                 kfree(kbuf);
467                 return -EFAULT;
468         }
469
470         count = stm_write(stm->data, stmf->output.master, stmf->output.channel,
471                           kbuf, count);
472
473         kfree(kbuf);
474
475         return count;
476 }
477
478 static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
479 {
480         struct stm_file *stmf = file->private_data;
481         struct stm_device *stm = stmf->stm;
482         unsigned long size, phys;
483
484         if (!stm->data->mmio_addr)
485                 return -EOPNOTSUPP;
486
487         if (vma->vm_pgoff)
488                 return -EINVAL;
489
490         size = vma->vm_end - vma->vm_start;
491
492         if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
493                 return -EINVAL;
494
495         phys = stm->data->mmio_addr(stm->data, stmf->output.master,
496                                     stmf->output.channel,
497                                     stmf->output.nr_chans);
498
499         if (!phys)
500                 return -EINVAL;
501
502         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
503         vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
504         vm_iomap_memory(vma, phys, size);
505
506         return 0;
507 }
508
509 static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
510 {
511         struct stm_device *stm = stmf->stm;
512         struct stp_policy_id *id;
513         int ret = -EINVAL;
514         u32 size;
515
516         if (stmf->output.nr_chans)
517                 return -EBUSY;
518
519         if (copy_from_user(&size, arg, sizeof(size)))
520                 return -EFAULT;
521
522         if (size >= PATH_MAX + sizeof(*id))
523                 return -EINVAL;
524
525         /*
526          * size + 1 to make sure the .id string at the bottom is terminated,
527          * which is also why memdup_user() is not useful here
528          */
529         id = kzalloc(size + 1, GFP_KERNEL);
530         if (!id)
531                 return -ENOMEM;
532
533         if (copy_from_user(id, arg, size)) {
534                 ret = -EFAULT;
535                 goto err_free;
536         }
537
538         if (id->__reserved_0 || id->__reserved_1)
539                 goto err_free;
540
541         if (id->width < 1 ||
542             id->width > PAGE_SIZE / stm->data->sw_mmiosz)
543                 goto err_free;
544
545         ret = stm_file_assign(stmf, id->id, id->width);
546         if (ret)
547                 goto err_free;
548
549         if (stm->data->link)
550                 ret = stm->data->link(stm->data, stmf->output.master,
551                                       stmf->output.channel);
552
553         if (ret)
554                 stm_output_free(stmf->stm, &stmf->output);
555
556 err_free:
557         kfree(id);
558
559         return ret;
560 }
561
562 static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
563 {
564         struct stp_policy_id id = {
565                 .size           = sizeof(id),
566                 .master         = stmf->output.master,
567                 .channel        = stmf->output.channel,
568                 .width          = stmf->output.nr_chans,
569                 .__reserved_0   = 0,
570                 .__reserved_1   = 0,
571         };
572
573         return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
574 }
575
576 static long
577 stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
578 {
579         struct stm_file *stmf = file->private_data;
580         struct stm_data *stm_data = stmf->stm->data;
581         int err = -ENOTTY;
582         u64 options;
583
584         switch (cmd) {
585         case STP_POLICY_ID_SET:
586                 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
587                 if (err)
588                         return err;
589
590                 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
591
592         case STP_POLICY_ID_GET:
593                 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
594
595         case STP_SET_OPTIONS:
596                 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
597                         return -EFAULT;
598
599                 if (stm_data->set_options)
600                         err = stm_data->set_options(stm_data,
601                                                     stmf->output.master,
602                                                     stmf->output.channel,
603                                                     stmf->output.nr_chans,
604                                                     options);
605
606                 break;
607         default:
608                 break;
609         }
610
611         return err;
612 }
613
614 #ifdef CONFIG_COMPAT
615 static long
616 stm_char_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
617 {
618         return stm_char_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
619 }
620 #else
621 #define stm_char_compat_ioctl   NULL
622 #endif
623
624 static const struct file_operations stm_fops = {
625         .open           = stm_char_open,
626         .release        = stm_char_release,
627         .write          = stm_char_write,
628         .mmap           = stm_char_mmap,
629         .unlocked_ioctl = stm_char_ioctl,
630         .compat_ioctl   = stm_char_compat_ioctl,
631         .llseek         = no_llseek,
632 };
633
634 static void stm_device_release(struct device *dev)
635 {
636         struct stm_device *stm = to_stm_device(dev);
637
638         kfree(stm);
639 }
640
641 int stm_register_device(struct device *parent, struct stm_data *stm_data,
642                         struct module *owner)
643 {
644         struct stm_device *stm;
645         unsigned int nmasters;
646         int err = -ENOMEM;
647
648         if (!stm_core_up)
649                 return -EPROBE_DEFER;
650
651         if (!stm_data->packet || !stm_data->sw_nchannels)
652                 return -EINVAL;
653
654         nmasters = stm_data->sw_end - stm_data->sw_start;
655         stm = kzalloc(sizeof(*stm) + nmasters * sizeof(void *), GFP_KERNEL);
656         if (!stm)
657                 return -ENOMEM;
658
659         stm->major = register_chrdev(0, stm_data->name, &stm_fops);
660         if (stm->major < 0)
661                 goto err_free;
662
663         device_initialize(&stm->dev);
664         stm->dev.devt = MKDEV(stm->major, 0);
665         stm->dev.class = &stm_class;
666         stm->dev.parent = parent;
667         stm->dev.release = stm_device_release;
668
669         mutex_init(&stm->link_mutex);
670         spin_lock_init(&stm->link_lock);
671         INIT_LIST_HEAD(&stm->link_list);
672
673         /* initialize the object before it is accessible via sysfs */
674         spin_lock_init(&stm->mc_lock);
675         mutex_init(&stm->policy_mutex);
676         stm->sw_nmasters = nmasters;
677         stm->owner = owner;
678         stm->data = stm_data;
679         stm_data->stm = stm;
680
681         err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
682         if (err)
683                 goto err_device;
684
685         err = device_add(&stm->dev);
686         if (err)
687                 goto err_device;
688
689         return 0;
690
691 err_device:
692         unregister_chrdev(stm->major, stm_data->name);
693
694         /* matches device_initialize() above */
695         put_device(&stm->dev);
696 err_free:
697         kfree(stm);
698
699         return err;
700 }
701 EXPORT_SYMBOL_GPL(stm_register_device);
702
703 static int __stm_source_link_drop(struct stm_source_device *src,
704                                   struct stm_device *stm);
705
706 void stm_unregister_device(struct stm_data *stm_data)
707 {
708         struct stm_device *stm = stm_data->stm;
709         struct stm_source_device *src, *iter;
710         int i, ret;
711
712         mutex_lock(&stm->link_mutex);
713         list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
714                 ret = __stm_source_link_drop(src, stm);
715                 /*
716                  * src <-> stm link must not change under the same
717                  * stm::link_mutex, so complain loudly if it has;
718                  * also in this situation ret!=0 means this src is
719                  * not connected to this stm and it should be otherwise
720                  * safe to proceed with the tear-down of stm.
721                  */
722                 WARN_ON_ONCE(ret);
723         }
724         mutex_unlock(&stm->link_mutex);
725
726         synchronize_srcu(&stm_source_srcu);
727
728         unregister_chrdev(stm->major, stm_data->name);
729
730         mutex_lock(&stm->policy_mutex);
731         if (stm->policy)
732                 stp_policy_unbind(stm->policy);
733         mutex_unlock(&stm->policy_mutex);
734
735         for (i = 0; i < stm->sw_nmasters; i++)
736                 stp_master_free(stm, i);
737
738         device_unregister(&stm->dev);
739         stm_data->stm = NULL;
740 }
741 EXPORT_SYMBOL_GPL(stm_unregister_device);
742
743 /*
744  * stm::link_list access serialization uses a spinlock and a mutex; holding
745  * either of them guarantees that the list is stable; modification requires
746  * holding both of them.
747  *
748  * Lock ordering is as follows:
749  *   stm::link_mutex
750  *     stm::link_lock
751  *       src::link_lock
752  */
753
754 /**
755  * stm_source_link_add() - connect an stm_source device to an stm device
756  * @src:        stm_source device
757  * @stm:        stm device
758  *
759  * This function establishes a link from stm_source to an stm device so that
760  * the former can send out trace data to the latter.
761  *
762  * Return:      0 on success, -errno otherwise.
763  */
764 static int stm_source_link_add(struct stm_source_device *src,
765                                struct stm_device *stm)
766 {
767         char *id;
768         int err;
769
770         mutex_lock(&stm->link_mutex);
771         spin_lock(&stm->link_lock);
772         spin_lock(&src->link_lock);
773
774         /* src->link is dereferenced under stm_source_srcu but not the list */
775         rcu_assign_pointer(src->link, stm);
776         list_add_tail(&src->link_entry, &stm->link_list);
777
778         spin_unlock(&src->link_lock);
779         spin_unlock(&stm->link_lock);
780         mutex_unlock(&stm->link_mutex);
781
782         id = kstrdup(src->data->name, GFP_KERNEL);
783         if (id) {
784                 src->policy_node =
785                         stp_policy_node_lookup(stm, id);
786
787                 kfree(id);
788         }
789
790         err = stm_output_assign(stm, src->data->nr_chans,
791                                 src->policy_node, &src->output);
792
793         if (src->policy_node)
794                 stp_policy_node_put(src->policy_node);
795
796         if (err)
797                 goto fail_detach;
798
799         /* this is to notify the STM device that a new link has been made */
800         if (stm->data->link)
801                 err = stm->data->link(stm->data, src->output.master,
802                                       src->output.channel);
803
804         if (err)
805                 goto fail_free_output;
806
807         /* this is to let the source carry out all necessary preparations */
808         if (src->data->link)
809                 src->data->link(src->data);
810
811         return 0;
812
813 fail_free_output:
814         stm_output_free(stm, &src->output);
815
816 fail_detach:
817         mutex_lock(&stm->link_mutex);
818         spin_lock(&stm->link_lock);
819         spin_lock(&src->link_lock);
820
821         rcu_assign_pointer(src->link, NULL);
822         list_del_init(&src->link_entry);
823
824         spin_unlock(&src->link_lock);
825         spin_unlock(&stm->link_lock);
826         mutex_unlock(&stm->link_mutex);
827
828         return err;
829 }
830
831 /**
832  * __stm_source_link_drop() - detach stm_source from an stm device
833  * @src:        stm_source device
834  * @stm:        stm device
835  *
836  * If @stm is @src::link, disconnect them from one another and put the
837  * reference on the @stm device.
838  *
839  * Caller must hold stm::link_mutex.
840  */
841 static int __stm_source_link_drop(struct stm_source_device *src,
842                                   struct stm_device *stm)
843 {
844         struct stm_device *link;
845         int ret = 0;
846
847         lockdep_assert_held(&stm->link_mutex);
848
849         /* for stm::link_list modification, we hold both mutex and spinlock */
850         spin_lock(&stm->link_lock);
851         spin_lock(&src->link_lock);
852         link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
853
854         /*
855          * The linked device may have changed since we last looked, because
856          * we weren't holding the src::link_lock back then; if this is the
857          * case, tell the caller to retry.
858          */
859         if (link != stm) {
860                 ret = -EAGAIN;
861                 goto unlock;
862         }
863
864         stm_output_free(link, &src->output);
865         list_del_init(&src->link_entry);
866         /* matches stm_find_device() from stm_source_link_store() */
867         stm_put_device(link);
868         rcu_assign_pointer(src->link, NULL);
869
870 unlock:
871         spin_unlock(&src->link_lock);
872         spin_unlock(&stm->link_lock);
873
874         /*
875          * Call the unlink callbacks for both source and stm, when we know
876          * that we have actually performed the unlinking.
877          */
878         if (!ret) {
879                 if (src->data->unlink)
880                         src->data->unlink(src->data);
881
882                 if (stm->data->unlink)
883                         stm->data->unlink(stm->data, src->output.master,
884                                           src->output.channel);
885         }
886
887         return ret;
888 }
889
890 /**
891  * stm_source_link_drop() - detach stm_source from its stm device
892  * @src:        stm_source device
893  *
894  * Unlinking means disconnecting from source's STM device; after this
895  * writes will be unsuccessful until it is linked to a new STM device.
896  *
897  * This will happen on "stm_source_link" sysfs attribute write to undo
898  * the existing link (if any), or on linked STM device's de-registration.
899  */
900 static void stm_source_link_drop(struct stm_source_device *src)
901 {
902         struct stm_device *stm;
903         int idx, ret;
904
905 retry:
906         idx = srcu_read_lock(&stm_source_srcu);
907         /*
908          * The stm device will be valid for the duration of this
909          * read section, but the link may change before we grab
910          * the src::link_lock in __stm_source_link_drop().
911          */
912         stm = srcu_dereference(src->link, &stm_source_srcu);
913
914         ret = 0;
915         if (stm) {
916                 mutex_lock(&stm->link_mutex);
917                 ret = __stm_source_link_drop(src, stm);
918                 mutex_unlock(&stm->link_mutex);
919         }
920
921         srcu_read_unlock(&stm_source_srcu, idx);
922
923         /* if it did change, retry */
924         if (ret == -EAGAIN)
925                 goto retry;
926 }
927
928 static ssize_t stm_source_link_show(struct device *dev,
929                                     struct device_attribute *attr,
930                                     char *buf)
931 {
932         struct stm_source_device *src = to_stm_source_device(dev);
933         struct stm_device *stm;
934         int idx, ret;
935
936         idx = srcu_read_lock(&stm_source_srcu);
937         stm = srcu_dereference(src->link, &stm_source_srcu);
938         ret = sprintf(buf, "%s\n",
939                       stm ? dev_name(&stm->dev) : "<none>");
940         srcu_read_unlock(&stm_source_srcu, idx);
941
942         return ret;
943 }
944
945 static ssize_t stm_source_link_store(struct device *dev,
946                                      struct device_attribute *attr,
947                                      const char *buf, size_t count)
948 {
949         struct stm_source_device *src = to_stm_source_device(dev);
950         struct stm_device *link;
951         int err;
952
953         stm_source_link_drop(src);
954
955         link = stm_find_device(buf);
956         if (!link)
957                 return -EINVAL;
958
959         err = stm_source_link_add(src, link);
960         if (err) {
961                 /* matches the stm_find_device() above */
962                 stm_put_device(link);
963         }
964
965         return err ? : count;
966 }
967
968 static DEVICE_ATTR_RW(stm_source_link);
969
970 static struct attribute *stm_source_attrs[] = {
971         &dev_attr_stm_source_link.attr,
972         NULL,
973 };
974
975 ATTRIBUTE_GROUPS(stm_source);
976
977 static struct class stm_source_class = {
978         .name           = "stm_source",
979         .dev_groups     = stm_source_groups,
980 };
981
982 static void stm_source_device_release(struct device *dev)
983 {
984         struct stm_source_device *src = to_stm_source_device(dev);
985
986         kfree(src);
987 }
988
989 /**
990  * stm_source_register_device() - register an stm_source device
991  * @parent:     parent device
992  * @data:       device description structure
993  *
994  * This will create a device of stm_source class that can write
995  * data to an stm device once linked.
996  *
997  * Return:      0 on success, -errno otherwise.
998  */
999 int stm_source_register_device(struct device *parent,
1000                                struct stm_source_data *data)
1001 {
1002         struct stm_source_device *src;
1003         int err;
1004
1005         if (!stm_core_up)
1006                 return -EPROBE_DEFER;
1007
1008         src = kzalloc(sizeof(*src), GFP_KERNEL);
1009         if (!src)
1010                 return -ENOMEM;
1011
1012         device_initialize(&src->dev);
1013         src->dev.class = &stm_source_class;
1014         src->dev.parent = parent;
1015         src->dev.release = stm_source_device_release;
1016
1017         err = kobject_set_name(&src->dev.kobj, "%s", data->name);
1018         if (err)
1019                 goto err;
1020
1021         err = device_add(&src->dev);
1022         if (err)
1023                 goto err;
1024
1025         stm_output_init(&src->output);
1026         spin_lock_init(&src->link_lock);
1027         INIT_LIST_HEAD(&src->link_entry);
1028         src->data = data;
1029         data->src = src;
1030
1031         return 0;
1032
1033 err:
1034         put_device(&src->dev);
1035         kfree(src);
1036
1037         return err;
1038 }
1039 EXPORT_SYMBOL_GPL(stm_source_register_device);
1040
1041 /**
1042  * stm_source_unregister_device() - unregister an stm_source device
1043  * @data:       device description that was used to register the device
1044  *
1045  * This will remove a previously created stm_source device from the system.
1046  */
1047 void stm_source_unregister_device(struct stm_source_data *data)
1048 {
1049         struct stm_source_device *src = data->src;
1050
1051         stm_source_link_drop(src);
1052
1053         device_destroy(&stm_source_class, src->dev.devt);
1054 }
1055 EXPORT_SYMBOL_GPL(stm_source_unregister_device);
1056
1057 int stm_source_write(struct stm_source_data *data, unsigned int chan,
1058                      const char *buf, size_t count)
1059 {
1060         struct stm_source_device *src = data->src;
1061         struct stm_device *stm;
1062         int idx;
1063
1064         if (!src->output.nr_chans)
1065                 return -ENODEV;
1066
1067         if (chan >= src->output.nr_chans)
1068                 return -EINVAL;
1069
1070         idx = srcu_read_lock(&stm_source_srcu);
1071
1072         stm = srcu_dereference(src->link, &stm_source_srcu);
1073         if (stm)
1074                 count = stm_write(stm->data, src->output.master,
1075                                   src->output.channel + chan,
1076                                   buf, count);
1077         else
1078                 count = -ENODEV;
1079
1080         srcu_read_unlock(&stm_source_srcu, idx);
1081
1082         return count;
1083 }
1084 EXPORT_SYMBOL_GPL(stm_source_write);
1085
1086 static int __init stm_core_init(void)
1087 {
1088         int err;
1089
1090         err = class_register(&stm_class);
1091         if (err)
1092                 return err;
1093
1094         err = class_register(&stm_source_class);
1095         if (err)
1096                 goto err_stm;
1097
1098         err = stp_configfs_init();
1099         if (err)
1100                 goto err_src;
1101
1102         init_srcu_struct(&stm_source_srcu);
1103
1104         stm_core_up++;
1105
1106         return 0;
1107
1108 err_src:
1109         class_unregister(&stm_source_class);
1110 err_stm:
1111         class_unregister(&stm_class);
1112
1113         return err;
1114 }
1115
1116 module_init(stm_core_init);
1117
1118 static void __exit stm_core_exit(void)
1119 {
1120         cleanup_srcu_struct(&stm_source_srcu);
1121         class_unregister(&stm_source_class);
1122         class_unregister(&stm_class);
1123         stp_configfs_exit();
1124 }
1125
1126 module_exit(stm_core_exit);
1127
1128 MODULE_LICENSE("GPL v2");
1129 MODULE_DESCRIPTION("System Trace Module device class");
1130 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");