xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/qxl/qxl_ttm.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: qxl_ttm.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2013 Red Hat Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Dave Airlie
25  *          Alon Levy
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: qxl_ttm.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $");
30 
31 #include <linux/delay.h>
32 
33 #include <drm/drm.h>
34 #include <drm/drm_file.h>
35 #include <drm/drm_debugfs.h>
36 #include <drm/qxl_drm.h>
37 #include <drm/ttm/ttm_bo_api.h>
38 #include <drm/ttm/ttm_bo_driver.h>
39 #include <drm/ttm/ttm_module.h>
40 #include <drm/ttm/ttm_page_alloc.h>
41 #include <drm/ttm/ttm_placement.h>
42 
43 #include "qxl_drv.h"
44 #include "qxl_object.h"
45 
qxl_get_qdev(struct ttm_bo_device * bdev)46 static struct qxl_device *qxl_get_qdev(struct ttm_bo_device *bdev)
47 {
48 	struct qxl_mman *mman;
49 	struct qxl_device *qdev;
50 
51 	mman = container_of(bdev, struct qxl_mman, bdev);
52 	qdev = container_of(mman, struct qxl_device, mman);
53 	return qdev;
54 }
55 
qxl_invalidate_caches(struct ttm_bo_device * bdev,uint32_t flags)56 static int qxl_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
57 {
58 	return 0;
59 }
60 
qxl_init_mem_type(struct ttm_bo_device * bdev,uint32_t type,struct ttm_mem_type_manager * man)61 static int qxl_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
62 			     struct ttm_mem_type_manager *man)
63 {
64 	struct qxl_device *qdev = qxl_get_qdev(bdev);
65 	unsigned int gpu_offset_shift =
66 		64 - (qdev->rom->slot_gen_bits + qdev->rom->slot_id_bits + 8);
67 	struct qxl_memslot *slot;
68 
69 	switch (type) {
70 	case TTM_PL_SYSTEM:
71 		/* System memory */
72 		man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
73 		man->available_caching = TTM_PL_MASK_CACHING;
74 		man->default_caching = TTM_PL_FLAG_CACHED;
75 		break;
76 	case TTM_PL_VRAM:
77 	case TTM_PL_PRIV:
78 		/* "On-card" video ram */
79 		slot = (type == TTM_PL_VRAM) ?
80 			&qdev->main_slot : &qdev->surfaces_slot;
81 		slot->gpu_offset = (uint64_t)type << gpu_offset_shift;
82 		man->func = &ttm_bo_manager_func;
83 		man->gpu_offset = slot->gpu_offset;
84 		man->flags = TTM_MEMTYPE_FLAG_FIXED |
85 			     TTM_MEMTYPE_FLAG_MAPPABLE;
86 		man->available_caching = TTM_PL_MASK_CACHING;
87 		man->default_caching = TTM_PL_FLAG_CACHED;
88 		break;
89 	default:
90 		DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
91 		return -EINVAL;
92 	}
93 	return 0;
94 }
95 
qxl_evict_flags(struct ttm_buffer_object * bo,struct ttm_placement * placement)96 static void qxl_evict_flags(struct ttm_buffer_object *bo,
97 				struct ttm_placement *placement)
98 {
99 	struct qxl_bo *qbo;
100 	static const struct ttm_place placements = {
101 		.fpfn = 0,
102 		.lpfn = 0,
103 		.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
104 	};
105 
106 	if (!qxl_ttm_bo_is_qxl_bo(bo)) {
107 		placement->placement = &placements;
108 		placement->busy_placement = &placements;
109 		placement->num_placement = 1;
110 		placement->num_busy_placement = 1;
111 		return;
112 	}
113 	qbo = to_qxl_bo(bo);
114 	qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
115 	*placement = qbo->placement;
116 }
117 
qxl_ttm_io_mem_reserve(struct ttm_bo_device * bdev,struct ttm_mem_reg * mem)118 int qxl_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
119 			   struct ttm_mem_reg *mem)
120 {
121 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
122 	struct qxl_device *qdev = qxl_get_qdev(bdev);
123 
124 	mem->bus.addr = NULL;
125 	mem->bus.offset = 0;
126 	mem->bus.size = mem->num_pages << PAGE_SHIFT;
127 	mem->bus.base = 0;
128 	mem->bus.is_iomem = false;
129 	if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
130 		return -EINVAL;
131 	switch (mem->mem_type) {
132 	case TTM_PL_SYSTEM:
133 		/* system memory */
134 		return 0;
135 	case TTM_PL_VRAM:
136 		mem->bus.is_iomem = true;
137 		mem->bus.base = qdev->vram_base;
138 		mem->bus.offset = mem->start << PAGE_SHIFT;
139 		break;
140 	case TTM_PL_PRIV:
141 		mem->bus.is_iomem = true;
142 		mem->bus.base = qdev->surfaceram_base;
143 		mem->bus.offset = mem->start << PAGE_SHIFT;
144 		break;
145 	default:
146 		return -EINVAL;
147 	}
148 	return 0;
149 }
150 
qxl_ttm_io_mem_free(struct ttm_bo_device * bdev,struct ttm_mem_reg * mem)151 static void qxl_ttm_io_mem_free(struct ttm_bo_device *bdev,
152 				struct ttm_mem_reg *mem)
153 {
154 }
155 
156 /*
157  * TTM backend functions.
158  */
159 struct qxl_ttm_tt {
160 	struct ttm_tt		        ttm;
161 	struct qxl_device		*qdev;
162 	u64				offset;
163 };
164 
qxl_ttm_backend_bind(struct ttm_tt * ttm,struct ttm_mem_reg * bo_mem)165 static int qxl_ttm_backend_bind(struct ttm_tt *ttm,
166 				struct ttm_mem_reg *bo_mem)
167 {
168 	struct qxl_ttm_tt *gtt = (void *)ttm;
169 
170 	gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
171 	if (!ttm->num_pages) {
172 		WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
173 		     ttm->num_pages, bo_mem, ttm);
174 	}
175 	/* Not implemented */
176 	return -1;
177 }
178 
qxl_ttm_backend_unbind(struct ttm_tt * ttm)179 static int qxl_ttm_backend_unbind(struct ttm_tt *ttm)
180 {
181 	/* Not implemented */
182 	return -1;
183 }
184 
qxl_ttm_backend_destroy(struct ttm_tt * ttm)185 static void qxl_ttm_backend_destroy(struct ttm_tt *ttm)
186 {
187 	struct qxl_ttm_tt *gtt = (void *)ttm;
188 
189 	ttm_tt_fini(&gtt->ttm);
190 	kfree(gtt);
191 }
192 
193 static struct ttm_backend_func qxl_backend_func = {
194 	.bind = &qxl_ttm_backend_bind,
195 	.unbind = &qxl_ttm_backend_unbind,
196 	.destroy = &qxl_ttm_backend_destroy,
197 };
198 
qxl_ttm_tt_create(struct ttm_buffer_object * bo,uint32_t page_flags)199 static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
200 					uint32_t page_flags)
201 {
202 	struct qxl_device *qdev;
203 	struct qxl_ttm_tt *gtt;
204 
205 	qdev = qxl_get_qdev(bo->bdev);
206 	gtt = kzalloc(sizeof(struct qxl_ttm_tt), GFP_KERNEL);
207 	if (gtt == NULL)
208 		return NULL;
209 	gtt->ttm.func = &qxl_backend_func;
210 	gtt->qdev = qdev;
211 	if (ttm_tt_init(&gtt->ttm, bo, page_flags)) {
212 		kfree(gtt);
213 		return NULL;
214 	}
215 	return &gtt->ttm;
216 }
217 
qxl_move_null(struct ttm_buffer_object * bo,struct ttm_mem_reg * new_mem)218 static void qxl_move_null(struct ttm_buffer_object *bo,
219 			     struct ttm_mem_reg *new_mem)
220 {
221 	struct ttm_mem_reg *old_mem = &bo->mem;
222 
223 	BUG_ON(old_mem->mm_node != NULL);
224 	*old_mem = *new_mem;
225 	new_mem->mm_node = NULL;
226 }
227 
qxl_bo_move(struct ttm_buffer_object * bo,bool evict,struct ttm_operation_ctx * ctx,struct ttm_mem_reg * new_mem)228 static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
229 		       struct ttm_operation_ctx *ctx,
230 		       struct ttm_mem_reg *new_mem)
231 {
232 	struct ttm_mem_reg *old_mem = &bo->mem;
233 	int ret;
234 
235 	ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
236 	if (ret)
237 		return ret;
238 
239 	if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
240 		qxl_move_null(bo, new_mem);
241 		return 0;
242 	}
243 	return ttm_bo_move_memcpy(bo, ctx, new_mem);
244 }
245 
qxl_bo_move_notify(struct ttm_buffer_object * bo,bool evict,struct ttm_mem_reg * new_mem)246 static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
247 			       bool evict,
248 			       struct ttm_mem_reg *new_mem)
249 {
250 	struct qxl_bo *qbo;
251 	struct qxl_device *qdev;
252 
253 	if (!qxl_ttm_bo_is_qxl_bo(bo))
254 		return;
255 	qbo = to_qxl_bo(bo);
256 	qdev = qbo->tbo.base.dev->dev_private;
257 
258 	if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
259 		qxl_surface_evict(qdev, qbo, new_mem ? true : false);
260 }
261 
262 static struct ttm_bo_driver qxl_bo_driver = {
263 	.ttm_tt_create = &qxl_ttm_tt_create,
264 	.invalidate_caches = &qxl_invalidate_caches,
265 	.init_mem_type = &qxl_init_mem_type,
266 	.eviction_valuable = ttm_bo_eviction_valuable,
267 	.evict_flags = &qxl_evict_flags,
268 	.move = &qxl_bo_move,
269 	.io_mem_reserve = &qxl_ttm_io_mem_reserve,
270 	.io_mem_free = &qxl_ttm_io_mem_free,
271 	.move_notify = &qxl_bo_move_notify,
272 };
273 
qxl_ttm_init(struct qxl_device * qdev)274 int qxl_ttm_init(struct qxl_device *qdev)
275 {
276 	int r;
277 	int num_io_pages; /* != rom->num_io_pages, we include surface0 */
278 
279 	/* No others user of address space so set it to 0 */
280 	r = ttm_bo_device_init(&qdev->mman.bdev,
281 			       &qxl_bo_driver,
282 			       qdev->ddev.anon_inode->i_mapping,
283 			       qdev->ddev.vma_offset_manager,
284 			       false);
285 	if (r) {
286 		DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
287 		return r;
288 	}
289 	/* NOTE: this includes the framebuffer (aka surface 0) */
290 	num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
291 	r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_VRAM,
292 			   num_io_pages);
293 	if (r) {
294 		DRM_ERROR("Failed initializing VRAM heap.\n");
295 		return r;
296 	}
297 	r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_PRIV,
298 			   qdev->surfaceram_size / PAGE_SIZE);
299 	if (r) {
300 		DRM_ERROR("Failed initializing Surfaces heap.\n");
301 		return r;
302 	}
303 	DRM_INFO("qxl: %uM of VRAM memory size\n",
304 		 (unsigned int)qdev->vram_size / (1024 * 1024));
305 	DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
306 		 ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
307 	DRM_INFO("qxl: %uM of Surface memory size\n",
308 		 (unsigned int)qdev->surfaceram_size / (1024 * 1024));
309 	return 0;
310 }
311 
qxl_ttm_fini(struct qxl_device * qdev)312 void qxl_ttm_fini(struct qxl_device *qdev)
313 {
314 	ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_VRAM);
315 	ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_PRIV);
316 	ttm_bo_device_release(&qdev->mman.bdev);
317 	DRM_INFO("qxl: ttm finalized\n");
318 }
319 
320 #define QXL_DEBUGFS_MEM_TYPES 2
321 
322 #if defined(CONFIG_DEBUG_FS)
qxl_mm_dump_table(struct seq_file * m,void * data)323 static int qxl_mm_dump_table(struct seq_file *m, void *data)
324 {
325 	struct drm_info_node *node = (struct drm_info_node *)m->private;
326 	struct drm_mm *mm = (struct drm_mm *)node->info_ent->data;
327 	struct drm_printer p = drm_seq_file_printer(m);
328 
329 	spin_lock(&ttm_bo_glob.lru_lock);
330 	drm_mm_print(mm, &p);
331 	spin_unlock(&ttm_bo_glob.lru_lock);
332 	return 0;
333 }
334 #endif
335 
qxl_ttm_debugfs_init(struct qxl_device * qdev)336 int qxl_ttm_debugfs_init(struct qxl_device *qdev)
337 {
338 #if defined(CONFIG_DEBUG_FS)
339 	static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
340 	static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
341 	unsigned int i;
342 
343 	for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
344 		if (i == 0)
345 			sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
346 		else
347 			sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
348 		qxl_mem_types_list[i].name = qxl_mem_types_names[i];
349 		qxl_mem_types_list[i].show = &qxl_mm_dump_table;
350 		qxl_mem_types_list[i].driver_features = 0;
351 		if (i == 0)
352 			qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_VRAM].priv;
353 		else
354 			qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_PRIV].priv;
355 
356 	}
357 	return qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);
358 #else
359 	return 0;
360 #endif
361 }
362