Angel Pons | ebda03e | 2020-04-02 23:48:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 3 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 4 | #include <commonlib/helpers.h> |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 5 | #include <commonlib/region.h> |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 6 | #include <string.h> |
| 7 | |
Aaron Durbin | 02103e3 | 2017-12-14 15:22:04 -0700 | [diff] [blame] | 8 | int region_is_subregion(const struct region *p, const struct region *c) |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 9 | { |
| 10 | if (region_offset(c) < region_offset(p)) |
| 11 | return 0; |
| 12 | |
Julius Werner | 5c82c44 | 2019-08-15 21:25:16 -0700 | [diff] [blame] | 13 | if (region_end(c) > region_end(p)) |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 14 | return 0; |
| 15 | |
Julius Werner | 5c82c44 | 2019-08-15 21:25:16 -0700 | [diff] [blame] | 16 | if (region_end(c) < region_offset(c)) |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 17 | return 0; |
| 18 | |
| 19 | return 1; |
| 20 | } |
| 21 | |
| 22 | static int normalize_and_ok(const struct region *outer, struct region *inner) |
| 23 | { |
| 24 | inner->offset += region_offset(outer); |
Aaron Durbin | 02103e3 | 2017-12-14 15:22:04 -0700 | [diff] [blame] | 25 | return region_is_subregion(outer, inner); |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static const struct region_device *rdev_root(const struct region_device *rdev) |
| 29 | { |
| 30 | if (rdev->root == NULL) |
| 31 | return rdev; |
| 32 | return rdev->root; |
| 33 | } |
| 34 | |
Aaron Durbin | 990ab7e | 2015-12-15 13:29:41 -0600 | [diff] [blame] | 35 | ssize_t rdev_relative_offset(const struct region_device *p, |
| 36 | const struct region_device *c) |
| 37 | { |
| 38 | if (rdev_root(p) != rdev_root(c)) |
| 39 | return -1; |
| 40 | |
Aaron Durbin | 02103e3 | 2017-12-14 15:22:04 -0700 | [diff] [blame] | 41 | if (!region_is_subregion(&p->region, &c->region)) |
Aaron Durbin | 990ab7e | 2015-12-15 13:29:41 -0600 | [diff] [blame] | 42 | return -1; |
| 43 | |
| 44 | return region_device_offset(c) - region_device_offset(p); |
| 45 | } |
| 46 | |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 47 | void *rdev_mmap(const struct region_device *rd, size_t offset, size_t size) |
| 48 | { |
| 49 | const struct region_device *rdev; |
| 50 | struct region req = { |
| 51 | .offset = offset, |
| 52 | .size = size, |
| 53 | }; |
| 54 | |
| 55 | if (!normalize_and_ok(&rd->region, &req)) |
| 56 | return NULL; |
| 57 | |
| 58 | rdev = rdev_root(rd); |
| 59 | |
Aaron Durbin | edd7959 | 2016-08-10 11:39:00 -0500 | [diff] [blame] | 60 | if (rdev->ops->mmap == NULL) |
| 61 | return NULL; |
| 62 | |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 63 | return rdev->ops->mmap(rdev, req.offset, req.size); |
| 64 | } |
| 65 | |
| 66 | int rdev_munmap(const struct region_device *rd, void *mapping) |
| 67 | { |
| 68 | const struct region_device *rdev; |
| 69 | |
| 70 | rdev = rdev_root(rd); |
| 71 | |
Aaron Durbin | edd7959 | 2016-08-10 11:39:00 -0500 | [diff] [blame] | 72 | if (rdev->ops->munmap == NULL) |
| 73 | return -1; |
| 74 | |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 75 | return rdev->ops->munmap(rdev, mapping); |
| 76 | } |
| 77 | |
| 78 | ssize_t rdev_readat(const struct region_device *rd, void *b, size_t offset, |
| 79 | size_t size) |
| 80 | { |
| 81 | const struct region_device *rdev; |
| 82 | struct region req = { |
| 83 | .offset = offset, |
| 84 | .size = size, |
| 85 | }; |
| 86 | |
| 87 | if (!normalize_and_ok(&rd->region, &req)) |
| 88 | return -1; |
| 89 | |
| 90 | rdev = rdev_root(rd); |
| 91 | |
| 92 | return rdev->ops->readat(rdev, b, req.offset, req.size); |
| 93 | } |
| 94 | |
Aaron Durbin | 258a350 | 2016-08-04 14:33:58 -0500 | [diff] [blame] | 95 | ssize_t rdev_writeat(const struct region_device *rd, const void *b, |
| 96 | size_t offset, size_t size) |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 97 | { |
| 98 | const struct region_device *rdev; |
| 99 | struct region req = { |
| 100 | .offset = offset, |
| 101 | .size = size, |
| 102 | }; |
| 103 | |
| 104 | if (!normalize_and_ok(&rd->region, &req)) |
| 105 | return -1; |
| 106 | |
| 107 | rdev = rdev_root(rd); |
| 108 | |
| 109 | if (rdev->ops->writeat == NULL) |
| 110 | return -1; |
| 111 | |
| 112 | return rdev->ops->writeat(rdev, b, req.offset, req.size); |
| 113 | } |
| 114 | |
| 115 | ssize_t rdev_eraseat(const struct region_device *rd, size_t offset, |
| 116 | size_t size) |
| 117 | { |
| 118 | const struct region_device *rdev; |
| 119 | struct region req = { |
| 120 | .offset = offset, |
| 121 | .size = size, |
| 122 | }; |
| 123 | |
| 124 | if (!normalize_and_ok(&rd->region, &req)) |
| 125 | return -1; |
| 126 | |
| 127 | rdev = rdev_root(rd); |
| 128 | |
| 129 | /* If the eraseat ptr is NULL we assume that the erase |
| 130 | * function was completed successfully. */ |
| 131 | if (rdev->ops->eraseat == NULL) |
| 132 | return size; |
| 133 | |
| 134 | return rdev->ops->eraseat(rdev, req.offset, req.size); |
| 135 | } |
| 136 | |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 137 | int rdev_chain(struct region_device *child, const struct region_device *parent, |
| 138 | size_t offset, size_t size) |
| 139 | { |
| 140 | struct region req = { |
| 141 | .offset = offset, |
| 142 | .size = size, |
| 143 | }; |
| 144 | |
| 145 | if (!normalize_and_ok(&parent->region, &req)) |
| 146 | return -1; |
| 147 | |
| 148 | /* Keep track of root region device. Note the offsets are relative |
| 149 | * to the root device. */ |
| 150 | child->root = rdev_root(parent); |
| 151 | child->ops = NULL; |
| 152 | child->region.offset = req.offset; |
| 153 | child->region.size = req.size; |
| 154 | |
| 155 | return 0; |
| 156 | } |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 157 | |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 158 | static void mem_region_device_init(struct mem_region_device *mdev, |
| 159 | const struct region_device_ops *ops, void *base, size_t size) |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 160 | { |
| 161 | memset(mdev, 0, sizeof(*mdev)); |
| 162 | mdev->base = base; |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 163 | mdev->rdev.ops = ops; |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 164 | mdev->rdev.region.size = size; |
| 165 | } |
| 166 | |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 167 | void mem_region_device_ro_init(struct mem_region_device *mdev, void *base, |
| 168 | size_t size) |
| 169 | { |
| 170 | return mem_region_device_init(mdev, &mem_rdev_ro_ops, base, size); |
| 171 | } |
| 172 | |
| 173 | void mem_region_device_rw_init(struct mem_region_device *mdev, void *base, |
| 174 | size_t size) |
| 175 | { |
| 176 | return mem_region_device_init(mdev, &mem_rdev_rw_ops, base, size); |
| 177 | } |
| 178 | |
Furquan Shaikh | 2b57691 | 2016-06-19 23:16:45 -0700 | [diff] [blame] | 179 | void region_device_init(struct region_device *rdev, |
| 180 | const struct region_device_ops *ops, size_t offset, |
| 181 | size_t size) |
| 182 | { |
| 183 | memset(rdev, 0, sizeof(*rdev)); |
| 184 | rdev->root = NULL; |
| 185 | rdev->ops = ops; |
| 186 | rdev->region.offset = offset; |
| 187 | rdev->region.size = size; |
| 188 | } |
| 189 | |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 190 | static void xlate_region_device_init(struct xlate_region_device *xdev, |
| 191 | const struct region_device_ops *ops, |
| 192 | const struct region_device *access_dev, |
| 193 | size_t sub_offset, size_t sub_size, |
| 194 | size_t parent_size) |
Furquan Shaikh | 2b57691 | 2016-06-19 23:16:45 -0700 | [diff] [blame] | 195 | { |
| 196 | memset(xdev, 0, sizeof(*xdev)); |
| 197 | xdev->access_dev = access_dev; |
| 198 | xdev->sub_region.offset = sub_offset; |
| 199 | xdev->sub_region.size = sub_size; |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 200 | region_device_init(&xdev->rdev, ops, 0, parent_size); |
| 201 | } |
| 202 | |
| 203 | void xlate_region_device_ro_init(struct xlate_region_device *xdev, |
| 204 | const struct region_device *access_dev, |
| 205 | size_t sub_offset, size_t sub_size, |
| 206 | size_t parent_size) |
| 207 | { |
| 208 | xlate_region_device_init(xdev, &xlate_rdev_ro_ops, access_dev, |
| 209 | sub_offset, sub_size, parent_size); |
| 210 | } |
| 211 | |
| 212 | void xlate_region_device_rw_init(struct xlate_region_device *xdev, |
| 213 | const struct region_device *access_dev, |
| 214 | size_t sub_offset, size_t sub_size, |
| 215 | size_t parent_size) |
| 216 | { |
| 217 | xlate_region_device_init(xdev, &xlate_rdev_rw_ops, access_dev, |
| 218 | sub_offset, sub_size, parent_size); |
Furquan Shaikh | 2b57691 | 2016-06-19 23:16:45 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 221 | static void *mdev_mmap(const struct region_device *rd, size_t offset, |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 222 | size_t size __unused) |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 223 | { |
| 224 | const struct mem_region_device *mdev; |
| 225 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 226 | mdev = container_of(rd, __typeof__(*mdev), rdev); |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 227 | |
| 228 | return &mdev->base[offset]; |
| 229 | } |
| 230 | |
Lee Leahy | 36d5b41 | 2017-03-10 11:02:11 -0800 | [diff] [blame] | 231 | static int mdev_munmap(const struct region_device *rd __unused, |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 232 | void *mapping __unused) |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 233 | { |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static ssize_t mdev_readat(const struct region_device *rd, void *b, |
| 238 | size_t offset, size_t size) |
| 239 | { |
| 240 | const struct mem_region_device *mdev; |
| 241 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 242 | mdev = container_of(rd, __typeof__(*mdev), rdev); |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 243 | |
| 244 | memcpy(b, &mdev->base[offset], size); |
| 245 | |
| 246 | return size; |
| 247 | } |
| 248 | |
Aaron Durbin | 258a350 | 2016-08-04 14:33:58 -0500 | [diff] [blame] | 249 | static ssize_t mdev_writeat(const struct region_device *rd, const void *b, |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 250 | size_t offset, size_t size) |
| 251 | { |
| 252 | const struct mem_region_device *mdev; |
| 253 | |
| 254 | mdev = container_of(rd, __typeof__(*mdev), rdev); |
| 255 | |
| 256 | memcpy(&mdev->base[offset], b, size); |
| 257 | |
| 258 | return size; |
| 259 | } |
| 260 | |
| 261 | static ssize_t mdev_eraseat(const struct region_device *rd, size_t offset, |
| 262 | size_t size) |
| 263 | { |
| 264 | const struct mem_region_device *mdev; |
| 265 | |
| 266 | mdev = container_of(rd, __typeof__(*mdev), rdev); |
| 267 | |
| 268 | memset(&mdev->base[offset], 0, size); |
| 269 | |
| 270 | return size; |
| 271 | } |
| 272 | |
| 273 | const struct region_device_ops mem_rdev_ro_ops = { |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 274 | .mmap = mdev_mmap, |
| 275 | .munmap = mdev_munmap, |
| 276 | .readat = mdev_readat, |
| 277 | }; |
Aaron Durbin | e62cf52 | 2015-03-27 01:58:06 -0500 | [diff] [blame] | 278 | |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 279 | const struct region_device_ops mem_rdev_rw_ops = { |
| 280 | .mmap = mdev_mmap, |
| 281 | .munmap = mdev_munmap, |
| 282 | .readat = mdev_readat, |
| 283 | .writeat = mdev_writeat, |
| 284 | .eraseat = mdev_eraseat, |
| 285 | }; |
| 286 | |
Aaron Durbin | e62cf52 | 2015-03-27 01:58:06 -0500 | [diff] [blame] | 287 | void mmap_helper_device_init(struct mmap_helper_region_device *mdev, |
| 288 | void *cache, size_t cache_size) |
| 289 | { |
| 290 | mem_pool_init(&mdev->pool, cache, cache_size); |
| 291 | } |
| 292 | |
| 293 | void *mmap_helper_rdev_mmap(const struct region_device *rd, size_t offset, |
| 294 | size_t size) |
| 295 | { |
| 296 | struct mmap_helper_region_device *mdev; |
| 297 | void *mapping; |
| 298 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 299 | mdev = container_of((void *)rd, __typeof__(*mdev), rdev); |
Aaron Durbin | e62cf52 | 2015-03-27 01:58:06 -0500 | [diff] [blame] | 300 | |
| 301 | mapping = mem_pool_alloc(&mdev->pool, size); |
| 302 | |
| 303 | if (mapping == NULL) |
| 304 | return NULL; |
| 305 | |
| 306 | if (rd->ops->readat(rd, mapping, offset, size) != size) { |
| 307 | mem_pool_free(&mdev->pool, mapping); |
| 308 | return NULL; |
| 309 | } |
| 310 | |
| 311 | return mapping; |
| 312 | } |
| 313 | |
| 314 | int mmap_helper_rdev_munmap(const struct region_device *rd, void *mapping) |
| 315 | { |
| 316 | struct mmap_helper_region_device *mdev; |
| 317 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 318 | mdev = container_of((void *)rd, __typeof__(*mdev), rdev); |
Aaron Durbin | e62cf52 | 2015-03-27 01:58:06 -0500 | [diff] [blame] | 319 | |
| 320 | mem_pool_free(&mdev->pool, mapping); |
| 321 | |
| 322 | return 0; |
| 323 | } |
Aaron Durbin | 5907eb8 | 2015-10-28 16:09:42 -0500 | [diff] [blame] | 324 | |
| 325 | static void *xlate_mmap(const struct region_device *rd, size_t offset, |
| 326 | size_t size) |
| 327 | { |
| 328 | const struct xlate_region_device *xldev; |
| 329 | struct region req = { |
| 330 | .offset = offset, |
| 331 | .size = size, |
| 332 | }; |
| 333 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 334 | xldev = container_of(rd, __typeof__(*xldev), rdev); |
Aaron Durbin | 5907eb8 | 2015-10-28 16:09:42 -0500 | [diff] [blame] | 335 | |
Aaron Durbin | 02103e3 | 2017-12-14 15:22:04 -0700 | [diff] [blame] | 336 | if (!region_is_subregion(&xldev->sub_region, &req)) |
Aaron Durbin | 5907eb8 | 2015-10-28 16:09:42 -0500 | [diff] [blame] | 337 | return NULL; |
| 338 | |
| 339 | offset -= region_offset(&xldev->sub_region); |
| 340 | |
| 341 | return rdev_mmap(xldev->access_dev, offset, size); |
| 342 | } |
| 343 | |
| 344 | static int xlate_munmap(const struct region_device *rd, void *mapping) |
| 345 | { |
| 346 | const struct xlate_region_device *xldev; |
| 347 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 348 | xldev = container_of(rd, __typeof__(*xldev), rdev); |
Aaron Durbin | 5907eb8 | 2015-10-28 16:09:42 -0500 | [diff] [blame] | 349 | |
| 350 | return rdev_munmap(xldev->access_dev, mapping); |
| 351 | } |
| 352 | |
| 353 | static ssize_t xlate_readat(const struct region_device *rd, void *b, |
| 354 | size_t offset, size_t size) |
| 355 | { |
| 356 | struct region req = { |
| 357 | .offset = offset, |
| 358 | .size = size, |
| 359 | }; |
| 360 | const struct xlate_region_device *xldev; |
| 361 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 362 | xldev = container_of(rd, __typeof__(*xldev), rdev); |
Aaron Durbin | 5907eb8 | 2015-10-28 16:09:42 -0500 | [diff] [blame] | 363 | |
Aaron Durbin | 02103e3 | 2017-12-14 15:22:04 -0700 | [diff] [blame] | 364 | if (!region_is_subregion(&xldev->sub_region, &req)) |
Aaron Durbin | 5907eb8 | 2015-10-28 16:09:42 -0500 | [diff] [blame] | 365 | return -1; |
| 366 | |
| 367 | offset -= region_offset(&xldev->sub_region); |
| 368 | |
| 369 | return rdev_readat(xldev->access_dev, b, offset, size); |
| 370 | } |
| 371 | |
Aaron Durbin | 258a350 | 2016-08-04 14:33:58 -0500 | [diff] [blame] | 372 | static ssize_t xlate_writeat(const struct region_device *rd, const void *b, |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 373 | size_t offset, size_t size) |
| 374 | { |
| 375 | struct region req = { |
| 376 | .offset = offset, |
| 377 | .size = size, |
| 378 | }; |
| 379 | const struct xlate_region_device *xldev; |
| 380 | |
| 381 | xldev = container_of(rd, __typeof__(*xldev), rdev); |
| 382 | |
Aaron Durbin | 02103e3 | 2017-12-14 15:22:04 -0700 | [diff] [blame] | 383 | if (!region_is_subregion(&xldev->sub_region, &req)) |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 384 | return -1; |
| 385 | |
| 386 | offset -= region_offset(&xldev->sub_region); |
| 387 | |
| 388 | return rdev_writeat(xldev->access_dev, b, offset, size); |
| 389 | } |
| 390 | |
| 391 | static ssize_t xlate_eraseat(const struct region_device *rd, |
| 392 | size_t offset, size_t size) |
| 393 | { |
| 394 | struct region req = { |
| 395 | .offset = offset, |
| 396 | .size = size, |
| 397 | }; |
| 398 | const struct xlate_region_device *xldev; |
| 399 | |
| 400 | xldev = container_of(rd, __typeof__(*xldev), rdev); |
| 401 | |
Aaron Durbin | 02103e3 | 2017-12-14 15:22:04 -0700 | [diff] [blame] | 402 | if (!region_is_subregion(&xldev->sub_region, &req)) |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 403 | return -1; |
| 404 | |
| 405 | offset -= region_offset(&xldev->sub_region); |
| 406 | |
| 407 | return rdev_eraseat(xldev->access_dev, offset, size); |
| 408 | } |
| 409 | |
| 410 | const struct region_device_ops xlate_rdev_ro_ops = { |
Aaron Durbin | 5907eb8 | 2015-10-28 16:09:42 -0500 | [diff] [blame] | 411 | .mmap = xlate_mmap, |
| 412 | .munmap = xlate_munmap, |
| 413 | .readat = xlate_readat, |
| 414 | }; |
Antonello Dettori | e5f48d2 | 2016-06-22 21:09:08 +0200 | [diff] [blame] | 415 | |
| 416 | const struct region_device_ops xlate_rdev_rw_ops = { |
| 417 | .mmap = xlate_mmap, |
| 418 | .munmap = xlate_munmap, |
| 419 | .readat = xlate_readat, |
| 420 | .writeat = xlate_writeat, |
| 421 | .eraseat = xlate_eraseat, |
| 422 | }; |
Aaron Durbin | 256db40 | 2016-12-03 17:08:08 -0600 | [diff] [blame] | 423 | |
| 424 | |
| 425 | static void *incoherent_mmap(const struct region_device *rd, size_t offset, |
| 426 | size_t size) |
| 427 | { |
| 428 | const struct incoherent_rdev *irdev; |
| 429 | |
| 430 | irdev = container_of(rd, const struct incoherent_rdev, rdev); |
| 431 | |
| 432 | return rdev_mmap(irdev->read, offset, size); |
| 433 | } |
| 434 | |
| 435 | static int incoherent_munmap(const struct region_device *rd, void *mapping) |
| 436 | { |
| 437 | const struct incoherent_rdev *irdev; |
| 438 | |
| 439 | irdev = container_of(rd, const struct incoherent_rdev, rdev); |
| 440 | |
| 441 | return rdev_munmap(irdev->read, mapping); |
| 442 | } |
| 443 | |
| 444 | static ssize_t incoherent_readat(const struct region_device *rd, void *b, |
| 445 | size_t offset, size_t size) |
| 446 | { |
| 447 | const struct incoherent_rdev *irdev; |
| 448 | |
| 449 | irdev = container_of(rd, const struct incoherent_rdev, rdev); |
| 450 | |
| 451 | return rdev_readat(irdev->read, b, offset, size); |
| 452 | } |
| 453 | |
| 454 | static ssize_t incoherent_writeat(const struct region_device *rd, const void *b, |
| 455 | size_t offset, size_t size) |
| 456 | { |
| 457 | const struct incoherent_rdev *irdev; |
| 458 | |
| 459 | irdev = container_of(rd, const struct incoherent_rdev, rdev); |
| 460 | |
| 461 | return rdev_writeat(irdev->write, b, offset, size); |
| 462 | } |
| 463 | |
| 464 | static ssize_t incoherent_eraseat(const struct region_device *rd, size_t offset, |
| 465 | size_t size) |
| 466 | { |
| 467 | const struct incoherent_rdev *irdev; |
| 468 | |
| 469 | irdev = container_of(rd, const struct incoherent_rdev, rdev); |
| 470 | |
| 471 | return rdev_eraseat(irdev->write, offset, size); |
| 472 | } |
| 473 | |
| 474 | static const struct region_device_ops incoherent_rdev_ops = { |
| 475 | .mmap = incoherent_mmap, |
| 476 | .munmap = incoherent_munmap, |
| 477 | .readat = incoherent_readat, |
| 478 | .writeat = incoherent_writeat, |
| 479 | .eraseat = incoherent_eraseat, |
| 480 | }; |
| 481 | |
| 482 | const struct region_device *incoherent_rdev_init(struct incoherent_rdev *irdev, |
| 483 | const struct region *r, |
| 484 | const struct region_device *read, |
| 485 | const struct region_device *write) |
| 486 | { |
| 487 | const size_t size = region_sz(r); |
| 488 | |
| 489 | if (size != region_device_sz(read) || size != region_device_sz(write)) |
| 490 | return NULL; |
| 491 | |
| 492 | /* The region is represented as offset 0 to size. That way, the generic |
| 493 | * rdev operations can be called on the read or write implementation |
| 494 | * without any unnecessary translation because the offsets all start |
| 495 | * at 0. */ |
| 496 | region_device_init(&irdev->rdev, &incoherent_rdev_ops, 0, size); |
| 497 | irdev->read = read; |
| 498 | irdev->write = write; |
| 499 | |
| 500 | return &irdev->rdev; |
| 501 | } |