vmwgfx_resource.c 44 KB

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