WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpu / drm / qxl / qxl_ttm.c
blob7dd0c69baa478dc02c20f73d6ba698384f479895
1 /*
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
23 * Alon Levy
26 #include <linux/delay.h>
28 #include <drm/drm.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_placement.h>
37 #include "qxl_drv.h"
38 #include "qxl_object.h"
40 static struct qxl_device *qxl_get_qdev(struct ttm_bo_device *bdev)
42 struct qxl_mman *mman;
43 struct qxl_device *qdev;
45 mman = container_of(bdev, struct qxl_mman, bdev);
46 qdev = container_of(mman, struct qxl_device, mman);
47 return qdev;
50 static void qxl_evict_flags(struct ttm_buffer_object *bo,
51 struct ttm_placement *placement)
53 struct qxl_bo *qbo;
54 static const struct ttm_place placements = {
55 .fpfn = 0,
56 .lpfn = 0,
57 .mem_type = TTM_PL_SYSTEM,
58 .flags = 0
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;
66 return;
68 qbo = to_qxl_bo(bo);
69 qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU);
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) {
79 case TTM_PL_SYSTEM:
80 /* system memory */
81 return 0;
82 case TTM_PL_VRAM:
83 mem->bus.is_iomem = true;
84 mem->bus.offset = (mem->start << PAGE_SHIFT) + qdev->vram_base;
85 mem->bus.caching = ttm_cached;
86 break;
87 case TTM_PL_PRIV:
88 mem->bus.is_iomem = true;
89 mem->bus.offset = (mem->start << PAGE_SHIFT) +
90 qdev->surfaceram_base;
91 mem->bus.caching = ttm_cached;
92 break;
93 default:
94 return -EINVAL;
96 return 0;
100 * TTM backend functions.
102 static void qxl_ttm_backend_destroy(struct ttm_bo_device *bdev,
103 struct ttm_tt *ttm)
105 ttm_tt_destroy_common(bdev, ttm);
106 ttm_tt_fini(ttm);
107 kfree(ttm);
110 static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
111 uint32_t page_flags)
113 struct ttm_tt *ttm;
115 ttm = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
116 if (ttm == NULL)
117 return NULL;
118 if (ttm_tt_init(ttm, bo, page_flags, ttm_cached)) {
119 kfree(ttm);
120 return NULL;
122 return ttm;
125 static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
126 bool evict,
127 struct ttm_resource *new_mem)
129 struct qxl_bo *qbo;
130 struct qxl_device *qdev;
132 if (!qxl_ttm_bo_is_qxl_bo(bo))
133 return;
134 qbo = to_qxl_bo(bo);
135 qdev = to_qxl(qbo->tbo.base.dev);
137 if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
138 qxl_surface_evict(qdev, qbo, new_mem ? true : false);
141 static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
142 struct ttm_operation_ctx *ctx,
143 struct ttm_resource *new_mem,
144 struct ttm_place *hop)
146 struct ttm_resource *old_mem = &bo->mem;
147 int ret;
149 qxl_bo_move_notify(bo, evict, new_mem);
151 ret = ttm_bo_wait_ctx(bo, ctx);
152 if (ret)
153 goto out;
155 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
156 ttm_bo_move_null(bo, new_mem);
157 return 0;
159 ret = ttm_bo_move_memcpy(bo, ctx, new_mem);
160 out:
161 if (ret) {
162 swap(*new_mem, bo->mem);
163 qxl_bo_move_notify(bo, false, new_mem);
164 swap(*new_mem, bo->mem);
166 return ret;
169 static void qxl_bo_delete_mem_notify(struct ttm_buffer_object *bo)
171 qxl_bo_move_notify(bo, false, NULL);
174 static struct ttm_bo_driver qxl_bo_driver = {
175 .ttm_tt_create = &qxl_ttm_tt_create,
176 .ttm_tt_destroy = &qxl_ttm_backend_destroy,
177 .eviction_valuable = ttm_bo_eviction_valuable,
178 .evict_flags = &qxl_evict_flags,
179 .move = &qxl_bo_move,
180 .io_mem_reserve = &qxl_ttm_io_mem_reserve,
181 .delete_mem_notify = &qxl_bo_delete_mem_notify,
184 static int qxl_ttm_init_mem_type(struct qxl_device *qdev,
185 unsigned int type,
186 uint64_t size)
188 return ttm_range_man_init(&qdev->mman.bdev, type, false, size);
191 int qxl_ttm_init(struct qxl_device *qdev)
193 int r;
194 int num_io_pages; /* != rom->num_io_pages, we include surface0 */
196 /* No others user of address space so set it to 0 */
197 r = ttm_bo_device_init(&qdev->mman.bdev, &qxl_bo_driver, NULL,
198 qdev->ddev.anon_inode->i_mapping,
199 qdev->ddev.vma_offset_manager,
200 false, false);
201 if (r) {
202 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
203 return r;
205 /* NOTE: this includes the framebuffer (aka surface 0) */
206 num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
207 r = qxl_ttm_init_mem_type(qdev, TTM_PL_VRAM, num_io_pages);
208 if (r) {
209 DRM_ERROR("Failed initializing VRAM heap.\n");
210 return r;
212 r = qxl_ttm_init_mem_type(qdev, TTM_PL_PRIV,
213 qdev->surfaceram_size / PAGE_SIZE);
214 if (r) {
215 DRM_ERROR("Failed initializing Surfaces heap.\n");
216 return r;
218 DRM_INFO("qxl: %uM of VRAM memory size\n",
219 (unsigned int)qdev->vram_size / (1024 * 1024));
220 DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
221 ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
222 DRM_INFO("qxl: %uM of Surface memory size\n",
223 (unsigned int)qdev->surfaceram_size / (1024 * 1024));
224 return 0;
227 void qxl_ttm_fini(struct qxl_device *qdev)
229 ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_VRAM);
230 ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_PRIV);
231 ttm_bo_device_release(&qdev->mman.bdev);
232 DRM_INFO("qxl: ttm finalized\n");
235 #define QXL_DEBUGFS_MEM_TYPES 2
237 #if defined(CONFIG_DEBUG_FS)
238 static int qxl_mm_dump_table(struct seq_file *m, void *data)
240 struct drm_info_node *node = (struct drm_info_node *)m->private;
241 struct ttm_resource_manager *man = (struct ttm_resource_manager *)node->info_ent->data;
242 struct drm_printer p = drm_seq_file_printer(m);
244 ttm_resource_manager_debug(man, &p);
245 return 0;
247 #endif
249 void qxl_ttm_debugfs_init(struct qxl_device *qdev)
251 #if defined(CONFIG_DEBUG_FS)
252 static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
253 static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
254 unsigned int i;
256 for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
257 if (i == 0)
258 sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
259 else
260 sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
261 qxl_mem_types_list[i].name = qxl_mem_types_names[i];
262 qxl_mem_types_list[i].show = &qxl_mm_dump_table;
263 qxl_mem_types_list[i].driver_features = 0;
264 if (i == 0)
265 qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_VRAM);
266 else
267 qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_PRIV);
270 qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);
271 #endif