Linux Audio

Check our new training course

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