shmif_busops.c revision 1.1 1 1.1 pooka /* $NetBSD: shmif_busops.c,v 1.1 2010/08/12 21:41:47 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.1 pooka * Copyright (c) 2009 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.1 pooka #include <sys/cdefs.h>
31 1.1 pooka __KERNEL_RCSID(0, "$NetBSD: shmif_busops.c,v 1.1 2010/08/12 21:41:47 pooka Exp $");
32 1.1 pooka
33 1.1 pooka #include <sys/param.h>
34 1.1 pooka #include <sys/atomic.h>
35 1.1 pooka
36 1.1 pooka #include "shmifvar.h"
37 1.1 pooka
38 1.1 pooka #ifndef _KERNEL
39 1.1 pooka #include <assert.h>
40 1.1 pooka #define KASSERT(a) assert(a)
41 1.1 pooka #else
42 1.1 pooka #include <rump/rumpuser.h>
43 1.1 pooka #endif
44 1.1 pooka
45 1.1 pooka #define LOCK_UNLOCKED 0
46 1.1 pooka #define LOCK_LOCKED 1
47 1.1 pooka
48 1.1 pooka /*
49 1.1 pooka * This locking needs work and will misbehave severely if:
50 1.1 pooka * 1) the backing memory has to be paged in
51 1.1 pooka * 2) some lockholder exits while holding the lock
52 1.1 pooka */
53 1.1 pooka void
54 1.1 pooka shmif_lockbus(struct shmif_mem *busmem)
55 1.1 pooka {
56 1.1 pooka
57 1.1 pooka while (atomic_cas_32(&busmem->shm_lock,
58 1.1 pooka LOCK_UNLOCKED, LOCK_LOCKED) == LOCK_LOCKED)
59 1.1 pooka continue;
60 1.1 pooka membar_enter();
61 1.1 pooka }
62 1.1 pooka
63 1.1 pooka void
64 1.1 pooka shmif_unlockbus(struct shmif_mem *busmem)
65 1.1 pooka {
66 1.1 pooka unsigned int old;
67 1.1 pooka
68 1.1 pooka membar_exit();
69 1.1 pooka old = atomic_swap_32(&busmem->shm_lock, LOCK_UNLOCKED);
70 1.1 pooka KASSERT(old == LOCK_LOCKED);
71 1.1 pooka }
72 1.1 pooka
73 1.1 pooka uint32_t
74 1.1 pooka shmif_advance(uint32_t oldoff, uint32_t delta)
75 1.1 pooka {
76 1.1 pooka uint32_t newoff;
77 1.1 pooka
78 1.1 pooka newoff = oldoff + delta;
79 1.1 pooka if (newoff >= BUSMEM_DATASIZE)
80 1.1 pooka newoff -= (BUSMEM_DATASIZE);
81 1.1 pooka return newoff;
82 1.1 pooka
83 1.1 pooka }
84 1.1 pooka
85 1.1 pooka uint32_t
86 1.1 pooka shmif_busread(struct shmif_mem *busmem, void *dest, uint32_t off, size_t len,
87 1.1 pooka bool *wrap)
88 1.1 pooka {
89 1.1 pooka size_t chunk;
90 1.1 pooka
91 1.1 pooka KASSERT(len < (BUSMEM_DATASIZE) && off <= BUSMEM_DATASIZE);
92 1.1 pooka chunk = MIN(len, BUSMEM_DATASIZE - off);
93 1.1 pooka memcpy(dest, busmem->shm_data + off, chunk);
94 1.1 pooka len -= chunk;
95 1.1 pooka
96 1.1 pooka if (len == 0)
97 1.1 pooka return off + chunk;
98 1.1 pooka
99 1.1 pooka /* else, wraps around */
100 1.1 pooka off = 0;
101 1.1 pooka *wrap = true;
102 1.1 pooka
103 1.1 pooka /* finish reading */
104 1.1 pooka memcpy((uint8_t *)dest + chunk, busmem->shm_data + off, len);
105 1.1 pooka return off + len;
106 1.1 pooka }
107 1.1 pooka
108 1.1 pooka void
109 1.1 pooka shmif_advancefirst(struct shmif_mem *busmem, uint32_t off, size_t len)
110 1.1 pooka {
111 1.1 pooka
112 1.1 pooka while (off <= busmem->shm_first + PKTLEN_SIZE
113 1.1 pooka && off+len > busmem->shm_first) {
114 1.1 pooka DPRINTF(("advancefirst: old offset %d, ", busmem->shm_first));
115 1.1 pooka busmem->shm_first = shmif_nextpktoff(busmem, busmem->shm_first);
116 1.1 pooka DPRINTF(("new offset: %d\n", busmem->shm_first));
117 1.1 pooka }
118 1.1 pooka }
119 1.1 pooka
120 1.1 pooka uint32_t
121 1.1 pooka shmif_buswrite(struct shmif_mem *busmem, uint32_t off, void *data, size_t len,
122 1.1 pooka bool *wrap)
123 1.1 pooka {
124 1.1 pooka size_t chunk;
125 1.1 pooka
126 1.1 pooka KASSERT(len < (BUSMEM_DATASIZE) && off <= BUSMEM_DATASIZE);
127 1.1 pooka
128 1.1 pooka chunk = MIN(len, BUSMEM_DATASIZE - off);
129 1.1 pooka len -= chunk;
130 1.1 pooka
131 1.1 pooka shmif_advancefirst(busmem, off, chunk + (len ? 1 : 0));
132 1.1 pooka
133 1.1 pooka memcpy(busmem->shm_data + off, data, chunk);
134 1.1 pooka
135 1.1 pooka DPRINTF(("buswrite: wrote %d bytes to %d", chunk, off));
136 1.1 pooka
137 1.1 pooka if (len == 0) {
138 1.1 pooka DPRINTF(("\n"));
139 1.1 pooka return off + chunk;
140 1.1 pooka }
141 1.1 pooka
142 1.1 pooka DPRINTF((", wrapped bytes %d to 0\n", len));
143 1.1 pooka
144 1.1 pooka /* else, wraps around */
145 1.1 pooka off = 0;
146 1.1 pooka *wrap = true;
147 1.1 pooka
148 1.1 pooka shmif_advancefirst(busmem, off, len);
149 1.1 pooka
150 1.1 pooka /* finish writing */
151 1.1 pooka memcpy(busmem->shm_data + off, (uint8_t *)data + chunk, len);
152 1.1 pooka return off + len;
153 1.1 pooka }
154 1.1 pooka
155 1.1 pooka uint32_t
156 1.1 pooka shmif_nextpktoff(struct shmif_mem *busmem, uint32_t oldoff)
157 1.1 pooka {
158 1.1 pooka uint32_t oldlen;
159 1.1 pooka bool dummy;
160 1.1 pooka
161 1.1 pooka shmif_busread(busmem, &oldlen, oldoff, PKTLEN_SIZE, &dummy);
162 1.1 pooka KASSERT(oldlen < BUSMEM_DATASIZE);
163 1.1 pooka
164 1.1 pooka return shmif_advance(oldoff, PKTLEN_SIZE + oldlen);
165 1.1 pooka }
166