vmwgfx_resource.c 42 KB

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