xref: /linux/drivers/gpu/drm/qxl/qxl_ttm.c (revision 56fb34d86e875dbb0d3e6a81c5d3d035db373031)
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 
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 
51 static struct vm_operations_struct qxl_ttm_vm_ops;
52 static const struct vm_operations_struct *ttm_vm_ops;
53 
54 static vm_fault_t qxl_ttm_fault(struct vm_fault *vmf)
55 {
56 	struct ttm_buffer_object *bo;
57 	vm_fault_t ret;
58 
59 	bo = (struct ttm_buffer_object *)vmf->vma->vm_private_data;
60 	if (bo == NULL)
61 		return VM_FAULT_NOPAGE;
62 	ret = ttm_vm_ops->fault(vmf);
63 	return ret;
64 }
65 
66 int qxl_mmap(struct file *filp, struct vm_area_struct *vma)
67 {
68 	int r;
69 	struct drm_file *file_priv = filp->private_data;
70 	struct qxl_device *qdev = file_priv->minor->dev->dev_private;
71 
72 	if (qdev == NULL) {
73 		DRM_ERROR(
74 		 "filp->private_data->minor->dev->dev_private == NULL\n");
75 		return -EINVAL;
76 	}
77 	DRM_DEBUG_DRIVER("filp->private_data = 0x%p, vma->vm_pgoff = %lx\n",
78 		  filp->private_data, vma->vm_pgoff);
79 
80 	r = ttm_bo_mmap(filp, vma, &qdev->mman.bdev);
81 	if (unlikely(r != 0))
82 		return r;
83 	if (unlikely(ttm_vm_ops == NULL)) {
84 		ttm_vm_ops = vma->vm_ops;
85 		qxl_ttm_vm_ops = *ttm_vm_ops;
86 		qxl_ttm_vm_ops.fault = &qxl_ttm_fault;
87 	}
88 	vma->vm_ops = &qxl_ttm_vm_ops;
89 	return 0;
90 }
91 
92 static int qxl_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
93 {
94 	return 0;
95 }
96 
97 static int qxl_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
98 			     struct ttm_mem_type_manager *man)
99 {
100 	struct qxl_device *qdev = qxl_get_qdev(bdev);
101 	unsigned int gpu_offset_shift =
102 		64 - (qdev->rom->slot_gen_bits + qdev->rom->slot_id_bits + 8);
103 	struct qxl_memslot *slot;
104 
105 	switch (type) {
106 	case TTM_PL_SYSTEM:
107 		/* System memory */
108 		man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
109 		man->available_caching = TTM_PL_MASK_CACHING;
110 		man->default_caching = TTM_PL_FLAG_CACHED;
111 		break;
112 	case TTM_PL_VRAM:
113 	case TTM_PL_PRIV:
114 		/* "On-card" video ram */
115 		slot = (type == TTM_PL_VRAM) ?
116 			&qdev->main_slot : &qdev->surfaces_slot;
117 		slot->gpu_offset = (uint64_t)type << gpu_offset_shift;
118 		man->func = &ttm_bo_manager_func;
119 		man->gpu_offset = slot->gpu_offset;
120 		man->flags = TTM_MEMTYPE_FLAG_FIXED |
121 			     TTM_MEMTYPE_FLAG_MAPPABLE;
122 		man->available_caching = TTM_PL_MASK_CACHING;
123 		man->default_caching = TTM_PL_FLAG_CACHED;
124 		break;
125 	default:
126 		DRM_ERROR("Unsupported memory type %u\n", (unsigned int)type);
127 		return -EINVAL;
128 	}
129 	return 0;
130 }
131 
132 static void qxl_evict_flags(struct ttm_buffer_object *bo,
133 				struct ttm_placement *placement)
134 {
135 	struct qxl_bo *qbo;
136 	static const struct ttm_place placements = {
137 		.fpfn = 0,
138 		.lpfn = 0,
139 		.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
140 	};
141 
142 	if (!qxl_ttm_bo_is_qxl_bo(bo)) {
143 		placement->placement = &placements;
144 		placement->busy_placement = &placements;
145 		placement->num_placement = 1;
146 		placement->num_busy_placement = 1;
147 		return;
148 	}
149 	qbo = to_qxl_bo(bo);
150 	qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
151 	*placement = qbo->placement;
152 }
153 
154 static int qxl_verify_access(struct ttm_buffer_object *bo, struct file *filp)
155 {
156 	struct qxl_bo *qbo = to_qxl_bo(bo);
157 
158 	return drm_vma_node_verify_access(&qbo->tbo.base.vma_node,
159 					  filp->private_data);
160 }
161 
162 static int qxl_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
163 				  struct ttm_mem_reg *mem)
164 {
165 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
166 	struct qxl_device *qdev = qxl_get_qdev(bdev);
167 
168 	mem->bus.addr = NULL;
169 	mem->bus.offset = 0;
170 	mem->bus.size = mem->num_pages << PAGE_SHIFT;
171 	mem->bus.base = 0;
172 	mem->bus.is_iomem = false;
173 	if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
174 		return -EINVAL;
175 	switch (mem->mem_type) {
176 	case TTM_PL_SYSTEM:
177 		/* system memory */
178 		return 0;
179 	case TTM_PL_VRAM:
180 		mem->bus.is_iomem = true;
181 		mem->bus.base = qdev->vram_base;
182 		mem->bus.offset = mem->start << PAGE_SHIFT;
183 		break;
184 	case TTM_PL_PRIV:
185 		mem->bus.is_iomem = true;
186 		mem->bus.base = qdev->surfaceram_base;
187 		mem->bus.offset = mem->start << PAGE_SHIFT;
188 		break;
189 	default:
190 		return -EINVAL;
191 	}
192 	return 0;
193 }
194 
195 static void qxl_ttm_io_mem_free(struct ttm_bo_device *bdev,
196 				struct ttm_mem_reg *mem)
197 {
198 }
199 
200 /*
201  * TTM backend functions.
202  */
203 struct qxl_ttm_tt {
204 	struct ttm_tt		        ttm;
205 	struct qxl_device		*qdev;
206 	u64				offset;
207 };
208 
209 static int qxl_ttm_backend_bind(struct ttm_tt *ttm,
210 				struct ttm_mem_reg *bo_mem)
211 {
212 	struct qxl_ttm_tt *gtt = (void *)ttm;
213 
214 	gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
215 	if (!ttm->num_pages) {
216 		WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
217 		     ttm->num_pages, bo_mem, ttm);
218 	}
219 	/* Not implemented */
220 	return -1;
221 }
222 
223 static int qxl_ttm_backend_unbind(struct ttm_tt *ttm)
224 {
225 	/* Not implemented */
226 	return -1;
227 }
228 
229 static void qxl_ttm_backend_destroy(struct ttm_tt *ttm)
230 {
231 	struct qxl_ttm_tt *gtt = (void *)ttm;
232 
233 	ttm_tt_fini(&gtt->ttm);
234 	kfree(gtt);
235 }
236 
237 static struct ttm_backend_func qxl_backend_func = {
238 	.bind = &qxl_ttm_backend_bind,
239 	.unbind = &qxl_ttm_backend_unbind,
240 	.destroy = &qxl_ttm_backend_destroy,
241 };
242 
243 static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
244 					uint32_t page_flags)
245 {
246 	struct qxl_device *qdev;
247 	struct qxl_ttm_tt *gtt;
248 
249 	qdev = qxl_get_qdev(bo->bdev);
250 	gtt = kzalloc(sizeof(struct qxl_ttm_tt), GFP_KERNEL);
251 	if (gtt == NULL)
252 		return NULL;
253 	gtt->ttm.func = &qxl_backend_func;
254 	gtt->qdev = qdev;
255 	if (ttm_tt_init(&gtt->ttm, bo, page_flags)) {
256 		kfree(gtt);
257 		return NULL;
258 	}
259 	return &gtt->ttm;
260 }
261 
262 static void qxl_move_null(struct ttm_buffer_object *bo,
263 			     struct ttm_mem_reg *new_mem)
264 {
265 	struct ttm_mem_reg *old_mem = &bo->mem;
266 
267 	BUG_ON(old_mem->mm_node != NULL);
268 	*old_mem = *new_mem;
269 	new_mem->mm_node = NULL;
270 }
271 
272 static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
273 		       struct ttm_operation_ctx *ctx,
274 		       struct ttm_mem_reg *new_mem)
275 {
276 	struct ttm_mem_reg *old_mem = &bo->mem;
277 	int ret;
278 
279 	ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
280 	if (ret)
281 		return ret;
282 
283 	if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
284 		qxl_move_null(bo, new_mem);
285 		return 0;
286 	}
287 	return ttm_bo_move_memcpy(bo, ctx, new_mem);
288 }
289 
290 static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
291 			       bool evict,
292 			       struct ttm_mem_reg *new_mem)
293 {
294 	struct qxl_bo *qbo;
295 	struct qxl_device *qdev;
296 
297 	if (!qxl_ttm_bo_is_qxl_bo(bo))
298 		return;
299 	qbo = to_qxl_bo(bo);
300 	qdev = qbo->tbo.base.dev->dev_private;
301 
302 	if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
303 		qxl_surface_evict(qdev, qbo, new_mem ? true : false);
304 }
305 
306 static struct ttm_bo_driver qxl_bo_driver = {
307 	.ttm_tt_create = &qxl_ttm_tt_create,
308 	.invalidate_caches = &qxl_invalidate_caches,
309 	.init_mem_type = &qxl_init_mem_type,
310 	.eviction_valuable = ttm_bo_eviction_valuable,
311 	.evict_flags = &qxl_evict_flags,
312 	.move = &qxl_bo_move,
313 	.verify_access = &qxl_verify_access,
314 	.io_mem_reserve = &qxl_ttm_io_mem_reserve,
315 	.io_mem_free = &qxl_ttm_io_mem_free,
316 	.move_notify = &qxl_bo_move_notify,
317 };
318 
319 int qxl_ttm_init(struct qxl_device *qdev)
320 {
321 	int r;
322 	int num_io_pages; /* != rom->num_io_pages, we include surface0 */
323 
324 	/* No others user of address space so set it to 0 */
325 	r = ttm_bo_device_init(&qdev->mman.bdev,
326 			       &qxl_bo_driver,
327 			       qdev->ddev.anon_inode->i_mapping,
328 			       false);
329 	if (r) {
330 		DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
331 		return r;
332 	}
333 	/* NOTE: this includes the framebuffer (aka surface 0) */
334 	num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
335 	r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_VRAM,
336 			   num_io_pages);
337 	if (r) {
338 		DRM_ERROR("Failed initializing VRAM heap.\n");
339 		return r;
340 	}
341 	r = ttm_bo_init_mm(&qdev->mman.bdev, TTM_PL_PRIV,
342 			   qdev->surfaceram_size / PAGE_SIZE);
343 	if (r) {
344 		DRM_ERROR("Failed initializing Surfaces heap.\n");
345 		return r;
346 	}
347 	DRM_INFO("qxl: %uM of VRAM memory size\n",
348 		 (unsigned int)qdev->vram_size / (1024 * 1024));
349 	DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
350 		 ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
351 	DRM_INFO("qxl: %uM of Surface memory size\n",
352 		 (unsigned int)qdev->surfaceram_size / (1024 * 1024));
353 	return 0;
354 }
355 
356 void qxl_ttm_fini(struct qxl_device *qdev)
357 {
358 	ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_VRAM);
359 	ttm_bo_clean_mm(&qdev->mman.bdev, TTM_PL_PRIV);
360 	ttm_bo_device_release(&qdev->mman.bdev);
361 	DRM_INFO("qxl: ttm finalized\n");
362 }
363 
364 #define QXL_DEBUGFS_MEM_TYPES 2
365 
366 #if defined(CONFIG_DEBUG_FS)
367 static int qxl_mm_dump_table(struct seq_file *m, void *data)
368 {
369 	struct drm_info_node *node = (struct drm_info_node *)m->private;
370 	struct drm_mm *mm = (struct drm_mm *)node->info_ent->data;
371 	struct drm_device *dev = node->minor->dev;
372 	struct qxl_device *rdev = dev->dev_private;
373 	struct ttm_bo_global *glob = rdev->mman.bdev.glob;
374 	struct drm_printer p = drm_seq_file_printer(m);
375 
376 	spin_lock(&glob->lru_lock);
377 	drm_mm_print(mm, &p);
378 	spin_unlock(&glob->lru_lock);
379 	return 0;
380 }
381 #endif
382 
383 int qxl_ttm_debugfs_init(struct qxl_device *qdev)
384 {
385 #if defined(CONFIG_DEBUG_FS)
386 	static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
387 	static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
388 	unsigned int i;
389 
390 	for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
391 		if (i == 0)
392 			sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
393 		else
394 			sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
395 		qxl_mem_types_list[i].name = qxl_mem_types_names[i];
396 		qxl_mem_types_list[i].show = &qxl_mm_dump_table;
397 		qxl_mem_types_list[i].driver_features = 0;
398 		if (i == 0)
399 			qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_VRAM].priv;
400 		else
401 			qxl_mem_types_list[i].data = qdev->mman.bdev.man[TTM_PL_PRIV].priv;
402 
403 	}
404 	return qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);
405 #else
406 	return 0;
407 #endif
408 }
409