PCI irq in sync with new Bochs BIOS
[qemu] / hw / cuda.c
1 /*
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
26 //#define DEBUG_CUDA
27 //#define DEBUG_CUDA_PACKET
28
29 /* 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 #define CUDA_ADB_POLL_FREQ 50
89
90 /* CUDA returns time_t's offset from Jan 1, 1904, not 1970 */
91 #define RTC_OFFSET                      2082844800
92
93 typedef struct CUDATimer {
94     unsigned int latch;
95     uint16_t counter_value; /* counter value at load time */
96     int64_t load_time;
97     int64_t next_irq_time;
98     QEMUTimer *timer;
99 } CUDATimer;
100
101 typedef struct CUDAState {
102     /* cuda registers */
103     uint8_t b;      /* B-side data */
104     uint8_t a;      /* A-side data */
105     uint8_t dirb;   /* B-side direction (1=output) */
106     uint8_t dira;   /* A-side direction (1=output) */
107     uint8_t sr;     /* Shift register */
108     uint8_t acr;    /* Auxiliary control register */
109     uint8_t pcr;    /* Peripheral control register */
110     uint8_t ifr;    /* Interrupt flag register */
111     uint8_t ier;    /* Interrupt enable register */
112     uint8_t anh;    /* A-side data, no handshake */
113
114     CUDATimer timers[2];
115     
116     uint8_t last_b; /* last value of B register */
117     uint8_t last_acr; /* last value of B register */
118     
119     int data_in_size;
120     int data_in_index;
121     int data_out_index;
122
123     int irq;
124     openpic_t *openpic;
125     uint8_t autopoll;
126     uint8_t data_in[128];
127     uint8_t data_out[16];
128     QEMUTimer *adb_poll_timer;
129 } CUDAState;
130
131 static CUDAState cuda_state;
132 ADBBusState adb_bus;
133
134 static void cuda_update(CUDAState *s);
135 static void cuda_receive_packet_from_host(CUDAState *s, 
136                                           const uint8_t *data, int len);
137 static void cuda_timer_update(CUDAState *s, CUDATimer *ti, 
138                               int64_t current_time);
139
140 static void cuda_update_irq(CUDAState *s)
141 {
142     if (s->ifr & s->ier & (SR_INT | T1_INT)) {
143         openpic_set_irq(s->openpic, s->irq, 1);
144     } else {
145         openpic_set_irq(s->openpic, s->irq, 0);
146     }
147 }
148
149 static unsigned int get_counter(CUDATimer *s)
150 {
151     int64_t d;
152     unsigned int counter;
153
154     d = muldiv64(qemu_get_clock(vm_clock) - s->load_time, 
155                  CUDA_TIMER_FREQ, ticks_per_sec);
156     if (d <= s->counter_value) {
157         counter = d;
158     } else {
159         counter = s->latch - 1 - ((d - s->counter_value) % s->latch);
160     }
161     return counter;
162 }
163
164 static void set_counter(CUDAState *s, CUDATimer *ti, unsigned int val)
165 {
166 #ifdef DEBUG_CUDA
167     printf("cuda: T%d.counter=%d\n",
168            1 + (ti->timer == NULL), val);
169 #endif
170     ti->load_time = qemu_get_clock(vm_clock);
171     ti->counter_value = val;
172     cuda_timer_update(s, ti, ti->load_time);
173 }
174
175 static int64_t get_next_irq_time(CUDATimer *s, int64_t current_time)
176 {
177     int64_t d, next_time, base;
178     /* current counter value */
179     d = muldiv64(current_time - s->load_time, 
180                  CUDA_TIMER_FREQ, ticks_per_sec);
181     if (d <= s->counter_value) {
182         next_time = s->counter_value + 1;
183     } else {
184         base = ((d - s->counter_value) / s->latch);
185         base = (base * s->latch) + s->counter_value;
186         next_time = base + s->latch;
187     }
188 #ifdef DEBUG_CUDA
189     printf("latch=%d counter=%lld delta_next=%lld\n", 
190            s->latch, d, next_time - d);
191 #endif
192     next_time = muldiv64(next_time, ticks_per_sec, CUDA_TIMER_FREQ) + 
193         s->load_time;
194     if (next_time <= current_time)
195         next_time = current_time + 1;
196     return next_time;
197 }
198
199 static void cuda_timer_update(CUDAState *s, CUDATimer *ti, 
200                               int64_t current_time)
201 {
202     if (!ti->timer)
203         return;
204     if ((s->acr & T1MODE) != T1MODE_CONT) {
205         qemu_del_timer(ti->timer);
206     } else {
207         ti->next_irq_time = get_next_irq_time(ti, current_time);
208         qemu_mod_timer(ti->timer, ti->next_irq_time);
209     }
210 }
211
212 static void cuda_timer1(void *opaque)
213 {
214     CUDAState *s = opaque;
215     CUDATimer *ti = &s->timers[0];
216
217     cuda_timer_update(s, ti, ti->next_irq_time);
218     s->ifr |= T1_INT;
219     cuda_update_irq(s);
220 }
221
222 static uint32_t cuda_readb(void *opaque, target_phys_addr_t addr)
223 {
224     CUDAState *s = opaque;
225     uint32_t val;
226
227     addr = (addr >> 9) & 0xf;
228     switch(addr) {
229     case 0:
230         val = s->b;
231         break;
232     case 1:
233         val = s->a;
234         break;
235     case 2:
236         val = s->dirb;
237         break;
238     case 3:
239         val = s->dira;
240         break;
241     case 4:
242         val = get_counter(&s->timers[0]) & 0xff;
243         s->ifr &= ~T1_INT;
244         cuda_update_irq(s);
245         break;
246     case 5:
247         val = get_counter(&s->timers[0]) >> 8;
248         s->ifr &= ~T1_INT;
249         cuda_update_irq(s);
250         break;
251     case 6:
252         val = s->timers[0].latch & 0xff;
253         break;
254     case 7:
255         val = (s->timers[0].latch >> 8) & 0xff;
256         break;
257     case 8:
258         val = get_counter(&s->timers[1]) & 0xff;
259         break;
260     case 9:
261         val = get_counter(&s->timers[1]) >> 8;
262         break;
263     case 10:
264         val = s->sr;
265         s->ifr &= ~SR_INT;
266         cuda_update_irq(s);
267         break;
268     case 11:
269         val = s->acr;
270         break;
271     case 12:
272         val = s->pcr;
273         break;
274     case 13:
275         val = s->ifr;
276         break;
277     case 14:
278         val = s->ier;
279         break;
280     default:
281     case 15:
282         val = s->anh;
283         break;
284     }
285 #ifdef DEBUG_CUDA
286     if (addr != 13 || val != 0)
287         printf("cuda: read: reg=0x%x val=%02x\n", addr, val);
288 #endif
289     return val;
290 }
291
292 static void cuda_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
293 {
294     CUDAState *s = opaque;
295     
296     addr = (addr >> 9) & 0xf;
297 #ifdef DEBUG_CUDA
298     printf("cuda: write: reg=0x%x val=%02x\n", addr, val);
299 #endif
300
301     switch(addr) {
302     case 0:
303         s->b = val;
304         cuda_update(s);
305         break;
306     case 1:
307         s->a = val;
308         break;
309     case 2:
310         s->dirb = val;
311         break;
312     case 3:
313         s->dira = val;
314         break;
315     case 4:
316         val = val | (get_counter(&s->timers[0]) & 0xff00);
317         set_counter(s, &s->timers[0], val);
318         break;
319     case 5:
320         val = (val << 8) |  (get_counter(&s->timers[0]) & 0xff);
321         set_counter(s, &s->timers[0], val);
322         break;
323     case 6:
324         s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
325         cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
326         break;
327     case 7:
328         s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
329         cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
330         break;
331     case 8:
332         val = val | (get_counter(&s->timers[1]) & 0xff00);
333         set_counter(s, &s->timers[1], val);
334         break;
335     case 9:
336         val = (val << 8) |  (get_counter(&s->timers[1]) & 0xff);
337         set_counter(s, &s->timers[1], val);
338         break;
339     case 10:
340         s->sr = val;
341         break;
342     case 11:
343         s->acr = val;
344         cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
345         cuda_update(s);
346         break;
347     case 12:
348         s->pcr = val;
349         break;
350     case 13:
351         /* reset bits */
352         s->ifr &= ~val;
353         cuda_update_irq(s);
354         break;
355     case 14:
356         if (val & IER_SET) {
357             /* set bits */
358             s->ier |= val & 0x7f;
359         } else {
360             /* reset bits */
361             s->ier &= ~val;
362         }
363         cuda_update_irq(s);
364         break;
365     default:
366     case 15:
367         s->anh = val;
368         break;
369     }
370 }
371
372 /* NOTE: TIP and TREQ are negated */
373 static void cuda_update(CUDAState *s)
374 {
375     int packet_received, len;
376
377     packet_received = 0;
378     if (!(s->b & TIP)) {
379         /* transfer requested from host */
380
381         if (s->acr & SR_OUT) {
382             /* data output */
383             if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
384                 if (s->data_out_index < sizeof(s->data_out)) {
385 #ifdef DEBUG_CUDA
386                     printf("cuda: send: %02x\n", s->sr);
387 #endif
388                     s->data_out[s->data_out_index++] = s->sr;
389                     s->ifr |= SR_INT;
390                     cuda_update_irq(s);
391                 }
392             }
393         } else {
394             if (s->data_in_index < s->data_in_size) {
395                 /* data input */
396                 if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
397                     s->sr = s->data_in[s->data_in_index++];
398 #ifdef DEBUG_CUDA
399                     printf("cuda: recv: %02x\n", s->sr);
400 #endif
401                     /* indicate end of transfer */
402                     if (s->data_in_index >= s->data_in_size) {
403                         s->b = (s->b | TREQ);
404                     }
405                     s->ifr |= SR_INT;
406                     cuda_update_irq(s);
407                 }
408             }
409         }
410     } else {
411         /* no transfer requested: handle sync case */
412         if ((s->last_b & TIP) && (s->b & TACK) != (s->last_b & TACK)) {
413             /* update TREQ state each time TACK change state */
414             if (s->b & TACK)
415                 s->b = (s->b | TREQ);
416             else
417                 s->b = (s->b & ~TREQ);
418             s->ifr |= SR_INT;
419             cuda_update_irq(s);
420         } else {
421             if (!(s->last_b & TIP)) {
422                 /* handle end of host to cuda transfert */
423                 packet_received = (s->data_out_index > 0);
424                 /* always an IRQ at the end of transfert */
425                 s->ifr |= SR_INT;
426                 cuda_update_irq(s);
427             }
428             /* signal if there is data to read */
429             if (s->data_in_index < s->data_in_size) {
430                 s->b = (s->b & ~TREQ);
431             }
432         }
433     }
434
435     s->last_acr = s->acr;
436     s->last_b = s->b;
437
438     /* NOTE: cuda_receive_packet_from_host() can call cuda_update()
439        recursively */
440     if (packet_received) {
441         len = s->data_out_index;
442         s->data_out_index = 0;
443         cuda_receive_packet_from_host(s, s->data_out, len);
444     }
445 }
446
447 static void cuda_send_packet_to_host(CUDAState *s, 
448                                      const uint8_t *data, int len)
449 {
450 #ifdef DEBUG_CUDA_PACKET
451     {
452         int i;
453         printf("cuda_send_packet_to_host:\n");
454         for(i = 0; i < len; i++)
455             printf(" %02x", data[i]);
456         printf("\n");
457     }
458 #endif
459     memcpy(s->data_in, data, len);
460     s->data_in_size = len;
461     s->data_in_index = 0;
462     cuda_update(s);
463     s->ifr |= SR_INT;
464     cuda_update_irq(s);
465 }
466
467 static void cuda_adb_poll(void *opaque)
468 {
469     CUDAState *s = opaque;
470     uint8_t obuf[ADB_MAX_OUT_LEN + 2];
471     int olen;
472
473     olen = adb_poll(&adb_bus, obuf + 2);
474     if (olen > 0) {
475         obuf[0] = ADB_PACKET;
476         obuf[1] = 0x40; /* polled data */
477         cuda_send_packet_to_host(s, obuf, olen + 2);
478     }
479     qemu_mod_timer(s->adb_poll_timer, 
480                    qemu_get_clock(vm_clock) + 
481                    (ticks_per_sec / CUDA_ADB_POLL_FREQ));
482 }
483
484 static void cuda_receive_packet(CUDAState *s, 
485                                 const uint8_t *data, int len)
486 {
487     uint8_t obuf[16];
488     int ti, autopoll;
489
490     switch(data[0]) {
491     case CUDA_AUTOPOLL:
492         autopoll = (data[1] != 0);
493         if (autopoll != s->autopoll) {
494             s->autopoll = autopoll;
495             if (autopoll) {
496                 qemu_mod_timer(s->adb_poll_timer, 
497                                qemu_get_clock(vm_clock) + 
498                                (ticks_per_sec / CUDA_ADB_POLL_FREQ));
499             } else {
500                 qemu_del_timer(s->adb_poll_timer);
501             }
502         }
503         obuf[0] = CUDA_PACKET;
504         obuf[1] = data[1];
505         cuda_send_packet_to_host(s, obuf, 2);
506         break;
507     case CUDA_GET_TIME:
508         /* XXX: add time support ? */
509         ti = time(NULL) + RTC_OFFSET;
510         obuf[0] = CUDA_PACKET;
511         obuf[1] = 0;
512         obuf[2] = 0;
513         obuf[3] = ti >> 24;
514         obuf[4] = ti >> 16;
515         obuf[5] = ti >> 8;
516         obuf[6] = ti;
517         cuda_send_packet_to_host(s, obuf, 7);
518         break;
519     case CUDA_SET_TIME:
520     case CUDA_FILE_SERVER_FLAG:
521     case CUDA_SET_DEVICE_LIST:
522     case CUDA_SET_AUTO_RATE:
523     case CUDA_SET_POWER_MESSAGES:
524         obuf[0] = CUDA_PACKET;
525         obuf[1] = 0;
526         cuda_send_packet_to_host(s, obuf, 2);
527         break;
528     case CUDA_POWERDOWN:
529         obuf[0] = CUDA_PACKET;
530         obuf[1] = 0;
531         cuda_send_packet_to_host(s, obuf, 2);
532         qemu_system_shutdown_request();
533         break;
534     default:
535         break;
536     }
537 }
538
539 static void cuda_receive_packet_from_host(CUDAState *s, 
540                                           const uint8_t *data, int len)
541 {
542 #ifdef DEBUG_CUDA_PACKET
543     {
544         int i;
545         printf("cuda_receive_packet_to_host:\n");
546         for(i = 0; i < len; i++)
547             printf(" %02x", data[i]);
548         printf("\n");
549     }
550 #endif
551     switch(data[0]) {
552     case ADB_PACKET:
553         {
554             uint8_t obuf[ADB_MAX_OUT_LEN + 2];
555             int olen;
556             olen = adb_request(&adb_bus, obuf + 2, data + 1, len - 1);
557             if (olen > 0) {
558                 obuf[0] = ADB_PACKET;
559                 obuf[1] = 0x00;
560             } else {
561                 /* error */
562                 obuf[0] = ADB_PACKET;
563                 obuf[1] = -olen;
564                 olen = 0;
565             }
566             cuda_send_packet_to_host(s, obuf, olen + 2);
567         }
568         break;
569     case CUDA_PACKET:
570         cuda_receive_packet(s, data + 1, len - 1);
571         break;
572     }
573 }
574
575 static void cuda_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
576 {
577 }
578
579 static void cuda_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
580 {
581 }
582
583 static uint32_t cuda_readw (void *opaque, target_phys_addr_t addr)
584 {
585     return 0;
586 }
587
588 static uint32_t cuda_readl (void *opaque, target_phys_addr_t addr)
589 {
590     return 0;
591 }
592
593 static CPUWriteMemoryFunc *cuda_write[] = {
594     &cuda_writeb,
595     &cuda_writew,
596     &cuda_writel,
597 };
598
599 static CPUReadMemoryFunc *cuda_read[] = {
600     &cuda_readb,
601     &cuda_readw,
602     &cuda_readl,
603 };
604
605 int cuda_init(openpic_t *openpic, int irq)
606 {
607     CUDAState *s = &cuda_state;
608     int cuda_mem_index;
609
610     s->openpic = openpic;
611     s->irq = irq;
612
613     s->timers[0].timer = qemu_new_timer(vm_clock, cuda_timer1, s);
614     s->timers[0].latch = 0x10000;
615     set_counter(s, &s->timers[0], 0xffff);
616     s->timers[1].latch = 0x10000;
617     s->ier = T1_INT | SR_INT;
618     set_counter(s, &s->timers[1], 0xffff);
619
620     s->adb_poll_timer = qemu_new_timer(vm_clock, cuda_adb_poll, s);
621     cuda_mem_index = cpu_register_io_memory(0, cuda_read, cuda_write, s);
622     return cuda_mem_index;
623 }