xref: /netbsd/sys/external/bsd/drm2/dist/drm/qxl/qxl_gem.c (revision 677dec6e)
1 /*	$NetBSD: qxl_gem.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2013 Red Hat Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Dave Airlie
25  *          Alon Levy
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: qxl_gem.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $");
30 
31 #include <drm/drm.h>
32 
33 #include "qxl_drv.h"
34 #include "qxl_object.h"
35 
qxl_gem_object_free(struct drm_gem_object * gobj)36 void qxl_gem_object_free(struct drm_gem_object *gobj)
37 {
38 	struct qxl_bo *qobj = gem_to_qxl_bo(gobj);
39 	struct qxl_device *qdev;
40 	struct ttm_buffer_object *tbo;
41 
42 	qdev = (struct qxl_device *)gobj->dev->dev_private;
43 
44 	qxl_surface_evict(qdev, qobj, false);
45 
46 	tbo = &qobj->tbo;
47 	ttm_bo_put(tbo);
48 }
49 
qxl_gem_object_create(struct qxl_device * qdev,int size,int alignment,int initial_domain,bool discardable,bool kernel,struct qxl_surface * surf,struct drm_gem_object ** obj)50 int qxl_gem_object_create(struct qxl_device *qdev, int size,
51 			  int alignment, int initial_domain,
52 			  bool discardable, bool kernel,
53 			  struct qxl_surface *surf,
54 			  struct drm_gem_object **obj)
55 {
56 	struct qxl_bo *qbo;
57 	int r;
58 
59 	*obj = NULL;
60 	/* At least align on page size */
61 	if (alignment < PAGE_SIZE)
62 		alignment = PAGE_SIZE;
63 	r = qxl_bo_create(qdev, size, kernel, false, initial_domain, surf, &qbo);
64 	if (r) {
65 		if (r != -ERESTARTSYS)
66 			DRM_ERROR(
67 			"Failed to allocate GEM object (%d, %d, %u, %d)\n",
68 				  size, initial_domain, alignment, r);
69 		return r;
70 	}
71 	*obj = &qbo->tbo.base;
72 
73 	mutex_lock(&qdev->gem.mutex);
74 	list_add_tail(&qbo->list, &qdev->gem.objects);
75 	mutex_unlock(&qdev->gem.mutex);
76 
77 	return 0;
78 }
79 
qxl_gem_object_create_with_handle(struct qxl_device * qdev,struct drm_file * file_priv,u32 domain,size_t size,struct qxl_surface * surf,struct qxl_bo ** qobj,uint32_t * handle)80 int qxl_gem_object_create_with_handle(struct qxl_device *qdev,
81 				      struct drm_file *file_priv,
82 				      u32 domain,
83 				      size_t size,
84 				      struct qxl_surface *surf,
85 				      struct qxl_bo **qobj,
86 				      uint32_t *handle)
87 {
88 	struct drm_gem_object *gobj;
89 	int r;
90 
91 	BUG_ON(!qobj);
92 	BUG_ON(!handle);
93 
94 	r = qxl_gem_object_create(qdev, size, 0,
95 				  domain,
96 				  false, false, surf,
97 				  &gobj);
98 	if (r)
99 		return -ENOMEM;
100 	r = drm_gem_handle_create(file_priv, gobj, handle);
101 	if (r)
102 		return r;
103 	/* drop reference from allocate - handle holds it now */
104 	*qobj = gem_to_qxl_bo(gobj);
105 	drm_gem_object_put_unlocked(gobj);
106 	return 0;
107 }
108 
qxl_gem_object_open(struct drm_gem_object * obj,struct drm_file * file_priv)109 int qxl_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
110 {
111 	return 0;
112 }
113 
qxl_gem_object_close(struct drm_gem_object * obj,struct drm_file * file_priv)114 void qxl_gem_object_close(struct drm_gem_object *obj,
115 			  struct drm_file *file_priv)
116 {
117 }
118 
qxl_gem_init(struct qxl_device * qdev)119 void qxl_gem_init(struct qxl_device *qdev)
120 {
121 	INIT_LIST_HEAD(&qdev->gem.objects);
122 }
123 
qxl_gem_fini(struct qxl_device * qdev)124 void qxl_gem_fini(struct qxl_device *qdev)
125 {
126 	qxl_bo_force_delete(qdev);
127 }
128