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