shmif_busops.c revision 1.8 1 1.8 pooka /* $NetBSD: shmif_busops.c,v 1.8 2011/01/12 16:12:30 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.8 pooka __KERNEL_RCSID(0, "$NetBSD: shmif_busops.c,v 1.8 2011/01/12 16:12:30 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 #ifndef _KERNEL
37 1.1 pooka #include <assert.h>
38 1.8 pooka #include <stdbool.h>
39 1.8 pooka #include <string.h>
40 1.8 pooka
41 1.1 pooka #define KASSERT(a) assert(a)
42 1.1 pooka #else
43 1.1 pooka #include <rump/rumpuser.h>
44 1.1 pooka #endif
45 1.1 pooka
46 1.8 pooka #include "shmifvar.h"
47 1.8 pooka
48 1.1 pooka uint32_t
49 1.1 pooka shmif_advance(uint32_t oldoff, uint32_t delta)
50 1.1 pooka {
51 1.1 pooka uint32_t newoff;
52 1.1 pooka
53 1.1 pooka newoff = oldoff + delta;
54 1.1 pooka if (newoff >= BUSMEM_DATASIZE)
55 1.5 pooka newoff -= BUSMEM_DATASIZE;
56 1.1 pooka return newoff;
57 1.1 pooka }
58 1.1 pooka
59 1.1 pooka uint32_t
60 1.1 pooka shmif_busread(struct shmif_mem *busmem, void *dest, uint32_t off, size_t len,
61 1.1 pooka bool *wrap)
62 1.1 pooka {
63 1.1 pooka size_t chunk;
64 1.1 pooka
65 1.5 pooka KASSERT(len < (BUSMEM_DATASIZE/2) && off <= BUSMEM_DATASIZE);
66 1.1 pooka chunk = MIN(len, BUSMEM_DATASIZE - off);
67 1.1 pooka memcpy(dest, busmem->shm_data + off, chunk);
68 1.1 pooka len -= chunk;
69 1.1 pooka
70 1.6 pooka if (off + chunk == BUSMEM_DATASIZE)
71 1.6 pooka *wrap = true;
72 1.1 pooka
73 1.6 pooka if (len == 0) {
74 1.6 pooka return (off + chunk) % BUSMEM_DATASIZE;
75 1.6 pooka }
76 1.1 pooka
77 1.1 pooka /* finish reading */
78 1.5 pooka memcpy((uint8_t *)dest + chunk, busmem->shm_data, len);
79 1.5 pooka return len;
80 1.1 pooka }
81 1.1 pooka
82 1.1 pooka void
83 1.1 pooka shmif_advancefirst(struct shmif_mem *busmem, uint32_t off, size_t len)
84 1.1 pooka {
85 1.1 pooka
86 1.2 pooka while (off <= busmem->shm_first + sizeof(struct shmif_pkthdr)
87 1.1 pooka && off+len > busmem->shm_first) {
88 1.1 pooka DPRINTF(("advancefirst: old offset %d, ", busmem->shm_first));
89 1.1 pooka busmem->shm_first = shmif_nextpktoff(busmem, busmem->shm_first);
90 1.1 pooka DPRINTF(("new offset: %d\n", busmem->shm_first));
91 1.1 pooka }
92 1.1 pooka }
93 1.1 pooka
94 1.1 pooka uint32_t
95 1.1 pooka shmif_buswrite(struct shmif_mem *busmem, uint32_t off, void *data, size_t len,
96 1.1 pooka bool *wrap)
97 1.1 pooka {
98 1.1 pooka size_t chunk;
99 1.7 pooka bool filledbus;
100 1.1 pooka
101 1.5 pooka KASSERT(len < (BUSMEM_DATASIZE/2) && off <= BUSMEM_DATASIZE);
102 1.1 pooka
103 1.1 pooka chunk = MIN(len, BUSMEM_DATASIZE - off);
104 1.1 pooka len -= chunk;
105 1.7 pooka filledbus = (off+chunk == BUSMEM_DATASIZE);
106 1.1 pooka
107 1.7 pooka shmif_advancefirst(busmem, off, chunk + (filledbus ? 1 : 0));
108 1.1 pooka
109 1.1 pooka memcpy(busmem->shm_data + off, data, chunk);
110 1.1 pooka
111 1.1 pooka DPRINTF(("buswrite: wrote %d bytes to %d", chunk, off));
112 1.1 pooka
113 1.7 pooka if (filledbus) {
114 1.6 pooka *wrap = true;
115 1.7 pooka }
116 1.6 pooka
117 1.1 pooka if (len == 0) {
118 1.1 pooka DPRINTF(("\n"));
119 1.6 pooka return (off + chunk) % BUSMEM_DATASIZE;
120 1.1 pooka }
121 1.1 pooka
122 1.1 pooka DPRINTF((", wrapped bytes %d to 0\n", len));
123 1.1 pooka
124 1.6 pooka shmif_advancefirst(busmem, 0, len);
125 1.1 pooka
126 1.1 pooka /* finish writing */
127 1.6 pooka memcpy(busmem->shm_data, (uint8_t *)data + chunk, len);
128 1.6 pooka return len;
129 1.1 pooka }
130 1.1 pooka
131 1.1 pooka uint32_t
132 1.1 pooka shmif_nextpktoff(struct shmif_mem *busmem, uint32_t oldoff)
133 1.1 pooka {
134 1.2 pooka struct shmif_pkthdr sp;
135 1.1 pooka bool dummy;
136 1.1 pooka
137 1.2 pooka shmif_busread(busmem, &sp, oldoff, sizeof(sp), &dummy);
138 1.2 pooka KASSERT(sp.sp_len < BUSMEM_DATASIZE);
139 1.1 pooka
140 1.2 pooka return shmif_advance(oldoff, sizeof(sp) + sp.sp_len);
141 1.1 pooka }
142