1 // SPDX-License-Identifier: GPL-2.0
3 * Intel(R) Trace Hub Memory Storage Unit
5 * Copyright (C) 2014-2015 Intel Corporation.
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/types.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/uaccess.h>
14 #include <linux/sizes.h>
15 #include <linux/printk.h>
16 #include <linux/slab.h>
20 #include <linux/dma-mapping.h>
23 #include <asm/set_memory.h>
29 #define msc_dev(x) (&(x)->thdev->dev)
32 * struct msc_window - multiblock mode window descriptor
33 * @entry: window list linkage (msc::win_list)
34 * @pgoff: page offset into the buffer that this window starts at
35 * @nr_blocks: number of blocks (pages) in this window
36 * @sgt: array of block descriptors
39 struct list_head entry;
41 unsigned int nr_blocks;
47 * struct msc_iter - iterator for msc buffer
48 * @entry: msc::iter_list linkage
49 * @msc: pointer to the MSC device
50 * @start_win: oldest window
51 * @win: current window
52 * @offset: current logical offset into the buffer
53 * @start_block: oldest block in the window
54 * @block: block number in the window
55 * @block_off: offset into current block
56 * @wrap_count: block wrapping handling
57 * @eof: end of buffer reached
60 struct list_head entry;
62 struct msc_window *start_win;
63 struct msc_window *win;
67 unsigned int block_off;
68 unsigned int wrap_count;
73 * struct msc - MSC device representation
74 * @reg_base: register window base address
75 * @thdev: intel_th_device pointer
76 * @win_list: list of windows in multiblock mode
77 * @single_sgt: single mode buffer
78 * @nr_pages: total number of pages allocated for this buffer
79 * @single_sz: amount of data in single mode
80 * @single_wrap: single mode wrap occurred
81 * @base: buffer's base pointer
82 * @base_addr: buffer's base address
83 * @user_count: number of users of the buffer
84 * @mmap_count: number of mappings
85 * @buf_mutex: mutex to serialize access to buffer-related bits
87 * @enabled: MSC is enabled
88 * @wrap: wrapping is enabled
89 * @mode: MSC operating mode
90 * @burst_len: write burst length
91 * @index: number of this MSC in the MSU
94 void __iomem *reg_base;
95 void __iomem *msu_base;
96 struct intel_th_device *thdev;
98 struct list_head win_list;
99 struct sg_table single_sgt;
100 unsigned long nr_pages;
101 unsigned long single_sz;
102 unsigned int single_wrap : 1;
104 dma_addr_t base_addr;
106 /* <0: no buffer, 0: no users, >0: active users */
110 struct mutex buf_mutex;
112 struct list_head iter_list;
115 unsigned int enabled : 1,
119 unsigned int burst_len;
123 static inline bool msc_block_is_empty(struct msc_block_desc *bdesc)
125 /* header hasn't been written */
126 if (!bdesc->valid_dw)
129 /* valid_dw includes the header */
130 if (!msc_data_sz(bdesc))
136 static inline struct msc_block_desc *
137 msc_win_block(struct msc_window *win, unsigned int block)
139 return sg_virt(&win->sgt.sgl[block]);
142 static inline dma_addr_t
143 msc_win_baddr(struct msc_window *win, unsigned int block)
145 return sg_dma_address(&win->sgt.sgl[block]);
148 static inline unsigned long
149 msc_win_bpfn(struct msc_window *win, unsigned int block)
151 return msc_win_baddr(win, block) >> PAGE_SHIFT;
155 * msc_oldest_window() - locate the window with oldest data
158 * This should only be used in multiblock mode. Caller should hold the
159 * msc::user_count reference.
161 * Return: the oldest window with valid data
163 static struct msc_window *msc_oldest_window(struct msc *msc)
165 struct msc_window *win;
166 u32 reg = ioread32(msc->reg_base + REG_MSU_MSC0NWSA);
167 unsigned long win_addr = (unsigned long)reg << PAGE_SHIFT;
168 unsigned int found = 0;
170 if (list_empty(&msc->win_list))
174 * we might need a radix tree for this, depending on how
175 * many windows a typical user would allocate; ideally it's
176 * something like 2, in which case we're good
178 list_for_each_entry(win, &msc->win_list, entry) {
179 if (sg_dma_address(win->sgt.sgl) == win_addr)
182 /* skip the empty ones */
183 if (msc_block_is_empty(msc_win_block(win, 0)))
190 return list_first_entry(&msc->win_list, struct msc_window, entry);
194 * msc_win_oldest_block() - locate the oldest block in a given window
195 * @win: window to look at
197 * Return: index of the block with the oldest data
199 static unsigned int msc_win_oldest_block(struct msc_window *win)
202 struct msc_block_desc *bdesc = msc_win_block(win, 0);
204 /* without wrapping, first block is the oldest */
205 if (!msc_block_wrapped(bdesc))
209 * with wrapping, last written block contains both the newest and the
210 * oldest data for this window.
212 for (blk = 0; blk < win->nr_blocks; blk++) {
213 bdesc = msc_win_block(win, blk);
215 if (msc_block_last_written(bdesc))
223 * msc_is_last_win() - check if a window is the last one for a given MSC
225 * Return: true if @win is the last window in MSC's multiblock buffer
227 static inline bool msc_is_last_win(struct msc_window *win)
229 return win->entry.next == &win->msc->win_list;
233 * msc_next_window() - return next window in the multiblock buffer
234 * @win: current window
236 * Return: window following the current one
238 static struct msc_window *msc_next_window(struct msc_window *win)
240 if (msc_is_last_win(win))
241 return list_first_entry(&win->msc->win_list, struct msc_window,
244 return list_next_entry(win, entry);
247 static struct msc_block_desc *msc_iter_bdesc(struct msc_iter *iter)
249 return msc_win_block(iter->win, iter->block);
252 static void msc_iter_init(struct msc_iter *iter)
254 memset(iter, 0, sizeof(*iter));
255 iter->start_block = -1;
259 static struct msc_iter *msc_iter_install(struct msc *msc)
261 struct msc_iter *iter;
263 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
265 return ERR_PTR(-ENOMEM);
267 mutex_lock(&msc->buf_mutex);
270 * Reading and tracing are mutually exclusive; if msc is
271 * enabled, open() will fail; otherwise existing readers
272 * will prevent enabling the msc and the rest of fops don't
273 * need to worry about it.
277 iter = ERR_PTR(-EBUSY);
284 list_add_tail(&iter->entry, &msc->iter_list);
286 mutex_unlock(&msc->buf_mutex);
291 static void msc_iter_remove(struct msc_iter *iter, struct msc *msc)
293 mutex_lock(&msc->buf_mutex);
294 list_del(&iter->entry);
295 mutex_unlock(&msc->buf_mutex);
300 static void msc_iter_block_start(struct msc_iter *iter)
302 if (iter->start_block != -1)
305 iter->start_block = msc_win_oldest_block(iter->win);
306 iter->block = iter->start_block;
307 iter->wrap_count = 0;
310 * start with the block with oldest data; if data has wrapped
311 * in this window, it should be in this block
313 if (msc_block_wrapped(msc_iter_bdesc(iter)))
314 iter->wrap_count = 2;
318 static int msc_iter_win_start(struct msc_iter *iter, struct msc *msc)
320 /* already started, nothing to do */
324 iter->start_win = msc_oldest_window(msc);
325 if (!iter->start_win)
328 iter->win = iter->start_win;
329 iter->start_block = -1;
331 msc_iter_block_start(iter);
336 static int msc_iter_win_advance(struct msc_iter *iter)
338 iter->win = msc_next_window(iter->win);
339 iter->start_block = -1;
341 if (iter->win == iter->start_win) {
346 msc_iter_block_start(iter);
351 static int msc_iter_block_advance(struct msc_iter *iter)
356 if (iter->wrap_count && iter->block == iter->start_block) {
358 if (!iter->wrap_count)
359 /* copied newest data from the wrapped block */
360 return msc_iter_win_advance(iter);
363 /* no wrapping, check for last written block */
364 if (!iter->wrap_count && msc_block_last_written(msc_iter_bdesc(iter)))
365 /* copied newest data for the window */
366 return msc_iter_win_advance(iter);
369 if (++iter->block == iter->win->nr_blocks)
372 /* no wrapping, sanity check in case there is no last written block */
373 if (!iter->wrap_count && iter->block == iter->start_block)
374 return msc_iter_win_advance(iter);
380 * msc_buffer_iterate() - go through multiblock buffer's data
381 * @iter: iterator structure
382 * @size: amount of data to scan
383 * @data: callback's private data
384 * @fn: iterator callback
386 * This will start at the window which will be written to next (containing
387 * the oldest data) and work its way to the current window, calling @fn
388 * for each chunk of data as it goes.
390 * Caller should have msc::user_count reference to make sure the buffer
391 * doesn't disappear from under us.
393 * Return: amount of data actually scanned.
396 msc_buffer_iterate(struct msc_iter *iter, size_t size, void *data,
397 unsigned long (*fn)(void *, void *, size_t))
399 struct msc *msc = iter->msc;
401 unsigned int advance;
406 /* start with the oldest window */
407 if (msc_iter_win_start(iter, msc))
411 unsigned long data_bytes = msc_data_sz(msc_iter_bdesc(iter));
412 void *src = (void *)msc_iter_bdesc(iter) + MSC_BDESC;
413 size_t tocopy = data_bytes, copied = 0;
414 size_t remaining = 0;
419 * If block wrapping happened, we need to visit the last block
420 * twice, because it contains both the oldest and the newest
421 * data in this window.
423 * First time (wrap_count==2), in the very beginning, to collect
424 * the oldest data, which is in the range
425 * (data_bytes..DATA_IN_PAGE).
427 * Second time (wrap_count==1), it's just like any other block,
428 * containing data in the range of [MSC_BDESC..data_bytes].
430 if (iter->block == iter->start_block && iter->wrap_count == 2) {
431 tocopy = DATA_IN_PAGE - data_bytes;
438 tocopy -= iter->block_off;
439 src += iter->block_off;
446 remaining = fn(data, src, tocopy);
451 copied = tocopy - remaining;
453 iter->block_off += copied;
454 iter->offset += copied;
460 if (msc_iter_block_advance(iter))
469 * msc_buffer_clear_hw_header() - clear hw header for multiblock
472 static void msc_buffer_clear_hw_header(struct msc *msc)
474 struct msc_window *win;
476 list_for_each_entry(win, &msc->win_list, entry) {
478 size_t hw_sz = sizeof(struct msc_block_desc) -
479 offsetof(struct msc_block_desc, hw_tag);
481 for (blk = 0; blk < win->nr_blocks; blk++) {
482 struct msc_block_desc *bdesc = msc_win_block(win, blk);
484 memset(&bdesc->hw_tag, 0, hw_sz);
489 static int intel_th_msu_init(struct msc *msc)
496 mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
497 mintctl |= msc->index ? M1BLIE : M0BLIE;
498 iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
499 if (mintctl != ioread32(msc->msu_base + REG_MSU_MINTCTL)) {
500 dev_info(msc_dev(msc), "MINTCTL ignores writes: no usable interrupts\n");
505 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
506 iowrite32(msusts, msc->msu_base + REG_MSU_MSUSTS);
511 static void intel_th_msu_deinit(struct msc *msc)
518 mintctl = ioread32(msc->msu_base + REG_MSU_MINTCTL);
519 mintctl &= msc->index ? ~M1BLIE : ~M0BLIE;
520 iowrite32(mintctl, msc->msu_base + REG_MSU_MINTCTL);
524 * msc_configure() - set up MSC hardware
525 * @msc: the MSC device to configure
527 * Program storage mode, wrapping, burst length and trace buffer address
528 * into a given MSC. Then, enable tracing and set msc::enabled.
529 * The latter is serialized on msc::buf_mutex, so make sure to hold it.
531 static int msc_configure(struct msc *msc)
535 lockdep_assert_held(&msc->buf_mutex);
537 if (msc->mode > MSC_MODE_MULTI)
540 if (msc->mode == MSC_MODE_MULTI)
541 msc_buffer_clear_hw_header(msc);
543 reg = msc->base_addr >> PAGE_SHIFT;
544 iowrite32(reg, msc->reg_base + REG_MSU_MSC0BAR);
546 if (msc->mode == MSC_MODE_SINGLE) {
548 iowrite32(reg, msc->reg_base + REG_MSU_MSC0SIZE);
551 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
552 reg &= ~(MSC_MODE | MSC_WRAPEN | MSC_EN | MSC_RD_HDR_OVRD);
555 reg |= msc->mode << __ffs(MSC_MODE);
556 reg |= msc->burst_len << __ffs(MSC_LEN);
561 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
563 msc->thdev->output.multiblock = msc->mode == MSC_MODE_MULTI;
564 intel_th_trace_enable(msc->thdev);
572 * msc_disable() - disable MSC hardware
573 * @msc: MSC device to disable
575 * If @msc is enabled, disable tracing on the switch and then disable MSC
576 * storage. Caller must hold msc::buf_mutex.
578 static void msc_disable(struct msc *msc)
582 lockdep_assert_held(&msc->buf_mutex);
584 intel_th_trace_disable(msc->thdev);
586 if (msc->mode == MSC_MODE_SINGLE) {
587 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
588 msc->single_wrap = !!(reg & MSCSTS_WRAPSTAT);
590 reg = ioread32(msc->reg_base + REG_MSU_MSC0MWP);
591 msc->single_sz = reg & ((msc->nr_pages << PAGE_SHIFT) - 1);
592 dev_dbg(msc_dev(msc), "MSCnMWP: %08x/%08lx, wrap: %d\n",
593 reg, msc->single_sz, msc->single_wrap);
596 reg = ioread32(msc->reg_base + REG_MSU_MSC0CTL);
598 iowrite32(reg, msc->reg_base + REG_MSU_MSC0CTL);
601 iowrite32(0, msc->reg_base + REG_MSU_MSC0BAR);
602 iowrite32(0, msc->reg_base + REG_MSU_MSC0SIZE);
604 dev_dbg(msc_dev(msc), "MSCnNWSA: %08x\n",
605 ioread32(msc->reg_base + REG_MSU_MSC0NWSA));
607 reg = ioread32(msc->reg_base + REG_MSU_MSC0STS);
608 dev_dbg(msc_dev(msc), "MSCnSTS: %08x\n", reg);
611 static int intel_th_msc_activate(struct intel_th_device *thdev)
613 struct msc *msc = dev_get_drvdata(&thdev->dev);
616 if (!atomic_inc_unless_negative(&msc->user_count))
619 mutex_lock(&msc->buf_mutex);
621 /* if there are readers, refuse */
622 if (list_empty(&msc->iter_list))
623 ret = msc_configure(msc);
625 mutex_unlock(&msc->buf_mutex);
628 atomic_dec(&msc->user_count);
633 static void intel_th_msc_deactivate(struct intel_th_device *thdev)
635 struct msc *msc = dev_get_drvdata(&thdev->dev);
637 mutex_lock(&msc->buf_mutex);
640 atomic_dec(&msc->user_count);
642 mutex_unlock(&msc->buf_mutex);
646 * msc_buffer_contig_alloc() - allocate a contiguous buffer for SINGLE mode
648 * @size: allocation size in bytes
650 * This modifies msc::base, which requires msc::buf_mutex to serialize, so the
651 * caller is expected to hold it.
653 * Return: 0 on success, -errno otherwise.
655 static int msc_buffer_contig_alloc(struct msc *msc, unsigned long size)
657 unsigned long nr_pages = size >> PAGE_SHIFT;
658 unsigned int order = get_order(size);
665 ret = sg_alloc_table(&msc->single_sgt, 1, GFP_KERNEL);
670 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
674 split_page(page, order);
675 sg_set_buf(msc->single_sgt.sgl, page_address(page), size);
677 ret = dma_map_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl, 1,
682 msc->nr_pages = nr_pages;
683 msc->base = page_address(page);
684 msc->base_addr = sg_dma_address(msc->single_sgt.sgl);
689 __free_pages(page, order);
692 sg_free_table(&msc->single_sgt);
699 * msc_buffer_contig_free() - free a contiguous buffer
700 * @msc: MSC configured in SINGLE mode
702 static void msc_buffer_contig_free(struct msc *msc)
706 dma_unmap_sg(msc_dev(msc)->parent->parent, msc->single_sgt.sgl,
708 sg_free_table(&msc->single_sgt);
710 for (off = 0; off < msc->nr_pages << PAGE_SHIFT; off += PAGE_SIZE) {
711 struct page *page = virt_to_page(msc->base + off);
713 page->mapping = NULL;
721 * msc_buffer_contig_get_page() - find a page at a given offset
722 * @msc: MSC configured in SINGLE mode
723 * @pgoff: page offset
725 * Return: page, if @pgoff is within the range, NULL otherwise.
727 static struct page *msc_buffer_contig_get_page(struct msc *msc,
730 if (pgoff >= msc->nr_pages)
733 return virt_to_page(msc->base + (pgoff << PAGE_SHIFT));
736 static int __msc_buffer_win_alloc(struct msc_window *win,
737 unsigned int nr_blocks)
739 struct scatterlist *sg_ptr;
743 ret = sg_alloc_table(&win->sgt, nr_blocks, GFP_KERNEL);
747 for_each_sg(win->sgt.sgl, sg_ptr, nr_blocks, i) {
748 block = dma_alloc_coherent(msc_dev(win->msc)->parent->parent,
749 PAGE_SIZE, &sg_dma_address(sg_ptr),
754 sg_set_buf(sg_ptr, block, PAGE_SIZE);
760 for (i--; i >= 0; i--)
761 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
762 msc_win_block(win, i),
763 msc_win_baddr(win, i));
765 sg_free_table(&win->sgt);
771 * msc_buffer_win_alloc() - alloc a window for a multiblock mode
773 * @nr_blocks: number of pages in this window
775 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
776 * to serialize, so the caller is expected to hold it.
778 * Return: 0 on success, -errno otherwise.
780 static int msc_buffer_win_alloc(struct msc *msc, unsigned int nr_blocks)
782 struct msc_window *win;
783 int ret = -ENOMEM, i;
789 * This limitation hold as long as we need random access to the
790 * block. When that changes, this can go away.
792 if (nr_blocks > SG_MAX_SINGLE_ALLOC)
795 win = kzalloc(sizeof(*win), GFP_KERNEL);
801 if (!list_empty(&msc->win_list)) {
802 struct msc_window *prev = list_last_entry(&msc->win_list,
806 /* This works as long as blocks are page-sized */
807 win->pgoff = prev->pgoff + prev->nr_blocks;
810 ret = __msc_buffer_win_alloc(win, nr_blocks);
815 for (i = 0; i < ret; i++)
816 /* Set the page as uncached */
817 set_memory_uc((unsigned long)msc_win_block(win, i), 1);
820 win->nr_blocks = ret;
822 if (list_empty(&msc->win_list)) {
823 msc->base = msc_win_block(win, 0);
824 msc->base_addr = msc_win_baddr(win, 0);
827 list_add_tail(&win->entry, &msc->win_list);
828 msc->nr_pages += nr_blocks;
838 static void __msc_buffer_win_free(struct msc *msc, struct msc_window *win)
842 for (i = 0; i < win->nr_blocks; i++) {
843 struct page *page = sg_page(&win->sgt.sgl[i]);
845 page->mapping = NULL;
846 dma_free_coherent(msc_dev(win->msc)->parent->parent, PAGE_SIZE,
847 msc_win_block(win, i), msc_win_baddr(win, i));
849 sg_free_table(&win->sgt);
853 * msc_buffer_win_free() - free a window from MSC's window list
855 * @win: window to free
857 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
858 * to serialize, so the caller is expected to hold it.
860 static void msc_buffer_win_free(struct msc *msc, struct msc_window *win)
864 msc->nr_pages -= win->nr_blocks;
866 list_del(&win->entry);
867 if (list_empty(&msc->win_list)) {
873 for (i = 0; i < win->nr_blocks; i++)
874 /* Reset the page to write-back */
875 set_memory_wb((unsigned long)msc_win_block(win, i), 1);
878 __msc_buffer_win_free(msc, win);
884 * msc_buffer_relink() - set up block descriptors for multiblock mode
887 * This traverses msc::win_list, which requires msc::buf_mutex to serialize,
888 * so the caller is expected to hold it.
890 static void msc_buffer_relink(struct msc *msc)
892 struct msc_window *win, *next_win;
894 /* call with msc::mutex locked */
895 list_for_each_entry(win, &msc->win_list, entry) {
900 * Last window's next_win should point to the first window
901 * and MSC_SW_TAG_LASTWIN should be set.
903 if (msc_is_last_win(win)) {
904 sw_tag |= MSC_SW_TAG_LASTWIN;
905 next_win = list_first_entry(&msc->win_list,
906 struct msc_window, entry);
908 next_win = list_next_entry(win, entry);
911 for (blk = 0; blk < win->nr_blocks; blk++) {
912 struct msc_block_desc *bdesc = msc_win_block(win, blk);
914 memset(bdesc, 0, sizeof(*bdesc));
916 bdesc->next_win = msc_win_bpfn(next_win, 0);
919 * Similarly to last window, last block should point
922 if (blk == win->nr_blocks - 1) {
923 sw_tag |= MSC_SW_TAG_LASTBLK;
924 bdesc->next_blk = msc_win_bpfn(win, 0);
926 bdesc->next_blk = msc_win_bpfn(win, blk + 1);
929 bdesc->sw_tag = sw_tag;
930 bdesc->block_sz = PAGE_SIZE / 64;
935 * Make the above writes globally visible before tracing is
936 * enabled to make sure hardware sees them coherently.
941 static void msc_buffer_multi_free(struct msc *msc)
943 struct msc_window *win, *iter;
945 list_for_each_entry_safe(win, iter, &msc->win_list, entry)
946 msc_buffer_win_free(msc, win);
949 static int msc_buffer_multi_alloc(struct msc *msc, unsigned long *nr_pages,
950 unsigned int nr_wins)
954 for (i = 0; i < nr_wins; i++) {
955 ret = msc_buffer_win_alloc(msc, nr_pages[i]);
957 msc_buffer_multi_free(msc);
962 msc_buffer_relink(msc);
968 * msc_buffer_free() - free buffers for MSC
971 * Free MSC's storage buffers.
973 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex to
974 * serialize, so the caller is expected to hold it.
976 static void msc_buffer_free(struct msc *msc)
978 if (msc->mode == MSC_MODE_SINGLE)
979 msc_buffer_contig_free(msc);
980 else if (msc->mode == MSC_MODE_MULTI)
981 msc_buffer_multi_free(msc);
985 * msc_buffer_alloc() - allocate a buffer for MSC
987 * @size: allocation size in bytes
989 * Allocate a storage buffer for MSC, depending on the msc::mode, it will be
990 * either done via msc_buffer_contig_alloc() for SINGLE operation mode or
991 * msc_buffer_win_alloc() for multiblock operation. The latter allocates one
992 * window per invocation, so in multiblock mode this can be called multiple
993 * times for the same MSC to allocate multiple windows.
995 * This modifies msc::win_list and msc::base, which requires msc::buf_mutex
996 * to serialize, so the caller is expected to hold it.
998 * Return: 0 on success, -errno otherwise.
1000 static int msc_buffer_alloc(struct msc *msc, unsigned long *nr_pages,
1001 unsigned int nr_wins)
1005 /* -1: buffer not allocated */
1006 if (atomic_read(&msc->user_count) != -1)
1009 if (msc->mode == MSC_MODE_SINGLE) {
1013 ret = msc_buffer_contig_alloc(msc, nr_pages[0] << PAGE_SHIFT);
1014 } else if (msc->mode == MSC_MODE_MULTI) {
1015 ret = msc_buffer_multi_alloc(msc, nr_pages, nr_wins);
1021 /* allocation should be visible before the counter goes to 0 */
1022 smp_mb__before_atomic();
1024 if (WARN_ON_ONCE(atomic_cmpxchg(&msc->user_count, -1, 0) != -1))
1032 * msc_buffer_unlocked_free_unless_used() - free a buffer unless it's in use
1035 * This will free MSC buffer unless it is in use or there is no allocated
1037 * Caller needs to hold msc::buf_mutex.
1039 * Return: 0 on successful deallocation or if there was no buffer to
1040 * deallocate, -EBUSY if there are active users.
1042 static int msc_buffer_unlocked_free_unless_used(struct msc *msc)
1046 count = atomic_cmpxchg(&msc->user_count, 0, -1);
1048 /* > 0: buffer is allocated and has users */
1051 /* 0: buffer is allocated, no users */
1053 msc_buffer_free(msc);
1054 /* < 0: no buffer, nothing to do */
1060 * msc_buffer_free_unless_used() - free a buffer unless it's in use
1063 * This is a locked version of msc_buffer_unlocked_free_unless_used().
1065 static int msc_buffer_free_unless_used(struct msc *msc)
1069 mutex_lock(&msc->buf_mutex);
1070 ret = msc_buffer_unlocked_free_unless_used(msc);
1071 mutex_unlock(&msc->buf_mutex);
1077 * msc_buffer_get_page() - get MSC buffer page at a given offset
1079 * @pgoff: page offset into the storage buffer
1081 * This traverses msc::win_list, so holding msc::buf_mutex is expected from
1084 * Return: page if @pgoff corresponds to a valid buffer page or NULL.
1086 static struct page *msc_buffer_get_page(struct msc *msc, unsigned long pgoff)
1088 struct msc_window *win;
1090 if (msc->mode == MSC_MODE_SINGLE)
1091 return msc_buffer_contig_get_page(msc, pgoff);
1093 list_for_each_entry(win, &msc->win_list, entry)
1094 if (pgoff >= win->pgoff && pgoff < win->pgoff + win->nr_blocks)
1100 pgoff -= win->pgoff;
1101 return sg_page(&win->sgt.sgl[pgoff]);
1105 * struct msc_win_to_user_struct - data for copy_to_user() callback
1106 * @buf: userspace buffer to copy data to
1107 * @offset: running offset
1109 struct msc_win_to_user_struct {
1111 unsigned long offset;
1115 * msc_win_to_user() - iterator for msc_buffer_iterate() to copy data to user
1116 * @data: callback's private data
1117 * @src: source buffer
1118 * @len: amount of data to copy from the source buffer
1120 static unsigned long msc_win_to_user(void *data, void *src, size_t len)
1122 struct msc_win_to_user_struct *u = data;
1125 ret = copy_to_user(u->buf + u->offset, src, len);
1126 u->offset += len - ret;
1133 * file operations' callbacks
1136 static int intel_th_msc_open(struct inode *inode, struct file *file)
1138 struct intel_th_device *thdev = file->private_data;
1139 struct msc *msc = dev_get_drvdata(&thdev->dev);
1140 struct msc_iter *iter;
1142 if (!capable(CAP_SYS_RAWIO))
1145 iter = msc_iter_install(msc);
1147 return PTR_ERR(iter);
1149 file->private_data = iter;
1151 return nonseekable_open(inode, file);
1154 static int intel_th_msc_release(struct inode *inode, struct file *file)
1156 struct msc_iter *iter = file->private_data;
1157 struct msc *msc = iter->msc;
1159 msc_iter_remove(iter, msc);
1165 msc_single_to_user(struct msc *msc, char __user *buf, loff_t off, size_t len)
1167 unsigned long size = msc->nr_pages << PAGE_SHIFT, rem = len;
1168 unsigned long start = off, tocopy = 0;
1170 if (msc->single_wrap) {
1171 start += msc->single_sz;
1173 tocopy = min(rem, size - start);
1174 if (copy_to_user(buf, msc->base + start, tocopy))
1184 tocopy = min(rem, msc->single_sz - start);
1185 if (copy_to_user(buf, msc->base + start, tocopy))
1194 if (copy_to_user(buf, msc->base + start, rem))
1200 static ssize_t intel_th_msc_read(struct file *file, char __user *buf,
1201 size_t len, loff_t *ppos)
1203 struct msc_iter *iter = file->private_data;
1204 struct msc *msc = iter->msc;
1209 if (!atomic_inc_unless_negative(&msc->user_count))
1212 if (msc->mode == MSC_MODE_SINGLE && !msc->single_wrap)
1213 size = msc->single_sz;
1215 size = msc->nr_pages << PAGE_SHIFT;
1223 if (off + len >= size)
1226 if (msc->mode == MSC_MODE_SINGLE) {
1227 ret = msc_single_to_user(msc, buf, off, len);
1230 } else if (msc->mode == MSC_MODE_MULTI) {
1231 struct msc_win_to_user_struct u = {
1236 ret = msc_buffer_iterate(iter, len, &u, msc_win_to_user);
1238 *ppos = iter->offset;
1244 atomic_dec(&msc->user_count);
1250 * vm operations callbacks (vm_ops)
1253 static void msc_mmap_open(struct vm_area_struct *vma)
1255 struct msc_iter *iter = vma->vm_file->private_data;
1256 struct msc *msc = iter->msc;
1258 atomic_inc(&msc->mmap_count);
1261 static void msc_mmap_close(struct vm_area_struct *vma)
1263 struct msc_iter *iter = vma->vm_file->private_data;
1264 struct msc *msc = iter->msc;
1267 if (!atomic_dec_and_mutex_lock(&msc->mmap_count, &msc->buf_mutex))
1270 /* drop page _refcounts */
1271 for (pg = 0; pg < msc->nr_pages; pg++) {
1272 struct page *page = msc_buffer_get_page(msc, pg);
1274 if (WARN_ON_ONCE(!page))
1278 page->mapping = NULL;
1281 /* last mapping -- drop user_count */
1282 atomic_dec(&msc->user_count);
1283 mutex_unlock(&msc->buf_mutex);
1286 static vm_fault_t msc_mmap_fault(struct vm_fault *vmf)
1288 struct msc_iter *iter = vmf->vma->vm_file->private_data;
1289 struct msc *msc = iter->msc;
1291 vmf->page = msc_buffer_get_page(msc, vmf->pgoff);
1293 return VM_FAULT_SIGBUS;
1295 get_page(vmf->page);
1296 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
1297 vmf->page->index = vmf->pgoff;
1302 static const struct vm_operations_struct msc_mmap_ops = {
1303 .open = msc_mmap_open,
1304 .close = msc_mmap_close,
1305 .fault = msc_mmap_fault,
1308 static int intel_th_msc_mmap(struct file *file, struct vm_area_struct *vma)
1310 unsigned long size = vma->vm_end - vma->vm_start;
1311 struct msc_iter *iter = vma->vm_file->private_data;
1312 struct msc *msc = iter->msc;
1315 if (!size || offset_in_page(size))
1321 /* grab user_count once per mmap; drop in msc_mmap_close() */
1322 if (!atomic_inc_unless_negative(&msc->user_count))
1325 if (msc->mode != MSC_MODE_SINGLE &&
1326 msc->mode != MSC_MODE_MULTI)
1329 if (size >> PAGE_SHIFT != msc->nr_pages)
1332 atomic_set(&msc->mmap_count, 1);
1337 atomic_dec(&msc->user_count);
1339 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1340 vma->vm_flags |= VM_DONTEXPAND | VM_DONTCOPY;
1341 vma->vm_ops = &msc_mmap_ops;
1345 static const struct file_operations intel_th_msc_fops = {
1346 .open = intel_th_msc_open,
1347 .release = intel_th_msc_release,
1348 .read = intel_th_msc_read,
1349 .mmap = intel_th_msc_mmap,
1350 .llseek = no_llseek,
1351 .owner = THIS_MODULE,
1354 static void intel_th_msc_wait_empty(struct intel_th_device *thdev)
1356 struct msc *msc = dev_get_drvdata(&thdev->dev);
1357 unsigned long count;
1360 for (reg = 0, count = MSC_PLE_WAITLOOP_DEPTH;
1361 count && !(reg & MSCSTS_PLE); count--) {
1362 reg = __raw_readl(msc->reg_base + REG_MSU_MSC0STS);
1367 dev_dbg(msc_dev(msc), "timeout waiting for MSC0 PLE\n");
1370 static int intel_th_msc_init(struct msc *msc)
1372 atomic_set(&msc->user_count, -1);
1374 msc->mode = MSC_MODE_MULTI;
1375 mutex_init(&msc->buf_mutex);
1376 INIT_LIST_HEAD(&msc->win_list);
1377 INIT_LIST_HEAD(&msc->iter_list);
1380 (ioread32(msc->reg_base + REG_MSU_MSC0CTL) & MSC_LEN) >>
1386 static irqreturn_t intel_th_msc_interrupt(struct intel_th_device *thdev)
1388 struct msc *msc = dev_get_drvdata(&thdev->dev);
1389 u32 msusts = ioread32(msc->msu_base + REG_MSU_MSUSTS);
1390 u32 mask = msc->index ? MSUSTS_MSC1BLAST : MSUSTS_MSC0BLAST;
1392 if (!(msusts & mask)) {
1401 static const char * const msc_mode[] = {
1402 [MSC_MODE_SINGLE] = "single",
1403 [MSC_MODE_MULTI] = "multi",
1404 [MSC_MODE_EXI] = "ExI",
1405 [MSC_MODE_DEBUG] = "debug",
1409 wrap_show(struct device *dev, struct device_attribute *attr, char *buf)
1411 struct msc *msc = dev_get_drvdata(dev);
1413 return scnprintf(buf, PAGE_SIZE, "%d\n", msc->wrap);
1417 wrap_store(struct device *dev, struct device_attribute *attr, const char *buf,
1420 struct msc *msc = dev_get_drvdata(dev);
1424 ret = kstrtoul(buf, 10, &val);
1433 static DEVICE_ATTR_RW(wrap);
1436 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
1438 struct msc *msc = dev_get_drvdata(dev);
1440 return scnprintf(buf, PAGE_SIZE, "%s\n", msc_mode[msc->mode]);
1444 mode_store(struct device *dev, struct device_attribute *attr, const char *buf,
1447 struct msc *msc = dev_get_drvdata(dev);
1452 if (!capable(CAP_SYS_RAWIO))
1455 cp = memchr(buf, '\n', len);
1459 for (i = 0; i < ARRAY_SIZE(msc_mode); i++)
1460 if (!strncmp(msc_mode[i], buf, len))
1466 mutex_lock(&msc->buf_mutex);
1467 ret = msc_buffer_unlocked_free_unless_used(msc);
1470 mutex_unlock(&msc->buf_mutex);
1472 return ret ? ret : size;
1475 static DEVICE_ATTR_RW(mode);
1478 nr_pages_show(struct device *dev, struct device_attribute *attr, char *buf)
1480 struct msc *msc = dev_get_drvdata(dev);
1481 struct msc_window *win;
1484 mutex_lock(&msc->buf_mutex);
1486 if (msc->mode == MSC_MODE_SINGLE)
1487 count = scnprintf(buf, PAGE_SIZE, "%ld\n", msc->nr_pages);
1488 else if (msc->mode == MSC_MODE_MULTI) {
1489 list_for_each_entry(win, &msc->win_list, entry) {
1490 count += scnprintf(buf + count, PAGE_SIZE - count,
1491 "%d%c", win->nr_blocks,
1492 msc_is_last_win(win) ? '\n' : ',');
1495 count = scnprintf(buf, PAGE_SIZE, "unsupported\n");
1498 mutex_unlock(&msc->buf_mutex);
1504 nr_pages_store(struct device *dev, struct device_attribute *attr,
1505 const char *buf, size_t size)
1507 struct msc *msc = dev_get_drvdata(dev);
1508 unsigned long val, *win = NULL, *rewin;
1510 const char *p = buf;
1512 int ret, nr_wins = 0;
1514 if (!capable(CAP_SYS_RAWIO))
1517 ret = msc_buffer_free_unless_used(msc);
1521 /* scan the comma-separated list of allocation sizes */
1522 end = memchr(buf, '\n', len);
1527 end = memchr(p, ',', len);
1528 s = kstrndup(p, end ? end - p : len, GFP_KERNEL);
1534 ret = kstrtoul(s, 10, &val);
1540 if (nr_wins && msc->mode == MSC_MODE_SINGLE) {
1546 rewin = krealloc(win, sizeof(*win) * nr_wins, GFP_KERNEL);
1553 win[nr_wins - 1] = val;
1558 /* consume the number and the following comma, hence +1 */
1563 mutex_lock(&msc->buf_mutex);
1564 ret = msc_buffer_alloc(msc, win, nr_wins);
1565 mutex_unlock(&msc->buf_mutex);
1570 return ret ? ret : size;
1573 static DEVICE_ATTR_RW(nr_pages);
1576 win_switch_store(struct device *dev, struct device_attribute *attr,
1577 const char *buf, size_t size)
1579 struct msc *msc = dev_get_drvdata(dev);
1583 ret = kstrtoul(buf, 10, &val);
1590 mutex_lock(&msc->buf_mutex);
1591 if (msc->mode != MSC_MODE_MULTI)
1594 ret = intel_th_trace_switch(msc->thdev);
1595 mutex_unlock(&msc->buf_mutex);
1597 return ret ? ret : size;
1600 static DEVICE_ATTR_WO(win_switch);
1602 static struct attribute *msc_output_attrs[] = {
1603 &dev_attr_wrap.attr,
1604 &dev_attr_mode.attr,
1605 &dev_attr_nr_pages.attr,
1606 &dev_attr_win_switch.attr,
1610 static struct attribute_group msc_output_group = {
1611 .attrs = msc_output_attrs,
1614 static int intel_th_msc_probe(struct intel_th_device *thdev)
1616 struct device *dev = &thdev->dev;
1617 struct resource *res;
1622 res = intel_th_device_get_resource(thdev, IORESOURCE_MEM, 0);
1626 base = devm_ioremap(dev, res->start, resource_size(res));
1630 msc = devm_kzalloc(dev, sizeof(*msc), GFP_KERNEL);
1634 res = intel_th_device_get_resource(thdev, IORESOURCE_IRQ, 1);
1638 msc->index = thdev->id;
1641 msc->reg_base = base + msc->index * 0x100;
1642 msc->msu_base = base;
1644 err = intel_th_msu_init(msc);
1648 err = intel_th_msc_init(msc);
1652 dev_set_drvdata(dev, msc);
1657 static void intel_th_msc_remove(struct intel_th_device *thdev)
1659 struct msc *msc = dev_get_drvdata(&thdev->dev);
1662 intel_th_msc_deactivate(thdev);
1663 intel_th_msu_deinit(msc);
1666 * Buffers should not be used at this point except if the
1667 * output character device is still open and the parent
1668 * device gets detached from its bus, which is a FIXME.
1670 ret = msc_buffer_free_unless_used(msc);
1674 static struct intel_th_driver intel_th_msc_driver = {
1675 .probe = intel_th_msc_probe,
1676 .remove = intel_th_msc_remove,
1677 .irq = intel_th_msc_interrupt,
1678 .wait_empty = intel_th_msc_wait_empty,
1679 .activate = intel_th_msc_activate,
1680 .deactivate = intel_th_msc_deactivate,
1681 .fops = &intel_th_msc_fops,
1682 .attr_group = &msc_output_group,
1685 .owner = THIS_MODULE,
1689 module_driver(intel_th_msc_driver,
1690 intel_th_driver_register,
1691 intel_th_driver_unregister);
1693 MODULE_LICENSE("GPL v2");
1694 MODULE_DESCRIPTION("Intel(R) Trace Hub Memory Storage Unit driver");
1695 MODULE_AUTHOR("Alexander Shishkin <alexander.shishkin@linux.intel.com>");