Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* Copyright (C) 2014-2018 Broadcom */ |
| 3 | |
| 4 | #include <drm/drmP.h> |
| 5 | #include <drm/drm_syncobj.h> |
| 6 | #include <linux/module.h> |
| 7 | #include <linux/platform_device.h> |
| 8 | #include <linux/pm_runtime.h> |
Eric Anholt | eea9b97 | 2019-03-08 09:43:36 -0800 | [diff] [blame] | 9 | #include <linux/reset.h> |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 10 | #include <linux/device.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/sched/signal.h> |
| 13 | |
| 14 | #include "uapi/drm/v3d_drm.h" |
| 15 | #include "v3d_drv.h" |
| 16 | #include "v3d_regs.h" |
| 17 | #include "v3d_trace.h" |
| 18 | |
| 19 | static void |
| 20 | v3d_init_core(struct v3d_dev *v3d, int core) |
| 21 | { |
| 22 | /* Set OVRTMUOUT, which means that the texture sampler uniform |
| 23 | * configuration's tmu output type field is used, instead of |
| 24 | * using the hardware default behavior based on the texture |
| 25 | * type. If you want the default behavior, you can still put |
| 26 | * "2" in the indirect texture state's output_type field. |
| 27 | */ |
Eric Anholt | a7dde1b | 2019-02-20 15:36:57 -0800 | [diff] [blame] | 28 | if (v3d->ver < 40) |
| 29 | V3D_CORE_WRITE(core, V3D_CTL_MISCCFG, V3D_MISCCFG_OVRTMUOUT); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 30 | |
| 31 | /* Whenever we flush the L2T cache, we always want to flush |
| 32 | * the whole thing. |
| 33 | */ |
| 34 | V3D_CORE_WRITE(core, V3D_CTL_L2TFLSTA, 0); |
| 35 | V3D_CORE_WRITE(core, V3D_CTL_L2TFLEND, ~0); |
| 36 | } |
| 37 | |
| 38 | /* Sets invariant state for the HW. */ |
| 39 | static void |
| 40 | v3d_init_hw_state(struct v3d_dev *v3d) |
| 41 | { |
| 42 | v3d_init_core(v3d, 0); |
| 43 | } |
| 44 | |
| 45 | static void |
| 46 | v3d_idle_axi(struct v3d_dev *v3d, int core) |
| 47 | { |
| 48 | V3D_CORE_WRITE(core, V3D_GMP_CFG, V3D_GMP_CFG_STOP_REQ); |
| 49 | |
| 50 | if (wait_for((V3D_CORE_READ(core, V3D_GMP_STATUS) & |
| 51 | (V3D_GMP_STATUS_RD_COUNT_MASK | |
| 52 | V3D_GMP_STATUS_WR_COUNT_MASK | |
| 53 | V3D_GMP_STATUS_CFG_BUSY)) == 0, 100)) { |
| 54 | DRM_ERROR("Failed to wait for safe GMP shutdown\n"); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | static void |
| 59 | v3d_idle_gca(struct v3d_dev *v3d) |
| 60 | { |
| 61 | if (v3d->ver >= 41) |
| 62 | return; |
| 63 | |
| 64 | V3D_GCA_WRITE(V3D_GCA_SAFE_SHUTDOWN, V3D_GCA_SAFE_SHUTDOWN_EN); |
| 65 | |
| 66 | if (wait_for((V3D_GCA_READ(V3D_GCA_SAFE_SHUTDOWN_ACK) & |
| 67 | V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED) == |
| 68 | V3D_GCA_SAFE_SHUTDOWN_ACK_ACKED, 100)) { |
| 69 | DRM_ERROR("Failed to wait for safe GCA shutdown\n"); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static void |
Eric Anholt | eea9b97 | 2019-03-08 09:43:36 -0800 | [diff] [blame] | 74 | v3d_reset_by_bridge(struct v3d_dev *v3d) |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 75 | { |
| 76 | int version = V3D_BRIDGE_READ(V3D_TOP_GR_BRIDGE_REVISION); |
| 77 | |
| 78 | if (V3D_GET_FIELD(version, V3D_TOP_GR_BRIDGE_MAJOR) == 2) { |
| 79 | V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, |
| 80 | V3D_TOP_GR_BRIDGE_SW_INIT_0_V3D_CLK_108_SW_INIT); |
| 81 | V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_0, 0); |
| 82 | |
| 83 | /* GFXH-1383: The SW_INIT may cause a stray write to address 0 |
| 84 | * of the unit, so reset it to its power-on value here. |
| 85 | */ |
| 86 | V3D_WRITE(V3D_HUB_AXICFG, V3D_HUB_AXICFG_MAX_LEN_MASK); |
| 87 | } else { |
| 88 | WARN_ON_ONCE(V3D_GET_FIELD(version, |
| 89 | V3D_TOP_GR_BRIDGE_MAJOR) != 7); |
| 90 | V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, |
| 91 | V3D_TOP_GR_BRIDGE_SW_INIT_1_V3D_CLK_108_SW_INIT); |
| 92 | V3D_BRIDGE_WRITE(V3D_TOP_GR_BRIDGE_SW_INIT_1, 0); |
| 93 | } |
Eric Anholt | eea9b97 | 2019-03-08 09:43:36 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static void |
| 97 | v3d_reset_v3d(struct v3d_dev *v3d) |
| 98 | { |
| 99 | if (v3d->reset) |
| 100 | reset_control_reset(v3d->reset); |
| 101 | else |
| 102 | v3d_reset_by_bridge(v3d); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 103 | |
| 104 | v3d_init_hw_state(v3d); |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | v3d_reset(struct v3d_dev *v3d) |
| 109 | { |
| 110 | struct drm_device *dev = &v3d->drm; |
| 111 | |
| 112 | DRM_ERROR("Resetting GPU.\n"); |
| 113 | trace_v3d_reset_begin(dev); |
| 114 | |
| 115 | /* XXX: only needed for safe powerdown, not reset. */ |
| 116 | if (false) |
| 117 | v3d_idle_axi(v3d, 0); |
| 118 | |
| 119 | v3d_idle_gca(v3d); |
| 120 | v3d_reset_v3d(v3d); |
| 121 | |
| 122 | v3d_mmu_set_page_table(v3d); |
| 123 | v3d_irq_reset(v3d); |
| 124 | |
| 125 | trace_v3d_reset_end(dev); |
| 126 | } |
| 127 | |
| 128 | static void |
| 129 | v3d_flush_l3(struct v3d_dev *v3d) |
| 130 | { |
| 131 | if (v3d->ver < 41) { |
| 132 | u32 gca_ctrl = V3D_GCA_READ(V3D_GCA_CACHE_CTRL); |
| 133 | |
| 134 | V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL, |
| 135 | gca_ctrl | V3D_GCA_CACHE_CTRL_FLUSH); |
| 136 | |
| 137 | if (v3d->ver < 33) { |
| 138 | V3D_GCA_WRITE(V3D_GCA_CACHE_CTRL, |
| 139 | gca_ctrl & ~V3D_GCA_CACHE_CTRL_FLUSH); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Eric Anholt | 7b9d2fe | 2018-12-03 14:24:37 -0800 | [diff] [blame] | 144 | /* Invalidates the (read-only) L2C cache. This was the L2 cache for |
| 145 | * uniforms and instructions on V3D 3.2. |
| 146 | */ |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 147 | static void |
Eric Anholt | 7b9d2fe | 2018-12-03 14:24:37 -0800 | [diff] [blame] | 148 | v3d_invalidate_l2c(struct v3d_dev *v3d, int core) |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 149 | { |
Eric Anholt | 7b9d2fe | 2018-12-03 14:24:37 -0800 | [diff] [blame] | 150 | if (v3d->ver > 32) |
| 151 | return; |
| 152 | |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 153 | V3D_CORE_WRITE(core, V3D_CTL_L2CACTL, |
| 154 | V3D_L2CACTL_L2CCLR | |
| 155 | V3D_L2CACTL_L2CENA); |
| 156 | } |
| 157 | |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 158 | /* Invalidates texture L2 cachelines */ |
| 159 | static void |
| 160 | v3d_flush_l2t(struct v3d_dev *v3d, int core) |
| 161 | { |
Eric Anholt | 51c1b6f | 2018-12-03 14:24:36 -0800 | [diff] [blame] | 162 | /* While there is a busy bit (V3D_L2TCACTL_L2TFLS), we don't |
| 163 | * need to wait for completion before dispatching the job -- |
| 164 | * L2T accesses will be stalled until the flush has completed. |
| 165 | */ |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 166 | V3D_CORE_WRITE(core, V3D_CTL_L2TCACTL, |
| 167 | V3D_L2TCACTL_L2TFLS | |
| 168 | V3D_SET_FIELD(V3D_L2TCACTL_FLM_FLUSH, V3D_L2TCACTL_FLM)); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* Invalidates the slice caches. These are read-only caches. */ |
| 172 | static void |
| 173 | v3d_invalidate_slices(struct v3d_dev *v3d, int core) |
| 174 | { |
| 175 | V3D_CORE_WRITE(core, V3D_CTL_SLCACTL, |
| 176 | V3D_SET_FIELD(0xf, V3D_SLCACTL_TVCCS) | |
| 177 | V3D_SET_FIELD(0xf, V3D_SLCACTL_TDCCS) | |
| 178 | V3D_SET_FIELD(0xf, V3D_SLCACTL_UCC) | |
| 179 | V3D_SET_FIELD(0xf, V3D_SLCACTL_ICC)); |
| 180 | } |
| 181 | |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 182 | void |
| 183 | v3d_invalidate_caches(struct v3d_dev *v3d) |
| 184 | { |
Eric Anholt | aa5beec | 2018-12-03 14:24:38 -0800 | [diff] [blame] | 185 | /* Invalidate the caches from the outside in. That way if |
| 186 | * another CL's concurrent use of nearby memory were to pull |
| 187 | * an invalidated cacheline back in, we wouldn't leave stale |
| 188 | * data in the inner cache. |
| 189 | */ |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 190 | v3d_flush_l3(v3d); |
Eric Anholt | 7b9d2fe | 2018-12-03 14:24:37 -0800 | [diff] [blame] | 191 | v3d_invalidate_l2c(v3d, 0); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 192 | v3d_flush_l2t(v3d, 0); |
Eric Anholt | aa5beec | 2018-12-03 14:24:38 -0800 | [diff] [blame] | 193 | v3d_invalidate_slices(v3d, 0); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 196 | /* Takes the reservation lock on all the BOs being referenced, so that |
| 197 | * at queue submit time we can update the reservations. |
| 198 | * |
| 199 | * We don't lock the RCL the tile alloc/state BOs, or overflow memory |
| 200 | * (all of which are on exec->unref_list). They're entirely private |
| 201 | * to v3d, so we don't attach dma-buf fences to them. |
| 202 | */ |
| 203 | static int |
Eric Anholt | d4c3022 | 2019-04-16 15:58:52 -0700 | [diff] [blame] | 204 | v3d_lock_bo_reservations(struct drm_gem_object **bos, |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 205 | int bo_count, |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 206 | struct ww_acquire_ctx *acquire_ctx) |
| 207 | { |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 208 | int i, ret; |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 209 | |
Eric Anholt | d4c3022 | 2019-04-16 15:58:52 -0700 | [diff] [blame] | 210 | ret = drm_gem_lock_reservations(bos, bo_count, acquire_ctx); |
Eric Anholt | c2b3e61 | 2019-03-08 08:17:14 -0800 | [diff] [blame] | 211 | if (ret) |
| 212 | return ret; |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 213 | |
| 214 | /* Reserve space for our shared (read-only) fence references, |
| 215 | * before we commit the CL to the hardware. |
| 216 | */ |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 217 | for (i = 0; i < bo_count; i++) { |
Eric Anholt | d4c3022 | 2019-04-16 15:58:52 -0700 | [diff] [blame] | 218 | ret = reservation_object_reserve_shared(bos[i]->resv, 1); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 219 | if (ret) { |
Eric Anholt | d4c3022 | 2019-04-16 15:58:52 -0700 | [diff] [blame] | 220 | drm_gem_unlock_reservations(bos, bo_count, |
| 221 | acquire_ctx); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 222 | return ret; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /** |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 230 | * v3d_lookup_bos() - Sets up job->bo[] with the GEM objects |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 231 | * referenced by the job. |
| 232 | * @dev: DRM device |
| 233 | * @file_priv: DRM file for this fd |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 234 | * @job: V3D job being set up |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 235 | * |
| 236 | * The command validator needs to reference BOs by their index within |
| 237 | * the submitted job's BO list. This does the validation of the job's |
| 238 | * BO list and reference counting for the lifetime of the job. |
| 239 | * |
| 240 | * Note that this function doesn't need to unreference the BOs on |
| 241 | * failure, because that will happen at v3d_exec_cleanup() time. |
| 242 | */ |
| 243 | static int |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 244 | v3d_lookup_bos(struct drm_device *dev, |
| 245 | struct drm_file *file_priv, |
| 246 | struct v3d_job *job, |
| 247 | u64 bo_handles, |
| 248 | u32 bo_count) |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 249 | { |
| 250 | u32 *handles; |
| 251 | int ret = 0; |
| 252 | int i; |
| 253 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 254 | job->bo_count = bo_count; |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 255 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 256 | if (!job->bo_count) { |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 257 | /* See comment on bo_index for why we have to check |
| 258 | * this. |
| 259 | */ |
| 260 | DRM_DEBUG("Rendering requires BOs\n"); |
| 261 | return -EINVAL; |
| 262 | } |
| 263 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 264 | job->bo = kvmalloc_array(job->bo_count, |
| 265 | sizeof(struct drm_gem_cma_object *), |
| 266 | GFP_KERNEL | __GFP_ZERO); |
| 267 | if (!job->bo) { |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 268 | DRM_DEBUG("Failed to allocate validated BO pointers\n"); |
| 269 | return -ENOMEM; |
| 270 | } |
| 271 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 272 | handles = kvmalloc_array(job->bo_count, sizeof(u32), GFP_KERNEL); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 273 | if (!handles) { |
| 274 | ret = -ENOMEM; |
| 275 | DRM_DEBUG("Failed to allocate incoming GEM handles\n"); |
| 276 | goto fail; |
| 277 | } |
| 278 | |
| 279 | if (copy_from_user(handles, |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 280 | (void __user *)(uintptr_t)bo_handles, |
| 281 | job->bo_count * sizeof(u32))) { |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 282 | ret = -EFAULT; |
| 283 | DRM_DEBUG("Failed to copy in GEM handles\n"); |
| 284 | goto fail; |
| 285 | } |
| 286 | |
| 287 | spin_lock(&file_priv->table_lock); |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 288 | for (i = 0; i < job->bo_count; i++) { |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 289 | struct drm_gem_object *bo = idr_find(&file_priv->object_idr, |
| 290 | handles[i]); |
| 291 | if (!bo) { |
| 292 | DRM_DEBUG("Failed to look up GEM BO %d: %d\n", |
| 293 | i, handles[i]); |
| 294 | ret = -ENOENT; |
| 295 | spin_unlock(&file_priv->table_lock); |
| 296 | goto fail; |
| 297 | } |
| 298 | drm_gem_object_get(bo); |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 299 | job->bo[i] = bo; |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 300 | } |
| 301 | spin_unlock(&file_priv->table_lock); |
| 302 | |
| 303 | fail: |
| 304 | kvfree(handles); |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | static void |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 309 | v3d_job_free(struct kref *ref) |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 310 | { |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 311 | struct v3d_job *job = container_of(ref, struct v3d_job, refcount); |
| 312 | int i; |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 313 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 314 | for (i = 0; i < job->bo_count; i++) { |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 315 | if (job->bo[i]) |
Eric Anholt | d4c3022 | 2019-04-16 15:58:52 -0700 | [diff] [blame] | 316 | drm_gem_object_put_unlocked(job->bo[i]); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 317 | } |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 318 | kvfree(job->bo); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 319 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 320 | dma_fence_put(job->in_fence); |
| 321 | dma_fence_put(job->irq_fence); |
| 322 | dma_fence_put(job->done_fence); |
| 323 | |
| 324 | pm_runtime_mark_last_busy(job->v3d->dev); |
| 325 | pm_runtime_put_autosuspend(job->v3d->dev); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 326 | |
| 327 | kfree(job); |
| 328 | } |
| 329 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 330 | static void |
| 331 | v3d_render_job_free(struct kref *ref) |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 332 | { |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 333 | struct v3d_render_job *job = container_of(ref, struct v3d_render_job, |
| 334 | base.refcount); |
| 335 | struct v3d_bo *bo, *save; |
| 336 | |
| 337 | list_for_each_entry_safe(bo, save, &job->unref_list, unref_head) { |
| 338 | drm_gem_object_put_unlocked(&bo->base.base); |
| 339 | } |
| 340 | |
| 341 | v3d_job_free(ref); |
| 342 | } |
| 343 | |
| 344 | void v3d_job_put(struct v3d_job *job) |
| 345 | { |
| 346 | kref_put(&job->refcount, job->free); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 349 | int |
| 350 | v3d_wait_bo_ioctl(struct drm_device *dev, void *data, |
| 351 | struct drm_file *file_priv) |
| 352 | { |
| 353 | int ret; |
| 354 | struct drm_v3d_wait_bo *args = data; |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 355 | ktime_t start = ktime_get(); |
| 356 | u64 delta_ns; |
| 357 | unsigned long timeout_jiffies = |
| 358 | nsecs_to_jiffies_timeout(args->timeout_ns); |
| 359 | |
| 360 | if (args->pad != 0) |
| 361 | return -EINVAL; |
| 362 | |
Rob Herring | 8d66830 | 2019-02-02 09:41:57 -0600 | [diff] [blame] | 363 | ret = drm_gem_reservation_object_wait(file_priv, args->handle, |
| 364 | true, timeout_jiffies); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 365 | |
| 366 | /* Decrement the user's timeout, in case we got interrupted |
| 367 | * such that the ioctl will be restarted. |
| 368 | */ |
| 369 | delta_ns = ktime_to_ns(ktime_sub(ktime_get(), start)); |
| 370 | if (delta_ns < args->timeout_ns) |
| 371 | args->timeout_ns -= delta_ns; |
| 372 | else |
| 373 | args->timeout_ns = 0; |
| 374 | |
| 375 | /* Asked to wait beyond the jiffie/scheduler precision? */ |
| 376 | if (ret == -ETIME && args->timeout_ns) |
| 377 | ret = -EAGAIN; |
| 378 | |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 379 | return ret; |
| 380 | } |
| 381 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 382 | static int |
| 383 | v3d_job_init(struct v3d_dev *v3d, struct drm_file *file_priv, |
| 384 | struct v3d_job *job, void (*free)(struct kref *ref), |
| 385 | u32 in_sync) |
| 386 | { |
| 387 | int ret; |
| 388 | |
| 389 | job->v3d = v3d; |
| 390 | job->free = free; |
| 391 | |
| 392 | ret = pm_runtime_get_sync(v3d->dev); |
| 393 | if (ret < 0) |
| 394 | return ret; |
| 395 | |
| 396 | ret = drm_syncobj_find_fence(file_priv, in_sync, 0, 0, &job->in_fence); |
| 397 | if (ret == -EINVAL) { |
| 398 | pm_runtime_put_autosuspend(v3d->dev); |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | kref_init(&job->refcount); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static int |
| 408 | v3d_push_job(struct v3d_file_priv *v3d_priv, |
| 409 | struct v3d_job *job, enum v3d_queue queue) |
| 410 | { |
| 411 | int ret; |
| 412 | |
| 413 | ret = drm_sched_job_init(&job->base, &v3d_priv->sched_entity[queue], |
| 414 | v3d_priv); |
| 415 | if (ret) |
| 416 | return ret; |
| 417 | |
| 418 | job->done_fence = dma_fence_get(&job->base.s_fence->finished); |
| 419 | |
| 420 | /* put by scheduler job completion */ |
| 421 | kref_get(&job->refcount); |
| 422 | |
| 423 | drm_sched_entity_push_job(&job->base, &v3d_priv->sched_entity[queue]); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | static void |
| 429 | v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv, |
| 430 | struct v3d_job *job, |
| 431 | struct ww_acquire_ctx *acquire_ctx, |
| 432 | u32 out_sync) |
| 433 | { |
| 434 | struct drm_syncobj *sync_out; |
| 435 | int i; |
| 436 | |
| 437 | for (i = 0; i < job->bo_count; i++) { |
| 438 | /* XXX: Use shared fences for read-only objects. */ |
| 439 | reservation_object_add_excl_fence(job->bo[i]->resv, |
| 440 | job->done_fence); |
| 441 | } |
| 442 | |
| 443 | drm_gem_unlock_reservations(job->bo, job->bo_count, acquire_ctx); |
| 444 | |
| 445 | /* Update the return sync object for the job */ |
| 446 | sync_out = drm_syncobj_find(file_priv, out_sync); |
| 447 | if (sync_out) { |
| 448 | drm_syncobj_replace_fence(sync_out, job->done_fence); |
| 449 | drm_syncobj_put(sync_out); |
| 450 | } |
| 451 | } |
| 452 | |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 453 | /** |
| 454 | * v3d_submit_cl_ioctl() - Submits a job (frame) to the V3D. |
| 455 | * @dev: DRM device |
| 456 | * @data: ioctl argument |
| 457 | * @file_priv: DRM file for this fd |
| 458 | * |
| 459 | * This is the main entrypoint for userspace to submit a 3D frame to |
| 460 | * the GPU. Userspace provides the binner command list (if |
| 461 | * applicable), and the kernel sets up the render command list to draw |
| 462 | * to the framebuffer described in the ioctl, using the command lists |
| 463 | * that the 3D engine's binner will produce. |
| 464 | */ |
| 465 | int |
| 466 | v3d_submit_cl_ioctl(struct drm_device *dev, void *data, |
| 467 | struct drm_file *file_priv) |
| 468 | { |
| 469 | struct v3d_dev *v3d = to_v3d_dev(dev); |
| 470 | struct v3d_file_priv *v3d_priv = file_priv->driver_priv; |
| 471 | struct drm_v3d_submit_cl *args = data; |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 472 | struct v3d_bin_job *bin = NULL; |
| 473 | struct v3d_render_job *render; |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 474 | struct ww_acquire_ctx acquire_ctx; |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 475 | int ret = 0; |
| 476 | |
Eric Anholt | 55a9b74 | 2018-11-30 16:57:58 -0800 | [diff] [blame] | 477 | trace_v3d_submit_cl_ioctl(&v3d->drm, args->rcl_start, args->rcl_end); |
| 478 | |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 479 | if (args->pad != 0) { |
| 480 | DRM_INFO("pad must be zero: %d\n", args->pad); |
| 481 | return -EINVAL; |
| 482 | } |
| 483 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 484 | render = kcalloc(1, sizeof(*render), GFP_KERNEL); |
| 485 | if (!render) |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 486 | return -ENOMEM; |
| 487 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 488 | render->start = args->rcl_start; |
| 489 | render->end = args->rcl_end; |
| 490 | INIT_LIST_HEAD(&render->unref_list); |
| 491 | |
| 492 | ret = v3d_job_init(v3d, file_priv, &render->base, |
| 493 | v3d_render_job_free, args->in_sync_rcl); |
| 494 | if (ret) { |
| 495 | kfree(render); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 496 | return ret; |
| 497 | } |
| 498 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 499 | if (args->bcl_start != args->bcl_end) { |
| 500 | bin = kcalloc(1, sizeof(*bin), GFP_KERNEL); |
| 501 | if (!bin) |
| 502 | return -ENOMEM; |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 503 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 504 | ret = v3d_job_init(v3d, file_priv, &bin->base, |
| 505 | v3d_job_free, args->in_sync_bcl); |
| 506 | if (ret) { |
| 507 | v3d_job_put(&render->base); |
| 508 | return ret; |
| 509 | } |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 510 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 511 | bin->start = args->bcl_start; |
| 512 | bin->end = args->bcl_end; |
| 513 | bin->qma = args->qma; |
| 514 | bin->qms = args->qms; |
| 515 | bin->qts = args->qts; |
| 516 | bin->render = render; |
| 517 | } |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 518 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 519 | ret = v3d_lookup_bos(dev, file_priv, &render->base, |
| 520 | args->bo_handles, args->bo_handle_count); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 521 | if (ret) |
| 522 | goto fail; |
| 523 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 524 | ret = v3d_lock_bo_reservations(render->base.bo, render->base.bo_count, |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 525 | &acquire_ctx); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 526 | if (ret) |
| 527 | goto fail; |
| 528 | |
Eric Anholt | 7122b68 | 2018-06-06 10:48:51 -0700 | [diff] [blame] | 529 | mutex_lock(&v3d->sched_lock); |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 530 | if (bin) { |
| 531 | ret = v3d_push_job(v3d_priv, &bin->base, V3D_BIN); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 532 | if (ret) |
| 533 | goto fail_unreserve; |
| 534 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 535 | render->bin_done_fence = dma_fence_get(bin->base.done_fence); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 538 | ret = v3d_push_job(v3d_priv, &render->base, V3D_RENDER); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 539 | if (ret) |
| 540 | goto fail_unreserve; |
Eric Anholt | 7122b68 | 2018-06-06 10:48:51 -0700 | [diff] [blame] | 541 | mutex_unlock(&v3d->sched_lock); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 542 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 543 | v3d_attach_fences_and_unlock_reservation(file_priv, |
| 544 | &render->base, &acquire_ctx, |
| 545 | args->out_sync); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 546 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 547 | if (bin) |
| 548 | v3d_job_put(&bin->base); |
| 549 | v3d_job_put(&render->base); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 550 | |
| 551 | return 0; |
| 552 | |
| 553 | fail_unreserve: |
Eric Anholt | 7122b68 | 2018-06-06 10:48:51 -0700 | [diff] [blame] | 554 | mutex_unlock(&v3d->sched_lock); |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 555 | drm_gem_unlock_reservations(render->base.bo, |
| 556 | render->base.bo_count, &acquire_ctx); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 557 | fail: |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 558 | if (bin) |
| 559 | v3d_job_put(&bin->base); |
| 560 | v3d_job_put(&render->base); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 561 | |
| 562 | return ret; |
| 563 | } |
| 564 | |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 565 | /** |
| 566 | * v3d_submit_tfu_ioctl() - Submits a TFU (texture formatting) job to the V3D. |
| 567 | * @dev: DRM device |
| 568 | * @data: ioctl argument |
| 569 | * @file_priv: DRM file for this fd |
| 570 | * |
| 571 | * Userspace provides the register setup for the TFU, which we don't |
| 572 | * need to validate since the TFU is behind the MMU. |
| 573 | */ |
| 574 | int |
| 575 | v3d_submit_tfu_ioctl(struct drm_device *dev, void *data, |
| 576 | struct drm_file *file_priv) |
| 577 | { |
| 578 | struct v3d_dev *v3d = to_v3d_dev(dev); |
| 579 | struct v3d_file_priv *v3d_priv = file_priv->driver_priv; |
| 580 | struct drm_v3d_submit_tfu *args = data; |
| 581 | struct v3d_tfu_job *job; |
| 582 | struct ww_acquire_ctx acquire_ctx; |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 583 | int ret = 0; |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 584 | |
Eric Anholt | 55a9b74 | 2018-11-30 16:57:58 -0800 | [diff] [blame] | 585 | trace_v3d_submit_tfu_ioctl(&v3d->drm, args->iia); |
| 586 | |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 587 | job = kcalloc(1, sizeof(*job), GFP_KERNEL); |
| 588 | if (!job) |
| 589 | return -ENOMEM; |
| 590 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 591 | ret = v3d_job_init(v3d, file_priv, &job->base, |
| 592 | v3d_job_free, args->in_sync); |
| 593 | if (ret) { |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 594 | kfree(job); |
| 595 | return ret; |
| 596 | } |
| 597 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 598 | job->base.bo = kcalloc(ARRAY_SIZE(args->bo_handles), |
| 599 | sizeof(*job->base.bo), GFP_KERNEL); |
| 600 | if (!job->base.bo) { |
| 601 | v3d_job_put(&job->base); |
| 602 | return -ENOMEM; |
| 603 | } |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 604 | |
| 605 | job->args = *args; |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 606 | |
| 607 | spin_lock(&file_priv->table_lock); |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 608 | for (job->base.bo_count = 0; |
| 609 | job->base.bo_count < ARRAY_SIZE(args->bo_handles); |
| 610 | job->base.bo_count++) { |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 611 | struct drm_gem_object *bo; |
| 612 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 613 | if (!args->bo_handles[job->base.bo_count]) |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 614 | break; |
| 615 | |
| 616 | bo = idr_find(&file_priv->object_idr, |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 617 | args->bo_handles[job->base.bo_count]); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 618 | if (!bo) { |
| 619 | DRM_DEBUG("Failed to look up GEM BO %d: %d\n", |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 620 | job->base.bo_count, |
| 621 | args->bo_handles[job->base.bo_count]); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 622 | ret = -ENOENT; |
| 623 | spin_unlock(&file_priv->table_lock); |
| 624 | goto fail; |
| 625 | } |
| 626 | drm_gem_object_get(bo); |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 627 | job->base.bo[job->base.bo_count] = bo; |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 628 | } |
| 629 | spin_unlock(&file_priv->table_lock); |
| 630 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 631 | ret = v3d_lock_bo_reservations(job->base.bo, job->base.bo_count, |
| 632 | &acquire_ctx); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 633 | if (ret) |
| 634 | goto fail; |
| 635 | |
| 636 | mutex_lock(&v3d->sched_lock); |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 637 | ret = v3d_push_job(v3d_priv, &job->base, V3D_TFU); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 638 | if (ret) |
| 639 | goto fail_unreserve; |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 640 | mutex_unlock(&v3d->sched_lock); |
| 641 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 642 | v3d_attach_fences_and_unlock_reservation(file_priv, |
| 643 | &job->base, &acquire_ctx, |
| 644 | args->out_sync); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 645 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 646 | v3d_job_put(&job->base); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 647 | |
| 648 | return 0; |
| 649 | |
| 650 | fail_unreserve: |
| 651 | mutex_unlock(&v3d->sched_lock); |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 652 | drm_gem_unlock_reservations(job->base.bo, job->base.bo_count, |
| 653 | &acquire_ctx); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 654 | fail: |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 655 | v3d_job_put(&job->base); |
Eric Anholt | 1584f16 | 2018-11-28 15:09:25 -0800 | [diff] [blame] | 656 | |
| 657 | return ret; |
| 658 | } |
| 659 | |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 660 | int |
| 661 | v3d_gem_init(struct drm_device *dev) |
| 662 | { |
| 663 | struct v3d_dev *v3d = to_v3d_dev(dev); |
| 664 | u32 pt_size = 4096 * 1024; |
| 665 | int ret, i; |
| 666 | |
| 667 | for (i = 0; i < V3D_MAX_QUEUES; i++) |
| 668 | v3d->queue[i].fence_context = dma_fence_context_alloc(1); |
| 669 | |
| 670 | spin_lock_init(&v3d->mm_lock); |
| 671 | spin_lock_init(&v3d->job_lock); |
| 672 | mutex_init(&v3d->bo_lock); |
| 673 | mutex_init(&v3d->reset_lock); |
Eric Anholt | 7122b68 | 2018-06-06 10:48:51 -0700 | [diff] [blame] | 674 | mutex_init(&v3d->sched_lock); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 675 | |
| 676 | /* Note: We don't allocate address 0. Various bits of HW |
| 677 | * treat 0 as special, such as the occlusion query counters |
| 678 | * where 0 means "disabled". |
| 679 | */ |
| 680 | drm_mm_init(&v3d->mm, 1, pt_size / sizeof(u32) - 1); |
| 681 | |
| 682 | v3d->pt = dma_alloc_wc(v3d->dev, pt_size, |
| 683 | &v3d->pt_paddr, |
| 684 | GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO); |
| 685 | if (!v3d->pt) { |
| 686 | drm_mm_takedown(&v3d->mm); |
| 687 | dev_err(v3d->dev, |
| 688 | "Failed to allocate page tables. " |
| 689 | "Please ensure you have CMA enabled.\n"); |
| 690 | return -ENOMEM; |
| 691 | } |
| 692 | |
| 693 | v3d_init_hw_state(v3d); |
| 694 | v3d_mmu_set_page_table(v3d); |
| 695 | |
| 696 | ret = v3d_sched_init(v3d); |
| 697 | if (ret) { |
| 698 | drm_mm_takedown(&v3d->mm); |
| 699 | dma_free_coherent(v3d->dev, 4096 * 1024, (void *)v3d->pt, |
| 700 | v3d->pt_paddr); |
| 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | void |
| 707 | v3d_gem_destroy(struct drm_device *dev) |
| 708 | { |
| 709 | struct v3d_dev *v3d = to_v3d_dev(dev); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 710 | |
| 711 | v3d_sched_fini(v3d); |
| 712 | |
Eric Anholt | a783a09 | 2019-04-16 15:58:53 -0700 | [diff] [blame^] | 713 | /* Waiting for jobs to finish would need to be done before |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 714 | * unregistering V3D. |
| 715 | */ |
Eric Anholt | 14d1d19 | 2018-06-05 12:03:01 -0700 | [diff] [blame] | 716 | WARN_ON(v3d->bin_job); |
| 717 | WARN_ON(v3d->render_job); |
Eric Anholt | 57692c9 | 2018-04-30 11:10:58 -0700 | [diff] [blame] | 718 | |
| 719 | drm_mm_takedown(&v3d->mm); |
| 720 | |
| 721 | dma_free_coherent(v3d->dev, 4096 * 1024, (void *)v3d->pt, v3d->pt_paddr); |
| 722 | } |