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