linux/samples/vfio-mdev/mbochs.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Mediated virtual PCI display host device driver
   4 *
   5 * Emulate enough of qemu stdvga to make bochs-drm.ko happy.  That is
   6 * basically the vram memory bar and the bochs dispi interface vbe
   7 * registers in the mmio register bar.  Specifically it does *not*
   8 * include any legacy vga stuff.  Device looks a lot like "qemu -device
   9 * secondary-vga".
  10 *
  11 *   (c) Gerd Hoffmann <kraxel@redhat.com>
  12 *
  13 * based on mtty driver which is:
  14 *   Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  15 *       Author: Neo Jia <cjia@nvidia.com>
  16 *               Kirti Wankhede <kwankhede@nvidia.com>
  17 *
  18 * This program is free software; you can redistribute it and/or modify
  19 * it under the terms of the GNU General Public License version 2 as
  20 * published by the Free Software Foundation.
  21 */
  22#include <linux/init.h>
  23#include <linux/module.h>
  24#include <linux/device.h>
  25#include <linux/kernel.h>
  26#include <linux/slab.h>
  27#include <linux/vmalloc.h>
  28#include <linux/cdev.h>
  29#include <linux/vfio.h>
  30#include <linux/iommu.h>
  31#include <linux/sysfs.h>
  32#include <linux/mdev.h>
  33#include <linux/pci.h>
  34#include <linux/dma-buf.h>
  35#include <linux/highmem.h>
  36#include <drm/drm_fourcc.h>
  37#include <drm/drm_rect.h>
  38#include <drm/drm_modeset_lock.h>
  39#include <drm/drm_property.h>
  40#include <drm/drm_plane.h>
  41
  42
  43#define VBE_DISPI_INDEX_ID              0x0
  44#define VBE_DISPI_INDEX_XRES            0x1
  45#define VBE_DISPI_INDEX_YRES            0x2
  46#define VBE_DISPI_INDEX_BPP             0x3
  47#define VBE_DISPI_INDEX_ENABLE          0x4
  48#define VBE_DISPI_INDEX_BANK            0x5
  49#define VBE_DISPI_INDEX_VIRT_WIDTH      0x6
  50#define VBE_DISPI_INDEX_VIRT_HEIGHT     0x7
  51#define VBE_DISPI_INDEX_X_OFFSET        0x8
  52#define VBE_DISPI_INDEX_Y_OFFSET        0x9
  53#define VBE_DISPI_INDEX_VIDEO_MEMORY_64K 0xa
  54#define VBE_DISPI_INDEX_COUNT           0xb
  55
  56#define VBE_DISPI_ID0                   0xB0C0
  57#define VBE_DISPI_ID1                   0xB0C1
  58#define VBE_DISPI_ID2                   0xB0C2
  59#define VBE_DISPI_ID3                   0xB0C3
  60#define VBE_DISPI_ID4                   0xB0C4
  61#define VBE_DISPI_ID5                   0xB0C5
  62
  63#define VBE_DISPI_DISABLED              0x00
  64#define VBE_DISPI_ENABLED               0x01
  65#define VBE_DISPI_GETCAPS               0x02
  66#define VBE_DISPI_8BIT_DAC              0x20
  67#define VBE_DISPI_LFB_ENABLED           0x40
  68#define VBE_DISPI_NOCLEARMEM            0x80
  69
  70
  71#define MBOCHS_NAME               "mbochs"
  72#define MBOCHS_CLASS_NAME         "mbochs"
  73
  74#define MBOCHS_CONFIG_SPACE_SIZE  0xff
  75#define MBOCHS_MMIO_BAR_OFFSET    PAGE_SIZE
  76#define MBOCHS_MMIO_BAR_SIZE      PAGE_SIZE
  77#define MBOCHS_MEMORY_BAR_OFFSET  (MBOCHS_MMIO_BAR_OFFSET + \
  78                                   MBOCHS_MMIO_BAR_SIZE)
  79
  80#define STORE_LE16(addr, val)   (*(u16 *)addr = val)
  81#define STORE_LE32(addr, val)   (*(u32 *)addr = val)
  82
  83
  84MODULE_LICENSE("GPL v2");
  85
  86static int max_mbytes = 256;
  87module_param_named(count, max_mbytes, int, 0444);
  88MODULE_PARM_DESC(mem, "megabytes available to " MBOCHS_NAME " devices");
  89
  90
  91#define MBOCHS_TYPE_1 "small"
  92#define MBOCHS_TYPE_2 "medium"
  93#define MBOCHS_TYPE_3 "large"
  94
  95static const struct mbochs_type {
  96        const char *name;
  97        u32 mbytes;
  98} mbochs_types[] = {
  99        {
 100                .name   = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_1,
 101                .mbytes = 4,
 102        }, {
 103                .name   = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_2,
 104                .mbytes = 16,
 105        }, {
 106                .name   = MBOCHS_CLASS_NAME "-" MBOCHS_TYPE_3,
 107                .mbytes = 64,
 108        },
 109};
 110
 111
 112static dev_t            mbochs_devt;
 113static struct class     *mbochs_class;
 114static struct cdev      mbochs_cdev;
 115static struct device    mbochs_dev;
 116static int              mbochs_used_mbytes;
 117
 118struct mbochs_mode {
 119        u32 drm_format;
 120        u32 bytepp;
 121        u32 width;
 122        u32 height;
 123        u32 stride;
 124        u32 __pad;
 125        u64 offset;
 126        u64 size;
 127};
 128
 129struct mbochs_dmabuf {
 130        struct mbochs_mode mode;
 131        u32 id;
 132        struct page **pages;
 133        pgoff_t pagecount;
 134        struct dma_buf *buf;
 135        struct mdev_state *mdev_state;
 136        struct list_head next;
 137        bool unlinked;
 138};
 139
 140/* State of each mdev device */
 141struct mdev_state {
 142        u8 *vconfig;
 143        u64 bar_mask[3];
 144        u32 memory_bar_mask;
 145        struct mutex ops_lock;
 146        struct mdev_device *mdev;
 147        struct vfio_device_info dev_info;
 148
 149        const struct mbochs_type *type;
 150        u16 vbe[VBE_DISPI_INDEX_COUNT];
 151        u64 memsize;
 152        struct page **pages;
 153        pgoff_t pagecount;
 154
 155        struct list_head dmabufs;
 156        u32 active_id;
 157        u32 next_id;
 158};
 159
 160static const char *vbe_name_list[VBE_DISPI_INDEX_COUNT] = {
 161        [VBE_DISPI_INDEX_ID]               = "id",
 162        [VBE_DISPI_INDEX_XRES]             = "xres",
 163        [VBE_DISPI_INDEX_YRES]             = "yres",
 164        [VBE_DISPI_INDEX_BPP]              = "bpp",
 165        [VBE_DISPI_INDEX_ENABLE]           = "enable",
 166        [VBE_DISPI_INDEX_BANK]             = "bank",
 167        [VBE_DISPI_INDEX_VIRT_WIDTH]       = "virt-width",
 168        [VBE_DISPI_INDEX_VIRT_HEIGHT]      = "virt-height",
 169        [VBE_DISPI_INDEX_X_OFFSET]         = "x-offset",
 170        [VBE_DISPI_INDEX_Y_OFFSET]         = "y-offset",
 171        [VBE_DISPI_INDEX_VIDEO_MEMORY_64K] = "video-mem",
 172};
 173
 174static const char *vbe_name(u32 index)
 175{
 176        if (index < ARRAY_SIZE(vbe_name_list))
 177                return vbe_name_list[index];
 178        return "(invalid)";
 179}
 180
 181static struct page *__mbochs_get_page(struct mdev_state *mdev_state,
 182                                      pgoff_t pgoff);
 183static struct page *mbochs_get_page(struct mdev_state *mdev_state,
 184                                    pgoff_t pgoff);
 185
 186static const struct mbochs_type *mbochs_find_type(struct kobject *kobj)
 187{
 188        int i;
 189
 190        for (i = 0; i < ARRAY_SIZE(mbochs_types); i++)
 191                if (strcmp(mbochs_types[i].name, kobj->name) == 0)
 192                        return mbochs_types + i;
 193        return NULL;
 194}
 195
 196static void mbochs_create_config_space(struct mdev_state *mdev_state)
 197{
 198        STORE_LE16((u16 *) &mdev_state->vconfig[PCI_VENDOR_ID],
 199                   0x1234);
 200        STORE_LE16((u16 *) &mdev_state->vconfig[PCI_DEVICE_ID],
 201                   0x1111);
 202        STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_VENDOR_ID],
 203                   PCI_SUBVENDOR_ID_REDHAT_QUMRANET);
 204        STORE_LE16((u16 *) &mdev_state->vconfig[PCI_SUBSYSTEM_ID],
 205                   PCI_SUBDEVICE_ID_QEMU);
 206
 207        STORE_LE16((u16 *) &mdev_state->vconfig[PCI_COMMAND],
 208                   PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
 209        STORE_LE16((u16 *) &mdev_state->vconfig[PCI_CLASS_DEVICE],
 210                   PCI_CLASS_DISPLAY_OTHER);
 211        mdev_state->vconfig[PCI_CLASS_REVISION] =  0x01;
 212
 213        STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_0],
 214                   PCI_BASE_ADDRESS_SPACE_MEMORY |
 215                   PCI_BASE_ADDRESS_MEM_TYPE_32  |
 216                   PCI_BASE_ADDRESS_MEM_PREFETCH);
 217        mdev_state->bar_mask[0] = ~(mdev_state->memsize) + 1;
 218
 219        STORE_LE32((u32 *) &mdev_state->vconfig[PCI_BASE_ADDRESS_2],
 220                   PCI_BASE_ADDRESS_SPACE_MEMORY |
 221                   PCI_BASE_ADDRESS_MEM_TYPE_32);
 222        mdev_state->bar_mask[2] = ~(MBOCHS_MMIO_BAR_SIZE) + 1;
 223}
 224
 225static int mbochs_check_framebuffer(struct mdev_state *mdev_state,
 226                                    struct mbochs_mode *mode)
 227{
 228        struct device *dev = mdev_dev(mdev_state->mdev);
 229        u16 *vbe = mdev_state->vbe;
 230        u32 virt_width;
 231
 232        WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
 233
 234        if (!(vbe[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED))
 235                goto nofb;
 236
 237        memset(mode, 0, sizeof(*mode));
 238        switch (vbe[VBE_DISPI_INDEX_BPP]) {
 239        case 32:
 240                mode->drm_format = DRM_FORMAT_XRGB8888;
 241                mode->bytepp = 4;
 242                break;
 243        default:
 244                dev_info_ratelimited(dev, "%s: bpp %d not supported\n",
 245                                     __func__, vbe[VBE_DISPI_INDEX_BPP]);
 246                goto nofb;
 247        }
 248
 249        mode->width  = vbe[VBE_DISPI_INDEX_XRES];
 250        mode->height = vbe[VBE_DISPI_INDEX_YRES];
 251        virt_width  = vbe[VBE_DISPI_INDEX_VIRT_WIDTH];
 252        if (virt_width < mode->width)
 253                virt_width = mode->width;
 254        mode->stride = virt_width * mode->bytepp;
 255        mode->size   = (u64)mode->stride * mode->height;
 256        mode->offset = ((u64)vbe[VBE_DISPI_INDEX_X_OFFSET] * mode->bytepp +
 257                       (u64)vbe[VBE_DISPI_INDEX_Y_OFFSET] * mode->stride);
 258
 259        if (mode->width < 64 || mode->height < 64) {
 260                dev_info_ratelimited(dev, "%s: invalid resolution %dx%d\n",
 261                                     __func__, mode->width, mode->height);
 262                goto nofb;
 263        }
 264        if (mode->offset + mode->size > mdev_state->memsize) {
 265                dev_info_ratelimited(dev, "%s: framebuffer memory overflow\n",
 266                                     __func__);
 267                goto nofb;
 268        }
 269
 270        return 0;
 271
 272nofb:
 273        memset(mode, 0, sizeof(*mode));
 274        return -EINVAL;
 275}
 276
 277static bool mbochs_modes_equal(struct mbochs_mode *mode1,
 278                               struct mbochs_mode *mode2)
 279{
 280        return memcmp(mode1, mode2, sizeof(struct mbochs_mode)) == 0;
 281}
 282
 283static void handle_pci_cfg_write(struct mdev_state *mdev_state, u16 offset,
 284                                 char *buf, u32 count)
 285{
 286        struct device *dev = mdev_dev(mdev_state->mdev);
 287        int index = (offset - PCI_BASE_ADDRESS_0) / 0x04;
 288        u32 cfg_addr;
 289
 290        switch (offset) {
 291        case PCI_BASE_ADDRESS_0:
 292        case PCI_BASE_ADDRESS_2:
 293                cfg_addr = *(u32 *)buf;
 294
 295                if (cfg_addr == 0xffffffff) {
 296                        cfg_addr = (cfg_addr & mdev_state->bar_mask[index]);
 297                } else {
 298                        cfg_addr &= PCI_BASE_ADDRESS_MEM_MASK;
 299                        if (cfg_addr)
 300                                dev_info(dev, "BAR #%d @ 0x%x\n",
 301                                         index, cfg_addr);
 302                }
 303
 304                cfg_addr |= (mdev_state->vconfig[offset] &
 305                             ~PCI_BASE_ADDRESS_MEM_MASK);
 306                STORE_LE32(&mdev_state->vconfig[offset], cfg_addr);
 307                break;
 308        }
 309}
 310
 311static void handle_mmio_write(struct mdev_state *mdev_state, u16 offset,
 312                              char *buf, u32 count)
 313{
 314        struct device *dev = mdev_dev(mdev_state->mdev);
 315        int index;
 316        u16 reg16;
 317
 318        switch (offset) {
 319        case 0x400 ... 0x41f: /* vga ioports remapped */
 320                goto unhandled;
 321        case 0x500 ... 0x515: /* bochs dispi interface */
 322                if (count != 2)
 323                        goto unhandled;
 324                index = (offset - 0x500) / 2;
 325                reg16 = *(u16 *)buf;
 326                if (index < ARRAY_SIZE(mdev_state->vbe))
 327                        mdev_state->vbe[index] = reg16;
 328                dev_dbg(dev, "%s: vbe write %d = %d (%s)\n",
 329                        __func__, index, reg16, vbe_name(index));
 330                break;
 331        case 0x600 ... 0x607: /* qemu extended regs */
 332                goto unhandled;
 333        default:
 334unhandled:
 335                dev_dbg(dev, "%s: @0x%03x, count %d (unhandled)\n",
 336                        __func__, offset, count);
 337                break;
 338        }
 339}
 340
 341static void handle_mmio_read(struct mdev_state *mdev_state, u16 offset,
 342                             char *buf, u32 count)
 343{
 344        struct device *dev = mdev_dev(mdev_state->mdev);
 345        u16 reg16 = 0;
 346        int index;
 347
 348        switch (offset) {
 349        case 0x500 ... 0x515: /* bochs dispi interface */
 350                if (count != 2)
 351                        goto unhandled;
 352                index = (offset - 0x500) / 2;
 353                if (index < ARRAY_SIZE(mdev_state->vbe))
 354                        reg16 = mdev_state->vbe[index];
 355                dev_dbg(dev, "%s: vbe read %d = %d (%s)\n",
 356                        __func__, index, reg16, vbe_name(index));
 357                *(u16 *)buf = reg16;
 358                break;
 359        default:
 360unhandled:
 361                dev_dbg(dev, "%s: @0x%03x, count %d (unhandled)\n",
 362                        __func__, offset, count);
 363                memset(buf, 0, count);
 364                break;
 365        }
 366}
 367
 368static ssize_t mdev_access(struct mdev_device *mdev, char *buf, size_t count,
 369                           loff_t pos, bool is_write)
 370{
 371        struct mdev_state *mdev_state = mdev_get_drvdata(mdev);
 372        struct device *dev = mdev_dev(mdev);
 373        struct page *pg;
 374        loff_t poff;
 375        char *map;
 376        int ret = 0;
 377
 378        mutex_lock(&mdev_state->ops_lock);
 379
 380        if (pos < MBOCHS_CONFIG_SPACE_SIZE) {
 381                if (is_write)
 382                        handle_pci_cfg_write(mdev_state, pos, buf, count);
 383                else
 384                        memcpy(buf, (mdev_state->vconfig + pos), count);
 385
 386        } else if (pos >= MBOCHS_MMIO_BAR_OFFSET &&
 387                   pos + count <= MBOCHS_MEMORY_BAR_OFFSET) {
 388                pos -= MBOCHS_MMIO_BAR_OFFSET;
 389                if (is_write)
 390                        handle_mmio_write(mdev_state, pos, buf, count);
 391                else
 392                        handle_mmio_read(mdev_state, pos, buf, count);
 393
 394        } else if (pos >= MBOCHS_MEMORY_BAR_OFFSET &&
 395                   pos + count <=
 396                   MBOCHS_MEMORY_BAR_OFFSET + mdev_state->memsize) {
 397                pos -= MBOCHS_MMIO_BAR_OFFSET;
 398                poff = pos & ~PAGE_MASK;
 399                pg = __mbochs_get_page(mdev_state, pos >> PAGE_SHIFT);
 400                map = kmap(pg);
 401                if (is_write)
 402                        memcpy(map + poff, buf, count);
 403                else
 404                        memcpy(buf, map + poff, count);
 405                kunmap(pg);
 406                put_page(pg);
 407
 408        } else {
 409                dev_dbg(dev, "%s: %s @0x%llx (unhandled)\n",
 410                        __func__, is_write ? "WR" : "RD", pos);
 411                ret = -1;
 412                goto accessfailed;
 413        }
 414
 415        ret = count;
 416
 417
 418accessfailed:
 419        mutex_unlock(&mdev_state->ops_lock);
 420
 421        return ret;
 422}
 423
 424static int mbochs_reset(struct mdev_device *mdev)
 425{
 426        struct mdev_state *mdev_state = mdev_get_drvdata(mdev);
 427        u32 size64k = mdev_state->memsize / (64 * 1024);
 428        int i;
 429
 430        for (i = 0; i < ARRAY_SIZE(mdev_state->vbe); i++)
 431                mdev_state->vbe[i] = 0;
 432        mdev_state->vbe[VBE_DISPI_INDEX_ID] = VBE_DISPI_ID5;
 433        mdev_state->vbe[VBE_DISPI_INDEX_VIDEO_MEMORY_64K] = size64k;
 434        return 0;
 435}
 436
 437static int mbochs_create(struct kobject *kobj, struct mdev_device *mdev)
 438{
 439        const struct mbochs_type *type = mbochs_find_type(kobj);
 440        struct device *dev = mdev_dev(mdev);
 441        struct mdev_state *mdev_state;
 442
 443        if (!type)
 444                type = &mbochs_types[0];
 445        if (type->mbytes + mbochs_used_mbytes > max_mbytes)
 446                return -ENOMEM;
 447
 448        mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
 449        if (mdev_state == NULL)
 450                return -ENOMEM;
 451
 452        mdev_state->vconfig = kzalloc(MBOCHS_CONFIG_SPACE_SIZE, GFP_KERNEL);
 453        if (mdev_state->vconfig == NULL)
 454                goto err_mem;
 455
 456        mdev_state->memsize = type->mbytes * 1024 * 1024;
 457        mdev_state->pagecount = mdev_state->memsize >> PAGE_SHIFT;
 458        mdev_state->pages = kcalloc(mdev_state->pagecount,
 459                                    sizeof(struct page *),
 460                                    GFP_KERNEL);
 461        if (!mdev_state->pages)
 462                goto err_mem;
 463
 464        dev_info(dev, "%s: %s, %d MB, %ld pages\n", __func__,
 465                 kobj->name, type->mbytes, mdev_state->pagecount);
 466
 467        mutex_init(&mdev_state->ops_lock);
 468        mdev_state->mdev = mdev;
 469        mdev_set_drvdata(mdev, mdev_state);
 470        INIT_LIST_HEAD(&mdev_state->dmabufs);
 471        mdev_state->next_id = 1;
 472
 473        mdev_state->type = type;
 474        mbochs_create_config_space(mdev_state);
 475        mbochs_reset(mdev);
 476
 477        mbochs_used_mbytes += type->mbytes;
 478        return 0;
 479
 480err_mem:
 481        kfree(mdev_state->vconfig);
 482        kfree(mdev_state);
 483        return -ENOMEM;
 484}
 485
 486static int mbochs_remove(struct mdev_device *mdev)
 487{
 488        struct mdev_state *mdev_state = mdev_get_drvdata(mdev);
 489
 490        mbochs_used_mbytes -= mdev_state->type->mbytes;
 491        mdev_set_drvdata(mdev, NULL);
 492        kfree(mdev_state->pages);
 493        kfree(mdev_state->vconfig);
 494        kfree(mdev_state);
 495        return 0;
 496}
 497
 498static ssize_t mbochs_read(struct mdev_device *mdev, char __user *buf,
 499                           size_t count, loff_t *ppos)
 500{
 501        unsigned int done = 0;
 502        int ret;
 503
 504        while (count) {
 505                size_t filled;
 506
 507                if (count >= 4 && !(*ppos % 4)) {
 508                        u32 val;
 509
 510                        ret =  mdev_access(mdev, (char *)&val, sizeof(val),
 511                                           *ppos, false);
 512                        if (ret <= 0)
 513                                goto read_err;
 514
 515                        if (copy_to_user(buf, &val, sizeof(val)))
 516                                goto read_err;
 517
 518                        filled = 4;
 519                } else if (count >= 2 && !(*ppos % 2)) {
 520                        u16 val;
 521
 522                        ret = mdev_access(mdev, (char *)&val, sizeof(val),
 523                                          *ppos, false);
 524                        if (ret <= 0)
 525                                goto read_err;
 526
 527                        if (copy_to_user(buf, &val, sizeof(val)))
 528                                goto read_err;
 529
 530                        filled = 2;
 531                } else {
 532                        u8 val;
 533
 534                        ret = mdev_access(mdev, (char *)&val, sizeof(val),
 535                                          *ppos, false);
 536                        if (ret <= 0)
 537                                goto read_err;
 538
 539                        if (copy_to_user(buf, &val, sizeof(val)))
 540                                goto read_err;
 541
 542                        filled = 1;
 543                }
 544
 545                count -= filled;
 546                done += filled;
 547                *ppos += filled;
 548                buf += filled;
 549        }
 550
 551        return done;
 552
 553read_err:
 554        return -EFAULT;
 555}
 556
 557static ssize_t mbochs_write(struct mdev_device *mdev, const char __user *buf,
 558                            size_t count, loff_t *ppos)
 559{
 560        unsigned int done = 0;
 561        int ret;
 562
 563        while (count) {
 564                size_t filled;
 565
 566                if (count >= 4 && !(*ppos % 4)) {
 567                        u32 val;
 568
 569                        if (copy_from_user(&val, buf, sizeof(val)))
 570                                goto write_err;
 571
 572                        ret = mdev_access(mdev, (char *)&val, sizeof(val),
 573                                          *ppos, true);
 574                        if (ret <= 0)
 575                                goto write_err;
 576
 577                        filled = 4;
 578                } else if (count >= 2 && !(*ppos % 2)) {
 579                        u16 val;
 580
 581                        if (copy_from_user(&val, buf, sizeof(val)))
 582                                goto write_err;
 583
 584                        ret = mdev_access(mdev, (char *)&val, sizeof(val),
 585                                          *ppos, true);
 586                        if (ret <= 0)
 587                                goto write_err;
 588
 589                        filled = 2;
 590                } else {
 591                        u8 val;
 592
 593                        if (copy_from_user(&val, buf, sizeof(val)))
 594                                goto write_err;
 595
 596                        ret = mdev_access(mdev, (char *)&val, sizeof(val),
 597                                          *ppos, true);
 598                        if (ret <= 0)
 599                                goto write_err;
 600
 601                        filled = 1;
 602                }
 603                count -= filled;
 604                done += filled;
 605                *ppos += filled;
 606                buf += filled;
 607        }
 608
 609        return done;
 610write_err:
 611        return -EFAULT;
 612}
 613
 614static struct page *__mbochs_get_page(struct mdev_state *mdev_state,
 615                                      pgoff_t pgoff)
 616{
 617        WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
 618
 619        if (!mdev_state->pages[pgoff]) {
 620                mdev_state->pages[pgoff] =
 621                        alloc_pages(GFP_HIGHUSER | __GFP_ZERO, 0);
 622                if (!mdev_state->pages[pgoff])
 623                        return NULL;
 624        }
 625
 626        get_page(mdev_state->pages[pgoff]);
 627        return mdev_state->pages[pgoff];
 628}
 629
 630static struct page *mbochs_get_page(struct mdev_state *mdev_state,
 631                                    pgoff_t pgoff)
 632{
 633        struct page *page;
 634
 635        if (WARN_ON(pgoff >= mdev_state->pagecount))
 636                return NULL;
 637
 638        mutex_lock(&mdev_state->ops_lock);
 639        page = __mbochs_get_page(mdev_state, pgoff);
 640        mutex_unlock(&mdev_state->ops_lock);
 641
 642        return page;
 643}
 644
 645static void mbochs_put_pages(struct mdev_state *mdev_state)
 646{
 647        struct device *dev = mdev_dev(mdev_state->mdev);
 648        int i, count = 0;
 649
 650        WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
 651
 652        for (i = 0; i < mdev_state->pagecount; i++) {
 653                if (!mdev_state->pages[i])
 654                        continue;
 655                put_page(mdev_state->pages[i]);
 656                mdev_state->pages[i] = NULL;
 657                count++;
 658        }
 659        dev_dbg(dev, "%s: %d pages released\n", __func__, count);
 660}
 661
 662static vm_fault_t mbochs_region_vm_fault(struct vm_fault *vmf)
 663{
 664        struct vm_area_struct *vma = vmf->vma;
 665        struct mdev_state *mdev_state = vma->vm_private_data;
 666        pgoff_t page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
 667
 668        if (page_offset >= mdev_state->pagecount)
 669                return VM_FAULT_SIGBUS;
 670
 671        vmf->page = mbochs_get_page(mdev_state, page_offset);
 672        if (!vmf->page)
 673                return VM_FAULT_SIGBUS;
 674
 675        return 0;
 676}
 677
 678static const struct vm_operations_struct mbochs_region_vm_ops = {
 679        .fault = mbochs_region_vm_fault,
 680};
 681
 682static int mbochs_mmap(struct mdev_device *mdev, struct vm_area_struct *vma)
 683{
 684        struct mdev_state *mdev_state = mdev_get_drvdata(mdev);
 685
 686        if (vma->vm_pgoff != MBOCHS_MEMORY_BAR_OFFSET >> PAGE_SHIFT)
 687                return -EINVAL;
 688        if (vma->vm_end < vma->vm_start)
 689                return -EINVAL;
 690        if (vma->vm_end - vma->vm_start > mdev_state->memsize)
 691                return -EINVAL;
 692        if ((vma->vm_flags & VM_SHARED) == 0)
 693                return -EINVAL;
 694
 695        vma->vm_ops = &mbochs_region_vm_ops;
 696        vma->vm_private_data = mdev_state;
 697        return 0;
 698}
 699
 700static vm_fault_t mbochs_dmabuf_vm_fault(struct vm_fault *vmf)
 701{
 702        struct vm_area_struct *vma = vmf->vma;
 703        struct mbochs_dmabuf *dmabuf = vma->vm_private_data;
 704
 705        if (WARN_ON(vmf->pgoff >= dmabuf->pagecount))
 706                return VM_FAULT_SIGBUS;
 707
 708        vmf->page = dmabuf->pages[vmf->pgoff];
 709        get_page(vmf->page);
 710        return 0;
 711}
 712
 713static const struct vm_operations_struct mbochs_dmabuf_vm_ops = {
 714        .fault = mbochs_dmabuf_vm_fault,
 715};
 716
 717static int mbochs_mmap_dmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
 718{
 719        struct mbochs_dmabuf *dmabuf = buf->priv;
 720        struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
 721
 722        dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
 723
 724        if ((vma->vm_flags & VM_SHARED) == 0)
 725                return -EINVAL;
 726
 727        vma->vm_ops = &mbochs_dmabuf_vm_ops;
 728        vma->vm_private_data = dmabuf;
 729        return 0;
 730}
 731
 732static void mbochs_print_dmabuf(struct mbochs_dmabuf *dmabuf,
 733                                const char *prefix)
 734{
 735        struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
 736        u32 fourcc = dmabuf->mode.drm_format;
 737
 738        dev_dbg(dev, "%s/%d: %c%c%c%c, %dx%d, stride %d, off 0x%llx, size 0x%llx, pages %ld\n",
 739                prefix, dmabuf->id,
 740                fourcc ? ((fourcc >>  0) & 0xff) : '-',
 741                fourcc ? ((fourcc >>  8) & 0xff) : '-',
 742                fourcc ? ((fourcc >> 16) & 0xff) : '-',
 743                fourcc ? ((fourcc >> 24) & 0xff) : '-',
 744                dmabuf->mode.width, dmabuf->mode.height, dmabuf->mode.stride,
 745                dmabuf->mode.offset, dmabuf->mode.size, dmabuf->pagecount);
 746}
 747
 748static struct sg_table *mbochs_map_dmabuf(struct dma_buf_attachment *at,
 749                                          enum dma_data_direction direction)
 750{
 751        struct mbochs_dmabuf *dmabuf = at->dmabuf->priv;
 752        struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
 753        struct sg_table *sg;
 754
 755        dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
 756
 757        sg = kzalloc(sizeof(*sg), GFP_KERNEL);
 758        if (!sg)
 759                goto err1;
 760        if (sg_alloc_table_from_pages(sg, dmabuf->pages, dmabuf->pagecount,
 761                                      0, dmabuf->mode.size, GFP_KERNEL) < 0)
 762                goto err2;
 763        if (!dma_map_sg(at->dev, sg->sgl, sg->nents, direction))
 764                goto err3;
 765
 766        return sg;
 767
 768err3:
 769        sg_free_table(sg);
 770err2:
 771        kfree(sg);
 772err1:
 773        return ERR_PTR(-ENOMEM);
 774}
 775
 776static void mbochs_unmap_dmabuf(struct dma_buf_attachment *at,
 777                                struct sg_table *sg,
 778                                enum dma_data_direction direction)
 779{
 780        struct mbochs_dmabuf *dmabuf = at->dmabuf->priv;
 781        struct device *dev = mdev_dev(dmabuf->mdev_state->mdev);
 782
 783        dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
 784
 785        sg_free_table(sg);
 786        kfree(sg);
 787}
 788
 789static void mbochs_release_dmabuf(struct dma_buf *buf)
 790{
 791        struct mbochs_dmabuf *dmabuf = buf->priv;
 792        struct mdev_state *mdev_state = dmabuf->mdev_state;
 793        struct device *dev = mdev_dev(mdev_state->mdev);
 794        pgoff_t pg;
 795
 796        dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
 797
 798        for (pg = 0; pg < dmabuf->pagecount; pg++)
 799                put_page(dmabuf->pages[pg]);
 800
 801        mutex_lock(&mdev_state->ops_lock);
 802        dmabuf->buf = NULL;
 803        if (dmabuf->unlinked)
 804                kfree(dmabuf);
 805        mutex_unlock(&mdev_state->ops_lock);
 806}
 807
 808static struct dma_buf_ops mbochs_dmabuf_ops = {
 809        .map_dma_buf      = mbochs_map_dmabuf,
 810        .unmap_dma_buf    = mbochs_unmap_dmabuf,
 811        .release          = mbochs_release_dmabuf,
 812        .mmap             = mbochs_mmap_dmabuf,
 813};
 814
 815static struct mbochs_dmabuf *mbochs_dmabuf_alloc(struct mdev_state *mdev_state,
 816                                                 struct mbochs_mode *mode)
 817{
 818        struct mbochs_dmabuf *dmabuf;
 819        pgoff_t page_offset, pg;
 820
 821        WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
 822
 823        dmabuf = kzalloc(sizeof(struct mbochs_dmabuf), GFP_KERNEL);
 824        if (!dmabuf)
 825                return NULL;
 826
 827        dmabuf->mode = *mode;
 828        dmabuf->id = mdev_state->next_id++;
 829        dmabuf->pagecount = DIV_ROUND_UP(mode->size, PAGE_SIZE);
 830        dmabuf->pages = kcalloc(dmabuf->pagecount, sizeof(struct page *),
 831                                GFP_KERNEL);
 832        if (!dmabuf->pages)
 833                goto err_free_dmabuf;
 834
 835        page_offset = dmabuf->mode.offset >> PAGE_SHIFT;
 836        for (pg = 0; pg < dmabuf->pagecount; pg++) {
 837                dmabuf->pages[pg] = __mbochs_get_page(mdev_state,
 838                                                      page_offset + pg);
 839                if (!dmabuf->pages[pg])
 840                        goto err_free_pages;
 841        }
 842
 843        dmabuf->mdev_state = mdev_state;
 844        list_add(&dmabuf->next, &mdev_state->dmabufs);
 845
 846        mbochs_print_dmabuf(dmabuf, __func__);
 847        return dmabuf;
 848
 849err_free_pages:
 850        while (pg > 0)
 851                put_page(dmabuf->pages[--pg]);
 852        kfree(dmabuf->pages);
 853err_free_dmabuf:
 854        kfree(dmabuf);
 855        return NULL;
 856}
 857
 858static struct mbochs_dmabuf *
 859mbochs_dmabuf_find_by_mode(struct mdev_state *mdev_state,
 860                           struct mbochs_mode *mode)
 861{
 862        struct mbochs_dmabuf *dmabuf;
 863
 864        WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
 865
 866        list_for_each_entry(dmabuf, &mdev_state->dmabufs, next)
 867                if (mbochs_modes_equal(&dmabuf->mode, mode))
 868                        return dmabuf;
 869
 870        return NULL;
 871}
 872
 873static struct mbochs_dmabuf *
 874mbochs_dmabuf_find_by_id(struct mdev_state *mdev_state, u32 id)
 875{
 876        struct mbochs_dmabuf *dmabuf;
 877
 878        WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
 879
 880        list_for_each_entry(dmabuf, &mdev_state->dmabufs, next)
 881                if (dmabuf->id == id)
 882                        return dmabuf;
 883
 884        return NULL;
 885}
 886
 887static int mbochs_dmabuf_export(struct mbochs_dmabuf *dmabuf)
 888{
 889        struct mdev_state *mdev_state = dmabuf->mdev_state;
 890        struct device *dev = mdev_dev(mdev_state->mdev);
 891        DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
 892        struct dma_buf *buf;
 893
 894        WARN_ON(!mutex_is_locked(&mdev_state->ops_lock));
 895
 896        if (!IS_ALIGNED(dmabuf->mode.offset, PAGE_SIZE)) {
 897                dev_info_ratelimited(dev, "%s: framebuffer not page-aligned\n",
 898                                     __func__);
 899                return -EINVAL;
 900        }
 901
 902        exp_info.ops = &mbochs_dmabuf_ops;
 903        exp_info.size = dmabuf->mode.size;
 904        exp_info.priv = dmabuf;
 905
 906        buf = dma_buf_export(&exp_info);
 907        if (IS_ERR(buf)) {
 908                dev_info_ratelimited(dev, "%s: dma_buf_export failed: %ld\n",
 909                                     __func__, PTR_ERR(buf));
 910                return PTR_ERR(buf);
 911        }
 912
 913        dmabuf->buf = buf;
 914        dev_dbg(dev, "%s: %d\n", __func__, dmabuf->id);
 915        return 0;
 916}
 917
 918static int mbochs_get_region_info(struct mdev_device *mdev,
 919                                  struct vfio_region_info *region_info,
 920                                  u16 *cap_type_id, void **cap_type)
 921{
 922        struct mdev_state *mdev_state;
 923
 924        mdev_state = mdev_get_drvdata(mdev);
 925        if (!mdev_state)
 926                return -EINVAL;
 927
 928        if (region_info->index >= VFIO_PCI_NUM_REGIONS)
 929                return -EINVAL;
 930
 931        switch (region_info->index) {
 932        case VFIO_PCI_CONFIG_REGION_INDEX:
 933                region_info->offset = 0;
 934                region_info->size   = MBOCHS_CONFIG_SPACE_SIZE;
 935                region_info->flags  = (VFIO_REGION_INFO_FLAG_READ |
 936                                       VFIO_REGION_INFO_FLAG_WRITE);
 937                break;
 938        case VFIO_PCI_BAR0_REGION_INDEX:
 939                region_info->offset = MBOCHS_MEMORY_BAR_OFFSET;
 940                region_info->size   = mdev_state->memsize;
 941                region_info->flags  = (VFIO_REGION_INFO_FLAG_READ  |
 942                                       VFIO_REGION_INFO_FLAG_WRITE |
 943                                       VFIO_REGION_INFO_FLAG_MMAP);
 944                break;
 945        case VFIO_PCI_BAR2_REGION_INDEX:
 946                region_info->offset = MBOCHS_MMIO_BAR_OFFSET;
 947                region_info->size   = MBOCHS_MMIO_BAR_SIZE;
 948                region_info->flags  = (VFIO_REGION_INFO_FLAG_READ  |
 949                                       VFIO_REGION_INFO_FLAG_WRITE);
 950                break;
 951        default:
 952                region_info->size   = 0;
 953                region_info->offset = 0;
 954                region_info->flags  = 0;
 955        }
 956
 957        return 0;
 958}
 959
 960static int mbochs_get_irq_info(struct mdev_device *mdev,
 961                               struct vfio_irq_info *irq_info)
 962{
 963        irq_info->count = 0;
 964        return 0;
 965}
 966
 967static int mbochs_get_device_info(struct mdev_device *mdev,
 968                                  struct vfio_device_info *dev_info)
 969{
 970        dev_info->flags = VFIO_DEVICE_FLAGS_PCI;
 971        dev_info->num_regions = VFIO_PCI_NUM_REGIONS;
 972        dev_info->num_irqs = VFIO_PCI_NUM_IRQS;
 973        return 0;
 974}
 975
 976static int mbochs_query_gfx_plane(struct mdev_device *mdev,
 977                                  struct vfio_device_gfx_plane_info *plane)
 978{
 979        struct mdev_state *mdev_state = mdev_get_drvdata(mdev);
 980        struct device *dev = mdev_dev(mdev);
 981        struct mbochs_dmabuf *dmabuf;
 982        struct mbochs_mode mode;
 983        int ret;
 984
 985        if (plane->flags & VFIO_GFX_PLANE_TYPE_PROBE) {
 986                if (plane->flags == (VFIO_GFX_PLANE_TYPE_PROBE |
 987                                     VFIO_GFX_PLANE_TYPE_DMABUF))
 988                        return 0;
 989                return -EINVAL;
 990        }
 991
 992        if (plane->flags != VFIO_GFX_PLANE_TYPE_DMABUF)
 993                return -EINVAL;
 994
 995        plane->drm_format_mod = 0;
 996        plane->x_pos          = 0;
 997        plane->y_pos          = 0;
 998        plane->x_hot          = 0;
 999        plane->y_hot          = 0;
1000
1001        mutex_lock(&mdev_state->ops_lock);
1002
1003        ret = -EINVAL;
1004        if (plane->drm_plane_type == DRM_PLANE_TYPE_PRIMARY)
1005                ret = mbochs_check_framebuffer(mdev_state, &mode);
1006        if (ret < 0) {
1007                plane->drm_format     = 0;
1008                plane->width          = 0;
1009                plane->height         = 0;
1010                plane->stride         = 0;
1011                plane->size           = 0;
1012                plane->dmabuf_id      = 0;
1013                goto done;
1014        }
1015
1016        dmabuf = mbochs_dmabuf_find_by_mode(mdev_state, &mode);
1017        if (!dmabuf)
1018                mbochs_dmabuf_alloc(mdev_state, &mode);
1019        if (!dmabuf) {
1020                mutex_unlock(&mdev_state->ops_lock);
1021                return -ENOMEM;
1022        }
1023
1024        plane->drm_format     = dmabuf->mode.drm_format;
1025        plane->width          = dmabuf->mode.width;
1026        plane->height         = dmabuf->mode.height;
1027        plane->stride         = dmabuf->mode.stride;
1028        plane->size           = dmabuf->mode.size;
1029        plane->dmabuf_id      = dmabuf->id;
1030
1031done:
1032        if (plane->drm_plane_type == DRM_PLANE_TYPE_PRIMARY &&
1033            mdev_state->active_id != plane->dmabuf_id) {
1034                dev_dbg(dev, "%s: primary: %d => %d\n", __func__,
1035                        mdev_state->active_id, plane->dmabuf_id);
1036                mdev_state->active_id = plane->dmabuf_id;
1037        }
1038        mutex_unlock(&mdev_state->ops_lock);
1039        return 0;
1040}
1041
1042static int mbochs_get_gfx_dmabuf(struct mdev_device *mdev,
1043                                 u32 id)
1044{
1045        struct mdev_state *mdev_state = mdev_get_drvdata(mdev);
1046        struct mbochs_dmabuf *dmabuf;
1047
1048        mutex_lock(&mdev_state->ops_lock);
1049
1050        dmabuf = mbochs_dmabuf_find_by_id(mdev_state, id);
1051        if (!dmabuf) {
1052                mutex_unlock(&mdev_state->ops_lock);
1053                return -ENOENT;
1054        }
1055
1056        if (!dmabuf->buf)
1057                mbochs_dmabuf_export(dmabuf);
1058
1059        mutex_unlock(&mdev_state->ops_lock);
1060
1061        if (!dmabuf->buf)
1062                return -EINVAL;
1063
1064        return dma_buf_fd(dmabuf->buf, 0);
1065}
1066
1067static long mbochs_ioctl(struct mdev_device *mdev, unsigned int cmd,
1068                        unsigned long arg)
1069{
1070        int ret = 0;
1071        unsigned long minsz;
1072        struct mdev_state *mdev_state;
1073
1074        mdev_state = mdev_get_drvdata(mdev);
1075
1076        switch (cmd) {
1077        case VFIO_DEVICE_GET_INFO:
1078        {
1079                struct vfio_device_info info;
1080
1081                minsz = offsetofend(struct vfio_device_info, num_irqs);
1082
1083                if (copy_from_user(&info, (void __user *)arg, minsz))
1084                        return -EFAULT;
1085
1086                if (info.argsz < minsz)
1087                        return -EINVAL;
1088
1089                ret = mbochs_get_device_info(mdev, &info);
1090                if (ret)
1091                        return ret;
1092
1093                memcpy(&mdev_state->dev_info, &info, sizeof(info));
1094
1095                if (copy_to_user((void __user *)arg, &info, minsz))
1096                        return -EFAULT;
1097
1098                return 0;
1099        }
1100        case VFIO_DEVICE_GET_REGION_INFO:
1101        {
1102                struct vfio_region_info info;
1103                u16 cap_type_id = 0;
1104                void *cap_type = NULL;
1105
1106                minsz = offsetofend(struct vfio_region_info, offset);
1107
1108                if (copy_from_user(&info, (void __user *)arg, minsz))
1109                        return -EFAULT;
1110
1111                if (info.argsz < minsz)
1112                        return -EINVAL;
1113
1114                ret = mbochs_get_region_info(mdev, &info, &cap_type_id,
1115                                           &cap_type);
1116                if (ret)
1117                        return ret;
1118
1119                if (copy_to_user((void __user *)arg, &info, minsz))
1120                        return -EFAULT;
1121
1122                return 0;
1123        }
1124
1125        case VFIO_DEVICE_GET_IRQ_INFO:
1126        {
1127                struct vfio_irq_info info;
1128
1129                minsz = offsetofend(struct vfio_irq_info, count);
1130
1131                if (copy_from_user(&info, (void __user *)arg, minsz))
1132                        return -EFAULT;
1133
1134                if ((info.argsz < minsz) ||
1135                    (info.index >= mdev_state->dev_info.num_irqs))
1136                        return -EINVAL;
1137
1138                ret = mbochs_get_irq_info(mdev, &info);
1139                if (ret)
1140                        return ret;
1141
1142                if (copy_to_user((void __user *)arg, &info, minsz))
1143                        return -EFAULT;
1144
1145                return 0;
1146        }
1147
1148        case VFIO_DEVICE_QUERY_GFX_PLANE:
1149        {
1150                struct vfio_device_gfx_plane_info plane;
1151
1152                minsz = offsetofend(struct vfio_device_gfx_plane_info,
1153                                    region_index);
1154
1155                if (copy_from_user(&plane, (void __user *)arg, minsz))
1156                        return -EFAULT;
1157
1158                if (plane.argsz < minsz)
1159                        return -EINVAL;
1160
1161                ret = mbochs_query_gfx_plane(mdev, &plane);
1162                if (ret)
1163                        return ret;
1164
1165                if (copy_to_user((void __user *)arg, &plane, minsz))
1166                        return -EFAULT;
1167
1168                return 0;
1169        }
1170
1171        case VFIO_DEVICE_GET_GFX_DMABUF:
1172        {
1173                u32 dmabuf_id;
1174
1175                if (get_user(dmabuf_id, (__u32 __user *)arg))
1176                        return -EFAULT;
1177
1178                return mbochs_get_gfx_dmabuf(mdev, dmabuf_id);
1179        }
1180
1181        case VFIO_DEVICE_SET_IRQS:
1182                return -EINVAL;
1183
1184        case VFIO_DEVICE_RESET:
1185                return mbochs_reset(mdev);
1186        }
1187        return -ENOTTY;
1188}
1189
1190static int mbochs_open(struct mdev_device *mdev)
1191{
1192        if (!try_module_get(THIS_MODULE))
1193                return -ENODEV;
1194
1195        return 0;
1196}
1197
1198static void mbochs_close(struct mdev_device *mdev)
1199{
1200        struct mdev_state *mdev_state = mdev_get_drvdata(mdev);
1201        struct mbochs_dmabuf *dmabuf, *tmp;
1202
1203        mutex_lock(&mdev_state->ops_lock);
1204
1205        list_for_each_entry_safe(dmabuf, tmp, &mdev_state->dmabufs, next) {
1206                list_del(&dmabuf->next);
1207                if (dmabuf->buf) {
1208                        /* free in mbochs_release_dmabuf() */
1209                        dmabuf->unlinked = true;
1210                } else {
1211                        kfree(dmabuf);
1212                }
1213        }
1214        mbochs_put_pages(mdev_state);
1215
1216        mutex_unlock(&mdev_state->ops_lock);
1217        module_put(THIS_MODULE);
1218}
1219
1220static ssize_t
1221memory_show(struct device *dev, struct device_attribute *attr,
1222            char *buf)
1223{
1224        struct mdev_device *mdev = mdev_from_dev(dev);
1225        struct mdev_state *mdev_state = mdev_get_drvdata(mdev);
1226
1227        return sprintf(buf, "%d MB\n", mdev_state->type->mbytes);
1228}
1229static DEVICE_ATTR_RO(memory);
1230
1231static struct attribute *mdev_dev_attrs[] = {
1232        &dev_attr_memory.attr,
1233        NULL,
1234};
1235
1236static const struct attribute_group mdev_dev_group = {
1237        .name  = "vendor",
1238        .attrs = mdev_dev_attrs,
1239};
1240
1241const struct attribute_group *mdev_dev_groups[] = {
1242        &mdev_dev_group,
1243        NULL,
1244};
1245
1246static ssize_t
1247name_show(struct kobject *kobj, struct device *dev, char *buf)
1248{
1249        return sprintf(buf, "%s\n", kobj->name);
1250}
1251MDEV_TYPE_ATTR_RO(name);
1252
1253static ssize_t
1254description_show(struct kobject *kobj, struct device *dev, char *buf)
1255{
1256        const struct mbochs_type *type = mbochs_find_type(kobj);
1257
1258        return sprintf(buf, "virtual display, %d MB video memory\n",
1259                       type ? type->mbytes  : 0);
1260}
1261MDEV_TYPE_ATTR_RO(description);
1262
1263static ssize_t
1264available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
1265{
1266        const struct mbochs_type *type = mbochs_find_type(kobj);
1267        int count = (max_mbytes - mbochs_used_mbytes) / type->mbytes;
1268
1269        return sprintf(buf, "%d\n", count);
1270}
1271MDEV_TYPE_ATTR_RO(available_instances);
1272
1273static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
1274                               char *buf)
1275{
1276        return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
1277}
1278MDEV_TYPE_ATTR_RO(device_api);
1279
1280static struct attribute *mdev_types_attrs[] = {
1281        &mdev_type_attr_name.attr,
1282        &mdev_type_attr_description.attr,
1283        &mdev_type_attr_device_api.attr,
1284        &mdev_type_attr_available_instances.attr,
1285        NULL,
1286};
1287
1288static struct attribute_group mdev_type_group1 = {
1289        .name  = MBOCHS_TYPE_1,
1290        .attrs = mdev_types_attrs,
1291};
1292
1293static struct attribute_group mdev_type_group2 = {
1294        .name  = MBOCHS_TYPE_2,
1295        .attrs = mdev_types_attrs,
1296};
1297
1298static struct attribute_group mdev_type_group3 = {
1299        .name  = MBOCHS_TYPE_3,
1300        .attrs = mdev_types_attrs,
1301};
1302
1303static struct attribute_group *mdev_type_groups[] = {
1304        &mdev_type_group1,
1305        &mdev_type_group2,
1306        &mdev_type_group3,
1307        NULL,
1308};
1309
1310static const struct mdev_parent_ops mdev_fops = {
1311        .owner                  = THIS_MODULE,
1312        .mdev_attr_groups       = mdev_dev_groups,
1313        .supported_type_groups  = mdev_type_groups,
1314        .create                 = mbochs_create,
1315        .remove                 = mbochs_remove,
1316        .open                   = mbochs_open,
1317        .release                = mbochs_close,
1318        .read                   = mbochs_read,
1319        .write                  = mbochs_write,
1320        .ioctl                  = mbochs_ioctl,
1321        .mmap                   = mbochs_mmap,
1322};
1323
1324static const struct file_operations vd_fops = {
1325        .owner          = THIS_MODULE,
1326};
1327
1328static void mbochs_device_release(struct device *dev)
1329{
1330        /* nothing */
1331}
1332
1333static int __init mbochs_dev_init(void)
1334{
1335        int ret = 0;
1336
1337        ret = alloc_chrdev_region(&mbochs_devt, 0, MINORMASK, MBOCHS_NAME);
1338        if (ret < 0) {
1339                pr_err("Error: failed to register mbochs_dev, err: %d\n", ret);
1340                return ret;
1341        }
1342        cdev_init(&mbochs_cdev, &vd_fops);
1343        cdev_add(&mbochs_cdev, mbochs_devt, MINORMASK);
1344        pr_info("%s: major %d\n", __func__, MAJOR(mbochs_devt));
1345
1346        mbochs_class = class_create(THIS_MODULE, MBOCHS_CLASS_NAME);
1347        if (IS_ERR(mbochs_class)) {
1348                pr_err("Error: failed to register mbochs_dev class\n");
1349                ret = PTR_ERR(mbochs_class);
1350                goto failed1;
1351        }
1352        mbochs_dev.class = mbochs_class;
1353        mbochs_dev.release = mbochs_device_release;
1354        dev_set_name(&mbochs_dev, "%s", MBOCHS_NAME);
1355
1356        ret = device_register(&mbochs_dev);
1357        if (ret)
1358                goto failed2;
1359
1360        ret = mdev_register_device(&mbochs_dev, &mdev_fops);
1361        if (ret)
1362                goto failed3;
1363
1364        return 0;
1365
1366failed3:
1367        device_unregister(&mbochs_dev);
1368failed2:
1369        class_destroy(mbochs_class);
1370failed1:
1371        cdev_del(&mbochs_cdev);
1372        unregister_chrdev_region(mbochs_devt, MINORMASK);
1373        return ret;
1374}
1375
1376static void __exit mbochs_dev_exit(void)
1377{
1378        mbochs_dev.bus = NULL;
1379        mdev_unregister_device(&mbochs_dev);
1380
1381        device_unregister(&mbochs_dev);
1382        cdev_del(&mbochs_cdev);
1383        unregister_chrdev_region(mbochs_devt, MINORMASK);
1384        class_destroy(mbochs_class);
1385        mbochs_class = NULL;
1386}
1387
1388module_init(mbochs_dev_init)
1389module_exit(mbochs_dev_exit)
1390