vmwgfx_resource.c 42 KB

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