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 int qxl_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
52 struct ttm_mem_type_manager *man)
54 struct qxl_device *qdev = qxl_get_qdev(bdev);
55 unsigned int gpu_offset_shift =
56 64 - (qdev->rom->slot_gen_bits + qdev->rom->slot_id_bits + 8);
57 struct qxl_memslot *slot;
62 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
63 man->available_caching = TTM_PL_MASK_CACHING;
64 man->default_caching = TTM_PL_FLAG_CACHED;
68 /* "On-card" video ram */
69 slot = (type == TTM_PL_VRAM) ?
70 &qdev->main_slot : &qdev->surfaces_slot;
71 slot->gpu_offset = (uint64_t)type << gpu_offset_shift;
72 man->func = &ttm_bo_manager_func;
73 man->gpu_offset = slot->gpu_offset;
74 man->flags = TTM_MEMTYPE_FLAG_FIXED |
75 TTM_MEMTYPE_FLAG_MAPPABLE;
76 man->available_caching = TTM_PL_MASK_CACHING;
77 man->default_caching = TTM_PL_FLAG_CACHED;
80 DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
86 static void qxl_evict_flags(struct ttm_buffer_object *bo,
87 struct ttm_placement *placement)
90 static const struct ttm_place placements = {
93 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
96 if (!qxl_ttm_bo_is_qxl_bo(bo)) {
97 placement->placement = &placements;
98 placement->busy_placement = &placements;
99 placement->num_placement = 1;
100 placement->num_busy_placement = 1;
104 qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
105 *placement = qbo->placement;
108 int qxl_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
109 struct ttm_mem_reg *mem)
111 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
112 struct qxl_device *qdev = qxl_get_qdev(bdev);
114 mem->bus.addr = NULL;
116 mem->bus.size = mem->num_pages << PAGE_SHIFT;
118 mem->bus.is_iomem = false;
119 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
121 switch (mem->mem_type) {
126 mem->bus.is_iomem = true;
127 mem->bus.base = qdev->vram_base;
128 mem->bus.offset = mem->start << PAGE_SHIFT;
131 mem->bus.is_iomem = true;
132 mem->bus.base = qdev->surfaceram_base;
133 mem->bus.offset = mem->start << PAGE_SHIFT;
141 static void qxl_ttm_io_mem_free(struct ttm_bo_device *bdev,
142 struct ttm_mem_reg *mem)
147 * TTM backend functions.
151 struct qxl_device *qdev;
155 static int qxl_ttm_backend_bind(struct ttm_tt *ttm,
156 struct ttm_mem_reg *bo_mem)
158 struct qxl_ttm_tt *gtt = (void *)ttm;
160 gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
161 if (!ttm->num_pages) {
162 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
163 ttm->num_pages, bo_mem, ttm);
165 /* Not implemented */
169 static int qxl_ttm_backend_unbind(struct ttm_tt *ttm)
171 /* Not implemented */
175 static void qxl_ttm_backend_destroy(struct ttm_tt *ttm)
177 struct qxl_ttm_tt *gtt = (void *)ttm;
179 ttm_tt_fini(>t->ttm);
183 static struct ttm_backend_func qxl_backend_func = {
184 .bind = &qxl_ttm_backend_bind,
185 .unbind = &qxl_ttm_backend_unbind,
186 .destroy = &qxl_ttm_backend_destroy,
189 static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
192 struct qxl_device *qdev;
193 struct qxl_ttm_tt *gtt;
195 qdev = qxl_get_qdev(bo->bdev);
196 gtt = kzalloc(sizeof(struct qxl_ttm_tt), GFP_KERNEL);
199 gtt->ttm.func = &qxl_backend_func;
201 if (ttm_tt_init(>t->ttm, bo, page_flags)) {
208 static void qxl_move_null(struct ttm_buffer_object *bo,
209 struct ttm_mem_reg *new_mem)
211 struct ttm_mem_reg *old_mem = &bo->mem;
213 BUG_ON(old_mem->mm_node != NULL);
215 new_mem->mm_node = NULL;
218 static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
219 struct ttm_operation_ctx *ctx,
220 struct ttm_mem_reg *new_mem)
222 struct ttm_mem_reg *old_mem = &bo->mem;
225 ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
229 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
230 qxl_move_null(bo, new_mem);
233 return ttm_bo_move_memcpy(bo, ctx, new_mem);
236 static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
238 struct ttm_mem_reg *new_mem)
241 struct qxl_device *qdev;
243 if (!qxl_ttm_bo_is_qxl_bo(bo))
246 qdev = to_qxl(qbo->tbo.base.dev);
248 if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
249 qxl_surface_evict(qdev, qbo, new_mem ? true : false);
252 static struct ttm_bo_driver qxl_bo_driver = {
253 .ttm_tt_create = &qxl_ttm_tt_create,
254 .init_mem_type = &qxl_init_mem_type,
255 .eviction_valuable = ttm_bo_eviction_valuable,
256 .evict_flags = &qxl_evict_flags,
257 .move = &qxl_bo_move,
258 .io_mem_reserve = &qxl_ttm_io_mem_reserve,
259 .io_mem_free = &qxl_ttm_io_mem_free,
260 .move_notify = &qxl_bo_move_notify,
263 int qxl_ttm_init(struct qxl_device *qdev)
266 int num_io_pages; /* != rom->num_io_pages, we include surface0 */
268 /* No others user of address space so set it to 0 */
269 r = ttm_bo_device_init(&qdev->mman.bdev,
271 qdev->ddev.anon_inode->i_mapping,
272 qdev->ddev.vma_offset_manager,
275 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
278 /* NOTE: this includes the framebuffer (aka surface 0) */
279 num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
280 r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_VRAM,
283 DRM_ERROR("Failed initializing VRAM heap.\n");
286 r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_PRIV,
287 qdev->surfaceram_size / PAGE_SIZE);
289 DRM_ERROR("Failed initializing Surfaces heap.\n");
292 DRM_INFO("qxl: %uM of VRAM memory size\n",
293 (unsigned int)qdev->vram_size / (1024 * 1024));
294 DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
295 ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
296 DRM_INFO("qxl: %uM of Surface memory size\n",
297 (unsigned int)qdev->surfaceram_size / (1024 * 1024));
301 void qxl_ttm_fini(struct qxl_device *qdev)
303 ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_VRAM);
304 ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_PRIV);
305 ttm_bo_device_release(&qdev->mman.bdev);
306 DRM_INFO("qxl: ttm finalized\n");
309 #define QXL_DEBUGFS_MEM_TYPES 2
311 #if defined(CONFIG_DEBUG_FS)
312 static int qxl_mm_dump_table(struct seq_file *m, void *data)
314 struct drm_info_node *node = (struct drm_info_node *)m->private;
315 struct drm_mm *mm = (struct drm_mm *)node->info_ent->data;
316 struct drm_printer p = drm_seq_file_printer(m);
318 spin_lock(&ttm_bo_glob.lru_lock);
319 drm_mm_print(mm, &p);
320 spin_unlock(&ttm_bo_glob.lru_lock);
325 void qxl_ttm_debugfs_init(struct qxl_device *qdev)
327 #if defined(CONFIG_DEBUG_FS)
328 static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
329 static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
332 for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
334 sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
336 sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
337 qxl_mem_types_list[i].name = qxl_mem_types_names[i];
338 qxl_mem_types_list[i].show = &qxl_mm_dump_table;
339 qxl_mem_types_list[i].driver_features = 0;
341 qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_VRAM].priv;
343 qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_PRIV].priv;
346 qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);