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		size_t hw_sz = sizeof(struct msc_block_desc) -
662			offsetof(struct msc_block_desc, hw_tag);
663
664		for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
665			struct msc_block_desc *bdesc = sg_virt(sg);
666
667			memset(&bdesc->hw_tag, 0, hw_sz);
668		}
669	}
670}
671
672static int intel_th_msu_init(struct msc *msc)
673{
674	u32 mintctl, msusts;
675
676	if (!msc->do_irq)
677		return 0;
678
679	if (!msc->mbuf)
680		return 0;
681
682	mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
683	mintctl |= msc->index ? M1BLIE : M0BLIE;
684	iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
685	if (mintctl != ioread32(msc->msu_base + REG_MSU_MINTCTL)) {
686		dev_info(msc_dev(msc), "MINTCTL ignores writes: no usable interrupts\n");
687		msc->do_irq = 0;
688		return 0;
689	}
690
691	msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
692	iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
693
694	return 0;
695}
696
697static void intel_th_msu_deinit(struct msc *msc)
698{
699	u32 mintctl;
700
701	if (!msc->do_irq)
702		return;
703
704	mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
705	mintctl &= msc->index ? ~M1BLIE : ~M0BLIE;
706	iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
707}
708
709static int msc_win_set_lockout(struct msc_window *win,
710			       enum lockout_state expect,
711			       enum lockout_state new)
712{
713	enum lockout_state old;
714	unsigned long flags;
715	int ret = 0;
716
717	if (!win->msc->mbuf)
718		return 0;
719
720	spin_lock_irqsave(&win->lo_lock, flags);
721	old = win->lockout;
722
723	if (old != expect) {
724		ret = -EINVAL;
725		goto unlock;
726	}
727
728	win->lockout = new;
729
730	if (old == expect && new == WIN_LOCKED)
731		atomic_inc(&win->msc->user_count);
732	else if (old == expect && old == WIN_LOCKED)
733		atomic_dec(&win->msc->user_count);
734
735unlock:
736	spin_unlock_irqrestore(&win->lo_lock, flags);
737
738	if (ret) {
739		if (expect == WIN_READY && old == WIN_LOCKED)
740			return -EBUSY;
741
742		/* from intel_th_msc_window_unlock(), don't warn if not locked */
743		if (expect == WIN_LOCKED && old == new)
744			return 0;
745
746		dev_warn_ratelimited(msc_dev(win->msc),
747				     "expected lockout state %d, got %d\n",
748				     expect, old);
749	}
750
751	return ret;
752}
753/**
754 * msc_configure() - set up MSC hardware
755 * @msc:	the MSC device to configure
756 *
757 * Program storage mode, wrapping, burst length and trace buffer address
758 * into a given MSC. Then, enable tracing and set msc::enabled.
759 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
760 */
761static int msc_configure(struct msc *msc)
762{
763	u32 reg;
764
765	lockdep_assert_held(&msc->buf_mutex);
766
767	if (msc->mode > MSC_MODE_MULTI)
768		return -EINVAL;
769
770	if (msc->mode == MSC_MODE_MULTI) {
771		if (msc_win_set_lockout(msc->cur_win, WIN_READY, WIN_INUSE))
772			return -EBUSY;
773
774		msc_buffer_clear_hw_header(msc);
775	}
776
777	msc->orig_addr = ioread32(msc->reg_base + REG_MSU_MSC0BAR);
778	msc->orig_sz   = ioread32(msc->reg_base + REG_MSU_MSC0SIZE);
779
780	reg = msc->base_addr >> PAGE_SHIFT;
781	iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
782
783	if (msc->mode == MSC_MODE_SINGLE) {
784		reg = msc->nr_pages;
785		iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
786	}
787
788	reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
789	reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
790
791	reg |= MSC_EN;
792	reg |= msc->mode << __ffs(MSC_MODE);
793	reg |= msc->burst_len << __ffs(MSC_LEN);
794
795	if (msc->wrap)
796		reg |= MSC_WRAPEN;
797
798	iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
799
800	intel_th_msu_init(msc);
801
802	msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
803	intel_th_trace_enable(msc->thdev);
804	msc->enabled = 1;
805
806	if (msc->mbuf && msc->mbuf->activate)
807		msc->mbuf->activate(msc->mbuf_priv);
808
809	return 0;
810}
811
812/**
813 * msc_disable() - disable MSC hardware
814 * @msc:	MSC device to disable
815 *
816 * If @msc is enabled, disable tracing on the switch and then disable MSC
817 * storage. Caller must hold msc::buf_mutex.
818 */
819static void msc_disable(struct msc *msc)
820{
821	struct msc_window *win = msc->cur_win;
822	u32 reg;
823
824	lockdep_assert_held(&msc->buf_mutex);
825
826	if (msc->mode == MSC_MODE_MULTI)
827		msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
828
829	if (msc->mbuf && msc->mbuf->deactivate)
830		msc->mbuf->deactivate(msc->mbuf_priv);
831	intel_th_msu_deinit(msc);
832	intel_th_trace_disable(msc->thdev);
833
834	if (msc->mode == MSC_MODE_SINGLE) {
835		reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
836		msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
837
838		reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
839		msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
840		dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
841			reg, msc->single_sz, msc->single_wrap);
842	}
843
844	reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
845	reg &= ~MSC_EN;
846	iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
847
848	if (msc->mbuf && msc->mbuf->ready)
849		msc->mbuf->ready(msc->mbuf_priv, win->sgt,
850				 msc_win_total_sz(win));
851
852	msc->enabled = 0;
853
854	iowrite32(msc->orig_addr, msc->reg_base + REG_MSU_MSC0BAR);
855	iowrite32(msc->orig_sz, msc->reg_base + REG_MSU_MSC0SIZE);
856
857	dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
858		ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
859
860	reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
861	dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
862
863	reg = ioread32(msc->reg_base + REG_MSU_MSUSTS);
864	reg &= msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
865	iowrite32(reg, msc->reg_base + REG_MSU_MSUSTS);
866}
867
868static int intel_th_msc_activate(struct intel_th_device *thdev)
869{
870	struct msc *msc = dev_get_drvdata(&thdev->dev);
871	int ret = -EBUSY;
872
873	if (!atomic_inc_unless_negative(&msc->user_count))
874		return -ENODEV;
875
876	mutex_lock(&msc->buf_mutex);
877
878	/* if there are readers, refuse */
879	if (list_empty(&msc->iter_list))
880		ret = msc_configure(msc);
881
882	mutex_unlock(&msc->buf_mutex);
883
884	if (ret)
885		atomic_dec(&msc->user_count);
886
887	return ret;
888}
889
890static void intel_th_msc_deactivate(struct intel_th_device *thdev)
891{
892	struct msc *msc = dev_get_drvdata(&thdev->dev);
893
894	mutex_lock(&msc->buf_mutex);
895	if (msc->enabled) {
896		msc_disable(msc);
897		atomic_dec(&msc->user_count);
898	}
899	mutex_unlock(&msc->buf_mutex);
900}
901
902/**
903 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
904 * @msc:	MSC device
905 * @size:	allocation size in bytes
906 *
907 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
908 * caller is expected to hold it.
909 *
910 * Return:	0 on success, -errno otherwise.
911 */
912static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
913{
914	unsigned long nr_pages = size >> PAGE_SHIFT;
915	unsigned int order = get_order(size);
916	struct page *page;
917	int ret;
918
919	if (!size)
920		return 0;
921
922	ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
923	if (ret)
924		goto err_out;
925
926	ret = -ENOMEM;
927	page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
928	if (!page)
929		goto err_free_sgt;
930
931	split_page(page, order);
932	sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
933
934	ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
935			 DMA_FROM_DEVICE);
936	if (ret < 0)
937		goto err_free_pages;
938
939	msc->nr_pages = nr_pages;
940	msc->base = page_address(page);
941	msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
942
943	return 0;
944
945err_free_pages:
946	__free_pages(page, order);
947
948err_free_sgt:
949	sg_free_table(&msc->single_sgt);
950
951err_out:
952	return ret;
953}
954
955/**
956 * msc_buffer_contig_free() - free a contiguous buffer
957 * @msc:	MSC configured in SINGLE mode
958 */
959static void msc_buffer_contig_free(struct msc *msc)
960{
961	unsigned long off;
962
963	dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
964		     1, DMA_FROM_DEVICE);
965	sg_free_table(&msc->single_sgt);
966
967	for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
968		struct page *page = virt_to_page(msc->base + off);
969
970		page->mapping = NULL;
971		__free_page(page);
972	}
973
974	msc->nr_pages = 0;
975}
976
977/**
978 * msc_buffer_contig_get_page() - find a page at a given offset
979 * @msc:	MSC configured in SINGLE mode
980 * @pgoff:	page offset
981 *
982 * Return:	page, if @pgoff is within the range, NULL otherwise.
983 */
984static struct page *msc_buffer_contig_get_page(struct msc *msc,
985					       unsigned long pgoff)
986{
987	if (pgoff >= msc->nr_pages)
988		return NULL;
989
990	return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
991}
992
993static int __msc_buffer_win_alloc(struct msc_window *win,
994				  unsigned int nr_segs)
995{
996	struct scatterlist *sg_ptr;
997	void *block;
998	int i, ret;
999
1000	ret = sg_alloc_table(win->sgt, nr_segs, GFP_KERNEL);
1001	if (ret)
1002		return -ENOMEM;
1003
1004	for_each_sg(win->sgt->sgl, sg_ptr, nr_segs, i) {
1005		block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
1006					  PAGE_SIZE, &sg_dma_address(sg_ptr),
1007					  GFP_KERNEL);
1008		if (!block)
1009			goto err_nomem;
1010
1011		sg_set_buf(sg_ptr, block, PAGE_SIZE);
1012	}
1013
1014	return nr_segs;
1015
1016err_nomem:
1017	for_each_sg(win->sgt->sgl, sg_ptr, i, ret)
1018		dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1019				  sg_virt(sg_ptr), sg_dma_address(sg_ptr));
1020
1021	sg_free_table(win->sgt);
1022
1023	return -ENOMEM;
1024}
1025
1026#ifdef CONFIG_X86
1027static void msc_buffer_set_uc(struct msc_window *win, unsigned int nr_segs)
1028{
1029	struct scatterlist *sg_ptr;
1030	int i;
1031
1032	for_each_sg(win->sgt->sgl, sg_ptr, nr_segs, i) {
1033		/* Set the page as uncached */
1034		set_memory_uc((unsigned long)sg_virt(sg_ptr),
1035			      PFN_DOWN(sg_ptr->length));
1036	}
1037}
1038
1039static void msc_buffer_set_wb(struct msc_window *win)
1040{
1041	struct scatterlist *sg_ptr;
1042	int i;
1043
1044	for_each_sg(win->sgt->sgl, sg_ptr, win->nr_segs, i) {
1045		/* Reset the page to write-back */
1046		set_memory_wb((unsigned long)sg_virt(sg_ptr),
1047			      PFN_DOWN(sg_ptr->length));
1048	}
1049}
1050#else /* !X86 */
1051static inline void
1052msc_buffer_set_uc(struct msc_window *win, unsigned int nr_segs) {}
1053static inline void msc_buffer_set_wb(struct msc_window *win) {}
1054#endif /* CONFIG_X86 */
1055
1056static struct page *msc_sg_page(struct scatterlist *sg)
1057{
1058	void *addr = sg_virt(sg);
1059
1060	if (is_vmalloc_addr(addr))
1061		return vmalloc_to_page(addr);
1062
1063	return sg_page(sg);
1064}
1065
1066/**
1067 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
1068 * @msc:	MSC device
1069 * @nr_blocks:	number of pages in this window
1070 *
1071 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1072 * to serialize, so the caller is expected to hold it.
1073 *
1074 * Return:	0 on success, -errno otherwise.
1075 */
1076static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
1077{
1078	struct msc_window *win;
1079	int ret = -ENOMEM;
1080
1081	if (!nr_blocks)
1082		return 0;
1083
1084	win = kzalloc(sizeof(*win), GFP_KERNEL);
1085	if (!win)
1086		return -ENOMEM;
1087
1088	win->msc = msc;
1089	win->sgt = &win->_sgt;
1090	win->lockout = WIN_READY;
1091	spin_lock_init(&win->lo_lock);
1092
1093	if (!list_empty(&msc->win_list)) {
1094		struct msc_window *prev = list_last_entry(&msc->win_list,
1095							  struct msc_window,
1096							  entry);
1097
1098		win->pgoff = prev->pgoff + prev->nr_blocks;
1099	}
1100
1101	if (msc->mbuf && msc->mbuf->alloc_window)
1102		ret = msc->mbuf->alloc_window(msc->mbuf_priv, &win->sgt,
1103					      nr_blocks << PAGE_SHIFT);
1104	else
1105		ret = __msc_buffer_win_alloc(win, nr_blocks);
1106
1107	if (ret <= 0)
1108		goto err_nomem;
1109
1110	msc_buffer_set_uc(win, ret);
1111
1112	win->nr_segs = ret;
1113	win->nr_blocks = nr_blocks;
1114
1115	if (list_empty(&msc->win_list)) {
1116		msc->base = msc_win_base(win);
1117		msc->base_addr = msc_win_base_dma(win);
1118		msc->cur_win = win;
1119	}
1120
1121	list_add_tail(&win->entry, &msc->win_list);
1122	msc->nr_pages += nr_blocks;
1123
1124	return 0;
1125
1126err_nomem:
1127	kfree(win);
1128
1129	return ret;
1130}
1131
1132static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1133{
1134	struct scatterlist *sg;
1135	int i;
1136
1137	for_each_sg(win->sgt->sgl, sg, win->nr_segs, i) {
1138		struct page *page = msc_sg_page(sg);
1139
1140		page->mapping = NULL;
1141		dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
1142				  sg_virt(sg), sg_dma_address(sg));
1143	}
1144	sg_free_table(win->sgt);
1145}
1146
1147/**
1148 * msc_buffer_win_free() - free a window from MSC's window list
1149 * @msc:	MSC device
1150 * @win:	window to free
1151 *
1152 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1153 * to serialize, so the caller is expected to hold it.
1154 */
1155static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
1156{
1157	msc->nr_pages -= win->nr_blocks;
1158
1159	list_del(&win->entry);
1160	if (list_empty(&msc->win_list)) {
1161		msc->base = NULL;
1162		msc->base_addr = 0;
1163	}
1164
1165	msc_buffer_set_wb(win);
1166
1167	if (msc->mbuf && msc->mbuf->free_window)
1168		msc->mbuf->free_window(msc->mbuf_priv, win->sgt);
1169	else
1170		__msc_buffer_win_free(msc, win);
1171
1172	kfree(win);
1173}
1174
1175/**
1176 * msc_buffer_relink() - set up block descriptors for multiblock mode
1177 * @msc:	MSC device
1178 *
1179 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
1180 * so the caller is expected to hold it.
1181 */
1182static void msc_buffer_relink(struct msc *msc)
1183{
1184	struct msc_window *win, *next_win;
1185
1186	/* call with msc::mutex locked */
1187	list_for_each_entry(win, &msc->win_list, entry) {
1188		struct scatterlist *sg;
1189		unsigned int blk;
1190		u32 sw_tag = 0;
1191
1192		/*
1193		 * Last window's next_win should point to the first window
1194		 * and MSC_SW_TAG_LASTWIN should be set.
1195		 */
1196		if (msc_is_last_win(win)) {
1197			sw_tag |= MSC_SW_TAG_LASTWIN;
1198			next_win = list_first_entry(&msc->win_list,
1199						    struct msc_window, entry);
1200		} else {
1201			next_win = list_next_entry(win, entry);
1202		}
1203
1204		for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1205			struct msc_block_desc *bdesc = sg_virt(sg);
1206
1207			memset(bdesc, 0, sizeof(*bdesc));
1208
1209			bdesc->next_win = msc_win_base_pfn(next_win);
1210
1211			/*
1212			 * Similarly to last window, last block should point
1213			 * to the first one.
1214			 */
1215			if (blk == win->nr_segs - 1) {
1216				sw_tag |= MSC_SW_TAG_LASTBLK;
1217				bdesc->next_blk = msc_win_base_pfn(win);
1218			} else {
1219				dma_addr_t addr = sg_dma_address(sg_next(sg));
1220
1221				bdesc->next_blk = PFN_DOWN(addr);
1222			}
1223
1224			bdesc->sw_tag = sw_tag;
1225			bdesc->block_sz = sg->length / 64;
1226		}
1227	}
1228
1229	/*
1230	 * Make the above writes globally visible before tracing is
1231	 * enabled to make sure hardware sees them coherently.
1232	 */
1233	wmb();
1234}
1235
1236static void msc_buffer_multi_free(struct msc *msc)
1237{
1238	struct msc_window *win, *iter;
1239
1240	list_for_each_entry_safe(win, iter, &msc->win_list, entry)
1241		msc_buffer_win_free(msc, win);
1242}
1243
1244static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
1245				  unsigned int nr_wins)
1246{
1247	int ret, i;
1248
1249	for (i = 0; i < nr_wins; i++) {
1250		ret = msc_buffer_win_alloc(msc, nr_pages[i]);
1251		if (ret) {
1252			msc_buffer_multi_free(msc);
1253			return ret;
1254		}
1255	}
1256
1257	msc_buffer_relink(msc);
1258
1259	return 0;
1260}
1261
1262/**
1263 * msc_buffer_free() - free buffers for MSC
1264 * @msc:	MSC device
1265 *
1266 * Free MSC's storage buffers.
1267 *
1268 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
1269 * serialize, so the caller is expected to hold it.
1270 */
1271static void msc_buffer_free(struct msc *msc)
1272{
1273	if (msc->mode == MSC_MODE_SINGLE)
1274		msc_buffer_contig_free(msc);
1275	else if (msc->mode == MSC_MODE_MULTI)
1276		msc_buffer_multi_free(msc);
1277}
1278
1279/**
1280 * msc_buffer_alloc() - allocate a buffer for MSC
1281 * @msc:	MSC device
1282 * @size:	allocation size in bytes
1283 *
1284 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
1285 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
1286 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
1287 * window per invocation, so in multiblock mode this can be called multiple
1288 * times for the same MSC to allocate multiple windows.
1289 *
1290 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
1291 * to serialize, so the caller is expected to hold it.
1292 *
1293 * Return:	0 on success, -errno otherwise.
1294 */
1295static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
1296			    unsigned int nr_wins)
1297{
1298	int ret;
1299
1300	/* -1: buffer not allocated */
1301	if (atomic_read(&msc->user_count) != -1)
1302		return -EBUSY;
1303
1304	if (msc->mode == MSC_MODE_SINGLE) {
1305		if (nr_wins != 1)
1306			return -EINVAL;
1307
1308		ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
1309	} else if (msc->mode == MSC_MODE_MULTI) {
1310		ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
1311	} else {
1312		ret = -EINVAL;
1313	}
1314
1315	if (!ret) {
1316		/* allocation should be visible before the counter goes to 0 */
1317		smp_mb__before_atomic();
1318
1319		if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
1320			return -EINVAL;
1321	}
1322
1323	return ret;
1324}
1325
1326/**
1327 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
1328 * @msc:	MSC device
1329 *
1330 * This will free MSC buffer unless it is in use or there is no allocated
1331 * buffer.
1332 * Caller needs to hold msc::buf_mutex.
1333 *
1334 * Return:	0 on successful deallocation or if there was no buffer to
1335 *		deallocate, -EBUSY if there are active users.
1336 */
1337static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1338{
1339	int count, ret = 0;
1340
1341	count = atomic_cmpxchg(&msc->user_count, 0, -1);
1342
1343	/* > 0: buffer is allocated and has users */
1344	if (count > 0)
1345		ret = -EBUSY;
1346	/* 0: buffer is allocated, no users */
1347	else if (!count)
1348		msc_buffer_free(msc);
1349	/* < 0: no buffer, nothing to do */
1350
1351	return ret;
1352}
1353
1354/**
1355 * msc_buffer_free_unless_used() - free a buffer unless it's in use
1356 * @msc:	MSC device
1357 *
1358 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1359 */
1360static int msc_buffer_free_unless_used(struct msc *msc)
1361{
1362	int ret;
1363
1364	mutex_lock(&msc->buf_mutex);
1365	ret = msc_buffer_unlocked_free_unless_used(msc);
1366	mutex_unlock(&msc->buf_mutex);
1367
1368	return ret;
1369}
1370
1371/**
1372 * msc_buffer_get_page() - get MSC buffer page at a given offset
1373 * @msc:	MSC device
1374 * @pgoff:	page offset into the storage buffer
1375 *
1376 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1377 * the caller.
1378 *
1379 * Return:	page if @pgoff corresponds to a valid buffer page or NULL.
1380 */
1381static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1382{
1383	struct msc_window *win;
1384	struct scatterlist *sg;
1385	unsigned int blk;
1386
1387	if (msc->mode == MSC_MODE_SINGLE)
1388		return msc_buffer_contig_get_page(msc, pgoff);
1389
1390	list_for_each_entry(win, &msc->win_list, entry)
1391		if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1392			goto found;
1393
1394	return NULL;
1395
1396found:
1397	pgoff -= win->pgoff;
1398
1399	for_each_sg(win->sgt->sgl, sg, win->nr_segs, blk) {
1400		struct page *page = msc_sg_page(sg);
1401		size_t pgsz = PFN_DOWN(sg->length);
1402
1403		if (pgoff < pgsz)
1404			return page + pgoff;
1405
1406		pgoff -= pgsz;
1407	}
1408
1409	return NULL;
1410}
1411
1412/**
1413 * struct msc_win_to_user_struct - data for copy_to_user() callback
1414 * @buf:	userspace buffer to copy data to
1415 * @offset:	running offset
1416 */
1417struct msc_win_to_user_struct {
1418	char __user	*buf;
1419	unsigned long	offset;
1420};
1421
1422/**
1423 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1424 * @data:	callback's private data
1425 * @src:	source buffer
1426 * @len:	amount of data to copy from the source buffer
1427 */
1428static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1429{
1430	struct msc_win_to_user_struct *u = data;
1431	unsigned long ret;
1432
1433	ret = copy_to_user(u->buf + u->offset, src, len);
1434	u->offset += len - ret;
1435
1436	return ret;
1437}
1438
1439
1440/*
1441 * file operations' callbacks
1442 */
1443
1444static int intel_th_msc_open(struct inode *inode, struct file *file)
1445{
1446	struct intel_th_device *thdev = file->private_data;
1447	struct msc *msc = dev_get_drvdata(&thdev->dev);
1448	struct msc_iter *iter;
1449
1450	if (!capable(CAP_SYS_RAWIO))
1451		return -EPERM;
1452
1453	iter = msc_iter_install(msc);
1454	if (IS_ERR(iter))
1455		return PTR_ERR(iter);
1456
1457	file->private_data = iter;
1458
1459	return nonseekable_open(inode, file);
1460}
1461
1462static int intel_th_msc_release(struct inode *inode, struct file *file)
1463{
1464	struct msc_iter *iter = file->private_data;
1465	struct msc *msc = iter->msc;
1466
1467	msc_iter_remove(iter, msc);
1468
1469	return 0;
1470}
1471
1472static ssize_t
1473msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1474{
1475	unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1476	unsigned long start = off, tocopy = 0;
1477
1478	if (msc->single_wrap) {
1479		start += msc->single_sz;
1480		if (start < size) {
1481			tocopy = min(rem, size - start);
1482			if (copy_to_user(buf, msc->base + start, tocopy))
1483				return -EFAULT;
1484
1485			buf += tocopy;
1486			rem -= tocopy;
1487			start += tocopy;
1488		}
1489
1490		start &= size - 1;
1491		if (rem) {
1492			tocopy = min(rem, msc->single_sz - start);
1493			if (copy_to_user(buf, msc->base + start, tocopy))
1494				return -EFAULT;
1495
1496			rem -= tocopy;
1497		}
1498
1499		return len - rem;
1500	}
1501
1502	if (copy_to_user(buf, msc->base + start, rem))
1503		return -EFAULT;
1504
1505	return len;
1506}
1507
1508static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1509				 size_t len, loff_t *ppos)
1510{
1511	struct msc_iter *iter = file->private_data;
1512	struct msc *msc = iter->msc;
1513	size_t size;
1514	loff_t off = *ppos;
1515	ssize_t ret = 0;
1516
1517	if (!atomic_inc_unless_negative(&msc->user_count))
1518		return 0;
1519
1520	if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1521		size = msc->single_sz;
1522	else
1523		size = msc->nr_pages << PAGE_SHIFT;
1524
1525	if (!size)
1526		goto put_count;
1527
1528	if (off >= size)
1529		goto put_count;
1530
1531	if (off + len >= size)
1532		len = size - off;
1533
1534	if (msc->mode == MSC_MODE_SINGLE) {
1535		ret = msc_single_to_user(msc, buf, off, len);
1536		if (ret >= 0)
1537			*ppos += ret;
1538	} else if (msc->mode == MSC_MODE_MULTI) {
1539		struct msc_win_to_user_struct u = {
1540			.buf	= buf,
1541			.offset	= 0,
1542		};
1543
1544		ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1545		if (ret >= 0)
1546			*ppos = iter->offset;
1547	} else {
1548		ret = -EINVAL;
1549	}
1550
1551put_count:
1552	atomic_dec(&msc->user_count);
1553
1554	return ret;
1555}
1556
1557/*
1558 * vm operations callbacks (vm_ops)
1559 */
1560
1561static void msc_mmap_open(struct vm_area_struct *vma)
1562{
1563	struct msc_iter *iter = vma->vm_file->private_data;
1564	struct msc *msc = iter->msc;
1565
1566	atomic_inc(&msc->mmap_count);
1567}
1568
1569static void msc_mmap_close(struct vm_area_struct *vma)
1570{
1571	struct msc_iter *iter = vma->vm_file->private_data;
1572	struct msc *msc = iter->msc;
1573	unsigned long pg;
1574
1575	if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1576		return;
1577
1578	/* drop page _refcounts */
1579	for (pg = 0; pg < msc->nr_pages; pg++) {
1580		struct page *page = msc_buffer_get_page(msc, pg);
1581
1582		if (WARN_ON_ONCE(!page))
1583			continue;
1584
1585		if (page->mapping)
1586			page->mapping = NULL;
1587	}
1588
1589	/* last mapping -- drop user_count */
1590	atomic_dec(&msc->user_count);
1591	mutex_unlock(&msc->buf_mutex);
1592}
1593
1594static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
1595{
1596	struct msc_iter *iter = vmf->vma->vm_file->private_data;
1597	struct msc *msc = iter->msc;
1598
1599	vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1600	if (!vmf->page)
1601		return VM_FAULT_SIGBUS;
1602
1603	get_page(vmf->page);
1604	vmf->page->mapping = vmf->vma->vm_file->f_mapping;
1605	vmf->page->index = vmf->pgoff;
1606
1607	return 0;
1608}
1609
1610static const struct vm_operations_struct msc_mmap_ops = {
1611	.open	= msc_mmap_open,
1612	.close	= msc_mmap_close,
1613	.fault	= msc_mmap_fault,
1614};
1615
1616static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1617{
1618	unsigned long size = vma->vm_end - vma->vm_start;
1619	struct msc_iter *iter = vma->vm_file->private_data;
1620	struct msc *msc = iter->msc;
1621	int ret = -EINVAL;
1622
1623	if (!size || offset_in_page(size))
1624		return -EINVAL;
1625
1626	if (vma->vm_pgoff)
1627		return -EINVAL;
1628
1629	/* grab user_count once per mmap; drop in msc_mmap_close() */
1630	if (!atomic_inc_unless_negative(&msc->user_count))
1631		return -EINVAL;
1632
1633	if (msc->mode != MSC_MODE_SINGLE &&
1634	    msc->mode != MSC_MODE_MULTI)
1635		goto out;
1636
1637	if (size >> PAGE_SHIFT != msc->nr_pages)
1638		goto out;
1639
1640	atomic_set(&msc->mmap_count, 1);
1641	ret = 0;
1642
1643out:
1644	if (ret)
1645		atomic_dec(&msc->user_count);
1646
1647	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1648	vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1649	vma->vm_ops = &msc_mmap_ops;
1650	return ret;
1651}
1652
1653static const struct file_operations intel_th_msc_fops = {
1654	.open		= intel_th_msc_open,
1655	.release	= intel_th_msc_release,
1656	.read		= intel_th_msc_read,
1657	.mmap		= intel_th_msc_mmap,
1658	.llseek		= no_llseek,
1659	.owner		= THIS_MODULE,
1660};
1661
1662static void intel_th_msc_wait_empty(struct intel_th_device *thdev)
1663{
1664	struct msc *msc = dev_get_drvdata(&thdev->dev);
1665	unsigned long count;
1666	u32 reg;
1667
1668	for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
1669	     count && !(reg & MSCSTS_PLE); count--) {
1670		reg = __raw_readl(msc->reg_base + REG_MSU_MSC0STS);
1671		cpu_relax();
1672	}
1673
1674	if (!count)
1675		dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
1676}
1677
1678static int intel_th_msc_init(struct msc *msc)
1679{
1680	atomic_set(&msc->user_count, -1);
1681
1682	msc->mode = msc->multi_is_broken ? MSC_MODE_SINGLE : MSC_MODE_MULTI;
1683	mutex_init(&msc->buf_mutex);
1684	INIT_LIST_HEAD(&msc->win_list);
1685	INIT_LIST_HEAD(&msc->iter_list);
1686
1687	msc->burst_len =
1688		(ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1689		__ffs(MSC_LEN);
1690
1691	return 0;
1692}
1693
1694static int msc_win_switch(struct msc *msc)
1695{
1696	struct msc_window *first;
1697
1698	if (list_empty(&msc->win_list))
1699		return -EINVAL;
1700
1701	first = list_first_entry(&msc->win_list, struct msc_window, entry);
1702
1703	if (msc_is_last_win(msc->cur_win))
1704		msc->cur_win = first;
1705	else
1706		msc->cur_win = list_next_entry(msc->cur_win, entry);
1707
1708	msc->base = msc_win_base(msc->cur_win);
1709	msc->base_addr = msc_win_base_dma(msc->cur_win);
1710
1711	intel_th_trace_switch(msc->thdev);
1712
1713	return 0;
1714}
1715
1716/**
1717 * intel_th_msc_window_unlock - put the window back in rotation
1718 * @dev:	MSC device to which this relates
1719 * @sgt:	buffer's sg_table for the window, does nothing if NULL
1720 */
1721void intel_th_msc_window_unlock(struct device *dev, struct sg_table *sgt)
1722{
1723	struct msc *msc = dev_get_drvdata(dev);
1724	struct msc_window *win;
1725
1726	if (!sgt)
1727		return;
1728
1729	win = msc_find_window(msc, sgt, false);
1730	if (!win)
1731		return;
1732
1733	msc_win_set_lockout(win, WIN_LOCKED, WIN_READY);
1734	if (msc->switch_on_unlock == win) {
1735		msc->switch_on_unlock = NULL;
1736		msc_win_switch(msc);
1737	}
1738}
1739EXPORT_SYMBOL_GPL(intel_th_msc_window_unlock);
1740
1741static void msc_work(struct work_struct *work)
1742{
1743	struct msc *msc = container_of(work, struct msc, work);
1744
1745	intel_th_msc_deactivate(msc->thdev);
1746}
1747
1748static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1749{
1750	struct msc *msc = dev_get_drvdata(&thdev->dev);
1751	u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1752	u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1753	struct msc_window *win, *next_win;
1754
1755	if (!msc->do_irq || !msc->mbuf)
1756		return IRQ_NONE;
1757
1758	msusts &= mask;
1759
1760	if (!msusts)
1761		return msc->enabled ? IRQ_HANDLED : IRQ_NONE;
1762
1763	iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
1764
1765	if (!msc->enabled)
1766		return IRQ_NONE;
1767
1768	/* grab the window before we do the switch */
1769	win = msc->cur_win;
1770	if (!win)
1771		return IRQ_HANDLED;
1772	next_win = msc_next_window(win);
1773	if (!next_win)
1774		return IRQ_HANDLED;
1775
1776	/* next window: if READY, proceed, if LOCKED, stop the trace */
1777	if (msc_win_set_lockout(next_win, WIN_READY, WIN_INUSE)) {
1778		if (msc->stop_on_full)
1779			schedule_work(&msc->work);
1780		else
1781			msc->switch_on_unlock = next_win;
1782
1783		return IRQ_HANDLED;
1784	}
1785
1786	/* current window: INUSE -> LOCKED */
1787	msc_win_set_lockout(win, WIN_INUSE, WIN_LOCKED);
1788
1789	msc_win_switch(msc);
1790
1791	if (msc->mbuf && msc->mbuf->ready)
1792		msc->mbuf->ready(msc->mbuf_priv, win->sgt,
1793				 msc_win_total_sz(win));
1794
1795	return IRQ_HANDLED;
1796}
1797
1798static const char * const msc_mode[] = {
1799	[MSC_MODE_SINGLE]	= "single",
1800	[MSC_MODE_MULTI]	= "multi",
1801	[MSC_MODE_EXI]		= "ExI",
1802	[MSC_MODE_DEBUG]	= "debug",
1803};
1804
1805static ssize_t
1806wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1807{
1808	struct msc *msc = dev_get_drvdata(dev);
1809
1810	return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1811}
1812
1813static ssize_t
1814wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1815	   size_t size)
1816{
1817	struct msc *msc = dev_get_drvdata(dev);
1818	unsigned long val;
1819	int ret;
1820
1821	ret = kstrtoul(buf, 10, &val);
1822	if (ret)
1823		return ret;
1824
1825	msc->wrap = !!val;
1826
1827	return size;
1828}
1829
1830static DEVICE_ATTR_RW(wrap);
1831
1832static void msc_buffer_unassign(struct msc *msc)
1833{
1834	lockdep_assert_held(&msc->buf_mutex);
1835
1836	if (!msc->mbuf)
1837		return;
1838
1839	msc->mbuf->unassign(msc->mbuf_priv);
1840	msu_buffer_put(msc->mbuf);
1841	msc->mbuf_priv = NULL;
1842	msc->mbuf = NULL;
1843}
1844
1845static ssize_t
1846mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1847{
1848	struct msc *msc = dev_get_drvdata(dev);
1849	const char *mode = msc_mode[msc->mode];
1850	ssize_t ret;
1851
1852	mutex_lock(&msc->buf_mutex);
1853	if (msc->mbuf)
1854		mode = msc->mbuf->name;
1855	ret = scnprintf(buf, PAGE_SIZE, "%s\n", mode);
1856	mutex_unlock(&msc->buf_mutex);
1857
1858	return ret;
1859}
1860
1861static ssize_t
1862mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1863	   size_t size)
1864{
1865	const struct msu_buffer *mbuf = NULL;
1866	struct msc *msc = dev_get_drvdata(dev);
1867	size_t len = size;
1868	char *cp, *mode;
1869	int i, ret;
1870
1871	if (!capable(CAP_SYS_RAWIO))
1872		return -EPERM;
1873
1874	cp = memchr(buf, '\n', len);
1875	if (cp)
1876		len = cp - buf;
1877
1878	mode = kstrndup(buf, len, GFP_KERNEL);
1879	if (!mode)
1880		return -ENOMEM;
1881
1882	i = match_string(msc_mode, ARRAY_SIZE(msc_mode), mode);
1883	if (i >= 0) {
1884		kfree(mode);
1885		goto found;
1886	}
1887
1888	/* Buffer sinks only work with a usable IRQ */
1889	if (!msc->do_irq) {
1890		kfree(mode);
1891		return -EINVAL;
1892	}
1893
1894	mbuf = msu_buffer_get(mode);
1895	kfree(mode);
1896	if (mbuf)
1897		goto found;
1898
1899	return -EINVAL;
1900
1901found:
1902	if (i == MSC_MODE_MULTI && msc->multi_is_broken)
1903		return -EOPNOTSUPP;
1904
1905	mutex_lock(&msc->buf_mutex);
1906	ret = 0;
1907
1908	/* Same buffer: do nothing */
1909	if (mbuf && mbuf == msc->mbuf) {
1910		/* put the extra reference we just got */
1911		msu_buffer_put(mbuf);
1912		goto unlock;
1913	}
1914
1915	ret = msc_buffer_unlocked_free_unless_used(msc);
1916	if (ret)
1917		goto unlock;
1918
1919	if (mbuf) {
1920		void *mbuf_priv = mbuf->assign(dev, &i);
1921
1922		if (!mbuf_priv) {
1923			ret = -ENOMEM;
1924			goto unlock;
1925		}
1926
1927		msc_buffer_unassign(msc);
1928		msc->mbuf_priv = mbuf_priv;
1929		msc->mbuf = mbuf;
1930	} else {
1931		msc_buffer_unassign(msc);
1932	}
1933
1934	msc->mode = i;
1935
1936unlock:
1937	if (ret && mbuf)
1938		msu_buffer_put(mbuf);
1939	mutex_unlock(&msc->buf_mutex);
1940
1941	return ret ? ret : size;
1942}
1943
1944static DEVICE_ATTR_RW(mode);
1945
1946static ssize_t
1947nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1948{
1949	struct msc *msc = dev_get_drvdata(dev);
1950	struct msc_window *win;
1951	size_t count = 0;
1952
1953	mutex_lock(&msc->buf_mutex);
1954
1955	if (msc->mode == MSC_MODE_SINGLE)
1956		count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1957	else if (msc->mode == MSC_MODE_MULTI) {
1958		list_for_each_entry(win, &msc->win_list, entry) {
1959			count += scnprintf(buf + count, PAGE_SIZE - count,
1960					   "%d%c", win->nr_blocks,
1961					   msc_is_last_win(win) ? '\n' : ',');
1962		}
1963	} else {
1964		count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1965	}
1966
1967	mutex_unlock(&msc->buf_mutex);
1968
1969	return count;
1970}
1971
1972static ssize_t
1973nr_pages_store(struct device *dev, struct device_attribute *attr,
1974	       const char *buf, size_t size)
1975{
1976	struct msc *msc = dev_get_drvdata(dev);
1977	unsigned long val, *win = NULL, *rewin;
1978	size_t len = size;
1979	const char *p = buf;
1980	char *end, *s;
1981	int ret, nr_wins = 0;
1982
1983	if (!capable(CAP_SYS_RAWIO))
1984		return -EPERM;
1985
1986	ret = msc_buffer_free_unless_used(msc);
1987	if (ret)
1988		return ret;
1989
1990	/* scan the comma-separated list of allocation sizes */
1991	end = memchr(buf, '\n', len);
1992	if (end)
1993		len = end - buf;
1994
1995	do {
1996		end = memchr(p, ',', len);
1997		s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1998		if (!s) {
1999			ret = -ENOMEM;
2000			goto free_win;
2001		}
2002
2003		ret = kstrtoul(s, 10, &val);
2004		kfree(s);
2005
2006		if (ret || !val)
2007			goto free_win;
2008
2009		if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
2010			ret = -EINVAL;
2011			goto free_win;
2012		}
2013
2014		nr_wins++;
2015		rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
2016		if (!rewin) {
2017			kfree(win);
2018			return -ENOMEM;
2019		}
2020
2021		win = rewin;
2022		win[nr_wins - 1] = val;
2023
2024		if (!end)
2025			break;
2026
2027		/* consume the number and the following comma, hence +1 */
2028		len -= end - p + 1;
2029		p = end + 1;
2030	} while (len);
2031
2032	mutex_lock(&msc->buf_mutex);
2033	ret = msc_buffer_alloc(msc, win, nr_wins);
2034	mutex_unlock(&msc->buf_mutex);
2035
2036free_win:
2037	kfree(win);
2038
2039	return ret ? ret : size;
2040}
2041
2042static DEVICE_ATTR_RW(nr_pages);
2043
2044static ssize_t
2045win_switch_store(struct device *dev, struct device_attribute *attr,
2046		 const char *buf, size_t size)
2047{
2048	struct msc *msc = dev_get_drvdata(dev);
2049	unsigned long val;
2050	int ret;
2051
2052	ret = kstrtoul(buf, 10, &val);
2053	if (ret)
2054		return ret;
2055
2056	if (val != 1)
2057		return -EINVAL;
2058
2059	ret = -EINVAL;
2060	mutex_lock(&msc->buf_mutex);
2061	/*
2062	 * Window switch can only happen in the "multi" mode.
2063	 * If a external buffer is engaged, they have the full
2064	 * control over window switching.
2065	 */
2066	if (msc->mode == MSC_MODE_MULTI && !msc->mbuf)
2067		ret = msc_win_switch(msc);
2068	mutex_unlock(&msc->buf_mutex);
2069
2070	return ret ? ret : size;
2071}
2072
2073static DEVICE_ATTR_WO(win_switch);
2074
2075static ssize_t stop_on_full_show(struct device *dev,
2076				 struct device_attribute *attr, char *buf)
2077{
2078	struct msc *msc = dev_get_drvdata(dev);
2079
2080	return sprintf(buf, "%d\n", msc->stop_on_full);
2081}
2082
2083static ssize_t stop_on_full_store(struct device *dev,
2084				  struct device_attribute *attr,
2085				  const char *buf, size_t size)
2086{
2087	struct msc *msc = dev_get_drvdata(dev);
2088	int ret;
2089
2090	ret = kstrtobool(buf, &msc->stop_on_full);
2091	if (ret)
2092		return ret;
2093
2094	return size;
2095}
2096
2097static DEVICE_ATTR_RW(stop_on_full);
2098
2099static struct attribute *msc_output_attrs[] = {
2100	&dev_attr_wrap.attr,
2101	&dev_attr_mode.attr,
2102	&dev_attr_nr_pages.attr,
2103	&dev_attr_win_switch.attr,
2104	&dev_attr_stop_on_full.attr,
2105	NULL,
2106};
2107
2108static struct attribute_group msc_output_group = {
2109	.attrs	= msc_output_attrs,
2110};
2111
2112static int intel_th_msc_probe(struct intel_th_device *thdev)
2113{
2114	struct device *dev = &thdev->dev;
2115	struct resource *res;
2116	struct msc *msc;
2117	void __iomem *base;
2118	int err;
2119
2120	res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
2121	if (!res)
2122		return -ENODEV;
2123
2124	base = devm_ioremap(dev, res->start, resource_size(res));
2125	if (!base)
2126		return -ENOMEM;
2127
2128	msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
2129	if (!msc)
2130		return -ENOMEM;
2131
2132	res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
2133	if (!res)
2134		msc->do_irq = 1;
2135
2136	if (INTEL_TH_CAP(to_intel_th(thdev), multi_is_broken))
2137		msc->multi_is_broken = 1;
2138
2139	msc->index = thdev->id;
2140
2141	msc->thdev = thdev;
2142	msc->reg_base = base + msc->index * 0x100;
2143	msc->msu_base = base;
2144
2145	INIT_WORK(&msc->work, msc_work);
2146	err = intel_th_msc_init(msc);
2147	if (err)
2148		return err;
2149
2150	dev_set_drvdata(dev, msc);
2151
2152	return 0;
2153}
2154
2155static void intel_th_msc_remove(struct intel_th_device *thdev)
2156{
2157	struct msc *msc = dev_get_drvdata(&thdev->dev);
2158	int ret;
2159
2160	intel_th_msc_deactivate(thdev);
2161
2162	/*
2163	 * Buffers should not be used at this point except if the
2164	 * output character device is still open and the parent
2165	 * device gets detached from its bus, which is a FIXME.
2166	 */
2167	ret = msc_buffer_free_unless_used(msc);
2168	WARN_ON_ONCE(ret);
2169}
2170
2171static struct intel_th_driver intel_th_msc_driver = {
2172	.probe	= intel_th_msc_probe,
2173	.remove	= intel_th_msc_remove,
2174	.irq		= intel_th_msc_interrupt,
2175	.wait_empty	= intel_th_msc_wait_empty,
2176	.activate	= intel_th_msc_activate,
2177	.deactivate	= intel_th_msc_deactivate,
2178	.fops	= &intel_th_msc_fops,
2179	.attr_group	= &msc_output_group,
2180	.driver	= {
2181		.name	= "msc",
2182		.owner	= THIS_MODULE,
2183	},
2184};
2185
2186module_driver(intel_th_msc_driver,
2187	      intel_th_driver_register,
2188	      intel_th_driver_unregister);
2189
2190MODULE_LICENSE("GPL v2");
2191MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
2192MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");
2193