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 list_head iter_list;
130 unsigned int enabled : 1,
133 unsigned int burst_len;
137 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
139 /* header hasn't been written */
140 if (!bdesc->valid_dw)
143 /* valid_dw includes the header */
144 if (!msc_data_sz(bdesc))
151 * msc_oldest_window() - locate the window with oldest data
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))
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)
178 /* skip the empty ones */
179 if (msc_block_is_empty(win->block[0].bdesc))
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)
198 struct msc_block_desc *bdesc = win->block[0].bdesc;
200 /* without wrapping, first block is the oldest */
201 if (!msc_block_wrapped(bdesc))
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))
219 * msc_is_last_win() - check if a window is the last one for a given MSC
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,
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;
255 static struct msc_iter *msc_iter_install(struct msc *msc)
257 struct msc_iter *iter;
259 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
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.
273 iter = ERR_PTR(-EBUSY);
280 list_add_tail(&iter->entry, &msc->iter_list);
282 mutex_unlock(&msc->buf_mutex);
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);
296 static void msc_iter_block_start(struct msc_iter *iter)
298 if (iter->start_block != -1)
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 */
320 iter->start_win = msc_oldest_window(msc);
321 if (!iter->start_win)
324 iter->win = iter->start_win;
325 iter->start_block = -1;
327 msc_iter_block_start(iter);
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) {
342 msc_iter_block_start(iter);
347 static int msc_iter_block_advance(struct msc_iter *iter)
352 if (iter->wrap_count && iter->block == iter->start_block) {
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);
365 if (++iter->block == iter->win->nr_blocks)
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);
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.
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;
397 unsigned int advance;
402 /* start with the oldest window */
403 if (msc_iter_win_start(iter, msc))
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;
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;
434 tocopy -= iter->block_off;
435 src += iter->block_off;
442 remaining = fn(data, src, tocopy);
447 copied = tocopy - remaining;
449 iter->block_off += copied;
450 iter->offset += copied;
456 if (msc_iter_block_advance(iter))
465 * msc_buffer_clear_hw_header() - clear hw header for multiblock
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) {
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)
497 lockdep_assert_held(&msc->buf_mutex);
499 if (msc->mode > MSC_MODE_MULTI)
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) {
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);
517 reg |= msc->mode << __ffs(MSC_MODE);
518 reg |= msc->burst_len << __ffs(MSC_LEN);
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);
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)
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);
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);
569 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
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);
587 if (!atomic_inc_unless_negative(&msc->user_count))
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);
599 atomic_dec(&msc->user_count);
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);
611 atomic_dec(&msc->user_count);
613 mutex_unlock(&msc->buf_mutex);
617 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
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 long nr_pages = size >> PAGE_SHIFT;
629 unsigned int order = get_order(size);
636 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
641 page = alloc_pages(GFP_KERNEL | __GFP_ZERO | GFP_DMA32, order);
645 split_page(page, order);
646 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
648 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
653 msc->nr_pages = nr_pages;
654 msc->base = page_address(page);
655 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
660 __free_pages(page, order);
663 sg_free_table(&msc->single_sgt);
670 * msc_buffer_contig_free() - free a contiguous buffer
671 * @msc: MSC configured in SINGLE mode
673 static void msc_buffer_contig_free(struct msc *msc)
677 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
679 sg_free_table(&msc->single_sgt);
681 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
682 struct page *page = virt_to_page(msc->base + off);
684 page->mapping = NULL;
692 * msc_buffer_contig_get_page() - find a page at a given offset
693 * @msc: MSC configured in SINGLE mode
694 * @pgoff: page offset
696 * Return: page, if @pgoff is within the range, NULL otherwise.
698 static struct page *msc_buffer_contig_get_page(struct msc *msc,
701 if (pgoff >= msc->nr_pages)
704 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
708 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
710 * @nr_blocks: number of pages in this window
712 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
713 * to serialize, so the caller is expected to hold it.
715 * Return: 0 on success, -errno otherwise.
717 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
719 struct msc_window *win;
720 unsigned long size = PAGE_SIZE;
721 int i, ret = -ENOMEM;
726 win = kzalloc(offsetof(struct msc_window, block[nr_blocks]),
731 if (!list_empty(&msc->win_list)) {
732 struct msc_window *prev = list_entry(msc->win_list.prev,
733 struct msc_window, entry);
735 win->pgoff = prev->pgoff + prev->nr_blocks;
738 for (i = 0; i < nr_blocks; i++) {
739 win->block[i].bdesc = dma_alloc_coherent(msc_dev(msc), size,
744 /* Set the page as uncached */
745 set_memory_uc((unsigned long)win->block[i].bdesc, 1);
748 if (!win->block[i].bdesc)
753 win->nr_blocks = nr_blocks;
755 if (list_empty(&msc->win_list)) {
756 msc->base = win->block[0].bdesc;
757 msc->base_addr = win->block[0].addr;
760 list_add_tail(&win->entry, &msc->win_list);
761 msc->nr_pages += nr_blocks;
766 for (i--; i >= 0; i--) {
768 /* Reset the page to write-back before releasing */
769 set_memory_wb((unsigned long)win->block[i].bdesc, 1);
771 dma_free_coherent(msc_dev(msc), size, win->block[i].bdesc,
780 * msc_buffer_win_free() - free a window from MSC's window list
782 * @win: window to free
784 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
785 * to serialize, so the caller is expected to hold it.
787 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
791 msc->nr_pages -= win->nr_blocks;
793 list_del(&win->entry);
794 if (list_empty(&msc->win_list)) {
799 for (i = 0; i < win->nr_blocks; i++) {
800 struct page *page = virt_to_page(win->block[i].bdesc);
802 page->mapping = NULL;
804 /* Reset the page to write-back before releasing */
805 set_memory_wb((unsigned long)win->block[i].bdesc, 1);
807 dma_free_coherent(msc_dev(win->msc), PAGE_SIZE,
808 win->block[i].bdesc, win->block[i].addr);
815 * msc_buffer_relink() - set up block descriptors for multiblock mode
818 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
819 * so the caller is expected to hold it.
821 static void msc_buffer_relink(struct msc *msc)
823 struct msc_window *win, *next_win;
825 /* call with msc::mutex locked */
826 list_for_each_entry(win, &msc->win_list, entry) {
831 * Last window's next_win should point to the first window
832 * and MSC_SW_TAG_LASTWIN should be set.
834 if (msc_is_last_win(win)) {
835 sw_tag |= MSC_SW_TAG_LASTWIN;
836 next_win = list_entry(msc->win_list.next,
837 struct msc_window, entry);
839 next_win = list_entry(win->entry.next,
840 struct msc_window, entry);
843 for (blk = 0; blk < win->nr_blocks; blk++) {
844 struct msc_block_desc *bdesc = win->block[blk].bdesc;
846 memset(bdesc, 0, sizeof(*bdesc));
848 bdesc->next_win = next_win->block[0].addr >> PAGE_SHIFT;
851 * Similarly to last window, last block should point
854 if (blk == win->nr_blocks - 1) {
855 sw_tag |= MSC_SW_TAG_LASTBLK;
857 win->block[0].addr >> PAGE_SHIFT;
860 win->block[blk + 1].addr >> PAGE_SHIFT;
863 bdesc->sw_tag = sw_tag;
864 bdesc->block_sz = PAGE_SIZE / 64;
869 * Make the above writes globally visible before tracing is
870 * enabled to make sure hardware sees them coherently.
875 static void msc_buffer_multi_free(struct msc *msc)
877 struct msc_window *win, *iter;
879 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
880 msc_buffer_win_free(msc, win);
883 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
884 unsigned int nr_wins)
888 for (i = 0; i < nr_wins; i++) {
889 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
891 msc_buffer_multi_free(msc);
896 msc_buffer_relink(msc);
902 * msc_buffer_free() - free buffers for MSC
905 * Free MSC's storage buffers.
907 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
908 * serialize, so the caller is expected to hold it.
910 static void msc_buffer_free(struct msc *msc)
912 if (msc->mode == MSC_MODE_SINGLE)
913 msc_buffer_contig_free(msc);
914 else if (msc->mode == MSC_MODE_MULTI)
915 msc_buffer_multi_free(msc);
919 * msc_buffer_alloc() - allocate a buffer for MSC
921 * @size: allocation size in bytes
923 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
924 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
925 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
926 * window per invocation, so in multiblock mode this can be called multiple
927 * times for the same MSC to allocate multiple windows.
929 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
930 * to serialize, so the caller is expected to hold it.
932 * Return: 0 on success, -errno otherwise.
934 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
935 unsigned int nr_wins)
939 /* -1: buffer not allocated */
940 if (atomic_read(&msc->user_count) != -1)
943 if (msc->mode == MSC_MODE_SINGLE) {
947 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
948 } else if (msc->mode == MSC_MODE_MULTI) {
949 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
955 /* allocation should be visible before the counter goes to 0 */
956 smp_mb__before_atomic();
958 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
966 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
969 * This will free MSC buffer unless it is in use or there is no allocated
971 * Caller needs to hold msc::buf_mutex.
973 * Return: 0 on successful deallocation or if there was no buffer to
974 * deallocate, -EBUSY if there are active users.
976 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
980 count = atomic_cmpxchg(&msc->user_count, 0, -1);
982 /* > 0: buffer is allocated and has users */
985 /* 0: buffer is allocated, no users */
987 msc_buffer_free(msc);
988 /* < 0: no buffer, nothing to do */
994 * msc_buffer_free_unless_used() - free a buffer unless it's in use
997 * This is a locked version of msc_buffer_unlocked_free_unless_used().
999 static int msc_buffer_free_unless_used(struct msc *msc)
1003 mutex_lock(&msc->buf_mutex);
1004 ret = msc_buffer_unlocked_free_unless_used(msc);
1005 mutex_unlock(&msc->buf_mutex);
1011 * msc_buffer_get_page() - get MSC buffer page at a given offset
1013 * @pgoff: page offset into the storage buffer
1015 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1018 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1020 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1022 struct msc_window *win;
1024 if (msc->mode == MSC_MODE_SINGLE)
1025 return msc_buffer_contig_get_page(msc, pgoff);
1027 list_for_each_entry(win, &msc->win_list, entry)
1028 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1034 pgoff -= win->pgoff;
1035 return virt_to_page(win->block[pgoff].bdesc);
1039 * struct msc_win_to_user_struct - data for copy_to_user() callback
1040 * @buf: userspace buffer to copy data to
1041 * @offset: running offset
1043 struct msc_win_to_user_struct {
1045 unsigned long offset;
1049 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1050 * @data: callback's private data
1051 * @src: source buffer
1052 * @len: amount of data to copy from the source buffer
1054 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1056 struct msc_win_to_user_struct *u = data;
1059 ret = copy_to_user(u->buf + u->offset, src, len);
1060 u->offset += len - ret;
1067 * file operations' callbacks
1070 static int intel_th_msc_open(struct inode *inode, struct file *file)
1072 struct intel_th_device *thdev = file->private_data;
1073 struct msc *msc = dev_get_drvdata(&thdev->dev);
1074 struct msc_iter *iter;
1076 if (!capable(CAP_SYS_RAWIO))
1079 iter = msc_iter_install(msc);
1081 return PTR_ERR(iter);
1083 file->private_data = iter;
1085 return nonseekable_open(inode, file);
1088 static int intel_th_msc_release(struct inode *inode, struct file *file)
1090 struct msc_iter *iter = file->private_data;
1091 struct msc *msc = iter->msc;
1093 msc_iter_remove(iter, msc);
1099 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1101 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1102 unsigned long start = off, tocopy = 0;
1104 if (msc->single_wrap) {
1105 start += msc->single_sz;
1107 tocopy = min(rem, size - start);
1108 if (copy_to_user(buf, msc->base + start, tocopy))
1118 tocopy = min(rem, msc->single_sz - start);
1119 if (copy_to_user(buf, msc->base + start, tocopy))
1128 if (copy_to_user(buf, msc->base + start, rem))
1134 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1135 size_t len, loff_t *ppos)
1137 struct msc_iter *iter = file->private_data;
1138 struct msc *msc = iter->msc;
1143 if (!atomic_inc_unless_negative(&msc->user_count))
1146 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1147 size = msc->single_sz;
1149 size = msc->nr_pages << PAGE_SHIFT;
1157 if (off + len >= size)
1160 if (msc->mode == MSC_MODE_SINGLE) {
1161 ret = msc_single_to_user(msc, buf, off, len);
1164 } else if (msc->mode == MSC_MODE_MULTI) {
1165 struct msc_win_to_user_struct u = {
1170 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1172 *ppos = iter->offset;
1178 atomic_dec(&msc->user_count);
1184 * vm operations callbacks (vm_ops)
1187 static void msc_mmap_open(struct vm_area_struct *vma)
1189 struct msc_iter *iter = vma->vm_file->private_data;
1190 struct msc *msc = iter->msc;
1192 atomic_inc(&msc->mmap_count);
1195 static void msc_mmap_close(struct vm_area_struct *vma)
1197 struct msc_iter *iter = vma->vm_file->private_data;
1198 struct msc *msc = iter->msc;
1201 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1204 /* drop page _refcounts */
1205 for (pg = 0; pg < msc->nr_pages; pg++) {
1206 struct page *page = msc_buffer_get_page(msc, pg);
1208 if (WARN_ON_ONCE(!page))
1212 page->mapping = NULL;
1215 /* last mapping -- drop user_count */
1216 atomic_dec(&msc->user_count);
1217 mutex_unlock(&msc->buf_mutex);
1220 static int msc_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1222 struct msc_iter *iter = vma->vm_file->private_data;
1223 struct msc *msc = iter->msc;
1225 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1227 return VM_FAULT_SIGBUS;
1229 get_page(vmf->page);
1230 vmf->page->mapping = vma->vm_file->f_mapping;
1231 vmf->page->index = vmf->pgoff;
1236 static const struct vm_operations_struct msc_mmap_ops = {
1237 .open = msc_mmap_open,
1238 .close = msc_mmap_close,
1239 .fault = msc_mmap_fault,
1242 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1244 unsigned long size = vma->vm_end - vma->vm_start;
1245 struct msc_iter *iter = vma->vm_file->private_data;
1246 struct msc *msc = iter->msc;
1249 if (!size || offset_in_page(size))
1255 /* grab user_count once per mmap; drop in msc_mmap_close() */
1256 if (!atomic_inc_unless_negative(&msc->user_count))
1259 if (msc->mode != MSC_MODE_SINGLE &&
1260 msc->mode != MSC_MODE_MULTI)
1263 if (size >> PAGE_SHIFT != msc->nr_pages)
1266 atomic_set(&msc->mmap_count, 1);
1271 atomic_dec(&msc->user_count);
1273 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1274 vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1275 vma->vm_ops = &msc_mmap_ops;
1279 static const struct file_operations intel_th_msc_fops = {
1280 .open = intel_th_msc_open,
1281 .release = intel_th_msc_release,
1282 .read = intel_th_msc_read,
1283 .mmap = intel_th_msc_mmap,
1284 .llseek = no_llseek,
1285 .owner = THIS_MODULE,
1288 static int intel_th_msc_init(struct msc *msc)
1290 atomic_set(&msc->user_count, -1);
1292 msc->mode = MSC_MODE_MULTI;
1293 mutex_init(&msc->buf_mutex);
1294 INIT_LIST_HEAD(&msc->win_list);
1295 INIT_LIST_HEAD(&msc->iter_list);
1298 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1304 static const char * const msc_mode[] = {
1305 [MSC_MODE_SINGLE] = "single",
1306 [MSC_MODE_MULTI] = "multi",
1307 [MSC_MODE_EXI] = "ExI",
1308 [MSC_MODE_DEBUG] = "debug",
1312 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1314 struct msc *msc = dev_get_drvdata(dev);
1316 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1320 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1323 struct msc *msc = dev_get_drvdata(dev);
1327 ret = kstrtoul(buf, 10, &val);
1336 static DEVICE_ATTR_RW(wrap);
1339 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1341 struct msc *msc = dev_get_drvdata(dev);
1343 return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
1347 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1350 struct msc *msc = dev_get_drvdata(dev);
1355 if (!capable(CAP_SYS_RAWIO))
1358 cp = memchr(buf, '\n', len);
1362 for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1363 if (!strncmp(msc_mode[i], buf, len))
1369 mutex_lock(&msc->buf_mutex);
1370 ret = msc_buffer_unlocked_free_unless_used(msc);
1373 mutex_unlock(&msc->buf_mutex);
1375 return ret ? ret : size;
1378 static DEVICE_ATTR_RW(mode);
1381 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1383 struct msc *msc = dev_get_drvdata(dev);
1384 struct msc_window *win;
1387 mutex_lock(&msc->buf_mutex);
1389 if (msc->mode == MSC_MODE_SINGLE)
1390 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1391 else if (msc->mode == MSC_MODE_MULTI) {
1392 list_for_each_entry(win, &msc->win_list, entry) {
1393 count += scnprintf(buf + count, PAGE_SIZE - count,
1394 "%d%c", win->nr_blocks,
1395 msc_is_last_win(win) ? '\n' : ',');
1398 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1401 mutex_unlock(&msc->buf_mutex);
1407 nr_pages_store(struct device *dev, struct device_attribute *attr,
1408 const char *buf, size_t size)
1410 struct msc *msc = dev_get_drvdata(dev);
1411 unsigned long val, *win = NULL, *rewin;
1413 const char *p = buf;
1415 int ret, nr_wins = 0;
1417 if (!capable(CAP_SYS_RAWIO))
1420 ret = msc_buffer_free_unless_used(msc);
1424 /* scan the comma-separated list of allocation sizes */
1425 end = memchr(buf, '\n', len);
1430 end = memchr(p, ',', len);
1431 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1437 ret = kstrtoul(s, 10, &val);
1443 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1449 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1456 win[nr_wins - 1] = val;
1461 /* consume the number and the following comma, hence +1 */
1466 mutex_lock(&msc->buf_mutex);
1467 ret = msc_buffer_alloc(msc, win, nr_wins);
1468 mutex_unlock(&msc->buf_mutex);
1473 return ret ? ret : size;
1476 static DEVICE_ATTR_RW(nr_pages);
1478 static struct attribute *msc_output_attrs[] = {
1479 &dev_attr_wrap.attr,
1480 &dev_attr_mode.attr,
1481 &dev_attr_nr_pages.attr,
1485 static struct attribute_group msc_output_group = {
1486 .attrs = msc_output_attrs,
1489 static int intel_th_msc_probe(struct intel_th_device *thdev)
1491 struct device *dev = &thdev->dev;
1492 struct resource *res;
1497 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1501 base = devm_ioremap(dev, res->start, resource_size(res));
1505 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1509 msc->index = thdev->id;
1512 msc->reg_base = base + msc->index * 0x100;
1514 err = intel_th_msc_init(msc);
1518 dev_set_drvdata(dev, msc);
1523 static void intel_th_msc_remove(struct intel_th_device *thdev)
1525 struct msc *msc = dev_get_drvdata(&thdev->dev);
1528 intel_th_msc_deactivate(thdev);
1531 * Buffers should not be used at this point except if the
1532 * output character device is still open and the parent
1533 * device gets detached from its bus, which is a FIXME.
1535 ret = msc_buffer_free_unless_used(msc);
1539 static struct intel_th_driver intel_th_msc_driver = {
1540 .probe = intel_th_msc_probe,
1541 .remove = intel_th_msc_remove,
1542 .activate = intel_th_msc_activate,
1543 .deactivate = intel_th_msc_deactivate,
1544 .fops = &intel_th_msc_fops,
1545 .attr_group = &msc_output_group,
1548 .owner = THIS_MODULE,
1552 module_driver(intel_th_msc_driver,
1553 intel_th_driver_register,
1554 intel_th_driver_unregister);
1556 MODULE_LICENSE("GPL v2");
1557 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1558 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");