vmwgfx_cmdbuf_res.c 9.9 KB

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