vmwgfx_cmdbuf_res.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /**************************************************************************
  2. *
  3. * Copyright © 2014-2015 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. #define VMW_CMDBUF_RES_MAN_HT_ORDER 12
  30. /**
  31. * struct vmw_cmdbuf_res - Command buffer managed resource entry.
  32. *
  33. * @res: Refcounted pointer to a struct vmw_resource.
  34. * @hash: Hash entry for the manager hash table.
  35. * @head: List head used either by the staging list or the manager list
  36. * of commited resources.
  37. * @state: Staging state of this resource entry.
  38. * @man: Pointer to a resource manager for this entry.
  39. */
  40. struct vmw_cmdbuf_res {
  41. struct vmw_resource *res;
  42. struct drm_hash_item hash;
  43. struct list_head head;
  44. enum vmw_cmdbuf_res_state state;
  45. struct vmw_cmdbuf_res_manager *man;
  46. };
  47. /**
  48. * struct vmw_cmdbuf_res_manager - Command buffer resource manager.
  49. *
  50. * @resources: Hash table containing staged and commited command buffer
  51. * resources
  52. * @list: List of commited command buffer resources.
  53. * @dev_priv: Pointer to a device private structure.
  54. *
  55. * @resources and @list are protected by the cmdbuf mutex for now.
  56. */
  57. struct vmw_cmdbuf_res_manager {
  58. struct drm_open_hash resources;
  59. struct list_head list;
  60. struct vmw_private *dev_priv;
  61. };
  62. /**
  63. * vmw_cmdbuf_res_lookup - Look up a command buffer resource
  64. *
  65. * @man: Pointer to the command buffer resource manager
  66. * @resource_type: The resource type, that combined with the user key
  67. * identifies the resource.
  68. * @user_key: The user key.
  69. *
  70. * Returns a valid refcounted struct vmw_resource pointer on success,
  71. * an error pointer on failure.
  72. */
  73. struct vmw_resource *
  74. vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
  75. enum vmw_cmdbuf_res_type res_type,
  76. u32 user_key)
  77. {
  78. struct drm_hash_item *hash;
  79. int ret;
  80. unsigned long key = user_key | (res_type << 24);
  81. ret = drm_ht_find_item(&man->resources, key, &hash);
  82. if (unlikely(ret != 0))
  83. return ERR_PTR(ret);
  84. return vmw_resource_reference
  85. (drm_hash_entry(hash, struct vmw_cmdbuf_res, hash)->res);
  86. }
  87. /**
  88. * vmw_cmdbuf_res_free - Free a command buffer resource.
  89. *
  90. * @man: Pointer to the command buffer resource manager
  91. * @entry: Pointer to a struct vmw_cmdbuf_res.
  92. *
  93. * Frees a struct vmw_cmdbuf_res entry and drops its reference to the
  94. * struct vmw_resource.
  95. */
  96. static void vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager *man,
  97. struct vmw_cmdbuf_res *entry)
  98. {
  99. list_del(&entry->head);
  100. WARN_ON(drm_ht_remove_item(&man->resources, &entry->hash));
  101. vmw_resource_unreference(&entry->res);
  102. kfree(entry);
  103. }
  104. /**
  105. * vmw_cmdbuf_res_commit - Commit a list of command buffer resource actions
  106. *
  107. * @list: Caller's list of command buffer resource actions.
  108. *
  109. * This function commits a list of command buffer resource
  110. * additions or removals.
  111. * It is typically called when the execbuf ioctl call triggering these
  112. * actions has commited the fifo contents to the device.
  113. */
  114. void vmw_cmdbuf_res_commit(struct list_head *list)
  115. {
  116. struct vmw_cmdbuf_res *entry, *next;
  117. list_for_each_entry_safe(entry, next, list, head) {
  118. list_del(&entry->head);
  119. if (entry->res->func->commit_notify)
  120. entry->res->func->commit_notify(entry->res,
  121. entry->state);
  122. switch (entry->state) {
  123. case VMW_CMDBUF_RES_ADD:
  124. entry->state = VMW_CMDBUF_RES_COMMITTED;
  125. list_add_tail(&entry->head, &entry->man->list);
  126. break;
  127. case VMW_CMDBUF_RES_DEL:
  128. vmw_resource_unreference(&entry->res);
  129. kfree(entry);
  130. break;
  131. default:
  132. BUG();
  133. break;
  134. }
  135. }
  136. }
  137. /**
  138. * vmw_cmdbuf_res_revert - Revert a list of command buffer resource actions
  139. *
  140. * @man: Pointer to the command buffer resource manager
  141. * @list: Caller's list of command buffer resource action
  142. *
  143. * This function reverts a list of command buffer resource
  144. * additions or removals.
  145. * It is typically called when the execbuf ioctl call triggering these
  146. * actions failed for some reason, and the command stream was never
  147. * submitted.
  148. */
  149. void vmw_cmdbuf_res_revert(struct list_head *list)
  150. {
  151. struct vmw_cmdbuf_res *entry, *next;
  152. int ret;
  153. list_for_each_entry_safe(entry, next, list, head) {
  154. switch (entry->state) {
  155. case VMW_CMDBUF_RES_ADD:
  156. vmw_cmdbuf_res_free(entry->man, entry);
  157. break;
  158. case VMW_CMDBUF_RES_DEL:
  159. ret = drm_ht_insert_item(&entry->man->resources,
  160. &entry->hash);
  161. list_del(&entry->head);
  162. list_add_tail(&entry->head, &entry->man->list);
  163. entry->state = VMW_CMDBUF_RES_COMMITTED;
  164. break;
  165. default:
  166. BUG();
  167. break;
  168. }
  169. }
  170. }
  171. /**
  172. * vmw_cmdbuf_res_add - Stage a command buffer managed resource for addition.
  173. *
  174. * @man: Pointer to the command buffer resource manager.
  175. * @res_type: The resource type.
  176. * @user_key: The user-space id of the resource.
  177. * @res: Valid (refcount != 0) pointer to a struct vmw_resource.
  178. * @list: The staging list.
  179. *
  180. * This function allocates a struct vmw_cmdbuf_res entry and adds the
  181. * resource to the hash table of the manager identified by @man. The
  182. * entry is then put on the staging list identified by @list.
  183. */
  184. int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
  185. enum vmw_cmdbuf_res_type res_type,
  186. u32 user_key,
  187. struct vmw_resource *res,
  188. struct list_head *list)
  189. {
  190. struct vmw_cmdbuf_res *cres;
  191. int ret;
  192. cres = kzalloc(sizeof(*cres), GFP_KERNEL);
  193. if (unlikely(!cres))
  194. return -ENOMEM;
  195. cres->hash.key = user_key | (res_type << 24);
  196. ret = drm_ht_insert_item(&man->resources, &cres->hash);
  197. if (unlikely(ret != 0)) {
  198. kfree(cres);
  199. goto out_invalid_key;
  200. }
  201. cres->state = VMW_CMDBUF_RES_ADD;
  202. cres->res = vmw_resource_reference(res);
  203. cres->man = man;
  204. list_add_tail(&cres->head, list);
  205. out_invalid_key:
  206. return ret;
  207. }
  208. /**
  209. * vmw_cmdbuf_res_remove - Stage a command buffer managed resource for removal.
  210. *
  211. * @man: Pointer to the command buffer resource manager.
  212. * @res_type: The resource type.
  213. * @user_key: The user-space id of the resource.
  214. * @list: The staging list.
  215. * @res_p: If the resource is in an already committed state, points to the
  216. * struct vmw_resource on successful return. The pointer will be
  217. * non ref-counted.
  218. *
  219. * This function looks up the struct vmw_cmdbuf_res entry from the manager
  220. * hash table and, if it exists, removes it. Depending on its current staging
  221. * state it then either removes the entry from the staging list or adds it
  222. * to it with a staging state of removal.
  223. */
  224. int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
  225. enum vmw_cmdbuf_res_type res_type,
  226. u32 user_key,
  227. struct list_head *list,
  228. struct vmw_resource **res_p)
  229. {
  230. struct vmw_cmdbuf_res *entry;
  231. struct drm_hash_item *hash;
  232. int ret;
  233. ret = drm_ht_find_item(&man->resources, user_key | (res_type << 24),
  234. &hash);
  235. if (likely(ret != 0))
  236. return -EINVAL;
  237. entry = drm_hash_entry(hash, struct vmw_cmdbuf_res, hash);
  238. switch (entry->state) {
  239. case VMW_CMDBUF_RES_ADD:
  240. vmw_cmdbuf_res_free(man, entry);
  241. *res_p = NULL;
  242. break;
  243. case VMW_CMDBUF_RES_COMMITTED:
  244. (void) drm_ht_remove_item(&man->resources, &entry->hash);
  245. list_del(&entry->head);
  246. entry->state = VMW_CMDBUF_RES_DEL;
  247. list_add_tail(&entry->head, list);
  248. *res_p = entry->res;
  249. break;
  250. default:
  251. BUG();
  252. break;
  253. }
  254. return 0;
  255. }
  256. /**
  257. * vmw_cmdbuf_res_man_create - Allocate a command buffer managed resource
  258. * manager.
  259. *
  260. * @dev_priv: Pointer to a struct vmw_private
  261. *
  262. * Allocates and initializes a command buffer managed resource manager. Returns
  263. * an error pointer on failure.
  264. */
  265. struct vmw_cmdbuf_res_manager *
  266. vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
  267. {
  268. struct vmw_cmdbuf_res_manager *man;
  269. int ret;
  270. man = kzalloc(sizeof(*man), GFP_KERNEL);
  271. if (!man)
  272. return ERR_PTR(-ENOMEM);
  273. man->dev_priv = dev_priv;
  274. INIT_LIST_HEAD(&man->list);
  275. ret = drm_ht_create(&man->resources, VMW_CMDBUF_RES_MAN_HT_ORDER);
  276. if (ret == 0)
  277. return man;
  278. kfree(man);
  279. return ERR_PTR(ret);
  280. }
  281. /**
  282. * vmw_cmdbuf_res_man_destroy - Destroy a command buffer managed resource
  283. * manager.
  284. *
  285. * @man: Pointer to the manager to destroy.
  286. *
  287. * This function destroys a command buffer managed resource manager and
  288. * unreferences / frees all command buffer managed resources and -entries
  289. * associated with it.
  290. */
  291. void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man)
  292. {
  293. struct vmw_cmdbuf_res *entry, *next;
  294. list_for_each_entry_safe(entry, next, &man->list, head)
  295. vmw_cmdbuf_res_free(man, entry);
  296. drm_ht_remove(&man->resources);
  297. kfree(man);
  298. }
  299. /**
  300. *
  301. * vmw_cmdbuf_res_man_size - Return the size of a command buffer managed
  302. * resource manager
  303. *
  304. * Returns the approximate allocation size of a command buffer managed
  305. * resource manager.
  306. */
  307. size_t vmw_cmdbuf_res_man_size(void)
  308. {
  309. static size_t res_man_size;
  310. if (unlikely(res_man_size == 0))
  311. res_man_size =
  312. ttm_round_pot(sizeof(struct vmw_cmdbuf_res_manager)) +
  313. ttm_round_pot(sizeof(struct hlist_head) <<
  314. VMW_CMDBUF_RES_MAN_HT_ORDER);
  315. return res_man_size;
  316. }