[PATCH net-next v7 04/13] net: pse-pd: Add support for PSE power domains

Kory Maincent posted 13 patches 10 months, 1 week ago
There is a newer version of this series
[PATCH net-next v7 04/13] net: pse-pd: Add support for PSE power domains
Posted by Kory Maincent 10 months, 1 week ago
From: Kory Maincent (Dent Project) <kory.maincent@bootlin.com>

Introduce PSE power domain support as groundwork for upcoming port
priority features. Multiple PSE PIs can now be grouped under a single
PSE power domain, enabling future enhancements like defining available
power budgets, port priority modes, and disconnection policies. This
setup will allow the system to assess whether activating a port would
exceed the available power budget, preventing over-budget states
proactively.

Signed-off-by: Kory Maincent (Dent Project) <kory.maincent@bootlin.com>
---
Changes in v7:
- Add reference count and mutex lock for PSE power domain in case of PSE
  from different controllers want to register the same PSE power domain.

Changes in v6:
- nitpick change.

Changes in v4:
- Add kdoc.
- Fix null dereference in pse_flush_pw_ds function.

Changes in v3:
- Remove pw_budget variable.

Changes in v2:
- new patch.
---
 drivers/net/pse-pd/pse_core.c | 138 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/pse-pd/pse.h    |   2 +
 2 files changed, 140 insertions(+)

diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index 8755c2e00b6a..d26045e6ca85 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -13,8 +13,12 @@
 #include <linux/regulator/driver.h>
 #include <linux/regulator/machine.h>
 
+#define PSE_PW_D_LIMIT INT_MAX
+
 static DEFINE_MUTEX(pse_list_mutex);
 static LIST_HEAD(pse_controller_list);
+static DEFINE_XARRAY_ALLOC(pse_pw_d_map);
+static DEFINE_MUTEX(pse_pw_d_mutex);
 
 /**
  * struct pse_control - a PSE control
@@ -35,6 +39,18 @@ struct pse_control {
 	struct phy_device *attached_phydev;
 };
 
+/**
+ * struct pse_power_domain - a PSE power domain
+ * @id: ID of the power domain
+ * @supply: Power supply the Power Domain
+ * @refcnt: Number of gets of this pse_power_domain
+ */
+struct pse_power_domain {
+	int id;
+	struct regulator *supply;
+	struct kref refcnt;
+};
+
 static int of_load_single_pse_pi_pairset(struct device_node *node,
 					 struct pse_pi *pi,
 					 int pairset_num)
@@ -440,6 +456,123 @@ devm_pse_pi_regulator_register(struct pse_controller_dev *pcdev,
 	return 0;
 }
 
+static void __pse_pw_d_release(struct kref *kref)
+{
+	struct pse_power_domain *pw_d = container_of(kref,
+						     struct pse_power_domain,
+						     refcnt);
+
+	regulator_put(pw_d->supply);
+	xa_erase(&pse_pw_d_map, pw_d->id);
+}
+
+/**
+ * pse_flush_pw_ds - flush all PSE power domains of a PSE
+ * @pcdev: a pointer to the initialized PSE controller device
+ */
+static void pse_flush_pw_ds(struct pse_controller_dev *pcdev)
+{
+	struct pse_power_domain *pw_d;
+	int i;
+
+	for (i = 0; i < pcdev->nr_lines; i++) {
+		if (!pcdev->pi[i].pw_d)
+			continue;
+
+		pw_d = xa_load(&pse_pw_d_map, pcdev->pi[i].pw_d->id);
+		if (!pw_d)
+			continue;
+
+		kref_put_mutex(&pw_d->refcnt, __pse_pw_d_release,
+			       &pse_pw_d_mutex);
+	}
+}
+
+/**
+ * devm_pse_alloc_pw_d - allocate a new PSE power domain for a device
+ * @dev: device that is registering this PSE power domain
+ *
+ * Return: Pointer to the newly allocated PSE power domain or error pointers
+ */
+static struct pse_power_domain *devm_pse_alloc_pw_d(struct device *dev)
+{
+	struct pse_power_domain *pw_d;
+	int index, ret;
+
+	pw_d = devm_kzalloc(dev, sizeof(*pw_d), GFP_KERNEL);
+	if (!pw_d)
+		return ERR_PTR(-ENOMEM);
+
+	ret = xa_alloc(&pse_pw_d_map, &index, pw_d, XA_LIMIT(1, PSE_PW_D_LIMIT),
+		       GFP_KERNEL);
+	if (ret)
+		return ERR_PTR(ret);
+
+	pw_d->id = index;
+	return pw_d;
+}
+
+/**
+ * pse_register_pw_ds - register the PSE power domains for a PSE
+ * @pcdev: a pointer to the PSE controller device
+ *
+ * Return: 0 on success and failure value on error
+ */
+static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
+{
+	int i, ret = 0;
+
+	mutex_lock(&pse_pw_d_mutex);
+	for (i = 0; i < pcdev->nr_lines; i++) {
+		struct regulator_dev *rdev = pcdev->pi[i].rdev;
+		struct pse_power_domain *pw_d;
+		struct regulator *supply;
+		bool present = false;
+		unsigned long index;
+
+		/* No regulator or regulator parent supply registered.
+		 * We need a regulator parent to register a PSE power domain
+		 */
+		if (!rdev || !rdev->supply)
+			continue;
+
+		xa_for_each(&pse_pw_d_map, index, pw_d) {
+			/* Power supply already registered as a PSE power
+			 * domain.
+			 */
+			if (regulator_is_equal(pw_d->supply, rdev->supply)) {
+				present = true;
+				pcdev->pi[i].pw_d = pw_d;
+				break;
+			}
+		}
+		if (present) {
+			kref_get(&pw_d->refcnt);
+			continue;
+		}
+
+		pw_d = devm_pse_alloc_pw_d(pcdev->dev);
+		if (IS_ERR_OR_NULL(pw_d)) {
+			ret = PTR_ERR(pw_d);
+			goto out;
+		}
+
+		supply = regulator_get(&rdev->dev, rdev->supply_name);
+		if (IS_ERR(supply)) {
+			xa_erase(&pse_pw_d_map, pw_d->id);
+			ret = PTR_ERR(supply);
+			goto out;
+		}
+
+		pw_d->supply = supply;
+		pcdev->pi[i].pw_d = pw_d;
+	}
+
+out:
+	mutex_unlock(&pse_pw_d_mutex);
+	return ret;
+}
+
 /**
  * pse_controller_register - register a PSE controller device
  * @pcdev: a pointer to the initialized PSE controller device
@@ -499,6 +632,10 @@ int pse_controller_register(struct pse_controller_dev *pcdev)
 			return ret;
 	}
 
+	ret = pse_register_pw_ds(pcdev);
+	if (ret)
+		return ret;
+
 	mutex_lock(&pse_list_mutex);
 	list_add(&pcdev->list, &pse_controller_list);
 	mutex_unlock(&pse_list_mutex);
@@ -513,6 +650,7 @@ EXPORT_SYMBOL_GPL(pse_controller_register);
  */
 void pse_controller_unregister(struct pse_controller_dev *pcdev)
 {
+	pse_flush_pw_ds(pcdev);
 	pse_release_pis(pcdev);
 	mutex_lock(&pse_list_mutex);
 	list_del(&pcdev->list);
diff --git a/include/linux/pse-pd/pse.h b/include/linux/pse-pd/pse.h
index 5d41a1c984bd..5201a0fb3d74 100644
--- a/include/linux/pse-pd/pse.h
+++ b/include/linux/pse-pd/pse.h
@@ -220,12 +220,14 @@ struct pse_pi_pairset {
  * @np: device node pointer of the PSE PI node
  * @rdev: regulator represented by the PSE PI
  * @admin_state_enabled: PI enabled state
+ * @pw_d: Power domain of the PSE PI
  */
 struct pse_pi {
 	struct pse_pi_pairset pairset[2];
 	struct device_node *np;
 	struct regulator_dev *rdev;
 	bool admin_state_enabled;
+	struct pse_power_domain *pw_d;
 };
 
 /**

-- 
2.34.1
Re: [PATCH net-next v7 04/13] net: pse-pd: Add support for PSE power domains
Posted by Oleksij Rempel 10 months, 1 week ago
Hi Kory,

here are some points

On Tue, Apr 08, 2025 at 04:32:13PM +0200, Kory Maincent wrote:
> From: Kory Maincent (Dent Project) <kory.maincent@bootlin.com>
> 
> Introduce PSE power domain support as groundwork for upcoming port
> priority features. Multiple PSE PIs can now be grouped under a single
> PSE power domain, enabling future enhancements like defining available
> power budgets, port priority modes, and disconnection policies. This
> setup will allow the system to assess whether activating a port would
> exceed the available power budget, preventing over-budget states
> proactively.
> 
> Signed-off-by: Kory Maincent (Dent Project) <kory.maincent@bootlin.com>
> ---
> Changes in v7:
> - Add reference count and mutex lock for PSE power domain in case of PSE
>   from different controllers want to register the same PSE power domain.
> 
> Changes in v6:
> - nitpick change.
> 
> Changes in v4:
> - Add kdoc.
> - Fix null dereference in pse_flush_pw_ds function.
> 
> Changes in v3:
> - Remove pw_budget variable.
> 
> Changes in v2:
> - new patch.
> ---
>  drivers/net/pse-pd/pse_core.c | 138 ++++++++++++++++++++++++++++++++++++++++++
>  include/linux/pse-pd/pse.h    |   2 +
>  2 files changed, 140 insertions(+)
> 
> diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
> index 8755c2e00b6a..d26045e6ca85 100644
> --- a/drivers/net/pse-pd/pse_core.c
> +++ b/drivers/net/pse-pd/pse_core.c
> @@ -13,8 +13,12 @@
>  #include <linux/regulator/driver.h>
>  #include <linux/regulator/machine.h>
>  
> +#define PSE_PW_D_LIMIT INT_MAX
> +
>  static DEFINE_MUTEX(pse_list_mutex);
>  static LIST_HEAD(pse_controller_list);
> +static DEFINE_XARRAY_ALLOC(pse_pw_d_map);
> +static DEFINE_MUTEX(pse_pw_d_mutex);
>  
>  /**
>   * struct pse_control - a PSE control
> @@ -35,6 +39,18 @@ struct pse_control {
>  	struct phy_device *attached_phydev;
>  };
>  
> +/**
> + * struct pse_power_domain - a PSE power domain
> + * @id: ID of the power domain
> + * @supply: Power supply the Power Domain
> + * @refcnt: Number of gets of this pse_power_domain
> + */
> +struct pse_power_domain {
> +	int id;
> +	struct regulator *supply;
> +	struct kref refcnt;
> +};
> +
>  static int of_load_single_pse_pi_pairset(struct device_node *node,
>  					 struct pse_pi *pi,
>  					 int pairset_num)
> @@ -440,6 +456,123 @@ devm_pse_pi_regulator_register(struct pse_controller_dev *pcdev,
>  	return 0;
>  }
>  
> +static void __pse_pw_d_release(struct kref *kref)
> +{
> +	struct pse_power_domain *pw_d = container_of(kref,
> +						     struct pse_power_domain,
> +						     refcnt);
> +
> +	regulator_put(pw_d->supply);
> +	xa_erase(&pse_pw_d_map, pw_d->id);
> +}
> +
> +/**
> + * pse_flush_pw_ds - flush all PSE power domains of a PSE
> + * @pcdev: a pointer to the initialized PSE controller device
> + */
> +static void pse_flush_pw_ds(struct pse_controller_dev *pcdev)
> +{
> +	struct pse_power_domain *pw_d;
> +	int i;
> +
> +	for (i = 0; i < pcdev->nr_lines; i++) {
> +		if (!pcdev->pi[i].pw_d)
> +			continue;
> +
> +		pw_d = xa_load(&pse_pw_d_map, pcdev->pi[i].pw_d->id);
> +		if (!pw_d)
> +			continue;
> +
> +		kref_put_mutex(&pw_d->refcnt, __pse_pw_d_release,
> +			       &pse_pw_d_mutex);
> +	}
> +}
> +
> +/**
> + * devm_pse_alloc_pw_d - allocate a new PSE power domain for a device
> + * @dev: device that is registering this PSE power domain
> + *
> + * Return: Pointer to the newly allocated PSE power domain or error pointers
> + */
> +static struct pse_power_domain *devm_pse_alloc_pw_d(struct device *dev)
> +{
> +	struct pse_power_domain *pw_d;
> +	int index, ret;
> +
> +	pw_d = devm_kzalloc(dev, sizeof(*pw_d), GFP_KERNEL);
> +	if (!pw_d)
> +		return ERR_PTR(-ENOMEM);
> +
> +	ret = xa_alloc(&pse_pw_d_map, &index, pw_d, XA_LIMIT(1, PSE_PW_D_LIMIT),
> +		       GFP_KERNEL);
> +	if (ret)
> +		return ERR_PTR(ret);

Missing "kref_init(&pw_d->refcnt);" ?

> +	pw_d->id = index;
> +	return pw_d;
> +}
> +
> +/**
> + * pse_register_pw_ds - register the PSE power domains for a PSE
> + * @pcdev: a pointer to the PSE controller device
> + *
> + * Return: 0 on success and failure value on error
> + */
> +static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
> +{
> +	int i, ret = 0;
> +
> +	mutex_lock(&pse_pw_d_mutex);
> +	for (i = 0; i < pcdev->nr_lines; i++) {
> +		struct regulator_dev *rdev = pcdev->pi[i].rdev;
> +		struct pse_power_domain *pw_d;
> +		struct regulator *supply;
> +		bool present = false;
> +		unsigned long index;
> +
> +		/* No regulator or regulator parent supply registered.
> +		 * We need a regulator parent to register a PSE power domain
> +		 */
> +		if (!rdev || !rdev->supply)
> +			continue;
> +
> +		xa_for_each(&pse_pw_d_map, index, pw_d) {
> +			/* Power supply already registered as a PSE power
> +			 * domain.
> +			 */
> +			if (regulator_is_equal(pw_d->supply, rdev->supply)) {
> +				present = true;
> +				pcdev->pi[i].pw_d = pw_d;
> +				break;
> +			}
> +		}
> +		if (present) {
> +			kref_get(&pw_d->refcnt);
> +			continue;
> +		}
> +
> +		pw_d = devm_pse_alloc_pw_d(pcdev->dev);
> +		if (IS_ERR_OR_NULL(pw_d)) {

s/IS_ERR_OR_NULL/IS_ERR

devm_pse_alloc_pw_d() is not returning NULL.

> +			ret = PTR_ERR(pw_d);
> +			goto out;
> +		}
> +
> +		supply = regulator_get(&rdev->dev, rdev->supply_name);
> +		if (IS_ERR(supply)) {
> +			xa_erase(&pse_pw_d_map, pw_d->id);
> +			ret = PTR_ERR(supply);

Here:
Either we need to ensure pse_flush_pw_ds() handles incomplete setups
or immediately clean up earlier entries in the loop when an error
occurs.

> +			goto out;
> +		}
> +
> +		pw_d->supply = supply;
> +		pcdev->pi[i].pw_d = pw_d;
> +	}
> +
> +out:
> +	mutex_unlock(&pse_pw_d_mutex);
> +	return ret;
> +}
 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Re: [PATCH net-next v7 04/13] net: pse-pd: Add support for PSE power domains
Posted by Kory Maincent 10 months ago
Hello Oleksij,

On Tue, 8 Apr 2025 20:09:36 +0200
Oleksij Rempel <o.rempel@pengutronix.de> wrote:

> Hi Kory,
> 
> here are some points
> 
> On Tue, Apr 08, 2025 at 04:32:13PM +0200, Kory Maincent wrote:
> > From: Kory Maincent (Dent Project) <kory.maincent@bootlin.com>
> > +static struct pse_power_domain *devm_pse_alloc_pw_d(struct device *dev)
> > +{
> > +	struct pse_power_domain *pw_d;
> > +	int index, ret;
> > +
> > +	pw_d = devm_kzalloc(dev, sizeof(*pw_d), GFP_KERNEL);
> > +	if (!pw_d)
> > +		return ERR_PTR(-ENOMEM);
> > +
> > +	ret = xa_alloc(&pse_pw_d_map, &index, pw_d, XA_LIMIT(1,
> > PSE_PW_D_LIMIT),
> > +		       GFP_KERNEL);
> > +	if (ret)
> > +		return ERR_PTR(ret);  
> 
> Missing "kref_init(&pw_d->refcnt);" ?

Oh yes, indeed thanks.

> > +
> > +		pw_d = devm_pse_alloc_pw_d(pcdev->dev);
> > +		if (IS_ERR_OR_NULL(pw_d)) {  
> 
> s/IS_ERR_OR_NULL/IS_ERR
> 
> devm_pse_alloc_pw_d() is not returning NULL.
 
Yes, that's right!

> > +			ret = PTR_ERR(pw_d);
> > +			goto out;
> > +		}
> > +
> > +		supply = regulator_get(&rdev->dev, rdev->supply_name);
> > +		if (IS_ERR(supply)) {
> > +			xa_erase(&pse_pw_d_map, pw_d->id);
> > +			ret = PTR_ERR(supply);  
> 
> Here:
> Either we need to ensure pse_flush_pw_ds() handles incomplete setups
> or immediately clean up earlier entries in the loop when an error
> occurs.

The pw_d has not yet been saved to pcdev->pi[i].pw_d so we need to remove the
pw_d from the xarray here. pse_flush_pw_ds will deal with all the pw_d entries
in pcdev->pi[x].

Regards,
-- 
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com