2 * Copyright 2013 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Authors: Dave Airlie
26 #include <linux/delay.h>
29 #include <drm/drm_file.h>
30 #include <drm/drm_debugfs.h>
31 #include <drm/qxl_drm.h>
32 #include <drm/ttm/ttm_bo_api.h>
33 #include <drm/ttm/ttm_bo_driver.h>
34 #include <drm/ttm/ttm_module.h>
35 #include <drm/ttm/ttm_page_alloc.h>
36 #include <drm/ttm/ttm_placement.h>
39 #include "qxl_object.h"
41 static struct qxl_device *qxl_get_qdev(struct ttm_bo_device *bdev)
43 struct qxl_mman *mman;
44 struct qxl_device *qdev;
46 mman = container_of(bdev, struct qxl_mman, bdev);
47 qdev = container_of(mman, struct qxl_device, mman);
51 static void qxl_evict_flags(struct ttm_buffer_object *bo,
52 struct ttm_placement *placement)
55 static const struct ttm_place placements = {
58 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
61 if (!qxl_ttm_bo_is_qxl_bo(bo)) {
62 placement->placement = &placements;
63 placement->busy_placement = &placements;
64 placement->num_placement = 1;
65 placement->num_busy_placement = 1;
69 qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
70 *placement = qbo->placement;
73 int qxl_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
74 struct ttm_resource *mem)
76 struct qxl_device *qdev = qxl_get_qdev(bdev);
78 switch (mem->mem_type) {
83 mem->bus.is_iomem = true;
84 mem->bus.offset = (mem->start << PAGE_SHIFT) + qdev->vram_base;
87 mem->bus.is_iomem = true;
88 mem->bus.offset = (mem->start << PAGE_SHIFT) +
89 qdev->surfaceram_base;
98 * TTM backend functions.
102 struct qxl_device *qdev;
106 static int qxl_ttm_backend_bind(struct ttm_bo_device *bdev,
108 struct ttm_resource *bo_mem)
110 struct qxl_ttm_tt *gtt = (void *)ttm;
112 gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
113 if (!ttm->num_pages) {
114 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
115 ttm->num_pages, bo_mem, ttm);
117 /* Not implemented */
121 static void qxl_ttm_backend_unbind(struct ttm_bo_device *bdev,
124 /* Not implemented */
127 static void qxl_ttm_backend_destroy(struct ttm_bo_device *bdev,
130 struct qxl_ttm_tt *gtt = (void *)ttm;
132 ttm_tt_fini(>t->ttm);
136 static struct ttm_backend_func qxl_backend_func = {
137 .bind = &qxl_ttm_backend_bind,
138 .unbind = &qxl_ttm_backend_unbind,
139 .destroy = &qxl_ttm_backend_destroy,
142 static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
145 struct qxl_device *qdev;
146 struct qxl_ttm_tt *gtt;
148 qdev = qxl_get_qdev(bo->bdev);
149 gtt = kzalloc(sizeof(struct qxl_ttm_tt), GFP_KERNEL);
152 gtt->ttm.func = &qxl_backend_func;
154 if (ttm_tt_init(>t->ttm, bo, page_flags)) {
161 static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
162 struct ttm_operation_ctx *ctx,
163 struct ttm_resource *new_mem)
165 struct ttm_resource *old_mem = &bo->mem;
168 ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
172 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
173 ttm_bo_move_null(bo, new_mem);
176 return ttm_bo_move_memcpy(bo, ctx, new_mem);
179 static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
181 struct ttm_resource *new_mem)
184 struct qxl_device *qdev;
186 if (!qxl_ttm_bo_is_qxl_bo(bo))
189 qdev = to_qxl(qbo->tbo.base.dev);
191 if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
192 qxl_surface_evict(qdev, qbo, new_mem ? true : false);
195 static struct ttm_bo_driver qxl_bo_driver = {
196 .ttm_tt_create = &qxl_ttm_tt_create,
197 .eviction_valuable = ttm_bo_eviction_valuable,
198 .evict_flags = &qxl_evict_flags,
199 .move = &qxl_bo_move,
200 .io_mem_reserve = &qxl_ttm_io_mem_reserve,
201 .move_notify = &qxl_bo_move_notify,
204 static int qxl_ttm_init_mem_type(struct qxl_device *qdev,
208 return ttm_range_man_init(&qdev->mman.bdev, type, TTM_PL_MASK_CACHING,
209 TTM_PL_FLAG_CACHED, false, size);
212 int qxl_ttm_init(struct qxl_device *qdev)
215 int num_io_pages; /* != rom->num_io_pages, we include surface0 */
217 /* No others user of address space so set it to 0 */
218 r = ttm_bo_device_init(&qdev->mman.bdev,
220 qdev->ddev.anon_inode->i_mapping,
221 qdev->ddev.vma_offset_manager,
224 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
227 /* NOTE: this includes the framebuffer (aka surface 0) */
228 num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
229 r = qxl_ttm_init_mem_type(qdev, TTM_PL_VRAM, num_io_pages);
231 DRM_ERROR("Failed initializing VRAM heap.\n");
234 r = qxl_ttm_init_mem_type(qdev, TTM_PL_PRIV,
235 qdev->surfaceram_size / PAGE_SIZE);
237 DRM_ERROR("Failed initializing Surfaces heap.\n");
240 DRM_INFO("qxl: %uM of VRAM memory size\n",
241 (unsigned int)qdev->vram_size / (1024 * 1024));
242 DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
243 ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
244 DRM_INFO("qxl: %uM of Surface memory size\n",
245 (unsigned int)qdev->surfaceram_size / (1024 * 1024));
249 void qxl_ttm_fini(struct qxl_device *qdev)
251 ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_VRAM);
252 ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_PRIV);
253 ttm_bo_device_release(&qdev->mman.bdev);
254 DRM_INFO("qxl: ttm finalized\n");
257 #define QXL_DEBUGFS_MEM_TYPES 2
259 #if defined(CONFIG_DEBUG_FS)
260 static int qxl_mm_dump_table(struct seq_file *m, void *data)
262 struct drm_info_node *node = (struct drm_info_node *)m->private;
263 struct ttm_resource_manager *man = (struct ttm_resource_manager *)node->info_ent->data;
264 struct drm_printer p = drm_seq_file_printer(m);
266 ttm_resource_manager_debug(man, &p);
271 void qxl_ttm_debugfs_init(struct qxl_device *qdev)
273 #if defined(CONFIG_DEBUG_FS)
274 static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
275 static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
278 for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
280 sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
282 sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
283 qxl_mem_types_list[i].name = qxl_mem_types_names[i];
284 qxl_mem_types_list[i].show = &qxl_mm_dump_table;
285 qxl_mem_types_list[i].driver_features = 0;
287 qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_VRAM);
289 qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_PRIV);
292 qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);