1169689Skan/*	$NetBSD: vmwgfx_simple_resource.c,v 1.2 2021/12/18 23:45:45 riastradh Exp $	*/
2169689Skan
3169689Skan// SPDX-License-Identifier: GPL-2.0 OR MIT
4169689Skan/**************************************************************************
5169689Skan *
6169689Skan * Copyright 2016 VMware, Inc., Palo Alto, CA., USA
7169689Skan *
8169689Skan * Permission is hereby granted, free of charge, to any person obtaining a
9169689Skan * copy of this software and associated documentation files (the
10169689Skan * "Software"), to deal in the Software without restriction, including
11169689Skan * without limitation the rights to use, copy, modify, merge, publish,
12169689Skan * distribute, sub license, and/or sell copies of the Software, and to
13169689Skan * permit persons to whom the Software is furnished to do so, subject to
14169689Skan * the following conditions:
15169689Skan *
16169689Skan * The above copyright notice and this permission notice (including the
17169689Skan * next paragraph) shall be included in all copies or substantial portions
18169689Skan * of the Software.
19169689Skan *
20169689Skan * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21169689Skan * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22169689Skan * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23169689Skan * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24169689Skan * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25169689Skan * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26169689Skan * USE OR OTHER DEALINGS IN THE SOFTWARE.
27169689Skan *
28169689Skan **************************************************************************/
29169689Skan
30169689Skan#include <sys/cdefs.h>
31169689Skan__KERNEL_RCSID(0, "$NetBSD: vmwgfx_simple_resource.c,v 1.2 2021/12/18 23:45:45 riastradh Exp $");
32169689Skan
33169689Skan#include "vmwgfx_drv.h"
34169689Skan#include "vmwgfx_resource_priv.h"
35169689Skan
36169689Skan/**
37169689Skan * struct vmw_user_simple_resource - User-space simple resource struct
38169689Skan *
39169689Skan * @base: The TTM base object implementing user-space visibility.
40169689Skan * @account_size: How much memory was accounted for this object.
41169689Skan * @simple: The embedded struct vmw_simple_resource.
42169689Skan */
43169689Skanstruct vmw_user_simple_resource {
44169689Skan	struct ttm_base_object base;
45169689Skan	size_t account_size;
46169689Skan	struct vmw_simple_resource simple;
47169689Skan/*
48169689Skan * Nothing to be placed after @simple, since size of @simple is
49169689Skan * unknown.
50169689Skan */
51169689Skan};
52169689Skan
53169689Skan
54169689Skan/**
55169689Skan * vmw_simple_resource_init - Initialize a simple resource object.
56169689Skan *
57169689Skan * @dev_priv: Pointer to a struct device private.
58169689Skan * @simple: The struct vmw_simple_resource to initialize.
59169689Skan * @data: Data passed to the information initialization function.
60169689Skan * @res_free: Function pointer to destroy the simple resource.
61169689Skan *
62169689Skan * Returns:
63169689Skan *   0 if succeeded.
64169689Skan *   Negative error value if error, in which case the resource will have been
65169689Skan * freed.
66169689Skan */
67169689Skanstatic int vmw_simple_resource_init(struct vmw_private *dev_priv,
68169689Skan				    struct vmw_simple_resource *simple,
69169689Skan				    void *data,
70169689Skan				    void (*res_free)(struct vmw_resource *res))
71169689Skan{
72169689Skan	struct vmw_resource *res = &simple->res;
73169689Skan	int ret;
74169689Skan
75169689Skan	ret = vmw_resource_init(dev_priv, res, false, res_free,
76169689Skan				&simple->func->res_func);
77169689Skan
78169689Skan	if (ret) {
79169689Skan		res_free(res);
80169689Skan		return ret;
81169689Skan	}
82169689Skan
83169689Skan	ret = simple->func->init(res, data);
84169689Skan	if (ret) {
85169689Skan		vmw_resource_unreference(&res);
86169689Skan		return ret;
87169689Skan	}
88169689Skan
89169689Skan	simple->res.hw_destroy = simple->func->hw_destroy;
90169689Skan
91169689Skan	return 0;
92169689Skan}
93169689Skan
94169689Skan/**
95169689Skan * vmw_simple_resource_free - Free a simple resource object.
96169689Skan *
97169689Skan * @res: The struct vmw_resource member of the simple resource object.
98169689Skan *
99169689Skan * Frees memory and memory accounting for the object.
100169689Skan */
101169689Skanstatic void vmw_simple_resource_free(struct vmw_resource *res)
102169689Skan{
103169689Skan	struct vmw_user_simple_resource *usimple =
104169689Skan		container_of(res, struct vmw_user_simple_resource,
105169689Skan			     simple.res);
106169689Skan	struct vmw_private *dev_priv = res->dev_priv;
107169689Skan	size_t size = usimple->account_size;
108169689Skan
109169689Skan	ttm_base_object_kfree(usimple, base);
110169689Skan	ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
111169689Skan}
112169689Skan
113169689Skan/**
114169689Skan * vmw_simple_resource_base_release - TTM object release callback
115169689Skan *
116169689Skan * @p_base: The struct ttm_base_object member of the simple resource object.
117169689Skan *
118169689Skan * Called when the last reference to the embedded struct ttm_base_object is
119169689Skan * gone. Typically results in an object free, unless there are other
120169689Skan * references to the embedded struct vmw_resource.
121169689Skan */
122169689Skanstatic void vmw_simple_resource_base_release(struct ttm_base_object **p_base)
123169689Skan{
124169689Skan	struct ttm_base_object *base = *p_base;
125169689Skan	struct vmw_user_simple_resource *usimple =
126169689Skan		container_of(base, struct vmw_user_simple_resource, base);
127169689Skan	struct vmw_resource *res = &usimple->simple.res;
128169689Skan
129169689Skan	*p_base = NULL;
130169689Skan	vmw_resource_unreference(&res);
131169689Skan}
132169689Skan
133169689Skan/**
134169689Skan * vmw_simple_resource_create_ioctl - Helper to set up an ioctl function to
135169689Skan * create a struct vmw_simple_resource.
136169689Skan *
137169689Skan * @dev: Pointer to a struct drm device.
138169689Skan * @data: Ioctl argument.
139169689Skan * @file_priv: Pointer to a struct drm_file identifying the caller.
140169689Skan * @func: Pointer to a struct vmw_simple_resource_func identifying the
141169689Skan * simple resource type.
142169689Skan *
143169689Skan * Returns:
144169689Skan *   0 if success,
145169689Skan *   Negative error value on error.
146169689Skan */
147169689Skanint
148169689Skanvmw_simple_resource_create_ioctl(struct drm_device *dev, void *data,
149169689Skan				 struct drm_file *file_priv,
150169689Skan				 const struct vmw_simple_resource_func *func)
151169689Skan{
152169689Skan	struct vmw_private *dev_priv = vmw_priv(dev);
153169689Skan	struct vmw_user_simple_resource *usimple;
154169689Skan	struct vmw_resource *res;
155169689Skan	struct vmw_resource *tmp;
156169689Skan	struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
157169689Skan	struct ttm_operation_ctx ctx = {
158169689Skan		.interruptible = true,
159169689Skan		.no_wait_gpu = false
160169689Skan	};
161169689Skan	size_t alloc_size;
162169689Skan	size_t account_size;
163169689Skan	int ret;
164169689Skan
165169689Skan	alloc_size = offsetof(struct vmw_user_simple_resource, simple) +
166169689Skan	  func->size;
167169689Skan	account_size = ttm_round_pot(alloc_size) + VMW_IDA_ACC_SIZE +
168169689Skan		TTM_OBJ_EXTRA_SIZE;
169169689Skan
170169689Skan	ret = ttm_read_lock(&dev_priv->reservation_sem, true);
171169689Skan	if (ret)
172169689Skan		return ret;
173169689Skan
174169689Skan	ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), account_size,
175169689Skan				   &ctx);
176169689Skan	ttm_read_unlock(&dev_priv->reservation_sem);
177169689Skan	if (ret) {
178169689Skan		if (ret != -ERESTARTSYS)
179169689Skan			DRM_ERROR("Out of graphics memory for %s"
180169689Skan				  " creation.\n", func->res_func.type_name);
181169689Skan
182169689Skan		goto out_ret;
183169689Skan	}
184169689Skan
185169689Skan	usimple = kzalloc(alloc_size, GFP_KERNEL);
186169689Skan	if (!usimple) {
187169689Skan		ttm_mem_global_free(vmw_mem_glob(dev_priv),
188169689Skan				    account_size);
189169689Skan		ret = -ENOMEM;
190169689Skan		goto out_ret;
191169689Skan	}
192169689Skan
193169689Skan	usimple->simple.func = func;
194169689Skan	usimple->account_size = account_size;
195169689Skan	res = &usimple->simple.res;
196169689Skan	usimple->base.shareable = false;
197169689Skan	usimple->base.tfile = NULL;
198169689Skan
199169689Skan	/*
200169689Skan	 * From here on, the destructor takes over resource freeing.
201169689Skan	 */
202169689Skan	ret = vmw_simple_resource_init(dev_priv, &usimple->simple,
203169689Skan				       data, vmw_simple_resource_free);
204169689Skan	if (ret)
205169689Skan		goto out_ret;
206169689Skan
207169689Skan	tmp = vmw_resource_reference(res);
208169689Skan	ret = ttm_base_object_init(tfile, &usimple->base, false,
209169689Skan				   func->ttm_res_type,
210169689Skan				   &vmw_simple_resource_base_release, NULL);
211169689Skan
212169689Skan	if (ret) {
213169689Skan		vmw_resource_unreference(&tmp);
214169689Skan		goto out_err;
215169689Skan	}
216169689Skan
217169689Skan	func->set_arg_handle(data, usimple->base.handle);
218169689Skanout_err:
219169689Skan	vmw_resource_unreference(&res);
220169689Skanout_ret:
221169689Skan	return ret;
222169689Skan}
223169689Skan
224169689Skan/**
225169689Skan * vmw_simple_resource_lookup - Look up a simple resource from its user-space
226169689Skan * handle.
227169689Skan *
228169689Skan * @tfile: struct ttm_object_file identifying the caller.
229169689Skan * @handle: The user-space handle.
230169689Skan * @func: The struct vmw_simple_resource_func identifying the simple resource
231169689Skan * type.
232169689Skan *
233169689Skan * Returns: Refcounted pointer to the embedded struct vmw_resource if
234169689Skan * successfule. Error pointer otherwise.
235169689Skan */
236169689Skanstruct vmw_resource *
237169689Skanvmw_simple_resource_lookup(struct ttm_object_file *tfile,
238169689Skan			   uint32_t handle,
239169689Skan			   const struct vmw_simple_resource_func *func)
240169689Skan{
241169689Skan	struct vmw_user_simple_resource *usimple;
242169689Skan	struct ttm_base_object *base;
243169689Skan	struct vmw_resource *res;
244169689Skan
245169689Skan	base = ttm_base_object_lookup(tfile, handle);
246169689Skan	if (!base) {
247169689Skan		VMW_DEBUG_USER("Invalid %s handle 0x%08lx.\n",
248169689Skan			       func->res_func.type_name,
249169689Skan			       (unsigned long) handle);
250169689Skan		return ERR_PTR(-ESRCH);
251169689Skan	}
252169689Skan
253169689Skan	if (ttm_base_object_type(base) != func->ttm_res_type) {
254169689Skan		ttm_base_object_unref(&base);
255169689Skan		VMW_DEBUG_USER("Invalid type of %s handle 0x%08lx.\n",
256169689Skan			       func->res_func.type_name,
257169689Skan			       (unsigned long) handle);
258169689Skan		return ERR_PTR(-EINVAL);
259169689Skan	}
260169689Skan
261169689Skan	usimple = container_of(base, typeof(*usimple), base);
262169689Skan	res = vmw_resource_reference(&usimple->simple.res);
263169689Skan	ttm_base_object_unref(&base);
264169689Skan
265169689Skan	return res;
266169689Skan}
267169689Skan