xref: /linux/drivers/gpu/drm/qxl/qxl_object.c (revision b77e0ce62d63a761ffb7f7245a215a49f5921c2f)
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/dma-buf-map.h>
27 #include <linux/io-mapping.h>
28 
29 #include "qxl_drv.h"
30 #include "qxl_object.h"
31 
32 static void qxl_ttm_bo_destroy(struct ttm_buffer_object *tbo)
33 {
34 	struct qxl_bo *bo;
35 	struct qxl_device *qdev;
36 
37 	bo = to_qxl_bo(tbo);
38 	qdev = to_qxl(bo->tbo.base.dev);
39 
40 	qxl_surface_evict(qdev, bo, false);
41 	WARN_ON_ONCE(bo->map_count > 0);
42 	mutex_lock(&qdev->gem.mutex);
43 	list_del_init(&bo->list);
44 	mutex_unlock(&qdev->gem.mutex);
45 	drm_gem_object_release(&bo->tbo.base);
46 	kfree(bo);
47 }
48 
49 bool qxl_ttm_bo_is_qxl_bo(struct ttm_buffer_object *bo)
50 {
51 	if (bo->destroy == &qxl_ttm_bo_destroy)
52 		return true;
53 	return false;
54 }
55 
56 void qxl_ttm_placement_from_domain(struct qxl_bo *qbo, u32 domain)
57 {
58 	u32 c = 0;
59 	u32 pflag = 0;
60 	unsigned int i;
61 
62 	if (qbo->tbo.base.size <= PAGE_SIZE)
63 		pflag |= TTM_PL_FLAG_TOPDOWN;
64 
65 	qbo->placement.placement = qbo->placements;
66 	qbo->placement.busy_placement = qbo->placements;
67 	if (domain == QXL_GEM_DOMAIN_VRAM) {
68 		qbo->placements[c].mem_type = TTM_PL_VRAM;
69 		qbo->placements[c++].flags = pflag;
70 	}
71 	if (domain == QXL_GEM_DOMAIN_SURFACE) {
72 		qbo->placements[c].mem_type = TTM_PL_PRIV;
73 		qbo->placements[c++].flags = pflag;
74 		qbo->placements[c].mem_type = TTM_PL_VRAM;
75 		qbo->placements[c++].flags = pflag;
76 	}
77 	if (domain == QXL_GEM_DOMAIN_CPU) {
78 		qbo->placements[c].mem_type = TTM_PL_SYSTEM;
79 		qbo->placements[c++].flags = pflag;
80 	}
81 	if (!c) {
82 		qbo->placements[c].mem_type = TTM_PL_SYSTEM;
83 		qbo->placements[c++].flags = 0;
84 	}
85 	qbo->placement.num_placement = c;
86 	qbo->placement.num_busy_placement = c;
87 	for (i = 0; i < c; ++i) {
88 		qbo->placements[i].fpfn = 0;
89 		qbo->placements[i].lpfn = 0;
90 	}
91 }
92 
93 static const struct drm_gem_object_funcs qxl_object_funcs = {
94 	.free = qxl_gem_object_free,
95 	.open = qxl_gem_object_open,
96 	.close = qxl_gem_object_close,
97 	.pin = qxl_gem_prime_pin,
98 	.unpin = qxl_gem_prime_unpin,
99 	.get_sg_table = qxl_gem_prime_get_sg_table,
100 	.vmap = qxl_gem_prime_vmap,
101 	.vunmap = qxl_gem_prime_vunmap,
102 	.mmap = drm_gem_ttm_mmap,
103 	.print_info = drm_gem_ttm_print_info,
104 };
105 
106 int qxl_bo_create(struct qxl_device *qdev,
107 		  unsigned long size, bool kernel, bool pinned, u32 domain,
108 		  struct qxl_surface *surf,
109 		  struct qxl_bo **bo_ptr)
110 {
111 	struct ttm_operation_ctx ctx = { !kernel, false };
112 	struct qxl_bo *bo;
113 	enum ttm_bo_type type;
114 	int r;
115 
116 	if (kernel)
117 		type = ttm_bo_type_kernel;
118 	else
119 		type = ttm_bo_type_device;
120 	*bo_ptr = NULL;
121 	bo = kzalloc(sizeof(struct qxl_bo), GFP_KERNEL);
122 	if (bo == NULL)
123 		return -ENOMEM;
124 	size = roundup(size, PAGE_SIZE);
125 	r = drm_gem_object_init(&qdev->ddev, &bo->tbo.base, size);
126 	if (unlikely(r)) {
127 		kfree(bo);
128 		return r;
129 	}
130 	bo->tbo.base.funcs = &qxl_object_funcs;
131 	bo->type = domain;
132 	bo->surface_id = 0;
133 	INIT_LIST_HEAD(&bo->list);
134 
135 	if (surf)
136 		bo->surf = *surf;
137 
138 	qxl_ttm_placement_from_domain(bo, domain);
139 
140 	r = ttm_bo_init_reserved(&qdev->mman.bdev, &bo->tbo, size, type,
141 				 &bo->placement, 0, &ctx, size,
142 				 NULL, NULL, &qxl_ttm_bo_destroy);
143 	if (unlikely(r != 0)) {
144 		if (r != -ERESTARTSYS)
145 			dev_err(qdev->ddev.dev,
146 				"object_init failed for (%lu, 0x%08X)\n",
147 				size, domain);
148 		return r;
149 	}
150 	if (pinned)
151 		ttm_bo_pin(&bo->tbo);
152 	ttm_bo_unreserve(&bo->tbo);
153 	*bo_ptr = bo;
154 	return 0;
155 }
156 
157 int qxl_bo_kmap(struct qxl_bo *bo, struct dma_buf_map *map)
158 {
159 	int r;
160 
161 	if (bo->kptr) {
162 		bo->map_count++;
163 		goto out;
164 	}
165 	r = ttm_bo_vmap(&bo->tbo, &bo->map);
166 	if (r)
167 		return r;
168 	bo->map_count = 1;
169 
170 	/* TODO: Remove kptr in favor of map everywhere. */
171 	if (bo->map.is_iomem)
172 		bo->kptr = (void *)bo->map.vaddr_iomem;
173 	else
174 		bo->kptr = bo->map.vaddr;
175 
176 out:
177 	*map = bo->map;
178 	return 0;
179 }
180 
181 void *qxl_bo_kmap_atomic_page(struct qxl_device *qdev,
182 			      struct qxl_bo *bo, int page_offset)
183 {
184 	unsigned long offset;
185 	void *rptr;
186 	int ret;
187 	struct io_mapping *map;
188 	struct dma_buf_map bo_map;
189 
190 	if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
191 		map = qdev->vram_mapping;
192 	else if (bo->tbo.mem.mem_type == TTM_PL_PRIV)
193 		map = qdev->surface_mapping;
194 	else
195 		goto fallback;
196 
197 	offset = bo->tbo.mem.start << PAGE_SHIFT;
198 	return io_mapping_map_atomic_wc(map, offset + page_offset);
199 fallback:
200 	if (bo->kptr) {
201 		rptr = bo->kptr + (page_offset * PAGE_SIZE);
202 		return rptr;
203 	}
204 
205 	ret = qxl_bo_kmap(bo, &bo_map);
206 	if (ret)
207 		return NULL;
208 	rptr = bo_map.vaddr; /* TODO: Use mapping abstraction properly */
209 
210 	rptr += page_offset * PAGE_SIZE;
211 	return rptr;
212 }
213 
214 void qxl_bo_kunmap(struct qxl_bo *bo)
215 {
216 	if (bo->kptr == NULL)
217 		return;
218 	bo->map_count--;
219 	if (bo->map_count > 0)
220 		return;
221 	bo->kptr = NULL;
222 	ttm_bo_vunmap(&bo->tbo, &bo->map);
223 }
224 
225 void qxl_bo_kunmap_atomic_page(struct qxl_device *qdev,
226 			       struct qxl_bo *bo, void *pmap)
227 {
228 	if ((bo->tbo.mem.mem_type != TTM_PL_VRAM) &&
229 	    (bo->tbo.mem.mem_type != TTM_PL_PRIV))
230 		goto fallback;
231 
232 	io_mapping_unmap_atomic(pmap);
233 	return;
234  fallback:
235 	qxl_bo_kunmap(bo);
236 }
237 
238 void qxl_bo_unref(struct qxl_bo **bo)
239 {
240 	if ((*bo) == NULL)
241 		return;
242 
243 	drm_gem_object_put(&(*bo)->tbo.base);
244 	*bo = NULL;
245 }
246 
247 struct qxl_bo *qxl_bo_ref(struct qxl_bo *bo)
248 {
249 	drm_gem_object_get(&bo->tbo.base);
250 	return bo;
251 }
252 
253 static int __qxl_bo_pin(struct qxl_bo *bo)
254 {
255 	struct ttm_operation_ctx ctx = { false, false };
256 	struct drm_device *ddev = bo->tbo.base.dev;
257 	int r;
258 
259 	if (bo->tbo.pin_count) {
260 		ttm_bo_pin(&bo->tbo);
261 		return 0;
262 	}
263 	qxl_ttm_placement_from_domain(bo, bo->type);
264 	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
265 	if (likely(r == 0))
266 		ttm_bo_pin(&bo->tbo);
267 	if (unlikely(r != 0))
268 		dev_err(ddev->dev, "%p pin failed\n", bo);
269 	return r;
270 }
271 
272 static void __qxl_bo_unpin(struct qxl_bo *bo)
273 {
274 	ttm_bo_unpin(&bo->tbo);
275 }
276 
277 /*
278  * Reserve the BO before pinning the object.  If the BO was reserved
279  * beforehand, use the internal version directly __qxl_bo_pin.
280  *
281  */
282 int qxl_bo_pin(struct qxl_bo *bo)
283 {
284 	int r;
285 
286 	r = qxl_bo_reserve(bo);
287 	if (r)
288 		return r;
289 
290 	r = __qxl_bo_pin(bo);
291 	qxl_bo_unreserve(bo);
292 	return r;
293 }
294 
295 /*
296  * Reserve the BO before pinning the object.  If the BO was reserved
297  * beforehand, use the internal version directly __qxl_bo_unpin.
298  *
299  */
300 int qxl_bo_unpin(struct qxl_bo *bo)
301 {
302 	int r;
303 
304 	r = qxl_bo_reserve(bo);
305 	if (r)
306 		return r;
307 
308 	__qxl_bo_unpin(bo);
309 	qxl_bo_unreserve(bo);
310 	return 0;
311 }
312 
313 void qxl_bo_force_delete(struct qxl_device *qdev)
314 {
315 	struct qxl_bo *bo, *n;
316 
317 	if (list_empty(&qdev->gem.objects))
318 		return;
319 	dev_err(qdev->ddev.dev, "Userspace still has active objects !\n");
320 	list_for_each_entry_safe(bo, n, &qdev->gem.objects, list) {
321 		dev_err(qdev->ddev.dev, "%p %p %lu %lu force free\n",
322 			&bo->tbo.base, bo, (unsigned long)bo->tbo.base.size,
323 			*((unsigned long *)&bo->tbo.base.refcount));
324 		mutex_lock(&qdev->gem.mutex);
325 		list_del_init(&bo->list);
326 		mutex_unlock(&qdev->gem.mutex);
327 		/* this should unref the ttm bo */
328 		drm_gem_object_put(&bo->tbo.base);
329 	}
330 }
331 
332 int qxl_bo_init(struct qxl_device *qdev)
333 {
334 	return qxl_ttm_init(qdev);
335 }
336 
337 void qxl_bo_fini(struct qxl_device *qdev)
338 {
339 	qxl_ttm_fini(qdev);
340 }
341 
342 int qxl_bo_check_id(struct qxl_device *qdev, struct qxl_bo *bo)
343 {
344 	int ret;
345 
346 	if (bo->type == QXL_GEM_DOMAIN_SURFACE && bo->surface_id == 0) {
347 		/* allocate a surface id for this surface now */
348 		ret = qxl_surface_id_alloc(qdev, bo);
349 		if (ret)
350 			return ret;
351 
352 		ret = qxl_hw_surface_alloc(qdev, bo);
353 		if (ret)
354 			return ret;
355 	}
356 	return 0;
357 }
358 
359 int qxl_surf_evict(struct qxl_device *qdev)
360 {
361 	struct ttm_resource_manager *man;
362 
363 	man = ttm_manager_type(&qdev->mman.bdev, TTM_PL_PRIV);
364 	return ttm_resource_manager_evict_all(&qdev->mman.bdev, man);
365 }
366 
367 int qxl_vram_evict(struct qxl_device *qdev)
368 {
369 	struct ttm_resource_manager *man;
370 
371 	man = ttm_manager_type(&qdev->mman.bdev, TTM_PL_VRAM);
372 	return ttm_resource_manager_evict_all(&qdev->mman.bdev, man);
373 }
374