@@ -338,13 +338,10 @@ static int hda_dsp_send_pm_gate_ipc(struct snd_sof_dev *sdev, u32 flags)
338338 sizeof (pm_gate ), & reply , sizeof (reply ));
339339}
340340
341- int hda_dsp_set_power_state (struct snd_sof_dev * sdev ,
342- enum sof_d0_substate d0_substate )
341+ static int hda_dsp_update_d0i3c_register (struct snd_sof_dev * sdev , u8 value )
343342{
344343 struct hdac_bus * bus = sof_to_bus (sdev );
345- u32 flags ;
346344 int ret ;
347- u8 value ;
348345
349346 /* Write to D0I3C after Command-In-Progress bit is cleared */
350347 ret = hda_dsp_wait_d0i3c_done (sdev );
@@ -354,7 +351,6 @@ int hda_dsp_set_power_state(struct snd_sof_dev *sdev,
354351 }
355352
356353 /* Update D0I3C register */
357- value = d0_substate == SOF_DSP_D0I3 ? SOF_HDA_VS_D0I3C_I3 : 0 ;
358354 snd_hdac_chip_updateb (bus , VS_D0I3C , SOF_HDA_VS_D0I3C_I3 , value );
359355
360356 /* Wait for cmd in progress to be cleared before exiting the function */
@@ -367,20 +363,160 @@ int hda_dsp_set_power_state(struct snd_sof_dev *sdev,
367363 dev_vdbg (bus -> dev , "D0I3C updated, register = 0x%x\n" ,
368364 snd_hdac_chip_readb (bus , VS_D0I3C ));
369365
370- if (d0_substate == SOF_DSP_D0I0 )
371- flags = HDA_PM_PPG ;/* prevent power gating in D0 */
372- else
373- flags = HDA_PM_NO_DMA_TRACE ;/* disable DMA trace in D0I3*/
366+ return 0 ;
367+ }
374368
375- /* sending pm_gate IPC */
376- ret = hda_dsp_send_pm_gate_ipc (sdev , flags );
369+ static int hda_dsp_set_D0_state (struct snd_sof_dev * sdev ,
370+ const struct sof_dsp_power_state * target_state )
371+ {
372+ u32 flags = 0 ;
373+ int ret ;
374+ u8 value = 0 ;
375+
376+ /*
377+ * Sanity check for illegal state transitions
378+ * The only allowed transitions are:
379+ * 1. D3 -> D0I0
380+ * 2. D0I0 -> D0I3
381+ * 3. D0I3 -> D0I0
382+ */
383+ switch (sdev -> dsp_power_state .state ) {
384+ case SOF_DSP_PM_D0 :
385+ /* Follow the sequence below for D0 substate transitions */
386+ break ;
387+ case SOF_DSP_PM_D3 :
388+ /* Follow regular flow for D3 -> D0 transition */
389+ return 0 ;
390+ default :
391+ dev_err (sdev -> dev , "error: transition from %d to %d not allowed\n" ,
392+ sdev -> dsp_power_state .state , target_state -> state );
393+ return - EINVAL ;
394+ }
395+
396+ /* Set flags and register value for D0 target substate */
397+ if (target_state -> substate == SOF_HDA_DSP_PM_D0I3 ) {
398+ value = SOF_HDA_VS_D0I3C_I3 ;
399+
400+ /* disable DMA trace in D0I3 */
401+ flags = HDA_PM_NO_DMA_TRACE ;
402+ } else {
403+ /* prevent power gating in D0I0 */
404+ flags = HDA_PM_PPG ;
405+ }
406+
407+ /* update D0I3C register */
408+ ret = hda_dsp_update_d0i3c_register (sdev , value );
377409 if (ret < 0 )
410+ return ret ;
411+
412+ /*
413+ * Notify the DSP of the state change.
414+ * If this IPC fails, revert the D0I3C register update in order
415+ * to prevent partial state change.
416+ */
417+ ret = hda_dsp_send_pm_gate_ipc (sdev , flags );
418+ if (ret < 0 ) {
378419 dev_err (sdev -> dev ,
379420 "error: PM_GATE ipc error %d\n" , ret );
421+ goto revert ;
422+ }
423+
424+ return ret ;
425+
426+ revert :
427+ /* fallback to the previous register value */
428+ value = value ? 0 : SOF_HDA_VS_D0I3C_I3 ;
429+
430+ /*
431+ * This can fail but return the IPC error to signal that
432+ * the state change failed.
433+ */
434+ hda_dsp_update_d0i3c_register (sdev , value );
380435
381436 return ret ;
382437}
383438
439+ /*
440+ * All DSP power state transitions are initiated by the driver.
441+ * If the requested state change fails, the error is simply returned.
442+ * Further state transitions are attempted only when the set_power_save() op
443+ * is called again either because of a new IPC sent to the DSP or
444+ * during system suspend/resume.
445+ */
446+ int hda_dsp_set_power_state (struct snd_sof_dev * sdev ,
447+ const struct sof_dsp_power_state * target_state )
448+ {
449+ int ret = 0 ;
450+
451+ /* Nothing to do if the DSP is already in the requested state */
452+ if (target_state -> state == sdev -> dsp_power_state .state &&
453+ target_state -> substate == sdev -> dsp_power_state .substate )
454+ return 0 ;
455+
456+ switch (target_state -> state ) {
457+ case SOF_DSP_PM_D0 :
458+ ret = hda_dsp_set_D0_state (sdev , target_state );
459+ break ;
460+ case SOF_DSP_PM_D3 :
461+ /* The only allowed transition is: D0I0 -> D3 */
462+ if (sdev -> dsp_power_state .state == SOF_DSP_PM_D0 &&
463+ sdev -> dsp_power_state .substate == SOF_HDA_DSP_PM_D0I0 )
464+ break ;
465+
466+ dev_err (sdev -> dev ,
467+ "error: transition from %d to %d not allowed\n" ,
468+ sdev -> dsp_power_state .state , target_state -> state );
469+ return - EINVAL ;
470+ default :
471+ dev_err (sdev -> dev , "error: target state unsupported %d\n" ,
472+ target_state -> state );
473+ return - EINVAL ;
474+ }
475+ if (ret < 0 ) {
476+ dev_err (sdev -> dev ,
477+ "failed to set requested target DSP state %d substate %d\n" ,
478+ target_state -> state , target_state -> substate );
479+ return ret ;
480+ }
481+
482+ sdev -> dsp_power_state = * target_state ;
483+ dev_dbg (sdev -> dev , "New DSP state %d substate %d\n" ,
484+ target_state -> state , target_state -> substate );
485+ return ret ;
486+ }
487+
488+ /*
489+ * Audio DSP states may transform as below:-
490+ *
491+ * D0I3 compatible stream
492+ * Runtime +---------------------+ opened only, timeout
493+ * suspend | +--------------------+
494+ * +------------+ D0(active) | |
495+ * | | <---------------+ |
496+ * | +--------> | | |
497+ * | |Runtime +--^--+---------^--+--+ The last | |
498+ * | |resume | | | | opened D0I3 | |
499+ * | | | | | | compatible | |
500+ * | | resume| | | | stream closed | |
501+ * | | from | | D3 | | | |
502+ * | | D3 | |suspend | | d0i3 | |
503+ * | | | | | |suspend | |
504+ * | | | | | | | |
505+ * | | | | | | | |
506+ * +-v---+-----------+--v-------+ | | +------+----v----+
507+ * | | | +-----------> |
508+ * | D3 (suspended) | | | D0I3 +-----+
509+ * | | +--------------+ | |
510+ * | | resume from | | |
511+ * +-------------------^--------+ d0i3 suspend +----------------+ |
512+ * | |
513+ * | D3 suspend |
514+ * +------------------------------------------------+
515+ *
516+ * d0i3_suspend = s0_suspend && D0I3 stream opened,
517+ * D3 suspend = !d0i3_suspend,
518+ */
519+
384520static int hda_suspend (struct snd_sof_dev * sdev , bool runtime_suspend )
385521{
386522 struct sof_intel_hda_dev * hda = sdev -> pdata -> hw_pdata ;
@@ -480,8 +616,22 @@ int hda_dsp_resume(struct snd_sof_dev *sdev)
480616{
481617 struct sof_intel_hda_dev * hda = sdev -> pdata -> hw_pdata ;
482618 struct pci_dev * pci = to_pci_dev (sdev -> dev );
619+ const struct sof_dsp_power_state target_state = {
620+ .state = SOF_DSP_PM_D0 ,
621+ .substate = SOF_HDA_DSP_PM_D0I0 ,
622+ };
623+ int ret ;
624+
625+ /* resume from D0I3 */
626+ if (sdev -> dsp_power_state .state == SOF_DSP_PM_D0 ) {
627+ /* Set DSP power state */
628+ ret = hda_dsp_set_power_state (sdev , & target_state );
629+ if (ret < 0 ) {
630+ dev_err (sdev -> dev , "error: setting dsp state %d substate %d\n" ,
631+ target_state .state , target_state .substate );
632+ return ret ;
633+ }
483634
484- if (sdev -> system_suspend_target == SOF_SUSPEND_S0IX ) {
485635 /* restore L1SEN bit */
486636 if (hda -> l1_support_changed )
487637 snd_sof_dsp_update_bits (sdev , HDA_DSP_HDA_BAR ,
@@ -495,13 +645,27 @@ int hda_dsp_resume(struct snd_sof_dev *sdev)
495645 }
496646
497647 /* init hda controller. DSP cores will be powered up during fw boot */
498- return hda_resume (sdev , false);
648+ ret = hda_resume (sdev , false);
649+ if (ret < 0 )
650+ return ret ;
651+
652+ hda_dsp_set_power_state (sdev , & target_state );
653+ return ret ;
499654}
500655
501656int hda_dsp_runtime_resume (struct snd_sof_dev * sdev )
502657{
658+ const struct sof_dsp_power_state target_state = {
659+ .state = SOF_DSP_PM_D0 ,
660+ };
661+ int ret ;
662+
503663 /* init hda controller. DSP cores will be powered up during fw boot */
504- return hda_resume (sdev , true);
664+ ret = hda_resume (sdev , true);
665+ if (ret < 0 )
666+ return ret ;
667+
668+ return hda_dsp_set_power_state (sdev , & target_state );
505669}
506670
507671int hda_dsp_runtime_idle (struct snd_sof_dev * sdev )
@@ -519,18 +683,41 @@ int hda_dsp_runtime_idle(struct snd_sof_dev *sdev)
519683
520684int hda_dsp_runtime_suspend (struct snd_sof_dev * sdev )
521685{
686+ const struct sof_dsp_power_state target_state = {
687+ .state = SOF_DSP_PM_D3 ,
688+ };
689+ int ret ;
690+
522691 /* stop hda controller and power dsp off */
523- return hda_suspend (sdev , true);
692+ ret = hda_suspend (sdev , true);
693+ if (ret < 0 )
694+ return ret ;
695+
696+ return hda_dsp_set_power_state (sdev , & target_state );
524697}
525698
526- int hda_dsp_suspend (struct snd_sof_dev * sdev )
699+ int hda_dsp_suspend (struct snd_sof_dev * sdev , u32 target_state )
527700{
528701 struct sof_intel_hda_dev * hda = sdev -> pdata -> hw_pdata ;
529702 struct hdac_bus * bus = sof_to_bus (sdev );
530703 struct pci_dev * pci = to_pci_dev (sdev -> dev );
704+ const struct sof_dsp_power_state target_dsp_state = {
705+ .state = target_state ,
706+ .substate = target_state == SOF_DSP_PM_D0 ?
707+ SOF_HDA_DSP_PM_D0I3 : 0 ,
708+ };
531709 int ret ;
532710
533- if (sdev -> system_suspend_target == SOF_SUSPEND_S0IX ) {
711+ if (target_state == SOF_DSP_PM_D0 ) {
712+ /* Set DSP power state */
713+ ret = hda_dsp_set_power_state (sdev , & target_dsp_state );
714+ if (ret < 0 ) {
715+ dev_err (sdev -> dev , "error: setting dsp state %d substate %d\n" ,
716+ target_dsp_state .state ,
717+ target_dsp_state .substate );
718+ return ret ;
719+ }
720+
534721 /* enable L1SEN to make sure the system can enter S0Ix */
535722 hda -> l1_support_changed =
536723 snd_sof_dsp_update_bits (sdev , HDA_DSP_HDA_BAR ,
@@ -551,7 +738,7 @@ int hda_dsp_suspend(struct snd_sof_dev *sdev)
551738 return ret ;
552739 }
553740
554- return 0 ;
741+ return hda_dsp_set_power_state ( sdev , & target_dsp_state ) ;
555742}
556743
557744int hda_dsp_set_hw_params_upon_resume (struct snd_sof_dev * sdev )
0 commit comments