xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_ttm.c (revision 8b0d47e8)
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 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 		.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
59 	};
60 
61 	if (!qxl_ttm_bo_is_qxl_bo(bo)) {
62 		placement->placement = &placements;
63 		placement->busy_placement = &placements;
64 		placement->num_placement = 1;
65 		placement->num_busy_placement = 1;
66 		return;
67 	}
68 	qbo = to_qxl_bo(bo);
69 	qxl_ttm_placement_from_domain(qbo, QXL_GEM_DOMAIN_CPU, false);
70 	*placement = qbo->placement;
71 }
72 
73 int qxl_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
74 			   struct ttm_resource *mem)
75 {
76 	struct qxl_device *qdev = qxl_get_qdev(bdev);
77 
78 	mem->bus.addr = NULL;
79 	mem->bus.offset = 0;
80 	mem->bus.size = mem->num_pages << PAGE_SHIFT;
81 	mem->bus.base = 0;
82 	mem->bus.is_iomem = false;
83 
84 	switch (mem->mem_type) {
85 	case TTM_PL_SYSTEM:
86 		/* system memory */
87 		return 0;
88 	case TTM_PL_VRAM:
89 		mem->bus.is_iomem = true;
90 		mem->bus.base = qdev->vram_base;
91 		mem->bus.offset = mem->start << PAGE_SHIFT;
92 		break;
93 	case TTM_PL_PRIV:
94 		mem->bus.is_iomem = true;
95 		mem->bus.base = qdev->surfaceram_base;
96 		mem->bus.offset = mem->start << PAGE_SHIFT;
97 		break;
98 	default:
99 		return -EINVAL;
100 	}
101 	return 0;
102 }
103 
104 /*
105  * TTM backend functions.
106  */
107 struct qxl_ttm_tt {
108 	struct ttm_tt		        ttm;
109 	struct qxl_device		*qdev;
110 	u64				offset;
111 };
112 
113 static int qxl_ttm_backend_bind(struct ttm_tt *ttm,
114 				struct ttm_resource *bo_mem)
115 {
116 	struct qxl_ttm_tt *gtt = (void *)ttm;
117 
118 	gtt->offset = (unsigned long)(bo_mem->start << PAGE_SHIFT);
119 	if (!ttm->num_pages) {
120 		WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
121 		     ttm->num_pages, bo_mem, ttm);
122 	}
123 	/* Not implemented */
124 	return -1;
125 }
126 
127 static int qxl_ttm_backend_unbind(struct ttm_tt *ttm)
128 {
129 	/* Not implemented */
130 	return -1;
131 }
132 
133 static void qxl_ttm_backend_destroy(struct ttm_tt *ttm)
134 {
135 	struct qxl_ttm_tt *gtt = (void *)ttm;
136 
137 	ttm_tt_fini(&gtt->ttm);
138 	kfree(gtt);
139 }
140 
141 static struct ttm_backend_func qxl_backend_func = {
142 	.bind = &qxl_ttm_backend_bind,
143 	.unbind = &qxl_ttm_backend_unbind,
144 	.destroy = &qxl_ttm_backend_destroy,
145 };
146 
147 static struct ttm_tt *qxl_ttm_tt_create(struct ttm_buffer_object *bo,
148 					uint32_t page_flags)
149 {
150 	struct qxl_device *qdev;
151 	struct qxl_ttm_tt *gtt;
152 
153 	qdev = qxl_get_qdev(bo->bdev);
154 	gtt = kzalloc(sizeof(struct qxl_ttm_tt), GFP_KERNEL);
155 	if (gtt == NULL)
156 		return NULL;
157 	gtt->ttm.func = &qxl_backend_func;
158 	gtt->qdev = qdev;
159 	if (ttm_tt_init(&gtt->ttm, bo, page_flags)) {
160 		kfree(gtt);
161 		return NULL;
162 	}
163 	return &gtt->ttm;
164 }
165 
166 static void qxl_move_null(struct ttm_buffer_object *bo,
167 			     struct ttm_resource *new_mem)
168 {
169 	struct ttm_resource *old_mem = &bo->mem;
170 
171 	BUG_ON(old_mem->mm_node != NULL);
172 	*old_mem = *new_mem;
173 	new_mem->mm_node = NULL;
174 }
175 
176 static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
177 		       struct ttm_operation_ctx *ctx,
178 		       struct ttm_resource *new_mem)
179 {
180 	struct ttm_resource *old_mem = &bo->mem;
181 	int ret;
182 
183 	ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
184 	if (ret)
185 		return ret;
186 
187 	if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
188 		qxl_move_null(bo, new_mem);
189 		return 0;
190 	}
191 	return ttm_bo_move_memcpy(bo, ctx, new_mem);
192 }
193 
194 static void qxl_bo_move_notify(struct ttm_buffer_object *bo,
195 			       bool evict,
196 			       struct ttm_resource *new_mem)
197 {
198 	struct qxl_bo *qbo;
199 	struct qxl_device *qdev;
200 
201 	if (!qxl_ttm_bo_is_qxl_bo(bo))
202 		return;
203 	qbo = to_qxl_bo(bo);
204 	qdev = to_qxl(qbo->tbo.base.dev);
205 
206 	if (bo->mem.mem_type == TTM_PL_PRIV && qbo->surface_id)
207 		qxl_surface_evict(qdev, qbo, new_mem ? true : false);
208 }
209 
210 static struct ttm_bo_driver qxl_bo_driver = {
211 	.ttm_tt_create = &qxl_ttm_tt_create,
212 	.eviction_valuable = ttm_bo_eviction_valuable,
213 	.evict_flags = &qxl_evict_flags,
214 	.move = &qxl_bo_move,
215 	.io_mem_reserve = &qxl_ttm_io_mem_reserve,
216 	.move_notify = &qxl_bo_move_notify,
217 };
218 
219 static int qxl_ttm_init_mem_type(struct qxl_device *qdev,
220 				 unsigned int type,
221 				 uint64_t size)
222 {
223 	return ttm_range_man_init(&qdev->mman.bdev, type, TTM_PL_MASK_CACHING,
224 				  TTM_PL_FLAG_CACHED, false, size);
225 }
226 
227 int qxl_ttm_init(struct qxl_device *qdev)
228 {
229 	int r;
230 	int num_io_pages; /* != rom->num_io_pages, we include surface0 */
231 
232 	/* No others user of address space so set it to 0 */
233 	r = ttm_bo_device_init(&qdev->mman.bdev,
234 			       &qxl_bo_driver,
235 			       qdev->ddev.anon_inode->i_mapping,
236 			       qdev->ddev.vma_offset_manager,
237 			       false);
238 	if (r) {
239 		DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
240 		return r;
241 	}
242 	/* NOTE: this includes the framebuffer (aka surface 0) */
243 	num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
244 	r = qxl_ttm_init_mem_type(qdev, TTM_PL_VRAM, num_io_pages);
245 	if (r) {
246 		DRM_ERROR("Failed initializing VRAM heap.\n");
247 		return r;
248 	}
249 	r = qxl_ttm_init_mem_type(qdev, TTM_PL_PRIV,
250 				  qdev->surfaceram_size / PAGE_SIZE);
251 	if (r) {
252 		DRM_ERROR("Failed initializing Surfaces heap.\n");
253 		return r;
254 	}
255 	DRM_INFO("qxl: %uM of VRAM memory size\n",
256 		 (unsigned int)qdev->vram_size / (1024 * 1024));
257 	DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
258 		 ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
259 	DRM_INFO("qxl: %uM of Surface memory size\n",
260 		 (unsigned int)qdev->surfaceram_size / (1024 * 1024));
261 	return 0;
262 }
263 
264 void qxl_ttm_fini(struct qxl_device *qdev)
265 {
266 	ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_VRAM);
267 	ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_PRIV);
268 	ttm_bo_device_release(&qdev->mman.bdev);
269 	DRM_INFO("qxl: ttm finalized\n");
270 }
271 
272 #define QXL_DEBUGFS_MEM_TYPES 2
273 
274 #if defined(CONFIG_DEBUG_FS)
275 static int qxl_mm_dump_table(struct seq_file *m, void *data)
276 {
277 	struct drm_info_node *node = (struct drm_info_node *)m->private;
278 	struct ttm_resource_manager *man = (struct ttm_resource_manager *)node->info_ent->data;
279 	struct drm_printer p = drm_seq_file_printer(m);
280 
281 	ttm_resource_manager_debug(man, &p);
282 	return 0;
283 }
284 #endif
285 
286 void qxl_ttm_debugfs_init(struct qxl_device *qdev)
287 {
288 #if defined(CONFIG_DEBUG_FS)
289 	static struct drm_info_list qxl_mem_types_list[QXL_DEBUGFS_MEM_TYPES];
290 	static char qxl_mem_types_names[QXL_DEBUGFS_MEM_TYPES][32];
291 	unsigned int i;
292 
293 	for (i = 0; i < QXL_DEBUGFS_MEM_TYPES; i++) {
294 		if (i == 0)
295 			sprintf(qxl_mem_types_names[i], "qxl_mem_mm");
296 		else
297 			sprintf(qxl_mem_types_names[i], "qxl_surf_mm");
298 		qxl_mem_types_list[i].name = qxl_mem_types_names[i];
299 		qxl_mem_types_list[i].show = &qxl_mm_dump_table;
300 		qxl_mem_types_list[i].driver_features = 0;
301 		if (i == 0)
302 			qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_VRAM);
303 		else
304 			qxl_mem_types_list[i].data = ttm_manager_type(&qdev->mman.bdev, TTM_PL_PRIV);
305 
306 	}
307 	qxl_debugfs_add_files(qdev, qxl_mem_types_list, i);
308 #endif
309 }
310