blob: bfc5fc8471dbf566ba63b6445f4e0ec183a4a132 [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
136static void *mdev_mmap(const struct region_device *rd, size_t offset,
Aaron Durbinca0a6762015-12-15 17:49:12 -0600137 size_t size __unused)
Aaron Durbinb419c1a82015-03-27 01:03:45 -0500138{
139 const struct mem_region_device *mdev;
140
Aaron Durbinca0a6762015-12-15 17:49:12 -0600141 mdev = container_of(rd, __typeof__(*mdev), rdev);
Aaron Durbinb419c1a82015-03-27 01:03:45 -0500142
143 return &mdev->base[offset];
144}
145
Aaron Durbinca0a6762015-12-15 17:49:12 -0600146static int mdev_munmap(const struct region_device * rd __unused,
147 void *mapping __unused)
Aaron Durbinb419c1a82015-03-27 01:03:45 -0500148{
149 return 0;
150}
151
152static ssize_t mdev_readat(const struct region_device *rd, void *b,
153 size_t offset, size_t size)
154{
155 const struct mem_region_device *mdev;
156
Aaron Durbinca0a6762015-12-15 17:49:12 -0600157 mdev = container_of(rd, __typeof__(*mdev), rdev);
Aaron Durbinb419c1a82015-03-27 01:03:45 -0500158
159 memcpy(b, &mdev->base[offset], size);
160
161 return size;
162}
163
164const struct region_device_ops mem_rdev_ops = {
165 .mmap = mdev_mmap,
166 .munmap = mdev_munmap,
167 .readat = mdev_readat,
168};
Aaron Durbine62cf522015-03-27 01:58:06 -0500169
170void mmap_helper_device_init(struct mmap_helper_region_device *mdev,
171 void *cache, size_t cache_size)
172{
173 mem_pool_init(&mdev->pool, cache, cache_size);
174}
175
176void *mmap_helper_rdev_mmap(const struct region_device *rd, size_t offset,
177 size_t size)
178{
179 struct mmap_helper_region_device *mdev;
180 void *mapping;
181
Aaron Durbinca0a6762015-12-15 17:49:12 -0600182 mdev = container_of((void *)rd, __typeof__(*mdev), rdev);
Aaron Durbine62cf522015-03-27 01:58:06 -0500183
184 mapping = mem_pool_alloc(&mdev->pool, size);
185
186 if (mapping == NULL)
187 return NULL;
188
189 if (rd->ops->readat(rd, mapping, offset, size) != size) {
190 mem_pool_free(&mdev->pool, mapping);
191 return NULL;
192 }
193
194 return mapping;
195}
196
197int mmap_helper_rdev_munmap(const struct region_device *rd, void *mapping)
198{
199 struct mmap_helper_region_device *mdev;
200
Aaron Durbinca0a6762015-12-15 17:49:12 -0600201 mdev = container_of((void *)rd, __typeof__(*mdev), rdev);
Aaron Durbine62cf522015-03-27 01:58:06 -0500202
203 mem_pool_free(&mdev->pool, mapping);
204
205 return 0;
206}
Aaron Durbin5907eb82015-10-28 16:09:42 -0500207
208static void *xlate_mmap(const struct region_device *rd, size_t offset,
209 size_t size)
210{
211 const struct xlate_region_device *xldev;
212 struct region req = {
213 .offset = offset,
214 .size = size,
215 };
216
Aaron Durbinca0a6762015-12-15 17:49:12 -0600217 xldev = container_of(rd, __typeof__(*xldev), rdev);
Aaron Durbin5907eb82015-10-28 16:09:42 -0500218
219 if (!is_subregion(&xldev->sub_region, &req))
220 return NULL;
221
222 offset -= region_offset(&xldev->sub_region);
223
224 return rdev_mmap(xldev->access_dev, offset, size);
225}
226
227static int xlate_munmap(const struct region_device *rd, void *mapping)
228{
229 const struct xlate_region_device *xldev;
230
Aaron Durbinca0a6762015-12-15 17:49:12 -0600231 xldev = container_of(rd, __typeof__(*xldev), rdev);
Aaron Durbin5907eb82015-10-28 16:09:42 -0500232
233 return rdev_munmap(xldev->access_dev, mapping);
234}
235
236static ssize_t xlate_readat(const struct region_device *rd, void *b,
237 size_t offset, size_t size)
238{
239 struct region req = {
240 .offset = offset,
241 .size = size,
242 };
243 const struct xlate_region_device *xldev;
244
Aaron Durbinca0a6762015-12-15 17:49:12 -0600245 xldev = container_of(rd, __typeof__(*xldev), rdev);
Aaron Durbin5907eb82015-10-28 16:09:42 -0500246
247 if (!is_subregion(&xldev->sub_region, &req))
248 return -1;
249
250 offset -= region_offset(&xldev->sub_region);
251
252 return rdev_readat(xldev->access_dev, b, offset, size);
253}
254
255const struct region_device_ops xlate_rdev_ops = {
256 .mmap = xlate_mmap,
257 .munmap = xlate_munmap,
258 .readat = xlate_readat,
259};