1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2016 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
29 #include "vmwgfx_resource_priv.h"
32 * struct vmw_user_simple_resource - User-space simple resource struct
34 * @base: The TTM base object implementing user-space visibility.
35 * @account_size: How much memory was accounted for this object.
36 * @simple: The embedded struct vmw_simple_resource.
38 struct vmw_user_simple_resource
{
39 struct ttm_base_object base
;
41 struct vmw_simple_resource simple
;
43 * Nothing to be placed after @simple, since size of @simple is
50 * vmw_simple_resource_init - Initialize a simple resource object.
52 * @dev_priv: Pointer to a struct device private.
53 * @simple: The struct vmw_simple_resource to initialize.
54 * @data: Data passed to the information initialization function.
55 * @res_free: Function pointer to destroy the simple resource.
59 * Negative error value if error, in which case the resource will have been
62 static int vmw_simple_resource_init(struct vmw_private
*dev_priv
,
63 struct vmw_simple_resource
*simple
,
65 void (*res_free
)(struct vmw_resource
*res
))
67 struct vmw_resource
*res
= &simple
->res
;
70 ret
= vmw_resource_init(dev_priv
, res
, false, res_free
,
71 &simple
->func
->res_func
);
78 ret
= simple
->func
->init(res
, data
);
80 vmw_resource_unreference(&res
);
84 simple
->res
.hw_destroy
= simple
->func
->hw_destroy
;
90 * vmw_simple_resource_free - Free a simple resource object.
92 * @res: The struct vmw_resource member of the simple resource object.
94 * Frees memory and memory accounting for the object.
96 static void vmw_simple_resource_free(struct vmw_resource
*res
)
98 struct vmw_user_simple_resource
*usimple
=
99 container_of(res
, struct vmw_user_simple_resource
,
101 struct vmw_private
*dev_priv
= res
->dev_priv
;
102 size_t size
= usimple
->account_size
;
104 ttm_base_object_kfree(usimple
, base
);
105 ttm_mem_global_free(vmw_mem_glob(dev_priv
), size
);
109 * vmw_simple_resource_base_release - TTM object release callback
111 * @p_base: The struct ttm_base_object member of the simple resource object.
113 * Called when the last reference to the embedded struct ttm_base_object is
114 * gone. Typically results in an object free, unless there are other
115 * references to the embedded struct vmw_resource.
117 static void vmw_simple_resource_base_release(struct ttm_base_object
**p_base
)
119 struct ttm_base_object
*base
= *p_base
;
120 struct vmw_user_simple_resource
*usimple
=
121 container_of(base
, struct vmw_user_simple_resource
, base
);
122 struct vmw_resource
*res
= &usimple
->simple
.res
;
125 vmw_resource_unreference(&res
);
129 * vmw_simple_resource_create_ioctl - Helper to set up an ioctl function to
130 * create a struct vmw_simple_resource.
132 * @dev: Pointer to a struct drm device.
133 * @data: Ioctl argument.
134 * @file_priv: Pointer to a struct drm_file identifying the caller.
135 * @func: Pointer to a struct vmw_simple_resource_func identifying the
136 * simple resource type.
140 * Negative error value on error.
143 vmw_simple_resource_create_ioctl(struct drm_device
*dev
, void *data
,
144 struct drm_file
*file_priv
,
145 const struct vmw_simple_resource_func
*func
)
147 struct vmw_private
*dev_priv
= vmw_priv(dev
);
148 struct vmw_user_simple_resource
*usimple
;
149 struct vmw_resource
*res
;
150 struct vmw_resource
*tmp
;
151 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
152 struct ttm_operation_ctx ctx
= {
153 .interruptible
= true,
160 alloc_size
= offsetof(struct vmw_user_simple_resource
, simple
) +
162 account_size
= ttm_round_pot(alloc_size
) + VMW_IDA_ACC_SIZE
+
165 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, true);
169 ret
= ttm_mem_global_alloc(vmw_mem_glob(dev_priv
), account_size
,
171 ttm_read_unlock(&dev_priv
->reservation_sem
);
173 if (ret
!= -ERESTARTSYS
)
174 DRM_ERROR("Out of graphics memory for %s"
175 " creation.\n", func
->res_func
.type_name
);
180 usimple
= kzalloc(alloc_size
, GFP_KERNEL
);
182 ttm_mem_global_free(vmw_mem_glob(dev_priv
),
188 usimple
->simple
.func
= func
;
189 usimple
->account_size
= account_size
;
190 res
= &usimple
->simple
.res
;
191 usimple
->base
.shareable
= false;
192 usimple
->base
.tfile
= NULL
;
195 * From here on, the destructor takes over resource freeing.
197 ret
= vmw_simple_resource_init(dev_priv
, &usimple
->simple
,
198 data
, vmw_simple_resource_free
);
202 tmp
= vmw_resource_reference(res
);
203 ret
= ttm_base_object_init(tfile
, &usimple
->base
, false,
205 &vmw_simple_resource_base_release
, NULL
);
208 vmw_resource_unreference(&tmp
);
212 func
->set_arg_handle(data
, usimple
->base
.handle
);
214 vmw_resource_unreference(&res
);
220 * vmw_simple_resource_lookup - Look up a simple resource from its user-space
223 * @tfile: struct ttm_object_file identifying the caller.
224 * @handle: The user-space handle.
225 * @func: The struct vmw_simple_resource_func identifying the simple resource
228 * Returns: Refcounted pointer to the embedded struct vmw_resource if
229 * successfule. Error pointer otherwise.
231 struct vmw_resource
*
232 vmw_simple_resource_lookup(struct ttm_object_file
*tfile
,
234 const struct vmw_simple_resource_func
*func
)
236 struct vmw_user_simple_resource
*usimple
;
237 struct ttm_base_object
*base
;
238 struct vmw_resource
*res
;
240 base
= ttm_base_object_lookup(tfile
, handle
);
242 DRM_ERROR("Invalid %s handle 0x%08lx.\n",
243 func
->res_func
.type_name
,
244 (unsigned long) handle
);
245 return ERR_PTR(-ESRCH
);
248 if (ttm_base_object_type(base
) != func
->ttm_res_type
) {
249 ttm_base_object_unref(&base
);
250 DRM_ERROR("Invalid type of %s handle 0x%08lx.\n",
251 func
->res_func
.type_name
,
252 (unsigned long) handle
);
253 return ERR_PTR(-EINVAL
);
256 usimple
= container_of(base
, typeof(*usimple
), base
);
257 res
= vmw_resource_reference(&usimple
->simple
.res
);
258 ttm_base_object_unref(&base
);