Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Intel(R) Trace Hub Memory Storage Unit
   4 *
   5 * Copyright (C) 2014-2015 Intel Corporation.
 
 
 
 
 
 
 
 
 
   6 */
   7
   8#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
   9
  10#include <linux/types.h>
  11#include <linux/module.h>
  12#include <linux/device.h>
  13#include <linux/uaccess.h>
  14#include <linux/sizes.h>
  15#include <linux/printk.h>
  16#include <linux/slab.h>
  17#include <linux/mm.h>
  18#include <linux/fs.h>
  19#include <linux/io.h>
  20#include <linux/workqueue.h>
  21#include <linux/dma-mapping.h>
  22
  23#ifdef CONFIG_X86
  24#include <asm/set_memory.h>
  25#endif
  26
  27#include <linux/intel_th.h>
  28#include "intel_th.h"
  29#include "msu.h"
  30
  31#define msc_dev(x) (&(x)->thdev->dev)
  32
  33/*
  34 * Lockout state transitions:
  35 *   READY -> INUSE -+-> LOCKED -+-> READY -> etc.
  36 *                   \-----------/
  37 * WIN_READY:	window can be used by HW
  38 * WIN_INUSE:	window is in use
  39 * WIN_LOCKED:	window is filled up and is being processed by the buffer
  40 * handling code
  41 *
  42 * All state transitions happen automatically, except for the LOCKED->READY,
  43 * which needs to be signalled by the buffer code by calling
  44 * intel_th_msc_window_unlock().
  45 *
  46 * When the interrupt handler has to switch to the next window, it checks
  47 * whether it's READY, and if it is, it performs the switch and tracing
  48 * continues. If it's LOCKED, it stops the trace.
  49 */
  50enum lockout_state {
  51	WIN_READY = 0,
  52	WIN_INUSE,
  53	WIN_LOCKED
  54};
  55
  56/**
  57 * struct msc_window - multiblock mode window descriptor
  58 * @entry:	window list linkage (msc::win_list)
  59 * @pgoff:	page offset into the buffer that this window starts at
  60 * @lockout:	lockout state, see comment below
  61 * @lo_lock:	lockout state serialization
  62 * @nr_blocks:	number of blocks (pages) in this window
  63 * @nr_segs:	number of segments in this window (<= @nr_blocks)
  64 * @_sgt:	array of block descriptors
  65 * @sgt:	array of block descriptors
  66 */
  67struct msc_window {
  68	struct list_head	entry;
  69	unsigned long		pgoff;
  70	enum lockout_state	lockout;
  71	spinlock_t		lo_lock;
  72	unsigned int		nr_blocks;
  73	unsigned int		nr_segs;
  74	struct msc		*msc;
  75	struct sg_table		_sgt;
  76	struct sg_table		*sgt;
  77};
  78
  79/**
  80 * struct msc_iter - iterator for msc buffer
  81 * @entry:		msc::iter_list linkage
  82 * @msc:		pointer to the MSC device
  83 * @start_win:		oldest window
  84 * @win:		current window
  85 * @offset:		current logical offset into the buffer
  86 * @start_block:	oldest block in the window
  87 * @block:		block number in the window
  88 * @block_off:		offset into current block
  89 * @wrap_count:		block wrapping handling
  90 * @eof:		end of buffer reached
  91 */
  92struct msc_iter {
  93	struct list_head	entry;
  94	struct msc		*msc;
  95	struct msc_window	*start_win;
  96	struct msc_window	*win;
  97	unsigned long		offset;
  98	struct scatterlist	*start_block;
  99	struct scatterlist	*block;
 100	unsigned int		block_off;
 101	unsigned int		wrap_count;
 102	unsigned int		eof;
 103};
 104
 105/**
 106 * struct msc - MSC device representation
 107 * @reg_base:		register window base address
 108 * @thdev:		intel_th_device pointer
 109 * @mbuf:		MSU buffer, if assigned
 110 * @mbuf_priv		MSU buffer's private data, if @mbuf
 111 * @win_list:		list of windows in multiblock mode
 112 * @single_sgt:		single mode buffer
 113 * @cur_win:		current window
 114 * @nr_pages:		total number of pages allocated for this buffer
 115 * @single_sz:		amount of data in single mode
 116 * @single_wrap:	single mode wrap occurred
 117 * @base:		buffer's base pointer
 118 * @base_addr:		buffer's base address
 119 * @user_count:		number of users of the buffer
 120 * @mmap_count:		number of mappings
 121 * @buf_mutex:		mutex to serialize access to buffer-related bits
 122
 123 * @enabled:		MSC is enabled
 124 * @wrap:		wrapping is enabled
 125 * @mode:		MSC operating mode
 126 * @burst_len:		write burst length
 127 * @index:		number of this MSC in the MSU
 128 */
 129struct msc {
 130	void __iomem		*reg_base;
 131	void __iomem		*msu_base;
 132	struct intel_th_device	*thdev;
 133
 134	const struct msu_buffer	*mbuf;
 135	void			*mbuf_priv;
 136
 137	struct work_struct	work;
 138	struct list_head	win_list;
 139	struct sg_table		single_sgt;
 140	struct msc_window	*cur_win;
 141	struct msc_window	*switch_on_unlock;
 142	unsigned long		nr_pages;
 143	unsigned long		single_sz;
 144	unsigned int		single_wrap : 1;
 145	void			*base;
 146	dma_addr_t		base_addr;
 147	u32			orig_addr;
 148	u32			orig_sz;
 149
 150	/* <0: no buffer, 0: no users, >0: active users */
 151	atomic_t		user_count;
 152
 153	atomic_t		mmap_count;
 154	struct mutex		buf_mutex;
 155
 156	struct list_head	iter_list;
 157
 158	bool			stop_on_full;
 159
 160	/* config */
 161	unsigned int		enabled : 1,
 162				wrap	: 1,
 163				do_irq	: 1,
 164				multi_is_broken : 1;
 165	unsigned int		mode;
 166	unsigned int		burst_len;
 167	unsigned int		index;
 168};
 169
 170static LIST_HEAD(msu_buffer_list);
 171static DEFINE_MUTEX(msu_buffer_mutex);
 172
 173/**
 174 * struct msu_buffer_entry - internal MSU buffer bookkeeping
 175 * @entry:	link to msu_buffer_list
 176 * @mbuf:	MSU buffer object
 177 * @owner:	module that provides this MSU buffer
 178 */
 179struct msu_buffer_entry {
 180	struct list_head	entry;
 181	const struct msu_buffer	*mbuf;
 182	struct module		*owner;
 183};
 184
 185static struct msu_buffer_entry *__msu_buffer_entry_find(const char *name)
 186{
 187	struct msu_buffer_entry *mbe;
 188
 189	lockdep_assert_held(&msu_buffer_mutex);
 190
 191	list_for_each_entry(mbe, &msu_buffer_list, entry) {
 192		if (!strcmp(mbe->mbuf->name, name))
 193			return mbe;
 194	}
 195
 196	return NULL;
 197}
 198
 199static const struct msu_buffer *
 200msu_buffer_get(const char *name)
 201{
 202	struct msu_buffer_entry *mbe;
 203
 204	mutex_lock(&msu_buffer_mutex);
 205	mbe = __msu_buffer_entry_find(name);
 206	if (mbe && !try_module_get(mbe->owner))
 207		mbe = NULL;
 208	mutex_unlock(&msu_buffer_mutex);
 209
 210	return mbe ? mbe->mbuf : NULL;
 211}
 212
 213static void msu_buffer_put(const struct msu_buffer *mbuf)
 214{
 215	struct msu_buffer_entry *mbe;
 216
 217	mutex_lock(&msu_buffer_mutex);
 218	mbe = __msu_buffer_entry_find(mbuf->name);
 219	if (mbe)
 220		module_put(mbe->owner);
 221	mutex_unlock(&msu_buffer_mutex);
 222}
 223
 224int intel_th_msu_buffer_register(const struct msu_buffer *mbuf,
 225				 struct module *owner)
 226{
 227	struct msu_buffer_entry *mbe;
 228	int ret = 0;
 229
 230	mbe = kzalloc(sizeof(*mbe), GFP_KERNEL);
 231	if (!mbe)
 232		return -ENOMEM;
 233
 234	mutex_lock(&msu_buffer_mutex);
 235	if (__msu_buffer_entry_find(mbuf->name)) {
 236		ret = -EEXIST;
 237		kfree(mbe);
 238		goto unlock;
 239	}
 240
 241	mbe->mbuf = mbuf;
 242	mbe->owner = owner;
 243	list_add_tail(&mbe->entry, &msu_buffer_list);
 244unlock:
 245	mutex_unlock(&msu_buffer_mutex);
 246
 247	return ret;
 248}
 249EXPORT_SYMBOL_GPL(intel_th_msu_buffer_register);
 250
 251void intel_th_msu_buffer_unregister(const struct msu_buffer *mbuf)
 252{
 253	struct msu_buffer_entry *mbe;
 254
 255	mutex_lock(&msu_buffer_mutex);
 256	mbe = __msu_buffer_entry_find(mbuf->name);
 257	if (mbe) {
 258		list_del(&mbe->entry);
 259		kfree(mbe);
 260	}
 261	mutex_unlock(&msu_buffer_mutex);
 262}
 263EXPORT_SYMBOL_GPL(intel_th_msu_buffer_unregister);
 264
 265static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
 266{
 267	/* header hasn't been written */
 268	if (!bdesc->valid_dw)
 269		return true;
 270
 271	/* valid_dw includes the header */
 272	if (!msc_data_sz(bdesc))
 273		return true;
 274
 275	return false;
 276}
 277
 278static inline struct scatterlist *msc_win_base_sg(struct msc_window *win)
 279{
 280	return win->sgt->sgl;
 281}
 282
 283static inline struct msc_block_desc *msc_win_base(struct msc_window *win)
 284{
 285	return sg_virt(msc_win_base_sg(win));
 286}
 287
 288static inline dma_addr_t msc_win_base_dma(struct msc_window *win)
 289{
 290	return sg_dma_address(msc_win_base_sg(win));
 291}
 292
 293static inline unsigned long
 294msc_win_base_pfn(struct msc_window *win)
 295{
 296	return PFN_DOWN(msc_win_base_dma(win));
 297}
 298
 299/**
 300 * msc_is_last_win() - check if a window is the last one for a given MSC
 301 * @win:	window
 302 * Return:	true if @win is the last window in MSC's multiblock buffer
 303 */
 304static inline bool msc_is_last_win(struct msc_window *win)
 305{
 306	return win->entry.next == &win->msc->win_list;
 307}
 308
 309/**
 310 * msc_next_window() - return next window in the multiblock buffer
 311 * @win:	current window
 312 *
 313 * Return:	window following the current one
 314 */
 315static struct msc_window *msc_next_window(struct msc_window *win)
 316{
 317	if (msc_is_last_win(win))
 318		return list_first_entry(&win->msc->win_list, struct msc_window,
 319					entry);
 320
 321	return list_next_entry(win, entry);
 322}
 323
 324static size_t msc_win_total_sz(struct msc_window *win)
 325{
 326	struct scatterlist *sg;
 327	unsigned int blk;
 328	size_t size = 0;
 329
 330	for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
 331		struct msc_block_desc *bdesc = sg_virt(sg);
 332
 333		if (msc_block_wrapped(bdesc))
 334			return (size_t)win->nr_blocks << PAGE_SHIFT;
 335
 336		size += msc_total_sz(bdesc);
 337		if (msc_block_last_written(bdesc))
 338			break;
 339	}
 340
 341	return size;
 342}
 343
 344/**
 345 * msc_find_window() - find a window matching a given sg_table
 346 * @msc:	MSC device
 347 * @sgt:	SG table of the window
 348 * @nonempty:	skip over empty windows
 349 *
 350 * Return:	MSC window structure pointer or NULL if the window
 351 *		could not be found.
 
 
 352 */
 353static struct msc_window *
 354msc_find_window(struct msc *msc, struct sg_table *sgt, bool nonempty)
 355{
 356	struct msc_window *win;
 
 
 357	unsigned int found = 0;
 358
 359	if (list_empty(&msc->win_list))
 360		return NULL;
 361
 362	/*
 363	 * we might need a radix tree for this, depending on how
 364	 * many windows a typical user would allocate; ideally it's
 365	 * something like 2, in which case we're good
 366	 */
 367	list_for_each_entry(win, &msc->win_list, entry) {
 368		if (win->sgt == sgt)
 369			found++;
 370
 371		/* skip the empty ones */
 372		if (nonempty && msc_block_is_empty(msc_win_base(win)))
 373			continue;
 374
 375		if (found)
 376			return win;
 377	}
 378
 379	return NULL;
 380}
 381
 382/**
 383 * msc_oldest_window() - locate the window with oldest data
 384 * @msc:	MSC device
 385 *
 386 * This should only be used in multiblock mode. Caller should hold the
 387 * msc::user_count reference.
 388 *
 389 * Return:	the oldest window with valid data
 390 */
 391static struct msc_window *msc_oldest_window(struct msc *msc)
 392{
 393	struct msc_window *win;
 394
 395	if (list_empty(&msc->win_list))
 396		return NULL;
 397
 398	win = msc_find_window(msc, msc_next_window(msc->cur_win)->sgt, true);
 399	if (win)
 400		return win;
 401
 402	return list_first_entry(&msc->win_list, struct msc_window, entry);
 403}
 404
 405/**
 406 * msc_win_oldest_sg() - locate the oldest block in a given window
 407 * @win:	window to look at
 408 *
 409 * Return:	index of the block with the oldest data
 410 */
 411static struct scatterlist *msc_win_oldest_sg(struct msc_window *win)
 412{
 413	unsigned int blk;
 414	struct scatterlist *sg;
 415	struct msc_block_desc *bdesc = msc_win_base(win);
 416
 417	/* without wrapping, first block is the oldest */
 418	if (!msc_block_wrapped(bdesc))
 419		return msc_win_base_sg(win);
 420
 421	/*
 422	 * with wrapping, last written block contains both the newest and the
 423	 * oldest data for this window.
 424	 */
 425	for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
 426		struct msc_block_desc *bdesc = sg_virt(sg);
 427
 428		if (msc_block_last_written(bdesc))
 429			return sg;
 430	}
 431
 432	return msc_win_base_sg(win);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 433}
 434
 435static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
 436{
 437	return sg_virt(iter->block);
 
 
 
 
 
 
 
 438}
 439
 440static struct msc_iter *msc_iter_install(struct msc *msc)
 441{
 442	struct msc_iter *iter;
 443
 444	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
 445	if (!iter)
 446		return ERR_PTR(-ENOMEM);
 447
 448	mutex_lock(&msc->buf_mutex);
 449
 450	/*
 451	 * Reading and tracing are mutually exclusive; if msc is
 452	 * enabled, open() will fail; otherwise existing readers
 453	 * will prevent enabling the msc and the rest of fops don't
 454	 * need to worry about it.
 455	 */
 456	if (msc->enabled) {
 457		kfree(iter);
 458		iter = ERR_PTR(-EBUSY);
 459		goto unlock;
 460	}
 461
 
 462	iter->msc = msc;
 463
 464	list_add_tail(&iter->entry, &msc->iter_list);
 465unlock:
 466	mutex_unlock(&msc->buf_mutex);
 467
 468	return iter;
 469}
 470
 471static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
 472{
 473	mutex_lock(&msc->buf_mutex);
 474	list_del(&iter->entry);
 475	mutex_unlock(&msc->buf_mutex);
 476
 477	kfree(iter);
 478}
 479
 480static void msc_iter_block_start(struct msc_iter *iter)
 481{
 482	if (iter->start_block)
 483		return;
 484
 485	iter->start_block = msc_win_oldest_sg(iter->win);
 486	iter->block = iter->start_block;
 487	iter->wrap_count = 0;
 488
 489	/*
 490	 * start with the block with oldest data; if data has wrapped
 491	 * in this window, it should be in this block
 492	 */
 493	if (msc_block_wrapped(msc_iter_bdesc(iter)))
 494		iter->wrap_count = 2;
 495
 496}
 497
 498static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
 499{
 500	/* already started, nothing to do */
 501	if (iter->start_win)
 502		return 0;
 503
 504	iter->start_win = msc_oldest_window(msc);
 505	if (!iter->start_win)
 506		return -EINVAL;
 507
 508	iter->win = iter->start_win;
 509	iter->start_block = NULL;
 510
 511	msc_iter_block_start(iter);
 512
 513	return 0;
 514}
 515
 516static int msc_iter_win_advance(struct msc_iter *iter)
 517{
 518	iter->win = msc_next_window(iter->win);
 519	iter->start_block = NULL;
 520
 521	if (iter->win == iter->start_win) {
 522		iter->eof++;
 523		return 1;
 524	}
 525
 526	msc_iter_block_start(iter);
 527
 528	return 0;
 529}
 530
 531static int msc_iter_block_advance(struct msc_iter *iter)
 532{
 533	iter->block_off = 0;
 534
 535	/* wrapping */
 536	if (iter->wrap_count && iter->block == iter->start_block) {
 537		iter->wrap_count--;
 538		if (!iter->wrap_count)
 539			/* copied newest data from the wrapped block */
 540			return msc_iter_win_advance(iter);
 541	}
 542
 543	/* no wrapping, check for last written block */
 544	if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
 545		/* copied newest data for the window */
 546		return msc_iter_win_advance(iter);
 547
 548	/* block advance */
 549	if (sg_is_last(iter->block))
 550		iter->block = msc_win_base_sg(iter->win);
 551	else
 552		iter->block = sg_next(iter->block);
 553
 554	/* no wrapping, sanity check in case there is no last written block */
 555	if (!iter->wrap_count && iter->block == iter->start_block)
 556		return msc_iter_win_advance(iter);
 557
 558	return 0;
 559}
 560
 561/**
 562 * msc_buffer_iterate() - go through multiblock buffer's data
 563 * @iter:	iterator structure
 564 * @size:	amount of data to scan
 565 * @data:	callback's private data
 566 * @fn:		iterator callback
 567 *
 568 * This will start at the window which will be written to next (containing
 569 * the oldest data) and work its way to the current window, calling @fn
 570 * for each chunk of data as it goes.
 571 *
 572 * Caller should have msc::user_count reference to make sure the buffer
 573 * doesn't disappear from under us.
 574 *
 575 * Return:	amount of data actually scanned.
 576 */
 577static ssize_t
 578msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
 579		   unsigned long (*fn)(void *, void *, size_t))
 580{
 581	struct msc *msc = iter->msc;
 582	size_t len = size;
 583	unsigned int advance;
 584
 585	if (iter->eof)
 586		return 0;
 587
 588	/* start with the oldest window */
 589	if (msc_iter_win_start(iter, msc))
 590		return 0;
 591
 592	do {
 593		unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
 594		void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
 595		size_t tocopy = data_bytes, copied = 0;
 596		size_t remaining = 0;
 597
 598		advance = 1;
 599
 600		/*
 601		 * If block wrapping happened, we need to visit the last block
 602		 * twice, because it contains both the oldest and the newest
 603		 * data in this window.
 604		 *
 605		 * First time (wrap_count==2), in the very beginning, to collect
 606		 * the oldest data, which is in the range
 607		 * (data_bytes..DATA_IN_PAGE).
 608		 *
 609		 * Second time (wrap_count==1), it's just like any other block,
 610		 * containing data in the range of [MSC_BDESC..data_bytes].
 611		 */
 612		if (iter->block == iter->start_block && iter->wrap_count == 2) {
 613			tocopy = DATA_IN_PAGE - data_bytes;
 614			src += data_bytes;
 615		}
 616
 617		if (!tocopy)
 618			goto next_block;
 619
 620		tocopy -= iter->block_off;
 621		src += iter->block_off;
 622
 623		if (len < tocopy) {
 624			tocopy = len;
 625			advance = 0;
 626		}
 627
 628		remaining = fn(data, src, tocopy);
 629
 630		if (remaining)
 631			advance = 0;
 632
 633		copied = tocopy - remaining;
 634		len -= copied;
 635		iter->block_off += copied;
 636		iter->offset += copied;
 637
 638		if (!advance)
 639			break;
 640
 641next_block:
 642		if (msc_iter_block_advance(iter))
 643			break;
 644
 645	} while (len);
 646
 647	return size - len;
 648}
 649
 650/**
 651 * msc_buffer_clear_hw_header() - clear hw header for multiblock
 652 * @msc:	MSC device
 653 */
 654static void msc_buffer_clear_hw_header(struct msc *msc)
 655{
 656	struct msc_window *win;
 657	struct scatterlist *sg;
 658
 659	list_for_each_entry(win, &msc->win_list, entry) {
 660		unsigned int blk;
 
 
 661
 662		for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
 663			struct msc_block_desc *bdesc = sg_virt(sg);
 664
 665			memset_startat(bdesc, 0, hw_tag);
 666		}
 667	}
 668}
 669
 670static int intel_th_msu_init(struct msc *msc)
 671{
 672	u32 mintctl, msusts;
 673
 674	if (!msc->do_irq)
 675		return 0;
 676
 677	if (!msc->mbuf)
 678		return 0;
 679
 680	mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
 681	mintctl |= msc->index ? M1BLIE : M0BLIE;
 682	iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
 683	if (mintctl != ioread32(msc->msu_base + REG_MSU_MINTCTL)) {
 684		dev_info(msc_dev(msc), "MINTCTL ignores writes: no usable interrupts\n");
 685		msc->do_irq = 0;
 686		return 0;
 687	}
 688
 689	msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
 690	iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
 691
 692	return 0;
 693}
 694
 695static void intel_th_msu_deinit(struct msc *msc)
 696{
 697	u32 mintctl;
 698
 699	if (!msc->do_irq)
 700		return;
 701
 702	mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
 703	mintctl &= msc->index ? ~M1BLIE : ~M0BLIE;
 704	iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
 705}
 706
 707static int msc_win_set_lockout(struct msc_window *win,
 708			       enum lockout_state expect,
 709			       enum lockout_state new)
 710{
 711	enum lockout_state old;
 712	unsigned long flags;
 713	int ret = 0;
 714
 715	if (!win->msc->mbuf)
 716		return 0;
 717
 718	spin_lock_irqsave(&win->lo_lock, flags);
 719	old = win->lockout;
 720
 721	if (old != expect) {
 722		ret = -EINVAL;
 723		goto unlock;
 724	}
 725
 726	win->lockout = new;
 727
 728	if (old == expect && new == WIN_LOCKED)
 729		atomic_inc(&win->msc->user_count);
 730	else if (old == expect && old == WIN_LOCKED)
 731		atomic_dec(&win->msc->user_count);
 732
 733unlock:
 734	spin_unlock_irqrestore(&win->lo_lock, flags);
 735
 736	if (ret) {
 737		if (expect == WIN_READY && old == WIN_LOCKED)
 738			return -EBUSY;
 739
 740		/* from intel_th_msc_window_unlock(), don't warn if not locked */
 741		if (expect == WIN_LOCKED && old == new)
 742			return 0;
 743
 744		dev_warn_ratelimited(msc_dev(win->msc),
 745				     "expected lockout state %d, got %d\n",
 746				     expect, old);
 747	}
 748
 749	return ret;
 750}
 751/**
 752 * msc_configure() - set up MSC hardware
 753 * @msc:	the MSC device to configure
 754 *
 755 * Program storage mode, wrapping, burst length and trace buffer address
 756 * into a given MSC. Then, enable tracing and set msc::enabled.
 757 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
 758 */
 759static int msc_configure(struct msc *msc)
 760{
 761	u32 reg;
 762
 763	lockdep_assert_held(&msc->buf_mutex);
 764
 765	if (msc->mode > MSC_MODE_MULTI)
 766		return -EINVAL;
 767
 768	if (msc->mode == MSC_MODE_MULTI) {
 769		if (msc_win_set_lockout(msc->cur_win, WIN_READY, WIN_INUSE))
 770			return -EBUSY;
 771
 
 772		msc_buffer_clear_hw_header(msc);
 773	}
 774
 775	msc->orig_addr = ioread32(msc->reg_base + REG_MSU_MSC0BAR);
 776	msc->orig_sz   = ioread32(msc->reg_base + REG_MSU_MSC0SIZE);
 777
 778	reg = msc->base_addr >> PAGE_SHIFT;
 779	iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
 780
 781	if (msc->mode == MSC_MODE_SINGLE) {
 782		reg = msc->nr_pages;
 783		iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
 784	}
 785
 786	reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
 787	reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
 788
 789	reg |= MSC_EN;
 790	reg |= msc->mode << __ffs(MSC_MODE);
 791	reg |= msc->burst_len << __ffs(MSC_LEN);
 792
 793	if (msc->wrap)
 794		reg |= MSC_WRAPEN;
 795
 796	iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
 797
 798	intel_th_msu_init(msc);
 799
 800	msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
 801	intel_th_trace_enable(msc->thdev);
 802	msc->enabled = 1;
 803
 804	if (msc->mbuf && msc->mbuf->activate)
 805		msc->mbuf->activate(msc->mbuf_priv);
 806
 807	return 0;
 808}
 809
 810/**
 811 * msc_disable() - disable MSC hardware
 812 * @msc:	MSC device to disable
 813 *
 814 * If @msc is enabled, disable tracing on the switch and then disable MSC
 815 * storage. Caller must hold msc::buf_mutex.
 816 */
 817static void msc_disable(struct msc *msc)
 818{
 819	struct msc_window *win = msc->cur_win;
 820	u32 reg;
 821
 822	lockdep_assert_held(&msc->buf_mutex);
 823
 824	if (msc->mode == MSC_MODE_MULTI)
 825		msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
 826
 827	if (msc->mbuf && msc->mbuf->deactivate)
 828		msc->mbuf->deactivate(msc->mbuf_priv);
 829	intel_th_msu_deinit(msc);
 830	intel_th_trace_disable(msc->thdev);
 831
 832	if (msc->mode == MSC_MODE_SINGLE) {
 
 833		reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
 
 
 
 
 
 
 
 834		msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
 835
 836		reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
 837		msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
 838		dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
 839			reg, msc->single_sz, msc->single_wrap);
 840	}
 841
 842	reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
 843	reg &= ~MSC_EN;
 844	iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
 845
 846	if (msc->mbuf && msc->mbuf->ready)
 847		msc->mbuf->ready(msc->mbuf_priv, win->sgt,
 848				 msc_win_total_sz(win));
 849
 850	msc->enabled = 0;
 851
 852	iowrite32(msc->orig_addr, msc->reg_base + REG_MSU_MSC0BAR);
 853	iowrite32(msc->orig_sz, msc->reg_base + REG_MSU_MSC0SIZE);
 854
 855	dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
 856		ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
 857
 858	reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
 859	dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
 860
 861	reg = ioread32(msc->reg_base + REG_MSU_MSUSTS);
 862	reg &= msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
 863	iowrite32(reg, msc->reg_base + REG_MSU_MSUSTS);
 864}
 865
 866static int intel_th_msc_activate(struct intel_th_device *thdev)
 867{
 868	struct msc *msc = dev_get_drvdata(&thdev->dev);
 869	int ret = -EBUSY;
 870
 871	if (!atomic_inc_unless_negative(&msc->user_count))
 872		return -ENODEV;
 873
 874	mutex_lock(&msc->buf_mutex);
 875
 876	/* if there are readers, refuse */
 877	if (list_empty(&msc->iter_list))
 878		ret = msc_configure(msc);
 879
 880	mutex_unlock(&msc->buf_mutex);
 881
 882	if (ret)
 883		atomic_dec(&msc->user_count);
 884
 885	return ret;
 886}
 887
 888static void intel_th_msc_deactivate(struct intel_th_device *thdev)
 889{
 890	struct msc *msc = dev_get_drvdata(&thdev->dev);
 891
 892	mutex_lock(&msc->buf_mutex);
 893	if (msc->enabled) {
 894		msc_disable(msc);
 895		atomic_dec(&msc->user_count);
 896	}
 897	mutex_unlock(&msc->buf_mutex);
 898}
 899
 900/**
 901 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
 902 * @msc:	MSC device
 903 * @size:	allocation size in bytes
 904 *
 905 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
 906 * caller is expected to hold it.
 907 *
 908 * Return:	0 on success, -errno otherwise.
 909 */
 910static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
 911{
 912	unsigned long nr_pages = size >> PAGE_SHIFT;
 913	unsigned int order = get_order(size);
 914	struct page *page;
 915	int ret;
 916
 917	if (!size)
 918		return 0;
 919
 920	ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
 921	if (ret)
 922		goto err_out;
 923
 924	ret = -ENOMEM;
 925	page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
 926	if (!page)
 927		goto err_free_sgt;
 928
 929	split_page(page, order);
 930	sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
 931
 932	ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
 933			 DMA_FROM_DEVICE);
 934	if (ret < 0)
 935		goto err_free_pages;
 936
 937	msc->nr_pages = nr_pages;
 938	msc->base = page_address(page);
 939	msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
 940
 941	return 0;
 942
 943err_free_pages:
 944	__free_pages(page, order);
 945
 946err_free_sgt:
 947	sg_free_table(&msc->single_sgt);
 948
 949err_out:
 950	return ret;
 951}
 952
 953/**
 954 * msc_buffer_contig_free() - free a contiguous buffer
 955 * @msc:	MSC configured in SINGLE mode
 956 */
 957static void msc_buffer_contig_free(struct msc *msc)
 958{
 959	unsigned long off;
 960
 961	dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
 962		     1, DMA_FROM_DEVICE);
 963	sg_free_table(&msc->single_sgt);
 964
 965	for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
 966		struct page *page = virt_to_page(msc->base + off);
 967
 968		page->mapping = NULL;
 969		__free_page(page);
 970	}
 971
 972	msc->nr_pages = 0;
 973}
 974
 975/**
 976 * msc_buffer_contig_get_page() - find a page at a given offset
 977 * @msc:	MSC configured in SINGLE mode
 978 * @pgoff:	page offset
 979 *
 980 * Return:	page, if @pgoff is within the range, NULL otherwise.
 981 */
 982static struct page *msc_buffer_contig_get_page(struct msc *msc,
 983					       unsigned long pgoff)
 984{
 985	if (pgoff >= msc->nr_pages)
 986		return NULL;
 987
 988	return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
 989}
 990
 991static int __msc_buffer_win_alloc(struct msc_window *win,
 992				  unsigned int nr_segs)
 993{
 994	struct scatterlist *sg_ptr;
 995	void *block;
 996	int i, ret;
 997
 998	ret = sg_alloc_table(win->sgt, nr_segs, GFP_KERNEL);
 999	if (ret)
1000		return -ENOMEM;
1001
1002	for_each_sg(win->sgt->sgl, sg_ptr, nr_segs, i) {
1003		block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
1004					  PAGE_SIZE, &sg_dma_address(sg_ptr),
1005					  GFP_KERNEL);
1006		if (!block)
1007			goto err_nomem;
1008
1009		sg_set_buf(sg_ptr, block, PAGE_SIZE);
1010	}
1011
1012	return nr_segs;
1013
1014err_nomem:
1015	for_each_sg(win->sgt->sgl, sg_ptr, i, ret)
1016		dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1017				  sg_virt(sg_ptr), sg_dma_address(sg_ptr));
1018
1019	sg_free_table(win->sgt);
1020
1021	return -ENOMEM;
1022}
1023
1024#ifdef CONFIG_X86
1025static void msc_buffer_set_uc(struct msc *msc)
1026{
1027	struct scatterlist *sg_ptr;
1028	struct msc_window *win;
1029	int i;
1030
1031	if (msc->mode == MSC_MODE_SINGLE) {
1032		set_memory_uc((unsigned long)msc->base, msc->nr_pages);
1033		return;
1034	}
1035
1036	list_for_each_entry(win, &msc->win_list, entry) {
1037		for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
1038			/* Set the page as uncached */
1039			set_memory_uc((unsigned long)sg_virt(sg_ptr),
1040					PFN_DOWN(sg_ptr->length));
1041		}
1042	}
1043}
1044
1045static void msc_buffer_set_wb(struct msc *msc)
1046{
1047	struct scatterlist *sg_ptr;
1048	struct msc_window *win;
1049	int i;
1050
1051	if (msc->mode == MSC_MODE_SINGLE) {
1052		set_memory_wb((unsigned long)msc->base, msc->nr_pages);
1053		return;
1054	}
1055
1056	list_for_each_entry(win, &msc->win_list, entry) {
1057		for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
1058			/* Reset the page to write-back */
1059			set_memory_wb((unsigned long)sg_virt(sg_ptr),
1060					PFN_DOWN(sg_ptr->length));
1061		}
1062	}
1063}
1064#else /* !X86 */
1065static inline void
1066msc_buffer_set_uc(struct msc *msc) {}
1067static inline void msc_buffer_set_wb(struct msc *msc) {}
1068#endif /* CONFIG_X86 */
1069
1070static struct page *msc_sg_page(struct scatterlist *sg)
1071{
1072	void *addr = sg_virt(sg);
1073
1074	if (is_vmalloc_addr(addr))
1075		return vmalloc_to_page(addr);
1076
1077	return sg_page(sg);
1078}
1079
1080/**
1081 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
1082 * @msc:	MSC device
1083 * @nr_blocks:	number of pages in this window
1084 *
1085 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1086 * to serialize, so the caller is expected to hold it.
1087 *
1088 * Return:	0 on success, -errno otherwise.
1089 */
1090static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
1091{
1092	struct msc_window *win;
1093	int ret = -ENOMEM;
 
1094
1095	if (!nr_blocks)
1096		return 0;
1097
1098	win = kzalloc(sizeof(*win), GFP_KERNEL);
 
1099	if (!win)
1100		return -ENOMEM;
1101
1102	win->msc = msc;
1103	win->sgt = &win->_sgt;
1104	win->lockout = WIN_READY;
1105	spin_lock_init(&win->lo_lock);
1106
1107	if (!list_empty(&msc->win_list)) {
1108		struct msc_window *prev = list_last_entry(&msc->win_list,
1109							  struct msc_window,
1110							  entry);
1111
1112		win->pgoff = prev->pgoff + prev->nr_blocks;
1113	}
1114
1115	if (msc->mbuf && msc->mbuf->alloc_window)
1116		ret = msc->mbuf->alloc_window(msc->mbuf_priv, &win->sgt,
1117					      nr_blocks << PAGE_SHIFT);
1118	else
1119		ret = __msc_buffer_win_alloc(win, nr_blocks);
1120
1121	if (ret <= 0)
1122		goto err_nomem;
 
 
 
 
 
 
1123
1124	win->nr_segs = ret;
1125	win->nr_blocks = nr_blocks;
1126
1127	if (list_empty(&msc->win_list)) {
1128		msc->base = msc_win_base(win);
1129		msc->base_addr = msc_win_base_dma(win);
1130		msc->cur_win = win;
1131	}
1132
1133	list_add_tail(&win->entry, &msc->win_list);
1134	msc->nr_pages += nr_blocks;
1135
1136	return 0;
1137
1138err_nomem:
 
 
 
 
 
 
 
 
1139	kfree(win);
1140
1141	return ret;
1142}
1143
1144static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1145{
1146	struct scatterlist *sg;
1147	int i;
1148
1149	for_each_sg(win->sgt->sgl, sg, win->nr_segs, i) {
1150		struct page *page = msc_sg_page(sg);
1151
1152		page->mapping = NULL;
1153		dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1154				  sg_virt(sg), sg_dma_address(sg));
1155	}
1156	sg_free_table(win->sgt);
1157}
1158
1159/**
1160 * msc_buffer_win_free() - free a window from MSC's window list
1161 * @msc:	MSC device
1162 * @win:	window to free
1163 *
1164 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1165 * to serialize, so the caller is expected to hold it.
1166 */
1167static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1168{
 
 
1169	msc->nr_pages -= win->nr_blocks;
1170
1171	list_del(&win->entry);
1172	if (list_empty(&msc->win_list)) {
1173		msc->base = NULL;
1174		msc->base_addr = 0;
1175	}
1176
1177	if (msc->mbuf && msc->mbuf->free_window)
1178		msc->mbuf->free_window(msc->mbuf_priv, win->sgt);
1179	else
1180		__msc_buffer_win_free(msc, win);
 
 
 
 
 
 
 
1181
1182	kfree(win);
1183}
1184
1185/**
1186 * msc_buffer_relink() - set up block descriptors for multiblock mode
1187 * @msc:	MSC device
1188 *
1189 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
1190 * so the caller is expected to hold it.
1191 */
1192static void msc_buffer_relink(struct msc *msc)
1193{
1194	struct msc_window *win, *next_win;
1195
1196	/* call with msc::mutex locked */
1197	list_for_each_entry(win, &msc->win_list, entry) {
1198		struct scatterlist *sg;
1199		unsigned int blk;
1200		u32 sw_tag = 0;
1201
1202		/*
1203		 * Last window's next_win should point to the first window
1204		 * and MSC_SW_TAG_LASTWIN should be set.
1205		 */
1206		if (msc_is_last_win(win)) {
1207			sw_tag |= MSC_SW_TAG_LASTWIN;
1208			next_win = list_first_entry(&msc->win_list,
1209						    struct msc_window, entry);
1210		} else {
1211			next_win = list_next_entry(win, entry);
 
1212		}
1213
1214		for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1215			struct msc_block_desc *bdesc = sg_virt(sg);
1216
1217			memset(bdesc, 0, sizeof(*bdesc));
1218
1219			bdesc->next_win = msc_win_base_pfn(next_win);
1220
1221			/*
1222			 * Similarly to last window, last block should point
1223			 * to the first one.
1224			 */
1225			if (blk == win->nr_segs - 1) {
1226				sw_tag |= MSC_SW_TAG_LASTBLK;
1227				bdesc->next_blk = msc_win_base_pfn(win);
 
1228			} else {
1229				dma_addr_t addr = sg_dma_address(sg_next(sg));
1230
1231				bdesc->next_blk = PFN_DOWN(addr);
1232			}
1233
1234			bdesc->sw_tag = sw_tag;
1235			bdesc->block_sz = sg->length / 64;
1236		}
1237	}
1238
1239	/*
1240	 * Make the above writes globally visible before tracing is
1241	 * enabled to make sure hardware sees them coherently.
1242	 */
1243	wmb();
1244}
1245
1246static void msc_buffer_multi_free(struct msc *msc)
1247{
1248	struct msc_window *win, *iter;
1249
1250	list_for_each_entry_safe(win, iter, &msc->win_list, entry)
1251		msc_buffer_win_free(msc, win);
1252}
1253
1254static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
1255				  unsigned int nr_wins)
1256{
1257	int ret, i;
1258
1259	for (i = 0; i < nr_wins; i++) {
1260		ret = msc_buffer_win_alloc(msc, nr_pages[i]);
1261		if (ret) {
1262			msc_buffer_multi_free(msc);
1263			return ret;
1264		}
1265	}
1266
1267	msc_buffer_relink(msc);
1268
1269	return 0;
1270}
1271
1272/**
1273 * msc_buffer_free() - free buffers for MSC
1274 * @msc:	MSC device
1275 *
1276 * Free MSC's storage buffers.
1277 *
1278 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
1279 * serialize, so the caller is expected to hold it.
1280 */
1281static void msc_buffer_free(struct msc *msc)
1282{
1283	msc_buffer_set_wb(msc);
1284
1285	if (msc->mode == MSC_MODE_SINGLE)
1286		msc_buffer_contig_free(msc);
1287	else if (msc->mode == MSC_MODE_MULTI)
1288		msc_buffer_multi_free(msc);
1289}
1290
1291/**
1292 * msc_buffer_alloc() - allocate a buffer for MSC
1293 * @msc:	MSC device
1294 * @size:	allocation size in bytes
1295 *
1296 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
1297 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
1298 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
1299 * window per invocation, so in multiblock mode this can be called multiple
1300 * times for the same MSC to allocate multiple windows.
1301 *
1302 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1303 * to serialize, so the caller is expected to hold it.
1304 *
1305 * Return:	0 on success, -errno otherwise.
1306 */
1307static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
1308			    unsigned int nr_wins)
1309{
1310	int ret;
1311
1312	/* -1: buffer not allocated */
1313	if (atomic_read(&msc->user_count) != -1)
1314		return -EBUSY;
1315
1316	if (msc->mode == MSC_MODE_SINGLE) {
1317		if (nr_wins != 1)
1318			return -EINVAL;
1319
1320		ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
1321	} else if (msc->mode == MSC_MODE_MULTI) {
1322		ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
1323	} else {
1324		ret = -EINVAL;
1325	}
1326
1327	if (!ret) {
1328		msc_buffer_set_uc(msc);
1329
1330		/* allocation should be visible before the counter goes to 0 */
1331		smp_mb__before_atomic();
1332
1333		if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
1334			return -EINVAL;
1335	}
1336
1337	return ret;
1338}
1339
1340/**
1341 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
1342 * @msc:	MSC device
1343 *
1344 * This will free MSC buffer unless it is in use or there is no allocated
1345 * buffer.
1346 * Caller needs to hold msc::buf_mutex.
1347 *
1348 * Return:	0 on successful deallocation or if there was no buffer to
1349 *		deallocate, -EBUSY if there are active users.
1350 */
1351static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1352{
1353	int count, ret = 0;
1354
1355	count = atomic_cmpxchg(&msc->user_count, 0, -1);
1356
1357	/* > 0: buffer is allocated and has users */
1358	if (count > 0)
1359		ret = -EBUSY;
1360	/* 0: buffer is allocated, no users */
1361	else if (!count)
1362		msc_buffer_free(msc);
1363	/* < 0: no buffer, nothing to do */
1364
1365	return ret;
1366}
1367
1368/**
1369 * msc_buffer_free_unless_used() - free a buffer unless it's in use
1370 * @msc:	MSC device
1371 *
1372 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1373 */
1374static int msc_buffer_free_unless_used(struct msc *msc)
1375{
1376	int ret;
1377
1378	mutex_lock(&msc->buf_mutex);
1379	ret = msc_buffer_unlocked_free_unless_used(msc);
1380	mutex_unlock(&msc->buf_mutex);
1381
1382	return ret;
1383}
1384
1385/**
1386 * msc_buffer_get_page() - get MSC buffer page at a given offset
1387 * @msc:	MSC device
1388 * @pgoff:	page offset into the storage buffer
1389 *
1390 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1391 * the caller.
1392 *
1393 * Return:	page if @pgoff corresponds to a valid buffer page or NULL.
1394 */
1395static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1396{
1397	struct msc_window *win;
1398	struct scatterlist *sg;
1399	unsigned int blk;
1400
1401	if (msc->mode == MSC_MODE_SINGLE)
1402		return msc_buffer_contig_get_page(msc, pgoff);
1403
1404	list_for_each_entry(win, &msc->win_list, entry)
1405		if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1406			goto found;
1407
1408	return NULL;
1409
1410found:
1411	pgoff -= win->pgoff;
1412
1413	for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1414		struct page *page = msc_sg_page(sg);
1415		size_t pgsz = PFN_DOWN(sg->length);
1416
1417		if (pgoff < pgsz)
1418			return page + pgoff;
1419
1420		pgoff -= pgsz;
1421	}
1422
1423	return NULL;
1424}
1425
1426/**
1427 * struct msc_win_to_user_struct - data for copy_to_user() callback
1428 * @buf:	userspace buffer to copy data to
1429 * @offset:	running offset
1430 */
1431struct msc_win_to_user_struct {
1432	char __user	*buf;
1433	unsigned long	offset;
1434};
1435
1436/**
1437 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1438 * @data:	callback's private data
1439 * @src:	source buffer
1440 * @len:	amount of data to copy from the source buffer
1441 */
1442static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1443{
1444	struct msc_win_to_user_struct *u = data;
1445	unsigned long ret;
1446
1447	ret = copy_to_user(u->buf + u->offset, src, len);
1448	u->offset += len - ret;
1449
1450	return ret;
1451}
1452
1453
1454/*
1455 * file operations' callbacks
1456 */
1457
1458static int intel_th_msc_open(struct inode *inode, struct file *file)
1459{
1460	struct intel_th_device *thdev = file->private_data;
1461	struct msc *msc = dev_get_drvdata(&thdev->dev);
1462	struct msc_iter *iter;
1463
1464	if (!capable(CAP_SYS_RAWIO))
1465		return -EPERM;
1466
1467	iter = msc_iter_install(msc);
1468	if (IS_ERR(iter))
1469		return PTR_ERR(iter);
1470
1471	file->private_data = iter;
1472
1473	return nonseekable_open(inode, file);
1474}
1475
1476static int intel_th_msc_release(struct inode *inode, struct file *file)
1477{
1478	struct msc_iter *iter = file->private_data;
1479	struct msc *msc = iter->msc;
1480
1481	msc_iter_remove(iter, msc);
1482
1483	return 0;
1484}
1485
1486static ssize_t
1487msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1488{
1489	unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1490	unsigned long start = off, tocopy = 0;
1491
1492	if (msc->single_wrap) {
1493		start += msc->single_sz;
1494		if (start < size) {
1495			tocopy = min(rem, size - start);
1496			if (copy_to_user(buf, msc->base + start, tocopy))
1497				return -EFAULT;
1498
1499			buf += tocopy;
1500			rem -= tocopy;
1501			start += tocopy;
1502		}
1503
1504		start &= size - 1;
1505		if (rem) {
1506			tocopy = min(rem, msc->single_sz - start);
1507			if (copy_to_user(buf, msc->base + start, tocopy))
1508				return -EFAULT;
1509
1510			rem -= tocopy;
1511		}
1512
1513		return len - rem;
1514	}
1515
1516	if (copy_to_user(buf, msc->base + start, rem))
1517		return -EFAULT;
1518
1519	return len;
1520}
1521
1522static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1523				 size_t len, loff_t *ppos)
1524{
1525	struct msc_iter *iter = file->private_data;
1526	struct msc *msc = iter->msc;
1527	size_t size;
1528	loff_t off = *ppos;
1529	ssize_t ret = 0;
1530
1531	if (!atomic_inc_unless_negative(&msc->user_count))
1532		return 0;
1533
1534	if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1535		size = msc->single_sz;
1536	else
1537		size = msc->nr_pages << PAGE_SHIFT;
1538
1539	if (!size)
1540		goto put_count;
1541
1542	if (off >= size)
1543		goto put_count;
1544
1545	if (off + len >= size)
1546		len = size - off;
1547
1548	if (msc->mode == MSC_MODE_SINGLE) {
1549		ret = msc_single_to_user(msc, buf, off, len);
1550		if (ret >= 0)
1551			*ppos += ret;
1552	} else if (msc->mode == MSC_MODE_MULTI) {
1553		struct msc_win_to_user_struct u = {
1554			.buf	= buf,
1555			.offset	= 0,
1556		};
1557
1558		ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1559		if (ret >= 0)
1560			*ppos = iter->offset;
1561	} else {
1562		ret = -EINVAL;
1563	}
1564
1565put_count:
1566	atomic_dec(&msc->user_count);
1567
1568	return ret;
1569}
1570
1571/*
1572 * vm operations callbacks (vm_ops)
1573 */
1574
1575static void msc_mmap_open(struct vm_area_struct *vma)
1576{
1577	struct msc_iter *iter = vma->vm_file->private_data;
1578	struct msc *msc = iter->msc;
1579
1580	atomic_inc(&msc->mmap_count);
1581}
1582
1583static void msc_mmap_close(struct vm_area_struct *vma)
1584{
1585	struct msc_iter *iter = vma->vm_file->private_data;
1586	struct msc *msc = iter->msc;
1587	unsigned long pg;
1588
1589	if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1590		return;
1591
1592	/* drop page _refcounts */
1593	for (pg = 0; pg < msc->nr_pages; pg++) {
1594		struct page *page = msc_buffer_get_page(msc, pg);
1595
1596		if (WARN_ON_ONCE(!page))
1597			continue;
1598
1599		if (page->mapping)
1600			page->mapping = NULL;
1601	}
1602
1603	/* last mapping -- drop user_count */
1604	atomic_dec(&msc->user_count);
1605	mutex_unlock(&msc->buf_mutex);
1606}
1607
1608static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
1609{
1610	struct msc_iter *iter = vmf->vma->vm_file->private_data;
1611	struct msc *msc = iter->msc;
1612
1613	vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1614	if (!vmf->page)
1615		return VM_FAULT_SIGBUS;
1616
1617	get_page(vmf->page);
1618	vmf->page->mapping = vmf->vma->vm_file->f_mapping;
1619	vmf->page->index = vmf->pgoff;
1620
1621	return 0;
1622}
1623
1624static const struct vm_operations_struct msc_mmap_ops = {
1625	.open	= msc_mmap_open,
1626	.close	= msc_mmap_close,
1627	.fault	= msc_mmap_fault,
1628};
1629
1630static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1631{
1632	unsigned long size = vma->vm_end - vma->vm_start;
1633	struct msc_iter *iter = vma->vm_file->private_data;
1634	struct msc *msc = iter->msc;
1635	int ret = -EINVAL;
1636
1637	if (!size || offset_in_page(size))
1638		return -EINVAL;
1639
1640	if (vma->vm_pgoff)
1641		return -EINVAL;
1642
1643	/* grab user_count once per mmap; drop in msc_mmap_close() */
1644	if (!atomic_inc_unless_negative(&msc->user_count))
1645		return -EINVAL;
1646
1647	if (msc->mode != MSC_MODE_SINGLE &&
1648	    msc->mode != MSC_MODE_MULTI)
1649		goto out;
1650
1651	if (size >> PAGE_SHIFT != msc->nr_pages)
1652		goto out;
1653
1654	atomic_set(&msc->mmap_count, 1);
1655	ret = 0;
1656
1657out:
1658	if (ret)
1659		atomic_dec(&msc->user_count);
1660
1661	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1662	vm_flags_set(vma, VM_DONTEXPAND | VM_DONTCOPY);
1663	vma->vm_ops = &msc_mmap_ops;
1664	return ret;
1665}
1666
1667static const struct file_operations intel_th_msc_fops = {
1668	.open		= intel_th_msc_open,
1669	.release	= intel_th_msc_release,
1670	.read		= intel_th_msc_read,
1671	.mmap		= intel_th_msc_mmap,
1672	.llseek		= no_llseek,
1673	.owner		= THIS_MODULE,
1674};
1675
1676static void intel_th_msc_wait_empty(struct intel_th_device *thdev)
1677{
1678	struct msc *msc = dev_get_drvdata(&thdev->dev);
1679	unsigned long count;
1680	u32 reg;
1681
1682	for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
1683	     count && !(reg & MSCSTS_PLE); count--) {
1684		reg = __raw_readl(msc->reg_base + REG_MSU_MSC0STS);
1685		cpu_relax();
1686	}
1687
1688	if (!count)
1689		dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
1690}
1691
1692static int intel_th_msc_init(struct msc *msc)
1693{
1694	atomic_set(&msc->user_count, -1);
1695
1696	msc->mode = msc->multi_is_broken ? MSC_MODE_SINGLE : MSC_MODE_MULTI;
1697	mutex_init(&msc->buf_mutex);
1698	INIT_LIST_HEAD(&msc->win_list);
1699	INIT_LIST_HEAD(&msc->iter_list);
1700
1701	msc->burst_len =
1702		(ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1703		__ffs(MSC_LEN);
1704
1705	return 0;
1706}
1707
1708static int msc_win_switch(struct msc *msc)
1709{
1710	struct msc_window *first;
1711
1712	if (list_empty(&msc->win_list))
1713		return -EINVAL;
1714
1715	first = list_first_entry(&msc->win_list, struct msc_window, entry);
1716
1717	if (msc_is_last_win(msc->cur_win))
1718		msc->cur_win = first;
1719	else
1720		msc->cur_win = list_next_entry(msc->cur_win, entry);
1721
1722	msc->base = msc_win_base(msc->cur_win);
1723	msc->base_addr = msc_win_base_dma(msc->cur_win);
1724
1725	intel_th_trace_switch(msc->thdev);
1726
1727	return 0;
1728}
1729
1730/**
1731 * intel_th_msc_window_unlock - put the window back in rotation
1732 * @dev:	MSC device to which this relates
1733 * @sgt:	buffer's sg_table for the window, does nothing if NULL
1734 */
1735void intel_th_msc_window_unlock(struct device *dev, struct sg_table *sgt)
1736{
1737	struct msc *msc = dev_get_drvdata(dev);
1738	struct msc_window *win;
1739
1740	if (!sgt)
1741		return;
1742
1743	win = msc_find_window(msc, sgt, false);
1744	if (!win)
1745		return;
1746
1747	msc_win_set_lockout(win, WIN_LOCKED, WIN_READY);
1748	if (msc->switch_on_unlock == win) {
1749		msc->switch_on_unlock = NULL;
1750		msc_win_switch(msc);
1751	}
1752}
1753EXPORT_SYMBOL_GPL(intel_th_msc_window_unlock);
1754
1755static void msc_work(struct work_struct *work)
1756{
1757	struct msc *msc = container_of(work, struct msc, work);
1758
1759	intel_th_msc_deactivate(msc->thdev);
1760}
1761
1762static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1763{
1764	struct msc *msc = dev_get_drvdata(&thdev->dev);
1765	u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1766	u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1767	struct msc_window *win, *next_win;
1768
1769	if (!msc->do_irq || !msc->mbuf)
1770		return IRQ_NONE;
1771
1772	msusts &= mask;
1773
1774	if (!msusts)
1775		return msc->enabled ? IRQ_HANDLED : IRQ_NONE;
1776
1777	iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
1778
1779	if (!msc->enabled)
1780		return IRQ_NONE;
1781
1782	/* grab the window before we do the switch */
1783	win = msc->cur_win;
1784	if (!win)
1785		return IRQ_HANDLED;
1786	next_win = msc_next_window(win);
1787	if (!next_win)
1788		return IRQ_HANDLED;
1789
1790	/* next window: if READY, proceed, if LOCKED, stop the trace */
1791	if (msc_win_set_lockout(next_win, WIN_READY, WIN_INUSE)) {
1792		if (msc->stop_on_full)
1793			schedule_work(&msc->work);
1794		else
1795			msc->switch_on_unlock = next_win;
1796
1797		return IRQ_HANDLED;
1798	}
1799
1800	/* current window: INUSE -> LOCKED */
1801	msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
1802
1803	msc_win_switch(msc);
1804
1805	if (msc->mbuf && msc->mbuf->ready)
1806		msc->mbuf->ready(msc->mbuf_priv, win->sgt,
1807				 msc_win_total_sz(win));
1808
1809	return IRQ_HANDLED;
1810}
1811
1812static const char * const msc_mode[] = {
1813	[MSC_MODE_SINGLE]	= "single",
1814	[MSC_MODE_MULTI]	= "multi",
1815	[MSC_MODE_EXI]		= "ExI",
1816	[MSC_MODE_DEBUG]	= "debug",
1817};
1818
1819static ssize_t
1820wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1821{
1822	struct msc *msc = dev_get_drvdata(dev);
1823
1824	return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1825}
1826
1827static ssize_t
1828wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1829	   size_t size)
1830{
1831	struct msc *msc = dev_get_drvdata(dev);
1832	unsigned long val;
1833	int ret;
1834
1835	ret = kstrtoul(buf, 10, &val);
1836	if (ret)
1837		return ret;
1838
1839	msc->wrap = !!val;
1840
1841	return size;
1842}
1843
1844static DEVICE_ATTR_RW(wrap);
1845
1846static void msc_buffer_unassign(struct msc *msc)
1847{
1848	lockdep_assert_held(&msc->buf_mutex);
1849
1850	if (!msc->mbuf)
1851		return;
1852
1853	msc->mbuf->unassign(msc->mbuf_priv);
1854	msu_buffer_put(msc->mbuf);
1855	msc->mbuf_priv = NULL;
1856	msc->mbuf = NULL;
1857}
1858
1859static ssize_t
1860mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1861{
1862	struct msc *msc = dev_get_drvdata(dev);
1863	const char *mode = msc_mode[msc->mode];
1864	ssize_t ret;
1865
1866	mutex_lock(&msc->buf_mutex);
1867	if (msc->mbuf)
1868		mode = msc->mbuf->name;
1869	ret = scnprintf(buf, PAGE_SIZE, "%s\n", mode);
1870	mutex_unlock(&msc->buf_mutex);
1871
1872	return ret;
1873}
1874
1875static ssize_t
1876mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1877	   size_t size)
1878{
1879	const struct msu_buffer *mbuf = NULL;
1880	struct msc *msc = dev_get_drvdata(dev);
1881	size_t len = size;
1882	char *cp, *mode;
1883	int i, ret;
1884
1885	if (!capable(CAP_SYS_RAWIO))
1886		return -EPERM;
1887
1888	cp = memchr(buf, '\n', len);
1889	if (cp)
1890		len = cp - buf;
1891
1892	mode = kstrndup(buf, len, GFP_KERNEL);
1893	if (!mode)
1894		return -ENOMEM;
1895
1896	i = match_string(msc_mode, ARRAY_SIZE(msc_mode), mode);
1897	if (i >= 0) {
1898		kfree(mode);
1899		goto found;
1900	}
1901
1902	/* Buffer sinks only work with a usable IRQ */
1903	if (!msc->do_irq) {
1904		kfree(mode);
1905		return -EINVAL;
1906	}
1907
1908	mbuf = msu_buffer_get(mode);
1909	kfree(mode);
1910	if (mbuf)
1911		goto found;
1912
1913	return -EINVAL;
1914
1915found:
1916	if (i == MSC_MODE_MULTI && msc->multi_is_broken)
1917		return -EOPNOTSUPP;
1918
1919	mutex_lock(&msc->buf_mutex);
1920	ret = 0;
1921
1922	/* Same buffer: do nothing */
1923	if (mbuf && mbuf == msc->mbuf) {
1924		/* put the extra reference we just got */
1925		msu_buffer_put(mbuf);
1926		goto unlock;
1927	}
1928
1929	ret = msc_buffer_unlocked_free_unless_used(msc);
1930	if (ret)
1931		goto unlock;
1932
1933	if (mbuf) {
1934		void *mbuf_priv = mbuf->assign(dev, &i);
1935
1936		if (!mbuf_priv) {
1937			ret = -ENOMEM;
1938			goto unlock;
1939		}
1940
1941		msc_buffer_unassign(msc);
1942		msc->mbuf_priv = mbuf_priv;
1943		msc->mbuf = mbuf;
1944	} else {
1945		msc_buffer_unassign(msc);
1946	}
1947
1948	msc->mode = i;
1949
1950unlock:
1951	if (ret && mbuf)
1952		msu_buffer_put(mbuf);
1953	mutex_unlock(&msc->buf_mutex);
1954
1955	return ret ? ret : size;
1956}
1957
1958static DEVICE_ATTR_RW(mode);
1959
1960static ssize_t
1961nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1962{
1963	struct msc *msc = dev_get_drvdata(dev);
1964	struct msc_window *win;
1965	size_t count = 0;
1966
1967	mutex_lock(&msc->buf_mutex);
1968
1969	if (msc->mode == MSC_MODE_SINGLE)
1970		count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1971	else if (msc->mode == MSC_MODE_MULTI) {
1972		list_for_each_entry(win, &msc->win_list, entry) {
1973			count += scnprintf(buf + count, PAGE_SIZE - count,
1974					   "%d%c", win->nr_blocks,
1975					   msc_is_last_win(win) ? '\n' : ',');
1976		}
1977	} else {
1978		count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1979	}
1980
1981	mutex_unlock(&msc->buf_mutex);
1982
1983	return count;
1984}
1985
1986static ssize_t
1987nr_pages_store(struct device *dev, struct device_attribute *attr,
1988	       const char *buf, size_t size)
1989{
1990	struct msc *msc = dev_get_drvdata(dev);
1991	unsigned long val, *win = NULL, *rewin;
1992	size_t len = size;
1993	const char *p = buf;
1994	char *end, *s;
1995	int ret, nr_wins = 0;
1996
1997	if (!capable(CAP_SYS_RAWIO))
1998		return -EPERM;
1999
2000	ret = msc_buffer_free_unless_used(msc);
2001	if (ret)
2002		return ret;
2003
2004	/* scan the comma-separated list of allocation sizes */
2005	end = memchr(buf, '\n', len);
2006	if (end)
2007		len = end - buf;
2008
2009	do {
2010		end = memchr(p, ',', len);
2011		s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
2012		if (!s) {
2013			ret = -ENOMEM;
2014			goto free_win;
2015		}
2016
2017		ret = kstrtoul(s, 10, &val);
2018		kfree(s);
2019
2020		if (ret || !val)
2021			goto free_win;
2022
2023		if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
2024			ret = -EINVAL;
2025			goto free_win;
2026		}
2027
2028		nr_wins++;
2029		rewin = krealloc_array(win, nr_wins, sizeof(*win), GFP_KERNEL);
2030		if (!rewin) {
2031			kfree(win);
2032			return -ENOMEM;
2033		}
2034
2035		win = rewin;
2036		win[nr_wins - 1] = val;
2037
2038		if (!end)
2039			break;
2040
2041		/* consume the number and the following comma, hence +1 */
2042		len -= end - p + 1;
2043		p = end + 1;
2044	} while (len);
2045
2046	mutex_lock(&msc->buf_mutex);
2047	ret = msc_buffer_alloc(msc, win, nr_wins);
2048	mutex_unlock(&msc->buf_mutex);
2049
2050free_win:
2051	kfree(win);
2052
2053	return ret ? ret : size;
2054}
2055
2056static DEVICE_ATTR_RW(nr_pages);
2057
2058static ssize_t
2059win_switch_store(struct device *dev, struct device_attribute *attr,
2060		 const char *buf, size_t size)
2061{
2062	struct msc *msc = dev_get_drvdata(dev);
2063	unsigned long val;
2064	int ret;
2065
2066	ret = kstrtoul(buf, 10, &val);
2067	if (ret)
2068		return ret;
2069
2070	if (val != 1)
2071		return -EINVAL;
2072
2073	ret = -EINVAL;
2074	mutex_lock(&msc->buf_mutex);
2075	/*
2076	 * Window switch can only happen in the "multi" mode.
2077	 * If a external buffer is engaged, they have the full
2078	 * control over window switching.
2079	 */
2080	if (msc->mode == MSC_MODE_MULTI && !msc->mbuf)
2081		ret = msc_win_switch(msc);
2082	mutex_unlock(&msc->buf_mutex);
2083
2084	return ret ? ret : size;
2085}
2086
2087static DEVICE_ATTR_WO(win_switch);
2088
2089static ssize_t stop_on_full_show(struct device *dev,
2090				 struct device_attribute *attr, char *buf)
2091{
2092	struct msc *msc = dev_get_drvdata(dev);
2093
2094	return sprintf(buf, "%d\n", msc->stop_on_full);
2095}
2096
2097static ssize_t stop_on_full_store(struct device *dev,
2098				  struct device_attribute *attr,
2099				  const char *buf, size_t size)
2100{
2101	struct msc *msc = dev_get_drvdata(dev);
2102	int ret;
2103
2104	ret = kstrtobool(buf, &msc->stop_on_full);
2105	if (ret)
2106		return ret;
2107
2108	return size;
2109}
2110
2111static DEVICE_ATTR_RW(stop_on_full);
2112
2113static struct attribute *msc_output_attrs[] = {
2114	&dev_attr_wrap.attr,
2115	&dev_attr_mode.attr,
2116	&dev_attr_nr_pages.attr,
2117	&dev_attr_win_switch.attr,
2118	&dev_attr_stop_on_full.attr,
2119	NULL,
2120};
2121
2122static const struct attribute_group msc_output_group = {
2123	.attrs	= msc_output_attrs,
2124};
2125
2126static int intel_th_msc_probe(struct intel_th_device *thdev)
2127{
2128	struct device *dev = &thdev->dev;
2129	struct resource *res;
2130	struct msc *msc;
2131	void __iomem *base;
2132	int err;
2133
2134	res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
2135	if (!res)
2136		return -ENODEV;
2137
2138	base = devm_ioremap(dev, res->start, resource_size(res));
2139	if (!base)
2140		return -ENOMEM;
2141
2142	msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
2143	if (!msc)
2144		return -ENOMEM;
2145
2146	res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
2147	if (!res)
2148		msc->do_irq = 1;
2149
2150	if (INTEL_TH_CAP(to_intel_th(thdev), multi_is_broken))
2151		msc->multi_is_broken = 1;
2152
2153	msc->index = thdev->id;
2154
2155	msc->thdev = thdev;
2156	msc->reg_base = base + msc->index * 0x100;
2157	msc->msu_base = base;
2158
2159	INIT_WORK(&msc->work, msc_work);
2160	err = intel_th_msc_init(msc);
2161	if (err)
2162		return err;
2163
2164	dev_set_drvdata(dev, msc);
2165
2166	return 0;
2167}
2168
2169static void intel_th_msc_remove(struct intel_th_device *thdev)
2170{
2171	struct msc *msc = dev_get_drvdata(&thdev->dev);
2172	int ret;
2173
2174	intel_th_msc_deactivate(thdev);
2175
2176	/*
2177	 * Buffers should not be used at this point except if the
2178	 * output character device is still open and the parent
2179	 * device gets detached from its bus, which is a FIXME.
2180	 */
2181	ret = msc_buffer_free_unless_used(msc);
2182	WARN_ON_ONCE(ret);
2183}
2184
2185static struct intel_th_driver intel_th_msc_driver = {
2186	.probe	= intel_th_msc_probe,
2187	.remove	= intel_th_msc_remove,
2188	.irq		= intel_th_msc_interrupt,
2189	.wait_empty	= intel_th_msc_wait_empty,
2190	.activate	= intel_th_msc_activate,
2191	.deactivate	= intel_th_msc_deactivate,
2192	.fops	= &intel_th_msc_fops,
2193	.attr_group	= &msc_output_group,
2194	.driver	= {
2195		.name	= "msc",
2196		.owner	= THIS_MODULE,
2197	},
2198};
2199
2200module_driver(intel_th_msc_driver,
2201	      intel_th_driver_register,
2202	      intel_th_driver_unregister);
2203
2204MODULE_LICENSE("GPL v2");
2205MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
2206MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");
v4.10.11
 
   1/*
   2 * Intel(R) Trace Hub Memory Storage Unit
   3 *
   4 * Copyright (C) 2014-2015 Intel Corporation.
   5 *
   6 * This program is free software; you can redistribute it and/or modify it
   7 * under the terms and conditions of the GNU General Public License,
   8 * version 2, as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope it will be useful, but WITHOUT
  11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  13 * more details.
  14 */
  15
  16#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
  17
  18#include <linux/types.h>
  19#include <linux/module.h>
  20#include <linux/device.h>
  21#include <linux/uaccess.h>
  22#include <linux/sizes.h>
  23#include <linux/printk.h>
  24#include <linux/slab.h>
  25#include <linux/mm.h>
  26#include <linux/fs.h>
  27#include <linux/io.h>
 
  28#include <linux/dma-mapping.h>
  29
  30#include <asm/cacheflush.h>
 
 
  31
 
  32#include "intel_th.h"
  33#include "msu.h"
  34
  35#define msc_dev(x) (&(x)->thdev->dev)
  36
  37/**
  38 * struct msc_block - multiblock mode block descriptor
  39 * @bdesc:	pointer to hardware descriptor (beginning of the block)
  40 * @addr:	physical address of the block
  41 */
  42struct msc_block {
  43	struct msc_block_desc	*bdesc;
  44	dma_addr_t		addr;
 
 
 
 
 
 
 
 
 
 
 
 
 
  45};
  46
  47/**
  48 * struct msc_window - multiblock mode window descriptor
  49 * @entry:	window list linkage (msc::win_list)
  50 * @pgoff:	page offset into the buffer that this window starts at
 
 
  51 * @nr_blocks:	number of blocks (pages) in this window
  52 * @block:	array of block descriptors
 
 
  53 */
  54struct msc_window {
  55	struct list_head	entry;
  56	unsigned long		pgoff;
 
 
  57	unsigned int		nr_blocks;
 
  58	struct msc		*msc;
  59	struct msc_block	block[0];
 
  60};
  61
  62/**
  63 * struct msc_iter - iterator for msc buffer
  64 * @entry:		msc::iter_list linkage
  65 * @msc:		pointer to the MSC device
  66 * @start_win:		oldest window
  67 * @win:		current window
  68 * @offset:		current logical offset into the buffer
  69 * @start_block:	oldest block in the window
  70 * @block:		block number in the window
  71 * @block_off:		offset into current block
  72 * @wrap_count:		block wrapping handling
  73 * @eof:		end of buffer reached
  74 */
  75struct msc_iter {
  76	struct list_head	entry;
  77	struct msc		*msc;
  78	struct msc_window	*start_win;
  79	struct msc_window	*win;
  80	unsigned long		offset;
  81	int			start_block;
  82	int			block;
  83	unsigned int		block_off;
  84	unsigned int		wrap_count;
  85	unsigned int		eof;
  86};
  87
  88/**
  89 * struct msc - MSC device representation
  90 * @reg_base:		register window base address
  91 * @thdev:		intel_th_device pointer
 
 
  92 * @win_list:		list of windows in multiblock mode
 
 
  93 * @nr_pages:		total number of pages allocated for this buffer
  94 * @single_sz:		amount of data in single mode
  95 * @single_wrap:	single mode wrap occurred
  96 * @base:		buffer's base pointer
  97 * @base_addr:		buffer's base address
  98 * @user_count:		number of users of the buffer
  99 * @mmap_count:		number of mappings
 100 * @buf_mutex:		mutex to serialize access to buffer-related bits
 101
 102 * @enabled:		MSC is enabled
 103 * @wrap:		wrapping is enabled
 104 * @mode:		MSC operating mode
 105 * @burst_len:		write burst length
 106 * @index:		number of this MSC in the MSU
 107 */
 108struct msc {
 109	void __iomem		*reg_base;
 
 110	struct intel_th_device	*thdev;
 111
 
 
 
 
 112	struct list_head	win_list;
 
 
 
 113	unsigned long		nr_pages;
 114	unsigned long		single_sz;
 115	unsigned int		single_wrap : 1;
 116	void			*base;
 117	dma_addr_t		base_addr;
 
 
 118
 119	/* <0: no buffer, 0: no users, >0: active users */
 120	atomic_t		user_count;
 121
 122	atomic_t		mmap_count;
 123	struct mutex		buf_mutex;
 124
 125	struct list_head	iter_list;
 126
 
 
 127	/* config */
 128	unsigned int		enabled : 1,
 129				wrap	: 1;
 
 
 130	unsigned int		mode;
 131	unsigned int		burst_len;
 132	unsigned int		index;
 133};
 134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 135static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
 136{
 137	/* header hasn't been written */
 138	if (!bdesc->valid_dw)
 139		return true;
 140
 141	/* valid_dw includes the header */
 142	if (!msc_data_sz(bdesc))
 143		return true;
 144
 145	return false;
 146}
 147
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 148/**
 149 * msc_oldest_window() - locate the window with oldest data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 150 * @msc:	MSC device
 
 
 151 *
 152 * This should only be used in multiblock mode. Caller should hold the
 153 * msc::user_count reference.
 154 *
 155 * Return:	the oldest window with valid data
 156 */
 157static struct msc_window *msc_oldest_window(struct msc *msc)
 
 158{
 159	struct msc_window *win;
 160	u32 reg = ioread32(msc->reg_base + REG_MSU_MSC0NWSA);
 161	unsigned long win_addr = (unsigned long)reg << PAGE_SHIFT;
 162	unsigned int found = 0;
 163
 164	if (list_empty(&msc->win_list))
 165		return NULL;
 166
 167	/*
 168	 * we might need a radix tree for this, depending on how
 169	 * many windows a typical user would allocate; ideally it's
 170	 * something like 2, in which case we're good
 171	 */
 172	list_for_each_entry(win, &msc->win_list, entry) {
 173		if (win->block[0].addr == win_addr)
 174			found++;
 175
 176		/* skip the empty ones */
 177		if (msc_block_is_empty(win->block[0].bdesc))
 178			continue;
 179
 180		if (found)
 181			return win;
 182	}
 183
 184	return list_entry(msc->win_list.next, struct msc_window, entry);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 185}
 186
 187/**
 188 * msc_win_oldest_block() - locate the oldest block in a given window
 189 * @win:	window to look at
 190 *
 191 * Return:	index of the block with the oldest data
 192 */
 193static unsigned int msc_win_oldest_block(struct msc_window *win)
 194{
 195	unsigned int blk;
 196	struct msc_block_desc *bdesc = win->block[0].bdesc;
 
 197
 198	/* without wrapping, first block is the oldest */
 199	if (!msc_block_wrapped(bdesc))
 200		return 0;
 201
 202	/*
 203	 * with wrapping, last written block contains both the newest and the
 204	 * oldest data for this window.
 205	 */
 206	for (blk = 0; blk < win->nr_blocks; blk++) {
 207		bdesc = win->block[blk].bdesc;
 208
 209		if (msc_block_last_written(bdesc))
 210			return blk;
 211	}
 212
 213	return 0;
 214}
 215
 216/**
 217 * msc_is_last_win() - check if a window is the last one for a given MSC
 218 * @win:	window
 219 * Return:	true if @win is the last window in MSC's multiblock buffer
 220 */
 221static inline bool msc_is_last_win(struct msc_window *win)
 222{
 223	return win->entry.next == &win->msc->win_list;
 224}
 225
 226/**
 227 * msc_next_window() - return next window in the multiblock buffer
 228 * @win:	current window
 229 *
 230 * Return:	window following the current one
 231 */
 232static struct msc_window *msc_next_window(struct msc_window *win)
 233{
 234	if (msc_is_last_win(win))
 235		return list_entry(win->msc->win_list.next, struct msc_window,
 236				  entry);
 237
 238	return list_entry(win->entry.next, struct msc_window, entry);
 239}
 240
 241static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
 242{
 243	return iter->win->block[iter->block].bdesc;
 244}
 245
 246static void msc_iter_init(struct msc_iter *iter)
 247{
 248	memset(iter, 0, sizeof(*iter));
 249	iter->start_block = -1;
 250	iter->block = -1;
 251}
 252
 253static struct msc_iter *msc_iter_install(struct msc *msc)
 254{
 255	struct msc_iter *iter;
 256
 257	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
 258	if (!iter)
 259		return ERR_PTR(-ENOMEM);
 260
 261	mutex_lock(&msc->buf_mutex);
 262
 263	/*
 264	 * Reading and tracing are mutually exclusive; if msc is
 265	 * enabled, open() will fail; otherwise existing readers
 266	 * will prevent enabling the msc and the rest of fops don't
 267	 * need to worry about it.
 268	 */
 269	if (msc->enabled) {
 270		kfree(iter);
 271		iter = ERR_PTR(-EBUSY);
 272		goto unlock;
 273	}
 274
 275	msc_iter_init(iter);
 276	iter->msc = msc;
 277
 278	list_add_tail(&iter->entry, &msc->iter_list);
 279unlock:
 280	mutex_unlock(&msc->buf_mutex);
 281
 282	return iter;
 283}
 284
 285static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
 286{
 287	mutex_lock(&msc->buf_mutex);
 288	list_del(&iter->entry);
 289	mutex_unlock(&msc->buf_mutex);
 290
 291	kfree(iter);
 292}
 293
 294static void msc_iter_block_start(struct msc_iter *iter)
 295{
 296	if (iter->start_block != -1)
 297		return;
 298
 299	iter->start_block = msc_win_oldest_block(iter->win);
 300	iter->block = iter->start_block;
 301	iter->wrap_count = 0;
 302
 303	/*
 304	 * start with the block with oldest data; if data has wrapped
 305	 * in this window, it should be in this block
 306	 */
 307	if (msc_block_wrapped(msc_iter_bdesc(iter)))
 308		iter->wrap_count = 2;
 309
 310}
 311
 312static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
 313{
 314	/* already started, nothing to do */
 315	if (iter->start_win)
 316		return 0;
 317
 318	iter->start_win = msc_oldest_window(msc);
 319	if (!iter->start_win)
 320		return -EINVAL;
 321
 322	iter->win = iter->start_win;
 323	iter->start_block = -1;
 324
 325	msc_iter_block_start(iter);
 326
 327	return 0;
 328}
 329
 330static int msc_iter_win_advance(struct msc_iter *iter)
 331{
 332	iter->win = msc_next_window(iter->win);
 333	iter->start_block = -1;
 334
 335	if (iter->win == iter->start_win) {
 336		iter->eof++;
 337		return 1;
 338	}
 339
 340	msc_iter_block_start(iter);
 341
 342	return 0;
 343}
 344
 345static int msc_iter_block_advance(struct msc_iter *iter)
 346{
 347	iter->block_off = 0;
 348
 349	/* wrapping */
 350	if (iter->wrap_count && iter->block == iter->start_block) {
 351		iter->wrap_count--;
 352		if (!iter->wrap_count)
 353			/* copied newest data from the wrapped block */
 354			return msc_iter_win_advance(iter);
 355	}
 356
 357	/* no wrapping, check for last written block */
 358	if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
 359		/* copied newest data for the window */
 360		return msc_iter_win_advance(iter);
 361
 362	/* block advance */
 363	if (++iter->block == iter->win->nr_blocks)
 364		iter->block = 0;
 
 
 365
 366	/* no wrapping, sanity check in case there is no last written block */
 367	if (!iter->wrap_count && iter->block == iter->start_block)
 368		return msc_iter_win_advance(iter);
 369
 370	return 0;
 371}
 372
 373/**
 374 * msc_buffer_iterate() - go through multiblock buffer's data
 375 * @iter:	iterator structure
 376 * @size:	amount of data to scan
 377 * @data:	callback's private data
 378 * @fn:		iterator callback
 379 *
 380 * This will start at the window which will be written to next (containing
 381 * the oldest data) and work its way to the current window, calling @fn
 382 * for each chunk of data as it goes.
 383 *
 384 * Caller should have msc::user_count reference to make sure the buffer
 385 * doesn't disappear from under us.
 386 *
 387 * Return:	amount of data actually scanned.
 388 */
 389static ssize_t
 390msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
 391		   unsigned long (*fn)(void *, void *, size_t))
 392{
 393	struct msc *msc = iter->msc;
 394	size_t len = size;
 395	unsigned int advance;
 396
 397	if (iter->eof)
 398		return 0;
 399
 400	/* start with the oldest window */
 401	if (msc_iter_win_start(iter, msc))
 402		return 0;
 403
 404	do {
 405		unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
 406		void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
 407		size_t tocopy = data_bytes, copied = 0;
 408		size_t remaining = 0;
 409
 410		advance = 1;
 411
 412		/*
 413		 * If block wrapping happened, we need to visit the last block
 414		 * twice, because it contains both the oldest and the newest
 415		 * data in this window.
 416		 *
 417		 * First time (wrap_count==2), in the very beginning, to collect
 418		 * the oldest data, which is in the range
 419		 * (data_bytes..DATA_IN_PAGE).
 420		 *
 421		 * Second time (wrap_count==1), it's just like any other block,
 422		 * containing data in the range of [MSC_BDESC..data_bytes].
 423		 */
 424		if (iter->block == iter->start_block && iter->wrap_count == 2) {
 425			tocopy = DATA_IN_PAGE - data_bytes;
 426			src += data_bytes;
 427		}
 428
 429		if (!tocopy)
 430			goto next_block;
 431
 432		tocopy -= iter->block_off;
 433		src += iter->block_off;
 434
 435		if (len < tocopy) {
 436			tocopy = len;
 437			advance = 0;
 438		}
 439
 440		remaining = fn(data, src, tocopy);
 441
 442		if (remaining)
 443			advance = 0;
 444
 445		copied = tocopy - remaining;
 446		len -= copied;
 447		iter->block_off += copied;
 448		iter->offset += copied;
 449
 450		if (!advance)
 451			break;
 452
 453next_block:
 454		if (msc_iter_block_advance(iter))
 455			break;
 456
 457	} while (len);
 458
 459	return size - len;
 460}
 461
 462/**
 463 * msc_buffer_clear_hw_header() - clear hw header for multiblock
 464 * @msc:	MSC device
 465 */
 466static void msc_buffer_clear_hw_header(struct msc *msc)
 467{
 468	struct msc_window *win;
 
 469
 470	list_for_each_entry(win, &msc->win_list, entry) {
 471		unsigned int blk;
 472		size_t hw_sz = sizeof(struct msc_block_desc) -
 473			offsetof(struct msc_block_desc, hw_tag);
 474
 475		for (blk = 0; blk < win->nr_blocks; blk++) {
 476			struct msc_block_desc *bdesc = win->block[blk].bdesc;
 477
 478			memset(&bdesc->hw_tag, 0, hw_sz);
 479		}
 480	}
 481}
 482
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 483/**
 484 * msc_configure() - set up MSC hardware
 485 * @msc:	the MSC device to configure
 486 *
 487 * Program storage mode, wrapping, burst length and trace buffer address
 488 * into a given MSC. Then, enable tracing and set msc::enabled.
 489 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
 490 */
 491static int msc_configure(struct msc *msc)
 492{
 493	u32 reg;
 494
 495	lockdep_assert_held(&msc->buf_mutex);
 496
 497	if (msc->mode > MSC_MODE_MULTI)
 498		return -ENOTSUPP;
 
 
 
 
 499
 500	if (msc->mode == MSC_MODE_MULTI)
 501		msc_buffer_clear_hw_header(msc);
 
 
 
 
 502
 503	reg = msc->base_addr >> PAGE_SHIFT;
 504	iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
 505
 506	if (msc->mode == MSC_MODE_SINGLE) {
 507		reg = msc->nr_pages;
 508		iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
 509	}
 510
 511	reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
 512	reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
 513
 514	reg |= MSC_EN;
 515	reg |= msc->mode << __ffs(MSC_MODE);
 516	reg |= msc->burst_len << __ffs(MSC_LEN);
 517
 518	if (msc->wrap)
 519		reg |= MSC_WRAPEN;
 520
 521	iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
 522
 
 
 523	msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
 524	intel_th_trace_enable(msc->thdev);
 525	msc->enabled = 1;
 526
 
 
 527
 528	return 0;
 529}
 530
 531/**
 532 * msc_disable() - disable MSC hardware
 533 * @msc:	MSC device to disable
 534 *
 535 * If @msc is enabled, disable tracing on the switch and then disable MSC
 536 * storage. Caller must hold msc::buf_mutex.
 537 */
 538static void msc_disable(struct msc *msc)
 539{
 540	unsigned long count;
 541	u32 reg;
 542
 543	lockdep_assert_held(&msc->buf_mutex);
 544
 
 
 
 
 
 
 545	intel_th_trace_disable(msc->thdev);
 546
 547	for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
 548	     count && !(reg & MSCSTS_PLE); count--) {
 549		reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
 550		cpu_relax();
 551	}
 552
 553	if (!count)
 554		dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
 555
 556	if (msc->mode == MSC_MODE_SINGLE) {
 557		msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
 558
 559		reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
 560		msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
 561		dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
 562			reg, msc->single_sz, msc->single_wrap);
 563	}
 564
 565	reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
 566	reg &= ~MSC_EN;
 567	iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
 
 
 
 
 
 568	msc->enabled = 0;
 569
 570	iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
 571	iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
 572
 573	dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
 574		ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
 575
 576	reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
 577	dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
 
 
 
 
 578}
 579
 580static int intel_th_msc_activate(struct intel_th_device *thdev)
 581{
 582	struct msc *msc = dev_get_drvdata(&thdev->dev);
 583	int ret = -EBUSY;
 584
 585	if (!atomic_inc_unless_negative(&msc->user_count))
 586		return -ENODEV;
 587
 588	mutex_lock(&msc->buf_mutex);
 589
 590	/* if there are readers, refuse */
 591	if (list_empty(&msc->iter_list))
 592		ret = msc_configure(msc);
 593
 594	mutex_unlock(&msc->buf_mutex);
 595
 596	if (ret)
 597		atomic_dec(&msc->user_count);
 598
 599	return ret;
 600}
 601
 602static void intel_th_msc_deactivate(struct intel_th_device *thdev)
 603{
 604	struct msc *msc = dev_get_drvdata(&thdev->dev);
 605
 606	mutex_lock(&msc->buf_mutex);
 607	if (msc->enabled) {
 608		msc_disable(msc);
 609		atomic_dec(&msc->user_count);
 610	}
 611	mutex_unlock(&msc->buf_mutex);
 612}
 613
 614/**
 615 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
 616 * @msc:	MSC device
 617 * @size:	allocation size in bytes
 618 *
 619 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
 620 * caller is expected to hold it.
 621 *
 622 * Return:	0 on success, -errno otherwise.
 623 */
 624static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
 625{
 
 626	unsigned int order = get_order(size);
 627	struct page *page;
 
 628
 629	if (!size)
 630		return 0;
 631
 632	page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
 
 
 
 
 
 633	if (!page)
 634		return -ENOMEM;
 635
 636	split_page(page, order);
 637	msc->nr_pages = size >> PAGE_SHIFT;
 
 
 
 
 
 
 
 638	msc->base = page_address(page);
 639	msc->base_addr = page_to_phys(page);
 640
 641	return 0;
 
 
 
 
 
 
 
 
 
 642}
 643
 644/**
 645 * msc_buffer_contig_free() - free a contiguous buffer
 646 * @msc:	MSC configured in SINGLE mode
 647 */
 648static void msc_buffer_contig_free(struct msc *msc)
 649{
 650	unsigned long off;
 651
 
 
 
 
 652	for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
 653		struct page *page = virt_to_page(msc->base + off);
 654
 655		page->mapping = NULL;
 656		__free_page(page);
 657	}
 658
 659	msc->nr_pages = 0;
 660}
 661
 662/**
 663 * msc_buffer_contig_get_page() - find a page at a given offset
 664 * @msc:	MSC configured in SINGLE mode
 665 * @pgoff:	page offset
 666 *
 667 * Return:	page, if @pgoff is within the range, NULL otherwise.
 668 */
 669static struct page *msc_buffer_contig_get_page(struct msc *msc,
 670					       unsigned long pgoff)
 671{
 672	if (pgoff >= msc->nr_pages)
 673		return NULL;
 674
 675	return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
 676}
 677
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 678/**
 679 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
 680 * @msc:	MSC device
 681 * @nr_blocks:	number of pages in this window
 682 *
 683 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
 684 * to serialize, so the caller is expected to hold it.
 685 *
 686 * Return:	0 on success, -errno otherwise.
 687 */
 688static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
 689{
 690	struct msc_window *win;
 691	unsigned long size = PAGE_SIZE;
 692	int i, ret = -ENOMEM;
 693
 694	if (!nr_blocks)
 695		return 0;
 696
 697	win = kzalloc(offsetof(struct msc_window, block[nr_blocks]),
 698		      GFP_KERNEL);
 699	if (!win)
 700		return -ENOMEM;
 701
 
 
 
 
 
 702	if (!list_empty(&msc->win_list)) {
 703		struct msc_window *prev = list_entry(msc->win_list.prev,
 704						     struct msc_window, entry);
 
 705
 706		win->pgoff = prev->pgoff + prev->nr_blocks;
 707	}
 708
 709	for (i = 0; i < nr_blocks; i++) {
 710		win->block[i].bdesc = dma_alloc_coherent(msc_dev(msc), size,
 711							 &win->block[i].addr,
 712							 GFP_KERNEL);
 
 713
 714#ifdef CONFIG_X86
 715		/* Set the page as uncached */
 716		set_memory_uc((unsigned long)win->block[i].bdesc, 1);
 717#endif
 718
 719		if (!win->block[i].bdesc)
 720			goto err_nomem;
 721	}
 722
 723	win->msc = msc;
 724	win->nr_blocks = nr_blocks;
 725
 726	if (list_empty(&msc->win_list)) {
 727		msc->base = win->block[0].bdesc;
 728		msc->base_addr = win->block[0].addr;
 
 729	}
 730
 731	list_add_tail(&win->entry, &msc->win_list);
 732	msc->nr_pages += nr_blocks;
 733
 734	return 0;
 735
 736err_nomem:
 737	for (i--; i >= 0; i--) {
 738#ifdef CONFIG_X86
 739		/* Reset the page to write-back before releasing */
 740		set_memory_wb((unsigned long)win->block[i].bdesc, 1);
 741#endif
 742		dma_free_coherent(msc_dev(msc), size, win->block[i].bdesc,
 743				  win->block[i].addr);
 744	}
 745	kfree(win);
 746
 747	return ret;
 748}
 749
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 750/**
 751 * msc_buffer_win_free() - free a window from MSC's window list
 752 * @msc:	MSC device
 753 * @win:	window to free
 754 *
 755 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
 756 * to serialize, so the caller is expected to hold it.
 757 */
 758static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
 759{
 760	int i;
 761
 762	msc->nr_pages -= win->nr_blocks;
 763
 764	list_del(&win->entry);
 765	if (list_empty(&msc->win_list)) {
 766		msc->base = NULL;
 767		msc->base_addr = 0;
 768	}
 769
 770	for (i = 0; i < win->nr_blocks; i++) {
 771		struct page *page = virt_to_page(win->block[i].bdesc);
 772
 773		page->mapping = NULL;
 774#ifdef CONFIG_X86
 775		/* Reset the page to write-back before releasing */
 776		set_memory_wb((unsigned long)win->block[i].bdesc, 1);
 777#endif
 778		dma_free_coherent(msc_dev(win->msc), PAGE_SIZE,
 779				  win->block[i].bdesc, win->block[i].addr);
 780	}
 781
 782	kfree(win);
 783}
 784
 785/**
 786 * msc_buffer_relink() - set up block descriptors for multiblock mode
 787 * @msc:	MSC device
 788 *
 789 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
 790 * so the caller is expected to hold it.
 791 */
 792static void msc_buffer_relink(struct msc *msc)
 793{
 794	struct msc_window *win, *next_win;
 795
 796	/* call with msc::mutex locked */
 797	list_for_each_entry(win, &msc->win_list, entry) {
 
 798		unsigned int blk;
 799		u32 sw_tag = 0;
 800
 801		/*
 802		 * Last window's next_win should point to the first window
 803		 * and MSC_SW_TAG_LASTWIN should be set.
 804		 */
 805		if (msc_is_last_win(win)) {
 806			sw_tag |= MSC_SW_TAG_LASTWIN;
 807			next_win = list_entry(msc->win_list.next,
 808					      struct msc_window, entry);
 809		} else {
 810			next_win = list_entry(win->entry.next,
 811					      struct msc_window, entry);
 812		}
 813
 814		for (blk = 0; blk < win->nr_blocks; blk++) {
 815			struct msc_block_desc *bdesc = win->block[blk].bdesc;
 816
 817			memset(bdesc, 0, sizeof(*bdesc));
 818
 819			bdesc->next_win = next_win->block[0].addr >> PAGE_SHIFT;
 820
 821			/*
 822			 * Similarly to last window, last block should point
 823			 * to the first one.
 824			 */
 825			if (blk == win->nr_blocks - 1) {
 826				sw_tag |= MSC_SW_TAG_LASTBLK;
 827				bdesc->next_blk =
 828					win->block[0].addr >> PAGE_SHIFT;
 829			} else {
 830				bdesc->next_blk =
 831					win->block[blk + 1].addr >> PAGE_SHIFT;
 
 832			}
 833
 834			bdesc->sw_tag = sw_tag;
 835			bdesc->block_sz = PAGE_SIZE / 64;
 836		}
 837	}
 838
 839	/*
 840	 * Make the above writes globally visible before tracing is
 841	 * enabled to make sure hardware sees them coherently.
 842	 */
 843	wmb();
 844}
 845
 846static void msc_buffer_multi_free(struct msc *msc)
 847{
 848	struct msc_window *win, *iter;
 849
 850	list_for_each_entry_safe(win, iter, &msc->win_list, entry)
 851		msc_buffer_win_free(msc, win);
 852}
 853
 854static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
 855				  unsigned int nr_wins)
 856{
 857	int ret, i;
 858
 859	for (i = 0; i < nr_wins; i++) {
 860		ret = msc_buffer_win_alloc(msc, nr_pages[i]);
 861		if (ret) {
 862			msc_buffer_multi_free(msc);
 863			return ret;
 864		}
 865	}
 866
 867	msc_buffer_relink(msc);
 868
 869	return 0;
 870}
 871
 872/**
 873 * msc_buffer_free() - free buffers for MSC
 874 * @msc:	MSC device
 875 *
 876 * Free MSC's storage buffers.
 877 *
 878 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
 879 * serialize, so the caller is expected to hold it.
 880 */
 881static void msc_buffer_free(struct msc *msc)
 882{
 
 
 883	if (msc->mode == MSC_MODE_SINGLE)
 884		msc_buffer_contig_free(msc);
 885	else if (msc->mode == MSC_MODE_MULTI)
 886		msc_buffer_multi_free(msc);
 887}
 888
 889/**
 890 * msc_buffer_alloc() - allocate a buffer for MSC
 891 * @msc:	MSC device
 892 * @size:	allocation size in bytes
 893 *
 894 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
 895 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
 896 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
 897 * window per invocation, so in multiblock mode this can be called multiple
 898 * times for the same MSC to allocate multiple windows.
 899 *
 900 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
 901 * to serialize, so the caller is expected to hold it.
 902 *
 903 * Return:	0 on success, -errno otherwise.
 904 */
 905static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
 906			    unsigned int nr_wins)
 907{
 908	int ret;
 909
 910	/* -1: buffer not allocated */
 911	if (atomic_read(&msc->user_count) != -1)
 912		return -EBUSY;
 913
 914	if (msc->mode == MSC_MODE_SINGLE) {
 915		if (nr_wins != 1)
 916			return -EINVAL;
 917
 918		ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
 919	} else if (msc->mode == MSC_MODE_MULTI) {
 920		ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
 921	} else {
 922		ret = -ENOTSUPP;
 923	}
 924
 925	if (!ret) {
 
 
 926		/* allocation should be visible before the counter goes to 0 */
 927		smp_mb__before_atomic();
 928
 929		if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
 930			return -EINVAL;
 931	}
 932
 933	return ret;
 934}
 935
 936/**
 937 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
 938 * @msc:	MSC device
 939 *
 940 * This will free MSC buffer unless it is in use or there is no allocated
 941 * buffer.
 942 * Caller needs to hold msc::buf_mutex.
 943 *
 944 * Return:	0 on successful deallocation or if there was no buffer to
 945 *		deallocate, -EBUSY if there are active users.
 946 */
 947static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
 948{
 949	int count, ret = 0;
 950
 951	count = atomic_cmpxchg(&msc->user_count, 0, -1);
 952
 953	/* > 0: buffer is allocated and has users */
 954	if (count > 0)
 955		ret = -EBUSY;
 956	/* 0: buffer is allocated, no users */
 957	else if (!count)
 958		msc_buffer_free(msc);
 959	/* < 0: no buffer, nothing to do */
 960
 961	return ret;
 962}
 963
 964/**
 965 * msc_buffer_free_unless_used() - free a buffer unless it's in use
 966 * @msc:	MSC device
 967 *
 968 * This is a locked version of msc_buffer_unlocked_free_unless_used().
 969 */
 970static int msc_buffer_free_unless_used(struct msc *msc)
 971{
 972	int ret;
 973
 974	mutex_lock(&msc->buf_mutex);
 975	ret = msc_buffer_unlocked_free_unless_used(msc);
 976	mutex_unlock(&msc->buf_mutex);
 977
 978	return ret;
 979}
 980
 981/**
 982 * msc_buffer_get_page() - get MSC buffer page at a given offset
 983 * @msc:	MSC device
 984 * @pgoff:	page offset into the storage buffer
 985 *
 986 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
 987 * the caller.
 988 *
 989 * Return:	page if @pgoff corresponds to a valid buffer page or NULL.
 990 */
 991static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
 992{
 993	struct msc_window *win;
 
 
 994
 995	if (msc->mode == MSC_MODE_SINGLE)
 996		return msc_buffer_contig_get_page(msc, pgoff);
 997
 998	list_for_each_entry(win, &msc->win_list, entry)
 999		if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1000			goto found;
1001
1002	return NULL;
1003
1004found:
1005	pgoff -= win->pgoff;
1006	return virt_to_page(win->block[pgoff].bdesc);
 
 
 
 
 
 
 
 
 
 
 
1007}
1008
1009/**
1010 * struct msc_win_to_user_struct - data for copy_to_user() callback
1011 * @buf:	userspace buffer to copy data to
1012 * @offset:	running offset
1013 */
1014struct msc_win_to_user_struct {
1015	char __user	*buf;
1016	unsigned long	offset;
1017};
1018
1019/**
1020 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1021 * @data:	callback's private data
1022 * @src:	source buffer
1023 * @len:	amount of data to copy from the source buffer
1024 */
1025static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1026{
1027	struct msc_win_to_user_struct *u = data;
1028	unsigned long ret;
1029
1030	ret = copy_to_user(u->buf + u->offset, src, len);
1031	u->offset += len - ret;
1032
1033	return ret;
1034}
1035
1036
1037/*
1038 * file operations' callbacks
1039 */
1040
1041static int intel_th_msc_open(struct inode *inode, struct file *file)
1042{
1043	struct intel_th_device *thdev = file->private_data;
1044	struct msc *msc = dev_get_drvdata(&thdev->dev);
1045	struct msc_iter *iter;
1046
1047	if (!capable(CAP_SYS_RAWIO))
1048		return -EPERM;
1049
1050	iter = msc_iter_install(msc);
1051	if (IS_ERR(iter))
1052		return PTR_ERR(iter);
1053
1054	file->private_data = iter;
1055
1056	return nonseekable_open(inode, file);
1057}
1058
1059static int intel_th_msc_release(struct inode *inode, struct file *file)
1060{
1061	struct msc_iter *iter = file->private_data;
1062	struct msc *msc = iter->msc;
1063
1064	msc_iter_remove(iter, msc);
1065
1066	return 0;
1067}
1068
1069static ssize_t
1070msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1071{
1072	unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1073	unsigned long start = off, tocopy = 0;
1074
1075	if (msc->single_wrap) {
1076		start += msc->single_sz;
1077		if (start < size) {
1078			tocopy = min(rem, size - start);
1079			if (copy_to_user(buf, msc->base + start, tocopy))
1080				return -EFAULT;
1081
1082			buf += tocopy;
1083			rem -= tocopy;
1084			start += tocopy;
1085		}
1086
1087		start &= size - 1;
1088		if (rem) {
1089			tocopy = min(rem, msc->single_sz - start);
1090			if (copy_to_user(buf, msc->base + start, tocopy))
1091				return -EFAULT;
1092
1093			rem -= tocopy;
1094		}
1095
1096		return len - rem;
1097	}
1098
1099	if (copy_to_user(buf, msc->base + start, rem))
1100		return -EFAULT;
1101
1102	return len;
1103}
1104
1105static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1106				 size_t len, loff_t *ppos)
1107{
1108	struct msc_iter *iter = file->private_data;
1109	struct msc *msc = iter->msc;
1110	size_t size;
1111	loff_t off = *ppos;
1112	ssize_t ret = 0;
1113
1114	if (!atomic_inc_unless_negative(&msc->user_count))
1115		return 0;
1116
1117	if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1118		size = msc->single_sz;
1119	else
1120		size = msc->nr_pages << PAGE_SHIFT;
1121
1122	if (!size)
1123		goto put_count;
1124
1125	if (off >= size)
1126		goto put_count;
1127
1128	if (off + len >= size)
1129		len = size - off;
1130
1131	if (msc->mode == MSC_MODE_SINGLE) {
1132		ret = msc_single_to_user(msc, buf, off, len);
1133		if (ret >= 0)
1134			*ppos += ret;
1135	} else if (msc->mode == MSC_MODE_MULTI) {
1136		struct msc_win_to_user_struct u = {
1137			.buf	= buf,
1138			.offset	= 0,
1139		};
1140
1141		ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1142		if (ret >= 0)
1143			*ppos = iter->offset;
1144	} else {
1145		ret = -ENOTSUPP;
1146	}
1147
1148put_count:
1149	atomic_dec(&msc->user_count);
1150
1151	return ret;
1152}
1153
1154/*
1155 * vm operations callbacks (vm_ops)
1156 */
1157
1158static void msc_mmap_open(struct vm_area_struct *vma)
1159{
1160	struct msc_iter *iter = vma->vm_file->private_data;
1161	struct msc *msc = iter->msc;
1162
1163	atomic_inc(&msc->mmap_count);
1164}
1165
1166static void msc_mmap_close(struct vm_area_struct *vma)
1167{
1168	struct msc_iter *iter = vma->vm_file->private_data;
1169	struct msc *msc = iter->msc;
1170	unsigned long pg;
1171
1172	if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1173		return;
1174
1175	/* drop page _refcounts */
1176	for (pg = 0; pg < msc->nr_pages; pg++) {
1177		struct page *page = msc_buffer_get_page(msc, pg);
1178
1179		if (WARN_ON_ONCE(!page))
1180			continue;
1181
1182		if (page->mapping)
1183			page->mapping = NULL;
1184	}
1185
1186	/* last mapping -- drop user_count */
1187	atomic_dec(&msc->user_count);
1188	mutex_unlock(&msc->buf_mutex);
1189}
1190
1191static int msc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1192{
1193	struct msc_iter *iter = vma->vm_file->private_data;
1194	struct msc *msc = iter->msc;
1195
1196	vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1197	if (!vmf->page)
1198		return VM_FAULT_SIGBUS;
1199
1200	get_page(vmf->page);
1201	vmf->page->mapping = vma->vm_file->f_mapping;
1202	vmf->page->index = vmf->pgoff;
1203
1204	return 0;
1205}
1206
1207static const struct vm_operations_struct msc_mmap_ops = {
1208	.open	= msc_mmap_open,
1209	.close	= msc_mmap_close,
1210	.fault	= msc_mmap_fault,
1211};
1212
1213static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1214{
1215	unsigned long size = vma->vm_end - vma->vm_start;
1216	struct msc_iter *iter = vma->vm_file->private_data;
1217	struct msc *msc = iter->msc;
1218	int ret = -EINVAL;
1219
1220	if (!size || offset_in_page(size))
1221		return -EINVAL;
1222
1223	if (vma->vm_pgoff)
1224		return -EINVAL;
1225
1226	/* grab user_count once per mmap; drop in msc_mmap_close() */
1227	if (!atomic_inc_unless_negative(&msc->user_count))
1228		return -EINVAL;
1229
1230	if (msc->mode != MSC_MODE_SINGLE &&
1231	    msc->mode != MSC_MODE_MULTI)
1232		goto out;
1233
1234	if (size >> PAGE_SHIFT != msc->nr_pages)
1235		goto out;
1236
1237	atomic_set(&msc->mmap_count, 1);
1238	ret = 0;
1239
1240out:
1241	if (ret)
1242		atomic_dec(&msc->user_count);
1243
1244	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1245	vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1246	vma->vm_ops = &msc_mmap_ops;
1247	return ret;
1248}
1249
1250static const struct file_operations intel_th_msc_fops = {
1251	.open		= intel_th_msc_open,
1252	.release	= intel_th_msc_release,
1253	.read		= intel_th_msc_read,
1254	.mmap		= intel_th_msc_mmap,
1255	.llseek		= no_llseek,
1256	.owner		= THIS_MODULE,
1257};
1258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1259static int intel_th_msc_init(struct msc *msc)
1260{
1261	atomic_set(&msc->user_count, -1);
1262
1263	msc->mode = MSC_MODE_MULTI;
1264	mutex_init(&msc->buf_mutex);
1265	INIT_LIST_HEAD(&msc->win_list);
1266	INIT_LIST_HEAD(&msc->iter_list);
1267
1268	msc->burst_len =
1269		(ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1270		__ffs(MSC_LEN);
1271
1272	return 0;
1273}
1274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1275static const char * const msc_mode[] = {
1276	[MSC_MODE_SINGLE]	= "single",
1277	[MSC_MODE_MULTI]	= "multi",
1278	[MSC_MODE_EXI]		= "ExI",
1279	[MSC_MODE_DEBUG]	= "debug",
1280};
1281
1282static ssize_t
1283wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1284{
1285	struct msc *msc = dev_get_drvdata(dev);
1286
1287	return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1288}
1289
1290static ssize_t
1291wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1292	   size_t size)
1293{
1294	struct msc *msc = dev_get_drvdata(dev);
1295	unsigned long val;
1296	int ret;
1297
1298	ret = kstrtoul(buf, 10, &val);
1299	if (ret)
1300		return ret;
1301
1302	msc->wrap = !!val;
1303
1304	return size;
1305}
1306
1307static DEVICE_ATTR_RW(wrap);
1308
 
 
 
 
 
 
 
 
 
 
 
 
 
1309static ssize_t
1310mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1311{
1312	struct msc *msc = dev_get_drvdata(dev);
 
 
1313
1314	return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
 
 
 
 
 
 
1315}
1316
1317static ssize_t
1318mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1319	   size_t size)
1320{
 
1321	struct msc *msc = dev_get_drvdata(dev);
1322	size_t len = size;
1323	char *cp;
1324	int i, ret;
1325
1326	if (!capable(CAP_SYS_RAWIO))
1327		return -EPERM;
1328
1329	cp = memchr(buf, '\n', len);
1330	if (cp)
1331		len = cp - buf;
1332
1333	for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1334		if (!strncmp(msc_mode[i], buf, len))
1335			goto found;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1336
1337	return -EINVAL;
1338
1339found:
 
 
 
1340	mutex_lock(&msc->buf_mutex);
 
 
 
 
 
 
 
 
 
1341	ret = msc_buffer_unlocked_free_unless_used(msc);
1342	if (!ret)
1343		msc->mode = i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1344	mutex_unlock(&msc->buf_mutex);
1345
1346	return ret ? ret : size;
1347}
1348
1349static DEVICE_ATTR_RW(mode);
1350
1351static ssize_t
1352nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1353{
1354	struct msc *msc = dev_get_drvdata(dev);
1355	struct msc_window *win;
1356	size_t count = 0;
1357
1358	mutex_lock(&msc->buf_mutex);
1359
1360	if (msc->mode == MSC_MODE_SINGLE)
1361		count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1362	else if (msc->mode == MSC_MODE_MULTI) {
1363		list_for_each_entry(win, &msc->win_list, entry) {
1364			count += scnprintf(buf + count, PAGE_SIZE - count,
1365					   "%d%c", win->nr_blocks,
1366					   msc_is_last_win(win) ? '\n' : ',');
1367		}
1368	} else {
1369		count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1370	}
1371
1372	mutex_unlock(&msc->buf_mutex);
1373
1374	return count;
1375}
1376
1377static ssize_t
1378nr_pages_store(struct device *dev, struct device_attribute *attr,
1379	       const char *buf, size_t size)
1380{
1381	struct msc *msc = dev_get_drvdata(dev);
1382	unsigned long val, *win = NULL, *rewin;
1383	size_t len = size;
1384	const char *p = buf;
1385	char *end, *s;
1386	int ret, nr_wins = 0;
1387
1388	if (!capable(CAP_SYS_RAWIO))
1389		return -EPERM;
1390
1391	ret = msc_buffer_free_unless_used(msc);
1392	if (ret)
1393		return ret;
1394
1395	/* scan the comma-separated list of allocation sizes */
1396	end = memchr(buf, '\n', len);
1397	if (end)
1398		len = end - buf;
1399
1400	do {
1401		end = memchr(p, ',', len);
1402		s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1403		if (!s) {
1404			ret = -ENOMEM;
1405			goto free_win;
1406		}
1407
1408		ret = kstrtoul(s, 10, &val);
1409		kfree(s);
1410
1411		if (ret || !val)
1412			goto free_win;
1413
1414		if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1415			ret = -EINVAL;
1416			goto free_win;
1417		}
1418
1419		nr_wins++;
1420		rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1421		if (!rewin) {
1422			kfree(win);
1423			return -ENOMEM;
1424		}
1425
1426		win = rewin;
1427		win[nr_wins - 1] = val;
1428
1429		if (!end)
1430			break;
1431
1432		len -= end - p;
 
1433		p = end + 1;
1434	} while (len);
1435
1436	mutex_lock(&msc->buf_mutex);
1437	ret = msc_buffer_alloc(msc, win, nr_wins);
1438	mutex_unlock(&msc->buf_mutex);
1439
1440free_win:
1441	kfree(win);
1442
1443	return ret ? ret : size;
1444}
1445
1446static DEVICE_ATTR_RW(nr_pages);
1447
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1448static struct attribute *msc_output_attrs[] = {
1449	&dev_attr_wrap.attr,
1450	&dev_attr_mode.attr,
1451	&dev_attr_nr_pages.attr,
 
 
1452	NULL,
1453};
1454
1455static struct attribute_group msc_output_group = {
1456	.attrs	= msc_output_attrs,
1457};
1458
1459static int intel_th_msc_probe(struct intel_th_device *thdev)
1460{
1461	struct device *dev = &thdev->dev;
1462	struct resource *res;
1463	struct msc *msc;
1464	void __iomem *base;
1465	int err;
1466
1467	res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1468	if (!res)
1469		return -ENODEV;
1470
1471	base = devm_ioremap(dev, res->start, resource_size(res));
1472	if (!base)
1473		return -ENOMEM;
1474
1475	msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1476	if (!msc)
1477		return -ENOMEM;
1478
 
 
 
 
 
 
 
1479	msc->index = thdev->id;
1480
1481	msc->thdev = thdev;
1482	msc->reg_base = base + msc->index * 0x100;
 
1483
 
1484	err = intel_th_msc_init(msc);
1485	if (err)
1486		return err;
1487
1488	dev_set_drvdata(dev, msc);
1489
1490	return 0;
1491}
1492
1493static void intel_th_msc_remove(struct intel_th_device *thdev)
1494{
1495	struct msc *msc = dev_get_drvdata(&thdev->dev);
1496	int ret;
1497
1498	intel_th_msc_deactivate(thdev);
1499
1500	/*
1501	 * Buffers should not be used at this point except if the
1502	 * output character device is still open and the parent
1503	 * device gets detached from its bus, which is a FIXME.
1504	 */
1505	ret = msc_buffer_free_unless_used(msc);
1506	WARN_ON_ONCE(ret);
1507}
1508
1509static struct intel_th_driver intel_th_msc_driver = {
1510	.probe	= intel_th_msc_probe,
1511	.remove	= intel_th_msc_remove,
 
 
1512	.activate	= intel_th_msc_activate,
1513	.deactivate	= intel_th_msc_deactivate,
1514	.fops	= &intel_th_msc_fops,
1515	.attr_group	= &msc_output_group,
1516	.driver	= {
1517		.name	= "msc",
1518		.owner	= THIS_MODULE,
1519	},
1520};
1521
1522module_driver(intel_th_msc_driver,
1523	      intel_th_driver_register,
1524	      intel_th_driver_unregister);
1525
1526MODULE_LICENSE("GPL v2");
1527MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1528MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");