vmwgfx_resource.c 47 KB

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