Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2015 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 14 | */ |
| 15 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 16 | #include <commonlib/helpers.h> |
Aaron Durbin | dc9f5cd | 2015-09-08 13:34:43 -0500 | [diff] [blame] | 17 | #include <commonlib/region.h> |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 18 | #include <string.h> |
| 19 | |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 20 | static inline size_t region_end(const struct region *r) |
| 21 | { |
| 22 | return region_sz(r) + region_offset(r); |
| 23 | } |
| 24 | |
| 25 | static int is_subregion(const struct region *p, const struct region *c) |
| 26 | { |
| 27 | if (region_offset(c) < region_offset(p)) |
| 28 | return 0; |
| 29 | |
| 30 | if (region_sz(c) > region_sz(p)) |
| 31 | return 0; |
| 32 | |
| 33 | if (region_end(c) > region_end(p)) |
| 34 | return 0; |
| 35 | |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | static int normalize_and_ok(const struct region *outer, struct region *inner) |
| 40 | { |
| 41 | inner->offset += region_offset(outer); |
| 42 | return is_subregion(outer, inner); |
| 43 | } |
| 44 | |
| 45 | static const struct region_device *rdev_root(const struct region_device *rdev) |
| 46 | { |
| 47 | if (rdev->root == NULL) |
| 48 | return rdev; |
| 49 | return rdev->root; |
| 50 | } |
| 51 | |
Aaron Durbin | 990ab7e | 2015-12-15 13:29:41 -0600 | [diff] [blame] | 52 | ssize_t rdev_relative_offset(const struct region_device *p, |
| 53 | const struct region_device *c) |
| 54 | { |
| 55 | if (rdev_root(p) != rdev_root(c)) |
| 56 | return -1; |
| 57 | |
| 58 | if (!is_subregion(&p->region, &c->region)) |
| 59 | return -1; |
| 60 | |
| 61 | return region_device_offset(c) - region_device_offset(p); |
| 62 | } |
| 63 | |
Aaron Durbin | 5d5f4b3 | 2015-03-26 14:39:07 -0500 | [diff] [blame] | 64 | void *rdev_mmap(const struct region_device *rd, size_t offset, size_t size) |
| 65 | { |
| 66 | const struct region_device *rdev; |
| 67 | struct region req = { |
| 68 | .offset = offset, |
| 69 | .size = size, |
| 70 | }; |
| 71 | |
| 72 | if (!normalize_and_ok(&rd->region, &req)) |
| 73 | return NULL; |
| 74 | |
| 75 | rdev = rdev_root(rd); |
| 76 | |
| 77 | return rdev->ops->mmap(rdev, req.offset, req.size); |
| 78 | } |
| 79 | |
| 80 | int rdev_munmap(const struct region_device *rd, void *mapping) |
| 81 | { |
| 82 | const struct region_device *rdev; |
| 83 | |
| 84 | rdev = rdev_root(rd); |
| 85 | |
| 86 | return rdev->ops->munmap(rdev, mapping); |
| 87 | } |
| 88 | |
| 89 | ssize_t rdev_readat(const struct region_device *rd, void *b, size_t offset, |
| 90 | size_t size) |
| 91 | { |
| 92 | const struct region_device *rdev; |
| 93 | struct region req = { |
| 94 | .offset = offset, |
| 95 | .size = size, |
| 96 | }; |
| 97 | |
| 98 | if (!normalize_and_ok(&rd->region, &req)) |
| 99 | return -1; |
| 100 | |
| 101 | rdev = rdev_root(rd); |
| 102 | |
| 103 | return rdev->ops->readat(rdev, b, req.offset, req.size); |
| 104 | } |
| 105 | |
| 106 | int rdev_chain(struct region_device *child, const struct region_device *parent, |
| 107 | size_t offset, size_t size) |
| 108 | { |
| 109 | struct region req = { |
| 110 | .offset = offset, |
| 111 | .size = size, |
| 112 | }; |
| 113 | |
| 114 | if (!normalize_and_ok(&parent->region, &req)) |
| 115 | return -1; |
| 116 | |
| 117 | /* Keep track of root region device. Note the offsets are relative |
| 118 | * to the root device. */ |
| 119 | child->root = rdev_root(parent); |
| 120 | child->ops = NULL; |
| 121 | child->region.offset = req.offset; |
| 122 | child->region.size = req.size; |
| 123 | |
| 124 | return 0; |
| 125 | } |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 126 | |
| 127 | void mem_region_device_init(struct mem_region_device *mdev, void *base, |
| 128 | size_t size) |
| 129 | { |
| 130 | memset(mdev, 0, sizeof(*mdev)); |
| 131 | mdev->base = base; |
| 132 | mdev->rdev.ops = &mem_rdev_ops; |
| 133 | mdev->rdev.region.size = size; |
| 134 | } |
| 135 | |
Furquan Shaikh | 2b57691 | 2016-06-19 23:16:45 -0700 | [diff] [blame^] | 136 | void region_device_init(struct region_device *rdev, |
| 137 | const struct region_device_ops *ops, size_t offset, |
| 138 | size_t size) |
| 139 | { |
| 140 | memset(rdev, 0, sizeof(*rdev)); |
| 141 | rdev->root = NULL; |
| 142 | rdev->ops = ops; |
| 143 | rdev->region.offset = offset; |
| 144 | rdev->region.size = size; |
| 145 | } |
| 146 | |
| 147 | void xlate_region_device_init(struct xlate_region_device *xdev, |
| 148 | const struct region_device *access_dev, |
| 149 | size_t sub_offset, size_t sub_size, |
| 150 | size_t parent_size) |
| 151 | { |
| 152 | memset(xdev, 0, sizeof(*xdev)); |
| 153 | xdev->access_dev = access_dev; |
| 154 | xdev->sub_region.offset = sub_offset; |
| 155 | xdev->sub_region.size = sub_size; |
| 156 | region_device_init(&xdev->rdev, &xlate_rdev_ops, 0, parent_size); |
| 157 | } |
| 158 | |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 159 | static void *mdev_mmap(const struct region_device *rd, size_t offset, |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 160 | size_t size __unused) |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 161 | { |
| 162 | const struct mem_region_device *mdev; |
| 163 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 164 | mdev = container_of(rd, __typeof__(*mdev), rdev); |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 165 | |
| 166 | return &mdev->base[offset]; |
| 167 | } |
| 168 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 169 | static int mdev_munmap(const struct region_device * rd __unused, |
| 170 | void *mapping __unused) |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 171 | { |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static ssize_t mdev_readat(const struct region_device *rd, void *b, |
| 176 | size_t offset, size_t size) |
| 177 | { |
| 178 | const struct mem_region_device *mdev; |
| 179 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 180 | mdev = container_of(rd, __typeof__(*mdev), rdev); |
Aaron Durbin | b419c1a8 | 2015-03-27 01:03:45 -0500 | [diff] [blame] | 181 | |
| 182 | memcpy(b, &mdev->base[offset], size); |
| 183 | |
| 184 | return size; |
| 185 | } |
| 186 | |
| 187 | const struct region_device_ops mem_rdev_ops = { |
| 188 | .mmap = mdev_mmap, |
| 189 | .munmap = mdev_munmap, |
| 190 | .readat = mdev_readat, |
| 191 | }; |
Aaron Durbin | e62cf52 | 2015-03-27 01:58:06 -0500 | [diff] [blame] | 192 | |
| 193 | void mmap_helper_device_init(struct mmap_helper_region_device *mdev, |
| 194 | void *cache, size_t cache_size) |
| 195 | { |
| 196 | mem_pool_init(&mdev->pool, cache, cache_size); |
| 197 | } |
| 198 | |
| 199 | void *mmap_helper_rdev_mmap(const struct region_device *rd, size_t offset, |
| 200 | size_t size) |
| 201 | { |
| 202 | struct mmap_helper_region_device *mdev; |
| 203 | void *mapping; |
| 204 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 205 | mdev = container_of((void *)rd, __typeof__(*mdev), rdev); |
Aaron Durbin | e62cf52 | 2015-03-27 01:58:06 -0500 | [diff] [blame] | 206 | |
| 207 | mapping = mem_pool_alloc(&mdev->pool, size); |
| 208 | |
| 209 | if (mapping == NULL) |
| 210 | return NULL; |
| 211 | |
| 212 | if (rd->ops->readat(rd, mapping, offset, size) != size) { |
| 213 | mem_pool_free(&mdev->pool, mapping); |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | return mapping; |
| 218 | } |
| 219 | |
| 220 | int mmap_helper_rdev_munmap(const struct region_device *rd, void *mapping) |
| 221 | { |
| 222 | struct mmap_helper_region_device *mdev; |
| 223 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 224 | mdev = container_of((void *)rd, __typeof__(*mdev), rdev); |
Aaron Durbin | e62cf52 | 2015-03-27 01:58:06 -0500 | [diff] [blame] | 225 | |
| 226 | mem_pool_free(&mdev->pool, mapping); |
| 227 | |
| 228 | return 0; |
| 229 | } |
Aaron Durbin | 5907eb8 | 2015-10-28 16:09:42 -0500 | [diff] [blame] | 230 | |
| 231 | static void *xlate_mmap(const struct region_device *rd, size_t offset, |
| 232 | size_t size) |
| 233 | { |
| 234 | const struct xlate_region_device *xldev; |
| 235 | struct region req = { |
| 236 | .offset = offset, |
| 237 | .size = size, |
| 238 | }; |
| 239 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 240 | xldev = container_of(rd, __typeof__(*xldev), rdev); |
Aaron Durbin | 5907eb8 | 2015-10-28 16:09:42 -0500 | [diff] [blame] | 241 | |
| 242 | if (!is_subregion(&xldev->sub_region, &req)) |
| 243 | return NULL; |
| 244 | |
| 245 | offset -= region_offset(&xldev->sub_region); |
| 246 | |
| 247 | return rdev_mmap(xldev->access_dev, offset, size); |
| 248 | } |
| 249 | |
| 250 | static int xlate_munmap(const struct region_device *rd, void *mapping) |
| 251 | { |
| 252 | const struct xlate_region_device *xldev; |
| 253 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 254 | xldev = container_of(rd, __typeof__(*xldev), rdev); |
Aaron Durbin | 5907eb8 | 2015-10-28 16:09:42 -0500 | [diff] [blame] | 255 | |
| 256 | return rdev_munmap(xldev->access_dev, mapping); |
| 257 | } |
| 258 | |
| 259 | static ssize_t xlate_readat(const struct region_device *rd, void *b, |
| 260 | size_t offset, size_t size) |
| 261 | { |
| 262 | struct region req = { |
| 263 | .offset = offset, |
| 264 | .size = size, |
| 265 | }; |
| 266 | const struct xlate_region_device *xldev; |
| 267 | |
Aaron Durbin | ca0a676 | 2015-12-15 17:49:12 -0600 | [diff] [blame] | 268 | xldev = container_of(rd, __typeof__(*xldev), rdev); |
Aaron Durbin | 5907eb8 | 2015-10-28 16:09:42 -0500 | [diff] [blame] | 269 | |
| 270 | if (!is_subregion(&xldev->sub_region, &req)) |
| 271 | return -1; |
| 272 | |
| 273 | offset -= region_offset(&xldev->sub_region); |
| 274 | |
| 275 | return rdev_readat(xldev->access_dev, b, offset, size); |
| 276 | } |
| 277 | |
| 278 | const struct region_device_ops xlate_rdev_ops = { |
| 279 | .mmap = xlate_mmap, |
| 280 | .munmap = xlate_munmap, |
| 281 | .readat = xlate_readat, |
| 282 | }; |