|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +// |
| 3 | +// Register cache access API - maple tree based cache |
| 4 | +// |
| 5 | +// Copyright 2023 Arm, Ltd |
| 6 | +// |
| 7 | +// Author: Mark Brown <broonie@kernel.org> |
| 8 | + |
| 9 | +#include <linux/debugfs.h> |
| 10 | +#include <linux/device.h> |
| 11 | +#include <linux/maple_tree.h> |
| 12 | +#include <linux/slab.h> |
| 13 | + |
| 14 | +#include "internal.h" |
| 15 | + |
| 16 | +static int regcache_maple_read(struct regmap *map, |
| 17 | + unsigned int reg, unsigned int *value) |
| 18 | +{ |
| 19 | + struct maple_tree *mt = map->cache; |
| 20 | + MA_STATE(mas, mt, reg, reg); |
| 21 | + unsigned long *entry; |
| 22 | + |
| 23 | + rcu_read_lock(); |
| 24 | + |
| 25 | + entry = mas_find(&mas, reg); |
| 26 | + if (!entry) { |
| 27 | + rcu_read_unlock(); |
| 28 | + return -ENOENT; |
| 29 | + } |
| 30 | + |
| 31 | + *value = entry[reg - mas.index]; |
| 32 | + |
| 33 | + rcu_read_unlock(); |
| 34 | + |
| 35 | + return 0; |
| 36 | +} |
| 37 | + |
| 38 | +static int regcache_maple_write(struct regmap *map, unsigned int reg, |
| 39 | + unsigned int val) |
| 40 | +{ |
| 41 | + struct maple_tree *mt = map->cache; |
| 42 | + MA_STATE(mas, mt, reg, reg); |
| 43 | + unsigned long *entry, *upper, *lower; |
| 44 | + unsigned long index, last; |
| 45 | + size_t lower_sz, upper_sz; |
| 46 | + int ret; |
| 47 | + |
| 48 | + rcu_read_lock(); |
| 49 | + |
| 50 | + entry = mas_find(&mas, reg); |
| 51 | + if (entry) { |
| 52 | + entry[reg - mas.index] = val; |
| 53 | + rcu_read_unlock(); |
| 54 | + return 0; |
| 55 | + } |
| 56 | + |
| 57 | + /* Any adjacent entries to extend/merge? */ |
| 58 | + mas_set_range(&mas, reg - 1, reg + 1); |
| 59 | + index = reg; |
| 60 | + last = reg; |
| 61 | + |
| 62 | + lower = mas_find(&mas, reg - 1); |
| 63 | + if (lower) { |
| 64 | + index = mas.index; |
| 65 | + lower_sz = (mas.last - mas.index + 1) * sizeof(unsigned long); |
| 66 | + } |
| 67 | + |
| 68 | + upper = mas_find(&mas, reg + 1); |
| 69 | + if (upper) { |
| 70 | + last = mas.last; |
| 71 | + upper_sz = (mas.last - mas.index + 1) * sizeof(unsigned long); |
| 72 | + } |
| 73 | + |
| 74 | + rcu_read_unlock(); |
| 75 | + |
| 76 | + entry = kmalloc((last - index + 1) * sizeof(unsigned long), |
| 77 | + GFP_KERNEL); |
| 78 | + if (!entry) |
| 79 | + return -ENOMEM; |
| 80 | + |
| 81 | + if (lower) |
| 82 | + memcpy(entry, lower, lower_sz); |
| 83 | + entry[reg - index] = val; |
| 84 | + if (upper) |
| 85 | + memcpy(&entry[reg - index + 1], upper, upper_sz); |
| 86 | + |
| 87 | + /* |
| 88 | + * This is safe because the regmap lock means the Maple lock |
| 89 | + * is redundant, but we need to take it due to lockdep asserts |
| 90 | + * in the maple tree code. |
| 91 | + */ |
| 92 | + mas_lock(&mas); |
| 93 | + |
| 94 | + mas_set_range(&mas, index, last); |
| 95 | + ret = mas_store_gfp(&mas, entry, GFP_KERNEL); |
| 96 | + |
| 97 | + mas_unlock(&mas); |
| 98 | + |
| 99 | + if (ret == 0) { |
| 100 | + kfree(lower); |
| 101 | + kfree(upper); |
| 102 | + } |
| 103 | + |
| 104 | + return ret; |
| 105 | +} |
| 106 | + |
| 107 | +static int regcache_maple_drop(struct regmap *map, unsigned int min, |
| 108 | + unsigned int max) |
| 109 | +{ |
| 110 | + struct maple_tree *mt = map->cache; |
| 111 | + MA_STATE(mas, mt, min, max); |
| 112 | + unsigned long *entry, *lower, *upper; |
| 113 | + unsigned long lower_index, lower_last; |
| 114 | + unsigned long upper_index, upper_last; |
| 115 | + int ret; |
| 116 | + |
| 117 | + lower = NULL; |
| 118 | + upper = NULL; |
| 119 | + |
| 120 | + mas_lock(&mas); |
| 121 | + |
| 122 | + mas_for_each(&mas, entry, max) { |
| 123 | + /* |
| 124 | + * This is safe because the regmap lock means the |
| 125 | + * Maple lock is redundant, but we need to take it due |
| 126 | + * to lockdep asserts in the maple tree code. |
| 127 | + */ |
| 128 | + mas_unlock(&mas); |
| 129 | + |
| 130 | + /* Do we need to save any of this entry? */ |
| 131 | + if (mas.index < min) { |
| 132 | + lower_index = mas.index; |
| 133 | + lower_last = min -1; |
| 134 | + |
| 135 | + lower = kmemdup(entry, ((min - mas.index) * |
| 136 | + sizeof(unsigned long)), |
| 137 | + GFP_KERNEL); |
| 138 | + if (!lower) { |
| 139 | + ret = -ENOMEM; |
| 140 | + goto out; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if (mas.last > max) { |
| 145 | + upper_index = max + 1; |
| 146 | + upper_last = mas.last; |
| 147 | + |
| 148 | + upper = kmemdup(&entry[max + 1], |
| 149 | + ((mas.last - max) * |
| 150 | + sizeof(unsigned long)), |
| 151 | + GFP_KERNEL); |
| 152 | + if (!upper) { |
| 153 | + ret = -ENOMEM; |
| 154 | + goto out; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + kfree(entry); |
| 159 | + mas_lock(&mas); |
| 160 | + mas_erase(&mas); |
| 161 | + |
| 162 | + /* Insert new nodes with the saved data */ |
| 163 | + if (lower) { |
| 164 | + mas_set_range(&mas, lower_index, lower_last); |
| 165 | + ret = mas_store_gfp(&mas, lower, GFP_KERNEL); |
| 166 | + if (ret != 0) |
| 167 | + goto out; |
| 168 | + lower = NULL; |
| 169 | + } |
| 170 | + |
| 171 | + if (upper) { |
| 172 | + mas_set_range(&mas, upper_index, upper_last); |
| 173 | + ret = mas_store_gfp(&mas, upper, GFP_KERNEL); |
| 174 | + if (ret != 0) |
| 175 | + goto out; |
| 176 | + upper = NULL; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | +out: |
| 181 | + mas_unlock(&mas); |
| 182 | + kfree(lower); |
| 183 | + kfree(upper); |
| 184 | + |
| 185 | + return ret; |
| 186 | +} |
| 187 | + |
| 188 | +static int regcache_maple_sync(struct regmap *map, unsigned int min, |
| 189 | + unsigned int max) |
| 190 | +{ |
| 191 | + struct maple_tree *mt = map->cache; |
| 192 | + unsigned long *entry; |
| 193 | + MA_STATE(mas, mt, min, max); |
| 194 | + unsigned long lmin = min; |
| 195 | + unsigned long lmax = max; |
| 196 | + unsigned int r; |
| 197 | + int ret; |
| 198 | + |
| 199 | + map->cache_bypass = true; |
| 200 | + |
| 201 | + rcu_read_lock(); |
| 202 | + |
| 203 | + mas_for_each(&mas, entry, max) { |
| 204 | + for (r = max(mas.index, lmin); r <= min(mas.last, lmax); r++) { |
| 205 | + ret = regcache_sync_val(map, r, entry[r - mas.index]); |
| 206 | + if (ret != 0) |
| 207 | + goto out; |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | +out: |
| 212 | + rcu_read_unlock(); |
| 213 | + |
| 214 | + map->cache_bypass = false; |
| 215 | + |
| 216 | + return ret; |
| 217 | +} |
| 218 | + |
| 219 | +static int regcache_maple_exit(struct regmap *map) |
| 220 | +{ |
| 221 | + struct maple_tree *mt = map->cache; |
| 222 | + MA_STATE(mas, mt, 0, UINT_MAX); |
| 223 | + unsigned int *entry;; |
| 224 | + |
| 225 | + /* if we've already been called then just return */ |
| 226 | + if (!mt) |
| 227 | + return 0; |
| 228 | + |
| 229 | + mas_lock(&mas); |
| 230 | + mas_for_each(&mas, entry, UINT_MAX) |
| 231 | + kfree(entry); |
| 232 | + __mt_destroy(mt); |
| 233 | + mas_unlock(&mas); |
| 234 | + |
| 235 | + kfree(mt); |
| 236 | + map->cache = NULL; |
| 237 | + |
| 238 | + return 0; |
| 239 | +} |
| 240 | + |
| 241 | +static int regcache_maple_init(struct regmap *map) |
| 242 | +{ |
| 243 | + struct maple_tree *mt; |
| 244 | + int i; |
| 245 | + int ret; |
| 246 | + |
| 247 | + mt = kmalloc(sizeof(*mt), GFP_KERNEL); |
| 248 | + if (!mt) |
| 249 | + return -ENOMEM; |
| 250 | + map->cache = mt; |
| 251 | + |
| 252 | + mt_init(mt); |
| 253 | + |
| 254 | + for (i = 0; i < map->num_reg_defaults; i++) { |
| 255 | + ret = regcache_maple_write(map, |
| 256 | + map->reg_defaults[i].reg, |
| 257 | + map->reg_defaults[i].def); |
| 258 | + if (ret) |
| 259 | + goto err; |
| 260 | + } |
| 261 | + |
| 262 | + return 0; |
| 263 | + |
| 264 | +err: |
| 265 | + regcache_maple_exit(map); |
| 266 | + return ret; |
| 267 | +} |
| 268 | + |
| 269 | +struct regcache_ops regcache_maple_ops = { |
| 270 | + .type = REGCACHE_MAPLE, |
| 271 | + .name = "maple", |
| 272 | + .init = regcache_maple_init, |
| 273 | + .exit = regcache_maple_exit, |
| 274 | + .read = regcache_maple_read, |
| 275 | + .write = regcache_maple_write, |
| 276 | + .drop = regcache_maple_drop, |
| 277 | + .sync = regcache_maple_sync, |
| 278 | +}; |
0 commit comments