Skip to content

SoundWire: fix pm_runtime on host and codec sides - #4345

Merged
plbossart merged 18 commits into
thesofproject:topic/sof-devfrom
plbossart:fix/sdw-runtime-pm
Jun 30, 2023
Merged

plbossart merged 18 commits into
thesofproject:topic/sof-devfrom
plbossart:fix/sdw-runtime-pm

Conversation

@plbossart

Copy link
Copy Markdown
Member

No description provided.

@greg-intel

Copy link
Copy Markdown

SOFCI TEST

1 similar comment
@aiChaoSONG

Copy link
Copy Markdown

SOFCI TEST

@plbossart
plbossart marked this pull request as ready for review May 10, 2023 14:45
@plbossart

Copy link
Copy Markdown
Member Author

@bardliao @RanderWang @ranj063 @ujfalusi can you take a look.

@bardliao

Copy link
Copy Markdown
Collaborator

@plbossart IMHO, the main issue is that the codec is not attached in startup. The runtime_pm of master devices are already enabled by sdw_master_device_add which will be called in probe.
In other words, sdw-master-0 is already active when intel_link_startup() is called.
And the second commit doesn't help on the issue that we start a playback before the codec first attached.
Because the Intel link device is not getting resume from intel_link_startup() or intel_resume().
snd_soc_pcm_component_pm_runtime_get() will get intel_link device and codec device in order. The master device will be active when we try to get the codec device since the master device is its parent. However, the codec will be attached after intel_link device is active, and it could be before the master device is active.

@plbossart

Copy link
Copy Markdown
Member Author

sdw-master-0 is already active when intel_link_startup() is called.

We don't control when the startup() happens @bardliao. While I agree with you that in the nominal case the sdw-master-N device is likely active in the probe, the parent isn't, and when we enable pm_runtime in the parent we should make sure the sdw-master remains active. It's not going to solve everything but it's a first step to make sure the states are consistent.

And the second commit doesn't help on the issue that we start a playback before the codec first attached. Because the Intel link device is not getting resume from intel_link_startup() or intel_resume(). snd_soc_pcm_component_pm_runtime_get() will get intel_link device and codec device in order. The master device will be active when we try to get the codec device since the master device is its parent. However, the codec will be attached after intel_link device is active, and it could be before the master device is active.

that's a different problem IMHO. The card should not be used until all codecs are attached, I agree with that, but I don't have a solution. we would need to add a deferred probe or something.

@plbossart
plbossart requested a review from naveen-manohar as a code owner May 11, 2023 20:10
@plbossart

plbossart commented May 11, 2023 •

Copy link
Copy Markdown
Member Author

@bardliao @shumingfan can you take a look at the last patches? I think my interpretation of pm_runtime is mostly correct

but the codecs complain with things I don't understand
[ 100.427531] kernel: rt715-sdca sdw:3:025d:0714:01: Failed to get private value: 2000000 => 0000 ret=-16
[ 100.427825] kernel: rt711-sdca sdw:0:025d:0711:01: Failed to get private value: 200001a => 0000 ret=-16
[ 100.427834] kernel: rt711-sdca sdw:0:025d:0711:01: Failed to get private value: 6100008 => 0000 ret=-16
[ 100.427843] kernel: rt711-sdca sdw:0:025d:0711:01: Failed to get private value: 2000000 => 0001 ret=-16
[ 100.427851] kernel: rt711-sdca sdw:0:025d:0711:01: Failed to get private value: 6100003 => ffff8de6 ret=-16
[ 100.427870] kernel: rt711-sdca sdw:0:025d:0711:01: Failed to get private value: 20000b0 => ffff8de6 ret=-16
[ 100.427933] kernel: rt711-sdca sdw:0:025d:0711:01: Failed to get private value: 5f00001 => ffff8de6 ret=-16
[ 100.438246] kernel: rt711-sdca sdw:0:025d:0711:01: rt711_sdca_io_init, calibration failed!
[ 100.451586] kernel: rt711-sdca sdw:0:025d:0711:01: Failed to get private value: 610003c => 0000 ret=-16
[ 100.451591] kernel: rt711-sdca sdw:0:025d:0711:01: Failed to get private value: 6100038 => 0000 ret=-16
[ 100.451595] kernel: rt711-sdca sdw:0:025d:0711:01: Failed to get private value: 2000008 => 0c60 ret=-16
I am not sure what those private values are.

Never mind, I forgot to restore the cache status in io_init(). Too much multitasking, works for me now.

@plbossart
plbossart force-pushed the fix/sdw-runtime-pm branch from 3d4d390 to 19a7c73 Compare May 11, 2023 21:51
@plbossart plbossart changed the title SoundWire: intel_auxdevice: fix sdw-master-N pm_runtime status SoundWire: fix pm_runtime on host and codec sides May 11, 2023
@plbossart
plbossart marked this pull request as draft May 11, 2023 21:57

@ranj063 ranj063 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @plbossart

@ranj063 ranj063 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @plbossart

ranj063
ranj063 previously approved these changes May 11, 2023

@ranj063 ranj063 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @plbossart

Comment thread sound/soc/codecs/max98363.c Resolved
bardliao
bardliao previously approved these changes May 12, 2023
@plbossart

Copy link
Copy Markdown
Member Author

@bardliao thanks for the review. Can you confirm that it solves the issue #4328 for you? I was unable to reproduce it even with hundreds of cycles, not sure what device causes this.

RanderWang
RanderWang previously approved these changes May 15, 2023
@bardliao

Copy link
Copy Markdown
Collaborator

@bardliao thanks for the review. Can you confirm that it solves the issue #4328 for you? I was unable to reproduce it even with hundreds of cycles, not sure what device causes this.

As far as I know, the only device model that ever met the issue is ADLP_RVP_SDW_IPC4ZPH. I didn't reproduce the issue with this PR so far. But I met IO error in rt711_jack_detect_handler, ret -16 issue twice.
dmesg_0515.txt

@plbossart

Copy link
Copy Markdown
Member Author

@bardliao thanks for the review. Can you confirm that it solves the issue #4328 for you? I was unable to reproduce it even with hundreds of cycles, not sure what device causes this.

As far as I know, the only device model that ever met the issue is ADLP_RVP_SDW_IPC4ZPH. I didn't reproduce the issue with this PR so far. But I met IO error in rt711_jack_detect_handler, ret -16 issue twice. dmesg_0515.txt

Thanks for testing @bardliao. I don't think this can be qualified as success if we get this 'IO error'. I am not sure what happens here, I really don't understand how the jack is handled. @shumingfan can you help with guidance/comments on what could cause this error?

BTW the pm-runtime errors are reported by an end-users on a Dell SDCA device, see issue #4353

@plbossart
plbossart dismissed stale reviews from RanderWang, bardliao, and ranj063 via 05840dc May 15, 2023 18:53
@plbossart

plbossart commented May 15, 2023 •

Copy link
Copy Markdown
Member Author

@bardliao the error -16 means -EBUSY, I suspect this happens while we try to read from a cache-only regmap, but I can't figure out how this can happen. I added a bunch of traces, can you retry?
I can also retry but I don't know which device you were using.

plbossart added 7 commits June 5, 2023 18:57
This patch suggests enabling pm_runtime during the probe, but marking the
device as 'active' only after it is enumerated. That will force a
dependency between the card and the codec, pm_runtime_get_sync() will
have to wait for the codec device to resume and hence implicitly wait
for the enumeration/initialization to be completed. In the nominal
case where the codec device is already active the get_sync() would
only perform a ref-count increase.

The changes are directly inspired by RT711 and RT711-sdca changes.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
This patch suggests enabling pm_runtime during the probe, but marking the
device as 'active' only after it is enumerated. That will force a
dependency between the card and the codec, pm_runtime_get_sync() will
have to wait for the codec device to resume and hence implicitly wait
for the enumeration/initialization to be completed. In the nominal
case where the codec device is already active the get_sync() would
only perform a ref-count increase.

The changes are directly inspired by RT711 and RT711-sdca changes.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
This patch suggests enabling pm_runtime during the probe, but marking the
device as 'active' only after it is enumerated. That will force a
dependency between the card and the codec, pm_runtime_get_sync() will
have to wait for the codec device to resume and hence implicitly wait
for the enumeration/initialization to be completed. In the nominal
case where the codec device is already active the get_sync() would
only perform a ref-count increase.

The changes are directly inspired by RT711 and RT711-sdca changes.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
This patch suggests enabling pm_runtime during the probe, but marking the
device as 'active' only after it is enumerated. That will force a
dependency between the card and the codec, pm_runtime_get_sync() will
have to wait for the codec device to resume and hence implicitly wait
for the enumeration/initialization to be completed. In the nominal
case where the codec device is already active the get_sync() would
only perform a ref-count increase.

The changes are directly inspired by RT711 and RT711-sdca changes.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
This patch suggests enabling pm_runtime during the probe, but marking the
device as 'active' only after it is enumerated. That will force a
dependency between the card and the codec, pm_runtime_get_sync() will
have to wait for the codec device to resume and hence implicitly wait
for the enumeration/initialization to be completed. In the nominal
case where the codec device is already active the get_sync() would
only perform a ref-count increase.

The changes are directly inspired by RT711 and RT711-sdca changes.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
This patch suggests enabling pm_runtime during the probe, but marking the
device as 'active' only after it is enumerated. That will force a
dependency between the card and the codec, pm_runtime_get_sync() will
have to wait for the codec device to resume and hence implicitly wait
for the enumeration/initialization to be completed. In the nominal
case where the codec device is already active the get_sync() would
only perform a ref-count increase.

The changes are directly inspired by RT711 and RT711-sdca changes.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
…nded'

This patch suggests enabling pm_runtime during the probe, but marking the
device as 'active' only after it is enumerated. That will force a
dependency between the card and the codec, pm_runtime_get_sync() will
have to wait for the codec device to resume and hence implicitly wait
for the enumeration/initialization to be completed. In the nominal
case where the codec device is already active the get_sync() would
only perform a ref-count increase.

The changes are directly inspired by RT711 and RT711-sdca changes.

Signed-off-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
@plbossart
plbossart force-pushed the fix/sdw-runtime-pm branch from 32dc825 to 741e069 Compare June 5, 2023 23:59
@plbossart
plbossart requested review from RanderWang and bardliao June 5, 2023 23:59
@plbossart

Copy link
Copy Markdown
Member Author

@charleskeepax @rfvirgil FYI, not sure if this applies you cs42l42 and other codec drivers you are working on.

@RanderWang RanderWang left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. A minor question.

Comment thread sound/soc/codecs/rt711.c
return ret;

/* set autosuspend parameters */
pm_runtime_set_autosuspend_delay(dev, 3000);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in SOF driver we set it to SND_SOF_SUSPEND_DELAY_MS = 2000. I don't know why it is 3000 ? And 3000 is used for all sdw codec can we use a macro definition to replace it ?

@charleskeepax

Copy link
Copy Markdown

This is what we were talking about a few weeks ago on the sync call. The cs42l43 driver does not call snd_soc_register_component until after the device has enumerated, although that is hard to achieve unless you are an MFD driver. The cs35l56 driver is waiting in component_probe for the device to enumerate, effectively blocking card registration until the device has enumerated. cs42l42 does probably still have the problem, as we haven't got around to fixing that one yet.

I can't say I totally follow all the pm_runtime gymnastics you guys are doing to work around the issues, but it would probably be a lot simpler and less prone to introducing new issues to simply delay registration of the card as we are doing. In general having a situation where user-space can talk to hardware that might not be available yet is just going to be a bottomless hole of issues.

@plbossart

Copy link
Copy Markdown
Member Author

I am not sure how your solution works @charleskeepax

The card will be as a result of the SOF driver creating a platform device. If the required components are not present, the deferred probe mechanism will kick in.

But the catch is that the probe will be retried only when other devices are registered (or drivers probed?). Registering an ASoC component later does not guarantee that the probe would be triggered.

That's a generic problem with deferred probes, it does not work in case of async registration of resources...What we would need is to register this component but explicitly force the deferred probe queue to be re-evaluated. Unfortunately GregKH didn't want my one-line solution in https://lore.kernel.org/alsa-devel/20210817190057.255264-1-pierre-louis.bossart@linux.intel.com/

@charleskeepax

charleskeepax commented Jun 6, 2023 •

Copy link
Copy Markdown

I am not sure how your solution works

I am assuming here you refer to the cs42l43 solution not the cs35l56, please correct me if that is wrong. Although I think the cs35l56 solution would probably work better for you guys since you really need an MFD for the cs42l43 solution, since you really need 2 struct devices.

The card will be as a result of the SOF driver creating a platform device. If the required components are not present, the deferred probe mechanism will kick in.

Sort of, probing of the ASoC components is a kinda weird 2 stage affair in its own right, the struct device for the card is a result of the platform device yes, but the sound card itself is created separately by ASoC. So the CODEC driver not being present will not actually cause the machine driver to probe defer. That ASoC level stuff just runs on each new component registration (snd_soc_register_component/card) and when all the bits of the card are ready it creates it, so the driver model is irrelevant there.

At least I thought that was how it worked got me doubting myself now.... no ok that is non-sense, apologies there.

The machine driver does probe defer, that is why you need the MFD for this solution to work. The deferred probes retrigger whenever a new driver binds, in the case of cs42l43 it is an MFD and we dont register the CODEC driver until the device has enumerated (mfd_add_devices) so when that driver is added that is what causes the probe defer list to be revisited.

@plbossart

Copy link
Copy Markdown
Member Author

The deferred probes retrigger whenever a new driver binds, in the case of cs42l43 it is an MFD and we dont register the CODEC driver until the device has enumerated (mfd_add_devices) so when that driver is added that is what causes the probe defer list to be revisited.

Right, but the probe defer list is only revisited when a new driver becoming attached, and not on the ASoC component being registered.

We are in agreement, your solution does not work for 'generic' codecs which don't register a new device :-)

@charleskeepax

Copy link
Copy Markdown

Yeah agreed there for the cs42l43 solution, but the cs35l56 solution of waiting for the enumeration in component_probe should work for non-MFD devices.

@plbossart

Copy link
Copy Markdown
Member Author

Yeah agreed there for the cs42l43 solution, but the cs35l56 solution of waiting for the enumeration in component_probe should work for non-MFD devices.

I don't know what you are referring to for cs35l56, I don't see the component_probe being used.

+static int cs35l56_sdw_probe(struct sdw_slave *peripheral, const struct sdw_device_id *id)
+{
+	struct device *dev = &peripheral->dev;
+	struct cs35l56_private *cs35l56;
+	int ret;
+
+	cs35l56 = devm_kzalloc(dev, sizeof(*cs35l56), GFP_KERNEL);
+	if (!cs35l56)
+		return -ENOMEM;
+
+	cs35l56->dev = dev;
+	cs35l56->sdw_peripheral = peripheral;
+	INIT_WORK(&cs35l56->sdw_irq_work, cs35l56_sdw_irq_work);
+
+	dev_set_drvdata(dev, cs35l56);
+
+	cs35l56->regmap = devm_regmap_init(dev, &cs35l56_regmap_bus_sdw,
+					   peripheral, &cs35l56_regmap_sdw);
+	if (IS_ERR(cs35l56->regmap)) {
+		ret = PTR_ERR(cs35l56->regmap);
+		return dev_err_probe(dev, ret, "Failed to allocate register map\n");
+	}
+
+	/* Start in cache-only until device is enumerated */
+	regcache_cache_only(cs35l56->regmap, true);
+
+	ret = cs35l56_common_probe(cs35l56);
+	if (ret != 0)
+		return ret;
+
+	return 0;
+}

@rfvirgil

rfvirgil commented Jun 6, 2023 via email

Copy link
Copy Markdown

@plbossart

Copy link
Copy Markdown
Member Author

Right, but this PR only tries to solve the problem of when to enable pm_runtime. That's different to the issue of waiting for the device to be initialized.

If pm_runtime is enabled when the hardware is attached, we do see error cases, and there is also the issue of the -EACCESS failing silently.

What this PR does is keep the device in 'suspended' mode until it's attached/initialized, and if you try to use the device before its initialized, the resume will wait for the attachment/initialization.

@charleskeepax

Copy link
Copy Markdown

Certainly yes you don't want to have cases where you have the device fully probed and the pm_runtime is not yet enabled. You would probably need to move the pm_runtime_enable either way, I haven't looked at the drivers in question in detail to be sure though. You could treat this as solving the pm_runtime issue and consider waiting for the device to initialise separately.
I guess my only real point was this seems to introduce some complexity around flags to track if things are the first resume and doing init stuff in resume feels odd. Whereas the alternative where everything is done in probe (and component_probe) feels more natural, and should solve the pm runtime stuff in the process (well maybe not the aux_device bits need to understand that a little more first). But none of these are our codecs so, not sure it matters too much.

@plbossart

Copy link
Copy Markdown
Member Author

@bardliao @ranj063 can I ask you to re-look at this PR? @yongzhi1 tells me it helps with a problem he encountered.

@bardliao bardliao left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @plbossart It looks good to me.

@plbossart

Copy link
Copy Markdown
Member Author

alright, time to merge this PR. That will give us 2-3 weeks of actual testing before it's send upstream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants