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