Skip to contentSkip to conversation

drm/panel: Add device link in drm_panel_attach()

PATCH Applied Jyri Sarha 6 participants 29 messages Apr 26, 2018 – Oct 22, 2019

Subsystems (4)
lore
Share & tools
lore

Jyri Sarha

author
#0 lore raw

v4 cover drm/panel: Add device link in drm_panel_attach()

I guess the first patch should be mergable. With the second, maybe it is better to wait until we have a full solution including the bridges too. What should I do to get the first patch merged?

The third version of these patches can be found here:
https://lists.freedesktop.org/archives/dri-devel/2018-April/174221.html

Changes since v3:
- "drm/panel: Remove drm_panel_detach() calls from all panel drives"
- Update kerneldoc for drm_panel_detach() and drm_panel_attach() according to Daniel Vetter's suugestion and add his reviewed-by.
- "drm/panel: Add device_link from panel device to drm device"
- none

The first patch could be squashed to second, but kept is separate since I think it is correct even without the second patch.

With these patches unbinding a panel driver in use does not cause nasty backtraces and corrupted drm core structures, but instead it cleanly unbinds the drm master device at the same time.

The only down side (currently[1]) is that the drm device does not reprobe if the panel driver is bound again, but everything should work if the drm master driver is bound manually.

Best regards,
Jyri

Jyri Sarha (2):
drm/panel: Remove drm_panel_detach() calls from all panel drives drm/panel: Add device_link from panel device to drm device

 drivers/gpu/drm/drm_panel.c                          | 16 ++++++++++++++++
 drivers/gpu/drm/panel/panel-innolux-p079zca.c        |  1 -
 drivers/gpu/drm/panel/panel-jdi-lt070me05000.c       |  1 -
 drivers/gpu/drm/panel/panel-lvds.c                   |  1 -
 drivers/gpu/drm/panel/panel-panasonic-vvx10f034n00.c |  1 -
 drivers/gpu/drm/panel/panel-seiko-43wvf1g.c          |  1 -
 drivers/gpu/drm/panel/panel-sharp-lq101r1sx01.c      |  1 -
 drivers/gpu/drm/panel/panel-sharp-ls043t1le01.c      |  1 -
 drivers/gpu/drm/panel/panel-simple.c                 |  1 -
 drivers/gpu/drm/panel/panel-sitronix-st7789v.c       |  1 -
 include/drm/drm_panel.h                              |  1 +
 11 files changed, 17 insertions(+), 9 deletions(-)
Signature · 6 lines
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author
#1 lore raw

PATCH v4 1/2 drm/panel: Remove drm_panel_detach() calls from all panel drives

Remove all drm_panel_detach() calls from all panel drives update the kernel doc for drm_panel_detach().

Setting the connector and drm to NULL when the drm panel device is going away hardly serves any purpose. Usually the the whole memory structure is freed right after the remove call. However, calling the detach function from the master drm device, and setting the connector pointer to NULL, has the logic of marking the panel again as available for another drm master to attach. The usual situation would be the same drm master device binding again.

---
 drivers/gpu/drm/drm_panel.c                          | 6 ++++++
 drivers/gpu/drm/panel/panel-innolux-p079zca.c        | 1 -
 drivers/gpu/drm/panel/panel-jdi-lt070me05000.c       | 1 -
 drivers/gpu/drm/panel/panel-lvds.c                   | 1 -
 drivers/gpu/drm/panel/panel-panasonic-vvx10f034n00.c | 1 -
 drivers/gpu/drm/panel/panel-seiko-43wvf1g.c          | 1 -
 drivers/gpu/drm/panel/panel-sharp-lq101r1sx01.c      | 1 -
 drivers/gpu/drm/panel/panel-sharp-ls043t1le01.c      | 1 -
 drivers/gpu/drm/panel/panel-simple.c                 | 1 -
 drivers/gpu/drm/panel/panel-sitronix-st7789v.c       | 1 -
 10 files changed, 6 insertions(+), 9 deletions(-)
drivers/gpu/drm/drm_panel.c +6 -0
index 308d442..71e4075 100644
@@ -94,6 +94,9 @@ EXPORT_SYMBOL(drm_panel_remove);
  *
  * An error is returned if the panel is already attached to another connector.
  *
+ * When unloading, the driver should detach from the panel by calling
+ * drm_panel_detach().
+ *
  * Return: 0 on success or a negative error code on failure.
  */
 int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
@@ -115,6 +118,9 @@ EXPORT_SYMBOL(drm_panel_attach);
  * Detaches a panel from the connector it is attached to. If a panel is not
  * attached to any connector this is effectively a no-op.
  *
+ * This function should not be called by the panel device itself. It
+ * is only for the drm device that called drm_panel_attach().
+ *
  * Return: 0 on success or a negative error code on failure.
  */
 int drm_panel_detach(struct drm_panel *panel)
drivers/gpu/drm/panel/panel-innolux-p079zca.c +0 -1
index 57df39b..bb53e08 100644
@@ -292,7 +292,6 @@ static int innolux_panel_remove(struct mipi_dsi_device *dsi)
 		DRM_DEV_ERROR(&dsi->dev, "failed to detach from DSI host: %d\n",
 			      err);
 
-	drm_panel_detach(&innolux->base);
 	innolux_panel_del(innolux);
 
 	return 0;
drivers/gpu/drm/panel/panel-jdi-lt070me05000.c +0 -1
index 0a94ab7..99caa78 100644
@@ -500,7 +500,6 @@ static int jdi_panel_remove(struct mipi_dsi_device *dsi)
 		dev_err(&dsi->dev, "failed to detach from DSI host: %d\n",
 			ret);
 
-	drm_panel_detach(&jdi->base);
 	jdi_panel_del(jdi);
 
 	return 0;
drivers/gpu/drm/panel/panel-lvds.c +0 -1
index 5185819..8a16878 100644
@@ -282,7 +282,6 @@ static int panel_lvds_remove(struct platform_device *pdev)
 {
 	struct panel_lvds *lvds = dev_get_drvdata(&pdev->dev);
 
-	drm_panel_detach(&lvds->panel);
 	drm_panel_remove(&lvds->panel);
 
 	panel_lvds_disable(&lvds->panel);
drivers/gpu/drm/panel/panel-panasonic-vvx10f034n00.c +0 -1
index 74a8061..cb4dfb9 100644
@@ -299,7 +299,6 @@ static int wuxga_nt_panel_remove(struct mipi_dsi_device *dsi)
 	if (ret < 0)
 		dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
 
-	drm_panel_detach(&wuxga_nt->base);
 	wuxga_nt_panel_del(wuxga_nt);
 
 	return 0;
drivers/gpu/drm/panel/panel-seiko-43wvf1g.c +0 -1
index 71c09ed..75f9253 100644
@@ -292,7 +292,6 @@ static int seiko_panel_remove(struct platform_device *pdev)
 {
 	struct seiko_panel *panel = dev_get_drvdata(&pdev->dev);
 
-	drm_panel_detach(&panel->base);
 	drm_panel_remove(&panel->base);
 
 	seiko_panel_disable(&panel->base);
drivers/gpu/drm/panel/panel-sharp-lq101r1sx01.c +0 -1
index 6bf8730..02fc0f5 100644
@@ -418,7 +418,6 @@ static int sharp_panel_remove(struct mipi_dsi_device *dsi)
 	if (err < 0)
 		dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
 
-	drm_panel_detach(&sharp->base);
 	sharp_panel_del(sharp);
 
 	return 0;
drivers/gpu/drm/panel/panel-sharp-ls043t1le01.c +0 -1
index 494aa9b..e5cae00 100644
@@ -327,7 +327,6 @@ static int sharp_nt_panel_remove(struct mipi_dsi_device *dsi)
 	if (ret < 0)
 		dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret);
 
-	drm_panel_detach(&sharp_nt->base);
 	sharp_nt_panel_del(sharp_nt);
 
 	return 0;
drivers/gpu/drm/panel/panel-simple.c +0 -1
index f2d96611..9388acf 100644
@@ -364,7 +364,6 @@ static int panel_simple_remove(struct device *dev)
 {
 	struct panel_simple *panel = dev_get_drvdata(dev);
 
-	drm_panel_detach(&panel->base);
 	drm_panel_remove(&panel->base);
 
 	panel_simple_disable(&panel->base);
drivers/gpu/drm/panel/panel-sitronix-st7789v.c +0 -1
index 358c64e..74284e5 100644
@@ -419,7 +419,6 @@ static int st7789v_remove(struct spi_device *spi)
 {
 	struct st7789v *ctx = spi_get_drvdata(spi);
 
-	drm_panel_detach(&ctx->panel);
 	drm_panel_remove(&ctx->panel);
 
 	if (ctx->backlight)
Signature · 6 lines
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author
#2 lore raw

PATCH v4 2/2 drm/panel: Add device_link from panel device to drm device

In reply to Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

---
 drivers/gpu/drm/drm_panel.c | 10 ++++++++++
 include/drm/drm_panel.h     |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/drm_panel.c +10 -0
index 71e4075..965530a 100644
@@ -24,6 +24,7 @@
 #include <linux/err.h>
 #include <linux/module.h>
 
+#include <drm/drm_device.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_panel.h>
 
@@ -104,6 +105,13 @@ int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
 	if (panel->connector)
 		return -EBUSY;
 
+	panel->link = device_link_add(connector->dev->dev, panel->dev, 0);
+	if (!panel->link) {
+		dev_err(panel->dev, "failed to link panel to %s\n",
+			dev_name(connector->dev->dev));
+		return -EINVAL;
+	}
+
 	panel->connector = connector;
 	panel->drm = connector->dev;
 
@@ -125,6 +133,8 @@ EXPORT_SYMBOL(drm_panel_attach);
  */
 int drm_panel_detach(struct drm_panel *panel)
 {
+	device_link_del(panel->link);
+
 	panel->connector = NULL;
 	panel->drm = NULL;
 
include/drm/drm_panel.h +1 -0
index 14ac240..26a1b5f 100644
@@ -89,6 +89,7 @@ struct drm_panel {
 	struct drm_device *drm;
 	struct drm_connector *connector;
 	struct device *dev;
+	struct device_link *link;
 
 	const struct drm_panel_funcs *funcs;
 
Signature · 6 lines
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#3 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.
The trick in case of exynos-dsi is to use mipi_dsi attach/detach callbacks to get notifications about drm_panel's appearance/disappearance, I guess ultimately such callbacks should be a part of drm_panel
framework.

Below description how all the code works:
Initialization:
1. exynos_dsi (component of exynos_drm) at the end of bind callback registers mipi_dsi bus.
2. during mipi_dsi bus registration or later (if panel driver was not yet registered), panel driver is bound ie - its probe callback is called.
3. during panel's probe drm_panel_add is called, and later mipi_dsi_attach.
4. mipi_dsi_attach calls dsi .attach callback of exynos_dsi.
5. exynos_dsi.attach callback looks for drm_panel, calls drm_panel_attach and changes connector's status to connected.
....
Panel's unbind:
1. device_release_driver calls panel's remove callback.
2. panel's remove callback calls mipi_dsi_detach.
3. mipi_dsi_detach calls exynos_dsi.detach callback.
4. exynos_dsi.detach calls drm_panel_detach and changes connector's state to disconnected.

Panel's rebind is equivalent of steps 2-4 of Initialization.

I have wrote the code few years ago, and it seemed to me the only safe way to handle dynamic panel bind/unbind.
For example please note that step 5 of initialization and steps 2-4 of unbind are executed always under panel's device_lock so it makes it safe. Otherwise you risk that between panel lookup and panel attach panel's driver can be unbind and consumer is left with dangling panel's pointer, so even with your patch most of the drivers are still buggy.

And few words about the bug(?) in device_link code: If you call device_link_add from probe of the supplier(as it is in this case) its status is set to DL_STATE_DORMANT, and after successful bind it is changed to DL_STATE_AVAILABLE, which is incorrect (it should be DL_STATE_ACTIVE), as a result on supplier's unbind consumer is not unbound.

Regards
Andrzej

Quoted 45 lines
---
 drivers/gpu/drm/drm_panel.c | 10 ++++++++++
 include/drm/drm_panel.h     |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/drm_panel.c +10 -0
index 71e4075..965530a 100644
@@ -24,6 +24,7 @@
 #include <linux/err.h>
 #include <linux/module.h>
 
+#include <drm/drm_device.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_panel.h>
 
@@ -104,6 +105,13 @@ int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
 	if (panel->connector)
 		return -EBUSY;
 
+	panel->link = device_link_add(connector->dev->dev, panel->dev, 0);
+	if (!panel->link) {
+		dev_err(panel->dev, "failed to link panel to %s\n",
+			dev_name(connector->dev->dev));
+		return -EINVAL;
+	}
+
 	panel->connector = connector;
 	panel->drm = connector->dev;
 
@@ -125,6 +133,8 @@ EXPORT_SYMBOL(drm_panel_attach);
  */
 int drm_panel_detach(struct drm_panel *panel)
 {
+	device_link_del(panel->link);
+
 	panel->connector = NULL;
 	panel->drm = NULL;
 
include/drm/drm_panel.h +0 -0
index 14ac240..26a1b5f 100644

include/drm/drm_panel.h struct drm_panel { · line 95

Quoted 7 lines
include/drm/drm_panel.h +1 -0
@@ -89,6 +89,7 @@ struct drm_panel {
 	struct drm_device *drm;
 	struct drm_connector *connector;
 	struct device *dev;
+	struct device_link *link;
 
 	const struct drm_panel_funcs *funcs;
 

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

author
#4 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 25 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 3 lines

Making device_links for panels optional seems to me the best solution, what do you think?

Sounds good to me. Unfortunately I have no time to spend on this in the coming two weeks. If you can come up with a fix, please send patch and put me in cc.

I am also fine with reverting the patch.

Quoted 9 lines

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.
The trick in case of exynos-dsi is to use mipi_dsi attach/detach callbacks to get notifications about drm_panel's appearance/disappearance, I guess ultimately such callbacks should be a part of drm_panel
framework.

That sounds like an ultimate solution, especially if there is a default behaviour that unbinds the master drm device as the most devices do not need anything else.

Best regards,
Jyri

Quoted 91 lines

Below description how all the code works:
Initialization:
1. exynos_dsi (component of exynos_drm) at the end of bind callback registers mipi_dsi bus.
2. during mipi_dsi bus registration or later (if panel driver was not yet registered), panel driver is bound ie - its probe callback is called.
3. during panel's probe drm_panel_add is called, and later mipi_dsi_attach.
4. mipi_dsi_attach calls dsi .attach callback of exynos_dsi.
5. exynos_dsi.attach callback looks for drm_panel, calls drm_panel_attach and changes connector's status to connected.
....
Panel's unbind:
1. device_release_driver calls panel's remove callback.
2. panel's remove callback calls mipi_dsi_detach.
3. mipi_dsi_detach calls exynos_dsi.detach callback.
4. exynos_dsi.detach calls drm_panel_detach and changes connector's state to disconnected.

Panel's rebind is equivalent of steps 2-4 of Initialization.

I have wrote the code few years ago, and it seemed to me the only safe way to handle dynamic panel bind/unbind.
For example please note that step 5 of initialization and steps 2-4 of unbind are executed always under panel's device_lock so it makes it safe. Otherwise you risk that between panel lookup and panel attach panel's driver can be unbind and consumer is left with dangling panel's pointer, so even with your patch most of the drivers are still buggy.

And few words about the bug(?) in device_link code: If you call device_link_add from probe of the supplier(as it is in this case) its status is set to DL_STATE_DORMANT, and after successful bind it is changed to DL_STATE_AVAILABLE, which is incorrect (it should be DL_STATE_ACTIVE), as a result on supplier's unbind consumer is not unbound.

Regards
Andrzej

Quoted 52 lines
---
 drivers/gpu/drm/drm_panel.c | 10 ++++++++++
 include/drm/drm_panel.h     |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/drm_panel.c +10 -0
index 71e4075..965530a 100644
@@ -24,6 +24,7 @@
 #include <linux/err.h>
 #include <linux/module.h>
 
+#include <drm/drm_device.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_panel.h>
 
@@ -104,6 +105,13 @@ int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
 	if (panel->connector)
 		return -EBUSY;
 
+	panel->link = device_link_add(connector->dev->dev, panel->dev, 0);
+	if (!panel->link) {
+		dev_err(panel->dev, "failed to link panel to %s\n",
+			dev_name(connector->dev->dev));
+		return -EINVAL;
+	}
+
 	panel->connector = connector;
 	panel->drm = connector->dev;
 
@@ -125,6 +133,8 @@ EXPORT_SYMBOL(drm_panel_attach);
  */
 int drm_panel_detach(struct drm_panel *panel)
 {
+	device_link_del(panel->link);
+
 	panel->connector = NULL;
 	panel->drm = NULL;
 
include/drm/drm_panel.h +1 -0
index 14ac240..26a1b5f 100644
@@ -89,6 +89,7 @@ struct drm_panel {
 	struct drm_device *drm;
 	struct drm_connector *connector;
 	struct device *dev;
+	struct device_link *link;
 
 	const struct drm_panel_funcs *funcs;
 
Signature · 6 lines
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki.
Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

In reply to Andrzej Hajda
Quoted 25 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

The function device_is_bound seems like a good candidate for that check?

Maybe device_link_add should automatically create a DL_FLAG_STATELESS link if either consumer or supplier has "proven" (with a successful probe) that they can exist without the other end? That would also work for us, since a DL_FLAG_STATELESS link does no harm in our case...

Cheers,
Peter

Quoted 93 lines

The trick in case of exynos-dsi is to use mipi_dsi attach/detach callbacks to get notifications about drm_panel's appearance/disappearance, I guess ultimately such callbacks should be a part of drm_panel
framework.

Below description how all the code works:
Initialization:
1. exynos_dsi (component of exynos_drm) at the end of bind callback registers mipi_dsi bus.
2. during mipi_dsi bus registration or later (if panel driver was not yet registered), panel driver is bound ie - its probe callback is called.
3. during panel's probe drm_panel_add is called, and later mipi_dsi_attach.
4. mipi_dsi_attach calls dsi .attach callback of exynos_dsi.
5. exynos_dsi.attach callback looks for drm_panel, calls drm_panel_attach and changes connector's status to connected.
....
Panel's unbind:
1. device_release_driver calls panel's remove callback.
2. panel's remove callback calls mipi_dsi_detach.
3. mipi_dsi_detach calls exynos_dsi.detach callback.
4. exynos_dsi.detach calls drm_panel_detach and changes connector's state to disconnected.

Panel's rebind is equivalent of steps 2-4 of Initialization.

I have wrote the code few years ago, and it seemed to me the only safe way to handle dynamic panel bind/unbind.
For example please note that step 5 of initialization and steps 2-4 of unbind are executed always under panel's device_lock so it makes it safe. Otherwise you risk that between panel lookup and panel attach panel's driver can be unbind and consumer is left with dangling panel's pointer, so even with your patch most of the drivers are still buggy.

And few words about the bug(?) in device_link code: If you call device_link_add from probe of the supplier(as it is in this case) its status is set to DL_STATE_DORMANT, and after successful bind it is changed to DL_STATE_AVAILABLE, which is incorrect (it should be DL_STATE_ACTIVE), as a result on supplier's unbind consumer is not unbound.

Regards
Andrzej

Quoted 50 lines
---
 drivers/gpu/drm/drm_panel.c | 10 ++++++++++
 include/drm/drm_panel.h     |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/drm_panel.c +10 -0
index 71e4075..965530a 100644
@@ -24,6 +24,7 @@
 #include <linux/err.h>
 #include <linux/module.h>
 
+#include <drm/drm_device.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_panel.h>
 
@@ -104,6 +105,13 @@ int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
 	if (panel->connector)
 		return -EBUSY;
 
+	panel->link = device_link_add(connector->dev->dev, panel->dev, 0);
+	if (!panel->link) {
+		dev_err(panel->dev, "failed to link panel to %s\n",
+			dev_name(connector->dev->dev));
+		return -EINVAL;
+	}
+
 	panel->connector = connector;
 	panel->drm = connector->dev;
 
@@ -125,6 +133,8 @@ EXPORT_SYMBOL(drm_panel_attach);
  */
 int drm_panel_detach(struct drm_panel *panel)
 {
+	device_link_del(panel->link);
+
 	panel->connector = NULL;
 	panel->drm = NULL;
 
include/drm/drm_panel.h +1 -0
index 14ac240..26a1b5f 100644
@@ -89,6 +89,7 @@ struct drm_panel {
 	struct drm_device *drm;
 	struct drm_connector *connector;
 	struct device *dev;
+	struct device_link *link;
 
Quoted 4 lines

include/drm/drm_panel.h struct drm_panel { · line 95

Quoted 2 lines
include/drm/drm_panel.h +0 -0
 	const struct drm_panel_funcs *funcs;
 

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 56 lines · Peter Rosin
Quoted 25 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Quoted 5 lines

The function device_is_bound seems like a good candidate for that check?

Maybe device_link_add should automatically create a DL_FLAG_STATELESS link if either consumer or supplier has "proven" (with a successful probe) that

s/either consumer or supplier/the consumer/

The supplier should always be able to probe w/o the consumer being present.

Quoted 104 lines

they can exist without the other end? That would also work for us, since a DL_FLAG_STATELESS link does no harm in our case...

Cheers,
Peter

Quoted 97 lines

The trick in case of exynos-dsi is to use mipi_dsi attach/detach callbacks to get notifications about drm_panel's appearance/disappearance, I guess ultimately such callbacks should be a part of drm_panel
framework.

Below description how all the code works:
Initialization:
1. exynos_dsi (component of exynos_drm) at the end of bind callback registers mipi_dsi bus.
2. during mipi_dsi bus registration or later (if panel driver was not yet registered), panel driver is bound ie - its probe callback is called.
3. during panel's probe drm_panel_add is called, and later mipi_dsi_attach.
4. mipi_dsi_attach calls dsi .attach callback of exynos_dsi.
5. exynos_dsi.attach callback looks for drm_panel, calls drm_panel_attach and changes connector's status to connected.
....
Panel's unbind:
1. device_release_driver calls panel's remove callback.
2. panel's remove callback calls mipi_dsi_detach.
3. mipi_dsi_detach calls exynos_dsi.detach callback.
4. exynos_dsi.detach calls drm_panel_detach and changes connector's state to disconnected.

Panel's rebind is equivalent of steps 2-4 of Initialization.

I have wrote the code few years ago, and it seemed to me the only safe way to handle dynamic panel bind/unbind.
For example please note that step 5 of initialization and steps 2-4 of unbind are executed always under panel's device_lock so it makes it safe. Otherwise you risk that between panel lookup and panel attach panel's driver can be unbind and consumer is left with dangling panel's pointer, so even with your patch most of the drivers are still buggy.

And few words about the bug(?) in device_link code: If you call device_link_add from probe of the supplier(as it is in this case) its status is set to DL_STATE_DORMANT, and after successful bind it is changed to DL_STATE_AVAILABLE, which is incorrect (it should be DL_STATE_ACTIVE), as a result on supplier's unbind consumer is not unbound.

Regards
Andrzej

Quoted 52 lines
---
 drivers/gpu/drm/drm_panel.c | 10 ++++++++++
 include/drm/drm_panel.h     |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/drm_panel.c +10 -0
index 71e4075..965530a 100644
@@ -24,6 +24,7 @@
 #include <linux/err.h>
 #include <linux/module.h>
 
+#include <drm/drm_device.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_panel.h>
 
@@ -104,6 +105,13 @@ int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
 	if (panel->connector)
 		return -EBUSY;
 
+	panel->link = device_link_add(connector->dev->dev, panel->dev, 0);
+	if (!panel->link) {
+		dev_err(panel->dev, "failed to link panel to %s\n",
+			dev_name(connector->dev->dev));
+		return -EINVAL;
+	}
+
 	panel->connector = connector;
 	panel->drm = connector->dev;
 
@@ -125,6 +133,8 @@ EXPORT_SYMBOL(drm_panel_attach);
  */
 int drm_panel_detach(struct drm_panel *panel)
 {
+	device_link_del(panel->link);
+
 	panel->connector = NULL;
 	panel->drm = NULL;
 
include/drm/drm_panel.h +1 -0
index 14ac240..26a1b5f 100644
@@ -89,6 +89,7 @@ struct drm_panel {
 	struct drm_device *drm;
 	struct drm_connector *connector;
 	struct device *dev;
+	struct device_link *link;
 
 	const struct drm_panel_funcs *funcs;
 

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#7 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

In reply to Peter Rosin
Quoted 45 lines · Peter Rosin
Quoted 24 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

Quoted 4 lines

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

And this is also incorrect from Documentation PoV:
 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.

Quoted 4 lines

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Making it automatic looks tempting, but also error prone. In case of component framework bind callbacks can be called from probe of any component, so sometimes it can be called also from exynos-dsi probe, sometimes not (depending on order of probing, which we cannot rely on). So in some cases we will end-up with links, sometimes without. Ie following scenarios are possible in drm_panel_attach:
1. exynos-dsi bound, panel during probe.
2. exynos-dsi during probe, panel during probe.

Quoted 8 lines

The function device_is_bound seems like a good candidate for that check?

Maybe device_link_add should automatically create a DL_FLAG_STATELESS link if either consumer or supplier has "proven" (with a successful probe) that they can exist without the other end? That would also work for us, since a DL_FLAG_STATELESS link does no harm in our case...

What will be benefit of having stateless device_links, in case of panels/bridges?

Regards
Andrzej

Quoted 103 lines

Cheers,
Peter

Quoted 96 lines

The trick in case of exynos-dsi is to use mipi_dsi attach/detach callbacks to get notifications about drm_panel's appearance/disappearance, I guess ultimately such callbacks should be a part of drm_panel
framework.

Below description how all the code works:
Initialization:
1. exynos_dsi (component of exynos_drm) at the end of bind callback registers mipi_dsi bus.
2. during mipi_dsi bus registration or later (if panel driver was not yet registered), panel driver is bound ie - its probe callback is called.
3. during panel's probe drm_panel_add is called, and later mipi_dsi_attach.
4. mipi_dsi_attach calls dsi .attach callback of exynos_dsi.
5. exynos_dsi.attach callback looks for drm_panel, calls drm_panel_attach and changes connector's status to connected.
....
Panel's unbind:
1. device_release_driver calls panel's remove callback.
2. panel's remove callback calls mipi_dsi_detach.
3. mipi_dsi_detach calls exynos_dsi.detach callback.
4. exynos_dsi.detach calls drm_panel_detach and changes connector's state to disconnected.

Panel's rebind is equivalent of steps 2-4 of Initialization.

I have wrote the code few years ago, and it seemed to me the only safe way to handle dynamic panel bind/unbind.
For example please note that step 5 of initialization and steps 2-4 of unbind are executed always under panel's device_lock so it makes it safe. Otherwise you risk that between panel lookup and panel attach panel's driver can be unbind and consumer is left with dangling panel's pointer, so even with your patch most of the drivers are still buggy.

And few words about the bug(?) in device_link code: If you call device_link_add from probe of the supplier(as it is in this case) its status is set to DL_STATE_DORMANT, and after successful bind it is changed to DL_STATE_AVAILABLE, which is incorrect (it should be DL_STATE_ACTIVE), as a result on supplier's unbind consumer is not unbound.

Regards
Andrzej

Quoted 52 lines
---
 drivers/gpu/drm/drm_panel.c | 10 ++++++++++
 include/drm/drm_panel.h     |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/drm_panel.c +10 -0
index 71e4075..965530a 100644
@@ -24,6 +24,7 @@
 #include <linux/err.h>
 #include <linux/module.h>
 
+#include <drm/drm_device.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_panel.h>
 
@@ -104,6 +105,13 @@ int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
 	if (panel->connector)
 		return -EBUSY;
 
+	panel->link = device_link_add(connector->dev->dev, panel->dev, 0);
+	if (!panel->link) {
+		dev_err(panel->dev, "failed to link panel to %s\n",
+			dev_name(connector->dev->dev));
+		return -EINVAL;
+	}
+
 	panel->connector = connector;
 	panel->drm = connector->dev;
 
@@ -125,6 +133,8 @@ EXPORT_SYMBOL(drm_panel_attach);
  */
 int drm_panel_detach(struct drm_panel *panel)
 {
+	device_link_del(panel->link);
+
 	panel->connector = NULL;
 	panel->drm = NULL;
 
include/drm/drm_panel.h +1 -0
index 14ac240..26a1b5f 100644
@@ -89,6 +89,7 @@ struct drm_panel {
 	struct drm_device *drm;
 	struct drm_connector *connector;
 	struct device *dev;
+	struct device_link *link;
 
 	const struct drm_panel_funcs *funcs;
 

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#8 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 61 lines · Andrzej Hajda
Quoted 45 lines · Peter Rosin
Quoted 24 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

Quoted 4 lines

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

And this is also incorrect from Documentation PoV:
 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.

Quoted 4 lines

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Quoted 8 lines

Making it automatic looks tempting, but also error prone. In case of component framework bind callbacks can be called from probe of any component, so sometimes it can be called also from exynos-dsi probe, sometimes not (depending on order of probing, which we cannot rely on). So in some cases we will end-up with links, sometimes without. Ie following scenarios are possible in drm_panel_attach:
1. exynos-dsi bound, panel during probe.
2. exynos-dsi during probe, panel during probe.

2. exynos-dsi during probe, panel bound? Or is this case 3, and 2 happens when drivers probe in parallel?

Whichever, you are right, I naively thought that the bind happened after the probe of all devices, but naturally it happens as part of the last device to register its component, and that normally happens during its probe.

Sigh. So, scratch that, I guess we need a flag...

Quoted 5 lines
Quoted 5 lines

The function device_is_bound seems like a good candidate for that check?

Quoted 7 lines
Quoted 4 lines

Maybe device_link_add should automatically create a DL_FLAG_STATELESS link if either consumer or supplier has "proven" (with a successful probe) that they can exist without the other end? That would also work for us, since a DL_FLAG_STATELESS link does no harm in our case...

What will be benefit of having stateless device_links, in case of panels/bridges?

To have device suspend/resume happen in a predictable order? But maybe that has no value whatsoever?

Cheers,
Peter

Quoted 108 lines

Regards
Andrzej

Quoted 103 lines

Cheers,
Peter

Quoted 96 lines

The trick in case of exynos-dsi is to use mipi_dsi attach/detach callbacks to get notifications about drm_panel's appearance/disappearance, I guess ultimately such callbacks should be a part of drm_panel
framework.

Below description how all the code works:
Initialization:
1. exynos_dsi (component of exynos_drm) at the end of bind callback registers mipi_dsi bus.
2. during mipi_dsi bus registration or later (if panel driver was not yet registered), panel driver is bound ie - its probe callback is called.
3. during panel's probe drm_panel_add is called, and later mipi_dsi_attach.
4. mipi_dsi_attach calls dsi .attach callback of exynos_dsi.
5. exynos_dsi.attach callback looks for drm_panel, calls drm_panel_attach and changes connector's status to connected.
....
Panel's unbind:
1. device_release_driver calls panel's remove callback.
2. panel's remove callback calls mipi_dsi_detach.
3. mipi_dsi_detach calls exynos_dsi.detach callback.
4. exynos_dsi.detach calls drm_panel_detach and changes connector's state to disconnected.

Panel's rebind is equivalent of steps 2-4 of Initialization.

I have wrote the code few years ago, and it seemed to me the only safe way to handle dynamic panel bind/unbind.
For example please note that step 5 of initialization and steps 2-4 of unbind are executed always under panel's device_lock so it makes it safe. Otherwise you risk that between panel lookup and panel attach panel's driver can be unbind and consumer is left with dangling panel's pointer, so even with your patch most of the drivers are still buggy.

And few words about the bug(?) in device_link code: If you call device_link_add from probe of the supplier(as it is in this case) its status is set to DL_STATE_DORMANT, and after successful bind it is changed to DL_STATE_AVAILABLE, which is incorrect (it should be DL_STATE_ACTIVE), as a result on supplier's unbind consumer is not unbound.

Regards
Andrzej

Quoted 52 lines
---
 drivers/gpu/drm/drm_panel.c | 10 ++++++++++
 include/drm/drm_panel.h     |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/drm_panel.c +10 -0
index 71e4075..965530a 100644
@@ -24,6 +24,7 @@
 #include <linux/err.h>
 #include <linux/module.h>
 
+#include <drm/drm_device.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_panel.h>
 
@@ -104,6 +105,13 @@ int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
 	if (panel->connector)
 		return -EBUSY;
 
+	panel->link = device_link_add(connector->dev->dev, panel->dev, 0);
+	if (!panel->link) {
+		dev_err(panel->dev, "failed to link panel to %s\n",
+			dev_name(connector->dev->dev));
+		return -EINVAL;
+	}
+
 	panel->connector = connector;
 	panel->drm = connector->dev;
 
@@ -125,6 +133,8 @@ EXPORT_SYMBOL(drm_panel_attach);
  */
 int drm_panel_detach(struct drm_panel *panel)
 {
+	device_link_del(panel->link);
+
 	panel->connector = NULL;
 	panel->drm = NULL;
 
include/drm/drm_panel.h +1 -0
index 14ac240..26a1b5f 100644
@@ -89,6 +89,7 @@ struct drm_panel {
 	struct drm_device *drm;
 	struct drm_connector *connector;
 	struct device *dev;
+	struct device_link *link;
 
 	const struct drm_panel_funcs *funcs;
 

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#9 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 67 lines · Peter Rosin
Quoted 58 lines · Andrzej Hajda
Quoted 45 lines · Peter Rosin
Quoted 24 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

Quoted 4 lines

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

And this is also incorrect from Documentation PoV:
 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.

Quoted 3 lines

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Quoted 8 lines

Making it automatic looks tempting, but also error prone. In case of component framework bind callbacks can be called from probe of any component, so sometimes it can be called also from exynos-dsi probe, sometimes not (depending on order of probing, which we cannot rely on). So in some cases we will end-up with links, sometimes without. Ie following scenarios are possible in drm_panel_attach:
1. exynos-dsi bound, panel during probe.
2. exynos-dsi during probe, panel during probe.

Quoted 2 lines

2. exynos-dsi during probe, panel bound? Or is this case 3, and 2 happens when drivers probe in parallel?

Panel is always probed not earlier than the end of exynos-dsi bind, so only scenarios 1 and 2 should be possible.

Quoted 10 lines

Whichever, you are right, I naively thought that the bind happened after the probe of all devices, but naturally it happens as part of the last device to register its component, and that normally happens during its probe.

Sigh. So, scratch that, I guess we need a flag...

Quoted 3 lines
Quoted 3 lines

The function device_is_bound seems like a good candidate for that check?

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Maybe device_link_add should automatically create a DL_FLAG_STATELESS link if either consumer or supplier has "proven" (with a successful probe) that they can exist without the other end? That would also work for us, since a DL_FLAG_STATELESS link does no harm in our case...

What will be benefit of having stateless device_links, in case of panels/bridges?

To have device suspend/resume happen in a predictable order? But maybe that has no value whatsoever?

At the moment panels do not have linux pm code, so I guess their pm is done entirely by drm framework.
I am not sure about bridges.

Regards
Andrzej

Quoted 111 lines

Cheers,
Peter

Quoted 104 lines

Regards
Andrzej

Quoted 100 lines

Cheers,
Peter

Quoted 95 lines

The trick in case of exynos-dsi is to use mipi_dsi attach/detach callbacks to get notifications about drm_panel's appearance/disappearance, I guess ultimately such callbacks should be a part of drm_panel
framework.

Below description how all the code works:
Initialization:
1. exynos_dsi (component of exynos_drm) at the end of bind callback registers mipi_dsi bus.
2. during mipi_dsi bus registration or later (if panel driver was not yet registered), panel driver is bound ie - its probe callback is called.
3. during panel's probe drm_panel_add is called, and later mipi_dsi_attach.
4. mipi_dsi_attach calls dsi .attach callback of exynos_dsi.
5. exynos_dsi.attach callback looks for drm_panel, calls drm_panel_attach and changes connector's status to connected.
....
Panel's unbind:
1. device_release_driver calls panel's remove callback.
2. panel's remove callback calls mipi_dsi_detach.
3. mipi_dsi_detach calls exynos_dsi.detach callback.
4. exynos_dsi.detach calls drm_panel_detach and changes connector's state to disconnected.

Panel's rebind is equivalent of steps 2-4 of Initialization.

I have wrote the code few years ago, and it seemed to me the only safe way to handle dynamic panel bind/unbind.
For example please note that step 5 of initialization and steps 2-4 of unbind are executed always under panel's device_lock so it makes it safe. Otherwise you risk that between panel lookup and panel attach panel's driver can be unbind and consumer is left with dangling panel's pointer, so even with your patch most of the drivers are still buggy.

And few words about the bug(?) in device_link code: If you call device_link_add from probe of the supplier(as it is in this case) its status is set to DL_STATE_DORMANT, and after successful bind it is changed to DL_STATE_AVAILABLE, which is incorrect (it should be DL_STATE_ACTIVE), as a result on supplier's unbind consumer is not unbound.

Regards
Andrzej

Quoted 52 lines
---
 drivers/gpu/drm/drm_panel.c | 10 ++++++++++
 include/drm/drm_panel.h     |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/drm_panel.c +10 -0
index 71e4075..965530a 100644
@@ -24,6 +24,7 @@
 #include <linux/err.h>
 #include <linux/module.h>
 
+#include <drm/drm_device.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_panel.h>
 
@@ -104,6 +105,13 @@ int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
 	if (panel->connector)
 		return -EBUSY;
 
+	panel->link = device_link_add(connector->dev->dev, panel->dev, 0);
+	if (!panel->link) {
+		dev_err(panel->dev, "failed to link panel to %s\n",
+			dev_name(connector->dev->dev));
+		return -EINVAL;
+	}
+
 	panel->connector = connector;
 	panel->drm = connector->dev;
 
@@ -125,6 +133,8 @@ EXPORT_SYMBOL(drm_panel_attach);
  */
 int drm_panel_detach(struct drm_panel *panel)
 {
+	device_link_del(panel->link);
+
 	panel->connector = NULL;
 	panel->drm = NULL;
 
include/drm/drm_panel.h +1 -0
index 14ac240..26a1b5f 100644
@@ -89,6 +89,7 @@ struct drm_panel {
 	struct drm_device *drm;
 	struct drm_connector *connector;
 	struct device *dev;
+	struct device_link *link;
 
 	const struct drm_panel_funcs *funcs;
 

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#10 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 68 lines · Andrzej Hajda
Quoted 67 lines · Peter Rosin
Quoted 66 lines · Andrzej Hajda
Quoted 45 lines · Peter Rosin
Quoted 24 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

Quoted 4 lines

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

And this is also incorrect from Documentation PoV:
 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.

Quoted 3 lines

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Making it automatic looks tempting, but also error prone. In case of component framework bind callbacks can be called from probe of any component, so sometimes it can be called also from exynos-dsi probe, sometimes not (depending on order of probing, which we cannot rely on). So in some cases we will end-up with links, sometimes without. Ie following scenarios are possible in drm_panel_attach:
1. exynos-dsi bound, panel during probe.
2. exynos-dsi during probe, panel during probe.

Quoted 12 lines
Quoted 2 lines

2. exynos-dsi during probe, panel bound? Or is this case 3, and 2 happens when drivers probe in parallel?

Panel is always probed not earlier than the end of exynos-dsi bind, so only scenarios 1 and 2 should be possible.

Quoted 6 lines

Whichever, you are right, I naively thought that the bind happened after the probe of all devices, but naturally it happens as part of the last device to register its component, and that normally happens during its probe.

Sigh. So, scratch that, I guess we need a flag...

I looked into that, and it seems very fragile to get the flag to be correct for all cases. That road is bound to produce lots of bugs, and it will be hard to get it right. In short, I would not descend into that particular rabbit hole.

Can it be assumed that the drm_device of the encoder in drm_bridge_attach is a master component, if the drm "cluster" is componentized? Are there currently other ways of handling drm driver binding changes than components?

If the answers are "yes" and "no", it might be possible to check if encoder->dev is a master component and only establish the device link if it isn't. All it would take is to add a component_device_is_master() query function to drivers/base/component.c

Something like this (untested):

bool component_device_is_master(struct device *dev)

{
	struct master *m;

	mutex_lock(&component_mutex);
	m = __master_find(dev, NULL);
	mutex_unlock(&component_mutex);

	return !!m;
}

EXPORT_SYMBOL_GPL(component_device_is_master);

And then check that before calling device_link_add().

Cheers,
Peter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#11 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 79 lines · Peter Rosin
Quoted 78 lines · Andrzej Hajda
Quoted 67 lines · Peter Rosin
Quoted 58 lines · Andrzej Hajda
Quoted 45 lines · Peter Rosin
Quoted 24 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

Quoted 4 lines

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

And this is also incorrect from Documentation PoV:
 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.

Quoted 3 lines

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Quoted 8 lines

Making it automatic looks tempting, but also error prone. In case of component framework bind callbacks can be called from probe of any component, so sometimes it can be called also from exynos-dsi probe, sometimes not (depending on order of probing, which we cannot rely on). So in some cases we will end-up with links, sometimes without. Ie following scenarios are possible in drm_panel_attach:
1. exynos-dsi bound, panel during probe.
2. exynos-dsi during probe, panel during probe.

Quoted 2 lines

2. exynos-dsi during probe, panel bound? Or is this case 3, and 2 happens when drivers probe in parallel?

Panel is always probed not earlier than the end of exynos-dsi bind, so only scenarios 1 and 2 should be possible.

Quoted 5 lines

Whichever, you are right, I naively thought that the bind happened after the probe of all devices, but naturally it happens as part of the last device to register its component, and that normally happens during its probe.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Sigh. So, scratch that, I guess we need a flag...

I looked into that, and it seems very fragile to get the flag to be correct for all cases. That road is bound to produce lots of bugs, and it will be hard to get it right. In short, I would not descend into that particular rabbit hole.

Can it be assumed that the drm_device of the encoder in drm_bridge_attach is a master component, if the drm "cluster" is componentized?

I wouldn't call it assumption, I would say rather it is a common practice.

Quoted 3 lines

Are there
currently other ways of handling drm driver binding changes than components?

No, there are drivers which do not use components, but call drm_panel_attach:

$ for d in drivers/gpu/drm/*/; do git grep -q 'struct component_ops' $d
&& continue; git grep -q drm_panel_attach $d && echo $d; done
drivers/gpu/drm/bridge/
drivers/gpu/drm/fsl-dcu/
drivers/gpu/drm/mxsfb/
drivers/gpu/drm/rcar-du/
drivers/gpu/drm/tegra/

Components are optional.

Quoted 17 lines

If the answers are "yes" and "no", it might be possible to check if encoder->dev is a master component and only establish the device link if it isn't. All it would take is to add a component_device_is_master() query function to drivers/base/component.c
Something like this (untested):

bool component_device_is_master(struct device *dev)

{
	struct master *m;

	mutex_lock(&component_mutex);
	m = __master_find(dev, NULL);
	mutex_unlock(&component_mutex);

	return !!m;
}
Quoted 3 lines

EXPORT_SYMBOL_GPL(component_device_is_master);

And then check that before calling device_link_add().

Why do not use simpler solution, just create function drm_panel_attach_without_link and call it explicitly from drivers, or add additional flag argument to either drm_panel_attach either to drm_panel structure? Maybe it will not be prettier but will be more explicit.

Regards
Andrzej

Quoted 6 lines

Cheers,
Peter

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#12 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 105 lines · Andrzej Hajda
Quoted 79 lines · Peter Rosin
Quoted 78 lines · Andrzej Hajda
Quoted 67 lines · Peter Rosin
Quoted 58 lines · Andrzej Hajda
Quoted 45 lines · Peter Rosin
Quoted 24 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

Quoted 4 lines

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

And this is also incorrect from Documentation PoV:
 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.

Quoted 3 lines

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Quoted 8 lines

Making it automatic looks tempting, but also error prone. In case of component framework bind callbacks can be called from probe of any component, so sometimes it can be called also from exynos-dsi probe, sometimes not (depending on order of probing, which we cannot rely on). So in some cases we will end-up with links, sometimes without. Ie following scenarios are possible in drm_panel_attach:
1. exynos-dsi bound, panel during probe.
2. exynos-dsi during probe, panel during probe.

Quoted 2 lines

2. exynos-dsi during probe, panel bound? Or is this case 3, and 2 happens when drivers probe in parallel?

Panel is always probed not earlier than the end of exynos-dsi bind, so only scenarios 1 and 2 should be possible.

Quoted 5 lines

Whichever, you are right, I naively thought that the bind happened after the probe of all devices, but naturally it happens as part of the last device to register its component, and that normally happens during its probe.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Sigh. So, scratch that, I guess we need a flag...

I looked into that, and it seems very fragile to get the flag to be correct for all cases. That road is bound to produce lots of bugs, and it will be hard to get it right. In short, I would not descend into that particular rabbit hole.

Can it be assumed that the drm_device of the encoder in drm_bridge_attach is a master component, if the drm "cluster" is componentized?

I wouldn't call it assumption, I would say rather it is a common practice.

Quoted 3 lines

Are there
currently other ways of handling drm driver binding changes than components?

No, there are drivers which do not use components, but call drm_panel_attach:

$ for d in drivers/gpu/drm/*/; do git grep -q 'struct component_ops' $d
&& continue; git grep -q drm_panel_attach $d && echo $d; done
drivers/gpu/drm/bridge/
drivers/gpu/drm/fsl-dcu/
drivers/gpu/drm/mxsfb/
drivers/gpu/drm/rcar-du/
drivers/gpu/drm/tegra/
Quoted 1 line

Components are optional.

Yes, of course components are optional. The question was if there are currently *other* ways (i.e. not the component framework, not device links) of dealing with disappearing panels/bridges. However, see below, the question is irrelevant with my below suggestion.

Quoted 19 lines
Quoted 19 lines

If the answers are "yes" and "no", it might be possible to check if encoder->dev is a master component and only establish the device link if it isn't. All it would take is to add a component_device_is_master() query function to drivers/base/component.c
Something like this (untested):

bool component_device_is_master(struct device *dev)

{
	struct master *m;

	mutex_lock(&component_mutex);
	m = __master_find(dev, NULL);
	mutex_unlock(&component_mutex);

	return !!m;
}

EXPORT_SYMBOL_GPL(component_device_is_master);

Quoted 7 lines
Quoted 1 line

And then check that before calling device_link_add().

Why do not use simpler solution, just create function drm_panel_attach_without_link and call it explicitly from drivers, or add additional flag argument to either drm_panel_attach either to drm_panel structure? Maybe it will not be prettier but will be more explicit.

Because, if you take bridges into account as well and add a drm_bridge_attach_without_link, which function do you call from e.g. drm_simple_display_pipe_attach_bridge()? Sure, you can probably verify the callers and come to the conclusion that you maybe always want the link, currently. Same question for the tda998x driver, which function to use for attaching the bridge would have to depend on how the driver was bound (as a component or not, yes I know, currently tda998x only does component, but the whole reason I'm involved is that I want it to also register as a standalone drm_bridge). Not doing this with some automatic check simply leads to combinatorial hell.

Maybe a better solution is for the drm driver to record whether it wants links in its struct drm_device, before calling drm_dev_register? That's also explicit. drm_panel_attach/drm_bridge_attach could then easily determine if the link should be made. IMHO, it would also be quite easy to set this correctly for any given drm_device.

Cheers,
Peter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#13 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 151 lines · Peter Rosin
Quoted 105 lines · Andrzej Hajda
Quoted 79 lines · Peter Rosin
Quoted 78 lines · Andrzej Hajda
Quoted 67 lines · Peter Rosin
Quoted 58 lines · Andrzej Hajda
Quoted 45 lines · Peter Rosin
Quoted 24 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

Quoted 4 lines

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

And this is also incorrect from Documentation PoV:
 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.

Quoted 3 lines

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Quoted 8 lines

Making it automatic looks tempting, but also error prone. In case of component framework bind callbacks can be called from probe of any component, so sometimes it can be called also from exynos-dsi probe, sometimes not (depending on order of probing, which we cannot rely on). So in some cases we will end-up with links, sometimes without. Ie following scenarios are possible in drm_panel_attach:
1. exynos-dsi bound, panel during probe.
2. exynos-dsi during probe, panel during probe.

Quoted 2 lines

2. exynos-dsi during probe, panel bound? Or is this case 3, and 2 happens when drivers probe in parallel?

Panel is always probed not earlier than the end of exynos-dsi bind, so only scenarios 1 and 2 should be possible.

Quoted 5 lines

Whichever, you are right, I naively thought that the bind happened after the probe of all devices, but naturally it happens as part of the last device to register its component, and that normally happens during its probe.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Sigh. So, scratch that, I guess we need a flag...

I looked into that, and it seems very fragile to get the flag to be correct for all cases. That road is bound to produce lots of bugs, and it will be hard to get it right. In short, I would not descend into that particular rabbit hole.

Can it be assumed that the drm_device of the encoder in drm_bridge_attach is a master component, if the drm "cluster" is componentized?

I wouldn't call it assumption, I would say rather it is a common practice.

Quoted 3 lines

Are there
currently other ways of handling drm driver binding changes than components?

No, there are drivers which do not use components, but call drm_panel_attach:

$ for d in drivers/gpu/drm/*/; do git grep -q 'struct component_ops' $d
&& continue; git grep -q drm_panel_attach $d && echo $d; done
drivers/gpu/drm/bridge/
drivers/gpu/drm/fsl-dcu/
drivers/gpu/drm/mxsfb/
drivers/gpu/drm/rcar-du/
drivers/gpu/drm/tegra/
Quoted 1 line

Components are optional.

Yes, of course components are optional. The question was if there are currently *other* ways (i.e. not the component framework, not device links) of dealing with disappearing panels/bridges. However, see below, the question is irrelevant with my below suggestion.

Quoted 19 lines
Quoted 19 lines

If the answers are "yes" and "no", it might be possible to check if encoder->dev is a master component and only establish the device link if it isn't. All it would take is to add a component_device_is_master() query function to drivers/base/component.c
Something like this (untested):

bool component_device_is_master(struct device *dev)

{
	struct master *m;

	mutex_lock(&component_mutex);
	m = __master_find(dev, NULL);
	mutex_unlock(&component_mutex);

	return !!m;
}

EXPORT_SYMBOL_GPL(component_device_is_master);

Quoted 7 lines
Quoted 1 line

And then check that before calling device_link_add().

Why do not use simpler solution, just create function drm_panel_attach_without_link and call it explicitly from drivers, or add additional flag argument to either drm_panel_attach either to drm_panel structure? Maybe it will not be prettier but will be more explicit.

Because, if you take bridges into account as well and add a drm_bridge_attach_without_link, which function do you call from e.g. drm_simple_display_pipe_attach_bridge()? Sure, you can probably verify the callers and come to the conclusion that you maybe always want the link, currently. Same question for the tda998x driver, which function to use for attaching the bridge would have to depend on how the driver was bound (as a component or not, yes I know, currently tda998x only does component, but the whole reason I'm involved is that I want it to also register as a standalone drm_bridge). Not doing this with some automatic check simply leads to combinatorial hell.

Quoted 5 lines

Maybe a better solution is for the drm driver to record whether it wants links in its struct drm_device, before calling drm_dev_register? That's also explicit. drm_panel_attach/drm_bridge_attach could then easily determine if the link should be made. IMHO, it would also be quite easy to set this correctly for any given drm_device.

While pursuing that idea, I found several examples of drivers that deal with both component-parts not wanting the device-link and non- component-parts wanting the device-link. So, a dead end.

Cheers,
Peter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#14 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

In reply to Peter Rosin
Quoted 134 lines · Peter Rosin
Quoted 103 lines · Andrzej Hajda
Quoted 79 lines · Peter Rosin
Quoted 78 lines · Andrzej Hajda
Quoted 67 lines · Peter Rosin
Quoted 58 lines · Andrzej Hajda
Quoted 45 lines · Peter Rosin
Quoted 24 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

Quoted 4 lines

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

And this is also incorrect from Documentation PoV:
 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.

Quoted 3 lines

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Quoted 8 lines

Making it automatic looks tempting, but also error prone. In case of component framework bind callbacks can be called from probe of any component, so sometimes it can be called also from exynos-dsi probe, sometimes not (depending on order of probing, which we cannot rely on). So in some cases we will end-up with links, sometimes without. Ie following scenarios are possible in drm_panel_attach:
1. exynos-dsi bound, panel during probe.
2. exynos-dsi during probe, panel during probe.

Quoted 2 lines

2. exynos-dsi during probe, panel bound? Or is this case 3, and 2 happens when drivers probe in parallel?

Panel is always probed not earlier than the end of exynos-dsi bind, so only scenarios 1 and 2 should be possible.

Quoted 5 lines

Whichever, you are right, I naively thought that the bind happened after the probe of all devices, but naturally it happens as part of the last device to register its component, and that normally happens during its probe.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Sigh. So, scratch that, I guess we need a flag...

I looked into that, and it seems very fragile to get the flag to be correct for all cases. That road is bound to produce lots of bugs, and it will be hard to get it right. In short, I would not descend into that particular rabbit hole.

Can it be assumed that the drm_device of the encoder in drm_bridge_attach is a master component, if the drm "cluster" is componentized?

I wouldn't call it assumption, I would say rather it is a common practice.

Quoted 3 lines

Are there
currently other ways of handling drm driver binding changes than components?

No, there are drivers which do not use components, but call drm_panel_attach:

$ for d in drivers/gpu/drm/*/; do git grep -q 'struct component_ops' $d
&& continue; git grep -q drm_panel_attach $d && echo $d; done
drivers/gpu/drm/bridge/
drivers/gpu/drm/fsl-dcu/
drivers/gpu/drm/mxsfb/
drivers/gpu/drm/rcar-du/
drivers/gpu/drm/tegra/
Quoted 1 line

Components are optional.

Yes, of course components are optional. The question was if there are currently *other* ways (i.e. not the component framework, not device links) of dealing with disappearing panels/bridges. However, see below, the question is irrelevant with my below suggestion.

Quoted 18 lines
Quoted 18 lines

If the answers are "yes" and "no", it might be possible to check if encoder->dev is a master component and only establish the device link if it isn't. All it would take is to add a component_device_is_master() query function to drivers/base/component.c
Something like this (untested):

bool component_device_is_master(struct device *dev)

{
	struct master *m;

	mutex_lock(&component_mutex);
	m = __master_find(dev, NULL);
	mutex_unlock(&component_mutex);

	return !!m;
}

EXPORT_SYMBOL_GPL(component_device_is_master);

Quoted 6 lines
Quoted 1 line

And then check that before calling device_link_add().

Why do not use simpler solution, just create function drm_panel_attach_without_link and call it explicitly from drivers, or add additional flag argument to either drm_panel_attach either to drm_panel structure? Maybe it will not be prettier but will be more explicit.

Quoted 10 lines

Because, if you take bridges into account as well and add a drm_bridge_attach_without_link, which function do you call from e.g. drm_simple_display_pipe_attach_bridge()? Sure, you can probably verify the callers and come to the conclusion that you maybe always want the link, currently. Same question for the tda998x driver, which function to use for attaching the bridge would have to depend on how the driver was bound (as a component or not, yes I know, currently tda998x only does component, but the whole reason I'm involved is that I want it to also register as a standalone drm_bridge). Not doing this with some automatic check simply leads to combinatorial hell.

I was focused on panels, which are managed not by drm core, but by upstream pipeline element (bridge/encoder). For them decision about using device links should be made by the manager not drm core, I guess. In case of bridges it is different, bridges are attached by upstream elements, but then they are tracked/managed/detached only by drm core (at least this is current practice).  If somebody wants to implement dynamic bridges this pattern cannot be used, ie bridge should be attached/detached by upstream element, like in case of panels.

Quoted 6 lines

Maybe a better solution is for the drm driver to record whether it wants links in its struct drm_device, before calling drm_dev_register? That's also explicit. drm_panel_attach/drm_bridge_attach could then easily determine if the link should be made. IMHO, it would also be quite easy to set this correctly for any given drm_device.

As I said earlier I think decision about link creation should be always up to element which performs attachment, It only knows what should be attached, so it is the best candidate to handle dynamic unbind and re-bind of the downstream element.

Regards
Andrzej

Quoted 6 lines

Cheers,
Peter

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#15 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 162 lines · Andrzej Hajda
Quoted 134 lines · Peter Rosin
Quoted 103 lines · Andrzej Hajda
Quoted 79 lines · Peter Rosin
Quoted 78 lines · Andrzej Hajda
Quoted 67 lines · Peter Rosin
Quoted 58 lines · Andrzej Hajda
Quoted 45 lines · Peter Rosin
Quoted 24 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

Quoted 4 lines

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

And this is also incorrect from Documentation PoV:
 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.

Quoted 3 lines

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Quoted 8 lines

Making it automatic looks tempting, but also error prone. In case of component framework bind callbacks can be called from probe of any component, so sometimes it can be called also from exynos-dsi probe, sometimes not (depending on order of probing, which we cannot rely on). So in some cases we will end-up with links, sometimes without. Ie following scenarios are possible in drm_panel_attach:
1. exynos-dsi bound, panel during probe.
2. exynos-dsi during probe, panel during probe.

Quoted 2 lines

2. exynos-dsi during probe, panel bound? Or is this case 3, and 2 happens when drivers probe in parallel?

Panel is always probed not earlier than the end of exynos-dsi bind, so only scenarios 1 and 2 should be possible.

Quoted 5 lines

Whichever, you are right, I naively thought that the bind happened after the probe of all devices, but naturally it happens as part of the last device to register its component, and that normally happens during its probe.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Sigh. So, scratch that, I guess we need a flag...

I looked into that, and it seems very fragile to get the flag to be correct for all cases. That road is bound to produce lots of bugs, and it will be hard to get it right. In short, I would not descend into that particular rabbit hole.

Can it be assumed that the drm_device of the encoder in drm_bridge_attach is a master component, if the drm "cluster" is componentized?

I wouldn't call it assumption, I would say rather it is a common practice.

Quoted 3 lines

Are there
currently other ways of handling drm driver binding changes than components?

No, there are drivers which do not use components, but call drm_panel_attach:

$ for d in drivers/gpu/drm/*/; do git grep -q 'struct component_ops' $d
&& continue; git grep -q drm_panel_attach $d && echo $d; done
drivers/gpu/drm/bridge/
drivers/gpu/drm/fsl-dcu/
drivers/gpu/drm/mxsfb/
drivers/gpu/drm/rcar-du/
drivers/gpu/drm/tegra/
Quoted 1 line

Components are optional.

Yes, of course components are optional. The question was if there are currently *other* ways (i.e. not the component framework, not device links) of dealing with disappearing panels/bridges. However, see below, the question is irrelevant with my below suggestion.

Quoted 18 lines
Quoted 18 lines

If the answers are "yes" and "no", it might be possible to check if encoder->dev is a master component and only establish the device link if it isn't. All it would take is to add a component_device_is_master() query function to drivers/base/component.c
Something like this (untested):

bool component_device_is_master(struct device *dev)

{
	struct master *m;

	mutex_lock(&component_mutex);
	m = __master_find(dev, NULL);
	mutex_unlock(&component_mutex);

	return !!m;
}

EXPORT_SYMBOL_GPL(component_device_is_master);

Quoted 6 lines
Quoted 1 line

And then check that before calling device_link_add().

Why do not use simpler solution, just create function drm_panel_attach_without_link and call it explicitly from drivers, or add additional flag argument to either drm_panel_attach either to drm_panel structure? Maybe it will not be prettier but will be more explicit.

Quoted 10 lines

Because, if you take bridges into account as well and add a drm_bridge_attach_without_link, which function do you call from e.g. drm_simple_display_pipe_attach_bridge()? Sure, you can probably verify the callers and come to the conclusion that you maybe always want the link, currently. Same question for the tda998x driver, which function to use for attaching the bridge would have to depend on how the driver was bound (as a component or not, yes I know, currently tda998x only does component, but the whole reason I'm involved is that I want it to also register as a standalone drm_bridge). Not doing this with some automatic check simply leads to combinatorial hell.

I was focused on panels, which are managed not by drm core, but by upstream pipeline element (bridge/encoder). For them decision about using device links should be made by the manager not drm core, I guess. In case of bridges it is different, bridges are attached by upstream elements, but then they are tracked/managed/detached only by drm core (at least this is current practice).  If somebody wants to implement dynamic bridges this pattern cannot be used, ie bridge should be attached/detached by upstream element, like in case of panels.

Quoted 6 lines

Maybe a better solution is for the drm driver to record whether it wants links in its struct drm_device, before calling drm_dev_register? That's also explicit. drm_panel_attach/drm_bridge_attach could then easily determine if the link should be made. IMHO, it would also be quite easy to set this correctly for any given drm_device.

Quoted 4 lines

As I said earlier I think decision about link creation should be always up to element which performs attachment, It only knows what should be attached, so it is the best candidate to handle dynamic unbind and re-bind of the downstream element.

But does the attacher in fact know *what* should be attached? And how? Take e.g. the drm_panel_attach in analogix_dp_bridge_attach, in analogix_dp_core.c. Should that attach be with or without a device-link? That function has no knowledge whatsoever about the requirements for dp->plat_data->panel. That panel could e.g. come from rockchip_dp_probe, in analogix_dp-rockchip.c, which can be further traced back to drm_of_find_panel_or_bridge. But what kind of requirements do that panel have? It might be a dsi panel, in which case we had better not create a device-link, as you have shown previously in the thread. Or it might be some little trivial panel, like panel-lg-lg4573.c, in which case we *really* want the device- link so that the panel doesn't disappear on us.

Maybe it's easy to see this, if you know the ins and outs of the code. But I don't see it. And I don't see how this path leads to maintainable code. I still think the link-or-no-link decision needs to be in a central place.

Cheers,
Peter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#16 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 181 lines · Peter Rosin
Quoted 162 lines · Andrzej Hajda
Quoted 134 lines · Peter Rosin
Quoted 103 lines · Andrzej Hajda
Quoted 79 lines · Peter Rosin
Quoted 78 lines · Andrzej Hajda
Quoted 67 lines · Peter Rosin
Quoted 58 lines · Andrzej Hajda
Quoted 45 lines · Peter Rosin
Quoted 24 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

Quoted 4 lines

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

And this is also incorrect from Documentation PoV:
 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.

Quoted 3 lines

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Quoted 8 lines

Making it automatic looks tempting, but also error prone. In case of component framework bind callbacks can be called from probe of any component, so sometimes it can be called also from exynos-dsi probe, sometimes not (depending on order of probing, which we cannot rely on). So in some cases we will end-up with links, sometimes without. Ie following scenarios are possible in drm_panel_attach:
1. exynos-dsi bound, panel during probe.
2. exynos-dsi during probe, panel during probe.

Quoted 2 lines

2. exynos-dsi during probe, panel bound? Or is this case 3, and 2 happens when drivers probe in parallel?

Panel is always probed not earlier than the end of exynos-dsi bind, so only scenarios 1 and 2 should be possible.

Quoted 5 lines

Whichever, you are right, I naively thought that the bind happened after the probe of all devices, but naturally it happens as part of the last device to register its component, and that normally happens during its probe.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Sigh. So, scratch that, I guess we need a flag...

I looked into that, and it seems very fragile to get the flag to be correct for all cases. That road is bound to produce lots of bugs, and it will be hard to get it right. In short, I would not descend into that particular rabbit hole.

Can it be assumed that the drm_device of the encoder in drm_bridge_attach is a master component, if the drm "cluster" is componentized?

I wouldn't call it assumption, I would say rather it is a common practice.

Quoted 3 lines

Are there
currently other ways of handling drm driver binding changes than components?

No, there are drivers which do not use components, but call drm_panel_attach:

$ for d in drivers/gpu/drm/*/; do git grep -q 'struct component_ops' $d
&& continue; git grep -q drm_panel_attach $d && echo $d; done
drivers/gpu/drm/bridge/
drivers/gpu/drm/fsl-dcu/
drivers/gpu/drm/mxsfb/
drivers/gpu/drm/rcar-du/
drivers/gpu/drm/tegra/
Quoted 1 line

Components are optional.

Yes, of course components are optional. The question was if there are currently *other* ways (i.e. not the component framework, not device links) of dealing with disappearing panels/bridges. However, see below, the question is irrelevant with my below suggestion.

Quoted 18 lines
Quoted 18 lines

If the answers are "yes" and "no", it might be possible to check if encoder->dev is a master component and only establish the device link if it isn't. All it would take is to add a component_device_is_master() query function to drivers/base/component.c
Something like this (untested):

bool component_device_is_master(struct device *dev)

{
	struct master *m;

	mutex_lock(&component_mutex);
	m = __master_find(dev, NULL);
	mutex_unlock(&component_mutex);

	return !!m;
}

EXPORT_SYMBOL_GPL(component_device_is_master);

Quoted 6 lines
Quoted 1 line

And then check that before calling device_link_add().

Why do not use simpler solution, just create function drm_panel_attach_without_link and call it explicitly from drivers, or add additional flag argument to either drm_panel_attach either to drm_panel structure? Maybe it will not be prettier but will be more explicit.

Quoted 10 lines

Because, if you take bridges into account as well and add a drm_bridge_attach_without_link, which function do you call from e.g. drm_simple_display_pipe_attach_bridge()? Sure, you can probably verify the callers and come to the conclusion that you maybe always want the link, currently. Same question for the tda998x driver, which function to use for attaching the bridge would have to depend on how the driver was bound (as a component or not, yes I know, currently tda998x only does component, but the whole reason I'm involved is that I want it to also register as a standalone drm_bridge). Not doing this with some automatic check simply leads to combinatorial hell.

I was focused on panels, which are managed not by drm core, but by upstream pipeline element (bridge/encoder). For them decision about using device links should be made by the manager not drm core, I guess. In case of bridges it is different, bridges are attached by upstream elements, but then they are tracked/managed/detached only by drm core (at least this is current practice).  If somebody wants to implement dynamic bridges this pattern cannot be used, ie bridge should be attached/detached by upstream element, like in case of panels.

Quoted 6 lines

Maybe a better solution is for the drm driver to record whether it wants links in its struct drm_device, before calling drm_dev_register? That's also explicit. drm_panel_attach/drm_bridge_attach could then easily determine if the link should be made. IMHO, it would also be quite easy to set this correctly for any given drm_device.

Quoted 4 lines

As I said earlier I think decision about link creation should be always up to element which performs attachment, It only knows what should be attached, so it is the best candidate to handle dynamic unbind and re-bind of the downstream element.

But does the attacher in fact know *what* should be attached? And how? Take e.g. the drm_panel_attach in analogix_dp_bridge_attach, in analogix_dp_core.c. Should that attach be with or without a device-link? That function has no knowledge whatsoever about the requirements for dp->plat_data->panel. That panel could e.g. come from rockchip_dp_probe, in analogix_dp-rockchip.c, which can be further traced back to drm_of_find_panel_or_bridge. But what kind of requirements do that panel have? It might be a dsi panel, in which case we had better not create a device-link, as you have shown previously in the thread. Or it might be some little trivial panel, like panel-lg-lg4573.c, in which case we *really* want the device- link so that the panel doesn't disappear on us.

Quoted 4 lines

Maybe it's easy to see this, if you know the ins and outs of the code. But I don't see it. And I don't see how this path leads to maintainable code. I still think the link-or-no-link decision needs to be in a central place.

Are not all dsi panels client on a bus, and therefore managed by a dsi host? And are not all dsi panels attached to a dsi connector?

How about this:

int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)

{
	int ret;

	if (panel->connector)
		return -EBUSY;

	if (connector->connector_type != DRM_MODE_CONNECTOR_DSI) {
		panel->link = device_link_add(connector->dev->dev,
					      panel->dev, 0);
		if (!panel->link) {
			dev_err(panel->dev, "failed to link panel to %s\n",
				dev_name(connector->dev->dev));
			return -EINVAL;
		}
	}

	panel->connector = connector;
	panel->drm = connector->dev;

	return 0;
}

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#17 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 161 lines · Peter Rosin
Quoted 160 lines · Peter Rosin
Quoted 159 lines · Andrzej Hajda
Quoted 134 lines · Peter Rosin
Quoted 103 lines · Andrzej Hajda
Quoted 79 lines · Peter Rosin
Quoted 78 lines · Andrzej Hajda
Quoted 67 lines · Peter Rosin
Quoted 58 lines · Andrzej Hajda
Quoted 45 lines · Peter Rosin
Quoted 24 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 6 lines

Making device_links for panels optional seems to me the best solution, what do you think?

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.

Ah, that's a pretty strong hint that we are doing something unusual. So, I had a deeper look and I think that device-links (with state, i.e. not DL_FLAG_STATELESS links) are assumed to be created by the .probe function of either the consumer or the supplier. Then it seems to works as expected. And that makes some sense too, because a link indicates that there exist a dependency between the two and that the consumer cannot really exist without the supplier. This is also what happens for the drm devices (panel/bridge consumers) Jyri and I are working with; they call panel or bridge attach during their probe. But this is not the case for exynos, which calls panel/bridge attach at some later stage, and that obviously violates the assumption that the device-link consumer cannot exist w/o the supplier ("obviously" since the drm driver has managed to successfully probe without the supplier).

Quoted 4 lines

So, when the panel/bridge supplier is probed after the consumer is bound, the link starts out as DL_STATE_DORMANT, and progresses to DL_STATE_AVAILABLE once the panel/bridge has finished the probe. This is not what *we* want.

And this is also incorrect from Documentation PoV:
 * @DL_STATE_DORMANT: None of the supplier/consumer drivers is present.
 * @DL_STATE_AVAILABLE: The supplier driver is present, but the consumer is not.

Quoted 3 lines

So, one idea I have is to, on panel/bridge attach, verify if the consumer is in its probe, and only create the link if that is the case. So, the link would be optional, but it would all be automatic.

Quoted 8 lines

Making it automatic looks tempting, but also error prone. In case of component framework bind callbacks can be called from probe of any component, so sometimes it can be called also from exynos-dsi probe, sometimes not (depending on order of probing, which we cannot rely on). So in some cases we will end-up with links, sometimes without. Ie following scenarios are possible in drm_panel_attach:
1. exynos-dsi bound, panel during probe.
2. exynos-dsi during probe, panel during probe.

Quoted 2 lines

2. exynos-dsi during probe, panel bound? Or is this case 3, and 2 happens when drivers probe in parallel?

Panel is always probed not earlier than the end of exynos-dsi bind, so only scenarios 1 and 2 should be possible.

Quoted 5 lines

Whichever, you are right, I naively thought that the bind happened after the probe of all devices, but naturally it happens as part of the last device to register its component, and that normally happens during its probe.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Sigh. So, scratch that, I guess we need a flag...

I looked into that, and it seems very fragile to get the flag to be correct for all cases. That road is bound to produce lots of bugs, and it will be hard to get it right. In short, I would not descend into that particular rabbit hole.

Can it be assumed that the drm_device of the encoder in drm_bridge_attach is a master component, if the drm "cluster" is componentized?

I wouldn't call it assumption, I would say rather it is a common practice.

Quoted 3 lines

Are there
currently other ways of handling drm driver binding changes than components?

No, there are drivers which do not use components, but call drm_panel_attach:

$ for d in drivers/gpu/drm/*/; do git grep -q 'struct component_ops' $d
&& continue; git grep -q drm_panel_attach $d && echo $d; done
drivers/gpu/drm/bridge/
drivers/gpu/drm/fsl-dcu/
drivers/gpu/drm/mxsfb/
drivers/gpu/drm/rcar-du/
drivers/gpu/drm/tegra/
Quoted 1 line

Components are optional.

Yes, of course components are optional. The question was if there are currently *other* ways (i.e. not the component framework, not device links) of dealing with disappearing panels/bridges. However, see below, the question is irrelevant with my below suggestion.

Quoted 18 lines
Quoted 18 lines

If the answers are "yes" and "no", it might be possible to check if encoder->dev is a master component and only establish the device link if it isn't. All it would take is to add a component_device_is_master() query function to drivers/base/component.c
Something like this (untested):

bool component_device_is_master(struct device *dev)

{
	struct master *m;

	mutex_lock(&component_mutex);
	m = __master_find(dev, NULL);
	mutex_unlock(&component_mutex);

	return !!m;
}

EXPORT_SYMBOL_GPL(component_device_is_master);

Quoted 6 lines
Quoted 1 line

And then check that before calling device_link_add().

Why do not use simpler solution, just create function drm_panel_attach_without_link and call it explicitly from drivers, or add additional flag argument to either drm_panel_attach either to drm_panel structure? Maybe it will not be prettier but will be more explicit.

Quoted 10 lines

Because, if you take bridges into account as well and add a drm_bridge_attach_without_link, which function do you call from e.g. drm_simple_display_pipe_attach_bridge()? Sure, you can probably verify the callers and come to the conclusion that you maybe always want the link, currently. Same question for the tda998x driver, which function to use for attaching the bridge would have to depend on how the driver was bound (as a component or not, yes I know, currently tda998x only does component, but the whole reason I'm involved is that I want it to also register as a standalone drm_bridge). Not doing this with some automatic check simply leads to combinatorial hell.

I was focused on panels, which are managed not by drm core, but by upstream pipeline element (bridge/encoder). For them decision about using device links should be made by the manager not drm core, I guess. In case of bridges it is different, bridges are attached by upstream elements, but then they are tracked/managed/detached only by drm core (at least this is current practice).  If somebody wants to implement dynamic bridges this pattern cannot be used, ie bridge should be attached/detached by upstream element, like in case of panels.

Quoted 5 lines

Maybe a better solution is for the drm driver to record whether it wants links in its struct drm_device, before calling drm_dev_register? That's also explicit. drm_panel_attach/drm_bridge_attach could then easily determine if the link should be made. IMHO, it would also be quite easy to set this correctly for any given drm_device.

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines

As I said earlier I think decision about link creation should be always up to element which performs attachment, It only knows what should be attached, so it is the best candidate to handle dynamic unbind and re-bind of the downstream element.

But does the attacher in fact know *what* should be attached? And how? Take e.g. the drm_panel_attach in analogix_dp_bridge_attach, in analogix_dp_core.c. Should that attach be with or without a device-link?

analogix_dp_core.c is a 'library' used by exynos_dp and rockchip_dp, neither of these drivers perform dynamic panel management so answer is
simple: device_link will protect them from panel unbind.

Quoted 9 lines
Quoted 9 lines

That function has no knowledge whatsoever about the requirements for dp->plat_data->panel. That panel could e.g. come from rockchip_dp_probe, in analogix_dp-rockchip.c, which can be further traced back to drm_of_find_panel_or_bridge. But what kind of requirements do that panel have? It might be a dsi panel, in which case we had better not create a device-link, as you have shown previously in the thread. Or it might be some little trivial panel, like panel-lg-lg4573.c, in which case we *really* want the device- link so that the panel doesn't disappear on us.

In general it is not a matter of panel, but of panel's consumer, ie upstream pipe element, for example dsi host.

Quoted 7 lines
Quoted 5 lines

Maybe it's easy to see this, if you know the ins and outs of the code. But I don't see it. And I don't see how this path leads to maintainable code. I still think the link-or-no-link decision needs to be in a central place.

Are not all dsi panels client on a bus, and therefore managed by a dsi host?

Panels are managed by dsi host only if dsi host implements it, and it is up to dsi host author, it is not mandatory. The only case I am aware of is exynos-dsi. I guess most developers are not aware of the problem, or they do not care, or ... whatever. This patch shows it very clearly.

Quoted 18 lines

And are not all dsi panels attached to a dsi connector?

How about this:

int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)

{
	int ret;

	if (panel->connector)
		return -EBUSY;

	if (connector->connector_type != DRM_MODE_CONNECTOR_DSI) {
		panel->link = device_link_add(connector->dev->dev,
					      panel->dev, 0);
		if (!panel->link) {
			dev_err(panel->dev, "failed to link panel to %s\n",
				dev_name(connector->dev->dev));
			return -EINVAL;
Quoted 8 lines
		}
	}

	panel->connector = connector;
	panel->drm = connector->dev;

	return 0;
}

With such approach you do not protect dsi hosts without dynamic panel management support. As I said earlier: it is matter of panel consumers (for example dsi host), ie if it implements dynamic panel management, it should attach panel without creating device_links, as it take care of disappearing panels. If it does not perform dynamic management it should rely on default behavior - links should be created, to protect drm device from using dangling pointers.

Regards
Andrzej

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#18 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

On 2018-05-23 11:39, Andrzej Hajda wrote:

*snip*

Quoted 4 lines

Panels are managed by dsi host only if dsi host implements it, and it is up to dsi host author, it is not mandatory. The only case I am aware of is exynos-dsi. I guess most developers are not aware of the problem, or they do not care, or ... whatever. This patch shows it very clearly.

*snip*

Quoted 7 lines

With such approach you do not protect dsi hosts without dynamic panel management support. As I said earlier: it is matter of panel consumers (for example dsi host), ie if it implements dynamic panel management, it should attach panel without creating device_links, as it take care of disappearing panels. If it does not perform dynamic management it should rely on default behavior - links should be created, to protect drm device from using dangling pointers.

So, you're saying that the only place that needs to be patched to call drm_panel_attach_without_link is exynos_dsi_host_attach()?

What about the dw-mipi-dsi driver in rockchip/dw-mipi-dsi.c? It also calls drm_panel_attach/drm_panel_detach in its dw_mipi_dsi_host_attach and dw_mipi_dsi_host_detach functions.

Similar thing in bridge/synopsis/dw-mipi-dsi.c, but it calls the wrappers drm_panel_bridge_add/remove to create an implicit bridge that in turn handles the panel. So, do we need a to add a drm_panel_bridge_add_without_link too?

What about tegra/dsi.c? It also calls drm_panel_attach in its tegra_dsi_host_attach function? But that one doesn't detach the panel in its tegra_dsi_host_detach. Is that a bug?

Cheers,
Peter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#19 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 17 lines · Peter Rosin

On 2018-05-23 11:39, Andrzej Hajda wrote:

*snip*

Quoted 4 lines

Panels are managed by dsi host only if dsi host implements it, and it is up to dsi host author, it is not mandatory. The only case I am aware of is exynos-dsi. I guess most developers are not aware of the problem, or they do not care, or ... whatever. This patch shows it very clearly.

*snip*

Quoted 7 lines

With such approach you do not protect dsi hosts without dynamic panel management support. As I said earlier: it is matter of panel consumers (for example dsi host), ie if it implements dynamic panel management, it should attach panel without creating device_links, as it take care of disappearing panels. If it does not perform dynamic management it should rely on default behavior - links should be created, to protect drm device from using dangling pointers.

Quoted 2 lines

So, you're saying that the only place that needs to be patched to call drm_panel_attach_without_link is exynos_dsi_host_attach()?

It should be at least there, but I suppose only there.

Quoted 4 lines

What about the dw-mipi-dsi driver in rockchip/dw-mipi-dsi.c? It also calls drm_panel_attach/drm_panel_detach in its dw_mipi_dsi_host_attach and dw_mipi_dsi_host_detach functions.

dw_mipi_dsi_host_detach calls only drm_panel_detach, dangling pointer will stay in dsi->panel, and nothing prevents from using it. I guess it is just buggy code.

Quoted 5 lines

Similar thing in bridge/synopsis/dw-mipi-dsi.c, but it calls the wrappers drm_panel_bridge_add/remove to create an implicit bridge that in turn handles the panel. So, do we need a to add a drm_panel_bridge_add_without_link too?

Again looks like a buggy code, before panel removal it should be turned off, it should be performed under lock,....

Quoted 4 lines

What about tegra/dsi.c? It also calls drm_panel_attach in its tegra_dsi_host_attach function? But that one doesn't detach the panel in its tegra_dsi_host_detach. Is that a bug?

Also looks like a bug, but this is dual-channel device so I am not so sure.

Regards
Andrzej
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#20 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 24 lines · Andrzej Hajda
Quoted 17 lines · Peter Rosin

On 2018-05-23 11:39, Andrzej Hajda wrote:

*snip*

Quoted 4 lines

Panels are managed by dsi host only if dsi host implements it, and it is up to dsi host author, it is not mandatory. The only case I am aware of is exynos-dsi. I guess most developers are not aware of the problem, or they do not care, or ... whatever. This patch shows it very clearly.

*snip*

Quoted 7 lines

With such approach you do not protect dsi hosts without dynamic panel management support. As I said earlier: it is matter of panel consumers (for example dsi host), ie if it implements dynamic panel management, it should attach panel without creating device_links, as it take care of disappearing panels. If it does not perform dynamic management it should rely on default behavior - links should be created, to protect drm device from using dangling pointers.

Quoted 2 lines

So, you're saying that the only place that needs to be patched to call drm_panel_attach_without_link is exynos_dsi_host_attach()?

It should be at least there, but I suppose only there.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

What about the dw-mipi-dsi driver in rockchip/dw-mipi-dsi.c? It also calls drm_panel_attach/drm_panel_detach in its dw_mipi_dsi_host_attach and dw_mipi_dsi_host_detach functions.

dw_mipi_dsi_host_detach calls only drm_panel_detach, dangling pointer will stay in dsi->panel, and nothing prevents from using it. I guess it is just buggy code.

Yeah, now I see it.

Quoted 8 lines
Quoted 5 lines

Similar thing in bridge/synopsis/dw-mipi-dsi.c, but it calls the wrappers drm_panel_bridge_add/remove to create an implicit bridge that in turn handles the panel. So, do we need a to add a drm_panel_bridge_add_without_link too?

Again looks like a buggy code, before panel removal it should be turned off, it should be performed under lock,....

Yeah, that looks like crap code.

Quoted 6 lines
Quoted 4 lines

What about tegra/dsi.c? It also calls drm_panel_attach in its tegra_dsi_host_attach function? But that one doesn't detach the panel in its tegra_dsi_host_detach. Is that a bug?

Also looks like a bug, but this is dual-channel device so I am not so sure.

It's very confusing and hard to get a grip on how things are supposed to be done when everything appears to be riddled with misunderstandings...

While browsing the code base I originally thought many more sites needed the drm_panel_attach_without_link variant, but that appears to not be the case. Thanks for your patient explanations!

Cheers,
Peter
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#21 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

In reply to Andrzej Hajda
Quoted 26 lines · Andrzej Hajda
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi Jyri,

This patch breaks few platforms: tm2, tm2e, trats2 - ie all platforms using exynos_drm_dsi and dsi panels.
Exynos-DSI properly handles panels unbind - ie references to panel are properly removed on panels removal and respective drm_connector enters disconnected state, without destroying whole drm device.
And again on panel driver re-bind drm_panel is properly attached to exynos-dsi and panel pipeline is working again.
This patch will break this behavior, ie it will destroy whole drm device.

Quoted 2 lines

Making device_links for panels optional seems to me the best solution, what do you think?

I think all the device_link bugs have been fixed by now (lots of discussion in some other thread around using them for bridges). Time to resurrect/retest this patch and land it?

I still like the idea very much of making panel/bridges Just Work.

Cheers, Daniel

Quoted 105 lines

The funny thing is that due to bug in device link code, your patch has no effect on these platforms. So it does not hurt these platform yet, but the bug will be fixed sooner or later.
The trick in case of exynos-dsi is to use mipi_dsi attach/detach callbacks to get notifications about drm_panel's appearance/disappearance, I guess ultimately such callbacks should be a part of drm_panel
framework.

Below description how all the code works:
Initialization:
1. exynos_dsi (component of exynos_drm) at the end of bind callback registers mipi_dsi bus.
2. during mipi_dsi bus registration or later (if panel driver was not yet registered), panel driver is bound ie - its probe callback is called.
3. during panel's probe drm_panel_add is called, and later mipi_dsi_attach.
4. mipi_dsi_attach calls dsi .attach callback of exynos_dsi.
5. exynos_dsi.attach callback looks for drm_panel, calls drm_panel_attach and changes connector's status to connected.
....
Panel's unbind:
1. device_release_driver calls panel's remove callback.
2. panel's remove callback calls mipi_dsi_detach.
3. mipi_dsi_detach calls exynos_dsi.detach callback.
4. exynos_dsi.detach calls drm_panel_detach and changes connector's state to disconnected.

Panel's rebind is equivalent of steps 2-4 of Initialization.

I have wrote the code few years ago, and it seemed to me the only safe way to handle dynamic panel bind/unbind.
For example please note that step 5 of initialization and steps 2-4 of unbind are executed always under panel's device_lock so it makes it safe. Otherwise you risk that between panel lookup and panel attach panel's driver can be unbind and consumer is left with dangling panel's pointer, so even with your patch most of the drivers are still buggy.

And few words about the bug(?) in device_link code: If you call device_link_add from probe of the supplier(as it is in this case) its status is set to DL_STATE_DORMANT, and after successful bind it is changed to DL_STATE_AVAILABLE, which is incorrect (it should be DL_STATE_ACTIVE), as a result on supplier's unbind consumer is not unbound.

Regards
Andrzej

Quoted 45 lines
---
 drivers/gpu/drm/drm_panel.c | 10 ++++++++++
 include/drm/drm_panel.h     |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/drm_panel.c +10 -0
index 71e4075..965530a 100644
@@ -24,6 +24,7 @@
 #include <linux/err.h>
 #include <linux/module.h>
 
+#include <drm/drm_device.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_panel.h>
 
@@ -104,6 +105,13 @@ int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
      if (panel->connector)
              return -EBUSY;
 
+     panel->link = device_link_add(connector->dev->dev, panel->dev, 0);
+     if (!panel->link) {
+             dev_err(panel->dev, "failed to link panel to %s\n",
+                     dev_name(connector->dev->dev));
+             return -EINVAL;
+     }
+
      panel->connector = connector;
      panel->drm = connector->dev;
 
@@ -125,6 +133,8 @@ EXPORT_SYMBOL(drm_panel_attach);
  */
 int drm_panel_detach(struct drm_panel *panel)
 {
+     device_link_del(panel->link);
+
      panel->connector = NULL;
      panel->drm = NULL;
 
include/drm/drm_panel.h +0 -0
index 14ac240..26a1b5f 100644

include/drm/drm_panel.h struct drm_panel { · line 95

Quoted 7 lines
include/drm/drm_panel.h +1 -0
@@ -89,6 +89,7 @@ struct drm_panel {
      struct drm_device *drm;
      struct drm_connector *connector;
      struct device *dev;
+     struct device_link *link;
 
      const struct drm_panel_funcs *funcs;
 

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#22 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

In reply to Jyri Sarha
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Just noticed this complains when building docs:

./include/drm/drm_panel.h:98: warning: Function parameter or member 'link' not described in 'drm_panel'

Care to fix this? Also would be good to capture some of the discussions that ensued from this patch in the docs ...

Thanks, Daniel

Quoted 55 lines
---
 drivers/gpu/drm/drm_panel.c | 10 ++++++++++
 include/drm/drm_panel.h     |  1 +
 2 files changed, 11 insertions(+)
drivers/gpu/drm/drm_panel.c +10 -0
index 71e4075..965530a 100644
@@ -24,6 +24,7 @@
 #include <linux/err.h>
 #include <linux/module.h>
 
+#include <drm/drm_device.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_panel.h>
 
@@ -104,6 +105,13 @@ int drm_panel_attach(struct drm_panel *panel, struct drm_connector *connector)
        if (panel->connector)
                return -EBUSY;
 
+       panel->link = device_link_add(connector->dev->dev, panel->dev, 0);
+       if (!panel->link) {
+               dev_err(panel->dev, "failed to link panel to %s\n",
+                       dev_name(connector->dev->dev));
+               return -EINVAL;
+       }
+
        panel->connector = connector;
        panel->drm = connector->dev;
 
@@ -125,6 +133,8 @@ EXPORT_SYMBOL(drm_panel_attach);
  */
 int drm_panel_detach(struct drm_panel *panel)
 {
+       device_link_del(panel->link);
+
        panel->connector = NULL;
        panel->drm = NULL;
 
include/drm/drm_panel.h +1 -1
index 14ac240..26a1b5f 100644
@@ -89,6 +89,7 @@ struct drm_panel {
        struct drm_device *drm;
        struct drm_connector *connector;
        struct device *dev;
+       struct device_link *link;
 
        const struct drm_panel_funcs *funcs;
 
--

Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#23 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

In reply to Jyri Sarha
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi I have a problem with an in-development driver and this patch.

Do not worry! It is no regression. I just need help to do things right now.

My panel is a DSI device on a DSI master:

                mcde@a0350000 {
                        status = "okay";

                        dsi@a0351000 {
                                port {
                                        dsi0_out: endpoint {
                                                remote-endpoint = <&panel_in>;
                                        };
                                };

                                panel: display {
                                        compatible = "samsung,s6d16d0";
                                        reg = <0>;
                                        /* VDD1 on the component */
                                        power-supply = <&ab8500_ldo_aux1_reg>;
                                        reset-gpios = <&gpio2 1

GPIO_ACTIVE_LOW>;

                                        port {
                                                panel_in: endpoint {
remote-endpoint = <&dsi0_out>;
                                                };
                                        };
                                };
                        };
                };

So in my DSI host driver .bind() I do:

    drm_encoder_init(drm, encoder, &mcde_dsi_encoder_funcs,
             DRM_MODE_ENCODER_DSI, NULL);
    drm_encoder_helper_add(encoder, &mcde_dsi_encoder_helper_funcs);
    ret = drm_connector_init(encoder->dev, connector,
                 &mcde_dsi_connector_funcs,
                 DRM_MODE_CONNECTOR_DSI);

    drm_connector_helper_add(connector, &mcde_dsi_connector_helper_funcs);
    drm_connector_attach_encoder(connector, encoder);
    drm_connector_register(connector);

This works fine. I managed to create the encoder and connector for this DSI. Then:

    ret = drm_of_find_panel_or_bridge(dev->of_node,
                      0, 0, &panel, &bridge);
    if (panel) {
        bridge = drm_panel_bridge_add(panel,
                          DRM_MODE_CONNECTOR_DSI);
        if (IS_ERR(bridge)) {
            dev_err(dev, "error adding panel bridge\n");
            return PTR_ERR(bridge);
        }
        dev_info(dev, "connected to panel\n");
        d->panel = panel;
    }
    d->connector.status = connector_status_connected;
    ret = drm_bridge_attach(encoder, bridge, NULL);

And this is where it blows up: it fails in device_link_add(connector->dev->dev, panel->dev, 0); device_is_dependent(consumer, supplier) because the consumer (connector) is dependent on the supplier (bridge).

This happens because the connector struct device is the same as the bridge struct device, I suppose.

Isn't that OK? Please help me to see what's wrong with my architecture here, and what I need to do :/ I tried to follow examples set by MSM and Exynos DSI.

Yours,
Linus Walleij
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#24 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 80 lines · Linus Walleij
Quoted 6 lines · Jyri Sarha

Add device_link from panel device (supplier) to drm device (consumer) when drm_panel_attach() is called. This patch should protect the master drm driver if an attached panel driver unbinds while it is in use. The device_link should make sure the drm device is unbound before the panel driver becomes unavailable.

Quoted 6 lines

The device_link is removed when drm_panel_detach() is called. The drm_panel_detach() should be called by the consumer DRM driver, not the panel driver, otherwise both drivers are racing to delete the same link.

Hi I have a problem with an in-development driver and this patch.

Do not worry! It is no regression. I just need help to do things right now.

My panel is a DSI device on a DSI master:

                mcde@a0350000 {
                        status = "okay";

                        dsi@a0351000 {
                                port {
                                        dsi0_out: endpoint {
                                                remote-endpoint = <&panel_in>;
                                        };
                                };

                                panel: display {
                                        compatible = "samsung,s6d16d0";
                                        reg = <0>;
                                        /* VDD1 on the component */
                                        power-supply = <&ab8500_ldo_aux1_reg>;
                                        reset-gpios = <&gpio2 1

GPIO_ACTIVE_LOW>;

                                        port {
                                                panel_in: endpoint {
remote-endpoint = <&dsi0_out>;
                                                };
                                        };
                                };
                        };
                };

So in my DSI host driver .bind() I do:

    drm_encoder_init(drm, encoder, &mcde_dsi_encoder_funcs,
             DRM_MODE_ENCODER_DSI, NULL);
    drm_encoder_helper_add(encoder, &mcde_dsi_encoder_helper_funcs);
    ret = drm_connector_init(encoder->dev, connector,
                 &mcde_dsi_connector_funcs,
                 DRM_MODE_CONNECTOR_DSI);

    drm_connector_helper_add(connector, &mcde_dsi_connector_helper_funcs);
    drm_connector_attach_encoder(connector, encoder);
    drm_connector_register(connector);

This works fine. I managed to create the encoder and connector for this DSI. Then:

    ret = drm_of_find_panel_or_bridge(dev->of_node,
                      0, 0, &panel, &bridge);
    if (panel) {
        bridge = drm_panel_bridge_add(panel,
                          DRM_MODE_CONNECTOR_DSI);
        if (IS_ERR(bridge)) {
            dev_err(dev, "error adding panel bridge\n");
            return PTR_ERR(bridge);
        }
        dev_info(dev, "connected to panel\n");
        d->panel = panel;
    }
    d->connector.status = connector_status_connected;
    ret = drm_bridge_attach(encoder, bridge, NULL);
Quoted 7 lines

And this is where it blows up: it fails in device_link_add(connector->dev->dev, panel->dev, 0); device_is_dependent(consumer, supplier) because the consumer (connector) is dependent on the supplier (bridge).

This happens because the connector struct device is the same as the bridge struct device, I suppose.

I guess it is rather because the code tries to make circular dependency:
1. panel depends on dsi-host because it is MIPI-DSI child device.
2. dsi-host probably depends on drm parent device (connector->dev->dev)
- what drm driver do you use?
3. drm parent dev depends on panel: this patch adds this dependency.

If 2nd point is true it becomes circular dependency, but please verify it.

Regards
Andrzej

Quoted 11 lines

Isn't that OK? Please help me to see what's wrong with my architecture here, and what I need to do :/ I tried to follow examples set by MSM and Exynos DSI.

Yours,
Linus Walleij
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#25 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 8 lines · Andrzej Hajda

[Me]

Quoted 2 lines

This happens because the connector struct device is the same as the bridge struct device, I suppose.

I guess it is rather because the code tries to make circular dependency:
1. panel depends on dsi-host because it is MIPI-DSI child device.
2. dsi-host probably depends on drm parent device (connector->dev->dev)
- what drm driver do you use?

The driver is added in this patch, it's at this uncomfortable stage where I have to make a big upfront design for a new DRM driver and everything is shaky and unreviewed.

So to get it out for proper review it needs to be working and to get it working I need review :D DRM development catch 22.

But here is the patch adding it all, in some in-flight state:
https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-stericsson.git/commit/?h=ux500-mcde

Thanks a lot for these notes, I hope I can figure it out!

Quoted 3 lines

3. drm parent dev depends on panel: this patch adds this dependency.

If 2nd point is true it becomes circular dependency, but please verify it.

I tried to not make the DRM parent dev depend on the panel.

AFAICT (1) is true, (2) is true but not (3).

Yours,
Linus Walleij
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#26 lore raw

Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device

Quoted 18 lines · Linus Walleij
Quoted 7 lines · Andrzej Hajda

[Me]

Quoted 2 lines

This happens because the connector struct device is the same as the bridge struct device, I suppose.

I guess it is rather because the code tries to make circular dependency:
1. panel depends on dsi-host because it is MIPI-DSI child device.
2. dsi-host probably depends on drm parent device (connector->dev->dev)
- what drm driver do you use?

The driver is added in this patch, it's at this uncomfortable stage where I have to make a big upfront design for a new DRM driver and everything is shaky and unreviewed.

So to get it out for proper review it needs to be working and to get it working I need review :D DRM development catch 22.

But here is the patch adding it all, in some in-flight state:
https://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-stericsson.git/commit/?h=ux500-mcde

Quoted 8 lines

Thanks a lot for these notes, I hope I can figure it out!

Quoted 3 lines

3. drm parent dev depends on panel: this patch adds this dependency.

If 2nd point is true it becomes circular dependency, but please verify it.

I tried to not make the DRM parent dev depend on the panel.

AFAICT (1) is true, (2) is true but not (3).

See code of drm_panel_attach after subject patch [1]:
    panel->link = device_link_add(connector->dev->dev, panel->dev, 0);

It is quite clear that connector->dev->dev is 'drm_device parent device' so you have drm-parent depends on panel->dev.
IMO this is incorrect dependency, and the cause of error, it is not drm_device parent who depends on the panel, but drm_device itself.

[1]:
https://elixir.bootlin.com/linux/v4.19-rc1/source/drivers/gpu/drm/drm_panel.c#L108

Regards
Andrzej

Quoted 5 lines

Yours,
Linus Walleij

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#27 lore raw
In reply to Jyri Sarha
Quoted 3 lines · Jyri Sarha

I guess the first patch should be mergable. With the second, maybe it is better to wait until we have a full solution including the bridges too. What should I do to get the first patch merged?

I don't see a reason why patch 2 can't be applied as-is right now. The bridges can always be fixed in a separate patch.

Applied both of these to drm-misc-next, thanks.

Thierry

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#28 lore raw
Quoted 9 lines · Thierry Reding
Quoted 3 lines · Jyri Sarha

I guess the first patch should be mergable. With the second, maybe it is better to wait until we have a full solution including the bridges too. What should I do to get the first patch merged?

I don't see a reason why patch 2 can't be applied as-is right now. The bridges can always be fixed in a separate patch.

Applied both of these to drm-misc-next, thanks.

Hmm, so why have they not (yet?) landed in linux-next.git? It's been almost a month...

Cheers,
Peter

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel