xref: /linux/drivers/gpu/drm/qxl/qxl_gem.c (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
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 <drm/drm.h>
27 #include <drm/drm_print.h>
28 
29 #include "qxl_drv.h"
30 #include "qxl_object.h"
31 
32 void qxl_gem_object_free(struct drm_gem_object *gobj)
33 {
34 	struct qxl_bo *qobj = gem_to_qxl_bo(gobj);
35 	struct qxl_device *qdev;
36 	struct ttm_buffer_object *tbo;
37 
38 	qdev = to_qxl(gobj->dev);
39 
40 	qxl_surface_evict(qdev, qobj, false);
41 
42 	tbo = &qobj->tbo;
43 	ttm_bo_fini(tbo);
44 }
45 
46 int qxl_gem_object_create(struct qxl_device *qdev, int size,
47 			  int alignment, int initial_domain,
48 			  bool discardable, bool kernel,
49 			  struct qxl_surface *surf,
50 			  struct drm_gem_object **obj)
51 {
52 	struct qxl_bo *qbo;
53 	int r;
54 
55 	*obj = NULL;
56 	/* At least align on page size */
57 	if (alignment < PAGE_SIZE)
58 		alignment = PAGE_SIZE;
59 	r = qxl_bo_create(qdev, size, kernel, false, initial_domain, 0, surf, &qbo);
60 	if (r) {
61 		if (r != -ERESTARTSYS)
62 			DRM_ERROR(
63 			"Failed to allocate GEM object (%d, %d, %u, %d)\n",
64 				  size, initial_domain, alignment, r);
65 		return r;
66 	}
67 	*obj = &qbo->tbo.base;
68 
69 	mutex_lock(&qdev->gem.mutex);
70 	list_add_tail(&qbo->list, &qdev->gem.objects);
71 	mutex_unlock(&qdev->gem.mutex);
72 
73 	return 0;
74 }
75 
76 /*
77  * If the caller passed a valid gobj pointer, it is responsible to call
78  * drm_gem_object_put() when it no longer needs to acess the object.
79  *
80  * If gobj is NULL, it is handled internally.
81  */
82 int qxl_gem_object_create_with_handle(struct qxl_device *qdev,
83 				      struct drm_file *file_priv,
84 				      u32 domain,
85 				      size_t size,
86 				      struct qxl_surface *surf,
87 				      struct drm_gem_object **gobj,
88 				      uint32_t *handle)
89 {
90 	int r;
91 	struct drm_gem_object *local_gobj;
92 
93 	BUG_ON(!handle);
94 
95 	r = qxl_gem_object_create(qdev, size, 0,
96 				  domain,
97 				  false, false, surf,
98 				  &local_gobj);
99 	if (r)
100 		return -ENOMEM;
101 	r = drm_gem_handle_create(file_priv, local_gobj, handle);
102 	if (r)
103 		return r;
104 
105 	if (gobj)
106 		*gobj = local_gobj;
107 	else
108 		/* drop reference from allocate - handle holds it now */
109 		drm_gem_object_put(local_gobj);
110 
111 	return 0;
112 }
113 
114 int qxl_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
115 {
116 	return 0;
117 }
118 
119 void qxl_gem_object_close(struct drm_gem_object *obj,
120 			  struct drm_file *file_priv)
121 {
122 }
123 
124 void qxl_gem_init(struct qxl_device *qdev)
125 {
126 	INIT_LIST_HEAD(&qdev->gem.objects);
127 }
128 
129 void qxl_gem_fini(struct qxl_device *qdev)
130 {
131 	qxl_bo_force_delete(qdev);
132 }
133