xref: /openbmc/linux/drivers/gpu/drm/vmwgfx/vmwgfx_va.c (revision b8d312aa)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright 2012-2016 VMware, Inc., Palo Alto, CA., USA
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #include "vmwgfx_drv.h"
29 #include "vmwgfx_resource_priv.h"
30 
31 /**
32  * struct vmw_stream - Overlay stream simple resource.
33  * @sres: The simple resource we derive from.
34  * @stream_id: The overlay stream id.
35  */
36 struct vmw_stream {
37 	struct vmw_simple_resource sres;
38 	u32 stream_id;
39 };
40 
41 /**
42  * vmw_stream - Typecast a struct vmw_resource to a struct vmw_stream.
43  * @res: Pointer to the struct vmw_resource.
44  *
45  * Returns: Returns a pointer to the struct vmw_stream.
46  */
47 static struct vmw_stream *
48 vmw_stream(struct vmw_resource *res)
49 {
50 	return container_of(res, struct vmw_stream, sres.res);
51 }
52 
53 /***************************************************************************
54  * Simple resource callbacks for struct vmw_stream
55  **************************************************************************/
56 static void vmw_stream_hw_destroy(struct vmw_resource *res)
57 {
58 	struct vmw_private *dev_priv = res->dev_priv;
59 	struct vmw_stream *stream = vmw_stream(res);
60 	int ret;
61 
62 	ret = vmw_overlay_unref(dev_priv, stream->stream_id);
63 	WARN_ON_ONCE(ret != 0);
64 }
65 
66 static int vmw_stream_init(struct vmw_resource *res, void *data)
67 {
68 	struct vmw_stream *stream = vmw_stream(res);
69 
70 	return vmw_overlay_claim(res->dev_priv, &stream->stream_id);
71 }
72 
73 static void vmw_stream_set_arg_handle(void *data, u32 handle)
74 {
75 	struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
76 
77 	arg->stream_id = handle;
78 }
79 
80 static const struct vmw_simple_resource_func va_stream_func = {
81 	.res_func = {
82 		.res_type = vmw_res_stream,
83 		.needs_backup = false,
84 		.may_evict = false,
85 		.type_name = "overlay stream",
86 		.backup_placement = NULL,
87 		.create = NULL,
88 		.destroy = NULL,
89 		.bind = NULL,
90 		.unbind = NULL
91 	},
92 	.ttm_res_type = VMW_RES_STREAM,
93 	.size = sizeof(struct vmw_stream),
94 	.init = vmw_stream_init,
95 	.hw_destroy = vmw_stream_hw_destroy,
96 	.set_arg_handle = vmw_stream_set_arg_handle,
97 };
98 
99 /***************************************************************************
100  * End simple resource callbacks for struct vmw_stream
101  **************************************************************************/
102 
103 /**
104  * vmw_stream_unref_ioctl - Ioctl to unreference a user-space handle to
105  * a struct vmw_stream.
106  * @dev: Pointer to the drm device.
107  * @data: The ioctl argument
108  * @file_priv: Pointer to a struct drm_file identifying the caller.
109  *
110  * Return:
111  *   0 if successful.
112  *   Negative error value on failure.
113  */
114 int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
115 			   struct drm_file *file_priv)
116 {
117 	struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
118 
119 	return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
120 					 arg->stream_id, TTM_REF_USAGE);
121 }
122 
123 /**
124  * vmw_stream_claim_ioctl - Ioctl to claim a struct vmw_stream overlay.
125  * @dev: Pointer to the drm device.
126  * @data: The ioctl argument
127  * @file_priv: Pointer to a struct drm_file identifying the caller.
128  *
129  * Return:
130  *   0 if successful.
131  *   Negative error value on failure.
132  */
133 int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
134 			   struct drm_file *file_priv)
135 {
136 	return vmw_simple_resource_create_ioctl(dev, data, file_priv,
137 						&va_stream_func);
138 }
139 
140 /**
141  * vmw_user_stream_lookup - Look up a struct vmw_user_stream from a handle.
142  * @dev_priv: Pointer to a struct vmw_private.
143  * @tfile: struct ttm_object_file identifying the caller.
144  * @inout_id: In: The user-space handle. Out: The stream id.
145  * @out: On output contains a refcounted pointer to the embedded
146  * struct vmw_resource.
147  *
148  * Return:
149  *   0 if successful.
150  *   Negative error value on failure.
151  */
152 int vmw_user_stream_lookup(struct vmw_private *dev_priv,
153 			   struct ttm_object_file *tfile,
154 			   uint32_t *inout_id, struct vmw_resource **out)
155 {
156 	struct vmw_stream *stream;
157 	struct vmw_resource *res =
158 		vmw_simple_resource_lookup(tfile, *inout_id, &va_stream_func);
159 
160 	if (IS_ERR(res))
161 		return PTR_ERR(res);
162 
163 	stream = vmw_stream(res);
164 	*inout_id = stream->stream_id;
165 	*out = res;
166 
167 	return 0;
168 }
169