aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / dev / virtio_snd.c
| 1 | /* virtio_snd.c -- virtio-sound PCM output for the chirp (virtio_snd.h). */ |
| 2 | #include "virtio_snd.h" |
| 3 | #include <string.h> |
| 4 | |
| 5 | static void put32(uint8_t *p, uint32_t v) { memcpy(p, &v, 4); } |
| 6 | static uint32_t get32(const uint8_t *p) |
| 7 | { |
| 8 | uint32_t v; |
| 9 | memcpy(&v, p, 4); |
| 10 | return v; |
| 11 | } |
| 12 | static uint64_t get64(const uint8_t *p) |
| 13 | { |
| 14 | uint64_t v; |
| 15 | memcpy(&v, p, 8); |
| 16 | return v; |
| 17 | } |
| 18 | |
| 19 | /* struct virtio_snd_query_info: hdr.code, start_id, count, size */ |
| 20 | size_t vsnd_cmd_query(uint8_t *out, size_t cap, uint32_t code, uint32_t start, uint32_t count, uint32_t size) |
| 21 | { |
| 22 | if (!out || cap < 16) |
| 23 | return 0; |
| 24 | put32(out, code), put32(out + 4, start), put32(out + 8, count), put32(out + 12, size); |
| 25 | return 16; |
| 26 | } |
| 27 | |
| 28 | /* struct virtio_snd_pcm_set_params: hdr.code, hdr.stream_id, buffer_bytes, |
| 29 | * period_bytes, features, channels, format, rate, padding */ |
| 30 | size_t vsnd_cmd_set_params(uint8_t *out, size_t cap, uint32_t stream, uint32_t buffer_bytes, uint32_t period_bytes, |
| 31 | uint8_t channels, uint8_t format, uint8_t rate) |
| 32 | { |
| 33 | if (!out || cap < 24) |
| 34 | return 0; |
| 35 | memset(out, 0, 24); |
| 36 | put32(out, VSND_R_PCM_SET_PARAMS), put32(out + 4, stream), put32(out + 8, buffer_bytes); |
| 37 | put32(out + 12, period_bytes); |
| 38 | out[20] = channels, out[21] = format, out[22] = rate; |
| 39 | return 24; |
| 40 | } |
| 41 | |
| 42 | /* struct virtio_snd_pcm_hdr: hdr.code, stream_id */ |
| 43 | size_t vsnd_cmd_pcm(uint8_t *out, size_t cap, uint32_t code, uint32_t stream) |
| 44 | { |
| 45 | if (!out || cap < 8) |
| 46 | return 0; |
| 47 | put32(out, code), put32(out + 4, stream); |
| 48 | return 8; |
| 49 | } |
| 50 | |
| 51 | /* reply: struct virtio_snd_hdr (status), then n struct virtio_snd_pcm_info: |
| 52 | * hda_fn_nid, features, formats (64), rates (64), direction, channels_min, |
| 53 | * channels_max, padding[5] */ |
| 54 | int vsnd_pick_output(const uint8_t *resp, size_t len, uint32_t n) |
| 55 | { |
| 56 | if (len < 4 || get32(resp) != VSND_S_OK) |
| 57 | return -1; |
| 58 | for (uint32_t i = 0; i < n && 4u + (i + 1) * VSND_INFO_BYTES <= len; i++) { |
| 59 | const uint8_t *p = resp + 4 + i * VSND_INFO_BYTES; |
| 60 | if (p[24] == VSND_D_OUTPUT && (get64(p + 8) >> VSND_FMT_S16 & 1) && (get64(p + 16) >> VSND_RATE_48000 & 1) && |
| 61 | p[25] <= 2 && p[26] >= 2) |
| 62 | return (int)i; |
| 63 | } |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | #ifndef CK_HOSTED |
| 68 | #include "../ui/chirp.h" |
| 69 | #include "ck.h" |
| 70 | |
| 71 | #define VSND_SPINS 200000000u |
| 72 | #define VSND_PERIOD_FRAMES 4800u /* 100 ms */ |
| 73 | #define VSND_PERIOD_BYTES (VSND_PERIOD_FRAMES * 4u) |
| 74 | #define VSND_SLOTS 4u /* 400 ms queued ahead: 3 descriptors each */ |
| 75 | #define VSND_SLOT_DATA 64u /* xfer header at 0, status at 16, samples from 64 */ |
| 76 | #define VSND_SLOT_BYTES 20480u |
| 77 | #define VSND_LEAD_MS 150u /* start this long before the chirp comes in */ |
| 78 | #define VSND_END_FRAME (CK_CHIRP_FRAMES + VSND_PERIOD_FRAMES) /* the chirp, then 100 ms quiet */ |
| 79 | |
| 80 | enum { OFF, READY, PLAYING, DONE }; |
| 81 | |
| 82 | static struct { |
| 83 | vq_dev *dev; |
| 84 | int state; |
| 85 | uint32_t stream; |
| 86 | uint8_t *cmd, *resp, *slot; |
| 87 | uint64_t cmd_bus, resp_bus, slot_bus; |
| 88 | uint8_t busy[VSND_SLOTS]; |
| 89 | uint8_t slot_of_head[VQ_SIZE]; |
| 90 | uint32_t pos, start_ms, start_frame, periods, starved, bad; |
| 91 | } s; |
| 92 | |
| 93 | static int control(size_t len, uint32_t resp_len) |
| 94 | { |
| 95 | vq_buf b[2] = {{s.cmd_bus, (uint32_t)len, 0}, {s.resp_bus, resp_len, 1}}; |
| 96 | uint32_t wrote = 0; |
| 97 | memset(s.resp, 0, 8); |
| 98 | int e = vq_call(s.dev, VSND_VQ_CONTROL, b, 2, VSND_SPINS, &wrote); |
| 99 | return e ? -1000 - e : (wrote >= 4 && get32(s.resp) == VSND_S_OK ? 0 : (int)get32(s.resp)); |
| 100 | } |
| 101 | |
| 102 | int ck_vsnd_attach(vq_dev *dev, uint8_t *mem, uint64_t bus) |
| 103 | { |
| 104 | _Static_assert(VSND_EXTRA_BYTES == 2u * 4096u + VSND_SLOTS * VSND_SLOT_BYTES, "VSND_EXTRA_BYTES"); |
| 105 | s.dev = dev; |
| 106 | vq_ready(s.dev); |
| 107 | s.cmd = mem, s.cmd_bus = bus; |
| 108 | s.resp = s.cmd + 4096u, s.resp_bus = s.cmd_bus + 4096u; |
| 109 | s.slot = s.resp + 4096u, s.slot_bus = s.resp_bus + 4096u; |
| 110 | /* struct virtio_snd_config: jacks, streams, chmaps */ |
| 111 | uint32_t streams = vq_cfg32(s.dev, 4); |
| 112 | if (streams == 0 || streams > 64) { |
| 113 | ck_printf("virtio_snd: no PCM streams (%u)\n", streams); |
| 114 | return -2; |
| 115 | } |
| 116 | int rc = control(vsnd_cmd_query(s.cmd, 4096, VSND_R_PCM_INFO, 0, streams, VSND_INFO_BYTES), |
| 117 | 4u + streams * VSND_INFO_BYTES); |
| 118 | int pick = rc ? -1 : vsnd_pick_output(s.resp, 4u + streams * VSND_INFO_BYTES, streams); |
| 119 | if (pick < 0) { |
| 120 | ck_printf("virtio_snd: no stereo S16 48 kHz output among %u streams (rc=%d)\n", streams, rc); |
| 121 | return -3; |
| 122 | } |
| 123 | s.stream = (uint32_t)pick; |
| 124 | if ((rc = control(vsnd_cmd_set_params(s.cmd, 4096, s.stream, VSND_SLOTS * VSND_PERIOD_BYTES, VSND_PERIOD_BYTES, 2, |
| 125 | VSND_FMT_S16, VSND_RATE_48000), |
| 126 | 8)) || |
| 127 | (rc = control(vsnd_cmd_pcm(s.cmd, 4096, VSND_R_PCM_PREPARE, s.stream), 8))) { |
| 128 | ck_printf("virtio_snd: stream %u refused (status 0x%x)\n", s.stream, (unsigned)rc); |
| 129 | return -4; |
| 130 | } |
| 131 | s.state = READY; |
| 132 | ck_printf("virtio_snd: stream %u of %u ready, stereo S16 48000 Hz, %u ms periods, %u queued\n", s.stream, streams, |
| 133 | VSND_PERIOD_FRAMES / 48u, VSND_SLOTS); |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* Fills slot i with the next period from the night's position and posts it. */ |
| 138 | static int feed(unsigned i) |
| 139 | { |
| 140 | uint8_t *p = s.slot + i * VSND_SLOT_BYTES; |
| 141 | uint64_t bus = s.slot_bus + i * VSND_SLOT_BYTES; |
| 142 | put32(p, s.stream); |
| 143 | memset(p + 16, 0, 8); |
| 144 | ck_chirp_fill((int16_t *)(void *)(p + VSND_SLOT_DATA), s.pos, VSND_PERIOD_FRAMES); |
| 145 | vq_buf b[3] = {{bus, 4, 0}, {bus + VSND_SLOT_DATA, VSND_PERIOD_BYTES, 0}, {bus + 16, 8, 1}}; |
| 146 | uint16_t head; |
| 147 | if (vq_post(s.dev, VSND_VQ_TX, b, 3, &head)) |
| 148 | return -1; |
| 149 | s.slot_of_head[head] = (uint8_t)i; |
| 150 | s.busy[i] = 1; |
| 151 | s.pos += VSND_PERIOD_FRAMES; |
| 152 | s.periods++; |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | int ck_vsnd_step(uint32_t night_ms, uint32_t audio_frame) |
| 157 | { |
| 158 | if (s.state == READY) { |
| 159 | if (night_ms + VSND_LEAD_MS < CK_CHIRP_START_MS) |
| 160 | return 1; |
| 161 | s.pos = s.start_frame = audio_frame, s.start_ms = night_ms; |
| 162 | for (unsigned i = 0; i < VSND_SLOTS; i++) |
| 163 | if (feed(i)) |
| 164 | break; |
| 165 | int rc = control(vsnd_cmd_pcm(s.cmd, 4096, VSND_R_PCM_START, s.stream), 8); |
| 166 | if (rc) { |
| 167 | ck_printf("virtio_snd: start refused (status 0x%x)\n", (unsigned)rc); |
| 168 | s.state = DONE; |
| 169 | return 0; |
| 170 | } |
| 171 | s.state = PLAYING; |
| 172 | return 1; |
| 173 | } |
| 174 | if (s.state != PLAYING) |
| 175 | return 0; |
| 176 | unsigned busy = 0; |
| 177 | for (unsigned i = 0; i < VSND_SLOTS; i++) |
| 178 | busy += s.busy[i]; |
| 179 | uint16_t head; |
| 180 | uint32_t wrote; |
| 181 | int done_any = 0; |
| 182 | while (vq_reap(s.dev, VSND_VQ_TX, &head, &wrote) == VQ_OK) { |
| 183 | unsigned i = s.slot_of_head[head]; |
| 184 | uint8_t *p = s.slot + i * VSND_SLOT_BYTES; |
| 185 | if (wrote < 4 || get32(p + 16) != VSND_S_OK) |
| 186 | s.bad++; |
| 187 | s.busy[i] = 0; |
| 188 | done_any = 1; |
| 189 | if (busy-- == 1 && s.pos < VSND_END_FRAME) |
| 190 | s.starved++; /* the device ran out before this turn came */ |
| 191 | } |
| 192 | if (s.dev->broken) { |
| 193 | ck_printf("virtio_snd: device broken mid-stream\n"); |
| 194 | s.state = DONE; |
| 195 | return 0; |
| 196 | } |
| 197 | if (done_any) |
| 198 | for (unsigned i = 0; i < VSND_SLOTS && s.pos < VSND_END_FRAME; i++) |
| 199 | if (!s.busy[i]) |
| 200 | feed(i); |
| 201 | for (unsigned i = 0; i < VSND_SLOTS; i++) |
| 202 | if (s.busy[i]) |
| 203 | return 1; |
| 204 | if (s.pos < VSND_END_FRAME) |
| 205 | return 1; |
| 206 | int rc = control(vsnd_cmd_pcm(s.cmd, 4096, VSND_R_PCM_STOP, s.stream), 8); |
| 207 | if (!rc) |
| 208 | rc = control(vsnd_cmd_pcm(s.cmd, 4096, VSND_R_PCM_RELEASE, s.stream), 8); |
| 209 | ck_printf("sound: virtio played the chirp from night %u ms (frame %u): %u periods of 100 ms, %u starved, %u " |
| 210 | "refused, stop %s at night %u ms\n", |
| 211 | s.start_ms, s.start_frame, s.periods, s.starved, s.bad, rc ? "refused" : "ok", night_ms); |
| 212 | s.state = DONE; |
| 213 | return 0; |
| 214 | } |
| 215 | #endif |
| 216 | |
| 217 | #if !defined(CK_HOSTED) && !defined(CK_X86) |
| 218 | #include "virtio_bind.h" |
| 219 | static vq_bind snd_bind; |
| 220 | int ck_vsnd_open(const pci_system *pci) |
| 221 | { |
| 222 | /* 0x1059 = 0x1040 + device id 25 (virtio 1.x 4.1.2.1). Queues: control, event, tx. */ |
| 223 | if (vq_bind_open(&snd_bind, "virtio_snd", pci, 0x1059u, 3, VSND_EXTRA_BYTES)) |
| 224 | return -1; |
| 225 | return ck_vsnd_attach(&snd_bind.dev, snd_bind.mem + snd_bind.rings, snd_bind.phys + snd_bind.rings); |
| 226 | } |
| 227 | #endif |