vmwgfx_simple_resource.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2016 VMware, Inc., Palo Alto, CA., USA
  5. *
  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:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  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.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_drv.h"
  28. #include "vmwgfx_resource_priv.h"
  29. /**
  30. * struct vmw_user_simple_resource - User-space simple resource struct
  31. *
  32. * @base: The TTM base object implementing user-space visibility.
  33. * @simple: The embedded struct vmw_simple_resource.
  34. */
  35. struct vmw_user_simple_resource {
  36. struct ttm_base_object base;
  37. struct vmw_simple_resource simple;
  38. /*
  39. * Nothing to be placed after @simple, since size of @simple is
  40. * unknown.
  41. */
  42. };
  43. /**
  44. * vmw_simple_resource_init - Initialize a simple resource object.
  45. *
  46. * @dev_priv: Pointer to a struct device private.
  47. * @simple: The struct vmw_simple_resource to initialize.
  48. * @data: Data passed to the information initialization function.
  49. * @res_free: Function pointer to destroy the simple resource.
  50. *
  51. * Returns:
  52. * 0 if succeeded.
  53. * Negative error value if error, in which case the resource will have been
  54. * freed.
  55. */
  56. static int vmw_simple_resource_init(struct vmw_private *dev_priv,
  57. struct vmw_simple_resource *simple,
  58. void *data,
  59. void (*res_free)(struct vmw_resource *res))
  60. {
  61. struct vmw_resource *res = &simple->res;
  62. int ret;
  63. ret = vmw_resource_init(dev_priv, res, false, res_free,
  64. &simple->func->res_func);
  65. if (ret) {
  66. res_free(res);
  67. return ret;
  68. }
  69. ret = simple->func->init(res, data);
  70. if (ret) {
  71. vmw_resource_unreference(&res);
  72. return ret;
  73. }
  74. simple->res.hw_destroy = simple->func->hw_destroy;
  75. return 0;
  76. }
  77. /**
  78. * vmw_simple_resource_free - Free a simple resource object.
  79. *
  80. * @res: The struct vmw_resource member of the simple resource object.
  81. *
  82. * Frees memory for the object.
  83. */
  84. static void vmw_simple_resource_free(struct vmw_resource *res)
  85. {
  86. struct vmw_user_simple_resource *usimple =
  87. container_of(res, struct vmw_user_simple_resource,
  88. simple.res);
  89. ttm_base_object_kfree(usimple, base);
  90. }
  91. /**
  92. * vmw_simple_resource_base_release - TTM object release callback
  93. *
  94. * @p_base: The struct ttm_base_object member of the simple resource object.
  95. *
  96. * Called when the last reference to the embedded struct ttm_base_object is
  97. * gone. Typically results in an object free, unless there are other
  98. * references to the embedded struct vmw_resource.
  99. */
  100. static void vmw_simple_resource_base_release(struct ttm_base_object **p_base)
  101. {
  102. struct ttm_base_object *base = *p_base;
  103. struct vmw_user_simple_resource *usimple =
  104. container_of(base, struct vmw_user_simple_resource, base);
  105. struct vmw_resource *res = &usimple->simple.res;
  106. *p_base = NULL;
  107. vmw_resource_unreference(&res);
  108. }
  109. /**
  110. * vmw_simple_resource_create_ioctl - Helper to set up an ioctl function to
  111. * create a struct vmw_simple_resource.
  112. *
  113. * @dev: Pointer to a struct drm device.
  114. * @data: Ioctl argument.
  115. * @file_priv: Pointer to a struct drm_file identifying the caller.
  116. * @func: Pointer to a struct vmw_simple_resource_func identifying the
  117. * simple resource type.
  118. *
  119. * Returns:
  120. * 0 if success,
  121. * Negative error value on error.
  122. */
  123. int
  124. vmw_simple_resource_create_ioctl(struct drm_device *dev, void *data,
  125. struct drm_file *file_priv,
  126. const struct vmw_simple_resource_func *func)
  127. {
  128. struct vmw_private *dev_priv = vmw_priv(dev);
  129. struct vmw_user_simple_resource *usimple;
  130. struct vmw_resource *res;
  131. struct vmw_resource *tmp;
  132. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  133. size_t alloc_size;
  134. int ret;
  135. alloc_size = offsetof(struct vmw_user_simple_resource, simple) +
  136. func->size;
  137. usimple = kzalloc(alloc_size, GFP_KERNEL);
  138. if (!usimple) {
  139. ret = -ENOMEM;
  140. goto out_ret;
  141. }
  142. usimple->simple.func = func;
  143. res = &usimple->simple.res;
  144. usimple->base.shareable = false;
  145. usimple->base.tfile = NULL;
  146. /*
  147. * From here on, the destructor takes over resource freeing.
  148. */
  149. ret = vmw_simple_resource_init(dev_priv, &usimple->simple,
  150. data, vmw_simple_resource_free);
  151. if (ret)
  152. goto out_ret;
  153. tmp = vmw_resource_reference(res);
  154. ret = ttm_base_object_init(tfile, &usimple->base, false,
  155. func->ttm_res_type,
  156. &vmw_simple_resource_base_release);
  157. if (ret) {
  158. vmw_resource_unreference(&tmp);
  159. goto out_err;
  160. }
  161. func->set_arg_handle(data, usimple->base.handle);
  162. out_err:
  163. vmw_resource_unreference(&res);
  164. out_ret:
  165. return ret;
  166. }
  167. /**
  168. * vmw_simple_resource_lookup - Look up a simple resource from its user-space
  169. * handle.
  170. *
  171. * @tfile: struct ttm_object_file identifying the caller.
  172. * @handle: The user-space handle.
  173. * @func: The struct vmw_simple_resource_func identifying the simple resource
  174. * type.
  175. *
  176. * Returns: Refcounted pointer to the embedded struct vmw_resource if
  177. * successful. Error pointer otherwise.
  178. */
  179. struct vmw_resource *
  180. vmw_simple_resource_lookup(struct ttm_object_file *tfile,
  181. uint32_t handle,
  182. const struct vmw_simple_resource_func *func)
  183. {
  184. struct vmw_user_simple_resource *usimple;
  185. struct ttm_base_object *base;
  186. struct vmw_resource *res;
  187. base = ttm_base_object_lookup(tfile, handle);
  188. if (!base) {
  189. VMW_DEBUG_USER("Invalid %s handle 0x%08lx.\n",
  190. func->res_func.type_name,
  191. (unsigned long) handle);
  192. return ERR_PTR(-ESRCH);
  193. }
  194. if (ttm_base_object_type(base) != func->ttm_res_type) {
  195. ttm_base_object_unref(&base);
  196. VMW_DEBUG_USER("Invalid type of %s handle 0x%08lx.\n",
  197. func->res_func.type_name,
  198. (unsigned long) handle);
  199. return ERR_PTR(-EINVAL);
  200. }
  201. usimple = container_of(base, typeof(*usimple), base);
  202. res = vmw_resource_reference(&usimple->simple.res);
  203. ttm_base_object_unref(&base);
  204. return res;
  205. }