Linux Audio

Check our new training course

Real-Time Linux with PREEMPT_RT training

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