Skip to content

Commit bacdbe0

Browse files
nvswarrenbroonie
authored andcommitted
regmap: introduce fast_io busses, and use a spinlock for them
Some bus types have very fast IO. For these, acquiring a mutex for every IO operation is a significant overhead. Allow busses to indicate their IO is fast, and enhance regmap to use a spinlock for those busses. [Currently limited to native endian registers -- broonie] Signed-off-by: Stephen Warren <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent 0135bbc commit bacdbe0

File tree

5 files changed

+67
-30
lines changed

5 files changed

+67
-30
lines changed

drivers/base/regmap/internal.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ struct regmap_format {
3131
unsigned int (*parse_val)(void *buf);
3232
};
3333

34+
typedef void (*regmap_lock)(struct regmap *map);
35+
typedef void (*regmap_unlock)(struct regmap *map);
36+
3437
struct regmap {
35-
struct mutex lock;
38+
struct mutex mutex;
39+
spinlock_t spinlock;
40+
regmap_lock lock;
41+
regmap_unlock unlock;
3642

3743
struct device *dev; /* Device we do I/O on */
3844
void *work_buf; /* Scratch buffer used to format I/O */

drivers/base/regmap/regcache-rbtree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static int rbtree_show(struct seq_file *s, void *ignored)
139139
int nodes = 0;
140140
int registers = 0;
141141

142-
mutex_lock(&map->lock);
142+
map->lock(map);
143143

144144
for (node = rb_first(&rbtree_ctx->root); node != NULL;
145145
node = rb_next(node)) {
@@ -155,7 +155,7 @@ static int rbtree_show(struct seq_file *s, void *ignored)
155155
seq_printf(s, "%d nodes, %d registers, average %d registers\n",
156156
nodes, registers, registers / nodes);
157157

158-
mutex_unlock(&map->lock);
158+
map->unlock(map);
159159

160160
return 0;
161161
}

drivers/base/regmap/regcache.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ int regcache_sync(struct regmap *map)
264264

265265
BUG_ON(!map->cache_ops || !map->cache_ops->sync);
266266

267-
mutex_lock(&map->lock);
267+
map->lock(map);
268268
/* Remember the initial bypass state */
269269
bypass = map->cache_bypass;
270270
dev_dbg(map->dev, "Syncing %s cache\n",
@@ -296,7 +296,7 @@ int regcache_sync(struct regmap *map)
296296
trace_regcache_sync(map->dev, name, "stop");
297297
/* Restore the bypass state */
298298
map->cache_bypass = bypass;
299-
mutex_unlock(&map->lock);
299+
map->unlock(map);
300300

301301
return ret;
302302
}
@@ -323,7 +323,7 @@ int regcache_sync_region(struct regmap *map, unsigned int min,
323323

324324
BUG_ON(!map->cache_ops || !map->cache_ops->sync);
325325

326-
mutex_lock(&map->lock);
326+
map->lock(map);
327327

328328
/* Remember the initial bypass state */
329329
bypass = map->cache_bypass;
@@ -342,7 +342,7 @@ int regcache_sync_region(struct regmap *map, unsigned int min,
342342
trace_regcache_sync(map->dev, name, "stop region");
343343
/* Restore the bypass state */
344344
map->cache_bypass = bypass;
345-
mutex_unlock(&map->lock);
345+
map->unlock(map);
346346

347347
return ret;
348348
}
@@ -361,11 +361,11 @@ int regcache_sync_region(struct regmap *map, unsigned int min,
361361
*/
362362
void regcache_cache_only(struct regmap *map, bool enable)
363363
{
364-
mutex_lock(&map->lock);
364+
map->lock(map);
365365
WARN_ON(map->cache_bypass && enable);
366366
map->cache_only = enable;
367367
trace_regmap_cache_only(map->dev, enable);
368-
mutex_unlock(&map->lock);
368+
map->unlock(map);
369369
}
370370
EXPORT_SYMBOL_GPL(regcache_cache_only);
371371

@@ -380,9 +380,9 @@ EXPORT_SYMBOL_GPL(regcache_cache_only);
380380
*/
381381
void regcache_mark_dirty(struct regmap *map)
382382
{
383-
mutex_lock(&map->lock);
383+
map->lock(map);
384384
map->cache_dirty = true;
385-
mutex_unlock(&map->lock);
385+
map->unlock(map);
386386
}
387387
EXPORT_SYMBOL_GPL(regcache_mark_dirty);
388388

@@ -399,11 +399,11 @@ EXPORT_SYMBOL_GPL(regcache_mark_dirty);
399399
*/
400400
void regcache_cache_bypass(struct regmap *map, bool enable)
401401
{
402-
mutex_lock(&map->lock);
402+
map->lock(map);
403403
WARN_ON(map->cache_only && enable);
404404
map->cache_bypass = enable;
405405
trace_regmap_cache_bypass(map->dev, enable);
406-
mutex_unlock(&map->lock);
406+
map->unlock(map);
407407
}
408408
EXPORT_SYMBOL_GPL(regcache_cache_bypass);
409409

drivers/base/regmap/regmap.c

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,26 @@ static unsigned int regmap_parse_32(void *buf)
158158
return b[0];
159159
}
160160

161+
static void regmap_lock_mutex(struct regmap *map)
162+
{
163+
mutex_lock(&map->mutex);
164+
}
165+
166+
static void regmap_unlock_mutex(struct regmap *map)
167+
{
168+
mutex_unlock(&map->mutex);
169+
}
170+
171+
static void regmap_lock_spinlock(struct regmap *map)
172+
{
173+
spin_lock(&map->spinlock);
174+
}
175+
176+
static void regmap_unlock_spinlock(struct regmap *map)
177+
{
178+
spin_unlock(&map->spinlock);
179+
}
180+
161181
/**
162182
* regmap_init(): Initialise register map
163183
*
@@ -187,7 +207,15 @@ struct regmap *regmap_init(struct device *dev,
187207
goto err;
188208
}
189209

190-
mutex_init(&map->lock);
210+
if (bus->fast_io) {
211+
spin_lock_init(&map->spinlock);
212+
map->lock = regmap_lock_spinlock;
213+
map->unlock = regmap_unlock_spinlock;
214+
} else {
215+
mutex_init(&map->mutex);
216+
map->lock = regmap_lock_mutex;
217+
map->unlock = regmap_unlock_mutex;
218+
}
191219
map->format.buf_size = (config->reg_bits + config->val_bits) / 8;
192220
map->format.reg_bytes = DIV_ROUND_UP(config->reg_bits, 8);
193221
map->format.pad_bytes = config->pad_bits / 8;
@@ -365,7 +393,7 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
365393
{
366394
int ret;
367395

368-
mutex_lock(&map->lock);
396+
map->lock(map);
369397

370398
regcache_exit(map);
371399
regmap_debugfs_exit(map);
@@ -384,7 +412,7 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
384412

385413
ret = regcache_init(map, config);
386414

387-
mutex_unlock(&map->lock);
415+
map->unlock(map);
388416

389417
return ret;
390418
}
@@ -536,11 +564,11 @@ int regmap_write(struct regmap *map, unsigned int reg, unsigned int val)
536564
{
537565
int ret;
538566

539-
mutex_lock(&map->lock);
567+
map->lock(map);
540568

541569
ret = _regmap_write(map, reg, val);
542570

543-
mutex_unlock(&map->lock);
571+
map->unlock(map);
544572

545573
return ret;
546574
}
@@ -567,11 +595,11 @@ int regmap_raw_write(struct regmap *map, unsigned int reg,
567595
{
568596
int ret;
569597

570-
mutex_lock(&map->lock);
598+
map->lock(map);
571599

572600
ret = _regmap_raw_write(map, reg, val, val_len);
573601

574-
mutex_unlock(&map->lock);
602+
map->unlock(map);
575603

576604
return ret;
577605
}
@@ -601,7 +629,7 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
601629
if (!map->format.parse_val)
602630
return -EINVAL;
603631

604-
mutex_lock(&map->lock);
632+
map->lock(map);
605633

606634
/* No formatting is require if val_byte is 1 */
607635
if (val_bytes == 1) {
@@ -622,7 +650,7 @@ int regmap_bulk_write(struct regmap *map, unsigned int reg, const void *val,
622650
kfree(wval);
623651

624652
out:
625-
mutex_unlock(&map->lock);
653+
map->unlock(map);
626654
return ret;
627655
}
628656
EXPORT_SYMBOL_GPL(regmap_bulk_write);
@@ -696,11 +724,11 @@ int regmap_read(struct regmap *map, unsigned int reg, unsigned int *val)
696724
{
697725
int ret;
698726

699-
mutex_lock(&map->lock);
727+
map->lock(map);
700728

701729
ret = _regmap_read(map, reg, val);
702730

703-
mutex_unlock(&map->lock);
731+
map->unlock(map);
704732

705733
return ret;
706734
}
@@ -725,7 +753,7 @@ int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
725753
unsigned int v;
726754
int ret, i;
727755

728-
mutex_lock(&map->lock);
756+
map->lock(map);
729757

730758
if (regmap_volatile_range(map, reg, val_count) || map->cache_bypass ||
731759
map->cache_type == REGCACHE_NONE) {
@@ -746,7 +774,7 @@ int regmap_raw_read(struct regmap *map, unsigned int reg, void *val,
746774
}
747775

748776
out:
749-
mutex_unlock(&map->lock);
777+
map->unlock(map);
750778

751779
return ret;
752780
}
@@ -799,7 +827,7 @@ static int _regmap_update_bits(struct regmap *map, unsigned int reg,
799827
int ret;
800828
unsigned int tmp, orig;
801829

802-
mutex_lock(&map->lock);
830+
map->lock(map);
803831

804832
ret = _regmap_read(map, reg, &orig);
805833
if (ret != 0)
@@ -816,7 +844,7 @@ static int _regmap_update_bits(struct regmap *map, unsigned int reg,
816844
}
817845

818846
out:
819-
mutex_unlock(&map->lock);
847+
map->unlock(map);
820848

821849
return ret;
822850
}
@@ -883,7 +911,7 @@ int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
883911
if (map->patch)
884912
return -EBUSY;
885913

886-
mutex_lock(&map->lock);
914+
map->lock(map);
887915

888916
bypass = map->cache_bypass;
889917

@@ -911,7 +939,7 @@ int regmap_register_patch(struct regmap *map, const struct reg_default *regs,
911939
out:
912940
map->cache_bypass = bypass;
913941

914-
mutex_unlock(&map->lock);
942+
map->unlock(map);
915943

916944
return ret;
917945
}

include/linux/regmap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ typedef void (*regmap_hw_free_context)(void *context);
110110
/**
111111
* Description of a hardware bus for the register map infrastructure.
112112
*
113+
* @fast_io: Register IO is fast. Use a spinlock instead of a mutex
114+
* to perform locking.
113115
* @write: Write operation.
114116
* @gather_write: Write operation with split register/value, return -ENOTSUPP
115117
* if not implemented on a given device.
@@ -119,6 +121,7 @@ typedef void (*regmap_hw_free_context)(void *context);
119121
* a read.
120122
*/
121123
struct regmap_bus {
124+
bool fast_io;
122125
regmap_hw_write write;
123126
regmap_hw_gather_write gather_write;
124127
regmap_hw_read read;

0 commit comments

Comments
 (0)