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