shmif_busops.c revision 1.6 1 1.6 pooka /* $NetBSD: shmif_busops.c,v 1.6 2010/08/17 11:35:23 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.6 pooka __KERNEL_RCSID(0, "$NetBSD: shmif_busops.c,v 1.6 2010/08/17 11:35:23 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 uint32_t
46 1.1 pooka shmif_advance(uint32_t oldoff, uint32_t delta)
47 1.1 pooka {
48 1.1 pooka uint32_t newoff;
49 1.1 pooka
50 1.1 pooka newoff = oldoff + delta;
51 1.1 pooka if (newoff >= BUSMEM_DATASIZE)
52 1.5 pooka newoff -= BUSMEM_DATASIZE;
53 1.1 pooka return newoff;
54 1.1 pooka }
55 1.1 pooka
56 1.1 pooka uint32_t
57 1.1 pooka shmif_busread(struct shmif_mem *busmem, void *dest, uint32_t off, size_t len,
58 1.1 pooka bool *wrap)
59 1.1 pooka {
60 1.1 pooka size_t chunk;
61 1.1 pooka
62 1.5 pooka KASSERT(len < (BUSMEM_DATASIZE/2) && off <= BUSMEM_DATASIZE);
63 1.1 pooka chunk = MIN(len, BUSMEM_DATASIZE - off);
64 1.1 pooka memcpy(dest, busmem->shm_data + off, chunk);
65 1.1 pooka len -= chunk;
66 1.1 pooka
67 1.6 pooka if (off + chunk == BUSMEM_DATASIZE)
68 1.6 pooka *wrap = true;
69 1.1 pooka
70 1.6 pooka if (len == 0) {
71 1.6 pooka return (off + chunk) % BUSMEM_DATASIZE;
72 1.6 pooka }
73 1.1 pooka
74 1.1 pooka /* finish reading */
75 1.5 pooka memcpy((uint8_t *)dest + chunk, busmem->shm_data, len);
76 1.5 pooka return len;
77 1.1 pooka }
78 1.1 pooka
79 1.1 pooka void
80 1.1 pooka shmif_advancefirst(struct shmif_mem *busmem, uint32_t off, size_t len)
81 1.1 pooka {
82 1.1 pooka
83 1.2 pooka while (off <= busmem->shm_first + sizeof(struct shmif_pkthdr)
84 1.1 pooka && off+len > busmem->shm_first) {
85 1.1 pooka DPRINTF(("advancefirst: old offset %d, ", busmem->shm_first));
86 1.1 pooka busmem->shm_first = shmif_nextpktoff(busmem, busmem->shm_first);
87 1.1 pooka DPRINTF(("new offset: %d\n", busmem->shm_first));
88 1.1 pooka }
89 1.1 pooka }
90 1.1 pooka
91 1.1 pooka uint32_t
92 1.1 pooka shmif_buswrite(struct shmif_mem *busmem, uint32_t off, void *data, size_t len,
93 1.1 pooka bool *wrap)
94 1.1 pooka {
95 1.1 pooka size_t chunk;
96 1.1 pooka
97 1.5 pooka KASSERT(len < (BUSMEM_DATASIZE/2) && off <= BUSMEM_DATASIZE);
98 1.1 pooka
99 1.1 pooka chunk = MIN(len, BUSMEM_DATASIZE - off);
100 1.1 pooka len -= chunk;
101 1.1 pooka
102 1.1 pooka shmif_advancefirst(busmem, off, chunk + (len ? 1 : 0));
103 1.1 pooka
104 1.1 pooka memcpy(busmem->shm_data + off, data, chunk);
105 1.1 pooka
106 1.1 pooka DPRINTF(("buswrite: wrote %d bytes to %d", chunk, off));
107 1.1 pooka
108 1.6 pooka if (off + chunk == BUSMEM_DATASIZE)
109 1.6 pooka *wrap = true;
110 1.6 pooka
111 1.1 pooka if (len == 0) {
112 1.1 pooka DPRINTF(("\n"));
113 1.6 pooka return (off + chunk) % BUSMEM_DATASIZE;
114 1.1 pooka }
115 1.1 pooka
116 1.1 pooka DPRINTF((", wrapped bytes %d to 0\n", len));
117 1.1 pooka
118 1.6 pooka shmif_advancefirst(busmem, 0, len);
119 1.1 pooka
120 1.1 pooka /* finish writing */
121 1.6 pooka memcpy(busmem->shm_data, (uint8_t *)data + chunk, len);
122 1.6 pooka return len;
123 1.1 pooka }
124 1.1 pooka
125 1.1 pooka uint32_t
126 1.1 pooka shmif_nextpktoff(struct shmif_mem *busmem, uint32_t oldoff)
127 1.1 pooka {
128 1.2 pooka struct shmif_pkthdr sp;
129 1.1 pooka bool dummy;
130 1.1 pooka
131 1.2 pooka shmif_busread(busmem, &sp, oldoff, sizeof(sp), &dummy);
132 1.2 pooka KASSERT(sp.sp_len < BUSMEM_DATASIZE);
133 1.1 pooka
134 1.2 pooka return shmif_advance(oldoff, sizeof(sp) + sp.sp_len);
135 1.1 pooka }
136