Linux Audio

Check our new training course

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