90
90
* used for all of GuC submission but that could change in the future.
91
91
*
92
92
* guc->submission_state.lock
93
- * Protects guc_id allocation for the given GuC, i.e. only one context can be
94
- * doing guc_id allocation operations at a time for each GuC in the system .
93
+ * Global lock for GuC submission state. Protects guc_ids and destroyed contexts
94
+ * list .
95
95
*
96
96
* ce->guc_state.lock
97
97
* Protects everything under ce->guc_state. Ensures that a context is in the
@@ -719,6 +719,7 @@ static void scrub_guc_desc_for_outstanding_g2h(struct intel_guc *guc)
719
719
if (deregister )
720
720
guc_signal_context_fence (ce );
721
721
if (destroyed ) {
722
+ intel_gt_pm_put_async (guc_to_gt (guc ));
722
723
release_guc_id (guc , ce );
723
724
__guc_context_destroy (ce );
724
725
}
@@ -797,6 +798,8 @@ static void guc_flush_submissions(struct intel_guc *guc)
797
798
spin_unlock_irqrestore (& sched_engine -> lock , flags );
798
799
}
799
800
801
+ static void guc_flush_destroyed_contexts (struct intel_guc * guc );
802
+
800
803
void intel_guc_submission_reset_prepare (struct intel_guc * guc )
801
804
{
802
805
int i ;
@@ -815,6 +818,7 @@ void intel_guc_submission_reset_prepare(struct intel_guc *guc)
815
818
spin_unlock_irq (& guc_to_gt (guc )-> irq_lock );
816
819
817
820
guc_flush_submissions (guc );
821
+ guc_flush_destroyed_contexts (guc );
818
822
819
823
/*
820
824
* Handle any outstanding G2Hs before reset. Call IRQ handler directly
@@ -1126,6 +1130,8 @@ void intel_guc_submission_reset_finish(struct intel_guc *guc)
1126
1130
intel_gt_unpark_heartbeats (guc_to_gt (guc ));
1127
1131
}
1128
1132
1133
+ static void destroyed_worker_func (struct work_struct * w );
1134
+
1129
1135
/*
1130
1136
* Set up the memory resources to be shared with the GuC (via the GGTT)
1131
1137
* at firmware loading time.
@@ -1151,6 +1157,9 @@ int intel_guc_submission_init(struct intel_guc *guc)
1151
1157
spin_lock_init (& guc -> submission_state .lock );
1152
1158
INIT_LIST_HEAD (& guc -> submission_state .guc_id_list );
1153
1159
ida_init (& guc -> submission_state .guc_ids );
1160
+ INIT_LIST_HEAD (& guc -> submission_state .destroyed_contexts );
1161
+ INIT_WORK (& guc -> submission_state .destroyed_worker ,
1162
+ destroyed_worker_func );
1154
1163
1155
1164
return 0 ;
1156
1165
}
@@ -1160,6 +1169,7 @@ void intel_guc_submission_fini(struct intel_guc *guc)
1160
1169
if (!guc -> lrc_desc_pool )
1161
1170
return ;
1162
1171
1172
+ guc_flush_destroyed_contexts (guc );
1163
1173
guc_lrc_desc_pool_destroy (guc );
1164
1174
i915_sched_engine_put (guc -> sched_engine );
1165
1175
}
@@ -1859,11 +1869,30 @@ static void guc_context_sched_disable(struct intel_context *ce)
1859
1869
static inline void guc_lrc_desc_unpin (struct intel_context * ce )
1860
1870
{
1861
1871
struct intel_guc * guc = ce_to_guc (ce );
1872
+ struct intel_gt * gt = guc_to_gt (guc );
1873
+ unsigned long flags ;
1874
+ bool disabled ;
1862
1875
1876
+ GEM_BUG_ON (!intel_gt_pm_is_awake (gt ));
1863
1877
GEM_BUG_ON (!lrc_desc_registered (guc , ce -> guc_id .id ));
1864
1878
GEM_BUG_ON (ce != __get_context (guc , ce -> guc_id .id ));
1865
1879
GEM_BUG_ON (context_enabled (ce ));
1866
1880
1881
+ /* Seal race with Reset */
1882
+ spin_lock_irqsave (& ce -> guc_state .lock , flags );
1883
+ disabled = submission_disabled (guc );
1884
+ if (likely (!disabled )) {
1885
+ __intel_gt_pm_get (gt );
1886
+ set_context_destroyed (ce );
1887
+ clr_context_registered (ce );
1888
+ }
1889
+ spin_unlock_irqrestore (& ce -> guc_state .lock , flags );
1890
+ if (unlikely (disabled )) {
1891
+ release_guc_id (guc , ce );
1892
+ __guc_context_destroy (ce );
1893
+ return ;
1894
+ }
1895
+
1867
1896
deregister_context (ce , ce -> guc_id .id );
1868
1897
}
1869
1898
@@ -1891,78 +1920,86 @@ static void __guc_context_destroy(struct intel_context *ce)
1891
1920
}
1892
1921
}
1893
1922
1923
+ static void guc_flush_destroyed_contexts (struct intel_guc * guc )
1924
+ {
1925
+ struct intel_context * ce , * cn ;
1926
+ unsigned long flags ;
1927
+
1928
+ GEM_BUG_ON (!submission_disabled (guc ) &&
1929
+ guc_submission_initialized (guc ));
1930
+
1931
+ spin_lock_irqsave (& guc -> submission_state .lock , flags );
1932
+ list_for_each_entry_safe (ce , cn ,
1933
+ & guc -> submission_state .destroyed_contexts ,
1934
+ destroyed_link ) {
1935
+ list_del_init (& ce -> destroyed_link );
1936
+ __release_guc_id (guc , ce );
1937
+ __guc_context_destroy (ce );
1938
+ }
1939
+ spin_unlock_irqrestore (& guc -> submission_state .lock , flags );
1940
+ }
1941
+
1942
+ static void deregister_destroyed_contexts (struct intel_guc * guc )
1943
+ {
1944
+ struct intel_context * ce , * cn ;
1945
+ unsigned long flags ;
1946
+
1947
+ spin_lock_irqsave (& guc -> submission_state .lock , flags );
1948
+ list_for_each_entry_safe (ce , cn ,
1949
+ & guc -> submission_state .destroyed_contexts ,
1950
+ destroyed_link ) {
1951
+ list_del_init (& ce -> destroyed_link );
1952
+ guc_lrc_desc_unpin (ce );
1953
+ }
1954
+ spin_unlock_irqrestore (& guc -> submission_state .lock , flags );
1955
+ }
1956
+
1957
+ static void destroyed_worker_func (struct work_struct * w )
1958
+ {
1959
+ struct intel_guc * guc = container_of (w , struct intel_guc ,
1960
+ submission_state .destroyed_worker );
1961
+ struct intel_gt * gt = guc_to_gt (guc );
1962
+ int tmp ;
1963
+
1964
+ with_intel_gt_pm (gt , tmp )
1965
+ deregister_destroyed_contexts (guc );
1966
+ }
1967
+
1894
1968
static void guc_context_destroy (struct kref * kref )
1895
1969
{
1896
1970
struct intel_context * ce = container_of (kref , typeof (* ce ), ref );
1897
- struct intel_runtime_pm * runtime_pm = ce -> engine -> uncore -> rpm ;
1898
1971
struct intel_guc * guc = ce_to_guc (ce );
1899
- intel_wakeref_t wakeref ;
1900
1972
unsigned long flags ;
1901
- bool disabled ;
1973
+ bool destroy ;
1902
1974
1903
1975
/*
1904
1976
* If the guc_id is invalid this context has been stolen and we can free
1905
1977
* it immediately. Also can be freed immediately if the context is not
1906
1978
* registered with the GuC or the GuC is in the middle of a reset.
1907
1979
*/
1908
- if (context_guc_id_invalid (ce )) {
1909
- __guc_context_destroy (ce );
1910
- return ;
1911
- } else if (submission_disabled (guc ) ||
1912
- !lrc_desc_registered (guc , ce -> guc_id .id )) {
1913
- release_guc_id (guc , ce );
1914
- __guc_context_destroy (ce );
1915
- return ;
1916
- }
1917
-
1918
- /*
1919
- * We have to acquire the context spinlock and check guc_id again, if it
1920
- * is valid it hasn't been stolen and needs to be deregistered. We
1921
- * delete this context from the list of unpinned guc_id available to
1922
- * steal to seal a race with guc_lrc_desc_pin(). When the G2H CTB
1923
- * returns indicating this context has been deregistered the guc_id is
1924
- * returned to the pool of available guc_id.
1925
- */
1926
1980
spin_lock_irqsave (& guc -> submission_state .lock , flags );
1927
- if (context_guc_id_invalid (ce )) {
1928
- spin_unlock_irqrestore (& guc -> submission_state .lock , flags );
1929
- __guc_context_destroy (ce );
1930
- return ;
1981
+ destroy = submission_disabled (guc ) || context_guc_id_invalid (ce ) ||
1982
+ !lrc_desc_registered (guc , ce -> guc_id .id );
1983
+ if (likely (!destroy )) {
1984
+ if (!list_empty (& ce -> guc_id .link ))
1985
+ list_del_init (& ce -> guc_id .link );
1986
+ list_add_tail (& ce -> destroyed_link ,
1987
+ & guc -> submission_state .destroyed_contexts );
1988
+ } else {
1989
+ __release_guc_id (guc , ce );
1931
1990
}
1932
-
1933
- if (!list_empty (& ce -> guc_id .link ))
1934
- list_del_init (& ce -> guc_id .link );
1935
1991
spin_unlock_irqrestore (& guc -> submission_state .lock , flags );
1936
-
1937
- /* Seal race with Reset */
1938
- spin_lock_irqsave (& ce -> guc_state .lock , flags );
1939
- disabled = submission_disabled (guc );
1940
- if (likely (!disabled )) {
1941
- set_context_destroyed (ce );
1942
- clr_context_registered (ce );
1943
- }
1944
- spin_unlock_irqrestore (& ce -> guc_state .lock , flags );
1945
- if (unlikely (disabled )) {
1946
- release_guc_id (guc , ce );
1992
+ if (unlikely (destroy )) {
1947
1993
__guc_context_destroy (ce );
1948
1994
return ;
1949
1995
}
1950
1996
1951
1997
/*
1952
- * We defer GuC context deregistration until the context is destroyed
1953
- * in order to save on CTBs. With this optimization ideally we only need
1954
- * 1 CTB to register the context during the first pin and 1 CTB to
1955
- * deregister the context when the context is destroyed. Without this
1956
- * optimization, a CTB would be needed every pin & unpin.
1957
- *
1958
- * XXX: Need to acqiure the runtime wakeref as this can be triggered
1959
- * from context_free_worker when runtime wakeref is not held.
1960
- * guc_lrc_desc_unpin requires the runtime as a GuC register is written
1961
- * in H2G CTB to deregister the context. A future patch may defer this
1962
- * H2G CTB if the runtime wakeref is zero.
1998
+ * We use a worker to issue the H2G to deregister the context as we can
1999
+ * take the GT PM for the first time which isn't allowed from an atomic
2000
+ * context.
1963
2001
*/
1964
- with_intel_runtime_pm (runtime_pm , wakeref )
1965
- guc_lrc_desc_unpin (ce );
2002
+ queue_work (system_unbound_wq , & guc -> submission_state .destroyed_worker );
1966
2003
}
1967
2004
1968
2005
static int guc_context_alloc (struct intel_context * ce )
@@ -2798,6 +2835,7 @@ int intel_guc_deregister_done_process_msg(struct intel_guc *guc,
2798
2835
intel_context_put (ce );
2799
2836
} else if (context_destroyed (ce )) {
2800
2837
/* Context has been destroyed */
2838
+ intel_gt_pm_put_async (guc_to_gt (guc ));
2801
2839
release_guc_id (guc , ce );
2802
2840
__guc_context_destroy (ce );
2803
2841
}
0 commit comments