vmwgfx_resource.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /**************************************************************************
  3. *
  4. * Copyright 2009-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 <drm/vmwgfx_drm.h>
  29. #include <drm/ttm/ttm_object.h>
  30. #include <drm/ttm/ttm_placement.h>
  31. #include <drm/drmP.h>
  32. #include "vmwgfx_resource_priv.h"
  33. #include "vmwgfx_binding.h"
  34. #define VMW_RES_EVICT_ERR_COUNT 10
  35. struct vmw_user_dma_buffer {
  36. struct ttm_prime_object prime;
  37. struct vmw_dma_buffer dma;
  38. };
  39. struct vmw_bo_user_rep {
  40. uint32_t handle;
  41. uint64_t map_handle;
  42. };
  43. static inline struct vmw_dma_buffer *
  44. vmw_dma_buffer(struct ttm_buffer_object *bo)
  45. {
  46. return container_of(bo, struct vmw_dma_buffer, base);
  47. }
  48. static inline struct vmw_user_dma_buffer *
  49. vmw_user_dma_buffer(struct ttm_buffer_object *bo)
  50. {
  51. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  52. return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
  53. }
  54. struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
  55. {
  56. kref_get(&res->kref);
  57. return res;
  58. }
  59. struct vmw_resource *
  60. vmw_resource_reference_unless_doomed(struct vmw_resource *res)
  61. {
  62. return kref_get_unless_zero(&res->kref) ? res : NULL;
  63. }
  64. /**
  65. * vmw_resource_release_id - release a resource id to the id manager.
  66. *
  67. * @res: Pointer to the resource.
  68. *
  69. * Release the resource id to the resource id manager and set it to -1
  70. */
  71. void vmw_resource_release_id(struct vmw_resource *res)
  72. {
  73. struct vmw_private *dev_priv = res->dev_priv;
  74. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  75. write_lock(&dev_priv->resource_lock);
  76. if (res->id != -1)
  77. idr_remove(idr, res->id);
  78. res->id = -1;
  79. write_unlock(&dev_priv->resource_lock);
  80. }
  81. static void vmw_resource_release(struct kref *kref)
  82. {
  83. struct vmw_resource *res =
  84. container_of(kref, struct vmw_resource, kref);
  85. struct vmw_private *dev_priv = res->dev_priv;
  86. int id;
  87. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  88. write_lock(&dev_priv->resource_lock);
  89. res->avail = false;
  90. list_del_init(&res->lru_head);
  91. write_unlock(&dev_priv->resource_lock);
  92. if (res->backup) {
  93. struct ttm_buffer_object *bo = &res->backup->base;
  94. ttm_bo_reserve(bo, false, false, NULL);
  95. if (!list_empty(&res->mob_head) &&
  96. res->func->unbind != NULL) {
  97. struct ttm_validate_buffer val_buf;
  98. val_buf.bo = bo;
  99. val_buf.shared = false;
  100. res->func->unbind(res, false, &val_buf);
  101. }
  102. res->backup_dirty = false;
  103. list_del_init(&res->mob_head);
  104. ttm_bo_unreserve(bo);
  105. vmw_dmabuf_unreference(&res->backup);
  106. }
  107. if (likely(res->hw_destroy != NULL)) {
  108. mutex_lock(&dev_priv->binding_mutex);
  109. vmw_binding_res_list_kill(&res->binding_head);
  110. mutex_unlock(&dev_priv->binding_mutex);
  111. res->hw_destroy(res);
  112. }
  113. id = res->id;
  114. if (res->res_free != NULL)
  115. res->res_free(res);
  116. else
  117. kfree(res);
  118. write_lock(&dev_priv->resource_lock);
  119. if (id != -1)
  120. idr_remove(idr, id);
  121. write_unlock(&dev_priv->resource_lock);
  122. }
  123. void vmw_resource_unreference(struct vmw_resource **p_res)
  124. {
  125. struct vmw_resource *res = *p_res;
  126. *p_res = NULL;
  127. kref_put(&res->kref, vmw_resource_release);
  128. }
  129. /**
  130. * vmw_resource_alloc_id - release a resource id to the id manager.
  131. *
  132. * @res: Pointer to the resource.
  133. *
  134. * Allocate the lowest free resource from the resource manager, and set
  135. * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
  136. */
  137. int vmw_resource_alloc_id(struct vmw_resource *res)
  138. {
  139. struct vmw_private *dev_priv = res->dev_priv;
  140. int ret;
  141. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  142. BUG_ON(res->id != -1);
  143. idr_preload(GFP_KERNEL);
  144. write_lock(&dev_priv->resource_lock);
  145. ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT);
  146. if (ret >= 0)
  147. res->id = ret;
  148. write_unlock(&dev_priv->resource_lock);
  149. idr_preload_end();
  150. return ret < 0 ? ret : 0;
  151. }
  152. /**
  153. * vmw_resource_init - initialize a struct vmw_resource
  154. *
  155. * @dev_priv: Pointer to a device private struct.
  156. * @res: The struct vmw_resource to initialize.
  157. * @obj_type: Resource object type.
  158. * @delay_id: Boolean whether to defer device id allocation until
  159. * the first validation.
  160. * @res_free: Resource destructor.
  161. * @func: Resource function table.
  162. */
  163. int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
  164. bool delay_id,
  165. void (*res_free) (struct vmw_resource *res),
  166. const struct vmw_res_func *func)
  167. {
  168. kref_init(&res->kref);
  169. res->hw_destroy = NULL;
  170. res->res_free = res_free;
  171. res->avail = false;
  172. res->dev_priv = dev_priv;
  173. res->func = func;
  174. INIT_LIST_HEAD(&res->lru_head);
  175. INIT_LIST_HEAD(&res->mob_head);
  176. INIT_LIST_HEAD(&res->binding_head);
  177. res->id = -1;
  178. res->backup = NULL;
  179. res->backup_offset = 0;
  180. res->backup_dirty = false;
  181. res->res_dirty = false;
  182. if (delay_id)
  183. return 0;
  184. else
  185. return vmw_resource_alloc_id(res);
  186. }
  187. /**
  188. * vmw_resource_activate
  189. *
  190. * @res: Pointer to the newly created resource
  191. * @hw_destroy: Destroy function. NULL if none.
  192. *
  193. * Activate a resource after the hardware has been made aware of it.
  194. * Set tye destroy function to @destroy. Typically this frees the
  195. * resource and destroys the hardware resources associated with it.
  196. * Activate basically means that the function vmw_resource_lookup will
  197. * find it.
  198. */
  199. void vmw_resource_activate(struct vmw_resource *res,
  200. void (*hw_destroy) (struct vmw_resource *))
  201. {
  202. struct vmw_private *dev_priv = res->dev_priv;
  203. write_lock(&dev_priv->resource_lock);
  204. res->avail = true;
  205. res->hw_destroy = hw_destroy;
  206. write_unlock(&dev_priv->resource_lock);
  207. }
  208. /**
  209. * vmw_user_resource_lookup_handle - lookup a struct resource from a
  210. * TTM user-space handle and perform basic type checks
  211. *
  212. * @dev_priv: Pointer to a device private struct
  213. * @tfile: Pointer to a struct ttm_object_file identifying the caller
  214. * @handle: The TTM user-space handle
  215. * @converter: Pointer to an object describing the resource type
  216. * @p_res: On successful return the location pointed to will contain
  217. * a pointer to a refcounted struct vmw_resource.
  218. *
  219. * If the handle can't be found or is associated with an incorrect resource
  220. * type, -EINVAL will be returned.
  221. */
  222. int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
  223. struct ttm_object_file *tfile,
  224. uint32_t handle,
  225. const struct vmw_user_resource_conv
  226. *converter,
  227. struct vmw_resource **p_res)
  228. {
  229. struct ttm_base_object *base;
  230. struct vmw_resource *res;
  231. int ret = -EINVAL;
  232. base = ttm_base_object_lookup(tfile, handle);
  233. if (unlikely(base == NULL))
  234. return -EINVAL;
  235. if (unlikely(ttm_base_object_type(base) != converter->object_type))
  236. goto out_bad_resource;
  237. res = converter->base_obj_to_res(base);
  238. read_lock(&dev_priv->resource_lock);
  239. if (!res->avail || res->res_free != converter->res_free) {
  240. read_unlock(&dev_priv->resource_lock);
  241. goto out_bad_resource;
  242. }
  243. kref_get(&res->kref);
  244. read_unlock(&dev_priv->resource_lock);
  245. *p_res = res;
  246. ret = 0;
  247. out_bad_resource:
  248. ttm_base_object_unref(&base);
  249. return ret;
  250. }
  251. /**
  252. * Helper function that looks either a surface or dmabuf.
  253. *
  254. * The pointer this pointed at by out_surf and out_buf needs to be null.
  255. */
  256. int vmw_user_lookup_handle(struct vmw_private *dev_priv,
  257. struct ttm_object_file *tfile,
  258. uint32_t handle,
  259. struct vmw_surface **out_surf,
  260. struct vmw_dma_buffer **out_buf)
  261. {
  262. struct vmw_resource *res;
  263. int ret;
  264. BUG_ON(*out_surf || *out_buf);
  265. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
  266. user_surface_converter,
  267. &res);
  268. if (!ret) {
  269. *out_surf = vmw_res_to_srf(res);
  270. return 0;
  271. }
  272. *out_surf = NULL;
  273. ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf, NULL);
  274. return ret;
  275. }
  276. /**
  277. * Buffer management.
  278. */
  279. /**
  280. * vmw_dmabuf_acc_size - Calculate the pinned memory usage of buffers
  281. *
  282. * @dev_priv: Pointer to a struct vmw_private identifying the device.
  283. * @size: The requested buffer size.
  284. * @user: Whether this is an ordinary dma buffer or a user dma buffer.
  285. */
  286. static size_t vmw_dmabuf_acc_size(struct vmw_private *dev_priv, size_t size,
  287. bool user)
  288. {
  289. static size_t struct_size, user_struct_size;
  290. size_t num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  291. size_t page_array_size = ttm_round_pot(num_pages * sizeof(void *));
  292. if (unlikely(struct_size == 0)) {
  293. size_t backend_size = ttm_round_pot(vmw_tt_size);
  294. struct_size = backend_size +
  295. ttm_round_pot(sizeof(struct vmw_dma_buffer));
  296. user_struct_size = backend_size +
  297. ttm_round_pot(sizeof(struct vmw_user_dma_buffer));
  298. }
  299. if (dev_priv->map_mode == vmw_dma_alloc_coherent)
  300. page_array_size +=
  301. ttm_round_pot(num_pages * sizeof(dma_addr_t));
  302. return ((user) ? user_struct_size : struct_size) +
  303. page_array_size;
  304. }
  305. void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
  306. {
  307. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  308. vmw_dma_buffer_unmap(vmw_bo);
  309. kfree(vmw_bo);
  310. }
  311. static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  312. {
  313. struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
  314. vmw_dma_buffer_unmap(&vmw_user_bo->dma);
  315. ttm_prime_object_kfree(vmw_user_bo, prime);
  316. }
  317. int vmw_dmabuf_init(struct vmw_private *dev_priv,
  318. struct vmw_dma_buffer *vmw_bo,
  319. size_t size, struct ttm_placement *placement,
  320. bool interruptible,
  321. void (*bo_free) (struct ttm_buffer_object *bo))
  322. {
  323. struct ttm_bo_device *bdev = &dev_priv->bdev;
  324. size_t acc_size;
  325. int ret;
  326. bool user = (bo_free == &vmw_user_dmabuf_destroy);
  327. BUG_ON(!bo_free && (!user && (bo_free != vmw_dmabuf_bo_free)));
  328. acc_size = vmw_dmabuf_acc_size(dev_priv, size, user);
  329. memset(vmw_bo, 0, sizeof(*vmw_bo));
  330. INIT_LIST_HEAD(&vmw_bo->res_list);
  331. ret = ttm_bo_init(bdev, &vmw_bo->base, size,
  332. ttm_bo_type_device, placement,
  333. 0, interruptible, acc_size,
  334. NULL, NULL, bo_free);
  335. return ret;
  336. }
  337. static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
  338. {
  339. struct vmw_user_dma_buffer *vmw_user_bo;
  340. struct ttm_base_object *base = *p_base;
  341. struct ttm_buffer_object *bo;
  342. *p_base = NULL;
  343. if (unlikely(base == NULL))
  344. return;
  345. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
  346. prime.base);
  347. bo = &vmw_user_bo->dma.base;
  348. ttm_bo_unref(&bo);
  349. }
  350. static void vmw_user_dmabuf_ref_obj_release(struct ttm_base_object *base,
  351. enum ttm_ref_type ref_type)
  352. {
  353. struct vmw_user_dma_buffer *user_bo;
  354. user_bo = container_of(base, struct vmw_user_dma_buffer, prime.base);
  355. switch (ref_type) {
  356. case TTM_REF_SYNCCPU_WRITE:
  357. ttm_bo_synccpu_write_release(&user_bo->dma.base);
  358. break;
  359. default:
  360. BUG();
  361. }
  362. }
  363. /**
  364. * vmw_user_dmabuf_alloc - Allocate a user dma buffer
  365. *
  366. * @dev_priv: Pointer to a struct device private.
  367. * @tfile: Pointer to a struct ttm_object_file on which to register the user
  368. * object.
  369. * @size: Size of the dma buffer.
  370. * @shareable: Boolean whether the buffer is shareable with other open files.
  371. * @handle: Pointer to where the handle value should be assigned.
  372. * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
  373. * should be assigned.
  374. */
  375. int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
  376. struct ttm_object_file *tfile,
  377. uint32_t size,
  378. bool shareable,
  379. uint32_t *handle,
  380. struct vmw_dma_buffer **p_dma_buf,
  381. struct ttm_base_object **p_base)
  382. {
  383. struct vmw_user_dma_buffer *user_bo;
  384. struct ttm_buffer_object *tmp;
  385. int ret;
  386. user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
  387. if (unlikely(!user_bo)) {
  388. DRM_ERROR("Failed to allocate a buffer.\n");
  389. return -ENOMEM;
  390. }
  391. ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
  392. (dev_priv->has_mob) ?
  393. &vmw_sys_placement :
  394. &vmw_vram_sys_placement, true,
  395. &vmw_user_dmabuf_destroy);
  396. if (unlikely(ret != 0))
  397. return ret;
  398. tmp = ttm_bo_reference(&user_bo->dma.base);
  399. ret = ttm_prime_object_init(tfile,
  400. size,
  401. &user_bo->prime,
  402. shareable,
  403. ttm_buffer_type,
  404. &vmw_user_dmabuf_release,
  405. &vmw_user_dmabuf_ref_obj_release);
  406. if (unlikely(ret != 0)) {
  407. ttm_bo_unref(&tmp);
  408. goto out_no_base_object;
  409. }
  410. *p_dma_buf = &user_bo->dma;
  411. if (p_base) {
  412. *p_base = &user_bo->prime.base;
  413. kref_get(&(*p_base)->refcount);
  414. }
  415. *handle = user_bo->prime.base.hash.key;
  416. out_no_base_object:
  417. return ret;
  418. }
  419. /**
  420. * vmw_user_dmabuf_verify_access - verify access permissions on this
  421. * buffer object.
  422. *
  423. * @bo: Pointer to the buffer object being accessed
  424. * @tfile: Identifying the caller.
  425. */
  426. int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
  427. struct ttm_object_file *tfile)
  428. {
  429. struct vmw_user_dma_buffer *vmw_user_bo;
  430. if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
  431. return -EPERM;
  432. vmw_user_bo = vmw_user_dma_buffer(bo);
  433. /* Check that the caller has opened the object. */
  434. if (likely(ttm_ref_object_exists(tfile, &vmw_user_bo->prime.base)))
  435. return 0;
  436. DRM_ERROR("Could not grant buffer access.\n");
  437. return -EPERM;
  438. }
  439. /**
  440. * vmw_user_dmabuf_synccpu_grab - Grab a struct vmw_user_dma_buffer for cpu
  441. * access, idling previous GPU operations on the buffer and optionally
  442. * blocking it for further command submissions.
  443. *
  444. * @user_bo: Pointer to the buffer object being grabbed for CPU access
  445. * @tfile: Identifying the caller.
  446. * @flags: Flags indicating how the grab should be performed.
  447. *
  448. * A blocking grab will be automatically released when @tfile is closed.
  449. */
  450. static int vmw_user_dmabuf_synccpu_grab(struct vmw_user_dma_buffer *user_bo,
  451. struct ttm_object_file *tfile,
  452. uint32_t flags)
  453. {
  454. struct ttm_buffer_object *bo = &user_bo->dma.base;
  455. bool existed;
  456. int ret;
  457. if (flags & drm_vmw_synccpu_allow_cs) {
  458. bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
  459. long lret;
  460. lret = reservation_object_wait_timeout_rcu(bo->resv, true, true,
  461. nonblock ? 0 : MAX_SCHEDULE_TIMEOUT);
  462. if (!lret)
  463. return -EBUSY;
  464. else if (lret < 0)
  465. return lret;
  466. return 0;
  467. }
  468. ret = ttm_bo_synccpu_write_grab
  469. (bo, !!(flags & drm_vmw_synccpu_dontblock));
  470. if (unlikely(ret != 0))
  471. return ret;
  472. ret = ttm_ref_object_add(tfile, &user_bo->prime.base,
  473. TTM_REF_SYNCCPU_WRITE, &existed, false);
  474. if (ret != 0 || existed)
  475. ttm_bo_synccpu_write_release(&user_bo->dma.base);
  476. return ret;
  477. }
  478. /**
  479. * vmw_user_dmabuf_synccpu_release - Release a previous grab for CPU access,
  480. * and unblock command submission on the buffer if blocked.
  481. *
  482. * @handle: Handle identifying the buffer object.
  483. * @tfile: Identifying the caller.
  484. * @flags: Flags indicating the type of release.
  485. */
  486. static int vmw_user_dmabuf_synccpu_release(uint32_t handle,
  487. struct ttm_object_file *tfile,
  488. uint32_t flags)
  489. {
  490. if (!(flags & drm_vmw_synccpu_allow_cs))
  491. return ttm_ref_object_base_unref(tfile, handle,
  492. TTM_REF_SYNCCPU_WRITE);
  493. return 0;
  494. }
  495. /**
  496. * vmw_user_dmabuf_synccpu_release - ioctl function implementing the synccpu
  497. * functionality.
  498. *
  499. * @dev: Identifies the drm device.
  500. * @data: Pointer to the ioctl argument.
  501. * @file_priv: Identifies the caller.
  502. *
  503. * This function checks the ioctl arguments for validity and calls the
  504. * relevant synccpu functions.
  505. */
  506. int vmw_user_dmabuf_synccpu_ioctl(struct drm_device *dev, void *data,
  507. struct drm_file *file_priv)
  508. {
  509. struct drm_vmw_synccpu_arg *arg =
  510. (struct drm_vmw_synccpu_arg *) data;
  511. struct vmw_dma_buffer *dma_buf;
  512. struct vmw_user_dma_buffer *user_bo;
  513. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  514. struct ttm_base_object *buffer_base;
  515. int ret;
  516. if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0
  517. || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write |
  518. drm_vmw_synccpu_dontblock |
  519. drm_vmw_synccpu_allow_cs)) != 0) {
  520. DRM_ERROR("Illegal synccpu flags.\n");
  521. return -EINVAL;
  522. }
  523. switch (arg->op) {
  524. case drm_vmw_synccpu_grab:
  525. ret = vmw_user_dmabuf_lookup(tfile, arg->handle, &dma_buf,
  526. &buffer_base);
  527. if (unlikely(ret != 0))
  528. return ret;
  529. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer,
  530. dma);
  531. ret = vmw_user_dmabuf_synccpu_grab(user_bo, tfile, arg->flags);
  532. vmw_dmabuf_unreference(&dma_buf);
  533. ttm_base_object_unref(&buffer_base);
  534. if (unlikely(ret != 0 && ret != -ERESTARTSYS &&
  535. ret != -EBUSY)) {
  536. DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n",
  537. (unsigned int) arg->handle);
  538. return ret;
  539. }
  540. break;
  541. case drm_vmw_synccpu_release:
  542. ret = vmw_user_dmabuf_synccpu_release(arg->handle, tfile,
  543. arg->flags);
  544. if (unlikely(ret != 0)) {
  545. DRM_ERROR("Failed synccpu release on handle 0x%08x.\n",
  546. (unsigned int) arg->handle);
  547. return ret;
  548. }
  549. break;
  550. default:
  551. DRM_ERROR("Invalid synccpu operation.\n");
  552. return -EINVAL;
  553. }
  554. return 0;
  555. }
  556. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  557. struct drm_file *file_priv)
  558. {
  559. struct vmw_private *dev_priv = vmw_priv(dev);
  560. union drm_vmw_alloc_dmabuf_arg *arg =
  561. (union drm_vmw_alloc_dmabuf_arg *)data;
  562. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  563. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  564. struct vmw_dma_buffer *dma_buf;
  565. uint32_t handle;
  566. int ret;
  567. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  568. if (unlikely(ret != 0))
  569. return ret;
  570. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  571. req->size, false, &handle, &dma_buf,
  572. NULL);
  573. if (unlikely(ret != 0))
  574. goto out_no_dmabuf;
  575. rep->handle = handle;
  576. rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
  577. rep->cur_gmr_id = handle;
  578. rep->cur_gmr_offset = 0;
  579. vmw_dmabuf_unreference(&dma_buf);
  580. out_no_dmabuf:
  581. ttm_read_unlock(&dev_priv->reservation_sem);
  582. return ret;
  583. }
  584. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  585. struct drm_file *file_priv)
  586. {
  587. struct drm_vmw_unref_dmabuf_arg *arg =
  588. (struct drm_vmw_unref_dmabuf_arg *)data;
  589. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  590. arg->handle,
  591. TTM_REF_USAGE);
  592. }
  593. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  594. uint32_t handle, struct vmw_dma_buffer **out,
  595. struct ttm_base_object **p_base)
  596. {
  597. struct vmw_user_dma_buffer *vmw_user_bo;
  598. struct ttm_base_object *base;
  599. base = ttm_base_object_lookup(tfile, handle);
  600. if (unlikely(base == NULL)) {
  601. pr_err("Invalid buffer object handle 0x%08lx\n",
  602. (unsigned long)handle);
  603. return -ESRCH;
  604. }
  605. if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
  606. ttm_base_object_unref(&base);
  607. pr_err("Invalid buffer object handle 0x%08lx\n",
  608. (unsigned long)handle);
  609. return -EINVAL;
  610. }
  611. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
  612. prime.base);
  613. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  614. if (p_base)
  615. *p_base = base;
  616. else
  617. ttm_base_object_unref(&base);
  618. *out = &vmw_user_bo->dma;
  619. return 0;
  620. }
  621. int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
  622. struct vmw_dma_buffer *dma_buf,
  623. uint32_t *handle)
  624. {
  625. struct vmw_user_dma_buffer *user_bo;
  626. if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
  627. return -EINVAL;
  628. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
  629. *handle = user_bo->prime.base.hash.key;
  630. return ttm_ref_object_add(tfile, &user_bo->prime.base,
  631. TTM_REF_USAGE, NULL, false);
  632. }
  633. /**
  634. * vmw_dumb_create - Create a dumb kms buffer
  635. *
  636. * @file_priv: Pointer to a struct drm_file identifying the caller.
  637. * @dev: Pointer to the drm device.
  638. * @args: Pointer to a struct drm_mode_create_dumb structure
  639. *
  640. * This is a driver callback for the core drm create_dumb functionality.
  641. * Note that this is very similar to the vmw_dmabuf_alloc ioctl, except
  642. * that the arguments have a different format.
  643. */
  644. int vmw_dumb_create(struct drm_file *file_priv,
  645. struct drm_device *dev,
  646. struct drm_mode_create_dumb *args)
  647. {
  648. struct vmw_private *dev_priv = vmw_priv(dev);
  649. struct vmw_dma_buffer *dma_buf;
  650. int ret;
  651. args->pitch = args->width * ((args->bpp + 7) / 8);
  652. args->size = args->pitch * args->height;
  653. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  654. if (unlikely(ret != 0))
  655. return ret;
  656. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  657. args->size, false, &args->handle,
  658. &dma_buf, NULL);
  659. if (unlikely(ret != 0))
  660. goto out_no_dmabuf;
  661. vmw_dmabuf_unreference(&dma_buf);
  662. out_no_dmabuf:
  663. ttm_read_unlock(&dev_priv->reservation_sem);
  664. return ret;
  665. }
  666. /**
  667. * vmw_dumb_map_offset - Return the address space offset of a dumb buffer
  668. *
  669. * @file_priv: Pointer to a struct drm_file identifying the caller.
  670. * @dev: Pointer to the drm device.
  671. * @handle: Handle identifying the dumb buffer.
  672. * @offset: The address space offset returned.
  673. *
  674. * This is a driver callback for the core drm dumb_map_offset functionality.
  675. */
  676. int vmw_dumb_map_offset(struct drm_file *file_priv,
  677. struct drm_device *dev, uint32_t handle,
  678. uint64_t *offset)
  679. {
  680. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  681. struct vmw_dma_buffer *out_buf;
  682. int ret;
  683. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf, NULL);
  684. if (ret != 0)
  685. return -EINVAL;
  686. *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
  687. vmw_dmabuf_unreference(&out_buf);
  688. return 0;
  689. }
  690. /**
  691. * vmw_dumb_destroy - Destroy a dumb boffer
  692. *
  693. * @file_priv: Pointer to a struct drm_file identifying the caller.
  694. * @dev: Pointer to the drm device.
  695. * @handle: Handle identifying the dumb buffer.
  696. *
  697. * This is a driver callback for the core drm dumb_destroy functionality.
  698. */
  699. int vmw_dumb_destroy(struct drm_file *file_priv,
  700. struct drm_device *dev,
  701. uint32_t handle)
  702. {
  703. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  704. handle, TTM_REF_USAGE);
  705. }
  706. /**
  707. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  708. *
  709. * @res: The resource for which to allocate a backup buffer.
  710. * @interruptible: Whether any sleeps during allocation should be
  711. * performed while interruptible.
  712. */
  713. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  714. bool interruptible)
  715. {
  716. unsigned long size =
  717. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  718. struct vmw_dma_buffer *backup;
  719. int ret;
  720. if (likely(res->backup)) {
  721. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  722. return 0;
  723. }
  724. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  725. if (unlikely(!backup))
  726. return -ENOMEM;
  727. ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
  728. res->func->backup_placement,
  729. interruptible,
  730. &vmw_dmabuf_bo_free);
  731. if (unlikely(ret != 0))
  732. goto out_no_dmabuf;
  733. res->backup = backup;
  734. out_no_dmabuf:
  735. return ret;
  736. }
  737. /**
  738. * vmw_resource_do_validate - Make a resource up-to-date and visible
  739. * to the device.
  740. *
  741. * @res: The resource to make visible to the device.
  742. * @val_buf: Information about a buffer possibly
  743. * containing backup data if a bind operation is needed.
  744. *
  745. * On hardware resource shortage, this function returns -EBUSY and
  746. * should be retried once resources have been freed up.
  747. */
  748. static int vmw_resource_do_validate(struct vmw_resource *res,
  749. struct ttm_validate_buffer *val_buf)
  750. {
  751. int ret = 0;
  752. const struct vmw_res_func *func = res->func;
  753. if (unlikely(res->id == -1)) {
  754. ret = func->create(res);
  755. if (unlikely(ret != 0))
  756. return ret;
  757. }
  758. if (func->bind &&
  759. ((func->needs_backup && list_empty(&res->mob_head) &&
  760. val_buf->bo != NULL) ||
  761. (!func->needs_backup && val_buf->bo != NULL))) {
  762. ret = func->bind(res, val_buf);
  763. if (unlikely(ret != 0))
  764. goto out_bind_failed;
  765. if (func->needs_backup)
  766. list_add_tail(&res->mob_head, &res->backup->res_list);
  767. }
  768. /*
  769. * Only do this on write operations, and move to
  770. * vmw_resource_unreserve if it can be called after
  771. * backup buffers have been unreserved. Otherwise
  772. * sort out locking.
  773. */
  774. res->res_dirty = true;
  775. return 0;
  776. out_bind_failed:
  777. func->destroy(res);
  778. return ret;
  779. }
  780. /**
  781. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  782. * command submission.
  783. *
  784. * @res: Pointer to the struct vmw_resource to unreserve.
  785. * @switch_backup: Backup buffer has been switched.
  786. * @new_backup: Pointer to new backup buffer if command submission
  787. * switched. May be NULL.
  788. * @new_backup_offset: New backup offset if @switch_backup is true.
  789. *
  790. * Currently unreserving a resource means putting it back on the device's
  791. * resource lru list, so that it can be evicted if necessary.
  792. */
  793. void vmw_resource_unreserve(struct vmw_resource *res,
  794. bool switch_backup,
  795. struct vmw_dma_buffer *new_backup,
  796. unsigned long new_backup_offset)
  797. {
  798. struct vmw_private *dev_priv = res->dev_priv;
  799. if (!list_empty(&res->lru_head))
  800. return;
  801. if (switch_backup && new_backup != res->backup) {
  802. if (res->backup) {
  803. lockdep_assert_held(&res->backup->base.resv->lock.base);
  804. list_del_init(&res->mob_head);
  805. vmw_dmabuf_unreference(&res->backup);
  806. }
  807. if (new_backup) {
  808. res->backup = vmw_dmabuf_reference(new_backup);
  809. lockdep_assert_held(&new_backup->base.resv->lock.base);
  810. list_add_tail(&res->mob_head, &new_backup->res_list);
  811. } else {
  812. res->backup = NULL;
  813. }
  814. }
  815. if (switch_backup)
  816. res->backup_offset = new_backup_offset;
  817. if (!res->func->may_evict || res->id == -1 || res->pin_count)
  818. return;
  819. write_lock(&dev_priv->resource_lock);
  820. list_add_tail(&res->lru_head,
  821. &res->dev_priv->res_lru[res->func->res_type]);
  822. write_unlock(&dev_priv->resource_lock);
  823. }
  824. /**
  825. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  826. * for a resource and in that case, allocate
  827. * one, reserve and validate it.
  828. *
  829. * @res: The resource for which to allocate a backup buffer.
  830. * @interruptible: Whether any sleeps during allocation should be
  831. * performed while interruptible.
  832. * @val_buf: On successful return contains data about the
  833. * reserved and validated backup buffer.
  834. */
  835. static int
  836. vmw_resource_check_buffer(struct vmw_resource *res,
  837. bool interruptible,
  838. struct ttm_validate_buffer *val_buf)
  839. {
  840. struct ttm_operation_ctx ctx = { true, false };
  841. struct list_head val_list;
  842. bool backup_dirty = false;
  843. int ret;
  844. if (unlikely(res->backup == NULL)) {
  845. ret = vmw_resource_buf_alloc(res, interruptible);
  846. if (unlikely(ret != 0))
  847. return ret;
  848. }
  849. INIT_LIST_HEAD(&val_list);
  850. val_buf->bo = ttm_bo_reference(&res->backup->base);
  851. val_buf->shared = false;
  852. list_add_tail(&val_buf->head, &val_list);
  853. ret = ttm_eu_reserve_buffers(NULL, &val_list, interruptible, NULL);
  854. if (unlikely(ret != 0))
  855. goto out_no_reserve;
  856. if (res->func->needs_backup && list_empty(&res->mob_head))
  857. return 0;
  858. backup_dirty = res->backup_dirty;
  859. ret = ttm_bo_validate(&res->backup->base,
  860. res->func->backup_placement,
  861. &ctx);
  862. if (unlikely(ret != 0))
  863. goto out_no_validate;
  864. return 0;
  865. out_no_validate:
  866. ttm_eu_backoff_reservation(NULL, &val_list);
  867. out_no_reserve:
  868. ttm_bo_unref(&val_buf->bo);
  869. if (backup_dirty)
  870. vmw_dmabuf_unreference(&res->backup);
  871. return ret;
  872. }
  873. /**
  874. * vmw_resource_reserve - Reserve a resource for command submission
  875. *
  876. * @res: The resource to reserve.
  877. *
  878. * This function takes the resource off the LRU list and make sure
  879. * a backup buffer is present for guest-backed resources. However,
  880. * the buffer may not be bound to the resource at this point.
  881. *
  882. */
  883. int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
  884. bool no_backup)
  885. {
  886. struct vmw_private *dev_priv = res->dev_priv;
  887. int ret;
  888. write_lock(&dev_priv->resource_lock);
  889. list_del_init(&res->lru_head);
  890. write_unlock(&dev_priv->resource_lock);
  891. if (res->func->needs_backup && res->backup == NULL &&
  892. !no_backup) {
  893. ret = vmw_resource_buf_alloc(res, interruptible);
  894. if (unlikely(ret != 0)) {
  895. DRM_ERROR("Failed to allocate a backup buffer "
  896. "of size %lu. bytes\n",
  897. (unsigned long) res->backup_size);
  898. return ret;
  899. }
  900. }
  901. return 0;
  902. }
  903. /**
  904. * vmw_resource_backoff_reservation - Unreserve and unreference a
  905. * backup buffer
  906. *.
  907. * @val_buf: Backup buffer information.
  908. */
  909. static void
  910. vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
  911. {
  912. struct list_head val_list;
  913. if (likely(val_buf->bo == NULL))
  914. return;
  915. INIT_LIST_HEAD(&val_list);
  916. list_add_tail(&val_buf->head, &val_list);
  917. ttm_eu_backoff_reservation(NULL, &val_list);
  918. ttm_bo_unref(&val_buf->bo);
  919. }
  920. /**
  921. * vmw_resource_do_evict - Evict a resource, and transfer its data
  922. * to a backup buffer.
  923. *
  924. * @res: The resource to evict.
  925. * @interruptible: Whether to wait interruptible.
  926. */
  927. static int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
  928. {
  929. struct ttm_validate_buffer val_buf;
  930. const struct vmw_res_func *func = res->func;
  931. int ret;
  932. BUG_ON(!func->may_evict);
  933. val_buf.bo = NULL;
  934. val_buf.shared = false;
  935. ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
  936. if (unlikely(ret != 0))
  937. return ret;
  938. if (unlikely(func->unbind != NULL &&
  939. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  940. ret = func->unbind(res, res->res_dirty, &val_buf);
  941. if (unlikely(ret != 0))
  942. goto out_no_unbind;
  943. list_del_init(&res->mob_head);
  944. }
  945. ret = func->destroy(res);
  946. res->backup_dirty = true;
  947. res->res_dirty = false;
  948. out_no_unbind:
  949. vmw_resource_backoff_reservation(&val_buf);
  950. return ret;
  951. }
  952. /**
  953. * vmw_resource_validate - Make a resource up-to-date and visible
  954. * to the device.
  955. *
  956. * @res: The resource to make visible to the device.
  957. *
  958. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  959. * be reserved and validated.
  960. * On hardware resource shortage, this function will repeatedly evict
  961. * resources of the same type until the validation succeeds.
  962. */
  963. int vmw_resource_validate(struct vmw_resource *res)
  964. {
  965. int ret;
  966. struct vmw_resource *evict_res;
  967. struct vmw_private *dev_priv = res->dev_priv;
  968. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  969. struct ttm_validate_buffer val_buf;
  970. unsigned err_count = 0;
  971. if (!res->func->create)
  972. return 0;
  973. val_buf.bo = NULL;
  974. val_buf.shared = false;
  975. if (res->backup)
  976. val_buf.bo = &res->backup->base;
  977. do {
  978. ret = vmw_resource_do_validate(res, &val_buf);
  979. if (likely(ret != -EBUSY))
  980. break;
  981. write_lock(&dev_priv->resource_lock);
  982. if (list_empty(lru_list) || !res->func->may_evict) {
  983. DRM_ERROR("Out of device device resources "
  984. "for %s.\n", res->func->type_name);
  985. ret = -EBUSY;
  986. write_unlock(&dev_priv->resource_lock);
  987. break;
  988. }
  989. evict_res = vmw_resource_reference
  990. (list_first_entry(lru_list, struct vmw_resource,
  991. lru_head));
  992. list_del_init(&evict_res->lru_head);
  993. write_unlock(&dev_priv->resource_lock);
  994. ret = vmw_resource_do_evict(evict_res, true);
  995. if (unlikely(ret != 0)) {
  996. write_lock(&dev_priv->resource_lock);
  997. list_add_tail(&evict_res->lru_head, lru_list);
  998. write_unlock(&dev_priv->resource_lock);
  999. if (ret == -ERESTARTSYS ||
  1000. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1001. vmw_resource_unreference(&evict_res);
  1002. goto out_no_validate;
  1003. }
  1004. }
  1005. vmw_resource_unreference(&evict_res);
  1006. } while (1);
  1007. if (unlikely(ret != 0))
  1008. goto out_no_validate;
  1009. else if (!res->func->needs_backup && res->backup) {
  1010. list_del_init(&res->mob_head);
  1011. vmw_dmabuf_unreference(&res->backup);
  1012. }
  1013. return 0;
  1014. out_no_validate:
  1015. return ret;
  1016. }
  1017. /**
  1018. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  1019. * object without unreserving it.
  1020. *
  1021. * @bo: Pointer to the struct ttm_buffer_object to fence.
  1022. * @fence: Pointer to the fence. If NULL, this function will
  1023. * insert a fence into the command stream..
  1024. *
  1025. * Contrary to the ttm_eu version of this function, it takes only
  1026. * a single buffer object instead of a list, and it also doesn't
  1027. * unreserve the buffer object, which needs to be done separately.
  1028. */
  1029. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1030. struct vmw_fence_obj *fence)
  1031. {
  1032. struct ttm_bo_device *bdev = bo->bdev;
  1033. struct vmw_private *dev_priv =
  1034. container_of(bdev, struct vmw_private, bdev);
  1035. if (fence == NULL) {
  1036. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1037. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1038. dma_fence_put(&fence->base);
  1039. } else
  1040. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1041. }
  1042. /**
  1043. * vmw_resource_move_notify - TTM move_notify_callback
  1044. *
  1045. * @bo: The TTM buffer object about to move.
  1046. * @mem: The struct ttm_mem_reg indicating to what memory
  1047. * region the move is taking place.
  1048. *
  1049. * Evicts the Guest Backed hardware resource if the backup
  1050. * buffer is being moved out of MOB memory.
  1051. * Note that this function should not race with the resource
  1052. * validation code as long as it accesses only members of struct
  1053. * resource that remain static while bo::res is !NULL and
  1054. * while we have @bo reserved. struct resource::backup is *not* a
  1055. * static member. The resource validation code will take care
  1056. * to set @bo::res to NULL, while having @bo reserved when the
  1057. * buffer is no longer bound to the resource, so @bo:res can be
  1058. * used to determine whether there is a need to unbind and whether
  1059. * it is safe to unbind.
  1060. */
  1061. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1062. struct ttm_mem_reg *mem)
  1063. {
  1064. struct vmw_dma_buffer *dma_buf;
  1065. if (mem == NULL)
  1066. return;
  1067. if (bo->destroy != vmw_dmabuf_bo_free &&
  1068. bo->destroy != vmw_user_dmabuf_destroy)
  1069. return;
  1070. dma_buf = container_of(bo, struct vmw_dma_buffer, base);
  1071. /*
  1072. * Kill any cached kernel maps before move. An optimization could
  1073. * be to do this iff source or destination memory type is VRAM.
  1074. */
  1075. vmw_dma_buffer_unmap(dma_buf);
  1076. if (mem->mem_type != VMW_PL_MOB) {
  1077. struct vmw_resource *res, *n;
  1078. struct ttm_validate_buffer val_buf;
  1079. val_buf.bo = bo;
  1080. val_buf.shared = false;
  1081. list_for_each_entry_safe(res, n, &dma_buf->res_list, mob_head) {
  1082. if (unlikely(res->func->unbind == NULL))
  1083. continue;
  1084. (void) res->func->unbind(res, true, &val_buf);
  1085. res->backup_dirty = true;
  1086. res->res_dirty = false;
  1087. list_del_init(&res->mob_head);
  1088. }
  1089. (void) ttm_bo_wait(bo, false, false);
  1090. }
  1091. }
  1092. /**
  1093. * vmw_resource_swap_notify - swapout notify callback.
  1094. *
  1095. * @bo: The buffer object to be swapped out.
  1096. */
  1097. void vmw_resource_swap_notify(struct ttm_buffer_object *bo)
  1098. {
  1099. if (bo->destroy != vmw_dmabuf_bo_free &&
  1100. bo->destroy != vmw_user_dmabuf_destroy)
  1101. return;
  1102. /* Kill any cached kernel maps before swapout */
  1103. vmw_dma_buffer_unmap(vmw_dma_buffer(bo));
  1104. }
  1105. /**
  1106. * vmw_query_readback_all - Read back cached query states
  1107. *
  1108. * @dx_query_mob: Buffer containing the DX query MOB
  1109. *
  1110. * Read back cached states from the device if they exist. This function
  1111. * assumings binding_mutex is held.
  1112. */
  1113. int vmw_query_readback_all(struct vmw_dma_buffer *dx_query_mob)
  1114. {
  1115. struct vmw_resource *dx_query_ctx;
  1116. struct vmw_private *dev_priv;
  1117. struct {
  1118. SVGA3dCmdHeader header;
  1119. SVGA3dCmdDXReadbackAllQuery body;
  1120. } *cmd;
  1121. /* No query bound, so do nothing */
  1122. if (!dx_query_mob || !dx_query_mob->dx_query_ctx)
  1123. return 0;
  1124. dx_query_ctx = dx_query_mob->dx_query_ctx;
  1125. dev_priv = dx_query_ctx->dev_priv;
  1126. cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), dx_query_ctx->id);
  1127. if (unlikely(cmd == NULL)) {
  1128. DRM_ERROR("Failed reserving FIFO space for "
  1129. "query MOB read back.\n");
  1130. return -ENOMEM;
  1131. }
  1132. cmd->header.id = SVGA_3D_CMD_DX_READBACK_ALL_QUERY;
  1133. cmd->header.size = sizeof(cmd->body);
  1134. cmd->body.cid = dx_query_ctx->id;
  1135. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  1136. /* Triggers a rebind the next time affected context is bound */
  1137. dx_query_mob->dx_query_ctx = NULL;
  1138. return 0;
  1139. }
  1140. /**
  1141. * vmw_query_move_notify - Read back cached query states
  1142. *
  1143. * @bo: The TTM buffer object about to move.
  1144. * @mem: The memory region @bo is moving to.
  1145. *
  1146. * Called before the query MOB is swapped out to read back cached query
  1147. * states from the device.
  1148. */
  1149. void vmw_query_move_notify(struct ttm_buffer_object *bo,
  1150. struct ttm_mem_reg *mem)
  1151. {
  1152. struct vmw_dma_buffer *dx_query_mob;
  1153. struct ttm_bo_device *bdev = bo->bdev;
  1154. struct vmw_private *dev_priv;
  1155. dev_priv = container_of(bdev, struct vmw_private, bdev);
  1156. mutex_lock(&dev_priv->binding_mutex);
  1157. dx_query_mob = container_of(bo, struct vmw_dma_buffer, base);
  1158. if (mem == NULL || !dx_query_mob || !dx_query_mob->dx_query_ctx) {
  1159. mutex_unlock(&dev_priv->binding_mutex);
  1160. return;
  1161. }
  1162. /* If BO is being moved from MOB to system memory */
  1163. if (mem->mem_type == TTM_PL_SYSTEM && bo->mem.mem_type == VMW_PL_MOB) {
  1164. struct vmw_fence_obj *fence;
  1165. (void) vmw_query_readback_all(dx_query_mob);
  1166. mutex_unlock(&dev_priv->binding_mutex);
  1167. /* Create a fence and attach the BO to it */
  1168. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1169. vmw_fence_single_bo(bo, fence);
  1170. if (fence != NULL)
  1171. vmw_fence_obj_unreference(&fence);
  1172. (void) ttm_bo_wait(bo, false, false);
  1173. } else
  1174. mutex_unlock(&dev_priv->binding_mutex);
  1175. }
  1176. /**
  1177. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1178. *
  1179. * @res: The resource being queried.
  1180. */
  1181. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1182. {
  1183. return res->func->needs_backup;
  1184. }
  1185. /**
  1186. * vmw_resource_evict_type - Evict all resources of a specific type
  1187. *
  1188. * @dev_priv: Pointer to a device private struct
  1189. * @type: The resource type to evict
  1190. *
  1191. * To avoid thrashing starvation or as part of the hibernation sequence,
  1192. * try to evict all evictable resources of a specific type.
  1193. */
  1194. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1195. enum vmw_res_type type)
  1196. {
  1197. struct list_head *lru_list = &dev_priv->res_lru[type];
  1198. struct vmw_resource *evict_res;
  1199. unsigned err_count = 0;
  1200. int ret;
  1201. do {
  1202. write_lock(&dev_priv->resource_lock);
  1203. if (list_empty(lru_list))
  1204. goto out_unlock;
  1205. evict_res = vmw_resource_reference(
  1206. list_first_entry(lru_list, struct vmw_resource,
  1207. lru_head));
  1208. list_del_init(&evict_res->lru_head);
  1209. write_unlock(&dev_priv->resource_lock);
  1210. ret = vmw_resource_do_evict(evict_res, false);
  1211. if (unlikely(ret != 0)) {
  1212. write_lock(&dev_priv->resource_lock);
  1213. list_add_tail(&evict_res->lru_head, lru_list);
  1214. write_unlock(&dev_priv->resource_lock);
  1215. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1216. vmw_resource_unreference(&evict_res);
  1217. return;
  1218. }
  1219. }
  1220. vmw_resource_unreference(&evict_res);
  1221. } while (1);
  1222. out_unlock:
  1223. write_unlock(&dev_priv->resource_lock);
  1224. }
  1225. /**
  1226. * vmw_resource_evict_all - Evict all evictable resources
  1227. *
  1228. * @dev_priv: Pointer to a device private struct
  1229. *
  1230. * To avoid thrashing starvation or as part of the hibernation sequence,
  1231. * evict all evictable resources. In particular this means that all
  1232. * guest-backed resources that are registered with the device are
  1233. * evicted and the OTable becomes clean.
  1234. */
  1235. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1236. {
  1237. enum vmw_res_type type;
  1238. mutex_lock(&dev_priv->cmdbuf_mutex);
  1239. for (type = 0; type < vmw_res_max; ++type)
  1240. vmw_resource_evict_type(dev_priv, type);
  1241. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1242. }
  1243. /**
  1244. * vmw_resource_pin - Add a pin reference on a resource
  1245. *
  1246. * @res: The resource to add a pin reference on
  1247. *
  1248. * This function adds a pin reference, and if needed validates the resource.
  1249. * Having a pin reference means that the resource can never be evicted, and
  1250. * its id will never change as long as there is a pin reference.
  1251. * This function returns 0 on success and a negative error code on failure.
  1252. */
  1253. int vmw_resource_pin(struct vmw_resource *res, bool interruptible)
  1254. {
  1255. struct ttm_operation_ctx ctx = { interruptible, false };
  1256. struct vmw_private *dev_priv = res->dev_priv;
  1257. int ret;
  1258. ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  1259. mutex_lock(&dev_priv->cmdbuf_mutex);
  1260. ret = vmw_resource_reserve(res, interruptible, false);
  1261. if (ret)
  1262. goto out_no_reserve;
  1263. if (res->pin_count == 0) {
  1264. struct vmw_dma_buffer *vbo = NULL;
  1265. if (res->backup) {
  1266. vbo = res->backup;
  1267. ttm_bo_reserve(&vbo->base, interruptible, false, NULL);
  1268. if (!vbo->pin_count) {
  1269. ret = ttm_bo_validate
  1270. (&vbo->base,
  1271. res->func->backup_placement,
  1272. &ctx);
  1273. if (ret) {
  1274. ttm_bo_unreserve(&vbo->base);
  1275. goto out_no_validate;
  1276. }
  1277. }
  1278. /* Do we really need to pin the MOB as well? */
  1279. vmw_bo_pin_reserved(vbo, true);
  1280. }
  1281. ret = vmw_resource_validate(res);
  1282. if (vbo)
  1283. ttm_bo_unreserve(&vbo->base);
  1284. if (ret)
  1285. goto out_no_validate;
  1286. }
  1287. res->pin_count++;
  1288. out_no_validate:
  1289. vmw_resource_unreserve(res, false, NULL, 0UL);
  1290. out_no_reserve:
  1291. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1292. ttm_write_unlock(&dev_priv->reservation_sem);
  1293. return ret;
  1294. }
  1295. /**
  1296. * vmw_resource_unpin - Remove a pin reference from a resource
  1297. *
  1298. * @res: The resource to remove a pin reference from
  1299. *
  1300. * Having a pin reference means that the resource can never be evicted, and
  1301. * its id will never change as long as there is a pin reference.
  1302. */
  1303. void vmw_resource_unpin(struct vmw_resource *res)
  1304. {
  1305. struct vmw_private *dev_priv = res->dev_priv;
  1306. int ret;
  1307. (void) ttm_read_lock(&dev_priv->reservation_sem, false);
  1308. mutex_lock(&dev_priv->cmdbuf_mutex);
  1309. ret = vmw_resource_reserve(res, false, true);
  1310. WARN_ON(ret);
  1311. WARN_ON(res->pin_count == 0);
  1312. if (--res->pin_count == 0 && res->backup) {
  1313. struct vmw_dma_buffer *vbo = res->backup;
  1314. (void) ttm_bo_reserve(&vbo->base, false, false, NULL);
  1315. vmw_bo_pin_reserved(vbo, false);
  1316. ttm_bo_unreserve(&vbo->base);
  1317. }
  1318. vmw_resource_unreserve(res, false, NULL, 0UL);
  1319. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1320. ttm_read_unlock(&dev_priv->reservation_sem);
  1321. }
  1322. /**
  1323. * vmw_res_type - Return the resource type
  1324. *
  1325. * @res: Pointer to the resource
  1326. */
  1327. enum vmw_res_type vmw_res_type(const struct vmw_resource *res)
  1328. {
  1329. return res->func->res_type;
  1330. }