vmwgfx_resource.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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. struct vmw_user_dma_buffer {
  34. struct ttm_base_object base;
  35. struct vmw_dma_buffer dma;
  36. };
  37. struct vmw_bo_user_rep {
  38. uint32_t handle;
  39. uint64_t map_handle;
  40. };
  41. struct vmw_stream {
  42. struct vmw_resource res;
  43. uint32_t stream_id;
  44. };
  45. struct vmw_user_stream {
  46. struct ttm_base_object base;
  47. struct vmw_stream stream;
  48. };
  49. static uint64_t vmw_user_stream_size;
  50. static const struct vmw_res_func vmw_stream_func = {
  51. .res_type = vmw_res_stream,
  52. .needs_backup = false,
  53. .may_evict = false,
  54. .type_name = "video streams",
  55. .backup_placement = NULL,
  56. .create = NULL,
  57. .destroy = NULL,
  58. .bind = NULL,
  59. .unbind = NULL
  60. };
  61. static inline struct vmw_dma_buffer *
  62. vmw_dma_buffer(struct ttm_buffer_object *bo)
  63. {
  64. return container_of(bo, struct vmw_dma_buffer, base);
  65. }
  66. static inline struct vmw_user_dma_buffer *
  67. vmw_user_dma_buffer(struct ttm_buffer_object *bo)
  68. {
  69. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  70. return container_of(vmw_bo, struct vmw_user_dma_buffer, dma);
  71. }
  72. struct vmw_resource *vmw_resource_reference(struct vmw_resource *res)
  73. {
  74. kref_get(&res->kref);
  75. return res;
  76. }
  77. /**
  78. * vmw_resource_release_id - release a resource id to the id manager.
  79. *
  80. * @res: Pointer to the resource.
  81. *
  82. * Release the resource id to the resource id manager and set it to -1
  83. */
  84. void vmw_resource_release_id(struct vmw_resource *res)
  85. {
  86. struct vmw_private *dev_priv = res->dev_priv;
  87. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  88. write_lock(&dev_priv->resource_lock);
  89. if (res->id != -1)
  90. idr_remove(idr, res->id);
  91. res->id = -1;
  92. write_unlock(&dev_priv->resource_lock);
  93. }
  94. static void vmw_resource_release(struct kref *kref)
  95. {
  96. struct vmw_resource *res =
  97. container_of(kref, struct vmw_resource, kref);
  98. struct vmw_private *dev_priv = res->dev_priv;
  99. int id;
  100. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  101. res->avail = false;
  102. list_del_init(&res->lru_head);
  103. write_unlock(&dev_priv->resource_lock);
  104. if (res->backup) {
  105. struct ttm_buffer_object *bo = &res->backup->base;
  106. ttm_bo_reserve(bo, false, false, false, 0);
  107. if (!list_empty(&res->mob_head) &&
  108. res->func->unbind != NULL) {
  109. struct ttm_validate_buffer val_buf;
  110. val_buf.bo = bo;
  111. res->func->unbind(res, false, &val_buf);
  112. }
  113. res->backup_dirty = false;
  114. list_del_init(&res->mob_head);
  115. ttm_bo_unreserve(bo);
  116. vmw_dmabuf_unreference(&res->backup);
  117. }
  118. if (likely(res->hw_destroy != NULL))
  119. res->hw_destroy(res);
  120. id = res->id;
  121. if (res->res_free != NULL)
  122. res->res_free(res);
  123. else
  124. kfree(res);
  125. write_lock(&dev_priv->resource_lock);
  126. if (id != -1)
  127. idr_remove(idr, id);
  128. }
  129. void vmw_resource_unreference(struct vmw_resource **p_res)
  130. {
  131. struct vmw_resource *res = *p_res;
  132. struct vmw_private *dev_priv = res->dev_priv;
  133. *p_res = NULL;
  134. write_lock(&dev_priv->resource_lock);
  135. kref_put(&res->kref, vmw_resource_release);
  136. write_unlock(&dev_priv->resource_lock);
  137. }
  138. /**
  139. * vmw_resource_alloc_id - release a resource id to the id manager.
  140. *
  141. * @res: Pointer to the resource.
  142. *
  143. * Allocate the lowest free resource from the resource manager, and set
  144. * @res->id to that id. Returns 0 on success and -ENOMEM on failure.
  145. */
  146. int vmw_resource_alloc_id(struct vmw_resource *res)
  147. {
  148. struct vmw_private *dev_priv = res->dev_priv;
  149. int ret;
  150. struct idr *idr = &dev_priv->res_idr[res->func->res_type];
  151. BUG_ON(res->id != -1);
  152. idr_preload(GFP_KERNEL);
  153. write_lock(&dev_priv->resource_lock);
  154. ret = idr_alloc(idr, res, 1, 0, GFP_NOWAIT);
  155. if (ret >= 0)
  156. res->id = ret;
  157. write_unlock(&dev_priv->resource_lock);
  158. idr_preload_end();
  159. return ret < 0 ? ret : 0;
  160. }
  161. /**
  162. * vmw_resource_init - initialize a struct vmw_resource
  163. *
  164. * @dev_priv: Pointer to a device private struct.
  165. * @res: The struct vmw_resource to initialize.
  166. * @obj_type: Resource object type.
  167. * @delay_id: Boolean whether to defer device id allocation until
  168. * the first validation.
  169. * @res_free: Resource destructor.
  170. * @func: Resource function table.
  171. */
  172. int vmw_resource_init(struct vmw_private *dev_priv, struct vmw_resource *res,
  173. bool delay_id,
  174. void (*res_free) (struct vmw_resource *res),
  175. const struct vmw_res_func *func)
  176. {
  177. kref_init(&res->kref);
  178. res->hw_destroy = NULL;
  179. res->res_free = res_free;
  180. res->avail = false;
  181. res->dev_priv = dev_priv;
  182. res->func = func;
  183. INIT_LIST_HEAD(&res->lru_head);
  184. INIT_LIST_HEAD(&res->mob_head);
  185. res->id = -1;
  186. res->backup = NULL;
  187. res->backup_offset = 0;
  188. res->backup_dirty = false;
  189. res->res_dirty = false;
  190. if (delay_id)
  191. return 0;
  192. else
  193. return vmw_resource_alloc_id(res);
  194. }
  195. /**
  196. * vmw_resource_activate
  197. *
  198. * @res: Pointer to the newly created resource
  199. * @hw_destroy: Destroy function. NULL if none.
  200. *
  201. * Activate a resource after the hardware has been made aware of it.
  202. * Set tye destroy function to @destroy. Typically this frees the
  203. * resource and destroys the hardware resources associated with it.
  204. * Activate basically means that the function vmw_resource_lookup will
  205. * find it.
  206. */
  207. void vmw_resource_activate(struct vmw_resource *res,
  208. void (*hw_destroy) (struct vmw_resource *))
  209. {
  210. struct vmw_private *dev_priv = res->dev_priv;
  211. write_lock(&dev_priv->resource_lock);
  212. res->avail = true;
  213. res->hw_destroy = hw_destroy;
  214. write_unlock(&dev_priv->resource_lock);
  215. }
  216. struct vmw_resource *vmw_resource_lookup(struct vmw_private *dev_priv,
  217. struct idr *idr, int id)
  218. {
  219. struct vmw_resource *res;
  220. read_lock(&dev_priv->resource_lock);
  221. res = idr_find(idr, id);
  222. if (res && res->avail)
  223. kref_get(&res->kref);
  224. else
  225. res = NULL;
  226. read_unlock(&dev_priv->resource_lock);
  227. if (unlikely(res == NULL))
  228. return NULL;
  229. return res;
  230. }
  231. /**
  232. * vmw_user_resource_lookup_handle - lookup a struct resource from a
  233. * TTM user-space handle and perform basic type checks
  234. *
  235. * @dev_priv: Pointer to a device private struct
  236. * @tfile: Pointer to a struct ttm_object_file identifying the caller
  237. * @handle: The TTM user-space handle
  238. * @converter: Pointer to an object describing the resource type
  239. * @p_res: On successful return the location pointed to will contain
  240. * a pointer to a refcounted struct vmw_resource.
  241. *
  242. * If the handle can't be found or is associated with an incorrect resource
  243. * type, -EINVAL will be returned.
  244. */
  245. int vmw_user_resource_lookup_handle(struct vmw_private *dev_priv,
  246. struct ttm_object_file *tfile,
  247. uint32_t handle,
  248. const struct vmw_user_resource_conv
  249. *converter,
  250. struct vmw_resource **p_res)
  251. {
  252. struct ttm_base_object *base;
  253. struct vmw_resource *res;
  254. int ret = -EINVAL;
  255. base = ttm_base_object_lookup(tfile, handle);
  256. if (unlikely(base == NULL))
  257. return -EINVAL;
  258. if (unlikely(base->object_type != converter->object_type))
  259. goto out_bad_resource;
  260. res = converter->base_obj_to_res(base);
  261. read_lock(&dev_priv->resource_lock);
  262. if (!res->avail || res->res_free != converter->res_free) {
  263. read_unlock(&dev_priv->resource_lock);
  264. goto out_bad_resource;
  265. }
  266. kref_get(&res->kref);
  267. read_unlock(&dev_priv->resource_lock);
  268. *p_res = res;
  269. ret = 0;
  270. out_bad_resource:
  271. ttm_base_object_unref(&base);
  272. return ret;
  273. }
  274. /**
  275. * Helper function that looks either a surface or dmabuf.
  276. *
  277. * The pointer this pointed at by out_surf and out_buf needs to be null.
  278. */
  279. int vmw_user_lookup_handle(struct vmw_private *dev_priv,
  280. struct ttm_object_file *tfile,
  281. uint32_t handle,
  282. struct vmw_surface **out_surf,
  283. struct vmw_dma_buffer **out_buf)
  284. {
  285. struct vmw_resource *res;
  286. int ret;
  287. BUG_ON(*out_surf || *out_buf);
  288. ret = vmw_user_resource_lookup_handle(dev_priv, tfile, handle,
  289. user_surface_converter,
  290. &res);
  291. if (!ret) {
  292. *out_surf = vmw_res_to_srf(res);
  293. return 0;
  294. }
  295. *out_surf = NULL;
  296. ret = vmw_user_dmabuf_lookup(tfile, handle, out_buf);
  297. return ret;
  298. }
  299. /**
  300. * Buffer management.
  301. */
  302. void vmw_dmabuf_bo_free(struct ttm_buffer_object *bo)
  303. {
  304. struct vmw_dma_buffer *vmw_bo = vmw_dma_buffer(bo);
  305. kfree(vmw_bo);
  306. }
  307. int vmw_dmabuf_init(struct vmw_private *dev_priv,
  308. struct vmw_dma_buffer *vmw_bo,
  309. size_t size, struct ttm_placement *placement,
  310. bool interruptible,
  311. void (*bo_free) (struct ttm_buffer_object *bo))
  312. {
  313. struct ttm_bo_device *bdev = &dev_priv->bdev;
  314. size_t acc_size;
  315. int ret;
  316. BUG_ON(!bo_free);
  317. acc_size = ttm_bo_acc_size(bdev, size, sizeof(struct vmw_dma_buffer));
  318. memset(vmw_bo, 0, sizeof(*vmw_bo));
  319. INIT_LIST_HEAD(&vmw_bo->res_list);
  320. ret = ttm_bo_init(bdev, &vmw_bo->base, size,
  321. ttm_bo_type_device, placement,
  322. 0, interruptible,
  323. NULL, acc_size, NULL, bo_free);
  324. return ret;
  325. }
  326. static void vmw_user_dmabuf_destroy(struct ttm_buffer_object *bo)
  327. {
  328. struct vmw_user_dma_buffer *vmw_user_bo = vmw_user_dma_buffer(bo);
  329. ttm_base_object_kfree(vmw_user_bo, base);
  330. }
  331. static void vmw_user_dmabuf_release(struct ttm_base_object **p_base)
  332. {
  333. struct vmw_user_dma_buffer *vmw_user_bo;
  334. struct ttm_base_object *base = *p_base;
  335. struct ttm_buffer_object *bo;
  336. *p_base = NULL;
  337. if (unlikely(base == NULL))
  338. return;
  339. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  340. bo = &vmw_user_bo->dma.base;
  341. ttm_bo_unref(&bo);
  342. }
  343. /**
  344. * vmw_user_dmabuf_alloc - Allocate a user dma buffer
  345. *
  346. * @dev_priv: Pointer to a struct device private.
  347. * @tfile: Pointer to a struct ttm_object_file on which to register the user
  348. * object.
  349. * @size: Size of the dma buffer.
  350. * @shareable: Boolean whether the buffer is shareable with other open files.
  351. * @handle: Pointer to where the handle value should be assigned.
  352. * @p_dma_buf: Pointer to where the refcounted struct vmw_dma_buffer pointer
  353. * should be assigned.
  354. */
  355. int vmw_user_dmabuf_alloc(struct vmw_private *dev_priv,
  356. struct ttm_object_file *tfile,
  357. uint32_t size,
  358. bool shareable,
  359. uint32_t *handle,
  360. struct vmw_dma_buffer **p_dma_buf)
  361. {
  362. struct vmw_user_dma_buffer *user_bo;
  363. struct ttm_buffer_object *tmp;
  364. int ret;
  365. user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
  366. if (unlikely(user_bo == NULL)) {
  367. DRM_ERROR("Failed to allocate a buffer.\n");
  368. return -ENOMEM;
  369. }
  370. ret = vmw_dmabuf_init(dev_priv, &user_bo->dma, size,
  371. &vmw_vram_sys_placement, true,
  372. &vmw_user_dmabuf_destroy);
  373. if (unlikely(ret != 0))
  374. return ret;
  375. tmp = ttm_bo_reference(&user_bo->dma.base);
  376. ret = ttm_base_object_init(tfile,
  377. &user_bo->base,
  378. shareable,
  379. ttm_buffer_type,
  380. &vmw_user_dmabuf_release, NULL);
  381. if (unlikely(ret != 0)) {
  382. ttm_bo_unref(&tmp);
  383. goto out_no_base_object;
  384. }
  385. *p_dma_buf = &user_bo->dma;
  386. *handle = user_bo->base.hash.key;
  387. out_no_base_object:
  388. return ret;
  389. }
  390. /**
  391. * vmw_user_dmabuf_verify_access - verify access permissions on this
  392. * buffer object.
  393. *
  394. * @bo: Pointer to the buffer object being accessed
  395. * @tfile: Identifying the caller.
  396. */
  397. int vmw_user_dmabuf_verify_access(struct ttm_buffer_object *bo,
  398. struct ttm_object_file *tfile)
  399. {
  400. struct vmw_user_dma_buffer *vmw_user_bo;
  401. if (unlikely(bo->destroy != vmw_user_dmabuf_destroy))
  402. return -EPERM;
  403. vmw_user_bo = vmw_user_dma_buffer(bo);
  404. return (vmw_user_bo->base.tfile == tfile ||
  405. vmw_user_bo->base.shareable) ? 0 : -EPERM;
  406. }
  407. int vmw_dmabuf_alloc_ioctl(struct drm_device *dev, void *data,
  408. struct drm_file *file_priv)
  409. {
  410. struct vmw_private *dev_priv = vmw_priv(dev);
  411. union drm_vmw_alloc_dmabuf_arg *arg =
  412. (union drm_vmw_alloc_dmabuf_arg *)data;
  413. struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
  414. struct drm_vmw_dmabuf_rep *rep = &arg->rep;
  415. struct vmw_dma_buffer *dma_buf;
  416. uint32_t handle;
  417. struct vmw_master *vmaster = vmw_master(file_priv->master);
  418. int ret;
  419. ret = ttm_read_lock(&vmaster->lock, true);
  420. if (unlikely(ret != 0))
  421. return ret;
  422. ret = vmw_user_dmabuf_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
  423. req->size, false, &handle, &dma_buf);
  424. if (unlikely(ret != 0))
  425. goto out_no_dmabuf;
  426. rep->handle = handle;
  427. rep->map_handle = dma_buf->base.addr_space_offset;
  428. rep->cur_gmr_id = handle;
  429. rep->cur_gmr_offset = 0;
  430. vmw_dmabuf_unreference(&dma_buf);
  431. out_no_dmabuf:
  432. ttm_read_unlock(&vmaster->lock);
  433. return ret;
  434. }
  435. int vmw_dmabuf_unref_ioctl(struct drm_device *dev, void *data,
  436. struct drm_file *file_priv)
  437. {
  438. struct drm_vmw_unref_dmabuf_arg *arg =
  439. (struct drm_vmw_unref_dmabuf_arg *)data;
  440. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  441. arg->handle,
  442. TTM_REF_USAGE);
  443. }
  444. int vmw_user_dmabuf_lookup(struct ttm_object_file *tfile,
  445. uint32_t handle, struct vmw_dma_buffer **out)
  446. {
  447. struct vmw_user_dma_buffer *vmw_user_bo;
  448. struct ttm_base_object *base;
  449. base = ttm_base_object_lookup(tfile, handle);
  450. if (unlikely(base == NULL)) {
  451. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  452. (unsigned long)handle);
  453. return -ESRCH;
  454. }
  455. if (unlikely(base->object_type != ttm_buffer_type)) {
  456. ttm_base_object_unref(&base);
  457. printk(KERN_ERR "Invalid buffer object handle 0x%08lx.\n",
  458. (unsigned long)handle);
  459. return -EINVAL;
  460. }
  461. vmw_user_bo = container_of(base, struct vmw_user_dma_buffer, base);
  462. (void)ttm_bo_reference(&vmw_user_bo->dma.base);
  463. ttm_base_object_unref(&base);
  464. *out = &vmw_user_bo->dma;
  465. return 0;
  466. }
  467. int vmw_user_dmabuf_reference(struct ttm_object_file *tfile,
  468. struct vmw_dma_buffer *dma_buf)
  469. {
  470. struct vmw_user_dma_buffer *user_bo;
  471. if (dma_buf->base.destroy != vmw_user_dmabuf_destroy)
  472. return -EINVAL;
  473. user_bo = container_of(dma_buf, struct vmw_user_dma_buffer, dma);
  474. return ttm_ref_object_add(tfile, &user_bo->base, TTM_REF_USAGE, NULL);
  475. }
  476. /*
  477. * Stream management
  478. */
  479. static void vmw_stream_destroy(struct vmw_resource *res)
  480. {
  481. struct vmw_private *dev_priv = res->dev_priv;
  482. struct vmw_stream *stream;
  483. int ret;
  484. DRM_INFO("%s: unref\n", __func__);
  485. stream = container_of(res, struct vmw_stream, res);
  486. ret = vmw_overlay_unref(dev_priv, stream->stream_id);
  487. WARN_ON(ret != 0);
  488. }
  489. static int vmw_stream_init(struct vmw_private *dev_priv,
  490. struct vmw_stream *stream,
  491. void (*res_free) (struct vmw_resource *res))
  492. {
  493. struct vmw_resource *res = &stream->res;
  494. int ret;
  495. ret = vmw_resource_init(dev_priv, res, false, res_free,
  496. &vmw_stream_func);
  497. if (unlikely(ret != 0)) {
  498. if (res_free == NULL)
  499. kfree(stream);
  500. else
  501. res_free(&stream->res);
  502. return ret;
  503. }
  504. ret = vmw_overlay_claim(dev_priv, &stream->stream_id);
  505. if (ret) {
  506. vmw_resource_unreference(&res);
  507. return ret;
  508. }
  509. DRM_INFO("%s: claimed\n", __func__);
  510. vmw_resource_activate(&stream->res, vmw_stream_destroy);
  511. return 0;
  512. }
  513. static void vmw_user_stream_free(struct vmw_resource *res)
  514. {
  515. struct vmw_user_stream *stream =
  516. container_of(res, struct vmw_user_stream, stream.res);
  517. struct vmw_private *dev_priv = res->dev_priv;
  518. ttm_base_object_kfree(stream, base);
  519. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  520. vmw_user_stream_size);
  521. }
  522. /**
  523. * This function is called when user space has no more references on the
  524. * base object. It releases the base-object's reference on the resource object.
  525. */
  526. static void vmw_user_stream_base_release(struct ttm_base_object **p_base)
  527. {
  528. struct ttm_base_object *base = *p_base;
  529. struct vmw_user_stream *stream =
  530. container_of(base, struct vmw_user_stream, base);
  531. struct vmw_resource *res = &stream->stream.res;
  532. *p_base = NULL;
  533. vmw_resource_unreference(&res);
  534. }
  535. int vmw_stream_unref_ioctl(struct drm_device *dev, void *data,
  536. struct drm_file *file_priv)
  537. {
  538. struct vmw_private *dev_priv = vmw_priv(dev);
  539. struct vmw_resource *res;
  540. struct vmw_user_stream *stream;
  541. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  542. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  543. struct idr *idr = &dev_priv->res_idr[vmw_res_stream];
  544. int ret = 0;
  545. res = vmw_resource_lookup(dev_priv, idr, arg->stream_id);
  546. if (unlikely(res == NULL))
  547. return -EINVAL;
  548. if (res->res_free != &vmw_user_stream_free) {
  549. ret = -EINVAL;
  550. goto out;
  551. }
  552. stream = container_of(res, struct vmw_user_stream, stream.res);
  553. if (stream->base.tfile != tfile) {
  554. ret = -EINVAL;
  555. goto out;
  556. }
  557. ttm_ref_object_base_unref(tfile, stream->base.hash.key, TTM_REF_USAGE);
  558. out:
  559. vmw_resource_unreference(&res);
  560. return ret;
  561. }
  562. int vmw_stream_claim_ioctl(struct drm_device *dev, void *data,
  563. struct drm_file *file_priv)
  564. {
  565. struct vmw_private *dev_priv = vmw_priv(dev);
  566. struct vmw_user_stream *stream;
  567. struct vmw_resource *res;
  568. struct vmw_resource *tmp;
  569. struct drm_vmw_stream_arg *arg = (struct drm_vmw_stream_arg *)data;
  570. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  571. struct vmw_master *vmaster = vmw_master(file_priv->master);
  572. int ret;
  573. /*
  574. * Approximate idr memory usage with 128 bytes. It will be limited
  575. * by maximum number_of streams anyway?
  576. */
  577. if (unlikely(vmw_user_stream_size == 0))
  578. vmw_user_stream_size = ttm_round_pot(sizeof(*stream)) + 128;
  579. ret = ttm_read_lock(&vmaster->lock, true);
  580. if (unlikely(ret != 0))
  581. return ret;
  582. ret = ttm_mem_global_alloc(vmw_mem_glob(dev_priv),
  583. vmw_user_stream_size,
  584. false, true);
  585. if (unlikely(ret != 0)) {
  586. if (ret != -ERESTARTSYS)
  587. DRM_ERROR("Out of graphics memory for stream"
  588. " creation.\n");
  589. goto out_unlock;
  590. }
  591. stream = kmalloc(sizeof(*stream), GFP_KERNEL);
  592. if (unlikely(stream == NULL)) {
  593. ttm_mem_global_free(vmw_mem_glob(dev_priv),
  594. vmw_user_stream_size);
  595. ret = -ENOMEM;
  596. goto out_unlock;
  597. }
  598. res = &stream->stream.res;
  599. stream->base.shareable = false;
  600. stream->base.tfile = NULL;
  601. /*
  602. * From here on, the destructor takes over resource freeing.
  603. */
  604. ret = vmw_stream_init(dev_priv, &stream->stream, vmw_user_stream_free);
  605. if (unlikely(ret != 0))
  606. goto out_unlock;
  607. tmp = vmw_resource_reference(res);
  608. ret = ttm_base_object_init(tfile, &stream->base, false, VMW_RES_STREAM,
  609. &vmw_user_stream_base_release, NULL);
  610. if (unlikely(ret != 0)) {
  611. vmw_resource_unreference(&tmp);
  612. goto out_err;
  613. }
  614. arg->stream_id = res->id;
  615. out_err:
  616. vmw_resource_unreference(&res);
  617. out_unlock:
  618. ttm_read_unlock(&vmaster->lock);
  619. return ret;
  620. }
  621. int vmw_user_stream_lookup(struct vmw_private *dev_priv,
  622. struct ttm_object_file *tfile,
  623. uint32_t *inout_id, struct vmw_resource **out)
  624. {
  625. struct vmw_user_stream *stream;
  626. struct vmw_resource *res;
  627. int ret;
  628. res = vmw_resource_lookup(dev_priv, &dev_priv->res_idr[vmw_res_stream],
  629. *inout_id);
  630. if (unlikely(res == NULL))
  631. return -EINVAL;
  632. if (res->res_free != &vmw_user_stream_free) {
  633. ret = -EINVAL;
  634. goto err_ref;
  635. }
  636. stream = container_of(res, struct vmw_user_stream, stream.res);
  637. if (stream->base.tfile != tfile) {
  638. ret = -EPERM;
  639. goto err_ref;
  640. }
  641. *inout_id = stream->stream.stream_id;
  642. *out = res;
  643. return 0;
  644. err_ref:
  645. vmw_resource_unreference(&res);
  646. return ret;
  647. }
  648. int vmw_dumb_create(struct drm_file *file_priv,
  649. struct drm_device *dev,
  650. struct drm_mode_create_dumb *args)
  651. {
  652. struct vmw_private *dev_priv = vmw_priv(dev);
  653. struct vmw_master *vmaster = vmw_master(file_priv->master);
  654. struct vmw_user_dma_buffer *vmw_user_bo;
  655. struct ttm_buffer_object *tmp;
  656. int ret;
  657. args->pitch = args->width * ((args->bpp + 7) / 8);
  658. args->size = args->pitch * args->height;
  659. vmw_user_bo = kzalloc(sizeof(*vmw_user_bo), GFP_KERNEL);
  660. if (vmw_user_bo == NULL)
  661. return -ENOMEM;
  662. ret = ttm_read_lock(&vmaster->lock, true);
  663. if (ret != 0) {
  664. kfree(vmw_user_bo);
  665. return ret;
  666. }
  667. ret = vmw_dmabuf_init(dev_priv, &vmw_user_bo->dma, args->size,
  668. &vmw_vram_sys_placement, true,
  669. &vmw_user_dmabuf_destroy);
  670. if (ret != 0)
  671. goto out_no_dmabuf;
  672. tmp = ttm_bo_reference(&vmw_user_bo->dma.base);
  673. ret = ttm_base_object_init(vmw_fpriv(file_priv)->tfile,
  674. &vmw_user_bo->base,
  675. false,
  676. ttm_buffer_type,
  677. &vmw_user_dmabuf_release, NULL);
  678. if (unlikely(ret != 0))
  679. goto out_no_base_object;
  680. args->handle = vmw_user_bo->base.hash.key;
  681. out_no_base_object:
  682. ttm_bo_unref(&tmp);
  683. out_no_dmabuf:
  684. ttm_read_unlock(&vmaster->lock);
  685. return ret;
  686. }
  687. int vmw_dumb_map_offset(struct drm_file *file_priv,
  688. struct drm_device *dev, uint32_t handle,
  689. uint64_t *offset)
  690. {
  691. struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
  692. struct vmw_dma_buffer *out_buf;
  693. int ret;
  694. ret = vmw_user_dmabuf_lookup(tfile, handle, &out_buf);
  695. if (ret != 0)
  696. return -EINVAL;
  697. *offset = out_buf->base.addr_space_offset;
  698. vmw_dmabuf_unreference(&out_buf);
  699. return 0;
  700. }
  701. int vmw_dumb_destroy(struct drm_file *file_priv,
  702. struct drm_device *dev,
  703. uint32_t handle)
  704. {
  705. return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
  706. handle, TTM_REF_USAGE);
  707. }
  708. /**
  709. * vmw_resource_buf_alloc - Allocate a backup buffer for a resource.
  710. *
  711. * @res: The resource for which to allocate a backup buffer.
  712. * @interruptible: Whether any sleeps during allocation should be
  713. * performed while interruptible.
  714. */
  715. static int vmw_resource_buf_alloc(struct vmw_resource *res,
  716. bool interruptible)
  717. {
  718. unsigned long size =
  719. (res->backup_size + PAGE_SIZE - 1) & PAGE_MASK;
  720. struct vmw_dma_buffer *backup;
  721. int ret;
  722. if (likely(res->backup)) {
  723. BUG_ON(res->backup->base.num_pages * PAGE_SIZE < size);
  724. return 0;
  725. }
  726. backup = kzalloc(sizeof(*backup), GFP_KERNEL);
  727. if (unlikely(backup == NULL))
  728. return -ENOMEM;
  729. ret = vmw_dmabuf_init(res->dev_priv, backup, res->backup_size,
  730. res->func->backup_placement,
  731. interruptible,
  732. &vmw_dmabuf_bo_free);
  733. if (unlikely(ret != 0))
  734. goto out_no_dmabuf;
  735. res->backup = backup;
  736. out_no_dmabuf:
  737. return ret;
  738. }
  739. /**
  740. * vmw_resource_do_validate - Make a resource up-to-date and visible
  741. * to the device.
  742. *
  743. * @res: The resource to make visible to the device.
  744. * @val_buf: Information about a buffer possibly
  745. * containing backup data if a bind operation is needed.
  746. *
  747. * On hardware resource shortage, this function returns -EBUSY and
  748. * should be retried once resources have been freed up.
  749. */
  750. static int vmw_resource_do_validate(struct vmw_resource *res,
  751. struct ttm_validate_buffer *val_buf)
  752. {
  753. int ret = 0;
  754. const struct vmw_res_func *func = res->func;
  755. if (unlikely(res->id == -1)) {
  756. ret = func->create(res);
  757. if (unlikely(ret != 0))
  758. return ret;
  759. }
  760. if (func->bind &&
  761. ((func->needs_backup && list_empty(&res->mob_head) &&
  762. val_buf->bo != NULL) ||
  763. (!func->needs_backup && val_buf->bo != NULL))) {
  764. ret = func->bind(res, val_buf);
  765. if (unlikely(ret != 0))
  766. goto out_bind_failed;
  767. if (func->needs_backup)
  768. list_add_tail(&res->mob_head, &res->backup->res_list);
  769. }
  770. /*
  771. * Only do this on write operations, and move to
  772. * vmw_resource_unreserve if it can be called after
  773. * backup buffers have been unreserved. Otherwise
  774. * sort out locking.
  775. */
  776. res->res_dirty = true;
  777. return 0;
  778. out_bind_failed:
  779. func->destroy(res);
  780. return ret;
  781. }
  782. /**
  783. * vmw_resource_unreserve - Unreserve a resource previously reserved for
  784. * command submission.
  785. *
  786. * @res: Pointer to the struct vmw_resource to unreserve.
  787. * @new_backup: Pointer to new backup buffer if command submission
  788. * switched.
  789. * @new_backup_offset: New backup offset if @new_backup is !NULL.
  790. *
  791. * Currently unreserving a resource means putting it back on the device's
  792. * resource lru list, so that it can be evicted if necessary.
  793. */
  794. void vmw_resource_unreserve(struct vmw_resource *res,
  795. struct vmw_dma_buffer *new_backup,
  796. unsigned long new_backup_offset)
  797. {
  798. struct vmw_private *dev_priv = res->dev_priv;
  799. if (!list_empty(&res->lru_head))
  800. return;
  801. if (new_backup && new_backup != res->backup) {
  802. if (res->backup) {
  803. BUG_ON(!ttm_bo_is_reserved(&res->backup->base));
  804. list_del_init(&res->mob_head);
  805. vmw_dmabuf_unreference(&res->backup);
  806. }
  807. res->backup = vmw_dmabuf_reference(new_backup);
  808. BUG_ON(!ttm_bo_is_reserved(&new_backup->base));
  809. list_add_tail(&res->mob_head, &new_backup->res_list);
  810. }
  811. if (new_backup)
  812. res->backup_offset = new_backup_offset;
  813. if (!res->func->may_evict)
  814. return;
  815. write_lock(&dev_priv->resource_lock);
  816. list_add_tail(&res->lru_head,
  817. &res->dev_priv->res_lru[res->func->res_type]);
  818. write_unlock(&dev_priv->resource_lock);
  819. }
  820. /**
  821. * vmw_resource_check_buffer - Check whether a backup buffer is needed
  822. * for a resource and in that case, allocate
  823. * one, reserve and validate it.
  824. *
  825. * @res: The resource for which to allocate a backup buffer.
  826. * @interruptible: Whether any sleeps during allocation should be
  827. * performed while interruptible.
  828. * @val_buf: On successful return contains data about the
  829. * reserved and validated backup buffer.
  830. */
  831. int vmw_resource_check_buffer(struct vmw_resource *res,
  832. bool interruptible,
  833. struct ttm_validate_buffer *val_buf)
  834. {
  835. struct list_head val_list;
  836. bool backup_dirty = false;
  837. int ret;
  838. if (unlikely(res->backup == NULL)) {
  839. ret = vmw_resource_buf_alloc(res, interruptible);
  840. if (unlikely(ret != 0))
  841. return ret;
  842. }
  843. INIT_LIST_HEAD(&val_list);
  844. val_buf->bo = ttm_bo_reference(&res->backup->base);
  845. list_add_tail(&val_buf->head, &val_list);
  846. ret = ttm_eu_reserve_buffers(&val_list);
  847. if (unlikely(ret != 0))
  848. goto out_no_reserve;
  849. if (res->func->needs_backup && list_empty(&res->mob_head))
  850. return 0;
  851. backup_dirty = res->backup_dirty;
  852. ret = ttm_bo_validate(&res->backup->base,
  853. res->func->backup_placement,
  854. true, false);
  855. if (unlikely(ret != 0))
  856. goto out_no_validate;
  857. return 0;
  858. out_no_validate:
  859. ttm_eu_backoff_reservation(&val_list);
  860. out_no_reserve:
  861. ttm_bo_unref(&val_buf->bo);
  862. if (backup_dirty)
  863. vmw_dmabuf_unreference(&res->backup);
  864. return ret;
  865. }
  866. /**
  867. * vmw_resource_reserve - Reserve a resource for command submission
  868. *
  869. * @res: The resource to reserve.
  870. *
  871. * This function takes the resource off the LRU list and make sure
  872. * a backup buffer is present for guest-backed resources. However,
  873. * the buffer may not be bound to the resource at this point.
  874. *
  875. */
  876. int vmw_resource_reserve(struct vmw_resource *res, bool no_backup)
  877. {
  878. struct vmw_private *dev_priv = res->dev_priv;
  879. int ret;
  880. write_lock(&dev_priv->resource_lock);
  881. list_del_init(&res->lru_head);
  882. write_unlock(&dev_priv->resource_lock);
  883. if (res->func->needs_backup && res->backup == NULL &&
  884. !no_backup) {
  885. ret = vmw_resource_buf_alloc(res, true);
  886. if (unlikely(ret != 0))
  887. return ret;
  888. }
  889. return 0;
  890. }
  891. /**
  892. * vmw_resource_backoff_reservation - Unreserve and unreference a
  893. * backup buffer
  894. *.
  895. * @val_buf: Backup buffer information.
  896. */
  897. void vmw_resource_backoff_reservation(struct ttm_validate_buffer *val_buf)
  898. {
  899. struct list_head val_list;
  900. if (likely(val_buf->bo == NULL))
  901. return;
  902. INIT_LIST_HEAD(&val_list);
  903. list_add_tail(&val_buf->head, &val_list);
  904. ttm_eu_backoff_reservation(&val_list);
  905. ttm_bo_unref(&val_buf->bo);
  906. }
  907. /**
  908. * vmw_resource_do_evict - Evict a resource, and transfer its data
  909. * to a backup buffer.
  910. *
  911. * @res: The resource to evict.
  912. */
  913. int vmw_resource_do_evict(struct vmw_resource *res)
  914. {
  915. struct ttm_validate_buffer val_buf;
  916. const struct vmw_res_func *func = res->func;
  917. int ret;
  918. BUG_ON(!func->may_evict);
  919. val_buf.bo = NULL;
  920. ret = vmw_resource_check_buffer(res, true, &val_buf);
  921. if (unlikely(ret != 0))
  922. return ret;
  923. if (unlikely(func->unbind != NULL &&
  924. (!func->needs_backup || !list_empty(&res->mob_head)))) {
  925. ret = func->unbind(res, res->res_dirty, &val_buf);
  926. if (unlikely(ret != 0))
  927. goto out_no_unbind;
  928. list_del_init(&res->mob_head);
  929. }
  930. ret = func->destroy(res);
  931. res->backup_dirty = true;
  932. res->res_dirty = false;
  933. out_no_unbind:
  934. vmw_resource_backoff_reservation(&val_buf);
  935. return ret;
  936. }
  937. /**
  938. * vmw_resource_validate - Make a resource up-to-date and visible
  939. * to the device.
  940. *
  941. * @res: The resource to make visible to the device.
  942. *
  943. * On succesful return, any backup DMA buffer pointed to by @res->backup will
  944. * be reserved and validated.
  945. * On hardware resource shortage, this function will repeatedly evict
  946. * resources of the same type until the validation succeeds.
  947. */
  948. int vmw_resource_validate(struct vmw_resource *res)
  949. {
  950. int ret;
  951. struct vmw_resource *evict_res;
  952. struct vmw_private *dev_priv = res->dev_priv;
  953. struct list_head *lru_list = &dev_priv->res_lru[res->func->res_type];
  954. struct ttm_validate_buffer val_buf;
  955. if (likely(!res->func->may_evict))
  956. return 0;
  957. val_buf.bo = NULL;
  958. if (res->backup)
  959. val_buf.bo = &res->backup->base;
  960. do {
  961. ret = vmw_resource_do_validate(res, &val_buf);
  962. if (likely(ret != -EBUSY))
  963. break;
  964. write_lock(&dev_priv->resource_lock);
  965. if (list_empty(lru_list) || !res->func->may_evict) {
  966. DRM_ERROR("Out of device device id entries "
  967. "for %s.\n", res->func->type_name);
  968. ret = -EBUSY;
  969. write_unlock(&dev_priv->resource_lock);
  970. break;
  971. }
  972. evict_res = vmw_resource_reference
  973. (list_first_entry(lru_list, struct vmw_resource,
  974. lru_head));
  975. list_del_init(&evict_res->lru_head);
  976. write_unlock(&dev_priv->resource_lock);
  977. vmw_resource_do_evict(evict_res);
  978. vmw_resource_unreference(&evict_res);
  979. } while (1);
  980. if (unlikely(ret != 0))
  981. goto out_no_validate;
  982. else if (!res->func->needs_backup && res->backup) {
  983. list_del_init(&res->mob_head);
  984. vmw_dmabuf_unreference(&res->backup);
  985. }
  986. return 0;
  987. out_no_validate:
  988. return ret;
  989. }
  990. /**
  991. * vmw_fence_single_bo - Utility function to fence a single TTM buffer
  992. * object without unreserving it.
  993. *
  994. * @bo: Pointer to the struct ttm_buffer_object to fence.
  995. * @fence: Pointer to the fence. If NULL, this function will
  996. * insert a fence into the command stream..
  997. *
  998. * Contrary to the ttm_eu version of this function, it takes only
  999. * a single buffer object instead of a list, and it also doesn't
  1000. * unreserve the buffer object, which needs to be done separately.
  1001. */
  1002. void vmw_fence_single_bo(struct ttm_buffer_object *bo,
  1003. struct vmw_fence_obj *fence)
  1004. {
  1005. struct ttm_bo_device *bdev = bo->bdev;
  1006. struct ttm_bo_driver *driver = bdev->driver;
  1007. struct vmw_fence_obj *old_fence_obj;
  1008. struct vmw_private *dev_priv =
  1009. container_of(bdev, struct vmw_private, bdev);
  1010. if (fence == NULL)
  1011. vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
  1012. else
  1013. driver->sync_obj_ref(fence);
  1014. spin_lock(&bdev->fence_lock);
  1015. old_fence_obj = bo->sync_obj;
  1016. bo->sync_obj = fence;
  1017. spin_unlock(&bdev->fence_lock);
  1018. if (old_fence_obj)
  1019. vmw_fence_obj_unreference(&old_fence_obj);
  1020. }
  1021. /**
  1022. * vmw_resource_move_notify - TTM move_notify_callback
  1023. *
  1024. * @bo: The TTM buffer object about to move.
  1025. * @mem: The truct ttm_mem_reg indicating to what memory
  1026. * region the move is taking place.
  1027. *
  1028. * For now does nothing.
  1029. */
  1030. void vmw_resource_move_notify(struct ttm_buffer_object *bo,
  1031. struct ttm_mem_reg *mem)
  1032. {
  1033. }
  1034. /**
  1035. * vmw_resource_needs_backup - Return whether a resource needs a backup buffer.
  1036. *
  1037. * @res: The resource being queried.
  1038. */
  1039. bool vmw_resource_needs_backup(const struct vmw_resource *res)
  1040. {
  1041. return res->func->needs_backup;
  1042. }
  1043. /**
  1044. * vmw_resource_evict_type - Evict all resources of a specific type
  1045. *
  1046. * @dev_priv: Pointer to a device private struct
  1047. * @type: The resource type to evict
  1048. *
  1049. * To avoid thrashing starvation or as part of the hibernation sequence,
  1050. * evict all evictable resources of a specific type.
  1051. */
  1052. static void vmw_resource_evict_type(struct vmw_private *dev_priv,
  1053. enum vmw_res_type type)
  1054. {
  1055. struct list_head *lru_list = &dev_priv->res_lru[type];
  1056. struct vmw_resource *evict_res;
  1057. do {
  1058. write_lock(&dev_priv->resource_lock);
  1059. if (list_empty(lru_list))
  1060. goto out_unlock;
  1061. evict_res = vmw_resource_reference(
  1062. list_first_entry(lru_list, struct vmw_resource,
  1063. lru_head));
  1064. list_del_init(&evict_res->lru_head);
  1065. write_unlock(&dev_priv->resource_lock);
  1066. vmw_resource_do_evict(evict_res);
  1067. vmw_resource_unreference(&evict_res);
  1068. } while (1);
  1069. out_unlock:
  1070. write_unlock(&dev_priv->resource_lock);
  1071. }
  1072. /**
  1073. * vmw_resource_evict_all - Evict all evictable resources
  1074. *
  1075. * @dev_priv: Pointer to a device private struct
  1076. *
  1077. * To avoid thrashing starvation or as part of the hibernation sequence,
  1078. * evict all evictable resources. In particular this means that all
  1079. * guest-backed resources that are registered with the device are
  1080. * evicted and the OTable becomes clean.
  1081. */
  1082. void vmw_resource_evict_all(struct vmw_private *dev_priv)
  1083. {
  1084. enum vmw_res_type type;
  1085. mutex_lock(&dev_priv->cmdbuf_mutex);
  1086. for (type = 0; type < vmw_res_max; ++type)
  1087. vmw_resource_evict_type(dev_priv, type);
  1088. mutex_unlock(&dev_priv->cmdbuf_mutex);
  1089. }