18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright © 2014 Broadcom
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the next
128c2ecf20Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
138c2ecf20Sopenharmony_ci * Software.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188c2ecf20Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
208c2ecf20Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
218c2ecf20Sopenharmony_ci * IN THE SOFTWARE.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include <linux/module.h>
258c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
268c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
278c2ecf20Sopenharmony_ci#include <linux/device.h>
288c2ecf20Sopenharmony_ci#include <linux/io.h>
298c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
308c2ecf20Sopenharmony_ci#include <linux/dma-fence-array.h>
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include <drm/drm_syncobj.h>
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#include "uapi/drm/vc4_drm.h"
358c2ecf20Sopenharmony_ci#include "vc4_drv.h"
368c2ecf20Sopenharmony_ci#include "vc4_regs.h"
378c2ecf20Sopenharmony_ci#include "vc4_trace.h"
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic void
408c2ecf20Sopenharmony_civc4_queue_hangcheck(struct drm_device *dev)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	mod_timer(&vc4->hangcheck.timer,
458c2ecf20Sopenharmony_ci		  round_jiffies_up(jiffies + msecs_to_jiffies(100)));
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistruct vc4_hang_state {
498c2ecf20Sopenharmony_ci	struct drm_vc4_get_hang_state user_state;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	u32 bo_count;
528c2ecf20Sopenharmony_ci	struct drm_gem_object **bo;
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic void
568c2ecf20Sopenharmony_civc4_free_hang_state(struct drm_device *dev, struct vc4_hang_state *state)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	unsigned int i;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	for (i = 0; i < state->user_state.bo_count; i++)
618c2ecf20Sopenharmony_ci		drm_gem_object_put(state->bo[i]);
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	kfree(state);
648c2ecf20Sopenharmony_ci}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ciint
678c2ecf20Sopenharmony_civc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
688c2ecf20Sopenharmony_ci			 struct drm_file *file_priv)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct drm_vc4_get_hang_state *get_state = data;
718c2ecf20Sopenharmony_ci	struct drm_vc4_get_hang_state_bo *bo_state;
728c2ecf20Sopenharmony_ci	struct vc4_hang_state *kernel_state;
738c2ecf20Sopenharmony_ci	struct drm_vc4_get_hang_state *state;
748c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
758c2ecf20Sopenharmony_ci	unsigned long irqflags;
768c2ecf20Sopenharmony_ci	u32 i;
778c2ecf20Sopenharmony_ci	int ret = 0;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (!vc4->v3d) {
808c2ecf20Sopenharmony_ci		DRM_DEBUG("VC4_GET_HANG_STATE with no VC4 V3D probed\n");
818c2ecf20Sopenharmony_ci		return -ENODEV;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vc4->job_lock, irqflags);
858c2ecf20Sopenharmony_ci	kernel_state = vc4->hang_state;
868c2ecf20Sopenharmony_ci	if (!kernel_state) {
878c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
888c2ecf20Sopenharmony_ci		return -ENOENT;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci	state = &kernel_state->user_state;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/* If the user's array isn't big enough, just return the
938c2ecf20Sopenharmony_ci	 * required array size.
948c2ecf20Sopenharmony_ci	 */
958c2ecf20Sopenharmony_ci	if (get_state->bo_count < state->bo_count) {
968c2ecf20Sopenharmony_ci		get_state->bo_count = state->bo_count;
978c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
988c2ecf20Sopenharmony_ci		return 0;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	vc4->hang_state = NULL;
1028c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	/* Save the user's BO pointer, so we don't stomp it with the memcpy. */
1058c2ecf20Sopenharmony_ci	state->bo = get_state->bo;
1068c2ecf20Sopenharmony_ci	memcpy(get_state, state, sizeof(*state));
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	bo_state = kcalloc(state->bo_count, sizeof(*bo_state), GFP_KERNEL);
1098c2ecf20Sopenharmony_ci	if (!bo_state) {
1108c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1118c2ecf20Sopenharmony_ci		goto err_free;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	for (i = 0; i < state->bo_count; i++) {
1158c2ecf20Sopenharmony_ci		struct vc4_bo *vc4_bo = to_vc4_bo(kernel_state->bo[i]);
1168c2ecf20Sopenharmony_ci		u32 handle;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci		ret = drm_gem_handle_create(file_priv, kernel_state->bo[i],
1198c2ecf20Sopenharmony_ci					    &handle);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci		if (ret) {
1228c2ecf20Sopenharmony_ci			state->bo_count = i;
1238c2ecf20Sopenharmony_ci			goto err_delete_handle;
1248c2ecf20Sopenharmony_ci		}
1258c2ecf20Sopenharmony_ci		bo_state[i].handle = handle;
1268c2ecf20Sopenharmony_ci		bo_state[i].paddr = vc4_bo->base.paddr;
1278c2ecf20Sopenharmony_ci		bo_state[i].size = vc4_bo->base.base.size;
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	if (copy_to_user(u64_to_user_ptr(get_state->bo),
1318c2ecf20Sopenharmony_ci			 bo_state,
1328c2ecf20Sopenharmony_ci			 state->bo_count * sizeof(*bo_state)))
1338c2ecf20Sopenharmony_ci		ret = -EFAULT;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cierr_delete_handle:
1368c2ecf20Sopenharmony_ci	if (ret) {
1378c2ecf20Sopenharmony_ci		for (i = 0; i < state->bo_count; i++)
1388c2ecf20Sopenharmony_ci			drm_gem_handle_delete(file_priv, bo_state[i].handle);
1398c2ecf20Sopenharmony_ci	}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cierr_free:
1428c2ecf20Sopenharmony_ci	vc4_free_hang_state(dev, kernel_state);
1438c2ecf20Sopenharmony_ci	kfree(bo_state);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	return ret;
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic void
1498c2ecf20Sopenharmony_civc4_save_hang_state(struct drm_device *dev)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
1528c2ecf20Sopenharmony_ci	struct drm_vc4_get_hang_state *state;
1538c2ecf20Sopenharmony_ci	struct vc4_hang_state *kernel_state;
1548c2ecf20Sopenharmony_ci	struct vc4_exec_info *exec[2];
1558c2ecf20Sopenharmony_ci	struct vc4_bo *bo;
1568c2ecf20Sopenharmony_ci	unsigned long irqflags;
1578c2ecf20Sopenharmony_ci	unsigned int i, j, k, unref_list_count;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	kernel_state = kcalloc(1, sizeof(*kernel_state), GFP_KERNEL);
1608c2ecf20Sopenharmony_ci	if (!kernel_state)
1618c2ecf20Sopenharmony_ci		return;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	state = &kernel_state->user_state;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vc4->job_lock, irqflags);
1668c2ecf20Sopenharmony_ci	exec[0] = vc4_first_bin_job(vc4);
1678c2ecf20Sopenharmony_ci	exec[1] = vc4_first_render_job(vc4);
1688c2ecf20Sopenharmony_ci	if (!exec[0] && !exec[1]) {
1698c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
1708c2ecf20Sopenharmony_ci		return;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* Get the bos from both binner and renderer into hang state. */
1748c2ecf20Sopenharmony_ci	state->bo_count = 0;
1758c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
1768c2ecf20Sopenharmony_ci		if (!exec[i])
1778c2ecf20Sopenharmony_ci			continue;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci		unref_list_count = 0;
1808c2ecf20Sopenharmony_ci		list_for_each_entry(bo, &exec[i]->unref_list, unref_head)
1818c2ecf20Sopenharmony_ci			unref_list_count++;
1828c2ecf20Sopenharmony_ci		state->bo_count += exec[i]->bo_count + unref_list_count;
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	kernel_state->bo = kcalloc(state->bo_count,
1868c2ecf20Sopenharmony_ci				   sizeof(*kernel_state->bo), GFP_ATOMIC);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	if (!kernel_state->bo) {
1898c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
1908c2ecf20Sopenharmony_ci		return;
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	k = 0;
1948c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
1958c2ecf20Sopenharmony_ci		if (!exec[i])
1968c2ecf20Sopenharmony_ci			continue;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci		for (j = 0; j < exec[i]->bo_count; j++) {
1998c2ecf20Sopenharmony_ci			bo = to_vc4_bo(&exec[i]->bo[j]->base);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci			/* Retain BOs just in case they were marked purgeable.
2028c2ecf20Sopenharmony_ci			 * This prevents the BO from being purged before
2038c2ecf20Sopenharmony_ci			 * someone had a chance to dump the hang state.
2048c2ecf20Sopenharmony_ci			 */
2058c2ecf20Sopenharmony_ci			WARN_ON(!refcount_read(&bo->usecnt));
2068c2ecf20Sopenharmony_ci			refcount_inc(&bo->usecnt);
2078c2ecf20Sopenharmony_ci			drm_gem_object_get(&exec[i]->bo[j]->base);
2088c2ecf20Sopenharmony_ci			kernel_state->bo[k++] = &exec[i]->bo[j]->base;
2098c2ecf20Sopenharmony_ci		}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci		list_for_each_entry(bo, &exec[i]->unref_list, unref_head) {
2128c2ecf20Sopenharmony_ci			/* No need to retain BOs coming from the ->unref_list
2138c2ecf20Sopenharmony_ci			 * because they are naturally unpurgeable.
2148c2ecf20Sopenharmony_ci			 */
2158c2ecf20Sopenharmony_ci			drm_gem_object_get(&bo->base.base);
2168c2ecf20Sopenharmony_ci			kernel_state->bo[k++] = &bo->base.base;
2178c2ecf20Sopenharmony_ci		}
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	WARN_ON_ONCE(k != state->bo_count);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	if (exec[0])
2238c2ecf20Sopenharmony_ci		state->start_bin = exec[0]->ct0ca;
2248c2ecf20Sopenharmony_ci	if (exec[1])
2258c2ecf20Sopenharmony_ci		state->start_render = exec[1]->ct1ca;
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	state->ct0ca = V3D_READ(V3D_CTNCA(0));
2308c2ecf20Sopenharmony_ci	state->ct0ea = V3D_READ(V3D_CTNEA(0));
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	state->ct1ca = V3D_READ(V3D_CTNCA(1));
2338c2ecf20Sopenharmony_ci	state->ct1ea = V3D_READ(V3D_CTNEA(1));
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	state->ct0cs = V3D_READ(V3D_CTNCS(0));
2368c2ecf20Sopenharmony_ci	state->ct1cs = V3D_READ(V3D_CTNCS(1));
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	state->ct0ra0 = V3D_READ(V3D_CT00RA0);
2398c2ecf20Sopenharmony_ci	state->ct1ra0 = V3D_READ(V3D_CT01RA0);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	state->bpca = V3D_READ(V3D_BPCA);
2428c2ecf20Sopenharmony_ci	state->bpcs = V3D_READ(V3D_BPCS);
2438c2ecf20Sopenharmony_ci	state->bpoa = V3D_READ(V3D_BPOA);
2448c2ecf20Sopenharmony_ci	state->bpos = V3D_READ(V3D_BPOS);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	state->vpmbase = V3D_READ(V3D_VPMBASE);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	state->dbge = V3D_READ(V3D_DBGE);
2498c2ecf20Sopenharmony_ci	state->fdbgo = V3D_READ(V3D_FDBGO);
2508c2ecf20Sopenharmony_ci	state->fdbgb = V3D_READ(V3D_FDBGB);
2518c2ecf20Sopenharmony_ci	state->fdbgr = V3D_READ(V3D_FDBGR);
2528c2ecf20Sopenharmony_ci	state->fdbgs = V3D_READ(V3D_FDBGS);
2538c2ecf20Sopenharmony_ci	state->errstat = V3D_READ(V3D_ERRSTAT);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	/* We need to turn purgeable BOs into unpurgeable ones so that
2568c2ecf20Sopenharmony_ci	 * userspace has a chance to dump the hang state before the kernel
2578c2ecf20Sopenharmony_ci	 * decides to purge those BOs.
2588c2ecf20Sopenharmony_ci	 * Note that BO consistency at dump time cannot be guaranteed. For
2598c2ecf20Sopenharmony_ci	 * example, if the owner of these BOs decides to re-use them or mark
2608c2ecf20Sopenharmony_ci	 * them purgeable again there's nothing we can do to prevent it.
2618c2ecf20Sopenharmony_ci	 */
2628c2ecf20Sopenharmony_ci	for (i = 0; i < kernel_state->user_state.bo_count; i++) {
2638c2ecf20Sopenharmony_ci		struct vc4_bo *bo = to_vc4_bo(kernel_state->bo[i]);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci		if (bo->madv == __VC4_MADV_NOTSUPP)
2668c2ecf20Sopenharmony_ci			continue;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci		mutex_lock(&bo->madv_lock);
2698c2ecf20Sopenharmony_ci		if (!WARN_ON(bo->madv == __VC4_MADV_PURGED))
2708c2ecf20Sopenharmony_ci			bo->madv = VC4_MADV_WILLNEED;
2718c2ecf20Sopenharmony_ci		refcount_dec(&bo->usecnt);
2728c2ecf20Sopenharmony_ci		mutex_unlock(&bo->madv_lock);
2738c2ecf20Sopenharmony_ci	}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vc4->job_lock, irqflags);
2768c2ecf20Sopenharmony_ci	if (vc4->hang_state) {
2778c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
2788c2ecf20Sopenharmony_ci		vc4_free_hang_state(dev, kernel_state);
2798c2ecf20Sopenharmony_ci	} else {
2808c2ecf20Sopenharmony_ci		vc4->hang_state = kernel_state;
2818c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
2828c2ecf20Sopenharmony_ci	}
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_cistatic void
2868c2ecf20Sopenharmony_civc4_reset(struct drm_device *dev)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	DRM_INFO("Resetting GPU.\n");
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	mutex_lock(&vc4->power_lock);
2938c2ecf20Sopenharmony_ci	if (vc4->power_refcount) {
2948c2ecf20Sopenharmony_ci		/* Power the device off and back on the by dropping the
2958c2ecf20Sopenharmony_ci		 * reference on runtime PM.
2968c2ecf20Sopenharmony_ci		 */
2978c2ecf20Sopenharmony_ci		pm_runtime_put_sync_suspend(&vc4->v3d->pdev->dev);
2988c2ecf20Sopenharmony_ci		pm_runtime_get_sync(&vc4->v3d->pdev->dev);
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci	mutex_unlock(&vc4->power_lock);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	vc4_irq_reset(dev);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	/* Rearm the hangcheck -- another job might have been waiting
3058c2ecf20Sopenharmony_ci	 * for our hung one to get kicked off, and vc4_irq_reset()
3068c2ecf20Sopenharmony_ci	 * would have started it.
3078c2ecf20Sopenharmony_ci	 */
3088c2ecf20Sopenharmony_ci	vc4_queue_hangcheck(dev);
3098c2ecf20Sopenharmony_ci}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistatic void
3128c2ecf20Sopenharmony_civc4_reset_work(struct work_struct *work)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 =
3158c2ecf20Sopenharmony_ci		container_of(work, struct vc4_dev, hangcheck.reset_work);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	vc4_save_hang_state(&vc4->base);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	vc4_reset(&vc4->base);
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic void
3238c2ecf20Sopenharmony_civc4_hangcheck_elapsed(struct timer_list *t)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = from_timer(vc4, t, hangcheck.timer);
3268c2ecf20Sopenharmony_ci	struct drm_device *dev = &vc4->base;
3278c2ecf20Sopenharmony_ci	uint32_t ct0ca, ct1ca;
3288c2ecf20Sopenharmony_ci	unsigned long irqflags;
3298c2ecf20Sopenharmony_ci	struct vc4_exec_info *bin_exec, *render_exec;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vc4->job_lock, irqflags);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	bin_exec = vc4_first_bin_job(vc4);
3348c2ecf20Sopenharmony_ci	render_exec = vc4_first_render_job(vc4);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	/* If idle, we can stop watching for hangs. */
3378c2ecf20Sopenharmony_ci	if (!bin_exec && !render_exec) {
3388c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
3398c2ecf20Sopenharmony_ci		return;
3408c2ecf20Sopenharmony_ci	}
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	ct0ca = V3D_READ(V3D_CTNCA(0));
3438c2ecf20Sopenharmony_ci	ct1ca = V3D_READ(V3D_CTNCA(1));
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	/* If we've made any progress in execution, rearm the timer
3468c2ecf20Sopenharmony_ci	 * and wait.
3478c2ecf20Sopenharmony_ci	 */
3488c2ecf20Sopenharmony_ci	if ((bin_exec && ct0ca != bin_exec->last_ct0ca) ||
3498c2ecf20Sopenharmony_ci	    (render_exec && ct1ca != render_exec->last_ct1ca)) {
3508c2ecf20Sopenharmony_ci		if (bin_exec)
3518c2ecf20Sopenharmony_ci			bin_exec->last_ct0ca = ct0ca;
3528c2ecf20Sopenharmony_ci		if (render_exec)
3538c2ecf20Sopenharmony_ci			render_exec->last_ct1ca = ct1ca;
3548c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
3558c2ecf20Sopenharmony_ci		vc4_queue_hangcheck(dev);
3568c2ecf20Sopenharmony_ci		return;
3578c2ecf20Sopenharmony_ci	}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	/* We've gone too long with no progress, reset.  This has to
3628c2ecf20Sopenharmony_ci	 * be done from a work struct, since resetting can sleep and
3638c2ecf20Sopenharmony_ci	 * this timer hook isn't allowed to.
3648c2ecf20Sopenharmony_ci	 */
3658c2ecf20Sopenharmony_ci	schedule_work(&vc4->hangcheck.reset_work);
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic void
3698c2ecf20Sopenharmony_cisubmit_cl(struct drm_device *dev, uint32_t thread, uint32_t start, uint32_t end)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	/* Set the current and end address of the control list.
3748c2ecf20Sopenharmony_ci	 * Writing the end register is what starts the job.
3758c2ecf20Sopenharmony_ci	 */
3768c2ecf20Sopenharmony_ci	V3D_WRITE(V3D_CTNCA(thread), start);
3778c2ecf20Sopenharmony_ci	V3D_WRITE(V3D_CTNEA(thread), end);
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ciint
3818c2ecf20Sopenharmony_civc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno, uint64_t timeout_ns,
3828c2ecf20Sopenharmony_ci		   bool interruptible)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
3858c2ecf20Sopenharmony_ci	int ret = 0;
3868c2ecf20Sopenharmony_ci	unsigned long timeout_expire;
3878c2ecf20Sopenharmony_ci	DEFINE_WAIT(wait);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	if (vc4->finished_seqno >= seqno)
3908c2ecf20Sopenharmony_ci		return 0;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	if (timeout_ns == 0)
3938c2ecf20Sopenharmony_ci		return -ETIME;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	timeout_expire = jiffies + nsecs_to_jiffies(timeout_ns);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	trace_vc4_wait_for_seqno_begin(dev, seqno, timeout_ns);
3988c2ecf20Sopenharmony_ci	for (;;) {
3998c2ecf20Sopenharmony_ci		prepare_to_wait(&vc4->job_wait_queue, &wait,
4008c2ecf20Sopenharmony_ci				interruptible ? TASK_INTERRUPTIBLE :
4018c2ecf20Sopenharmony_ci				TASK_UNINTERRUPTIBLE);
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci		if (interruptible && signal_pending(current)) {
4048c2ecf20Sopenharmony_ci			ret = -ERESTARTSYS;
4058c2ecf20Sopenharmony_ci			break;
4068c2ecf20Sopenharmony_ci		}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci		if (vc4->finished_seqno >= seqno)
4098c2ecf20Sopenharmony_ci			break;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci		if (timeout_ns != ~0ull) {
4128c2ecf20Sopenharmony_ci			if (time_after_eq(jiffies, timeout_expire)) {
4138c2ecf20Sopenharmony_ci				ret = -ETIME;
4148c2ecf20Sopenharmony_ci				break;
4158c2ecf20Sopenharmony_ci			}
4168c2ecf20Sopenharmony_ci			schedule_timeout(timeout_expire - jiffies);
4178c2ecf20Sopenharmony_ci		} else {
4188c2ecf20Sopenharmony_ci			schedule();
4198c2ecf20Sopenharmony_ci		}
4208c2ecf20Sopenharmony_ci	}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	finish_wait(&vc4->job_wait_queue, &wait);
4238c2ecf20Sopenharmony_ci	trace_vc4_wait_for_seqno_end(dev, seqno);
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	return ret;
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic void
4298c2ecf20Sopenharmony_civc4_flush_caches(struct drm_device *dev)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	/* Flush the GPU L2 caches.  These caches sit on top of system
4348c2ecf20Sopenharmony_ci	 * L3 (the 128kb or so shared with the CPU), and are
4358c2ecf20Sopenharmony_ci	 * non-allocating in the L3.
4368c2ecf20Sopenharmony_ci	 */
4378c2ecf20Sopenharmony_ci	V3D_WRITE(V3D_L2CACTL,
4388c2ecf20Sopenharmony_ci		  V3D_L2CACTL_L2CCLR);
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci	V3D_WRITE(V3D_SLCACTL,
4418c2ecf20Sopenharmony_ci		  VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
4428c2ecf20Sopenharmony_ci		  VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC) |
4438c2ecf20Sopenharmony_ci		  VC4_SET_FIELD(0xf, V3D_SLCACTL_UCC) |
4448c2ecf20Sopenharmony_ci		  VC4_SET_FIELD(0xf, V3D_SLCACTL_ICC));
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic void
4488c2ecf20Sopenharmony_civc4_flush_texture_caches(struct drm_device *dev)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	V3D_WRITE(V3D_L2CACTL,
4538c2ecf20Sopenharmony_ci		  V3D_L2CACTL_L2CCLR);
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	V3D_WRITE(V3D_SLCACTL,
4568c2ecf20Sopenharmony_ci		  VC4_SET_FIELD(0xf, V3D_SLCACTL_T1CC) |
4578c2ecf20Sopenharmony_ci		  VC4_SET_FIELD(0xf, V3D_SLCACTL_T0CC));
4588c2ecf20Sopenharmony_ci}
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci/* Sets the registers for the next job to be actually be executed in
4618c2ecf20Sopenharmony_ci * the hardware.
4628c2ecf20Sopenharmony_ci *
4638c2ecf20Sopenharmony_ci * The job_lock should be held during this.
4648c2ecf20Sopenharmony_ci */
4658c2ecf20Sopenharmony_civoid
4668c2ecf20Sopenharmony_civc4_submit_next_bin_job(struct drm_device *dev)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
4698c2ecf20Sopenharmony_ci	struct vc4_exec_info *exec;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ciagain:
4728c2ecf20Sopenharmony_ci	exec = vc4_first_bin_job(vc4);
4738c2ecf20Sopenharmony_ci	if (!exec)
4748c2ecf20Sopenharmony_ci		return;
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci	vc4_flush_caches(dev);
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	/* Only start the perfmon if it was not already started by a previous
4798c2ecf20Sopenharmony_ci	 * job.
4808c2ecf20Sopenharmony_ci	 */
4818c2ecf20Sopenharmony_ci	if (exec->perfmon && vc4->active_perfmon != exec->perfmon)
4828c2ecf20Sopenharmony_ci		vc4_perfmon_start(vc4, exec->perfmon);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	/* Either put the job in the binner if it uses the binner, or
4858c2ecf20Sopenharmony_ci	 * immediately move it to the to-be-rendered queue.
4868c2ecf20Sopenharmony_ci	 */
4878c2ecf20Sopenharmony_ci	if (exec->ct0ca != exec->ct0ea) {
4888c2ecf20Sopenharmony_ci		submit_cl(dev, 0, exec->ct0ca, exec->ct0ea);
4898c2ecf20Sopenharmony_ci	} else {
4908c2ecf20Sopenharmony_ci		struct vc4_exec_info *next;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci		vc4_move_job_to_render(dev, exec);
4938c2ecf20Sopenharmony_ci		next = vc4_first_bin_job(vc4);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci		/* We can't start the next bin job if the previous job had a
4968c2ecf20Sopenharmony_ci		 * different perfmon instance attached to it. The same goes
4978c2ecf20Sopenharmony_ci		 * if one of them had a perfmon attached to it and the other
4988c2ecf20Sopenharmony_ci		 * one doesn't.
4998c2ecf20Sopenharmony_ci		 */
5008c2ecf20Sopenharmony_ci		if (next && next->perfmon == exec->perfmon)
5018c2ecf20Sopenharmony_ci			goto again;
5028c2ecf20Sopenharmony_ci	}
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_civoid
5068c2ecf20Sopenharmony_civc4_submit_next_render_job(struct drm_device *dev)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
5098c2ecf20Sopenharmony_ci	struct vc4_exec_info *exec = vc4_first_render_job(vc4);
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	if (!exec)
5128c2ecf20Sopenharmony_ci		return;
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	/* A previous RCL may have written to one of our textures, and
5158c2ecf20Sopenharmony_ci	 * our full cache flush at bin time may have occurred before
5168c2ecf20Sopenharmony_ci	 * that RCL completed.  Flush the texture cache now, but not
5178c2ecf20Sopenharmony_ci	 * the instructions or uniforms (since we don't write those
5188c2ecf20Sopenharmony_ci	 * from an RCL).
5198c2ecf20Sopenharmony_ci	 */
5208c2ecf20Sopenharmony_ci	vc4_flush_texture_caches(dev);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	submit_cl(dev, 1, exec->ct1ca, exec->ct1ea);
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_civoid
5268c2ecf20Sopenharmony_civc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec)
5278c2ecf20Sopenharmony_ci{
5288c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
5298c2ecf20Sopenharmony_ci	bool was_empty = list_empty(&vc4->render_job_list);
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	list_move_tail(&exec->head, &vc4->render_job_list);
5328c2ecf20Sopenharmony_ci	if (was_empty)
5338c2ecf20Sopenharmony_ci		vc4_submit_next_render_job(dev);
5348c2ecf20Sopenharmony_ci}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_cistatic void
5378c2ecf20Sopenharmony_civc4_update_bo_seqnos(struct vc4_exec_info *exec, uint64_t seqno)
5388c2ecf20Sopenharmony_ci{
5398c2ecf20Sopenharmony_ci	struct vc4_bo *bo;
5408c2ecf20Sopenharmony_ci	unsigned i;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	for (i = 0; i < exec->bo_count; i++) {
5438c2ecf20Sopenharmony_ci		bo = to_vc4_bo(&exec->bo[i]->base);
5448c2ecf20Sopenharmony_ci		bo->seqno = seqno;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci		dma_resv_add_shared_fence(bo->base.base.resv, exec->fence);
5478c2ecf20Sopenharmony_ci	}
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	list_for_each_entry(bo, &exec->unref_list, unref_head) {
5508c2ecf20Sopenharmony_ci		bo->seqno = seqno;
5518c2ecf20Sopenharmony_ci	}
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	for (i = 0; i < exec->rcl_write_bo_count; i++) {
5548c2ecf20Sopenharmony_ci		bo = to_vc4_bo(&exec->rcl_write_bo[i]->base);
5558c2ecf20Sopenharmony_ci		bo->write_seqno = seqno;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci		dma_resv_add_excl_fence(bo->base.base.resv, exec->fence);
5588c2ecf20Sopenharmony_ci	}
5598c2ecf20Sopenharmony_ci}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_cistatic void
5628c2ecf20Sopenharmony_civc4_unlock_bo_reservations(struct drm_device *dev,
5638c2ecf20Sopenharmony_ci			   struct vc4_exec_info *exec,
5648c2ecf20Sopenharmony_ci			   struct ww_acquire_ctx *acquire_ctx)
5658c2ecf20Sopenharmony_ci{
5668c2ecf20Sopenharmony_ci	int i;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	for (i = 0; i < exec->bo_count; i++) {
5698c2ecf20Sopenharmony_ci		struct drm_gem_object *bo = &exec->bo[i]->base;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci		dma_resv_unlock(bo->resv);
5728c2ecf20Sopenharmony_ci	}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	ww_acquire_fini(acquire_ctx);
5758c2ecf20Sopenharmony_ci}
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci/* Takes the reservation lock on all the BOs being referenced, so that
5788c2ecf20Sopenharmony_ci * at queue submit time we can update the reservations.
5798c2ecf20Sopenharmony_ci *
5808c2ecf20Sopenharmony_ci * We don't lock the RCL the tile alloc/state BOs, or overflow memory
5818c2ecf20Sopenharmony_ci * (all of which are on exec->unref_list).  They're entirely private
5828c2ecf20Sopenharmony_ci * to vc4, so we don't attach dma-buf fences to them.
5838c2ecf20Sopenharmony_ci */
5848c2ecf20Sopenharmony_cistatic int
5858c2ecf20Sopenharmony_civc4_lock_bo_reservations(struct drm_device *dev,
5868c2ecf20Sopenharmony_ci			 struct vc4_exec_info *exec,
5878c2ecf20Sopenharmony_ci			 struct ww_acquire_ctx *acquire_ctx)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	int contended_lock = -1;
5908c2ecf20Sopenharmony_ci	int i, ret;
5918c2ecf20Sopenharmony_ci	struct drm_gem_object *bo;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	ww_acquire_init(acquire_ctx, &reservation_ww_class);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ciretry:
5968c2ecf20Sopenharmony_ci	if (contended_lock != -1) {
5978c2ecf20Sopenharmony_ci		bo = &exec->bo[contended_lock]->base;
5988c2ecf20Sopenharmony_ci		ret = dma_resv_lock_slow_interruptible(bo->resv, acquire_ctx);
5998c2ecf20Sopenharmony_ci		if (ret) {
6008c2ecf20Sopenharmony_ci			ww_acquire_done(acquire_ctx);
6018c2ecf20Sopenharmony_ci			return ret;
6028c2ecf20Sopenharmony_ci		}
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	for (i = 0; i < exec->bo_count; i++) {
6068c2ecf20Sopenharmony_ci		if (i == contended_lock)
6078c2ecf20Sopenharmony_ci			continue;
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci		bo = &exec->bo[i]->base;
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci		ret = dma_resv_lock_interruptible(bo->resv, acquire_ctx);
6128c2ecf20Sopenharmony_ci		if (ret) {
6138c2ecf20Sopenharmony_ci			int j;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci			for (j = 0; j < i; j++) {
6168c2ecf20Sopenharmony_ci				bo = &exec->bo[j]->base;
6178c2ecf20Sopenharmony_ci				dma_resv_unlock(bo->resv);
6188c2ecf20Sopenharmony_ci			}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci			if (contended_lock != -1 && contended_lock >= i) {
6218c2ecf20Sopenharmony_ci				bo = &exec->bo[contended_lock]->base;
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci				dma_resv_unlock(bo->resv);
6248c2ecf20Sopenharmony_ci			}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci			if (ret == -EDEADLK) {
6278c2ecf20Sopenharmony_ci				contended_lock = i;
6288c2ecf20Sopenharmony_ci				goto retry;
6298c2ecf20Sopenharmony_ci			}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci			ww_acquire_done(acquire_ctx);
6328c2ecf20Sopenharmony_ci			return ret;
6338c2ecf20Sopenharmony_ci		}
6348c2ecf20Sopenharmony_ci	}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	ww_acquire_done(acquire_ctx);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	/* Reserve space for our shared (read-only) fence references,
6398c2ecf20Sopenharmony_ci	 * before we commit the CL to the hardware.
6408c2ecf20Sopenharmony_ci	 */
6418c2ecf20Sopenharmony_ci	for (i = 0; i < exec->bo_count; i++) {
6428c2ecf20Sopenharmony_ci		bo = &exec->bo[i]->base;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci		ret = dma_resv_reserve_shared(bo->resv, 1);
6458c2ecf20Sopenharmony_ci		if (ret) {
6468c2ecf20Sopenharmony_ci			vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
6478c2ecf20Sopenharmony_ci			return ret;
6488c2ecf20Sopenharmony_ci		}
6498c2ecf20Sopenharmony_ci	}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	return 0;
6528c2ecf20Sopenharmony_ci}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci/* Queues a struct vc4_exec_info for execution.  If no job is
6558c2ecf20Sopenharmony_ci * currently executing, then submits it.
6568c2ecf20Sopenharmony_ci *
6578c2ecf20Sopenharmony_ci * Unlike most GPUs, our hardware only handles one command list at a
6588c2ecf20Sopenharmony_ci * time.  To queue multiple jobs at once, we'd need to edit the
6598c2ecf20Sopenharmony_ci * previous command list to have a jump to the new one at the end, and
6608c2ecf20Sopenharmony_ci * then bump the end address.  That's a change for a later date,
6618c2ecf20Sopenharmony_ci * though.
6628c2ecf20Sopenharmony_ci */
6638c2ecf20Sopenharmony_cistatic int
6648c2ecf20Sopenharmony_civc4_queue_submit(struct drm_device *dev, struct vc4_exec_info *exec,
6658c2ecf20Sopenharmony_ci		 struct ww_acquire_ctx *acquire_ctx,
6668c2ecf20Sopenharmony_ci		 struct drm_syncobj *out_sync)
6678c2ecf20Sopenharmony_ci{
6688c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
6698c2ecf20Sopenharmony_ci	struct vc4_exec_info *renderjob;
6708c2ecf20Sopenharmony_ci	uint64_t seqno;
6718c2ecf20Sopenharmony_ci	unsigned long irqflags;
6728c2ecf20Sopenharmony_ci	struct vc4_fence *fence;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	fence = kzalloc(sizeof(*fence), GFP_KERNEL);
6758c2ecf20Sopenharmony_ci	if (!fence)
6768c2ecf20Sopenharmony_ci		return -ENOMEM;
6778c2ecf20Sopenharmony_ci	fence->dev = dev;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vc4->job_lock, irqflags);
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci	seqno = ++vc4->emit_seqno;
6828c2ecf20Sopenharmony_ci	exec->seqno = seqno;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	dma_fence_init(&fence->base, &vc4_fence_ops, &vc4->job_lock,
6858c2ecf20Sopenharmony_ci		       vc4->dma_fence_context, exec->seqno);
6868c2ecf20Sopenharmony_ci	fence->seqno = exec->seqno;
6878c2ecf20Sopenharmony_ci	exec->fence = &fence->base;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	if (out_sync)
6908c2ecf20Sopenharmony_ci		drm_syncobj_replace_fence(out_sync, exec->fence);
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci	vc4_update_bo_seqnos(exec, seqno);
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	vc4_unlock_bo_reservations(dev, exec, acquire_ctx);
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	list_add_tail(&exec->head, &vc4->bin_job_list);
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	/* If no bin job was executing and if the render job (if any) has the
6998c2ecf20Sopenharmony_ci	 * same perfmon as our job attached to it (or if both jobs don't have
7008c2ecf20Sopenharmony_ci	 * perfmon activated), then kick ours off.  Otherwise, it'll get
7018c2ecf20Sopenharmony_ci	 * started when the previous job's flush/render done interrupt occurs.
7028c2ecf20Sopenharmony_ci	 */
7038c2ecf20Sopenharmony_ci	renderjob = vc4_first_render_job(vc4);
7048c2ecf20Sopenharmony_ci	if (vc4_first_bin_job(vc4) == exec &&
7058c2ecf20Sopenharmony_ci	    (!renderjob || renderjob->perfmon == exec->perfmon)) {
7068c2ecf20Sopenharmony_ci		vc4_submit_next_bin_job(dev);
7078c2ecf20Sopenharmony_ci		vc4_queue_hangcheck(dev);
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	return 0;
7138c2ecf20Sopenharmony_ci}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci/**
7168c2ecf20Sopenharmony_ci * vc4_cl_lookup_bos() - Sets up exec->bo[] with the GEM objects
7178c2ecf20Sopenharmony_ci * referenced by the job.
7188c2ecf20Sopenharmony_ci * @dev: DRM device
7198c2ecf20Sopenharmony_ci * @file_priv: DRM file for this fd
7208c2ecf20Sopenharmony_ci * @exec: V3D job being set up
7218c2ecf20Sopenharmony_ci *
7228c2ecf20Sopenharmony_ci * The command validator needs to reference BOs by their index within
7238c2ecf20Sopenharmony_ci * the submitted job's BO list.  This does the validation of the job's
7248c2ecf20Sopenharmony_ci * BO list and reference counting for the lifetime of the job.
7258c2ecf20Sopenharmony_ci */
7268c2ecf20Sopenharmony_cistatic int
7278c2ecf20Sopenharmony_civc4_cl_lookup_bos(struct drm_device *dev,
7288c2ecf20Sopenharmony_ci		  struct drm_file *file_priv,
7298c2ecf20Sopenharmony_ci		  struct vc4_exec_info *exec)
7308c2ecf20Sopenharmony_ci{
7318c2ecf20Sopenharmony_ci	struct drm_vc4_submit_cl *args = exec->args;
7328c2ecf20Sopenharmony_ci	uint32_t *handles;
7338c2ecf20Sopenharmony_ci	int ret = 0;
7348c2ecf20Sopenharmony_ci	int i;
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	exec->bo_count = args->bo_handle_count;
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci	if (!exec->bo_count) {
7398c2ecf20Sopenharmony_ci		/* See comment on bo_index for why we have to check
7408c2ecf20Sopenharmony_ci		 * this.
7418c2ecf20Sopenharmony_ci		 */
7428c2ecf20Sopenharmony_ci		DRM_DEBUG("Rendering requires BOs to validate\n");
7438c2ecf20Sopenharmony_ci		return -EINVAL;
7448c2ecf20Sopenharmony_ci	}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	exec->bo = kvmalloc_array(exec->bo_count,
7478c2ecf20Sopenharmony_ci				    sizeof(struct drm_gem_cma_object *),
7488c2ecf20Sopenharmony_ci				    GFP_KERNEL | __GFP_ZERO);
7498c2ecf20Sopenharmony_ci	if (!exec->bo) {
7508c2ecf20Sopenharmony_ci		DRM_ERROR("Failed to allocate validated BO pointers\n");
7518c2ecf20Sopenharmony_ci		return -ENOMEM;
7528c2ecf20Sopenharmony_ci	}
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	handles = kvmalloc_array(exec->bo_count, sizeof(uint32_t), GFP_KERNEL);
7558c2ecf20Sopenharmony_ci	if (!handles) {
7568c2ecf20Sopenharmony_ci		ret = -ENOMEM;
7578c2ecf20Sopenharmony_ci		DRM_ERROR("Failed to allocate incoming GEM handles\n");
7588c2ecf20Sopenharmony_ci		goto fail;
7598c2ecf20Sopenharmony_ci	}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	if (copy_from_user(handles, u64_to_user_ptr(args->bo_handles),
7628c2ecf20Sopenharmony_ci			   exec->bo_count * sizeof(uint32_t))) {
7638c2ecf20Sopenharmony_ci		ret = -EFAULT;
7648c2ecf20Sopenharmony_ci		DRM_ERROR("Failed to copy in GEM handles\n");
7658c2ecf20Sopenharmony_ci		goto fail;
7668c2ecf20Sopenharmony_ci	}
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	spin_lock(&file_priv->table_lock);
7698c2ecf20Sopenharmony_ci	for (i = 0; i < exec->bo_count; i++) {
7708c2ecf20Sopenharmony_ci		struct drm_gem_object *bo = idr_find(&file_priv->object_idr,
7718c2ecf20Sopenharmony_ci						     handles[i]);
7728c2ecf20Sopenharmony_ci		if (!bo) {
7738c2ecf20Sopenharmony_ci			DRM_DEBUG("Failed to look up GEM BO %d: %d\n",
7748c2ecf20Sopenharmony_ci				  i, handles[i]);
7758c2ecf20Sopenharmony_ci			ret = -EINVAL;
7768c2ecf20Sopenharmony_ci			break;
7778c2ecf20Sopenharmony_ci		}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci		drm_gem_object_get(bo);
7808c2ecf20Sopenharmony_ci		exec->bo[i] = (struct drm_gem_cma_object *)bo;
7818c2ecf20Sopenharmony_ci	}
7828c2ecf20Sopenharmony_ci	spin_unlock(&file_priv->table_lock);
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	if (ret)
7858c2ecf20Sopenharmony_ci		goto fail_put_bo;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	for (i = 0; i < exec->bo_count; i++) {
7888c2ecf20Sopenharmony_ci		ret = vc4_bo_inc_usecnt(to_vc4_bo(&exec->bo[i]->base));
7898c2ecf20Sopenharmony_ci		if (ret)
7908c2ecf20Sopenharmony_ci			goto fail_dec_usecnt;
7918c2ecf20Sopenharmony_ci	}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	kvfree(handles);
7948c2ecf20Sopenharmony_ci	return 0;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_cifail_dec_usecnt:
7978c2ecf20Sopenharmony_ci	/* Decrease usecnt on acquired objects.
7988c2ecf20Sopenharmony_ci	 * We cannot rely on  vc4_complete_exec() to release resources here,
7998c2ecf20Sopenharmony_ci	 * because vc4_complete_exec() has no information about which BO has
8008c2ecf20Sopenharmony_ci	 * had its ->usecnt incremented.
8018c2ecf20Sopenharmony_ci	 * To make things easier we just free everything explicitly and set
8028c2ecf20Sopenharmony_ci	 * exec->bo to NULL so that vc4_complete_exec() skips the 'BO release'
8038c2ecf20Sopenharmony_ci	 * step.
8048c2ecf20Sopenharmony_ci	 */
8058c2ecf20Sopenharmony_ci	for (i-- ; i >= 0; i--)
8068c2ecf20Sopenharmony_ci		vc4_bo_dec_usecnt(to_vc4_bo(&exec->bo[i]->base));
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_cifail_put_bo:
8098c2ecf20Sopenharmony_ci	/* Release any reference to acquired objects. */
8108c2ecf20Sopenharmony_ci	for (i = 0; i < exec->bo_count && exec->bo[i]; i++)
8118c2ecf20Sopenharmony_ci		drm_gem_object_put(&exec->bo[i]->base);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_cifail:
8148c2ecf20Sopenharmony_ci	kvfree(handles);
8158c2ecf20Sopenharmony_ci	kvfree(exec->bo);
8168c2ecf20Sopenharmony_ci	exec->bo = NULL;
8178c2ecf20Sopenharmony_ci	return ret;
8188c2ecf20Sopenharmony_ci}
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_cistatic int
8218c2ecf20Sopenharmony_civc4_get_bcl(struct drm_device *dev, struct vc4_exec_info *exec)
8228c2ecf20Sopenharmony_ci{
8238c2ecf20Sopenharmony_ci	struct drm_vc4_submit_cl *args = exec->args;
8248c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
8258c2ecf20Sopenharmony_ci	void *temp = NULL;
8268c2ecf20Sopenharmony_ci	void *bin;
8278c2ecf20Sopenharmony_ci	int ret = 0;
8288c2ecf20Sopenharmony_ci	uint32_t bin_offset = 0;
8298c2ecf20Sopenharmony_ci	uint32_t shader_rec_offset = roundup(bin_offset + args->bin_cl_size,
8308c2ecf20Sopenharmony_ci					     16);
8318c2ecf20Sopenharmony_ci	uint32_t uniforms_offset = shader_rec_offset + args->shader_rec_size;
8328c2ecf20Sopenharmony_ci	uint32_t exec_size = uniforms_offset + args->uniforms_size;
8338c2ecf20Sopenharmony_ci	uint32_t temp_size = exec_size + (sizeof(struct vc4_shader_state) *
8348c2ecf20Sopenharmony_ci					  args->shader_rec_count);
8358c2ecf20Sopenharmony_ci	struct vc4_bo *bo;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	if (shader_rec_offset < args->bin_cl_size ||
8388c2ecf20Sopenharmony_ci	    uniforms_offset < shader_rec_offset ||
8398c2ecf20Sopenharmony_ci	    exec_size < uniforms_offset ||
8408c2ecf20Sopenharmony_ci	    args->shader_rec_count >= (UINT_MAX /
8418c2ecf20Sopenharmony_ci					  sizeof(struct vc4_shader_state)) ||
8428c2ecf20Sopenharmony_ci	    temp_size < exec_size) {
8438c2ecf20Sopenharmony_ci		DRM_DEBUG("overflow in exec arguments\n");
8448c2ecf20Sopenharmony_ci		ret = -EINVAL;
8458c2ecf20Sopenharmony_ci		goto fail;
8468c2ecf20Sopenharmony_ci	}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	/* Allocate space where we'll store the copied in user command lists
8498c2ecf20Sopenharmony_ci	 * and shader records.
8508c2ecf20Sopenharmony_ci	 *
8518c2ecf20Sopenharmony_ci	 * We don't just copy directly into the BOs because we need to
8528c2ecf20Sopenharmony_ci	 * read the contents back for validation, and I think the
8538c2ecf20Sopenharmony_ci	 * bo->vaddr is uncached access.
8548c2ecf20Sopenharmony_ci	 */
8558c2ecf20Sopenharmony_ci	temp = kvmalloc_array(temp_size, 1, GFP_KERNEL);
8568c2ecf20Sopenharmony_ci	if (!temp) {
8578c2ecf20Sopenharmony_ci		DRM_ERROR("Failed to allocate storage for copying "
8588c2ecf20Sopenharmony_ci			  "in bin/render CLs.\n");
8598c2ecf20Sopenharmony_ci		ret = -ENOMEM;
8608c2ecf20Sopenharmony_ci		goto fail;
8618c2ecf20Sopenharmony_ci	}
8628c2ecf20Sopenharmony_ci	bin = temp + bin_offset;
8638c2ecf20Sopenharmony_ci	exec->shader_rec_u = temp + shader_rec_offset;
8648c2ecf20Sopenharmony_ci	exec->uniforms_u = temp + uniforms_offset;
8658c2ecf20Sopenharmony_ci	exec->shader_state = temp + exec_size;
8668c2ecf20Sopenharmony_ci	exec->shader_state_size = args->shader_rec_count;
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	if (copy_from_user(bin,
8698c2ecf20Sopenharmony_ci			   u64_to_user_ptr(args->bin_cl),
8708c2ecf20Sopenharmony_ci			   args->bin_cl_size)) {
8718c2ecf20Sopenharmony_ci		ret = -EFAULT;
8728c2ecf20Sopenharmony_ci		goto fail;
8738c2ecf20Sopenharmony_ci	}
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	if (copy_from_user(exec->shader_rec_u,
8768c2ecf20Sopenharmony_ci			   u64_to_user_ptr(args->shader_rec),
8778c2ecf20Sopenharmony_ci			   args->shader_rec_size)) {
8788c2ecf20Sopenharmony_ci		ret = -EFAULT;
8798c2ecf20Sopenharmony_ci		goto fail;
8808c2ecf20Sopenharmony_ci	}
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	if (copy_from_user(exec->uniforms_u,
8838c2ecf20Sopenharmony_ci			   u64_to_user_ptr(args->uniforms),
8848c2ecf20Sopenharmony_ci			   args->uniforms_size)) {
8858c2ecf20Sopenharmony_ci		ret = -EFAULT;
8868c2ecf20Sopenharmony_ci		goto fail;
8878c2ecf20Sopenharmony_ci	}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	bo = vc4_bo_create(dev, exec_size, true, VC4_BO_TYPE_BCL);
8908c2ecf20Sopenharmony_ci	if (IS_ERR(bo)) {
8918c2ecf20Sopenharmony_ci		DRM_ERROR("Couldn't allocate BO for binning\n");
8928c2ecf20Sopenharmony_ci		ret = PTR_ERR(bo);
8938c2ecf20Sopenharmony_ci		goto fail;
8948c2ecf20Sopenharmony_ci	}
8958c2ecf20Sopenharmony_ci	exec->exec_bo = &bo->base;
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	list_add_tail(&to_vc4_bo(&exec->exec_bo->base)->unref_head,
8988c2ecf20Sopenharmony_ci		      &exec->unref_list);
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	exec->ct0ca = exec->exec_bo->paddr + bin_offset;
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	exec->bin_u = bin;
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	exec->shader_rec_v = exec->exec_bo->vaddr + shader_rec_offset;
9058c2ecf20Sopenharmony_ci	exec->shader_rec_p = exec->exec_bo->paddr + shader_rec_offset;
9068c2ecf20Sopenharmony_ci	exec->shader_rec_size = args->shader_rec_size;
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	exec->uniforms_v = exec->exec_bo->vaddr + uniforms_offset;
9098c2ecf20Sopenharmony_ci	exec->uniforms_p = exec->exec_bo->paddr + uniforms_offset;
9108c2ecf20Sopenharmony_ci	exec->uniforms_size = args->uniforms_size;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	ret = vc4_validate_bin_cl(dev,
9138c2ecf20Sopenharmony_ci				  exec->exec_bo->vaddr + bin_offset,
9148c2ecf20Sopenharmony_ci				  bin,
9158c2ecf20Sopenharmony_ci				  exec);
9168c2ecf20Sopenharmony_ci	if (ret)
9178c2ecf20Sopenharmony_ci		goto fail;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	ret = vc4_validate_shader_recs(dev, exec);
9208c2ecf20Sopenharmony_ci	if (ret)
9218c2ecf20Sopenharmony_ci		goto fail;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	if (exec->found_tile_binning_mode_config_packet) {
9248c2ecf20Sopenharmony_ci		ret = vc4_v3d_bin_bo_get(vc4, &exec->bin_bo_used);
9258c2ecf20Sopenharmony_ci		if (ret)
9268c2ecf20Sopenharmony_ci			goto fail;
9278c2ecf20Sopenharmony_ci	}
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	/* Block waiting on any previous rendering into the CS's VBO,
9308c2ecf20Sopenharmony_ci	 * IB, or textures, so that pixels are actually written by the
9318c2ecf20Sopenharmony_ci	 * time we try to read them.
9328c2ecf20Sopenharmony_ci	 */
9338c2ecf20Sopenharmony_ci	ret = vc4_wait_for_seqno(dev, exec->bin_dep_seqno, ~0ull, true);
9348c2ecf20Sopenharmony_ci
9358c2ecf20Sopenharmony_cifail:
9368c2ecf20Sopenharmony_ci	kvfree(temp);
9378c2ecf20Sopenharmony_ci	return ret;
9388c2ecf20Sopenharmony_ci}
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_cistatic void
9418c2ecf20Sopenharmony_civc4_complete_exec(struct drm_device *dev, struct vc4_exec_info *exec)
9428c2ecf20Sopenharmony_ci{
9438c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
9448c2ecf20Sopenharmony_ci	unsigned long irqflags;
9458c2ecf20Sopenharmony_ci	unsigned i;
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	/* If we got force-completed because of GPU reset rather than
9488c2ecf20Sopenharmony_ci	 * through our IRQ handler, signal the fence now.
9498c2ecf20Sopenharmony_ci	 */
9508c2ecf20Sopenharmony_ci	if (exec->fence) {
9518c2ecf20Sopenharmony_ci		dma_fence_signal(exec->fence);
9528c2ecf20Sopenharmony_ci		dma_fence_put(exec->fence);
9538c2ecf20Sopenharmony_ci	}
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	if (exec->bo) {
9568c2ecf20Sopenharmony_ci		for (i = 0; i < exec->bo_count; i++) {
9578c2ecf20Sopenharmony_ci			struct vc4_bo *bo = to_vc4_bo(&exec->bo[i]->base);
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci			vc4_bo_dec_usecnt(bo);
9608c2ecf20Sopenharmony_ci			drm_gem_object_put(&exec->bo[i]->base);
9618c2ecf20Sopenharmony_ci		}
9628c2ecf20Sopenharmony_ci		kvfree(exec->bo);
9638c2ecf20Sopenharmony_ci	}
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci	while (!list_empty(&exec->unref_list)) {
9668c2ecf20Sopenharmony_ci		struct vc4_bo *bo = list_first_entry(&exec->unref_list,
9678c2ecf20Sopenharmony_ci						     struct vc4_bo, unref_head);
9688c2ecf20Sopenharmony_ci		list_del(&bo->unref_head);
9698c2ecf20Sopenharmony_ci		drm_gem_object_put(&bo->base.base);
9708c2ecf20Sopenharmony_ci	}
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	/* Free up the allocation of any bin slots we used. */
9738c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vc4->job_lock, irqflags);
9748c2ecf20Sopenharmony_ci	vc4->bin_alloc_used &= ~exec->bin_slots;
9758c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_ci	/* Release the reference on the binner BO if needed. */
9788c2ecf20Sopenharmony_ci	if (exec->bin_bo_used)
9798c2ecf20Sopenharmony_ci		vc4_v3d_bin_bo_put(vc4);
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	/* Release the reference we had on the perf monitor. */
9828c2ecf20Sopenharmony_ci	vc4_perfmon_put(exec->perfmon);
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	vc4_v3d_pm_put(vc4);
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	kfree(exec);
9878c2ecf20Sopenharmony_ci}
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_civoid
9908c2ecf20Sopenharmony_civc4_job_handle_completed(struct vc4_dev *vc4)
9918c2ecf20Sopenharmony_ci{
9928c2ecf20Sopenharmony_ci	unsigned long irqflags;
9938c2ecf20Sopenharmony_ci	struct vc4_seqno_cb *cb, *cb_temp;
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vc4->job_lock, irqflags);
9968c2ecf20Sopenharmony_ci	while (!list_empty(&vc4->job_done_list)) {
9978c2ecf20Sopenharmony_ci		struct vc4_exec_info *exec =
9988c2ecf20Sopenharmony_ci			list_first_entry(&vc4->job_done_list,
9998c2ecf20Sopenharmony_ci					 struct vc4_exec_info, head);
10008c2ecf20Sopenharmony_ci		list_del(&exec->head);
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vc4->job_lock, irqflags);
10038c2ecf20Sopenharmony_ci		vc4_complete_exec(&vc4->base, exec);
10048c2ecf20Sopenharmony_ci		spin_lock_irqsave(&vc4->job_lock, irqflags);
10058c2ecf20Sopenharmony_ci	}
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	list_for_each_entry_safe(cb, cb_temp, &vc4->seqno_cb_list, work.entry) {
10088c2ecf20Sopenharmony_ci		if (cb->seqno <= vc4->finished_seqno) {
10098c2ecf20Sopenharmony_ci			list_del_init(&cb->work.entry);
10108c2ecf20Sopenharmony_ci			schedule_work(&cb->work);
10118c2ecf20Sopenharmony_ci		}
10128c2ecf20Sopenharmony_ci	}
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
10158c2ecf20Sopenharmony_ci}
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_cistatic void vc4_seqno_cb_work(struct work_struct *work)
10188c2ecf20Sopenharmony_ci{
10198c2ecf20Sopenharmony_ci	struct vc4_seqno_cb *cb = container_of(work, struct vc4_seqno_cb, work);
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	cb->func(cb);
10228c2ecf20Sopenharmony_ci}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ciint vc4_queue_seqno_cb(struct drm_device *dev,
10258c2ecf20Sopenharmony_ci		       struct vc4_seqno_cb *cb, uint64_t seqno,
10268c2ecf20Sopenharmony_ci		       void (*func)(struct vc4_seqno_cb *cb))
10278c2ecf20Sopenharmony_ci{
10288c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
10298c2ecf20Sopenharmony_ci	int ret = 0;
10308c2ecf20Sopenharmony_ci	unsigned long irqflags;
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci	cb->func = func;
10338c2ecf20Sopenharmony_ci	INIT_WORK(&cb->work, vc4_seqno_cb_work);
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vc4->job_lock, irqflags);
10368c2ecf20Sopenharmony_ci	if (seqno > vc4->finished_seqno) {
10378c2ecf20Sopenharmony_ci		cb->seqno = seqno;
10388c2ecf20Sopenharmony_ci		list_add_tail(&cb->work.entry, &vc4->seqno_cb_list);
10398c2ecf20Sopenharmony_ci	} else {
10408c2ecf20Sopenharmony_ci		schedule_work(&cb->work);
10418c2ecf20Sopenharmony_ci	}
10428c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vc4->job_lock, irqflags);
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_ci	return ret;
10458c2ecf20Sopenharmony_ci}
10468c2ecf20Sopenharmony_ci
10478c2ecf20Sopenharmony_ci/* Scheduled when any job has been completed, this walks the list of
10488c2ecf20Sopenharmony_ci * jobs that had completed and unrefs their BOs and frees their exec
10498c2ecf20Sopenharmony_ci * structs.
10508c2ecf20Sopenharmony_ci */
10518c2ecf20Sopenharmony_cistatic void
10528c2ecf20Sopenharmony_civc4_job_done_work(struct work_struct *work)
10538c2ecf20Sopenharmony_ci{
10548c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 =
10558c2ecf20Sopenharmony_ci		container_of(work, struct vc4_dev, job_done_work);
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	vc4_job_handle_completed(vc4);
10588c2ecf20Sopenharmony_ci}
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_cistatic int
10618c2ecf20Sopenharmony_civc4_wait_for_seqno_ioctl_helper(struct drm_device *dev,
10628c2ecf20Sopenharmony_ci				uint64_t seqno,
10638c2ecf20Sopenharmony_ci				uint64_t *timeout_ns)
10648c2ecf20Sopenharmony_ci{
10658c2ecf20Sopenharmony_ci	unsigned long start = jiffies;
10668c2ecf20Sopenharmony_ci	int ret = vc4_wait_for_seqno(dev, seqno, *timeout_ns, true);
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	if ((ret == -EINTR || ret == -ERESTARTSYS) && *timeout_ns != ~0ull) {
10698c2ecf20Sopenharmony_ci		uint64_t delta = jiffies_to_nsecs(jiffies - start);
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci		if (*timeout_ns >= delta)
10728c2ecf20Sopenharmony_ci			*timeout_ns -= delta;
10738c2ecf20Sopenharmony_ci	}
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	return ret;
10768c2ecf20Sopenharmony_ci}
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ciint
10798c2ecf20Sopenharmony_civc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
10808c2ecf20Sopenharmony_ci		     struct drm_file *file_priv)
10818c2ecf20Sopenharmony_ci{
10828c2ecf20Sopenharmony_ci	struct drm_vc4_wait_seqno *args = data;
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	return vc4_wait_for_seqno_ioctl_helper(dev, args->seqno,
10858c2ecf20Sopenharmony_ci					       &args->timeout_ns);
10868c2ecf20Sopenharmony_ci}
10878c2ecf20Sopenharmony_ci
10888c2ecf20Sopenharmony_ciint
10898c2ecf20Sopenharmony_civc4_wait_bo_ioctl(struct drm_device *dev, void *data,
10908c2ecf20Sopenharmony_ci		  struct drm_file *file_priv)
10918c2ecf20Sopenharmony_ci{
10928c2ecf20Sopenharmony_ci	int ret;
10938c2ecf20Sopenharmony_ci	struct drm_vc4_wait_bo *args = data;
10948c2ecf20Sopenharmony_ci	struct drm_gem_object *gem_obj;
10958c2ecf20Sopenharmony_ci	struct vc4_bo *bo;
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci	if (args->pad != 0)
10988c2ecf20Sopenharmony_ci		return -EINVAL;
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_ci	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
11018c2ecf20Sopenharmony_ci	if (!gem_obj) {
11028c2ecf20Sopenharmony_ci		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
11038c2ecf20Sopenharmony_ci		return -EINVAL;
11048c2ecf20Sopenharmony_ci	}
11058c2ecf20Sopenharmony_ci	bo = to_vc4_bo(gem_obj);
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ci	ret = vc4_wait_for_seqno_ioctl_helper(dev, bo->seqno,
11088c2ecf20Sopenharmony_ci					      &args->timeout_ns);
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	drm_gem_object_put(gem_obj);
11118c2ecf20Sopenharmony_ci	return ret;
11128c2ecf20Sopenharmony_ci}
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci/**
11158c2ecf20Sopenharmony_ci * vc4_submit_cl_ioctl() - Submits a job (frame) to the VC4.
11168c2ecf20Sopenharmony_ci * @dev: DRM device
11178c2ecf20Sopenharmony_ci * @data: ioctl argument
11188c2ecf20Sopenharmony_ci * @file_priv: DRM file for this fd
11198c2ecf20Sopenharmony_ci *
11208c2ecf20Sopenharmony_ci * This is the main entrypoint for userspace to submit a 3D frame to
11218c2ecf20Sopenharmony_ci * the GPU.  Userspace provides the binner command list (if
11228c2ecf20Sopenharmony_ci * applicable), and the kernel sets up the render command list to draw
11238c2ecf20Sopenharmony_ci * to the framebuffer described in the ioctl, using the command lists
11248c2ecf20Sopenharmony_ci * that the 3D engine's binner will produce.
11258c2ecf20Sopenharmony_ci */
11268c2ecf20Sopenharmony_ciint
11278c2ecf20Sopenharmony_civc4_submit_cl_ioctl(struct drm_device *dev, void *data,
11288c2ecf20Sopenharmony_ci		    struct drm_file *file_priv)
11298c2ecf20Sopenharmony_ci{
11308c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
11318c2ecf20Sopenharmony_ci	struct vc4_file *vc4file = file_priv->driver_priv;
11328c2ecf20Sopenharmony_ci	struct drm_vc4_submit_cl *args = data;
11338c2ecf20Sopenharmony_ci	struct drm_syncobj *out_sync = NULL;
11348c2ecf20Sopenharmony_ci	struct vc4_exec_info *exec;
11358c2ecf20Sopenharmony_ci	struct ww_acquire_ctx acquire_ctx;
11368c2ecf20Sopenharmony_ci	struct dma_fence *in_fence;
11378c2ecf20Sopenharmony_ci	int ret = 0;
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ci	if (!vc4->v3d) {
11408c2ecf20Sopenharmony_ci		DRM_DEBUG("VC4_SUBMIT_CL with no VC4 V3D probed\n");
11418c2ecf20Sopenharmony_ci		return -ENODEV;
11428c2ecf20Sopenharmony_ci	}
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_ci	if ((args->flags & ~(VC4_SUBMIT_CL_USE_CLEAR_COLOR |
11458c2ecf20Sopenharmony_ci			     VC4_SUBMIT_CL_FIXED_RCL_ORDER |
11468c2ecf20Sopenharmony_ci			     VC4_SUBMIT_CL_RCL_ORDER_INCREASING_X |
11478c2ecf20Sopenharmony_ci			     VC4_SUBMIT_CL_RCL_ORDER_INCREASING_Y)) != 0) {
11488c2ecf20Sopenharmony_ci		DRM_DEBUG("Unknown flags: 0x%02x\n", args->flags);
11498c2ecf20Sopenharmony_ci		return -EINVAL;
11508c2ecf20Sopenharmony_ci	}
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci	if (args->pad2 != 0) {
11538c2ecf20Sopenharmony_ci		DRM_DEBUG("Invalid pad: 0x%08x\n", args->pad2);
11548c2ecf20Sopenharmony_ci		return -EINVAL;
11558c2ecf20Sopenharmony_ci	}
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	exec = kcalloc(1, sizeof(*exec), GFP_KERNEL);
11588c2ecf20Sopenharmony_ci	if (!exec) {
11598c2ecf20Sopenharmony_ci		DRM_ERROR("malloc failure on exec struct\n");
11608c2ecf20Sopenharmony_ci		return -ENOMEM;
11618c2ecf20Sopenharmony_ci	}
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	ret = vc4_v3d_pm_get(vc4);
11648c2ecf20Sopenharmony_ci	if (ret) {
11658c2ecf20Sopenharmony_ci		kfree(exec);
11668c2ecf20Sopenharmony_ci		return ret;
11678c2ecf20Sopenharmony_ci	}
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci	exec->args = args;
11708c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&exec->unref_list);
11718c2ecf20Sopenharmony_ci
11728c2ecf20Sopenharmony_ci	ret = vc4_cl_lookup_bos(dev, file_priv, exec);
11738c2ecf20Sopenharmony_ci	if (ret)
11748c2ecf20Sopenharmony_ci		goto fail;
11758c2ecf20Sopenharmony_ci
11768c2ecf20Sopenharmony_ci	if (args->perfmonid) {
11778c2ecf20Sopenharmony_ci		exec->perfmon = vc4_perfmon_find(vc4file,
11788c2ecf20Sopenharmony_ci						 args->perfmonid);
11798c2ecf20Sopenharmony_ci		if (!exec->perfmon) {
11808c2ecf20Sopenharmony_ci			ret = -ENOENT;
11818c2ecf20Sopenharmony_ci			goto fail;
11828c2ecf20Sopenharmony_ci		}
11838c2ecf20Sopenharmony_ci	}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	if (args->in_sync) {
11868c2ecf20Sopenharmony_ci		ret = drm_syncobj_find_fence(file_priv, args->in_sync,
11878c2ecf20Sopenharmony_ci					     0, 0, &in_fence);
11888c2ecf20Sopenharmony_ci		if (ret)
11898c2ecf20Sopenharmony_ci			goto fail;
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci		/* When the fence (or fence array) is exclusively from our
11928c2ecf20Sopenharmony_ci		 * context we can skip the wait since jobs are executed in
11938c2ecf20Sopenharmony_ci		 * order of their submission through this ioctl and this can
11948c2ecf20Sopenharmony_ci		 * only have fences from a prior job.
11958c2ecf20Sopenharmony_ci		 */
11968c2ecf20Sopenharmony_ci		if (!dma_fence_match_context(in_fence,
11978c2ecf20Sopenharmony_ci					     vc4->dma_fence_context)) {
11988c2ecf20Sopenharmony_ci			ret = dma_fence_wait(in_fence, true);
11998c2ecf20Sopenharmony_ci			if (ret) {
12008c2ecf20Sopenharmony_ci				dma_fence_put(in_fence);
12018c2ecf20Sopenharmony_ci				goto fail;
12028c2ecf20Sopenharmony_ci			}
12038c2ecf20Sopenharmony_ci		}
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_ci		dma_fence_put(in_fence);
12068c2ecf20Sopenharmony_ci	}
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	if (exec->args->bin_cl_size != 0) {
12098c2ecf20Sopenharmony_ci		ret = vc4_get_bcl(dev, exec);
12108c2ecf20Sopenharmony_ci		if (ret)
12118c2ecf20Sopenharmony_ci			goto fail;
12128c2ecf20Sopenharmony_ci	} else {
12138c2ecf20Sopenharmony_ci		exec->ct0ca = 0;
12148c2ecf20Sopenharmony_ci		exec->ct0ea = 0;
12158c2ecf20Sopenharmony_ci	}
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci	ret = vc4_get_rcl(dev, exec);
12188c2ecf20Sopenharmony_ci	if (ret)
12198c2ecf20Sopenharmony_ci		goto fail;
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci	ret = vc4_lock_bo_reservations(dev, exec, &acquire_ctx);
12228c2ecf20Sopenharmony_ci	if (ret)
12238c2ecf20Sopenharmony_ci		goto fail;
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	if (args->out_sync) {
12268c2ecf20Sopenharmony_ci		out_sync = drm_syncobj_find(file_priv, args->out_sync);
12278c2ecf20Sopenharmony_ci		if (!out_sync) {
12288c2ecf20Sopenharmony_ci			ret = -EINVAL;
12298c2ecf20Sopenharmony_ci			goto fail;
12308c2ecf20Sopenharmony_ci		}
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci		/* We replace the fence in out_sync in vc4_queue_submit since
12338c2ecf20Sopenharmony_ci		 * the render job could execute immediately after that call.
12348c2ecf20Sopenharmony_ci		 * If it finishes before our ioctl processing resumes the
12358c2ecf20Sopenharmony_ci		 * render job fence could already have been freed.
12368c2ecf20Sopenharmony_ci		 */
12378c2ecf20Sopenharmony_ci	}
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	/* Clear this out of the struct we'll be putting in the queue,
12408c2ecf20Sopenharmony_ci	 * since it's part of our stack.
12418c2ecf20Sopenharmony_ci	 */
12428c2ecf20Sopenharmony_ci	exec->args = NULL;
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci	ret = vc4_queue_submit(dev, exec, &acquire_ctx, out_sync);
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	/* The syncobj isn't part of the exec data and we need to free our
12478c2ecf20Sopenharmony_ci	 * reference even if job submission failed.
12488c2ecf20Sopenharmony_ci	 */
12498c2ecf20Sopenharmony_ci	if (out_sync)
12508c2ecf20Sopenharmony_ci		drm_syncobj_put(out_sync);
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci	if (ret)
12538c2ecf20Sopenharmony_ci		goto fail;
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_ci	/* Return the seqno for our job. */
12568c2ecf20Sopenharmony_ci	args->seqno = vc4->emit_seqno;
12578c2ecf20Sopenharmony_ci
12588c2ecf20Sopenharmony_ci	return 0;
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_cifail:
12618c2ecf20Sopenharmony_ci	vc4_complete_exec(&vc4->base, exec);
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci	return ret;
12648c2ecf20Sopenharmony_ci}
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_cistatic void vc4_gem_destroy(struct drm_device *dev, void *unused);
12678c2ecf20Sopenharmony_ciint vc4_gem_init(struct drm_device *dev)
12688c2ecf20Sopenharmony_ci{
12698c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci	vc4->dma_fence_context = dma_fence_context_alloc(1);
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&vc4->bin_job_list);
12748c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&vc4->render_job_list);
12758c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&vc4->job_done_list);
12768c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&vc4->seqno_cb_list);
12778c2ecf20Sopenharmony_ci	spin_lock_init(&vc4->job_lock);
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci	INIT_WORK(&vc4->hangcheck.reset_work, vc4_reset_work);
12808c2ecf20Sopenharmony_ci	timer_setup(&vc4->hangcheck.timer, vc4_hangcheck_elapsed, 0);
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci	INIT_WORK(&vc4->job_done_work, vc4_job_done_work);
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	mutex_init(&vc4->power_lock);
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&vc4->purgeable.list);
12878c2ecf20Sopenharmony_ci	mutex_init(&vc4->purgeable.lock);
12888c2ecf20Sopenharmony_ci
12898c2ecf20Sopenharmony_ci	return drmm_add_action_or_reset(dev, vc4_gem_destroy, NULL);
12908c2ecf20Sopenharmony_ci}
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_cistatic void vc4_gem_destroy(struct drm_device *dev, void *unused)
12938c2ecf20Sopenharmony_ci{
12948c2ecf20Sopenharmony_ci	struct vc4_dev *vc4 = to_vc4_dev(dev);
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	/* Waiting for exec to finish would need to be done before
12978c2ecf20Sopenharmony_ci	 * unregistering V3D.
12988c2ecf20Sopenharmony_ci	 */
12998c2ecf20Sopenharmony_ci	WARN_ON(vc4->emit_seqno != vc4->finished_seqno);
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	/* V3D should already have disabled its interrupt and cleared
13028c2ecf20Sopenharmony_ci	 * the overflow allocation registers.  Now free the object.
13038c2ecf20Sopenharmony_ci	 */
13048c2ecf20Sopenharmony_ci	if (vc4->bin_bo) {
13058c2ecf20Sopenharmony_ci		drm_gem_object_put(&vc4->bin_bo->base.base);
13068c2ecf20Sopenharmony_ci		vc4->bin_bo = NULL;
13078c2ecf20Sopenharmony_ci	}
13088c2ecf20Sopenharmony_ci
13098c2ecf20Sopenharmony_ci	if (vc4->hang_state)
13108c2ecf20Sopenharmony_ci		vc4_free_hang_state(dev, vc4->hang_state);
13118c2ecf20Sopenharmony_ci}
13128c2ecf20Sopenharmony_ci
13138c2ecf20Sopenharmony_ciint vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
13148c2ecf20Sopenharmony_ci			  struct drm_file *file_priv)
13158c2ecf20Sopenharmony_ci{
13168c2ecf20Sopenharmony_ci	struct drm_vc4_gem_madvise *args = data;
13178c2ecf20Sopenharmony_ci	struct drm_gem_object *gem_obj;
13188c2ecf20Sopenharmony_ci	struct vc4_bo *bo;
13198c2ecf20Sopenharmony_ci	int ret;
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	switch (args->madv) {
13228c2ecf20Sopenharmony_ci	case VC4_MADV_DONTNEED:
13238c2ecf20Sopenharmony_ci	case VC4_MADV_WILLNEED:
13248c2ecf20Sopenharmony_ci		break;
13258c2ecf20Sopenharmony_ci	default:
13268c2ecf20Sopenharmony_ci		return -EINVAL;
13278c2ecf20Sopenharmony_ci	}
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci	if (args->pad != 0)
13308c2ecf20Sopenharmony_ci		return -EINVAL;
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
13338c2ecf20Sopenharmony_ci	if (!gem_obj) {
13348c2ecf20Sopenharmony_ci		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
13358c2ecf20Sopenharmony_ci		return -ENOENT;
13368c2ecf20Sopenharmony_ci	}
13378c2ecf20Sopenharmony_ci
13388c2ecf20Sopenharmony_ci	bo = to_vc4_bo(gem_obj);
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci	/* Only BOs exposed to userspace can be purged. */
13418c2ecf20Sopenharmony_ci	if (bo->madv == __VC4_MADV_NOTSUPP) {
13428c2ecf20Sopenharmony_ci		DRM_DEBUG("madvise not supported on this BO\n");
13438c2ecf20Sopenharmony_ci		ret = -EINVAL;
13448c2ecf20Sopenharmony_ci		goto out_put_gem;
13458c2ecf20Sopenharmony_ci	}
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_ci	/* Not sure it's safe to purge imported BOs. Let's just assume it's
13488c2ecf20Sopenharmony_ci	 * not until proven otherwise.
13498c2ecf20Sopenharmony_ci	 */
13508c2ecf20Sopenharmony_ci	if (gem_obj->import_attach) {
13518c2ecf20Sopenharmony_ci		DRM_DEBUG("madvise not supported on imported BOs\n");
13528c2ecf20Sopenharmony_ci		ret = -EINVAL;
13538c2ecf20Sopenharmony_ci		goto out_put_gem;
13548c2ecf20Sopenharmony_ci	}
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ci	mutex_lock(&bo->madv_lock);
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci	if (args->madv == VC4_MADV_DONTNEED && bo->madv == VC4_MADV_WILLNEED &&
13598c2ecf20Sopenharmony_ci	    !refcount_read(&bo->usecnt)) {
13608c2ecf20Sopenharmony_ci		/* If the BO is about to be marked as purgeable, is not used
13618c2ecf20Sopenharmony_ci		 * and is not already purgeable or purged, add it to the
13628c2ecf20Sopenharmony_ci		 * purgeable list.
13638c2ecf20Sopenharmony_ci		 */
13648c2ecf20Sopenharmony_ci		vc4_bo_add_to_purgeable_pool(bo);
13658c2ecf20Sopenharmony_ci	} else if (args->madv == VC4_MADV_WILLNEED &&
13668c2ecf20Sopenharmony_ci		   bo->madv == VC4_MADV_DONTNEED &&
13678c2ecf20Sopenharmony_ci		   !refcount_read(&bo->usecnt)) {
13688c2ecf20Sopenharmony_ci		/* The BO has not been purged yet, just remove it from
13698c2ecf20Sopenharmony_ci		 * the purgeable list.
13708c2ecf20Sopenharmony_ci		 */
13718c2ecf20Sopenharmony_ci		vc4_bo_remove_from_purgeable_pool(bo);
13728c2ecf20Sopenharmony_ci	}
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	/* Save the purged state. */
13758c2ecf20Sopenharmony_ci	args->retained = bo->madv != __VC4_MADV_PURGED;
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	/* Update internal madv state only if the bo was not purged. */
13788c2ecf20Sopenharmony_ci	if (bo->madv != __VC4_MADV_PURGED)
13798c2ecf20Sopenharmony_ci		bo->madv = args->madv;
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	mutex_unlock(&bo->madv_lock);
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci	ret = 0;
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ciout_put_gem:
13868c2ecf20Sopenharmony_ci	drm_gem_object_put(gem_obj);
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci	return ret;
13898c2ecf20Sopenharmony_ci}
1390