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