Re: [PATCH v4 3/6] i3c: Add support for mastership request to I3C subsystem

Boris Brezillon
2019-04-01T19:18:50Z

On Mon, 1 Apr 2019 19:41:39 +0100
vitor <vitor.soares@synopsys.com> wrote:

> > void i3c_device_free_ibi(struct i3c_device *dev)
> > {
> > + int ret;
> > +
> > i3c_bus_normaluse_lock(dev->bus);
> > + ret = i3c_master_acquire_bus_ownership(dev->desc->common.master);
> > + if (ret)
> > + goto err_unlock_bus;
> > +
> > if (dev->desc) {
> > mutex_lock(&dev->desc->ibi_lock);
> > i3c_dev_free_ibi_locked(dev->desc);
> > mutex_unlock(&dev->desc->ibi_lock);
> > }
> > +
> > +err_unlock_bus:
> > i3c_bus_normaluse_unlock(dev->bus);
> > }
> > EXPORT_SYMBOL_GPL(i3c_device_free_ibi);
>
> Can't we verify if it is the current master no master.c side?

You mean acquiring the bus in i3c_dev_free_ibi_locked()? It doesn't
work if we want things to play well with lockdep (which tracks locking
order to make sure deadlocks can't happen). To acquire the bus you need
to take the bus lock in write/maintenance mode and downgrade it to a
read/normaluse lock once you're done. If you release/acquire the lock
inside i3c_dev_free_ibi_locked() that means some path will have

LOCK(bus);
LOCK(ibi);
UNLOCK(bus);
LOCK(bus);
UNLOCK(ibi);
UNLOCK(bus);

and others

LOCK(bus);
LOCK(ibi);
UNLOCK(ibi);
UNLOCK(bus);

BTW, it won't make a difference in term of LoC to move that to logic
master.c, and the i3c_master_acquire_bus_ownership() helper already
hides most of the complexity.

>
> > diff --git a/drivers/i3c/internals.h b/drivers/i3c/internals.h
> > index 86b7b44..929ca6b 100644
> > --- a/drivers/i3c/internals.h
> > +++ b/drivers/i3c/internals.h
> > @@ -14,6 +14,10 @@ extern struct bus_type i3c_bus_type;
> >
> > void i3c_bus_normaluse_lock(struct i3c_bus *bus);
> > void i3c_bus_normaluse_unlock(struct i3c_bus *bus);
> > +void i3c_bus_downgrade_maintenance_lock(struct i3c_bus *bus);
> > +int i3c_master_acquire_bus_ownership(struct i3c_master_controller *master);
> > +int i3c_master_request_mastership_locked(struct i3c_master_controller *master);
> > +
> >
> > int i3c_dev_do_priv_xfers_locked(struct i3c_dev_desc *dev,
> > struct i3c_priv_xfer *xfers,
> > diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
> > index aea4309..7a84158 100644
> > --- a/drivers/i3c/master.c
> > +++ b/drivers/i3c/master.c
> > @@ -93,6 +93,20 @@ void i3c_bus_normaluse_unlock(struct i3c_bus *bus)
> > up_read(&bus->lock);
> > }
> >
> > +/**
> > + * i3c_bus_downgrade_maintenance_lock - Downgrade the bus lock to normal
> > + * operation
> > + * @bus: I3C bus to downgrade the lock on
> > + *
> > + * Should be called when a maintenance operation is done and normal
> > + * operation is planned. See i3c_bus_maintenance_lock() and
> > + * i3c_bus_normaluse_lock() for more details.
> > + */
> > +void i3c_bus_downgrade_maintenance_lock(struct i3c_bus *bus)
> > +{
> > + downgrade_write(&bus->lock);
> > +}
> > +
> > static struct i3c_master_controller *dev_to_i3cmaster(struct device *dev)
> > {
> > return container_of(dev, struct i3c_master_controller, dev);
> > @@ -341,6 +355,22 @@ static int i3c_device_probe(struct device *dev)
> > return driver->probe(i3cdev);
> > }
> >
> > +static void i3c_master_enable_mr_events(struct i3c_master_controller *master)
> > +{
> > + if (!master->ops->enable_mr_events)
> > + return;
> > +
> > + master->ops->enable_mr_events(master);
> > +}
> > +
> > +static void i3c_master_disable_mr_events(struct i3c_master_controller *master)
> > +{
> > + if (!master->ops->disable_mr_events)
> > + return;
> > +
> > + master->ops->disable_mr_events(master);
> > +}
> > +
>
> do we want to do this in broadcast? Is it save to give mastership capabilities to all devices?

Good question. Sounds like a policy decision that we should leave to the
user (kernel parameter, sysfs iface?). Regarding the
i3c_master_controller_ops iface, if we want to allow MR on a per-master
basis, that's something we could store in the i3c_dev_desc struct and
let masters iterate over this list to decide who they should send
ENEC() events to. We probably also want a default policy like reject or
accept all.

Anyway, those are things we can work on once this patch series has
landed.

> > /**
> > + * i3c_master_register_new_devs() - register new devices
> > + * @master: master used to send frames on the bus
> > + *
> > + * This function is useful when devices were not added
> > + * during initialization or when new device joined the bus
> > + * which was under control of different master.
> > + */
> > +void i3c_master_register_new_devs(struct i3c_master_controller *master)
> > +{
> > + /*
> > + * We can register I3C devices received from master by DEFSLVS.
> > + */
> > + i3c_bus_normaluse_lock(&master->bus);
> > + i3c_master_register_new_i3c_devs(master);
> > + i3c_bus_normaluse_unlock(&master->bus);
> > +
> > + i3c_bus_normaluse_lock(&master->bus);
> > + i3c_master_register_new_i2c_devs(master);
> > + i3c_bus_normaluse_unlock(&master->bus);
> > +}
> > +EXPORT_SYMBOL_GPL(i3c_master_register_new_devs);
> > +
>
>  I would say a function to add DEVSLVS. From the master driver you can pack all
>  received slaves into i3c_ccc_defslvs struct and rely the task of add those
>  devices to the subsystem.

Unfortunately, controllers might have dedicated logic parsing the
DEFSLV frame and populating the device table (AFAIR Cadence controller
is working this way). Your solution would imply reconstructing a
DEFSLVS frame which I don't think is worth it. If we have several
controllers exposing directly the DEFSLVS frames, then we can add an
helper to do what you suggest though.