linux/drivers/gpu/drm/bochs/bochs_mm.c
<<
>>
Prefs
   1/*
   2 * This program is free software; you can redistribute it and/or modify
   3 * it under the terms of the GNU General Public License as published by
   4 * the Free Software Foundation; either version 2 of the License, or
   5 * (at your option) any later version.
   6 */
   7
   8#include "bochs.h"
   9
  10static void bochs_ttm_placement(struct bochs_bo *bo, int domain);
  11
  12/* ---------------------------------------------------------------------- */
  13
  14static inline struct bochs_device *bochs_bdev(struct ttm_bo_device *bd)
  15{
  16        return container_of(bd, struct bochs_device, ttm.bdev);
  17}
  18
  19static int bochs_ttm_mem_global_init(struct drm_global_reference *ref)
  20{
  21        return ttm_mem_global_init(ref->object);
  22}
  23
  24static void bochs_ttm_mem_global_release(struct drm_global_reference *ref)
  25{
  26        ttm_mem_global_release(ref->object);
  27}
  28
  29static int bochs_ttm_global_init(struct bochs_device *bochs)
  30{
  31        struct drm_global_reference *global_ref;
  32        int r;
  33
  34        global_ref = &bochs->ttm.mem_global_ref;
  35        global_ref->global_type = DRM_GLOBAL_TTM_MEM;
  36        global_ref->size = sizeof(struct ttm_mem_global);
  37        global_ref->init = &bochs_ttm_mem_global_init;
  38        global_ref->release = &bochs_ttm_mem_global_release;
  39        r = drm_global_item_ref(global_ref);
  40        if (r != 0) {
  41                DRM_ERROR("Failed setting up TTM memory accounting "
  42                          "subsystem.\n");
  43                return r;
  44        }
  45
  46        bochs->ttm.bo_global_ref.mem_glob =
  47                bochs->ttm.mem_global_ref.object;
  48        global_ref = &bochs->ttm.bo_global_ref.ref;
  49        global_ref->global_type = DRM_GLOBAL_TTM_BO;
  50        global_ref->size = sizeof(struct ttm_bo_global);
  51        global_ref->init = &ttm_bo_global_init;
  52        global_ref->release = &ttm_bo_global_release;
  53        r = drm_global_item_ref(global_ref);
  54        if (r != 0) {
  55                DRM_ERROR("Failed setting up TTM BO subsystem.\n");
  56                drm_global_item_unref(&bochs->ttm.mem_global_ref);
  57                return r;
  58        }
  59
  60        return 0;
  61}
  62
  63static void bochs_ttm_global_release(struct bochs_device *bochs)
  64{
  65        if (bochs->ttm.mem_global_ref.release == NULL)
  66                return;
  67
  68        drm_global_item_unref(&bochs->ttm.bo_global_ref.ref);
  69        drm_global_item_unref(&bochs->ttm.mem_global_ref);
  70        bochs->ttm.mem_global_ref.release = NULL;
  71}
  72
  73
  74static void bochs_bo_ttm_destroy(struct ttm_buffer_object *tbo)
  75{
  76        struct bochs_bo *bo;
  77
  78        bo = container_of(tbo, struct bochs_bo, bo);
  79        drm_gem_object_release(&bo->gem);
  80        kfree(bo);
  81}
  82
  83static bool bochs_ttm_bo_is_bochs_bo(struct ttm_buffer_object *bo)
  84{
  85        if (bo->destroy == &bochs_bo_ttm_destroy)
  86                return true;
  87        return false;
  88}
  89
  90static int bochs_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
  91                                  struct ttm_mem_type_manager *man)
  92{
  93        switch (type) {
  94        case TTM_PL_SYSTEM:
  95                man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
  96                man->available_caching = TTM_PL_MASK_CACHING;
  97                man->default_caching = TTM_PL_FLAG_CACHED;
  98                break;
  99        case TTM_PL_VRAM:
 100                man->func = &ttm_bo_manager_func;
 101                man->flags = TTM_MEMTYPE_FLAG_FIXED |
 102                        TTM_MEMTYPE_FLAG_MAPPABLE;
 103                man->available_caching = TTM_PL_FLAG_UNCACHED |
 104                        TTM_PL_FLAG_WC;
 105                man->default_caching = TTM_PL_FLAG_WC;
 106                break;
 107        default:
 108                DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
 109                return -EINVAL;
 110        }
 111        return 0;
 112}
 113
 114static void
 115bochs_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
 116{
 117        struct bochs_bo *bochsbo = bochs_bo(bo);
 118
 119        if (!bochs_ttm_bo_is_bochs_bo(bo))
 120                return;
 121
 122        bochs_ttm_placement(bochsbo, TTM_PL_FLAG_SYSTEM);
 123        *pl = bochsbo->placement;
 124}
 125
 126static int bochs_bo_verify_access(struct ttm_buffer_object *bo,
 127                                  struct file *filp)
 128{
 129        struct bochs_bo *bochsbo = bochs_bo(bo);
 130
 131        return drm_vma_node_verify_access(&bochsbo->gem.vma_node,
 132                                          filp->private_data);
 133}
 134
 135static int bochs_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
 136                                    struct ttm_mem_reg *mem)
 137{
 138        struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
 139        struct bochs_device *bochs = bochs_bdev(bdev);
 140
 141        mem->bus.addr = NULL;
 142        mem->bus.offset = 0;
 143        mem->bus.size = mem->num_pages << PAGE_SHIFT;
 144        mem->bus.base = 0;
 145        mem->bus.is_iomem = false;
 146        if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
 147                return -EINVAL;
 148        switch (mem->mem_type) {
 149        case TTM_PL_SYSTEM:
 150                /* system memory */
 151                return 0;
 152        case TTM_PL_VRAM:
 153                mem->bus.offset = mem->start << PAGE_SHIFT;
 154                mem->bus.base = bochs->fb_base;
 155                mem->bus.is_iomem = true;
 156                break;
 157        default:
 158                return -EINVAL;
 159                break;
 160        }
 161        return 0;
 162}
 163
 164static void bochs_ttm_io_mem_free(struct ttm_bo_device *bdev,
 165                                  struct ttm_mem_reg *mem)
 166{
 167}
 168
 169static void bochs_ttm_backend_destroy(struct ttm_tt *tt)
 170{
 171        ttm_tt_fini(tt);
 172        kfree(tt);
 173}
 174
 175static struct ttm_backend_func bochs_tt_backend_func = {
 176        .destroy = &bochs_ttm_backend_destroy,
 177};
 178
 179static struct ttm_tt *bochs_ttm_tt_create(struct ttm_bo_device *bdev,
 180                                          unsigned long size,
 181                                          uint32_t page_flags,
 182                                          struct page *dummy_read_page)
 183{
 184        struct ttm_tt *tt;
 185
 186        tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
 187        if (tt == NULL)
 188                return NULL;
 189        tt->func = &bochs_tt_backend_func;
 190        if (ttm_tt_init(tt, bdev, size, page_flags, dummy_read_page)) {
 191                kfree(tt);
 192                return NULL;
 193        }
 194        return tt;
 195}
 196
 197struct ttm_bo_driver bochs_bo_driver = {
 198        .ttm_tt_create = bochs_ttm_tt_create,
 199        .ttm_tt_populate = ttm_pool_populate,
 200        .ttm_tt_unpopulate = ttm_pool_unpopulate,
 201        .init_mem_type = bochs_bo_init_mem_type,
 202        .eviction_valuable = ttm_bo_eviction_valuable,
 203        .evict_flags = bochs_bo_evict_flags,
 204        .move = NULL,
 205        .verify_access = bochs_bo_verify_access,
 206        .io_mem_reserve = &bochs_ttm_io_mem_reserve,
 207        .io_mem_free = &bochs_ttm_io_mem_free,
 208        .lru_tail = &ttm_bo_default_lru_tail,
 209        .swap_lru_tail = &ttm_bo_default_swap_lru_tail,
 210};
 211
 212int bochs_mm_init(struct bochs_device *bochs)
 213{
 214        struct ttm_bo_device *bdev = &bochs->ttm.bdev;
 215        int ret;
 216
 217        ret = bochs_ttm_global_init(bochs);
 218        if (ret)
 219                return ret;
 220
 221        ret = ttm_bo_device_init(&bochs->ttm.bdev,
 222                                 bochs->ttm.bo_global_ref.ref.object,
 223                                 &bochs_bo_driver,
 224                                 bochs->dev->anon_inode->i_mapping,
 225                                 DRM_FILE_PAGE_OFFSET,
 226                                 true);
 227        if (ret) {
 228                DRM_ERROR("Error initialising bo driver; %d\n", ret);
 229                return ret;
 230        }
 231
 232        ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM,
 233                             bochs->fb_size >> PAGE_SHIFT);
 234        if (ret) {
 235                DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
 236                return ret;
 237        }
 238
 239        bochs->ttm.initialized = true;
 240        return 0;
 241}
 242
 243void bochs_mm_fini(struct bochs_device *bochs)
 244{
 245        if (!bochs->ttm.initialized)
 246                return;
 247
 248        ttm_bo_device_release(&bochs->ttm.bdev);
 249        bochs_ttm_global_release(bochs);
 250        bochs->ttm.initialized = false;
 251}
 252
 253static void bochs_ttm_placement(struct bochs_bo *bo, int domain)
 254{
 255        unsigned i;
 256        u32 c = 0;
 257        bo->placement.placement = bo->placements;
 258        bo->placement.busy_placement = bo->placements;
 259        if (domain & TTM_PL_FLAG_VRAM) {
 260                bo->placements[c++].flags = TTM_PL_FLAG_WC
 261                        | TTM_PL_FLAG_UNCACHED
 262                        | TTM_PL_FLAG_VRAM;
 263        }
 264        if (domain & TTM_PL_FLAG_SYSTEM) {
 265                bo->placements[c++].flags = TTM_PL_MASK_CACHING
 266                        | TTM_PL_FLAG_SYSTEM;
 267        }
 268        if (!c) {
 269                bo->placements[c++].flags = TTM_PL_MASK_CACHING
 270                        | TTM_PL_FLAG_SYSTEM;
 271        }
 272        for (i = 0; i < c; ++i) {
 273                bo->placements[i].fpfn = 0;
 274                bo->placements[i].lpfn = 0;
 275        }
 276        bo->placement.num_placement = c;
 277        bo->placement.num_busy_placement = c;
 278}
 279
 280static inline u64 bochs_bo_gpu_offset(struct bochs_bo *bo)
 281{
 282        return bo->bo.offset;
 283}
 284
 285int bochs_bo_pin(struct bochs_bo *bo, u32 pl_flag, u64 *gpu_addr)
 286{
 287        int i, ret;
 288
 289        if (bo->pin_count) {
 290                bo->pin_count++;
 291                if (gpu_addr)
 292                        *gpu_addr = bochs_bo_gpu_offset(bo);
 293                return 0;
 294        }
 295
 296        bochs_ttm_placement(bo, pl_flag);
 297        for (i = 0; i < bo->placement.num_placement; i++)
 298                bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
 299        ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
 300        if (ret)
 301                return ret;
 302
 303        bo->pin_count = 1;
 304        if (gpu_addr)
 305                *gpu_addr = bochs_bo_gpu_offset(bo);
 306        return 0;
 307}
 308
 309int bochs_bo_unpin(struct bochs_bo *bo)
 310{
 311        int i, ret;
 312
 313        if (!bo->pin_count) {
 314                DRM_ERROR("unpin bad %p\n", bo);
 315                return 0;
 316        }
 317        bo->pin_count--;
 318
 319        if (bo->pin_count)
 320                return 0;
 321
 322        for (i = 0; i < bo->placement.num_placement; i++)
 323                bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
 324        ret = ttm_bo_validate(&bo->bo, &bo->placement, false, false);
 325        if (ret)
 326                return ret;
 327
 328        return 0;
 329}
 330
 331int bochs_mmap(struct file *filp, struct vm_area_struct *vma)
 332{
 333        struct drm_file *file_priv;
 334        struct bochs_device *bochs;
 335
 336        if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
 337                return -EINVAL;
 338
 339        file_priv = filp->private_data;
 340        bochs = file_priv->minor->dev->dev_private;
 341        return ttm_bo_mmap(filp, vma, &bochs->ttm.bdev);
 342}
 343
 344/* ---------------------------------------------------------------------- */
 345
 346static int bochs_bo_create(struct drm_device *dev, int size, int align,
 347                           uint32_t flags, struct bochs_bo **pbochsbo)
 348{
 349        struct bochs_device *bochs = dev->dev_private;
 350        struct bochs_bo *bochsbo;
 351        size_t acc_size;
 352        int ret;
 353
 354        bochsbo = kzalloc(sizeof(struct bochs_bo), GFP_KERNEL);
 355        if (!bochsbo)
 356                return -ENOMEM;
 357
 358        ret = drm_gem_object_init(dev, &bochsbo->gem, size);
 359        if (ret) {
 360                kfree(bochsbo);
 361                return ret;
 362        }
 363
 364        bochsbo->bo.bdev = &bochs->ttm.bdev;
 365        bochsbo->bo.bdev->dev_mapping = dev->anon_inode->i_mapping;
 366
 367        bochs_ttm_placement(bochsbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
 368
 369        acc_size = ttm_bo_dma_acc_size(&bochs->ttm.bdev, size,
 370                                       sizeof(struct bochs_bo));
 371
 372        ret = ttm_bo_init(&bochs->ttm.bdev, &bochsbo->bo, size,
 373                          ttm_bo_type_device, &bochsbo->placement,
 374                          align >> PAGE_SHIFT, false, NULL, acc_size,
 375                          NULL, NULL, bochs_bo_ttm_destroy);
 376        if (ret)
 377                return ret;
 378
 379        *pbochsbo = bochsbo;
 380        return 0;
 381}
 382
 383int bochs_gem_create(struct drm_device *dev, u32 size, bool iskernel,
 384                     struct drm_gem_object **obj)
 385{
 386        struct bochs_bo *bochsbo;
 387        int ret;
 388
 389        *obj = NULL;
 390
 391        size = PAGE_ALIGN(size);
 392        if (size == 0)
 393                return -EINVAL;
 394
 395        ret = bochs_bo_create(dev, size, 0, 0, &bochsbo);
 396        if (ret) {
 397                if (ret != -ERESTARTSYS)
 398                        DRM_ERROR("failed to allocate GEM object\n");
 399                return ret;
 400        }
 401        *obj = &bochsbo->gem;
 402        return 0;
 403}
 404
 405int bochs_dumb_create(struct drm_file *file, struct drm_device *dev,
 406                      struct drm_mode_create_dumb *args)
 407{
 408        struct drm_gem_object *gobj;
 409        u32 handle;
 410        int ret;
 411
 412        args->pitch = args->width * ((args->bpp + 7) / 8);
 413        args->size = args->pitch * args->height;
 414
 415        ret = bochs_gem_create(dev, args->size, false,
 416                               &gobj);
 417        if (ret)
 418                return ret;
 419
 420        ret = drm_gem_handle_create(file, gobj, &handle);
 421        drm_gem_object_unreference_unlocked(gobj);
 422        if (ret)
 423                return ret;
 424
 425        args->handle = handle;
 426        return 0;
 427}
 428
 429static void bochs_bo_unref(struct bochs_bo **bo)
 430{
 431        struct ttm_buffer_object *tbo;
 432
 433        if ((*bo) == NULL)
 434                return;
 435
 436        tbo = &((*bo)->bo);
 437        ttm_bo_unref(&tbo);
 438        *bo = NULL;
 439}
 440
 441void bochs_gem_free_object(struct drm_gem_object *obj)
 442{
 443        struct bochs_bo *bochs_bo = gem_to_bochs_bo(obj);
 444
 445        bochs_bo_unref(&bochs_bo);
 446}
 447
 448int bochs_dumb_mmap_offset(struct drm_file *file, struct drm_device *dev,
 449                           uint32_t handle, uint64_t *offset)
 450{
 451        struct drm_gem_object *obj;
 452        struct bochs_bo *bo;
 453
 454        obj = drm_gem_object_lookup(file, handle);
 455        if (obj == NULL)
 456                return -ENOENT;
 457
 458        bo = gem_to_bochs_bo(obj);
 459        *offset = bochs_bo_mmap_offset(bo);
 460
 461        drm_gem_object_unreference_unlocked(obj);
 462        return 0;
 463}
 464
 465/* ---------------------------------------------------------------------- */
 466
 467static void bochs_user_framebuffer_destroy(struct drm_framebuffer *fb)
 468{
 469        struct bochs_framebuffer *bochs_fb = to_bochs_framebuffer(fb);
 470
 471        drm_gem_object_unreference_unlocked(bochs_fb->obj);
 472        drm_framebuffer_cleanup(fb);
 473        kfree(fb);
 474}
 475
 476static const struct drm_framebuffer_funcs bochs_fb_funcs = {
 477        .destroy = bochs_user_framebuffer_destroy,
 478};
 479
 480int bochs_framebuffer_init(struct drm_device *dev,
 481                           struct bochs_framebuffer *gfb,
 482                           const struct drm_mode_fb_cmd2 *mode_cmd,
 483                           struct drm_gem_object *obj)
 484{
 485        int ret;
 486
 487        drm_helper_mode_fill_fb_struct(&gfb->base, mode_cmd);
 488        gfb->obj = obj;
 489        ret = drm_framebuffer_init(dev, &gfb->base, &bochs_fb_funcs);
 490        if (ret) {
 491                DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
 492                return ret;
 493        }
 494        return 0;
 495}
 496
 497static struct drm_framebuffer *
 498bochs_user_framebuffer_create(struct drm_device *dev,
 499                              struct drm_file *filp,
 500                              const struct drm_mode_fb_cmd2 *mode_cmd)
 501{
 502        struct drm_gem_object *obj;
 503        struct bochs_framebuffer *bochs_fb;
 504        int ret;
 505
 506        DRM_DEBUG_DRIVER("%dx%d, format %c%c%c%c\n",
 507               mode_cmd->width, mode_cmd->height,
 508               (mode_cmd->pixel_format)       & 0xff,
 509               (mode_cmd->pixel_format >> 8)  & 0xff,
 510               (mode_cmd->pixel_format >> 16) & 0xff,
 511               (mode_cmd->pixel_format >> 24) & 0xff);
 512
 513        if (mode_cmd->pixel_format != DRM_FORMAT_XRGB8888)
 514                return ERR_PTR(-ENOENT);
 515
 516        obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
 517        if (obj == NULL)
 518                return ERR_PTR(-ENOENT);
 519
 520        bochs_fb = kzalloc(sizeof(*bochs_fb), GFP_KERNEL);
 521        if (!bochs_fb) {
 522                drm_gem_object_unreference_unlocked(obj);
 523                return ERR_PTR(-ENOMEM);
 524        }
 525
 526        ret = bochs_framebuffer_init(dev, bochs_fb, mode_cmd, obj);
 527        if (ret) {
 528                drm_gem_object_unreference_unlocked(obj);
 529                kfree(bochs_fb);
 530                return ERR_PTR(ret);
 531        }
 532        return &bochs_fb->base;
 533}
 534
 535const struct drm_mode_config_funcs bochs_mode_funcs = {
 536        .fb_create = bochs_user_framebuffer_create,
 537};
 538