vmwgfx_resource.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  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, NULL);
  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. /* Check that the caller has opened the object. */
  461. if (likely(ttm_ref_object_exists(tfile, &vmw_user_bo->prime.base)))
  462. return 0;
  463. DRM_ERROR("Could not grant buffer access.\n");
  464. return -EPERM;
  465. }
  466. /**
  467. * vmw_user_dmabuf_synccpu_grab - Grab a struct vmw_user_dma_buffer for cpu
  468. * access, idling previous GPU operations on the buffer and optionally
  469. * blocking it for further command submissions.
  470. *
  471. * @user_bo: Pointer to the buffer object being grabbed for CPU access
  472. * @tfile: Identifying the caller.
  473. * @flags: Flags indicating how the grab should be performed.
  474. *
  475. * A blocking grab will be automatically released when @tfile is closed.
  476. */
  477. static int vmw_user_dmabuf_synccpu_grab(struct vmw_user_dma_buffer *user_bo,
  478. struct ttm_object_file *tfile,
  479. uint32_t flags)
  480. {
  481. struct ttm_buffer_object *bo = &user_bo->dma.base;
  482. bool existed;
  483. int ret;
  484. if (flags & drm_vmw_synccpu_allow_cs) {
  485. bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
  486. ret = ttm_bo_reserve(bo, true, nonblock, false, NULL);
  487. if (!ret) {
  488. ret = ttm_bo_wait(bo, false, true, nonblock);
  489. ttm_bo_unreserve(bo);
  490. }
  491. return ret;
  492. }
  493. ret = ttm_bo_synccpu_write_grab
  494. (bo, !!(flags & drm_vmw_synccpu_dontblock));
  495. if (unlikely(ret != 0))
  496. return ret;
  497. ret = ttm_ref_object_add(tfile, &user_bo->prime.base,
  498. TTM_REF_SYNCCPU_WRITE, &existed);
  499. if (ret != 0 || existed)
  500. ttm_bo_synccpu_write_release(&user_bo->dma.base);
  501. return ret;
  502. }
  503. /**
  504. * vmw_user_dmabuf_synccpu_release - Release a previous grab for CPU access,
  505. * and unblock command submission on the buffer if blocked.
  506. *
  507. * @handle: Handle identifying the buffer object.
  508. * @tfile: Identifying the caller.
  509. * @flags: Flags indicating the type of release.
  510. */
  511. static int vmw_user_dmabuf_synccpu_release(uint32_t handle,
  512. struct ttm_object_file *tfile,
  513. uint32_t flags)
  514. {
  515. if (!(flags & drm_vmw_synccpu_allow_cs))
  516. return ttm_ref_object_base_unref(tfile, handle,
  517. TTM_REF_SYNCCPU_WRITE);
  518. return 0;
  519. }
  520. /**
  521. * vmw_user_dmabuf_synccpu_release - ioctl function implementing the synccpu
  522. * functionality.
  523. *
  524. * @dev: Identifies the drm device.
  525. * @data: Pointer to the ioctl argument.
  526. * @file_priv: Identifies the caller.
  527. *
  528. * This function checks the ioctl arguments for validity and calls the
  529. * relevant synccpu functions.
  530. */
  531. int vmw_user_dmabuf_synccpu_ioctl(struct drm_device *dev, void *data,
  532. struct drm_file *file_priv)
  533. {
  534. struct drm_vmw_synccpu_arg *arg =
  535. (struct drm_vmw_synccpu_arg *) data;
  536. struct vmw_dma_buffer *dma_buf;
  537. struct vmw_user_dma_buffer *user_bo;
  538. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  539. int ret;
  540. if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0
  541. || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write |
  542. drm_vmw_synccpu_dontblock |
  543. drm_vmw_synccpu_allow_cs)) != 0) {
  544. DRM_ERROR("Illegal synccpu flags.\n");
  545. return -EINVAL;
  546. }
  547. switch (arg->op) {
  548. case drm_vmw_synccpu_grab:
  549. ret = vmw_user_dmabuf_lookup(tfile, arg->handle, &dma_buf);
  550. if (unlikely(ret != 0))
  551. return ret;
  552. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer,
  553. dma);
  554. ret = vmw_user_dmabuf_synccpu_grab(user_bo, tfile, arg->flags);
  555. vmw_dmabuf_unreference(&dma_buf);
  556. if (unlikely(ret != 0 && ret != -ERESTARTSYS &&
  557. ret != -EBUSY)) {
  558. DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n",
  559. (unsigned int) arg->handle);
  560. return ret;
  561. }
  562. break;
  563. case drm_vmw_synccpu_release:
  564. ret = vmw_user_dmabuf_synccpu_release(arg->handle, tfile,
  565. arg->flags);
  566. if (unlikely(ret != 0)) {
  567. DRM_ERROR("Failed synccpu release on handle 0x%08x.\n",
  568. (unsigned int) arg->handle);
  569. return ret;
  570. }
  571. break;
  572. default:
  573. DRM_ERROR("Invalid synccpu operation.\n");
  574. return -EINVAL;
  575. }
  576. return 0;
  577. }
  578. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  579. struct drm_file *file_priv)
  580. {
  581. struct vmw_private *dev_priv = vmw_priv(dev);
  582. union drm_vmw_alloc_dmabuf_arg *arg =
  583. (union drm_vmw_alloc_dmabuf_arg *)data;
  584. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  585. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  586. struct vmw_dma_buffer *dma_buf;
  587. uint32_t handle;
  588. int ret;
  589. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  590. if (unlikely(ret != 0))
  591. return ret;
  592. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  593. req->size, false, &handle, &dma_buf);
  594. if (unlikely(ret != 0))
  595. goto out_no_dmabuf;
  596. rep->handle = handle;
  597. rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
  598. rep->cur_gmr_id = handle;
  599. rep->cur_gmr_offset = 0;
  600. vmw_dmabuf_unreference(&dma_buf);
  601. out_no_dmabuf:
  602. ttm_read_unlock(&dev_priv->reservation_sem);
  603. return ret;
  604. }
  605. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  606. struct drm_file *file_priv)
  607. {
  608. struct drm_vmw_unref_dmabuf_arg *arg =
  609. (struct drm_vmw_unref_dmabuf_arg *)data;
  610. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  611. arg->handle,
  612. TTM_REF_USAGE);
  613. }
  614. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  615. uint32_t handle, struct vmw_dma_buffer **out)
  616. {
  617. struct vmw_user_dma_buffer *vmw_user_bo;
  618. struct ttm_base_object *base;
  619. base = ttm_base_object_lookup(tfile, handle);
  620. if (unlikely(base == NULL)) {
  621. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  622. (unsigned long)handle);
  623. return -ESRCH;
  624. }
  625. if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
  626. ttm_base_object_unref(&base);
  627. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  628. (unsigned long)handle);
  629. return -EINVAL;
  630. }
  631. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
  632. prime.base);
  633. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  634. ttm_base_object_unref(&base);
  635. *out = &vmw_user_bo->dma;
  636. return 0;
  637. }
  638. int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
  639. struct vmw_dma_buffer *dma_buf,
  640. uint32_t *handle)
  641. {
  642. struct vmw_user_dma_buffer *user_bo;
  643. if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
  644. return -EINVAL;
  645. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
  646. *handle = user_bo->prime.base.hash.key;
  647. return ttm_ref_object_add(tfile, &user_bo->prime.base,
  648. TTM_REF_USAGE, NULL);
  649. }
  650. /*
  651. * Stream management
  652. */
  653. static void vmw_stream_destroy(struct vmw_resource *res)
  654. {
  655. struct vmw_private *dev_priv = res->dev_priv;
  656. struct vmw_stream *stream;
  657. int ret;
  658. DRM_INFO("%s: unref\n", __func__);
  659. stream = container_of(res, struct vmw_stream, res);
  660. ret = vmw_overlay_unref(dev_priv, stream->stream_id);
  661. WARN_ON(ret != 0);
  662. }
  663. static int vmw_stream_init(struct vmw_private *dev_priv,
  664. struct vmw_stream *stream,
  665. void (*res_free) (struct vmw_resource *res))
  666. {
  667. struct vmw_resource *res = &stream->res;
  668. int ret;
  669. ret = vmw_resource_init(dev_priv, res, false, res_free,
  670. &vmw_stream_func);
  671. if (unlikely(ret != 0)) {
  672. if (res_free == NULL)
  673. kfree(stream);
  674. else
  675. res_free(&stream->res);
  676. return ret;
  677. }
  678. ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
  679. if (ret) {
  680. vmw_resource_unreference(&res);
  681. return ret;
  682. }
  683. DRM_INFO("%s: claimed\n", __func__);
  684. vmw_resource_activate(&stream->res, vmw_stream_destroy);
  685. return 0;
  686. }
  687. static void vmw_user_stream_free(struct vmw_resource *res)
  688. {
  689. struct vmw_user_stream *stream =
  690. container_of(res, struct vmw_user_stream, stream.res);
  691. struct vmw_private *dev_priv = res->dev_priv;
  692. ttm_base_object_kfree(stream, base);
  693. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  694. vmw_user_stream_size);
  695. }
  696. /**
  697. * This function is called when user space has no more references on the
  698. * base object. It releases the base-object's reference on the resource object.
  699. */
  700. static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
  701. {
  702. struct ttm_base_object *base = *p_base;
  703. struct vmw_user_stream *stream =
  704. container_of(base, struct vmw_user_stream, base);
  705. struct vmw_resource *res = &stream->stream.res;
  706. *p_base = NULL;
  707. vmw_resource_unreference(&res);
  708. }
  709. int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
  710. struct drm_file *file_priv)
  711. {
  712. struct vmw_private *dev_priv = vmw_priv(dev);
  713. struct vmw_resource *res;
  714. struct vmw_user_stream *stream;
  715. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  716. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  717. struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
  718. int ret = 0;
  719. res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
  720. if (unlikely(res == NULL))
  721. return -EINVAL;
  722. if (res->res_free != &vmw_user_stream_free) {
  723. ret = -EINVAL;
  724. goto out;
  725. }
  726. stream = container_of(res, struct vmw_user_stream, stream.res);
  727. if (stream->base.tfile != tfile) {
  728. ret = -EINVAL;
  729. goto out;
  730. }
  731. ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
  732. out:
  733. vmw_resource_unreference(&res);
  734. return ret;
  735. }
  736. int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
  737. struct drm_file *file_priv)
  738. {
  739. struct vmw_private *dev_priv = vmw_priv(dev);
  740. struct vmw_user_stream *stream;
  741. struct vmw_resource *res;
  742. struct vmw_resource *tmp;
  743. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  744. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  745. int ret;
  746. /*
  747. * Approximate idr memory usage with 128 bytes. It will be limited
  748. * by maximum number_of streams anyway?
  749. */
  750. if (unlikely(vmw_user_stream_size == 0))
  751. vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
  752. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  753. if (unlikely(ret != 0))
  754. return ret;
  755. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  756. vmw_user_stream_size,
  757. false, true);
  758. if (unlikely(ret != 0)) {
  759. if (ret != -ERESTARTSYS)
  760. DRM_ERROR("Out of graphics memory for stream"
  761. " creation.\n");
  762. goto out_unlock;
  763. }
  764. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  765. if (unlikely(stream == NULL)) {
  766. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  767. vmw_user_stream_size);
  768. ret = -ENOMEM;
  769. goto out_unlock;
  770. }
  771. res = &stream->stream.res;
  772. stream->base.shareable = false;
  773. stream->base.tfile = NULL;
  774. /*
  775. * From here on, the destructor takes over resource freeing.
  776. */
  777. ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
  778. if (unlikely(ret != 0))
  779. goto out_unlock;
  780. tmp = vmw_resource_reference(res);
  781. ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
  782. &vmw_user_stream_base_release, NULL);
  783. if (unlikely(ret != 0)) {
  784. vmw_resource_unreference(&tmp);
  785. goto out_err;
  786. }
  787. arg->stream_id = res->id;
  788. out_err:
  789. vmw_resource_unreference(&res);
  790. out_unlock:
  791. ttm_read_unlock(&dev_priv->reservation_sem);
  792. return ret;
  793. }
  794. int vmw_user_stream_lookup(struct vmw_private *dev_priv,
  795. struct ttm_object_file *tfile,
  796. uint32_t *inout_id, struct vmw_resource **out)
  797. {
  798. struct vmw_user_stream *stream;
  799. struct vmw_resource *res;
  800. int ret;
  801. res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
  802. *inout_id);
  803. if (unlikely(res == NULL))
  804. return -EINVAL;
  805. if (res->res_free != &vmw_user_stream_free) {
  806. ret = -EINVAL;
  807. goto err_ref;
  808. }
  809. stream = container_of(res, struct vmw_user_stream, stream.res);
  810. if (stream->base.tfile != tfile) {
  811. ret = -EPERM;
  812. goto err_ref;
  813. }
  814. *inout_id = stream->stream.stream_id;
  815. *out = res;
  816. return 0;
  817. err_ref:
  818. vmw_resource_unreference(&res);
  819. return ret;
  820. }
  821. /**
  822. * vmw_dumb_create - Create a dumb kms buffer
  823. *
  824. * @file_priv: Pointer to a struct drm_file identifying the caller.
  825. * @dev: Pointer to the drm device.
  826. * @args: Pointer to a struct drm_mode_create_dumb structure
  827. *
  828. * This is a driver callback for the core drm create_dumb functionality.
  829. * Note that this is very similar to the vmw_dmabuf_alloc ioctl, except
  830. * that the arguments have a different format.
  831. */
  832. int vmw_dumb_create(struct drm_file *file_priv,
  833. struct drm_device *dev,
  834. struct drm_mode_create_dumb *args)
  835. {
  836. struct vmw_private *dev_priv = vmw_priv(dev);
  837. struct vmw_dma_buffer *dma_buf;
  838. int ret;
  839. args->pitch = args->width * ((args->bpp + 7) / 8);
  840. args->size = args->pitch * args->height;
  841. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  842. if (unlikely(ret != 0))
  843. return ret;
  844. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  845. args->size, false, &args->handle,
  846. &dma_buf);
  847. if (unlikely(ret != 0))
  848. goto out_no_dmabuf;
  849. vmw_dmabuf_unreference(&dma_buf);
  850. out_no_dmabuf:
  851. ttm_read_unlock(&dev_priv->reservation_sem);
  852. return ret;
  853. }
  854. /**
  855. * vmw_dumb_map_offset - Return the address space offset of a dumb buffer
  856. *
  857. * @file_priv: Pointer to a struct drm_file identifying the caller.
  858. * @dev: Pointer to the drm device.
  859. * @handle: Handle identifying the dumb buffer.
  860. * @offset: The address space offset returned.
  861. *
  862. * This is a driver callback for the core drm dumb_map_offset functionality.
  863. */
  864. int vmw_dumb_map_offset(struct drm_file *file_priv,
  865. struct drm_device *dev, uint32_t handle,
  866. uint64_t *offset)
  867. {
  868. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  869. struct vmw_dma_buffer *out_buf;
  870. int ret;
  871. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
  872. if (ret != 0)
  873. return -EINVAL;
  874. *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
  875. vmw_dmabuf_unreference(&out_buf);
  876. return 0;
  877. }
  878. /**
  879. * vmw_dumb_destroy - Destroy a dumb boffer
  880. *
  881. * @file_priv: Pointer to a struct drm_file identifying the caller.
  882. * @dev: Pointer to the drm device.
  883. * @handle: Handle identifying the dumb buffer.
  884. *
  885. * This is a driver callback for the core drm dumb_destroy functionality.
  886. */
  887. int vmw_dumb_destroy(struct drm_file *file_priv,
  888. struct drm_device *dev,
  889. uint32_t handle)
  890. {
  891. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  892. handle, TTM_REF_USAGE);
  893. }
  894. /**
  895. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  896. *
  897. * @res: The resource for which to allocate a backup buffer.
  898. * @interruptible: Whether any sleeps during allocation should be
  899. * performed while interruptible.
  900. */
  901. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  902. bool interruptible)
  903. {
  904. unsigned long size =
  905. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  906. struct vmw_dma_buffer *backup;
  907. int ret;
  908. if (likely(res->backup)) {
  909. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  910. return 0;
  911. }
  912. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  913. if (unlikely(backup == NULL))
  914. return -ENOMEM;
  915. ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
  916. res->func->backup_placement,
  917. interruptible,
  918. &vmw_dmabuf_bo_free);
  919. if (unlikely(ret != 0))
  920. goto out_no_dmabuf;
  921. res->backup = backup;
  922. out_no_dmabuf:
  923. return ret;
  924. }
  925. /**
  926. * vmw_resource_do_validate - Make a resource up-to-date and visible
  927. * to the device.
  928. *
  929. * @res: The resource to make visible to the device.
  930. * @val_buf: Information about a buffer possibly
  931. * containing backup data if a bind operation is needed.
  932. *
  933. * On hardware resource shortage, this function returns -EBUSY and
  934. * should be retried once resources have been freed up.
  935. */
  936. static int vmw_resource_do_validate(struct vmw_resource *res,
  937. struct ttm_validate_buffer *val_buf)
  938. {
  939. int ret = 0;
  940. const struct vmw_res_func *func = res->func;
  941. if (unlikely(res->id == -1)) {
  942. ret = func->create(res);
  943. if (unlikely(ret != 0))
  944. return ret;
  945. }
  946. if (func->bind &&
  947. ((func->needs_backup && list_empty(&res->mob_head) &&
  948. val_buf->bo != NULL) ||
  949. (!func->needs_backup && val_buf->bo != NULL))) {
  950. ret = func->bind(res, val_buf);
  951. if (unlikely(ret != 0))
  952. goto out_bind_failed;
  953. if (func->needs_backup)
  954. list_add_tail(&res->mob_head, &res->backup->res_list);
  955. }
  956. /*
  957. * Only do this on write operations, and move to
  958. * vmw_resource_unreserve if it can be called after
  959. * backup buffers have been unreserved. Otherwise
  960. * sort out locking.
  961. */
  962. res->res_dirty = true;
  963. return 0;
  964. out_bind_failed:
  965. func->destroy(res);
  966. return ret;
  967. }
  968. /**
  969. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  970. * command submission.
  971. *
  972. * @res: Pointer to the struct vmw_resource to unreserve.
  973. * @new_backup: Pointer to new backup buffer if command submission
  974. * switched.
  975. * @new_backup_offset: New backup offset if @new_backup is !NULL.
  976. *
  977. * Currently unreserving a resource means putting it back on the device's
  978. * resource lru list, so that it can be evicted if necessary.
  979. */
  980. void vmw_resource_unreserve(struct vmw_resource *res,
  981. struct vmw_dma_buffer *new_backup,
  982. unsigned long new_backup_offset)
  983. {
  984. struct vmw_private *dev_priv = res->dev_priv;
  985. if (!list_empty(&res->lru_head))
  986. return;
  987. if (new_backup && new_backup != res->backup) {
  988. if (res->backup) {
  989. lockdep_assert_held(&res->backup->base.resv->lock.base);
  990. list_del_init(&res->mob_head);
  991. vmw_dmabuf_unreference(&res->backup);
  992. }
  993. res->backup = vmw_dmabuf_reference(new_backup);
  994. lockdep_assert_held(&new_backup->base.resv->lock.base);
  995. list_add_tail(&res->mob_head, &new_backup->res_list);
  996. }
  997. if (new_backup)
  998. res->backup_offset = new_backup_offset;
  999. if (!res->func->may_evict || res->id == -1)
  1000. return;
  1001. write_lock(&dev_priv->resource_lock);
  1002. list_add_tail(&res->lru_head,
  1003. &res->dev_priv->res_lru[res->func->res_type]);
  1004. write_unlock(&dev_priv->resource_lock);
  1005. }
  1006. /**
  1007. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  1008. * for a resource and in that case, allocate
  1009. * one, reserve and validate it.
  1010. *
  1011. * @res: The resource for which to allocate a backup buffer.
  1012. * @interruptible: Whether any sleeps during allocation should be
  1013. * performed while interruptible.
  1014. * @val_buf: On successful return contains data about the
  1015. * reserved and validated backup buffer.
  1016. */
  1017. static int
  1018. vmw_resource_check_buffer(struct vmw_resource *res,
  1019. bool interruptible,
  1020. struct ttm_validate_buffer *val_buf)
  1021. {
  1022. struct list_head val_list;
  1023. bool backup_dirty = false;
  1024. int ret;
  1025. if (unlikely(res->backup == NULL)) {
  1026. ret = vmw_resource_buf_alloc(res, interruptible);
  1027. if (unlikely(ret != 0))
  1028. return ret;
  1029. }
  1030. INIT_LIST_HEAD(&val_list);
  1031. val_buf->bo = ttm_bo_reference(&res->backup->base);
  1032. list_add_tail(&val_buf->head, &val_list);
  1033. ret = ttm_eu_reserve_buffers(NULL, &val_list, interruptible);
  1034. if (unlikely(ret != 0))
  1035. goto out_no_reserve;
  1036. if (res->func->needs_backup && list_empty(&res->mob_head))
  1037. return 0;
  1038. backup_dirty = res->backup_dirty;
  1039. ret = ttm_bo_validate(&res->backup->base,
  1040. res->func->backup_placement,
  1041. true, false);
  1042. if (unlikely(ret != 0))
  1043. goto out_no_validate;
  1044. return 0;
  1045. out_no_validate:
  1046. ttm_eu_backoff_reservation(NULL, &val_list);
  1047. out_no_reserve:
  1048. ttm_bo_unref(&val_buf->bo);
  1049. if (backup_dirty)
  1050. vmw_dmabuf_unreference(&res->backup);
  1051. return ret;
  1052. }
  1053. /**
  1054. * vmw_resource_reserve - Reserve a resource for command submission
  1055. *
  1056. * @res: The resource to reserve.
  1057. *
  1058. * This function takes the resource off the LRU list and make sure
  1059. * a backup buffer is present for guest-backed resources. However,
  1060. * the buffer may not be bound to the resource at this point.
  1061. *
  1062. */
  1063. int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
  1064. {
  1065. struct vmw_private *dev_priv = res->dev_priv;
  1066. int ret;
  1067. write_lock(&dev_priv->resource_lock);
  1068. list_del_init(&res->lru_head);
  1069. write_unlock(&dev_priv->resource_lock);
  1070. if (res->func->needs_backup && res->backup == NULL &&
  1071. !no_backup) {
  1072. ret = vmw_resource_buf_alloc(res, true);
  1073. if (unlikely(ret != 0))
  1074. return ret;
  1075. }
  1076. return 0;
  1077. }
  1078. /**
  1079. * vmw_resource_backoff_reservation - Unreserve and unreference a
  1080. * backup buffer
  1081. *.
  1082. * @val_buf: Backup buffer information.
  1083. */
  1084. static void
  1085. vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
  1086. {
  1087. struct list_head val_list;
  1088. if (likely(val_buf->bo == NULL))
  1089. return;
  1090. INIT_LIST_HEAD(&val_list);
  1091. list_add_tail(&val_buf->head, &val_list);
  1092. ttm_eu_backoff_reservation(NULL, &val_list);
  1093. ttm_bo_unref(&val_buf->bo);
  1094. }
  1095. /**
  1096. * vmw_resource_do_evict - Evict a resource, and transfer its data
  1097. * to a backup buffer.
  1098. *
  1099. * @res: The resource to evict.
  1100. * @interruptible: Whether to wait interruptible.
  1101. */
  1102. int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
  1103. {
  1104. struct ttm_validate_buffer val_buf;
  1105. const struct vmw_res_func *func = res->func;
  1106. int ret;
  1107. BUG_ON(!func->may_evict);
  1108. val_buf.bo = NULL;
  1109. ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
  1110. if (unlikely(ret != 0))
  1111. return ret;
  1112. if (unlikely(func->unbind != NULL &&
  1113. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  1114. ret = func->unbind(res, res->res_dirty, &val_buf);
  1115. if (unlikely(ret != 0))
  1116. goto out_no_unbind;
  1117. list_del_init(&res->mob_head);
  1118. }
  1119. ret = func->destroy(res);
  1120. res->backup_dirty = true;
  1121. res->res_dirty = false;
  1122. out_no_unbind:
  1123. vmw_resource_backoff_reservation(&val_buf);
  1124. return ret;
  1125. }
  1126. /**
  1127. * vmw_resource_validate - Make a resource up-to-date and visible
  1128. * to the device.
  1129. *
  1130. * @res: The resource to make visible to the device.
  1131. *
  1132. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  1133. * be reserved and validated.
  1134. * On hardware resource shortage, this function will repeatedly evict
  1135. * resources of the same type until the validation succeeds.
  1136. */
  1137. int vmw_resource_validate(struct vmw_resource *res)
  1138. {
  1139. int ret;
  1140. struct vmw_resource *evict_res;
  1141. struct vmw_private *dev_priv = res->dev_priv;
  1142. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  1143. struct ttm_validate_buffer val_buf;
  1144. unsigned err_count = 0;
  1145. if (likely(!res->func->may_evict))
  1146. return 0;
  1147. val_buf.bo = NULL;
  1148. if (res->backup)
  1149. val_buf.bo = &res->backup->base;
  1150. do {
  1151. ret = vmw_resource_do_validate(res, &val_buf);
  1152. if (likely(ret != -EBUSY))
  1153. break;
  1154. write_lock(&dev_priv->resource_lock);
  1155. if (list_empty(lru_list) || !res->func->may_evict) {
  1156. DRM_ERROR("Out of device device resources "
  1157. "for %s.\n", res->func->type_name);
  1158. ret = -EBUSY;
  1159. write_unlock(&dev_priv->resource_lock);
  1160. break;
  1161. }
  1162. evict_res = vmw_resource_reference
  1163. (list_first_entry(lru_list, struct vmw_resource,
  1164. lru_head));
  1165. list_del_init(&evict_res->lru_head);
  1166. write_unlock(&dev_priv->resource_lock);
  1167. ret = vmw_resource_do_evict(evict_res, true);
  1168. if (unlikely(ret != 0)) {
  1169. write_lock(&dev_priv->resource_lock);
  1170. list_add_tail(&evict_res->lru_head, lru_list);
  1171. write_unlock(&dev_priv->resource_lock);
  1172. if (ret == -ERESTARTSYS ||
  1173. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1174. vmw_resource_unreference(&evict_res);
  1175. goto out_no_validate;
  1176. }
  1177. }
  1178. vmw_resource_unreference(&evict_res);
  1179. } while (1);
  1180. if (unlikely(ret != 0))
  1181. goto out_no_validate;
  1182. else if (!res->func->needs_backup && res->backup) {
  1183. list_del_init(&res->mob_head);
  1184. vmw_dmabuf_unreference(&res->backup);
  1185. }
  1186. return 0;
  1187. out_no_validate:
  1188. return ret;
  1189. }
  1190. /**
  1191. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  1192. * object without unreserving it.
  1193. *
  1194. * @bo: Pointer to the struct ttm_buffer_object to fence.
  1195. * @fence: Pointer to the fence. If NULL, this function will
  1196. * insert a fence into the command stream..
  1197. *
  1198. * Contrary to the ttm_eu version of this function, it takes only
  1199. * a single buffer object instead of a list, and it also doesn't
  1200. * unreserve the buffer object, which needs to be done separately.
  1201. */
  1202. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1203. struct vmw_fence_obj *fence)
  1204. {
  1205. struct ttm_bo_device *bdev = bo->bdev;
  1206. struct vmw_private *dev_priv =
  1207. container_of(bdev, struct vmw_private, bdev);
  1208. if (fence == NULL) {
  1209. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1210. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1211. fence_put(&fence->base);
  1212. } else
  1213. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1214. }
  1215. /**
  1216. * vmw_resource_move_notify - TTM move_notify_callback
  1217. *
  1218. * @bo: The TTM buffer object about to move.
  1219. * @mem: The truct ttm_mem_reg indicating to what memory
  1220. * region the move is taking place.
  1221. *
  1222. * Evicts the Guest Backed hardware resource if the backup
  1223. * buffer is being moved out of MOB memory.
  1224. * Note that this function should not race with the resource
  1225. * validation code as long as it accesses only members of struct
  1226. * resource that remain static while bo::res is !NULL and
  1227. * while we have @bo reserved. struct resource::backup is *not* a
  1228. * static member. The resource validation code will take care
  1229. * to set @bo::res to NULL, while having @bo reserved when the
  1230. * buffer is no longer bound to the resource, so @bo:res can be
  1231. * used to determine whether there is a need to unbind and whether
  1232. * it is safe to unbind.
  1233. */
  1234. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1235. struct ttm_mem_reg *mem)
  1236. {
  1237. struct vmw_dma_buffer *dma_buf;
  1238. if (mem == NULL)
  1239. return;
  1240. if (bo->destroy != vmw_dmabuf_bo_free &&
  1241. bo->destroy != vmw_user_dmabuf_destroy)
  1242. return;
  1243. dma_buf = container_of(bo, struct vmw_dma_buffer, base);
  1244. if (mem->mem_type != VMW_PL_MOB) {
  1245. struct vmw_resource *res, *n;
  1246. struct ttm_validate_buffer val_buf;
  1247. val_buf.bo = bo;
  1248. list_for_each_entry_safe(res, n, &dma_buf->res_list, mob_head) {
  1249. if (unlikely(res->func->unbind == NULL))
  1250. continue;
  1251. (void) res->func->unbind(res, true, &val_buf);
  1252. res->backup_dirty = true;
  1253. res->res_dirty = false;
  1254. list_del_init(&res->mob_head);
  1255. }
  1256. (void) ttm_bo_wait(bo, false, false, false);
  1257. }
  1258. }
  1259. /**
  1260. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1261. *
  1262. * @res: The resource being queried.
  1263. */
  1264. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1265. {
  1266. return res->func->needs_backup;
  1267. }
  1268. /**
  1269. * vmw_resource_evict_type - Evict all resources of a specific type
  1270. *
  1271. * @dev_priv: Pointer to a device private struct
  1272. * @type: The resource type to evict
  1273. *
  1274. * To avoid thrashing starvation or as part of the hibernation sequence,
  1275. * try to evict all evictable resources of a specific type.
  1276. */
  1277. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1278. enum vmw_res_type type)
  1279. {
  1280. struct list_head *lru_list = &dev_priv->res_lru[type];
  1281. struct vmw_resource *evict_res;
  1282. unsigned err_count = 0;
  1283. int ret;
  1284. do {
  1285. write_lock(&dev_priv->resource_lock);
  1286. if (list_empty(lru_list))
  1287. goto out_unlock;
  1288. evict_res = vmw_resource_reference(
  1289. list_first_entry(lru_list, struct vmw_resource,
  1290. lru_head));
  1291. list_del_init(&evict_res->lru_head);
  1292. write_unlock(&dev_priv->resource_lock);
  1293. ret = vmw_resource_do_evict(evict_res, false);
  1294. if (unlikely(ret != 0)) {
  1295. write_lock(&dev_priv->resource_lock);
  1296. list_add_tail(&evict_res->lru_head, lru_list);
  1297. write_unlock(&dev_priv->resource_lock);
  1298. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1299. vmw_resource_unreference(&evict_res);
  1300. return;
  1301. }
  1302. }
  1303. vmw_resource_unreference(&evict_res);
  1304. } while (1);
  1305. out_unlock:
  1306. write_unlock(&dev_priv->resource_lock);
  1307. }
  1308. /**
  1309. * vmw_resource_evict_all - Evict all evictable resources
  1310. *
  1311. * @dev_priv: Pointer to a device private struct
  1312. *
  1313. * To avoid thrashing starvation or as part of the hibernation sequence,
  1314. * evict all evictable resources. In particular this means that all
  1315. * guest-backed resources that are registered with the device are
  1316. * evicted and the OTable becomes clean.
  1317. */
  1318. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1319. {
  1320. enum vmw_res_type type;
  1321. mutex_lock(&dev_priv->cmdbuf_mutex);
  1322. for (type = 0; type < vmw_res_max; ++type)
  1323. vmw_resource_evict_type(dev_priv, type);
  1324. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1325. }