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
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>
28 #include <linux/dma-mapping.h>
30 #include <asm/cacheflush.h>
35 #define msc_dev(x) (&(x)->thdev->dev)
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
43 struct msc_block_desc *bdesc;
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
55 struct list_head entry;
57 unsigned int nr_blocks;
59 struct msc_block block[0];
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
76 struct list_head entry;
78 struct msc_window *start_win;
79 struct msc_window *win;
83 unsigned int block_off;
84 unsigned int wrap_count;
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
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
110 void __iomem *reg_base;
111 struct intel_th_device *thdev;
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;
119 dma_addr_t base_addr;
121 /* <0: no buffer, 0: no users, >0: active users */
125 struct mutex buf_mutex;
127 struct mutex iter_mutex;
128 struct list_head iter_list;
131 unsigned int enabled : 1,
134 unsigned int burst_len;
138 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
140 /* header hasn't been written */
141 if (!bdesc->valid_dw)
144 /* valid_dw includes the header */
145 if (!msc_data_sz(bdesc))
152 * msc_oldest_window() - locate the window with oldest data
155 * This should only be used in multiblock mode. Caller should hold the
156 * msc::user_count reference.
158 * Return: the oldest window with valid data
160 static struct msc_window *msc_oldest_window(struct msc *msc)
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;
167 if (list_empty(&msc->win_list))
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
175 list_for_each_entry(win, &msc->win_list, entry) {
176 if (win->block[0].addr == win_addr)
179 /* skip the empty ones */
180 if (msc_block_is_empty(win->block[0].bdesc))
187 return list_entry(msc->win_list.next, struct msc_window, entry);
191 * msc_win_oldest_block() - locate the oldest block in a given window
192 * @win: window to look at
194 * Return: index of the block with the oldest data
196 static unsigned int msc_win_oldest_block(struct msc_window *win)
199 struct msc_block_desc *bdesc = win->block[0].bdesc;
201 /* without wrapping, first block is the oldest */
202 if (!msc_block_wrapped(bdesc))
206 * with wrapping, last written block contains both the newest and the
207 * oldest data for this window.
209 for (blk = 0; blk < win->nr_blocks; blk++) {
210 bdesc = win->block[blk].bdesc;
212 if (msc_block_last_written(bdesc))
220 * msc_is_last_win() - check if a window is the last one for a given MSC
222 * Return: true if @win is the last window in MSC's multiblock buffer
224 static inline bool msc_is_last_win(struct msc_window *win)
226 return win->entry.next == &win->msc->win_list;
230 * msc_next_window() - return next window in the multiblock buffer
231 * @win: current window
233 * Return: window following the current one
235 static struct msc_window *msc_next_window(struct msc_window *win)
237 if (msc_is_last_win(win))
238 return list_entry(win->msc->win_list.next, struct msc_window,
241 return list_entry(win->entry.next, struct msc_window, entry);
244 static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
246 return iter->win->block[iter->block].bdesc;
249 static void msc_iter_init(struct msc_iter *iter)
251 memset(iter, 0, sizeof(*iter));
252 iter->start_block = -1;
256 static struct msc_iter *msc_iter_install(struct msc *msc)
258 struct msc_iter *iter;
260 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
267 mutex_lock(&msc->iter_mutex);
268 list_add_tail(&iter->entry, &msc->iter_list);
269 mutex_unlock(&msc->iter_mutex);
274 static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
276 mutex_lock(&msc->iter_mutex);
277 list_del(&iter->entry);
278 mutex_unlock(&msc->iter_mutex);
283 static void msc_iter_block_start(struct msc_iter *iter)
285 if (iter->start_block != -1)
288 iter->start_block = msc_win_oldest_block(iter->win);
289 iter->block = iter->start_block;
290 iter->wrap_count = 0;
293 * start with the block with oldest data; if data has wrapped
294 * in this window, it should be in this block
296 if (msc_block_wrapped(msc_iter_bdesc(iter)))
297 iter->wrap_count = 2;
301 static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
303 /* already started, nothing to do */
307 iter->start_win = msc_oldest_window(msc);
308 if (!iter->start_win)
311 iter->win = iter->start_win;
312 iter->start_block = -1;
314 msc_iter_block_start(iter);
319 static int msc_iter_win_advance(struct msc_iter *iter)
321 iter->win = msc_next_window(iter->win);
322 iter->start_block = -1;
324 if (iter->win == iter->start_win) {
329 msc_iter_block_start(iter);
334 static int msc_iter_block_advance(struct msc_iter *iter)
339 if (iter->wrap_count && iter->block == iter->start_block) {
341 if (!iter->wrap_count)
342 /* copied newest data from the wrapped block */
343 return msc_iter_win_advance(iter);
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);
352 if (++iter->block == iter->win->nr_blocks)
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);
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
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.
373 * Caller should have msc::user_count reference to make sure the buffer
374 * doesn't disappear from under us.
376 * Return: amount of data actually scanned.
379 msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
380 unsigned long (*fn)(void *, void *, size_t))
382 struct msc *msc = iter->msc;
384 unsigned int advance;
389 /* start with the oldest window */
390 if (msc_iter_win_start(iter, msc))
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;
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.
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).
410 * Second time (wrap_count==1), it's just like any other block,
411 * containing data in the range of [MSC_BDESC..data_bytes].
413 if (iter->block == iter->start_block && iter->wrap_count) {
414 tocopy = DATA_IN_PAGE - data_bytes;
421 tocopy -= iter->block_off;
422 src += iter->block_off;
429 remaining = fn(data, src, tocopy);
434 copied = tocopy - remaining;
436 iter->block_off += copied;
437 iter->offset += copied;
443 if (msc_iter_block_advance(iter))
452 * msc_buffer_clear_hw_header() - clear hw header for multiblock
455 static void msc_buffer_clear_hw_header(struct msc *msc)
457 struct msc_window *win;
459 mutex_lock(&msc->buf_mutex);
460 list_for_each_entry(win, &msc->win_list, entry) {
462 size_t hw_sz = sizeof(struct msc_block_desc) -
463 offsetof(struct msc_block_desc, hw_tag);
465 for (blk = 0; blk < win->nr_blocks; blk++) {
466 struct msc_block_desc *bdesc = win->block[blk].bdesc;
468 memset(&bdesc->hw_tag, 0, hw_sz);
471 mutex_unlock(&msc->buf_mutex);
475 * msc_configure() - set up MSC hardware
476 * @msc: the MSC device to configure
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.
481 static int msc_configure(struct msc *msc)
485 if (msc->mode > MSC_MODE_MULTI)
488 if (msc->mode == MSC_MODE_MULTI)
489 msc_buffer_clear_hw_header(msc);
491 reg = msc->base_addr >> PAGE_SHIFT;
492 iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
494 if (msc->mode == MSC_MODE_SINGLE) {
496 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
499 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
500 reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
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; */
511 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
514 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
515 intel_th_trace_enable(msc->thdev);
522 * msc_disable() - disable MSC hardware
523 * @msc: MSC device to disable
525 * If @msc is enabled, disable tracing on the switch and then disable MSC
528 static void msc_disable(struct msc *msc)
536 intel_th_trace_disable(msc->thdev);
538 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
539 count && !(reg & MSCSTS_PLE); count--) {
540 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
545 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
547 if (msc->mode == MSC_MODE_SINGLE) {
548 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
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);
556 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
558 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
561 iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
562 iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
564 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
565 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
567 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
568 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
571 static int intel_th_msc_activate(struct intel_th_device *thdev)
573 struct msc *msc = dev_get_drvdata(&thdev->dev);
576 if (!atomic_inc_unless_negative(&msc->user_count))
579 mutex_lock(&msc->iter_mutex);
580 if (!list_empty(&msc->iter_list))
582 mutex_unlock(&msc->iter_mutex);
585 atomic_dec(&msc->user_count);
591 return msc_configure(msc);
594 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
596 struct msc *msc = dev_get_drvdata(&thdev->dev);
600 atomic_dec(&msc->user_count);
604 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
606 * @size: allocation size in bytes
608 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
609 * caller is expected to hold it.
611 * Return: 0 on success, -errno otherwise.
613 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
615 unsigned long nr_pages = size >> PAGE_SHIFT;
616 unsigned int order = get_order(size);
623 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
628 page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
632 split_page(page, order);
633 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
635 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
640 msc->nr_pages = nr_pages;
641 msc->base = page_address(page);
642 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
647 __free_pages(page, order);
650 sg_free_table(&msc->single_sgt);
657 * msc_buffer_contig_free() - free a contiguous buffer
658 * @msc: MSC configured in SINGLE mode
660 static void msc_buffer_contig_free(struct msc *msc)
664 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
666 sg_free_table(&msc->single_sgt);
668 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
669 struct page *page = virt_to_page(msc->base + off);
671 page->mapping = NULL;
679 * msc_buffer_contig_get_page() - find a page at a given offset
680 * @msc: MSC configured in SINGLE mode
681 * @pgoff: page offset
683 * Return: page, if @pgoff is within the range, NULL otherwise.
685 static struct page *msc_buffer_contig_get_page(struct msc *msc,
688 if (pgoff >= msc->nr_pages)
691 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
695 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
697 * @nr_blocks: number of pages in this window
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.
702 * Return: 0 on success, -errno otherwise.
704 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
706 struct msc_window *win;
707 unsigned long size = PAGE_SIZE;
708 int i, ret = -ENOMEM;
713 win = kzalloc(offsetof(struct msc_window, block[nr_blocks]),
718 if (!list_empty(&msc->win_list)) {
719 struct msc_window *prev = list_entry(msc->win_list.prev,
720 struct msc_window, entry);
722 win->pgoff = prev->pgoff + prev->nr_blocks;
725 for (i = 0; i < nr_blocks; i++) {
726 win->block[i].bdesc = dma_alloc_coherent(msc_dev(msc), size,
731 /* Set the page as uncached */
732 set_memory_uc((unsigned long)win->block[i].bdesc, 1);
735 if (!win->block[i].bdesc)
740 win->nr_blocks = nr_blocks;
742 if (list_empty(&msc->win_list)) {
743 msc->base = win->block[0].bdesc;
744 msc->base_addr = win->block[0].addr;
747 list_add_tail(&win->entry, &msc->win_list);
748 msc->nr_pages += nr_blocks;
753 for (i--; i >= 0; i--) {
755 /* Reset the page to write-back before releasing */
756 set_memory_wb((unsigned long)win->block[i].bdesc, 1);
758 dma_free_coherent(msc_dev(msc), size, win->block[i].bdesc,
767 * msc_buffer_win_free() - free a window from MSC's window list
769 * @win: window to free
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.
774 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
778 msc->nr_pages -= win->nr_blocks;
780 list_del(&win->entry);
781 if (list_empty(&msc->win_list)) {
786 for (i = 0; i < win->nr_blocks; i++) {
787 struct page *page = virt_to_page(win->block[i].bdesc);
789 page->mapping = NULL;
791 /* Reset the page to write-back before releasing */
792 set_memory_wb((unsigned long)win->block[i].bdesc, 1);
794 dma_free_coherent(msc_dev(win->msc), PAGE_SIZE,
795 win->block[i].bdesc, win->block[i].addr);
802 * msc_buffer_relink() - set up block descriptors for multiblock mode
805 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
806 * so the caller is expected to hold it.
808 static void msc_buffer_relink(struct msc *msc)
810 struct msc_window *win, *next_win;
812 /* call with msc::mutex locked */
813 list_for_each_entry(win, &msc->win_list, entry) {
818 * Last window's next_win should point to the first window
819 * and MSC_SW_TAG_LASTWIN should be set.
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);
826 next_win = list_entry(win->entry.next,
827 struct msc_window, entry);
830 for (blk = 0; blk < win->nr_blocks; blk++) {
831 struct msc_block_desc *bdesc = win->block[blk].bdesc;
833 memset(bdesc, 0, sizeof(*bdesc));
835 bdesc->next_win = next_win->block[0].addr >> PAGE_SHIFT;
838 * Similarly to last window, last block should point
841 if (blk == win->nr_blocks - 1) {
842 sw_tag |= MSC_SW_TAG_LASTBLK;
844 win->block[0].addr >> PAGE_SHIFT;
847 win->block[blk + 1].addr >> PAGE_SHIFT;
850 bdesc->sw_tag = sw_tag;
851 bdesc->block_sz = PAGE_SIZE / 64;
856 * Make the above writes globally visible before tracing is
857 * enabled to make sure hardware sees them coherently.
862 static void msc_buffer_multi_free(struct msc *msc)
864 struct msc_window *win, *iter;
866 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
867 msc_buffer_win_free(msc, win);
870 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
871 unsigned int nr_wins)
875 for (i = 0; i < nr_wins; i++) {
876 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
878 msc_buffer_multi_free(msc);
883 msc_buffer_relink(msc);
889 * msc_buffer_free() - free buffers for MSC
892 * Free MSC's storage buffers.
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.
897 static void msc_buffer_free(struct msc *msc)
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);
906 * msc_buffer_alloc() - allocate a buffer for MSC
908 * @size: allocation size in bytes
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.
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.
919 * Return: 0 on success, -errno otherwise.
921 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
922 unsigned int nr_wins)
926 /* -1: buffer not allocated */
927 if (atomic_read(&msc->user_count) != -1)
930 if (msc->mode == MSC_MODE_SINGLE) {
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);
942 /* allocation should be visible before the counter goes to 0 */
943 smp_mb__before_atomic();
945 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
953 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
956 * This will free MSC buffer unless it is in use or there is no allocated
958 * Caller needs to hold msc::buf_mutex.
960 * Return: 0 on successful deallocation or if there was no buffer to
961 * deallocate, -EBUSY if there are active users.
963 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
967 count = atomic_cmpxchg(&msc->user_count, 0, -1);
969 /* > 0: buffer is allocated and has users */
972 /* 0: buffer is allocated, no users */
974 msc_buffer_free(msc);
975 /* < 0: no buffer, nothing to do */
981 * msc_buffer_free_unless_used() - free a buffer unless it's in use
984 * This is a locked version of msc_buffer_unlocked_free_unless_used().
986 static int msc_buffer_free_unless_used(struct msc *msc)
990 mutex_lock(&msc->buf_mutex);
991 ret = msc_buffer_unlocked_free_unless_used(msc);
992 mutex_unlock(&msc->buf_mutex);
998 * msc_buffer_get_page() - get MSC buffer page at a given offset
1000 * @pgoff: page offset into the storage buffer
1002 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1005 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1007 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1009 struct msc_window *win;
1011 if (msc->mode == MSC_MODE_SINGLE)
1012 return msc_buffer_contig_get_page(msc, pgoff);
1014 list_for_each_entry(win, &msc->win_list, entry)
1015 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1021 pgoff -= win->pgoff;
1022 return virt_to_page(win->block[pgoff].bdesc);
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
1030 struct msc_win_to_user_struct {
1032 unsigned long offset;
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
1041 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1043 struct msc_win_to_user_struct *u = data;
1046 ret = copy_to_user(u->buf + u->offset, src, len);
1047 u->offset += len - ret;
1054 * file operations' callbacks
1057 static int intel_th_msc_open(struct inode *inode, struct file *file)
1059 struct intel_th_device *thdev = file->private_data;
1060 struct msc *msc = dev_get_drvdata(&thdev->dev);
1061 struct msc_iter *iter;
1063 if (!capable(CAP_SYS_RAWIO))
1066 iter = msc_iter_install(msc);
1070 file->private_data = iter;
1072 return nonseekable_open(inode, file);
1075 static int intel_th_msc_release(struct inode *inode, struct file *file)
1077 struct msc_iter *iter = file->private_data;
1078 struct msc *msc = iter->msc;
1080 msc_iter_remove(iter, msc);
1086 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1088 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1089 unsigned long start = off, tocopy = 0;
1091 if (msc->single_wrap) {
1092 start += msc->single_sz;
1094 tocopy = min(rem, size - start);
1095 if (copy_to_user(buf, msc->base + start, tocopy))
1105 tocopy = min(rem, msc->single_sz - start);
1106 if (copy_to_user(buf, msc->base + start, tocopy))
1115 if (copy_to_user(buf, msc->base + start, rem))
1121 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1122 size_t len, loff_t *ppos)
1124 struct msc_iter *iter = file->private_data;
1125 struct msc *msc = iter->msc;
1130 if (!atomic_inc_unless_negative(&msc->user_count))
1138 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1139 size = msc->single_sz;
1141 size = msc->nr_pages << PAGE_SHIFT;
1150 if (off + len >= size)
1153 if (msc->mode == MSC_MODE_SINGLE) {
1154 ret = msc_single_to_user(msc, buf, off, len);
1157 } else if (msc->mode == MSC_MODE_MULTI) {
1158 struct msc_win_to_user_struct u = {
1163 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1165 *ppos = iter->offset;
1171 atomic_dec(&msc->user_count);
1177 * vm operations callbacks (vm_ops)
1180 static void msc_mmap_open(struct vm_area_struct *vma)
1182 struct msc_iter *iter = vma->vm_file->private_data;
1183 struct msc *msc = iter->msc;
1185 atomic_inc(&msc->mmap_count);
1188 static void msc_mmap_close(struct vm_area_struct *vma)
1190 struct msc_iter *iter = vma->vm_file->private_data;
1191 struct msc *msc = iter->msc;
1194 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1197 /* drop page _counts */
1198 for (pg = 0; pg < msc->nr_pages; pg++) {
1199 struct page *page = msc_buffer_get_page(msc, pg);
1201 if (WARN_ON_ONCE(!page))
1205 page->mapping = NULL;
1208 /* last mapping -- drop user_count */
1209 atomic_dec(&msc->user_count);
1210 mutex_unlock(&msc->buf_mutex);
1213 static int msc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1215 struct msc_iter *iter = vma->vm_file->private_data;
1216 struct msc *msc = iter->msc;
1218 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1220 return VM_FAULT_SIGBUS;
1222 get_page(vmf->page);
1223 vmf->page->mapping = vma->vm_file->f_mapping;
1224 vmf->page->index = vmf->pgoff;
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,
1235 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
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;
1242 if (!size || offset_in_page(size))
1248 /* grab user_count once per mmap; drop in msc_mmap_close() */
1249 if (!atomic_inc_unless_negative(&msc->user_count))
1252 if (msc->mode != MSC_MODE_SINGLE &&
1253 msc->mode != MSC_MODE_MULTI)
1256 if (size >> PAGE_SHIFT != msc->nr_pages)
1259 atomic_set(&msc->mmap_count, 1);
1264 atomic_dec(&msc->user_count);
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;
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,
1280 static int intel_th_msc_init(struct msc *msc)
1282 atomic_set(&msc->user_count, -1);
1284 msc->mode = MSC_MODE_MULTI;
1285 mutex_init(&msc->buf_mutex);
1286 INIT_LIST_HEAD(&msc->win_list);
1288 mutex_init(&msc->iter_mutex);
1289 INIT_LIST_HEAD(&msc->iter_list);
1292 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
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",
1306 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1308 struct msc *msc = dev_get_drvdata(dev);
1310 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1314 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1317 struct msc *msc = dev_get_drvdata(dev);
1321 ret = kstrtoul(buf, 10, &val);
1330 static DEVICE_ATTR_RW(wrap);
1333 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1335 struct msc *msc = dev_get_drvdata(dev);
1337 return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
1341 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1344 struct msc *msc = dev_get_drvdata(dev);
1349 if (!capable(CAP_SYS_RAWIO))
1352 cp = memchr(buf, '\n', len);
1356 for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1357 if (!strncmp(msc_mode[i], buf, len))
1363 mutex_lock(&msc->buf_mutex);
1364 ret = msc_buffer_unlocked_free_unless_used(msc);
1367 mutex_unlock(&msc->buf_mutex);
1369 return ret ? ret : size;
1372 static DEVICE_ATTR_RW(mode);
1375 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1377 struct msc *msc = dev_get_drvdata(dev);
1378 struct msc_window *win;
1381 mutex_lock(&msc->buf_mutex);
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' : ',');
1392 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1395 mutex_unlock(&msc->buf_mutex);
1401 nr_pages_store(struct device *dev, struct device_attribute *attr,
1402 const char *buf, size_t size)
1404 struct msc *msc = dev_get_drvdata(dev);
1405 unsigned long val, *win = NULL, *rewin;
1407 const char *p = buf;
1409 int ret, nr_wins = 0;
1411 if (!capable(CAP_SYS_RAWIO))
1414 ret = msc_buffer_free_unless_used(msc);
1418 /* scan the comma-separated list of allocation sizes */
1419 end = memchr(buf, '\n', len);
1424 end = memchr(p, ',', len);
1425 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1426 ret = kstrtoul(s, 10, &val);
1432 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1438 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1445 win[nr_wins - 1] = val;
1450 /* consume the number and the following comma, hence +1 */
1455 mutex_lock(&msc->buf_mutex);
1456 ret = msc_buffer_alloc(msc, win, nr_wins);
1457 mutex_unlock(&msc->buf_mutex);
1462 return ret ? ret : size;
1465 static DEVICE_ATTR_RW(nr_pages);
1467 static struct attribute *msc_output_attrs[] = {
1468 &dev_attr_wrap.attr,
1469 &dev_attr_mode.attr,
1470 &dev_attr_nr_pages.attr,
1474 static struct attribute_group msc_output_group = {
1475 .attrs = msc_output_attrs,
1478 static int intel_th_msc_probe(struct intel_th_device *thdev)
1480 struct device *dev = &thdev->dev;
1481 struct resource *res;
1486 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1490 base = devm_ioremap(dev, res->start, resource_size(res));
1494 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1498 msc->index = thdev->id;
1501 msc->reg_base = base + msc->index * 0x100;
1503 err = intel_th_msc_init(msc);
1507 err = sysfs_create_group(&dev->kobj, &msc_output_group);
1511 dev_set_drvdata(dev, msc);
1516 static void intel_th_msc_remove(struct intel_th_device *thdev)
1518 sysfs_remove_group(&thdev->dev.kobj, &msc_output_group);
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,
1529 .owner = THIS_MODULE,
1533 module_driver(intel_th_msc_driver,
1534 intel_th_driver_register,
1535 intel_th_driver_unregister);
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>");