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