From 94b264aca5685aeba5fb6babc097d64b78478a11 Mon Sep 17 00:00:00 2001 From: Sascha Hauer Date: Fri, 6 Mar 2020 14:53:04 +0100 Subject: Introduce slices slices, the barebox idea of locking barebox has pollers which execute code in the background whenever one of the delay functions (udelay, mdelay, ...) or is_timeout() are called. This introduces resource problems when some device triggers a poller by calling a delay function and then the poller code calls into the same device again. As an example consider a I2C GPIO expander which drives a LED which shall be used as a heartbeat LED: poller -> LED on/off -> GPIO high/low -> I2C transfer The I2C controller has a timeout loop using is_timeout() and thus can trigger a poller run. With this the following can happen during an unrelated I2C transfer: I2C transfer -> is_timeout() -> poller -> LED on/off -> GPIO high/low -> I2C transfer We end up with issuing an I2C transfer during another I2C transfer and things go downhill. Due to the lack of interrupts we can't do real locking in barebox. We use a mechanism called slices instead. A slice describes a resource to which other slices can be attached. Whenever a slice is needed it must be acquired. Acquiring a slice never fails, it just increases the acquired counter of the slice and its dependent slices. when a slice shall be used inside a poller it must first be tested if the slice is already in use. If it is, we can't do the operation on the slice now and must return and hope that we have more luck in the next poller call. slices can be attached other slices as dependencies. In the example above LED driver would add a dependency to the GPIO controller and the GPIO driver would add a dependency to the I2C bus: GPIO driver probe: slice_add(&gpio->slice, i2c_device_slice(i2cdev)); LED driver probe: slice_add(&led->slice, gpio_slice(gpio)); The GPIO code would call slice_acquire(&gpio->slice) before doing any operation on the GPIO chip providing this GPIO, likewise the I2C core would call slice_acquire(&i2cbus->slice) before doing an operation on this I2C bus. The heartbeat poller code would call slice_acquired(led_slice(led)) and only continue when the slice is not acquired. Signed-off-by: Sascha Hauer --- include/slice.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 include/slice.h (limited to 'include') diff --git a/include/slice.h b/include/slice.h new file mode 100644 index 0000000000..5538fc434a --- /dev/null +++ b/include/slice.h @@ -0,0 +1,31 @@ +#ifndef __SLICE_H +#define __SLICE_H + +enum slice_action { + SLICE_ACQUIRE = 1, + SLICE_RELEASE = -1, + SLICE_TEST = 0, +}; + +struct slice { + int acquired; + struct list_head deps; + char *name; + struct list_head list; +}; + +struct slice_entry { + struct slice *slice; + struct list_head list; +}; + +void slice_acquire(struct slice *slice); +void slice_release(struct slice *slice); +bool slice_acquired(struct slice *slice); +void slice_depends_on(struct slice *slice, struct slice *dep); +void slice_init(struct slice *slice, const char *name); +void slice_exit(struct slice *slice); + +void slice_debug_acquired(struct slice *slice); + +#endif /* __SLICE_H */ -- cgit v1.2.3