blob: 6f710746ca3685cccf4e12c6b7417fda445d73e9 [file] [log] [blame]
Aaron Durbin5d5f4b32015-03-26 14:39:07 -05001/*
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 Durbin5d5f4b32015-03-26 14:39:07 -050014 */
15
Aaron Durbinca0a6762015-12-15 17:49:12 -060016#include <commonlib/helpers.h>
Aaron Durbindc9f5cd2015-09-08 13:34:43 -050017#include <commonlib/region.h>
Aaron Durbin5d5f4b32015-03-26 14:39:07 -050018#include <string.h>
19
Aaron Durbin5d5f4b32015-03-26 14:39:07 -050020static inline size_t region_end(const struct region *r)
21{
22 return region_sz(r) + region_offset(r);
23}
24
25static 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
39static 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
45static 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 Durbin990ab7e2015-12-15 13:29:41 -060052ssize_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 Durbin5d5f4b32015-03-26 14:39:07 -050064void *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
80int 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
89ssize_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
106int 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 Durbinb419c1a82015-03-27 01:03:45 -0500126
127void 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 Shaikh2b576912016-06-19 23:16:45 -0700136void 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
147void 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 Durbinb419c1a82015-03-27 01:03:45 -0500159static void *mdev_mmap(const struct region_device *rd, size_t offset,
Aaron Durbinca0a6762015-12-15 17:49:12 -0600160 size_t size __unused)
Aaron Durbinb419c1a82015-03-27 01:03:45 -0500161{
162 const struct mem_region_device *mdev;
163
Aaron Durbinca0a6762015-12-15 17:49:12 -0600164 mdev = container_of(rd, __typeof__(*mdev), rdev);
Aaron Durbinb419c1a82015-03-27 01:03:45 -0500165
166 return &mdev->base[offset];
167}
168
Aaron Durbinca0a6762015-12-15 17:49:12 -0600169static int mdev_munmap(const struct region_device * rd __unused,
170 void *mapping __unused)
Aaron Durbinb419c1a82015-03-27 01:03:45 -0500171{
172 return 0;
173}
174
175static 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 Durbinca0a6762015-12-15 17:49:12 -0600180 mdev = container_of(rd, __typeof__(*mdev), rdev);
Aaron Durbinb419c1a82015-03-27 01:03:45 -0500181
182 memcpy(b, &mdev->base[offset], size);
183
184 return size;
185}
186
187const struct region_device_ops mem_rdev_ops = {
188 .mmap = mdev_mmap,
189 .munmap = mdev_munmap,
190 .readat = mdev_readat,
191};
Aaron Durbine62cf522015-03-27 01:58:06 -0500192
193void 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
199void *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 Durbinca0a6762015-12-15 17:49:12 -0600205 mdev = container_of((void *)rd, __typeof__(*mdev), rdev);
Aaron Durbine62cf522015-03-27 01:58:06 -0500206
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
220int mmap_helper_rdev_munmap(const struct region_device *rd, void *mapping)
221{
222 struct mmap_helper_region_device *mdev;
223
Aaron Durbinca0a6762015-12-15 17:49:12 -0600224 mdev = container_of((void *)rd, __typeof__(*mdev), rdev);
Aaron Durbine62cf522015-03-27 01:58:06 -0500225
226 mem_pool_free(&mdev->pool, mapping);
227
228 return 0;
229}
Aaron Durbin5907eb82015-10-28 16:09:42 -0500230
231static 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 Durbinca0a6762015-12-15 17:49:12 -0600240 xldev = container_of(rd, __typeof__(*xldev), rdev);
Aaron Durbin5907eb82015-10-28 16:09:42 -0500241
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
250static int xlate_munmap(const struct region_device *rd, void *mapping)
251{
252 const struct xlate_region_device *xldev;
253
Aaron Durbinca0a6762015-12-15 17:49:12 -0600254 xldev = container_of(rd, __typeof__(*xldev), rdev);
Aaron Durbin5907eb82015-10-28 16:09:42 -0500255
256 return rdev_munmap(xldev->access_dev, mapping);
257}
258
259static 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 Durbinca0a6762015-12-15 17:49:12 -0600268 xldev = container_of(rd, __typeof__(*xldev), rdev);
Aaron Durbin5907eb82015-10-28 16:09:42 -0500269
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
278const struct region_device_ops xlate_rdev_ops = {
279 .mmap = xlate_mmap,
280 .munmap = xlate_munmap,
281 .readat = xlate_readat,
282};