0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/pm_runtime.h>
0011 #include <linux/uaccess.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/device.h>
0015 #include <linux/compat.h>
0016 #include <linux/kdev_t.h>
0017 #include <linux/srcu.h>
0018 #include <linux/slab.h>
0019 #include <linux/stm.h>
0020 #include <linux/fs.h>
0021 #include <linux/mm.h>
0022 #include <linux/vmalloc.h>
0023 #include "stm.h"
0024
0025 #include <uapi/linux/stm.h>
0026
0027 static unsigned int stm_core_up;
0028
0029
0030
0031
0032
0033
0034 static struct srcu_struct stm_source_srcu;
0035
0036 static ssize_t masters_show(struct device *dev,
0037 struct device_attribute *attr,
0038 char *buf)
0039 {
0040 struct stm_device *stm = to_stm_device(dev);
0041 int ret;
0042
0043 ret = sprintf(buf, "%u %u\n", stm->data->sw_start, stm->data->sw_end);
0044
0045 return ret;
0046 }
0047
0048 static DEVICE_ATTR_RO(masters);
0049
0050 static ssize_t channels_show(struct device *dev,
0051 struct device_attribute *attr,
0052 char *buf)
0053 {
0054 struct stm_device *stm = to_stm_device(dev);
0055 int ret;
0056
0057 ret = sprintf(buf, "%u\n", stm->data->sw_nchannels);
0058
0059 return ret;
0060 }
0061
0062 static DEVICE_ATTR_RO(channels);
0063
0064 static ssize_t hw_override_show(struct device *dev,
0065 struct device_attribute *attr,
0066 char *buf)
0067 {
0068 struct stm_device *stm = to_stm_device(dev);
0069 int ret;
0070
0071 ret = sprintf(buf, "%u\n", stm->data->hw_override);
0072
0073 return ret;
0074 }
0075
0076 static DEVICE_ATTR_RO(hw_override);
0077
0078 static struct attribute *stm_attrs[] = {
0079 &dev_attr_masters.attr,
0080 &dev_attr_channels.attr,
0081 &dev_attr_hw_override.attr,
0082 NULL,
0083 };
0084
0085 ATTRIBUTE_GROUPS(stm);
0086
0087 static struct class stm_class = {
0088 .name = "stm",
0089 .dev_groups = stm_groups,
0090 };
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 struct stm_device *stm_find_device(const char *buf)
0105 {
0106 struct stm_device *stm;
0107 struct device *dev;
0108
0109 if (!stm_core_up)
0110 return NULL;
0111
0112 dev = class_find_device_by_name(&stm_class, buf);
0113 if (!dev)
0114 return NULL;
0115
0116 stm = to_stm_device(dev);
0117 if (!try_module_get(stm->owner)) {
0118
0119 put_device(dev);
0120 return NULL;
0121 }
0122
0123 return stm;
0124 }
0125
0126
0127
0128
0129
0130
0131
0132
0133 void stm_put_device(struct stm_device *stm)
0134 {
0135 module_put(stm->owner);
0136 put_device(&stm->dev);
0137 }
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 #define __stm_master(_s, _m) \
0148 ((_s)->masters[(_m) - (_s)->data->sw_start])
0149
0150 static inline struct stp_master *
0151 stm_master(struct stm_device *stm, unsigned int idx)
0152 {
0153 if (idx < stm->data->sw_start || idx > stm->data->sw_end)
0154 return NULL;
0155
0156 return __stm_master(stm, idx);
0157 }
0158
0159 static int stp_master_alloc(struct stm_device *stm, unsigned int idx)
0160 {
0161 struct stp_master *master;
0162
0163 master = kzalloc(struct_size(master, chan_map,
0164 BITS_TO_LONGS(stm->data->sw_nchannels)),
0165 GFP_ATOMIC);
0166 if (!master)
0167 return -ENOMEM;
0168
0169 master->nr_free = stm->data->sw_nchannels;
0170 __stm_master(stm, idx) = master;
0171
0172 return 0;
0173 }
0174
0175 static void stp_master_free(struct stm_device *stm, unsigned int idx)
0176 {
0177 struct stp_master *master = stm_master(stm, idx);
0178
0179 if (!master)
0180 return;
0181
0182 __stm_master(stm, idx) = NULL;
0183 kfree(master);
0184 }
0185
0186 static void stm_output_claim(struct stm_device *stm, struct stm_output *output)
0187 {
0188 struct stp_master *master = stm_master(stm, output->master);
0189
0190 lockdep_assert_held(&stm->mc_lock);
0191 lockdep_assert_held(&output->lock);
0192
0193 if (WARN_ON_ONCE(master->nr_free < output->nr_chans))
0194 return;
0195
0196 bitmap_allocate_region(&master->chan_map[0], output->channel,
0197 ilog2(output->nr_chans));
0198
0199 master->nr_free -= output->nr_chans;
0200 }
0201
0202 static void
0203 stm_output_disclaim(struct stm_device *stm, struct stm_output *output)
0204 {
0205 struct stp_master *master = stm_master(stm, output->master);
0206
0207 lockdep_assert_held(&stm->mc_lock);
0208 lockdep_assert_held(&output->lock);
0209
0210 bitmap_release_region(&master->chan_map[0], output->channel,
0211 ilog2(output->nr_chans));
0212
0213 master->nr_free += output->nr_chans;
0214 output->nr_chans = 0;
0215 }
0216
0217
0218
0219
0220
0221 static int find_free_channels(unsigned long *bitmap, unsigned int start,
0222 unsigned int end, unsigned int width)
0223 {
0224 unsigned int pos;
0225 int i;
0226
0227 for (pos = start; pos < end + 1; pos = ALIGN(pos, width)) {
0228 pos = find_next_zero_bit(bitmap, end + 1, pos);
0229 if (pos + width > end + 1)
0230 break;
0231
0232 if (pos & (width - 1))
0233 continue;
0234
0235 for (i = 1; i < width && !test_bit(pos + i, bitmap); i++)
0236 ;
0237 if (i == width)
0238 return pos;
0239
0240
0241 pos += i;
0242 }
0243
0244 return -1;
0245 }
0246
0247 static int
0248 stm_find_master_chan(struct stm_device *stm, unsigned int width,
0249 unsigned int *mstart, unsigned int mend,
0250 unsigned int *cstart, unsigned int cend)
0251 {
0252 struct stp_master *master;
0253 unsigned int midx;
0254 int pos, err;
0255
0256 for (midx = *mstart; midx <= mend; midx++) {
0257 if (!stm_master(stm, midx)) {
0258 err = stp_master_alloc(stm, midx);
0259 if (err)
0260 return err;
0261 }
0262
0263 master = stm_master(stm, midx);
0264
0265 if (!master->nr_free)
0266 continue;
0267
0268 pos = find_free_channels(master->chan_map, *cstart, cend,
0269 width);
0270 if (pos < 0)
0271 continue;
0272
0273 *mstart = midx;
0274 *cstart = pos;
0275 return 0;
0276 }
0277
0278 return -ENOSPC;
0279 }
0280
0281 static int stm_output_assign(struct stm_device *stm, unsigned int width,
0282 struct stp_policy_node *policy_node,
0283 struct stm_output *output)
0284 {
0285 unsigned int midx, cidx, mend, cend;
0286 int ret = -EINVAL;
0287
0288 if (width > stm->data->sw_nchannels)
0289 return -EINVAL;
0290
0291
0292 if (WARN_ON_ONCE(!policy_node))
0293 return -EINVAL;
0294
0295
0296
0297
0298
0299 stp_policy_node_get_ranges(policy_node, &midx, &mend, &cidx, &cend);
0300
0301 spin_lock(&stm->mc_lock);
0302 spin_lock(&output->lock);
0303
0304 if (WARN_ON_ONCE(output->nr_chans))
0305 goto unlock;
0306
0307 ret = stm_find_master_chan(stm, width, &midx, mend, &cidx, cend);
0308 if (ret < 0)
0309 goto unlock;
0310
0311 output->master = midx;
0312 output->channel = cidx;
0313 output->nr_chans = width;
0314 if (stm->pdrv->output_open) {
0315 void *priv = stp_policy_node_priv(policy_node);
0316
0317 if (WARN_ON_ONCE(!priv))
0318 goto unlock;
0319
0320
0321 ret = stm->pdrv->output_open(priv, output);
0322 if (ret)
0323 goto unlock;
0324 }
0325
0326 stm_output_claim(stm, output);
0327 dev_dbg(&stm->dev, "assigned %u:%u (+%u)\n", midx, cidx, width);
0328
0329 ret = 0;
0330 unlock:
0331 if (ret)
0332 output->nr_chans = 0;
0333
0334 spin_unlock(&output->lock);
0335 spin_unlock(&stm->mc_lock);
0336
0337 return ret;
0338 }
0339
0340 static void stm_output_free(struct stm_device *stm, struct stm_output *output)
0341 {
0342 spin_lock(&stm->mc_lock);
0343 spin_lock(&output->lock);
0344 if (output->nr_chans)
0345 stm_output_disclaim(stm, output);
0346 if (stm->pdrv && stm->pdrv->output_close)
0347 stm->pdrv->output_close(output);
0348 spin_unlock(&output->lock);
0349 spin_unlock(&stm->mc_lock);
0350 }
0351
0352 static void stm_output_init(struct stm_output *output)
0353 {
0354 spin_lock_init(&output->lock);
0355 }
0356
0357 static int major_match(struct device *dev, const void *data)
0358 {
0359 unsigned int major = *(unsigned int *)data;
0360
0361 return MAJOR(dev->devt) == major;
0362 }
0363
0364
0365
0366
0367
0368
0369 static struct list_head stm_pdrv_head;
0370 static struct mutex stm_pdrv_mutex;
0371
0372 struct stm_pdrv_entry {
0373 struct list_head entry;
0374 const struct stm_protocol_driver *pdrv;
0375 const struct config_item_type *node_type;
0376 };
0377
0378 static const struct stm_pdrv_entry *
0379 __stm_lookup_protocol(const char *name)
0380 {
0381 struct stm_pdrv_entry *pe;
0382
0383
0384
0385
0386 if (!name || !*name)
0387 name = "p_basic";
0388
0389 list_for_each_entry(pe, &stm_pdrv_head, entry) {
0390 if (!strcmp(name, pe->pdrv->name))
0391 return pe;
0392 }
0393
0394 return NULL;
0395 }
0396
0397 int stm_register_protocol(const struct stm_protocol_driver *pdrv)
0398 {
0399 struct stm_pdrv_entry *pe = NULL;
0400 int ret = -ENOMEM;
0401
0402 mutex_lock(&stm_pdrv_mutex);
0403
0404 if (__stm_lookup_protocol(pdrv->name)) {
0405 ret = -EEXIST;
0406 goto unlock;
0407 }
0408
0409 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
0410 if (!pe)
0411 goto unlock;
0412
0413 if (pdrv->policy_attr) {
0414 pe->node_type = get_policy_node_type(pdrv->policy_attr);
0415 if (!pe->node_type)
0416 goto unlock;
0417 }
0418
0419 list_add_tail(&pe->entry, &stm_pdrv_head);
0420 pe->pdrv = pdrv;
0421
0422 ret = 0;
0423 unlock:
0424 mutex_unlock(&stm_pdrv_mutex);
0425
0426 if (ret)
0427 kfree(pe);
0428
0429 return ret;
0430 }
0431 EXPORT_SYMBOL_GPL(stm_register_protocol);
0432
0433 void stm_unregister_protocol(const struct stm_protocol_driver *pdrv)
0434 {
0435 struct stm_pdrv_entry *pe, *iter;
0436
0437 mutex_lock(&stm_pdrv_mutex);
0438
0439 list_for_each_entry_safe(pe, iter, &stm_pdrv_head, entry) {
0440 if (pe->pdrv == pdrv) {
0441 list_del(&pe->entry);
0442
0443 if (pe->node_type) {
0444 kfree(pe->node_type->ct_attrs);
0445 kfree(pe->node_type);
0446 }
0447 kfree(pe);
0448 break;
0449 }
0450 }
0451
0452 mutex_unlock(&stm_pdrv_mutex);
0453 }
0454 EXPORT_SYMBOL_GPL(stm_unregister_protocol);
0455
0456 static bool stm_get_protocol(const struct stm_protocol_driver *pdrv)
0457 {
0458 return try_module_get(pdrv->owner);
0459 }
0460
0461 void stm_put_protocol(const struct stm_protocol_driver *pdrv)
0462 {
0463 module_put(pdrv->owner);
0464 }
0465
0466 int stm_lookup_protocol(const char *name,
0467 const struct stm_protocol_driver **pdrv,
0468 const struct config_item_type **node_type)
0469 {
0470 const struct stm_pdrv_entry *pe;
0471
0472 mutex_lock(&stm_pdrv_mutex);
0473
0474 pe = __stm_lookup_protocol(name);
0475 if (pe && pe->pdrv && stm_get_protocol(pe->pdrv)) {
0476 *pdrv = pe->pdrv;
0477 *node_type = pe->node_type;
0478 }
0479
0480 mutex_unlock(&stm_pdrv_mutex);
0481
0482 return pe ? 0 : -ENOENT;
0483 }
0484
0485 static int stm_char_open(struct inode *inode, struct file *file)
0486 {
0487 struct stm_file *stmf;
0488 struct device *dev;
0489 unsigned int major = imajor(inode);
0490 int err = -ENOMEM;
0491
0492 dev = class_find_device(&stm_class, NULL, &major, major_match);
0493 if (!dev)
0494 return -ENODEV;
0495
0496 stmf = kzalloc(sizeof(*stmf), GFP_KERNEL);
0497 if (!stmf)
0498 goto err_put_device;
0499
0500 err = -ENODEV;
0501 stm_output_init(&stmf->output);
0502 stmf->stm = to_stm_device(dev);
0503
0504 if (!try_module_get(stmf->stm->owner))
0505 goto err_free;
0506
0507 file->private_data = stmf;
0508
0509 return nonseekable_open(inode, file);
0510
0511 err_free:
0512 kfree(stmf);
0513 err_put_device:
0514
0515 put_device(dev);
0516
0517 return err;
0518 }
0519
0520 static int stm_char_release(struct inode *inode, struct file *file)
0521 {
0522 struct stm_file *stmf = file->private_data;
0523 struct stm_device *stm = stmf->stm;
0524
0525 if (stm->data->unlink)
0526 stm->data->unlink(stm->data, stmf->output.master,
0527 stmf->output.channel);
0528
0529 stm_output_free(stm, &stmf->output);
0530
0531
0532
0533
0534
0535 stm_put_device(stm);
0536 kfree(stmf);
0537
0538 return 0;
0539 }
0540
0541 static int
0542 stm_assign_first_policy(struct stm_device *stm, struct stm_output *output,
0543 char **ids, unsigned int width)
0544 {
0545 struct stp_policy_node *pn;
0546 int err, n;
0547
0548
0549
0550
0551
0552
0553
0554 for (n = 0, pn = NULL; ids[n] && !pn; n++)
0555 pn = stp_policy_node_lookup(stm, ids[n]);
0556
0557 if (!pn)
0558 return -EINVAL;
0559
0560 err = stm_output_assign(stm, width, pn, output);
0561
0562 stp_policy_node_put(pn);
0563
0564 return err;
0565 }
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576 ssize_t notrace stm_data_write(struct stm_data *data, unsigned int m,
0577 unsigned int c, bool ts_first, const void *buf,
0578 size_t count)
0579 {
0580 unsigned int flags = ts_first ? STP_PACKET_TIMESTAMPED : 0;
0581 ssize_t sz;
0582 size_t pos;
0583
0584 for (pos = 0, sz = 0; pos < count; pos += sz) {
0585 sz = min_t(unsigned int, count - pos, 8);
0586 sz = data->packet(data, m, c, STP_PACKET_DATA, flags, sz,
0587 &((u8 *)buf)[pos]);
0588 if (sz <= 0)
0589 break;
0590
0591 if (ts_first) {
0592 flags = 0;
0593 ts_first = false;
0594 }
0595 }
0596
0597 return sz < 0 ? sz : pos;
0598 }
0599 EXPORT_SYMBOL_GPL(stm_data_write);
0600
0601 static ssize_t notrace
0602 stm_write(struct stm_device *stm, struct stm_output *output,
0603 unsigned int chan, const char *buf, size_t count)
0604 {
0605 int err;
0606
0607
0608 if (!stm->pdrv)
0609 return -ENODEV;
0610
0611 err = stm->pdrv->write(stm->data, output, chan, buf, count);
0612 if (err < 0)
0613 return err;
0614
0615 return err;
0616 }
0617
0618 static ssize_t stm_char_write(struct file *file, const char __user *buf,
0619 size_t count, loff_t *ppos)
0620 {
0621 struct stm_file *stmf = file->private_data;
0622 struct stm_device *stm = stmf->stm;
0623 char *kbuf;
0624 int err;
0625
0626 if (count + 1 > PAGE_SIZE)
0627 count = PAGE_SIZE - 1;
0628
0629
0630
0631
0632
0633 if (!stmf->output.nr_chans) {
0634 char comm[sizeof(current->comm)];
0635 char *ids[] = { comm, "default", NULL };
0636
0637 get_task_comm(comm, current);
0638
0639 err = stm_assign_first_policy(stmf->stm, &stmf->output, ids, 1);
0640
0641
0642
0643
0644 if (err)
0645 return err;
0646 }
0647
0648 kbuf = kmalloc(count + 1, GFP_KERNEL);
0649 if (!kbuf)
0650 return -ENOMEM;
0651
0652 err = copy_from_user(kbuf, buf, count);
0653 if (err) {
0654 kfree(kbuf);
0655 return -EFAULT;
0656 }
0657
0658 pm_runtime_get_sync(&stm->dev);
0659
0660 count = stm_write(stm, &stmf->output, 0, kbuf, count);
0661
0662 pm_runtime_mark_last_busy(&stm->dev);
0663 pm_runtime_put_autosuspend(&stm->dev);
0664 kfree(kbuf);
0665
0666 return count;
0667 }
0668
0669 static void stm_mmap_open(struct vm_area_struct *vma)
0670 {
0671 struct stm_file *stmf = vma->vm_file->private_data;
0672 struct stm_device *stm = stmf->stm;
0673
0674 pm_runtime_get(&stm->dev);
0675 }
0676
0677 static void stm_mmap_close(struct vm_area_struct *vma)
0678 {
0679 struct stm_file *stmf = vma->vm_file->private_data;
0680 struct stm_device *stm = stmf->stm;
0681
0682 pm_runtime_mark_last_busy(&stm->dev);
0683 pm_runtime_put_autosuspend(&stm->dev);
0684 }
0685
0686 static const struct vm_operations_struct stm_mmap_vmops = {
0687 .open = stm_mmap_open,
0688 .close = stm_mmap_close,
0689 };
0690
0691 static int stm_char_mmap(struct file *file, struct vm_area_struct *vma)
0692 {
0693 struct stm_file *stmf = file->private_data;
0694 struct stm_device *stm = stmf->stm;
0695 unsigned long size, phys;
0696
0697 if (!stm->data->mmio_addr)
0698 return -EOPNOTSUPP;
0699
0700 if (vma->vm_pgoff)
0701 return -EINVAL;
0702
0703 size = vma->vm_end - vma->vm_start;
0704
0705 if (stmf->output.nr_chans * stm->data->sw_mmiosz != size)
0706 return -EINVAL;
0707
0708 phys = stm->data->mmio_addr(stm->data, stmf->output.master,
0709 stmf->output.channel,
0710 stmf->output.nr_chans);
0711
0712 if (!phys)
0713 return -EINVAL;
0714
0715 pm_runtime_get_sync(&stm->dev);
0716
0717 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
0718 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
0719 vma->vm_ops = &stm_mmap_vmops;
0720 vm_iomap_memory(vma, phys, size);
0721
0722 return 0;
0723 }
0724
0725 static int stm_char_policy_set_ioctl(struct stm_file *stmf, void __user *arg)
0726 {
0727 struct stm_device *stm = stmf->stm;
0728 struct stp_policy_id *id;
0729 char *ids[] = { NULL, NULL };
0730 int ret = -EINVAL, wlimit = 1;
0731 u32 size;
0732
0733 if (stmf->output.nr_chans)
0734 return -EBUSY;
0735
0736 if (copy_from_user(&size, arg, sizeof(size)))
0737 return -EFAULT;
0738
0739 if (size < sizeof(*id) || size >= PATH_MAX + sizeof(*id))
0740 return -EINVAL;
0741
0742
0743
0744
0745
0746 id = kzalloc(size + 1, GFP_KERNEL);
0747 if (!id)
0748 return -ENOMEM;
0749
0750 if (copy_from_user(id, arg, size)) {
0751 ret = -EFAULT;
0752 goto err_free;
0753 }
0754
0755 if (id->__reserved_0 || id->__reserved_1)
0756 goto err_free;
0757
0758 if (stm->data->sw_mmiosz)
0759 wlimit = PAGE_SIZE / stm->data->sw_mmiosz;
0760
0761 if (id->width < 1 || id->width > wlimit)
0762 goto err_free;
0763
0764 ids[0] = id->id;
0765 ret = stm_assign_first_policy(stmf->stm, &stmf->output, ids,
0766 id->width);
0767 if (ret)
0768 goto err_free;
0769
0770 if (stm->data->link)
0771 ret = stm->data->link(stm->data, stmf->output.master,
0772 stmf->output.channel);
0773
0774 if (ret)
0775 stm_output_free(stmf->stm, &stmf->output);
0776
0777 err_free:
0778 kfree(id);
0779
0780 return ret;
0781 }
0782
0783 static int stm_char_policy_get_ioctl(struct stm_file *stmf, void __user *arg)
0784 {
0785 struct stp_policy_id id = {
0786 .size = sizeof(id),
0787 .master = stmf->output.master,
0788 .channel = stmf->output.channel,
0789 .width = stmf->output.nr_chans,
0790 .__reserved_0 = 0,
0791 .__reserved_1 = 0,
0792 };
0793
0794 return copy_to_user(arg, &id, id.size) ? -EFAULT : 0;
0795 }
0796
0797 static long
0798 stm_char_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0799 {
0800 struct stm_file *stmf = file->private_data;
0801 struct stm_data *stm_data = stmf->stm->data;
0802 int err = -ENOTTY;
0803 u64 options;
0804
0805 switch (cmd) {
0806 case STP_POLICY_ID_SET:
0807 err = stm_char_policy_set_ioctl(stmf, (void __user *)arg);
0808 if (err)
0809 return err;
0810
0811 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
0812
0813 case STP_POLICY_ID_GET:
0814 return stm_char_policy_get_ioctl(stmf, (void __user *)arg);
0815
0816 case STP_SET_OPTIONS:
0817 if (copy_from_user(&options, (u64 __user *)arg, sizeof(u64)))
0818 return -EFAULT;
0819
0820 if (stm_data->set_options)
0821 err = stm_data->set_options(stm_data,
0822 stmf->output.master,
0823 stmf->output.channel,
0824 stmf->output.nr_chans,
0825 options);
0826
0827 break;
0828 default:
0829 break;
0830 }
0831
0832 return err;
0833 }
0834
0835 static const struct file_operations stm_fops = {
0836 .open = stm_char_open,
0837 .release = stm_char_release,
0838 .write = stm_char_write,
0839 .mmap = stm_char_mmap,
0840 .unlocked_ioctl = stm_char_ioctl,
0841 .compat_ioctl = compat_ptr_ioctl,
0842 .llseek = no_llseek,
0843 };
0844
0845 static void stm_device_release(struct device *dev)
0846 {
0847 struct stm_device *stm = to_stm_device(dev);
0848
0849 vfree(stm);
0850 }
0851
0852 int stm_register_device(struct device *parent, struct stm_data *stm_data,
0853 struct module *owner)
0854 {
0855 struct stm_device *stm;
0856 unsigned int nmasters;
0857 int err = -ENOMEM;
0858
0859 if (!stm_core_up)
0860 return -EPROBE_DEFER;
0861
0862 if (!stm_data->packet || !stm_data->sw_nchannels)
0863 return -EINVAL;
0864
0865 nmasters = stm_data->sw_end - stm_data->sw_start + 1;
0866 stm = vzalloc(sizeof(*stm) + nmasters * sizeof(void *));
0867 if (!stm)
0868 return -ENOMEM;
0869
0870 stm->major = register_chrdev(0, stm_data->name, &stm_fops);
0871 if (stm->major < 0)
0872 goto err_free;
0873
0874 device_initialize(&stm->dev);
0875 stm->dev.devt = MKDEV(stm->major, 0);
0876 stm->dev.class = &stm_class;
0877 stm->dev.parent = parent;
0878 stm->dev.release = stm_device_release;
0879
0880 mutex_init(&stm->link_mutex);
0881 spin_lock_init(&stm->link_lock);
0882 INIT_LIST_HEAD(&stm->link_list);
0883
0884
0885 spin_lock_init(&stm->mc_lock);
0886 mutex_init(&stm->policy_mutex);
0887 stm->sw_nmasters = nmasters;
0888 stm->owner = owner;
0889 stm->data = stm_data;
0890 stm_data->stm = stm;
0891
0892 err = kobject_set_name(&stm->dev.kobj, "%s", stm_data->name);
0893 if (err)
0894 goto err_device;
0895
0896 err = device_add(&stm->dev);
0897 if (err)
0898 goto err_device;
0899
0900
0901
0902
0903
0904
0905 pm_runtime_no_callbacks(&stm->dev);
0906 pm_runtime_use_autosuspend(&stm->dev);
0907 pm_runtime_set_autosuspend_delay(&stm->dev, 2000);
0908 pm_runtime_set_suspended(&stm->dev);
0909 pm_runtime_enable(&stm->dev);
0910
0911 return 0;
0912
0913 err_device:
0914 unregister_chrdev(stm->major, stm_data->name);
0915
0916
0917 put_device(&stm->dev);
0918 err_free:
0919 vfree(stm);
0920
0921 return err;
0922 }
0923 EXPORT_SYMBOL_GPL(stm_register_device);
0924
0925 static int __stm_source_link_drop(struct stm_source_device *src,
0926 struct stm_device *stm);
0927
0928 void stm_unregister_device(struct stm_data *stm_data)
0929 {
0930 struct stm_device *stm = stm_data->stm;
0931 struct stm_source_device *src, *iter;
0932 int i, ret;
0933
0934 pm_runtime_dont_use_autosuspend(&stm->dev);
0935 pm_runtime_disable(&stm->dev);
0936
0937 mutex_lock(&stm->link_mutex);
0938 list_for_each_entry_safe(src, iter, &stm->link_list, link_entry) {
0939 ret = __stm_source_link_drop(src, stm);
0940
0941
0942
0943
0944
0945
0946
0947 WARN_ON_ONCE(ret);
0948 }
0949 mutex_unlock(&stm->link_mutex);
0950
0951 synchronize_srcu(&stm_source_srcu);
0952
0953 unregister_chrdev(stm->major, stm_data->name);
0954
0955 mutex_lock(&stm->policy_mutex);
0956 if (stm->policy)
0957 stp_policy_unbind(stm->policy);
0958 mutex_unlock(&stm->policy_mutex);
0959
0960 for (i = stm->data->sw_start; i <= stm->data->sw_end; i++)
0961 stp_master_free(stm, i);
0962
0963 device_unregister(&stm->dev);
0964 stm_data->stm = NULL;
0965 }
0966 EXPORT_SYMBOL_GPL(stm_unregister_device);
0967
0968
0969
0970
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989 static int stm_source_link_add(struct stm_source_device *src,
0990 struct stm_device *stm)
0991 {
0992 char *ids[] = { NULL, "default", NULL };
0993 int err = -ENOMEM;
0994
0995 mutex_lock(&stm->link_mutex);
0996 spin_lock(&stm->link_lock);
0997 spin_lock(&src->link_lock);
0998
0999
1000 rcu_assign_pointer(src->link, stm);
1001 list_add_tail(&src->link_entry, &stm->link_list);
1002
1003 spin_unlock(&src->link_lock);
1004 spin_unlock(&stm->link_lock);
1005 mutex_unlock(&stm->link_mutex);
1006
1007 ids[0] = kstrdup(src->data->name, GFP_KERNEL);
1008 if (!ids[0])
1009 goto fail_detach;
1010
1011 err = stm_assign_first_policy(stm, &src->output, ids,
1012 src->data->nr_chans);
1013 kfree(ids[0]);
1014
1015 if (err)
1016 goto fail_detach;
1017
1018
1019 if (stm->data->link)
1020 err = stm->data->link(stm->data, src->output.master,
1021 src->output.channel);
1022
1023 if (err)
1024 goto fail_free_output;
1025
1026
1027 if (src->data->link)
1028 src->data->link(src->data);
1029
1030 return 0;
1031
1032 fail_free_output:
1033 stm_output_free(stm, &src->output);
1034
1035 fail_detach:
1036 mutex_lock(&stm->link_mutex);
1037 spin_lock(&stm->link_lock);
1038 spin_lock(&src->link_lock);
1039
1040 rcu_assign_pointer(src->link, NULL);
1041 list_del_init(&src->link_entry);
1042
1043 spin_unlock(&src->link_lock);
1044 spin_unlock(&stm->link_lock);
1045 mutex_unlock(&stm->link_mutex);
1046
1047 return err;
1048 }
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060 static int __stm_source_link_drop(struct stm_source_device *src,
1061 struct stm_device *stm)
1062 {
1063 struct stm_device *link;
1064 int ret = 0;
1065
1066 lockdep_assert_held(&stm->link_mutex);
1067
1068
1069 spin_lock(&stm->link_lock);
1070 spin_lock(&src->link_lock);
1071 link = srcu_dereference_check(src->link, &stm_source_srcu, 1);
1072
1073
1074
1075
1076
1077
1078 if (link != stm) {
1079 ret = -EAGAIN;
1080 goto unlock;
1081 }
1082
1083 stm_output_free(link, &src->output);
1084 list_del_init(&src->link_entry);
1085 pm_runtime_mark_last_busy(&link->dev);
1086 pm_runtime_put_autosuspend(&link->dev);
1087
1088 stm_put_device(link);
1089 rcu_assign_pointer(src->link, NULL);
1090
1091 unlock:
1092 spin_unlock(&src->link_lock);
1093 spin_unlock(&stm->link_lock);
1094
1095
1096
1097
1098
1099 if (!ret) {
1100 if (src->data->unlink)
1101 src->data->unlink(src->data);
1102
1103 if (stm->data->unlink)
1104 stm->data->unlink(stm->data, src->output.master,
1105 src->output.channel);
1106 }
1107
1108 return ret;
1109 }
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121 static void stm_source_link_drop(struct stm_source_device *src)
1122 {
1123 struct stm_device *stm;
1124 int idx, ret;
1125
1126 retry:
1127 idx = srcu_read_lock(&stm_source_srcu);
1128
1129
1130
1131
1132
1133 stm = srcu_dereference(src->link, &stm_source_srcu);
1134
1135 ret = 0;
1136 if (stm) {
1137 mutex_lock(&stm->link_mutex);
1138 ret = __stm_source_link_drop(src, stm);
1139 mutex_unlock(&stm->link_mutex);
1140 }
1141
1142 srcu_read_unlock(&stm_source_srcu, idx);
1143
1144
1145 if (ret == -EAGAIN)
1146 goto retry;
1147 }
1148
1149 static ssize_t stm_source_link_show(struct device *dev,
1150 struct device_attribute *attr,
1151 char *buf)
1152 {
1153 struct stm_source_device *src = to_stm_source_device(dev);
1154 struct stm_device *stm;
1155 int idx, ret;
1156
1157 idx = srcu_read_lock(&stm_source_srcu);
1158 stm = srcu_dereference(src->link, &stm_source_srcu);
1159 ret = sprintf(buf, "%s\n",
1160 stm ? dev_name(&stm->dev) : "<none>");
1161 srcu_read_unlock(&stm_source_srcu, idx);
1162
1163 return ret;
1164 }
1165
1166 static ssize_t stm_source_link_store(struct device *dev,
1167 struct device_attribute *attr,
1168 const char *buf, size_t count)
1169 {
1170 struct stm_source_device *src = to_stm_source_device(dev);
1171 struct stm_device *link;
1172 int err;
1173
1174 stm_source_link_drop(src);
1175
1176 link = stm_find_device(buf);
1177 if (!link)
1178 return -EINVAL;
1179
1180 pm_runtime_get(&link->dev);
1181
1182 err = stm_source_link_add(src, link);
1183 if (err) {
1184 pm_runtime_put_autosuspend(&link->dev);
1185
1186 stm_put_device(link);
1187 }
1188
1189 return err ? : count;
1190 }
1191
1192 static DEVICE_ATTR_RW(stm_source_link);
1193
1194 static struct attribute *stm_source_attrs[] = {
1195 &dev_attr_stm_source_link.attr,
1196 NULL,
1197 };
1198
1199 ATTRIBUTE_GROUPS(stm_source);
1200
1201 static struct class stm_source_class = {
1202 .name = "stm_source",
1203 .dev_groups = stm_source_groups,
1204 };
1205
1206 static void stm_source_device_release(struct device *dev)
1207 {
1208 struct stm_source_device *src = to_stm_source_device(dev);
1209
1210 kfree(src);
1211 }
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223 int stm_source_register_device(struct device *parent,
1224 struct stm_source_data *data)
1225 {
1226 struct stm_source_device *src;
1227 int err;
1228
1229 if (!stm_core_up)
1230 return -EPROBE_DEFER;
1231
1232 src = kzalloc(sizeof(*src), GFP_KERNEL);
1233 if (!src)
1234 return -ENOMEM;
1235
1236 device_initialize(&src->dev);
1237 src->dev.class = &stm_source_class;
1238 src->dev.parent = parent;
1239 src->dev.release = stm_source_device_release;
1240
1241 err = kobject_set_name(&src->dev.kobj, "%s", data->name);
1242 if (err)
1243 goto err;
1244
1245 pm_runtime_no_callbacks(&src->dev);
1246 pm_runtime_forbid(&src->dev);
1247
1248 err = device_add(&src->dev);
1249 if (err)
1250 goto err;
1251
1252 stm_output_init(&src->output);
1253 spin_lock_init(&src->link_lock);
1254 INIT_LIST_HEAD(&src->link_entry);
1255 src->data = data;
1256 data->src = src;
1257
1258 return 0;
1259
1260 err:
1261 put_device(&src->dev);
1262
1263 return err;
1264 }
1265 EXPORT_SYMBOL_GPL(stm_source_register_device);
1266
1267
1268
1269
1270
1271
1272
1273 void stm_source_unregister_device(struct stm_source_data *data)
1274 {
1275 struct stm_source_device *src = data->src;
1276
1277 stm_source_link_drop(src);
1278
1279 device_unregister(&src->dev);
1280 }
1281 EXPORT_SYMBOL_GPL(stm_source_unregister_device);
1282
1283 int notrace stm_source_write(struct stm_source_data *data,
1284 unsigned int chan,
1285 const char *buf, size_t count)
1286 {
1287 struct stm_source_device *src = data->src;
1288 struct stm_device *stm;
1289 int idx;
1290
1291 if (!src->output.nr_chans)
1292 return -ENODEV;
1293
1294 if (chan >= src->output.nr_chans)
1295 return -EINVAL;
1296
1297 idx = srcu_read_lock(&stm_source_srcu);
1298
1299 stm = srcu_dereference(src->link, &stm_source_srcu);
1300 if (stm)
1301 count = stm_write(stm, &src->output, chan, buf, count);
1302 else
1303 count = -ENODEV;
1304
1305 srcu_read_unlock(&stm_source_srcu, idx);
1306
1307 return count;
1308 }
1309 EXPORT_SYMBOL_GPL(stm_source_write);
1310
1311 static int __init stm_core_init(void)
1312 {
1313 int err;
1314
1315 err = class_register(&stm_class);
1316 if (err)
1317 return err;
1318
1319 err = class_register(&stm_source_class);
1320 if (err)
1321 goto err_stm;
1322
1323 err = stp_configfs_init();
1324 if (err)
1325 goto err_src;
1326
1327 init_srcu_struct(&stm_source_srcu);
1328 INIT_LIST_HEAD(&stm_pdrv_head);
1329 mutex_init(&stm_pdrv_mutex);
1330
1331
1332
1333
1334
1335 if (IS_ENABLED(CONFIG_STM_PROTO_BASIC))
1336 (void)request_module_nowait("stm_p_basic");
1337 stm_core_up++;
1338
1339 return 0;
1340
1341 err_src:
1342 class_unregister(&stm_source_class);
1343 err_stm:
1344 class_unregister(&stm_class);
1345
1346 return err;
1347 }
1348
1349 module_init(stm_core_init);
1350
1351 static void __exit stm_core_exit(void)
1352 {
1353 cleanup_srcu_struct(&stm_source_srcu);
1354 class_unregister(&stm_source_class);
1355 class_unregister(&stm_class);
1356 stp_configfs_exit();
1357 }
1358
1359 module_exit(stm_core_exit);
1360
1361 MODULE_LICENSE("GPL v2");
1362 MODULE_DESCRIPTION("System Trace Module device class");
1363 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");