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

Kory Maincent posted 12 patches 11 months, 1 week ago
There is a newer version of this series
[PATCH net-next v6 04/12] net: pse-pd: Add support for PSE power domains
Posted by Kory Maincent 11 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 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 | 113 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/pse-pd/pse.h    |   2 +
 2 files changed, 115 insertions(+)

diff --git a/drivers/net/pse-pd/pse_core.c b/drivers/net/pse-pd/pse_core.c
index baccec984486c..0f8a198f9f3b8 100644
--- a/drivers/net/pse-pd/pse_core.c
+++ b/drivers/net/pse-pd/pse_core.c
@@ -15,6 +15,7 @@
 
 static DEFINE_MUTEX(pse_list_mutex);
 static LIST_HEAD(pse_controller_list);
+static DEFINE_XARRAY_ALLOC(pse_pw_d_map);
 
 /**
  * struct pse_control - a PSE control
@@ -35,6 +36,16 @@ 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
+ */
+struct pse_power_domain {
+	int id;
+	struct regulator *supply;
+};
+
 static int of_load_single_pse_pi_pairset(struct device_node *node,
 					 struct pse_pi *pi,
 					 int pairset_num)
@@ -440,6 +451,103 @@ devm_pse_pi_regulator_register(struct pse_controller_dev *pcdev,
 	return 0;
 }
 
+/**
+ * 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) {
+			regulator_put(pw_d->supply);
+			xa_erase(&pse_pw_d_map, pw_d->id);
+		}
+	}
+}
+
+/**
+ * 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, INT_MAX), 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;
+
+	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)
+			continue;
+
+		pw_d = devm_pse_alloc_pw_d(pcdev->dev);
+		if (IS_ERR_OR_NULL(pw_d))
+			return PTR_ERR(pw_d);
+
+		supply = regulator_get(&rdev->dev, rdev->supply_name);
+		if (IS_ERR(supply)) {
+			xa_erase(&pse_pw_d_map, pw_d->id);
+			return PTR_ERR(supply);
+		}
+
+		pw_d->supply = supply;
+		pcdev->pi[i].pw_d = pw_d;
+	}
+
+	return 0;
+}
+
 /**
  * pse_controller_register - register a PSE controller device
  * @pcdev: a pointer to the initialized PSE controller device
@@ -499,6 +607,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 +625,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 5d41a1c984bd4..5201a0fb3d744 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 v6 04/12] net: pse-pd: Add support for PSE power domains
Posted by Oleksij Rempel 10 months, 4 weeks ago
On Tue, Mar 04, 2025 at 11:18:53AM +0100, Kory Maincent wrote:
> +/**
> + * 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) {
> +			regulator_put(pw_d->supply);
> +			xa_erase(&pse_pw_d_map, pw_d->id);
> +		}
> +	}
> +}
> +
> +/**
> + * 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, INT_MAX), GFP_KERNEL);

#define PSE_PW_D_LIMIT INT_MAX

XA_LIMIT(1, PSE_PW_D_LIMIT)

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

Should we use xa_lock() before iteration over the map?

> +		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)
> +			continue;
> +
> +		pw_d = devm_pse_alloc_pw_d(pcdev->dev);
> +		if (IS_ERR_OR_NULL(pw_d))
> +			return PTR_ERR(pw_d);

It is better to break the loop and roll back previous allocations.

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

same here.

> +		}
> +
> +		pw_d->supply = supply;
> +		pcdev->pi[i].pw_d = pw_d;
> +	}
> +
> +	return 0;
> +}
-- 
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 v6 04/12] net: pse-pd: Add support for PSE power domains
Posted by Kory Maincent 10 months, 3 weeks ago
On Mon, 17 Mar 2025 10:59:02 +0100
Oleksij Rempel <o.rempel@pengutronix.de> wrote:

> On Tue, Mar 04, 2025 at 11:18:53AM +0100, Kory Maincent wrote:

> > +static int pse_register_pw_ds(struct pse_controller_dev *pcdev)
> > +{
> > +	int i;
> > +
> > +	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;
> > +  
> 
> Should we use xa_lock() before iteration over the map?

I am scratching my head to find out the case where it may be needed.
The only case I think of is two PSE controller with PSE PI using the same power
supply. Which is not a good idea for well distributing power.
In this case we could have two pse_register_pw_ds running concurrently and we
could have issues. Not only the iteration over the map should be protected but
also the content of the pw_d entry. Also when we unbind one of the PSE
controller it will remove all the PSE power domains event if they are used
by another PSE. :/
I need to add refcount and lock on the PSE power domains to fix this case! 

> > +		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)
> > +			continue;
> > +
> > +		pw_d = devm_pse_alloc_pw_d(pcdev->dev);
> > +		if (IS_ERR_OR_NULL(pw_d))
> > +			return PTR_ERR(pw_d);  
> 
> It is better to break the loop and roll back previous allocations.

This will be done by pse_flush_pw_ds called by devm_pse_controller_release as
the pse_controller_register failed.

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