1dff96888SDirk Hohndel (VMware) // SPDX-License-Identifier: GPL-2.0 OR MIT
2d4d21902SThomas Hellstrom /**************************************************************************
3d4d21902SThomas Hellstrom  *
4dff96888SDirk Hohndel (VMware)  * Copyright 2016 VMware, Inc., Palo Alto, CA., USA
5d4d21902SThomas Hellstrom  *
6d4d21902SThomas Hellstrom  * Permission is hereby granted, free of charge, to any person obtaining a
7d4d21902SThomas Hellstrom  * copy of this software and associated documentation files (the
8d4d21902SThomas Hellstrom  * "Software"), to deal in the Software without restriction, including
9d4d21902SThomas Hellstrom  * without limitation the rights to use, copy, modify, merge, publish,
10d4d21902SThomas Hellstrom  * distribute, sub license, and/or sell copies of the Software, and to
11d4d21902SThomas Hellstrom  * permit persons to whom the Software is furnished to do so, subject to
12d4d21902SThomas Hellstrom  * the following conditions:
13d4d21902SThomas Hellstrom  *
14d4d21902SThomas Hellstrom  * The above copyright notice and this permission notice (including the
15d4d21902SThomas Hellstrom  * next paragraph) shall be included in all copies or substantial portions
16d4d21902SThomas Hellstrom  * of the Software.
17d4d21902SThomas Hellstrom  *
18d4d21902SThomas Hellstrom  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19d4d21902SThomas Hellstrom  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20d4d21902SThomas Hellstrom  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21d4d21902SThomas Hellstrom  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22d4d21902SThomas Hellstrom  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23d4d21902SThomas Hellstrom  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24d4d21902SThomas Hellstrom  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25d4d21902SThomas Hellstrom  *
26d4d21902SThomas Hellstrom  **************************************************************************/
27d4d21902SThomas Hellstrom 
28d4d21902SThomas Hellstrom #include "vmwgfx_drv.h"
29d4d21902SThomas Hellstrom #include "vmwgfx_resource_priv.h"
30d4d21902SThomas Hellstrom 
31d4d21902SThomas Hellstrom /**
32d4d21902SThomas Hellstrom  * struct vmw_user_simple_resource - User-space simple resource struct
33d4d21902SThomas Hellstrom  *
34d4d21902SThomas Hellstrom  * @base: The TTM base object implementing user-space visibility.
35d4d21902SThomas Hellstrom  * @simple: The embedded struct vmw_simple_resource.
36d4d21902SThomas Hellstrom  */
37d4d21902SThomas Hellstrom struct vmw_user_simple_resource {
38d4d21902SThomas Hellstrom 	struct ttm_base_object base;
39d4d21902SThomas Hellstrom 	struct vmw_simple_resource simple;
40d4d21902SThomas Hellstrom /*
41d4d21902SThomas Hellstrom  * Nothing to be placed after @simple, since size of @simple is
42d4d21902SThomas Hellstrom  * unknown.
43d4d21902SThomas Hellstrom  */
44d4d21902SThomas Hellstrom };
45d4d21902SThomas Hellstrom 
46d4d21902SThomas Hellstrom 
47d4d21902SThomas Hellstrom /**
48d4d21902SThomas Hellstrom  * vmw_simple_resource_init - Initialize a simple resource object.
49d4d21902SThomas Hellstrom  *
50d4d21902SThomas Hellstrom  * @dev_priv: Pointer to a struct device private.
51d4d21902SThomas Hellstrom  * @simple: The struct vmw_simple_resource to initialize.
52d4d21902SThomas Hellstrom  * @data: Data passed to the information initialization function.
53d4d21902SThomas Hellstrom  * @res_free: Function pointer to destroy the simple resource.
54d4d21902SThomas Hellstrom  *
55d4d21902SThomas Hellstrom  * Returns:
56d4d21902SThomas Hellstrom  *   0 if succeeded.
57d4d21902SThomas Hellstrom  *   Negative error value if error, in which case the resource will have been
58d4d21902SThomas Hellstrom  * freed.
59d4d21902SThomas Hellstrom  */
vmw_simple_resource_init(struct vmw_private * dev_priv,struct vmw_simple_resource * simple,void * data,void (* res_free)(struct vmw_resource * res))60d4d21902SThomas Hellstrom static int vmw_simple_resource_init(struct vmw_private *dev_priv,
61d4d21902SThomas Hellstrom 				    struct vmw_simple_resource *simple,
62d4d21902SThomas Hellstrom 				    void *data,
63d4d21902SThomas Hellstrom 				    void (*res_free)(struct vmw_resource *res))
64d4d21902SThomas Hellstrom {
65d4d21902SThomas Hellstrom 	struct vmw_resource *res = &simple->res;
66d4d21902SThomas Hellstrom 	int ret;
67d4d21902SThomas Hellstrom 
68d4d21902SThomas Hellstrom 	ret = vmw_resource_init(dev_priv, res, false, res_free,
69d4d21902SThomas Hellstrom 				&simple->func->res_func);
70d4d21902SThomas Hellstrom 
71d4d21902SThomas Hellstrom 	if (ret) {
72d4d21902SThomas Hellstrom 		res_free(res);
73d4d21902SThomas Hellstrom 		return ret;
74d4d21902SThomas Hellstrom 	}
75d4d21902SThomas Hellstrom 
76d4d21902SThomas Hellstrom 	ret = simple->func->init(res, data);
77d4d21902SThomas Hellstrom 	if (ret) {
78d4d21902SThomas Hellstrom 		vmw_resource_unreference(&res);
79d4d21902SThomas Hellstrom 		return ret;
80d4d21902SThomas Hellstrom 	}
81d4d21902SThomas Hellstrom 
8213289241SThomas Hellstrom 	simple->res.hw_destroy = simple->func->hw_destroy;
83d4d21902SThomas Hellstrom 
84d4d21902SThomas Hellstrom 	return 0;
85d4d21902SThomas Hellstrom }
86d4d21902SThomas Hellstrom 
87d4d21902SThomas Hellstrom /**
88d4d21902SThomas Hellstrom  * vmw_simple_resource_free - Free a simple resource object.
89d4d21902SThomas Hellstrom  *
90d4d21902SThomas Hellstrom  * @res: The struct vmw_resource member of the simple resource object.
91d4d21902SThomas Hellstrom  *
928aadeb8aSZack Rusin  * Frees memory for the object.
93d4d21902SThomas Hellstrom  */
vmw_simple_resource_free(struct vmw_resource * res)94d4d21902SThomas Hellstrom static void vmw_simple_resource_free(struct vmw_resource *res)
95d4d21902SThomas Hellstrom {
96d4d21902SThomas Hellstrom 	struct vmw_user_simple_resource *usimple =
97d4d21902SThomas Hellstrom 		container_of(res, struct vmw_user_simple_resource,
98d4d21902SThomas Hellstrom 			     simple.res);
99d4d21902SThomas Hellstrom 
100d4d21902SThomas Hellstrom 	ttm_base_object_kfree(usimple, base);
101d4d21902SThomas Hellstrom }
102d4d21902SThomas Hellstrom 
103d4d21902SThomas Hellstrom /**
104d4d21902SThomas Hellstrom  * vmw_simple_resource_base_release - TTM object release callback
105d4d21902SThomas Hellstrom  *
106d4d21902SThomas Hellstrom  * @p_base: The struct ttm_base_object member of the simple resource object.
107d4d21902SThomas Hellstrom  *
108d4d21902SThomas Hellstrom  * Called when the last reference to the embedded struct ttm_base_object is
109d4d21902SThomas Hellstrom  * gone. Typically results in an object free, unless there are other
110d4d21902SThomas Hellstrom  * references to the embedded struct vmw_resource.
111d4d21902SThomas Hellstrom  */
vmw_simple_resource_base_release(struct ttm_base_object ** p_base)112d4d21902SThomas Hellstrom static void vmw_simple_resource_base_release(struct ttm_base_object **p_base)
113d4d21902SThomas Hellstrom {
114d4d21902SThomas Hellstrom 	struct ttm_base_object *base = *p_base;
115d4d21902SThomas Hellstrom 	struct vmw_user_simple_resource *usimple =
116d4d21902SThomas Hellstrom 		container_of(base, struct vmw_user_simple_resource, base);
117d4d21902SThomas Hellstrom 	struct vmw_resource *res = &usimple->simple.res;
118d4d21902SThomas Hellstrom 
119d4d21902SThomas Hellstrom 	*p_base = NULL;
120d4d21902SThomas Hellstrom 	vmw_resource_unreference(&res);
121d4d21902SThomas Hellstrom }
122d4d21902SThomas Hellstrom 
123d4d21902SThomas Hellstrom /**
124d4d21902SThomas Hellstrom  * vmw_simple_resource_create_ioctl - Helper to set up an ioctl function to
125d4d21902SThomas Hellstrom  * create a struct vmw_simple_resource.
126d4d21902SThomas Hellstrom  *
127d4d21902SThomas Hellstrom  * @dev: Pointer to a struct drm device.
128d4d21902SThomas Hellstrom  * @data: Ioctl argument.
129d4d21902SThomas Hellstrom  * @file_priv: Pointer to a struct drm_file identifying the caller.
130d4d21902SThomas Hellstrom  * @func: Pointer to a struct vmw_simple_resource_func identifying the
131d4d21902SThomas Hellstrom  * simple resource type.
132d4d21902SThomas Hellstrom  *
133d4d21902SThomas Hellstrom  * Returns:
134d4d21902SThomas Hellstrom  *   0 if success,
135d4d21902SThomas Hellstrom  *   Negative error value on error.
136d4d21902SThomas Hellstrom  */
137d4d21902SThomas Hellstrom int
vmw_simple_resource_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv,const struct vmw_simple_resource_func * func)138d4d21902SThomas Hellstrom vmw_simple_resource_create_ioctl(struct drm_device *dev, void *data,
139d4d21902SThomas Hellstrom 				 struct drm_file *file_priv,
140d4d21902SThomas Hellstrom 				 const struct vmw_simple_resource_func *func)
141d4d21902SThomas Hellstrom {
142d4d21902SThomas Hellstrom 	struct vmw_private *dev_priv = vmw_priv(dev);
143d4d21902SThomas Hellstrom 	struct vmw_user_simple_resource *usimple;
144d4d21902SThomas Hellstrom 	struct vmw_resource *res;
145d4d21902SThomas Hellstrom 	struct vmw_resource *tmp;
146d4d21902SThomas Hellstrom 	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
147d4d21902SThomas Hellstrom 	size_t alloc_size;
148d4d21902SThomas Hellstrom 	int ret;
149d4d21902SThomas Hellstrom 
150d4d21902SThomas Hellstrom 	alloc_size = offsetof(struct vmw_user_simple_resource, simple) +
151d4d21902SThomas Hellstrom 	  func->size;
152d4d21902SThomas Hellstrom 
153d4d21902SThomas Hellstrom 	usimple = kzalloc(alloc_size, GFP_KERNEL);
154d4d21902SThomas Hellstrom 	if (!usimple) {
155d4d21902SThomas Hellstrom 		ret = -ENOMEM;
156d4d21902SThomas Hellstrom 		goto out_ret;
157d4d21902SThomas Hellstrom 	}
158d4d21902SThomas Hellstrom 
159d4d21902SThomas Hellstrom 	usimple->simple.func = func;
160d4d21902SThomas Hellstrom 	res = &usimple->simple.res;
161d4d21902SThomas Hellstrom 	usimple->base.shareable = false;
162d4d21902SThomas Hellstrom 	usimple->base.tfile = NULL;
163d4d21902SThomas Hellstrom 
164d4d21902SThomas Hellstrom 	/*
165d4d21902SThomas Hellstrom 	 * From here on, the destructor takes over resource freeing.
166d4d21902SThomas Hellstrom 	 */
167d4d21902SThomas Hellstrom 	ret = vmw_simple_resource_init(dev_priv, &usimple->simple,
168d4d21902SThomas Hellstrom 				       data, vmw_simple_resource_free);
169d4d21902SThomas Hellstrom 	if (ret)
170d4d21902SThomas Hellstrom 		goto out_ret;
171d4d21902SThomas Hellstrom 
172d4d21902SThomas Hellstrom 	tmp = vmw_resource_reference(res);
173d4d21902SThomas Hellstrom 	ret = ttm_base_object_init(tfile, &usimple->base, false,
174d4d21902SThomas Hellstrom 				   func->ttm_res_type,
1758afa13a0SZack Rusin 				   &vmw_simple_resource_base_release);
176d4d21902SThomas Hellstrom 
177d4d21902SThomas Hellstrom 	if (ret) {
178d4d21902SThomas Hellstrom 		vmw_resource_unreference(&tmp);
179d4d21902SThomas Hellstrom 		goto out_err;
180d4d21902SThomas Hellstrom 	}
181d4d21902SThomas Hellstrom 
182c7eae626SThomas Hellstrom 	func->set_arg_handle(data, usimple->base.handle);
183d4d21902SThomas Hellstrom out_err:
184d4d21902SThomas Hellstrom 	vmw_resource_unreference(&res);
185d4d21902SThomas Hellstrom out_ret:
186d4d21902SThomas Hellstrom 	return ret;
187d4d21902SThomas Hellstrom }
188d4d21902SThomas Hellstrom 
189d4d21902SThomas Hellstrom /**
190d4d21902SThomas Hellstrom  * vmw_simple_resource_lookup - Look up a simple resource from its user-space
191d4d21902SThomas Hellstrom  * handle.
192d4d21902SThomas Hellstrom  *
193d4d21902SThomas Hellstrom  * @tfile: struct ttm_object_file identifying the caller.
194d4d21902SThomas Hellstrom  * @handle: The user-space handle.
195d4d21902SThomas Hellstrom  * @func: The struct vmw_simple_resource_func identifying the simple resource
196d4d21902SThomas Hellstrom  * type.
197d4d21902SThomas Hellstrom  *
198d4d21902SThomas Hellstrom  * Returns: Refcounted pointer to the embedded struct vmw_resource if
199*05436815STom Rix  * successful. Error pointer otherwise.
200d4d21902SThomas Hellstrom  */
201d4d21902SThomas Hellstrom struct vmw_resource *
vmw_simple_resource_lookup(struct ttm_object_file * tfile,uint32_t handle,const struct vmw_simple_resource_func * func)202d4d21902SThomas Hellstrom vmw_simple_resource_lookup(struct ttm_object_file *tfile,
203d4d21902SThomas Hellstrom 			   uint32_t handle,
204d4d21902SThomas Hellstrom 			   const struct vmw_simple_resource_func *func)
205d4d21902SThomas Hellstrom {
206d4d21902SThomas Hellstrom 	struct vmw_user_simple_resource *usimple;
207d4d21902SThomas Hellstrom 	struct ttm_base_object *base;
208d4d21902SThomas Hellstrom 	struct vmw_resource *res;
209d4d21902SThomas Hellstrom 
210d4d21902SThomas Hellstrom 	base = ttm_base_object_lookup(tfile, handle);
211d4d21902SThomas Hellstrom 	if (!base) {
2125724f899SDeepak Rawat 		VMW_DEBUG_USER("Invalid %s handle 0x%08lx.\n",
213d4d21902SThomas Hellstrom 			       func->res_func.type_name,
214d4d21902SThomas Hellstrom 			       (unsigned long) handle);
215d4d21902SThomas Hellstrom 		return ERR_PTR(-ESRCH);
216d4d21902SThomas Hellstrom 	}
217d4d21902SThomas Hellstrom 
218d4d21902SThomas Hellstrom 	if (ttm_base_object_type(base) != func->ttm_res_type) {
219d4d21902SThomas Hellstrom 		ttm_base_object_unref(&base);
2205724f899SDeepak Rawat 		VMW_DEBUG_USER("Invalid type of %s handle 0x%08lx.\n",
221d4d21902SThomas Hellstrom 			       func->res_func.type_name,
222d4d21902SThomas Hellstrom 			       (unsigned long) handle);
223d4d21902SThomas Hellstrom 		return ERR_PTR(-EINVAL);
224d4d21902SThomas Hellstrom 	}
225d4d21902SThomas Hellstrom 
226d4d21902SThomas Hellstrom 	usimple = container_of(base, typeof(*usimple), base);
227d4d21902SThomas Hellstrom 	res = vmw_resource_reference(&usimple->simple.res);
228d4d21902SThomas Hellstrom 	ttm_base_object_unref(&base);
229d4d21902SThomas Hellstrom 
230d4d21902SThomas Hellstrom 	return res;
231d4d21902SThomas Hellstrom }
232