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