vmwgfx_context.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-2012 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  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. #include "ttm/ttm_placement.h"
  30. struct vmw_user_context {
  31. struct ttm_base_object base;
  32. struct vmw_resource res;
  33. };
  34. static void vmw_user_context_free(struct vmw_resource *res);
  35. static struct vmw_resource *
  36. vmw_user_context_base_to_res(struct ttm_base_object *base);
  37. static uint64_t vmw_user_context_size;
  38. static const struct vmw_user_resource_conv user_context_conv = {
  39. .object_type = VMW_RES_CONTEXT,
  40. .base_obj_to_res = vmw_user_context_base_to_res,
  41. .res_free = vmw_user_context_free
  42. };
  43. const struct vmw_user_resource_conv *user_context_converter =
  44. &user_context_conv;
  45. static const struct vmw_res_func vmw_legacy_context_func = {
  46. .res_type = vmw_res_context,
  47. .needs_backup = false,
  48. .may_evict = false,
  49. .type_name = "legacy contexts",
  50. .backup_placement = NULL,
  51. .create = NULL,
  52. .destroy = NULL,
  53. .bind = NULL,
  54. .unbind = NULL
  55. };
  56. /**
  57. * Context management:
  58. */
  59. static void vmw_hw_context_destroy(struct vmw_resource *res)
  60. {
  61. struct vmw_private *dev_priv = res->dev_priv;
  62. struct {
  63. SVGA3dCmdHeader header;
  64. SVGA3dCmdDestroyContext body;
  65. } *cmd;
  66. vmw_execbuf_release_pinned_bo(dev_priv);
  67. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  68. if (unlikely(cmd == NULL)) {
  69. DRM_ERROR("Failed reserving FIFO space for surface "
  70. "destruction.\n");
  71. return;
  72. }
  73. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_CONTEXT_DESTROY);
  74. cmd->header.size = cpu_to_le32(sizeof(cmd->body));
  75. cmd->body.cid = cpu_to_le32(res->id);
  76. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  77. vmw_3d_resource_dec(dev_priv, false);
  78. }
  79. static int vmw_context_init(struct vmw_private *dev_priv,
  80. struct vmw_resource *res,
  81. void (*res_free) (struct vmw_resource *res))
  82. {
  83. int ret;
  84. struct {
  85. SVGA3dCmdHeader header;
  86. SVGA3dCmdDefineContext body;
  87. } *cmd;
  88. ret = vmw_resource_init(dev_priv, res, false,
  89. res_free, &vmw_legacy_context_func);
  90. if (unlikely(ret != 0)) {
  91. DRM_ERROR("Failed to allocate a resource id.\n");
  92. goto out_early;
  93. }
  94. if (unlikely(res->id >= SVGA3D_MAX_CONTEXT_IDS)) {
  95. DRM_ERROR("Out of hw context ids.\n");
  96. vmw_resource_unreference(&res);
  97. return -ENOMEM;
  98. }
  99. cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
  100. if (unlikely(cmd == NULL)) {
  101. DRM_ERROR("Fifo reserve failed.\n");
  102. vmw_resource_unreference(&res);
  103. return -ENOMEM;
  104. }
  105. cmd->header.id = cpu_to_le32(SVGA_3D_CMD_CONTEXT_DEFINE);
  106. cmd->header.size = cpu_to_le32(sizeof(cmd->body));
  107. cmd->body.cid = cpu_to_le32(res->id);
  108. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  109. (void) vmw_3d_resource_inc(dev_priv, false);
  110. vmw_resource_activate(res, vmw_hw_context_destroy);
  111. return 0;
  112. out_early:
  113. if (res_free == NULL)
  114. kfree(res);
  115. else
  116. res_free(res);
  117. return ret;
  118. }
  119. struct vmw_resource *vmw_context_alloc(struct vmw_private *dev_priv)
  120. {
  121. struct vmw_resource *res = kmalloc(sizeof(*res), GFP_KERNEL);
  122. int ret;
  123. if (unlikely(res == NULL))
  124. return NULL;
  125. ret = vmw_context_init(dev_priv, res, NULL);
  126. return (ret == 0) ? res : NULL;
  127. }
  128. /**
  129. * User-space context management:
  130. */
  131. static struct vmw_resource *
  132. vmw_user_context_base_to_res(struct ttm_base_object *base)
  133. {
  134. return &(container_of(base, struct vmw_user_context, base)->res);
  135. }
  136. static void vmw_user_context_free(struct vmw_resource *res)
  137. {
  138. struct vmw_user_context *ctx =
  139. container_of(res, struct vmw_user_context, res);
  140. struct vmw_private *dev_priv = res->dev_priv;
  141. ttm_base_object_kfree(ctx, base);
  142. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  143. vmw_user_context_size);
  144. }
  145. /**
  146. * This function is called when user space has no more references on the
  147. * base object. It releases the base-object's reference on the resource object.
  148. */
  149. static void vmw_user_context_base_release(struct ttm_base_object **p_base)
  150. {
  151. struct ttm_base_object *base = *p_base;
  152. struct vmw_user_context *ctx =
  153. container_of(base, struct vmw_user_context, base);
  154. struct vmw_resource *res = &ctx->res;
  155. *p_base = NULL;
  156. vmw_resource_unreference(&res);
  157. }
  158. int vmw_context_destroy_ioctl(struct drm_device *dev, void *data,
  159. struct drm_file *file_priv)
  160. {
  161. struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
  162. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  163. return ttm_ref_object_base_unref(tfile, arg->cid, TTM_REF_USAGE);
  164. }
  165. int vmw_context_define_ioctl(struct drm_device *dev, void *data,
  166. struct drm_file *file_priv)
  167. {
  168. struct vmw_private *dev_priv = vmw_priv(dev);
  169. struct vmw_user_context *ctx;
  170. struct vmw_resource *res;
  171. struct vmw_resource *tmp;
  172. struct drm_vmw_context_arg *arg = (struct drm_vmw_context_arg *)data;
  173. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  174. struct vmw_master *vmaster = vmw_master(file_priv->master);
  175. int ret;
  176. /*
  177. * Approximate idr memory usage with 128 bytes. It will be limited
  178. * by maximum number_of contexts anyway.
  179. */
  180. if (unlikely(vmw_user_context_size == 0))
  181. vmw_user_context_size = ttm_round_pot(sizeof(*ctx)) + 128;
  182. ret = ttm_read_lock(&vmaster->lock, true);
  183. if (unlikely(ret != 0))
  184. return ret;
  185. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  186. vmw_user_context_size,
  187. false, true);
  188. if (unlikely(ret != 0)) {
  189. if (ret != -ERESTARTSYS)
  190. DRM_ERROR("Out of graphics memory for context"
  191. " creation.\n");
  192. goto out_unlock;
  193. }
  194. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  195. if (unlikely(ctx == NULL)) {
  196. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  197. vmw_user_context_size);
  198. ret = -ENOMEM;
  199. goto out_unlock;
  200. }
  201. res = &ctx->res;
  202. ctx->base.shareable = false;
  203. ctx->base.tfile = NULL;
  204. /*
  205. * From here on, the destructor takes over resource freeing.
  206. */
  207. ret = vmw_context_init(dev_priv, res, vmw_user_context_free);
  208. if (unlikely(ret != 0))
  209. goto out_unlock;
  210. tmp = vmw_resource_reference(&ctx->res);
  211. ret = ttm_base_object_init(tfile, &ctx->base, false, VMW_RES_CONTEXT,
  212. &vmw_user_context_base_release, NULL);
  213. if (unlikely(ret != 0)) {
  214. vmw_resource_unreference(&tmp);
  215. goto out_err;
  216. }
  217. arg->cid = ctx->base.hash.key;
  218. out_err:
  219. vmw_resource_unreference(&res);
  220. out_unlock:
  221. ttm_read_unlock(&vmaster->lock);
  222. return ret;
  223. }