shmif_busops.c revision 1.12 1 1.12 ozaki /* $NetBSD: shmif_busops.c,v 1.12 2014/09/17 04:20:58 ozaki-r Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.9 pooka * Copyright (c) 2009, 2010 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Development of this software was supported by The Nokia Foundation.
7 1.1 pooka *
8 1.1 pooka * Redistribution and use in source and binary forms, with or without
9 1.1 pooka * modification, are permitted provided that the following conditions
10 1.1 pooka * are met:
11 1.1 pooka * 1. Redistributions of source code must retain the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer.
13 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer in the
15 1.1 pooka * documentation and/or other materials provided with the distribution.
16 1.1 pooka *
17 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 pooka * SUCH DAMAGE.
28 1.1 pooka */
29 1.1 pooka
30 1.11 pooka #ifdef _KERNEL
31 1.1 pooka #include <sys/cdefs.h>
32 1.12 ozaki __KERNEL_RCSID(0, "$NetBSD: shmif_busops.c,v 1.12 2014/09/17 04:20:58 ozaki-r Exp $");
33 1.11 pooka #else
34 1.11 pooka #include <rump/rumpuser_port.h>
35 1.12 ozaki __RCSID("$NetBSD: shmif_busops.c,v 1.12 2014/09/17 04:20:58 ozaki-r Exp $");
36 1.11 pooka #endif
37 1.1 pooka
38 1.1 pooka #include <sys/param.h>
39 1.1 pooka
40 1.1 pooka #ifndef _KERNEL
41 1.1 pooka #include <assert.h>
42 1.10 pooka #include <inttypes.h>
43 1.8 pooka #include <stdbool.h>
44 1.8 pooka #include <string.h>
45 1.8 pooka
46 1.1 pooka #define KASSERT(a) assert(a)
47 1.1 pooka #endif
48 1.1 pooka
49 1.8 pooka #include "shmifvar.h"
50 1.8 pooka
51 1.1 pooka uint32_t
52 1.1 pooka shmif_advance(uint32_t oldoff, uint32_t delta)
53 1.1 pooka {
54 1.1 pooka uint32_t newoff;
55 1.1 pooka
56 1.1 pooka newoff = oldoff + delta;
57 1.1 pooka if (newoff >= BUSMEM_DATASIZE)
58 1.5 pooka newoff -= BUSMEM_DATASIZE;
59 1.1 pooka return newoff;
60 1.1 pooka }
61 1.1 pooka
62 1.1 pooka uint32_t
63 1.1 pooka shmif_busread(struct shmif_mem *busmem, void *dest, uint32_t off, size_t len,
64 1.1 pooka bool *wrap)
65 1.1 pooka {
66 1.1 pooka size_t chunk;
67 1.1 pooka
68 1.5 pooka KASSERT(len < (BUSMEM_DATASIZE/2) && off <= BUSMEM_DATASIZE);
69 1.1 pooka chunk = MIN(len, BUSMEM_DATASIZE - off);
70 1.1 pooka memcpy(dest, busmem->shm_data + off, chunk);
71 1.1 pooka len -= chunk;
72 1.1 pooka
73 1.6 pooka if (off + chunk == BUSMEM_DATASIZE)
74 1.6 pooka *wrap = true;
75 1.1 pooka
76 1.6 pooka if (len == 0) {
77 1.6 pooka return (off + chunk) % BUSMEM_DATASIZE;
78 1.6 pooka }
79 1.1 pooka
80 1.1 pooka /* finish reading */
81 1.5 pooka memcpy((uint8_t *)dest + chunk, busmem->shm_data, len);
82 1.5 pooka return len;
83 1.1 pooka }
84 1.1 pooka
85 1.1 pooka void
86 1.1 pooka shmif_advancefirst(struct shmif_mem *busmem, uint32_t off, size_t len)
87 1.1 pooka {
88 1.1 pooka
89 1.2 pooka while (off <= busmem->shm_first + sizeof(struct shmif_pkthdr)
90 1.1 pooka && off+len > busmem->shm_first) {
91 1.1 pooka DPRINTF(("advancefirst: old offset %d, ", busmem->shm_first));
92 1.1 pooka busmem->shm_first = shmif_nextpktoff(busmem, busmem->shm_first);
93 1.1 pooka DPRINTF(("new offset: %d\n", busmem->shm_first));
94 1.1 pooka }
95 1.1 pooka }
96 1.1 pooka
97 1.1 pooka uint32_t
98 1.1 pooka shmif_buswrite(struct shmif_mem *busmem, uint32_t off, void *data, size_t len,
99 1.1 pooka bool *wrap)
100 1.1 pooka {
101 1.1 pooka size_t chunk;
102 1.7 pooka bool filledbus;
103 1.1 pooka
104 1.5 pooka KASSERT(len < (BUSMEM_DATASIZE/2) && off <= BUSMEM_DATASIZE);
105 1.1 pooka
106 1.1 pooka chunk = MIN(len, BUSMEM_DATASIZE - off);
107 1.1 pooka len -= chunk;
108 1.7 pooka filledbus = (off+chunk == BUSMEM_DATASIZE);
109 1.1 pooka
110 1.7 pooka shmif_advancefirst(busmem, off, chunk + (filledbus ? 1 : 0));
111 1.1 pooka
112 1.1 pooka memcpy(busmem->shm_data + off, data, chunk);
113 1.1 pooka
114 1.12 ozaki DPRINTF(("buswrite: wrote %zu bytes to %d", chunk, off));
115 1.1 pooka
116 1.7 pooka if (filledbus) {
117 1.6 pooka *wrap = true;
118 1.7 pooka }
119 1.6 pooka
120 1.1 pooka if (len == 0) {
121 1.1 pooka DPRINTF(("\n"));
122 1.6 pooka return (off + chunk) % BUSMEM_DATASIZE;
123 1.1 pooka }
124 1.1 pooka
125 1.12 ozaki DPRINTF((", wrapped bytes %zu to 0\n", len));
126 1.1 pooka
127 1.6 pooka shmif_advancefirst(busmem, 0, len);
128 1.1 pooka
129 1.1 pooka /* finish writing */
130 1.6 pooka memcpy(busmem->shm_data, (uint8_t *)data + chunk, len);
131 1.6 pooka return len;
132 1.1 pooka }
133 1.1 pooka
134 1.1 pooka uint32_t
135 1.1 pooka shmif_nextpktoff(struct shmif_mem *busmem, uint32_t oldoff)
136 1.1 pooka {
137 1.2 pooka struct shmif_pkthdr sp;
138 1.1 pooka bool dummy;
139 1.1 pooka
140 1.2 pooka shmif_busread(busmem, &sp, oldoff, sizeof(sp), &dummy);
141 1.2 pooka KASSERT(sp.sp_len < BUSMEM_DATASIZE);
142 1.1 pooka
143 1.2 pooka return shmif_advance(oldoff, sizeof(sp) + sp.sp_len);
144 1.1 pooka }
145