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