Skip to contentSkip to conversation

drm/msm: Fix separate_gpu_kms for multi-gpu

PATCH Superseded Rob Clark 3 participants 3 messages Dec 2

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Rob Clark

author maintainer
#0 lore raw

PATCH 1/1 drm/msm: Fix separate_gpu_kms for multi-gpu

The default of auto-pick was not handling the case of multiple GPU devices. Handle this by matching GPU and DPU devices, so if a SoC has an equal # of display and GPU devices, they get paired up one- by-one.

---
I don't have such a device at hand. But this at least works when there is a single GPU+DPU. I'd appreciate if someone could test this on such a device.

 drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c |  7 ++++--
 drivers/gpu/drm/msm/msm_drv.c            | 28 ++++++++++++++++++------
 drivers/gpu/drm/msm/msm_drv.h            |  8 +++----
 drivers/gpu/drm/msm/msm_mdss.c           |  7 ++++--
 4 files changed, 35 insertions(+), 15 deletions(-)
drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c +5 -2
index ad08a040982f..e3d27e0020a0 100644
@@ -16,14 +16,17 @@
 #include "mdp4_kms.h"
 
 static const struct of_device_id mdp4_dt_match[];
-int msm_count_mdp4(void)
+int msm_count_mdp4(struct device_node *target_np)
 {
 	struct device_node *np;
 	int count = 0;
 
 	for_each_matching_node(np, mdp4_dt_match) {
-		if (of_device_is_available(np))
+		if (of_device_is_available(np)) {
 			count++;
+			if (target_np == np)
+				return count;
+		}
 	}
 
 	return count;
drivers/gpu/drm/msm/msm_drv.c +21 -7
index cd59daa02bf9..49bac1ec64db 100644
@@ -98,14 +98,17 @@ static inline bool msm_gpu_node_present(struct device_node *np)
 	return np && of_device_is_available(np) && adreno_has_gpu(np);
 }
 
-static int msm_count_gpus(void)
+static int msm_count_gpus(struct device_node *target_np)
 {
 	struct device_node *np;
 	int count = 0;
 
 	for_each_matching_node(np, msm_gpu_match) {
-		if (msm_gpu_node_present(np))
+		if (msm_gpu_node_present(np)) {
 			count++;
+			if (target_np == np)
+				return count;
+		}
 	}
 
 	return count;
@@ -113,11 +116,10 @@ static int msm_count_gpus(void)
 
 static bool msm_separate_gpu_kms_auto(void)
 {
-	int gpus = msm_count_gpus();
-	int display_subsystems = msm_count_mdss() + msm_count_mdp4();
+	int gpus = msm_count_gpus(NULL);
+	int display_subsystems = msm_count_mdss(NULL) + msm_count_mdp4(NULL);
 
-	/* Single card if there is exactly one GPU and one display subsystem */
-	if (gpus == 1 && display_subsystems == 1)
+	if (gpus != display_subsystems)
 		return false;
 
 	return true;
@@ -125,7 +127,7 @@ static bool msm_separate_gpu_kms_auto(void)
 
 bool msm_separate_gpu_kms_components(void)
 {
-	if (msm_count_gpus() == 0)
+	if (msm_count_gpus(NULL) == 0)
 		return true;
 
 	switch (separate_gpu_kms) {
@@ -1105,6 +1107,18 @@ static int add_gpu_components(struct device *dev,
 	if (!np || !msm_gpu_node_present(np))
 		return 0;
 
+	unsigned disp_idx = msm_count_mdss(dev->of_node) + msm_count_mdp4(dev->of_node);
+	unsigned gpu_idx = msm_count_gpus(gpu_np);
+
+	pr_err("disp_idx=%u, gpu_idx=%u\n", disp_idx, gpu_idx);
+
+	/*
+	 * If we have multiple GPUs, we should match the N'th GPU with the
+	 * N'th DPU.
+	 */
+	if (disp_idx && (disp_idx != gpu_idx))
+		return 0;
+
 	drm_of_component_match_add(dev, matchptr, component_compare_of, np);
 
 	struct device_node *gmu_np __free(device_node) = of_parse_phandle(np, "qcom,gmu", 0);
drivers/gpu/drm/msm/msm_drv.h +4 -4
index b65702ac0140..63d9ded69bb7 100644
@@ -394,11 +394,11 @@ static inline bool msm_dp_wide_bus_available(const struct msm_dp *dp_display)
 #ifdef CONFIG_DRM_MSM_MDP4
 void msm_mdp4_register(void);
 void msm_mdp4_unregister(void);
-int msm_count_mdp4(void);
+int msm_count_mdp4(struct device_node *target_np);
 #else
 static inline void msm_mdp4_register(void) {}
 static inline void msm_mdp4_unregister(void) {}
-static inline int msm_count_mdp4(void) { return 0; }
+static inline int msm_count_mdp4(struct device_node *target_np) { return 0; }
 #endif
 
 #ifdef CONFIG_DRM_MSM_MDP5
@@ -420,11 +420,11 @@ static inline void msm_dpu_unregister(void) {}
 #ifdef CONFIG_DRM_MSM_MDSS
 void msm_mdss_register(void);
 void msm_mdss_unregister(void);
-int msm_count_mdss(void);
+int msm_count_mdss(struct device_node *target_np);
 #else
 static inline void msm_mdss_register(void) {}
 static inline void msm_mdss_unregister(void) {}
-static inline int msm_count_mdss(void) { return 0; }
+static inline int msm_count_mdss(struct device_node *target_np) { return 0; }
 #endif
 
 #ifdef CONFIG_DEBUG_FS
drivers/gpu/drm/msm/msm_mdss.c +5 -2
index fb0261ae9c78..a320345a5736 100644
@@ -77,14 +77,17 @@ static int msm_mdss_parse_data_bus_icc_path(struct device *dev,
 
 static const struct of_device_id mdss_dt_match[];
 
-int msm_count_mdss(void)
+int msm_count_mdss(struct device_node *target_np)
 {
 	struct device_node *np;
 	int count = 0;
 
 	for_each_matching_node(np, mdss_dt_match) {
-		if (of_device_is_available(np))
+		if (of_device_is_available(np)) {
 			count++;
+			if (target_np == np)
+				return count;
+		}
 	}
 
 	return count;
Signature · 1 line
2.56.1
#1 lore raw
Quoted 68 lines · Rob Clark

The default of auto-pick was not handling the case of multiple GPU devices. Handle this by matching GPU and DPU devices, so if a SoC has an equal # of display and GPU devices, they get paired up one- by-one.

---
I don't have such a device at hand. But this at least works when there is a single GPU+DPU. I'd appreciate if someone could test this on such a device.

 drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c |  7 ++++--
 drivers/gpu/drm/msm/msm_drv.c            | 28 ++++++++++++++++++------
 drivers/gpu/drm/msm/msm_drv.h            |  8 +++----
 drivers/gpu/drm/msm/msm_mdss.c           |  7 ++++--
 4 files changed, 35 insertions(+), 15 deletions(-)
drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c +5 -2
index ad08a040982f..e3d27e0020a0 100644
@@ -16,14 +16,17 @@
 #include "mdp4_kms.h"
 
 static const struct of_device_id mdp4_dt_match[];
-int msm_count_mdp4(void)
+int msm_count_mdp4(struct device_node *target_np)
 {
        struct device_node *np;
        int count = 0;
 
        for_each_matching_node(np, mdp4_dt_match) {
-               if (of_device_is_available(np))
+               if (of_device_is_available(np)) {
                        count++;
+                       if (target_np == np)
+                               return count;
+               }
        }
 
        return count;
drivers/gpu/drm/msm/msm_drv.c +5 -2
index cd59daa02bf9..49bac1ec64db 100644
@@ -98,14 +98,17 @@ static inline bool msm_gpu_node_present(struct device_node *np)
        return np && of_device_is_available(np) && adreno_has_gpu(np);
 }
 
-static int msm_count_gpus(void)
+static int msm_count_gpus(struct device_node *target_np)
 {
        struct device_node *np;
        int count = 0;
 
        for_each_matching_node(np, msm_gpu_match) {
-               if (msm_gpu_node_present(np))
+               if (msm_gpu_node_present(np)) {
                        count++;
+                       if (target_np == np)
+                               return count;
+               }
        }
 
        return count;
@@ -113,11 +116,10 @@ static int msm_count_gpus(void)
 

drivers/gpu/drm/msm/msm_drv.c static int msm_count_gpus(void) · line 122

Quoted 10 lines
drivers/gpu/drm/msm/msm_drv.c +3 -4
 static bool msm_separate_gpu_kms_auto(void)
 {
-       int gpus = msm_count_gpus();
-       int display_subsystems = msm_count_mdss() + msm_count_mdp4();
+       int gpus = msm_count_gpus(NULL);
+       int display_subsystems = msm_count_mdss(NULL) + msm_count_mdp4(NULL);
 
-       /* Single card if there is exactly one GPU and one display subsystem */
-       if (gpus == 1 && display_subsystems == 1)
+       if (gpus != display_subsystems)

possibly if # of gpus != # of dpus, we should match up the first min(gpus, display_subsystems) and create singletons for whatever is left over

BR,
-R

Quoted 90 lines
drivers/gpu/drm/msm/msm_drv.c +13 -1
                return false;
 
        return true;
@@ -125,7 +127,7 @@ static bool msm_separate_gpu_kms_auto(void)
 
 bool msm_separate_gpu_kms_components(void)
 {
-       if (msm_count_gpus() == 0)
+       if (msm_count_gpus(NULL) == 0)
                return true;
 
        switch (separate_gpu_kms) {
@@ -1105,6 +1107,18 @@ static int add_gpu_components(struct device *dev,
        if (!np || !msm_gpu_node_present(np))
                return 0;
 
+       unsigned disp_idx = msm_count_mdss(dev->of_node) + msm_count_mdp4(dev->of_node);
+       unsigned gpu_idx = msm_count_gpus(gpu_np);
+
+       pr_err("disp_idx=%u, gpu_idx=%u\n", disp_idx, gpu_idx);
+
+       /*
+        * If we have multiple GPUs, we should match the N'th GPU with the
+        * N'th DPU.
+        */
+       if (disp_idx && (disp_idx != gpu_idx))
+               return 0;
+
        drm_of_component_match_add(dev, matchptr, component_compare_of, np);
 
        struct device_node *gmu_np __free(device_node) = of_parse_phandle(np, "qcom,gmu", 0);
drivers/gpu/drm/msm/msm_drv.h +4 -4
index b65702ac0140..63d9ded69bb7 100644
@@ -394,11 +394,11 @@ static inline bool msm_dp_wide_bus_available(const struct msm_dp *dp_display)
 #ifdef CONFIG_DRM_MSM_MDP4
 void msm_mdp4_register(void);
 void msm_mdp4_unregister(void);
-int msm_count_mdp4(void);
+int msm_count_mdp4(struct device_node *target_np);
 #else
 static inline void msm_mdp4_register(void) {}
 static inline void msm_mdp4_unregister(void) {}
-static inline int msm_count_mdp4(void) { return 0; }
+static inline int msm_count_mdp4(struct device_node *target_np) { return 0; }
 #endif
 
 #ifdef CONFIG_DRM_MSM_MDP5
@@ -420,11 +420,11 @@ static inline void msm_dpu_unregister(void) {}
 #ifdef CONFIG_DRM_MSM_MDSS
 void msm_mdss_register(void);
 void msm_mdss_unregister(void);
-int msm_count_mdss(void);
+int msm_count_mdss(struct device_node *target_np);
 #else
 static inline void msm_mdss_register(void) {}
 static inline void msm_mdss_unregister(void) {}
-static inline int msm_count_mdss(void) { return 0; }
+static inline int msm_count_mdss(struct device_node *target_np) { return 0; }
 #endif
 
 #ifdef CONFIG_DEBUG_FS
drivers/gpu/drm/msm/msm_mdss.c +5 -3
index fb0261ae9c78..a320345a5736 100644
@@ -77,14 +77,17 @@ static int msm_mdss_parse_data_bus_icc_path(struct device *dev,
 
 static const struct of_device_id mdss_dt_match[];
 
-int msm_count_mdss(void)
+int msm_count_mdss(struct device_node *target_np)
 {
        struct device_node *np;
        int count = 0;
 
        for_each_matching_node(np, mdss_dt_match) {
-               if (of_device_is_available(np))
+               if (of_device_is_available(np)) {
                        count++;
+                       if (target_np == np)
+                               return count;
+               }
        }
 
        return count;
--

2.56.1

#2 lore raw
In reply to Rob Clark
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] drm/msm: inverted boolean return value in msm_separate_gpu_kms_auto() - [High] drm/msm: unmatched…

Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] drm/msm: inverted boolean return value in msm_separate_gpu_kms_auto()
- [High] drm/msm: unmatched target_np returns total count in msm_count_*()
- [High] drm/msm: add_gpu_components() passes NULL gpu_np and checks only first GPU
- [Medium] drm/msm: device_node refcount leak on early return in msm_count_*()
- [Low] drm/msm: leftover pr_err() debug print in add_gpu_components() --

commit 26bd8b0728ea32f5eae5c7537ac1e3cf8ad995f9
Author: Rob Clark <[email protected]>

drm/msm: Fix separate_gpu_kms for multi-gpu

This commit updates separate_gpu_kms auto-selection and GPU component matching to pair GPU and display devices by index when a system has an equal number of GPUs and display subsystems.

Quoted 14 lines
drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c +1 -1
index ad08a040982fa..e3d27e0020a01 100644
@@ -16,14 +16,17 @@
 #include "mdp4_kms.h"
 
 static const struct of_device_id mdp4_dt_match[];
-int msm_count_mdp4(void)
+int msm_count_mdp4(struct device_node *target_np)
 {
 	struct device_node *np;
 	int count = 0;
 

drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c line 32

Quoted 10 lines
drivers/gpu/drm/msm/disp/mdp4/mdp4_kms.c +4 -1
 	for_each_matching_node(np, mdp4_dt_match) {
-		if (of_device_is_available(np))
+		if (of_device_is_available(np)) {
 			count++;
+			if (target_np == np)
+				return count;
+		}
 	}
 
 	return count;

[Severity: Medium]
Does returning directly from inside for_each_matching_node() leak the reference on np? for_each_matching_node() holds a reference on the loop cursor until the next iteration, so early returns in msm_count_mdp4(), msm_count_gpus(), and msm_count_mdss() need of_node_put(np).

Quoted 29 lines
drivers/gpu/drm/msm/msm_drv.c +5 -3
index cd59daa02bf94..49bac1ec64db9 100644
@@ -98,14 +98,17 @@ static inline bool msm_gpu_node_present(struct device_node *np)
 	return np && of_device_is_available(np) && adreno_has_gpu(np);
 }
 
-static int msm_count_gpus(void)
+static int msm_count_gpus(struct device_node *target_np)
 {
 	struct device_node *np;
 	int count = 0;
 
 	for_each_matching_node(np, msm_gpu_match) {
-		if (msm_gpu_node_present(np))
+		if (msm_gpu_node_present(np)) {
 			count++;
+			if (target_np == np)
+				return count;
+		}
 	}
 
 	return count;
@@ -113,11 +116,10 @@ static int msm_count_gpus(void)
 
 static bool msm_separate_gpu_kms_auto(void)
 {
-	int gpus = msm_count_gpus();

drivers/gpu/drm/msm/msm_drv.c static int msm_count_gpus(void) · line 125

Quoted 10 lines
drivers/gpu/drm/msm/msm_drv.c +3 -3
-	int display_subsystems = msm_count_mdss() + msm_count_mdp4();
+	int gpus = msm_count_gpus(NULL);
+	int display_subsystems = msm_count_mdss(NULL) + msm_count_mdp4(NULL);
 
-	/* Single card if there is exactly one GPU and one display subsystem */
-	if (gpus == 1 && display_subsystems == 1)
+	if (gpus != display_subsystems)
 		return false;
 
 	return true;

[Severity: High]
Is the boolean return value of msm_separate_gpu_kms_auto() inverted here?

In msm_separate_gpu_kms_components(), returning true means separate GPU and KMS DRM devices (matching separate_gpu_kms == 1), while false means combining them into a single DRM device (matching separate_gpu_kms == 0). Returning true when gpus == display_subsystems forces separate devices on 1-GPU + 1-display and N-GPU + N-display systems, and returning false when gpus != display_subsystems attempts to combine them on asymmetric or headless systems (leaving a standalone GPU unbound when display_subsystems is 0 because adreno_probe() skips msm_gpu_probe()).

drivers/gpu/drm/msm/msm_drv.c static int add_gpu_components(struct device *dev, · line 1110

Quoted 5 lines
drivers/gpu/drm/msm/msm_drv.c +1 -0
@@ -1105,6 +1107,18 @@ static int add_gpu_components(struct device *dev,
 	if (!np || !msm_gpu_node_present(np))
 		return 0;
 
+	unsigned disp_idx = msm_count_mdss(dev->of_node) + msm_count_mdp4(dev->of_node);

[Severity: High]
What happens when target_np is non-NULL but not found by msm_count_mdss(), msm_count_mdp4(), or msm_count_gpus()?

When target_np does not match any entry, those helpers finish the loop and return the total count of available nodes instead of 0. In add_gpu_components(), this causes two problems:

1. When called from msm_gpu_probe() for a standalone GPU master (kms_init is NULL), dev->of_node is a GPU node, so disp_idx becomes the total number of display subsystems in the system rather than 0. If disp_idx is non-zero and disp_idx != gpu_idx, add_gpu_components() returns 0 without adding anything to matchptr, and msm_drv_probe() then passes match == NULL to component_master_add_with_match(), causing a NULL pointer dereference on match->num.

2. When called from dpu_dev_probe() or mdp5_dev_probe(), dev is the child DPU or MDP5 device rather than the parent MDSS device, so dev->of_node never matches mdss_dt_match in msm_count_mdss() and disp_idx always evaluates to the total MDSS count instead of the display index.

drivers/gpu/drm/msm/msm_drv.c static int add_gpu_components(struct device *dev, · line 1111

Quoted 1 line
drivers/gpu/drm/msm/msm_drv.c +1 -0
+	unsigned gpu_idx = msm_count_gpus(gpu_np);

[Severity: High]
Can this pair KMS devices with GPUs when gpu_np is NULL on KMS probe paths?

dpu_dev_probe(), mdp5_dev_probe(), and mdp4_probe() all pass gpu_np = NULL to msm_drv_probe(). In add_gpu_components(), np is then always set to the first matching GPU node via of_find_matching_node(NULL, msm_gpu_match), and msm_count_gpus(gpu_np) is called with NULL, returning the total number of GPUs rather than a specific GPU index. On a 2-GPU system, gpu_idx is always 2 during KMS probe, so display 1 skips binding a GPU and display 2 binds the first GPU node.

drivers/gpu/drm/msm/msm_drv.c static int add_gpu_components(struct device *dev, · line 1113

Quoted 2 lines
drivers/gpu/drm/msm/msm_drv.c +2 -0
+
+	pr_err("disp_idx=%u, gpu_idx=%u\n", disp_idx, gpu_idx);

[Severity: Low]
This looks like a leftover debug printk that logs at KERN_ERR on every probe.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1