1 /*
2 * Copyright 2013 Red Hat Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Dave Airlie
23 * Alon Levy
24 */
25
26 #include <linux/delay.h>
27
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_page_alloc.h>
36 #include <drm/ttm/ttm_placement.h>
37
38 #include "qxl_drv.h"
39 #include "qxl_object.h"
40
qxl_get_qdev(struct ttm_bo_device * bdev)41 static struct qxl_device *qxl_get_qdev(struct ttm_bo_device *bdev)
42 {
43 struct qxl_mman *mman;
44 struct qxl_device *qdev;
45
46 mman = container_of(bdev, struct qxl_mman, bdev);
47 qdev = container_of(mman, struct qxl_device, mman);
48 return qdev;
49 }
50
qxl_evict_flags(struct ttm_buffer_object * bo,struct ttm_placement * placement)51 static void qxl_evict_flags(struct ttm_buffer_object *bo,
52 struct ttm_placement *placement)
53 {
54 struct qxl_bo *qbo;
55 static const struct ttm_place placements = {
56 .fpfn = 0,
57 .lpfn = 0,
58 .mem_type = TTM_PL_SYSTEM,
59 .flags = TTM_PL_MASK_CACHING
60 };
61
62 if (!qxl_ttm_bo_is_qxl_bo(bo)) {
63 placement->placement = &placements;
64 placement->busy_placement = &placements;
65 placement->num_placement = 1;
66 placement->num_busy_placement = 1;
67 return;
68 }
69 qbo = to_qxl_bo(bo);
70 qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
71 *placement = qbo->placement;
72 }
73
qxl_ttm_io_mem_reserve(struct ttm_bo_device * bdev,struct ttm_resource * mem)74 int qxl_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
75 struct ttm_resource *mem)
76 {
77 struct qxl_device *qdev = qxl_get_qdev(bdev);
78
79 switch (mem->mem_type) {
80 case TTM_PL_SYSTEM:
81 /* system memory */
82 return 0;
83 case TTM_PL_VRAM:
84 mem->bus.is_iomem = true;
85 mem->bus.offset = (mem->start << PAGE_SHIFT) + qdev->vram_base;
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 break;
92 default:
93 return -EINVAL;
94 }
95 return 0;
96 }
97
98 /*
99 * TTM backend functions.
100 */
101 struct qxl_ttm_tt {
102 struct ttm_tt ttm;
103 struct qxl_device *qdev;
104 u64 offset;
105 };
106
qxl_ttm_backend_bind(struct ttm_bo_device * bdev,struct ttm_tt * ttm,struct ttm_resource * bo_mem)107 static int qxl_ttm_backend_bind(struct ttm_bo_device *bdev,
108 struct ttm_tt *ttm,
109 struct ttm_resource *bo_mem)
110 {
111 struct qxl_ttm_tt *gtt = (void *)ttm;
112
113 gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
114 if (!ttm->num_pages) {
115 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
116 ttm->num_pages, bo_mem, ttm);
117 }
118 /* Not implemented */
119 return -1;
120 }
121
qxl_ttm_backend_unbind(struct ttm_bo_device * bdev,struct ttm_tt * ttm)122 static void qxl_ttm_backend_unbind(struct ttm_bo_device *bdev,
123 struct ttm_tt *ttm)
124 {
125 /* Not implemented */
126 }
127
qxl_ttm_backend_destroy(struct ttm_bo_device * bdev,struct ttm_tt * ttm)128 static void qxl_ttm_backend_destroy(struct ttm_bo_device *bdev,
129 struct ttm_tt *ttm)
130 {
131 struct qxl_ttm_tt *gtt = (void *)ttm;
132
133 ttm_tt_destroy_common(bdev, ttm);
134 ttm_tt_fini(>t->ttm);
135 kfree(gtt);
136 }
137
qxl_ttm_tt_create(struct ttm_buffer_object * bo,uint32_t page_flags)138 static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
139 uint32_t page_flags)
140 {
141 struct qxl_device *qdev;
142 struct qxl_ttm_tt *gtt;
143
144 qdev = qxl_get_qdev(bo->bdev);
145 gtt = kzalloc(sizeof(struct qxl_ttm_tt), GFP_KERNEL);
146 if (gtt == NULL)
147 return NULL;
148 gtt->qdev = qdev;
149 if (ttm_tt_init(>t->ttm, bo, page_flags)) {
150 kfree(gtt);
151 return NULL;
152 }
153 return >t->ttm;
154 }
155
qxl_bo_move(struct ttm_buffer_object * bo,bool evict,struct ttm_operation_ctx * ctx,struct ttm_resource * new_mem)156 static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
157 struct ttm_operation_ctx *ctx,
158 struct ttm_resource *new_mem)
159 {
160 struct ttm_resource *old_mem = &bo->mem;
161 int ret;
162
163 ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
164 if (ret)
165 return ret;
166
167 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
168 ttm_bo_move_null(bo, new_mem);
169 return 0;
170 }
171 return ttm_bo_move_memcpy(bo, ctx, new_mem);
172 }
173
qxl_bo_move_notify(struct ttm_buffer_object * bo,bool evict,struct ttm_resource * new_mem)174 static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
175 bool evict,
176 struct ttm_resource *new_mem)
177 {
178 struct qxl_bo *qbo;
179 struct qxl_device *qdev;
180
181 if (!qxl_ttm_bo_is_qxl_bo(bo))
182 return;
183 qbo = to_qxl_bo(bo);
184 qdev = to_qxl(qbo->tbo.base.dev);
185
186 if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
187 qxl_surface_evict(qdev, qbo, new_mem ? true : false);
188 }
189
190 static struct ttm_bo_driver qxl_bo_driver = {
191 .ttm_tt_create = &qxl_ttm_tt_create,
192 .ttm_tt_bind = &qxl_ttm_backend_bind,
193 .ttm_tt_destroy = &qxl_ttm_backend_destroy,
194 .ttm_tt_unbind = &qxl_ttm_backend_unbind,
195 .eviction_valuable = ttm_bo_eviction_valuable,
196 .evict_flags = &qxl_evict_flags,
197 .move = &qxl_bo_move,
198 .io_mem_reserve = &qxl_ttm_io_mem_reserve,
199 .move_notify = &qxl_bo_move_notify,
200 };
201
qxl_ttm_init_mem_type(struct qxl_device * qdev,unsigned int type,uint64_t size)202 static int qxl_ttm_init_mem_type(struct qxl_device *qdev,
203 unsigned int type,
204 uint64_t size)
205 {
206 return ttm_range_man_init(&qdev->mman.bdev, type, false, size);
207 }
208
qxl_ttm_init(struct qxl_device * qdev)209 int qxl_ttm_init(struct qxl_device *qdev)
210 {
211 int r;
212 int num_io_pages; /* != rom->num_io_pages, we include surface0 */
213
214 /* No others user of address space so set it to 0 */
215 r = ttm_bo_device_init(&qdev->mman.bdev,
216 &qxl_bo_driver,
217 qdev->ddev.anon_inode->i_mapping,
218 qdev->ddev.vma_offset_manager,
219 false);
220 if (r) {
221 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
222 return r;
223 }
224 /* NOTE: this includes the framebuffer (aka surface 0) */
225 num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
226 r = qxl_ttm_init_mem_type(qdev, TTM_PL_VRAM, num_io_pages);
227 if (r) {
228 DRM_ERROR("Failed initializing VRAM heap.\n");
229 return r;
230 }
231 r = qxl_ttm_init_mem_type(qdev, TTM_PL_PRIV,
232 qdev->surfaceram_size / PAGE_SIZE);
233 if (r) {
234 DRM_ERROR("Failed initializing Surfaces heap.\n");
235 return r;
236 }
237 DRM_INFO("qxl: %uM of VRAM memory size\n",
238 (unsigned int)qdev->vram_size / (1024 * 1024));
239 DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
240 ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
241 DRM_INFO("qxl: %uM of Surface memory size\n",
242 (unsigned int)qdev->surfaceram_size / (1024 * 1024));
243 return 0;
244 }
245
qxl_ttm_fini(struct qxl_device * qdev)246 void qxl_ttm_fini(struct qxl_device *qdev)
247 {
248 ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_VRAM);
249 ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_PRIV);
250 ttm_bo_device_release(&qdev->mman.bdev);
251 DRM_INFO("qxl: ttm finalized\n");
252 }
253
254 #define QXL_DEBUGFS_MEM_TYPES 2
255
256 #if defined(CONFIG_DEBUG_FS)
qxl_mm_dump_table(struct seq_file * m,void * data)257 static int qxl_mm_dump_table(struct seq_file *m, void *data)
258 {
259 struct drm_info_node *node = (struct drm_info_node *)m->private;
260 struct ttm_resource_manager *man = (struct ttm_resource_manager *)node->info_ent->data;
261 struct drm_printer p = drm_seq_file_printer(m);
262
263 ttm_resource_manager_debug(man, &p);
264 return 0;
265 }
266 #endif
267
qxl_ttm_debugfs_init(struct qxl_device * qdev)268 void qxl_ttm_debugfs_init(struct qxl_device *qdev)
269 {
270 #if defined(CONFIG_DEBUG_FS)
271 static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
272 static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
273 unsigned int i;
274
275 for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
276 if (i == 0)
277 sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
278 else
279 sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
280 qxl_mem_types_list[i].name = qxl_mem_types_names[i];
281 qxl_mem_types_list[i].show = &qxl_mm_dump_table;
282 qxl_mem_types_list[i].driver_features = 0;
283 if (i == 0)
284 qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_VRAM);
285 else
286 qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_PRIV);
287
288 }
289 qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);
290 #endif
291 }
292