2 * Copyright (c) 2016 Landon Fuller <landonf@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/malloc.h>
38 #include <machine/bus.h>
40 #include <dev/bhnd/bhnd.h>
42 #include "bhnd_nvram_private.h"
44 #include "bhnd_nvram_io.h"
45 #include "bhnd_nvram_iovar.h"
48 * BHND resource-backed NVRAM I/O context.
50 struct bhnd_nvram_iores {
51 struct bhnd_nvram_io io; /**< common I/O instance state */
52 struct bhnd_resource *res; /**< backing resource (borrowed ref) */
53 size_t offset; /**< offset within res */
54 size_t size; /**< size relative to the base offset */
55 u_int bus_width; /**< data type byte width to be used
56 when performing bus operations
57 on res. (1, 2, or 4 bytes) */
60 BHND_NVRAM_IOPS_DEFN(iores);
63 * Allocate and return a new I/O context backed by a borrowed reference to @p r.
65 * The caller is responsible for deallocating the returned I/O context via
66 * bhnd_nvram_io_free().
68 * @param r The resource to be mapped by the returned I/O
70 * @param offset Offset
71 * @param bus_width The required I/O width (1, 2, or 4 bytes) to be
72 * used when reading from @p r.
74 * @retval bhnd_nvram_io success.
75 * @retval NULL if allocation fails, or an invalid argument
78 struct bhnd_nvram_io *
79 bhnd_nvram_iores_new(struct bhnd_resource *r, bus_size_t offset,
80 bus_size_t size, u_int bus_width)
82 struct bhnd_nvram_iores *iores;
83 rman_res_t r_start, r_size;
85 /* Verify the bus width */
93 BHND_NV_LOG("invalid bus width %u\n", bus_width);
97 /* offset/size must not exceed our internal size_t representation,
98 * or our bus_size_t usage (note that BUS_SPACE_MAXSIZE may be less
99 * than 2^(sizeof(bus_size_t) * 32). */
100 if (size > SIZE_MAX || offset > SIZE_MAX) {
101 BHND_NV_LOG("offset %#jx+%#jx exceeds SIZE_MAX\n",
102 (uintmax_t)offset, (uintmax_t)offset);
106 if (size > BUS_SPACE_MAXSIZE || offset > BUS_SPACE_MAXSIZE)
108 BHND_NV_LOG("offset %#jx+%#jx exceeds BUS_SPACE_MAXSIZE\n",
109 (uintmax_t)offset, (uintmax_t)offset);
113 /* offset/size fall within the resource's mapped range */
114 r_size = rman_get_size(r->res);
115 r_start = rman_get_start(r->res);
116 if (r_size < offset || r_size < size || r_size - size < offset)
119 /* offset/size must be bus_width aligned */
120 if ((r_start + offset) % bus_width != 0) {
121 BHND_NV_LOG("base address %#jx+%#jx not aligned to bus width "
122 "%u\n", (uintmax_t)r_start, (uintmax_t)offset, bus_width);
126 if (size % bus_width != 0) {
127 BHND_NV_LOG("size %#jx not aligned to bus width %u\n",
128 (uintmax_t)size, bus_width);
132 /* Allocate and return the I/O context */
133 iores = malloc(sizeof(*iores), M_BHND_NVRAM, M_WAITOK);
134 iores->io.iops = &bhnd_nvram_iores_ops;
136 iores->offset = offset;
138 iores->bus_width = bus_width;
144 bhnd_nvram_iores_free(struct bhnd_nvram_io *io)
146 free(io, M_BHND_NVRAM);
150 bhnd_nvram_iores_getsize(struct bhnd_nvram_io *io)
152 struct bhnd_nvram_iores *iores = (struct bhnd_nvram_iores *)io;
153 return (iores->size);
157 bhnd_nvram_iores_setsize(struct bhnd_nvram_io *io, size_t size)
164 bhnd_nvram_iores_read_ptr(struct bhnd_nvram_io *io, size_t offset,
165 const void **ptr, size_t nbytes, size_t *navail)
172 bhnd_nvram_iores_write_ptr(struct bhnd_nvram_io *io, size_t offset,
173 void **ptr, size_t nbytes, size_t *navail)
180 * Validate @p offset and @p nbytes:
182 * - Verify that @p offset is mapped by the backing resource.
183 * - If less than @p nbytes are available at @p offset, write the actual number
184 * of bytes available to @p nbytes.
185 * - Verify that @p offset + @p nbytes are correctly aligned.
188 bhnd_nvram_iores_validate_req(struct bhnd_nvram_iores *iores, size_t offset,
191 /* Verify offset falls within the resource range */
192 if (offset > iores->size)
196 if (offset == iores->size) {
201 /* Verify offset alignment */
202 if (offset % iores->bus_width != 0)
205 /* Limit nbytes to available range and verify size alignment */
206 *nbytes = ummin(*nbytes, iores->size - offset);
207 if (*nbytes < iores->bus_width && *nbytes % iores->bus_width != 0)
215 bhnd_nvram_iores_read(struct bhnd_nvram_io *io, size_t offset, void *buffer,
218 struct bhnd_nvram_iores *iores;
223 iores = (struct bhnd_nvram_iores *)io;
225 /* Validate the request and determine the actual number of readable
228 if ((error = bhnd_nvram_iores_validate_req(iores, offset, &navail)))
231 /* At least nbytes must be readable */
235 /* Handle zero length read */
239 /* Determine actual resource offset and perform the read */
240 r_offset = iores->offset + offset;
241 switch (iores->bus_width) {
243 bhnd_bus_read_region_stream_1(iores->res, r_offset, buffer,
247 bhnd_bus_read_region_stream_2(iores->res, r_offset, buffer,
251 bhnd_bus_read_region_stream_4(iores->res, r_offset, buffer,
255 panic("unreachable!");
262 bhnd_nvram_iores_write(struct bhnd_nvram_io *io, size_t offset,
263 void *buffer, size_t nbytes)
265 struct bhnd_nvram_iores *iores;
270 iores = (struct bhnd_nvram_iores *)io;
272 /* Validate the request and determine the actual number of writable
275 if ((error = bhnd_nvram_iores_validate_req(iores, offset, &navail)))
278 /* At least nbytes must be writable */
282 /* Determine actual resource offset and perform the write */
283 r_offset = iores->offset + offset;
284 switch (iores->bus_width) {
286 bhnd_bus_write_region_stream_1(iores->res, r_offset, buffer,
290 bhnd_bus_write_region_stream_2(iores->res, r_offset, buffer,
294 bhnd_bus_write_region_stream_4(iores->res, r_offset, buffer,
298 panic("unreachable!");