Skip to contentSkip to conversation

WIP: Simplified device model implementation and demo

RFC PATCH New Simon Glass 2 participants 6 messages Apr 24, 2013 – Apr 27, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 WIP: Simplified device model implementation and demo

From: Pavel Herrmann <[email protected]>

** Please note that this is very early code. I am sending out an RFC to get comments. This is not a commit message. This will only build on sandbox and all you can do it try the 'demo' command. I am most interested in comments about how to optimise the implementation to minimise impact on existing drivers, minimise code bloat and make use of existing knowledge users may have another other driver model implementations (e.g. Linux).

This patch adds a very simple drive model implementation. It is taken from the driver model code developed by:

Marek Vasut <[email protected]>
Pavel Herrmann <[email protected]>
Viktor K?iv?k <[email protected]>
Tomas Hlavacek <[email protected]>

and possible others?

Please see doc/driver-model/README.txt for details of how to run this and what to look for.

You can find a test version of the code used here in branch dm2 at:

http://git.denx.de/u-boot-x86.git

(Branch dm contains the original implementation)

This patch requires sandbox generic board support and other enhancements.

---
 Makefile                          |   3 +
 arch/sandbox/include/asm/io.h     |   2 +-
 arch/sandbox/include/asm/types.h  |   4 +-
 common/Makefile                   |   1 +
 common/board_r.c                  |  56 ++++++
 common/cmd_demo.c                 | 126 +++++++++++++
 common/command.c                  |  10 +
 common/dm/Makefile                |  40 ++++
 common/dm/debug.c                 | 137 ++++++++++++++
 common/dm/driver.c                | 374 ++++++++++++++++++++++++++++++++++++++
 common/dm/lists.c                 | 138 ++++++++++++++
 common/dm/root.c                  | 101 ++++++++++
 common/dm/tree.c                  |  71 ++++++++
 common/dm/uclass.c                | 227 +++++++++++++++++++++++
 doc/driver-model/README.txt       | 307 +++++++++++++++++++++++++++++++
 drivers/demo/Makefile             |  44 +++++
 drivers/demo/demo-shape.c         | 106 +++++++++++
 drivers/demo/demo-simple.c        |  82 +++++++++
 drivers/demo/demo-uclass.c        |  52 ++++++
 include/asm-generic/global_data.h |   9 +
 include/asm-generic/gpio.h        |  20 ++
 include/command.h                 |   2 +
 include/common.h                  |   2 +-
 include/config_fallbacks.h        |   8 +
 include/configs/sandbox.h         |   2 +
 include/dm-demo.h                 |  47 +++++
 include/dm.h                      |  27 +++
 include/dm/debug.h                |  33 ++++
 include/dm/manager.h              |  57 ++++++
 include/dm/structures.h           |  97 ++++++++++
 include/dm/uclass.h               |  62 +++++++
 31 files changed, 2243 insertions(+), 4 deletions(-)
 create mode 100644 common/cmd_demo.c
 create mode 100644 common/dm/Makefile
 create mode 100644 common/dm/debug.c
 create mode 100644 common/dm/driver.c
 create mode 100644 common/dm/lists.c
 create mode 100644 common/dm/root.c
 create mode 100644 common/dm/tree.c
 create mode 100644 common/dm/uclass.c
 create mode 100644 doc/driver-model/README.txt
 create mode 100644 drivers/demo/Makefile
 create mode 100644 drivers/demo/demo-shape.c
 create mode 100644 drivers/demo/demo-simple.c
 create mode 100644 drivers/demo/demo-uclass.c
 create mode 100644 include/dm-demo.h
 create mode 100644 include/dm.h
 create mode 100644 include/dm/debug.h
 create mode 100644 include/dm/manager.h
 create mode 100644 include/dm/structures.h
 create mode 100644 include/dm/uclass.h
Makefile +3 -0
index 42f2b02..7f6e007 100644
@@ -337,6 +337,9 @@ LIBS-y += api/libapi.o
 LIBS-y += post/libpost.o
 LIBS-y += test/libtest.o
 
+LIBS-$(CONFIG_DM) += common/dm/libdm.o
+LIBS-$(CONFIG_DM) += drivers/demo/libdemo.o
+
 ifneq ($(CONFIG_AM33XX)$(CONFIG_OMAP34XX)$(CONFIG_OMAP44XX)$(CONFIG_OMAP54XX)$(CONFIG_TI814X),)
 LIBS-y += $(CPUDIR)/omap-common/libomap-common.o
 endif
arch/sandbox/include/asm/io.h +1 -1
index 0c022f1..321bc0d 100644
@@ -54,6 +54,6 @@ static inline void unmap_sysmem(const void *vaddr)
 }
 
 /* Map from a pointer to our RAM buffer */
-phys_addr_t map_to_sysmem(void *ptr);
+phys_addr_t map_to_sysmem(const void *ptr);
 
 #endif
arch/sandbox/include/asm/types.h +2 -2
index 2316c2d..07c2986 100644
@@ -64,8 +64,8 @@ typedef unsigned long long u64;
 #define BITS_PER_LONG	CONFIG_SANDBOX_BITS_PER_LONG
 
 typedef unsigned long dma_addr_t;
-typedef unsigned long phys_addr_t;
-typedef unsigned long phys_size_t;
+typedef u32 phys_addr_t;
+typedef u32 phys_size_t;
 
 #endif /* __KERNEL__ */
 
common/Makefile +1 -0
index 0e0fff1..680ad01 100644
@@ -85,6 +85,7 @@ COBJS-$(CONFIG_CMD_CONSOLE) += cmd_console.o
 COBJS-$(CONFIG_CMD_CPLBINFO) += cmd_cplbinfo.o
 COBJS-$(CONFIG_DATAFLASH_MMC_SELECT) += cmd_dataflash_mmc_mux.o
 COBJS-$(CONFIG_CMD_DATE) += cmd_date.o
+COBJS-$(CONFIG_CMD_DEMO) += cmd_demo.o
 COBJS-$(CONFIG_CMD_SOUND) += cmd_sound.o
 ifdef CONFIG_4xx
 COBJS-$(CONFIG_CMD_SETGETDCR) += cmd_dcr.o
common/board_r.c +56 -0

75 lines not on this page · raw message

common/cmd_demo.c new file +126 -0

126 lines not on this page · raw message

common/command.c +10 -0
index 305a236..70faa60 100644
@@ -554,3 +554,13 @@ enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
 		rc = cmd_usage(cmdtp);
 	return rc;
 }
+
+int cmd_process_error(cmd_tbl_t *cmdtp, int err)
+{
+	if (err) {
+		printf("Command '%s' failed: Error %d\n", cmdtp->name, err);
+		return 1;
+	}
+
+	return 0;
+}
common/dm/Makefile new file +40 -0
new file mode 100644
index 0000000..982a3d5
@@ -0,0 +1,40 @@
+# See file CREDITS for list of people who contributed to this
+# project.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+# MA 02111-1307 USA
+#
+
+include $(TOPDIR)/config.mk
+
+LIB	:= $(obj)libdm.o
+
+COBJS	:= debug.o driver.o lists.o tree.o root.o uclass.o
+SRCS	:= $(COBJS:.o=.c)
+OBJS	:= $(addprefix $(obj),$(COBJS))
+
+all:	$(LIB)
+
+$(LIB):	$(obj).depend $(OBJS)
+	$(call cmd_link_o_target, $(OBJS))
+
+#########################################################################
+
+# defines $(obj).depend target
+include $(SRCTREE)/rules.mk
+
+sinclude $(obj).depend
+
+#########################################################################
common/dm/debug.c new file +137 -0

137 lines not on this page · raw message

common/dm/driver.c new file +374 -0

374 lines not on this page · raw message

common/dm/lists.c new file +138 -0

138 lines not on this page · raw message

common/dm/root.c new file +101 -0

101 lines not on this page · raw message

common/dm/tree.c new file +71 -0

71 lines not on this page · raw message

common/dm/uclass.c new file +227 -0

227 lines not on this page · raw message

doc/driver-model/README.txt new file +307 -0

307 lines not on this page · raw message

drivers/demo/Makefile new file +44 -0

44 lines not on this page · raw message

drivers/demo/demo-shape.c new file +106 -0

106 lines not on this page · raw message

drivers/demo/demo-simple.c new file +82 -0

82 lines not on this page · raw message

drivers/demo/demo-uclass.c new file +52 -0

52 lines not on this page · raw message

include/asm-generic/global_data.h +9 -0
index 5416f46..4425284 100644
@@ -24,6 +24,9 @@
 
 #ifndef __ASM_GENERIC_GBL_DATA_H
 #define __ASM_GENERIC_GBL_DATA_H
+
+#include <linux/list.h>
+
 /*
  * The following data structure is placed in some memory which is
  * available very early after boot (like DPRAM on MPC8xx/MPC82xx, or
@@ -80,6 +83,12 @@ typedef struct global_data {
 	unsigned long start_addr_sp;	/* start_addr_stackpointer */
 	unsigned long reloc_off;
 	struct global_data *new_gd;	/* relocated global data */
+
+#ifdef CONFIG_DM
+	struct device	*dm_root;	/* Root instance for Driver Model */
+	struct list_head class_root;	/* Head of core tree */
+#endif
+
 	const void *fdt_blob;	/* Our device tree, NULL if none */
 	void *new_fdt;		/* Relocated FDT */
 	unsigned long fdt_size;	/* Space reserved for relocated FDT */
include/asm-generic/gpio.h +20 -0
index bfedbe4..ead3515 100644
@@ -94,4 +94,24 @@ int gpio_get_value(unsigned gpio);
  * @return 0 if ok, -1 on error
  */
 int gpio_set_value(unsigned gpio, int value);
+
+/*
+ * Driver model GPIO operations, refer to functions above for description.
+ * These function copy the old API.
+ *
+ * This is trying to be close to Linux GPIO API. Once the U-Boot uses the
+ * new DM GPIO API, this should be really easy to flip over to the Linux
+ * GPIO API-alike interface.
+ */
+struct dm_gpio_ops {
+	int	base;
+	u16	ngpio;
+	int	(*gpio_request)(unsigned gpio, const char *label);
+	int	(*gpio_free)(unsigned gpio);
+	int	(*gpio_direction_input)(unsigned gpio);
+	int	(*gpio_direction_output)(unsigned gpio, int value);
+	int	(*gpio_get_value)(unsigned gpio);
+	int	(*gpio_set_value)(unsigned gpio, int value);
+};
+
 #endif	/* _ASM_GENERIC_GPIO_H_ */
include/command.h +2 -0
index 65692fd..445c9e8 100644
@@ -80,6 +80,8 @@ extern int var_complete(int argc, char * const argv[], char last_char, int maxv,
 extern int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp);
 #endif
 
+int cmd_process_error(cmd_tbl_t *cmdtp, int err);
+
 /*
  * Monitor Command
  *
include/common.h +1 -1
index 28aa4b9..bdf598f 100644
@@ -911,7 +911,7 @@ static inline void unmap_sysmem(const void *vaddr)
 {
 }
 
-static inline phys_addr_t map_to_sysmem(void *ptr)
+static inline phys_addr_t map_to_sysmem(const void *ptr)
 {
 	return (phys_addr_t)(uintptr_t)ptr;
 }
include/config_fallbacks.h +8 -0
index e59ee96..c91a383 100644
@@ -53,4 +53,12 @@
 #define HAVE_BLOCK_DEVICE
 #endif
 
+#ifdef CONFIG_DM
+/* For now, enable the driver model demo command */
+#define CONFIG_DM_DEMO
+#define CONFIG_CMD_DEMO
+#define CONFIG_DM_DEMO_SIMPLE
+#define CONFIG_DM_DEMO_SHAPE
+#endif
+
 #endif	/* __CONFIG_FALLBACKS_H */
include/configs/sandbox.h +2 -0
index 788207d..76c4398 100644
@@ -22,6 +22,8 @@
 #ifndef __CONFIG_H
 #define __CONFIG_H
 
+#define CONFIG_DM
+
 /* Number of bits in a C 'long' on this architecture */
 #define CONFIG_SANDBOX_BITS_PER_LONG	64
 
include/dm-demo.h new file +47 -0

47 lines not on this page · raw message

include/dm.h new file +27 -0
new file mode 100644
index 0000000..6af3900
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2013 Google, Inc
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef _DM_H_
+#define _DM_H
+
+#include <dm/manager.h>
+#include <dm/structures.h>
+#include <dm/uclass.h>
+
+#endif
include/dm/debug.h new file +33 -0
new file mode 100644
index 0000000..3f5ea7b
@@ -0,0 +1,33 @@
+/*
+ * (C) Copyright 2012
+ * Pavel Herrmann <[email protected]>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+
+#ifndef _DM_DEBUG_H_
+#define _DM_DEBUG_H_ 1
+
+#include <dm/structures.h>
+
+int dm_dump_all(void);
+int dm_dump(struct device *i);
+
+#endif
include/dm/manager.h new file +57 -0

57 lines not on this page · raw message

include/dm/structures.h new file +97 -0

97 lines not on this page · raw message

include/dm/uclass.h new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
1.8.2.1
#1 lore raw

RFC PATCH 1/1 WIP: Simplified device model implementation and demo

Hello

Quoted 24 lines · On Wednesday 24 of April 2013 08:53:09 Simon Glass wrote:

From: Pavel Herrmann <[email protected]>

** Please note that this is very early code. I am sending out an RFC to get comments. This is not a commit message. This will only build on sandbox and all you can do it try the 'demo' command. I am most interested in comments about how to optimise the implementation to minimise impact on existing drivers, minimise code bloat and make use of existing knowledge users may have another other driver model implementations (e.g. Linux).

This patch adds a very simple drive model implementation. It is taken from the driver model code developed by:

Marek Vasut <[email protected]>
Pavel Herrmann <[email protected]>
Viktor K?iv?k <[email protected]>
Tomas Hlavacek <[email protected]>

and possible others?

Please see doc/driver-model/README.txt for details of how to run this and what to look for.

You can find a test version of the code used here in branch dm2 at:

Quoted 7 lines
   http://git.denx.de/u-boot-x86.git

(Branch dm contains the original implementation)

This patch requires sandbox generic board support and other enhancements.

Thanks for building on our design!

What actually happened (for those who were wondering) is that we sucesfully defended the university project part of this (which required us to have a working build, that did not necessarily adhere to what would be applicable to U-Boot upstream, so we dropped of the map for some time), and then went into "rethink and upgrade" mode of operations.

I can see that you have already implemented some ot he changes I had in mind (keeping size of private data as a part of the driver, so relocation can be done semi-automatically). I actually had a little more automation in mind (specifically the calls to core_replace, or uclass_replace in your naming scheme, and core_remove), which would mean that for a driver with a flat private data structure (which would be most of them), no relocation hook would be needed. Of course, we could arguably remove relocation as a whole, and use DM only after the relocation has taken place.

I agree with the naming change, we started wth the instances+cores, and then stuck with it (and added some more).

as for (some of) your changes, i would have a few comments:

the driver_instance wrapper around instance was created so that the driver itself doesn't have access to its status flags (currently only the activated flag) and siblings, as it should need them, and definitely should ever modify them. There was no other level of indirection, because the struct instance was embedded in struct driver_instance (eg. not a pointer).

I dont see where/if you actually do anything with the private_data_size (didnt read the whole yet), but i certianly didnt find it used for smarter relocations (see above).
There is one more problem with the DM structures management, which is not entirely obvious - there is currently no way to tell whether platform_data is in static memory or not (and therefore if it should be relocated/freed. this would of course require to remember the size somewhere). this will arise when you start handling dynamically generated devices (disks, USB)

As for removal of platform data, this is not possible. You can load them from FDT for "static" devices, but it will still be required in dynamic devices (again, port number for a disk, USB descriptor number for an USB device). Also relocation/deallocation of platform data is not currently handled in DM code, which causes a slight memory leak.

driver_activate() should not be called by the user (or bootup routine) at any point, instead it should be called by the classes when/before proxying the call to the driver specified implementation. We had it in our bootup code only to show that it is possible to do selectively.

having a list_head for uclass membership in the struct device is not really a good idea, for two reasons. the biggest one is that some of the classes will need to keep some information about the devices, that is not usable outside the class (id/name for a disk, mapping to global linear numbering for GPIO...). This kind of information is used in queries "give me a device with specific properties", instead of the generic "give me Nth device of this class", The other problem is that it disallows the device to support multiple classes (this could probably be worked around by having virtual child devices for this case) - an exapmle that would use this would be a RAM disk, which you would want to still behave as a normal disk, but be tracked by a separate class (so you can resize/delete/whatever it),

I like how you cache the driver structure for each device, instead of looking it up every time. I cannot recall why we didnt use this approach, but i would point out that the name is present/used so we can have runtime loadable drivers sometime in the future (so the symbol would not resolve at link time).

I see that you are not building on the latest version from DM branch. there were a few changes in the DM mechanism there (the one I see now is the USB driver match function, which is useless as defined before we actually implemented USB).

One more thing that is not possible in current DM - sometimes, we would like to call an operation on ourselves as a part of .probe(), which would create an infinite loop in with driver_activate(). an example of this is when probing a bus controller, you probably want to scan the bus.

Thats it for now, I will spend some time over your code in near future, to provide more comments

Pavel Herrmann

#2 lore raw

RFC PATCH 1/1 WIP: Simplified device model implementation and demo

Hi Peter,

Quoted 29 lines · Pavel Herrmann

Hello

Quoted 26 lines · On Wednesday 24 of April 2013 08:53:09 Simon Glass wrote:

From: Pavel Herrmann <[email protected]>

** Please note that this is very early code. I am sending out an RFC to get comments. This is not a commit message. This will only build on sandbox and all you can do it try the 'demo' command. I am most interested in comments about how to optimise the implementation to minimise impact on existing drivers, minimise code bloat and make use of existing knowledge users may have another other driver model implementations (e.g. Linux).

This patch adds a very simple drive model implementation. It is taken from the driver model code developed by:

Marek Vasut <[email protected]>
Pavel Herrmann <[email protected]>
Viktor K?iv?k <[email protected]>
Tomas Hlavacek <[email protected]>

and possible others?

Please see doc/driver-model/README.txt for details of how to run this and what to look for.

You can find a test version of the code used here in branch dm2 at:

   http://git.denx.de/u-boot-x86.git
Quoted 8 lines
Quoted 5 lines

(Branch dm contains the original implementation)

This patch requires sandbox generic board support and other enhancements.

Thanks for building on our design!

Thanks for the comments - it has taken me a bit of time to digest...

Quoted 6 lines

What actually happened (for those who were wondering) is that we sucesfully defended the university project part of this (which required us to have a working build, that did not necessarily adhere to what would be applicable to U-Boot upstream, so we dropped of the map for some time), and then went into "rethink and upgrade" mode of operations.

I'm pleased it all worked out well.

Quoted 9 lines

I can see that you have already implemented some ot he changes I had in mind (keeping size of private data as a part of the driver, so relocation can be done semi-automatically). I actually had a little more automation in mind (specifically the calls to core_replace, or uclass_replace in your naming scheme, and core_remove), which would mean that for a driver with a flat private data structure (which would be most of them), no relocation hook would be needed. Of course, we could arguably remove relocation as a whole, and use DM only after the relocation has taken place.

Yes I am beginning to think that we should drop relocation for now, and add it back when we find a problem that it solves.

Quoted 3 lines

I agree with the naming change, we started wth the instances+cores, and then stuck with it (and added some more).

OK good.

Quoted 8 lines

as for (some of) your changes, i would have a few comments:

the driver_instance wrapper around instance was created so that the driver itself doesn't have access to its status flags (currently only the activated flag) and siblings, as it should need them, and definitely should ever modify them. There was no other level of indirection, because the struct instance was embedded in struct driver_instance (eg. not a pointer).

OK I see. I don't think there is any problem with storing flags or the sibling list node info in the driver data. In fact the whole struct device can be considered to be private to the driver model, except the private data.

I wonder if we should allow drivers to embed struct device in their own structure as Linux does? Then it might avoid private data altogether in the common case. Perhaps driver model could be given a size value, which is the size of the entire structure including struct device, and it could automatically allocate this for you?

Quoted 4 lines

I dont see where/if you actually do anything with the private_data_size (didnt read the whole yet), but i certianly didnt find it used for smarter relocations (see above).

It is allocated for the driver automatically before probe() is callled, and freed after remove() returns.

Quoted 5 lines

There is one more problem with the DM structures management, which is not entirely obvious - there is currently no way to tell whether platform_data is in static memory or not (and therefore if it should be relocated/freed. this would of course require to remember the size somewhere). this will arise when you start handling dynamically generated devices (disks, USB)

I suppose we can check the address range? Or perhaps add a flag to indicate whether the device was created pre-relocation.

Quoted 6 lines

As for removal of platform data, this is not possible. You can load them from FDT for "static" devices, but it will still be required in dynamic devices (again, port number for a disk, USB descriptor number for an USB device). Also relocation/deallocation of platform data is not currently handled in DM code, which causes a slight memory leak.

Here I was referring to the need to declare platform_data static data in your board file. With FDT it should not be needed, since the driver can convert the FDT node into platform_data for you, and then use that.

For dynamic devices, they probably don't have platform_data anyway, but if they do, then I think the FDT can still be used.

It may be true that eliminating platform_data is a step too far in some cases, but I think it is possible.

Quoted 5 lines

driver_activate() should not be called by the user (or bootup routine) at any point, instead it should be called by the classes when/before proxying the call to the driver specified implementation. We had it in our bootup code only to show that it is possible to do selectively.

Yes I need to tidy that up.

Quoted 12 lines

having a list_head for uclass membership in the struct device is not really a good idea, for two reasons. the biggest one is that some of the classes will need to keep some information about the devices, that is not usable outside the class (id/name for a disk, mapping to global linear numbering for GPIO...). This kind of information is used in queries "give me a device with specific properties", instead of the generic "give me Nth device of this class", The other problem is that it disallows the device to support multiple classes (this could probably be worked around by having virtual child devices for this case) - an exapmle that would use this would be a RAM disk, which you would want to still behave as a normal disk, but be tracked by a separate class (so you can resize/delete/whatever it),

To take your second point first, I think that devices which implement multiple uclasses should be considered multi-function-devices (MFD). These then to sit in a uclass of their own. Then, they can allocate separate individual devices for each uclass. This seems cleaner than dealing with a device in multiple uclasses. It also mirrors how this is done in Linux.

Re the uclass information, that is true. We need a way for the uclass code to store information about each device in a uclass. I will have a think about how to do that, perhaps another private data pointer?

Quoted 6 lines

I like how you cache the driver structure for each device, instead of looking it up every time. I cannot recall why we didnt use this approach, but i would point out that the name is present/used so we can have runtime loadable drivers sometime in the future (so the symbol would not resolve at link time).

OK, there are a few things like that which could do with work.

Quoted 4 lines

I see that you are not building on the latest version from DM branch. there were a few changes in the DM mechanism there (the one I see now is the USB driver match function, which is useless as defined before we actually implemented USB).

OK - I did compare the code against the final commit in that branch. There certainly were changes, but not many to the core code, so I decided it did not matter for now.

Quoted 5 lines

One more thing that is not possible in current DM - sometimes, we would like to call an operation on ourselves as a part of .probe(), which would create an infinite loop in with driver_activate(). an example of this is when probing a bus controller, you probably want to scan the bus.

What sort of operation do you mean here? I would probably hold that when a driver is being probed, it can't use driver model calls to itself from within its probe function. If it needs to, it can call its own functions directly from probe. For example, if it wants to read from the device, ti can call its local read() function instead of trying to call the driver model 'read' handler for itself. The effect should be the same but it will be much less confusing.

But perhaps I have missed something here.

Quoted 3 lines

Thats it for now, I will spend some time over your code in near future, to provide more comments

Thanks again for your comments and insight. I am hoping to find the time to respin this again soon.

Regards,
Simon

Quoted 2 lines

Pavel Herrmann

#3 lore raw

RFC PATCH 1/1 WIP: Simplified device model implementation and demo

Hi

Quoted 3 lines · On Friday 26 of April 2013 06:30:17 Simon Glass wrote:

Hi Peter,

On Wed, Apr 24, 2013 at 9:51 AM, Pavel Herrmann <[email protected]>

Quoted 28 lines · wrote:
Quoted 6 lines

Hello

Quoted 1 line · On Wednesday 24 of April 2013 08:53:09 Simon Glass wrote:

snip

Thanks for building on our design!

Thanks for the comments - it has taken me a bit of time to digest...

Quoted 6 lines

What actually happened (for those who were wondering) is that we sucesfully
defended the university project part of this (which required us to have a working build, that did not necessarily adhere to what would be applicable to U-Boot upstream, so we dropped of the map for some time), and then went into "rethink and upgrade" mode of operations.

I'm pleased it all worked out well.

Quoted 9 lines

I can see that you have already implemented some ot he changes I had in mind (keeping size of private data as a part of the driver, so relocation can be done semi-automatically). I actually had a little more automation in mind (specifically the calls to core_replace, or uclass_replace in your naming scheme, and core_remove), which would mean that for a driver with a flat private data structure (which would be most of them), no relocation hook would be needed. Of course, we could arguably remove relocation as a whole, and use DM only after the relocation has taken place.

Quoted 2 lines

Yes I am beginning to think that we should drop relocation for now, and add it back when we find a problem that it solves.

That would certainly make things simpler, but then one must be very cautious not to make reintroduction of it impossible/significantly harder.

The basic idea behind the whole running in early phase was to get serial port (as a debug tool) up and running as soon as possible, without any special handling for it.
Later it went as far as doing the whole boot without relocation (if we have enough early memory, or we can initialize RAM but not copy the U-Boot binary there, just the kernel), and then went to being mostly just early access to serial port.

Quoted 14 lines
Quoted 2 lines

I agree with the naming change, we started wth the instances+cores, and then stuck with it (and added some more).

OK good.

Quoted 8 lines

as for (some of) your changes, i would have a few comments:

the driver_instance wrapper around instance was created so that the driver itself doesn't have access to its status flags (currently only the activated flag) and siblings, as it should need them, and definitely should ever modify them. There was no other level of indirection, because the struct instance was embedded in struct driver_instance (eg. not a pointer).

Quoted 4 lines

OK I see. I don't think there is any problem with storing flags or the sibling list node info in the driver data. In fact the whole struct device can be considered to be private to the driver model, except the private data.

parent and platform_data is also of use to the driver, for devices not direcly mapped in memory, but behind some non-transparent bus.

Quoted 5 lines

I wonder if we should allow drivers to embed struct device in their own structure as Linux does? Then it might avoid private data altogether in the common case. Perhaps driver model could be given a size value, which is the size of the entire structure including struct device, and it could automatically allocate this for you?

This would make any automation of relocation very hard (because you would need to keep this size information in the tree, instead of just using it once). otherwise I don't see much of a problem with that (the arguments would probably be the size of your whole data structure, and offset of struct device in it, or you would rather have that as a field in the u_boot_driver structure, solving the relocation issue as well).
One minor issue could arise if you didn't know the exact size of your private data at compile time, and would only get it after querying the parent in some way during .probe()

Quoted 14 lines
Quoted 3 lines

I dont see where/if you actually do anything with the private_data_size (didnt read the whole yet), but i certianly didnt find it used for smarter relocations (see above).

It is allocated for the driver automatically before probe() is callled, and freed after remove() returns.

Quoted 6 lines

There is one more problem with the DM structures management, which is not entirely obvious - there is currently no way to tell whether platform_data is in static memory or not (and therefore if it should be relocated/freed. this would of course require to remember the size somewhere). this will arise when you start handling dynamically generated devices (disks, USB)

Quoted 2 lines

I suppose we can check the address range? Or perhaps add a flag to indicate whether the device was created pre-relocation.

Address checking was one of the ideas we had in mind, but it was dismissed as "too hackish". Another idea was to just always make a copy on .reloc() regardless (no free, as reloc() assumes the old memory area will be discarded as a whole afterwards), and depend on free doing the right thing on .remove(). flags are probably a good idea, since you already have a field that is almost empty. You would either need an extra parameter to driver_bind() (since C doesnt have default argument values, it would need to be present in every call), or have a separate driver_dynamic_bind() function that would set the flag to indicate that platform_data is in dynamic memory.

This also applies to driver_info structure, but i think it would be safe to only allow driver_info + driver name + platform_data statically, or platform_data and driver_info on heap, driver name statically as valid combinations, so a single flag would be sufficient.

Quoted 11 lines
Quoted 5 lines

As for removal of platform data, this is not possible. You can load them from FDT for "static" devices, but it will still be required in dynamic devices (again, port number for a disk, USB descriptor number for an USB device). Also relocation/deallocation of platform data is not currently handled in DM code, which causes a slight memory leak.

Here I was referring to the need to declare platform_data static data in your board file. With FDT it should not be needed, since the driver can convert the FDT node into platform_data for you, and then use that.

Quoted 5 lines

For dynamic devices, they probably don't have platform_data anyway, but if they do, then I think the FDT can still be used.

It may be true that eliminating platform_data is a step too far in some cases, but I think it is possible.

Not sure we agree on what is "dynamic devices" here (or maybe on what you can do with FDT). I use "dynamic devices" to refer to devices that can be connected on runtime (or before, of course), and therefore not known before booting and actually scanning available busses. These devices are basically anything that sits on a Plug-and-Play bus, like USB, PCI or SATA

We had a rather long discussion (more than a year ago, apologies if i forgot/mixed something up) about the intended semantics of the parent/bus calls, but the one we chose actually needs platform_data for dynamically created devices.
One option for calling bus drivers had a call(str device bus, str device child, ...) template, where the bus would be responsible for mapping children to actual bus addresses, using some sort of structure in its private data.

The option we went for is the one with call(str device bus, whatever child_addr, ...) template. the child_addr type of course depends on the actual bus in question, and the actual value was stored in the platform_data of the child when it was found/bound.

The reason why we chose the second approach is because the first one doesn't handle situations where you have devices in the tree between the bus controller and the real child, but on the bus protocol those devices operate transparently (USB hubs for instance). In that case, the controller would receive a call from either a child it doesn't know, or from the hub, but then it would resolve a wrong bus address.

Quoted 20 lines
Quoted 4 lines

driver_activate() should not be called by the user (or bootup routine) at any point, instead it should be called by the classes when/before proxying the call to the driver specified implementation. We had it in our bootup code only to show that it is possible to do selectively.

Yes I need to tidy that up.

Quoted 4 lines

having a list_head for uclass membership in the struct device is not really a good idea, for two reasons. the biggest one is that some of the classes will need to keep some information about the devices, that is not usable outside the class (id/name for a disk, mapping to global linear

Quoted 8 lines

numbering for GPIO...). This kind of information is used in queries "give me a device with specific properties", instead of the generic "give me Nth device of this class", The other problem is that it disallows the device to support multiple classes (this could probably be worked around by having virtual child devices for this case) - an exapmle that would use this would be a RAM disk, which you would want to still behave as a normal disk, but be tracked by a separate class (so you can resize/delete/whatever it),

Quoted 6 lines

To take your second point first, I think that devices which implement multiple uclasses should be considered multi-function-devices (MFD). These then to sit in a uclass of their own. Then, they can allocate separate individual devices for each uclass. This seems cleaner than dealing with a device in multiple uclasses. It also mirrors how this is done in Linux.

I guess that is the smarter way of doing it, since there will not be many of such devices. The issue I see would be the interface betweeen the MFD and its children - you either could have a very generic interface for all of them, or have the children play dirty with the parent pointer (getting the ops structure directly and casting it as necessary, which was not really possible with our design, where all calls had to go through the class), or something in the middle (maybe having set interfaces for specific set of functions? that could get very ugly very quick)

Quoted 3 lines

Re the uclass information, that is true. We need a way for the uclass code to store information about each device in a uclass. I will have a think about how to do that, perhaps another private data pointer?

the original design for uclass structure had a strongly-typed (as in not depening on the actual class like now) list of devices in it, and had to store a separate list in the private data (which contained the ops, and possibly other data).
after debating proper pairing (structures with the same index being considered paired was considered not robust enough) of those structures we abondoned this idea and merged the two lists into one, making it opque to the outside of the class code.
Since you do not need the ops stored for each device, you would end up with most classes not keeping anything, and thus private data for any that do would probably be a way to go.
The only thing left is figuring out if you want to store a full mapping (having both the device pointer and its additional info in private data), or just keep the additional info and pair by list indicies (which basically works, but requires some work to code properly, and is harder to debug if things break)

Quoted 13 lines
Quoted 5 lines

I like how you cache the driver structure for each device, instead of looking it up every time. I cannot recall why we didnt use this approach, but i would point out that the name is present/used so we can have runtime loadable drivers sometime in the future (so the symbol would not resolve at link time).

OK, there are a few things like that which could do with work.

Quoted 5 lines

I see that you are not building on the latest version from DM branch. there
were a few changes in the DM mechanism there (the one I see now is the USB driver match function, which is useless as defined before we actually implemented USB).

Quoted 3 lines

OK - I did compare the code against the final commit in that branch. There certainly were changes, but not many to the core code, so I decided it did not matter for now.

Yes, most of the work was porting drivers, so we could show that all of the board works. the changes in core code was only done when we found some overlooked design flaws (I think there were some minor changes in class relocation, and the USB match, can't remember anything else)

Quoted 13 lines
Quoted 4 lines

One more thing that is not possible in current DM - sometimes, we would like to call an operation on ourselves as a part of .probe(), which would create an infinite loop in with driver_activate(). an example of this is when probing a bus controller, you probably want to scan the bus.

What sort of operation do you mean here? I would probably hold that when a driver is being probed, it can't use driver model calls to itself from within its probe function. If it needs to, it can call its own functions directly from probe. For example, if it wants to read from the device, ti can call its local read() function instead of trying to call the driver model 'read' handler for itself. The effect should be the same but it will be much less confusing.

Quoted 1 line

But perhaps I have missed something here.

The idea is that if your device exports only a very simple I/O, and more complex operations (like scanning for and initializing new child devices on a bus) are actually wrapped in the uclass code. this would mean that calling the complex code would call the proxy, resulting in a call to driver_activate() and an endless recursion.

This could probably be solved by having init_started and init_finished flags, to break the recursion (check for init_started, set init_started, call .probe(), set init_finished). This should work as long as the drivers are decent (using their class-provided functions only at the very end of .probe()), and some sanity checking (like what to do when removing a device that has init_started but not init_finished).

I am not sure where I encountered this issue at this point, so feel free to drop it until it comes up (or it doesn't, maybe it was caused by a bad prior design decision on my part).

Come to think about it, it would also be handy to do this from .bind() hook, but that should work fine at the moment (the use for this would be in USB storage, which would like to scan for partitions when found on USB, without any special handling in the USB code)

Quoted 5 lines
Quoted 2 lines

Thats it for now, I will spend some time over your code in near future, to provide more comments

Thanks again for your comments and insight. I am hoping to find the time to respin this again soon.

Apologies if this doesnt make sense at some point, the answers were not done in linear order, and thus may contain contradictions or forward references or be just plain wrong

Thanks
Pavel

#4 lore raw

RFC PATCH 1/1 WIP: Simplified device model implementation and demo

Hi Pavel,

Quoted 41 lines · Pavel Herrmann

Hi

Quoted 3 lines · On Friday 26 of April 2013 06:30:17 Simon Glass wrote:

Hi Peter,

On Wed, Apr 24, 2013 at 9:51 AM, Pavel Herrmann <[email protected]>

Quoted 28 lines · wrote:
Quoted 6 lines

Hello

Quoted 1 line · On Wednesday 24 of April 2013 08:53:09 Simon Glass wrote:

snip

Thanks for building on our design!

Thanks for the comments - it has taken me a bit of time to digest...

Quoted 6 lines

What actually happened (for those who were wondering) is that we sucesfully
defended the university project part of this (which required us to have a working build, that did not necessarily adhere to what would be applicable to U-Boot upstream, so we dropped of the map for some time), and then went into "rethink and upgrade" mode of operations.

I'm pleased it all worked out well.

Quoted 9 lines

I can see that you have already implemented some ot he changes I had in mind (keeping size of private data as a part of the driver, so relocation can be done semi-automatically). I actually had a little more automation in mind (specifically the calls to core_replace, or uclass_replace in your naming scheme, and core_remove), which would mean that for a driver with a flat private data structure (which would be most of them), no relocation hook would be needed. Of course, we could arguably remove relocation as a whole, and use DM only after the relocation has taken place.

Quoted 2 lines

Yes I am beginning to think that we should drop relocation for now, and add it back when we find a problem that it solves.

That would certainly make things simpler, but then one must be very cautious not to make reintroduction of it impossible/significantly harder.

Quoted 7 lines

The basic idea behind the whole running in early phase was to get serial port (as a debug tool) up and running as soon as possible, without any special handling for it.
Later it went as far as doing the whole boot without relocation (if we have enough early memory, or we can initialize RAM but not copy the U-Boot binary there, just the kernel), and then went to being mostly just early access to serial port.

OK, let's make the decision to drop it for now, in favour of some sort of link back to the pre-relocation data during probe. So we call the driver's probe() twice - one before relocation, once after. If the drivers wants to check whether it has already been probed, we can provide a way of doing this.

We perhaps need to add a capability flag, to indicate that the driver can be used prior to relocation. I suspect some won't work.

Quoted 15 lines
Quoted 15 lines
Quoted 3 lines

I agree with the naming change, we started wth the instances+cores, and then stuck with it (and added some more).

OK good.

Quoted 8 lines

as for (some of) your changes, i would have a few comments:

the driver_instance wrapper around instance was created so that the driver itself doesn't have access to its status flags (currently only the activated flag) and siblings, as it should need them, and definitely should ever modify them. There was no other level of indirection, because the struct instance was embedded in struct driver_instance (eg. not a pointer).

Quoted 7 lines
Quoted 4 lines

OK I see. I don't think there is any problem with storing flags or the sibling list node info in the driver data. In fact the whole struct device can be considered to be private to the driver model, except the private data.

parent and platform_data is also of use to the driver, for devices not direcly mapped in memory, but behind some non-transparent bus.

Yes that's right.

Quoted 7 lines
Quoted 6 lines

I wonder if we should allow drivers to embed struct device in their own structure as Linux does? Then it might avoid private data altogether in the common case. Perhaps driver model could be given a size value, which is the size of the entire structure including struct device, and it could automatically allocate this for you?

Quoted 9 lines

This would make any automation of relocation very hard (because you would need to keep this size information in the tree, instead of just using it once). otherwise I don't see much of a problem with that (the arguments would probably be the size of your whole data structure, and offset of struct device in it, or you would rather have that as a field in the u_boot_driver structure, solving the relocation issue as well).
One minor issue could arise if you didn't know the exact size of your private data at compile time, and would only get it after querying the parent in some way during .probe()

We could require the driver to provide this information in its structure if it wants to use this feature.

Quoted 28 lines
Quoted 15 lines
Quoted 4 lines

I dont see where/if you actually do anything with the private_data_size (didnt read the whole yet), but i certianly didnt find it used for smarter relocations (see above).

It is allocated for the driver automatically before probe() is callled, and freed after remove() returns.

Quoted 6 lines

There is one more problem with the DM structures management, which is not entirely obvious - there is currently no way to tell whether platform_data is in static memory or not (and therefore if it should be relocated/freed. this would of course require to remember the size somewhere). this will arise when you start handling dynamically generated devices (disks, USB)

Quoted 2 lines

I suppose we can check the address range? Or perhaps add a flag to indicate whether the device was created pre-relocation.

Address checking was one of the ideas we had in mind, but it was dismissed as "too hackish". Another idea was to just always make a copy on .reloc() regardless (no free, as reloc() assumes the old memory area will be discarded as a whole afterwards), and depend on free doing the right thing on .remove(). flags are probably a good idea, since you already have a field that is almost empty. You would either need an extra parameter to driver_bind() (since C doesnt have default argument values, it would need to be present in every call), or have a separate driver_dynamic_bind() function that would set the flag to indicate that platform_data is in dynamic memory.

Quoted 4 lines

This also applies to driver_info structure, but i think it would be safe to only allow driver_info + driver name + platform_data statically, or platform_data and driver_info on heap, driver name statically as valid combinations, so a single flag would be sufficient.

Well I don't think driver rmodel is responsible for platform data. It is an input to driver model.

So if it is static data then it will probably be in rodata or data sections, and will be relocated by U-Boot. We can go through and update the pointers in the hackish way if we wish.

If it is allocated data, then I am not sure, but that seems like a strange case.

For things like USB, it will be the USB subsystem drivers which create the platform_data presumably. So they are responsible for managing it, not driver model per se.

So I think/hope we can not worry about this for now...

Quoted 37 lines
Quoted 12 lines
Quoted 6 lines

As for removal of platform data, this is not possible. You can load them from FDT for "static" devices, but it will still be required in dynamic devices (again, port number for a disk, USB descriptor number for an USB device). Also relocation/deallocation of platform data is not currently handled in DM code, which causes a slight memory leak.

Here I was referring to the need to declare platform_data static data in your board file. With FDT it should not be needed, since the driver can convert the FDT node into platform_data for you, and then use that.

Quoted 5 lines

For dynamic devices, they probably don't have platform_data anyway, but if they do, then I think the FDT can still be used.

It may be true that eliminating platform_data is a step too far in some cases, but I think it is possible.

Not sure we agree on what is "dynamic devices" here (or maybe on what you can do with FDT). I use "dynamic devices" to refer to devices that can be connected on runtime (or before, of course), and therefore not known before booting and actually scanning available busses. These devices are basically anything that sits on a Plug-and-Play bus, like USB, PCI or SATA

We had a rather long discussion (more than a year ago, apologies if i forgot/mixed something up) about the intended semantics of the parent/bus calls, but the one we chose actually needs platform_data for dynamically created devices.
One option for calling bus drivers had a call(str device bus, str device child, ...) template, where the bus would be responsible for mapping children to actual bus addresses, using some sort of structure in its private data.

The option we went for is the one with call(str device bus, whatever child_addr, ...) template. the child_addr type of course depends on the actual bus in question, and the actual value was stored in the platform_data of the child when it was found/bound.

Quoted 6 lines

The reason why we chose the second approach is because the first one doesn't handle situations where you have devices in the tree between the bus controller and the real child, but on the bus protocol those devices operate transparently (USB hubs for instance). In that case, the controller would receive a call from either a child it doesn't know, or from the hub, but then it would resolve a wrong bus address.

OK, well in this case platform_data is no different for static and dynamic devices. In many cases, dynamic devices will not have platform_data, or a device tree node. But there should be nothing stopping that from being possible.

So yes I think providing platform data / FDT node to dynamic devices is fine.

Quoted 28 lines
Quoted 21 lines
Quoted 5 lines

driver_activate() should not be called by the user (or bootup routine) at any point, instead it should be called by the classes when/before proxying the call to the driver specified implementation. We had it in our bootup code only to show that it is possible to do selectively.

Yes I need to tidy that up.

Quoted 4 lines

having a list_head for uclass membership in the struct device is not really a good idea, for two reasons. the biggest one is that some of the classes will need to keep some information about the devices, that is not usable outside the class (id/name for a disk, mapping to global linear

Quoted 8 lines

numbering for GPIO...). This kind of information is used in queries "give me a device with specific properties", instead of the generic "give me Nth device of this class", The other problem is that it disallows the device to support multiple classes (this could probably be worked around by having virtual child devices for this case) - an exapmle that would use this would be a RAM disk, which you would want to still behave as a normal disk, but be tracked by a separate class (so you can resize/delete/whatever it),

Quoted 6 lines

To take your second point first, I think that devices which implement multiple uclasses should be considered multi-function-devices (MFD). These then to sit in a uclass of their own. Then, they can allocate separate individual devices for each uclass. This seems cleaner than dealing with a device in multiple uclasses. It also mirrors how this is done in Linux.

Quoted 8 lines

I guess that is the smarter way of doing it, since there will not be many of such devices. The issue I see would be the interface betweeen the MFD and its children - you either could have a very generic interface for all of them, or have the children play dirty with the parent pointer (getting the ops structure directly and casting it as necessary, which was not really possible with our design, where all calls had to go through the class), or something in the middle (maybe having set interfaces for specific set of functions? that could get very ugly very quick)

I think for now we need something device-specific. Each child will have knowledge of the parent driver, which may export some operations which the children can use. We can still use the driver model abstraction, but it doesn't need to be handled specially by driver model I think.

Quoted 5 lines
Quoted 4 lines

Re the uclass information, that is true. We need a way for the uclass code to store information about each device in a uclass. I will have a think about how to do that, perhaps another private data pointer?

Quoted 11 lines

the original design for uclass structure had a strongly-typed (as in not depening on the actual class like now) list of devices in it, and had to store a separate list in the private data (which contained the ops, and possibly other data).
after debating proper pairing (structures with the same index being considered paired was considered not robust enough) of those structures we abondoned this idea and merged the two lists into one, making it opque to the outside of the class code.
Since you do not need the ops stored for each device, you would end up with most classes not keeping anything, and thus private data for any that do would probably be a way to go.

'
OK.

Quoted 4 lines

The only thing left is figuring out if you want to store a full mapping (having both the device pointer and its additional info in private data), or just keep the additional info and pair by list indicies (which basically works, but requires some work to code properly, and is harder to debug if things break)

I am thinking putting a private data pointer into struct device, i.e. making that structure responsible for it. Of course this wastes a pointer if most uclasses don't need to store anything about each of their devices.

Quoted 14 lines
Quoted 14 lines
Quoted 6 lines

I like how you cache the driver structure for each device, instead of looking it up every time. I cannot recall why we didnt use this approach, but i would point out that the name is present/used so we can have runtime loadable drivers sometime in the future (so the symbol would not resolve at link time).

OK, there are a few things like that which could do with work.

Quoted 5 lines

I see that you are not building on the latest version from DM branch. there
were a few changes in the DM mechanism there (the one I see now is the USB driver match function, which is useless as defined before we actually implemented USB).

Quoted 8 lines
Quoted 3 lines

OK - I did compare the code against the final commit in that branch. There certainly were changes, but not many to the core code, so I decided it did not matter for now.

Yes, most of the work was porting drivers, so we could show that all of the board works. the changes in core code was only done when we found some overlooked design flaws (I think there were some minor changes in class relocation, and the USB match, can't remember anything else)

OK great.

Quoted 29 lines
Quoted 14 lines
Quoted 5 lines

One more thing that is not possible in current DM - sometimes, we would like to call an operation on ourselves as a part of .probe(), which would create an infinite loop in with driver_activate(). an example of this is when probing a bus controller, you probably want to scan the bus.

What sort of operation do you mean here? I would probably hold that when a driver is being probed, it can't use driver model calls to itself from within its probe function. If it needs to, it can call its own functions directly from probe. For example, if it wants to read from the device, ti can call its local read() function instead of trying to call the driver model 'read' handler for itself. The effect should be the same but it will be much less confusing.

Quoted 1 line

But perhaps I have missed something here.

The idea is that if your device exports only a very simple I/O, and more complex operations (like scanning for and initializing new child devices on a bus) are actually wrapped in the uclass code. this would mean that calling the complex code would call the proxy, resulting in a call to driver_activate() and an endless recursion.

This could probably be solved by having init_started and init_finished flags, to break the recursion (check for init_started, set init_started, call .probe(), set init_finished). This should work as long as the drivers are decent (using their class-provided functions only at the very end of .probe()), and some sanity checking (like what to do when removing a device that has init_started but not init_finished).

Quoted 8 lines

I am not sure where I encountered this issue at this point, so feel free to drop it until it comes up (or it doesn't, maybe it was caused by a bad prior design decision on my part).

Come to think about it, it would also be handy to do this from .bind() hook, but that should work fine at the moment (the use for this would be in USB storage, which would like to scan for partitions when found on USB, without any special handling in the USB code)

OK, let's leave this for now and see if it comes up again in practice. I'm not quite sure the best way to solve it, other than what I suggested.

Quoted 10 lines
Quoted 6 lines
Quoted 3 lines

Thats it for now, I will spend some time over your code in near future, to provide more comments

Thanks again for your comments and insight. I am hoping to find the time to respin this again soon.

Apologies if this doesnt make sense at some point, the answers were not done in linear order, and thus may contain contradictions or forward references or be just plain wrong

It makes sense to me, thanks for your comments. I will start looking at a new patch.

Regards,
Simon

Quoted 3 lines

Thanks
Pavel

#5 lore raw

RFC PATCH 1/1 WIP: Simplified device model implementation and demo

Quoted 3 lines · On Saturday 27 of April 2013 09:08:48 Simon Glass wrote:

Hi Pavel,

On Fri, Apr 26, 2013 at 8:35 AM, Pavel Herrmann <[email protected]>

Quoted 57 lines · wrote:
Quoted 48 lines

Hi

Quoted 3 lines · On Friday 26 of April 2013 06:30:17 Simon Glass wrote:

Hi Peter,

On Wed, Apr 24, 2013 at 9:51 AM, Pavel Herrmann <[email protected]>

Quoted 33 lines · wrote:
Quoted 6 lines

Hello

Quoted 1 line · On Wednesday 24 of April 2013 08:53:09 Simon Glass wrote:

snip

Thanks for building on our design!

Thanks for the comments - it has taken me a bit of time to digest...

Quoted 8 lines

What actually happened (for those who were wondering) is that we sucesfully
defended the university project part of this (which required us to have a
working build, that did not necessarily adhere to what would be applicable
to U-Boot upstream, so we dropped of the map for some time), and then went into "rethink and upgrade" mode of operations.

I'm pleased it all worked out well.

Quoted 4 lines

I can see that you have already implemented some ot he changes I had in mind (keeping size of private data as a part of the driver, so relocation
can be done semi-automatically). I actually had a little more

Quoted 8 lines

automation
in mind (specifically the calls to core_replace, or uclass_replace in your naming scheme, and core_remove), which would mean that for a driver
with a flat private data structure (which would be most of them), no relocation hook would be needed. Of course, we could arguably remove relocation as a whole, and use DM only after the relocation has taken place.

Quoted 2 lines

Yes I am beginning to think that we should drop relocation for now, and add it back when we find a problem that it solves.

That would certainly make things simpler, but then one must be very cautious not to make reintroduction of it impossible/significantly harder.

Quoted 8 lines

The basic idea behind the whole running in early phase was to get serial port (as a debug tool) up and running as soon as possible, without any special handling for it.
Later it went as far as doing the whole boot without relocation (if we have
enough early memory, or we can initialize RAM but not copy the U-Boot binary there, just the kernel), and then went to being mostly just early access to serial port.

Quoted 8 lines

OK, let's make the decision to drop it for now, in favour of some sort of link back to the pre-relocation data during probe. So we call the driver's probe() twice - one before relocation, once after. If the drivers wants to check whether it has already been probed, we can provide a way of doing this.

We perhaps need to add a capability flag, to indicate that the driver can be used prior to relocation. I suspect some won't work.

there is just too much that can happen - either the driver can work in both phases, or it works but needs a relocation hook (which is what we do now), or it can only work in one of the stages (probably the later one, but there might be some very wierd drivers), or it could work in both, but could not be easily relocated (therefore do .remove() and .probe() instead of .reloc()).

I agree the best way now is dropping this, but keeping it around as a possible advanced feature (like the runtime driver loading)

Quoted 42 lines
Quoted 16 lines
Quoted 16 lines
Quoted 2 lines

I agree with the naming change, we started wth the instances+cores, and then stuck with it (and added some more).

OK good.

Quoted 10 lines

as for (some of) your changes, i would have a few comments:

the driver_instance wrapper around instance was created so that the driver
itself doesn't have access to its status flags (currently only the activated flag) and siblings, as it should need them, and definitely should ever modify them. There was no other level of indirection, because
the struct instance was embedded in struct driver_instance (eg. not a pointer).

Quoted 7 lines
Quoted 4 lines

OK I see. I don't think there is any problem with storing flags or the sibling list node info in the driver data. In fact the whole struct device can be considered to be private to the driver model, except the private data.

parent and platform_data is also of use to the driver, for devices not direcly mapped in memory, but behind some non-transparent bus.

Yes that's right.

Quoted 6 lines
Quoted 5 lines

I wonder if we should allow drivers to embed struct device in their own structure as Linux does? Then it might avoid private data altogether in the common case. Perhaps driver model could be given a size value, which is the size of the entire structure including struct device, and it could automatically allocate this for you?

Quoted 9 lines

This would make any automation of relocation very hard (because you would need to keep this size information in the tree, instead of just using it once). otherwise I don't see much of a problem with that (the arguments would probably be the size of your whole data structure, and offset of struct device in it, or you would rather have that as a field in the u_boot_driver structure, solving the relocation issue as well). One minor issue could arise if you didn't know the exact size of your private data at compile time, and would only get it after querying the parent in some way during .probe()

Quoted 2 lines

We could require the driver to provide this information in its structure if it wants to use this feature.

This starts to bloat the driver structure a little, but as it is in rodata it should not be such an issue.
Agreed.

Quoted 44 lines
Quoted 31 lines
Quoted 17 lines
Quoted 3 lines

I dont see where/if you actually do anything with the private_data_size (didnt read the whole yet), but i certianly didnt find it used for smarter relocations (see above).

It is allocated for the driver automatically before probe() is callled, and freed after remove() returns.

Quoted 9 lines

There is one more problem with the DM structures management, which is not
entirely obvious - there is currently no way to tell whether platform_data
is in static memory or not (and therefore if it should be relocated/freed. this would of course require to remember the size somewhere). this will arise when you start handling dynamically generated
devices (disks, USB)

Quoted 2 lines

I suppose we can check the address range? Or perhaps add a flag to indicate whether the device was created pre-relocation.

Address checking was one of the ideas we had in mind, but it was dismissed as "too hackish". Another idea was to just always make a copy on .reloc() regardless (no free, as reloc() assumes the old memory area will be discarded as a whole afterwards), and depend on free doing the right thing on .remove(). flags are probably a good idea, since you already have a field that is almost empty. You would either need an extra parameter to driver_bind() (since C doesnt have default argument values, it would need to be present in every call), or have a separate driver_dynamic_bind() function that would set the flag to indicate that platform_data is in dynamic memory.

Quoted 5 lines

This also applies to driver_info structure, but i think it would be safe to
only allow driver_info + driver name + platform_data statically, or platform_data and driver_info on heap, driver name statically as valid combinations, so a single flag would be sufficient.

Well I don't think driver rmodel is responsible for platform data. It is an input to driver model.

So if it is static data then it will probably be in rodata or data sections, and will be relocated by U-Boot. We can go through and update the pointers in the hackish way if we wish.

Quoted 8 lines

If it is allocated data, then I am not sure, but that seems like a strange case.

For things like USB, it will be the USB subsystem drivers which create the platform_data presumably. So they are responsible for managing it, not driver model per se.

So I think/hope we can not worry about this for now...

I think the driver model has to worry about this, specifically the driver_unbind() function.

In case of USB (as implemented in our DM tree), the child nodes are created by the USB class, along with platform_data based on the driver probing. However, the class is not called when the device structure is being deallocated (not even via the class_unbind(), since the children do not implement USB interface). At this point, the driver_unbind() function should somehow know whether to free the platform_data as well.
This occurs on every call to "usb reset", so its not that rare.

Again, assuming we drop relocations now, this would only happen on busses that are rescanned on user command (like USB), but still should be handled in a clean and generic way.

Quoted 47 lines
Quoted 40 lines
Quoted 13 lines
Quoted 7 lines

As for removal of platform data, this is not possible. You can load them
from FDT for "static" devices, but it will still be required in dynamic devices (again, port number for a disk, USB descriptor number for an USB
device). Also relocation/deallocation of platform data is not currently handled in DM code, which causes a slight memory leak.

Here I was referring to the need to declare platform_data static data in your board file. With FDT it should not be needed, since the driver can convert the FDT node into platform_data for you, and then use that.

Quoted 5 lines

For dynamic devices, they probably don't have platform_data anyway, but if they do, then I think the FDT can still be used.

It may be true that eliminating platform_data is a step too far in some cases, but I think it is possible.

Not sure we agree on what is "dynamic devices" here (or maybe on what you can do with FDT). I use "dynamic devices" to refer to devices that can be connected on runtime (or before, of course), and therefore not known before booting and actually scanning available busses. These devices are basically anything that sits on a Plug-and-Play bus, like USB, PCI or SATA

We had a rather long discussion (more than a year ago, apologies if i forgot/mixed something up) about the intended semantics of the parent/bus calls, but the one we chose actually needs platform_data for dynamically created devices.
One option for calling bus drivers had a call(str device bus, str device child, ...) template, where the bus would be responsible for mapping children to actual bus addresses, using some sort of structure in its private data.

The option we went for is the one with call(str device bus, whatever child_addr, ...) template. the child_addr type of course depends on the actual bus in question, and the actual value was stored in the platform_data of the child when it was found/bound.

Quoted 6 lines

The reason why we chose the second approach is because the first one doesn't handle situations where you have devices in the tree between the bus controller and the real child, but on the bus protocol those devices operate transparently (USB hubs for instance). In that case, the controller would receive a call from either a child it doesn't know, or from the hub, but then it would resolve a wrong bus address.

Quoted 7 lines

OK, well in this case platform_data is no different for static and dynamic devices. In many cases, dynamic devices will not have platform_data, or a device tree node. But there should be nothing stopping that from being possible.

So yes I think providing platform data / FDT node to dynamic devices is fine.

Of course it would be possible to provide some dynamic devices with FDT nodes (like integrated devices that appear to be on PCI), but we should be able to handle the case where there is no such node, and therefore need a way to provide bus address information (which is basically what platform_data is meant for) to such a device.

maybe there is some misunderstanding caused by my lack of knowledge of what you can actually do with FDT, especially if you can add nodes in runtime. this could then effectively replace platform_data (except you would need a way to differentiate between these nodes in case there are multiple same devices present on the same bus)

Quoted 41 lines
Quoted 32 lines
Quoted 25 lines
Quoted 5 lines

driver_activate() should not be called by the user (or bootup routine) at
any point, instead it should be called by the classes when/before proxying the call to the driver specified implementation. We had it in our bootup code only to show that it is possible to do selectively.

Yes I need to tidy that up.

Quoted 8 lines

having a list_head for uclass membership in the struct device is not really a good idea, for two reasons. the biggest one is that some of the
classes will need to keep some information about the devices, that is not
usable outside the class (id/name for a disk, mapping to global linear numbering for GPIO...). This kind of information is used in queries "give

Quoted 8 lines

me a device with specific properties", instead of the generic "give me Nth device of this class", The other problem is that it disallows the device to support multiple classes (this could probably be worked around
by having virtual child devices for this case) - an exapmle that would use this would be a RAM disk, which you would want to still behave as a normal disk, but be tracked by a separate class (so you can resize/delete/whatever it),

Quoted 6 lines

To take your second point first, I think that devices which implement multiple uclasses should be considered multi-function-devices (MFD). These then to sit in a uclass of their own. Then, they can allocate separate individual devices for each uclass. This seems cleaner than dealing with a device in multiple uclasses. It also mirrors how this is done in Linux.

Quoted 8 lines

I guess that is the smarter way of doing it, since there will not be many of such devices. The issue I see would be the interface betweeen the MFD and its children - you either could have a very generic interface for all of them, or have the children play dirty with the parent pointer (getting the ops structure directly and casting it as necessary, which was not really possible with our design, where all calls had to go through the class), or something in the middle (maybe having set interfaces for specific set of functions? that could get very ugly very quick)

Quoted 5 lines

I think for now we need something device-specific. Each child will have knowledge of the parent driver, which may export some operations which the children can use. We can still use the driver model abstraction, but it doesn't need to be handled specially by driver model I think.

I had a thought (possibly very stupid) on how to handle this case in a somewhat clean manner:

The MFD interface would contain only a single function, lets call it "void *get_subops_for_class(struct driver *mfd, enum uclass_id)". this would return the ops structure for the specific class (or NULL in case it is not present in this device), which the child would use to call the interface implementation directly (not via the class proxy).
This would mean that there would only be a single virtual MFD child driver for each class (avoiding the driver explosion cause by each MFD driver implementing its own children), and that it would be quite simple (very similar to the proxying code of the class itself).
The MFD driver would then be responsible for creating the correct children, and providing them with specific interface implementations.

Quoted 25 lines
Quoted 4 lines
Quoted 3 lines

Re the uclass information, that is true. We need a way for the uclass code to store information about each device in a uclass. I will have a think about how to do that, perhaps another private data pointer?

Quoted 12 lines

the original design for uclass structure had a strongly-typed (as in not depening on the actual class like now) list of devices in it, and had to store a separate list in the private data (which contained the ops, and possibly other data).
after debating proper pairing (structures with the same index being considered paired was considered not robust enough) of those structures we abondoned this idea and merged the two lists into one, making it opque to the outside of the class code.
Since you do not need the ops stored for each device, you would end up with
most classes not keeping anything, and thus private data for any that do would probably be a way to go.

'
OK.

Quoted 5 lines

The only thing left is figuring out if you want to store a full mapping (having both the device pointer and its additional info in private data), or just keep the additional info and pair by list indicies (which basically works, but requires some work to code properly, and is harder to debug if things break)

Quoted 4 lines

I am thinking putting a private data pointer into struct device, i.e. making that structure responsible for it. Of course this wastes a pointer if most uclasses don't need to store anything about each of their devices.

I would be more concerned about the "here's a pointer, keep it, but don't look" approach. I understand that being fool-proof is not exactly a priority, but still (yes, if it were hidden somewhere you could still access it, but it would look obviously wrong in the code).
Keeping an extra pointer (something like class_private) should not be such an issue, considering you would have 200% increase in size if you went for the separate linked list (300% if it were doubly linked), especially if your malloc would be doubleword (as in 2 pointer sizes) aligned, and your structure would end up the same size anyways.

Quoted 80 lines
Quoted 21 lines
Quoted 17 lines
Quoted 7 lines

I like how you cache the driver structure for each device, instead of looking it up every time. I cannot recall why we didnt use this approach,
but i would point out that the name is present/used so we can have runtime loadable drivers sometime in the future (so the symbol would not
resolve at link time).

OK, there are a few things like that which could do with work.

Quoted 6 lines

I see that you are not building on the latest version from DM branch. there
were a few changes in the DM mechanism there (the one I see now is the USB
driver match function, which is useless as defined before we actually implemented USB).

Quoted 3 lines

OK - I did compare the code against the final commit in that branch. There certainly were changes, but not many to the core code, so I decided it did not matter for now.

Quoted 5 lines

Yes, most of the work was porting drivers, so we could show that all of the
board works. the changes in core code was only done when we found some overlooked design flaws (I think there were some minor changes in class relocation, and the USB match, can't remember anything else)

OK great.

Quoted 35 lines
Quoted 15 lines
Quoted 6 lines

One more thing that is not possible in current DM - sometimes, we would like to call an operation on ourselves as a part of .probe(), which would
create an infinite loop in with driver_activate(). an example of this is
when probing a bus controller, you probably want to scan the bus.

What sort of operation do you mean here? I would probably hold that when a driver is being probed, it can't use driver model calls to itself from within its probe function. If it needs to, it can call its own functions directly from probe. For example, if it wants to read from the device, ti can call its local read() function instead of trying to call the driver model 'read' handler for itself. The effect should be the same but it will be much less confusing.

Quoted 1 line

But perhaps I have missed something here.

The idea is that if your device exports only a very simple I/O, and more complex operations (like scanning for and initializing new child devices on a bus) are actually wrapped in the uclass code. this would mean that calling the complex code would call the proxy, resulting in a call to driver_activate() and an endless recursion.

This could probably be solved by having init_started and init_finished flags, to break the recursion (check for init_started, set init_started, call .probe(), set init_finished). This should work as long as the drivers are decent (using their class-provided functions only at the very end of .probe()), and some sanity checking (like what to do when removing a device that has init_started but not init_finished).

I am not sure where I encountered this issue at this point, so feel free to
drop it until it comes up (or it doesn't, maybe it was caused by a bad prior design decision on my part).

Quoted 4 lines

Come to think about it, it would also be handy to do this from .bind() hook, but that should work fine at the moment (the use for this would be in USB storage, which would like to scan for partitions when found on USB, without any special handling in the USB code)

OK, let's leave this for now and see if it comes up again in practice. I'm not quite sure the best way to solve it, other than what I suggested.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Thats it for now, I will spend some time over your code in near future, to
provide more comments

Thanks again for your comments and insight. I am hoping to find the time to respin this again soon.

Quoted 6 lines
Quoted 3 lines

Apologies if this doesnt make sense at some point, the answers were not done in linear order, and thus may contain contradictions or forward references or be just plain wrong

It makes sense to me, thanks for your comments. I will start looking at a new patch.

Thanks
Pavel