vmwgfx_resource.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-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 <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. kfree(vmw_bo);
  309. }
  310. static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  311. {
  312. struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
  313. ttm_prime_object_kfree(vmw_user_bo, prime);
  314. }
  315. int vmw_dmabuf_init(struct vmw_private *dev_priv,
  316. struct vmw_dma_buffer *vmw_bo,
  317. size_t size, struct ttm_placement *placement,
  318. bool interruptible,
  319. void (*bo_free) (struct ttm_buffer_object *bo))
  320. {
  321. struct ttm_bo_device *bdev = &dev_priv->bdev;
  322. size_t acc_size;
  323. int ret;
  324. bool user = (bo_free == &vmw_user_dmabuf_destroy);
  325. BUG_ON(!bo_free && (!user && (bo_free != vmw_dmabuf_bo_free)));
  326. acc_size = vmw_dmabuf_acc_size(dev_priv, size, user);
  327. memset(vmw_bo, 0, sizeof(*vmw_bo));
  328. INIT_LIST_HEAD(&vmw_bo->res_list);
  329. ret = ttm_bo_init(bdev, &vmw_bo->base, size,
  330. ttm_bo_type_device, placement,
  331. 0, interruptible,
  332. NULL, acc_size, NULL, NULL, bo_free);
  333. return ret;
  334. }
  335. static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
  336. {
  337. struct vmw_user_dma_buffer *vmw_user_bo;
  338. struct ttm_base_object *base = *p_base;
  339. struct ttm_buffer_object *bo;
  340. *p_base = NULL;
  341. if (unlikely(base == NULL))
  342. return;
  343. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
  344. prime.base);
  345. bo = &vmw_user_bo->dma.base;
  346. ttm_bo_unref(&bo);
  347. }
  348. static void vmw_user_dmabuf_ref_obj_release(struct ttm_base_object *base,
  349. enum ttm_ref_type ref_type)
  350. {
  351. struct vmw_user_dma_buffer *user_bo;
  352. user_bo = container_of(base, struct vmw_user_dma_buffer, prime.base);
  353. switch (ref_type) {
  354. case TTM_REF_SYNCCPU_WRITE:
  355. ttm_bo_synccpu_write_release(&user_bo->dma.base);
  356. break;
  357. default:
  358. BUG();
  359. }
  360. }
  361. /**
  362. * vmw_user_dmabuf_alloc - Allocate a user dma buffer
  363. *
  364. * @dev_priv: Pointer to a struct device private.
  365. * @tfile: Pointer to a struct ttm_object_file on which to register the user
  366. * object.
  367. * @size: Size of the dma buffer.
  368. * @shareable: Boolean whether the buffer is shareable with other open files.
  369. * @handle: Pointer to where the handle value should be assigned.
  370. * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
  371. * should be assigned.
  372. */
  373. int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
  374. struct ttm_object_file *tfile,
  375. uint32_t size,
  376. bool shareable,
  377. uint32_t *handle,
  378. struct vmw_dma_buffer **p_dma_buf,
  379. struct ttm_base_object **p_base)
  380. {
  381. struct vmw_user_dma_buffer *user_bo;
  382. struct ttm_buffer_object *tmp;
  383. int ret;
  384. user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
  385. if (unlikely(!user_bo)) {
  386. DRM_ERROR("Failed to allocate a buffer.\n");
  387. return -ENOMEM;
  388. }
  389. ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
  390. (dev_priv->has_mob) ?
  391. &vmw_sys_placement :
  392. &vmw_vram_sys_placement, true,
  393. &vmw_user_dmabuf_destroy);
  394. if (unlikely(ret != 0))
  395. return ret;
  396. tmp = ttm_bo_reference(&user_bo->dma.base);
  397. ret = ttm_prime_object_init(tfile,
  398. size,
  399. &user_bo->prime,
  400. shareable,
  401. ttm_buffer_type,
  402. &vmw_user_dmabuf_release,
  403. &vmw_user_dmabuf_ref_obj_release);
  404. if (unlikely(ret != 0)) {
  405. ttm_bo_unref(&tmp);
  406. goto out_no_base_object;
  407. }
  408. *p_dma_buf = &user_bo->dma;
  409. if (p_base) {
  410. *p_base = &user_bo->prime.base;
  411. kref_get(&(*p_base)->refcount);
  412. }
  413. *handle = user_bo->prime.base.hash.key;
  414. out_no_base_object:
  415. return ret;
  416. }
  417. /**
  418. * vmw_user_dmabuf_verify_access - verify access permissions on this
  419. * buffer object.
  420. *
  421. * @bo: Pointer to the buffer object being accessed
  422. * @tfile: Identifying the caller.
  423. */
  424. int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
  425. struct ttm_object_file *tfile)
  426. {
  427. struct vmw_user_dma_buffer *vmw_user_bo;
  428. if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
  429. return -EPERM;
  430. vmw_user_bo = vmw_user_dma_buffer(bo);
  431. /* Check that the caller has opened the object. */
  432. if (likely(ttm_ref_object_exists(tfile, &vmw_user_bo->prime.base)))
  433. return 0;
  434. DRM_ERROR("Could not grant buffer access.\n");
  435. return -EPERM;
  436. }
  437. /**
  438. * vmw_user_dmabuf_synccpu_grab - Grab a struct vmw_user_dma_buffer for cpu
  439. * access, idling previous GPU operations on the buffer and optionally
  440. * blocking it for further command submissions.
  441. *
  442. * @user_bo: Pointer to the buffer object being grabbed for CPU access
  443. * @tfile: Identifying the caller.
  444. * @flags: Flags indicating how the grab should be performed.
  445. *
  446. * A blocking grab will be automatically released when @tfile is closed.
  447. */
  448. static int vmw_user_dmabuf_synccpu_grab(struct vmw_user_dma_buffer *user_bo,
  449. struct ttm_object_file *tfile,
  450. uint32_t flags)
  451. {
  452. struct ttm_buffer_object *bo = &user_bo->dma.base;
  453. bool existed;
  454. int ret;
  455. if (flags & drm_vmw_synccpu_allow_cs) {
  456. bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
  457. long lret;
  458. lret = reservation_object_wait_timeout_rcu(bo->resv, true, true,
  459. nonblock ? 0 : MAX_SCHEDULE_TIMEOUT);
  460. if (!lret)
  461. return -EBUSY;
  462. else if (lret < 0)
  463. return lret;
  464. return 0;
  465. }
  466. ret = ttm_bo_synccpu_write_grab
  467. (bo, !!(flags & drm_vmw_synccpu_dontblock));
  468. if (unlikely(ret != 0))
  469. return ret;
  470. ret = ttm_ref_object_add(tfile, &user_bo->prime.base,
  471. TTM_REF_SYNCCPU_WRITE, &existed, false);
  472. if (ret != 0 || existed)
  473. ttm_bo_synccpu_write_release(&user_bo->dma.base);
  474. return ret;
  475. }
  476. /**
  477. * vmw_user_dmabuf_synccpu_release - Release a previous grab for CPU access,
  478. * and unblock command submission on the buffer if blocked.
  479. *
  480. * @handle: Handle identifying the buffer object.
  481. * @tfile: Identifying the caller.
  482. * @flags: Flags indicating the type of release.
  483. */
  484. static int vmw_user_dmabuf_synccpu_release(uint32_t handle,
  485. struct ttm_object_file *tfile,
  486. uint32_t flags)
  487. {
  488. if (!(flags & drm_vmw_synccpu_allow_cs))
  489. return ttm_ref_object_base_unref(tfile, handle,
  490. TTM_REF_SYNCCPU_WRITE);
  491. return 0;
  492. }
  493. /**
  494. * vmw_user_dmabuf_synccpu_release - ioctl function implementing the synccpu
  495. * functionality.
  496. *
  497. * @dev: Identifies the drm device.
  498. * @data: Pointer to the ioctl argument.
  499. * @file_priv: Identifies the caller.
  500. *
  501. * This function checks the ioctl arguments for validity and calls the
  502. * relevant synccpu functions.
  503. */
  504. int vmw_user_dmabuf_synccpu_ioctl(struct drm_device *dev, void *data,
  505. struct drm_file *file_priv)
  506. {
  507. struct drm_vmw_synccpu_arg *arg =
  508. (struct drm_vmw_synccpu_arg *) data;
  509. struct vmw_dma_buffer *dma_buf;
  510. struct vmw_user_dma_buffer *user_bo;
  511. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  512. struct ttm_base_object *buffer_base;
  513. int ret;
  514. if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0
  515. || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write |
  516. drm_vmw_synccpu_dontblock |
  517. drm_vmw_synccpu_allow_cs)) != 0) {
  518. DRM_ERROR("Illegal synccpu flags.\n");
  519. return -EINVAL;
  520. }
  521. switch (arg->op) {
  522. case drm_vmw_synccpu_grab:
  523. ret = vmw_user_dmabuf_lookup(tfile, arg->handle, &dma_buf,
  524. &buffer_base);
  525. if (unlikely(ret != 0))
  526. return ret;
  527. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer,
  528. dma);
  529. ret = vmw_user_dmabuf_synccpu_grab(user_bo, tfile, arg->flags);
  530. vmw_dmabuf_unreference(&dma_buf);
  531. ttm_base_object_unref(&buffer_base);
  532. if (unlikely(ret != 0 && ret != -ERESTARTSYS &&
  533. ret != -EBUSY)) {
  534. DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n",
  535. (unsigned int) arg->handle);
  536. return ret;
  537. }
  538. break;
  539. case drm_vmw_synccpu_release:
  540. ret = vmw_user_dmabuf_synccpu_release(arg->handle, tfile,
  541. arg->flags);
  542. if (unlikely(ret != 0)) {
  543. DRM_ERROR("Failed synccpu release on handle 0x%08x.\n",
  544. (unsigned int) arg->handle);
  545. return ret;
  546. }
  547. break;
  548. default:
  549. DRM_ERROR("Invalid synccpu operation.\n");
  550. return -EINVAL;
  551. }
  552. return 0;
  553. }
  554. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  555. struct drm_file *file_priv)
  556. {
  557. struct vmw_private *dev_priv = vmw_priv(dev);
  558. union drm_vmw_alloc_dmabuf_arg *arg =
  559. (union drm_vmw_alloc_dmabuf_arg *)data;
  560. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  561. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  562. struct vmw_dma_buffer *dma_buf;
  563. uint32_t handle;
  564. int ret;
  565. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  566. if (unlikely(ret != 0))
  567. return ret;
  568. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  569. req->size, false, &handle, &dma_buf,
  570. NULL);
  571. if (unlikely(ret != 0))
  572. goto out_no_dmabuf;
  573. rep->handle = handle;
  574. rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
  575. rep->cur_gmr_id = handle;
  576. rep->cur_gmr_offset = 0;
  577. vmw_dmabuf_unreference(&dma_buf);
  578. out_no_dmabuf:
  579. ttm_read_unlock(&dev_priv->reservation_sem);
  580. return ret;
  581. }
  582. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  583. struct drm_file *file_priv)
  584. {
  585. struct drm_vmw_unref_dmabuf_arg *arg =
  586. (struct drm_vmw_unref_dmabuf_arg *)data;
  587. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  588. arg->handle,
  589. TTM_REF_USAGE);
  590. }
  591. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  592. uint32_t handle, struct vmw_dma_buffer **out,
  593. struct ttm_base_object **p_base)
  594. {
  595. struct vmw_user_dma_buffer *vmw_user_bo;
  596. struct ttm_base_object *base;
  597. base = ttm_base_object_lookup(tfile, handle);
  598. if (unlikely(base == NULL)) {
  599. pr_err("Invalid buffer object handle 0x%08lx\n",
  600. (unsigned long)handle);
  601. return -ESRCH;
  602. }
  603. if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
  604. ttm_base_object_unref(&base);
  605. pr_err("Invalid buffer object handle 0x%08lx\n",
  606. (unsigned long)handle);
  607. return -EINVAL;
  608. }
  609. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
  610. prime.base);
  611. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  612. if (p_base)
  613. *p_base = base;
  614. else
  615. ttm_base_object_unref(&base);
  616. *out = &vmw_user_bo->dma;
  617. return 0;
  618. }
  619. int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
  620. struct vmw_dma_buffer *dma_buf,
  621. uint32_t *handle)
  622. {
  623. struct vmw_user_dma_buffer *user_bo;
  624. if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
  625. return -EINVAL;
  626. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
  627. *handle = user_bo->prime.base.hash.key;
  628. return ttm_ref_object_add(tfile, &user_bo->prime.base,
  629. TTM_REF_USAGE, NULL, false);
  630. }
  631. /**
  632. * vmw_dumb_create - Create a dumb kms buffer
  633. *
  634. * @file_priv: Pointer to a struct drm_file identifying the caller.
  635. * @dev: Pointer to the drm device.
  636. * @args: Pointer to a struct drm_mode_create_dumb structure
  637. *
  638. * This is a driver callback for the core drm create_dumb functionality.
  639. * Note that this is very similar to the vmw_dmabuf_alloc ioctl, except
  640. * that the arguments have a different format.
  641. */
  642. int vmw_dumb_create(struct drm_file *file_priv,
  643. struct drm_device *dev,
  644. struct drm_mode_create_dumb *args)
  645. {
  646. struct vmw_private *dev_priv = vmw_priv(dev);
  647. struct vmw_dma_buffer *dma_buf;
  648. int ret;
  649. args->pitch = args->width * ((args->bpp + 7) / 8);
  650. args->size = args->pitch * args->height;
  651. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  652. if (unlikely(ret != 0))
  653. return ret;
  654. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  655. args->size, false, &args->handle,
  656. &dma_buf, NULL);
  657. if (unlikely(ret != 0))
  658. goto out_no_dmabuf;
  659. vmw_dmabuf_unreference(&dma_buf);
  660. out_no_dmabuf:
  661. ttm_read_unlock(&dev_priv->reservation_sem);
  662. return ret;
  663. }
  664. /**
  665. * vmw_dumb_map_offset - Return the address space offset of a dumb buffer
  666. *
  667. * @file_priv: Pointer to a struct drm_file identifying the caller.
  668. * @dev: Pointer to the drm device.
  669. * @handle: Handle identifying the dumb buffer.
  670. * @offset: The address space offset returned.
  671. *
  672. * This is a driver callback for the core drm dumb_map_offset functionality.
  673. */
  674. int vmw_dumb_map_offset(struct drm_file *file_priv,
  675. struct drm_device *dev, uint32_t handle,
  676. uint64_t *offset)
  677. {
  678. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  679. struct vmw_dma_buffer *out_buf;
  680. int ret;
  681. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf, NULL);
  682. if (ret != 0)
  683. return -EINVAL;
  684. *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
  685. vmw_dmabuf_unreference(&out_buf);
  686. return 0;
  687. }
  688. /**
  689. * vmw_dumb_destroy - Destroy a dumb boffer
  690. *
  691. * @file_priv: Pointer to a struct drm_file identifying the caller.
  692. * @dev: Pointer to the drm device.
  693. * @handle: Handle identifying the dumb buffer.
  694. *
  695. * This is a driver callback for the core drm dumb_destroy functionality.
  696. */
  697. int vmw_dumb_destroy(struct drm_file *file_priv,
  698. struct drm_device *dev,
  699. uint32_t handle)
  700. {
  701. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  702. handle, TTM_REF_USAGE);
  703. }
  704. /**
  705. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  706. *
  707. * @res: The resource for which to allocate a backup buffer.
  708. * @interruptible: Whether any sleeps during allocation should be
  709. * performed while interruptible.
  710. */
  711. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  712. bool interruptible)
  713. {
  714. unsigned long size =
  715. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  716. struct vmw_dma_buffer *backup;
  717. int ret;
  718. if (likely(res->backup)) {
  719. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  720. return 0;
  721. }
  722. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  723. if (unlikely(!backup))
  724. return -ENOMEM;
  725. ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
  726. res->func->backup_placement,
  727. interruptible,
  728. &vmw_dmabuf_bo_free);
  729. if (unlikely(ret != 0))
  730. goto out_no_dmabuf;
  731. res->backup = backup;
  732. out_no_dmabuf:
  733. return ret;
  734. }
  735. /**
  736. * vmw_resource_do_validate - Make a resource up-to-date and visible
  737. * to the device.
  738. *
  739. * @res: The resource to make visible to the device.
  740. * @val_buf: Information about a buffer possibly
  741. * containing backup data if a bind operation is needed.
  742. *
  743. * On hardware resource shortage, this function returns -EBUSY and
  744. * should be retried once resources have been freed up.
  745. */
  746. static int vmw_resource_do_validate(struct vmw_resource *res,
  747. struct ttm_validate_buffer *val_buf)
  748. {
  749. int ret = 0;
  750. const struct vmw_res_func *func = res->func;
  751. if (unlikely(res->id == -1)) {
  752. ret = func->create(res);
  753. if (unlikely(ret != 0))
  754. return ret;
  755. }
  756. if (func->bind &&
  757. ((func->needs_backup && list_empty(&res->mob_head) &&
  758. val_buf->bo != NULL) ||
  759. (!func->needs_backup && val_buf->bo != NULL))) {
  760. ret = func->bind(res, val_buf);
  761. if (unlikely(ret != 0))
  762. goto out_bind_failed;
  763. if (func->needs_backup)
  764. list_add_tail(&res->mob_head, &res->backup->res_list);
  765. }
  766. /*
  767. * Only do this on write operations, and move to
  768. * vmw_resource_unreserve if it can be called after
  769. * backup buffers have been unreserved. Otherwise
  770. * sort out locking.
  771. */
  772. res->res_dirty = true;
  773. return 0;
  774. out_bind_failed:
  775. func->destroy(res);
  776. return ret;
  777. }
  778. /**
  779. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  780. * command submission.
  781. *
  782. * @res: Pointer to the struct vmw_resource to unreserve.
  783. * @switch_backup: Backup buffer has been switched.
  784. * @new_backup: Pointer to new backup buffer if command submission
  785. * switched. May be NULL.
  786. * @new_backup_offset: New backup offset if @switch_backup is true.
  787. *
  788. * Currently unreserving a resource means putting it back on the device's
  789. * resource lru list, so that it can be evicted if necessary.
  790. */
  791. void vmw_resource_unreserve(struct vmw_resource *res,
  792. bool switch_backup,
  793. struct vmw_dma_buffer *new_backup,
  794. unsigned long new_backup_offset)
  795. {
  796. struct vmw_private *dev_priv = res->dev_priv;
  797. if (!list_empty(&res->lru_head))
  798. return;
  799. if (switch_backup && new_backup != res->backup) {
  800. if (res->backup) {
  801. lockdep_assert_held(&res->backup->base.resv->lock.base);
  802. list_del_init(&res->mob_head);
  803. vmw_dmabuf_unreference(&res->backup);
  804. }
  805. if (new_backup) {
  806. res->backup = vmw_dmabuf_reference(new_backup);
  807. lockdep_assert_held(&new_backup->base.resv->lock.base);
  808. list_add_tail(&res->mob_head, &new_backup->res_list);
  809. } else {
  810. res->backup = NULL;
  811. }
  812. }
  813. if (switch_backup)
  814. res->backup_offset = new_backup_offset;
  815. if (!res->func->may_evict || res->id == -1 || res->pin_count)
  816. return;
  817. write_lock(&dev_priv->resource_lock);
  818. list_add_tail(&res->lru_head,
  819. &res->dev_priv->res_lru[res->func->res_type]);
  820. write_unlock(&dev_priv->resource_lock);
  821. }
  822. /**
  823. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  824. * for a resource and in that case, allocate
  825. * one, reserve and validate it.
  826. *
  827. * @res: The resource for which to allocate a backup buffer.
  828. * @interruptible: Whether any sleeps during allocation should be
  829. * performed while interruptible.
  830. * @val_buf: On successful return contains data about the
  831. * reserved and validated backup buffer.
  832. */
  833. static int
  834. vmw_resource_check_buffer(struct vmw_resource *res,
  835. bool interruptible,
  836. struct ttm_validate_buffer *val_buf)
  837. {
  838. struct list_head val_list;
  839. bool backup_dirty = false;
  840. int ret;
  841. if (unlikely(res->backup == NULL)) {
  842. ret = vmw_resource_buf_alloc(res, interruptible);
  843. if (unlikely(ret != 0))
  844. return ret;
  845. }
  846. INIT_LIST_HEAD(&val_list);
  847. val_buf->bo = ttm_bo_reference(&res->backup->base);
  848. val_buf->shared = false;
  849. list_add_tail(&val_buf->head, &val_list);
  850. ret = ttm_eu_reserve_buffers(NULL, &val_list, interruptible, NULL);
  851. if (unlikely(ret != 0))
  852. goto out_no_reserve;
  853. if (res->func->needs_backup && list_empty(&res->mob_head))
  854. return 0;
  855. backup_dirty = res->backup_dirty;
  856. ret = ttm_bo_validate(&res->backup->base,
  857. res->func->backup_placement,
  858. true, false);
  859. if (unlikely(ret != 0))
  860. goto out_no_validate;
  861. return 0;
  862. out_no_validate:
  863. ttm_eu_backoff_reservation(NULL, &val_list);
  864. out_no_reserve:
  865. ttm_bo_unref(&val_buf->bo);
  866. if (backup_dirty)
  867. vmw_dmabuf_unreference(&res->backup);
  868. return ret;
  869. }
  870. /**
  871. * vmw_resource_reserve - Reserve a resource for command submission
  872. *
  873. * @res: The resource to reserve.
  874. *
  875. * This function takes the resource off the LRU list and make sure
  876. * a backup buffer is present for guest-backed resources. However,
  877. * the buffer may not be bound to the resource at this point.
  878. *
  879. */
  880. int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
  881. bool no_backup)
  882. {
  883. struct vmw_private *dev_priv = res->dev_priv;
  884. int ret;
  885. write_lock(&dev_priv->resource_lock);
  886. list_del_init(&res->lru_head);
  887. write_unlock(&dev_priv->resource_lock);
  888. if (res->func->needs_backup && res->backup == NULL &&
  889. !no_backup) {
  890. ret = vmw_resource_buf_alloc(res, interruptible);
  891. if (unlikely(ret != 0)) {
  892. DRM_ERROR("Failed to allocate a backup buffer "
  893. "of size %lu. bytes\n",
  894. (unsigned long) res->backup_size);
  895. return ret;
  896. }
  897. }
  898. return 0;
  899. }
  900. /**
  901. * vmw_resource_backoff_reservation - Unreserve and unreference a
  902. * backup buffer
  903. *.
  904. * @val_buf: Backup buffer information.
  905. */
  906. static void
  907. vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
  908. {
  909. struct list_head val_list;
  910. if (likely(val_buf->bo == NULL))
  911. return;
  912. INIT_LIST_HEAD(&val_list);
  913. list_add_tail(&val_buf->head, &val_list);
  914. ttm_eu_backoff_reservation(NULL, &val_list);
  915. ttm_bo_unref(&val_buf->bo);
  916. }
  917. /**
  918. * vmw_resource_do_evict - Evict a resource, and transfer its data
  919. * to a backup buffer.
  920. *
  921. * @res: The resource to evict.
  922. * @interruptible: Whether to wait interruptible.
  923. */
  924. static int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
  925. {
  926. struct ttm_validate_buffer val_buf;
  927. const struct vmw_res_func *func = res->func;
  928. int ret;
  929. BUG_ON(!func->may_evict);
  930. val_buf.bo = NULL;
  931. val_buf.shared = false;
  932. ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
  933. if (unlikely(ret != 0))
  934. return ret;
  935. if (unlikely(func->unbind != NULL &&
  936. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  937. ret = func->unbind(res, res->res_dirty, &val_buf);
  938. if (unlikely(ret != 0))
  939. goto out_no_unbind;
  940. list_del_init(&res->mob_head);
  941. }
  942. ret = func->destroy(res);
  943. res->backup_dirty = true;
  944. res->res_dirty = false;
  945. out_no_unbind:
  946. vmw_resource_backoff_reservation(&val_buf);
  947. return ret;
  948. }
  949. /**
  950. * vmw_resource_validate - Make a resource up-to-date and visible
  951. * to the device.
  952. *
  953. * @res: The resource to make visible to the device.
  954. *
  955. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  956. * be reserved and validated.
  957. * On hardware resource shortage, this function will repeatedly evict
  958. * resources of the same type until the validation succeeds.
  959. */
  960. int vmw_resource_validate(struct vmw_resource *res)
  961. {
  962. int ret;
  963. struct vmw_resource *evict_res;
  964. struct vmw_private *dev_priv = res->dev_priv;
  965. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  966. struct ttm_validate_buffer val_buf;
  967. unsigned err_count = 0;
  968. if (!res->func->create)
  969. return 0;
  970. val_buf.bo = NULL;
  971. val_buf.shared = false;
  972. if (res->backup)
  973. val_buf.bo = &res->backup->base;
  974. do {
  975. ret = vmw_resource_do_validate(res, &val_buf);
  976. if (likely(ret != -EBUSY))
  977. break;
  978. write_lock(&dev_priv->resource_lock);
  979. if (list_empty(lru_list) || !res->func->may_evict) {
  980. DRM_ERROR("Out of device device resources "
  981. "for %s.\n", res->func->type_name);
  982. ret = -EBUSY;
  983. write_unlock(&dev_priv->resource_lock);
  984. break;
  985. }
  986. evict_res = vmw_resource_reference
  987. (list_first_entry(lru_list, struct vmw_resource,
  988. lru_head));
  989. list_del_init(&evict_res->lru_head);
  990. write_unlock(&dev_priv->resource_lock);
  991. ret = vmw_resource_do_evict(evict_res, true);
  992. if (unlikely(ret != 0)) {
  993. write_lock(&dev_priv->resource_lock);
  994. list_add_tail(&evict_res->lru_head, lru_list);
  995. write_unlock(&dev_priv->resource_lock);
  996. if (ret == -ERESTARTSYS ||
  997. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  998. vmw_resource_unreference(&evict_res);
  999. goto out_no_validate;
  1000. }
  1001. }
  1002. vmw_resource_unreference(&evict_res);
  1003. } while (1);
  1004. if (unlikely(ret != 0))
  1005. goto out_no_validate;
  1006. else if (!res->func->needs_backup && res->backup) {
  1007. list_del_init(&res->mob_head);
  1008. vmw_dmabuf_unreference(&res->backup);
  1009. }
  1010. return 0;
  1011. out_no_validate:
  1012. return ret;
  1013. }
  1014. /**
  1015. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  1016. * object without unreserving it.
  1017. *
  1018. * @bo: Pointer to the struct ttm_buffer_object to fence.
  1019. * @fence: Pointer to the fence. If NULL, this function will
  1020. * insert a fence into the command stream..
  1021. *
  1022. * Contrary to the ttm_eu version of this function, it takes only
  1023. * a single buffer object instead of a list, and it also doesn't
  1024. * unreserve the buffer object, which needs to be done separately.
  1025. */
  1026. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1027. struct vmw_fence_obj *fence)
  1028. {
  1029. struct ttm_bo_device *bdev = bo->bdev;
  1030. struct vmw_private *dev_priv =
  1031. container_of(bdev, struct vmw_private, bdev);
  1032. if (fence == NULL) {
  1033. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1034. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1035. dma_fence_put(&fence->base);
  1036. } else
  1037. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1038. }
  1039. /**
  1040. * vmw_resource_move_notify - TTM move_notify_callback
  1041. *
  1042. * @bo: The TTM buffer object about to move.
  1043. * @mem: The struct ttm_mem_reg indicating to what memory
  1044. * region the move is taking place.
  1045. *
  1046. * Evicts the Guest Backed hardware resource if the backup
  1047. * buffer is being moved out of MOB memory.
  1048. * Note that this function should not race with the resource
  1049. * validation code as long as it accesses only members of struct
  1050. * resource that remain static while bo::res is !NULL and
  1051. * while we have @bo reserved. struct resource::backup is *not* a
  1052. * static member. The resource validation code will take care
  1053. * to set @bo::res to NULL, while having @bo reserved when the
  1054. * buffer is no longer bound to the resource, so @bo:res can be
  1055. * used to determine whether there is a need to unbind and whether
  1056. * it is safe to unbind.
  1057. */
  1058. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1059. struct ttm_mem_reg *mem)
  1060. {
  1061. struct vmw_dma_buffer *dma_buf;
  1062. if (mem == NULL)
  1063. return;
  1064. if (bo->destroy != vmw_dmabuf_bo_free &&
  1065. bo->destroy != vmw_user_dmabuf_destroy)
  1066. return;
  1067. dma_buf = container_of(bo, struct vmw_dma_buffer, base);
  1068. if (mem->mem_type != VMW_PL_MOB) {
  1069. struct vmw_resource *res, *n;
  1070. struct ttm_validate_buffer val_buf;
  1071. val_buf.bo = bo;
  1072. val_buf.shared = false;
  1073. list_for_each_entry_safe(res, n, &dma_buf->res_list, mob_head) {
  1074. if (unlikely(res->func->unbind == NULL))
  1075. continue;
  1076. (void) res->func->unbind(res, true, &val_buf);
  1077. res->backup_dirty = true;
  1078. res->res_dirty = false;
  1079. list_del_init(&res->mob_head);
  1080. }
  1081. (void) ttm_bo_wait(bo, false, false);
  1082. }
  1083. }
  1084. /**
  1085. * vmw_query_readback_all - Read back cached query states
  1086. *
  1087. * @dx_query_mob: Buffer containing the DX query MOB
  1088. *
  1089. * Read back cached states from the device if they exist. This function
  1090. * assumings binding_mutex is held.
  1091. */
  1092. int vmw_query_readback_all(struct vmw_dma_buffer *dx_query_mob)
  1093. {
  1094. struct vmw_resource *dx_query_ctx;
  1095. struct vmw_private *dev_priv;
  1096. struct {
  1097. SVGA3dCmdHeader header;
  1098. SVGA3dCmdDXReadbackAllQuery body;
  1099. } *cmd;
  1100. /* No query bound, so do nothing */
  1101. if (!dx_query_mob || !dx_query_mob->dx_query_ctx)
  1102. return 0;
  1103. dx_query_ctx = dx_query_mob->dx_query_ctx;
  1104. dev_priv = dx_query_ctx->dev_priv;
  1105. cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), dx_query_ctx->id);
  1106. if (unlikely(cmd == NULL)) {
  1107. DRM_ERROR("Failed reserving FIFO space for "
  1108. "query MOB read back.\n");
  1109. return -ENOMEM;
  1110. }
  1111. cmd->header.id = SVGA_3D_CMD_DX_READBACK_ALL_QUERY;
  1112. cmd->header.size = sizeof(cmd->body);
  1113. cmd->body.cid = dx_query_ctx->id;
  1114. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  1115. /* Triggers a rebind the next time affected context is bound */
  1116. dx_query_mob->dx_query_ctx = NULL;
  1117. return 0;
  1118. }
  1119. /**
  1120. * vmw_query_move_notify - Read back cached query states
  1121. *
  1122. * @bo: The TTM buffer object about to move.
  1123. * @mem: The memory region @bo is moving to.
  1124. *
  1125. * Called before the query MOB is swapped out to read back cached query
  1126. * states from the device.
  1127. */
  1128. void vmw_query_move_notify(struct ttm_buffer_object *bo,
  1129. struct ttm_mem_reg *mem)
  1130. {
  1131. struct vmw_dma_buffer *dx_query_mob;
  1132. struct ttm_bo_device *bdev = bo->bdev;
  1133. struct vmw_private *dev_priv;
  1134. dev_priv = container_of(bdev, struct vmw_private, bdev);
  1135. mutex_lock(&dev_priv->binding_mutex);
  1136. dx_query_mob = container_of(bo, struct vmw_dma_buffer, base);
  1137. if (mem == NULL || !dx_query_mob || !dx_query_mob->dx_query_ctx) {
  1138. mutex_unlock(&dev_priv->binding_mutex);
  1139. return;
  1140. }
  1141. /* If BO is being moved from MOB to system memory */
  1142. if (mem->mem_type == TTM_PL_SYSTEM && bo->mem.mem_type == VMW_PL_MOB) {
  1143. struct vmw_fence_obj *fence;
  1144. (void) vmw_query_readback_all(dx_query_mob);
  1145. mutex_unlock(&dev_priv->binding_mutex);
  1146. /* Create a fence and attach the BO to it */
  1147. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1148. vmw_fence_single_bo(bo, fence);
  1149. if (fence != NULL)
  1150. vmw_fence_obj_unreference(&fence);
  1151. (void) ttm_bo_wait(bo, false, false);
  1152. } else
  1153. mutex_unlock(&dev_priv->binding_mutex);
  1154. }
  1155. /**
  1156. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1157. *
  1158. * @res: The resource being queried.
  1159. */
  1160. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1161. {
  1162. return res->func->needs_backup;
  1163. }
  1164. /**
  1165. * vmw_resource_evict_type - Evict all resources of a specific type
  1166. *
  1167. * @dev_priv: Pointer to a device private struct
  1168. * @type: The resource type to evict
  1169. *
  1170. * To avoid thrashing starvation or as part of the hibernation sequence,
  1171. * try to evict all evictable resources of a specific type.
  1172. */
  1173. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1174. enum vmw_res_type type)
  1175. {
  1176. struct list_head *lru_list = &dev_priv->res_lru[type];
  1177. struct vmw_resource *evict_res;
  1178. unsigned err_count = 0;
  1179. int ret;
  1180. do {
  1181. write_lock(&dev_priv->resource_lock);
  1182. if (list_empty(lru_list))
  1183. goto out_unlock;
  1184. evict_res = vmw_resource_reference(
  1185. list_first_entry(lru_list, struct vmw_resource,
  1186. lru_head));
  1187. list_del_init(&evict_res->lru_head);
  1188. write_unlock(&dev_priv->resource_lock);
  1189. ret = vmw_resource_do_evict(evict_res, false);
  1190. if (unlikely(ret != 0)) {
  1191. write_lock(&dev_priv->resource_lock);
  1192. list_add_tail(&evict_res->lru_head, lru_list);
  1193. write_unlock(&dev_priv->resource_lock);
  1194. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1195. vmw_resource_unreference(&evict_res);
  1196. return;
  1197. }
  1198. }
  1199. vmw_resource_unreference(&evict_res);
  1200. } while (1);
  1201. out_unlock:
  1202. write_unlock(&dev_priv->resource_lock);
  1203. }
  1204. /**
  1205. * vmw_resource_evict_all - Evict all evictable resources
  1206. *
  1207. * @dev_priv: Pointer to a device private struct
  1208. *
  1209. * To avoid thrashing starvation or as part of the hibernation sequence,
  1210. * evict all evictable resources. In particular this means that all
  1211. * guest-backed resources that are registered with the device are
  1212. * evicted and the OTable becomes clean.
  1213. */
  1214. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1215. {
  1216. enum vmw_res_type type;
  1217. mutex_lock(&dev_priv->cmdbuf_mutex);
  1218. for (type = 0; type < vmw_res_max; ++type)
  1219. vmw_resource_evict_type(dev_priv, type);
  1220. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1221. }
  1222. /**
  1223. * vmw_resource_pin - Add a pin reference on a resource
  1224. *
  1225. * @res: The resource to add a pin reference on
  1226. *
  1227. * This function adds a pin reference, and if needed validates the resource.
  1228. * Having a pin reference means that the resource can never be evicted, and
  1229. * its id will never change as long as there is a pin reference.
  1230. * This function returns 0 on success and a negative error code on failure.
  1231. */
  1232. int vmw_resource_pin(struct vmw_resource *res, bool interruptible)
  1233. {
  1234. struct vmw_private *dev_priv = res->dev_priv;
  1235. int ret;
  1236. ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  1237. mutex_lock(&dev_priv->cmdbuf_mutex);
  1238. ret = vmw_resource_reserve(res, interruptible, false);
  1239. if (ret)
  1240. goto out_no_reserve;
  1241. if (res->pin_count == 0) {
  1242. struct vmw_dma_buffer *vbo = NULL;
  1243. if (res->backup) {
  1244. vbo = res->backup;
  1245. ttm_bo_reserve(&vbo->base, interruptible, false, NULL);
  1246. if (!vbo->pin_count) {
  1247. ret = ttm_bo_validate
  1248. (&vbo->base,
  1249. res->func->backup_placement,
  1250. interruptible, false);
  1251. if (ret) {
  1252. ttm_bo_unreserve(&vbo->base);
  1253. goto out_no_validate;
  1254. }
  1255. }
  1256. /* Do we really need to pin the MOB as well? */
  1257. vmw_bo_pin_reserved(vbo, true);
  1258. }
  1259. ret = vmw_resource_validate(res);
  1260. if (vbo)
  1261. ttm_bo_unreserve(&vbo->base);
  1262. if (ret)
  1263. goto out_no_validate;
  1264. }
  1265. res->pin_count++;
  1266. out_no_validate:
  1267. vmw_resource_unreserve(res, false, NULL, 0UL);
  1268. out_no_reserve:
  1269. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1270. ttm_write_unlock(&dev_priv->reservation_sem);
  1271. return ret;
  1272. }
  1273. /**
  1274. * vmw_resource_unpin - Remove a pin reference from a resource
  1275. *
  1276. * @res: The resource to remove a pin reference from
  1277. *
  1278. * Having a pin reference means that the resource can never be evicted, and
  1279. * its id will never change as long as there is a pin reference.
  1280. */
  1281. void vmw_resource_unpin(struct vmw_resource *res)
  1282. {
  1283. struct vmw_private *dev_priv = res->dev_priv;
  1284. int ret;
  1285. (void) ttm_read_lock(&dev_priv->reservation_sem, false);
  1286. mutex_lock(&dev_priv->cmdbuf_mutex);
  1287. ret = vmw_resource_reserve(res, false, true);
  1288. WARN_ON(ret);
  1289. WARN_ON(res->pin_count == 0);
  1290. if (--res->pin_count == 0 && res->backup) {
  1291. struct vmw_dma_buffer *vbo = res->backup;
  1292. (void) ttm_bo_reserve(&vbo->base, false, false, NULL);
  1293. vmw_bo_pin_reserved(vbo, false);
  1294. ttm_bo_unreserve(&vbo->base);
  1295. }
  1296. vmw_resource_unreserve(res, false, NULL, 0UL);
  1297. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1298. ttm_read_unlock(&dev_priv->reservation_sem);
  1299. }
  1300. /**
  1301. * vmw_res_type - Return the resource type
  1302. *
  1303. * @res: Pointer to the resource
  1304. */
  1305. enum vmw_res_type vmw_res_type(const struct vmw_resource *res)
  1306. {
  1307. return res->func->res_type;
  1308. }