From nobody Sun Nov 24 03:57:53 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2CD521B446; Fri, 8 Nov 2024 15:37:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080238; cv=none; b=hl3cKePVbYea9UwAEA1yl20qCGhyXO0VdHXciwp5QRkgQyklWAubBh4bW0mP5G/0HYmWWKucwdqNr0KDFGNgQtVglhknpRNYgZtvrVFI/OGnaax/B85x6Kt1eCBy8mWExxOCHBzWYIafOq3cttJ0bXAKsI+qbMJ+xawG5g1IGRk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080238; c=relaxed/simple; bh=kxeO51kOwYXUp0OU5P8vMOAk78cA1NW78/VhRsNmjZU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=feRBP3TrxE6F0oOCg6im3Wm+pJy8v/ryvMPQ4A7AsoDecj6HXw702UTEocb8MIp0wd8X7KLRYefSZ+5QQVcuh12bhjFdzwuvryhQqHyY/70T97m85lqaUydm87VQ3QwymNU2zX/xL2CRsRSDk89tTxwBV1hXoZgP0LKEgItaSkc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=VIIWIdHc; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="VIIWIdHc" Received: by mail.gandi.net (Postfix) with ESMTPSA id 49C3120011; Fri, 8 Nov 2024 15:37:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1731080233; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uO2oXC73Ucf+OKFHsX5fbRnrJrLCb2JkxdDG4pxP7Tk=; b=VIIWIdHc2Js3Bd0nuOxvJ5fVXFPvUg7YsxOG3K/6H/Cw+eYJki3MW5yAC+0BzZHFF7E+xc P6xTVUAP08a7qE075C9DDu+/P7MsIEWXzIrV9DOpdCgLiFwfQpkRThEXA/DHdj3T9B0F8i yeDkOwVD5Xpa4McBOvTscnyx/Tyt8/OpqltrCpYfOX5vvO0K8Rp5AL6Dmzxymk0jmq/JG4 WpAqunjgFx3gzLQ8EZ7QSMMfweLfpA3IEeMd+8gTQw0grdhSwt/6EpZ5uygkNsxByvrlkW yOKyqxrPaRBolQNQ8tBgui2FhiLHlm5Q8E3/20cm72VDimq0PQlaaXD6TOdffQ== From: Romain Gantois Date: Fri, 08 Nov 2024 16:36:57 +0100 Subject: [PATCH 5/9] i2c: move ATR alias pool to a separate struct Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241108-fpc202-v1-5-fe42c698bc92@bootlin.com> References: <20241108-fpc202-v1-0-fe42c698bc92@bootlin.com> In-Reply-To: <20241108-fpc202-v1-0-fe42c698bc92@bootlin.com> To: Wolfram Sang , Tomi Valkeinen , Luca Ceresoli , Andi Shyti , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Derek Kiernan , Dragan Cvetic , Arnd Bergmann , Greg Kroah-Hartman , Mauro Carvalho Chehab Cc: Thomas Petazzoni , Kory Maincent , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-media@vger.kernel.org, Romain Gantois X-Mailer: b4 0.14.2 X-GND-Sasl: romain.gantois@bootlin.com Each I2C address translator (ATR) has a pool of client aliases which can be used as translation targets. Some ATRs have a single alias pool shared by all downstream channels, while others have a separate alias pool for each channel. Currently, this alias pool is represented by the "aliases", "num_aliases", and "use_mask" fields of struct i2c_atr. In preparation for adding per-channel alias pool support, move the "aliases", "num_aliases", "use_mask" and associated lock to a new struct called "struct alias_pool". Signed-off-by: Romain Gantois --- drivers/i2c/i2c-atr.c | 172 +++++++++++++++++++++++++++++++---------------= ---- 1 file changed, 107 insertions(+), 65 deletions(-) diff --git a/drivers/i2c/i2c-atr.c b/drivers/i2c/i2c-atr.c index 894787246846b9965deb03a7ec7eb600b102ddad..c873fe52288175151040a4c32e6= ed07735586004 100644 --- a/drivers/i2c/i2c-atr.c +++ b/drivers/i2c/i2c-atr.c @@ -34,6 +34,23 @@ struct i2c_atr_alias_pair { u16 alias; }; =20 +/** + * struct i2c_atr_alias_pool - Pool of client aliases available for an ATR. + * @size: Total number of aliases + * + * @lock: Lock protecting @aliases and @use_mask + * @aliases: Array of aliases, must hold exactly @size elements + * @use_mask: Mask of used aliases + */ +struct i2c_atr_alias_pool { + size_t size; + + /* Protects aliases and use_mask */ + spinlock_t lock; + u16 *aliases; + unsigned long *use_mask; +}; + /** * struct i2c_atr_chan - Data for a channel. * @adap: The &struct i2c_adapter for the channel @@ -67,10 +84,7 @@ struct i2c_atr_chan { * @algo: The &struct i2c_algorithm for adapters * @lock: Lock for the I2C bus segment (see &struct i2c_lock_operatio= ns) * @max_adapters: Maximum number of adapters this I2C ATR can have - * @num_aliases: Number of aliases in the aliases array - * @aliases: The aliases array - * @alias_mask_lock: Lock protecting alias_use_mask - * @alias_use_mask: Bitmask for used aliases in aliases array + * @alias_pool: Pool of available client aliases * @i2c_nb: Notifier for remote client add & del events * @adapter: Array of adapters */ @@ -86,17 +100,54 @@ struct i2c_atr { struct mutex lock; int max_adapters; =20 - size_t num_aliases; - const u16 *aliases; - /* Protects alias_use_mask */ - spinlock_t alias_mask_lock; - unsigned long *alias_use_mask; + struct i2c_atr_alias_pool *alias_pool; =20 struct notifier_block i2c_nb; =20 struct i2c_adapter *adapter[] __counted_by(max_adapters); }; =20 +static struct i2c_atr_alias_pool *i2c_atr_alloc_alias_pool(size_t num_alia= ses) +{ + struct i2c_atr_alias_pool *alias_pool; + int ret; + + alias_pool =3D kzalloc(sizeof(*alias_pool), GFP_KERNEL); + if (!alias_pool) + return ERR_PTR(-ENOMEM); + + alias_pool->size =3D num_aliases; + + alias_pool->aliases =3D kcalloc(num_aliases, sizeof(*alias_pool->aliases)= , GFP_KERNEL); + if (!alias_pool->aliases) { + ret =3D -ENOMEM; + goto err_free_alias_pool; + } + + alias_pool->use_mask =3D bitmap_zalloc(num_aliases, GFP_KERNEL); + if (!alias_pool->use_mask) { + ret =3D -ENOMEM; + goto err_free_aliases; + } + + spin_lock_init(&alias_pool->lock); + + return alias_pool; + +err_free_aliases: + kfree(alias_pool->aliases); +err_free_alias_pool: + kfree(alias_pool); + return ERR_PTR(ret); +} + +static void i2c_atr_free_alias_pool(struct i2c_atr_alias_pool *alias_pool) +{ + bitmap_free(alias_pool->use_mask); + kfree(alias_pool->aliases); + kfree(alias_pool); +} + static struct i2c_atr_alias_pair * i2c_atr_find_mapping_by_addr(const struct list_head *list, u16 phys_addr) { @@ -259,44 +310,42 @@ static const struct i2c_lock_operations i2c_atr_lock_= ops =3D { .unlock_bus =3D i2c_atr_unlock_bus, }; =20 -static int i2c_atr_reserve_alias(struct i2c_atr *atr) +static int i2c_atr_reserve_alias(struct i2c_atr_alias_pool *alias_pool) { unsigned long idx; + u16 alias; =20 - spin_lock(&atr->alias_mask_lock); + spin_lock(&alias_pool->lock); =20 - idx =3D find_first_zero_bit(atr->alias_use_mask, atr->num_aliases); - if (idx >=3D atr->num_aliases) { - spin_unlock(&atr->alias_mask_lock); - dev_err(atr->dev, "failed to find a free alias\n"); + idx =3D find_first_zero_bit(alias_pool->use_mask, alias_pool->size); + if (idx >=3D alias_pool->size) { + spin_unlock(&alias_pool->lock); return -EBUSY; } =20 - set_bit(idx, atr->alias_use_mask); + set_bit(idx, alias_pool->use_mask); =20 - spin_unlock(&atr->alias_mask_lock); + alias =3D alias_pool->aliases[idx]; =20 - return atr->aliases[idx]; + spin_unlock(&alias_pool->lock); + return alias; } =20 -static void i2c_atr_release_alias(struct i2c_atr *atr, u16 alias) +static void i2c_atr_release_alias(struct i2c_atr_alias_pool *alias_pool, u= 16 alias) { unsigned int idx; =20 - spin_lock(&atr->alias_mask_lock); + spin_lock(&alias_pool->lock); =20 - for (idx =3D 0; idx < atr->num_aliases; ++idx) { - if (atr->aliases[idx] =3D=3D alias) { - clear_bit(idx, atr->alias_use_mask); - spin_unlock(&atr->alias_mask_lock); + for (idx =3D 0; idx < alias_pool->size; ++idx) { + if (alias_pool->aliases[idx] =3D=3D alias) { + clear_bit(idx, alias_pool->use_mask); + spin_unlock(&alias_pool->lock); return; } } =20 - spin_unlock(&atr->alias_mask_lock); - - /* This should never happen */ - dev_warn(atr->dev, "Unable to find mapped alias\n"); + spin_unlock(&alias_pool->lock); } =20 static int i2c_atr_attach_addr(struct i2c_adapter *adapter, @@ -308,9 +357,11 @@ static int i2c_atr_attach_addr(struct i2c_adapter *ada= pter, u16 alias; int ret; =20 - ret =3D i2c_atr_reserve_alias(atr); - if (ret < 0) + ret =3D i2c_atr_reserve_alias(atr->alias_pool); + if (ret < 0) { + dev_err(atr->dev, "failed to find a free alias\n"); return ret; + } =20 alias =3D ret; =20 @@ -336,7 +387,7 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adap= ter, err_free: kfree(c2a); err_release_alias: - i2c_atr_release_alias(atr, alias); + i2c_atr_release_alias(atr->alias_pool, alias); =20 return ret; } @@ -357,7 +408,7 @@ static void i2c_atr_detach_addr(struct i2c_adapter *ada= pter, return; } =20 - i2c_atr_release_alias(atr, c2a->alias); + i2c_atr_release_alias(atr->alias_pool, c2a->alias); =20 dev_dbg(atr->dev, "chan%u: addr 0x%02x unmapped from alias 0x%02x\n", @@ -411,12 +462,11 @@ static int i2c_atr_bus_notifier_call(struct notifier_= block *nb, =20 static int i2c_atr_parse_alias_pool(struct i2c_atr *atr) { + struct i2c_atr_alias_pool *alias_pool; struct device *dev =3D atr->dev; - unsigned long *alias_use_mask; size_t num_aliases; unsigned int i; u32 *aliases32; - u16 *aliases16; int ret; =20 ret =3D fwnode_property_count_u32(dev_fwnode(dev), "i2c-alias-pool"); @@ -428,12 +478,23 @@ static int i2c_atr_parse_alias_pool(struct i2c_atr *a= tr) =20 num_aliases =3D ret; =20 - if (!num_aliases) + alias_pool =3D i2c_atr_alloc_alias_pool(num_aliases); + if (IS_ERR(alias_pool)) { + ret =3D PTR_ERR(alias_pool); + dev_err(dev, "Failed to allocate alias pool, err %d\n", ret); + return ret; + } + + atr->alias_pool =3D alias_pool; + + if (!alias_pool->size) return 0; =20 aliases32 =3D kcalloc(num_aliases, sizeof(*aliases32), GFP_KERNEL); - if (!aliases32) - return -ENOMEM; + if (!aliases32) { + ret =3D -ENOMEM; + goto err_free_alias_pool; + } =20 ret =3D fwnode_property_read_u32_array(dev_fwnode(dev), "i2c-alias-pool", aliases32, num_aliases); @@ -443,43 +504,27 @@ static int i2c_atr_parse_alias_pool(struct i2c_atr *a= tr) goto err_free_aliases32; } =20 - aliases16 =3D kcalloc(num_aliases, sizeof(*aliases16), GFP_KERNEL); - if (!aliases16) { - ret =3D -ENOMEM; - goto err_free_aliases32; - } - for (i =3D 0; i < num_aliases; i++) { if (!(aliases32[i] & 0xffff0000)) { - aliases16[i] =3D aliases32[i]; + alias_pool->aliases[i] =3D aliases32[i]; continue; } =20 dev_err(dev, "Failed to parse 'i2c-alias-pool' property: I2C flags are n= ot supported\n"); ret =3D -EINVAL; - goto err_free_aliases16; - } - - alias_use_mask =3D bitmap_zalloc(num_aliases, GFP_KERNEL); - if (!alias_use_mask) { - ret =3D -ENOMEM; - goto err_free_aliases16; + goto err_free_aliases32; } =20 kfree(aliases32); =20 - atr->num_aliases =3D num_aliases; - atr->aliases =3D aliases16; - atr->alias_use_mask =3D alias_use_mask; - - dev_dbg(dev, "i2c-alias-pool has %zu aliases", atr->num_aliases); + dev_dbg(dev, "i2c-alias-pool has %zu aliases", alias_pool->size); =20 return 0; =20 -err_free_aliases16: - kfree(aliases16); err_free_aliases32: kfree(aliases32); +err_free_alias_pool: + i2c_atr_free_alias_pool(alias_pool); return ret; } =20 @@ -500,7 +545,6 @@ struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent,= struct device *dev, return ERR_PTR(-ENOMEM); =20 mutex_init(&atr->lock); - spin_lock_init(&atr->alias_mask_lock); =20 atr->parent =3D parent; atr->dev =3D dev; @@ -520,13 +564,12 @@ struct i2c_atr *i2c_atr_new(struct i2c_adapter *paren= t, struct device *dev, atr->i2c_nb.notifier_call =3D i2c_atr_bus_notifier_call; ret =3D bus_register_notifier(&i2c_bus_type, &atr->i2c_nb); if (ret) - goto err_free_aliases; + goto err_free_alias_pool; =20 return atr; =20 -err_free_aliases: - bitmap_free(atr->alias_use_mask); - kfree(atr->aliases); +err_free_alias_pool: + i2c_atr_free_alias_pool(atr->alias_pool); err_destroy_mutex: mutex_destroy(&atr->lock); kfree(atr); @@ -543,8 +586,7 @@ void i2c_atr_delete(struct i2c_atr *atr) WARN_ON(atr->adapter[i]); =20 bus_unregister_notifier(&i2c_bus_type, &atr->i2c_nb); - bitmap_free(atr->alias_use_mask); - kfree(atr->aliases); + i2c_atr_free_alias_pool(atr->alias_pool); mutex_destroy(&atr->lock); kfree(atr); } --=20 2.47.0