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 12926a4dc29SJuan A. Suarez Romero v3d_perfmon_stop(v3d, v3d->active_perfmon, false); 13026a4dc29SJuan 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 38026a4dc29SJuan A. Suarez Romero if (job->perfmon) 38126a4dc29SJuan A. Suarez Romero v3d_perfmon_put(job->perfmon); 38226a4dc29SJuan 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 483dbe48d03SDaniel Vetter drm_sched_job_arm(&job->base); 484dbe48d03SDaniel Vetter 485a783a09eSEric Anholt job->done_fence = dma_fence_get(&job->base.s_fence->finished); 486a783a09eSEric Anholt 487a783a09eSEric Anholt /* put by scheduler job completion */ 488a783a09eSEric Anholt kref_get(&job->refcount); 489a783a09eSEric Anholt 490*0e10e9a1SDaniel Vetter drm_sched_entity_push_job(&job->base); 491a783a09eSEric Anholt 492a783a09eSEric Anholt return 0; 493a783a09eSEric Anholt } 494a783a09eSEric Anholt 495a783a09eSEric Anholt static void 496a783a09eSEric Anholt v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv, 497a783a09eSEric Anholt struct v3d_job *job, 498a783a09eSEric Anholt struct ww_acquire_ctx *acquire_ctx, 499d223f98fSEric Anholt u32 out_sync, 500d223f98fSEric Anholt struct dma_fence *done_fence) 501a783a09eSEric Anholt { 502a783a09eSEric Anholt struct drm_syncobj *sync_out; 503a783a09eSEric Anholt int i; 504a783a09eSEric Anholt 505a783a09eSEric Anholt for (i = 0; i < job->bo_count; i++) { 506a783a09eSEric Anholt /* XXX: Use shared fences for read-only objects. */ 50752791eeeSChristian König dma_resv_add_excl_fence(job->bo[i]->resv, 508a783a09eSEric Anholt job->done_fence); 509a783a09eSEric Anholt } 510a783a09eSEric Anholt 511a783a09eSEric Anholt drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx); 512a783a09eSEric Anholt 513a783a09eSEric Anholt /* Update the return sync object for the job */ 514a783a09eSEric Anholt sync_out = drm_syncobj_find(file_priv, out_sync); 515a783a09eSEric Anholt if (sync_out) { 516d223f98fSEric Anholt drm_syncobj_replace_fence(sync_out, done_fence); 517a783a09eSEric Anholt drm_syncobj_put(sync_out); 518a783a09eSEric Anholt } 519a783a09eSEric Anholt } 520a783a09eSEric Anholt 52157692c94SEric Anholt /** 52257692c94SEric Anholt * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D. 52357692c94SEric Anholt * @dev: DRM device 52457692c94SEric Anholt * @data: ioctl argument 52557692c94SEric Anholt * @file_priv: DRM file for this fd 52657692c94SEric Anholt * 52757692c94SEric Anholt * This is the main entrypoint for userspace to submit a 3D frame to 52857692c94SEric Anholt * the GPU. Userspace provides the binner command list (if 52957692c94SEric Anholt * applicable), and the kernel sets up the render command list to draw 53057692c94SEric Anholt * to the framebuffer described in the ioctl, using the command lists 53157692c94SEric Anholt * that the 3D engine's binner will produce. 53257692c94SEric Anholt */ 53357692c94SEric Anholt int 53457692c94SEric Anholt v3d_submit_cl_ioctl(struct drm_device *dev, void *data, 53557692c94SEric Anholt struct drm_file *file_priv) 53657692c94SEric Anholt { 53757692c94SEric Anholt struct v3d_dev *v3d = to_v3d_dev(dev); 53857692c94SEric Anholt struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 53957692c94SEric Anholt struct drm_v3d_submit_cl *args = data; 540a783a09eSEric Anholt struct v3d_bin_job *bin = NULL; 541a783a09eSEric Anholt struct v3d_render_job *render; 542455d56ceSIago Toral Quiroga struct v3d_job *clean_job = NULL; 543455d56ceSIago Toral Quiroga struct v3d_job *last_job; 54457692c94SEric Anholt struct ww_acquire_ctx acquire_ctx; 54557692c94SEric Anholt int ret = 0; 54657692c94SEric Anholt 54755a9b748SEric Anholt trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end); 54855a9b748SEric Anholt 54926a4dc29SJuan A. Suarez Romero if (args->pad != 0) 55026a4dc29SJuan A. Suarez Romero return -EINVAL; 55126a4dc29SJuan A. Suarez Romero 552455d56ceSIago Toral Quiroga if (args->flags != 0 && 553455d56ceSIago Toral Quiroga args->flags != DRM_V3D_SUBMIT_CL_FLUSH_CACHE) { 554455d56ceSIago Toral Quiroga DRM_INFO("invalid flags: %d\n", args->flags); 55557692c94SEric Anholt return -EINVAL; 55657692c94SEric Anholt } 55757692c94SEric Anholt 558a783a09eSEric Anholt render = kcalloc(1, sizeof(*render), GFP_KERNEL); 559a783a09eSEric Anholt if (!render) 56057692c94SEric Anholt return -ENOMEM; 56157692c94SEric Anholt 562a783a09eSEric Anholt render->start = args->rcl_start; 563a783a09eSEric Anholt render->end = args->rcl_end; 564a783a09eSEric Anholt INIT_LIST_HEAD(&render->unref_list); 565a783a09eSEric Anholt 566a783a09eSEric Anholt ret = v3d_job_init(v3d, file_priv, &render->base, 567a783a09eSEric Anholt v3d_render_job_free, args->in_sync_rcl); 568a783a09eSEric Anholt if (ret) { 569a783a09eSEric Anholt kfree(render); 57057692c94SEric Anholt return ret; 57157692c94SEric Anholt } 57257692c94SEric Anholt 573a783a09eSEric Anholt if (args->bcl_start != args->bcl_end) { 574a783a09eSEric Anholt bin = kcalloc(1, sizeof(*bin), GFP_KERNEL); 57529cd13cfSNavid Emamdoost if (!bin) { 57629cd13cfSNavid Emamdoost v3d_job_put(&render->base); 577a783a09eSEric Anholt return -ENOMEM; 57829cd13cfSNavid Emamdoost } 57957692c94SEric Anholt 580a783a09eSEric Anholt ret = v3d_job_init(v3d, file_priv, &bin->base, 581a783a09eSEric Anholt v3d_job_free, args->in_sync_bcl); 582a783a09eSEric Anholt if (ret) { 583a783a09eSEric Anholt v3d_job_put(&render->base); 58429cd13cfSNavid Emamdoost kfree(bin); 585a783a09eSEric Anholt return ret; 586a783a09eSEric Anholt } 58757692c94SEric Anholt 588a783a09eSEric Anholt bin->start = args->bcl_start; 589a783a09eSEric Anholt bin->end = args->bcl_end; 590a783a09eSEric Anholt bin->qma = args->qma; 591a783a09eSEric Anholt bin->qms = args->qms; 592a783a09eSEric Anholt bin->qts = args->qts; 593a783a09eSEric Anholt bin->render = render; 594a783a09eSEric Anholt } 59557692c94SEric Anholt 596455d56ceSIago Toral Quiroga if (args->flags & DRM_V3D_SUBMIT_CL_FLUSH_CACHE) { 597455d56ceSIago Toral Quiroga clean_job = kcalloc(1, sizeof(*clean_job), GFP_KERNEL); 598455d56ceSIago Toral Quiroga if (!clean_job) { 599455d56ceSIago Toral Quiroga ret = -ENOMEM; 600455d56ceSIago Toral Quiroga goto fail; 601455d56ceSIago Toral Quiroga } 602455d56ceSIago Toral Quiroga 603455d56ceSIago Toral Quiroga ret = v3d_job_init(v3d, file_priv, clean_job, v3d_job_free, 0); 604455d56ceSIago Toral Quiroga if (ret) { 605455d56ceSIago Toral Quiroga kfree(clean_job); 606455d56ceSIago Toral Quiroga clean_job = NULL; 607455d56ceSIago Toral Quiroga goto fail; 608455d56ceSIago Toral Quiroga } 609455d56ceSIago Toral Quiroga 610455d56ceSIago Toral Quiroga last_job = clean_job; 611455d56ceSIago Toral Quiroga } else { 612455d56ceSIago Toral Quiroga last_job = &render->base; 613455d56ceSIago Toral Quiroga } 614455d56ceSIago Toral Quiroga 615455d56ceSIago Toral Quiroga ret = v3d_lookup_bos(dev, file_priv, last_job, 616a783a09eSEric Anholt args->bo_handles, args->bo_handle_count); 61757692c94SEric Anholt if (ret) 61857692c94SEric Anholt goto fail; 61957692c94SEric Anholt 620455d56ceSIago Toral Quiroga ret = v3d_lock_bo_reservations(last_job, &acquire_ctx); 62157692c94SEric Anholt if (ret) 62257692c94SEric Anholt goto fail; 62357692c94SEric Anholt 62426a4dc29SJuan A. Suarez Romero if (args->perfmon_id) { 62526a4dc29SJuan A. Suarez Romero render->base.perfmon = v3d_perfmon_find(v3d_priv, 62626a4dc29SJuan A. Suarez Romero args->perfmon_id); 62726a4dc29SJuan A. Suarez Romero 62826a4dc29SJuan A. Suarez Romero if (!render->base.perfmon) { 62926a4dc29SJuan A. Suarez Romero ret = -ENOENT; 63026a4dc29SJuan A. Suarez Romero goto fail; 63126a4dc29SJuan A. Suarez Romero } 63226a4dc29SJuan A. Suarez Romero } 63326a4dc29SJuan A. Suarez Romero 6347122b68bSEric Anholt mutex_lock(&v3d->sched_lock); 635a783a09eSEric Anholt if (bin) { 63626a4dc29SJuan A. Suarez Romero bin->base.perfmon = render->base.perfmon; 63726a4dc29SJuan A. Suarez Romero v3d_perfmon_get(bin->base.perfmon); 638a783a09eSEric Anholt ret = v3d_push_job(v3d_priv, &bin->base, V3D_BIN); 63957692c94SEric Anholt if (ret) 64057692c94SEric Anholt goto fail_unreserve; 64157692c94SEric Anholt 642dffa9b7aSEric Anholt ret = drm_gem_fence_array_add(&render->base.deps, 643dffa9b7aSEric Anholt dma_fence_get(bin->base.done_fence)); 644dffa9b7aSEric Anholt if (ret) 645dffa9b7aSEric Anholt goto fail_unreserve; 64657692c94SEric Anholt } 64757692c94SEric Anholt 648a783a09eSEric Anholt ret = v3d_push_job(v3d_priv, &render->base, V3D_RENDER); 64957692c94SEric Anholt if (ret) 65057692c94SEric Anholt goto fail_unreserve; 651455d56ceSIago Toral Quiroga 652455d56ceSIago Toral Quiroga if (clean_job) { 653455d56ceSIago Toral Quiroga struct dma_fence *render_fence = 654455d56ceSIago Toral Quiroga dma_fence_get(render->base.done_fence); 655455d56ceSIago Toral Quiroga ret = drm_gem_fence_array_add(&clean_job->deps, render_fence); 656455d56ceSIago Toral Quiroga if (ret) 657455d56ceSIago Toral Quiroga goto fail_unreserve; 65826a4dc29SJuan A. Suarez Romero clean_job->perfmon = render->base.perfmon; 65926a4dc29SJuan A. Suarez Romero v3d_perfmon_get(clean_job->perfmon); 660455d56ceSIago Toral Quiroga ret = v3d_push_job(v3d_priv, clean_job, V3D_CACHE_CLEAN); 661455d56ceSIago Toral Quiroga if (ret) 662455d56ceSIago Toral Quiroga goto fail_unreserve; 663455d56ceSIago Toral Quiroga } 664455d56ceSIago Toral Quiroga 6657122b68bSEric Anholt mutex_unlock(&v3d->sched_lock); 66657692c94SEric Anholt 667a783a09eSEric Anholt v3d_attach_fences_and_unlock_reservation(file_priv, 668455d56ceSIago Toral Quiroga last_job, 669d223f98fSEric Anholt &acquire_ctx, 670d223f98fSEric Anholt args->out_sync, 671455d56ceSIago Toral Quiroga last_job->done_fence); 67257692c94SEric Anholt 673a783a09eSEric Anholt if (bin) 674a783a09eSEric Anholt v3d_job_put(&bin->base); 675a783a09eSEric Anholt v3d_job_put(&render->base); 676455d56ceSIago Toral Quiroga if (clean_job) 677455d56ceSIago Toral Quiroga v3d_job_put(clean_job); 67857692c94SEric Anholt 67957692c94SEric Anholt return 0; 68057692c94SEric Anholt 68157692c94SEric Anholt fail_unreserve: 6827122b68bSEric Anholt mutex_unlock(&v3d->sched_lock); 683455d56ceSIago Toral Quiroga drm_gem_unlock_reservations(last_job->bo, 684455d56ceSIago Toral Quiroga last_job->bo_count, &acquire_ctx); 68557692c94SEric Anholt fail: 686a783a09eSEric Anholt if (bin) 687a783a09eSEric Anholt v3d_job_put(&bin->base); 688a783a09eSEric Anholt v3d_job_put(&render->base); 689455d56ceSIago Toral Quiroga if (clean_job) 690455d56ceSIago Toral Quiroga v3d_job_put(clean_job); 69157692c94SEric Anholt 69257692c94SEric Anholt return ret; 69357692c94SEric Anholt } 69457692c94SEric Anholt 6951584f16cSEric Anholt /** 6961584f16cSEric Anholt * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D. 6971584f16cSEric Anholt * @dev: DRM device 6981584f16cSEric Anholt * @data: ioctl argument 6991584f16cSEric Anholt * @file_priv: DRM file for this fd 7001584f16cSEric Anholt * 7011584f16cSEric Anholt * Userspace provides the register setup for the TFU, which we don't 7021584f16cSEric Anholt * need to validate since the TFU is behind the MMU. 7031584f16cSEric Anholt */ 7041584f16cSEric Anholt int 7051584f16cSEric Anholt v3d_submit_tfu_ioctl(struct drm_device *dev, void *data, 7061584f16cSEric Anholt struct drm_file *file_priv) 7071584f16cSEric Anholt { 7081584f16cSEric Anholt struct v3d_dev *v3d = to_v3d_dev(dev); 7091584f16cSEric Anholt struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 7101584f16cSEric Anholt struct drm_v3d_submit_tfu *args = data; 7111584f16cSEric Anholt struct v3d_tfu_job *job; 7121584f16cSEric Anholt struct ww_acquire_ctx acquire_ctx; 7131584f16cSEric Anholt int ret = 0; 7141584f16cSEric Anholt 71555a9b748SEric Anholt trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia); 71655a9b748SEric Anholt 7171584f16cSEric Anholt job = kcalloc(1, sizeof(*job), GFP_KERNEL); 7181584f16cSEric Anholt if (!job) 7191584f16cSEric Anholt return -ENOMEM; 7201584f16cSEric Anholt 721a783a09eSEric Anholt ret = v3d_job_init(v3d, file_priv, &job->base, 722a783a09eSEric Anholt v3d_job_free, args->in_sync); 723a783a09eSEric Anholt if (ret) { 7241584f16cSEric Anholt kfree(job); 7251584f16cSEric Anholt return ret; 7261584f16cSEric Anholt } 7271584f16cSEric Anholt 728a783a09eSEric Anholt job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles), 729a783a09eSEric Anholt sizeof(*job->base.bo), GFP_KERNEL); 730a783a09eSEric Anholt if (!job->base.bo) { 731a783a09eSEric Anholt v3d_job_put(&job->base); 732a783a09eSEric Anholt return -ENOMEM; 733a783a09eSEric Anholt } 7341584f16cSEric Anholt 7351584f16cSEric Anholt job->args = *args; 7361584f16cSEric Anholt 7371584f16cSEric Anholt spin_lock(&file_priv->table_lock); 738a783a09eSEric Anholt for (job->base.bo_count = 0; 739a783a09eSEric Anholt job->base.bo_count < ARRAY_SIZE(args->bo_handles); 740a783a09eSEric Anholt job->base.bo_count++) { 7411584f16cSEric Anholt struct drm_gem_object *bo; 7421584f16cSEric Anholt 743a783a09eSEric Anholt if (!args->bo_handles[job->base.bo_count]) 7441584f16cSEric Anholt break; 7451584f16cSEric Anholt 7461584f16cSEric Anholt bo = idr_find(&file_priv->object_idr, 747a783a09eSEric Anholt args->bo_handles[job->base.bo_count]); 7481584f16cSEric Anholt if (!bo) { 7491584f16cSEric Anholt DRM_DEBUG("Failed to look up GEM BO %d: %d\n", 750a783a09eSEric Anholt job->base.bo_count, 751a783a09eSEric Anholt args->bo_handles[job->base.bo_count]); 7521584f16cSEric Anholt ret = -ENOENT; 7531584f16cSEric Anholt spin_unlock(&file_priv->table_lock); 7541584f16cSEric Anholt goto fail; 7551584f16cSEric Anholt } 7561584f16cSEric Anholt drm_gem_object_get(bo); 757a783a09eSEric Anholt job->base.bo[job->base.bo_count] = bo; 7581584f16cSEric Anholt } 7591584f16cSEric Anholt spin_unlock(&file_priv->table_lock); 7601584f16cSEric Anholt 761dffa9b7aSEric Anholt ret = v3d_lock_bo_reservations(&job->base, &acquire_ctx); 7621584f16cSEric Anholt if (ret) 7631584f16cSEric Anholt goto fail; 7641584f16cSEric Anholt 7651584f16cSEric Anholt mutex_lock(&v3d->sched_lock); 766a783a09eSEric Anholt ret = v3d_push_job(v3d_priv, &job->base, V3D_TFU); 7671584f16cSEric Anholt if (ret) 7681584f16cSEric Anholt goto fail_unreserve; 7691584f16cSEric Anholt mutex_unlock(&v3d->sched_lock); 7701584f16cSEric Anholt 771a783a09eSEric Anholt v3d_attach_fences_and_unlock_reservation(file_priv, 772a783a09eSEric Anholt &job->base, &acquire_ctx, 773d223f98fSEric Anholt args->out_sync, 774d223f98fSEric Anholt job->base.done_fence); 7751584f16cSEric Anholt 776a783a09eSEric Anholt v3d_job_put(&job->base); 7771584f16cSEric Anholt 7781584f16cSEric Anholt return 0; 7791584f16cSEric Anholt 7801584f16cSEric Anholt fail_unreserve: 7811584f16cSEric Anholt mutex_unlock(&v3d->sched_lock); 782a783a09eSEric Anholt drm_gem_unlock_reservations(job->base.bo, job->base.bo_count, 783a783a09eSEric Anholt &acquire_ctx); 7841584f16cSEric Anholt fail: 785a783a09eSEric Anholt v3d_job_put(&job->base); 7861584f16cSEric Anholt 7871584f16cSEric Anholt return ret; 7881584f16cSEric Anholt } 7891584f16cSEric Anholt 790d223f98fSEric Anholt /** 791d223f98fSEric Anholt * v3d_submit_csd_ioctl() - Submits a CSD (texture formatting) job to the V3D. 792d223f98fSEric Anholt * @dev: DRM device 793d223f98fSEric Anholt * @data: ioctl argument 794d223f98fSEric Anholt * @file_priv: DRM file for this fd 795d223f98fSEric Anholt * 796d223f98fSEric Anholt * Userspace provides the register setup for the CSD, which we don't 797d223f98fSEric Anholt * need to validate since the CSD is behind the MMU. 798d223f98fSEric Anholt */ 799d223f98fSEric Anholt int 800d223f98fSEric Anholt v3d_submit_csd_ioctl(struct drm_device *dev, void *data, 801d223f98fSEric Anholt struct drm_file *file_priv) 802d223f98fSEric Anholt { 803d223f98fSEric Anholt struct v3d_dev *v3d = to_v3d_dev(dev); 804d223f98fSEric Anholt struct v3d_file_priv *v3d_priv = file_priv->driver_priv; 805d223f98fSEric Anholt struct drm_v3d_submit_csd *args = data; 806d223f98fSEric Anholt struct v3d_csd_job *job; 807d223f98fSEric Anholt struct v3d_job *clean_job; 808d223f98fSEric Anholt struct ww_acquire_ctx acquire_ctx; 809d223f98fSEric Anholt int ret; 810d223f98fSEric Anholt 811d223f98fSEric Anholt trace_v3d_submit_csd_ioctl(&v3d->drm, args->cfg[5], args->cfg[6]); 812d223f98fSEric Anholt 813d223f98fSEric Anholt if (!v3d_has_csd(v3d)) { 814d223f98fSEric Anholt DRM_DEBUG("Attempting CSD submit on non-CSD hardware\n"); 815d223f98fSEric Anholt return -EINVAL; 816d223f98fSEric Anholt } 817d223f98fSEric Anholt 818d223f98fSEric Anholt job = kcalloc(1, sizeof(*job), GFP_KERNEL); 819d223f98fSEric Anholt if (!job) 820d223f98fSEric Anholt return -ENOMEM; 821d223f98fSEric Anholt 822d223f98fSEric Anholt ret = v3d_job_init(v3d, file_priv, &job->base, 823d223f98fSEric Anholt v3d_job_free, args->in_sync); 824d223f98fSEric Anholt if (ret) { 825d223f98fSEric Anholt kfree(job); 826d223f98fSEric Anholt return ret; 827d223f98fSEric Anholt } 828d223f98fSEric Anholt 829d223f98fSEric Anholt clean_job = kcalloc(1, sizeof(*clean_job), GFP_KERNEL); 830d223f98fSEric Anholt if (!clean_job) { 831d223f98fSEric Anholt v3d_job_put(&job->base); 832d223f98fSEric Anholt kfree(job); 833d223f98fSEric Anholt return -ENOMEM; 834d223f98fSEric Anholt } 835d223f98fSEric Anholt 836d223f98fSEric Anholt ret = v3d_job_init(v3d, file_priv, clean_job, v3d_job_free, 0); 837d223f98fSEric Anholt if (ret) { 838d223f98fSEric Anholt v3d_job_put(&job->base); 839d223f98fSEric Anholt kfree(clean_job); 840d223f98fSEric Anholt return ret; 841d223f98fSEric Anholt } 842d223f98fSEric Anholt 843d223f98fSEric Anholt job->args = *args; 844d223f98fSEric Anholt 845d223f98fSEric Anholt ret = v3d_lookup_bos(dev, file_priv, clean_job, 846d223f98fSEric Anholt args->bo_handles, args->bo_handle_count); 847d223f98fSEric Anholt if (ret) 848d223f98fSEric Anholt goto fail; 849d223f98fSEric Anholt 850dffa9b7aSEric Anholt ret = v3d_lock_bo_reservations(clean_job, &acquire_ctx); 851d223f98fSEric Anholt if (ret) 852d223f98fSEric Anholt goto fail; 853d223f98fSEric Anholt 85426a4dc29SJuan A. Suarez Romero if (args->perfmon_id) { 85526a4dc29SJuan A. Suarez Romero job->base.perfmon = v3d_perfmon_find(v3d_priv, 85626a4dc29SJuan A. Suarez Romero args->perfmon_id); 85726a4dc29SJuan A. Suarez Romero if (!job->base.perfmon) { 85826a4dc29SJuan A. Suarez Romero ret = -ENOENT; 85926a4dc29SJuan A. Suarez Romero goto fail; 86026a4dc29SJuan A. Suarez Romero } 86126a4dc29SJuan A. Suarez Romero } 86226a4dc29SJuan A. Suarez Romero 863d223f98fSEric Anholt mutex_lock(&v3d->sched_lock); 864d223f98fSEric Anholt ret = v3d_push_job(v3d_priv, &job->base, V3D_CSD); 865d223f98fSEric Anholt if (ret) 866d223f98fSEric Anholt goto fail_unreserve; 867d223f98fSEric Anholt 868dffa9b7aSEric Anholt ret = drm_gem_fence_array_add(&clean_job->deps, 869dffa9b7aSEric Anholt dma_fence_get(job->base.done_fence)); 870dffa9b7aSEric Anholt if (ret) 871dffa9b7aSEric Anholt goto fail_unreserve; 872dffa9b7aSEric Anholt 873d223f98fSEric Anholt ret = v3d_push_job(v3d_priv, clean_job, V3D_CACHE_CLEAN); 874d223f98fSEric Anholt if (ret) 875d223f98fSEric Anholt goto fail_unreserve; 876d223f98fSEric Anholt mutex_unlock(&v3d->sched_lock); 877d223f98fSEric Anholt 878d223f98fSEric Anholt v3d_attach_fences_and_unlock_reservation(file_priv, 879d223f98fSEric Anholt clean_job, 880d223f98fSEric Anholt &acquire_ctx, 881d223f98fSEric Anholt args->out_sync, 882d223f98fSEric Anholt clean_job->done_fence); 883d223f98fSEric Anholt 884d223f98fSEric Anholt v3d_job_put(&job->base); 885d223f98fSEric Anholt v3d_job_put(clean_job); 886d223f98fSEric Anholt 887d223f98fSEric Anholt return 0; 888d223f98fSEric Anholt 889d223f98fSEric Anholt fail_unreserve: 890d223f98fSEric Anholt mutex_unlock(&v3d->sched_lock); 891d223f98fSEric Anholt drm_gem_unlock_reservations(clean_job->bo, clean_job->bo_count, 892d223f98fSEric Anholt &acquire_ctx); 893d223f98fSEric Anholt fail: 894d223f98fSEric Anholt v3d_job_put(&job->base); 895d223f98fSEric Anholt v3d_job_put(clean_job); 896d223f98fSEric Anholt 897d223f98fSEric Anholt return ret; 898d223f98fSEric Anholt } 899d223f98fSEric Anholt 90057692c94SEric Anholt int 90157692c94SEric Anholt v3d_gem_init(struct drm_device *dev) 90257692c94SEric Anholt { 90357692c94SEric Anholt struct v3d_dev *v3d = to_v3d_dev(dev); 90457692c94SEric Anholt u32 pt_size = 4096 * 1024; 90557692c94SEric Anholt int ret, i; 90657692c94SEric Anholt 90757692c94SEric Anholt for (i = 0; i < V3D_MAX_QUEUES; i++) 90857692c94SEric Anholt v3d->queue[i].fence_context = dma_fence_context_alloc(1); 90957692c94SEric Anholt 91057692c94SEric Anholt spin_lock_init(&v3d->mm_lock); 91157692c94SEric Anholt spin_lock_init(&v3d->job_lock); 91257692c94SEric Anholt mutex_init(&v3d->bo_lock); 91357692c94SEric Anholt mutex_init(&v3d->reset_lock); 9147122b68bSEric Anholt mutex_init(&v3d->sched_lock); 915d223f98fSEric Anholt mutex_init(&v3d->cache_clean_lock); 91657692c94SEric Anholt 91757692c94SEric Anholt /* Note: We don't allocate address 0. Various bits of HW 91857692c94SEric Anholt * treat 0 as special, such as the occlusion query counters 91957692c94SEric Anholt * where 0 means "disabled". 92057692c94SEric Anholt */ 92157692c94SEric Anholt drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1); 92257692c94SEric Anholt 923bc662528SDaniel Vetter v3d->pt = dma_alloc_wc(v3d->drm.dev, pt_size, 92457692c94SEric Anholt &v3d->pt_paddr, 92557692c94SEric Anholt GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO); 92657692c94SEric Anholt if (!v3d->pt) { 92757692c94SEric Anholt drm_mm_takedown(&v3d->mm); 928bc662528SDaniel Vetter dev_err(v3d->drm.dev, 92957692c94SEric Anholt "Failed to allocate page tables. " 93057692c94SEric Anholt "Please ensure you have CMA enabled.\n"); 93157692c94SEric Anholt return -ENOMEM; 93257692c94SEric Anholt } 93357692c94SEric Anholt 93457692c94SEric Anholt v3d_init_hw_state(v3d); 93557692c94SEric Anholt v3d_mmu_set_page_table(v3d); 93657692c94SEric Anholt 93757692c94SEric Anholt ret = v3d_sched_init(v3d); 93857692c94SEric Anholt if (ret) { 93957692c94SEric Anholt drm_mm_takedown(&v3d->mm); 940bc662528SDaniel Vetter dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt, 94157692c94SEric Anholt v3d->pt_paddr); 94257692c94SEric Anholt } 94357692c94SEric Anholt 94457692c94SEric Anholt return 0; 94557692c94SEric Anholt } 94657692c94SEric Anholt 94757692c94SEric Anholt void 94857692c94SEric Anholt v3d_gem_destroy(struct drm_device *dev) 94957692c94SEric Anholt { 95057692c94SEric Anholt struct v3d_dev *v3d = to_v3d_dev(dev); 95157692c94SEric Anholt 95257692c94SEric Anholt v3d_sched_fini(v3d); 95357692c94SEric Anholt 954a783a09eSEric Anholt /* Waiting for jobs to finish would need to be done before 95557692c94SEric Anholt * unregistering V3D. 95657692c94SEric Anholt */ 95714d1d190SEric Anholt WARN_ON(v3d->bin_job); 95814d1d190SEric Anholt WARN_ON(v3d->render_job); 95957692c94SEric Anholt 96057692c94SEric Anholt drm_mm_takedown(&v3d->mm); 96157692c94SEric Anholt 962bc662528SDaniel Vetter dma_free_coherent(v3d->drm.dev, 4096 * 1024, (void *)v3d->pt, 963bc662528SDaniel Vetter v3d->pt_paddr); 96457692c94SEric Anholt } 965