Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0 OR MIT
0002 /**************************************************************************
0003  *
0004  * Copyright 2016 VMware, Inc., Palo Alto, CA., USA
0005  *
0006  * Permission is hereby granted, free of charge, to any person obtaining a
0007  * copy of this software and associated documentation files (the
0008  * "Software"), to deal in the Software without restriction, including
0009  * without limitation the rights to use, copy, modify, merge, publish,
0010  * distribute, sub license, and/or sell copies of the Software, and to
0011  * permit persons to whom the Software is furnished to do so, subject to
0012  * the following conditions:
0013  *
0014  * The above copyright notice and this permission notice (including the
0015  * next paragraph) shall be included in all copies or substantial portions
0016  * of the Software.
0017  *
0018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0020  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
0021  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
0022  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
0023  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
0024  * USE OR OTHER DEALINGS IN THE SOFTWARE.
0025  *
0026  **************************************************************************/
0027 
0028 #include "vmwgfx_drv.h"
0029 #include "vmwgfx_resource_priv.h"
0030 
0031 /**
0032  * struct vmw_user_simple_resource - User-space simple resource struct
0033  *
0034  * @base: The TTM base object implementing user-space visibility.
0035  * @simple: The embedded struct vmw_simple_resource.
0036  */
0037 struct vmw_user_simple_resource {
0038     struct ttm_base_object base;
0039     struct vmw_simple_resource simple;
0040 /*
0041  * Nothing to be placed after @simple, since size of @simple is
0042  * unknown.
0043  */
0044 };
0045 
0046 
0047 /**
0048  * vmw_simple_resource_init - Initialize a simple resource object.
0049  *
0050  * @dev_priv: Pointer to a struct device private.
0051  * @simple: The struct vmw_simple_resource to initialize.
0052  * @data: Data passed to the information initialization function.
0053  * @res_free: Function pointer to destroy the simple resource.
0054  *
0055  * Returns:
0056  *   0 if succeeded.
0057  *   Negative error value if error, in which case the resource will have been
0058  * freed.
0059  */
0060 static int vmw_simple_resource_init(struct vmw_private *dev_priv,
0061                     struct vmw_simple_resource *simple,
0062                     void *data,
0063                     void (*res_free)(struct vmw_resource *res))
0064 {
0065     struct vmw_resource *res = &simple->res;
0066     int ret;
0067 
0068     ret = vmw_resource_init(dev_priv, res, false, res_free,
0069                 &simple->func->res_func);
0070 
0071     if (ret) {
0072         res_free(res);
0073         return ret;
0074     }
0075 
0076     ret = simple->func->init(res, data);
0077     if (ret) {
0078         vmw_resource_unreference(&res);
0079         return ret;
0080     }
0081 
0082     simple->res.hw_destroy = simple->func->hw_destroy;
0083 
0084     return 0;
0085 }
0086 
0087 /**
0088  * vmw_simple_resource_free - Free a simple resource object.
0089  *
0090  * @res: The struct vmw_resource member of the simple resource object.
0091  *
0092  * Frees memory for the object.
0093  */
0094 static void vmw_simple_resource_free(struct vmw_resource *res)
0095 {
0096     struct vmw_user_simple_resource *usimple =
0097         container_of(res, struct vmw_user_simple_resource,
0098                  simple.res);
0099 
0100     ttm_base_object_kfree(usimple, base);
0101 }
0102 
0103 /**
0104  * vmw_simple_resource_base_release - TTM object release callback
0105  *
0106  * @p_base: The struct ttm_base_object member of the simple resource object.
0107  *
0108  * Called when the last reference to the embedded struct ttm_base_object is
0109  * gone. Typically results in an object free, unless there are other
0110  * references to the embedded struct vmw_resource.
0111  */
0112 static void vmw_simple_resource_base_release(struct ttm_base_object **p_base)
0113 {
0114     struct ttm_base_object *base = *p_base;
0115     struct vmw_user_simple_resource *usimple =
0116         container_of(base, struct vmw_user_simple_resource, base);
0117     struct vmw_resource *res = &usimple->simple.res;
0118 
0119     *p_base = NULL;
0120     vmw_resource_unreference(&res);
0121 }
0122 
0123 /**
0124  * vmw_simple_resource_create_ioctl - Helper to set up an ioctl function to
0125  * create a struct vmw_simple_resource.
0126  *
0127  * @dev: Pointer to a struct drm device.
0128  * @data: Ioctl argument.
0129  * @file_priv: Pointer to a struct drm_file identifying the caller.
0130  * @func: Pointer to a struct vmw_simple_resource_func identifying the
0131  * simple resource type.
0132  *
0133  * Returns:
0134  *   0 if success,
0135  *   Negative error value on error.
0136  */
0137 int
0138 vmw_simple_resource_create_ioctl(struct drm_device *dev, void *data,
0139                  struct drm_file *file_priv,
0140                  const struct vmw_simple_resource_func *func)
0141 {
0142     struct vmw_private *dev_priv = vmw_priv(dev);
0143     struct vmw_user_simple_resource *usimple;
0144     struct vmw_resource *res;
0145     struct vmw_resource *tmp;
0146     struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
0147     size_t alloc_size;
0148     int ret;
0149 
0150     alloc_size = offsetof(struct vmw_user_simple_resource, simple) +
0151       func->size;
0152 
0153     usimple = kzalloc(alloc_size, GFP_KERNEL);
0154     if (!usimple) {
0155         ret = -ENOMEM;
0156         goto out_ret;
0157     }
0158 
0159     usimple->simple.func = func;
0160     res = &usimple->simple.res;
0161     usimple->base.shareable = false;
0162     usimple->base.tfile = NULL;
0163 
0164     /*
0165      * From here on, the destructor takes over resource freeing.
0166      */
0167     ret = vmw_simple_resource_init(dev_priv, &usimple->simple,
0168                        data, vmw_simple_resource_free);
0169     if (ret)
0170         goto out_ret;
0171 
0172     tmp = vmw_resource_reference(res);
0173     ret = ttm_base_object_init(tfile, &usimple->base, false,
0174                    func->ttm_res_type,
0175                    &vmw_simple_resource_base_release);
0176 
0177     if (ret) {
0178         vmw_resource_unreference(&tmp);
0179         goto out_err;
0180     }
0181 
0182     func->set_arg_handle(data, usimple->base.handle);
0183 out_err:
0184     vmw_resource_unreference(&res);
0185 out_ret:
0186     return ret;
0187 }
0188 
0189 /**
0190  * vmw_simple_resource_lookup - Look up a simple resource from its user-space
0191  * handle.
0192  *
0193  * @tfile: struct ttm_object_file identifying the caller.
0194  * @handle: The user-space handle.
0195  * @func: The struct vmw_simple_resource_func identifying the simple resource
0196  * type.
0197  *
0198  * Returns: Refcounted pointer to the embedded struct vmw_resource if
0199  * successfule. Error pointer otherwise.
0200  */
0201 struct vmw_resource *
0202 vmw_simple_resource_lookup(struct ttm_object_file *tfile,
0203                uint32_t handle,
0204                const struct vmw_simple_resource_func *func)
0205 {
0206     struct vmw_user_simple_resource *usimple;
0207     struct ttm_base_object *base;
0208     struct vmw_resource *res;
0209 
0210     base = ttm_base_object_lookup(tfile, handle);
0211     if (!base) {
0212         VMW_DEBUG_USER("Invalid %s handle 0x%08lx.\n",
0213                    func->res_func.type_name,
0214                    (unsigned long) handle);
0215         return ERR_PTR(-ESRCH);
0216     }
0217 
0218     if (ttm_base_object_type(base) != func->ttm_res_type) {
0219         ttm_base_object_unref(&base);
0220         VMW_DEBUG_USER("Invalid type of %s handle 0x%08lx.\n",
0221                    func->res_func.type_name,
0222                    (unsigned long) handle);
0223         return ERR_PTR(-EINVAL);
0224     }
0225 
0226     usimple = container_of(base, typeof(*usimple), base);
0227     res = vmw_resource_reference(&usimple->simple.res);
0228     ttm_base_object_unref(&base);
0229 
0230     return res;
0231 }