Pins are the unit of most DPLL netlink interaction, so let a device instance carry a set of up to 128 of them. Each directory created under the pins/ group of an instance is one pin with type, frequency and direction attributes. The type takes gnss, int-oscillator, ext, synce-eth-port or mux, the names iproute2 prints for these pin types, the direction is input or output and the frequency must be one of the frequencies the driver advertises as supported for the type. The pin directory name becomes the board label of the pin, which gives userspace a stable handle to find a pin by. The attributes are writable only while the instance is not live and return EBUSY otherwise. At live=1 the pins are ordered as created and each pin records its index in that order, which is the pin index it is registered under. Their frequencies are validated against the per type tables, a MUX pin configured as output is rejected since a MUX pin stands for an input selector, and all pin handles are obtained up front, so that a failure leaves nothing half visible. The device is registered first and the pins after it, so that every pin registration event reaches userspace with a valid device, and a failure unwinds exactly what was registered in reverse order. While the instance is live the pin items are held with configfs_depend_item_unlocked(), which makes rmdir of a pin fail with EBUSY until the instance is taken down. configfs calls make_item and drop_item under the subsystem mutex but links and unlinks the pin directory outside of it, so a live transition racing with a pin mkdir or rmdir can find a pin it cannot hold and fails with ENOENT then. The pins register with the mandatory callbacks only, direction_get and state_on_dpll_get, plus frequency_get since the frequency is known from the configuration. The input and output ops tables are identical here and diverge when input pins gain their setters in the next patches. The state is always reported as disconnected for now, the settable pin features follow in separate patches. Signed-off-by: Petr Oros --- v6: - say in the comment on the pins array that it is valid from the pin setup of a live transition to the end of its teardown - say in the documentation that the EBUSY of a structural attribute is for a value it would otherwise accept - say in comments why the direction getter needs no lock and why dropping a device group needs no undeploy of the pin depends - drop the Reviewed-by and the Acked-by, the comments changed since they were given v5: - name the pin types the simulator takes in the changelog, int-nco is not among them - say in the documentation that the EINVAL of a failed live write does not name the pin - say in the comment on deploy why the lock is taken by hand - drop the initial reference of the pins group when the device is removed, with configfs_remove_default_groups() as fs/dlm does; the group is freed with the device and needs no release, as config_item_cleanup() calls one only for a type with item ops v4: - document the limit of 128 pins, that pins cannot be added to a live instance, the frequencies each pin type supports and the errors of the live write - correct the comments on what the subsystem mutex covers, a live write racing with a pin mkdir or rmdir fails with ENOENT v5: https://lore.kernel.org/all/20261125090522.3777494-4-poros@redhat.com/ v4: https://lore.kernel.org/all/20261112130359.840795-4-poros@redhat.com/ v3: https://lore.kernel.org/all/20261109093523.931201-4-poros@redhat.com/ --- Documentation/driver-api/dpllsim.rst | 61 +++++- drivers/dpll/dpllsim/core.c | 243 +++++++++++++++++++++- drivers/dpll/dpllsim/dpll.c | 288 ++++++++++++++++++++++++++- drivers/dpll/dpllsim/dpllsim.h | 34 ++++ 4 files changed, 609 insertions(+), 17 deletions(-) diff --git a/Documentation/driver-api/dpllsim.rst b/Documentation/driver-api/dpllsim.rst index fdb451ef2cbf92..97910165aa7abe 100644 --- a/Documentation/driver-api/dpllsim.rst +++ b/Documentation/driver-api/dpllsim.rst @@ -12,11 +12,14 @@ Configuration interface ======================= Devices are described in configfs under ``/sys/kernel/config/dpllsim/``. -A directory created there is a device instance. Attributes are written -while the instance is not live, then ``live`` is set to 1 to register -the device with the DPLL subsystem. Writing 0 unregisters it again. -Structural attributes return ``EBUSY`` from the start of a live -transition to the end of its take down. +A directory created there is a device instance, a directory created in +its ``pins/`` subdirectory is a pin. Attributes are written while the +instance is not live, then ``live`` is set to 1 to register the device +and its pins with the DPLL subsystem. Writing 0 unregisters everything +again. From the start of a live transition to the end of its take +down, a structural attribute returns ``EBUSY`` for a value it would +otherwise accept, and pins can neither be added nor removed. An +instance holds up to 128 pins, creating one more fails with ``ENOSPC``. Device directory ``/sys/kernel/config/dpllsim//``: @@ -31,17 +34,59 @@ Device directory ``/sys/kernel/config/dpllsim//``: ``live`` 1 registers the device, 0 unregisters it ======================= ===================================================== +Pin directory ``/sys/kernel/config/dpllsim//pins//``: + +============= =============================================================== +``type`` ``gnss``, ``int-oscillator``, ``ext``, ``synce-eth-port`` or + ``mux`` (default ``ext``) +``frequency`` Frequency in Hz, must be supported by the pin type + (default 0, which is rejected) +``direction`` ``input`` or ``output`` (default ``input``), a ``mux`` pin + must be an input +============= =============================================================== + +The pin directory name becomes the board label of the pin. Pins are +registered in the order the directories were created. + +The frequencies a pin type supports: + +================== ========================================================== +``gnss`` 1 Hz +``int-oscillator`` 25 MHz +``synce-eth-port`` 1.544 MHz, 2.048 MHz, 10 MHz and 25 MHz +``mux`` same as ``synce-eth-port`` +``ext`` 1 Hz, 10 MHz, 19.44 MHz, 25 MHz, 77.76 MHz, 125 MHz and + 156.25 MHz +================== ========================================================== + +The pin configuration is checked when 1 is written to ``live``. The +write fails with ``EINVAL`` when a pin has a frequency its type does not +support or when a ``mux`` pin is an output, and with ``ENOENT`` when a +pin directory is being created or removed at the same time. An +``EINVAL`` does not name the pin, the attributes of the pins show which +one it is. + Example ======= -Create a device and make it live:: +Create a device with a GNSS input, an external input and an external +output, then make it live:: cd /sys/kernel/config/dpllsim mkdir dev0 && cd dev0 echo 100 > clock_id + mkdir pins/gnss0 + echo gnss > pins/gnss0/type + echo 1 > pins/gnss0/frequency + mkdir pins/ext0 + echo 10000000 > pins/ext0/frequency + mkdir pins/out0 + echo 10000000 > pins/out0/frequency + echo output > pins/out0/direction echo 1 > live dpll device show + dpll pin show -To remove the device, write 0 to ``live`` and remove the device -directory. +To remove the device, write 0 to ``live``, remove the pin directories +and finally the device directory. diff --git a/drivers/dpll/dpllsim/core.c b/drivers/dpll/dpllsim/core.c index 779eea5f5b140c..d7162d34be1258 100644 --- a/drivers/dpll/dpllsim/core.c +++ b/drivers/dpll/dpllsim/core.c @@ -6,12 +6,14 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +#include #include #include #include #include #include #include +#include #include #include "dpllsim.h" @@ -23,6 +25,189 @@ static struct dpllsim_dev *to_dpllsim_dev(struct config_item *item) return container_of(to_config_group(item), struct dpllsim_dev, group); } +static struct dpllsim_pin *to_dpllsim_pin(struct config_item *item) +{ + return container_of(item, struct dpllsim_pin, item); +} + +static const struct { + const char *name; + enum dpll_pin_type type; +} dpllsim_pin_types[] = { + { "gnss", DPLL_PIN_TYPE_GNSS }, + { "int-oscillator", DPLL_PIN_TYPE_INT_OSCILLATOR }, + { "ext", DPLL_PIN_TYPE_EXT }, + { "synce-eth-port", DPLL_PIN_TYPE_SYNCE_ETH_PORT }, + { "mux", DPLL_PIN_TYPE_MUX }, +}; + +static ssize_t dpllsim_pin_type_show(struct config_item *item, char *page) +{ + struct dpllsim_pin *pin = to_dpllsim_pin(item); + int i; + + guard(mutex)(&pin->dev->lock); + for (i = 0; i < ARRAY_SIZE(dpllsim_pin_types); i++) + if (dpllsim_pin_types[i].type == pin->type) + return sysfs_emit(page, "%s\n", + dpllsim_pin_types[i].name); + return -EINVAL; +} + +static ssize_t dpllsim_pin_type_store(struct config_item *item, + const char *page, size_t count) +{ + struct dpllsim_pin *pin = to_dpllsim_pin(item); + int i; + + for (i = 0; i < ARRAY_SIZE(dpllsim_pin_types); i++) + if (sysfs_streq(page, dpllsim_pin_types[i].name)) + break; + if (i == ARRAY_SIZE(dpllsim_pin_types)) + return -EINVAL; + + guard(mutex)(&pin->dev->lock); + if (dpllsim_dev_frozen(pin->dev)) + return -EBUSY; + pin->type = dpllsim_pin_types[i].type; + return count; +} +CONFIGFS_ATTR(dpllsim_pin_, type); + +static ssize_t dpllsim_pin_direction_show(struct config_item *item, char *page) +{ + struct dpllsim_pin *pin = to_dpllsim_pin(item); + + guard(mutex)(&pin->dev->lock); + return sysfs_emit(page, "%s\n", + pin->direction == DPLL_PIN_DIRECTION_INPUT ? + "input" : "output"); +} + +static ssize_t dpllsim_pin_direction_store(struct config_item *item, + const char *page, size_t count) +{ + struct dpllsim_pin *pin = to_dpllsim_pin(item); + enum dpll_pin_direction direction; + + if (sysfs_streq(page, "input")) + direction = DPLL_PIN_DIRECTION_INPUT; + else if (sysfs_streq(page, "output")) + direction = DPLL_PIN_DIRECTION_OUTPUT; + else + return -EINVAL; + + guard(mutex)(&pin->dev->lock); + if (dpllsim_dev_frozen(pin->dev)) + return -EBUSY; + pin->direction = direction; + return count; +} +CONFIGFS_ATTR(dpllsim_pin_, direction); + +static ssize_t dpllsim_pin_frequency_show(struct config_item *item, char *page) +{ + struct dpllsim_pin *pin = to_dpllsim_pin(item); + + guard(mutex)(&pin->dev->lock); + return sysfs_emit(page, "%llu\n", pin->frequency); +} + +static ssize_t dpllsim_pin_frequency_store(struct config_item *item, + const char *page, size_t count) +{ + struct dpllsim_pin *pin = to_dpllsim_pin(item); + u64 val; + int err; + + err = kstrtou64(page, 10, &val); + if (err) + return err; + + guard(mutex)(&pin->dev->lock); + if (dpllsim_dev_frozen(pin->dev)) + return -EBUSY; + pin->frequency = val; + return count; +} +CONFIGFS_ATTR(dpllsim_pin_, frequency); + +static struct configfs_attribute *dpllsim_pin_attrs[] = { + &dpllsim_pin_attr_type, + &dpllsim_pin_attr_direction, + &dpllsim_pin_attr_frequency, + NULL, +}; + +static void dpllsim_pin_release(struct config_item *item) +{ + struct dpllsim_pin *pin = to_dpllsim_pin(item); + + kfree(pin); +} + +static const struct configfs_item_operations dpllsim_pin_item_ops = { + .release = dpllsim_pin_release, +}; + +static const struct config_item_type dpllsim_pin_type = { + .ct_item_ops = &dpllsim_pin_item_ops, + .ct_attrs = dpllsim_pin_attrs, + .ct_owner = THIS_MODULE, +}; + +static struct config_item *dpllsim_pins_make_item(struct config_group *group, + const char *name) +{ + struct dpllsim_dev *sim = container_of(group, struct dpllsim_dev, + pins_group); + struct dpllsim_pin *pin; + + lockdep_assert_held(&dpllsim_subsys.su_mutex); + + if (dpllsim_dev_frozen(sim)) + return ERR_PTR(-EBUSY); + if (list_count_nodes(&sim->pin_list) >= DPLLSIM_MAX_PINS) + return ERR_PTR(-ENOSPC); + + pin = kzalloc_obj(*pin); + if (!pin) + return ERR_PTR(-ENOMEM); + + config_item_init_type_name(&pin->item, name, &dpllsim_pin_type); + pin->dev = sim; + pin->type = DPLL_PIN_TYPE_EXT; + pin->direction = DPLL_PIN_DIRECTION_INPUT; + list_add_tail(&pin->list, &sim->pin_list); + + return &pin->item; +} + +/* + * make_item and drop_item run under the subsystem mutex like the live + * transitions, which keeps pin_list consistent. configfs links the + * directory of a new pin only after make_item returned and unlinks the + * one of a removed pin before it calls drop_item, both without the + * mutex. A live transition in between finds a pin on the list that + * configfs_depend_item_unlocked() does not know and fails with -ENOENT. + */ +static void dpllsim_pins_drop_item(struct config_group *group, + struct config_item *item) +{ + list_del(&to_dpllsim_pin(item)->list); + config_item_put(item); +} + +static const struct configfs_group_operations dpllsim_pins_group_ops = { + .make_item = dpllsim_pins_make_item, + .drop_item = dpllsim_pins_drop_item, +}; + +static const struct config_item_type dpllsim_pins_type = { + .ct_group_ops = &dpllsim_pins_group_ops, + .ct_owner = THIS_MODULE, +}; + /* The clock id is checked at live=1, so both share the subsystem mutex */ static ssize_t dpllsim_dev_clock_id_show(struct config_item *item, char *page) { @@ -82,9 +267,41 @@ static bool dpllsim_clock_id_in_use(struct dpllsim_dev *sim) return false; } +/* Pins of a live device cannot be removed */ +static void dpllsim_dev_undepend_pins(struct dpllsim_dev *sim, + struct dpllsim_pin *last) +{ + struct dpllsim_pin *pin; + + list_for_each_entry(pin, &sim->pin_list, list) { + if (pin == last) + break; + configfs_undepend_item_unlocked(&pin->item); + } +} + +static int dpllsim_dev_depend_pins(struct dpllsim_dev *sim) +{ + struct dpllsim_pin *pin; + int err; + + list_for_each_entry(pin, &sim->pin_list, list) { + err = configfs_depend_item_unlocked(&dpllsim_subsys, + &pin->item); + if (err) { + dpllsim_dev_undepend_pins(sim, pin); + return err; + } + } + + return 0; +} + /* - * The subsystem mutex excludes the live transitions of the other - * instances for the whole deploy sequence. + * The subsystem mutex keeps the make_item and drop_item callbacks of the + * pins and the live transitions of the other instances out for the whole + * deploy sequence, see dpllsim_pins_drop_item() for a pin mkdir or rmdir + * that is in flight around its callback. */ static ssize_t dpllsim_dev_live_store(struct config_item *item, const char *page, size_t count) @@ -104,6 +321,7 @@ static ssize_t dpllsim_dev_live_store(struct config_item *item, if (!live) { dpllsim_dev_undeploy(sim); + dpllsim_dev_undepend_pins(sim, NULL); return count; } @@ -112,10 +330,16 @@ static ssize_t dpllsim_dev_live_store(struct config_item *item, if (dpllsim_clock_id_in_use(sim)) return -EEXIST; - err = dpllsim_dev_deploy(sim); + err = dpllsim_dev_depend_pins(sim); if (err) return err; + err = dpllsim_dev_deploy(sim); + if (err) { + dpllsim_dev_undepend_pins(sim, NULL); + return err; + } + return count; } CONFIGFS_ATTR(dpllsim_dev_, live); @@ -154,17 +378,26 @@ static struct config_group *dpllsim_make_group(struct config_group *group, return ERR_PTR(-ENOMEM); mutex_init(&sim->lock); + INIT_LIST_HEAD(&sim->pin_list); sim->mode = DPLL_MODE_AUTOMATIC; sim->lock_status = DPLL_LOCK_STATUS_UNLOCKED; /* uAPI value for no error, reported as lock-status-error none */ sim->lock_status_error = DPLL_LOCK_STATUS_ERROR_NONE; config_group_init_type_name(&sim->group, name, &dpllsim_dev_type); + config_group_init_type_name(&sim->pins_group, "pins", + &dpllsim_pins_type); + configfs_add_default_group(&sim->pins_group, &sim->group); return &sim->group; } -/* Runs under the subsystem mutex, unlike release */ +/* Runs under the subsystem mutex, unlike release. A device with a + * pin left cannot be removed, configfs refuses the rmdir of a group + * that still holds an item, so a device that reaches this drop has no + * pin and no configfs depend of a pin to release. A live device is + * undeployed first. + */ static void dpllsim_drop_item(struct config_group *group, struct config_item *item) { @@ -172,6 +405,8 @@ static void dpllsim_drop_item(struct config_group *group, if (dpllsim_dev_live(sim)) dpllsim_dev_undeploy(sim); + /* Drops the initial reference of the pins group, embedded in sim */ + configfs_remove_default_groups(&sim->group); config_item_put(item); } diff --git a/drivers/dpll/dpllsim/dpll.c b/drivers/dpll/dpllsim/dpll.c index c127269fe9a232..8786293dd6d88d 100644 --- a/drivers/dpll/dpllsim/dpll.c +++ b/drivers/dpll/dpllsim/dpll.c @@ -4,12 +4,39 @@ * Copyright (c) 2026 Red Hat, Inc. */ +#include #include #include #include +#include #include "dpllsim.h" +static struct dpll_pin_frequency dpllsim_freq_1pps[] = { + DPLL_PIN_FREQUENCY_RANGE(1, 1), +}; + +static struct dpll_pin_frequency dpllsim_freq_synce[] = { + DPLL_PIN_FREQUENCY_RANGE(1544000, 1544000), + DPLL_PIN_FREQUENCY_RANGE(2048000, 2048000), + DPLL_PIN_FREQUENCY_RANGE(10000000, 10000000), + DPLL_PIN_FREQUENCY_RANGE(25000000, 25000000), +}; + +static struct dpll_pin_frequency dpllsim_freq_ext[] = { + DPLL_PIN_FREQUENCY_RANGE(1, 1), + DPLL_PIN_FREQUENCY_RANGE(10000000, 10000000), + DPLL_PIN_FREQUENCY_RANGE(19440000, 19440000), + DPLL_PIN_FREQUENCY_RANGE(25000000, 25000000), + DPLL_PIN_FREQUENCY_RANGE(77760000, 77760000), + DPLL_PIN_FREQUENCY_RANGE(125000000, 125000000), + DPLL_PIN_FREQUENCY_RANGE(156250000, 156250000), +}; + +static struct dpll_pin_frequency dpllsim_freq_int_osc[] = { + DPLL_PIN_FREQUENCY_RANGE(25000000, 25000000), +}; + static int dpllsim_mode_get(const struct dpll_device *dpll, void *priv, enum dpll_mode *mode, struct netlink_ext_ack *extack) @@ -39,10 +66,235 @@ static const struct dpll_device_ops dpllsim_dpll_ops = { .lock_status_get = dpllsim_lock_status_get, }; +static int dpllsim_pin_frequency_get(const struct dpll_pin *pin, + void *pin_priv, + const struct dpll_device *dpll, + void *dpll_priv, u64 *frequency, + struct netlink_ext_ack *extack) +{ + struct dpllsim_pin *sim_pin = pin_priv; + + guard(mutex)(&sim_pin->dev->lock); + *frequency = sim_pin->frequency; + return 0; +} + +/* The direction is configuration, written by the configfs store only, + * which is refused while the device is frozen. The callbacks run while + * the pin is registered, which is inside the frozen window, so no + * store can race this read. + */ +static int dpllsim_pin_direction_get(const struct dpll_pin *pin, + void *pin_priv, + const struct dpll_device *dpll, + void *dpll_priv, + enum dpll_pin_direction *direction, + struct netlink_ext_ack *extack) +{ + struct dpllsim_pin *sim_pin = pin_priv; + + *direction = sim_pin->direction; + return 0; +} + +static int dpllsim_pin_state_get(const struct dpll_pin *pin, void *pin_priv, + const struct dpll_device *dpll, + void *dpll_priv, enum dpll_pin_state *state, + struct netlink_ext_ack *extack) +{ + struct dpllsim_pin *sim_pin = pin_priv; + + guard(mutex)(&sim_pin->dev->lock); + *state = sim_pin->state; + return 0; +} + +static const struct dpll_pin_ops dpllsim_input_pin_ops = { + .frequency_get = dpllsim_pin_frequency_get, + .direction_get = dpllsim_pin_direction_get, + .state_on_dpll_get = dpllsim_pin_state_get, +}; + +static const struct dpll_pin_ops dpllsim_output_pin_ops = { + .frequency_get = dpllsim_pin_frequency_get, + .direction_get = dpllsim_pin_direction_get, + .state_on_dpll_get = dpllsim_pin_state_get, +}; + +static const struct dpll_pin_ops * +dpllsim_pin_select_ops(const struct dpllsim_pin *pin) +{ + if (pin->direction == DPLL_PIN_DIRECTION_INPUT) + return &dpllsim_input_pin_ops; + return &dpllsim_output_pin_ops; +} + +static void dpllsim_pin_props(struct dpllsim_pin *pin, + struct dpll_pin_properties *prop) +{ + prop->type = pin->type; + prop->board_label = config_item_name(&pin->item); + + switch (pin->type) { + case DPLL_PIN_TYPE_GNSS: + prop->freq_supported = dpllsim_freq_1pps; + prop->freq_supported_num = ARRAY_SIZE(dpllsim_freq_1pps); + break; + case DPLL_PIN_TYPE_INT_OSCILLATOR: + prop->freq_supported = dpllsim_freq_int_osc; + prop->freq_supported_num = ARRAY_SIZE(dpllsim_freq_int_osc); + break; + case DPLL_PIN_TYPE_MUX: + case DPLL_PIN_TYPE_SYNCE_ETH_PORT: + prop->freq_supported = dpllsim_freq_synce; + prop->freq_supported_num = ARRAY_SIZE(dpllsim_freq_synce); + break; + default: + prop->freq_supported = dpllsim_freq_ext; + prop->freq_supported_num = ARRAY_SIZE(dpllsim_freq_ext); + break; + } +} + +static bool dpllsim_pin_frequency_valid(struct dpllsim_pin *pin) +{ + struct dpll_pin_properties prop = {}; + int i; + + dpllsim_pin_props(pin, &prop); + for (i = 0; i < prop.freq_supported_num; i++) + if (pin->frequency >= prop.freq_supported[i].min && + pin->frequency <= prop.freq_supported[i].max) + return true; + + return false; +} + +/* A MUX pin stands for an input selector, so it is an input itself */ +static bool dpllsim_pin_direction_valid(const struct dpllsim_pin *pin) +{ + return pin->type != DPLL_PIN_TYPE_MUX || + pin->direction == DPLL_PIN_DIRECTION_INPUT; +} + +static void dpllsim_pin_reset(struct dpllsim_pin *pin) +{ + pin->ops = dpllsim_pin_select_ops(pin); + pin->state = DPLL_PIN_STATE_DISCONNECTED; +} + +/* Build sim->pins in registration order, the order the pins were created */ +static int dpllsim_order_pins(struct dpllsim_dev *sim) +{ + struct dpllsim_pin *pin; + int count = 0, i; + + list_for_each_entry(pin, &sim->pin_list, list) { + if (!dpllsim_pin_frequency_valid(pin) || + !dpllsim_pin_direction_valid(pin)) + return -EINVAL; + count++; + } + + sim->pins = kzalloc_objs(*sim->pins, count); + if (!sim->pins) + return -ENOMEM; + + sim->num_pins = 0; + list_for_each_entry(pin, &sim->pin_list, list) { + pin->idx = sim->num_pins; + sim->pins[sim->num_pins++] = pin; + } + + for (i = 0; i < sim->num_pins; i++) + dpllsim_pin_reset(sim->pins[i]); + + return 0; +} + +static void dpllsim_put_pins(struct dpllsim_dev *sim) +{ + int i; + + for (i = sim->num_pins - 1; i >= 0; i--) { + struct dpllsim_pin *pin = sim->pins[i]; + + if (pin->dpll_pin) { + dpll_pin_put(pin->dpll_pin, &pin->tracker); + pin->dpll_pin = NULL; + } + } +} + +static int dpllsim_get_pins(struct dpllsim_dev *sim) +{ + int i, err; + + for (i = 0; i < sim->num_pins; i++) { + struct dpllsim_pin *pin = sim->pins[i]; + struct dpll_pin_properties prop = {}; + struct dpll_pin *dpll_pin; + + dpllsim_pin_props(pin, &prop); + dpll_pin = dpll_pin_get(sim->clock_id, pin->idx, THIS_MODULE, + &prop, &pin->tracker); + if (IS_ERR(dpll_pin)) { + err = PTR_ERR(dpll_pin); + goto err_put_pins; + } + pin->dpll_pin = dpll_pin; + } + + return 0; + +err_put_pins: + dpllsim_put_pins(sim); + return err; +} + +static void dpllsim_unregister_pins(struct dpllsim_dev *sim) +{ + int i; + + for (i = sim->num_pins - 1; i >= 0; i--) { + struct dpllsim_pin *pin = sim->pins[i]; + + if (pin->registered) + dpll_pin_unregister(sim->dpll, pin->dpll_pin, pin->ops, + pin); + pin->registered = false; + } +} + +static int dpllsim_register_pins(struct dpllsim_dev *sim) +{ + struct dpllsim_pin *pin; + int i, err; + + for (i = 0; i < sim->num_pins; i++) { + pin = sim->pins[i]; + + err = dpll_pin_register(sim->dpll, pin->dpll_pin, pin->ops, + pin); + if (err) + goto err_unregister; + pin->registered = true; + } + + return 0; + +err_unregister: + dpllsim_unregister_pins(sim); + return err; +} + /* - * The unwind of a failed deploy uses goto, which include/linux/cleanup.h - * says not to mix with the scoped lock helpers, so the lock is taken by - * hand here. + * Registration order matters: the device must be registered before pins + * so that pin registration events reach userspace with a valid device, + * while pin handles must be obtained up front so that a failure leaves + * nothing half-visible. The unwind uses goto, which + * include/linux/cleanup.h says not to mix with the scoped lock helpers, + * so the lock is taken by hand here. */ int dpllsim_dev_deploy(struct dpllsim_dev *sim) { @@ -53,15 +305,27 @@ int dpllsim_dev_deploy(struct dpllsim_dev *sim) if (IS_ERR(dpll)) return PTR_ERR(dpll); - /* From here on the configuration is frozen */ + /* From here on the pin configuration is frozen */ mutex_lock(&sim->lock); sim->dpll = dpll; mutex_unlock(&sim->lock); + err = dpllsim_order_pins(sim); + if (err) + goto err_put_dpll; + + err = dpllsim_get_pins(sim); + if (err) + goto err_free_pins; + err = dpll_device_register(sim->dpll, DPLL_TYPE_PPS, &dpllsim_dpll_ops, sim); if (err) - goto err_put_dpll; + goto err_put_pins; + + err = dpllsim_register_pins(sim); + if (err) + goto err_unregister_device; mutex_lock(&sim->lock); sim->registered = true; @@ -69,6 +333,14 @@ int dpllsim_dev_deploy(struct dpllsim_dev *sim) return 0; +err_unregister_device: + dpll_device_unregister(sim->dpll, &dpllsim_dpll_ops, sim); +err_put_pins: + dpllsim_put_pins(sim); +err_free_pins: + kfree(sim->pins); + sim->pins = NULL; + sim->num_pins = 0; err_put_dpll: dpll_device_put(sim->dpll, &sim->tracker); mutex_lock(&sim->lock); @@ -82,8 +354,14 @@ void dpllsim_dev_undeploy(struct dpllsim_dev *sim) scoped_guard(mutex, &sim->lock) sim->registered = false; + dpllsim_unregister_pins(sim); dpll_device_unregister(sim->dpll, &dpllsim_dpll_ops, sim); + dpllsim_put_pins(sim); dpll_device_put(sim->dpll, &sim->tracker); scoped_guard(mutex, &sim->lock) sim->dpll = NULL; + + kfree(sim->pins); + sim->pins = NULL; + sim->num_pins = 0; } diff --git a/drivers/dpll/dpllsim/dpllsim.h b/drivers/dpll/dpllsim/dpllsim.h index 7b866f5fe929f5..7f766138974cbb 100644 --- a/drivers/dpll/dpllsim/dpllsim.h +++ b/drivers/dpll/dpllsim/dpllsim.h @@ -9,10 +9,39 @@ #include #include +#include #include +#define DPLLSIM_MAX_PINS 128 + +struct dpllsim_dev; + +struct dpllsim_pin { + struct config_item item; + struct list_head list; + struct dpllsim_dev *dev; + + /* Configuration, writable only until dpllsim_dev_frozen() */ + enum dpll_pin_type type; + enum dpll_pin_direction direction; + u64 frequency; + + /* Resolved at live=1 */ + int idx; + struct dpll_pin *dpll_pin; + const struct dpll_pin_ops *ops; + dpll_tracker tracker; + bool registered; + + /* Simulated state, guarded by dev->lock */ + enum dpll_pin_state state; +}; + struct dpllsim_dev { struct config_group group; + struct config_group pins_group; + /* Pin items, guarded by the configfs subsystem mutex */ + struct list_head pin_list; /* * Set from the start of a live transition until the device is @@ -29,6 +58,11 @@ struct dpllsim_dev { * dpllsim_dev_live(). */ bool registered; + /* Pins in registration order, valid from the pin setup of a live + * transition to the end of its teardown + */ + struct dpllsim_pin **pins; + int num_pins; /* Guarded by the subsystem mutex */ u64 clock_id; -- 2.56.1