@@ -162,10 +162,52 @@ v3d_flush_l2t(struct v3d_dev *v3d, int core)
162162 /* While there is a busy bit (V3D_L2TCACTL_L2TFLS), we don't
163163 * need to wait for completion before dispatching the job --
164164 * L2T accesses will be stalled until the flush has completed.
165+ * However, we do need to make sure we don't try to trigger a
166+ * new flush while the L2_CLEAN queue is trying to
167+ * synchronously clean after a job.
165168 */
169+ mutex_lock (& v3d -> cache_clean_lock );
166170 V3D_CORE_WRITE (core , V3D_CTL_L2TCACTL ,
167171 V3D_L2TCACTL_L2TFLS |
168172 V3D_SET_FIELD (V3D_L2TCACTL_FLM_FLUSH , V3D_L2TCACTL_FLM ));
173+ mutex_unlock (& v3d -> cache_clean_lock );
174+ }
175+
176+ /* Cleans texture L1 and L2 cachelines (writing back dirty data).
177+ *
178+ * For cleaning, which happens from the CACHE_CLEAN queue after CSD has
179+ * executed, we need to make sure that the clean is done before
180+ * signaling job completion. So, we synchronously wait before
181+ * returning, and we make sure that L2 invalidates don't happen in the
182+ * meantime to confuse our are-we-done checks.
183+ */
184+ void
185+ v3d_clean_caches (struct v3d_dev * v3d )
186+ {
187+ struct drm_device * dev = & v3d -> drm ;
188+ int core = 0 ;
189+
190+ trace_v3d_cache_clean_begin (dev );
191+
192+ V3D_CORE_WRITE (core , V3D_CTL_L2TCACTL , V3D_L2TCACTL_TMUWCF );
193+ if (wait_for (!(V3D_CORE_READ (core , V3D_CTL_L2TCACTL ) &
194+ V3D_L2TCACTL_L2TFLS ), 100 )) {
195+ DRM_ERROR ("Timeout waiting for L1T write combiner flush\n" );
196+ }
197+
198+ mutex_lock (& v3d -> cache_clean_lock );
199+ V3D_CORE_WRITE (core , V3D_CTL_L2TCACTL ,
200+ V3D_L2TCACTL_L2TFLS |
201+ V3D_SET_FIELD (V3D_L2TCACTL_FLM_CLEAN , V3D_L2TCACTL_FLM ));
202+
203+ if (wait_for (!(V3D_CORE_READ (core , V3D_CTL_L2TCACTL ) &
204+ V3D_L2TCACTL_L2TFLS ), 100 )) {
205+ DRM_ERROR ("Timeout waiting for L2T clean\n" );
206+ }
207+
208+ mutex_unlock (& v3d -> cache_clean_lock );
209+
210+ trace_v3d_cache_clean_end (dev );
169211}
170212
171213/* Invalidates the slice caches. These are read-only caches. */
@@ -584,7 +626,8 @@ static void
584626v3d_attach_fences_and_unlock_reservation (struct drm_file * file_priv ,
585627 struct v3d_job * job ,
586628 struct ww_acquire_ctx * acquire_ctx ,
587- u32 out_sync )
629+ u32 out_sync ,
630+ struct dma_fence * done_fence )
588631{
589632 struct drm_syncobj * sync_out ;
590633
@@ -594,7 +637,7 @@ v3d_attach_fences_and_unlock_reservation(struct drm_file *file_priv,
594637 /* Update the return sync object for the job */
595638 sync_out = drm_syncobj_find (file_priv , out_sync );
596639 if (sync_out ) {
597- drm_syncobj_replace_fence (sync_out , job -> done_fence );
640+ drm_syncobj_replace_fence (sync_out , done_fence );
598641 drm_syncobj_put (sync_out );
599642 }
600643}
@@ -691,8 +734,10 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
691734 mutex_unlock (& v3d -> sched_lock );
692735
693736 v3d_attach_fences_and_unlock_reservation (file_priv ,
694- & render -> base , & acquire_ctx ,
695- args -> out_sync );
737+ & render -> base ,
738+ & acquire_ctx ,
739+ args -> out_sync ,
740+ render -> base .done_fence );
696741
697742 if (bin )
698743 v3d_job_put (& bin -> base );
@@ -785,7 +830,8 @@ v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
785830
786831 v3d_attach_fences_and_unlock_reservation (file_priv ,
787832 & job -> base , & acquire_ctx ,
788- args -> out_sync );
833+ args -> out_sync ,
834+ job -> base .done_fence );
789835
790836 v3d_job_put (& job -> base );
791837
@@ -801,6 +847,105 @@ v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
801847 return ret ;
802848}
803849
850+ /**
851+ * v3d_submit_csd_ioctl() - Submits a CSD (texture formatting) job to the V3D.
852+ * @dev: DRM device
853+ * @data: ioctl argument
854+ * @file_priv: DRM file for this fd
855+ *
856+ * Userspace provides the register setup for the CSD, which we don't
857+ * need to validate since the CSD is behind the MMU.
858+ */
859+ int
860+ v3d_submit_csd_ioctl (struct drm_device * dev , void * data ,
861+ struct drm_file * file_priv )
862+ {
863+ struct v3d_dev * v3d = to_v3d_dev (dev );
864+ struct v3d_file_priv * v3d_priv = file_priv -> driver_priv ;
865+ struct drm_v3d_submit_csd * args = data ;
866+ struct v3d_csd_job * job ;
867+ struct v3d_job * clean_job ;
868+ struct ww_acquire_ctx acquire_ctx ;
869+ int ret ;
870+
871+ trace_v3d_submit_csd_ioctl (& v3d -> drm , args -> cfg [5 ], args -> cfg [6 ]);
872+
873+ if (!v3d_has_csd (v3d )) {
874+ DRM_DEBUG ("Attempting CSD submit on non-CSD hardware\n" );
875+ return - EINVAL ;
876+ }
877+
878+ job = kcalloc (1 , sizeof (* job ), GFP_KERNEL );
879+ if (!job )
880+ return - ENOMEM ;
881+
882+ ret = v3d_job_init (v3d , file_priv , & job -> base ,
883+ v3d_job_free , args -> in_sync );
884+ if (ret ) {
885+ kfree (job );
886+ return ret ;
887+ }
888+
889+ clean_job = kcalloc (1 , sizeof (* clean_job ), GFP_KERNEL );
890+ if (!clean_job ) {
891+ v3d_job_put (& job -> base );
892+ kfree (job );
893+ return - ENOMEM ;
894+ }
895+
896+ ret = v3d_job_init (v3d , file_priv , clean_job , v3d_job_free , 0 );
897+ if (ret ) {
898+ v3d_job_put (& job -> base );
899+ kfree (clean_job );
900+ return ret ;
901+ }
902+
903+ job -> args = * args ;
904+
905+ ret = v3d_lookup_bos (dev , file_priv , clean_job ,
906+ args -> bo_handles , args -> bo_handle_count );
907+ if (ret )
908+ goto fail ;
909+
910+ ret = v3d_lock_bo_reservations (clean_job , & acquire_ctx );
911+ if (ret )
912+ goto fail ;
913+
914+ mutex_lock (& v3d -> sched_lock );
915+ ret = v3d_push_job (v3d_priv , & job -> base , V3D_CSD );
916+ if (ret )
917+ goto fail_unreserve ;
918+
919+ ret = v3d_add_dep (clean_job , dma_fence_get (job -> base .done_fence ));
920+ if (ret )
921+ goto fail_unreserve ;
922+ ret = v3d_push_job (v3d_priv , clean_job , V3D_CACHE_CLEAN );
923+ if (ret )
924+ goto fail_unreserve ;
925+ mutex_unlock (& v3d -> sched_lock );
926+
927+ v3d_attach_fences_and_unlock_reservation (file_priv ,
928+ clean_job ,
929+ & acquire_ctx ,
930+ args -> out_sync ,
931+ clean_job -> done_fence );
932+
933+ v3d_job_put (& job -> base );
934+ v3d_job_put (clean_job );
935+
936+ return 0 ;
937+
938+ fail_unreserve :
939+ mutex_unlock (& v3d -> sched_lock );
940+ v3d_unlock_bo_reservations (clean_job -> bo , clean_job -> bo_count ,
941+ & acquire_ctx );
942+ fail :
943+ v3d_job_put (& job -> base );
944+ v3d_job_put (clean_job );
945+
946+ return ret ;
947+ }
948+
804949int
805950v3d_gem_init (struct drm_device * dev )
806951{
@@ -816,6 +961,7 @@ v3d_gem_init(struct drm_device *dev)
816961 mutex_init (& v3d -> bo_lock );
817962 mutex_init (& v3d -> reset_lock );
818963 mutex_init (& v3d -> sched_lock );
964+ mutex_init (& v3d -> cache_clean_lock );
819965
820966 /* Note: We don't allocate address 0. Various bits of HW
821967 * treat 0 as special, such as the occlusion query counters
0 commit comments