vmwgfx_simple_resource.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. * @account_size: How much memory was accounted for this object.
  34. * @simple: The embedded struct vmw_simple_resource.
  35. */
  36. struct vmw_user_simple_resource {
  37. struct ttm_base_object base;
  38. size_t account_size;
  39. struct vmw_simple_resource simple;
  40. /*
  41. * Nothing to be placed after @simple, since size of @simple is
  42. * unknown.
  43. */
  44. };
  45. /**
  46. * vmw_simple_resource_init - Initialize a simple resource object.
  47. *
  48. * @dev_priv: Pointer to a struct device private.
  49. * @simple: The struct vmw_simple_resource to initialize.
  50. * @data: Data passed to the information initialization function.
  51. * @res_free: Function pointer to destroy the simple resource.
  52. *
  53. * Returns:
  54. * 0 if succeeded.
  55. * Negative error value if error, in which case the resource will have been
  56. * freed.
  57. */
  58. static int vmw_simple_resource_init(struct vmw_private *dev_priv,
  59. struct vmw_simple_resource *simple,
  60. void *data,
  61. void (*res_free)(struct vmw_resource *res))
  62. {
  63. struct vmw_resource *res = &simple->res;
  64. int ret;
  65. ret = vmw_resource_init(dev_priv, res, false, res_free,
  66. &simple->func->res_func);
  67. if (ret) {
  68. res_free(res);
  69. return ret;
  70. }
  71. ret = simple->func->init(res, data);
  72. if (ret) {
  73. vmw_resource_unreference(&res);
  74. return ret;
  75. }
  76. simple->res.hw_destroy = simple->func->hw_destroy;
  77. return 0;
  78. }
  79. /**
  80. * vmw_simple_resource_free - Free a simple resource object.
  81. *
  82. * @res: The struct vmw_resource member of the simple resource object.
  83. *
  84. * Frees memory and memory accounting for the object.
  85. */
  86. static void vmw_simple_resource_free(struct vmw_resource *res)
  87. {
  88. struct vmw_user_simple_resource *usimple =
  89. container_of(res, struct vmw_user_simple_resource,
  90. simple.res);
  91. struct vmw_private *dev_priv = res->dev_priv;
  92. size_t size = usimple->account_size;
  93. ttm_base_object_kfree(usimple, base);
  94. ttm_mem_global_free(vmw_mem_glob(dev_priv), size);
  95. }
  96. /**
  97. * vmw_simple_resource_base_release - TTM object release callback
  98. *
  99. * @p_base: The struct ttm_base_object member of the simple resource object.
  100. *
  101. * Called when the last reference to the embedded struct ttm_base_object is
  102. * gone. Typically results in an object free, unless there are other
  103. * references to the embedded struct vmw_resource.
  104. */
  105. static void vmw_simple_resource_base_release(struct ttm_base_object **p_base)
  106. {
  107. struct ttm_base_object *base = *p_base;
  108. struct vmw_user_simple_resource *usimple =
  109. container_of(base, struct vmw_user_simple_resource, base);
  110. struct vmw_resource *res = &usimple->simple.res;
  111. *p_base = NULL;
  112. vmw_resource_unreference(&res);
  113. }
  114. /**
  115. * vmw_simple_resource_create_ioctl - Helper to set up an ioctl function to
  116. * create a struct vmw_simple_resource.
  117. *
  118. * @dev: Pointer to a struct drm device.
  119. * @data: Ioctl argument.
  120. * @file_priv: Pointer to a struct drm_file identifying the caller.
  121. * @func: Pointer to a struct vmw_simple_resource_func identifying the
  122. * simple resource type.
  123. *
  124. * Returns:
  125. * 0 if success,
  126. * Negative error value on error.
  127. */
  128. int
  129. vmw_simple_resource_create_ioctl(struct drm_device *dev, void *data,
  130. struct drm_file *file_priv,
  131. const struct vmw_simple_resource_func *func)
  132. {
  133. struct vmw_private *dev_priv = vmw_priv(dev);
  134. struct vmw_user_simple_resource *usimple;
  135. struct vmw_resource *res;
  136. struct vmw_resource *tmp;
  137. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  138. struct ttm_operation_ctx ctx = {
  139. .interruptible = true,
  140. .no_wait_gpu = false
  141. };
  142. size_t alloc_size;
  143. size_t account_size;
  144. int ret;
  145. alloc_size = offsetof(struct vmw_user_simple_resource, simple) +
  146. func->size;
  147. account_size = ttm_round_pot(alloc_size) + VMW_IDA_ACC_SIZE +
  148. TTM_OBJ_EXTRA_SIZE;
  149. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  150. if (ret)
  151. return ret;
  152. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv), account_size,
  153. &ctx);
  154. ttm_read_unlock(&dev_priv->reservation_sem);
  155. if (ret) {
  156. if (ret != -ERESTARTSYS)
  157. DRM_ERROR("Out of graphics memory for %s"
  158. " creation.\n", func->res_func.type_name);
  159. goto out_ret;
  160. }
  161. usimple = kzalloc(alloc_size, GFP_KERNEL);
  162. if (!usimple) {
  163. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  164. account_size);
  165. ret = -ENOMEM;
  166. goto out_ret;
  167. }
  168. usimple->simple.func = func;
  169. usimple->account_size = account_size;
  170. res = &usimple->simple.res;
  171. usimple->base.shareable = false;
  172. usimple->base.tfile = NULL;
  173. /*
  174. * From here on, the destructor takes over resource freeing.
  175. */
  176. ret = vmw_simple_resource_init(dev_priv, &usimple->simple,
  177. data, vmw_simple_resource_free);
  178. if (ret)
  179. goto out_ret;
  180. tmp = vmw_resource_reference(res);
  181. ret = ttm_base_object_init(tfile, &usimple->base, false,
  182. func->ttm_res_type,
  183. &vmw_simple_resource_base_release, NULL);
  184. if (ret) {
  185. vmw_resource_unreference(&tmp);
  186. goto out_err;
  187. }
  188. func->set_arg_handle(data, usimple->base.handle);
  189. out_err:
  190. vmw_resource_unreference(&res);
  191. out_ret:
  192. return ret;
  193. }
  194. /**
  195. * vmw_simple_resource_lookup - Look up a simple resource from its user-space
  196. * handle.
  197. *
  198. * @tfile: struct ttm_object_file identifying the caller.
  199. * @handle: The user-space handle.
  200. * @func: The struct vmw_simple_resource_func identifying the simple resource
  201. * type.
  202. *
  203. * Returns: Refcounted pointer to the embedded struct vmw_resource if
  204. * successfule. Error pointer otherwise.
  205. */
  206. struct vmw_resource *
  207. vmw_simple_resource_lookup(struct ttm_object_file *tfile,
  208. uint32_t handle,
  209. const struct vmw_simple_resource_func *func)
  210. {
  211. struct vmw_user_simple_resource *usimple;
  212. struct ttm_base_object *base;
  213. struct vmw_resource *res;
  214. base = ttm_base_object_lookup(tfile, handle);
  215. if (!base) {
  216. VMW_DEBUG_USER("Invalid %s handle 0x%08lx.\n",
  217. func->res_func.type_name,
  218. (unsigned long) handle);
  219. return ERR_PTR(-ESRCH);
  220. }
  221. if (ttm_base_object_type(base) != func->ttm_res_type) {
  222. ttm_base_object_unref(&base);
  223. VMW_DEBUG_USER("Invalid type of %s handle 0x%08lx.\n",
  224. func->res_func.type_name,
  225. (unsigned long) handle);
  226. return ERR_PTR(-EINVAL);
  227. }
  228. usimple = container_of(base, typeof(*usimple), base);
  229. res = vmw_resource_reference(&usimple->simple.res);
  230. ttm_base_object_unref(&base);
  231. return res;
  232. }