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