On Tue, Jul 14, 2026 at 7:47 PM AngeloGioacchino Del Regno
<[email protected]> wrote:
>
> Add support for the MediaTek Display Controller's Asynchronous
> Direct Link Controller.
>
> This is responsible for managing internal I/O connections between
> multiple display controllers in a leader to follower(s) topology,
> adding the ability to enable data exchange between multimedia
> related controllers and the display controller(s), and between
> multiple display controllers, effectively changing the hardware
> capabilities of multiple display output paths.
>
> For example, this could be used to support multiple outputs with
> lower resolution and/or color depth, or to support less outputs
> but with high resolution and/or color depth, up to 8k60 in some
> MediaTek SoCs like MT8196.
>
> Moreover please note that, on new generation SoCs, (MT8196, MT8894,
> MT6991, MT6993, etc), the Asynchronous DirectLink (DL_ASYNC) sub
> component is required to be set up in order to achieve any display
> output at all as, differently from slightly older ones (MT8195)
> where a display controller reset resulted in default valid outputs
> being set, the new ones have a "muted" configuration by default.
>
> Signed-off-by: AngeloGioacchino Del Regno 
> <[email protected]>
> ---
>  drivers/gpu/drm/mediatek/Makefile             |   1 +
>  drivers/gpu/drm/mediatek/mtk_ddp_comp.c       |  42 +-
>  drivers/gpu/drm/mediatek/mtk_ddp_comp.h       |  33 +-
>  .../gpu/drm/mediatek/mtk_disp_directlink.c    | 434 ++++++++++++++++++
>  drivers/gpu/drm/mediatek/mtk_disp_drv.h       |  12 +
>  drivers/gpu/drm/mediatek/mtk_drm_drv.c        | 160 ++++++-
>  drivers/gpu/drm/mediatek/mtk_drm_drv.h        |   1 +
>  include/linux/soc/mediatek/mtk-mmsys.h        |   4 +
>  8 files changed, 661 insertions(+), 26 deletions(-)
>  create mode 100644 drivers/gpu/drm/mediatek/mtk_disp_directlink.c
>
[snip]
> +       of_port = of_graph_get_port_by_id(node, of_port_id);

`of_port` is never released with `of_node_put()` on any return path.

> +       if (!of_port)
> +               return -ENOENT;
> +
> +       for_each_of_graph_port_endpoint(of_port, ep)
> +               num_links++;
> +
> +       clks = devm_kcalloc(dev, num_links, sizeof(*clks), GFP_KERNEL);
> +       if (!clks)
> +               return -ENOMEM;
> +
> +       link_ids = devm_kcalloc(dev, num_links, sizeof(*link_ids), 
> GFP_KERNEL);
> +       if (!link_ids)
> +               return -ENOMEM;
> +
> +       i = 0;
> +       for_each_of_graph_port_endpoint(of_port, ep) {
> +               struct of_endpoint endpoint;
> +               u32 hw_port_id;
> +               int ret;
> +
> +               ret = of_graph_parse_endpoint(ep, &endpoint);
> +               if (ret)
> +                       continue;
> +
> +               ret = of_property_read_u32(ep, "mediatek,port-id", 
> &hw_port_id);
> +               link_ids[i] = ret ? endpoint.id : hw_port_id;
> +               clks[i] = devm_get_clk_from_child(dev, ep, NULL);

`clks[]` and `link_ids[]` are populated sequentially
(`0..num_links-1`), while mtk_direct_link_config() /
mtk_direct_link_clk_enable() etc. index them by `comp->inst_id`. If
endpoint `reg` values are sparse or don't start at 0, this reads out
of bounds or hits the wrong entry.

Maybe calculate `max_ep_id`, size the arrays by `max_ep_id + 1`, and
index by `endpoint.id` (with bounds checking), or look up the entry by
`endpoint.id`.

> +               if (IS_ERR(clks[i])) {

Early return inside `for_each_of_graph_port_endpoint()` leaks `ep`
(missing `of_node_put(ep)`).

> +                       return dev_err_probe(dev, PTR_ERR(clks[i]),
> +                                            "Could not get clock\n");
> +               }
> +               i++;
> +       }
> +       dl_port->clks = clks;
> +       dl_port->link_ids = link_ids;
> +       dl_port->num_links = num_links;
> +
> +       return 0;
> +}
> +
> +static int mtk_direct_link_probe(struct platform_device *pdev)
> +{
> +       struct device *dev = &pdev->dev;
> +       struct device_node *main_node = dev->of_node;
> +       struct mtk_direct_link *dl_async;
> +       int in_mtx_id, out_mtx_id;
> +       unsigned int num_ports;
> +       int i, ret;
> +
> +       dl_async = devm_kzalloc(dev, sizeof(*dl_async), GFP_KERNEL);
> +       if (!dl_async)
> +               return -ENOMEM;
> +
> +       dl_async->data = device_get_match_data(dev);
> +
> +       dl_async->regs = of_iomap(dev->of_node, 0);

of_iomap() returns NULL on failure rather than an ERR_PTR, and leaks
the mapping on unbind. Use devm_platform_ioremap_resource(pdev, 0).

> +       if (IS_ERR(dl_async->regs))
> +               return dev_err_probe(&pdev->dev, PTR_ERR(dl_async->regs),
> +                                    "Could not get regs\n");
> +
> +       num_ports = of_graph_get_port_count(main_node);
> +       if (num_ports == 0)
> +               return dev_err_probe(dev, -ENOENT, "No Input/Output ports 
> found.\n");
> +
> +       /*
> +        * Get the Trigger ID for DL_IN_0 and for DL_OUT_0, as all of the 
> other
> +        * HW ports have linearly increasing IDs.
> +        */
> +       in_mtx_id = mtk_ddp_comp_get_mutex_trigger(main_node, MTK_DISP_DL_IN);
> +       out_mtx_id = mtk_ddp_comp_get_mutex_trigger(main_node, 
> MTK_DISP_DL_OUT);
> +       switch (num_ports) {
> +       default:
> +               /* fallthrough */
> +       case 2:
> +               if (in_mtx_id && out_mtx_id) {
> +                       ret = 0;
> +                       break;
> +               }
> +
> +               ret = in_mtx_id < 0 ? in_mtx_id : out_mtx_id;
> +               break;
> +       case 1:
> +               if (in_mtx_id || out_mtx_id) {

mtk_ddp_comp_get_mutex_trigger() returns negative error codes, while
ID 0 is valid. Check with `>= 0` explicitly.

> +                       if (in_mtx_id < 0)
> +                               in_mtx_id = 0;
> +                       if (out_mtx_id < 0)
> +                               out_mtx_id = 0;
> +
> +                       ret = 0;
> +                       break;
> +               }
> +
> +               ret = in_mtx_id < 0 ? in_mtx_id : out_mtx_id;
> +               break;
> +       }
> +
> +       if (ret)
> +               return dev_err_probe(dev, ret, "Could not get %s port 0 
> trigger ID\n",
> +                                    in_mtx_id < 0 ? "input" : "output");
> +
> +       if (in_mtx_id > U8_MAX || out_mtx_id > U8_MAX)
> +               return dev_err_probe(dev, -EINVAL, "Implausible trigger ID 
> found\n");
> +
> +       /* IDs are now validated and safe to assign */
> +       dl_async->dl_port[MTK_DISP_DL_IN].p0_mtx_trig_id = in_mtx_id;
> +       dl_async->dl_port[MTK_DISP_DL_OUT].p0_mtx_trig_id = out_mtx_id;
> +
> +       i = 0;
> +       for_each_of_graph_port(main_node, port_node) {
> +               u32 port_id;
> +
> +               of_property_read_u32(port_node, "reg", &port_id);
> +               if (port_id > 1)
> +                       continue;
> +
> +               ret = mtk_direct_link_port_probe(dev, &dl_async->dl_port[i], 
> port_id);
> +               if (ret)

Pass `&dl_async->dl_port[port_id]` instead of `i`.

mtk_direct_link_get_port() indexes `dl_port[]` by MTK_DISP_DL_IN (0)
and MTK_DISP_DL_OUT (1), so on an output-only node (port@1), using `i`
(0) populates `dl_port[0]` and leaves `dl_port[MTK_DISP_DL_OUT]`
uninitialized.

Also, `port_id` is uninitialized if reg is missing, and early return
inside `for_each_of_graph_port()` leaks `port_node`.

> diff --git a/drivers/gpu/drm/mediatek/mtk_drm_drv.c 
> b/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> index 75b3fd56d6af..4f39df23c51e 100644
> --- a/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> +++ b/drivers/gpu/drm/mediatek/mtk_drm_drv.c
> @@ -804,10 +822,27 @@ static int mtk_drm_of_get_first_input(struct device 
> *dev, struct device_node *no
>         if (ret)
>                 return -ENOENT;
>
> -       inst_id = mtk_ddp_comp_get_id(ep_dev_node, comp_type);
> +       inst_id = mtk_ddp_comp_get_id(ep_dev_node, ep_in, comp_type);
>         if (inst_id < 0)
>                 return inst_id;
>
> +       if (comp_type == MTK_DISP_DIRECT_LINK) {
> +               struct device_node *remote_port = 
> of_graph_get_remote_port(ep_in);
> +
> +               /* If there's a remote port this input is active, otherwise 
> it's unused */
> +               if (!remote_port)
> +                       return -ENOENT;
> +               of_node_put(remote_port);
> +
> +               /* All even ports describe inputs, all odd ports describe 
> outputs */
> +               comp_type = (crtc_endpoint + 1) % 2 ?
> +                           MTK_DISP_DIRECT_LINK_IN : 
> MTK_DISP_DIRECT_LINK_OUT;

`ep_in` and `ep_dev_node` were already put earlier in this function
(patch 37/46).

Also, `ep_in` is fetched from hardcoded `port@0` and `crtc_endpoint`
is the CRTC index (`0 = MAIN`, `1 = EXT`, ...), so `(crtc_endpoint +
1) % 2` misclassifies `CRTC_EXT` (`endpoint@1`) as
`MTK_DISP_DIRECT_LINK_OUT`; this is always `MTK_DISP_DIRECT_LINK_IN`.

> +
> +               dev_dbg(dev, "Found First DirectLink %s with port %pOF and ID 
> %u\n",
> +                       comp_type == MTK_DISP_DIRECT_LINK_OUT ? "OUT" : "IN",
> +                       ep_in, crtc_endpoint);
> +       }
> +
>         /* All ok! Pass the Component ID to the caller. */
>         comp_def->type = comp_type;
>         comp_def->inst_id = inst_id;
> @@ -848,7 +883,7 @@ static int mtk_drm_of_get_ddp_ep_cid(struct device *dev, 
> struct device_node *nod
>                                      struct mtk_drm_comp_definition *comp_def,
>                                      bool controller_arch_v2)
>  {
> -       struct device_node *ep_dev_node, *ep_out;
> +       struct device_node *ep_dev_node, *ep_out, *remote_ep;
>         enum mtk_ddp_comp_type comp_type;
>         int ret;
>
> @@ -856,10 +891,16 @@ static int mtk_drm_of_get_ddp_ep_cid(struct device 
> *dev, struct device_node *nod
>         if (!ep_out)
>                 return -EINVAL;
>
> -       ep_dev_node = of_graph_get_remote_port_parent(ep_out);
> +       remote_ep = of_graph_get_remote_endpoint(ep_out);
>         of_node_put(ep_out);
> -       if (!ep_dev_node)
> +       if (!remote_ep)
> +               return -ENOENT;
> +
> +       ep_dev_node = of_graph_get_port_parent(remote_ep);
> +       if (!ep_dev_node) {
> +               of_node_put(remote_ep);
>                 return -EINVAL;
> +       };
>
>         /*
>          * Pass the next node pointer regardless of failures in the later code
> @@ -877,17 +918,20 @@ static int mtk_drm_of_get_ddp_ep_cid(struct device 
> *dev, struct device_node *nod
>                         dev_dbg(dev, "Found connection to external mmsys 
> %pOF\n",
>                                 rmt_ctrlr_node);
>
> -                       of_node_put(ep_dev_node);
>                         of_node_put(rmt_ctrlr_node);
> +                       of_node_put(remote_ep);
>                         return -EREMOTE;
>                 }
>         }
>
> -       if (!of_device_is_available(ep_dev_node))
> +       if (!of_device_is_available(ep_dev_node)) {
> +               of_node_put(remote_ep);
>                 return -ENODEV;
> +       }
>
>         ret = mtk_drm_of_get_ddp_comp_type(ep_dev_node, &comp_type);
>         if (ret) {
> +               of_node_put(remote_ep);
>                 if (mtk_ovl_adaptor_is_comp_present(ep_dev_node)) {
>                         comp_def->type = MTK_DISP_OVL_ADAPTOR;
>                         comp_def->inst_id = 0;
> @@ -897,10 +941,26 @@ static int mtk_drm_of_get_ddp_ep_cid(struct device 
> *dev, struct device_node *nod
>                 return ret;
>         }
>
> -       ret = mtk_ddp_comp_get_id(ep_dev_node, comp_type);
> +       ret = mtk_ddp_comp_get_id(ep_dev_node, remote_ep, comp_type);
> +       of_node_put(remote_ep);
>         if (ret < 0)
>                 return ret;
>
> +       if (comp_type == MTK_DISP_DIRECT_LINK) {
> +               struct device_node *remote_port = 
> of_graph_get_remote_port(ep_out);
> +               u32 port_id;
> +
> +               of_property_read_u32(remote_port, "reg", &port_id);
> +               of_node_put(remote_port);
> +
> +               /* All even ports describe inputs, all odd ports describe 
> outputs */
> +               comp_type = (port_id + 1) % 2 ? MTK_DISP_DIRECT_LINK_IN : 
> MTK_DISP_DIRECT_LINK_OUT;
> +
> +               dev_dbg(dev, "Found DirectLink %s with port %pOF and ID %u\n",
> +                       comp_type == MTK_DISP_DIRECT_LINK_OUT ? "OUT" : "IN",
> +                       remote_port, port_id);
> +       }

`ep_out` was already put above, `remote_port` is logged after
`of_node_put()`, and `port_id` is uninitialized if `remote_port` is
NULL or lacks "reg".

> +
>         /* All ok! Pass the Component ID to the caller. */
>         comp_def->type = comp_type;
>         comp_def->inst_id = ret;
> @@ -1200,8 +1278,13 @@ static int mtk_drm_probe(struct platform_device *pdev)
>
>         hash_init(private->hlist.ddp_list);
>
> +       if (of_graph_is_present(phandle))
> +               node = phandle;
> +       else
> +               node = of_find_node_by_name(phandle, "direct-link");

of_find_node_by_name() calls of_node_put(phandle) on borrowed
`dev->parent->of_node`, walks the whole DT past phandle, and leaks the
returned node.
Use of_get_child_by_name(phandle, "direct-link") and put it when done.

> @@ -1273,12 +1363,45 @@ static int mtk_drm_probe(struct platform_device *pdev)
>                                                    node);
>                 }
>
> -               ret = mtk_ddp_comp_init(dev, node, &private->hlist,
> -                                       private->data->mmsys_id,
> -                                       comp_type, comp_inst_id);
> -               if (ret) {
> -                       of_node_put(node);
> -                       goto err_node;
> +               if (comp_type == MTK_DISP_DIRECT_LINK) {
> +                       for_each_of_graph_port(node, port) {
> +                               u32 port_id;
> +
> +                               of_property_read_u32(port, "reg", &port_id);
> +                               if (port_id > 1)
> +                                       continue;
> +
> +                               /* Even ports are inputs, odd ports are 
> outputs */
> +                               if (port_id % 2)
> +                                       comp_type = MTK_DISP_DIRECT_LINK_OUT;

`comp_type` is modified in place without being reset for even ports.
If port@1 appears before port@0 in DT (not preferred, but possible),
`comp_type` becomes `MTK_DISP_DIRECT_LINK_OUT` on the first iteration
and stays `OUT` for port@0 on the next iteration (also carried into
patch 40/46).

Compute the type per port instead, e.g. port_type = (port_id % 2) ?
MTK_DISP_DIRECT_LINK_OUT : MTK_DISP_DIRECT_LINK_IN;

> +
> +                               for_each_of_graph_port_endpoint(port, ep) {
> +                                       struct of_endpoint of_ep;
> +
> +                                       ret = of_graph_parse_endpoint(ep, 
> &of_ep);
> +                                       if (ret)
> +                                               break;
> +
> +                                       ret = mtk_ddp_comp_init(dev, node, 
> &private->hlist,
> +                                                               
> private->data->mmsys_id,
> +                                                               comp_type, 
> of_ep.id);
> +                                       if (ret)
> +                                               break;
> +                               }
> +
> +                               if (ret) {
> +                                       of_node_put(node);
> +                                       goto err_node;
> +                               }

Similarly, breaking out of `for_each_of_graph_port_endpoint()` leaks
`ep`, and jumping out of `for_each_of_graph_port()` via `goto
err_node` leaks `port` (missing `of_node_put()`, also carried into
patch 40/46).

Regards,
Fei

Reply via email to