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