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