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