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