blob: 544158f9ca9f570de74c0d66ec270906e980563d [file] [log] [blame]
bellard267002c2004-06-03 18:46:20 +00001/*
2 * QEMU CUDA support
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "vl.h"
25
bellard819e7122004-06-21 16:47:13 +000026//#define DEBUG_CUDA
27//#define DEBUG_CUDA_PACKET
28
bellard267002c2004-06-03 18:46:20 +000029/* Bits in B data register: all active low */
30#define TREQ 0x08 /* Transfer request (input) */
31#define TACK 0x10 /* Transfer acknowledge (output) */
32#define TIP 0x20 /* Transfer in progress (output) */
33
34/* Bits in ACR */
35#define SR_CTRL 0x1c /* Shift register control bits */
36#define SR_EXT 0x0c /* Shift on external clock */
37#define SR_OUT 0x10 /* Shift out if 1 */
38
39/* Bits in IFR and IER */
40#define IER_SET 0x80 /* set bits in IER */
41#define IER_CLR 0 /* clear bits in IER */
42#define SR_INT 0x04 /* Shift register full/empty */
43#define T1_INT 0x40 /* Timer 1 interrupt */
44
45/* Bits in ACR */
46#define T1MODE 0xc0 /* Timer 1 mode */
47#define T1MODE_CONT 0x40 /* continuous interrupts */
48
49/* commands (1st byte) */
50#define ADB_PACKET 0
51#define CUDA_PACKET 1
52#define ERROR_PACKET 2
53#define TIMER_PACKET 3
54#define POWER_PACKET 4
55#define MACIIC_PACKET 5
56#define PMU_PACKET 6
57
58
59/* CUDA commands (2nd byte) */
60#define CUDA_WARM_START 0x0
61#define CUDA_AUTOPOLL 0x1
62#define CUDA_GET_6805_ADDR 0x2
63#define CUDA_GET_TIME 0x3
64#define CUDA_GET_PRAM 0x7
65#define CUDA_SET_6805_ADDR 0x8
66#define CUDA_SET_TIME 0x9
67#define CUDA_POWERDOWN 0xa
68#define CUDA_POWERUP_TIME 0xb
69#define CUDA_SET_PRAM 0xc
70#define CUDA_MS_RESET 0xd
71#define CUDA_SEND_DFAC 0xe
72#define CUDA_BATTERY_SWAP_SENSE 0x10
73#define CUDA_RESET_SYSTEM 0x11
74#define CUDA_SET_IPL 0x12
75#define CUDA_FILE_SERVER_FLAG 0x13
76#define CUDA_SET_AUTO_RATE 0x14
77#define CUDA_GET_AUTO_RATE 0x16
78#define CUDA_SET_DEVICE_LIST 0x19
79#define CUDA_GET_DEVICE_LIST 0x1a
80#define CUDA_SET_ONE_SECOND_MODE 0x1b
81#define CUDA_SET_POWER_MESSAGES 0x21
82#define CUDA_GET_SET_IIC 0x22
83#define CUDA_WAKEUP 0x23
84#define CUDA_TIMER_TICKLE 0x24
85#define CUDA_COMBINED_FORMAT_IIC 0x25
86
87#define CUDA_TIMER_FREQ (4700000 / 6)
88
89typedef struct CUDATimer {
90 unsigned int latch;
91 uint16_t counter_value; /* counter value at load time */
92 int64_t load_time;
93 int64_t next_irq_time;
94 QEMUTimer *timer;
95} CUDATimer;
96
97typedef struct CUDAState {
98 /* cuda registers */
99 uint8_t b; /* B-side data */
100 uint8_t a; /* A-side data */
101 uint8_t dirb; /* B-side direction (1=output) */
102 uint8_t dira; /* A-side direction (1=output) */
103 uint8_t sr; /* Shift register */
104 uint8_t acr; /* Auxiliary control register */
105 uint8_t pcr; /* Peripheral control register */
106 uint8_t ifr; /* Interrupt flag register */
107 uint8_t ier; /* Interrupt enable register */
108 uint8_t anh; /* A-side data, no handshake */
109
110 CUDATimer timers[2];
111
112 uint8_t last_b; /* last value of B register */
113 uint8_t last_acr; /* last value of B register */
114
115 int data_in_size;
116 int data_in_index;
117 int data_out_index;
118
119 int irq;
bellard819e7122004-06-21 16:47:13 +0000120 openpic_t *openpic;
bellard267002c2004-06-03 18:46:20 +0000121 uint8_t autopoll;
122 uint8_t data_in[128];
123 uint8_t data_out[16];
124} CUDAState;
125
126static CUDAState cuda_state;
127ADBBusState adb_bus;
128
129static void cuda_update(CUDAState *s);
130static void cuda_receive_packet_from_host(CUDAState *s,
131 const uint8_t *data, int len);
bellard819e7122004-06-21 16:47:13 +0000132static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
133 int64_t current_time);
bellard267002c2004-06-03 18:46:20 +0000134
135static void cuda_update_irq(CUDAState *s)
136{
bellard819e7122004-06-21 16:47:13 +0000137 if (s->ifr & s->ier & (SR_INT | T1_INT)) {
138 openpic_set_irq(s->openpic, s->irq, 1);
bellard267002c2004-06-03 18:46:20 +0000139 } else {
bellard819e7122004-06-21 16:47:13 +0000140 openpic_set_irq(s->openpic, s->irq, 0);
bellard267002c2004-06-03 18:46:20 +0000141 }
142}
143
144static unsigned int get_counter(CUDATimer *s)
145{
146 int64_t d;
147 unsigned int counter;
148
149 d = muldiv64(qemu_get_clock(vm_clock) - s->load_time,
150 CUDA_TIMER_FREQ, ticks_per_sec);
151 if (d <= s->counter_value) {
152 counter = d;
153 } else {
154 counter = s->latch - 1 - ((d - s->counter_value) % s->latch);
155 }
156 return counter;
157}
158
bellard819e7122004-06-21 16:47:13 +0000159static void set_counter(CUDAState *s, CUDATimer *ti, unsigned int val)
bellard267002c2004-06-03 18:46:20 +0000160{
bellard819e7122004-06-21 16:47:13 +0000161#ifdef DEBUG_CUDA
162 printf("cuda: T%d.counter=%d\n",
163 1 + (ti->timer == NULL), val);
164#endif
165 ti->load_time = qemu_get_clock(vm_clock);
166 ti->counter_value = val;
167 cuda_timer_update(s, ti, ti->load_time);
bellard267002c2004-06-03 18:46:20 +0000168}
169
170static int64_t get_next_irq_time(CUDATimer *s, int64_t current_time)
171{
172 int64_t d, next_time, base;
173 /* current counter value */
174 d = muldiv64(current_time - s->load_time,
175 CUDA_TIMER_FREQ, ticks_per_sec);
176 if (d <= s->counter_value) {
177 next_time = s->counter_value + 1;
178 } else {
bellard819e7122004-06-21 16:47:13 +0000179 base = ((d - s->counter_value) / s->latch);
bellard267002c2004-06-03 18:46:20 +0000180 base = (base * s->latch) + s->counter_value;
181 next_time = base + s->latch;
182 }
bellard819e7122004-06-21 16:47:13 +0000183#ifdef DEBUG_CUDA
184 printf("latch=%d counter=%lld delta_next=%lld\n",
185 s->latch, d, next_time - d);
186#endif
bellard267002c2004-06-03 18:46:20 +0000187 next_time = muldiv64(next_time, ticks_per_sec, CUDA_TIMER_FREQ) +
188 s->load_time;
189 if (next_time <= current_time)
190 next_time = current_time + 1;
191 return next_time;
192}
193
bellard819e7122004-06-21 16:47:13 +0000194static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
195 int64_t current_time)
196{
197 if (!ti->timer)
198 return;
199 if ((s->acr & T1MODE) != T1MODE_CONT) {
200 qemu_del_timer(ti->timer);
201 } else {
202 ti->next_irq_time = get_next_irq_time(ti, current_time);
203 qemu_mod_timer(ti->timer, ti->next_irq_time);
204 }
205}
206
bellard267002c2004-06-03 18:46:20 +0000207static void cuda_timer1(void *opaque)
208{
209 CUDAState *s = opaque;
210 CUDATimer *ti = &s->timers[0];
211
bellard819e7122004-06-21 16:47:13 +0000212 cuda_timer_update(s, ti, ti->next_irq_time);
bellard267002c2004-06-03 18:46:20 +0000213 s->ifr |= T1_INT;
214 cuda_update_irq(s);
215}
216
217static uint32_t cuda_readb(void *opaque, target_phys_addr_t addr)
218{
219 CUDAState *s = opaque;
220 uint32_t val;
221
222 addr = (addr >> 9) & 0xf;
223 switch(addr) {
224 case 0:
225 val = s->b;
226 break;
227 case 1:
228 val = s->a;
229 break;
230 case 2:
231 val = s->dirb;
232 break;
233 case 3:
234 val = s->dira;
235 break;
236 case 4:
237 val = get_counter(&s->timers[0]) & 0xff;
238 s->ifr &= ~T1_INT;
239 cuda_update_irq(s);
240 break;
241 case 5:
242 val = get_counter(&s->timers[0]) >> 8;
243 s->ifr &= ~T1_INT;
244 cuda_update_irq(s);
245 break;
246 case 6:
247 val = s->timers[0].latch & 0xff;
248 break;
249 case 7:
250 val = (s->timers[0].latch >> 8) & 0xff;
251 break;
252 case 8:
253 val = get_counter(&s->timers[1]) & 0xff;
254 break;
255 case 9:
256 val = get_counter(&s->timers[1]) >> 8;
257 break;
258 case 10:
bellard819e7122004-06-21 16:47:13 +0000259 val = s->sr;
260 s->ifr &= ~SR_INT;
261 cuda_update_irq(s);
bellard267002c2004-06-03 18:46:20 +0000262 break;
263 case 11:
264 val = s->acr;
265 break;
266 case 12:
267 val = s->pcr;
268 break;
269 case 13:
270 val = s->ifr;
271 break;
272 case 14:
273 val = s->ier;
274 break;
275 default:
276 case 15:
277 val = s->anh;
278 break;
279 }
280#ifdef DEBUG_CUDA
bellard819e7122004-06-21 16:47:13 +0000281 if (addr != 13 || val != 0)
282 printf("cuda: read: reg=0x%x val=%02x\n", addr, val);
bellard267002c2004-06-03 18:46:20 +0000283#endif
284 return val;
285}
286
287static void cuda_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
288{
289 CUDAState *s = opaque;
290
291 addr = (addr >> 9) & 0xf;
292#ifdef DEBUG_CUDA
293 printf("cuda: write: reg=0x%x val=%02x\n", addr, val);
294#endif
295
296 switch(addr) {
297 case 0:
298 s->b = val;
299 cuda_update(s);
300 break;
301 case 1:
302 s->a = val;
303 break;
304 case 2:
305 s->dirb = val;
306 break;
307 case 3:
308 s->dira = val;
309 break;
310 case 4:
311 val = val | (get_counter(&s->timers[0]) & 0xff00);
bellard819e7122004-06-21 16:47:13 +0000312 set_counter(s, &s->timers[0], val);
bellard267002c2004-06-03 18:46:20 +0000313 break;
314 case 5:
315 val = (val << 8) | (get_counter(&s->timers[0]) & 0xff);
bellard819e7122004-06-21 16:47:13 +0000316 set_counter(s, &s->timers[0], val);
bellard267002c2004-06-03 18:46:20 +0000317 break;
318 case 6:
319 s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
bellard819e7122004-06-21 16:47:13 +0000320 cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
bellard267002c2004-06-03 18:46:20 +0000321 break;
322 case 7:
323 s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
bellard819e7122004-06-21 16:47:13 +0000324 cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
bellard267002c2004-06-03 18:46:20 +0000325 break;
326 case 8:
327 val = val | (get_counter(&s->timers[1]) & 0xff00);
bellard819e7122004-06-21 16:47:13 +0000328 set_counter(s, &s->timers[1], val);
bellard267002c2004-06-03 18:46:20 +0000329 break;
330 case 9:
331 val = (val << 8) | (get_counter(&s->timers[1]) & 0xff);
bellard819e7122004-06-21 16:47:13 +0000332 set_counter(s, &s->timers[1], val);
bellard267002c2004-06-03 18:46:20 +0000333 break;
334 case 10:
335 s->sr = val;
336 break;
337 case 11:
338 s->acr = val;
bellard819e7122004-06-21 16:47:13 +0000339 cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
bellard267002c2004-06-03 18:46:20 +0000340 cuda_update(s);
341 break;
342 case 12:
343 s->pcr = val;
344 break;
345 case 13:
346 /* reset bits */
347 s->ifr &= ~val;
348 cuda_update_irq(s);
349 break;
350 case 14:
351 if (val & IER_SET) {
352 /* set bits */
353 s->ier |= val & 0x7f;
354 } else {
355 /* reset bits */
356 s->ier &= ~val;
357 }
358 cuda_update_irq(s);
359 break;
360 default:
361 case 15:
362 s->anh = val;
363 break;
364 }
365}
366
367/* NOTE: TIP and TREQ are negated */
368static void cuda_update(CUDAState *s)
369{
bellard819e7122004-06-21 16:47:13 +0000370 int packet_received, len;
bellard267002c2004-06-03 18:46:20 +0000371
bellard819e7122004-06-21 16:47:13 +0000372 packet_received = 0;
373 if (!(s->b & TIP)) {
374 /* transfer requested from host */
375
376 if (s->acr & SR_OUT) {
377 /* data output */
378 if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
379 if (s->data_out_index < sizeof(s->data_out)) {
380#ifdef DEBUG_CUDA
381 printf("cuda: send: %02x\n", s->sr);
382#endif
383 s->data_out[s->data_out_index++] = s->sr;
384 s->ifr |= SR_INT;
385 cuda_update_irq(s);
386 }
bellard267002c2004-06-03 18:46:20 +0000387 }
bellard819e7122004-06-21 16:47:13 +0000388 } else {
389 if (s->data_in_index < s->data_in_size) {
390 /* data input */
391 if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
392 s->sr = s->data_in[s->data_in_index++];
393#ifdef DEBUG_CUDA
394 printf("cuda: recv: %02x\n", s->sr);
395#endif
396 /* indicate end of transfer */
397 if (s->data_in_index >= s->data_in_size) {
398 s->b = (s->b | TREQ);
399 }
400 s->ifr |= SR_INT;
401 cuda_update_irq(s);
402 }
403 }
404 }
405 } else {
406 /* no transfer requested: handle sync case */
407 if ((s->last_b & TIP) && (s->b & TACK) != (s->last_b & TACK)) {
408 /* update TREQ state each time TACK change state */
409 if (s->b & TACK)
410 s->b = (s->b | TREQ);
411 else
412 s->b = (s->b & ~TREQ);
bellard267002c2004-06-03 18:46:20 +0000413 s->ifr |= SR_INT;
414 cuda_update_irq(s);
bellard819e7122004-06-21 16:47:13 +0000415 } else {
416 if (!(s->last_b & TIP)) {
417 /* handle end of host to cuda transfert */
418 packet_received = (s->data_out_index > 0);
419 /* always an IRQ at the end of transfert */
420 s->ifr |= SR_INT;
421 cuda_update_irq(s);
422 }
423 /* signal if there is data to read */
424 if (s->data_in_index < s->data_in_size) {
425 s->b = (s->b & ~TREQ);
426 }
bellard267002c2004-06-03 18:46:20 +0000427 }
428 }
429
bellard267002c2004-06-03 18:46:20 +0000430 s->last_acr = s->acr;
431 s->last_b = s->b;
bellard819e7122004-06-21 16:47:13 +0000432
433 /* NOTE: cuda_receive_packet_from_host() can call cuda_update()
434 recursively */
435 if (packet_received) {
436 len = s->data_out_index;
437 s->data_out_index = 0;
438 cuda_receive_packet_from_host(s, s->data_out, len);
439 }
bellard267002c2004-06-03 18:46:20 +0000440}
441
442static void cuda_send_packet_to_host(CUDAState *s,
443 const uint8_t *data, int len)
444{
bellard819e7122004-06-21 16:47:13 +0000445#ifdef DEBUG_CUDA_PACKET
446 {
447 int i;
448 printf("cuda_send_packet_to_host:\n");
449 for(i = 0; i < len; i++)
450 printf(" %02x", data[i]);
451 printf("\n");
452 }
453#endif
bellard267002c2004-06-03 18:46:20 +0000454 memcpy(s->data_in, data, len);
455 s->data_in_size = len;
456 s->data_in_index = 0;
457 cuda_update(s);
458 s->ifr |= SR_INT;
459 cuda_update_irq(s);
460}
461
462void adb_send_packet(ADBBusState *bus, const uint8_t *buf, int len)
463{
464 CUDAState *s = &cuda_state;
465 uint8_t data[16];
466
467 memcpy(data + 1, buf, len);
468 data[0] = ADB_PACKET;
469 cuda_send_packet_to_host(s, data, len + 1);
470}
471
472static void cuda_receive_packet(CUDAState *s,
473 const uint8_t *data, int len)
474{
475 uint8_t obuf[16];
476 int ti;
477
478 switch(data[0]) {
479 case CUDA_AUTOPOLL:
480 s->autopoll = data[1];
481 obuf[0] = CUDA_PACKET;
482 obuf[1] = data[1];
483 cuda_send_packet_to_host(s, obuf, 2);
484 break;
485 case CUDA_GET_TIME:
486 /* XXX: add time support ? */
bellard819e7122004-06-21 16:47:13 +0000487 ti = time(NULL);
bellard267002c2004-06-03 18:46:20 +0000488 obuf[0] = CUDA_PACKET;
489 obuf[1] = 0;
490 obuf[2] = 0;
491 obuf[3] = ti >> 24;
492 obuf[4] = ti >> 16;
493 obuf[5] = ti >> 8;
494 obuf[6] = ti;
495 cuda_send_packet_to_host(s, obuf, 7);
496 break;
497 case CUDA_SET_TIME:
498 case CUDA_FILE_SERVER_FLAG:
499 case CUDA_SET_DEVICE_LIST:
500 case CUDA_SET_AUTO_RATE:
501 case CUDA_SET_POWER_MESSAGES:
502 obuf[0] = CUDA_PACKET;
503 obuf[1] = 0;
504 cuda_send_packet_to_host(s, obuf, 2);
505 break;
506 default:
507 break;
508 }
509}
510
511static void cuda_receive_packet_from_host(CUDAState *s,
512 const uint8_t *data, int len)
513{
bellard819e7122004-06-21 16:47:13 +0000514#ifdef DEBUG_CUDA_PACKET
515 {
516 int i;
517 printf("cuda_receive_packet_to_host:\n");
518 for(i = 0; i < len; i++)
519 printf(" %02x", data[i]);
520 printf("\n");
521 }
522#endif
bellard267002c2004-06-03 18:46:20 +0000523 switch(data[0]) {
524 case ADB_PACKET:
525 adb_receive_packet(&adb_bus, data + 1, len - 1);
526 break;
527 case CUDA_PACKET:
528 cuda_receive_packet(s, data + 1, len - 1);
529 break;
530 }
531}
532
533static void cuda_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
534{
535}
536
537static void cuda_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
538{
539}
540
541static uint32_t cuda_readw (void *opaque, target_phys_addr_t addr)
542{
543 return 0;
544}
545
546static uint32_t cuda_readl (void *opaque, target_phys_addr_t addr)
547{
548 return 0;
549}
550
551static CPUWriteMemoryFunc *cuda_write[] = {
552 &cuda_writeb,
553 &cuda_writew,
554 &cuda_writel,
555};
556
557static CPUReadMemoryFunc *cuda_read[] = {
558 &cuda_readb,
559 &cuda_readw,
560 &cuda_readl,
561};
562
bellard819e7122004-06-21 16:47:13 +0000563int cuda_init(openpic_t *openpic, int irq)
bellard267002c2004-06-03 18:46:20 +0000564{
565 CUDAState *s = &cuda_state;
566 int cuda_mem_index;
567
bellard819e7122004-06-21 16:47:13 +0000568 s->openpic = openpic;
569 s->irq = irq;
570
bellard267002c2004-06-03 18:46:20 +0000571 s->timers[0].timer = qemu_new_timer(vm_clock, cuda_timer1, s);
bellard819e7122004-06-21 16:47:13 +0000572 s->timers[0].latch = 0x10000;
573 set_counter(s, &s->timers[0], 0xffff);
bellard267002c2004-06-03 18:46:20 +0000574 s->timers[1].latch = 0x10000;
bellard819e7122004-06-21 16:47:13 +0000575 s->ier = T1_INT | SR_INT;
576 set_counter(s, &s->timers[1], 0xffff);
bellard267002c2004-06-03 18:46:20 +0000577 cuda_mem_index = cpu_register_io_memory(0, cuda_read, cuda_write, s);
578 return cuda_mem_index;
579}