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