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