vmwgfx_cmdbuf_res.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 == NULL))
  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. goto out_invalid_key;
  199. cres->state = VMW_CMDBUF_RES_ADD;
  200. cres->res = vmw_resource_reference(res);
  201. cres->man = man;
  202. list_add_tail(&cres->head, list);
  203. out_invalid_key:
  204. return ret;
  205. }
  206. /**
  207. * vmw_cmdbuf_res_remove - Stage a command buffer managed resource for removal.
  208. *
  209. * @man: Pointer to the command buffer resource manager.
  210. * @res_type: The resource type.
  211. * @user_key: The user-space id of the resource.
  212. * @list: The staging list.
  213. * @res_p: If the resource is in an already committed state, points to the
  214. * struct vmw_resource on successful return. The pointer will be
  215. * non ref-counted.
  216. *
  217. * This function looks up the struct vmw_cmdbuf_res entry from the manager
  218. * hash table and, if it exists, removes it. Depending on its current staging
  219. * state it then either removes the entry from the staging list or adds it
  220. * to it with a staging state of removal.
  221. */
  222. int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
  223. enum vmw_cmdbuf_res_type res_type,
  224. u32 user_key,
  225. struct list_head *list,
  226. struct vmw_resource **res_p)
  227. {
  228. struct vmw_cmdbuf_res *entry;
  229. struct drm_hash_item *hash;
  230. int ret;
  231. ret = drm_ht_find_item(&man->resources, user_key | (res_type << 24),
  232. &hash);
  233. if (likely(ret != 0))
  234. return -EINVAL;
  235. entry = drm_hash_entry(hash, struct vmw_cmdbuf_res, hash);
  236. switch (entry->state) {
  237. case VMW_CMDBUF_RES_ADD:
  238. vmw_cmdbuf_res_free(man, entry);
  239. *res_p = NULL;
  240. break;
  241. case VMW_CMDBUF_RES_COMMITTED:
  242. (void) drm_ht_remove_item(&man->resources, &entry->hash);
  243. list_del(&entry->head);
  244. entry->state = VMW_CMDBUF_RES_DEL;
  245. list_add_tail(&entry->head, list);
  246. *res_p = entry->res;
  247. break;
  248. default:
  249. BUG();
  250. break;
  251. }
  252. return 0;
  253. }
  254. /**
  255. * vmw_cmdbuf_res_man_create - Allocate a command buffer managed resource
  256. * manager.
  257. *
  258. * @dev_priv: Pointer to a struct vmw_private
  259. *
  260. * Allocates and initializes a command buffer managed resource manager. Returns
  261. * an error pointer on failure.
  262. */
  263. struct vmw_cmdbuf_res_manager *
  264. vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
  265. {
  266. struct vmw_cmdbuf_res_manager *man;
  267. int ret;
  268. man = kzalloc(sizeof(*man), GFP_KERNEL);
  269. if (man == NULL)
  270. return ERR_PTR(-ENOMEM);
  271. man->dev_priv = dev_priv;
  272. INIT_LIST_HEAD(&man->list);
  273. ret = drm_ht_create(&man->resources, VMW_CMDBUF_RES_MAN_HT_ORDER);
  274. if (ret == 0)
  275. return man;
  276. kfree(man);
  277. return ERR_PTR(ret);
  278. }
  279. /**
  280. * vmw_cmdbuf_res_man_destroy - Destroy a command buffer managed resource
  281. * manager.
  282. *
  283. * @man: Pointer to the manager to destroy.
  284. *
  285. * This function destroys a command buffer managed resource manager and
  286. * unreferences / frees all command buffer managed resources and -entries
  287. * associated with it.
  288. */
  289. void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man)
  290. {
  291. struct vmw_cmdbuf_res *entry, *next;
  292. list_for_each_entry_safe(entry, next, &man->list, head)
  293. vmw_cmdbuf_res_free(man, entry);
  294. drm_ht_remove(&man->resources);
  295. kfree(man);
  296. }
  297. /**
  298. *
  299. * vmw_cmdbuf_res_man_size - Return the size of a command buffer managed
  300. * resource manager
  301. *
  302. * Returns the approximate allocation size of a command buffer managed
  303. * resource manager.
  304. */
  305. size_t vmw_cmdbuf_res_man_size(void)
  306. {
  307. static size_t res_man_size;
  308. if (unlikely(res_man_size == 0))
  309. res_man_size =
  310. ttm_round_pot(sizeof(struct vmw_cmdbuf_res_manager)) +
  311. ttm_round_pot(sizeof(struct hlist_head) <<
  312. VMW_CMDBUF_RES_MAN_HT_ORDER);
  313. return res_man_size;
  314. }