xref: /linux/drivers/gpu/drm/qxl/qxl_ttm.c (revision 84b9b44b)
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.h>
33 #include <drm/ttm/ttm_placement.h>
34 #include <drm/ttm/ttm_range_manager.h>
35 #include <drm/ttm/ttm_tt.h>
36 
37 #include "qxl_drv.h"
38 #include "qxl_object.h"
39 
40 static struct qxl_device *qxl_get_qdev(struct ttm_device *bdev)
41 {
42 	struct qxl_mman *mman;
43 	struct qxl_device *qdev;
44 
45 	mman = container_of(bdev, struct qxl_mman, bdev);
46 	qdev = container_of(mman, struct qxl_device, mman);
47 	return qdev;
48 }
49 
50 static void qxl_evict_flags(struct ttm_buffer_object *bo,
51 				struct ttm_placement *placement)
52 {
53 	struct qxl_bo *qbo;
54 	static const struct ttm_place placements = {
55 		.fpfn = 0,
56 		.lpfn = 0,
57 		.mem_type = TTM_PL_SYSTEM,
58 		.flags = 0
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);
70 	*placement = qbo->placement;
71 }
72 
73 int qxl_ttm_io_mem_reserve(struct ttm_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.offset = (mem->start << PAGE_SHIFT) + qdev->vram_base;
85 		mem->bus.caching = ttm_write_combined;
86 		break;
87 	case TTM_PL_PRIV:
88 		mem->bus.is_iomem = true;
89 		mem->bus.offset = (mem->start << PAGE_SHIFT) +
90 			qdev->surfaceram_base;
91 		mem->bus.caching = ttm_write_combined;
92 		break;
93 	default:
94 		return -EINVAL;
95 	}
96 	return 0;
97 }
98 
99 /*
100  * TTM backend functions.
101  */
102 static void qxl_ttm_backend_destroy(struct ttm_device *bdev, struct ttm_tt *ttm)
103 {
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, 0)) {
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 			       struct ttm_resource *new_mem)
125 {
126 	struct qxl_bo *qbo;
127 	struct qxl_device *qdev;
128 
129 	if (!qxl_ttm_bo_is_qxl_bo(bo) || !bo->resource)
130 		return;
131 	qbo = to_qxl_bo(bo);
132 	qdev = to_qxl(qbo->tbo.base.dev);
133 
134 	if (bo->resource->mem_type == TTM_PL_PRIV && qbo->surface_id)
135 		qxl_surface_evict(qdev, qbo, new_mem ? true : false);
136 }
137 
138 static int qxl_bo_move(struct ttm_buffer_object *bo, bool evict,
139 		       struct ttm_operation_ctx *ctx,
140 		       struct ttm_resource *new_mem,
141 		       struct ttm_place *hop)
142 {
143 	struct ttm_resource *old_mem = bo->resource;
144 	int ret;
145 
146 	if (!old_mem) {
147 		if (new_mem->mem_type != TTM_PL_SYSTEM) {
148 			hop->mem_type = TTM_PL_SYSTEM;
149 			hop->flags = TTM_PL_FLAG_TEMPORARY;
150 			return -EMULTIHOP;
151 		}
152 
153 		ttm_bo_move_null(bo, new_mem);
154 		return 0;
155 	}
156 
157 	qxl_bo_move_notify(bo, new_mem);
158 
159 	ret = ttm_bo_wait_ctx(bo, ctx);
160 	if (ret)
161 		return ret;
162 
163 	if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
164 		ttm_bo_move_null(bo, new_mem);
165 		return 0;
166 	}
167 	return ttm_bo_move_memcpy(bo, ctx, new_mem);
168 }
169 
170 static void qxl_bo_delete_mem_notify(struct ttm_buffer_object *bo)
171 {
172 	qxl_bo_move_notify(bo, NULL);
173 }
174 
175 static struct ttm_device_funcs qxl_bo_driver = {
176 	.ttm_tt_create = &qxl_ttm_tt_create,
177 	.ttm_tt_destroy = &qxl_ttm_backend_destroy,
178 	.eviction_valuable = ttm_bo_eviction_valuable,
179 	.evict_flags = &qxl_evict_flags,
180 	.move = &qxl_bo_move,
181 	.io_mem_reserve = &qxl_ttm_io_mem_reserve,
182 	.delete_mem_notify = &qxl_bo_delete_mem_notify,
183 };
184 
185 static int qxl_ttm_init_mem_type(struct qxl_device *qdev,
186 				 unsigned int type,
187 				 uint64_t size)
188 {
189 	return ttm_range_man_init(&qdev->mman.bdev, type, false, size);
190 }
191 
192 int qxl_ttm_init(struct qxl_device *qdev)
193 {
194 	int r;
195 	int num_io_pages; /* != rom->num_io_pages, we include surface0 */
196 
197 	/* No others user of address space so set it to 0 */
198 	r = ttm_device_init(&qdev->mman.bdev, &qxl_bo_driver, NULL,
199 			    qdev->ddev.anon_inode->i_mapping,
200 			    qdev->ddev.vma_offset_manager,
201 			    false, false);
202 	if (r) {
203 		DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
204 		return r;
205 	}
206 	/* NOTE: this includes the framebuffer (aka surface 0) */
207 	num_io_pages = qdev->rom->ram_header_offset / PAGE_SIZE;
208 	r = qxl_ttm_init_mem_type(qdev, TTM_PL_VRAM, num_io_pages);
209 	if (r) {
210 		DRM_ERROR("Failed initializing VRAM heap.\n");
211 		return r;
212 	}
213 	r = qxl_ttm_init_mem_type(qdev, TTM_PL_PRIV,
214 				  qdev->surfaceram_size / PAGE_SIZE);
215 	if (r) {
216 		DRM_ERROR("Failed initializing Surfaces heap.\n");
217 		return r;
218 	}
219 	DRM_INFO("qxl: %uM of VRAM memory size\n",
220 		 (unsigned int)qdev->vram_size / (1024 * 1024));
221 	DRM_INFO("qxl: %luM of IO pages memory ready (VRAM domain)\n",
222 		 ((unsigned int)num_io_pages * PAGE_SIZE) / (1024 * 1024));
223 	DRM_INFO("qxl: %uM of Surface memory size\n",
224 		 (unsigned int)qdev->surfaceram_size / (1024 * 1024));
225 	return 0;
226 }
227 
228 void qxl_ttm_fini(struct qxl_device *qdev)
229 {
230 	ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_VRAM);
231 	ttm_range_man_fini(&qdev->mman.bdev, TTM_PL_PRIV);
232 	ttm_device_fini(&qdev->mman.bdev);
233 	DRM_INFO("qxl: ttm finalized\n");
234 }
235 
236 void qxl_ttm_debugfs_init(struct qxl_device *qdev)
237 {
238 #if defined(CONFIG_DEBUG_FS)
239 	ttm_resource_manager_create_debugfs(ttm_manager_type(&qdev->mman.bdev,
240 							     TTM_PL_VRAM),
241 					    qdev->ddev.primary->debugfs_root, "qxl_mem_mm");
242 	ttm_resource_manager_create_debugfs(ttm_manager_type(&qdev->mman.bdev,
243 							     TTM_PL_PRIV),
244 					    qdev->ddev.primary->debugfs_root, "qxl_surf_mm");
245 #endif
246 }
247