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