vmwgfx_resource.c 42 KB

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