Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2013 Red Hat Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors: Dave Airlie
0023  *          Alon Levy
0024  */
0025 
0026 #include <linux/delay.h>
0027 
0028 #include <drm/drm.h>
0029 #include <drm/drm_file.h>
0030 #include <drm/drm_debugfs.h>
0031 #include <drm/qxl_drm.h>
0032 #include <drm/ttm/ttm_bo_api.h>
0033 #include <drm/ttm/ttm_bo_driver.h>
0034 #include <drm/ttm/ttm_placement.h>
0035 #include <drm/ttm/ttm_range_manager.h>
0036 
0037 #include "qxl_drv.h"
0038 #include "qxl_object.h"
0039 
0040 static struct qxl_device *qxl_get_qdev(struct ttm_device *bdev)
0041 {
0042     struct qxl_mman *mman;
0043     struct qxl_device *qdev;
0044 
0045     mman = container_of(bdev, struct qxl_mman, bdev);
0046     qdev = container_of(mman, struct qxl_device, mman);
0047     return qdev;
0048 }
0049 
0050 static void qxl_evict_flags(struct ttm_buffer_object *bo,
0051                 struct ttm_placement *placement)
0052 {
0053     struct qxl_bo *qbo;
0054     static const struct ttm_place placements = {
0055         .fpfn = 0,
0056         .lpfn = 0,
0057         .mem_type = TTM_PL_SYSTEM,
0058         .flags = 0
0059     };
0060 
0061     if (!qxl_ttm_bo_is_qxl_bo(bo)) {
0062         placement->placement = &placements;
0063         placement->busy_placement = &placements;
0064         placement->num_placement = 1;
0065         placement->num_busy_placement = 1;
0066         return;
0067     }
0068     qbo = to_qxl_bo(bo);
0069     qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU);
0070     *placement = qbo->placement;
0071 }
0072 
0073 int qxl_ttm_io_mem_reserve(struct ttm_device *bdev,
0074                struct ttm_resource *mem)
0075 {
0076     struct qxl_device *qdev = qxl_get_qdev(bdev);
0077 
0078     switch (mem->mem_type) {
0079     case TTM_PL_SYSTEM:
0080         /* system memory */
0081         return 0;
0082     case TTM_PL_VRAM:
0083         mem->bus.is_iomem = true;
0084         mem->bus.offset = (mem->start << PAGE_SHIFT) + qdev->vram_base;
0085         mem->bus.caching = ttm_write_combined;
0086         break;
0087     case TTM_PL_PRIV:
0088         mem->bus.is_iomem = true;
0089         mem->bus.offset = (mem->start << PAGE_SHIFT) +
0090             qdev->surfaceram_base;
0091         mem->bus.caching = ttm_write_combined;
0092         break;
0093     default:
0094         return -EINVAL;
0095     }
0096     return 0;
0097 }
0098 
0099 /*
0100  * TTM backend functions.
0101  */
0102 static void qxl_ttm_backend_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
0103 {
0104     ttm_tt_fini(ttm);
0105     kfree(ttm);
0106 }
0107 
0108 static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
0109                     uint32_t page_flags)
0110 {
0111     struct ttm_tt *ttm;
0112 
0113     ttm = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
0114     if (ttm == NULL)
0115         return NULL;
0116     if (ttm_tt_init(ttm, bo, page_flags, ttm_cached, 0)) {
0117         kfree(ttm);
0118         return NULL;
0119     }
0120     return ttm;
0121 }
0122 
0123 static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
0124                    struct ttm_resource *new_mem)
0125 {
0126     struct qxl_bo *qbo;
0127     struct qxl_device *qdev;
0128 
0129     if (!qxl_ttm_bo_is_qxl_bo(bo) || !bo->resource)
0130         return;
0131     qbo = to_qxl_bo(bo);
0132     qdev = to_qxl(qbo->tbo.base.dev);
0133 
0134     if (bo->resource->mem_type == TTM_PL_PRIV && qbo->surface_id)
0135         qxl_surface_evict(qdev, qbo, new_mem ? true : false);
0136 }
0137 
0138 static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
0139                struct ttm_operation_ctx *ctx,
0140                struct ttm_resource *new_mem,
0141                struct ttm_place *hop)
0142 {
0143     struct ttm_resource *old_mem = bo->resource;
0144     int ret;
0145 
0146     qxl_bo_move_notify(bo, new_mem);
0147 
0148     ret = ttm_bo_wait_ctx(bo, ctx);
0149     if (ret)
0150         return ret;
0151 
0152     if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
0153         ttm_bo_move_null(bo, new_mem);
0154         return 0;
0155     }
0156     return ttm_bo_move_memcpy(bo, ctx, new_mem);
0157 }
0158 
0159 static void qxl_bo_delete_mem_notify(struct ttm_buffer_object *bo)
0160 {
0161     qxl_bo_move_notify(bo, NULL);
0162 }
0163 
0164 static struct ttm_device_funcs qxl_bo_driver = {
0165     .ttm_tt_create = &qxl_ttm_tt_create,
0166     .ttm_tt_destroy = &qxl_ttm_backend_destroy,
0167     .eviction_valuable = ttm_bo_eviction_valuable,
0168     .evict_flags = &qxl_evict_flags,
0169     .move = &qxl_bo_move,
0170     .io_mem_reserve = &qxl_ttm_io_mem_reserve,
0171     .delete_mem_notify = &qxl_bo_delete_mem_notify,
0172 };
0173 
0174 static int qxl_ttm_init_mem_type(struct qxl_device *qdev,
0175                  unsigned int type,
0176                  uint64_t size)
0177 {
0178     return ttm_range_man_init(&qdev->mman.bdev, type, false, size);
0179 }
0180 
0181 int qxl_ttm_init(struct qxl_device *qdev)
0182 {
0183     int r;
0184     int num_io_pages; /* != rom->num_io_pages, we include surface0 */
0185 
0186     /* No others user of address space so set it to 0 */
0187     r = ttm_device_init(&qdev->mman.bdev, &qxl_bo_driver, NULL,
0188                 qdev->ddev.anon_inode->i_mapping,
0189                 qdev->ddev.vma_offset_manager,
0190                 false, false);
0191     if (r) {
0192         DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
0193         return r;
0194     }
0195     /* NOTE: this includes the framebuffer (aka surface 0) */
0196     num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
0197     r = qxl_ttm_init_mem_type(qdev, TTM_PL_VRAM, num_io_pages);
0198     if (r) {
0199         DRM_ERROR("Failed initializing VRAM heap.\n");
0200         return r;
0201     }
0202     r = qxl_ttm_init_mem_type(qdev, TTM_PL_PRIV,
0203                   qdev->surfaceram_size / PAGE_SIZE);
0204     if (r) {
0205         DRM_ERROR("Failed initializing Surfaces heap.\n");
0206         return r;
0207     }
0208     DRM_INFO("qxl: %uM of VRAM memory size\n",
0209          (unsigned int)qdev->vram_size / (1024 * 1024));
0210     DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
0211          ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
0212     DRM_INFO("qxl: %uM of Surface memory size\n",
0213          (unsigned int)qdev->surfaceram_size / (1024 * 1024));
0214     return 0;
0215 }
0216 
0217 void qxl_ttm_fini(struct qxl_device *qdev)
0218 {
0219     ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_VRAM);
0220     ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_PRIV);
0221     ttm_device_fini(&qdev->mman.bdev);
0222     DRM_INFO("qxl: ttm finalized\n");
0223 }
0224 
0225 void qxl_ttm_debugfs_init(struct qxl_device *qdev)
0226 {
0227 #if defined(CONFIG_DEBUG_FS)
0228     ttm_resource_manager_create_debugfs(ttm_manager_type(&qdev->mman.bdev,
0229                                  TTM_PL_VRAM),
0230                         qdev->ddev.primary->debugfs_root, "qxl_mem_mm");
0231     ttm_resource_manager_create_debugfs(ttm_manager_type(&qdev->mman.bdev,
0232                                  TTM_PL_PRIV),
0233                         qdev->ddev.primary->debugfs_root, "qxl_surf_mm");
0234 #endif
0235 }