vmwgfx_resource.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  1. /**************************************************************************
  2. *
  3. * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "vmwgfx_drv.h"
  28. #include <drm/vmwgfx_drm.h>
  29. #include <drm/ttm/ttm_object.h>
  30. #include <drm/ttm/ttm_placement.h>
  31. #include <drm/drmP.h>
  32. #include "vmwgfx_resource_priv.h"
  33. #include "vmwgfx_binding.h"
  34. #define VMW_RES_EVICT_ERR_COUNT 10
  35. struct vmw_user_dma_buffer {
  36. struct ttm_prime_object prime;
  37. struct vmw_dma_buffer dma;
  38. };
  39. struct vmw_bo_user_rep {
  40. uint32_t handle;
  41. uint64_t map_handle;
  42. };
  43. struct vmw_stream {
  44. struct vmw_resource res;
  45. uint32_t stream_id;
  46. };
  47. struct vmw_user_stream {
  48. struct ttm_base_object base;
  49. struct vmw_stream stream;
  50. };
  51. static uint64_t vmw_user_stream_size;
  52. static const struct vmw_res_func vmw_stream_func = {
  53. .res_type = vmw_res_stream,
  54. .needs_backup = false,
  55. .may_evict = false,
  56. .type_name = "video streams",
  57. .backup_placement = NULL,
  58. .create = NULL,
  59. .destroy = NULL,
  60. .bind = NULL,
  61. .unbind = NULL
  62. };
  63. static inline struct vmw_dma_buffer *
  64. vmw_dma_buffer(struct ttm_buffer_object *bo)
  65. {
  66. return container_of(bo, struct vmw_dma_buffer, base);
  67. }
  68. static inline struct vmw_user_dma_buffer *
  69. vmw_user_dma_buffer(struct ttm_buffer_object *bo)
  70. {
  71. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  72. return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
  73. }
  74. struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
  75. {
  76. kref_get(&res->kref);
  77. return res;
  78. }
  79. struct vmw_resource *
  80. vmw_resource_reference_unless_doomed(struct vmw_resource *res)
  81. {
  82. return kref_get_unless_zero(&res->kref) ? res : NULL;
  83. }
  84. /**
  85. * vmw_resource_release_id - release a resource id to the id manager.
  86. *
  87. * @res: Pointer to the resource.
  88. *
  89. * Release the resource id to the resource id manager and set it to -1
  90. */
  91. void vmw_resource_release_id(struct vmw_resource *res)
  92. {
  93. struct vmw_private *dev_priv = res->dev_priv;
  94. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  95. write_lock(&dev_priv->resource_lock);
  96. if (res->id != -1)
  97. idr_remove(idr, res->id);
  98. res->id = -1;
  99. write_unlock(&dev_priv->resource_lock);
  100. }
  101. static void vmw_resource_release(struct kref *kref)
  102. {
  103. struct vmw_resource *res =
  104. container_of(kref, struct vmw_resource, kref);
  105. struct vmw_private *dev_priv = res->dev_priv;
  106. int id;
  107. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  108. write_lock(&dev_priv->resource_lock);
  109. res->avail = false;
  110. list_del_init(&res->lru_head);
  111. write_unlock(&dev_priv->resource_lock);
  112. if (res->backup) {
  113. struct ttm_buffer_object *bo = &res->backup->base;
  114. ttm_bo_reserve(bo, false, false, NULL);
  115. if (!list_empty(&res->mob_head) &&
  116. res->func->unbind != NULL) {
  117. struct ttm_validate_buffer val_buf;
  118. val_buf.bo = bo;
  119. val_buf.shared = false;
  120. res->func->unbind(res, false, &val_buf);
  121. }
  122. res->backup_dirty = false;
  123. list_del_init(&res->mob_head);
  124. ttm_bo_unreserve(bo);
  125. vmw_dmabuf_unreference(&res->backup);
  126. }
  127. if (likely(res->hw_destroy != NULL)) {
  128. mutex_lock(&dev_priv->binding_mutex);
  129. vmw_binding_res_list_kill(&res->binding_head);
  130. mutex_unlock(&dev_priv->binding_mutex);
  131. res->hw_destroy(res);
  132. }
  133. id = res->id;
  134. if (res->res_free != NULL)
  135. res->res_free(res);
  136. else
  137. kfree(res);
  138. write_lock(&dev_priv->resource_lock);
  139. if (id != -1)
  140. idr_remove(idr, id);
  141. write_unlock(&dev_priv->resource_lock);
  142. }
  143. void vmw_resource_unreference(struct vmw_resource **p_res)
  144. {
  145. struct vmw_resource *res = *p_res;
  146. *p_res = NULL;
  147. kref_put(&res->kref, vmw_resource_release);
  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. static 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 || !kref_get_unless_zero(&res->kref))
  235. res = NULL;
  236. read_unlock(&dev_priv->resource_lock);
  237. if (unlikely(res == NULL))
  238. return NULL;
  239. return res;
  240. }
  241. /**
  242. * vmw_user_resource_lookup_handle - lookup a struct resource from a
  243. * TTM user-space handle and perform basic type checks
  244. *
  245. * @dev_priv: Pointer to a device private struct
  246. * @tfile: Pointer to a struct ttm_object_file identifying the caller
  247. * @handle: The TTM user-space handle
  248. * @converter: Pointer to an object describing the resource type
  249. * @p_res: On successful return the location pointed to will contain
  250. * a pointer to a refcounted struct vmw_resource.
  251. *
  252. * If the handle can't be found or is associated with an incorrect resource
  253. * type, -EINVAL will be returned.
  254. */
  255. int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
  256. struct ttm_object_file *tfile,
  257. uint32_t handle,
  258. const struct vmw_user_resource_conv
  259. *converter,
  260. struct vmw_resource **p_res)
  261. {
  262. struct ttm_base_object *base;
  263. struct vmw_resource *res;
  264. int ret = -EINVAL;
  265. base = ttm_base_object_lookup(tfile, handle);
  266. if (unlikely(base == NULL))
  267. return -EINVAL;
  268. if (unlikely(ttm_base_object_type(base) != converter->object_type))
  269. goto out_bad_resource;
  270. res = converter->base_obj_to_res(base);
  271. read_lock(&dev_priv->resource_lock);
  272. if (!res->avail || res->res_free != converter->res_free) {
  273. read_unlock(&dev_priv->resource_lock);
  274. goto out_bad_resource;
  275. }
  276. kref_get(&res->kref);
  277. read_unlock(&dev_priv->resource_lock);
  278. *p_res = res;
  279. ret = 0;
  280. out_bad_resource:
  281. ttm_base_object_unref(&base);
  282. return ret;
  283. }
  284. /**
  285. * Helper function that looks either a surface or dmabuf.
  286. *
  287. * The pointer this pointed at by out_surf and out_buf needs to be null.
  288. */
  289. int vmw_user_lookup_handle(struct vmw_private *dev_priv,
  290. struct ttm_object_file *tfile,
  291. uint32_t handle,
  292. struct vmw_surface **out_surf,
  293. struct vmw_dma_buffer **out_buf)
  294. {
  295. struct vmw_resource *res;
  296. int ret;
  297. BUG_ON(*out_surf || *out_buf);
  298. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
  299. user_surface_converter,
  300. &res);
  301. if (!ret) {
  302. *out_surf = vmw_res_to_srf(res);
  303. return 0;
  304. }
  305. *out_surf = NULL;
  306. ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf, NULL);
  307. return ret;
  308. }
  309. /**
  310. * Buffer management.
  311. */
  312. /**
  313. * vmw_dmabuf_acc_size - Calculate the pinned memory usage of buffers
  314. *
  315. * @dev_priv: Pointer to a struct vmw_private identifying the device.
  316. * @size: The requested buffer size.
  317. * @user: Whether this is an ordinary dma buffer or a user dma buffer.
  318. */
  319. static size_t vmw_dmabuf_acc_size(struct vmw_private *dev_priv, size_t size,
  320. bool user)
  321. {
  322. static size_t struct_size, user_struct_size;
  323. size_t num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
  324. size_t page_array_size = ttm_round_pot(num_pages * sizeof(void *));
  325. if (unlikely(struct_size == 0)) {
  326. size_t backend_size = ttm_round_pot(vmw_tt_size);
  327. struct_size = backend_size +
  328. ttm_round_pot(sizeof(struct vmw_dma_buffer));
  329. user_struct_size = backend_size +
  330. ttm_round_pot(sizeof(struct vmw_user_dma_buffer));
  331. }
  332. if (dev_priv->map_mode == vmw_dma_alloc_coherent)
  333. page_array_size +=
  334. ttm_round_pot(num_pages * sizeof(dma_addr_t));
  335. return ((user) ? user_struct_size : struct_size) +
  336. page_array_size;
  337. }
  338. void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
  339. {
  340. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  341. kfree(vmw_bo);
  342. }
  343. static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  344. {
  345. struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
  346. ttm_prime_object_kfree(vmw_user_bo, prime);
  347. }
  348. int vmw_dmabuf_init(struct vmw_private *dev_priv,
  349. struct vmw_dma_buffer *vmw_bo,
  350. size_t size, struct ttm_placement *placement,
  351. bool interruptible,
  352. void (*bo_free) (struct ttm_buffer_object *bo))
  353. {
  354. struct ttm_bo_device *bdev = &dev_priv->bdev;
  355. size_t acc_size;
  356. int ret;
  357. bool user = (bo_free == &vmw_user_dmabuf_destroy);
  358. BUG_ON(!bo_free && (!user && (bo_free != vmw_dmabuf_bo_free)));
  359. acc_size = vmw_dmabuf_acc_size(dev_priv, size, user);
  360. memset(vmw_bo, 0, sizeof(*vmw_bo));
  361. INIT_LIST_HEAD(&vmw_bo->res_list);
  362. ret = ttm_bo_init(bdev, &vmw_bo->base, size,
  363. ttm_bo_type_device, placement,
  364. 0, interruptible,
  365. NULL, acc_size, NULL, NULL, bo_free);
  366. return ret;
  367. }
  368. static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
  369. {
  370. struct vmw_user_dma_buffer *vmw_user_bo;
  371. struct ttm_base_object *base = *p_base;
  372. struct ttm_buffer_object *bo;
  373. *p_base = NULL;
  374. if (unlikely(base == NULL))
  375. return;
  376. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
  377. prime.base);
  378. bo = &vmw_user_bo->dma.base;
  379. ttm_bo_unref(&bo);
  380. }
  381. static void vmw_user_dmabuf_ref_obj_release(struct ttm_base_object *base,
  382. enum ttm_ref_type ref_type)
  383. {
  384. struct vmw_user_dma_buffer *user_bo;
  385. user_bo = container_of(base, struct vmw_user_dma_buffer, prime.base);
  386. switch (ref_type) {
  387. case TTM_REF_SYNCCPU_WRITE:
  388. ttm_bo_synccpu_write_release(&user_bo->dma.base);
  389. break;
  390. default:
  391. BUG();
  392. }
  393. }
  394. /**
  395. * vmw_user_dmabuf_alloc - Allocate a user dma buffer
  396. *
  397. * @dev_priv: Pointer to a struct device private.
  398. * @tfile: Pointer to a struct ttm_object_file on which to register the user
  399. * object.
  400. * @size: Size of the dma buffer.
  401. * @shareable: Boolean whether the buffer is shareable with other open files.
  402. * @handle: Pointer to where the handle value should be assigned.
  403. * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
  404. * should be assigned.
  405. */
  406. int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
  407. struct ttm_object_file *tfile,
  408. uint32_t size,
  409. bool shareable,
  410. uint32_t *handle,
  411. struct vmw_dma_buffer **p_dma_buf,
  412. struct ttm_base_object **p_base)
  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. if (p_base) {
  443. *p_base = &user_bo->prime.base;
  444. kref_get(&(*p_base)->refcount);
  445. }
  446. *handle = user_bo->prime.base.hash.key;
  447. out_no_base_object:
  448. return ret;
  449. }
  450. /**
  451. * vmw_user_dmabuf_verify_access - verify access permissions on this
  452. * buffer object.
  453. *
  454. * @bo: Pointer to the buffer object being accessed
  455. * @tfile: Identifying the caller.
  456. */
  457. int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
  458. struct ttm_object_file *tfile)
  459. {
  460. struct vmw_user_dma_buffer *vmw_user_bo;
  461. if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
  462. return -EPERM;
  463. vmw_user_bo = vmw_user_dma_buffer(bo);
  464. /* Check that the caller has opened the object. */
  465. if (likely(ttm_ref_object_exists(tfile, &vmw_user_bo->prime.base)))
  466. return 0;
  467. DRM_ERROR("Could not grant buffer access.\n");
  468. return -EPERM;
  469. }
  470. /**
  471. * vmw_user_dmabuf_synccpu_grab - Grab a struct vmw_user_dma_buffer for cpu
  472. * access, idling previous GPU operations on the buffer and optionally
  473. * blocking it for further command submissions.
  474. *
  475. * @user_bo: Pointer to the buffer object being grabbed for CPU access
  476. * @tfile: Identifying the caller.
  477. * @flags: Flags indicating how the grab should be performed.
  478. *
  479. * A blocking grab will be automatically released when @tfile is closed.
  480. */
  481. static int vmw_user_dmabuf_synccpu_grab(struct vmw_user_dma_buffer *user_bo,
  482. struct ttm_object_file *tfile,
  483. uint32_t flags)
  484. {
  485. struct ttm_buffer_object *bo = &user_bo->dma.base;
  486. bool existed;
  487. int ret;
  488. if (flags & drm_vmw_synccpu_allow_cs) {
  489. bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
  490. long lret;
  491. if (nonblock)
  492. return reservation_object_test_signaled_rcu(bo->resv, true) ? 0 : -EBUSY;
  493. lret = reservation_object_wait_timeout_rcu(bo->resv, true, true, MAX_SCHEDULE_TIMEOUT);
  494. if (!lret)
  495. return -EBUSY;
  496. else if (lret < 0)
  497. return lret;
  498. return 0;
  499. }
  500. ret = ttm_bo_synccpu_write_grab
  501. (bo, !!(flags & drm_vmw_synccpu_dontblock));
  502. if (unlikely(ret != 0))
  503. return ret;
  504. ret = ttm_ref_object_add(tfile, &user_bo->prime.base,
  505. TTM_REF_SYNCCPU_WRITE, &existed);
  506. if (ret != 0 || existed)
  507. ttm_bo_synccpu_write_release(&user_bo->dma.base);
  508. return ret;
  509. }
  510. /**
  511. * vmw_user_dmabuf_synccpu_release - Release a previous grab for CPU access,
  512. * and unblock command submission on the buffer if blocked.
  513. *
  514. * @handle: Handle identifying the buffer object.
  515. * @tfile: Identifying the caller.
  516. * @flags: Flags indicating the type of release.
  517. */
  518. static int vmw_user_dmabuf_synccpu_release(uint32_t handle,
  519. struct ttm_object_file *tfile,
  520. uint32_t flags)
  521. {
  522. if (!(flags & drm_vmw_synccpu_allow_cs))
  523. return ttm_ref_object_base_unref(tfile, handle,
  524. TTM_REF_SYNCCPU_WRITE);
  525. return 0;
  526. }
  527. /**
  528. * vmw_user_dmabuf_synccpu_release - ioctl function implementing the synccpu
  529. * functionality.
  530. *
  531. * @dev: Identifies the drm device.
  532. * @data: Pointer to the ioctl argument.
  533. * @file_priv: Identifies the caller.
  534. *
  535. * This function checks the ioctl arguments for validity and calls the
  536. * relevant synccpu functions.
  537. */
  538. int vmw_user_dmabuf_synccpu_ioctl(struct drm_device *dev, void *data,
  539. struct drm_file *file_priv)
  540. {
  541. struct drm_vmw_synccpu_arg *arg =
  542. (struct drm_vmw_synccpu_arg *) data;
  543. struct vmw_dma_buffer *dma_buf;
  544. struct vmw_user_dma_buffer *user_bo;
  545. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  546. struct ttm_base_object *buffer_base;
  547. int ret;
  548. if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0
  549. || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write |
  550. drm_vmw_synccpu_dontblock |
  551. drm_vmw_synccpu_allow_cs)) != 0) {
  552. DRM_ERROR("Illegal synccpu flags.\n");
  553. return -EINVAL;
  554. }
  555. switch (arg->op) {
  556. case drm_vmw_synccpu_grab:
  557. ret = vmw_user_dmabuf_lookup(tfile, arg->handle, &dma_buf,
  558. &buffer_base);
  559. if (unlikely(ret != 0))
  560. return ret;
  561. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer,
  562. dma);
  563. ret = vmw_user_dmabuf_synccpu_grab(user_bo, tfile, arg->flags);
  564. vmw_dmabuf_unreference(&dma_buf);
  565. ttm_base_object_unref(&buffer_base);
  566. if (unlikely(ret != 0 && ret != -ERESTARTSYS &&
  567. ret != -EBUSY)) {
  568. DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n",
  569. (unsigned int) arg->handle);
  570. return ret;
  571. }
  572. break;
  573. case drm_vmw_synccpu_release:
  574. ret = vmw_user_dmabuf_synccpu_release(arg->handle, tfile,
  575. arg->flags);
  576. if (unlikely(ret != 0)) {
  577. DRM_ERROR("Failed synccpu release on handle 0x%08x.\n",
  578. (unsigned int) arg->handle);
  579. return ret;
  580. }
  581. break;
  582. default:
  583. DRM_ERROR("Invalid synccpu operation.\n");
  584. return -EINVAL;
  585. }
  586. return 0;
  587. }
  588. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  589. struct drm_file *file_priv)
  590. {
  591. struct vmw_private *dev_priv = vmw_priv(dev);
  592. union drm_vmw_alloc_dmabuf_arg *arg =
  593. (union drm_vmw_alloc_dmabuf_arg *)data;
  594. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  595. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  596. struct vmw_dma_buffer *dma_buf;
  597. uint32_t handle;
  598. int ret;
  599. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  600. if (unlikely(ret != 0))
  601. return ret;
  602. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  603. req->size, false, &handle, &dma_buf,
  604. NULL);
  605. if (unlikely(ret != 0))
  606. goto out_no_dmabuf;
  607. rep->handle = handle;
  608. rep->map_handle = drm_vma_node_offset_addr(&dma_buf->base.vma_node);
  609. rep->cur_gmr_id = handle;
  610. rep->cur_gmr_offset = 0;
  611. vmw_dmabuf_unreference(&dma_buf);
  612. out_no_dmabuf:
  613. ttm_read_unlock(&dev_priv->reservation_sem);
  614. return ret;
  615. }
  616. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  617. struct drm_file *file_priv)
  618. {
  619. struct drm_vmw_unref_dmabuf_arg *arg =
  620. (struct drm_vmw_unref_dmabuf_arg *)data;
  621. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  622. arg->handle,
  623. TTM_REF_USAGE);
  624. }
  625. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  626. uint32_t handle, struct vmw_dma_buffer **out,
  627. struct ttm_base_object **p_base)
  628. {
  629. struct vmw_user_dma_buffer *vmw_user_bo;
  630. struct ttm_base_object *base;
  631. base = ttm_base_object_lookup(tfile, handle);
  632. if (unlikely(base == NULL)) {
  633. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  634. (unsigned long)handle);
  635. return -ESRCH;
  636. }
  637. if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
  638. ttm_base_object_unref(&base);
  639. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  640. (unsigned long)handle);
  641. return -EINVAL;
  642. }
  643. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer,
  644. prime.base);
  645. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  646. if (p_base)
  647. *p_base = base;
  648. else
  649. ttm_base_object_unref(&base);
  650. *out = &vmw_user_bo->dma;
  651. return 0;
  652. }
  653. int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
  654. struct vmw_dma_buffer *dma_buf,
  655. uint32_t *handle)
  656. {
  657. struct vmw_user_dma_buffer *user_bo;
  658. if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
  659. return -EINVAL;
  660. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
  661. *handle = user_bo->prime.base.hash.key;
  662. return ttm_ref_object_add(tfile, &user_bo->prime.base,
  663. TTM_REF_USAGE, NULL);
  664. }
  665. /*
  666. * Stream management
  667. */
  668. static void vmw_stream_destroy(struct vmw_resource *res)
  669. {
  670. struct vmw_private *dev_priv = res->dev_priv;
  671. struct vmw_stream *stream;
  672. int ret;
  673. DRM_INFO("%s: unref\n", __func__);
  674. stream = container_of(res, struct vmw_stream, res);
  675. ret = vmw_overlay_unref(dev_priv, stream->stream_id);
  676. WARN_ON(ret != 0);
  677. }
  678. static int vmw_stream_init(struct vmw_private *dev_priv,
  679. struct vmw_stream *stream,
  680. void (*res_free) (struct vmw_resource *res))
  681. {
  682. struct vmw_resource *res = &stream->res;
  683. int ret;
  684. ret = vmw_resource_init(dev_priv, res, false, res_free,
  685. &vmw_stream_func);
  686. if (unlikely(ret != 0)) {
  687. if (res_free == NULL)
  688. kfree(stream);
  689. else
  690. res_free(&stream->res);
  691. return ret;
  692. }
  693. ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
  694. if (ret) {
  695. vmw_resource_unreference(&res);
  696. return ret;
  697. }
  698. DRM_INFO("%s: claimed\n", __func__);
  699. vmw_resource_activate(&stream->res, vmw_stream_destroy);
  700. return 0;
  701. }
  702. static void vmw_user_stream_free(struct vmw_resource *res)
  703. {
  704. struct vmw_user_stream *stream =
  705. container_of(res, struct vmw_user_stream, stream.res);
  706. struct vmw_private *dev_priv = res->dev_priv;
  707. ttm_base_object_kfree(stream, base);
  708. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  709. vmw_user_stream_size);
  710. }
  711. /**
  712. * This function is called when user space has no more references on the
  713. * base object. It releases the base-object's reference on the resource object.
  714. */
  715. static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
  716. {
  717. struct ttm_base_object *base = *p_base;
  718. struct vmw_user_stream *stream =
  719. container_of(base, struct vmw_user_stream, base);
  720. struct vmw_resource *res = &stream->stream.res;
  721. *p_base = NULL;
  722. vmw_resource_unreference(&res);
  723. }
  724. int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
  725. struct drm_file *file_priv)
  726. {
  727. struct vmw_private *dev_priv = vmw_priv(dev);
  728. struct vmw_resource *res;
  729. struct vmw_user_stream *stream;
  730. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  731. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  732. struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
  733. int ret = 0;
  734. res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
  735. if (unlikely(res == NULL))
  736. return -EINVAL;
  737. if (res->res_free != &vmw_user_stream_free) {
  738. ret = -EINVAL;
  739. goto out;
  740. }
  741. stream = container_of(res, struct vmw_user_stream, stream.res);
  742. if (stream->base.tfile != tfile) {
  743. ret = -EINVAL;
  744. goto out;
  745. }
  746. ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
  747. out:
  748. vmw_resource_unreference(&res);
  749. return ret;
  750. }
  751. int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
  752. struct drm_file *file_priv)
  753. {
  754. struct vmw_private *dev_priv = vmw_priv(dev);
  755. struct vmw_user_stream *stream;
  756. struct vmw_resource *res;
  757. struct vmw_resource *tmp;
  758. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  759. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  760. int ret;
  761. /*
  762. * Approximate idr memory usage with 128 bytes. It will be limited
  763. * by maximum number_of streams anyway?
  764. */
  765. if (unlikely(vmw_user_stream_size == 0))
  766. vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
  767. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  768. if (unlikely(ret != 0))
  769. return ret;
  770. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  771. vmw_user_stream_size,
  772. false, true);
  773. ttm_read_unlock(&dev_priv->reservation_sem);
  774. if (unlikely(ret != 0)) {
  775. if (ret != -ERESTARTSYS)
  776. DRM_ERROR("Out of graphics memory for stream"
  777. " creation.\n");
  778. goto out_ret;
  779. }
  780. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  781. if (unlikely(stream == NULL)) {
  782. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  783. vmw_user_stream_size);
  784. ret = -ENOMEM;
  785. goto out_ret;
  786. }
  787. res = &stream->stream.res;
  788. stream->base.shareable = false;
  789. stream->base.tfile = NULL;
  790. /*
  791. * From here on, the destructor takes over resource freeing.
  792. */
  793. ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
  794. if (unlikely(ret != 0))
  795. goto out_ret;
  796. tmp = vmw_resource_reference(res);
  797. ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
  798. &vmw_user_stream_base_release, NULL);
  799. if (unlikely(ret != 0)) {
  800. vmw_resource_unreference(&tmp);
  801. goto out_err;
  802. }
  803. arg->stream_id = res->id;
  804. out_err:
  805. vmw_resource_unreference(&res);
  806. out_ret:
  807. return ret;
  808. }
  809. int vmw_user_stream_lookup(struct vmw_private *dev_priv,
  810. struct ttm_object_file *tfile,
  811. uint32_t *inout_id, struct vmw_resource **out)
  812. {
  813. struct vmw_user_stream *stream;
  814. struct vmw_resource *res;
  815. int ret;
  816. res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
  817. *inout_id);
  818. if (unlikely(res == NULL))
  819. return -EINVAL;
  820. if (res->res_free != &vmw_user_stream_free) {
  821. ret = -EINVAL;
  822. goto err_ref;
  823. }
  824. stream = container_of(res, struct vmw_user_stream, stream.res);
  825. if (stream->base.tfile != tfile) {
  826. ret = -EPERM;
  827. goto err_ref;
  828. }
  829. *inout_id = stream->stream.stream_id;
  830. *out = res;
  831. return 0;
  832. err_ref:
  833. vmw_resource_unreference(&res);
  834. return ret;
  835. }
  836. /**
  837. * vmw_dumb_create - Create a dumb kms buffer
  838. *
  839. * @file_priv: Pointer to a struct drm_file identifying the caller.
  840. * @dev: Pointer to the drm device.
  841. * @args: Pointer to a struct drm_mode_create_dumb structure
  842. *
  843. * This is a driver callback for the core drm create_dumb functionality.
  844. * Note that this is very similar to the vmw_dmabuf_alloc ioctl, except
  845. * that the arguments have a different format.
  846. */
  847. int vmw_dumb_create(struct drm_file *file_priv,
  848. struct drm_device *dev,
  849. struct drm_mode_create_dumb *args)
  850. {
  851. struct vmw_private *dev_priv = vmw_priv(dev);
  852. struct vmw_dma_buffer *dma_buf;
  853. int ret;
  854. args->pitch = args->width * ((args->bpp + 7) / 8);
  855. args->size = args->pitch * args->height;
  856. ret = ttm_read_lock(&dev_priv->reservation_sem, true);
  857. if (unlikely(ret != 0))
  858. return ret;
  859. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  860. args->size, false, &args->handle,
  861. &dma_buf, NULL);
  862. if (unlikely(ret != 0))
  863. goto out_no_dmabuf;
  864. vmw_dmabuf_unreference(&dma_buf);
  865. out_no_dmabuf:
  866. ttm_read_unlock(&dev_priv->reservation_sem);
  867. return ret;
  868. }
  869. /**
  870. * vmw_dumb_map_offset - Return the address space offset of a dumb buffer
  871. *
  872. * @file_priv: Pointer to a struct drm_file identifying the caller.
  873. * @dev: Pointer to the drm device.
  874. * @handle: Handle identifying the dumb buffer.
  875. * @offset: The address space offset returned.
  876. *
  877. * This is a driver callback for the core drm dumb_map_offset functionality.
  878. */
  879. int vmw_dumb_map_offset(struct drm_file *file_priv,
  880. struct drm_device *dev, uint32_t handle,
  881. uint64_t *offset)
  882. {
  883. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  884. struct vmw_dma_buffer *out_buf;
  885. int ret;
  886. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf, NULL);
  887. if (ret != 0)
  888. return -EINVAL;
  889. *offset = drm_vma_node_offset_addr(&out_buf->base.vma_node);
  890. vmw_dmabuf_unreference(&out_buf);
  891. return 0;
  892. }
  893. /**
  894. * vmw_dumb_destroy - Destroy a dumb boffer
  895. *
  896. * @file_priv: Pointer to a struct drm_file identifying the caller.
  897. * @dev: Pointer to the drm device.
  898. * @handle: Handle identifying the dumb buffer.
  899. *
  900. * This is a driver callback for the core drm dumb_destroy functionality.
  901. */
  902. int vmw_dumb_destroy(struct drm_file *file_priv,
  903. struct drm_device *dev,
  904. uint32_t handle)
  905. {
  906. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  907. handle, TTM_REF_USAGE);
  908. }
  909. /**
  910. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  911. *
  912. * @res: The resource for which to allocate a backup buffer.
  913. * @interruptible: Whether any sleeps during allocation should be
  914. * performed while interruptible.
  915. */
  916. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  917. bool interruptible)
  918. {
  919. unsigned long size =
  920. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  921. struct vmw_dma_buffer *backup;
  922. int ret;
  923. if (likely(res->backup)) {
  924. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  925. return 0;
  926. }
  927. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  928. if (unlikely(backup == NULL))
  929. return -ENOMEM;
  930. ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
  931. res->func->backup_placement,
  932. interruptible,
  933. &vmw_dmabuf_bo_free);
  934. if (unlikely(ret != 0))
  935. goto out_no_dmabuf;
  936. res->backup = backup;
  937. out_no_dmabuf:
  938. return ret;
  939. }
  940. /**
  941. * vmw_resource_do_validate - Make a resource up-to-date and visible
  942. * to the device.
  943. *
  944. * @res: The resource to make visible to the device.
  945. * @val_buf: Information about a buffer possibly
  946. * containing backup data if a bind operation is needed.
  947. *
  948. * On hardware resource shortage, this function returns -EBUSY and
  949. * should be retried once resources have been freed up.
  950. */
  951. static int vmw_resource_do_validate(struct vmw_resource *res,
  952. struct ttm_validate_buffer *val_buf)
  953. {
  954. int ret = 0;
  955. const struct vmw_res_func *func = res->func;
  956. if (unlikely(res->id == -1)) {
  957. ret = func->create(res);
  958. if (unlikely(ret != 0))
  959. return ret;
  960. }
  961. if (func->bind &&
  962. ((func->needs_backup && list_empty(&res->mob_head) &&
  963. val_buf->bo != NULL) ||
  964. (!func->needs_backup && val_buf->bo != NULL))) {
  965. ret = func->bind(res, val_buf);
  966. if (unlikely(ret != 0))
  967. goto out_bind_failed;
  968. if (func->needs_backup)
  969. list_add_tail(&res->mob_head, &res->backup->res_list);
  970. }
  971. /*
  972. * Only do this on write operations, and move to
  973. * vmw_resource_unreserve if it can be called after
  974. * backup buffers have been unreserved. Otherwise
  975. * sort out locking.
  976. */
  977. res->res_dirty = true;
  978. return 0;
  979. out_bind_failed:
  980. func->destroy(res);
  981. return ret;
  982. }
  983. /**
  984. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  985. * command submission.
  986. *
  987. * @res: Pointer to the struct vmw_resource to unreserve.
  988. * @switch_backup: Backup buffer has been switched.
  989. * @new_backup: Pointer to new backup buffer if command submission
  990. * switched. May be NULL.
  991. * @new_backup_offset: New backup offset if @switch_backup is true.
  992. *
  993. * Currently unreserving a resource means putting it back on the device's
  994. * resource lru list, so that it can be evicted if necessary.
  995. */
  996. void vmw_resource_unreserve(struct vmw_resource *res,
  997. bool switch_backup,
  998. struct vmw_dma_buffer *new_backup,
  999. unsigned long new_backup_offset)
  1000. {
  1001. struct vmw_private *dev_priv = res->dev_priv;
  1002. if (!list_empty(&res->lru_head))
  1003. return;
  1004. if (switch_backup && new_backup != res->backup) {
  1005. if (res->backup) {
  1006. lockdep_assert_held(&res->backup->base.resv->lock.base);
  1007. list_del_init(&res->mob_head);
  1008. vmw_dmabuf_unreference(&res->backup);
  1009. }
  1010. if (new_backup) {
  1011. res->backup = vmw_dmabuf_reference(new_backup);
  1012. lockdep_assert_held(&new_backup->base.resv->lock.base);
  1013. list_add_tail(&res->mob_head, &new_backup->res_list);
  1014. } else {
  1015. res->backup = NULL;
  1016. }
  1017. }
  1018. if (switch_backup)
  1019. res->backup_offset = new_backup_offset;
  1020. if (!res->func->may_evict || res->id == -1 || res->pin_count)
  1021. return;
  1022. write_lock(&dev_priv->resource_lock);
  1023. list_add_tail(&res->lru_head,
  1024. &res->dev_priv->res_lru[res->func->res_type]);
  1025. write_unlock(&dev_priv->resource_lock);
  1026. }
  1027. /**
  1028. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  1029. * for a resource and in that case, allocate
  1030. * one, reserve and validate it.
  1031. *
  1032. * @res: The resource for which to allocate a backup buffer.
  1033. * @interruptible: Whether any sleeps during allocation should be
  1034. * performed while interruptible.
  1035. * @val_buf: On successful return contains data about the
  1036. * reserved and validated backup buffer.
  1037. */
  1038. static int
  1039. vmw_resource_check_buffer(struct vmw_resource *res,
  1040. bool interruptible,
  1041. struct ttm_validate_buffer *val_buf)
  1042. {
  1043. struct list_head val_list;
  1044. bool backup_dirty = false;
  1045. int ret;
  1046. if (unlikely(res->backup == NULL)) {
  1047. ret = vmw_resource_buf_alloc(res, interruptible);
  1048. if (unlikely(ret != 0))
  1049. return ret;
  1050. }
  1051. INIT_LIST_HEAD(&val_list);
  1052. val_buf->bo = ttm_bo_reference(&res->backup->base);
  1053. val_buf->shared = false;
  1054. list_add_tail(&val_buf->head, &val_list);
  1055. ret = ttm_eu_reserve_buffers(NULL, &val_list, interruptible, NULL);
  1056. if (unlikely(ret != 0))
  1057. goto out_no_reserve;
  1058. if (res->func->needs_backup && list_empty(&res->mob_head))
  1059. return 0;
  1060. backup_dirty = res->backup_dirty;
  1061. ret = ttm_bo_validate(&res->backup->base,
  1062. res->func->backup_placement,
  1063. true, false);
  1064. if (unlikely(ret != 0))
  1065. goto out_no_validate;
  1066. return 0;
  1067. out_no_validate:
  1068. ttm_eu_backoff_reservation(NULL, &val_list);
  1069. out_no_reserve:
  1070. ttm_bo_unref(&val_buf->bo);
  1071. if (backup_dirty)
  1072. vmw_dmabuf_unreference(&res->backup);
  1073. return ret;
  1074. }
  1075. /**
  1076. * vmw_resource_reserve - Reserve a resource for command submission
  1077. *
  1078. * @res: The resource to reserve.
  1079. *
  1080. * This function takes the resource off the LRU list and make sure
  1081. * a backup buffer is present for guest-backed resources. However,
  1082. * the buffer may not be bound to the resource at this point.
  1083. *
  1084. */
  1085. int vmw_resource_reserve(struct vmw_resource *res, bool interruptible,
  1086. bool no_backup)
  1087. {
  1088. struct vmw_private *dev_priv = res->dev_priv;
  1089. int ret;
  1090. write_lock(&dev_priv->resource_lock);
  1091. list_del_init(&res->lru_head);
  1092. write_unlock(&dev_priv->resource_lock);
  1093. if (res->func->needs_backup && res->backup == NULL &&
  1094. !no_backup) {
  1095. ret = vmw_resource_buf_alloc(res, interruptible);
  1096. if (unlikely(ret != 0)) {
  1097. DRM_ERROR("Failed to allocate a backup buffer "
  1098. "of size %lu. bytes\n",
  1099. (unsigned long) res->backup_size);
  1100. return ret;
  1101. }
  1102. }
  1103. return 0;
  1104. }
  1105. /**
  1106. * vmw_resource_backoff_reservation - Unreserve and unreference a
  1107. * backup buffer
  1108. *.
  1109. * @val_buf: Backup buffer information.
  1110. */
  1111. static void
  1112. vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
  1113. {
  1114. struct list_head val_list;
  1115. if (likely(val_buf->bo == NULL))
  1116. return;
  1117. INIT_LIST_HEAD(&val_list);
  1118. list_add_tail(&val_buf->head, &val_list);
  1119. ttm_eu_backoff_reservation(NULL, &val_list);
  1120. ttm_bo_unref(&val_buf->bo);
  1121. }
  1122. /**
  1123. * vmw_resource_do_evict - Evict a resource, and transfer its data
  1124. * to a backup buffer.
  1125. *
  1126. * @res: The resource to evict.
  1127. * @interruptible: Whether to wait interruptible.
  1128. */
  1129. static int vmw_resource_do_evict(struct vmw_resource *res, bool interruptible)
  1130. {
  1131. struct ttm_validate_buffer val_buf;
  1132. const struct vmw_res_func *func = res->func;
  1133. int ret;
  1134. BUG_ON(!func->may_evict);
  1135. val_buf.bo = NULL;
  1136. val_buf.shared = false;
  1137. ret = vmw_resource_check_buffer(res, interruptible, &val_buf);
  1138. if (unlikely(ret != 0))
  1139. return ret;
  1140. if (unlikely(func->unbind != NULL &&
  1141. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  1142. ret = func->unbind(res, res->res_dirty, &val_buf);
  1143. if (unlikely(ret != 0))
  1144. goto out_no_unbind;
  1145. list_del_init(&res->mob_head);
  1146. }
  1147. ret = func->destroy(res);
  1148. res->backup_dirty = true;
  1149. res->res_dirty = false;
  1150. out_no_unbind:
  1151. vmw_resource_backoff_reservation(&val_buf);
  1152. return ret;
  1153. }
  1154. /**
  1155. * vmw_resource_validate - Make a resource up-to-date and visible
  1156. * to the device.
  1157. *
  1158. * @res: The resource to make visible to the device.
  1159. *
  1160. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  1161. * be reserved and validated.
  1162. * On hardware resource shortage, this function will repeatedly evict
  1163. * resources of the same type until the validation succeeds.
  1164. */
  1165. int vmw_resource_validate(struct vmw_resource *res)
  1166. {
  1167. int ret;
  1168. struct vmw_resource *evict_res;
  1169. struct vmw_private *dev_priv = res->dev_priv;
  1170. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  1171. struct ttm_validate_buffer val_buf;
  1172. unsigned err_count = 0;
  1173. if (!res->func->create)
  1174. return 0;
  1175. val_buf.bo = NULL;
  1176. val_buf.shared = false;
  1177. if (res->backup)
  1178. val_buf.bo = &res->backup->base;
  1179. do {
  1180. ret = vmw_resource_do_validate(res, &val_buf);
  1181. if (likely(ret != -EBUSY))
  1182. break;
  1183. write_lock(&dev_priv->resource_lock);
  1184. if (list_empty(lru_list) || !res->func->may_evict) {
  1185. DRM_ERROR("Out of device device resources "
  1186. "for %s.\n", res->func->type_name);
  1187. ret = -EBUSY;
  1188. write_unlock(&dev_priv->resource_lock);
  1189. break;
  1190. }
  1191. evict_res = vmw_resource_reference
  1192. (list_first_entry(lru_list, struct vmw_resource,
  1193. lru_head));
  1194. list_del_init(&evict_res->lru_head);
  1195. write_unlock(&dev_priv->resource_lock);
  1196. ret = vmw_resource_do_evict(evict_res, true);
  1197. if (unlikely(ret != 0)) {
  1198. write_lock(&dev_priv->resource_lock);
  1199. list_add_tail(&evict_res->lru_head, lru_list);
  1200. write_unlock(&dev_priv->resource_lock);
  1201. if (ret == -ERESTARTSYS ||
  1202. ++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1203. vmw_resource_unreference(&evict_res);
  1204. goto out_no_validate;
  1205. }
  1206. }
  1207. vmw_resource_unreference(&evict_res);
  1208. } while (1);
  1209. if (unlikely(ret != 0))
  1210. goto out_no_validate;
  1211. else if (!res->func->needs_backup && res->backup) {
  1212. list_del_init(&res->mob_head);
  1213. vmw_dmabuf_unreference(&res->backup);
  1214. }
  1215. return 0;
  1216. out_no_validate:
  1217. return ret;
  1218. }
  1219. /**
  1220. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  1221. * object without unreserving it.
  1222. *
  1223. * @bo: Pointer to the struct ttm_buffer_object to fence.
  1224. * @fence: Pointer to the fence. If NULL, this function will
  1225. * insert a fence into the command stream..
  1226. *
  1227. * Contrary to the ttm_eu version of this function, it takes only
  1228. * a single buffer object instead of a list, and it also doesn't
  1229. * unreserve the buffer object, which needs to be done separately.
  1230. */
  1231. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1232. struct vmw_fence_obj *fence)
  1233. {
  1234. struct ttm_bo_device *bdev = bo->bdev;
  1235. struct vmw_private *dev_priv =
  1236. container_of(bdev, struct vmw_private, bdev);
  1237. if (fence == NULL) {
  1238. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1239. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1240. fence_put(&fence->base);
  1241. } else
  1242. reservation_object_add_excl_fence(bo->resv, &fence->base);
  1243. }
  1244. /**
  1245. * vmw_resource_move_notify - TTM move_notify_callback
  1246. *
  1247. * @bo: The TTM buffer object about to move.
  1248. * @mem: The struct ttm_mem_reg indicating to what memory
  1249. * region the move is taking place.
  1250. *
  1251. * Evicts the Guest Backed hardware resource if the backup
  1252. * buffer is being moved out of MOB memory.
  1253. * Note that this function should not race with the resource
  1254. * validation code as long as it accesses only members of struct
  1255. * resource that remain static while bo::res is !NULL and
  1256. * while we have @bo reserved. struct resource::backup is *not* a
  1257. * static member. The resource validation code will take care
  1258. * to set @bo::res to NULL, while having @bo reserved when the
  1259. * buffer is no longer bound to the resource, so @bo:res can be
  1260. * used to determine whether there is a need to unbind and whether
  1261. * it is safe to unbind.
  1262. */
  1263. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1264. struct ttm_mem_reg *mem)
  1265. {
  1266. struct vmw_dma_buffer *dma_buf;
  1267. if (mem == NULL)
  1268. return;
  1269. if (bo->destroy != vmw_dmabuf_bo_free &&
  1270. bo->destroy != vmw_user_dmabuf_destroy)
  1271. return;
  1272. dma_buf = container_of(bo, struct vmw_dma_buffer, base);
  1273. if (mem->mem_type != VMW_PL_MOB) {
  1274. struct vmw_resource *res, *n;
  1275. struct ttm_validate_buffer val_buf;
  1276. val_buf.bo = bo;
  1277. val_buf.shared = false;
  1278. list_for_each_entry_safe(res, n, &dma_buf->res_list, mob_head) {
  1279. if (unlikely(res->func->unbind == NULL))
  1280. continue;
  1281. (void) res->func->unbind(res, true, &val_buf);
  1282. res->backup_dirty = true;
  1283. res->res_dirty = false;
  1284. list_del_init(&res->mob_head);
  1285. }
  1286. (void) ttm_bo_wait(bo, false, false);
  1287. }
  1288. }
  1289. /**
  1290. * vmw_query_readback_all - Read back cached query states
  1291. *
  1292. * @dx_query_mob: Buffer containing the DX query MOB
  1293. *
  1294. * Read back cached states from the device if they exist. This function
  1295. * assumings binding_mutex is held.
  1296. */
  1297. int vmw_query_readback_all(struct vmw_dma_buffer *dx_query_mob)
  1298. {
  1299. struct vmw_resource *dx_query_ctx;
  1300. struct vmw_private *dev_priv;
  1301. struct {
  1302. SVGA3dCmdHeader header;
  1303. SVGA3dCmdDXReadbackAllQuery body;
  1304. } *cmd;
  1305. /* No query bound, so do nothing */
  1306. if (!dx_query_mob || !dx_query_mob->dx_query_ctx)
  1307. return 0;
  1308. dx_query_ctx = dx_query_mob->dx_query_ctx;
  1309. dev_priv = dx_query_ctx->dev_priv;
  1310. cmd = vmw_fifo_reserve_dx(dev_priv, sizeof(*cmd), dx_query_ctx->id);
  1311. if (unlikely(cmd == NULL)) {
  1312. DRM_ERROR("Failed reserving FIFO space for "
  1313. "query MOB read back.\n");
  1314. return -ENOMEM;
  1315. }
  1316. cmd->header.id = SVGA_3D_CMD_DX_READBACK_ALL_QUERY;
  1317. cmd->header.size = sizeof(cmd->body);
  1318. cmd->body.cid = dx_query_ctx->id;
  1319. vmw_fifo_commit(dev_priv, sizeof(*cmd));
  1320. /* Triggers a rebind the next time affected context is bound */
  1321. dx_query_mob->dx_query_ctx = NULL;
  1322. return 0;
  1323. }
  1324. /**
  1325. * vmw_query_move_notify - Read back cached query states
  1326. *
  1327. * @bo: The TTM buffer object about to move.
  1328. * @mem: The memory region @bo is moving to.
  1329. *
  1330. * Called before the query MOB is swapped out to read back cached query
  1331. * states from the device.
  1332. */
  1333. void vmw_query_move_notify(struct ttm_buffer_object *bo,
  1334. struct ttm_mem_reg *mem)
  1335. {
  1336. struct vmw_dma_buffer *dx_query_mob;
  1337. struct ttm_bo_device *bdev = bo->bdev;
  1338. struct vmw_private *dev_priv;
  1339. dev_priv = container_of(bdev, struct vmw_private, bdev);
  1340. mutex_lock(&dev_priv->binding_mutex);
  1341. dx_query_mob = container_of(bo, struct vmw_dma_buffer, base);
  1342. if (mem == NULL || !dx_query_mob || !dx_query_mob->dx_query_ctx) {
  1343. mutex_unlock(&dev_priv->binding_mutex);
  1344. return;
  1345. }
  1346. /* If BO is being moved from MOB to system memory */
  1347. if (mem->mem_type == TTM_PL_SYSTEM && bo->mem.mem_type == VMW_PL_MOB) {
  1348. struct vmw_fence_obj *fence;
  1349. (void) vmw_query_readback_all(dx_query_mob);
  1350. mutex_unlock(&dev_priv->binding_mutex);
  1351. /* Create a fence and attach the BO to it */
  1352. (void) vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1353. vmw_fence_single_bo(bo, fence);
  1354. if (fence != NULL)
  1355. vmw_fence_obj_unreference(&fence);
  1356. (void) ttm_bo_wait(bo, false, false);
  1357. } else
  1358. mutex_unlock(&dev_priv->binding_mutex);
  1359. }
  1360. /**
  1361. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1362. *
  1363. * @res: The resource being queried.
  1364. */
  1365. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1366. {
  1367. return res->func->needs_backup;
  1368. }
  1369. /**
  1370. * vmw_resource_evict_type - Evict all resources of a specific type
  1371. *
  1372. * @dev_priv: Pointer to a device private struct
  1373. * @type: The resource type to evict
  1374. *
  1375. * To avoid thrashing starvation or as part of the hibernation sequence,
  1376. * try to evict all evictable resources of a specific type.
  1377. */
  1378. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1379. enum vmw_res_type type)
  1380. {
  1381. struct list_head *lru_list = &dev_priv->res_lru[type];
  1382. struct vmw_resource *evict_res;
  1383. unsigned err_count = 0;
  1384. int ret;
  1385. do {
  1386. write_lock(&dev_priv->resource_lock);
  1387. if (list_empty(lru_list))
  1388. goto out_unlock;
  1389. evict_res = vmw_resource_reference(
  1390. list_first_entry(lru_list, struct vmw_resource,
  1391. lru_head));
  1392. list_del_init(&evict_res->lru_head);
  1393. write_unlock(&dev_priv->resource_lock);
  1394. ret = vmw_resource_do_evict(evict_res, false);
  1395. if (unlikely(ret != 0)) {
  1396. write_lock(&dev_priv->resource_lock);
  1397. list_add_tail(&evict_res->lru_head, lru_list);
  1398. write_unlock(&dev_priv->resource_lock);
  1399. if (++err_count > VMW_RES_EVICT_ERR_COUNT) {
  1400. vmw_resource_unreference(&evict_res);
  1401. return;
  1402. }
  1403. }
  1404. vmw_resource_unreference(&evict_res);
  1405. } while (1);
  1406. out_unlock:
  1407. write_unlock(&dev_priv->resource_lock);
  1408. }
  1409. /**
  1410. * vmw_resource_evict_all - Evict all evictable resources
  1411. *
  1412. * @dev_priv: Pointer to a device private struct
  1413. *
  1414. * To avoid thrashing starvation or as part of the hibernation sequence,
  1415. * evict all evictable resources. In particular this means that all
  1416. * guest-backed resources that are registered with the device are
  1417. * evicted and the OTable becomes clean.
  1418. */
  1419. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1420. {
  1421. enum vmw_res_type type;
  1422. mutex_lock(&dev_priv->cmdbuf_mutex);
  1423. for (type = 0; type < vmw_res_max; ++type)
  1424. vmw_resource_evict_type(dev_priv, type);
  1425. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1426. }
  1427. /**
  1428. * vmw_resource_pin - Add a pin reference on a resource
  1429. *
  1430. * @res: The resource to add a pin reference on
  1431. *
  1432. * This function adds a pin reference, and if needed validates the resource.
  1433. * Having a pin reference means that the resource can never be evicted, and
  1434. * its id will never change as long as there is a pin reference.
  1435. * This function returns 0 on success and a negative error code on failure.
  1436. */
  1437. int vmw_resource_pin(struct vmw_resource *res, bool interruptible)
  1438. {
  1439. struct vmw_private *dev_priv = res->dev_priv;
  1440. int ret;
  1441. ttm_write_lock(&dev_priv->reservation_sem, interruptible);
  1442. mutex_lock(&dev_priv->cmdbuf_mutex);
  1443. ret = vmw_resource_reserve(res, interruptible, false);
  1444. if (ret)
  1445. goto out_no_reserve;
  1446. if (res->pin_count == 0) {
  1447. struct vmw_dma_buffer *vbo = NULL;
  1448. if (res->backup) {
  1449. vbo = res->backup;
  1450. ttm_bo_reserve(&vbo->base, interruptible, false, NULL);
  1451. if (!vbo->pin_count) {
  1452. ret = ttm_bo_validate
  1453. (&vbo->base,
  1454. res->func->backup_placement,
  1455. interruptible, false);
  1456. if (ret) {
  1457. ttm_bo_unreserve(&vbo->base);
  1458. goto out_no_validate;
  1459. }
  1460. }
  1461. /* Do we really need to pin the MOB as well? */
  1462. vmw_bo_pin_reserved(vbo, true);
  1463. }
  1464. ret = vmw_resource_validate(res);
  1465. if (vbo)
  1466. ttm_bo_unreserve(&vbo->base);
  1467. if (ret)
  1468. goto out_no_validate;
  1469. }
  1470. res->pin_count++;
  1471. out_no_validate:
  1472. vmw_resource_unreserve(res, false, NULL, 0UL);
  1473. out_no_reserve:
  1474. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1475. ttm_write_unlock(&dev_priv->reservation_sem);
  1476. return ret;
  1477. }
  1478. /**
  1479. * vmw_resource_unpin - Remove a pin reference from a resource
  1480. *
  1481. * @res: The resource to remove a pin reference from
  1482. *
  1483. * Having a pin reference means that the resource can never be evicted, and
  1484. * its id will never change as long as there is a pin reference.
  1485. */
  1486. void vmw_resource_unpin(struct vmw_resource *res)
  1487. {
  1488. struct vmw_private *dev_priv = res->dev_priv;
  1489. int ret;
  1490. ttm_read_lock(&dev_priv->reservation_sem, false);
  1491. mutex_lock(&dev_priv->cmdbuf_mutex);
  1492. ret = vmw_resource_reserve(res, false, true);
  1493. WARN_ON(ret);
  1494. WARN_ON(res->pin_count == 0);
  1495. if (--res->pin_count == 0 && res->backup) {
  1496. struct vmw_dma_buffer *vbo = res->backup;
  1497. ttm_bo_reserve(&vbo->base, false, false, NULL);
  1498. vmw_bo_pin_reserved(vbo, false);
  1499. ttm_bo_unreserve(&vbo->base);
  1500. }
  1501. vmw_resource_unreserve(res, false, NULL, 0UL);
  1502. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1503. ttm_read_unlock(&dev_priv->reservation_sem);
  1504. }
  1505. /**
  1506. * vmw_res_type - Return the resource type
  1507. *
  1508. * @res: Pointer to the resource
  1509. */
  1510. enum vmw_res_type vmw_res_type(const struct vmw_resource *res)
  1511. {
  1512. return res->func->res_type;
  1513. }