@@ -89,13 +89,19 @@ dma_wait_for_async_tx(struct dma_async_tx_descriptor *tx)
8989 iter = tx ;
9090
9191 /* find the root of the unsubmitted dependency chain */
92- while ( iter -> cookie == - EBUSY ) {
92+ do {
9393 parent = iter -> parent ;
94- if (parent && parent -> cookie == - EBUSY )
95- iter = iter -> parent ;
96- else
94+ if (!parent )
9795 break ;
98- }
96+ else
97+ iter = parent ;
98+ } while (parent );
99+
100+ /* there is a small window for ->parent == NULL and
101+ * ->cookie == -EBUSY
102+ */
103+ while (iter -> cookie == - EBUSY )
104+ cpu_relax ();
99105
100106 status = dma_sync_wait (iter -> chan , iter -> cookie );
101107 } while (status == DMA_IN_PROGRESS || (iter != tx ));
@@ -111,24 +117,33 @@ EXPORT_SYMBOL_GPL(dma_wait_for_async_tx);
111117void
112118async_tx_run_dependencies (struct dma_async_tx_descriptor * tx )
113119{
114- struct dma_async_tx_descriptor * dep_tx , * _dep_tx ;
115- struct dma_device * dev ;
120+ struct dma_async_tx_descriptor * next = tx -> next ;
116121 struct dma_chan * chan ;
117122
118- list_for_each_entry_safe (dep_tx , _dep_tx , & tx -> depend_list ,
119- depend_node ) {
120- chan = dep_tx -> chan ;
121- dev = chan -> device ;
122- /* we can't depend on ourselves */
123- BUG_ON (chan == tx -> chan );
124- list_del (& dep_tx -> depend_node );
125- tx -> tx_submit (dep_tx );
126-
127- /* we need to poke the engine as client code does not
128- * know about dependency submission events
129- */
130- dev -> device_issue_pending (chan );
123+ if (!next )
124+ return ;
125+
126+ tx -> next = NULL ;
127+ chan = next -> chan ;
128+
129+ /* keep submitting up until a channel switch is detected
130+ * in that case we will be called again as a result of
131+ * processing the interrupt from async_tx_channel_switch
132+ */
133+ while (next && next -> chan == chan ) {
134+ struct dma_async_tx_descriptor * _next ;
135+
136+ spin_lock_bh (& next -> lock );
137+ next -> parent = NULL ;
138+ _next = next -> next ;
139+ next -> next = NULL ;
140+ spin_unlock_bh (& next -> lock );
141+
142+ next -> tx_submit (next );
143+ next = _next ;
131144 }
145+
146+ chan -> device -> device_issue_pending (chan );
132147}
133148EXPORT_SYMBOL_GPL (async_tx_run_dependencies );
134149
@@ -397,6 +412,92 @@ static void __exit async_tx_exit(void)
397412}
398413#endif
399414
415+
416+ /**
417+ * async_tx_channel_switch - queue an interrupt descriptor with a dependency
418+ * pre-attached.
419+ * @depend_tx: the operation that must finish before the new operation runs
420+ * @tx: the new operation
421+ */
422+ static void
423+ async_tx_channel_switch (struct dma_async_tx_descriptor * depend_tx ,
424+ struct dma_async_tx_descriptor * tx )
425+ {
426+ struct dma_chan * chan ;
427+ struct dma_device * device ;
428+ struct dma_async_tx_descriptor * intr_tx = (void * ) ~0 ;
429+
430+ /* first check to see if we can still append to depend_tx */
431+ spin_lock_bh (& depend_tx -> lock );
432+ if (depend_tx -> parent && depend_tx -> chan == tx -> chan ) {
433+ tx -> parent = depend_tx ;
434+ depend_tx -> next = tx ;
435+ intr_tx = NULL ;
436+ }
437+ spin_unlock_bh (& depend_tx -> lock );
438+
439+ if (!intr_tx )
440+ return ;
441+
442+ chan = depend_tx -> chan ;
443+ device = chan -> device ;
444+
445+ /* see if we can schedule an interrupt
446+ * otherwise poll for completion
447+ */
448+ if (dma_has_cap (DMA_INTERRUPT , device -> cap_mask ))
449+ intr_tx = device -> device_prep_dma_interrupt (chan );
450+ else
451+ intr_tx = NULL ;
452+
453+ if (intr_tx ) {
454+ intr_tx -> callback = NULL ;
455+ intr_tx -> callback_param = NULL ;
456+ tx -> parent = intr_tx ;
457+ /* safe to set ->next outside the lock since we know we are
458+ * not submitted yet
459+ */
460+ intr_tx -> next = tx ;
461+
462+ /* check if we need to append */
463+ spin_lock_bh (& depend_tx -> lock );
464+ if (depend_tx -> parent ) {
465+ intr_tx -> parent = depend_tx ;
466+ depend_tx -> next = intr_tx ;
467+ async_tx_ack (intr_tx );
468+ intr_tx = NULL ;
469+ }
470+ spin_unlock_bh (& depend_tx -> lock );
471+
472+ if (intr_tx ) {
473+ intr_tx -> parent = NULL ;
474+ intr_tx -> tx_submit (intr_tx );
475+ async_tx_ack (intr_tx );
476+ }
477+ } else {
478+ if (dma_wait_for_async_tx (depend_tx ) == DMA_ERROR )
479+ panic ("%s: DMA_ERROR waiting for depend_tx\n" ,
480+ __func__ );
481+ tx -> tx_submit (tx );
482+ }
483+ }
484+
485+
486+ /**
487+ * submit_disposition - while holding depend_tx->lock we must avoid submitting
488+ * new operations to prevent a circular locking dependency with
489+ * drivers that already hold a channel lock when calling
490+ * async_tx_run_dependencies.
491+ * @ASYNC_TX_SUBMITTED: we were able to append the new operation under the lock
492+ * @ASYNC_TX_CHANNEL_SWITCH: when the lock is dropped schedule a channel switch
493+ * @ASYNC_TX_DIRECT_SUBMIT: when the lock is dropped submit directly
494+ */
495+ enum submit_disposition {
496+ ASYNC_TX_SUBMITTED ,
497+ ASYNC_TX_CHANNEL_SWITCH ,
498+ ASYNC_TX_DIRECT_SUBMIT ,
499+ };
500+
400501void
401502async_tx_submit (struct dma_chan * chan , struct dma_async_tx_descriptor * tx ,
402503 enum async_tx_flags flags , struct dma_async_tx_descriptor * depend_tx ,
@@ -405,28 +506,54 @@ async_tx_submit(struct dma_chan *chan, struct dma_async_tx_descriptor *tx,
405506 tx -> callback = cb_fn ;
406507 tx -> callback_param = cb_param ;
407508
408- /* set this new tx to run after depend_tx if:
409- * 1/ a dependency exists (depend_tx is !NULL)
410- * 2/ the tx can not be submitted to the current channel
411- */
412- if (depend_tx && depend_tx -> chan != chan ) {
413- /* if ack is already set then we cannot be sure
509+ if (depend_tx ) {
510+ enum submit_disposition s ;
511+
512+ /* sanity check the dependency chain:
513+ * 1/ if ack is already set then we cannot be sure
414514 * we are referring to the correct operation
515+ * 2/ dependencies are 1:1 i.e. two transactions can
516+ * not depend on the same parent
415517 */
416- BUG_ON (depend_tx -> ack );
518+ BUG_ON (depend_tx -> ack || depend_tx -> next || tx -> parent );
417519
418- tx -> parent = depend_tx ;
520+ /* the lock prevents async_tx_run_dependencies from missing
521+ * the setting of ->next when ->parent != NULL
522+ */
419523 spin_lock_bh (& depend_tx -> lock );
420- list_add_tail (& tx -> depend_node , & depend_tx -> depend_list );
421- if (depend_tx -> cookie == 0 ) {
422- struct dma_chan * dep_chan = depend_tx -> chan ;
423- struct dma_device * dep_dev = dep_chan -> device ;
424- dep_dev -> device_dependency_added (dep_chan );
524+ if (depend_tx -> parent ) {
525+ /* we have a parent so we can not submit directly
526+ * if we are staying on the same channel: append
527+ * else: channel switch
528+ */
529+ if (depend_tx -> chan == chan ) {
530+ tx -> parent = depend_tx ;
531+ depend_tx -> next = tx ;
532+ s = ASYNC_TX_SUBMITTED ;
533+ } else
534+ s = ASYNC_TX_CHANNEL_SWITCH ;
535+ } else {
536+ /* we do not have a parent so we may be able to submit
537+ * directly if we are staying on the same channel
538+ */
539+ if (depend_tx -> chan == chan )
540+ s = ASYNC_TX_DIRECT_SUBMIT ;
541+ else
542+ s = ASYNC_TX_CHANNEL_SWITCH ;
425543 }
426544 spin_unlock_bh (& depend_tx -> lock );
427545
428- /* schedule an interrupt to trigger the channel switch */
429- async_trigger_callback (ASYNC_TX_ACK , depend_tx , NULL , NULL );
546+ switch (s ) {
547+ case ASYNC_TX_SUBMITTED :
548+ break ;
549+ case ASYNC_TX_CHANNEL_SWITCH :
550+ async_tx_channel_switch (depend_tx , tx );
551+ break ;
552+ case ASYNC_TX_DIRECT_SUBMIT :
553+ tx -> parent = NULL ;
554+ tx -> tx_submit (tx );
555+ break ;
556+ }
430557 } else {
431558 tx -> parent = NULL ;
432559 tx -> tx_submit (tx );
0 commit comments