* sort the PowerPC target object files
[qemu] / hw / cuda.c
1 /*
2  * QEMU PowerMac CUDA device support
3  *
4  * Copyright (c) 2004-2007 Fabrice Bellard
5  * Copyright (c) 2007 Jocelyn Mayer
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 #include "vl.h"
26 #include "ppc_mac.h"
27
28 /* XXX: implement all timer modes */
29
30 //#define DEBUG_CUDA
31 //#define DEBUG_CUDA_PACKET
32
33 /* Bits in B data register: all active low */
34 #define TREQ            0x08            /* Transfer request (input) */
35 #define TACK            0x10            /* Transfer acknowledge (output) */
36 #define TIP             0x20            /* Transfer in progress (output) */
37
38 /* Bits in ACR */
39 #define SR_CTRL         0x1c            /* Shift register control bits */
40 #define SR_EXT          0x0c            /* Shift on external clock */
41 #define SR_OUT          0x10            /* Shift out if 1 */
42
43 /* Bits in IFR and IER */
44 #define IER_SET         0x80            /* set bits in IER */
45 #define IER_CLR         0               /* clear bits in IER */
46 #define SR_INT          0x04            /* Shift register full/empty */
47 #define T1_INT          0x40            /* Timer 1 interrupt */
48 #define T2_INT          0x20            /* Timer 2 interrupt */
49
50 /* Bits in ACR */
51 #define T1MODE          0xc0            /* Timer 1 mode */
52 #define T1MODE_CONT     0x40            /*  continuous interrupts */
53
54 /* commands (1st byte) */
55 #define ADB_PACKET      0
56 #define CUDA_PACKET     1
57 #define ERROR_PACKET    2
58 #define TIMER_PACKET    3
59 #define POWER_PACKET    4
60 #define MACIIC_PACKET   5
61 #define PMU_PACKET      6
62
63
64 /* CUDA commands (2nd byte) */
65 #define CUDA_WARM_START                 0x0
66 #define CUDA_AUTOPOLL                   0x1
67 #define CUDA_GET_6805_ADDR              0x2
68 #define CUDA_GET_TIME                   0x3
69 #define CUDA_GET_PRAM                   0x7
70 #define CUDA_SET_6805_ADDR              0x8
71 #define CUDA_SET_TIME                   0x9
72 #define CUDA_POWERDOWN                  0xa
73 #define CUDA_POWERUP_TIME               0xb
74 #define CUDA_SET_PRAM                   0xc
75 #define CUDA_MS_RESET                   0xd
76 #define CUDA_SEND_DFAC                  0xe
77 #define CUDA_BATTERY_SWAP_SENSE         0x10
78 #define CUDA_RESET_SYSTEM               0x11
79 #define CUDA_SET_IPL                    0x12
80 #define CUDA_FILE_SERVER_FLAG           0x13
81 #define CUDA_SET_AUTO_RATE              0x14
82 #define CUDA_GET_AUTO_RATE              0x16
83 #define CUDA_SET_DEVICE_LIST            0x19
84 #define CUDA_GET_DEVICE_LIST            0x1a
85 #define CUDA_SET_ONE_SECOND_MODE        0x1b
86 #define CUDA_SET_POWER_MESSAGES         0x21
87 #define CUDA_GET_SET_IIC                0x22
88 #define CUDA_WAKEUP                     0x23
89 #define CUDA_TIMER_TICKLE               0x24
90 #define CUDA_COMBINED_FORMAT_IIC        0x25
91
92 #define CUDA_TIMER_FREQ (4700000 / 6)
93 #define CUDA_ADB_POLL_FREQ 50
94
95 /* CUDA returns time_t's offset from Jan 1, 1904, not 1970 */
96 #define RTC_OFFSET                      2082844800
97
98 typedef struct CUDATimer {
99     int index;
100     uint16_t latch;
101     uint16_t counter_value; /* counter value at load time */
102     int64_t load_time;
103     int64_t next_irq_time;
104     QEMUTimer *timer;
105 } CUDATimer;
106
107 typedef struct CUDAState {
108     /* cuda registers */
109     uint8_t b;      /* B-side data */
110     uint8_t a;      /* A-side data */
111     uint8_t dirb;   /* B-side direction (1=output) */
112     uint8_t dira;   /* A-side direction (1=output) */
113     uint8_t sr;     /* Shift register */
114     uint8_t acr;    /* Auxiliary control register */
115     uint8_t pcr;    /* Peripheral control register */
116     uint8_t ifr;    /* Interrupt flag register */
117     uint8_t ier;    /* Interrupt enable register */
118     uint8_t anh;    /* A-side data, no handshake */
119
120     CUDATimer timers[2];
121
122     uint8_t last_b; /* last value of B register */
123     uint8_t last_acr; /* last value of B register */
124
125     int data_in_size;
126     int data_in_index;
127     int data_out_index;
128
129     qemu_irq irq;
130     uint8_t autopoll;
131     uint8_t data_in[128];
132     uint8_t data_out[16];
133     QEMUTimer *adb_poll_timer;
134 } CUDAState;
135
136 static CUDAState cuda_state;
137 ADBBusState adb_bus;
138
139 static void cuda_update(CUDAState *s);
140 static void cuda_receive_packet_from_host(CUDAState *s,
141                                           const uint8_t *data, int len);
142 static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
143                               int64_t current_time);
144
145 static void cuda_update_irq(CUDAState *s)
146 {
147     if (s->ifr & s->ier & (SR_INT | T1_INT)) {
148         qemu_irq_raise(s->irq);
149     } else {
150         qemu_irq_lower(s->irq);
151     }
152 }
153
154 static unsigned int get_counter(CUDATimer *s)
155 {
156     int64_t d;
157     unsigned int counter;
158
159     d = muldiv64(qemu_get_clock(vm_clock) - s->load_time,
160                  CUDA_TIMER_FREQ, ticks_per_sec);
161     if (s->index == 0) {
162         /* the timer goes down from latch to -1 (period of latch + 2) */
163         if (d <= (s->counter_value + 1)) {
164             counter = (s->counter_value - d) & 0xffff;
165         } else {
166             counter = (d - (s->counter_value + 1)) % (s->latch + 2);
167             counter = (s->latch - counter) & 0xffff;
168         }
169     } else {
170         counter = (s->counter_value - d) & 0xffff;
171     }
172     return counter;
173 }
174
175 static void set_counter(CUDAState *s, CUDATimer *ti, unsigned int val)
176 {
177 #ifdef DEBUG_CUDA
178     printf("cuda: T%d.counter=%d\n",
179            1 + (ti->timer == NULL), val);
180 #endif
181     ti->load_time = qemu_get_clock(vm_clock);
182     ti->counter_value = val;
183     cuda_timer_update(s, ti, ti->load_time);
184 }
185
186 static int64_t get_next_irq_time(CUDATimer *s, int64_t current_time)
187 {
188     int64_t d, next_time;
189     unsigned int counter;
190
191     /* current counter value */
192     d = muldiv64(current_time - s->load_time,
193                  CUDA_TIMER_FREQ, ticks_per_sec);
194     /* the timer goes down from latch to -1 (period of latch + 2) */
195     if (d <= (s->counter_value + 1)) {
196         counter = (s->counter_value - d) & 0xffff;
197     } else {
198         counter = (d - (s->counter_value + 1)) % (s->latch + 2);
199         counter = (s->latch - counter) & 0xffff;
200     }
201
202     /* Note: we consider the irq is raised on 0 */
203     if (counter == 0xffff) {
204         next_time = d + s->latch + 1;
205     } else if (counter == 0) {
206         next_time = d + s->latch + 2;
207     } else {
208         next_time = d + counter;
209     }
210 #if 0
211 #ifdef DEBUG_CUDA
212     printf("latch=%d counter=%" PRId64 " delta_next=%" PRId64 "\n",
213            s->latch, d, next_time - d);
214 #endif
215 #endif
216     next_time = muldiv64(next_time, ticks_per_sec, CUDA_TIMER_FREQ) +
217         s->load_time;
218     if (next_time <= current_time)
219         next_time = current_time + 1;
220     return next_time;
221 }
222
223 static void cuda_timer_update(CUDAState *s, CUDATimer *ti,
224                               int64_t current_time)
225 {
226     if (!ti->timer)
227         return;
228     if ((s->acr & T1MODE) != T1MODE_CONT) {
229         qemu_del_timer(ti->timer);
230     } else {
231         ti->next_irq_time = get_next_irq_time(ti, current_time);
232         qemu_mod_timer(ti->timer, ti->next_irq_time);
233     }
234 }
235
236 static void cuda_timer1(void *opaque)
237 {
238     CUDAState *s = opaque;
239     CUDATimer *ti = &s->timers[0];
240
241     cuda_timer_update(s, ti, ti->next_irq_time);
242     s->ifr |= T1_INT;
243     cuda_update_irq(s);
244 }
245
246 static uint32_t cuda_readb(void *opaque, target_phys_addr_t addr)
247 {
248     CUDAState *s = opaque;
249     uint32_t val;
250
251     addr = (addr >> 9) & 0xf;
252     switch(addr) {
253     case 0:
254         val = s->b;
255         break;
256     case 1:
257         val = s->a;
258         break;
259     case 2:
260         val = s->dirb;
261         break;
262     case 3:
263         val = s->dira;
264         break;
265     case 4:
266         val = get_counter(&s->timers[0]) & 0xff;
267         s->ifr &= ~T1_INT;
268         cuda_update_irq(s);
269         break;
270     case 5:
271         val = get_counter(&s->timers[0]) >> 8;
272         cuda_update_irq(s);
273         break;
274     case 6:
275         val = s->timers[0].latch & 0xff;
276         break;
277     case 7:
278         /* XXX: check this */
279         val = (s->timers[0].latch >> 8) & 0xff;
280         break;
281     case 8:
282         val = get_counter(&s->timers[1]) & 0xff;
283         s->ifr &= ~T2_INT;
284         break;
285     case 9:
286         val = get_counter(&s->timers[1]) >> 8;
287         break;
288     case 10:
289         val = s->sr;
290         s->ifr &= ~SR_INT;
291         cuda_update_irq(s);
292         break;
293     case 11:
294         val = s->acr;
295         break;
296     case 12:
297         val = s->pcr;
298         break;
299     case 13:
300         val = s->ifr;
301         if (s->ifr & s->ier)
302             val |= 0x80;
303         break;
304     case 14:
305         val = s->ier | 0x80;
306         break;
307     default:
308     case 15:
309         val = s->anh;
310         break;
311     }
312 #ifdef DEBUG_CUDA
313     if (addr != 13 || val != 0)
314         printf("cuda: read: reg=0x%x val=%02x\n", addr, val);
315 #endif
316     return val;
317 }
318
319 static void cuda_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
320 {
321     CUDAState *s = opaque;
322
323     addr = (addr >> 9) & 0xf;
324 #ifdef DEBUG_CUDA
325     printf("cuda: write: reg=0x%x val=%02x\n", addr, val);
326 #endif
327
328     switch(addr) {
329     case 0:
330         s->b = val;
331         cuda_update(s);
332         break;
333     case 1:
334         s->a = val;
335         break;
336     case 2:
337         s->dirb = val;
338         break;
339     case 3:
340         s->dira = val;
341         break;
342     case 4:
343         s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
344         cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
345         break;
346     case 5:
347         s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
348         s->ifr &= ~T1_INT;
349         set_counter(s, &s->timers[0], s->timers[0].latch);
350         break;
351     case 6:
352         s->timers[0].latch = (s->timers[0].latch & 0xff00) | val;
353         cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
354         break;
355     case 7:
356         s->timers[0].latch = (s->timers[0].latch & 0xff) | (val << 8);
357         s->ifr &= ~T1_INT;
358         cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
359         break;
360     case 8:
361         s->timers[1].latch = val;
362         set_counter(s, &s->timers[1], val);
363         break;
364     case 9:
365         set_counter(s, &s->timers[1], (val << 8) | s->timers[1].latch);
366         break;
367     case 10:
368         s->sr = val;
369         break;
370     case 11:
371         s->acr = val;
372         cuda_timer_update(s, &s->timers[0], qemu_get_clock(vm_clock));
373         cuda_update(s);
374         break;
375     case 12:
376         s->pcr = val;
377         break;
378     case 13:
379         /* reset bits */
380         s->ifr &= ~val;
381         cuda_update_irq(s);
382         break;
383     case 14:
384         if (val & IER_SET) {
385             /* set bits */
386             s->ier |= val & 0x7f;
387         } else {
388             /* reset bits */
389             s->ier &= ~val;
390         }
391         cuda_update_irq(s);
392         break;
393     default:
394     case 15:
395         s->anh = val;
396         break;
397     }
398 }
399
400 /* NOTE: TIP and TREQ are negated */
401 static void cuda_update(CUDAState *s)
402 {
403     int packet_received, len;
404
405     packet_received = 0;
406     if (!(s->b & TIP)) {
407         /* transfer requested from host */
408
409         if (s->acr & SR_OUT) {
410             /* data output */
411             if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
412                 if (s->data_out_index < sizeof(s->data_out)) {
413 #ifdef DEBUG_CUDA
414                     printf("cuda: send: %02x\n", s->sr);
415 #endif
416                     s->data_out[s->data_out_index++] = s->sr;
417                     s->ifr |= SR_INT;
418                     cuda_update_irq(s);
419                 }
420             }
421         } else {
422             if (s->data_in_index < s->data_in_size) {
423                 /* data input */
424                 if ((s->b & (TACK | TIP)) != (s->last_b & (TACK | TIP))) {
425                     s->sr = s->data_in[s->data_in_index++];
426 #ifdef DEBUG_CUDA
427                     printf("cuda: recv: %02x\n", s->sr);
428 #endif
429                     /* indicate end of transfer */
430                     if (s->data_in_index >= s->data_in_size) {
431                         s->b = (s->b | TREQ);
432                     }
433                     s->ifr |= SR_INT;
434                     cuda_update_irq(s);
435                 }
436             }
437         }
438     } else {
439         /* no transfer requested: handle sync case */
440         if ((s->last_b & TIP) && (s->b & TACK) != (s->last_b & TACK)) {
441             /* update TREQ state each time TACK change state */
442             if (s->b & TACK)
443                 s->b = (s->b | TREQ);
444             else
445                 s->b = (s->b & ~TREQ);
446             s->ifr |= SR_INT;
447             cuda_update_irq(s);
448         } else {
449             if (!(s->last_b & TIP)) {
450                 /* handle end of host to cuda transfer */
451                 packet_received = (s->data_out_index > 0);
452                 /* always an IRQ at the end of transfer */
453                 s->ifr |= SR_INT;
454                 cuda_update_irq(s);
455             }
456             /* signal if there is data to read */
457             if (s->data_in_index < s->data_in_size) {
458                 s->b = (s->b & ~TREQ);
459             }
460         }
461     }
462
463     s->last_acr = s->acr;
464     s->last_b = s->b;
465
466     /* NOTE: cuda_receive_packet_from_host() can call cuda_update()
467        recursively */
468     if (packet_received) {
469         len = s->data_out_index;
470         s->data_out_index = 0;
471         cuda_receive_packet_from_host(s, s->data_out, len);
472     }
473 }
474
475 static void cuda_send_packet_to_host(CUDAState *s,
476                                      const uint8_t *data, int len)
477 {
478 #ifdef DEBUG_CUDA_PACKET
479     {
480         int i;
481         printf("cuda_send_packet_to_host:\n");
482         for(i = 0; i < len; i++)
483             printf(" %02x", data[i]);
484         printf("\n");
485     }
486 #endif
487     memcpy(s->data_in, data, len);
488     s->data_in_size = len;
489     s->data_in_index = 0;
490     cuda_update(s);
491     s->ifr |= SR_INT;
492     cuda_update_irq(s);
493 }
494
495 static void cuda_adb_poll(void *opaque)
496 {
497     CUDAState *s = opaque;
498     uint8_t obuf[ADB_MAX_OUT_LEN + 2];
499     int olen;
500
501     olen = adb_poll(&adb_bus, obuf + 2);
502     if (olen > 0) {
503         obuf[0] = ADB_PACKET;
504         obuf[1] = 0x40; /* polled data */
505         cuda_send_packet_to_host(s, obuf, olen + 2);
506     }
507     qemu_mod_timer(s->adb_poll_timer,
508                    qemu_get_clock(vm_clock) +
509                    (ticks_per_sec / CUDA_ADB_POLL_FREQ));
510 }
511
512 static void cuda_receive_packet(CUDAState *s,
513                                 const uint8_t *data, int len)
514 {
515     uint8_t obuf[16];
516     int ti, autopoll;
517
518     switch(data[0]) {
519     case CUDA_AUTOPOLL:
520         autopoll = (data[1] != 0);
521         if (autopoll != s->autopoll) {
522             s->autopoll = autopoll;
523             if (autopoll) {
524                 qemu_mod_timer(s->adb_poll_timer,
525                                qemu_get_clock(vm_clock) +
526                                (ticks_per_sec / CUDA_ADB_POLL_FREQ));
527             } else {
528                 qemu_del_timer(s->adb_poll_timer);
529             }
530         }
531         obuf[0] = CUDA_PACKET;
532         obuf[1] = data[1];
533         cuda_send_packet_to_host(s, obuf, 2);
534         break;
535     case CUDA_GET_TIME:
536     case CUDA_SET_TIME:
537         /* XXX: add time support ? */
538         ti = time(NULL) + RTC_OFFSET;
539         obuf[0] = CUDA_PACKET;
540         obuf[1] = 0;
541         obuf[2] = 0;
542         obuf[3] = ti >> 24;
543         obuf[4] = ti >> 16;
544         obuf[5] = ti >> 8;
545         obuf[6] = ti;
546         cuda_send_packet_to_host(s, obuf, 7);
547         break;
548     case CUDA_FILE_SERVER_FLAG:
549     case CUDA_SET_DEVICE_LIST:
550     case CUDA_SET_AUTO_RATE:
551     case CUDA_SET_POWER_MESSAGES:
552         obuf[0] = CUDA_PACKET;
553         obuf[1] = 0;
554         cuda_send_packet_to_host(s, obuf, 2);
555         break;
556     case CUDA_POWERDOWN:
557         obuf[0] = CUDA_PACKET;
558         obuf[1] = 0;
559         cuda_send_packet_to_host(s, obuf, 2);
560         qemu_system_shutdown_request();
561         break;
562     case CUDA_RESET_SYSTEM:
563         obuf[0] = CUDA_PACKET;
564         obuf[1] = 0;
565         cuda_send_packet_to_host(s, obuf, 2);
566         qemu_system_reset_request();
567         break;
568     default:
569         break;
570     }
571 }
572
573 static void cuda_receive_packet_from_host(CUDAState *s,
574                                           const uint8_t *data, int len)
575 {
576 #ifdef DEBUG_CUDA_PACKET
577     {
578         int i;
579         printf("cuda_receive_packet_from_host:\n");
580         for(i = 0; i < len; i++)
581             printf(" %02x", data[i]);
582         printf("\n");
583     }
584 #endif
585     switch(data[0]) {
586     case ADB_PACKET:
587         {
588             uint8_t obuf[ADB_MAX_OUT_LEN + 2];
589             int olen;
590             olen = adb_request(&adb_bus, obuf + 2, data + 1, len - 1);
591             if (olen > 0) {
592                 obuf[0] = ADB_PACKET;
593                 obuf[1] = 0x00;
594             } else {
595                 /* error */
596                 obuf[0] = ADB_PACKET;
597                 obuf[1] = -olen;
598                 olen = 0;
599             }
600             cuda_send_packet_to_host(s, obuf, olen + 2);
601         }
602         break;
603     case CUDA_PACKET:
604         cuda_receive_packet(s, data + 1, len - 1);
605         break;
606     }
607 }
608
609 static void cuda_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
610 {
611 }
612
613 static void cuda_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
614 {
615 }
616
617 static uint32_t cuda_readw (void *opaque, target_phys_addr_t addr)
618 {
619     return 0;
620 }
621
622 static uint32_t cuda_readl (void *opaque, target_phys_addr_t addr)
623 {
624     return 0;
625 }
626
627 static CPUWriteMemoryFunc *cuda_write[] = {
628     &cuda_writeb,
629     &cuda_writew,
630     &cuda_writel,
631 };
632
633 static CPUReadMemoryFunc *cuda_read[] = {
634     &cuda_readb,
635     &cuda_readw,
636     &cuda_readl,
637 };
638
639 void cuda_init (int *cuda_mem_index, qemu_irq irq)
640 {
641     CUDAState *s = &cuda_state;
642
643     s->irq = irq;
644
645     s->timers[0].index = 0;
646     s->timers[0].timer = qemu_new_timer(vm_clock, cuda_timer1, s);
647     s->timers[0].latch = 0xffff;
648     set_counter(s, &s->timers[0], 0xffff);
649
650     s->timers[1].index = 1;
651     s->timers[1].latch = 0;
652     //    s->ier = T1_INT | SR_INT;
653     s->ier = 0;
654     set_counter(s, &s->timers[1], 0xffff);
655
656     s->adb_poll_timer = qemu_new_timer(vm_clock, cuda_adb_poll, s);
657     *cuda_mem_index = cpu_register_io_memory(0, cuda_read, cuda_write, s);
658 }