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