vmwgfx_cmdbuf_res.c 9.6 KB

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