ntb: remove unneeded DRIVER_LICENSE #defines
[linux/fpc-iii.git] / drivers / hwtracing / intel_th / msu.c
blobdfb57eaa9f22a94d5e4a3454649f33f64f6a3f1c
1 /*
2 * Intel(R) Trace Hub Memory Storage Unit
4 * Copyright (C) 2014-2015 Intel Corporation.
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.
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.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
30 #ifdef CONFIG_X86
31 #include <asm/set_memory.h>
32 #endif
34 #include "intel_th.h"
35 #include "msu.h"
37 #define msc_dev(x) (&(x)->thdev->dev)
39 /**
40 * struct msc_block - multiblock mode block descriptor
41 * @bdesc: pointer to hardware descriptor (beginning of the block)
42 * @addr: physical address of the block
44 struct msc_block {
45 struct msc_block_desc *bdesc;
46 dma_addr_t addr;
49 /**
50 * struct msc_window - multiblock mode window descriptor
51 * @entry: window list linkage (msc::win_list)
52 * @pgoff: page offset into the buffer that this window starts at
53 * @nr_blocks: number of blocks (pages) in this window
54 * @block: array of block descriptors
56 struct msc_window {
57 struct list_head entry;
58 unsigned long pgoff;
59 unsigned int nr_blocks;
60 struct msc *msc;
61 struct msc_block block[0];
64 /**
65 * struct msc_iter - iterator for msc buffer
66 * @entry: msc::iter_list linkage
67 * @msc: pointer to the MSC device
68 * @start_win: oldest window
69 * @win: current window
70 * @offset: current logical offset into the buffer
71 * @start_block: oldest block in the window
72 * @block: block number in the window
73 * @block_off: offset into current block
74 * @wrap_count: block wrapping handling
75 * @eof: end of buffer reached
77 struct msc_iter {
78 struct list_head entry;
79 struct msc *msc;
80 struct msc_window *start_win;
81 struct msc_window *win;
82 unsigned long offset;
83 int start_block;
84 int block;
85 unsigned int block_off;
86 unsigned int wrap_count;
87 unsigned int eof;
90 /**
91 * struct msc - MSC device representation
92 * @reg_base: register window base address
93 * @thdev: intel_th_device pointer
94 * @win_list: list of windows in multiblock mode
95 * @nr_pages: total number of pages allocated for this buffer
96 * @single_sz: amount of data in single mode
97 * @single_wrap: single mode wrap occurred
98 * @base: buffer's base pointer
99 * @base_addr: buffer's base address
100 * @user_count: number of users of the buffer
101 * @mmap_count: number of mappings
102 * @buf_mutex: mutex to serialize access to buffer-related bits
104 * @enabled: MSC is enabled
105 * @wrap: wrapping is enabled
106 * @mode: MSC operating mode
107 * @burst_len: write burst length
108 * @index: number of this MSC in the MSU
110 struct msc {
111 void __iomem *reg_base;
112 struct intel_th_device *thdev;
114 struct list_head win_list;
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;
121 /* <0: no buffer, 0: no users, >0: active users */
122 atomic_t user_count;
124 atomic_t mmap_count;
125 struct mutex buf_mutex;
127 struct list_head iter_list;
129 /* config */
130 unsigned int enabled : 1,
131 wrap : 1;
132 unsigned int mode;
133 unsigned int burst_len;
134 unsigned int index;
137 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
139 /* header hasn't been written */
140 if (!bdesc->valid_dw)
141 return true;
143 /* valid_dw includes the header */
144 if (!msc_data_sz(bdesc))
145 return true;
147 return false;
151 * msc_oldest_window() - locate the window with oldest data
152 * @msc: MSC device
154 * This should only be used in multiblock mode. Caller should hold the
155 * msc::user_count reference.
157 * Return: the oldest window with valid data
159 static struct msc_window *msc_oldest_window(struct msc *msc)
161 struct msc_window *win;
162 u32 reg = ioread32(msc->reg_base + REG_MSU_MSC0NWSA);
163 unsigned long win_addr = (unsigned long)reg << PAGE_SHIFT;
164 unsigned int found = 0;
166 if (list_empty(&msc->win_list))
167 return NULL;
170 * we might need a radix tree for this, depending on how
171 * many windows a typical user would allocate; ideally it's
172 * something like 2, in which case we're good
174 list_for_each_entry(win, &msc->win_list, entry) {
175 if (win->block[0].addr == win_addr)
176 found++;
178 /* skip the empty ones */
179 if (msc_block_is_empty(win->block[0].bdesc))
180 continue;
182 if (found)
183 return win;
186 return list_entry(msc->win_list.next, struct msc_window, entry);
190 * msc_win_oldest_block() - locate the oldest block in a given window
191 * @win: window to look at
193 * Return: index of the block with the oldest data
195 static unsigned int msc_win_oldest_block(struct msc_window *win)
197 unsigned int blk;
198 struct msc_block_desc *bdesc = win->block[0].bdesc;
200 /* without wrapping, first block is the oldest */
201 if (!msc_block_wrapped(bdesc))
202 return 0;
205 * with wrapping, last written block contains both the newest and the
206 * oldest data for this window.
208 for (blk = 0; blk < win->nr_blocks; blk++) {
209 bdesc = win->block[blk].bdesc;
211 if (msc_block_last_written(bdesc))
212 return blk;
215 return 0;
219 * msc_is_last_win() - check if a window is the last one for a given MSC
220 * @win: window
221 * Return: true if @win is the last window in MSC's multiblock buffer
223 static inline bool msc_is_last_win(struct msc_window *win)
225 return win->entry.next == &win->msc->win_list;
229 * msc_next_window() - return next window in the multiblock buffer
230 * @win: current window
232 * Return: window following the current one
234 static struct msc_window *msc_next_window(struct msc_window *win)
236 if (msc_is_last_win(win))
237 return list_entry(win->msc->win_list.next, struct msc_window,
238 entry);
240 return list_entry(win->entry.next, struct msc_window, entry);
243 static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
245 return iter->win->block[iter->block].bdesc;
248 static void msc_iter_init(struct msc_iter *iter)
250 memset(iter, 0, sizeof(*iter));
251 iter->start_block = -1;
252 iter->block = -1;
255 static struct msc_iter *msc_iter_install(struct msc *msc)
257 struct msc_iter *iter;
259 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
260 if (!iter)
261 return ERR_PTR(-ENOMEM);
263 mutex_lock(&msc->buf_mutex);
266 * Reading and tracing are mutually exclusive; if msc is
267 * enabled, open() will fail; otherwise existing readers
268 * will prevent enabling the msc and the rest of fops don't
269 * need to worry about it.
271 if (msc->enabled) {
272 kfree(iter);
273 iter = ERR_PTR(-EBUSY);
274 goto unlock;
277 msc_iter_init(iter);
278 iter->msc = msc;
280 list_add_tail(&iter->entry, &msc->iter_list);
281 unlock:
282 mutex_unlock(&msc->buf_mutex);
284 return iter;
287 static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
289 mutex_lock(&msc->buf_mutex);
290 list_del(&iter->entry);
291 mutex_unlock(&msc->buf_mutex);
293 kfree(iter);
296 static void msc_iter_block_start(struct msc_iter *iter)
298 if (iter->start_block != -1)
299 return;
301 iter->start_block = msc_win_oldest_block(iter->win);
302 iter->block = iter->start_block;
303 iter->wrap_count = 0;
306 * start with the block with oldest data; if data has wrapped
307 * in this window, it should be in this block
309 if (msc_block_wrapped(msc_iter_bdesc(iter)))
310 iter->wrap_count = 2;
314 static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
316 /* already started, nothing to do */
317 if (iter->start_win)
318 return 0;
320 iter->start_win = msc_oldest_window(msc);
321 if (!iter->start_win)
322 return -EINVAL;
324 iter->win = iter->start_win;
325 iter->start_block = -1;
327 msc_iter_block_start(iter);
329 return 0;
332 static int msc_iter_win_advance(struct msc_iter *iter)
334 iter->win = msc_next_window(iter->win);
335 iter->start_block = -1;
337 if (iter->win == iter->start_win) {
338 iter->eof++;
339 return 1;
342 msc_iter_block_start(iter);
344 return 0;
347 static int msc_iter_block_advance(struct msc_iter *iter)
349 iter->block_off = 0;
351 /* wrapping */
352 if (iter->wrap_count && iter->block == iter->start_block) {
353 iter->wrap_count--;
354 if (!iter->wrap_count)
355 /* copied newest data from the wrapped block */
356 return msc_iter_win_advance(iter);
359 /* no wrapping, check for last written block */
360 if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
361 /* copied newest data for the window */
362 return msc_iter_win_advance(iter);
364 /* block advance */
365 if (++iter->block == iter->win->nr_blocks)
366 iter->block = 0;
368 /* no wrapping, sanity check in case there is no last written block */
369 if (!iter->wrap_count && iter->block == iter->start_block)
370 return msc_iter_win_advance(iter);
372 return 0;
376 * msc_buffer_iterate() - go through multiblock buffer's data
377 * @iter: iterator structure
378 * @size: amount of data to scan
379 * @data: callback's private data
380 * @fn: iterator callback
382 * This will start at the window which will be written to next (containing
383 * the oldest data) and work its way to the current window, calling @fn
384 * for each chunk of data as it goes.
386 * Caller should have msc::user_count reference to make sure the buffer
387 * doesn't disappear from under us.
389 * Return: amount of data actually scanned.
391 static ssize_t
392 msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
393 unsigned long (*fn)(void *, void *, size_t))
395 struct msc *msc = iter->msc;
396 size_t len = size;
397 unsigned int advance;
399 if (iter->eof)
400 return 0;
402 /* start with the oldest window */
403 if (msc_iter_win_start(iter, msc))
404 return 0;
406 do {
407 unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
408 void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
409 size_t tocopy = data_bytes, copied = 0;
410 size_t remaining = 0;
412 advance = 1;
415 * If block wrapping happened, we need to visit the last block
416 * twice, because it contains both the oldest and the newest
417 * data in this window.
419 * First time (wrap_count==2), in the very beginning, to collect
420 * the oldest data, which is in the range
421 * (data_bytes..DATA_IN_PAGE).
423 * Second time (wrap_count==1), it's just like any other block,
424 * containing data in the range of [MSC_BDESC..data_bytes].
426 if (iter->block == iter->start_block && iter->wrap_count == 2) {
427 tocopy = DATA_IN_PAGE - data_bytes;
428 src += data_bytes;
431 if (!tocopy)
432 goto next_block;
434 tocopy -= iter->block_off;
435 src += iter->block_off;
437 if (len < tocopy) {
438 tocopy = len;
439 advance = 0;
442 remaining = fn(data, src, tocopy);
444 if (remaining)
445 advance = 0;
447 copied = tocopy - remaining;
448 len -= copied;
449 iter->block_off += copied;
450 iter->offset += copied;
452 if (!advance)
453 break;
455 next_block:
456 if (msc_iter_block_advance(iter))
457 break;
459 } while (len);
461 return size - len;
465 * msc_buffer_clear_hw_header() - clear hw header for multiblock
466 * @msc: MSC device
468 static void msc_buffer_clear_hw_header(struct msc *msc)
470 struct msc_window *win;
472 list_for_each_entry(win, &msc->win_list, entry) {
473 unsigned int blk;
474 size_t hw_sz = sizeof(struct msc_block_desc) -
475 offsetof(struct msc_block_desc, hw_tag);
477 for (blk = 0; blk < win->nr_blocks; blk++) {
478 struct msc_block_desc *bdesc = win->block[blk].bdesc;
480 memset(&bdesc->hw_tag, 0, hw_sz);
486 * msc_configure() - set up MSC hardware
487 * @msc: the MSC device to configure
489 * Program storage mode, wrapping, burst length and trace buffer address
490 * into a given MSC. Then, enable tracing and set msc::enabled.
491 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
493 static int msc_configure(struct msc *msc)
495 u32 reg;
497 lockdep_assert_held(&msc->buf_mutex);
499 if (msc->mode > MSC_MODE_MULTI)
500 return -ENOTSUPP;
502 if (msc->mode == MSC_MODE_MULTI)
503 msc_buffer_clear_hw_header(msc);
505 reg = msc->base_addr >> PAGE_SHIFT;
506 iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
508 if (msc->mode == MSC_MODE_SINGLE) {
509 reg = msc->nr_pages;
510 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
513 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
514 reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
516 reg |= MSC_EN;
517 reg |= msc->mode << __ffs(MSC_MODE);
518 reg |= msc->burst_len << __ffs(MSC_LEN);
520 if (msc->wrap)
521 reg |= MSC_WRAPEN;
523 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
525 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
526 intel_th_trace_enable(msc->thdev);
527 msc->enabled = 1;
530 return 0;
534 * msc_disable() - disable MSC hardware
535 * @msc: MSC device to disable
537 * If @msc is enabled, disable tracing on the switch and then disable MSC
538 * storage. Caller must hold msc::buf_mutex.
540 static void msc_disable(struct msc *msc)
542 unsigned long count;
543 u32 reg;
545 lockdep_assert_held(&msc->buf_mutex);
547 intel_th_trace_disable(msc->thdev);
549 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
550 count && !(reg & MSCSTS_PLE); count--) {
551 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
552 cpu_relax();
555 if (!count)
556 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
558 if (msc->mode == MSC_MODE_SINGLE) {
559 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
561 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
562 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
563 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
564 reg, msc->single_sz, msc->single_wrap);
567 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
568 reg &= ~MSC_EN;
569 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
570 msc->enabled = 0;
572 iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
573 iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
575 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
576 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
578 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
579 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
582 static int intel_th_msc_activate(struct intel_th_device *thdev)
584 struct msc *msc = dev_get_drvdata(&thdev->dev);
585 int ret = -EBUSY;
587 if (!atomic_inc_unless_negative(&msc->user_count))
588 return -ENODEV;
590 mutex_lock(&msc->buf_mutex);
592 /* if there are readers, refuse */
593 if (list_empty(&msc->iter_list))
594 ret = msc_configure(msc);
596 mutex_unlock(&msc->buf_mutex);
598 if (ret)
599 atomic_dec(&msc->user_count);
601 return ret;
604 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
606 struct msc *msc = dev_get_drvdata(&thdev->dev);
608 mutex_lock(&msc->buf_mutex);
609 if (msc->enabled) {
610 msc_disable(msc);
611 atomic_dec(&msc->user_count);
613 mutex_unlock(&msc->buf_mutex);
617 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
618 * @msc: MSC device
619 * @size: allocation size in bytes
621 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
622 * caller is expected to hold it.
624 * Return: 0 on success, -errno otherwise.
626 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
628 unsigned int order = get_order(size);
629 struct page *page;
631 if (!size)
632 return 0;
634 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
635 if (!page)
636 return -ENOMEM;
638 split_page(page, order);
639 msc->nr_pages = size >> PAGE_SHIFT;
640 msc->base = page_address(page);
641 msc->base_addr = page_to_phys(page);
643 return 0;
647 * msc_buffer_contig_free() - free a contiguous buffer
648 * @msc: MSC configured in SINGLE mode
650 static void msc_buffer_contig_free(struct msc *msc)
652 unsigned long off;
654 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
655 struct page *page = virt_to_page(msc->base + off);
657 page->mapping = NULL;
658 __free_page(page);
661 msc->nr_pages = 0;
665 * msc_buffer_contig_get_page() - find a page at a given offset
666 * @msc: MSC configured in SINGLE mode
667 * @pgoff: page offset
669 * Return: page, if @pgoff is within the range, NULL otherwise.
671 static struct page *msc_buffer_contig_get_page(struct msc *msc,
672 unsigned long pgoff)
674 if (pgoff >= msc->nr_pages)
675 return NULL;
677 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
681 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
682 * @msc: MSC device
683 * @nr_blocks: number of pages in this window
685 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
686 * to serialize, so the caller is expected to hold it.
688 * Return: 0 on success, -errno otherwise.
690 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
692 struct msc_window *win;
693 unsigned long size = PAGE_SIZE;
694 int i, ret = -ENOMEM;
696 if (!nr_blocks)
697 return 0;
699 win = kzalloc(offsetof(struct msc_window, block[nr_blocks]),
700 GFP_KERNEL);
701 if (!win)
702 return -ENOMEM;
704 if (!list_empty(&msc->win_list)) {
705 struct msc_window *prev = list_entry(msc->win_list.prev,
706 struct msc_window, entry);
708 win->pgoff = prev->pgoff + prev->nr_blocks;
711 for (i = 0; i < nr_blocks; i++) {
712 win->block[i].bdesc =
713 dma_alloc_coherent(msc_dev(msc)->parent->parent, size,
714 &win->block[i].addr, GFP_KERNEL);
716 if (!win->block[i].bdesc)
717 goto err_nomem;
719 #ifdef CONFIG_X86
720 /* Set the page as uncached */
721 set_memory_uc((unsigned long)win->block[i].bdesc, 1);
722 #endif
725 win->msc = msc;
726 win->nr_blocks = nr_blocks;
728 if (list_empty(&msc->win_list)) {
729 msc->base = win->block[0].bdesc;
730 msc->base_addr = win->block[0].addr;
733 list_add_tail(&win->entry, &msc->win_list);
734 msc->nr_pages += nr_blocks;
736 return 0;
738 err_nomem:
739 for (i--; i >= 0; i--) {
740 #ifdef CONFIG_X86
741 /* Reset the page to write-back before releasing */
742 set_memory_wb((unsigned long)win->block[i].bdesc, 1);
743 #endif
744 dma_free_coherent(msc_dev(msc), size, win->block[i].bdesc,
745 win->block[i].addr);
747 kfree(win);
749 return ret;
753 * msc_buffer_win_free() - free a window from MSC's window list
754 * @msc: MSC device
755 * @win: window to free
757 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
758 * to serialize, so the caller is expected to hold it.
760 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
762 int i;
764 msc->nr_pages -= win->nr_blocks;
766 list_del(&win->entry);
767 if (list_empty(&msc->win_list)) {
768 msc->base = NULL;
769 msc->base_addr = 0;
772 for (i = 0; i < win->nr_blocks; i++) {
773 struct page *page = virt_to_page(win->block[i].bdesc);
775 page->mapping = NULL;
776 #ifdef CONFIG_X86
777 /* Reset the page to write-back before releasing */
778 set_memory_wb((unsigned long)win->block[i].bdesc, 1);
779 #endif
780 dma_free_coherent(msc_dev(win->msc), PAGE_SIZE,
781 win->block[i].bdesc, win->block[i].addr);
784 kfree(win);
788 * msc_buffer_relink() - set up block descriptors for multiblock mode
789 * @msc: MSC device
791 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
792 * so the caller is expected to hold it.
794 static void msc_buffer_relink(struct msc *msc)
796 struct msc_window *win, *next_win;
798 /* call with msc::mutex locked */
799 list_for_each_entry(win, &msc->win_list, entry) {
800 unsigned int blk;
801 u32 sw_tag = 0;
804 * Last window's next_win should point to the first window
805 * and MSC_SW_TAG_LASTWIN should be set.
807 if (msc_is_last_win(win)) {
808 sw_tag |= MSC_SW_TAG_LASTWIN;
809 next_win = list_entry(msc->win_list.next,
810 struct msc_window, entry);
811 } else {
812 next_win = list_entry(win->entry.next,
813 struct msc_window, entry);
816 for (blk = 0; blk < win->nr_blocks; blk++) {
817 struct msc_block_desc *bdesc = win->block[blk].bdesc;
819 memset(bdesc, 0, sizeof(*bdesc));
821 bdesc->next_win = next_win->block[0].addr >> PAGE_SHIFT;
824 * Similarly to last window, last block should point
825 * to the first one.
827 if (blk == win->nr_blocks - 1) {
828 sw_tag |= MSC_SW_TAG_LASTBLK;
829 bdesc->next_blk =
830 win->block[0].addr >> PAGE_SHIFT;
831 } else {
832 bdesc->next_blk =
833 win->block[blk + 1].addr >> PAGE_SHIFT;
836 bdesc->sw_tag = sw_tag;
837 bdesc->block_sz = PAGE_SIZE / 64;
842 * Make the above writes globally visible before tracing is
843 * enabled to make sure hardware sees them coherently.
845 wmb();
848 static void msc_buffer_multi_free(struct msc *msc)
850 struct msc_window *win, *iter;
852 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
853 msc_buffer_win_free(msc, win);
856 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
857 unsigned int nr_wins)
859 int ret, i;
861 for (i = 0; i < nr_wins; i++) {
862 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
863 if (ret) {
864 msc_buffer_multi_free(msc);
865 return ret;
869 msc_buffer_relink(msc);
871 return 0;
875 * msc_buffer_free() - free buffers for MSC
876 * @msc: MSC device
878 * Free MSC's storage buffers.
880 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
881 * serialize, so the caller is expected to hold it.
883 static void msc_buffer_free(struct msc *msc)
885 if (msc->mode == MSC_MODE_SINGLE)
886 msc_buffer_contig_free(msc);
887 else if (msc->mode == MSC_MODE_MULTI)
888 msc_buffer_multi_free(msc);
892 * msc_buffer_alloc() - allocate a buffer for MSC
893 * @msc: MSC device
894 * @size: allocation size in bytes
896 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
897 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
898 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
899 * window per invocation, so in multiblock mode this can be called multiple
900 * times for the same MSC to allocate multiple windows.
902 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
903 * to serialize, so the caller is expected to hold it.
905 * Return: 0 on success, -errno otherwise.
907 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
908 unsigned int nr_wins)
910 int ret;
912 /* -1: buffer not allocated */
913 if (atomic_read(&msc->user_count) != -1)
914 return -EBUSY;
916 if (msc->mode == MSC_MODE_SINGLE) {
917 if (nr_wins != 1)
918 return -EINVAL;
920 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
921 } else if (msc->mode == MSC_MODE_MULTI) {
922 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
923 } else {
924 ret = -ENOTSUPP;
927 if (!ret) {
928 /* allocation should be visible before the counter goes to 0 */
929 smp_mb__before_atomic();
931 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
932 return -EINVAL;
935 return ret;
939 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
940 * @msc: MSC device
942 * This will free MSC buffer unless it is in use or there is no allocated
943 * buffer.
944 * Caller needs to hold msc::buf_mutex.
946 * Return: 0 on successful deallocation or if there was no buffer to
947 * deallocate, -EBUSY if there are active users.
949 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
951 int count, ret = 0;
953 count = atomic_cmpxchg(&msc->user_count, 0, -1);
955 /* > 0: buffer is allocated and has users */
956 if (count > 0)
957 ret = -EBUSY;
958 /* 0: buffer is allocated, no users */
959 else if (!count)
960 msc_buffer_free(msc);
961 /* < 0: no buffer, nothing to do */
963 return ret;
967 * msc_buffer_free_unless_used() - free a buffer unless it's in use
968 * @msc: MSC device
970 * This is a locked version of msc_buffer_unlocked_free_unless_used().
972 static int msc_buffer_free_unless_used(struct msc *msc)
974 int ret;
976 mutex_lock(&msc->buf_mutex);
977 ret = msc_buffer_unlocked_free_unless_used(msc);
978 mutex_unlock(&msc->buf_mutex);
980 return ret;
984 * msc_buffer_get_page() - get MSC buffer page at a given offset
985 * @msc: MSC device
986 * @pgoff: page offset into the storage buffer
988 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
989 * the caller.
991 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
993 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
995 struct msc_window *win;
997 if (msc->mode == MSC_MODE_SINGLE)
998 return msc_buffer_contig_get_page(msc, pgoff);
1000 list_for_each_entry(win, &msc->win_list, entry)
1001 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1002 goto found;
1004 return NULL;
1006 found:
1007 pgoff -= win->pgoff;
1008 return virt_to_page(win->block[pgoff].bdesc);
1012 * struct msc_win_to_user_struct - data for copy_to_user() callback
1013 * @buf: userspace buffer to copy data to
1014 * @offset: running offset
1016 struct msc_win_to_user_struct {
1017 char __user *buf;
1018 unsigned long offset;
1022 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1023 * @data: callback's private data
1024 * @src: source buffer
1025 * @len: amount of data to copy from the source buffer
1027 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1029 struct msc_win_to_user_struct *u = data;
1030 unsigned long ret;
1032 ret = copy_to_user(u->buf + u->offset, src, len);
1033 u->offset += len - ret;
1035 return ret;
1040 * file operations' callbacks
1043 static int intel_th_msc_open(struct inode *inode, struct file *file)
1045 struct intel_th_device *thdev = file->private_data;
1046 struct msc *msc = dev_get_drvdata(&thdev->dev);
1047 struct msc_iter *iter;
1049 if (!capable(CAP_SYS_RAWIO))
1050 return -EPERM;
1052 iter = msc_iter_install(msc);
1053 if (IS_ERR(iter))
1054 return PTR_ERR(iter);
1056 file->private_data = iter;
1058 return nonseekable_open(inode, file);
1061 static int intel_th_msc_release(struct inode *inode, struct file *file)
1063 struct msc_iter *iter = file->private_data;
1064 struct msc *msc = iter->msc;
1066 msc_iter_remove(iter, msc);
1068 return 0;
1071 static ssize_t
1072 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1074 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1075 unsigned long start = off, tocopy = 0;
1077 if (msc->single_wrap) {
1078 start += msc->single_sz;
1079 if (start < size) {
1080 tocopy = min(rem, size - start);
1081 if (copy_to_user(buf, msc->base + start, tocopy))
1082 return -EFAULT;
1084 buf += tocopy;
1085 rem -= tocopy;
1086 start += tocopy;
1089 start &= size - 1;
1090 if (rem) {
1091 tocopy = min(rem, msc->single_sz - start);
1092 if (copy_to_user(buf, msc->base + start, tocopy))
1093 return -EFAULT;
1095 rem -= tocopy;
1098 return len - rem;
1101 if (copy_to_user(buf, msc->base + start, rem))
1102 return -EFAULT;
1104 return len;
1107 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1108 size_t len, loff_t *ppos)
1110 struct msc_iter *iter = file->private_data;
1111 struct msc *msc = iter->msc;
1112 size_t size;
1113 loff_t off = *ppos;
1114 ssize_t ret = 0;
1116 if (!atomic_inc_unless_negative(&msc->user_count))
1117 return 0;
1119 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1120 size = msc->single_sz;
1121 else
1122 size = msc->nr_pages << PAGE_SHIFT;
1124 if (!size)
1125 goto put_count;
1127 if (off >= size)
1128 goto put_count;
1130 if (off + len >= size)
1131 len = size - off;
1133 if (msc->mode == MSC_MODE_SINGLE) {
1134 ret = msc_single_to_user(msc, buf, off, len);
1135 if (ret >= 0)
1136 *ppos += ret;
1137 } else if (msc->mode == MSC_MODE_MULTI) {
1138 struct msc_win_to_user_struct u = {
1139 .buf = buf,
1140 .offset = 0,
1143 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1144 if (ret >= 0)
1145 *ppos = iter->offset;
1146 } else {
1147 ret = -ENOTSUPP;
1150 put_count:
1151 atomic_dec(&msc->user_count);
1153 return ret;
1157 * vm operations callbacks (vm_ops)
1160 static void msc_mmap_open(struct vm_area_struct *vma)
1162 struct msc_iter *iter = vma->vm_file->private_data;
1163 struct msc *msc = iter->msc;
1165 atomic_inc(&msc->mmap_count);
1168 static void msc_mmap_close(struct vm_area_struct *vma)
1170 struct msc_iter *iter = vma->vm_file->private_data;
1171 struct msc *msc = iter->msc;
1172 unsigned long pg;
1174 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1175 return;
1177 /* drop page _refcounts */
1178 for (pg = 0; pg < msc->nr_pages; pg++) {
1179 struct page *page = msc_buffer_get_page(msc, pg);
1181 if (WARN_ON_ONCE(!page))
1182 continue;
1184 if (page->mapping)
1185 page->mapping = NULL;
1188 /* last mapping -- drop user_count */
1189 atomic_dec(&msc->user_count);
1190 mutex_unlock(&msc->buf_mutex);
1193 static int msc_mmap_fault(struct vm_fault *vmf)
1195 struct msc_iter *iter = vmf->vma->vm_file->private_data;
1196 struct msc *msc = iter->msc;
1198 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1199 if (!vmf->page)
1200 return VM_FAULT_SIGBUS;
1202 get_page(vmf->page);
1203 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
1204 vmf->page->index = vmf->pgoff;
1206 return 0;
1209 static const struct vm_operations_struct msc_mmap_ops = {
1210 .open = msc_mmap_open,
1211 .close = msc_mmap_close,
1212 .fault = msc_mmap_fault,
1215 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1217 unsigned long size = vma->vm_end - vma->vm_start;
1218 struct msc_iter *iter = vma->vm_file->private_data;
1219 struct msc *msc = iter->msc;
1220 int ret = -EINVAL;
1222 if (!size || offset_in_page(size))
1223 return -EINVAL;
1225 if (vma->vm_pgoff)
1226 return -EINVAL;
1228 /* grab user_count once per mmap; drop in msc_mmap_close() */
1229 if (!atomic_inc_unless_negative(&msc->user_count))
1230 return -EINVAL;
1232 if (msc->mode != MSC_MODE_SINGLE &&
1233 msc->mode != MSC_MODE_MULTI)
1234 goto out;
1236 if (size >> PAGE_SHIFT != msc->nr_pages)
1237 goto out;
1239 atomic_set(&msc->mmap_count, 1);
1240 ret = 0;
1242 out:
1243 if (ret)
1244 atomic_dec(&msc->user_count);
1246 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1247 vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1248 vma->vm_ops = &msc_mmap_ops;
1249 return ret;
1252 static const struct file_operations intel_th_msc_fops = {
1253 .open = intel_th_msc_open,
1254 .release = intel_th_msc_release,
1255 .read = intel_th_msc_read,
1256 .mmap = intel_th_msc_mmap,
1257 .llseek = no_llseek,
1258 .owner = THIS_MODULE,
1261 static int intel_th_msc_init(struct msc *msc)
1263 atomic_set(&msc->user_count, -1);
1265 msc->mode = MSC_MODE_MULTI;
1266 mutex_init(&msc->buf_mutex);
1267 INIT_LIST_HEAD(&msc->win_list);
1268 INIT_LIST_HEAD(&msc->iter_list);
1270 msc->burst_len =
1271 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1272 __ffs(MSC_LEN);
1274 return 0;
1277 static const char * const msc_mode[] = {
1278 [MSC_MODE_SINGLE] = "single",
1279 [MSC_MODE_MULTI] = "multi",
1280 [MSC_MODE_EXI] = "ExI",
1281 [MSC_MODE_DEBUG] = "debug",
1284 static ssize_t
1285 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1287 struct msc *msc = dev_get_drvdata(dev);
1289 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1292 static ssize_t
1293 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1294 size_t size)
1296 struct msc *msc = dev_get_drvdata(dev);
1297 unsigned long val;
1298 int ret;
1300 ret = kstrtoul(buf, 10, &val);
1301 if (ret)
1302 return ret;
1304 msc->wrap = !!val;
1306 return size;
1309 static DEVICE_ATTR_RW(wrap);
1311 static ssize_t
1312 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1314 struct msc *msc = dev_get_drvdata(dev);
1316 return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
1319 static ssize_t
1320 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1321 size_t size)
1323 struct msc *msc = dev_get_drvdata(dev);
1324 size_t len = size;
1325 char *cp;
1326 int i, ret;
1328 if (!capable(CAP_SYS_RAWIO))
1329 return -EPERM;
1331 cp = memchr(buf, '\n', len);
1332 if (cp)
1333 len = cp - buf;
1335 for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1336 if (!strncmp(msc_mode[i], buf, len))
1337 goto found;
1339 return -EINVAL;
1341 found:
1342 mutex_lock(&msc->buf_mutex);
1343 ret = msc_buffer_unlocked_free_unless_used(msc);
1344 if (!ret)
1345 msc->mode = i;
1346 mutex_unlock(&msc->buf_mutex);
1348 return ret ? ret : size;
1351 static DEVICE_ATTR_RW(mode);
1353 static ssize_t
1354 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1356 struct msc *msc = dev_get_drvdata(dev);
1357 struct msc_window *win;
1358 size_t count = 0;
1360 mutex_lock(&msc->buf_mutex);
1362 if (msc->mode == MSC_MODE_SINGLE)
1363 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1364 else if (msc->mode == MSC_MODE_MULTI) {
1365 list_for_each_entry(win, &msc->win_list, entry) {
1366 count += scnprintf(buf + count, PAGE_SIZE - count,
1367 "%d%c", win->nr_blocks,
1368 msc_is_last_win(win) ? '\n' : ',');
1370 } else {
1371 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1374 mutex_unlock(&msc->buf_mutex);
1376 return count;
1379 static ssize_t
1380 nr_pages_store(struct device *dev, struct device_attribute *attr,
1381 const char *buf, size_t size)
1383 struct msc *msc = dev_get_drvdata(dev);
1384 unsigned long val, *win = NULL, *rewin;
1385 size_t len = size;
1386 const char *p = buf;
1387 char *end, *s;
1388 int ret, nr_wins = 0;
1390 if (!capable(CAP_SYS_RAWIO))
1391 return -EPERM;
1393 ret = msc_buffer_free_unless_used(msc);
1394 if (ret)
1395 return ret;
1397 /* scan the comma-separated list of allocation sizes */
1398 end = memchr(buf, '\n', len);
1399 if (end)
1400 len = end - buf;
1402 do {
1403 end = memchr(p, ',', len);
1404 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1405 if (!s) {
1406 ret = -ENOMEM;
1407 goto free_win;
1410 ret = kstrtoul(s, 10, &val);
1411 kfree(s);
1413 if (ret || !val)
1414 goto free_win;
1416 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1417 ret = -EINVAL;
1418 goto free_win;
1421 nr_wins++;
1422 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1423 if (!rewin) {
1424 kfree(win);
1425 return -ENOMEM;
1428 win = rewin;
1429 win[nr_wins - 1] = val;
1431 if (!end)
1432 break;
1434 len -= end - p;
1435 p = end + 1;
1436 } while (len);
1438 mutex_lock(&msc->buf_mutex);
1439 ret = msc_buffer_alloc(msc, win, nr_wins);
1440 mutex_unlock(&msc->buf_mutex);
1442 free_win:
1443 kfree(win);
1445 return ret ? ret : size;
1448 static DEVICE_ATTR_RW(nr_pages);
1450 static struct attribute *msc_output_attrs[] = {
1451 &dev_attr_wrap.attr,
1452 &dev_attr_mode.attr,
1453 &dev_attr_nr_pages.attr,
1454 NULL,
1457 static struct attribute_group msc_output_group = {
1458 .attrs = msc_output_attrs,
1461 static int intel_th_msc_probe(struct intel_th_device *thdev)
1463 struct device *dev = &thdev->dev;
1464 struct resource *res;
1465 struct msc *msc;
1466 void __iomem *base;
1467 int err;
1469 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1470 if (!res)
1471 return -ENODEV;
1473 base = devm_ioremap(dev, res->start, resource_size(res));
1474 if (!base)
1475 return -ENOMEM;
1477 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1478 if (!msc)
1479 return -ENOMEM;
1481 msc->index = thdev->id;
1483 msc->thdev = thdev;
1484 msc->reg_base = base + msc->index * 0x100;
1486 err = intel_th_msc_init(msc);
1487 if (err)
1488 return err;
1490 dev_set_drvdata(dev, msc);
1492 return 0;
1495 static void intel_th_msc_remove(struct intel_th_device *thdev)
1497 struct msc *msc = dev_get_drvdata(&thdev->dev);
1498 int ret;
1500 intel_th_msc_deactivate(thdev);
1503 * Buffers should not be used at this point except if the
1504 * output character device is still open and the parent
1505 * device gets detached from its bus, which is a FIXME.
1507 ret = msc_buffer_free_unless_used(msc);
1508 WARN_ON_ONCE(ret);
1511 static struct intel_th_driver intel_th_msc_driver = {
1512 .probe = intel_th_msc_probe,
1513 .remove = intel_th_msc_remove,
1514 .activate = intel_th_msc_activate,
1515 .deactivate = intel_th_msc_deactivate,
1516 .fops = &intel_th_msc_fops,
1517 .attr_group = &msc_output_group,
1518 .driver = {
1519 .name = "msc",
1520 .owner = THIS_MODULE,
1524 module_driver(intel_th_msc_driver,
1525 intel_th_driver_register,
1526 intel_th_driver_unregister);
1528 MODULE_LICENSE("GPL v2");
1529 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1530 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");