18038d2a9SDave Airlie // SPDX-License-Identifier: GPL-2.0 OR MIT
2f1d34bfdSThomas Hellstrom /**************************************************************************
3f1d34bfdSThomas Hellstrom *
409881d29SZack Rusin * Copyright © 2011-2023 VMware, Inc., Palo Alto, CA., USA
5f1d34bfdSThomas Hellstrom * All Rights Reserved.
6f1d34bfdSThomas Hellstrom *
7f1d34bfdSThomas Hellstrom * Permission is hereby granted, free of charge, to any person obtaining a
8f1d34bfdSThomas Hellstrom * copy of this software and associated documentation files (the
9f1d34bfdSThomas Hellstrom * "Software"), to deal in the Software without restriction, including
10f1d34bfdSThomas Hellstrom * without limitation the rights to use, copy, modify, merge, publish,
11f1d34bfdSThomas Hellstrom * distribute, sub license, and/or sell copies of the Software, and to
12f1d34bfdSThomas Hellstrom * permit persons to whom the Software is furnished to do so, subject to
13f1d34bfdSThomas Hellstrom * the following conditions:
14f1d34bfdSThomas Hellstrom *
15f1d34bfdSThomas Hellstrom * The above copyright notice and this permission notice (including the
16f1d34bfdSThomas Hellstrom * next paragraph) shall be included in all copies or substantial portions
17f1d34bfdSThomas Hellstrom * of the Software.
18f1d34bfdSThomas Hellstrom *
19f1d34bfdSThomas Hellstrom * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20f1d34bfdSThomas Hellstrom * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21f1d34bfdSThomas Hellstrom * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22f1d34bfdSThomas Hellstrom * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23f1d34bfdSThomas Hellstrom * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24f1d34bfdSThomas Hellstrom * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25f1d34bfdSThomas Hellstrom * USE OR OTHER DEALINGS IN THE SOFTWARE.
26f1d34bfdSThomas Hellstrom *
27f1d34bfdSThomas Hellstrom **************************************************************************/
28f1d34bfdSThomas Hellstrom
2909881d29SZack Rusin #include "vmwgfx_bo.h"
3009881d29SZack Rusin #include "vmwgfx_drv.h"
3109881d29SZack Rusin
3209881d29SZack Rusin
33f1d34bfdSThomas Hellstrom #include <drm/ttm/ttm_placement.h>
34f1d34bfdSThomas Hellstrom
vmw_bo_release(struct vmw_bo * vbo)35668b2066SZack Rusin static void vmw_bo_release(struct vmw_bo *vbo)
36668b2066SZack Rusin {
3791398b41SZack Rusin WARN_ON(vbo->tbo.base.funcs &&
3891398b41SZack Rusin kref_read(&vbo->tbo.base.refcount) != 0);
39668b2066SZack Rusin vmw_bo_unmap(vbo);
40668b2066SZack Rusin drm_gem_object_release(&vbo->tbo.base);
41668b2066SZack Rusin }
42668b2066SZack Rusin
43e9431ea5SThomas Hellstrom /**
4409881d29SZack Rusin * vmw_bo_free - vmw_bo destructor
456b2e8aa4SZack Rusin *
466b2e8aa4SZack Rusin * @bo: Pointer to the embedded struct ttm_buffer_object
476b2e8aa4SZack Rusin */
vmw_bo_free(struct ttm_buffer_object * bo)4809881d29SZack Rusin static void vmw_bo_free(struct ttm_buffer_object *bo)
496b2e8aa4SZack Rusin {
5009881d29SZack Rusin struct vmw_bo *vbo = to_vmw_bo(&bo->base);
516b2e8aa4SZack Rusin
5209881d29SZack Rusin WARN_ON(vbo->dirty);
5309881d29SZack Rusin WARN_ON(!RB_EMPTY_ROOT(&vbo->res_tree));
54668b2066SZack Rusin vmw_bo_release(vbo);
5509881d29SZack Rusin kfree(vbo);
566b2e8aa4SZack Rusin }
576b2e8aa4SZack Rusin
586b2e8aa4SZack Rusin /**
59f1d34bfdSThomas Hellstrom * vmw_bo_pin_in_placement - Validate a buffer to placement.
60f1d34bfdSThomas Hellstrom *
61f1d34bfdSThomas Hellstrom * @dev_priv: Driver private.
62f1d34bfdSThomas Hellstrom * @buf: DMA buffer to move.
63f1d34bfdSThomas Hellstrom * @placement: The placement to pin it.
64f1d34bfdSThomas Hellstrom * @interruptible: Use interruptible wait.
65e9431ea5SThomas Hellstrom * Return: Zero on success, Negative error code on failure. In particular
66e9431ea5SThomas Hellstrom * -ERESTARTSYS if interrupted by a signal
67f1d34bfdSThomas Hellstrom */
vmw_bo_pin_in_placement(struct vmw_private * dev_priv,struct vmw_bo * buf,struct ttm_placement * placement,bool interruptible)686703e28fSZack Rusin static int vmw_bo_pin_in_placement(struct vmw_private *dev_priv,
6909881d29SZack Rusin struct vmw_bo *buf,
70f1d34bfdSThomas Hellstrom struct ttm_placement *placement,
71f1d34bfdSThomas Hellstrom bool interruptible)
72f1d34bfdSThomas Hellstrom {
73f1d34bfdSThomas Hellstrom struct ttm_operation_ctx ctx = {interruptible, false };
74668b2066SZack Rusin struct ttm_buffer_object *bo = &buf->tbo;
75f1d34bfdSThomas Hellstrom int ret;
76f1d34bfdSThomas Hellstrom
77f1d34bfdSThomas Hellstrom vmw_execbuf_release_pinned_bo(dev_priv);
78f1d34bfdSThomas Hellstrom
79f1d34bfdSThomas Hellstrom ret = ttm_bo_reserve(bo, interruptible, false, NULL);
80f1d34bfdSThomas Hellstrom if (unlikely(ret != 0))
81f1d34bfdSThomas Hellstrom goto err;
82f1d34bfdSThomas Hellstrom
83f1d34bfdSThomas Hellstrom ret = ttm_bo_validate(bo, placement, &ctx);
84f1d34bfdSThomas Hellstrom if (!ret)
85f1d34bfdSThomas Hellstrom vmw_bo_pin_reserved(buf, true);
86f1d34bfdSThomas Hellstrom
87f1d34bfdSThomas Hellstrom ttm_bo_unreserve(bo);
88f1d34bfdSThomas Hellstrom err:
89f1d34bfdSThomas Hellstrom return ret;
90f1d34bfdSThomas Hellstrom }
91f1d34bfdSThomas Hellstrom
92e9431ea5SThomas Hellstrom
93f1d34bfdSThomas Hellstrom /**
94f1d34bfdSThomas Hellstrom * vmw_bo_pin_in_vram_or_gmr - Move a buffer to vram or gmr.
95f1d34bfdSThomas Hellstrom *
96f1d34bfdSThomas Hellstrom * This function takes the reservation_sem in write mode.
97f1d34bfdSThomas Hellstrom * Flushes and unpins the query bo to avoid failures.
98f1d34bfdSThomas Hellstrom *
99f1d34bfdSThomas Hellstrom * @dev_priv: Driver private.
100f1d34bfdSThomas Hellstrom * @buf: DMA buffer to move.
101f1d34bfdSThomas Hellstrom * @interruptible: Use interruptible wait.
102e9431ea5SThomas Hellstrom * Return: Zero on success, Negative error code on failure. In particular
103e9431ea5SThomas Hellstrom * -ERESTARTSYS if interrupted by a signal
104f1d34bfdSThomas Hellstrom */
vmw_bo_pin_in_vram_or_gmr(struct vmw_private * dev_priv,struct vmw_bo * buf,bool interruptible)105f1d34bfdSThomas Hellstrom int vmw_bo_pin_in_vram_or_gmr(struct vmw_private *dev_priv,
10609881d29SZack Rusin struct vmw_bo *buf,
107f1d34bfdSThomas Hellstrom bool interruptible)
108f1d34bfdSThomas Hellstrom {
109f1d34bfdSThomas Hellstrom struct ttm_operation_ctx ctx = {interruptible, false };
110668b2066SZack Rusin struct ttm_buffer_object *bo = &buf->tbo;
111f1d34bfdSThomas Hellstrom int ret;
112f1d34bfdSThomas Hellstrom
113f1d34bfdSThomas Hellstrom vmw_execbuf_release_pinned_bo(dev_priv);
114f1d34bfdSThomas Hellstrom
115f1d34bfdSThomas Hellstrom ret = ttm_bo_reserve(bo, interruptible, false, NULL);
116f1d34bfdSThomas Hellstrom if (unlikely(ret != 0))
117f1d34bfdSThomas Hellstrom goto err;
118f1d34bfdSThomas Hellstrom
11939985eeaSZack Rusin vmw_bo_placement_set(buf,
12039985eeaSZack Rusin VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
12139985eeaSZack Rusin VMW_BO_DOMAIN_GMR);
12239985eeaSZack Rusin ret = ttm_bo_validate(bo, &buf->placement, &ctx);
123f1d34bfdSThomas Hellstrom if (likely(ret == 0) || ret == -ERESTARTSYS)
124f1d34bfdSThomas Hellstrom goto out_unreserve;
125f1d34bfdSThomas Hellstrom
12639985eeaSZack Rusin vmw_bo_placement_set(buf,
12739985eeaSZack Rusin VMW_BO_DOMAIN_VRAM,
12839985eeaSZack Rusin VMW_BO_DOMAIN_VRAM);
12939985eeaSZack Rusin ret = ttm_bo_validate(bo, &buf->placement, &ctx);
130f1d34bfdSThomas Hellstrom
131f1d34bfdSThomas Hellstrom out_unreserve:
132f1d34bfdSThomas Hellstrom if (!ret)
133f1d34bfdSThomas Hellstrom vmw_bo_pin_reserved(buf, true);
134f1d34bfdSThomas Hellstrom
135f1d34bfdSThomas Hellstrom ttm_bo_unreserve(bo);
136f1d34bfdSThomas Hellstrom err:
137f1d34bfdSThomas Hellstrom return ret;
138f1d34bfdSThomas Hellstrom }
139f1d34bfdSThomas Hellstrom
140e9431ea5SThomas Hellstrom
141f1d34bfdSThomas Hellstrom /**
142f1d34bfdSThomas Hellstrom * vmw_bo_pin_in_vram - Move a buffer to vram.
143f1d34bfdSThomas Hellstrom *
144f1d34bfdSThomas Hellstrom * This function takes the reservation_sem in write mode.
145f1d34bfdSThomas Hellstrom * Flushes and unpins the query bo to avoid failures.
146f1d34bfdSThomas Hellstrom *
147f1d34bfdSThomas Hellstrom * @dev_priv: Driver private.
148f1d34bfdSThomas Hellstrom * @buf: DMA buffer to move.
149f1d34bfdSThomas Hellstrom * @interruptible: Use interruptible wait.
150e9431ea5SThomas Hellstrom * Return: Zero on success, Negative error code on failure. In particular
151e9431ea5SThomas Hellstrom * -ERESTARTSYS if interrupted by a signal
152f1d34bfdSThomas Hellstrom */
vmw_bo_pin_in_vram(struct vmw_private * dev_priv,struct vmw_bo * buf,bool interruptible)153f1d34bfdSThomas Hellstrom int vmw_bo_pin_in_vram(struct vmw_private *dev_priv,
15409881d29SZack Rusin struct vmw_bo *buf,
155f1d34bfdSThomas Hellstrom bool interruptible)
156f1d34bfdSThomas Hellstrom {
157f1d34bfdSThomas Hellstrom return vmw_bo_pin_in_placement(dev_priv, buf, &vmw_vram_placement,
158f1d34bfdSThomas Hellstrom interruptible);
159f1d34bfdSThomas Hellstrom }
160f1d34bfdSThomas Hellstrom
161e9431ea5SThomas Hellstrom
162f1d34bfdSThomas Hellstrom /**
163f1d34bfdSThomas Hellstrom * vmw_bo_pin_in_start_of_vram - Move a buffer to start of vram.
164f1d34bfdSThomas Hellstrom *
165f1d34bfdSThomas Hellstrom * This function takes the reservation_sem in write mode.
166f1d34bfdSThomas Hellstrom * Flushes and unpins the query bo to avoid failures.
167f1d34bfdSThomas Hellstrom *
168f1d34bfdSThomas Hellstrom * @dev_priv: Driver private.
169f1d34bfdSThomas Hellstrom * @buf: DMA buffer to pin.
170f1d34bfdSThomas Hellstrom * @interruptible: Use interruptible wait.
171e9431ea5SThomas Hellstrom * Return: Zero on success, Negative error code on failure. In particular
172e9431ea5SThomas Hellstrom * -ERESTARTSYS if interrupted by a signal
173f1d34bfdSThomas Hellstrom */
vmw_bo_pin_in_start_of_vram(struct vmw_private * dev_priv,struct vmw_bo * buf,bool interruptible)174f1d34bfdSThomas Hellstrom int vmw_bo_pin_in_start_of_vram(struct vmw_private *dev_priv,
17509881d29SZack Rusin struct vmw_bo *buf,
176f1d34bfdSThomas Hellstrom bool interruptible)
177f1d34bfdSThomas Hellstrom {
178f1d34bfdSThomas Hellstrom struct ttm_operation_ctx ctx = {interruptible, false };
179668b2066SZack Rusin struct ttm_buffer_object *bo = &buf->tbo;
180f1d34bfdSThomas Hellstrom int ret = 0;
181f1d34bfdSThomas Hellstrom
182f1d34bfdSThomas Hellstrom vmw_execbuf_release_pinned_bo(dev_priv);
183f1d34bfdSThomas Hellstrom ret = ttm_bo_reserve(bo, interruptible, false, NULL);
184f1d34bfdSThomas Hellstrom if (unlikely(ret != 0))
185f1d34bfdSThomas Hellstrom goto err_unlock;
186f1d34bfdSThomas Hellstrom
187f1d34bfdSThomas Hellstrom /*
188f1d34bfdSThomas Hellstrom * Is this buffer already in vram but not at the start of it?
189f1d34bfdSThomas Hellstrom * In that case, evict it first because TTM isn't good at handling
190f1d34bfdSThomas Hellstrom * that situation.
191f1d34bfdSThomas Hellstrom */
192d3116756SChristian König if (bo->resource->mem_type == TTM_PL_VRAM &&
193e3c92eb4SSomalapuram Amaranath bo->resource->start < PFN_UP(bo->resource->size) &&
194d3116756SChristian König bo->resource->start > 0 &&
195668b2066SZack Rusin buf->tbo.pin_count == 0) {
196f1d34bfdSThomas Hellstrom ctx.interruptible = false;
19739985eeaSZack Rusin vmw_bo_placement_set(buf,
19839985eeaSZack Rusin VMW_BO_DOMAIN_SYS,
19939985eeaSZack Rusin VMW_BO_DOMAIN_SYS);
20039985eeaSZack Rusin (void)ttm_bo_validate(bo, &buf->placement, &ctx);
201f1d34bfdSThomas Hellstrom }
202f1d34bfdSThomas Hellstrom
20339985eeaSZack Rusin vmw_bo_placement_set(buf,
20439985eeaSZack Rusin VMW_BO_DOMAIN_VRAM,
20539985eeaSZack Rusin VMW_BO_DOMAIN_VRAM);
20639985eeaSZack Rusin buf->places[0].lpfn = PFN_UP(bo->resource->size);
207e10644f8SIan Forbes buf->busy_places[0].lpfn = PFN_UP(bo->resource->size);
20839985eeaSZack Rusin ret = ttm_bo_validate(bo, &buf->placement, &ctx);
209f1d34bfdSThomas Hellstrom
210f1d34bfdSThomas Hellstrom /* For some reason we didn't end up at the start of vram */
211d3116756SChristian König WARN_ON(ret == 0 && bo->resource->start != 0);
212f1d34bfdSThomas Hellstrom if (!ret)
213f1d34bfdSThomas Hellstrom vmw_bo_pin_reserved(buf, true);
214f1d34bfdSThomas Hellstrom
215f1d34bfdSThomas Hellstrom ttm_bo_unreserve(bo);
216f1d34bfdSThomas Hellstrom err_unlock:
217f1d34bfdSThomas Hellstrom
218f1d34bfdSThomas Hellstrom return ret;
219f1d34bfdSThomas Hellstrom }
220f1d34bfdSThomas Hellstrom
221e9431ea5SThomas Hellstrom
222f1d34bfdSThomas Hellstrom /**
223f1d34bfdSThomas Hellstrom * vmw_bo_unpin - Unpin the buffer given buffer, does not move the buffer.
224f1d34bfdSThomas Hellstrom *
225f1d34bfdSThomas Hellstrom * This function takes the reservation_sem in write mode.
226f1d34bfdSThomas Hellstrom *
227f1d34bfdSThomas Hellstrom * @dev_priv: Driver private.
228f1d34bfdSThomas Hellstrom * @buf: DMA buffer to unpin.
229f1d34bfdSThomas Hellstrom * @interruptible: Use interruptible wait.
230e9431ea5SThomas Hellstrom * Return: Zero on success, Negative error code on failure. In particular
231e9431ea5SThomas Hellstrom * -ERESTARTSYS if interrupted by a signal
232f1d34bfdSThomas Hellstrom */
vmw_bo_unpin(struct vmw_private * dev_priv,struct vmw_bo * buf,bool interruptible)233f1d34bfdSThomas Hellstrom int vmw_bo_unpin(struct vmw_private *dev_priv,
23409881d29SZack Rusin struct vmw_bo *buf,
235f1d34bfdSThomas Hellstrom bool interruptible)
236f1d34bfdSThomas Hellstrom {
237668b2066SZack Rusin struct ttm_buffer_object *bo = &buf->tbo;
238f1d34bfdSThomas Hellstrom int ret;
239f1d34bfdSThomas Hellstrom
240f1d34bfdSThomas Hellstrom ret = ttm_bo_reserve(bo, interruptible, false, NULL);
241f1d34bfdSThomas Hellstrom if (unlikely(ret != 0))
242f1d34bfdSThomas Hellstrom goto err;
243f1d34bfdSThomas Hellstrom
244f1d34bfdSThomas Hellstrom vmw_bo_pin_reserved(buf, false);
245f1d34bfdSThomas Hellstrom
246f1d34bfdSThomas Hellstrom ttm_bo_unreserve(bo);
247f1d34bfdSThomas Hellstrom
248f1d34bfdSThomas Hellstrom err:
249f1d34bfdSThomas Hellstrom return ret;
250f1d34bfdSThomas Hellstrom }
251f1d34bfdSThomas Hellstrom
252f1d34bfdSThomas Hellstrom /**
253f1d34bfdSThomas Hellstrom * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement
254f1d34bfdSThomas Hellstrom * of a buffer.
255f1d34bfdSThomas Hellstrom *
256f1d34bfdSThomas Hellstrom * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved.
257f1d34bfdSThomas Hellstrom * @ptr: SVGAGuestPtr returning the result.
258f1d34bfdSThomas Hellstrom */
vmw_bo_get_guest_ptr(const struct ttm_buffer_object * bo,SVGAGuestPtr * ptr)259f1d34bfdSThomas Hellstrom void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo,
260f1d34bfdSThomas Hellstrom SVGAGuestPtr *ptr)
261f1d34bfdSThomas Hellstrom {
262d3116756SChristian König if (bo->resource->mem_type == TTM_PL_VRAM) {
263f1d34bfdSThomas Hellstrom ptr->gmrId = SVGA_GMR_FRAMEBUFFER;
264d3116756SChristian König ptr->offset = bo->resource->start << PAGE_SHIFT;
265f1d34bfdSThomas Hellstrom } else {
266d3116756SChristian König ptr->gmrId = bo->resource->start;
267f1d34bfdSThomas Hellstrom ptr->offset = 0;
268f1d34bfdSThomas Hellstrom }
269f1d34bfdSThomas Hellstrom }
270f1d34bfdSThomas Hellstrom
271f1d34bfdSThomas Hellstrom
272f1d34bfdSThomas Hellstrom /**
273f1d34bfdSThomas Hellstrom * vmw_bo_pin_reserved - Pin or unpin a buffer object without moving it.
274f1d34bfdSThomas Hellstrom *
275f1d34bfdSThomas Hellstrom * @vbo: The buffer object. Must be reserved.
276f1d34bfdSThomas Hellstrom * @pin: Whether to pin or unpin.
277f1d34bfdSThomas Hellstrom *
278f1d34bfdSThomas Hellstrom */
vmw_bo_pin_reserved(struct vmw_bo * vbo,bool pin)27909881d29SZack Rusin void vmw_bo_pin_reserved(struct vmw_bo *vbo, bool pin)
280f1d34bfdSThomas Hellstrom {
281f1d34bfdSThomas Hellstrom struct ttm_operation_ctx ctx = { false, true };
282f1d34bfdSThomas Hellstrom struct ttm_place pl;
283f1d34bfdSThomas Hellstrom struct ttm_placement placement;
284668b2066SZack Rusin struct ttm_buffer_object *bo = &vbo->tbo;
285d3116756SChristian König uint32_t old_mem_type = bo->resource->mem_type;
286f1d34bfdSThomas Hellstrom int ret;
287f1d34bfdSThomas Hellstrom
28852791eeeSChristian König dma_resv_assert_held(bo->base.resv);
289f1d34bfdSThomas Hellstrom
290fbe86ca5SChristian König if (pin == !!bo->pin_count)
291f1d34bfdSThomas Hellstrom return;
292f1d34bfdSThomas Hellstrom
293f1d34bfdSThomas Hellstrom pl.fpfn = 0;
294f1d34bfdSThomas Hellstrom pl.lpfn = 0;
295d3116756SChristian König pl.mem_type = bo->resource->mem_type;
296d3116756SChristian König pl.flags = bo->resource->placement;
297f1d34bfdSThomas Hellstrom
298f1d34bfdSThomas Hellstrom memset(&placement, 0, sizeof(placement));
299f1d34bfdSThomas Hellstrom placement.num_placement = 1;
300f1d34bfdSThomas Hellstrom placement.placement = &pl;
301f1d34bfdSThomas Hellstrom
302f1d34bfdSThomas Hellstrom ret = ttm_bo_validate(bo, &placement, &ctx);
303f1d34bfdSThomas Hellstrom
304d3116756SChristian König BUG_ON(ret != 0 || bo->resource->mem_type != old_mem_type);
305f1d34bfdSThomas Hellstrom
306fbe86ca5SChristian König if (pin)
307fbe86ca5SChristian König ttm_bo_pin(bo);
308fbe86ca5SChristian König else
309fbe86ca5SChristian König ttm_bo_unpin(bo);
310fbe86ca5SChristian König }
311f1d34bfdSThomas Hellstrom
312e9431ea5SThomas Hellstrom /**
313e9431ea5SThomas Hellstrom * vmw_bo_map_and_cache - Map a buffer object and cache the map
314f1d34bfdSThomas Hellstrom *
315f1d34bfdSThomas Hellstrom * @vbo: The buffer object to map
316f1d34bfdSThomas Hellstrom * Return: A kernel virtual address or NULL if mapping failed.
317f1d34bfdSThomas Hellstrom *
318f1d34bfdSThomas Hellstrom * This function maps a buffer object into the kernel address space, or
319f1d34bfdSThomas Hellstrom * returns the virtual kernel address of an already existing map. The virtual
320f1d34bfdSThomas Hellstrom * address remains valid as long as the buffer object is pinned or reserved.
321f1d34bfdSThomas Hellstrom * The cached map is torn down on either
322f1d34bfdSThomas Hellstrom * 1) Buffer object move
323f1d34bfdSThomas Hellstrom * 2) Buffer object swapout
324f1d34bfdSThomas Hellstrom * 3) Buffer object destruction
325f1d34bfdSThomas Hellstrom *
326f1d34bfdSThomas Hellstrom */
vmw_bo_map_and_cache(struct vmw_bo * vbo)32709881d29SZack Rusin void *vmw_bo_map_and_cache(struct vmw_bo *vbo)
328f1d34bfdSThomas Hellstrom {
329668b2066SZack Rusin struct ttm_buffer_object *bo = &vbo->tbo;
330f1d34bfdSThomas Hellstrom bool not_used;
331f1d34bfdSThomas Hellstrom void *virtual;
332f1d34bfdSThomas Hellstrom int ret;
333f1d34bfdSThomas Hellstrom
3340851b1ecSZack Rusin atomic_inc(&vbo->map_count);
3350851b1ecSZack Rusin
336f1d34bfdSThomas Hellstrom virtual = ttm_kmap_obj_virtual(&vbo->map, ¬_used);
337f1d34bfdSThomas Hellstrom if (virtual)
338f1d34bfdSThomas Hellstrom return virtual;
339f1d34bfdSThomas Hellstrom
340e3c92eb4SSomalapuram Amaranath ret = ttm_bo_kmap(bo, 0, PFN_UP(bo->base.size), &vbo->map);
341f1d34bfdSThomas Hellstrom if (ret)
342f1d34bfdSThomas Hellstrom DRM_ERROR("Buffer object map failed: %d.\n", ret);
343f1d34bfdSThomas Hellstrom
344f1d34bfdSThomas Hellstrom return ttm_kmap_obj_virtual(&vbo->map, ¬_used);
345f1d34bfdSThomas Hellstrom }
346e9431ea5SThomas Hellstrom
347e9431ea5SThomas Hellstrom
348e9431ea5SThomas Hellstrom /**
349e9431ea5SThomas Hellstrom * vmw_bo_unmap - Tear down a cached buffer object map.
350e9431ea5SThomas Hellstrom *
351e9431ea5SThomas Hellstrom * @vbo: The buffer object whose map we are tearing down.
352e9431ea5SThomas Hellstrom *
353e9431ea5SThomas Hellstrom * This function tears down a cached map set up using
35409881d29SZack Rusin * vmw_bo_map_and_cache().
355e9431ea5SThomas Hellstrom */
vmw_bo_unmap(struct vmw_bo * vbo)35609881d29SZack Rusin void vmw_bo_unmap(struct vmw_bo *vbo)
357e9431ea5SThomas Hellstrom {
3580851b1ecSZack Rusin int map_count;
3590851b1ecSZack Rusin
360e9431ea5SThomas Hellstrom if (vbo->map.bo == NULL)
361e9431ea5SThomas Hellstrom return;
362e9431ea5SThomas Hellstrom
3630851b1ecSZack Rusin map_count = atomic_dec_return(&vbo->map_count);
3640851b1ecSZack Rusin
3650851b1ecSZack Rusin if (!map_count) {
366e9431ea5SThomas Hellstrom ttm_bo_kunmap(&vbo->map);
367668b2066SZack Rusin vbo->map.bo = NULL;
368e9431ea5SThomas Hellstrom }
3690851b1ecSZack Rusin }
370e9431ea5SThomas Hellstrom
37135079323SChristian König
372b254557cSChristian König /**
373668b2066SZack Rusin * vmw_bo_init - Initialize a vmw buffer object
374b254557cSChristian König *
375b254557cSChristian König * @dev_priv: Pointer to the device private struct
376668b2066SZack Rusin * @vmw_bo: Buffer object to initialize
377668b2066SZack Rusin * @params: Parameters used to initialize the buffer object
378668b2066SZack Rusin * @destroy: The function used to delete the buffer object
379668b2066SZack Rusin * Returns: Zero on success, negative error code on error.
380b254557cSChristian König *
381b254557cSChristian König */
vmw_bo_init(struct vmw_private * dev_priv,struct vmw_bo * vmw_bo,struct vmw_bo_params * params,void (* destroy)(struct ttm_buffer_object *))382668b2066SZack Rusin static int vmw_bo_init(struct vmw_private *dev_priv,
383668b2066SZack Rusin struct vmw_bo *vmw_bo,
384668b2066SZack Rusin struct vmw_bo_params *params,
385668b2066SZack Rusin void (*destroy)(struct ttm_buffer_object *))
386b254557cSChristian König {
3878aadeb8aSZack Rusin struct ttm_operation_ctx ctx = {
388668b2066SZack Rusin .interruptible = params->bo_type != ttm_bo_type_kernel,
38965674218SZack Rusin .no_wait_gpu = false,
39065674218SZack Rusin .resv = params->resv,
3918aadeb8aSZack Rusin };
392668b2066SZack Rusin struct ttm_device *bdev = &dev_priv->bdev;
3938afa13a0SZack Rusin struct drm_device *vdev = &dev_priv->drm;
394b254557cSChristian König int ret;
395b254557cSChristian König
396668b2066SZack Rusin memset(vmw_bo, 0, sizeof(*vmw_bo));
397b254557cSChristian König
398668b2066SZack Rusin BUILD_BUG_ON(TTM_MAX_BO_PRIORITY <= 3);
399668b2066SZack Rusin vmw_bo->tbo.priority = 3;
400668b2066SZack Rusin vmw_bo->res_tree = RB_ROOT;
4010851b1ecSZack Rusin atomic_set(&vmw_bo->map_count, 0);
4028afa13a0SZack Rusin
403668b2066SZack Rusin params->size = ALIGN(params->size, PAGE_SIZE);
404668b2066SZack Rusin drm_gem_private_object_init(vdev, &vmw_bo->tbo.base, params->size);
405d02117f8SChristian König
406668b2066SZack Rusin vmw_bo_placement_set(vmw_bo, params->domain, params->busy_domain);
407668b2066SZack Rusin ret = ttm_bo_init_reserved(bdev, &vmw_bo->tbo, params->bo_type,
40865674218SZack Rusin &vmw_bo->placement, 0, &ctx,
40965674218SZack Rusin params->sg, params->resv, destroy);
410f07069daSChristian König if (unlikely(ret))
411668b2066SZack Rusin return ret;
412f07069daSChristian König
413668b2066SZack Rusin if (params->pin)
414668b2066SZack Rusin ttm_bo_pin(&vmw_bo->tbo);
415*5faf45beSIan Forbes if (!params->keep_resv)
416668b2066SZack Rusin ttm_bo_unreserve(&vmw_bo->tbo);
417b254557cSChristian König
418b254557cSChristian König return 0;
419b254557cSChristian König }
420e9431ea5SThomas Hellstrom
vmw_bo_create(struct vmw_private * vmw,struct vmw_bo_params * params,struct vmw_bo ** p_bo)4218afa13a0SZack Rusin int vmw_bo_create(struct vmw_private *vmw,
422668b2066SZack Rusin struct vmw_bo_params *params,
42309881d29SZack Rusin struct vmw_bo **p_bo)
4248afa13a0SZack Rusin {
4258afa13a0SZack Rusin int ret;
4268afa13a0SZack Rusin
4278afa13a0SZack Rusin *p_bo = kmalloc(sizeof(**p_bo), GFP_KERNEL);
4288afa13a0SZack Rusin if (unlikely(!*p_bo)) {
4298afa13a0SZack Rusin DRM_ERROR("Failed to allocate a buffer.\n");
4308afa13a0SZack Rusin return -ENOMEM;
4318afa13a0SZack Rusin }
4328afa13a0SZack Rusin
43336d421e6SZack Rusin /*
43436d421e6SZack Rusin * vmw_bo_init will delete the *p_bo object if it fails
43536d421e6SZack Rusin */
436668b2066SZack Rusin ret = vmw_bo_init(vmw, *p_bo, params, vmw_bo_free);
4378afa13a0SZack Rusin if (unlikely(ret != 0))
4388afa13a0SZack Rusin goto out_error;
4398afa13a0SZack Rusin
4408afa13a0SZack Rusin return ret;
4418afa13a0SZack Rusin out_error:
4428afa13a0SZack Rusin *p_bo = NULL;
4438afa13a0SZack Rusin return ret;
4448afa13a0SZack Rusin }
4458afa13a0SZack Rusin
446e9431ea5SThomas Hellstrom /**
44709881d29SZack Rusin * vmw_user_bo_synccpu_grab - Grab a struct vmw_bo for cpu
448e9431ea5SThomas Hellstrom * access, idling previous GPU operations on the buffer and optionally
449e9431ea5SThomas Hellstrom * blocking it for further command submissions.
450e9431ea5SThomas Hellstrom *
4518afa13a0SZack Rusin * @vmw_bo: Pointer to the buffer object being grabbed for CPU access
452e9431ea5SThomas Hellstrom * @flags: Flags indicating how the grab should be performed.
453e9431ea5SThomas Hellstrom * Return: Zero on success, Negative error code on error. In particular,
454e9431ea5SThomas Hellstrom * -EBUSY will be returned if a dontblock operation is requested and the
455e9431ea5SThomas Hellstrom * buffer object is busy, and -ERESTARTSYS will be returned if a wait is
456e9431ea5SThomas Hellstrom * interrupted by a signal.
457e9431ea5SThomas Hellstrom *
458e9431ea5SThomas Hellstrom * A blocking grab will be automatically released when @tfile is closed.
459e9431ea5SThomas Hellstrom */
vmw_user_bo_synccpu_grab(struct vmw_bo * vmw_bo,uint32_t flags)46009881d29SZack Rusin static int vmw_user_bo_synccpu_grab(struct vmw_bo *vmw_bo,
461e9431ea5SThomas Hellstrom uint32_t flags)
462e9431ea5SThomas Hellstrom {
4637fb03cc3SChristian König bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
464668b2066SZack Rusin struct ttm_buffer_object *bo = &vmw_bo->tbo;
465e9431ea5SThomas Hellstrom int ret;
466e9431ea5SThomas Hellstrom
467e9431ea5SThomas Hellstrom if (flags & drm_vmw_synccpu_allow_cs) {
468e9431ea5SThomas Hellstrom long lret;
469e9431ea5SThomas Hellstrom
4707bc80a54SChristian König lret = dma_resv_wait_timeout(bo->base.resv, DMA_RESV_USAGE_READ,
4717bc80a54SChristian König true, nonblock ? 0 :
472d3fae3b3SChristian König MAX_SCHEDULE_TIMEOUT);
473e9431ea5SThomas Hellstrom if (!lret)
474e9431ea5SThomas Hellstrom return -EBUSY;
475e9431ea5SThomas Hellstrom else if (lret < 0)
476e9431ea5SThomas Hellstrom return lret;
477e9431ea5SThomas Hellstrom return 0;
478e9431ea5SThomas Hellstrom }
479e9431ea5SThomas Hellstrom
4807fb03cc3SChristian König ret = ttm_bo_reserve(bo, true, nonblock, NULL);
4817fb03cc3SChristian König if (unlikely(ret != 0))
4827fb03cc3SChristian König return ret;
4837fb03cc3SChristian König
4847fb03cc3SChristian König ret = ttm_bo_wait(bo, true, nonblock);
4857fb03cc3SChristian König if (likely(ret == 0))
4868afa13a0SZack Rusin atomic_inc(&vmw_bo->cpu_writers);
4877fb03cc3SChristian König
4887fb03cc3SChristian König ttm_bo_unreserve(bo);
489e9431ea5SThomas Hellstrom if (unlikely(ret != 0))
490e9431ea5SThomas Hellstrom return ret;
491e9431ea5SThomas Hellstrom
492e9431ea5SThomas Hellstrom return ret;
493e9431ea5SThomas Hellstrom }
494e9431ea5SThomas Hellstrom
495e9431ea5SThomas Hellstrom /**
496e9431ea5SThomas Hellstrom * vmw_user_bo_synccpu_release - Release a previous grab for CPU access,
497e9431ea5SThomas Hellstrom * and unblock command submission on the buffer if blocked.
498e9431ea5SThomas Hellstrom *
4998afa13a0SZack Rusin * @filp: Identifying the caller.
500e9431ea5SThomas Hellstrom * @handle: Handle identifying the buffer object.
501e9431ea5SThomas Hellstrom * @flags: Flags indicating the type of release.
502e9431ea5SThomas Hellstrom */
vmw_user_bo_synccpu_release(struct drm_file * filp,uint32_t handle,uint32_t flags)5038afa13a0SZack Rusin static int vmw_user_bo_synccpu_release(struct drm_file *filp,
5048afa13a0SZack Rusin uint32_t handle,
505e9431ea5SThomas Hellstrom uint32_t flags)
506e9431ea5SThomas Hellstrom {
50709881d29SZack Rusin struct vmw_bo *vmw_bo;
5088afa13a0SZack Rusin int ret = vmw_user_bo_lookup(filp, handle, &vmw_bo);
509e9431ea5SThomas Hellstrom
51060c9ecd7SZack Rusin if (!ret) {
5118afa13a0SZack Rusin if (!(flags & drm_vmw_synccpu_allow_cs)) {
5128afa13a0SZack Rusin atomic_dec(&vmw_bo->cpu_writers);
5138afa13a0SZack Rusin }
51491398b41SZack Rusin vmw_user_bo_unref(&vmw_bo);
51560c9ecd7SZack Rusin }
5168afa13a0SZack Rusin
5178afa13a0SZack Rusin return ret;
518e9431ea5SThomas Hellstrom }
519e9431ea5SThomas Hellstrom
520e9431ea5SThomas Hellstrom
521e9431ea5SThomas Hellstrom /**
522e9431ea5SThomas Hellstrom * vmw_user_bo_synccpu_ioctl - ioctl function implementing the synccpu
523e9431ea5SThomas Hellstrom * functionality.
524e9431ea5SThomas Hellstrom *
525e9431ea5SThomas Hellstrom * @dev: Identifies the drm device.
526e9431ea5SThomas Hellstrom * @data: Pointer to the ioctl argument.
527e9431ea5SThomas Hellstrom * @file_priv: Identifies the caller.
528e9431ea5SThomas Hellstrom * Return: Zero on success, negative error code on error.
529e9431ea5SThomas Hellstrom *
530e9431ea5SThomas Hellstrom * This function checks the ioctl arguments for validity and calls the
531e9431ea5SThomas Hellstrom * relevant synccpu functions.
532e9431ea5SThomas Hellstrom */
vmw_user_bo_synccpu_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)533e9431ea5SThomas Hellstrom int vmw_user_bo_synccpu_ioctl(struct drm_device *dev, void *data,
534e9431ea5SThomas Hellstrom struct drm_file *file_priv)
535e9431ea5SThomas Hellstrom {
536e9431ea5SThomas Hellstrom struct drm_vmw_synccpu_arg *arg =
537e9431ea5SThomas Hellstrom (struct drm_vmw_synccpu_arg *) data;
53809881d29SZack Rusin struct vmw_bo *vbo;
539e9431ea5SThomas Hellstrom int ret;
540e9431ea5SThomas Hellstrom
541e9431ea5SThomas Hellstrom if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0
542e9431ea5SThomas Hellstrom || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write |
543e9431ea5SThomas Hellstrom drm_vmw_synccpu_dontblock |
544e9431ea5SThomas Hellstrom drm_vmw_synccpu_allow_cs)) != 0) {
545e9431ea5SThomas Hellstrom DRM_ERROR("Illegal synccpu flags.\n");
546e9431ea5SThomas Hellstrom return -EINVAL;
547e9431ea5SThomas Hellstrom }
548e9431ea5SThomas Hellstrom
549e9431ea5SThomas Hellstrom switch (arg->op) {
550e9431ea5SThomas Hellstrom case drm_vmw_synccpu_grab:
5518afa13a0SZack Rusin ret = vmw_user_bo_lookup(file_priv, arg->handle, &vbo);
552e9431ea5SThomas Hellstrom if (unlikely(ret != 0))
553e9431ea5SThomas Hellstrom return ret;
554e9431ea5SThomas Hellstrom
5558afa13a0SZack Rusin ret = vmw_user_bo_synccpu_grab(vbo, arg->flags);
55691398b41SZack Rusin vmw_user_bo_unref(&vbo);
557298799a2SZack Rusin if (unlikely(ret != 0)) {
558298799a2SZack Rusin if (ret == -ERESTARTSYS || ret == -EBUSY)
559298799a2SZack Rusin return -EBUSY;
560e9431ea5SThomas Hellstrom DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n",
561e9431ea5SThomas Hellstrom (unsigned int) arg->handle);
562e9431ea5SThomas Hellstrom return ret;
563e9431ea5SThomas Hellstrom }
564e9431ea5SThomas Hellstrom break;
565e9431ea5SThomas Hellstrom case drm_vmw_synccpu_release:
5668afa13a0SZack Rusin ret = vmw_user_bo_synccpu_release(file_priv,
5678afa13a0SZack Rusin arg->handle,
568e9431ea5SThomas Hellstrom arg->flags);
569e9431ea5SThomas Hellstrom if (unlikely(ret != 0)) {
570e9431ea5SThomas Hellstrom DRM_ERROR("Failed synccpu release on handle 0x%08x.\n",
571e9431ea5SThomas Hellstrom (unsigned int) arg->handle);
572e9431ea5SThomas Hellstrom return ret;
573e9431ea5SThomas Hellstrom }
574e9431ea5SThomas Hellstrom break;
575e9431ea5SThomas Hellstrom default:
576e9431ea5SThomas Hellstrom DRM_ERROR("Invalid synccpu operation.\n");
577e9431ea5SThomas Hellstrom return -EINVAL;
578e9431ea5SThomas Hellstrom }
579e9431ea5SThomas Hellstrom
580e9431ea5SThomas Hellstrom return 0;
581e9431ea5SThomas Hellstrom }
582e9431ea5SThomas Hellstrom
583e9431ea5SThomas Hellstrom /**
584e9431ea5SThomas Hellstrom * vmw_bo_unref_ioctl - Generic handle close ioctl.
585e9431ea5SThomas Hellstrom *
586e9431ea5SThomas Hellstrom * @dev: Identifies the drm device.
587e9431ea5SThomas Hellstrom * @data: Pointer to the ioctl argument.
588e9431ea5SThomas Hellstrom * @file_priv: Identifies the caller.
589e9431ea5SThomas Hellstrom * Return: Zero on success, negative error code on error.
590e9431ea5SThomas Hellstrom *
591e9431ea5SThomas Hellstrom * This function checks the ioctl arguments for validity and closes a
592e9431ea5SThomas Hellstrom * handle to a TTM base object, optionally freeing the object.
593e9431ea5SThomas Hellstrom */
vmw_bo_unref_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)594e9431ea5SThomas Hellstrom int vmw_bo_unref_ioctl(struct drm_device *dev, void *data,
595e9431ea5SThomas Hellstrom struct drm_file *file_priv)
596e9431ea5SThomas Hellstrom {
597e9431ea5SThomas Hellstrom struct drm_vmw_unref_dmabuf_arg *arg =
598e9431ea5SThomas Hellstrom (struct drm_vmw_unref_dmabuf_arg *)data;
599e9431ea5SThomas Hellstrom
600668b2066SZack Rusin return drm_gem_handle_delete(file_priv, arg->handle);
601e9431ea5SThomas Hellstrom }
602e9431ea5SThomas Hellstrom
603e9431ea5SThomas Hellstrom
604e9431ea5SThomas Hellstrom /**
605e9431ea5SThomas Hellstrom * vmw_user_bo_lookup - Look up a vmw user buffer object from a handle.
606e9431ea5SThomas Hellstrom *
6078afa13a0SZack Rusin * @filp: The file the handle is registered with.
608e9431ea5SThomas Hellstrom * @handle: The user buffer object handle
609e9431ea5SThomas Hellstrom * @out: Pointer to a where a pointer to the embedded
61009881d29SZack Rusin * struct vmw_bo should be placed.
611e9431ea5SThomas Hellstrom * Return: Zero on success, Negative error code on error.
612e9431ea5SThomas Hellstrom *
6139ef8d83eSZack Rusin * The vmw buffer object pointer will be refcounted (both ttm and gem)
614e9431ea5SThomas Hellstrom */
vmw_user_bo_lookup(struct drm_file * filp,u32 handle,struct vmw_bo ** out)6158afa13a0SZack Rusin int vmw_user_bo_lookup(struct drm_file *filp,
616668b2066SZack Rusin u32 handle,
61709881d29SZack Rusin struct vmw_bo **out)
618e9431ea5SThomas Hellstrom {
6198afa13a0SZack Rusin struct drm_gem_object *gobj;
620e9431ea5SThomas Hellstrom
6218afa13a0SZack Rusin gobj = drm_gem_object_lookup(filp, handle);
6228afa13a0SZack Rusin if (!gobj) {
623e9431ea5SThomas Hellstrom DRM_ERROR("Invalid buffer object handle 0x%08lx.\n",
624e9431ea5SThomas Hellstrom (unsigned long)handle);
625e9431ea5SThomas Hellstrom return -ESRCH;
626e9431ea5SThomas Hellstrom }
627e9431ea5SThomas Hellstrom
62809881d29SZack Rusin *out = to_vmw_bo(gobj);
629e9431ea5SThomas Hellstrom
630e9431ea5SThomas Hellstrom return 0;
631e9431ea5SThomas Hellstrom }
632e9431ea5SThomas Hellstrom
633b733bc2eSThomas Hellstrom /**
634e9431ea5SThomas Hellstrom * vmw_bo_fence_single - Utility function to fence a single TTM buffer
635e9431ea5SThomas Hellstrom * object without unreserving it.
636e9431ea5SThomas Hellstrom *
637e9431ea5SThomas Hellstrom * @bo: Pointer to the struct ttm_buffer_object to fence.
638e9431ea5SThomas Hellstrom * @fence: Pointer to the fence. If NULL, this function will
639e9431ea5SThomas Hellstrom * insert a fence into the command stream..
640e9431ea5SThomas Hellstrom *
641e9431ea5SThomas Hellstrom * Contrary to the ttm_eu version of this function, it takes only
642e9431ea5SThomas Hellstrom * a single buffer object instead of a list, and it also doesn't
643e9431ea5SThomas Hellstrom * unreserve the buffer object, which needs to be done separately.
644e9431ea5SThomas Hellstrom */
vmw_bo_fence_single(struct ttm_buffer_object * bo,struct vmw_fence_obj * fence)645e9431ea5SThomas Hellstrom void vmw_bo_fence_single(struct ttm_buffer_object *bo,
646e9431ea5SThomas Hellstrom struct vmw_fence_obj *fence)
647e9431ea5SThomas Hellstrom {
6488af8a109SChristian König struct ttm_device *bdev = bo->bdev;
649668b2066SZack Rusin struct vmw_private *dev_priv = vmw_priv_from_ttm(bdev);
650c8d4c18bSChristian König int ret;
651e9431ea5SThomas Hellstrom
652c8d4c18bSChristian König if (fence == NULL)
653e9431ea5SThomas Hellstrom vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
654c8d4c18bSChristian König else
655c8d4c18bSChristian König dma_fence_get(&fence->base);
656c8d4c18bSChristian König
657c8d4c18bSChristian König ret = dma_resv_reserve_fences(bo->base.resv, 1);
658c8d4c18bSChristian König if (!ret)
65973511edfSChristian König dma_resv_add_fence(bo->base.resv, &fence->base,
660b29895e1SChristian König DMA_RESV_USAGE_KERNEL);
661c8d4c18bSChristian König else
662c8d4c18bSChristian König /* Last resort fallback when we are OOM */
663c8d4c18bSChristian König dma_fence_wait(&fence->base, false);
664e9431ea5SThomas Hellstrom dma_fence_put(&fence->base);
665e9431ea5SThomas Hellstrom }
666e9431ea5SThomas Hellstrom
667e9431ea5SThomas Hellstrom
668e9431ea5SThomas Hellstrom /**
669e9431ea5SThomas Hellstrom * vmw_dumb_create - Create a dumb kms buffer
670e9431ea5SThomas Hellstrom *
671e9431ea5SThomas Hellstrom * @file_priv: Pointer to a struct drm_file identifying the caller.
672e9431ea5SThomas Hellstrom * @dev: Pointer to the drm device.
673e9431ea5SThomas Hellstrom * @args: Pointer to a struct drm_mode_create_dumb structure
674e9431ea5SThomas Hellstrom * Return: Zero on success, negative error code on failure.
675e9431ea5SThomas Hellstrom *
676e9431ea5SThomas Hellstrom * This is a driver callback for the core drm create_dumb functionality.
677e9431ea5SThomas Hellstrom * Note that this is very similar to the vmw_bo_alloc ioctl, except
678e9431ea5SThomas Hellstrom * that the arguments have a different format.
679e9431ea5SThomas Hellstrom */
vmw_dumb_create(struct drm_file * file_priv,struct drm_device * dev,struct drm_mode_create_dumb * args)680e9431ea5SThomas Hellstrom int vmw_dumb_create(struct drm_file *file_priv,
681e9431ea5SThomas Hellstrom struct drm_device *dev,
682e9431ea5SThomas Hellstrom struct drm_mode_create_dumb *args)
683e9431ea5SThomas Hellstrom {
684e9431ea5SThomas Hellstrom struct vmw_private *dev_priv = vmw_priv(dev);
68509881d29SZack Rusin struct vmw_bo *vbo;
6861c8d537bSZack Rusin int cpp = DIV_ROUND_UP(args->bpp, 8);
687e9431ea5SThomas Hellstrom int ret;
688e9431ea5SThomas Hellstrom
6891c8d537bSZack Rusin switch (cpp) {
6901c8d537bSZack Rusin case 1: /* DRM_FORMAT_C8 */
6911c8d537bSZack Rusin case 2: /* DRM_FORMAT_RGB565 */
6921c8d537bSZack Rusin case 4: /* DRM_FORMAT_XRGB8888 */
6931c8d537bSZack Rusin break;
6941c8d537bSZack Rusin default:
6951c8d537bSZack Rusin /*
6961c8d537bSZack Rusin * Dumb buffers don't allow anything else.
6971c8d537bSZack Rusin * This is tested via IGT's dumb_buffers
6981c8d537bSZack Rusin */
6991c8d537bSZack Rusin return -EINVAL;
7001c8d537bSZack Rusin }
7011c8d537bSZack Rusin
7021c8d537bSZack Rusin args->pitch = args->width * cpp;
7038afa13a0SZack Rusin args->size = ALIGN(args->pitch * args->height, PAGE_SIZE);
704e9431ea5SThomas Hellstrom
7058afa13a0SZack Rusin ret = vmw_gem_object_create_with_handle(dev_priv, file_priv,
7068afa13a0SZack Rusin args->size, &args->handle,
7078afa13a0SZack Rusin &vbo);
7089ef8d83eSZack Rusin /* drop reference from allocate - handle holds it now */
7099ef8d83eSZack Rusin drm_gem_object_put(&vbo->tbo.base);
710e9431ea5SThomas Hellstrom return ret;
711e9431ea5SThomas Hellstrom }
712e9431ea5SThomas Hellstrom
713e9431ea5SThomas Hellstrom /**
714e9431ea5SThomas Hellstrom * vmw_bo_swap_notify - swapout notify callback.
715e9431ea5SThomas Hellstrom *
716e9431ea5SThomas Hellstrom * @bo: The buffer object to be swapped out.
717e9431ea5SThomas Hellstrom */
vmw_bo_swap_notify(struct ttm_buffer_object * bo)718e9431ea5SThomas Hellstrom void vmw_bo_swap_notify(struct ttm_buffer_object *bo)
719e9431ea5SThomas Hellstrom {
720e9431ea5SThomas Hellstrom /* Kill any cached kernel maps before swapout */
72109881d29SZack Rusin vmw_bo_unmap(to_vmw_bo(&bo->base));
722e9431ea5SThomas Hellstrom }
723e9431ea5SThomas Hellstrom
724e9431ea5SThomas Hellstrom
725e9431ea5SThomas Hellstrom /**
726e9431ea5SThomas Hellstrom * vmw_bo_move_notify - TTM move_notify_callback
727e9431ea5SThomas Hellstrom *
728e9431ea5SThomas Hellstrom * @bo: The TTM buffer object about to move.
7292966141aSDave Airlie * @mem: The struct ttm_resource indicating to what memory
730e9431ea5SThomas Hellstrom * region the move is taking place.
731e9431ea5SThomas Hellstrom *
732e9431ea5SThomas Hellstrom * Detaches cached maps and device bindings that require that the
733e9431ea5SThomas Hellstrom * buffer doesn't move.
734e9431ea5SThomas Hellstrom */
vmw_bo_move_notify(struct ttm_buffer_object * bo,struct ttm_resource * mem)735e9431ea5SThomas Hellstrom void vmw_bo_move_notify(struct ttm_buffer_object *bo,
7362966141aSDave Airlie struct ttm_resource *mem)
737e9431ea5SThomas Hellstrom {
738668b2066SZack Rusin struct vmw_bo *vbo = to_vmw_bo(&bo->base);
739e9431ea5SThomas Hellstrom
740e9431ea5SThomas Hellstrom /*
741098d7d53SThomas Hellstrom * Kill any cached kernel maps before move to or from VRAM.
742098d7d53SThomas Hellstrom * With other types of moves, the underlying pages stay the same,
743098d7d53SThomas Hellstrom * and the map can be kept.
744e9431ea5SThomas Hellstrom */
745d3116756SChristian König if (mem->mem_type == TTM_PL_VRAM || bo->resource->mem_type == TTM_PL_VRAM)
746e9431ea5SThomas Hellstrom vmw_bo_unmap(vbo);
747e9431ea5SThomas Hellstrom
748e9431ea5SThomas Hellstrom /*
749e9431ea5SThomas Hellstrom * If we're moving a backup MOB out of MOB placement, then make sure we
750e9431ea5SThomas Hellstrom * read back all resource content first, and unbind the MOB from
751e9431ea5SThomas Hellstrom * the resource.
752e9431ea5SThomas Hellstrom */
753d3116756SChristian König if (mem->mem_type != VMW_PL_MOB && bo->resource->mem_type == VMW_PL_MOB)
754e9431ea5SThomas Hellstrom vmw_resource_unbind_list(vbo);
755e9431ea5SThomas Hellstrom }
75639985eeaSZack Rusin
75739985eeaSZack Rusin static u32
set_placement_list(struct ttm_place * pl,u32 domain)75839985eeaSZack Rusin set_placement_list(struct ttm_place *pl, u32 domain)
75939985eeaSZack Rusin {
76039985eeaSZack Rusin u32 n = 0;
76139985eeaSZack Rusin
76239985eeaSZack Rusin /*
76339985eeaSZack Rusin * The placements are ordered according to our preferences
76439985eeaSZack Rusin */
76539985eeaSZack Rusin if (domain & VMW_BO_DOMAIN_MOB) {
76639985eeaSZack Rusin pl[n].mem_type = VMW_PL_MOB;
76739985eeaSZack Rusin pl[n].flags = 0;
76839985eeaSZack Rusin pl[n].fpfn = 0;
76939985eeaSZack Rusin pl[n].lpfn = 0;
77039985eeaSZack Rusin n++;
77139985eeaSZack Rusin }
77239985eeaSZack Rusin if (domain & VMW_BO_DOMAIN_GMR) {
77339985eeaSZack Rusin pl[n].mem_type = VMW_PL_GMR;
77439985eeaSZack Rusin pl[n].flags = 0;
77539985eeaSZack Rusin pl[n].fpfn = 0;
77639985eeaSZack Rusin pl[n].lpfn = 0;
77739985eeaSZack Rusin n++;
77839985eeaSZack Rusin }
77939985eeaSZack Rusin if (domain & VMW_BO_DOMAIN_VRAM) {
78039985eeaSZack Rusin pl[n].mem_type = TTM_PL_VRAM;
78139985eeaSZack Rusin pl[n].flags = 0;
78239985eeaSZack Rusin pl[n].fpfn = 0;
78339985eeaSZack Rusin pl[n].lpfn = 0;
78439985eeaSZack Rusin n++;
78539985eeaSZack Rusin }
78639985eeaSZack Rusin if (domain & VMW_BO_DOMAIN_WAITABLE_SYS) {
78739985eeaSZack Rusin pl[n].mem_type = VMW_PL_SYSTEM;
78839985eeaSZack Rusin pl[n].flags = 0;
78939985eeaSZack Rusin pl[n].fpfn = 0;
79039985eeaSZack Rusin pl[n].lpfn = 0;
79139985eeaSZack Rusin n++;
79239985eeaSZack Rusin }
79339985eeaSZack Rusin if (domain & VMW_BO_DOMAIN_SYS) {
79439985eeaSZack Rusin pl[n].mem_type = TTM_PL_SYSTEM;
79539985eeaSZack Rusin pl[n].flags = 0;
79639985eeaSZack Rusin pl[n].fpfn = 0;
79739985eeaSZack Rusin pl[n].lpfn = 0;
79839985eeaSZack Rusin n++;
79939985eeaSZack Rusin }
80039985eeaSZack Rusin
80139985eeaSZack Rusin WARN_ON(!n);
80239985eeaSZack Rusin if (!n) {
80339985eeaSZack Rusin pl[n].mem_type = TTM_PL_SYSTEM;
80439985eeaSZack Rusin pl[n].flags = 0;
80539985eeaSZack Rusin pl[n].fpfn = 0;
80639985eeaSZack Rusin pl[n].lpfn = 0;
80739985eeaSZack Rusin n++;
80839985eeaSZack Rusin }
80939985eeaSZack Rusin return n;
81039985eeaSZack Rusin }
81139985eeaSZack Rusin
vmw_bo_placement_set(struct vmw_bo * bo,u32 domain,u32 busy_domain)81239985eeaSZack Rusin void vmw_bo_placement_set(struct vmw_bo *bo, u32 domain, u32 busy_domain)
81339985eeaSZack Rusin {
814668b2066SZack Rusin struct ttm_device *bdev = bo->tbo.bdev;
815668b2066SZack Rusin struct vmw_private *vmw = vmw_priv_from_ttm(bdev);
81639985eeaSZack Rusin struct ttm_placement *pl = &bo->placement;
81739985eeaSZack Rusin bool mem_compatible = false;
81839985eeaSZack Rusin u32 i;
81939985eeaSZack Rusin
82039985eeaSZack Rusin pl->placement = bo->places;
82139985eeaSZack Rusin pl->num_placement = set_placement_list(bo->places, domain);
82239985eeaSZack Rusin
823668b2066SZack Rusin if (drm_debug_enabled(DRM_UT_DRIVER) && bo->tbo.resource) {
82439985eeaSZack Rusin for (i = 0; i < pl->num_placement; ++i) {
825668b2066SZack Rusin if (bo->tbo.resource->mem_type == TTM_PL_SYSTEM ||
826668b2066SZack Rusin bo->tbo.resource->mem_type == pl->placement[i].mem_type)
82739985eeaSZack Rusin mem_compatible = true;
82839985eeaSZack Rusin }
82939985eeaSZack Rusin if (!mem_compatible)
83039985eeaSZack Rusin drm_warn(&vmw->drm,
83139985eeaSZack Rusin "%s: Incompatible transition from "
83239985eeaSZack Rusin "bo->base.resource->mem_type = %u to domain = %u\n",
833668b2066SZack Rusin __func__, bo->tbo.resource->mem_type, domain);
83439985eeaSZack Rusin }
83539985eeaSZack Rusin
83639985eeaSZack Rusin pl->busy_placement = bo->busy_places;
83739985eeaSZack Rusin pl->num_busy_placement = set_placement_list(bo->busy_places, busy_domain);
83839985eeaSZack Rusin }
83939985eeaSZack Rusin
vmw_bo_placement_set_default_accelerated(struct vmw_bo * bo)84039985eeaSZack Rusin void vmw_bo_placement_set_default_accelerated(struct vmw_bo *bo)
84139985eeaSZack Rusin {
842668b2066SZack Rusin struct ttm_device *bdev = bo->tbo.bdev;
843668b2066SZack Rusin struct vmw_private *vmw = vmw_priv_from_ttm(bdev);
84439985eeaSZack Rusin u32 domain = VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM;
84539985eeaSZack Rusin
84639985eeaSZack Rusin if (vmw->has_mob)
84739985eeaSZack Rusin domain = VMW_BO_DOMAIN_MOB;
84839985eeaSZack Rusin
84939985eeaSZack Rusin vmw_bo_placement_set(bo, domain, domain);
85039985eeaSZack Rusin }
851