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