diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2020-03-06 14:53:04 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2020-08-13 10:46:43 +0200 |
commit | 94b264aca5685aeba5fb6babc097d64b78478a11 (patch) | |
tree | 2bdf35e281171806dbfa188ce22948c5402ae285 /include | |
parent | f9f67c7827c90d9f54d392673e459679f7ce3bd6 (diff) | |
download | barebox-94b264aca5685aeba5fb6babc097d64b78478a11.tar.gz barebox-94b264aca5685aeba5fb6babc097d64b78478a11.tar.xz |
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 <s.hauer@pengutronix.de>
Diffstat (limited to 'include')
-rw-r--r-- | include/slice.h | 31 |
1 files changed, 31 insertions, 0 deletions
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 */ |