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