All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/2] drm/panel: Add device link in drm_panel_attach()
@ 2018-04-26  8:06 Jyri Sarha
  2018-04-26  8:06 ` [PATCH v4 1/2] drm/panel: Remove drm_panel_detach() calls from all panel drives Jyri Sarha
                   ` (2 more replies)
  0 siblings, 3 replies; 29+ messages in thread
From: Jyri Sarha @ 2018-04-26  8:06 UTC (permalink / raw)
  To: dri-devel
  Cc: airlied, Jyri Sarha, tomi.valkeinen, thierry.reding,
	laurent.pinchart, peda

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(-)

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

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH v4 1/2] drm/panel: Remove drm_panel_detach() calls from all panel drives
  2018-04-26  8:06 [PATCH v4 0/2] drm/panel: Add device link in drm_panel_attach() Jyri Sarha
@ 2018-04-26  8:06 ` Jyri Sarha
  2018-04-26  8:07 ` [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device Jyri Sarha
  2018-04-26 12:49 ` [PATCH v4 0/2] drm/panel: Add device link in drm_panel_attach() Thierry Reding
  2 siblings, 0 replies; 29+ messages in thread
From: Jyri Sarha @ 2018-04-26  8:06 UTC (permalink / raw)
  To: dri-devel
  Cc: airlied, Jyri Sarha, tomi.valkeinen, thierry.reding,
	laurent.pinchart, peda

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.

Signed-off-by: Jyri Sarha <jsarha@ti.com>
Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
---
 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(-)

diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
index 308d442..71e4075 100644
--- a/drivers/gpu/drm/drm_panel.c
+++ b/drivers/gpu/drm/drm_panel.c
@@ -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)
diff --git a/drivers/gpu/drm/panel/panel-innolux-p079zca.c b/drivers/gpu/drm/panel/panel-innolux-p079zca.c
index 57df39b..bb53e08 100644
--- a/drivers/gpu/drm/panel/panel-innolux-p079zca.c
+++ b/drivers/gpu/drm/panel/panel-innolux-p079zca.c
@@ -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;
diff --git a/drivers/gpu/drm/panel/panel-jdi-lt070me05000.c b/drivers/gpu/drm/panel/panel-jdi-lt070me05000.c
index 0a94ab7..99caa78 100644
--- a/drivers/gpu/drm/panel/panel-jdi-lt070me05000.c
+++ b/drivers/gpu/drm/panel/panel-jdi-lt070me05000.c
@@ -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;
diff --git a/drivers/gpu/drm/panel/panel-lvds.c b/drivers/gpu/drm/panel/panel-lvds.c
index 5185819..8a16878 100644
--- a/drivers/gpu/drm/panel/panel-lvds.c
+++ b/drivers/gpu/drm/panel/panel-lvds.c
@@ -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);
diff --git a/drivers/gpu/drm/panel/panel-panasonic-vvx10f034n00.c b/drivers/gpu/drm/panel/panel-panasonic-vvx10f034n00.c
index 74a8061..cb4dfb9 100644
--- a/drivers/gpu/drm/panel/panel-panasonic-vvx10f034n00.c
+++ b/drivers/gpu/drm/panel/panel-panasonic-vvx10f034n00.c
@@ -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;
diff --git a/drivers/gpu/drm/panel/panel-seiko-43wvf1g.c b/drivers/gpu/drm/panel/panel-seiko-43wvf1g.c
index 71c09ed..75f9253 100644
--- a/drivers/gpu/drm/panel/panel-seiko-43wvf1g.c
+++ b/drivers/gpu/drm/panel/panel-seiko-43wvf1g.c
@@ -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);
diff --git a/drivers/gpu/drm/panel/panel-sharp-lq101r1sx01.c b/drivers/gpu/drm/panel/panel-sharp-lq101r1sx01.c
index 6bf8730..02fc0f5 100644
--- a/drivers/gpu/drm/panel/panel-sharp-lq101r1sx01.c
+++ b/drivers/gpu/drm/panel/panel-sharp-lq101r1sx01.c
@@ -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;
diff --git a/drivers/gpu/drm/panel/panel-sharp-ls043t1le01.c b/drivers/gpu/drm/panel/panel-sharp-ls043t1le01.c
index 494aa9b..e5cae00 100644
--- a/drivers/gpu/drm/panel/panel-sharp-ls043t1le01.c
+++ b/drivers/gpu/drm/panel/panel-sharp-ls043t1le01.c
@@ -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;
diff --git a/drivers/gpu/drm/panel/panel-simple.c b/drivers/gpu/drm/panel/panel-simple.c
index f2d96611..9388acf 100644
--- a/drivers/gpu/drm/panel/panel-simple.c
+++ b/drivers/gpu/drm/panel/panel-simple.c
@@ -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);
diff --git a/drivers/gpu/drm/panel/panel-sitronix-st7789v.c b/drivers/gpu/drm/panel/panel-sitronix-st7789v.c
index 358c64e..74284e5 100644
--- a/drivers/gpu/drm/panel/panel-sitronix-st7789v.c
+++ b/drivers/gpu/drm/panel/panel-sitronix-st7789v.c
@@ -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)
-- 
Texas Instruments Finland Oy, Porkkalankatu 22, 00180 Helsinki. Y-tunnus/Business ID: 0615521-4. Kotipaikka/Domicile: Helsinki

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-04-26  8:06 [PATCH v4 0/2] drm/panel: Add device link in drm_panel_attach() Jyri Sarha
  2018-04-26  8:06 ` [PATCH v4 1/2] drm/panel: Remove drm_panel_detach() calls from all panel drives Jyri Sarha
@ 2018-04-26  8:07 ` Jyri Sarha
  2018-05-18 11:51   ` Andrzej Hajda
                     ` (2 more replies)
  2018-04-26 12:49 ` [PATCH v4 0/2] drm/panel: Add device link in drm_panel_attach() Thierry Reding
  2 siblings, 3 replies; 29+ messages in thread
From: Jyri Sarha @ 2018-04-26  8:07 UTC (permalink / raw)
  To: dri-devel
  Cc: airlied, Jyri Sarha, tomi.valkeinen, thierry.reding,
	laurent.pinchart, peda

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.

Signed-off-by: Jyri Sarha <jsarha@ti.com>
Reviewed-by: Eric Anholt <eric@anholt.net>
---
 drivers/gpu/drm/drm_panel.c | 10 ++++++++++
 include/drm/drm_panel.h     |  1 +
 2 files changed, 11 insertions(+)

diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
index 71e4075..965530a 100644
--- a/drivers/gpu/drm/drm_panel.c
+++ b/drivers/gpu/drm/drm_panel.c
@@ -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;
 
diff --git a/include/drm/drm_panel.h b/include/drm/drm_panel.h
index 14ac240..26a1b5f 100644
--- a/include/drm/drm_panel.h
+++ b/include/drm/drm_panel.h
@@ -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

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 0/2] drm/panel: Add device link in drm_panel_attach()
  2018-04-26  8:06 [PATCH v4 0/2] drm/panel: Add device link in drm_panel_attach() Jyri Sarha
  2018-04-26  8:06 ` [PATCH v4 1/2] drm/panel: Remove drm_panel_detach() calls from all panel drives Jyri Sarha
  2018-04-26  8:07 ` [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device Jyri Sarha
@ 2018-04-26 12:49 ` Thierry Reding
  2018-05-21  6:09   ` Peter Rosin
  2 siblings, 1 reply; 29+ messages in thread
From: Thierry Reding @ 2018-04-26 12:49 UTC (permalink / raw)
  To: Jyri Sarha; +Cc: airlied, tomi.valkeinen, dri-devel, laurent.pinchart, peda


[-- Attachment #1.1: Type: text/plain, Size: 438 bytes --]

On Thu, Apr 26, 2018 at 11:06:58AM +0300, Jyri Sarha wrote:
> 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

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

[-- Attachment #2: Type: text/plain, Size: 160 bytes --]

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-04-26  8:07 ` [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device Jyri Sarha
@ 2018-05-18 11:51   ` Andrzej Hajda
  2018-05-18 12:54     ` Jyri Sarha
                       ` (2 more replies)
  2018-08-09  8:56   ` Daniel Vetter
  2018-08-30 12:32   ` Linus Walleij
  2 siblings, 3 replies; 29+ messages in thread
From: Andrzej Hajda @ 2018-05-18 11:51 UTC (permalink / raw)
  To: Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart, peda

On 26.04.2018 10:07, Jyri Sarha wrote:
> 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.
>
> Signed-off-by: Jyri Sarha <jsarha@ti.com>
> Reviewed-by: Eric Anholt <eric@anholt.net>

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

> ---
>  drivers/gpu/drm/drm_panel.c | 10 ++++++++++
>  include/drm/drm_panel.h     |  1 +
>  2 files changed, 11 insertions(+)
>
> diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
> index 71e4075..965530a 100644
> --- a/drivers/gpu/drm/drm_panel.c
> +++ b/drivers/gpu/drm/drm_panel.c
> @@ -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;
>  
> diff --git a/include/drm/drm_panel.h b/include/drm/drm_panel.h
> index 14ac240..26a1b5f 100644
> --- a/include/drm/drm_panel.h
> +++ b/include/drm/drm_panel.h
> @@ -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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-18 11:51   ` Andrzej Hajda
@ 2018-05-18 12:54     ` Jyri Sarha
  2018-05-19 16:48     ` Peter Rosin
  2019-10-22  8:44     ` Daniel Vetter
  2 siblings, 0 replies; 29+ messages in thread
From: Jyri Sarha @ 2018-05-18 12:54 UTC (permalink / raw)
  To: Andrzej Hajda, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart, peda

On 18/05/18 14:51, Andrzej Hajda wrote:
> On 26.04.2018 10:07, Jyri Sarha wrote:
>> 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.
>>
>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>> Reviewed-by: Eric Anholt <eric@anholt.net>
> 
> 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?
> 

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.

> 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

> 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
> 
>> ---
>>  drivers/gpu/drm/drm_panel.c | 10 ++++++++++
>>  include/drm/drm_panel.h     |  1 +
>>  2 files changed, 11 insertions(+)
>>
>> diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
>> index 71e4075..965530a 100644
>> --- a/drivers/gpu/drm/drm_panel.c
>> +++ b/drivers/gpu/drm/drm_panel.c
>> @@ -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;
>>  
>> diff --git a/include/drm/drm_panel.h b/include/drm/drm_panel.h
>> index 14ac240..26a1b5f 100644
>> --- a/include/drm/drm_panel.h
>> +++ b/include/drm/drm_panel.h
>> @@ -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
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-18 11:51   ` Andrzej Hajda
  2018-05-18 12:54     ` Jyri Sarha
@ 2018-05-19 16:48     ` Peter Rosin
  2018-05-19 18:01       ` Peter Rosin
  2018-05-21  8:15       ` Andrzej Hajda
  2019-10-22  8:44     ` Daniel Vetter
  2 siblings, 2 replies; 29+ messages in thread
From: Peter Rosin @ 2018-05-19 16:48 UTC (permalink / raw)
  To: Andrzej Hajda, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 2018-05-18 13:51, Andrzej Hajda wrote:
> On 26.04.2018 10:07, Jyri Sarha wrote:
>> 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.
>>
>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>> Reviewed-by: Eric Anholt <eric@anholt.net>
> 
> 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.

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

> 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
> 
>> ---
>>  drivers/gpu/drm/drm_panel.c | 10 ++++++++++
>>  include/drm/drm_panel.h     |  1 +
>>  2 files changed, 11 insertions(+)
>>
>> diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
>> index 71e4075..965530a 100644
>> --- a/drivers/gpu/drm/drm_panel.c
>> +++ b/drivers/gpu/drm/drm_panel.c
>> @@ -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;
>>  
>> diff --git a/include/drm/drm_panel.h b/include/drm/drm_panel.h
>> index 14ac240..26a1b5f 100644
>> --- a/include/drm/drm_panel.h
>> +++ b/include/drm/drm_panel.h
>> @@ -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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-19 16:48     ` Peter Rosin
@ 2018-05-19 18:01       ` Peter Rosin
  2018-05-21  8:15       ` Andrzej Hajda
  1 sibling, 0 replies; 29+ messages in thread
From: Peter Rosin @ 2018-05-19 18:01 UTC (permalink / raw)
  To: Andrzej Hajda, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 2018-05-19 18:48, Peter Rosin wrote:
> On 2018-05-18 13:51, Andrzej Hajda wrote:
>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>> 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.
>>>
>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>>
>> 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.
> 
> 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

s/either consumer or supplier/the consumer/

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

> 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
> 
>> 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
>>
>>> ---
>>>  drivers/gpu/drm/drm_panel.c | 10 ++++++++++
>>>  include/drm/drm_panel.h     |  1 +
>>>  2 files changed, 11 insertions(+)
>>>
>>> diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
>>> index 71e4075..965530a 100644
>>> --- a/drivers/gpu/drm/drm_panel.c
>>> +++ b/drivers/gpu/drm/drm_panel.c
>>> @@ -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;
>>>  
>>> diff --git a/include/drm/drm_panel.h b/include/drm/drm_panel.h
>>> index 14ac240..26a1b5f 100644
>>> --- a/include/drm/drm_panel.h
>>> +++ b/include/drm/drm_panel.h
>>> @@ -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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 0/2] drm/panel: Add device link in drm_panel_attach()
  2018-04-26 12:49 ` [PATCH v4 0/2] drm/panel: Add device link in drm_panel_attach() Thierry Reding
@ 2018-05-21  6:09   ` Peter Rosin
  0 siblings, 0 replies; 29+ messages in thread
From: Peter Rosin @ 2018-05-21  6:09 UTC (permalink / raw)
  To: Thierry Reding, Jyri Sarha
  Cc: airlied, tomi.valkeinen, dri-devel, laurent.pinchart

On 2018-04-26 14:49, Thierry Reding wrote:
> On Thu, Apr 26, 2018 at 11:06:58AM +0300, Jyri Sarha wrote:
>> 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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-19 16:48     ` Peter Rosin
  2018-05-19 18:01       ` Peter Rosin
@ 2018-05-21  8:15       ` Andrzej Hajda
  2018-05-21  8:53         ` Peter Rosin
  1 sibling, 1 reply; 29+ messages in thread
From: Andrzej Hajda @ 2018-05-21  8:15 UTC (permalink / raw)
  To: Peter Rosin, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 19.05.2018 18:48, Peter Rosin wrote:
> On 2018-05-18 13:51, Andrzej Hajda wrote:
>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>> 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.
>>>
>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>> 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.
> 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.

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.

>
> 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.

>
> 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


>
> Cheers,
> Peter
>
>> 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
>>
>>> ---
>>>  drivers/gpu/drm/drm_panel.c | 10 ++++++++++
>>>  include/drm/drm_panel.h     |  1 +
>>>  2 files changed, 11 insertions(+)
>>>
>>> diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
>>> index 71e4075..965530a 100644
>>> --- a/drivers/gpu/drm/drm_panel.c
>>> +++ b/drivers/gpu/drm/drm_panel.c
>>> @@ -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;
>>>  
>>> diff --git a/include/drm/drm_panel.h b/include/drm/drm_panel.h
>>> index 14ac240..26a1b5f 100644
>>> --- a/include/drm/drm_panel.h
>>> +++ b/include/drm/drm_panel.h
>>> @@ -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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-21  8:15       ` Andrzej Hajda
@ 2018-05-21  8:53         ` Peter Rosin
  2018-05-21  9:21           ` Andrzej Hajda
  0 siblings, 1 reply; 29+ messages in thread
From: Peter Rosin @ 2018-05-21  8:53 UTC (permalink / raw)
  To: Andrzej Hajda, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 2018-05-21 10:15, Andrzej Hajda wrote:
> On 19.05.2018 18:48, Peter Rosin wrote:
>> On 2018-05-18 13:51, Andrzej Hajda wrote:
>>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>>> 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.
>>>>
>>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>>> 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.
>> 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.
> 
> 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.
> 
>>
>> 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.

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...

> 
>>
>> 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?

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

Cheers,
Peter

> Regards
> Andrzej
> 
> 
>>
>> Cheers,
>> Peter
>>
>>> 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
>>>
>>>> ---
>>>>  drivers/gpu/drm/drm_panel.c | 10 ++++++++++
>>>>  include/drm/drm_panel.h     |  1 +
>>>>  2 files changed, 11 insertions(+)
>>>>
>>>> diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
>>>> index 71e4075..965530a 100644
>>>> --- a/drivers/gpu/drm/drm_panel.c
>>>> +++ b/drivers/gpu/drm/drm_panel.c
>>>> @@ -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;
>>>>  
>>>> diff --git a/include/drm/drm_panel.h b/include/drm/drm_panel.h
>>>> index 14ac240..26a1b5f 100644
>>>> --- a/include/drm/drm_panel.h
>>>> +++ b/include/drm/drm_panel.h
>>>> @@ -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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-21  8:53         ` Peter Rosin
@ 2018-05-21  9:21           ` Andrzej Hajda
  2018-05-21 21:56             ` Peter Rosin
  0 siblings, 1 reply; 29+ messages in thread
From: Andrzej Hajda @ 2018-05-21  9:21 UTC (permalink / raw)
  To: Peter Rosin, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 21.05.2018 10:53, Peter Rosin wrote:
> On 2018-05-21 10:15, Andrzej Hajda wrote:
>> On 19.05.2018 18:48, Peter Rosin wrote:
>>> On 2018-05-18 13:51, Andrzej Hajda wrote:
>>>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>>>> 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.
>>>>>
>>>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>>>> 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.
>>> 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.
>> 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.
>>
>>> 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.
> 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.

> 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...
>
>>> 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?
> 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

>
> Cheers,
> Peter
>
>> Regards
>> Andrzej
>>
>>
>>> Cheers,
>>> Peter
>>>
>>>> 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
>>>>
>>>>> ---
>>>>>  drivers/gpu/drm/drm_panel.c | 10 ++++++++++
>>>>>  include/drm/drm_panel.h     |  1 +
>>>>>  2 files changed, 11 insertions(+)
>>>>>
>>>>> diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
>>>>> index 71e4075..965530a 100644
>>>>> --- a/drivers/gpu/drm/drm_panel.c
>>>>> +++ b/drivers/gpu/drm/drm_panel.c
>>>>> @@ -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;
>>>>>  
>>>>> diff --git a/include/drm/drm_panel.h b/include/drm/drm_panel.h
>>>>> index 14ac240..26a1b5f 100644
>>>>> --- a/include/drm/drm_panel.h
>>>>> +++ b/include/drm/drm_panel.h
>>>>> @@ -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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-21  9:21           ` Andrzej Hajda
@ 2018-05-21 21:56             ` Peter Rosin
  2018-05-22  6:29               ` Andrzej Hajda
  0 siblings, 1 reply; 29+ messages in thread
From: Peter Rosin @ 2018-05-21 21:56 UTC (permalink / raw)
  To: Andrzej Hajda, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 2018-05-21 11:21, Andrzej Hajda wrote:
> On 21.05.2018 10:53, Peter Rosin wrote:
>> On 2018-05-21 10:15, Andrzej Hajda wrote:
>>> On 19.05.2018 18:48, Peter Rosin wrote:
>>>> On 2018-05-18 13:51, Andrzej Hajda wrote:
>>>>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>>>>> 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.
>>>>>>
>>>>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>>>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>>>>> 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.
>>>> 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.
>>> 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.
>>>
>>>> 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.
>> 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.
> 
>> 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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-21 21:56             ` Peter Rosin
@ 2018-05-22  6:29               ` Andrzej Hajda
  2018-05-22  7:36                 ` Peter Rosin
  0 siblings, 1 reply; 29+ messages in thread
From: Andrzej Hajda @ 2018-05-22  6:29 UTC (permalink / raw)
  To: Peter Rosin, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 21.05.2018 23:56, Peter Rosin wrote:
> On 2018-05-21 11:21, Andrzej Hajda wrote:
>> On 21.05.2018 10:53, Peter Rosin wrote:
>>> On 2018-05-21 10:15, Andrzej Hajda wrote:
>>>> On 19.05.2018 18:48, Peter Rosin wrote:
>>>>> On 2018-05-18 13:51, Andrzej Hajda wrote:
>>>>>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>>>>>> 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.
>>>>>>>
>>>>>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>>>>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>>>>>> 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.
>>>>> 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.
>>>> 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.
>>>>
>>>>> 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.
>>> 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.
>>
>>> 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?

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

> 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.

>
> 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().

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


>
> Cheers,
> Peter
>
>
>

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-22  6:29               ` Andrzej Hajda
@ 2018-05-22  7:36                 ` Peter Rosin
  2018-05-22  8:37                   ` Peter Rosin
  2018-05-22  9:45                   ` Andrzej Hajda
  0 siblings, 2 replies; 29+ messages in thread
From: Peter Rosin @ 2018-05-22  7:36 UTC (permalink / raw)
  To: Andrzej Hajda, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 2018-05-22 08:29, Andrzej Hajda wrote:
> On 21.05.2018 23:56, Peter Rosin wrote:
>> On 2018-05-21 11:21, Andrzej Hajda wrote:
>>> On 21.05.2018 10:53, Peter Rosin wrote:
>>>> On 2018-05-21 10:15, Andrzej Hajda wrote:
>>>>> On 19.05.2018 18:48, Peter Rosin wrote:
>>>>>> On 2018-05-18 13:51, Andrzej Hajda wrote:
>>>>>>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>>>>>>> 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.
>>>>>>>>
>>>>>>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>>>>>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>>>>>>> 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.
>>>>>> 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.
>>>>> 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.
>>>>>
>>>>>> 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.
>>>> 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.
>>>
>>>> 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?
> 
> I wouldn't call it assumption, I would say rather it is a common practice.
> 
>> 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.

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.

>>
>> 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().
> 
> 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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-22  7:36                 ` Peter Rosin
@ 2018-05-22  8:37                   ` Peter Rosin
  2018-05-22  9:45                   ` Andrzej Hajda
  1 sibling, 0 replies; 29+ messages in thread
From: Peter Rosin @ 2018-05-22  8:37 UTC (permalink / raw)
  To: Andrzej Hajda, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 2018-05-22 09:36, Peter Rosin wrote:
> On 2018-05-22 08:29, Andrzej Hajda wrote:
>> On 21.05.2018 23:56, Peter Rosin wrote:
>>> On 2018-05-21 11:21, Andrzej Hajda wrote:
>>>> On 21.05.2018 10:53, Peter Rosin wrote:
>>>>> On 2018-05-21 10:15, Andrzej Hajda wrote:
>>>>>> On 19.05.2018 18:48, Peter Rosin wrote:
>>>>>>> On 2018-05-18 13:51, Andrzej Hajda wrote:
>>>>>>>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>>>>>>>> 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.
>>>>>>>>>
>>>>>>>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>>>>>>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>>>>>>>> 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.
>>>>>>> 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.
>>>>>> 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.
>>>>>>
>>>>>>> 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.
>>>>> 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.
>>>>
>>>>> 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?
>>
>> I wouldn't call it assumption, I would say rather it is a common practice.
>>
>>> 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.
> 
> 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.
> 
>>>
>>> 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().
>>
>> 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.

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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-22  7:36                 ` Peter Rosin
  2018-05-22  8:37                   ` Peter Rosin
@ 2018-05-22  9:45                   ` Andrzej Hajda
  2018-05-22 15:03                     ` Peter Rosin
  1 sibling, 1 reply; 29+ messages in thread
From: Andrzej Hajda @ 2018-05-22  9:45 UTC (permalink / raw)
  To: Peter Rosin, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 22.05.2018 09:36, Peter Rosin wrote:
> On 2018-05-22 08:29, Andrzej Hajda wrote:
>> On 21.05.2018 23:56, Peter Rosin wrote:
>>> On 2018-05-21 11:21, Andrzej Hajda wrote:
>>>> On 21.05.2018 10:53, Peter Rosin wrote:
>>>>> On 2018-05-21 10:15, Andrzej Hajda wrote:
>>>>>> On 19.05.2018 18:48, Peter Rosin wrote:
>>>>>>> On 2018-05-18 13:51, Andrzej Hajda wrote:
>>>>>>>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>>>>>>>> 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.
>>>>>>>>>
>>>>>>>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>>>>>>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>>>>>>>> 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.
>>>>>>> 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.
>>>>>> 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.
>>>>>>
>>>>>>> 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.
>>>>> 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.
>>>>
>>>>> 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?
>> I wouldn't call it assumption, I would say rather it is a common practice.
>>
>>> 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.
> 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.
>
>>> 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().
>> 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.

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.

>
> 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

>
> Cheers,
> Peter
>
>
>

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-22  9:45                   ` Andrzej Hajda
@ 2018-05-22 15:03                     ` Peter Rosin
  2018-05-23  8:29                       ` Peter Rosin
  0 siblings, 1 reply; 29+ messages in thread
From: Peter Rosin @ 2018-05-22 15:03 UTC (permalink / raw)
  To: Andrzej Hajda, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 2018-05-22 11:45, Andrzej Hajda wrote:
> On 22.05.2018 09:36, Peter Rosin wrote:
>> On 2018-05-22 08:29, Andrzej Hajda wrote:
>>> On 21.05.2018 23:56, Peter Rosin wrote:
>>>> On 2018-05-21 11:21, Andrzej Hajda wrote:
>>>>> On 21.05.2018 10:53, Peter Rosin wrote:
>>>>>> On 2018-05-21 10:15, Andrzej Hajda wrote:
>>>>>>> On 19.05.2018 18:48, Peter Rosin wrote:
>>>>>>>> On 2018-05-18 13:51, Andrzej Hajda wrote:
>>>>>>>>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>>>>>>>>> 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.
>>>>>>>>>>
>>>>>>>>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>>>>>>>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>>>>>>>>> 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.
>>>>>>>> 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.
>>>>>>> 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.
>>>>>>>
>>>>>>>> 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.
>>>>>> 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.
>>>>>
>>>>>> 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?
>>> I wouldn't call it assumption, I would say rather it is a common practice.
>>>
>>>> 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.
>> 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.
>>
>>>> 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().
>>> 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.
> 
> 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.
> 
>>
>> 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.

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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-22 15:03                     ` Peter Rosin
@ 2018-05-23  8:29                       ` Peter Rosin
  2018-05-23  9:39                         ` Andrzej Hajda
  0 siblings, 1 reply; 29+ messages in thread
From: Peter Rosin @ 2018-05-23  8:29 UTC (permalink / raw)
  To: Andrzej Hajda, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 2018-05-22 17:03, Peter Rosin wrote:
> On 2018-05-22 11:45, Andrzej Hajda wrote:
>> On 22.05.2018 09:36, Peter Rosin wrote:
>>> On 2018-05-22 08:29, Andrzej Hajda wrote:
>>>> On 21.05.2018 23:56, Peter Rosin wrote:
>>>>> On 2018-05-21 11:21, Andrzej Hajda wrote:
>>>>>> On 21.05.2018 10:53, Peter Rosin wrote:
>>>>>>> On 2018-05-21 10:15, Andrzej Hajda wrote:
>>>>>>>> On 19.05.2018 18:48, Peter Rosin wrote:
>>>>>>>>> On 2018-05-18 13:51, Andrzej Hajda wrote:
>>>>>>>>>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>>>>>>>>>> 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.
>>>>>>>>>>>
>>>>>>>>>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>>>>>>>>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>>>>>>>>>> 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.
>>>>>>>>> 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.
>>>>>>>> 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.
>>>>>>>>
>>>>>>>>> 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.
>>>>>>> 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.
>>>>>>
>>>>>>> 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?
>>>> I wouldn't call it assumption, I would say rather it is a common practice.
>>>>
>>>>> 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.
>>> 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.
>>>
>>>>> 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().
>>>> 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.
>>
>> 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.
>>
>>>
>>> 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.
> 
> 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.

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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-23  8:29                       ` Peter Rosin
@ 2018-05-23  9:39                         ` Andrzej Hajda
  2018-05-23 10:46                           ` Peter Rosin
  0 siblings, 1 reply; 29+ messages in thread
From: Andrzej Hajda @ 2018-05-23  9:39 UTC (permalink / raw)
  To: Peter Rosin, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 23.05.2018 10:29, Peter Rosin wrote:
> On 2018-05-22 17:03, Peter Rosin wrote:
>> On 2018-05-22 11:45, Andrzej Hajda wrote:
>>> On 22.05.2018 09:36, Peter Rosin wrote:
>>>> On 2018-05-22 08:29, Andrzej Hajda wrote:
>>>>> On 21.05.2018 23:56, Peter Rosin wrote:
>>>>>> On 2018-05-21 11:21, Andrzej Hajda wrote:
>>>>>>> On 21.05.2018 10:53, Peter Rosin wrote:
>>>>>>>> On 2018-05-21 10:15, Andrzej Hajda wrote:
>>>>>>>>> On 19.05.2018 18:48, Peter Rosin wrote:
>>>>>>>>>> On 2018-05-18 13:51, Andrzej Hajda wrote:
>>>>>>>>>>> On 26.04.2018 10:07, Jyri Sarha wrote:
>>>>>>>>>>>> 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.
>>>>>>>>>>>>
>>>>>>>>>>>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>>>>>>>>>>>> Reviewed-by: Eric Anholt <eric@anholt.net>
>>>>>>>>>>> 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.
>>>>>>>>>> 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.
>>>>>>>>> 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.
>>>>>>>>>
>>>>>>>>>> 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.
>>>>>>>> 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.
>>>>>>>
>>>>>>>> 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?
>>>>> I wouldn't call it assumption, I would say rather it is a common practice.
>>>>>
>>>>>> 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.
>>>> 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.
>>>>
>>>>>> 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().
>>>>> 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.
>>> 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.
>>>
>>>> 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.
>> 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.
>> 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.

>>
>> 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.

>  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;
> }

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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-23  9:39                         ` Andrzej Hajda
@ 2018-05-23 10:46                           ` Peter Rosin
  2018-05-23 12:38                             ` Andrzej Hajda
  0 siblings, 1 reply; 29+ messages in thread
From: Peter Rosin @ 2018-05-23 10:46 UTC (permalink / raw)
  To: Andrzej Hajda, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

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

*snip*

> 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*

> 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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-23 10:46                           ` Peter Rosin
@ 2018-05-23 12:38                             ` Andrzej Hajda
  2018-05-23 14:34                               ` Peter Rosin
  0 siblings, 1 reply; 29+ messages in thread
From: Andrzej Hajda @ 2018-05-23 12:38 UTC (permalink / raw)
  To: Peter Rosin, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 23.05.2018 12:46, Peter Rosin wrote:
> On 2018-05-23 11:39, Andrzej Hajda wrote:
>
> *snip*
>
>> 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*
>
>> 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()?

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

>
> 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.

>
> 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,....

>
> 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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-23 12:38                             ` Andrzej Hajda
@ 2018-05-23 14:34                               ` Peter Rosin
  0 siblings, 0 replies; 29+ messages in thread
From: Peter Rosin @ 2018-05-23 14:34 UTC (permalink / raw)
  To: Andrzej Hajda, Jyri Sarha, dri-devel
  Cc: airlied, Rafael J. Wysocki, tomi.valkeinen, thierry.reding,
	laurent.pinchart

On 2018-05-23 14:38, Andrzej Hajda wrote:
> On 23.05.2018 12:46, Peter Rosin wrote:
>> On 2018-05-23 11:39, Andrzej Hajda wrote:
>>
>> *snip*
>>
>>> 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*
>>
>>> 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()?
> 
> It should be at least there, but I suppose only there.
> 
>>
>> 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.

>>
>> 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.

>>
>> 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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-04-26  8:07 ` [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device Jyri Sarha
  2018-05-18 11:51   ` Andrzej Hajda
@ 2018-08-09  8:56   ` Daniel Vetter
  2018-08-30 12:32   ` Linus Walleij
  2 siblings, 0 replies; 29+ messages in thread
From: Daniel Vetter @ 2018-08-09  8:56 UTC (permalink / raw)
  To: Jyri Sarha
  Cc: Dave Airlie, dri-devel, Tomi Valkeinen, Thierry Reding,
	Laurent Pinchart, Peter Rosin

On Thu, Apr 26, 2018 at 10:07 AM, Jyri Sarha <jsarha@ti.com> wrote:
> 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.
>
> Signed-off-by: Jyri Sarha <jsarha@ti.com>
> Reviewed-by: Eric Anholt <eric@anholt.net>

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

> ---
>  drivers/gpu/drm/drm_panel.c | 10 ++++++++++
>  include/drm/drm_panel.h     |  1 +
>  2 files changed, 11 insertions(+)
>
> diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
> index 71e4075..965530a 100644
> --- a/drivers/gpu/drm/drm_panel.c
> +++ b/drivers/gpu/drm/drm_panel.c
> @@ -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;
>
> diff --git a/include/drm/drm_panel.h b/include/drm/drm_panel.h
> index 14ac240..26a1b5f 100644
> --- a/include/drm/drm_panel.h
> +++ b/include/drm/drm_panel.h
> @@ -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
>



-- 
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-04-26  8:07 ` [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device Jyri Sarha
  2018-05-18 11:51   ` Andrzej Hajda
  2018-08-09  8:56   ` Daniel Vetter
@ 2018-08-30 12:32   ` Linus Walleij
  2018-08-30 14:41     ` Andrzej Hajda
  2 siblings, 1 reply; 29+ messages in thread
From: Linus Walleij @ 2018-08-30 12:32 UTC (permalink / raw)
  To: Jyri Sarha
  Cc: Dave Airlie, open list:DRM PANEL DRIVERS, Tomi Valkeinen,
	thierry.reding, Laurent Pinchart, Peter Rosin

On Thu, Apr 26, 2018 at 10:07 AM Jyri Sarha <jsarha@ti.com> wrote:

> 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.
>
> Signed-off-by: Jyri Sarha <jsarha@ti.com>
> Reviewed-by: Eric Anholt <eric@anholt.net>

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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-08-30 12:32   ` Linus Walleij
@ 2018-08-30 14:41     ` Andrzej Hajda
  2018-08-31  8:56       ` Linus Walleij
  0 siblings, 1 reply; 29+ messages in thread
From: Andrzej Hajda @ 2018-08-30 14:41 UTC (permalink / raw)
  To: Linus Walleij, Jyri Sarha
  Cc: Dave Airlie, open list:DRM PANEL DRIVERS, Tomi Valkeinen,
	thierry.reding, Laurent Pinchart, Peter Rosin

On 30.08.2018 14:32, Linus Walleij wrote:
> On Thu, Apr 26, 2018 at 10:07 AM Jyri Sarha <jsarha@ti.com> wrote:
>
>> 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.
>>
>> Signed-off-by: Jyri Sarha <jsarha@ti.com>
>> Reviewed-by: Eric Anholt <eric@anholt.net>
> 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.

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

>
> 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
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel


_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-08-30 14:41     ` Andrzej Hajda
@ 2018-08-31  8:56       ` Linus Walleij
  2018-08-31 10:08         ` Andrzej Hajda
  0 siblings, 1 reply; 29+ messages in thread
From: Linus Walleij @ 2018-08-31  8:56 UTC (permalink / raw)
  To: Andrzej Hajda
  Cc: Dave Airlie, open list:DRM PANEL DRIVERS, Tomi Valkeinen,
	thierry.reding, Jyri Sarha, Peter Rosin, Laurent Pinchart

On Thu, Aug 30, 2018 at 4:41 PM Andrzej Hajda <a.hajda@samsung.com> wrote:
> [Me]
> > 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!

> 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
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-08-31  8:56       ` Linus Walleij
@ 2018-08-31 10:08         ` Andrzej Hajda
  0 siblings, 0 replies; 29+ messages in thread
From: Andrzej Hajda @ 2018-08-31 10:08 UTC (permalink / raw)
  To: Linus Walleij
  Cc: Dave Airlie, open list:DRM PANEL DRIVERS, Tomi Valkeinen,
	thierry.reding, Jyri Sarha, Peter Rosin, Laurent Pinchart

On 31.08.2018 10:56, Linus Walleij wrote:
> On Thu, Aug 30, 2018 at 4:41 PM Andrzej Hajda <a.hajda@samsung.com> wrote:
>> [Me]
>>> 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!
>
>> 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

>
> Yours,
> Linus Walleij
>
>

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device
  2018-05-18 11:51   ` Andrzej Hajda
  2018-05-18 12:54     ` Jyri Sarha
  2018-05-19 16:48     ` Peter Rosin
@ 2019-10-22  8:44     ` Daniel Vetter
  2 siblings, 0 replies; 29+ messages in thread
From: Daniel Vetter @ 2019-10-22  8:44 UTC (permalink / raw)
  To: Andrzej Hajda
  Cc: Dave Airlie, Tomi Valkeinen, Rafael J. Wysocki, dri-devel,
	Thierry Reding, Jyri Sarha, Peter Rosin, Laurent Pinchart

On Fri, May 18, 2018 at 1:52 PM Andrzej Hajda <a.hajda@samsung.com> wrote:
>
> On 26.04.2018 10:07, Jyri Sarha wrote:
> > 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.
> >
> > Signed-off-by: Jyri Sarha <jsarha@ti.com>
> > Reviewed-by: Eric Anholt <eric@anholt.net>
>
> 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?

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

>
> 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
>
> > ---
> >  drivers/gpu/drm/drm_panel.c | 10 ++++++++++
> >  include/drm/drm_panel.h     |  1 +
> >  2 files changed, 11 insertions(+)
> >
> > diff --git a/drivers/gpu/drm/drm_panel.c b/drivers/gpu/drm/drm_panel.c
> > index 71e4075..965530a 100644
> > --- a/drivers/gpu/drm/drm_panel.c
> > +++ b/drivers/gpu/drm/drm_panel.c
> > @@ -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;
> >
> > diff --git a/include/drm/drm_panel.h b/include/drm/drm_panel.h
> > index 14ac240..26a1b5f 100644
> > --- a/include/drm/drm_panel.h
> > +++ b/include/drm/drm_panel.h
> > @@ -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
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel



-- 
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2019-10-22  8:45 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-26  8:06 [PATCH v4 0/2] drm/panel: Add device link in drm_panel_attach() Jyri Sarha
2018-04-26  8:06 ` [PATCH v4 1/2] drm/panel: Remove drm_panel_detach() calls from all panel drives Jyri Sarha
2018-04-26  8:07 ` [PATCH v4 2/2] drm/panel: Add device_link from panel device to drm device Jyri Sarha
2018-05-18 11:51   ` Andrzej Hajda
2018-05-18 12:54     ` Jyri Sarha
2018-05-19 16:48     ` Peter Rosin
2018-05-19 18:01       ` Peter Rosin
2018-05-21  8:15       ` Andrzej Hajda
2018-05-21  8:53         ` Peter Rosin
2018-05-21  9:21           ` Andrzej Hajda
2018-05-21 21:56             ` Peter Rosin
2018-05-22  6:29               ` Andrzej Hajda
2018-05-22  7:36                 ` Peter Rosin
2018-05-22  8:37                   ` Peter Rosin
2018-05-22  9:45                   ` Andrzej Hajda
2018-05-22 15:03                     ` Peter Rosin
2018-05-23  8:29                       ` Peter Rosin
2018-05-23  9:39                         ` Andrzej Hajda
2018-05-23 10:46                           ` Peter Rosin
2018-05-23 12:38                             ` Andrzej Hajda
2018-05-23 14:34                               ` Peter Rosin
2019-10-22  8:44     ` Daniel Vetter
2018-08-09  8:56   ` Daniel Vetter
2018-08-30 12:32   ` Linus Walleij
2018-08-30 14:41     ` Andrzej Hajda
2018-08-31  8:56       ` Linus Walleij
2018-08-31 10:08         ` Andrzej Hajda
2018-04-26 12:49 ` [PATCH v4 0/2] drm/panel: Add device link in drm_panel_attach() Thierry Reding
2018-05-21  6:09   ` Peter Rosin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.