Port-arm archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: sunxigpio vs. LOCKDEBUG (PR 54664)



On Thu, 5 Aug 2021, Jared McNeill wrote:

How about adding a struct fdtbus_regulator to struct fdtbus_regulator_controller? Then container_of() magic to derive the controller from the regulator handle in subsequent fdtbus_regulator_* calls, and we can avoid the allocation completely.

Attached patch implements this. Compile tested only -- let me know if it works for you.

Take care,
Jared
Index: fdt_regulator.c
===================================================================
RCS file: /cvsroot/src/sys/dev/fdt/fdt_regulator.c,v
retrieving revision 1.8
diff -u -p -r1.8 fdt_regulator.c
--- fdt_regulator.c	27 May 2019 23:18:33 -0000	1.8
+++ fdt_regulator.c	6 Aug 2021 15:05:36 -0000
@@ -37,6 +37,9 @@ __KERNEL_RCSID(0, "$NetBSD: fdt_regulato
 #include <libfdt.h>
 #include <dev/fdt/fdtvar.h>
 
+#define	REGULATOR_TO_RC(_reg)	\
+	container_of((_reg), struct fdtbus_regulator_controller, rc_reg)
+
 struct fdtbus_regulator_controller {
 	device_t rc_dev;
 	int rc_phandle;
@@ -44,6 +47,8 @@ struct fdtbus_regulator_controller {
 
 	u_int rc_enable_ramp_delay;
 
+	struct fdtbus_regulator rc_reg;	/* handle returned by acquire() */
+
 	LIST_ENTRY(fdtbus_regulator_controller) rc_next;
 };
 
@@ -60,6 +65,7 @@ fdtbus_register_regulator_controller(dev
 	rc->rc_dev = dev;
 	rc->rc_phandle = phandle;
 	rc->rc_funcs = funcs;
+	rc->rc_reg.reg_rc = rc;
 
 	of_getprop_uint32(phandle, "regulator-enable-ramp-delay", &rc->rc_enable_ramp_delay);
 
@@ -85,7 +91,6 @@ struct fdtbus_regulator *
 fdtbus_regulator_acquire(int phandle, const char *prop)
 {
 	struct fdtbus_regulator_controller *rc;
-	struct fdtbus_regulator *reg;
 	int regulator_phandle;
 	int error;
 
@@ -105,26 +110,21 @@ fdtbus_regulator_acquire(int phandle, co
 		return NULL;
 	}
 
-	reg = kmem_alloc(sizeof(*reg), KM_SLEEP);
-	reg->reg_rc = rc;
-
-	return reg;
+	return &rc->rc_reg;
 }
 
 void
 fdtbus_regulator_release(struct fdtbus_regulator *reg)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 
 	rc->rc_funcs->release(rc->rc_dev);
-
-	kmem_free(reg, sizeof(*reg));
 }
 
 int
 fdtbus_regulator_enable(struct fdtbus_regulator *reg)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 	int error;
 
 	error = rc->rc_funcs->enable(rc->rc_dev, true);
@@ -140,7 +140,7 @@ fdtbus_regulator_enable(struct fdtbus_re
 int
 fdtbus_regulator_disable(struct fdtbus_regulator *reg)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 
 	if (of_hasprop(rc->rc_phandle, "regulator-always-on"))
 		return EIO;
@@ -152,7 +152,7 @@ int
 fdtbus_regulator_set_voltage(struct fdtbus_regulator *reg, u_int min_uvol,
     u_int max_uvol)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 
 	if (rc->rc_funcs->set_voltage == NULL)
 		return EINVAL;
@@ -163,7 +163,7 @@ fdtbus_regulator_set_voltage(struct fdtb
 int
 fdtbus_regulator_get_voltage(struct fdtbus_regulator *reg, u_int *puvol)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 
 	if (rc->rc_funcs->get_voltage == NULL)
 		return EINVAL;
@@ -175,7 +175,7 @@ int
 fdtbus_regulator_supports_voltage(struct fdtbus_regulator *reg, u_int min_uvol,
     u_int max_uvol)
 {
-	struct fdtbus_regulator_controller *rc = reg->reg_rc;
+	struct fdtbus_regulator_controller *rc = REGULATOR_TO_RC(reg);
 	u_int uvol;
 
 	if (rc->rc_funcs->set_voltage == NULL)


Home | Main Index | Thread Index | Old Index