• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
42 struct 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  */
54 struct 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  */
75 struct 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  * @single_sgt:		single mode buffer
94  * @nr_pages:		total number of pages allocated for this buffer
95  * @single_sz:		amount of data in single mode
96  * @single_wrap:	single mode wrap occurred
97  * @base:		buffer's base pointer
98  * @base_addr:		buffer's base address
99  * @user_count:		number of users of the buffer
100  * @mmap_count:		number of mappings
101  * @buf_mutex:		mutex to serialize access to buffer-related bits
102 
103  * @enabled:		MSC is enabled
104  * @wrap:		wrapping is enabled
105  * @mode:		MSC operating mode
106  * @burst_len:		write burst length
107  * @index:		number of this MSC in the MSU
108  */
109 struct msc {
110 	void __iomem		*reg_base;
111 	struct intel_th_device	*thdev;
112 
113 	struct list_head	win_list;
114 	struct sg_table		single_sgt;
115 	unsigned long		nr_pages;
116 	unsigned long		single_sz;
117 	unsigned int		single_wrap : 1;
118 	void			*base;
119 	dma_addr_t		base_addr;
120 
121 	/* <0: no buffer, 0: no users, >0: active users */
122 	atomic_t		user_count;
123 
124 	atomic_t		mmap_count;
125 	struct mutex		buf_mutex;
126 
127 	struct mutex		iter_mutex;
128 	struct list_head	iter_list;
129 
130 	/* config */
131 	unsigned int		enabled : 1,
132 				wrap	: 1;
133 	unsigned int		mode;
134 	unsigned int		burst_len;
135 	unsigned int		index;
136 };
137 
msc_block_is_empty(struct msc_block_desc * bdesc)138 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
139 {
140 	/* header hasn't been written */
141 	if (!bdesc->valid_dw)
142 		return true;
143 
144 	/* valid_dw includes the header */
145 	if (!msc_data_sz(bdesc))
146 		return true;
147 
148 	return false;
149 }
150 
151 /**
152  * msc_oldest_window() - locate the window with oldest data
153  * @msc:	MSC device
154  *
155  * This should only be used in multiblock mode. Caller should hold the
156  * msc::user_count reference.
157  *
158  * Return:	the oldest window with valid data
159  */
msc_oldest_window(struct msc * msc)160 static struct msc_window *msc_oldest_window(struct msc *msc)
161 {
162 	struct msc_window *win;
163 	u32 reg = ioread32(msc->reg_base + REG_MSU_MSC0NWSA);
164 	unsigned long win_addr = (unsigned long)reg << PAGE_SHIFT;
165 	unsigned int found = 0;
166 
167 	if (list_empty(&msc->win_list))
168 		return NULL;
169 
170 	/*
171 	 * we might need a radix tree for this, depending on how
172 	 * many windows a typical user would allocate; ideally it's
173 	 * something like 2, in which case we're good
174 	 */
175 	list_for_each_entry(win, &msc->win_list, entry) {
176 		if (win->block[0].addr == win_addr)
177 			found++;
178 
179 		/* skip the empty ones */
180 		if (msc_block_is_empty(win->block[0].bdesc))
181 			continue;
182 
183 		if (found)
184 			return win;
185 	}
186 
187 	return list_entry(msc->win_list.next, struct msc_window, entry);
188 }
189 
190 /**
191  * msc_win_oldest_block() - locate the oldest block in a given window
192  * @win:	window to look at
193  *
194  * Return:	index of the block with the oldest data
195  */
msc_win_oldest_block(struct msc_window * win)196 static unsigned int msc_win_oldest_block(struct msc_window *win)
197 {
198 	unsigned int blk;
199 	struct msc_block_desc *bdesc = win->block[0].bdesc;
200 
201 	/* without wrapping, first block is the oldest */
202 	if (!msc_block_wrapped(bdesc))
203 		return 0;
204 
205 	/*
206 	 * with wrapping, last written block contains both the newest and the
207 	 * oldest data for this window.
208 	 */
209 	for (blk = 0; blk < win->nr_blocks; blk++) {
210 		bdesc = win->block[blk].bdesc;
211 
212 		if (msc_block_last_written(bdesc))
213 			return blk;
214 	}
215 
216 	return 0;
217 }
218 
219 /**
220  * msc_is_last_win() - check if a window is the last one for a given MSC
221  * @win:	window
222  * Return:	true if @win is the last window in MSC's multiblock buffer
223  */
msc_is_last_win(struct msc_window * win)224 static inline bool msc_is_last_win(struct msc_window *win)
225 {
226 	return win->entry.next == &win->msc->win_list;
227 }
228 
229 /**
230  * msc_next_window() - return next window in the multiblock buffer
231  * @win:	current window
232  *
233  * Return:	window following the current one
234  */
msc_next_window(struct msc_window * win)235 static struct msc_window *msc_next_window(struct msc_window *win)
236 {
237 	if (msc_is_last_win(win))
238 		return list_entry(win->msc->win_list.next, struct msc_window,
239 				  entry);
240 
241 	return list_entry(win->entry.next, struct msc_window, entry);
242 }
243 
msc_iter_bdesc(struct msc_iter * iter)244 static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
245 {
246 	return iter->win->block[iter->block].bdesc;
247 }
248 
msc_iter_init(struct msc_iter * iter)249 static void msc_iter_init(struct msc_iter *iter)
250 {
251 	memset(iter, 0, sizeof(*iter));
252 	iter->start_block = -1;
253 	iter->block = -1;
254 }
255 
msc_iter_install(struct msc * msc)256 static struct msc_iter *msc_iter_install(struct msc *msc)
257 {
258 	struct msc_iter *iter;
259 
260 	iter = kzalloc(sizeof(*iter), GFP_KERNEL);
261 	if (!iter)
262 		return NULL;
263 
264 	msc_iter_init(iter);
265 	iter->msc = msc;
266 
267 	mutex_lock(&msc->iter_mutex);
268 	list_add_tail(&iter->entry, &msc->iter_list);
269 	mutex_unlock(&msc->iter_mutex);
270 
271 	return iter;
272 }
273 
msc_iter_remove(struct msc_iter * iter,struct msc * msc)274 static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
275 {
276 	mutex_lock(&msc->iter_mutex);
277 	list_del(&iter->entry);
278 	mutex_unlock(&msc->iter_mutex);
279 
280 	kfree(iter);
281 }
282 
msc_iter_block_start(struct msc_iter * iter)283 static void msc_iter_block_start(struct msc_iter *iter)
284 {
285 	if (iter->start_block != -1)
286 		return;
287 
288 	iter->start_block = msc_win_oldest_block(iter->win);
289 	iter->block = iter->start_block;
290 	iter->wrap_count = 0;
291 
292 	/*
293 	 * start with the block with oldest data; if data has wrapped
294 	 * in this window, it should be in this block
295 	 */
296 	if (msc_block_wrapped(msc_iter_bdesc(iter)))
297 		iter->wrap_count = 2;
298 
299 }
300 
msc_iter_win_start(struct msc_iter * iter,struct msc * msc)301 static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
302 {
303 	/* already started, nothing to do */
304 	if (iter->start_win)
305 		return 0;
306 
307 	iter->start_win = msc_oldest_window(msc);
308 	if (!iter->start_win)
309 		return -EINVAL;
310 
311 	iter->win = iter->start_win;
312 	iter->start_block = -1;
313 
314 	msc_iter_block_start(iter);
315 
316 	return 0;
317 }
318 
msc_iter_win_advance(struct msc_iter * iter)319 static int msc_iter_win_advance(struct msc_iter *iter)
320 {
321 	iter->win = msc_next_window(iter->win);
322 	iter->start_block = -1;
323 
324 	if (iter->win == iter->start_win) {
325 		iter->eof++;
326 		return 1;
327 	}
328 
329 	msc_iter_block_start(iter);
330 
331 	return 0;
332 }
333 
msc_iter_block_advance(struct msc_iter * iter)334 static int msc_iter_block_advance(struct msc_iter *iter)
335 {
336 	iter->block_off = 0;
337 
338 	/* wrapping */
339 	if (iter->wrap_count && iter->block == iter->start_block) {
340 		iter->wrap_count--;
341 		if (!iter->wrap_count)
342 			/* copied newest data from the wrapped block */
343 			return msc_iter_win_advance(iter);
344 	}
345 
346 	/* no wrapping, check for last written block */
347 	if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
348 		/* copied newest data for the window */
349 		return msc_iter_win_advance(iter);
350 
351 	/* block advance */
352 	if (++iter->block == iter->win->nr_blocks)
353 		iter->block = 0;
354 
355 	/* no wrapping, sanity check in case there is no last written block */
356 	if (!iter->wrap_count && iter->block == iter->start_block)
357 		return msc_iter_win_advance(iter);
358 
359 	return 0;
360 }
361 
362 /**
363  * msc_buffer_iterate() - go through multiblock buffer's data
364  * @iter:	iterator structure
365  * @size:	amount of data to scan
366  * @data:	callback's private data
367  * @fn:		iterator callback
368  *
369  * This will start at the window which will be written to next (containing
370  * the oldest data) and work its way to the current window, calling @fn
371  * for each chunk of data as it goes.
372  *
373  * Caller should have msc::user_count reference to make sure the buffer
374  * doesn't disappear from under us.
375  *
376  * Return:	amount of data actually scanned.
377  */
378 static ssize_t
msc_buffer_iterate(struct msc_iter * iter,size_t size,void * data,unsigned long (* fn)(void *,void *,size_t))379 msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
380 		   unsigned long (*fn)(void *, void *, size_t))
381 {
382 	struct msc *msc = iter->msc;
383 	size_t len = size;
384 	unsigned int advance;
385 
386 	if (iter->eof)
387 		return 0;
388 
389 	/* start with the oldest window */
390 	if (msc_iter_win_start(iter, msc))
391 		return 0;
392 
393 	do {
394 		unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
395 		void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
396 		size_t tocopy = data_bytes, copied = 0;
397 		size_t remaining = 0;
398 
399 		advance = 1;
400 
401 		/*
402 		 * If block wrapping happened, we need to visit the last block
403 		 * twice, because it contains both the oldest and the newest
404 		 * data in this window.
405 		 *
406 		 * First time (wrap_count==2), in the very beginning, to collect
407 		 * the oldest data, which is in the range
408 		 * (data_bytes..DATA_IN_PAGE).
409 		 *
410 		 * Second time (wrap_count==1), it's just like any other block,
411 		 * containing data in the range of [MSC_BDESC..data_bytes].
412 		 */
413 		if (iter->block == iter->start_block && iter->wrap_count) {
414 			tocopy = DATA_IN_PAGE - data_bytes;
415 			src += data_bytes;
416 		}
417 
418 		if (!tocopy)
419 			goto next_block;
420 
421 		tocopy -= iter->block_off;
422 		src += iter->block_off;
423 
424 		if (len < tocopy) {
425 			tocopy = len;
426 			advance = 0;
427 		}
428 
429 		remaining = fn(data, src, tocopy);
430 
431 		if (remaining)
432 			advance = 0;
433 
434 		copied = tocopy - remaining;
435 		len -= copied;
436 		iter->block_off += copied;
437 		iter->offset += copied;
438 
439 		if (!advance)
440 			break;
441 
442 next_block:
443 		if (msc_iter_block_advance(iter))
444 			break;
445 
446 	} while (len);
447 
448 	return size - len;
449 }
450 
451 /**
452  * msc_buffer_clear_hw_header() - clear hw header for multiblock
453  * @msc:	MSC device
454  */
msc_buffer_clear_hw_header(struct msc * msc)455 static void msc_buffer_clear_hw_header(struct msc *msc)
456 {
457 	struct msc_window *win;
458 
459 	mutex_lock(&msc->buf_mutex);
460 	list_for_each_entry(win, &msc->win_list, entry) {
461 		unsigned int blk;
462 		size_t hw_sz = sizeof(struct msc_block_desc) -
463 			offsetof(struct msc_block_desc, hw_tag);
464 
465 		for (blk = 0; blk < win->nr_blocks; blk++) {
466 			struct msc_block_desc *bdesc = win->block[blk].bdesc;
467 
468 			memset(&bdesc->hw_tag, 0, hw_sz);
469 		}
470 	}
471 	mutex_unlock(&msc->buf_mutex);
472 }
473 
474 /**
475  * msc_configure() - set up MSC hardware
476  * @msc:	the MSC device to configure
477  *
478  * Program storage mode, wrapping, burst length and trace buffer address
479  * into a given MSC. If msc::enabled is set, enable the trace, too.
480  */
msc_configure(struct msc * msc)481 static int msc_configure(struct msc *msc)
482 {
483 	u32 reg;
484 
485 	if (msc->mode > MSC_MODE_MULTI)
486 		return -EINVAL;
487 
488 	if (msc->mode == MSC_MODE_MULTI)
489 		msc_buffer_clear_hw_header(msc);
490 
491 	reg = msc->base_addr >> PAGE_SHIFT;
492 	iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
493 
494 	if (msc->mode == MSC_MODE_SINGLE) {
495 		reg = msc->nr_pages;
496 		iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
497 	}
498 
499 	reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
500 	reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
501 
502 	reg |= msc->mode << __ffs(MSC_MODE);
503 	reg |= msc->burst_len << __ffs(MSC_LEN);
504 	/*if (msc->mode == MSC_MODE_MULTI)
505 	  reg |= MSC_RD_HDR_OVRD; */
506 	if (msc->wrap)
507 		reg |= MSC_WRAPEN;
508 	if (msc->enabled)
509 		reg |= MSC_EN;
510 
511 	iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
512 
513 	if (msc->enabled) {
514 		msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
515 		intel_th_trace_enable(msc->thdev);
516 	}
517 
518 	return 0;
519 }
520 
521 /**
522  * msc_disable() - disable MSC hardware
523  * @msc:	MSC device to disable
524  *
525  * If @msc is enabled, disable tracing on the switch and then disable MSC
526  * storage.
527  */
msc_disable(struct msc * msc)528 static void msc_disable(struct msc *msc)
529 {
530 	unsigned long count;
531 	u32 reg;
532 
533 	if (!msc->enabled)
534 		return;
535 
536 	intel_th_trace_disable(msc->thdev);
537 
538 	for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
539 	     count && !(reg & MSCSTS_PLE); count--) {
540 		reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
541 		cpu_relax();
542 	}
543 
544 	if (!count)
545 		dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
546 
547 	if (msc->mode == MSC_MODE_SINGLE) {
548 		msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
549 
550 		reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
551 		msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
552 		dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
553 			reg, msc->single_sz, msc->single_wrap);
554 	}
555 
556 	reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
557 	reg &= ~MSC_EN;
558 	iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
559 	msc->enabled = 0;
560 
561 	iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
562 	iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
563 
564 	dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
565 		ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
566 
567 	reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
568 	dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
569 }
570 
intel_th_msc_activate(struct intel_th_device * thdev)571 static int intel_th_msc_activate(struct intel_th_device *thdev)
572 {
573 	struct msc *msc = dev_get_drvdata(&thdev->dev);
574 	int ret = 0;
575 
576 	if (!atomic_inc_unless_negative(&msc->user_count))
577 		return -ENODEV;
578 
579 	mutex_lock(&msc->iter_mutex);
580 	if (!list_empty(&msc->iter_list))
581 		ret = -EBUSY;
582 	mutex_unlock(&msc->iter_mutex);
583 
584 	if (ret) {
585 		atomic_dec(&msc->user_count);
586 		return ret;
587 	}
588 
589 	msc->enabled = 1;
590 
591 	return msc_configure(msc);
592 }
593 
intel_th_msc_deactivate(struct intel_th_device * thdev)594 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
595 {
596 	struct msc *msc = dev_get_drvdata(&thdev->dev);
597 
598 	msc_disable(msc);
599 
600 	atomic_dec(&msc->user_count);
601 }
602 
603 /**
604  * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
605  * @msc:	MSC device
606  * @size:	allocation size in bytes
607  *
608  * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
609  * caller is expected to hold it.
610  *
611  * Return:	0 on success, -errno otherwise.
612  */
msc_buffer_contig_alloc(struct msc * msc,unsigned long size)613 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
614 {
615 	unsigned long nr_pages = size >> PAGE_SHIFT;
616 	unsigned int order = get_order(size);
617 	struct page *page;
618 	int ret;
619 
620 	if (!size)
621 		return 0;
622 
623 	ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
624 	if (ret)
625 		goto err_out;
626 
627 	ret = -ENOMEM;
628 	page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
629 	if (!page)
630 		goto err_free_sgt;
631 
632 	split_page(page, order);
633 	sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
634 
635 	ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
636 			 DMA_FROM_DEVICE);
637 	if (ret < 0)
638 		goto err_free_pages;
639 
640 	msc->nr_pages = nr_pages;
641 	msc->base = page_address(page);
642 	msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
643 
644 	return 0;
645 
646 err_free_pages:
647 	__free_pages(page, order);
648 
649 err_free_sgt:
650 	sg_free_table(&msc->single_sgt);
651 
652 err_out:
653 	return ret;
654 }
655 
656 /**
657  * msc_buffer_contig_free() - free a contiguous buffer
658  * @msc:	MSC configured in SINGLE mode
659  */
msc_buffer_contig_free(struct msc * msc)660 static void msc_buffer_contig_free(struct msc *msc)
661 {
662 	unsigned long off;
663 
664 	dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
665 		     1, DMA_FROM_DEVICE);
666 	sg_free_table(&msc->single_sgt);
667 
668 	for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
669 		struct page *page = virt_to_page(msc->base + off);
670 
671 		page->mapping = NULL;
672 		__free_page(page);
673 	}
674 
675 	msc->nr_pages = 0;
676 }
677 
678 /**
679  * msc_buffer_contig_get_page() - find a page at a given offset
680  * @msc:	MSC configured in SINGLE mode
681  * @pgoff:	page offset
682  *
683  * Return:	page, if @pgoff is within the range, NULL otherwise.
684  */
msc_buffer_contig_get_page(struct msc * msc,unsigned long pgoff)685 static struct page *msc_buffer_contig_get_page(struct msc *msc,
686 					       unsigned long pgoff)
687 {
688 	if (pgoff >= msc->nr_pages)
689 		return NULL;
690 
691 	return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
692 }
693 
694 /**
695  * msc_buffer_win_alloc() - alloc a window for a multiblock mode
696  * @msc:	MSC device
697  * @nr_blocks:	number of pages in this window
698  *
699  * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
700  * to serialize, so the caller is expected to hold it.
701  *
702  * Return:	0 on success, -errno otherwise.
703  */
msc_buffer_win_alloc(struct msc * msc,unsigned int nr_blocks)704 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
705 {
706 	struct msc_window *win;
707 	unsigned long size = PAGE_SIZE;
708 	int i, ret = -ENOMEM;
709 
710 	if (!nr_blocks)
711 		return 0;
712 
713 	win = kzalloc(offsetof(struct msc_window, block[nr_blocks]),
714 		      GFP_KERNEL);
715 	if (!win)
716 		return -ENOMEM;
717 
718 	if (!list_empty(&msc->win_list)) {
719 		struct msc_window *prev = list_entry(msc->win_list.prev,
720 						     struct msc_window, entry);
721 
722 		win->pgoff = prev->pgoff + prev->nr_blocks;
723 	}
724 
725 	for (i = 0; i < nr_blocks; i++) {
726 		win->block[i].bdesc = dma_alloc_coherent(msc_dev(msc), size,
727 							 &win->block[i].addr,
728 							 GFP_KERNEL);
729 
730 #ifdef CONFIG_X86
731 		/* Set the page as uncached */
732 		set_memory_uc((unsigned long)win->block[i].bdesc, 1);
733 #endif
734 
735 		if (!win->block[i].bdesc)
736 			goto err_nomem;
737 	}
738 
739 	win->msc = msc;
740 	win->nr_blocks = nr_blocks;
741 
742 	if (list_empty(&msc->win_list)) {
743 		msc->base = win->block[0].bdesc;
744 		msc->base_addr = win->block[0].addr;
745 	}
746 
747 	list_add_tail(&win->entry, &msc->win_list);
748 	msc->nr_pages += nr_blocks;
749 
750 	return 0;
751 
752 err_nomem:
753 	for (i--; i >= 0; i--) {
754 #ifdef CONFIG_X86
755 		/* Reset the page to write-back before releasing */
756 		set_memory_wb((unsigned long)win->block[i].bdesc, 1);
757 #endif
758 		dma_free_coherent(msc_dev(msc), size, win->block[i].bdesc,
759 				  win->block[i].addr);
760 	}
761 	kfree(win);
762 
763 	return ret;
764 }
765 
766 /**
767  * msc_buffer_win_free() - free a window from MSC's window list
768  * @msc:	MSC device
769  * @win:	window to free
770  *
771  * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
772  * to serialize, so the caller is expected to hold it.
773  */
msc_buffer_win_free(struct msc * msc,struct msc_window * win)774 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
775 {
776 	int i;
777 
778 	msc->nr_pages -= win->nr_blocks;
779 
780 	list_del(&win->entry);
781 	if (list_empty(&msc->win_list)) {
782 		msc->base = NULL;
783 		msc->base_addr = 0;
784 	}
785 
786 	for (i = 0; i < win->nr_blocks; i++) {
787 		struct page *page = virt_to_page(win->block[i].bdesc);
788 
789 		page->mapping = NULL;
790 #ifdef CONFIG_X86
791 		/* Reset the page to write-back before releasing */
792 		set_memory_wb((unsigned long)win->block[i].bdesc, 1);
793 #endif
794 		dma_free_coherent(msc_dev(win->msc), PAGE_SIZE,
795 				  win->block[i].bdesc, win->block[i].addr);
796 	}
797 
798 	kfree(win);
799 }
800 
801 /**
802  * msc_buffer_relink() - set up block descriptors for multiblock mode
803  * @msc:	MSC device
804  *
805  * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
806  * so the caller is expected to hold it.
807  */
msc_buffer_relink(struct msc * msc)808 static void msc_buffer_relink(struct msc *msc)
809 {
810 	struct msc_window *win, *next_win;
811 
812 	/* call with msc::mutex locked */
813 	list_for_each_entry(win, &msc->win_list, entry) {
814 		unsigned int blk;
815 		u32 sw_tag = 0;
816 
817 		/*
818 		 * Last window's next_win should point to the first window
819 		 * and MSC_SW_TAG_LASTWIN should be set.
820 		 */
821 		if (msc_is_last_win(win)) {
822 			sw_tag |= MSC_SW_TAG_LASTWIN;
823 			next_win = list_entry(msc->win_list.next,
824 					      struct msc_window, entry);
825 		} else {
826 			next_win = list_entry(win->entry.next,
827 					      struct msc_window, entry);
828 		}
829 
830 		for (blk = 0; blk < win->nr_blocks; blk++) {
831 			struct msc_block_desc *bdesc = win->block[blk].bdesc;
832 
833 			memset(bdesc, 0, sizeof(*bdesc));
834 
835 			bdesc->next_win = next_win->block[0].addr >> PAGE_SHIFT;
836 
837 			/*
838 			 * Similarly to last window, last block should point
839 			 * to the first one.
840 			 */
841 			if (blk == win->nr_blocks - 1) {
842 				sw_tag |= MSC_SW_TAG_LASTBLK;
843 				bdesc->next_blk =
844 					win->block[0].addr >> PAGE_SHIFT;
845 			} else {
846 				bdesc->next_blk =
847 					win->block[blk + 1].addr >> PAGE_SHIFT;
848 			}
849 
850 			bdesc->sw_tag = sw_tag;
851 			bdesc->block_sz = PAGE_SIZE / 64;
852 		}
853 	}
854 
855 	/*
856 	 * Make the above writes globally visible before tracing is
857 	 * enabled to make sure hardware sees them coherently.
858 	 */
859 	wmb();
860 }
861 
msc_buffer_multi_free(struct msc * msc)862 static void msc_buffer_multi_free(struct msc *msc)
863 {
864 	struct msc_window *win, *iter;
865 
866 	list_for_each_entry_safe(win, iter, &msc->win_list, entry)
867 		msc_buffer_win_free(msc, win);
868 }
869 
msc_buffer_multi_alloc(struct msc * msc,unsigned long * nr_pages,unsigned int nr_wins)870 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
871 				  unsigned int nr_wins)
872 {
873 	int ret, i;
874 
875 	for (i = 0; i < nr_wins; i++) {
876 		ret = msc_buffer_win_alloc(msc, nr_pages[i]);
877 		if (ret) {
878 			msc_buffer_multi_free(msc);
879 			return ret;
880 		}
881 	}
882 
883 	msc_buffer_relink(msc);
884 
885 	return 0;
886 }
887 
888 /**
889  * msc_buffer_free() - free buffers for MSC
890  * @msc:	MSC device
891  *
892  * Free MSC's storage buffers.
893  *
894  * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
895  * serialize, so the caller is expected to hold it.
896  */
msc_buffer_free(struct msc * msc)897 static void msc_buffer_free(struct msc *msc)
898 {
899 	if (msc->mode == MSC_MODE_SINGLE)
900 		msc_buffer_contig_free(msc);
901 	else if (msc->mode == MSC_MODE_MULTI)
902 		msc_buffer_multi_free(msc);
903 }
904 
905 /**
906  * msc_buffer_alloc() - allocate a buffer for MSC
907  * @msc:	MSC device
908  * @size:	allocation size in bytes
909  *
910  * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
911  * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
912  * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
913  * window per invocation, so in multiblock mode this can be called multiple
914  * times for the same MSC to allocate multiple windows.
915  *
916  * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
917  * to serialize, so the caller is expected to hold it.
918  *
919  * Return:	0 on success, -errno otherwise.
920  */
msc_buffer_alloc(struct msc * msc,unsigned long * nr_pages,unsigned int nr_wins)921 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
922 			    unsigned int nr_wins)
923 {
924 	int ret;
925 
926 	/* -1: buffer not allocated */
927 	if (atomic_read(&msc->user_count) != -1)
928 		return -EBUSY;
929 
930 	if (msc->mode == MSC_MODE_SINGLE) {
931 		if (nr_wins != 1)
932 			return -EINVAL;
933 
934 		ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
935 	} else if (msc->mode == MSC_MODE_MULTI) {
936 		ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
937 	} else {
938 		ret = -EINVAL;
939 	}
940 
941 	if (!ret) {
942 		/* allocation should be visible before the counter goes to 0 */
943 		smp_mb__before_atomic();
944 
945 		if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
946 			return -EINVAL;
947 	}
948 
949 	return ret;
950 }
951 
952 /**
953  * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
954  * @msc:	MSC device
955  *
956  * This will free MSC buffer unless it is in use or there is no allocated
957  * buffer.
958  * Caller needs to hold msc::buf_mutex.
959  *
960  * Return:	0 on successful deallocation or if there was no buffer to
961  *		deallocate, -EBUSY if there are active users.
962  */
msc_buffer_unlocked_free_unless_used(struct msc * msc)963 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
964 {
965 	int count, ret = 0;
966 
967 	count = atomic_cmpxchg(&msc->user_count, 0, -1);
968 
969 	/* > 0: buffer is allocated and has users */
970 	if (count > 0)
971 		ret = -EBUSY;
972 	/* 0: buffer is allocated, no users */
973 	else if (!count)
974 		msc_buffer_free(msc);
975 	/* < 0: no buffer, nothing to do */
976 
977 	return ret;
978 }
979 
980 /**
981  * msc_buffer_free_unless_used() - free a buffer unless it's in use
982  * @msc:	MSC device
983  *
984  * This is a locked version of msc_buffer_unlocked_free_unless_used().
985  */
msc_buffer_free_unless_used(struct msc * msc)986 static int msc_buffer_free_unless_used(struct msc *msc)
987 {
988 	int ret;
989 
990 	mutex_lock(&msc->buf_mutex);
991 	ret = msc_buffer_unlocked_free_unless_used(msc);
992 	mutex_unlock(&msc->buf_mutex);
993 
994 	return ret;
995 }
996 
997 /**
998  * msc_buffer_get_page() - get MSC buffer page at a given offset
999  * @msc:	MSC device
1000  * @pgoff:	page offset into the storage buffer
1001  *
1002  * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1003  * the caller.
1004  *
1005  * Return:	page if @pgoff corresponds to a valid buffer page or NULL.
1006  */
msc_buffer_get_page(struct msc * msc,unsigned long pgoff)1007 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1008 {
1009 	struct msc_window *win;
1010 
1011 	if (msc->mode == MSC_MODE_SINGLE)
1012 		return msc_buffer_contig_get_page(msc, pgoff);
1013 
1014 	list_for_each_entry(win, &msc->win_list, entry)
1015 		if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1016 			goto found;
1017 
1018 	return NULL;
1019 
1020 found:
1021 	pgoff -= win->pgoff;
1022 	return virt_to_page(win->block[pgoff].bdesc);
1023 }
1024 
1025 /**
1026  * struct msc_win_to_user_struct - data for copy_to_user() callback
1027  * @buf:	userspace buffer to copy data to
1028  * @offset:	running offset
1029  */
1030 struct msc_win_to_user_struct {
1031 	char __user	*buf;
1032 	unsigned long	offset;
1033 };
1034 
1035 /**
1036  * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1037  * @data:	callback's private data
1038  * @src:	source buffer
1039  * @len:	amount of data to copy from the source buffer
1040  */
msc_win_to_user(void * data,void * src,size_t len)1041 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1042 {
1043 	struct msc_win_to_user_struct *u = data;
1044 	unsigned long ret;
1045 
1046 	ret = copy_to_user(u->buf + u->offset, src, len);
1047 	u->offset += len - ret;
1048 
1049 	return ret;
1050 }
1051 
1052 
1053 /*
1054  * file operations' callbacks
1055  */
1056 
intel_th_msc_open(struct inode * inode,struct file * file)1057 static int intel_th_msc_open(struct inode *inode, struct file *file)
1058 {
1059 	struct intel_th_device *thdev = file->private_data;
1060 	struct msc *msc = dev_get_drvdata(&thdev->dev);
1061 	struct msc_iter *iter;
1062 
1063 	if (!capable(CAP_SYS_RAWIO))
1064 		return -EPERM;
1065 
1066 	iter = msc_iter_install(msc);
1067 	if (!iter)
1068 		return -ENOMEM;
1069 
1070 	file->private_data = iter;
1071 
1072 	return nonseekable_open(inode, file);
1073 }
1074 
intel_th_msc_release(struct inode * inode,struct file * file)1075 static int intel_th_msc_release(struct inode *inode, struct file *file)
1076 {
1077 	struct msc_iter *iter = file->private_data;
1078 	struct msc *msc = iter->msc;
1079 
1080 	msc_iter_remove(iter, msc);
1081 
1082 	return 0;
1083 }
1084 
1085 static ssize_t
msc_single_to_user(struct msc * msc,char __user * buf,loff_t off,size_t len)1086 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1087 {
1088 	unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1089 	unsigned long start = off, tocopy = 0;
1090 
1091 	if (msc->single_wrap) {
1092 		start += msc->single_sz;
1093 		if (start < size) {
1094 			tocopy = min(rem, size - start);
1095 			if (copy_to_user(buf, msc->base + start, tocopy))
1096 				return -EFAULT;
1097 
1098 			buf += tocopy;
1099 			rem -= tocopy;
1100 			start += tocopy;
1101 		}
1102 
1103 		start &= size - 1;
1104 		if (rem) {
1105 			tocopy = min(rem, msc->single_sz - start);
1106 			if (copy_to_user(buf, msc->base + start, tocopy))
1107 				return -EFAULT;
1108 
1109 			rem -= tocopy;
1110 		}
1111 
1112 		return len - rem;
1113 	}
1114 
1115 	if (copy_to_user(buf, msc->base + start, rem))
1116 		return -EFAULT;
1117 
1118 	return len;
1119 }
1120 
intel_th_msc_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)1121 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1122 				 size_t len, loff_t *ppos)
1123 {
1124 	struct msc_iter *iter = file->private_data;
1125 	struct msc *msc = iter->msc;
1126 	size_t size;
1127 	loff_t off = *ppos;
1128 	ssize_t ret = 0;
1129 
1130 	if (!atomic_inc_unless_negative(&msc->user_count))
1131 		return 0;
1132 
1133 	if (msc->enabled) {
1134 		ret = -EBUSY;
1135 		goto put_count;
1136 	}
1137 
1138 	if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1139 		size = msc->single_sz;
1140 	else
1141 		size = msc->nr_pages << PAGE_SHIFT;
1142 
1143 	if (!size)
1144 		return 0;
1145 
1146 	if (off >= size) {
1147 		len = 0;
1148 		goto put_count;
1149 	}
1150 	if (off + len >= size)
1151 		len = size - off;
1152 
1153 	if (msc->mode == MSC_MODE_SINGLE) {
1154 		ret = msc_single_to_user(msc, buf, off, len);
1155 		if (ret >= 0)
1156 			*ppos += ret;
1157 	} else if (msc->mode == MSC_MODE_MULTI) {
1158 		struct msc_win_to_user_struct u = {
1159 			.buf	= buf,
1160 			.offset	= 0,
1161 		};
1162 
1163 		ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1164 		if (ret >= 0)
1165 			*ppos = iter->offset;
1166 	} else {
1167 		ret = -EINVAL;
1168 	}
1169 
1170 put_count:
1171 	atomic_dec(&msc->user_count);
1172 
1173 	return ret;
1174 }
1175 
1176 /*
1177  * vm operations callbacks (vm_ops)
1178  */
1179 
msc_mmap_open(struct vm_area_struct * vma)1180 static void msc_mmap_open(struct vm_area_struct *vma)
1181 {
1182 	struct msc_iter *iter = vma->vm_file->private_data;
1183 	struct msc *msc = iter->msc;
1184 
1185 	atomic_inc(&msc->mmap_count);
1186 }
1187 
msc_mmap_close(struct vm_area_struct * vma)1188 static void msc_mmap_close(struct vm_area_struct *vma)
1189 {
1190 	struct msc_iter *iter = vma->vm_file->private_data;
1191 	struct msc *msc = iter->msc;
1192 	unsigned long pg;
1193 
1194 	if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1195 		return;
1196 
1197 	/* drop page _counts */
1198 	for (pg = 0; pg < msc->nr_pages; pg++) {
1199 		struct page *page = msc_buffer_get_page(msc, pg);
1200 
1201 		if (WARN_ON_ONCE(!page))
1202 			continue;
1203 
1204 		if (page->mapping)
1205 			page->mapping = NULL;
1206 	}
1207 
1208 	/* last mapping -- drop user_count */
1209 	atomic_dec(&msc->user_count);
1210 	mutex_unlock(&msc->buf_mutex);
1211 }
1212 
msc_mmap_fault(struct vm_area_struct * vma,struct vm_fault * vmf)1213 static int msc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1214 {
1215 	struct msc_iter *iter = vma->vm_file->private_data;
1216 	struct msc *msc = iter->msc;
1217 
1218 	vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1219 	if (!vmf->page)
1220 		return VM_FAULT_SIGBUS;
1221 
1222 	get_page(vmf->page);
1223 	vmf->page->mapping = vma->vm_file->f_mapping;
1224 	vmf->page->index = vmf->pgoff;
1225 
1226 	return 0;
1227 }
1228 
1229 static const struct vm_operations_struct msc_mmap_ops = {
1230 	.open	= msc_mmap_open,
1231 	.close	= msc_mmap_close,
1232 	.fault	= msc_mmap_fault,
1233 };
1234 
intel_th_msc_mmap(struct file * file,struct vm_area_struct * vma)1235 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1236 {
1237 	unsigned long size = vma->vm_end - vma->vm_start;
1238 	struct msc_iter *iter = vma->vm_file->private_data;
1239 	struct msc *msc = iter->msc;
1240 	int ret = -EINVAL;
1241 
1242 	if (!size || offset_in_page(size))
1243 		return -EINVAL;
1244 
1245 	if (vma->vm_pgoff)
1246 		return -EINVAL;
1247 
1248 	/* grab user_count once per mmap; drop in msc_mmap_close() */
1249 	if (!atomic_inc_unless_negative(&msc->user_count))
1250 		return -EINVAL;
1251 
1252 	if (msc->mode != MSC_MODE_SINGLE &&
1253 	    msc->mode != MSC_MODE_MULTI)
1254 		goto out;
1255 
1256 	if (size >> PAGE_SHIFT != msc->nr_pages)
1257 		goto out;
1258 
1259 	atomic_set(&msc->mmap_count, 1);
1260 	ret = 0;
1261 
1262 out:
1263 	if (ret)
1264 		atomic_dec(&msc->user_count);
1265 
1266 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1267 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1268 	vma->vm_ops = &msc_mmap_ops;
1269 	return ret;
1270 }
1271 
1272 static const struct file_operations intel_th_msc_fops = {
1273 	.open		= intel_th_msc_open,
1274 	.release	= intel_th_msc_release,
1275 	.read		= intel_th_msc_read,
1276 	.mmap		= intel_th_msc_mmap,
1277 	.llseek		= no_llseek,
1278 };
1279 
intel_th_msc_init(struct msc * msc)1280 static int intel_th_msc_init(struct msc *msc)
1281 {
1282 	atomic_set(&msc->user_count, -1);
1283 
1284 	msc->mode = MSC_MODE_MULTI;
1285 	mutex_init(&msc->buf_mutex);
1286 	INIT_LIST_HEAD(&msc->win_list);
1287 
1288 	mutex_init(&msc->iter_mutex);
1289 	INIT_LIST_HEAD(&msc->iter_list);
1290 
1291 	msc->burst_len =
1292 		(ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1293 		__ffs(MSC_LEN);
1294 
1295 	return 0;
1296 }
1297 
1298 static const char * const msc_mode[] = {
1299 	[MSC_MODE_SINGLE]	= "single",
1300 	[MSC_MODE_MULTI]	= "multi",
1301 	[MSC_MODE_EXI]		= "ExI",
1302 	[MSC_MODE_DEBUG]	= "debug",
1303 };
1304 
1305 static ssize_t
wrap_show(struct device * dev,struct device_attribute * attr,char * buf)1306 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1307 {
1308 	struct msc *msc = dev_get_drvdata(dev);
1309 
1310 	return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1311 }
1312 
1313 static ssize_t
wrap_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1314 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1315 	   size_t size)
1316 {
1317 	struct msc *msc = dev_get_drvdata(dev);
1318 	unsigned long val;
1319 	int ret;
1320 
1321 	ret = kstrtoul(buf, 10, &val);
1322 	if (ret)
1323 		return ret;
1324 
1325 	msc->wrap = !!val;
1326 
1327 	return size;
1328 }
1329 
1330 static DEVICE_ATTR_RW(wrap);
1331 
1332 static ssize_t
mode_show(struct device * dev,struct device_attribute * attr,char * buf)1333 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1334 {
1335 	struct msc *msc = dev_get_drvdata(dev);
1336 
1337 	return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
1338 }
1339 
1340 static ssize_t
mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1341 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1342 	   size_t size)
1343 {
1344 	struct msc *msc = dev_get_drvdata(dev);
1345 	size_t len = size;
1346 	char *cp;
1347 	int i, ret;
1348 
1349 	if (!capable(CAP_SYS_RAWIO))
1350 		return -EPERM;
1351 
1352 	cp = memchr(buf, '\n', len);
1353 	if (cp)
1354 		len = cp - buf;
1355 
1356 	for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1357 		if (!strncmp(msc_mode[i], buf, len))
1358 			goto found;
1359 
1360 	return -EINVAL;
1361 
1362 found:
1363 	mutex_lock(&msc->buf_mutex);
1364 	ret = msc_buffer_unlocked_free_unless_used(msc);
1365 	if (!ret)
1366 		msc->mode = i;
1367 	mutex_unlock(&msc->buf_mutex);
1368 
1369 	return ret ? ret : size;
1370 }
1371 
1372 static DEVICE_ATTR_RW(mode);
1373 
1374 static ssize_t
nr_pages_show(struct device * dev,struct device_attribute * attr,char * buf)1375 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1376 {
1377 	struct msc *msc = dev_get_drvdata(dev);
1378 	struct msc_window *win;
1379 	size_t count = 0;
1380 
1381 	mutex_lock(&msc->buf_mutex);
1382 
1383 	if (msc->mode == MSC_MODE_SINGLE)
1384 		count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1385 	else if (msc->mode == MSC_MODE_MULTI) {
1386 		list_for_each_entry(win, &msc->win_list, entry) {
1387 			count += scnprintf(buf + count, PAGE_SIZE - count,
1388 					   "%d%c", win->nr_blocks,
1389 					   msc_is_last_win(win) ? '\n' : ',');
1390 		}
1391 	} else {
1392 		count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1393 	}
1394 
1395 	mutex_unlock(&msc->buf_mutex);
1396 
1397 	return count;
1398 }
1399 
1400 static ssize_t
nr_pages_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1401 nr_pages_store(struct device *dev, struct device_attribute *attr,
1402 	       const char *buf, size_t size)
1403 {
1404 	struct msc *msc = dev_get_drvdata(dev);
1405 	unsigned long val, *win = NULL, *rewin;
1406 	size_t len = size;
1407 	const char *p = buf;
1408 	char *end, *s;
1409 	int ret, nr_wins = 0;
1410 
1411 	if (!capable(CAP_SYS_RAWIO))
1412 		return -EPERM;
1413 
1414 	ret = msc_buffer_free_unless_used(msc);
1415 	if (ret)
1416 		return ret;
1417 
1418 	/* scan the comma-separated list of allocation sizes */
1419 	end = memchr(buf, '\n', len);
1420 	if (end)
1421 		len = end - buf;
1422 
1423 	do {
1424 		end = memchr(p, ',', len);
1425 		s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1426 		ret = kstrtoul(s, 10, &val);
1427 		kfree(s);
1428 
1429 		if (ret || !val)
1430 			goto free_win;
1431 
1432 		if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1433 			ret = -EINVAL;
1434 			goto free_win;
1435 		}
1436 
1437 		nr_wins++;
1438 		rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1439 		if (!rewin) {
1440 			kfree(win);
1441 			return -ENOMEM;
1442 		}
1443 
1444 		win = rewin;
1445 		win[nr_wins - 1] = val;
1446 
1447 		if (!end)
1448 			break;
1449 
1450 		/* consume the number and the following comma, hence +1 */
1451 		len -= end - p + 1;
1452 		p = end + 1;
1453 	} while (len);
1454 
1455 	mutex_lock(&msc->buf_mutex);
1456 	ret = msc_buffer_alloc(msc, win, nr_wins);
1457 	mutex_unlock(&msc->buf_mutex);
1458 
1459 free_win:
1460 	kfree(win);
1461 
1462 	return ret ? ret : size;
1463 }
1464 
1465 static DEVICE_ATTR_RW(nr_pages);
1466 
1467 static struct attribute *msc_output_attrs[] = {
1468 	&dev_attr_wrap.attr,
1469 	&dev_attr_mode.attr,
1470 	&dev_attr_nr_pages.attr,
1471 	NULL,
1472 };
1473 
1474 static struct attribute_group msc_output_group = {
1475 	.attrs	= msc_output_attrs,
1476 };
1477 
intel_th_msc_probe(struct intel_th_device * thdev)1478 static int intel_th_msc_probe(struct intel_th_device *thdev)
1479 {
1480 	struct device *dev = &thdev->dev;
1481 	struct resource *res;
1482 	struct msc *msc;
1483 	void __iomem *base;
1484 	int err;
1485 
1486 	res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1487 	if (!res)
1488 		return -ENODEV;
1489 
1490 	base = devm_ioremap(dev, res->start, resource_size(res));
1491 	if (!base)
1492 		return -ENOMEM;
1493 
1494 	msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1495 	if (!msc)
1496 		return -ENOMEM;
1497 
1498 	msc->index = thdev->id;
1499 
1500 	msc->thdev = thdev;
1501 	msc->reg_base = base + msc->index * 0x100;
1502 
1503 	err = intel_th_msc_init(msc);
1504 	if (err)
1505 		return err;
1506 
1507 	err = sysfs_create_group(&dev->kobj, &msc_output_group);
1508 	if (err)
1509 		return err;
1510 
1511 	dev_set_drvdata(dev, msc);
1512 
1513 	return 0;
1514 }
1515 
intel_th_msc_remove(struct intel_th_device * thdev)1516 static void intel_th_msc_remove(struct intel_th_device *thdev)
1517 {
1518 	sysfs_remove_group(&thdev->dev.kobj, &msc_output_group);
1519 }
1520 
1521 static struct intel_th_driver intel_th_msc_driver = {
1522 	.probe	= intel_th_msc_probe,
1523 	.remove	= intel_th_msc_remove,
1524 	.activate	= intel_th_msc_activate,
1525 	.deactivate	= intel_th_msc_deactivate,
1526 	.fops	= &intel_th_msc_fops,
1527 	.driver	= {
1528 		.name	= "msc",
1529 		.owner	= THIS_MODULE,
1530 	},
1531 };
1532 
1533 module_driver(intel_th_msc_driver,
1534 	      intel_th_driver_register,
1535 	      intel_th_driver_unregister);
1536 
1537 MODULE_LICENSE("GPL v2");
1538 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1539 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");
1540