Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp4078218rwb; Tue, 16 Aug 2022 14:07:42 -0700 (PDT) X-Google-Smtp-Source: AA6agR7UNnMqTZtTe8vzz8b+Cqir0n9I6Sifq+UhN/lKJmA/+LGUELbLTAheT3GTMqDf91YYPxt1 X-Received: by 2002:a05:6402:34c6:b0:43d:8cea:76c0 with SMTP id w6-20020a05640234c600b0043d8cea76c0mr20982503edc.268.1660684062529; Tue, 16 Aug 2022 14:07:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660684062; cv=none; d=google.com; s=arc-20160816; b=ilhSVgP2GR15Wvpst8VWWs69iDTgMPFqxMNWs+ixaMIlzWO3LajfYzu2A22UK9clyB yXcgSPAW4Zx86i92sTK0Af4qPdb1EN5sDt/vwSm+Mi14LJF8x6CCSbxhmY8ciW9CW1M3 H7Z7EfHAs9o/3AZvCtP93hPVAPYDp1GlJoA3+oIvqSq3htXte5+u2Be5WTUdIvtATr4H ZiRtubP0dH+sn0L/inmew3wWjiRMhUy3YbyvXUdiRitqDkd3z8z4QCEAGkNl+LfamUtq 7zuAE2sCfB23f8R+zKp3R68lkjXxSqPte1FtGUXg+LmPKWuTPKNH9o1LXr48iQLIMVWg RYQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=y+6u1zkk8J60+XTRv+ei7xljXugjN0ECqEpbeFFmEfA=; b=TQqW4EGGLehH3ftqUljU8xKvtjPorjjm1noh2YIj0Emmg5m82Bh0uEcCXk7h3QkbEt V284rWScdLA/2YRnmKwRO2QbOsoohUWDhxWNY/Yk28xQV0DKQc0pm2BBF2iTXQ3oo73T Fx9AstHtoo3uUQDEjFDmi676dj/E/mejVvPnBa8eylVK/9x2YL4LITLhl4ZDKmg6gGI2 XuXGFNb2ZrLcWjKaZeAIGsSWKb2NbGn1XL7UVOKdyHsIttkSjWGctyJ0YE4x2Hg6Z+w+ t1eaqi3nDxwzRXDCKhyrm9YyOV7MTCs2mEWzb/K/Cr1xSijUTq9bM89iIXd08gN8QLN3 7nvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LJcYKMe5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id eb9-20020a0564020d0900b0043e18c8248asi12282065edb.600.2022.08.16.14.07.17; Tue, 16 Aug 2022 14:07:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=LJcYKMe5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237295AbiHPUuk (ORCPT + 99 others); Tue, 16 Aug 2022 16:50:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237097AbiHPUui (ORCPT ); Tue, 16 Aug 2022 16:50:38 -0400 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72A0F8991A for ; Tue, 16 Aug 2022 13:50:36 -0700 (PDT) Received: by mail-lf1-x12d.google.com with SMTP id e15so16549225lfs.0 for ; Tue, 16 Aug 2022 13:50:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc; bh=y+6u1zkk8J60+XTRv+ei7xljXugjN0ECqEpbeFFmEfA=; b=LJcYKMe5iWHcfHmSZpZoCaFDjr5mpiGlraMtQHx3ch5EIMtpY4nD/31iklrJkCiV2f Ot+HThfEdOwyOUT1AzSzwFLC6Moo8zCRFCtLiXRdIEainTex+QF8ZDTV0tAkAa57Th72 wJyKWRfs7sWxcen/0oLPyG1oU8pzVnydIv48dEHG6eBJqx1A5uOiYHL3OUbiEK9rUCi4 J44F0txCiaNezLImiMBrEnqf3WtfqsA6Z9QVmezHvzVcWedfECorX7sgOEmGFl0wbUnY XhOGXvhpAGDKPFZZb9S/ik7n48UBPLCWQnEW5DVAc22hy9O5V2y/ENXKF2q/HCvy7ovN +mlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc; bh=y+6u1zkk8J60+XTRv+ei7xljXugjN0ECqEpbeFFmEfA=; b=DUFdCt0Fm0facHqU61lddFFUYGLXKWd8vYdLFbcJvkCDeUcZXjQkx71uti+l/+gFog 6iAZkbiaW6IJQppqJTeAASTFjSFJHtnbmVAM9fBsomKxgdW6dkQpvjY7ZuQLLEQi7S8F 7YKb8NYJd8NjUvdmUjjyU3eRuRlVOmdyk69qBxr0GP9jTeKxE6JBzD1JSvEP92rx05c8 6a7YRmzK73NlJbm2WFQ7eSHn9W/3A0EsdjWiL7k7XUqs30gsSqPwv99yxogiGHWXwmXy Xp2COrv5/FZ+pPgxqmu51PoDfLZVWCpeJCeTzzhQIgnRdwm3SGQb60CABtRgqloUyMJg WEag== X-Gm-Message-State: ACgBeo3bSmMTPU04g4ujFbk5z4pRwYHlwwqNzrL5ViaABvZasKOrgdaB tv4CxR7QSBTT+BRWJhsDZeJM24qT3wfT1w== X-Received: by 2002:ac2:58d6:0:b0:48b:2ef5:18c7 with SMTP id u22-20020ac258d6000000b0048b2ef518c7mr7372202lfo.605.1660683034707; Tue, 16 Aug 2022 13:50:34 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id s4-20020a2e83c4000000b002618fbebbbcsm670816ljh.47.2022.08.16.13.50.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Aug 2022 13:50:34 -0700 (PDT) From: Linus Walleij To: Mark Brown Cc: linux-kernel@vger.kernel.org, Linus Walleij Subject: [PATCH 1/2 v5] regmap: Support accelerated noinc operations Date: Tue, 16 Aug 2022 22:48:31 +0200 Message-Id: <20220816204832.265837-1-linus.walleij@linaro.org> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Several architectures have accelerated operations for MMIO operations writing to a single register, such as writesb, writesw, writesl, writesq, readsb, readsw, readsl and readsq but regmap currently cannot use them because we have no hooks for providing an accelerated noinc back-end for MMIO. Solve this by providing reg_[read/write]_noinc callbacks for the bus abstraction, so that the regmap-mmio bus can use this. Currently I do not see a need to support this for custom regmaps so it is only added to the bus. Callbacks are passed a void * with the array of values and a count which is the number of items of the byte chunk size for the specific register width. Signed-off-by: Linus Walleij --- ChangeLog v4->v5: - Repost with the other patch as it was discovered that the Hexagon needs fixing to support writesb/readsb. ChangeLog v3->v4: - Rebase on regmap for-next ChangeLog v2->v3: - Rebase on kernel v6.0-rc1 ChangeLog v1->v2: - Factor out and reuse the code to format and read or write a buffer of data to a noinc register at the cost of dropping const from the buffer pointer in the write call. This is a deadly sin in Rust and therefore impossible, but hey, this is C, and dropping a const is a lesser evil than not being able to reuse code. --- drivers/base/regmap/regmap.c | 123 ++++++++++++++++++++++++++++++++++- include/linux/regmap.h | 8 +++ 2 files changed, 128 insertions(+), 3 deletions(-) diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c index e371acea7e0e..41ff9f18b6a3 100644 --- a/drivers/base/regmap/regmap.c +++ b/drivers/base/regmap/regmap.c @@ -2129,6 +2129,99 @@ int regmap_raw_write(struct regmap *map, unsigned int reg, } EXPORT_SYMBOL_GPL(regmap_raw_write); +static int regmap_noinc_readwrite(struct regmap *map, unsigned int reg, + void *val, unsigned int val_len, bool write) +{ + size_t val_bytes = map->format.val_bytes; + size_t val_count = val_len / val_bytes; + unsigned int lastval; + u8 *u8p; + u16 *u16p; + u32 *u32p; +#ifdef CONFIG_64BIT + u64 *u64p; +#endif + int ret; + int i; + + switch (val_bytes) { + case 1: + u8p = val; + if (write) + lastval = (unsigned int)u8p[val_count - 1]; + break; + case 2: + u16p = val; + if (write) + lastval = (unsigned int)u16p[val_count - 1]; + break; + case 4: + u32p = val; + if (write) + lastval = (unsigned int)u32p[val_count - 1]; + break; +#ifdef CONFIG_64BIT + case 8: + u64p = val; + if (write) + lastval = (unsigned int)u64p[val_count - 1]; + break; +#endif + default: + return -EINVAL; + } + + /* + * Update the cache with the last value we write, the rest is just + * gone down in the hardware FIFO. We can't cache FIFOs. This makes + * sure a single read from the cache will work. + */ + if (write) { + if (!map->cache_bypass && !map->defer_caching) { + ret = regcache_write(map, reg, lastval); + if (ret != 0) + return ret; + if (map->cache_only) { + map->cache_dirty = true; + return 0; + } + } + ret = map->bus->reg_noinc_write(map->bus_context, reg, val, val_count); + } else { + ret = map->bus->reg_noinc_read(map->bus_context, reg, val, val_count); + } + + if (!ret && regmap_should_log(map)) { + dev_info(map->dev, "%x %s [", reg, write ? "<=" : "=>"); + for (i = 0; i < val_len; i++) { + switch (val_bytes) { + case 1: + pr_cont("%x", u8p[i]); + break; + case 2: + pr_cont("%x", u16p[i]); + break; + case 4: + pr_cont("%x", u32p[i]); + break; +#ifdef CONFIG_64BIT + case 8: + pr_cont("%llx", u64p[i]); + break; +#endif + default: + break; + } + if (i == (val_len - 1)) + pr_cont("]\n"); + else + pr_cont(","); + } + } + + return 0; +} + /** * regmap_noinc_write(): Write data from a register without incrementing the * register number @@ -2156,9 +2249,8 @@ int regmap_noinc_write(struct regmap *map, unsigned int reg, size_t write_len; int ret; - if (!map->write) - return -ENOTSUPP; - + if (!map->write && !(map->bus && map->bus->reg_noinc_write)) + return -EINVAL; if (val_len % map->format.val_bytes) return -EINVAL; if (!IS_ALIGNED(reg, map->reg_stride)) @@ -2173,6 +2265,15 @@ int regmap_noinc_write(struct regmap *map, unsigned int reg, goto out_unlock; } + /* + * Use the accelerated operation if we can. The val drops the const + * typing in order to facilitate code reuse in regmap_noinc_readwrite(). + */ + if (map->bus->reg_noinc_write) { + ret = regmap_noinc_readwrite(map, reg, (void *)val, val_len, true); + goto out_unlock; + } + while (val_len) { if (map->max_raw_write && map->max_raw_write < val_len) write_len = map->max_raw_write; @@ -2943,6 +3044,22 @@ int regmap_noinc_read(struct regmap *map, unsigned int reg, goto out_unlock; } + /* Use the accelerated operation if we can */ + if (map->bus->reg_noinc_read) { + /* + * We have not defined the FIFO semantics for cache, as the + * cache is just one value deep. Should we return the last + * written value? Just avoid this by always reading the FIFO + * even when using cache. Cache only will not work. + */ + if (map->cache_only) { + ret = -EBUSY; + goto out_unlock; + } + ret = regmap_noinc_readwrite(map, reg, val, val_len, false); + goto out_unlock; + } + while (val_len) { if (map->max_raw_read && map->max_raw_read < val_len) read_len = map->max_raw_read; diff --git a/include/linux/regmap.h b/include/linux/regmap.h index 8cccc247cd37..ca3434dca3a0 100644 --- a/include/linux/regmap.h +++ b/include/linux/regmap.h @@ -492,8 +492,12 @@ typedef int (*regmap_hw_read)(void *context, void *val_buf, size_t val_size); typedef int (*regmap_hw_reg_read)(void *context, unsigned int reg, unsigned int *val); +typedef int (*regmap_hw_reg_noinc_read)(void *context, unsigned int reg, + void *val, size_t val_count); typedef int (*regmap_hw_reg_write)(void *context, unsigned int reg, unsigned int val); +typedef int (*regmap_hw_reg_noinc_write)(void *context, unsigned int reg, + const void *val, size_t val_count); typedef int (*regmap_hw_reg_update_bits)(void *context, unsigned int reg, unsigned int mask, unsigned int val); typedef struct regmap_async *(*regmap_hw_async_alloc)(void); @@ -514,6 +518,8 @@ typedef void (*regmap_hw_free_context)(void *context); * must serialise with respect to non-async I/O. * @reg_write: Write a single register value to the given register address. This * write operation has to complete when returning from the function. + * @reg_write_noinc: Write multiple register value to the same register. This + * write operation has to complete when returning from the function. * @reg_update_bits: Update bits operation to be used against volatile * registers, intended for devices supporting some mechanism * for setting clearing bits without having to @@ -541,9 +547,11 @@ struct regmap_bus { regmap_hw_gather_write gather_write; regmap_hw_async_write async_write; regmap_hw_reg_write reg_write; + regmap_hw_reg_noinc_write reg_noinc_write; regmap_hw_reg_update_bits reg_update_bits; regmap_hw_read read; regmap_hw_reg_read reg_read; + regmap_hw_reg_noinc_read reg_noinc_read; regmap_hw_free_context free_context; regmap_hw_async_alloc async_alloc; u8 read_flag_mask; -- 2.37.2