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