157692c94SEric Anholt // SPDX-License-Identifier: GPL-2.0+ 257692c94SEric Anholt /* Copyright (C) 2014-2018 Broadcom */ 357692c94SEric Anholt 4220989e7SSam Ravnborg #include <linux/device.h> 5220989e7SSam Ravnborg #include <linux/dma-mapping.h> 6220989e7SSam Ravnborg #include <linux/io.h> 757692c94SEric Anholt #include <linux/module.h> 857692c94SEric Anholt #include <linux/platform_device.h> 957692c94SEric Anholt #include <linux/pm_runtime.h> 10eea9b97bSEric Anholt #include <linux/reset.h> 1157692c94SEric Anholt #include <linux/sched/signal.h> 12220989e7SSam Ravnborg #include <linux/uaccess.h> 1357692c94SEric Anholt 14220989e7SSam Ravnborg #include <drm/drm_syncobj.h> 15220989e7SSam Ravnborg #include <uapi/drm/v3d_drm.h> 16220989e7SSam Ravnborg 1757692c94SEric Anholt #include "v3d_drv.h" 1857692c94SEric Anholt #include "v3d_regs.h" 1957692c94SEric Anholt #include "v3d_trace.h" 2057692c94SEric Anholt 2157692c94SEric Anholt static void 2257692c94SEric Anholt v3d_init_core(struct v3d_dev *v3d, int core) 2357692c94SEric Anholt { 2457692c94SEric Anholt /* Set OVRTMUOUT, which means that the texture sampler uniform 2557692c94SEric Anholt * configuration's tmu output type field is used, instead of 2657692c94SEric Anholt * using the hardware default behavior based on the texture 2757692c94SEric Anholt * type. If you want the default behavior, you can still put 2857692c94SEric Anholt * "2" in the indirect texture state's output_type field. 2957692c94SEric Anholt */ 30a7dde1b7SEric Anholt if (v3d->ver < 40) 3157692c94SEric Anholt V3D_CORE_WRITE(core, V3D_CTL_MISCCFG, V3D_MISCCFG_OVRTMUOUT); 3257692c94SEric Anholt 3357692c94SEric Anholt /* Whenever we flush the L2T cache, we always want to flush 3457692c94SEric Anholt * the whole thing. 3557692c94SEric Anholt */ 3657692c94SEric Anholt V3D_CORE_WRITE(core, V3D_CTL_L2TFLSTA, 0); 3757692c94SEric Anholt V3D_CORE_WRITE(core, V3D_CTL_L2TFLEND, ~0); 3857692c94SEric Anholt } 3957692c94SEric Anholt 4057692c94SEric Anholt /* Sets invariant state for the HW. */ 4157692c94SEric Anholt static void 4257692c94SEric Anholt v3d_init_hw_state(struct v3d_dev *v3d) 4357692c94SEric Anholt { 4457692c94SEric Anholt v3d_init_core(v3d, 0); 4557692c94SEric Anholt } 4657692c94SEric Anholt 4757692c94SEric Anholt static void 4857692c94SEric Anholt v3d_idle_axi(struct v3d_dev *v3d, int core) 4957692c94SEric Anholt { 5057692c94SEric Anholt V3D_CORE_WRITE(core, V3D_GMP_CFG, V3D_GMP_CFG_STOP_REQ); 5157692c94SEric Anholt 5257692c94SEric Anholt if (wait_for((V3D_CORE_READ(core, V3D_GMP_STATUS) & 5357692c94SEric Anholt (V3D_GMP_STATUS_RD_COUNT_MASK | 5457692c94SEric Anholt V3D_GMP_STATUS_WR_COUNT_MASK | 5557692c94SEric Anholt V3D_GMP_STATUS_CFG_BUSY)) == 0, 100)) { 5657692c94SEric Anholt DRM_ERROR("Failed to wait for safe GMP shutdown\n"); 5757692c94SEric Anholt } 5857692c94SEric Anholt } 5957692c94SEric Anholt 6057692c94SEric Anholt static void 6157692c94SEric Anholt v3d_idle_gca(struct v3d_dev *v3d) 6257692c94SEric Anholt { 6357692c94SEric Anholt if (v3d->ver >= 41) 6457692c94SEric Anholt return; 6557692c94SEric Anholt 6657692c94SEric Anholt V3D_GCA_WRITE(V3D_GCA_SAFE_SHUTDOWN, V3D_GCA_SAFE_SHUTDOWN_EN); 6757692c94SEric Anholt 6857692c94SEric Anholt if (wait_for((V3D_GCA_READ(V3D_GCA_SAFE_SHUTDOWN_ACK) & 6957692c94SEric Anholt V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED) == 7057692c94SEric Anholt V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED, 100)) { 7157692c94SEric Anholt DRM_ERROR("Failed to wait for safe GCA shutdown\n"); 7257692c94SEric Anholt } 7357692c94SEric Anholt } 7457692c94SEric Anholt 7557692c94SEric Anholt static void 76eea9b97bSEric Anholt v3d_reset_by_bridge(struct v3d_dev *v3d) 7757692c94SEric Anholt { 7857692c94SEric Anholt int version = V3D_BRIDGE_READ(V3D_TOP_GR_BRIDGE_REVISION); 7957692c94SEric Anholt 8057692c94SEric Anholt if (V3D_GET_FIELD(version, V3D_TOP_GR_BRIDGE_MAJOR) == 2) { 8157692c94SEric Anholt V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 8257692c94SEric Anholt V3D_TOP_GR_BRIDGE_SW_INIT_0_V3D_CLK_108_SW_INIT); 8357692c94SEric Anholt V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 0); 8457692c94SEric Anholt 8557692c94SEric Anholt /* GFXH-1383: The SW_INIT may cause a stray write to address 0 8657692c94SEric Anholt * of the unit, so reset it to its power-on value here. 8757692c94SEric Anholt */ 8857692c94SEric Anholt V3D_WRITE(V3D_HUB_AXICFG, V3D_HUB_AXICFG_MAX_LEN_MASK); 8957692c94SEric Anholt } else { 9057692c94SEric Anholt WARN_ON_ONCE(V3D_GET_FIELD(version, 9157692c94SEric Anholt V3D_TOP_GR_BRIDGE_MAJOR) != 7); 9257692c94SEric Anholt V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 9357692c94SEric Anholt V3D_TOP_GR_BRIDGE_SW_INIT_1_V3D_CLK_108_SW_INIT); 9457692c94SEric Anholt V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 0); 9557692c94SEric Anholt } 96eea9b97bSEric Anholt } 97eea9b97bSEric Anholt 98eea9b97bSEric Anholt static void 99eea9b97bSEric Anholt v3d_reset_v3d(struct v3d_dev *v3d) 100eea9b97bSEric Anholt { 101eea9b97bSEric Anholt if (v3d->reset) 102eea9b97bSEric Anholt reset_control_reset(v3d->reset); 103eea9b97bSEric Anholt else 104eea9b97bSEric Anholt v3d_reset_by_bridge(v3d); 10557692c94SEric Anholt 10657692c94SEric Anholt v3d_init_hw_state(v3d); 10757692c94SEric Anholt } 10857692c94SEric Anholt 10957692c94SEric Anholt void 11057692c94SEric Anholt v3d_reset(struct v3d_dev *v3d) 11157692c94SEric Anholt { 11257692c94SEric Anholt struct drm_device *dev = &v3d->drm; 11357692c94SEric Anholt 1141ba9d7cbSEric Anholt DRM_DEV_ERROR(dev->dev, "Resetting GPU for hang.\n"); 1151ba9d7cbSEric Anholt DRM_DEV_ERROR(dev->dev, "V3D_ERR_STAT: 0x%08x\n", 1161ba9d7cbSEric Anholt V3D_CORE_READ(0, V3D_ERR_STAT)); 11757692c94SEric Anholt trace_v3d_reset_begin(dev); 11857692c94SEric Anholt 11957692c94SEric Anholt /* XXX: only needed for safe powerdown, not reset. */ 12057692c94SEric Anholt if (false) 12157692c94SEric Anholt v3d_idle_axi(v3d, 0); 12257692c94SEric Anholt 12357692c94SEric Anholt v3d_idle_gca(v3d); 12457692c94SEric Anholt v3d_reset_v3d(v3d); 12557692c94SEric Anholt 12657692c94SEric Anholt v3d_mmu_set_page_table(v3d); 12757692c94SEric Anholt v3d_irq_reset(v3d); 12857692c94SEric Anholt 129*26a4dc29SJuan A. Suarez Romero v3d_perfmon_stop(v3d, v3d->active_perfmon, false); 130*26a4dc29SJuan A. Suarez Romero 13157692c94SEric Anholt trace_v3d_reset_end(dev); 13257692c94SEric Anholt } 13357692c94SEric Anholt 13457692c94SEric Anholt static void 13557692c94SEric Anholt v3d_flush_l3(struct v3d_dev *v3d) 13657692c94SEric Anholt { 13757692c94SEric Anholt if (v3d->ver < 41) { 13857692c94SEric Anholt u32 gca_ctrl = V3D_GCA_READ(V3D_GCA_CACHE_CTRL); 13957692c94SEric Anholt 14057692c94SEric Anholt V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL, 14157692c94SEric Anholt gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH); 14257692c94SEric Anholt 14357692c94SEric Anholt if (v3d->ver < 33) { 14457692c94SEric Anholt V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL, 14557692c94SEric Anholt gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH); 14657692c94SEric Anholt } 14757692c94SEric Anholt } 14857692c94SEric Anholt } 14957692c94SEric Anholt 1507b9d2fe4SEric Anholt /* Invalidates the (read-only) L2C cache. This was the L2 cache for 1517b9d2fe4SEric Anholt * uniforms and instructions on V3D 3.2. 1527b9d2fe4SEric Anholt */ 15357692c94SEric Anholt static void 1547b9d2fe4SEric Anholt v3d_invalidate_l2c(struct v3d_dev *v3d, int core) 15557692c94SEric Anholt { 1567b9d2fe4SEric Anholt if (v3d->ver > 32) 1577b9d2fe4SEric Anholt return; 1587b9d2fe4SEric Anholt 15957692c94SEric Anholt V3D_CORE_WRITE(core, V3D_CTL_L2CACTL, 16057692c94SEric Anholt V3D_L2CACTL_L2CCLR | 16157692c94SEric Anholt V3D_L2CACTL_L2CENA); 16257692c94SEric Anholt } 16357692c94SEric Anholt 16457692c94SEric Anholt /* Invalidates texture L2 cachelines */ 16557692c94SEric Anholt static void 16657692c94SEric Anholt v3d_flush_l2t(struct v3d_dev *v3d, int core) 16757692c94SEric Anholt { 16851c1b6f9SEric Anholt /* While there is a busy bit (V3D_L2TCACTL_L2TFLS), we don't 16951c1b6f9SEric Anholt * need to wait for completion before dispatching the job -- 17051c1b6f9SEric Anholt * L2T accesses will be stalled until the flush has completed. 171d223f98fSEric Anholt * However, we do need to make sure we don't try to trigger a 172d223f98fSEric Anholt * new flush while the L2_CLEAN queue is trying to 173d223f98fSEric Anholt * synchronously clean after a job. 17451c1b6f9SEric Anholt */ 175d223f98fSEric Anholt mutex_lock(&v3d->cache_clean_lock); 17657692c94SEric Anholt V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, 17757692c94SEric Anholt V3D_L2TCACTL_L2TFLS | 17857692c94SEric Anholt V3D_SET_FIELD(V3D_L2TCACTL_FLM_FLUSH, V3D_L2TCACTL_FLM)); 179d223f98fSEric Anholt mutex_unlock(&v3d->cache_clean_lock); 180d223f98fSEric Anholt } 181d223f98fSEric Anholt 182d223f98fSEric Anholt /* Cleans texture L1 and L2 cachelines (writing back dirty data). 183d223f98fSEric Anholt * 184d223f98fSEric Anholt * For cleaning, which happens from the CACHE_CLEAN queue after CSD has 185d223f98fSEric Anholt * executed, we need to make sure that the clean is done before 186d223f98fSEric Anholt * signaling job completion. So, we synchronously wait before 187d223f98fSEric Anholt * returning, and we make sure that L2 invalidates don't happen in the 188d223f98fSEric Anholt * meantime to confuse our are-we-done checks. 189d223f98fSEric Anholt */ 190d223f98fSEric Anholt void 191d223f98fSEric Anholt v3d_clean_caches(struct v3d_dev *v3d) 192d223f98fSEric Anholt { 193d223f98fSEric Anholt struct drm_device *dev = &v3d->drm; 194d223f98fSEric Anholt int core = 0; 195d223f98fSEric Anholt 196d223f98fSEric Anholt trace_v3d_cache_clean_begin(dev); 197d223f98fSEric Anholt 198d223f98fSEric Anholt V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, V3D_L2TCACTL_TMUWCF); 199d223f98fSEric Anholt if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) & 200d223f98fSEric Anholt V3D_L2TCACTL_L2TFLS), 100)) { 201d223f98fSEric Anholt DRM_ERROR("Timeout waiting for L1T write combiner flush\n"); 202d223f98fSEric Anholt } 203d223f98fSEric Anholt 204d223f98fSEric Anholt mutex_lock(&v3d->cache_clean_lock); 205d223f98fSEric Anholt V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, 206d223f98fSEric Anholt V3D_L2TCACTL_L2TFLS | 207d223f98fSEric Anholt V3D_SET_FIELD(V3D_L2TCACTL_FLM_CLEAN, V3D_L2TCACTL_FLM)); 208d223f98fSEric Anholt 209d223f98fSEric Anholt if (wait_for(!(V3D_CORE_READ(core, V3D_CTL_L2TCACTL) & 210d223f98fSEric Anholt V3D_L2TCACTL_L2TFLS), 100)) { 211d223f98fSEric Anholt DRM_ERROR("Timeout waiting for L2T clean\n"); 212d223f98fSEric Anholt } 213d223f98fSEric Anholt 214d223f98fSEric Anholt mutex_unlock(&v3d->cache_clean_lock); 215d223f98fSEric Anholt 216d223f98fSEric Anholt trace_v3d_cache_clean_end(dev); 21757692c94SEric Anholt } 21857692c94SEric Anholt 21957692c94SEric Anholt /* Invalidates the slice caches. These are read-only caches. */ 22057692c94SEric Anholt static void 22157692c94SEric Anholt v3d_invalidate_slices(struct v3d_dev *v3d, int core) 22257692c94SEric Anholt { 22357692c94SEric Anholt V3D_CORE_WRITE(core, V3D_CTL_SLCACTL, 22457692c94SEric Anholt V3D_SET_FIELD(0xf, V3D_SLCACTL_TVCCS) | 22557692c94SEric Anholt V3D_SET_FIELD(0xf, V3D_SLCACTL_TDCCS) | 22657692c94SEric Anholt V3D_SET_FIELD(0xf, V3D_SLCACTL_UCC) | 22757692c94SEric Anholt V3D_SET_FIELD(0xf, V3D_SLCACTL_ICC)); 22857692c94SEric Anholt } 22957692c94SEric Anholt 23057692c94SEric Anholt void 23157692c94SEric Anholt v3d_invalidate_caches(struct v3d_dev *v3d) 23257692c94SEric Anholt { 233aa5beec3SEric Anholt /* Invalidate the caches from the outside in. That way if 234aa5beec3SEric Anholt * another CL's concurrent use of nearby memory were to pull 235aa5beec3SEric Anholt * an invalidated cacheline back in, we wouldn't leave stale 236aa5beec3SEric Anholt * data in the inner cache. 237aa5beec3SEric Anholt */ 23857692c94SEric Anholt v3d_flush_l3(v3d); 2397b9d2fe4SEric Anholt v3d_invalidate_l2c(v3d, 0); 24057692c94SEric Anholt v3d_flush_l2t(v3d, 0); 241aa5beec3SEric Anholt v3d_invalidate_slices(v3d, 0); 24257692c94SEric Anholt } 24357692c94SEric Anholt 24457692c94SEric Anholt /* Takes the reservation lock on all the BOs being referenced, so that 24557692c94SEric Anholt * at queue submit time we can update the reservations. 24657692c94SEric Anholt * 24757692c94SEric Anholt * We don't lock the RCL the tile alloc/state BOs, or overflow memory 24857692c94SEric Anholt * (all of which are on exec->unref_list). They're entirely private 24957692c94SEric Anholt * to v3d, so we don't attach dma-buf fences to them. 25057692c94SEric Anholt */ 25157692c94SEric Anholt static int 252dffa9b7aSEric Anholt v3d_lock_bo_reservations(struct v3d_job *job, 25357692c94SEric Anholt struct ww_acquire_ctx *acquire_ctx) 25457692c94SEric Anholt { 25557692c94SEric Anholt int i, ret; 25657692c94SEric Anholt 257dffa9b7aSEric Anholt ret = drm_gem_lock_reservations(job->bo, job->bo_count, acquire_ctx); 258c2b3e61aSEric Anholt if (ret) 25957692c94SEric Anholt return ret; 26057692c94SEric Anholt 261dffa9b7aSEric Anholt for (i = 0; i < job->bo_count; i++) { 262dffa9b7aSEric Anholt ret = drm_gem_fence_array_add_implicit(&job->deps, 263dffa9b7aSEric Anholt job->bo[i], true); 264dffa9b7aSEric Anholt if (ret) { 265dffa9b7aSEric Anholt drm_gem_unlock_reservations(job->bo, job->bo_count, 266dffa9b7aSEric Anholt acquire_ctx); 267dffa9b7aSEric Anholt return ret; 268dffa9b7aSEric Anholt } 269dffa9b7aSEric Anholt } 270dffa9b7aSEric Anholt 27157692c94SEric Anholt return 0; 27257692c94SEric Anholt } 27357692c94SEric Anholt 27457692c94SEric Anholt /** 275a783a09eSEric Anholt * v3d_lookup_bos() - Sets up job->bo[] with the GEM objects 27657692c94SEric Anholt * referenced by the job. 27757692c94SEric Anholt * @dev: DRM device 27857692c94SEric Anholt * @file_priv: DRM file for this fd 279a783a09eSEric Anholt * @job: V3D job being set up 280e226878eSLee Jones * @bo_handles: GEM handles 281e226878eSLee Jones * @bo_count: Number of GEM handles passed in 28257692c94SEric Anholt * 28357692c94SEric Anholt * The command validator needs to reference BOs by their index within 28457692c94SEric Anholt * the submitted job's BO list. This does the validation of the job's 28557692c94SEric Anholt * BO list and reference counting for the lifetime of the job. 28657692c94SEric Anholt * 28757692c94SEric Anholt * Note that this function doesn't need to unreference the BOs on 28857692c94SEric Anholt * failure, because that will happen at v3d_exec_cleanup() time. 28957692c94SEric Anholt */ 29057692c94SEric Anholt static int 291a783a09eSEric Anholt v3d_lookup_bos(struct drm_device *dev, 29257692c94SEric Anholt struct drm_file *file_priv, 293a783a09eSEric Anholt struct v3d_job *job, 294a783a09eSEric Anholt u64 bo_handles, 295a783a09eSEric Anholt u32 bo_count) 29657692c94SEric Anholt { 29757692c94SEric Anholt u32 *handles; 29857692c94SEric Anholt int ret = 0; 29957692c94SEric Anholt int i; 30057692c94SEric Anholt 301a783a09eSEric Anholt job->bo_count = bo_count; 30257692c94SEric Anholt 303a783a09eSEric Anholt if (!job->bo_count) { 30457692c94SEric Anholt /* See comment on bo_index for why we have to check 30557692c94SEric Anholt * this. 30657692c94SEric Anholt */ 30757692c94SEric Anholt DRM_DEBUG("Rendering requires BOs\n"); 30857692c94SEric Anholt return -EINVAL; 30957692c94SEric Anholt } 31057692c94SEric Anholt 311a783a09eSEric Anholt job->bo = kvmalloc_array(job->bo_count, 31257692c94SEric Anholt sizeof(struct drm_gem_cma_object *), 31357692c94SEric Anholt GFP_KERNEL | __GFP_ZERO); 314a783a09eSEric Anholt if (!job->bo) { 31557692c94SEric Anholt DRM_DEBUG("Failed to allocate validated BO pointers\n"); 31657692c94SEric Anholt return -ENOMEM; 31757692c94SEric Anholt } 31857692c94SEric Anholt 319a783a09eSEric Anholt handles = kvmalloc_array(job->bo_count, sizeof(u32), GFP_KERNEL); 32057692c94SEric Anholt if (!handles) { 32157692c94SEric Anholt ret = -ENOMEM; 32257692c94SEric Anholt DRM_DEBUG("Failed to allocate incoming GEM handles\n"); 32357692c94SEric Anholt goto fail; 32457692c94SEric Anholt } 32557692c94SEric Anholt 32657692c94SEric Anholt if (copy_from_user(handles, 327a783a09eSEric Anholt (void __user *)(uintptr_t)bo_handles, 328a783a09eSEric Anholt job->bo_count * sizeof(u32))) { 32957692c94SEric Anholt ret = -EFAULT; 33057692c94SEric Anholt DRM_DEBUG("Failed to copy in GEM handles\n"); 33157692c94SEric Anholt goto fail; 33257692c94SEric Anholt } 33357692c94SEric Anholt 33457692c94SEric Anholt spin_lock(&file_priv->table_lock); 335a783a09eSEric Anholt for (i = 0; i < job->bo_count; i++) { 33657692c94SEric Anholt struct drm_gem_object *bo = idr_find(&file_priv->object_idr, 33757692c94SEric Anholt handles[i]); 33857692c94SEric Anholt if (!bo) { 33957692c94SEric Anholt DRM_DEBUG("Failed to look up GEM BO %d: %d\n", 34057692c94SEric Anholt i, handles[i]); 34157692c94SEric Anholt ret = -ENOENT; 34257692c94SEric Anholt spin_unlock(&file_priv->table_lock); 34357692c94SEric Anholt goto fail; 34457692c94SEric Anholt } 34557692c94SEric Anholt drm_gem_object_get(bo); 346a783a09eSEric Anholt job->bo[i] = bo; 34757692c94SEric Anholt } 34857692c94SEric Anholt spin_unlock(&file_priv->table_lock); 34957692c94SEric Anholt 35057692c94SEric Anholt fail: 35157692c94SEric Anholt kvfree(handles); 35257692c94SEric Anholt return ret; 35357692c94SEric Anholt } 35457692c94SEric Anholt 35557692c94SEric Anholt static void 356a783a09eSEric Anholt v3d_job_free(struct kref *ref) 35757692c94SEric Anholt { 358a783a09eSEric Anholt struct v3d_job *job = container_of(ref, struct v3d_job, refcount); 359dffa9b7aSEric Anholt unsigned long index; 360dffa9b7aSEric Anholt struct dma_fence *fence; 361a783a09eSEric Anholt int i; 36257692c94SEric Anholt 363a783a09eSEric Anholt for (i = 0; i < job->bo_count; i++) { 3641584f16cSEric Anholt if (job->bo[i]) 3652b86189eSEmil Velikov drm_gem_object_put(job->bo[i]); 3661584f16cSEric Anholt } 367a783a09eSEric Anholt kvfree(job->bo); 3681584f16cSEric Anholt 369dffa9b7aSEric Anholt xa_for_each(&job->deps, index, fence) { 370dffa9b7aSEric Anholt dma_fence_put(fence); 371dffa9b7aSEric Anholt } 372dffa9b7aSEric Anholt xa_destroy(&job->deps); 373dffa9b7aSEric Anholt 374a783a09eSEric Anholt dma_fence_put(job->irq_fence); 375a783a09eSEric Anholt dma_fence_put(job->done_fence); 376a783a09eSEric Anholt 377bc662528SDaniel Vetter pm_runtime_mark_last_busy(job->v3d->drm.dev); 378bc662528SDaniel Vetter pm_runtime_put_autosuspend(job->v3d->drm.dev); 3791584f16cSEric Anholt 380*26a4dc29SJuan A. Suarez Romero if (job->perfmon) 381*26a4dc29SJuan A. Suarez Romero v3d_perfmon_put(job->perfmon); 382*26a4dc29SJuan A. Suarez Romero 3831584f16cSEric Anholt kfree(job); 3841584f16cSEric Anholt } 3851584f16cSEric Anholt 386a783a09eSEric Anholt static void 387a783a09eSEric Anholt v3d_render_job_free(struct kref *ref) 3881584f16cSEric Anholt { 389a783a09eSEric Anholt struct v3d_render_job *job = container_of(ref, struct v3d_render_job, 390a783a09eSEric Anholt base.refcount); 391a783a09eSEric Anholt struct v3d_bo *bo, *save; 392a783a09eSEric Anholt 393a783a09eSEric Anholt list_for_each_entry_safe(bo, save, &job->unref_list, unref_head) { 3942b86189eSEmil Velikov drm_gem_object_put(&bo->base.base); 395a783a09eSEric Anholt } 396a783a09eSEric Anholt 397a783a09eSEric Anholt v3d_job_free(ref); 398a783a09eSEric Anholt } 399a783a09eSEric Anholt 400a783a09eSEric Anholt void v3d_job_put(struct v3d_job *job) 401a783a09eSEric Anholt { 402a783a09eSEric Anholt kref_put(&job->refcount, job->free); 4031584f16cSEric Anholt } 4041584f16cSEric Anholt 40557692c94SEric Anholt int 40657692c94SEric Anholt v3d_wait_bo_ioctl(struct drm_device *dev, void *data, 40757692c94SEric Anholt struct drm_file *file_priv) 40857692c94SEric Anholt { 40957692c94SEric Anholt int ret; 41057692c94SEric Anholt struct drm_v3d_wait_bo *args = data; 41157692c94SEric Anholt ktime_t start = ktime_get(); 41257692c94SEric Anholt u64 delta_ns; 41357692c94SEric Anholt unsigned long timeout_jiffies = 41457692c94SEric Anholt nsecs_to_jiffies_timeout(args->timeout_ns); 41557692c94SEric Anholt 41657692c94SEric Anholt if (args->pad != 0) 41757692c94SEric Anholt return -EINVAL; 41857692c94SEric Anholt 41952791eeeSChristian König ret = drm_gem_dma_resv_wait(file_priv, args->handle, 4208d668309SRob Herring true, timeout_jiffies); 42157692c94SEric Anholt 42257692c94SEric Anholt /* Decrement the user's timeout, in case we got interrupted 42357692c94SEric Anholt * such that the ioctl will be restarted. 42457692c94SEric Anholt */ 42557692c94SEric Anholt delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start)); 42657692c94SEric Anholt if (delta_ns < args->timeout_ns) 42757692c94SEric Anholt args->timeout_ns -= delta_ns; 42857692c94SEric Anholt else 42957692c94SEric Anholt args->timeout_ns = 0; 43057692c94SEric Anholt 43157692c94SEric Anholt /* Asked to wait beyond the jiffie/scheduler precision? */ 43257692c94SEric Anholt if (ret == -ETIME && args->timeout_ns) 43357692c94SEric Anholt ret = -EAGAIN; 43457692c94SEric Anholt 43557692c94SEric Anholt return ret; 43657692c94SEric Anholt } 43757692c94SEric Anholt 438a783a09eSEric Anholt static int 439a783a09eSEric Anholt v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv, 440a783a09eSEric Anholt struct v3d_job *job, void (*free)(struct kref *ref), 441a783a09eSEric Anholt u32 in_sync) 442a783a09eSEric Anholt { 443dffa9b7aSEric Anholt struct dma_fence *in_fence = NULL; 444a783a09eSEric Anholt int ret; 445a783a09eSEric Anholt 446a783a09eSEric Anholt job->v3d = v3d; 447a783a09eSEric Anholt job->free = free; 448a783a09eSEric Anholt 449bc662528SDaniel Vetter ret = pm_runtime_get_sync(v3d->drm.dev); 450a783a09eSEric Anholt if (ret < 0) 451a783a09eSEric Anholt return ret; 452a783a09eSEric Anholt 453dffa9b7aSEric Anholt xa_init_flags(&job->deps, XA_FLAGS_ALLOC); 454dffa9b7aSEric Anholt 455dffa9b7aSEric Anholt ret = drm_syncobj_find_fence(file_priv, in_sync, 0, 0, &in_fence); 456dffa9b7aSEric Anholt if (ret == -EINVAL) 457dffa9b7aSEric Anholt goto fail; 458dffa9b7aSEric Anholt 459dffa9b7aSEric Anholt ret = drm_gem_fence_array_add(&job->deps, in_fence); 460dffa9b7aSEric Anholt if (ret) 461dffa9b7aSEric Anholt goto fail; 462a783a09eSEric Anholt 463a783a09eSEric Anholt kref_init(&job->refcount); 464a783a09eSEric Anholt 465a783a09eSEric Anholt return 0; 466dffa9b7aSEric Anholt fail: 467dffa9b7aSEric Anholt xa_destroy(&job->deps); 468bc662528SDaniel Vetter pm_runtime_put_autosuspend(v3d->drm.dev); 469dffa9b7aSEric Anholt return ret; 470a783a09eSEric Anholt } 471a783a09eSEric Anholt 472a783a09eSEric Anholt static int 473a783a09eSEric Anholt v3d_push_job(struct v3d_file_priv *v3d_priv, 474a783a09eSEric Anholt struct v3d_job *job, enum v3d_queue queue) 475a783a09eSEric Anholt { 476a783a09eSEric Anholt int ret; 477a783a09eSEric Anholt 478a783a09eSEric Anholt ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue], 479a783a09eSEric Anholt v3d_priv); 480a783a09eSEric Anholt if (ret) 481a783a09eSEric Anholt return ret; 482a783a09eSEric Anholt 483a783a09eSEric Anholt job->done_fence = dma_fence_get(&job->base.s_fence->finished); 484a783a09eSEric Anholt 485a783a09eSEric Anholt /* put by scheduler job completion */ 486a783a09eSEric Anholt kref_get(&job->refcount); 487a783a09eSEric Anholt 488a783a09eSEric Anholt drm_sched_entity_push_job(&job->base, &v3d_priv->sched_entity[queue]); 489a783a09eSEric Anholt 490a783a09eSEric Anholt return 0; 491a783a09eSEric Anholt } 492a783a09eSEric Anholt 493a783a09eSEric Anholt static void 494a783a09eSEric Anholt v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv, 495a783a09eSEric Anholt struct v3d_job *job, 496a783a09eSEric Anholt struct ww_acquire_ctx *acquire_ctx, 497d223f98fSEric Anholt u32 out_sync, 498d223f98fSEric Anholt struct dma_fence *done_fence) 499a783a09eSEric Anholt { 500a783a09eSEric Anholt struct drm_syncobj *sync_out; 501a783a09eSEric Anholt int i; 502a783a09eSEric Anholt 503a783a09eSEric Anholt for (i = 0; i < job->bo_count; i++) { 504a783a09eSEric Anholt /* XXX: Use shared fences for read-only objects. */ 50552791eeeSChristian König dma_resv_add_excl_fence(job->bo[i]->resv, 506a783a09eSEric Anholt job->done_fence); 507a783a09eSEric Anholt } 508a783a09eSEric Anholt 509a783a09eSEric Anholt drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx); 510a783a09eSEric Anholt 511a783a09eSEric Anholt /* Update the return sync object for the job */ 512a783a09eSEric Anholt sync_out = drm_syncobj_find(file_priv, out_sync); 513a783a09eSEric Anholt if (sync_out) { 514d223f98fSEric Anholt drm_syncobj_replace_fence(sync_out, done_fence); 515a783a09eSEric Anholt drm_syncobj_put(sync_out); 516a783a09eSEric Anholt } 517a783a09eSEric Anholt } 518a783a09eSEric Anholt 51957692c94SEric Anholt /** 52057692c94SEric Anholt * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D. 52157692c94SEric Anholt * @dev: DRM device 52257692c94SEric Anholt * @data: ioctl argument 52357692c94SEric Anholt * @file_priv: DRM file for this fd 52457692c94SEric Anholt * 52557692c94SEric Anholt * This is the main entrypoint for userspace to submit a 3D frame to 52657692c94SEric Anholt * the GPU. Userspace provides the binner command list (if 52757692c94SEric Anholt * applicable), and the kernel sets up the render command list to draw 52857692c94SEric Anholt * to the framebuffer described in the ioctl, using the command lists 52957692c94SEric Anholt * that the 3D engine's binner will produce. 53057692c94SEric Anholt */ 53157692c94SEric Anholt int 53257692c94SEric Anholt v3d_submit_cl_ioctl(struct drm_device *dev, void *data, 53357692c94SEric Anholt struct drm_file *file_priv) 53457692c94SEric Anholt { 53557692c94SEric Anholt struct v3d_dev *v3d = to_v3d_dev(dev); 53657692c94SEric Anholt struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 53757692c94SEric Anholt struct drm_v3d_submit_cl *args = data; 538a783a09eSEric Anholt struct v3d_bin_job *bin = NULL; 539a783a09eSEric Anholt struct v3d_render_job *render; 540455d56ceSIago Toral Quiroga struct v3d_job *clean_job = NULL; 541455d56ceSIago Toral Quiroga struct v3d_job *last_job; 54257692c94SEric Anholt struct ww_acquire_ctx acquire_ctx; 54357692c94SEric Anholt int ret = 0; 54457692c94SEric Anholt 54555a9b748SEric Anholt trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end); 54655a9b748SEric Anholt 547*26a4dc29SJuan A. Suarez Romero if (args->pad != 0) 548*26a4dc29SJuan A. Suarez Romero return -EINVAL; 549*26a4dc29SJuan A. Suarez Romero 550455d56ceSIago Toral Quiroga if (args->flags != 0 && 551455d56ceSIago Toral Quiroga args->flags != DRM_V3D_SUBMIT_CL_FLUSH_CACHE) { 552455d56ceSIago Toral Quiroga DRM_INFO("invalid flags: %d\n", args->flags); 55357692c94SEric Anholt return -EINVAL; 55457692c94SEric Anholt } 55557692c94SEric Anholt 556a783a09eSEric Anholt render = kcalloc(1, sizeof(*render), GFP_KERNEL); 557a783a09eSEric Anholt if (!render) 55857692c94SEric Anholt return -ENOMEM; 55957692c94SEric Anholt 560a783a09eSEric Anholt render->start = args->rcl_start; 561a783a09eSEric Anholt render->end = args->rcl_end; 562a783a09eSEric Anholt INIT_LIST_HEAD(&render->unref_list); 563a783a09eSEric Anholt 564a783a09eSEric Anholt ret = v3d_job_init(v3d, file_priv, &render->base, 565a783a09eSEric Anholt v3d_render_job_free, args->in_sync_rcl); 566a783a09eSEric Anholt if (ret) { 567a783a09eSEric Anholt kfree(render); 56857692c94SEric Anholt return ret; 56957692c94SEric Anholt } 57057692c94SEric Anholt 571a783a09eSEric Anholt if (args->bcl_start != args->bcl_end) { 572a783a09eSEric Anholt bin = kcalloc(1, sizeof(*bin), GFP_KERNEL); 57329cd13cfSNavid Emamdoost if (!bin) { 57429cd13cfSNavid Emamdoost v3d_job_put(&render->base); 575a783a09eSEric Anholt return -ENOMEM; 57629cd13cfSNavid Emamdoost } 57757692c94SEric Anholt 578a783a09eSEric Anholt ret = v3d_job_init(v3d, file_priv, &bin->base, 579a783a09eSEric Anholt v3d_job_free, args->in_sync_bcl); 580a783a09eSEric Anholt if (ret) { 581a783a09eSEric Anholt v3d_job_put(&render->base); 58229cd13cfSNavid Emamdoost kfree(bin); 583a783a09eSEric Anholt return ret; 584a783a09eSEric Anholt } 58557692c94SEric Anholt 586a783a09eSEric Anholt bin->start = args->bcl_start; 587a783a09eSEric Anholt bin->end = args->bcl_end; 588a783a09eSEric Anholt bin->qma = args->qma; 589a783a09eSEric Anholt bin->qms = args->qms; 590a783a09eSEric Anholt bin->qts = args->qts; 591a783a09eSEric Anholt bin->render = render; 592a783a09eSEric Anholt } 59357692c94SEric Anholt 594455d56ceSIago Toral Quiroga if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) { 595455d56ceSIago Toral Quiroga clean_job = kcalloc(1, sizeof(*clean_job), GFP_KERNEL); 596455d56ceSIago Toral Quiroga if (!clean_job) { 597455d56ceSIago Toral Quiroga ret = -ENOMEM; 598455d56ceSIago Toral Quiroga goto fail; 599455d56ceSIago Toral Quiroga } 600455d56ceSIago Toral Quiroga 601455d56ceSIago Toral Quiroga ret = v3d_job_init(v3d, file_priv, clean_job, v3d_job_free, 0); 602455d56ceSIago Toral Quiroga if (ret) { 603455d56ceSIago Toral Quiroga kfree(clean_job); 604455d56ceSIago Toral Quiroga clean_job = NULL; 605455d56ceSIago Toral Quiroga goto fail; 606455d56ceSIago Toral Quiroga } 607455d56ceSIago Toral Quiroga 608455d56ceSIago Toral Quiroga last_job = clean_job; 609455d56ceSIago Toral Quiroga } else { 610455d56ceSIago Toral Quiroga last_job = &render->base; 611455d56ceSIago Toral Quiroga } 612455d56ceSIago Toral Quiroga 613455d56ceSIago Toral Quiroga ret = v3d_lookup_bos(dev, file_priv, last_job, 614a783a09eSEric Anholt args->bo_handles, args->bo_handle_count); 61557692c94SEric Anholt if (ret) 61657692c94SEric Anholt goto fail; 61757692c94SEric Anholt 618455d56ceSIago Toral Quiroga ret = v3d_lock_bo_reservations(last_job, &acquire_ctx); 61957692c94SEric Anholt if (ret) 62057692c94SEric Anholt goto fail; 62157692c94SEric Anholt 622*26a4dc29SJuan A. Suarez Romero if (args->perfmon_id) { 623*26a4dc29SJuan A. Suarez Romero render->base.perfmon = v3d_perfmon_find(v3d_priv, 624*26a4dc29SJuan A. Suarez Romero args->perfmon_id); 625*26a4dc29SJuan A. Suarez Romero 626*26a4dc29SJuan A. Suarez Romero if (!render->base.perfmon) { 627*26a4dc29SJuan A. Suarez Romero ret = -ENOENT; 628*26a4dc29SJuan A. Suarez Romero goto fail; 629*26a4dc29SJuan A. Suarez Romero } 630*26a4dc29SJuan A. Suarez Romero } 631*26a4dc29SJuan A. Suarez Romero 6327122b68bSEric Anholt mutex_lock(&v3d->sched_lock); 633a783a09eSEric Anholt if (bin) { 634*26a4dc29SJuan A. Suarez Romero bin->base.perfmon = render->base.perfmon; 635*26a4dc29SJuan A. Suarez Romero v3d_perfmon_get(bin->base.perfmon); 636a783a09eSEric Anholt ret = v3d_push_job(v3d_priv, &bin->base, V3D_BIN); 63757692c94SEric Anholt if (ret) 63857692c94SEric Anholt goto fail_unreserve; 63957692c94SEric Anholt 640dffa9b7aSEric Anholt ret = drm_gem_fence_array_add(&render->base.deps, 641dffa9b7aSEric Anholt dma_fence_get(bin->base.done_fence)); 642dffa9b7aSEric Anholt if (ret) 643dffa9b7aSEric Anholt goto fail_unreserve; 64457692c94SEric Anholt } 64557692c94SEric Anholt 646a783a09eSEric Anholt ret = v3d_push_job(v3d_priv, &render->base, V3D_RENDER); 64757692c94SEric Anholt if (ret) 64857692c94SEric Anholt goto fail_unreserve; 649455d56ceSIago Toral Quiroga 650455d56ceSIago Toral Quiroga if (clean_job) { 651455d56ceSIago Toral Quiroga struct dma_fence *render_fence = 652455d56ceSIago Toral Quiroga dma_fence_get(render->base.done_fence); 653455d56ceSIago Toral Quiroga ret = drm_gem_fence_array_add(&clean_job->deps, render_fence); 654455d56ceSIago Toral Quiroga if (ret) 655455d56ceSIago Toral Quiroga goto fail_unreserve; 656*26a4dc29SJuan A. Suarez Romero clean_job->perfmon = render->base.perfmon; 657*26a4dc29SJuan A. Suarez Romero v3d_perfmon_get(clean_job->perfmon); 658455d56ceSIago Toral Quiroga ret = v3d_push_job(v3d_priv, clean_job, V3D_CACHE_CLEAN); 659455d56ceSIago Toral Quiroga if (ret) 660455d56ceSIago Toral Quiroga goto fail_unreserve; 661455d56ceSIago Toral Quiroga } 662455d56ceSIago Toral Quiroga 6637122b68bSEric Anholt mutex_unlock(&v3d->sched_lock); 66457692c94SEric Anholt 665a783a09eSEric Anholt v3d_attach_fences_and_unlock_reservation(file_priv, 666455d56ceSIago Toral Quiroga last_job, 667d223f98fSEric Anholt &acquire_ctx, 668d223f98fSEric Anholt args->out_sync, 669455d56ceSIago Toral Quiroga last_job->done_fence); 67057692c94SEric Anholt 671a783a09eSEric Anholt if (bin) 672a783a09eSEric Anholt v3d_job_put(&bin->base); 673a783a09eSEric Anholt v3d_job_put(&render->base); 674455d56ceSIago Toral Quiroga if (clean_job) 675455d56ceSIago Toral Quiroga v3d_job_put(clean_job); 67657692c94SEric Anholt 67757692c94SEric Anholt return 0; 67857692c94SEric Anholt 67957692c94SEric Anholt fail_unreserve: 6807122b68bSEric Anholt mutex_unlock(&v3d->sched_lock); 681455d56ceSIago Toral Quiroga drm_gem_unlock_reservations(last_job->bo, 682455d56ceSIago Toral Quiroga last_job->bo_count, &acquire_ctx); 68357692c94SEric Anholt fail: 684a783a09eSEric Anholt if (bin) 685a783a09eSEric Anholt v3d_job_put(&bin->base); 686a783a09eSEric Anholt v3d_job_put(&render->base); 687455d56ceSIago Toral Quiroga if (clean_job) 688455d56ceSIago Toral Quiroga v3d_job_put(clean_job); 68957692c94SEric Anholt 69057692c94SEric Anholt return ret; 69157692c94SEric Anholt } 69257692c94SEric Anholt 6931584f16cSEric Anholt /** 6941584f16cSEric Anholt * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D. 6951584f16cSEric Anholt * @dev: DRM device 6961584f16cSEric Anholt * @data: ioctl argument 6971584f16cSEric Anholt * @file_priv: DRM file for this fd 6981584f16cSEric Anholt * 6991584f16cSEric Anholt * Userspace provides the register setup for the TFU, which we don't 7001584f16cSEric Anholt * need to validate since the TFU is behind the MMU. 7011584f16cSEric Anholt */ 7021584f16cSEric Anholt int 7031584f16cSEric Anholt v3d_submit_tfu_ioctl(struct drm_device *dev, void *data, 7041584f16cSEric Anholt struct drm_file *file_priv) 7051584f16cSEric Anholt { 7061584f16cSEric Anholt struct v3d_dev *v3d = to_v3d_dev(dev); 7071584f16cSEric Anholt struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 7081584f16cSEric Anholt struct drm_v3d_submit_tfu *args = data; 7091584f16cSEric Anholt struct v3d_tfu_job *job; 7101584f16cSEric Anholt struct ww_acquire_ctx acquire_ctx; 7111584f16cSEric Anholt int ret = 0; 7121584f16cSEric Anholt 71355a9b748SEric Anholt trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia); 71455a9b748SEric Anholt 7151584f16cSEric Anholt job = kcalloc(1, sizeof(*job), GFP_KERNEL); 7161584f16cSEric Anholt if (!job) 7171584f16cSEric Anholt return -ENOMEM; 7181584f16cSEric Anholt 719a783a09eSEric Anholt ret = v3d_job_init(v3d, file_priv, &job->base, 720a783a09eSEric Anholt v3d_job_free, args->in_sync); 721a783a09eSEric Anholt if (ret) { 7221584f16cSEric Anholt kfree(job); 7231584f16cSEric Anholt return ret; 7241584f16cSEric Anholt } 7251584f16cSEric Anholt 726a783a09eSEric Anholt job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles), 727a783a09eSEric Anholt sizeof(*job->base.bo), GFP_KERNEL); 728a783a09eSEric Anholt if (!job->base.bo) { 729a783a09eSEric Anholt v3d_job_put(&job->base); 730a783a09eSEric Anholt return -ENOMEM; 731a783a09eSEric Anholt } 7321584f16cSEric Anholt 7331584f16cSEric Anholt job->args = *args; 7341584f16cSEric Anholt 7351584f16cSEric Anholt spin_lock(&file_priv->table_lock); 736a783a09eSEric Anholt for (job->base.bo_count = 0; 737a783a09eSEric Anholt job->base.bo_count < ARRAY_SIZE(args->bo_handles); 738a783a09eSEric Anholt job->base.bo_count++) { 7391584f16cSEric Anholt struct drm_gem_object *bo; 7401584f16cSEric Anholt 741a783a09eSEric Anholt if (!args->bo_handles[job->base.bo_count]) 7421584f16cSEric Anholt break; 7431584f16cSEric Anholt 7441584f16cSEric Anholt bo = idr_find(&file_priv->object_idr, 745a783a09eSEric Anholt args->bo_handles[job->base.bo_count]); 7461584f16cSEric Anholt if (!bo) { 7471584f16cSEric Anholt DRM_DEBUG("Failed to look up GEM BO %d: %d\n", 748a783a09eSEric Anholt job->base.bo_count, 749a783a09eSEric Anholt args->bo_handles[job->base.bo_count]); 7501584f16cSEric Anholt ret = -ENOENT; 7511584f16cSEric Anholt spin_unlock(&file_priv->table_lock); 7521584f16cSEric Anholt goto fail; 7531584f16cSEric Anholt } 7541584f16cSEric Anholt drm_gem_object_get(bo); 755a783a09eSEric Anholt job->base.bo[job->base.bo_count] = bo; 7561584f16cSEric Anholt } 7571584f16cSEric Anholt spin_unlock(&file_priv->table_lock); 7581584f16cSEric Anholt 759dffa9b7aSEric Anholt ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx); 7601584f16cSEric Anholt if (ret) 7611584f16cSEric Anholt goto fail; 7621584f16cSEric Anholt 7631584f16cSEric Anholt mutex_lock(&v3d->sched_lock); 764a783a09eSEric Anholt ret = v3d_push_job(v3d_priv, &job->base, V3D_TFU); 7651584f16cSEric Anholt if (ret) 7661584f16cSEric Anholt goto fail_unreserve; 7671584f16cSEric Anholt mutex_unlock(&v3d->sched_lock); 7681584f16cSEric Anholt 769a783a09eSEric Anholt v3d_attach_fences_and_unlock_reservation(file_priv, 770a783a09eSEric Anholt &job->base, &acquire_ctx, 771d223f98fSEric Anholt args->out_sync, 772d223f98fSEric Anholt job->base.done_fence); 7731584f16cSEric Anholt 774a783a09eSEric Anholt v3d_job_put(&job->base); 7751584f16cSEric Anholt 7761584f16cSEric Anholt return 0; 7771584f16cSEric Anholt 7781584f16cSEric Anholt fail_unreserve: 7791584f16cSEric Anholt mutex_unlock(&v3d->sched_lock); 780a783a09eSEric Anholt drm_gem_unlock_reservations(job->base.bo, job->base.bo_count, 781a783a09eSEric Anholt &acquire_ctx); 7821584f16cSEric Anholt fail: 783a783a09eSEric Anholt v3d_job_put(&job->base); 7841584f16cSEric Anholt 7851584f16cSEric Anholt return ret; 7861584f16cSEric Anholt } 7871584f16cSEric Anholt 788d223f98fSEric Anholt /** 789d223f98fSEric Anholt * v3d_submit_csd_ioctl() - Submits a CSD (texture formatting) job to the V3D. 790d223f98fSEric Anholt * @dev: DRM device 791d223f98fSEric Anholt * @data: ioctl argument 792d223f98fSEric Anholt * @file_priv: DRM file for this fd 793d223f98fSEric Anholt * 794d223f98fSEric Anholt * Userspace provides the register setup for the CSD, which we don't 795d223f98fSEric Anholt * need to validate since the CSD is behind the MMU. 796d223f98fSEric Anholt */ 797d223f98fSEric Anholt int 798d223f98fSEric Anholt v3d_submit_csd_ioctl(struct drm_device *dev, void *data, 799d223f98fSEric Anholt struct drm_file *file_priv) 800d223f98fSEric Anholt { 801d223f98fSEric Anholt struct v3d_dev *v3d = to_v3d_dev(dev); 802d223f98fSEric Anholt struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 803d223f98fSEric Anholt struct drm_v3d_submit_csd *args = data; 804d223f98fSEric Anholt struct v3d_csd_job *job; 805d223f98fSEric Anholt struct v3d_job *clean_job; 806d223f98fSEric Anholt struct ww_acquire_ctx acquire_ctx; 807d223f98fSEric Anholt int ret; 808d223f98fSEric Anholt 809d223f98fSEric Anholt trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]); 810d223f98fSEric Anholt 811d223f98fSEric Anholt if (!v3d_has_csd(v3d)) { 812d223f98fSEric Anholt DRM_DEBUG("Attempting CSD submit on non-CSD hardware\n"); 813d223f98fSEric Anholt return -EINVAL; 814d223f98fSEric Anholt } 815d223f98fSEric Anholt 816d223f98fSEric Anholt job = kcalloc(1, sizeof(*job), GFP_KERNEL); 817d223f98fSEric Anholt if (!job) 818d223f98fSEric Anholt return -ENOMEM; 819d223f98fSEric Anholt 820d223f98fSEric Anholt ret = v3d_job_init(v3d, file_priv, &job->base, 821d223f98fSEric Anholt v3d_job_free, args->in_sync); 822d223f98fSEric Anholt if (ret) { 823d223f98fSEric Anholt kfree(job); 824d223f98fSEric Anholt return ret; 825d223f98fSEric Anholt } 826d223f98fSEric Anholt 827d223f98fSEric Anholt clean_job = kcalloc(1, sizeof(*clean_job), GFP_KERNEL); 828d223f98fSEric Anholt if (!clean_job) { 829d223f98fSEric Anholt v3d_job_put(&job->base); 830d223f98fSEric Anholt kfree(job); 831d223f98fSEric Anholt return -ENOMEM; 832d223f98fSEric Anholt } 833d223f98fSEric Anholt 834d223f98fSEric Anholt ret = v3d_job_init(v3d, file_priv, clean_job, v3d_job_free, 0); 835d223f98fSEric Anholt if (ret) { 836d223f98fSEric Anholt v3d_job_put(&job->base); 837d223f98fSEric Anholt kfree(clean_job); 838d223f98fSEric Anholt return ret; 839d223f98fSEric Anholt } 840d223f98fSEric Anholt 841d223f98fSEric Anholt job->args = *args; 842d223f98fSEric Anholt 843d223f98fSEric Anholt ret = v3d_lookup_bos(dev, file_priv, clean_job, 844d223f98fSEric Anholt args->bo_handles, args->bo_handle_count); 845d223f98fSEric Anholt if (ret) 846d223f98fSEric Anholt goto fail; 847d223f98fSEric Anholt 848dffa9b7aSEric Anholt ret = v3d_lock_bo_reservations(clean_job, &acquire_ctx); 849d223f98fSEric Anholt if (ret) 850d223f98fSEric Anholt goto fail; 851d223f98fSEric Anholt 852*26a4dc29SJuan A. Suarez Romero if (args->perfmon_id) { 853*26a4dc29SJuan A. Suarez Romero job->base.perfmon = v3d_perfmon_find(v3d_priv, 854*26a4dc29SJuan A. Suarez Romero args->perfmon_id); 855*26a4dc29SJuan A. Suarez Romero if (!job->base.perfmon) { 856*26a4dc29SJuan A. Suarez Romero ret = -ENOENT; 857*26a4dc29SJuan A. Suarez Romero goto fail; 858*26a4dc29SJuan A. Suarez Romero } 859*26a4dc29SJuan A. Suarez Romero } 860*26a4dc29SJuan A. Suarez Romero 861d223f98fSEric Anholt mutex_lock(&v3d->sched_lock); 862d223f98fSEric Anholt ret = v3d_push_job(v3d_priv, &job->base, V3D_CSD); 863d223f98fSEric Anholt if (ret) 864d223f98fSEric Anholt goto fail_unreserve; 865d223f98fSEric Anholt 866dffa9b7aSEric Anholt ret = drm_gem_fence_array_add(&clean_job->deps, 867dffa9b7aSEric Anholt dma_fence_get(job->base.done_fence)); 868dffa9b7aSEric Anholt if (ret) 869dffa9b7aSEric Anholt goto fail_unreserve; 870dffa9b7aSEric Anholt 871d223f98fSEric Anholt ret = v3d_push_job(v3d_priv, clean_job, V3D_CACHE_CLEAN); 872d223f98fSEric Anholt if (ret) 873d223f98fSEric Anholt goto fail_unreserve; 874d223f98fSEric Anholt mutex_unlock(&v3d->sched_lock); 875d223f98fSEric Anholt 876d223f98fSEric Anholt v3d_attach_fences_and_unlock_reservation(file_priv, 877d223f98fSEric Anholt clean_job, 878d223f98fSEric Anholt &acquire_ctx, 879d223f98fSEric Anholt args->out_sync, 880d223f98fSEric Anholt clean_job->done_fence); 881d223f98fSEric Anholt 882d223f98fSEric Anholt v3d_job_put(&job->base); 883d223f98fSEric Anholt v3d_job_put(clean_job); 884d223f98fSEric Anholt 885d223f98fSEric Anholt return 0; 886d223f98fSEric Anholt 887d223f98fSEric Anholt fail_unreserve: 888d223f98fSEric Anholt mutex_unlock(&v3d->sched_lock); 889d223f98fSEric Anholt drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count, 890d223f98fSEric Anholt &acquire_ctx); 891d223f98fSEric Anholt fail: 892d223f98fSEric Anholt v3d_job_put(&job->base); 893d223f98fSEric Anholt v3d_job_put(clean_job); 894d223f98fSEric Anholt 895d223f98fSEric Anholt return ret; 896d223f98fSEric Anholt } 897d223f98fSEric Anholt 89857692c94SEric Anholt int 89957692c94SEric Anholt v3d_gem_init(struct drm_device *dev) 90057692c94SEric Anholt { 90157692c94SEric Anholt struct v3d_dev *v3d = to_v3d_dev(dev); 90257692c94SEric Anholt u32 pt_size = 4096 * 1024; 90357692c94SEric Anholt int ret, i; 90457692c94SEric Anholt 90557692c94SEric Anholt for (i = 0; i < V3D_MAX_QUEUES; i++) 90657692c94SEric Anholt v3d->queue[i].fence_context = dma_fence_context_alloc(1); 90757692c94SEric Anholt 90857692c94SEric Anholt spin_lock_init(&v3d->mm_lock); 90957692c94SEric Anholt spin_lock_init(&v3d->job_lock); 91057692c94SEric Anholt mutex_init(&v3d->bo_lock); 91157692c94SEric Anholt mutex_init(&v3d->reset_lock); 9127122b68bSEric Anholt mutex_init(&v3d->sched_lock); 913d223f98fSEric Anholt mutex_init(&v3d->cache_clean_lock); 91457692c94SEric Anholt 91557692c94SEric Anholt /* Note: We don't allocate address 0. Various bits of HW 91657692c94SEric Anholt * treat 0 as special, such as the occlusion query counters 91757692c94SEric Anholt * where 0 means "disabled". 91857692c94SEric Anholt */ 91957692c94SEric Anholt drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1); 92057692c94SEric Anholt 921bc662528SDaniel Vetter v3d->pt = dma_alloc_wc(v3d->drm.dev, pt_size, 92257692c94SEric Anholt &v3d->pt_paddr, 92357692c94SEric Anholt GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO); 92457692c94SEric Anholt if (!v3d->pt) { 92557692c94SEric Anholt drm_mm_takedown(&v3d->mm); 926bc662528SDaniel Vetter dev_err(v3d->drm.dev, 92757692c94SEric Anholt "Failed to allocate page tables. " 92857692c94SEric Anholt "Please ensure you have CMA enabled.\n"); 92957692c94SEric Anholt return -ENOMEM; 93057692c94SEric Anholt } 93157692c94SEric Anholt 93257692c94SEric Anholt v3d_init_hw_state(v3d); 93357692c94SEric Anholt v3d_mmu_set_page_table(v3d); 93457692c94SEric Anholt 93557692c94SEric Anholt ret = v3d_sched_init(v3d); 93657692c94SEric Anholt if (ret) { 93757692c94SEric Anholt drm_mm_takedown(&v3d->mm); 938bc662528SDaniel Vetter dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt, 93957692c94SEric Anholt v3d->pt_paddr); 94057692c94SEric Anholt } 94157692c94SEric Anholt 94257692c94SEric Anholt return 0; 94357692c94SEric Anholt } 94457692c94SEric Anholt 94557692c94SEric Anholt void 94657692c94SEric Anholt v3d_gem_destroy(struct drm_device *dev) 94757692c94SEric Anholt { 94857692c94SEric Anholt struct v3d_dev *v3d = to_v3d_dev(dev); 94957692c94SEric Anholt 95057692c94SEric Anholt v3d_sched_fini(v3d); 95157692c94SEric Anholt 952a783a09eSEric Anholt /* Waiting for jobs to finish would need to be done before 95357692c94SEric Anholt * unregistering V3D. 95457692c94SEric Anholt */ 95514d1d190SEric Anholt WARN_ON(v3d->bin_job); 95614d1d190SEric Anholt WARN_ON(v3d->render_job); 95757692c94SEric Anholt 95857692c94SEric Anholt drm_mm_takedown(&v3d->mm); 95957692c94SEric Anholt 960bc662528SDaniel Vetter dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt, 961bc662528SDaniel Vetter v3d->pt_paddr); 96257692c94SEric Anholt } 963