PowerPC prep/chrp/pmac support
[qemu] / vl.h
1 /*
2  * QEMU System Emulator header
3  * 
4  * Copyright (c) 2003 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 #ifndef VL_H
25 #define VL_H
26
27 /* we put basic includes here to avoid repeating them in device drivers */
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <inttypes.h>
33 #include <time.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/stat.h>
39
40 #ifndef O_LARGEFILE
41 #define O_LARGEFILE 0
42 #endif
43 #ifndef O_BINARY
44 #define O_BINARY 0
45 #endif
46
47 #ifdef _WIN32
48 #define lseek64 _lseeki64
49 #endif
50
51 #include "cpu.h"
52
53 #ifndef glue
54 #define xglue(x, y) x ## y
55 #define glue(x, y) xglue(x, y)
56 #define stringify(s)    tostring(s)
57 #define tostring(s)     #s
58 #endif
59
60 #if defined(WORDS_BIGENDIAN)
61 static inline uint32_t be32_to_cpu(uint32_t v)
62 {
63     return v;
64 }
65
66 static inline uint16_t be16_to_cpu(uint16_t v)
67 {
68     return v;
69 }
70
71 static inline uint32_t cpu_to_be32(uint32_t v)
72 {
73     return v;
74 }
75
76 static inline uint16_t cpu_to_be16(uint16_t v)
77 {
78     return v;
79 }
80
81 static inline uint32_t le32_to_cpu(uint32_t v)
82 {
83     return bswap32(v);
84 }
85
86 static inline uint16_t le16_to_cpu(uint16_t v)
87 {
88     return bswap16(v);
89 }
90
91 static inline uint32_t cpu_to_le32(uint32_t v)
92 {
93     return bswap32(v);
94 }
95
96 static inline uint16_t cpu_to_le16(uint16_t v)
97 {
98     return bswap16(v);
99 }
100
101 #else
102
103 static inline uint32_t be32_to_cpu(uint32_t v)
104 {
105     return bswap32(v);
106 }
107
108 static inline uint16_t be16_to_cpu(uint16_t v)
109 {
110     return bswap16(v);
111 }
112
113 static inline uint32_t cpu_to_be32(uint32_t v)
114 {
115     return bswap32(v);
116 }
117
118 static inline uint16_t cpu_to_be16(uint16_t v)
119 {
120     return bswap16(v);
121 }
122
123 static inline uint32_t le32_to_cpu(uint32_t v)
124 {
125     return v;
126 }
127
128 static inline uint16_t le16_to_cpu(uint16_t v)
129 {
130     return v;
131 }
132
133 static inline uint32_t cpu_to_le32(uint32_t v)
134 {
135     return v;
136 }
137
138 static inline uint16_t cpu_to_le16(uint16_t v)
139 {
140     return v;
141 }
142 #endif
143
144
145 /* vl.c */
146 extern int reset_requested;
147
148 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
149
150 void hw_error(const char *fmt, ...);
151
152 int load_image(const char *filename, uint8_t *addr);
153 extern const char *bios_dir;
154
155 void pstrcpy(char *buf, int buf_size, const char *str);
156 char *pstrcat(char *buf, int buf_size, const char *s);
157
158 int serial_open_device(void);
159
160 extern int vm_running;
161
162 typedef void VMStopHandler(void *opaque, int reason);
163
164 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque);
165 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque);
166
167 void vm_start(void);
168 void vm_stop(int reason);
169
170 extern int audio_enabled;
171 extern int ram_size;
172 extern int bios_size;
173
174 /* XXX: make it dynamic */
175 #if defined (TARGET_PPC)
176 #define BIOS_SIZE (512 * 1024)
177 #else
178 #define BIOS_SIZE 0
179 #endif
180
181 /* async I/O support */
182
183 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
184 typedef int IOCanRWHandler(void *opaque);
185
186 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read, 
187                              IOReadHandler *fd_read, void *opaque);
188 void qemu_del_fd_read_handler(int fd);
189
190 /* network redirectors support */
191
192 #define MAX_NICS 8
193
194 typedef struct NetDriverState {
195     int index; /* index number in QEMU */
196     uint8_t macaddr[6];
197     char ifname[16];
198     void (*send_packet)(struct NetDriverState *nd, 
199                         const uint8_t *buf, int size);
200     void (*add_read_packet)(struct NetDriverState *nd, 
201                             IOCanRWHandler *fd_can_read, 
202                             IOReadHandler *fd_read, void *opaque);
203     /* tun specific data */
204     int fd;
205     /* slirp specific data */
206 } NetDriverState;
207
208 extern int nb_nics;
209 extern NetDriverState nd_table[MAX_NICS];
210
211 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size);
212 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read, 
213                           IOReadHandler *fd_read, void *opaque);
214
215 /* timers */
216
217 typedef struct QEMUClock QEMUClock;
218 typedef struct QEMUTimer QEMUTimer;
219 typedef void QEMUTimerCB(void *opaque);
220
221 /* The real time clock should be used only for stuff which does not
222    change the virtual machine state, as it is run even if the virtual
223    machine is stopped. The real time clock has a frequency of 1000
224    Hz. */
225 extern QEMUClock *rt_clock;
226
227 /* Rge virtual clock is only run during the emulation. It is stopped
228    when the virtual machine is stopped. Virtual timers use a high
229    precision clock, usually cpu cycles (use ticks_per_sec). */
230 extern QEMUClock *vm_clock;
231
232 int64_t qemu_get_clock(QEMUClock *clock);
233
234 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque);
235 void qemu_free_timer(QEMUTimer *ts);
236 void qemu_del_timer(QEMUTimer *ts);
237 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
238 int qemu_timer_pending(QEMUTimer *ts);
239
240 extern int64_t ticks_per_sec;
241 extern int pit_min_timer_count;
242
243 void cpu_enable_ticks(void);
244 void cpu_disable_ticks(void);
245
246 /* VM Load/Save */
247
248 typedef FILE QEMUFile;
249
250 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
251 void qemu_put_byte(QEMUFile *f, int v);
252 void qemu_put_be16(QEMUFile *f, unsigned int v);
253 void qemu_put_be32(QEMUFile *f, unsigned int v);
254 void qemu_put_be64(QEMUFile *f, uint64_t v);
255 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size);
256 int qemu_get_byte(QEMUFile *f);
257 unsigned int qemu_get_be16(QEMUFile *f);
258 unsigned int qemu_get_be32(QEMUFile *f);
259 uint64_t qemu_get_be64(QEMUFile *f);
260
261 static inline void qemu_put_be64s(QEMUFile *f, const uint64_t *pv)
262 {
263     qemu_put_be64(f, *pv);
264 }
265
266 static inline void qemu_put_be32s(QEMUFile *f, const uint32_t *pv)
267 {
268     qemu_put_be32(f, *pv);
269 }
270
271 static inline void qemu_put_be16s(QEMUFile *f, const uint16_t *pv)
272 {
273     qemu_put_be16(f, *pv);
274 }
275
276 static inline void qemu_put_8s(QEMUFile *f, const uint8_t *pv)
277 {
278     qemu_put_byte(f, *pv);
279 }
280
281 static inline void qemu_get_be64s(QEMUFile *f, uint64_t *pv)
282 {
283     *pv = qemu_get_be64(f);
284 }
285
286 static inline void qemu_get_be32s(QEMUFile *f, uint32_t *pv)
287 {
288     *pv = qemu_get_be32(f);
289 }
290
291 static inline void qemu_get_be16s(QEMUFile *f, uint16_t *pv)
292 {
293     *pv = qemu_get_be16(f);
294 }
295
296 static inline void qemu_get_8s(QEMUFile *f, uint8_t *pv)
297 {
298     *pv = qemu_get_byte(f);
299 }
300
301 int64_t qemu_ftell(QEMUFile *f);
302 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence);
303
304 typedef void SaveStateHandler(QEMUFile *f, void *opaque);
305 typedef int LoadStateHandler(QEMUFile *f, void *opaque, int version_id);
306
307 int qemu_loadvm(const char *filename);
308 int qemu_savevm(const char *filename);
309 int register_savevm(const char *idstr, 
310                     int instance_id, 
311                     int version_id,
312                     SaveStateHandler *save_state,
313                     LoadStateHandler *load_state,
314                     void *opaque);
315 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts);
316 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts);
317
318 /* block.c */
319 typedef struct BlockDriverState BlockDriverState;
320
321 BlockDriverState *bdrv_new(const char *device_name);
322 void bdrv_delete(BlockDriverState *bs);
323 int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot);
324 void bdrv_close(BlockDriverState *bs);
325 int bdrv_read(BlockDriverState *bs, int64_t sector_num, 
326               uint8_t *buf, int nb_sectors);
327 int bdrv_write(BlockDriverState *bs, int64_t sector_num, 
328                const uint8_t *buf, int nb_sectors);
329 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr);
330 int bdrv_commit(BlockDriverState *bs);
331 void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size);
332
333 #define BDRV_TYPE_HD     0
334 #define BDRV_TYPE_CDROM  1
335 #define BDRV_TYPE_FLOPPY 2
336
337 void bdrv_set_geometry_hint(BlockDriverState *bs, 
338                             int cyls, int heads, int secs);
339 void bdrv_set_type_hint(BlockDriverState *bs, int type);
340 void bdrv_get_geometry_hint(BlockDriverState *bs, 
341                             int *pcyls, int *pheads, int *psecs);
342 int bdrv_get_type_hint(BlockDriverState *bs);
343 int bdrv_is_removable(BlockDriverState *bs);
344 int bdrv_is_read_only(BlockDriverState *bs);
345 int bdrv_is_inserted(BlockDriverState *bs);
346 int bdrv_is_locked(BlockDriverState *bs);
347 void bdrv_set_locked(BlockDriverState *bs, int locked);
348 void bdrv_set_change_cb(BlockDriverState *bs, 
349                         void (*change_cb)(void *opaque), void *opaque);
350
351 void bdrv_info(void);
352 BlockDriverState *bdrv_find(const char *name);
353
354 /* ISA bus */
355
356 extern target_phys_addr_t isa_mem_base;
357
358 typedef void (IOPortWriteFunc)(void *opaque, uint32_t address, uint32_t data);
359 typedef uint32_t (IOPortReadFunc)(void *opaque, uint32_t address);
360
361 int register_ioport_read(int start, int length, int size, 
362                          IOPortReadFunc *func, void *opaque);
363 int register_ioport_write(int start, int length, int size, 
364                           IOPortWriteFunc *func, void *opaque);
365 void isa_unassign_ioport(int start, int length);
366
367 /* PCI bus */
368
369 extern int pci_enabled;
370
371 extern target_phys_addr_t pci_mem_base;
372
373 typedef struct PCIDevice PCIDevice;
374
375 typedef void PCIConfigWriteFunc(PCIDevice *pci_dev, 
376                                 uint32_t address, uint32_t data, int len);
377 typedef uint32_t PCIConfigReadFunc(PCIDevice *pci_dev, 
378                                    uint32_t address, int len);
379 typedef void PCIMapIORegionFunc(PCIDevice *pci_dev, int region_num, 
380                                 uint32_t addr, uint32_t size, int type);
381
382 #define PCI_ADDRESS_SPACE_MEM           0x00
383 #define PCI_ADDRESS_SPACE_IO            0x01
384 #define PCI_ADDRESS_SPACE_MEM_PREFETCH  0x08
385
386 typedef struct PCIIORegion {
387     uint32_t addr; /* current PCI mapping address. -1 means not mapped */
388     uint32_t size;
389     uint8_t type;
390     PCIMapIORegionFunc *map_func;
391 } PCIIORegion;
392
393 struct PCIDevice {
394     /* PCI config space */
395     uint8_t config[256];
396
397     /* the following fields are read only */
398     int bus_num;
399     int devfn;
400     char name[64];
401     PCIIORegion io_regions[6];
402     
403     /* do not access the following fields */
404     PCIConfigReadFunc *config_read;
405     PCIConfigWriteFunc *config_write;
406     int irq_index;
407 };
408
409 PCIDevice *pci_register_device(const char *name, int instance_size,
410                                int bus_num, int devfn,
411                                PCIConfigReadFunc *config_read, 
412                                PCIConfigWriteFunc *config_write);
413
414 void pci_register_io_region(PCIDevice *pci_dev, int region_num, 
415                             uint32_t size, int type, 
416                             PCIMapIORegionFunc *map_func);
417
418 void pci_set_irq(PCIDevice *pci_dev, int irq_num, int level);
419
420 uint32_t pci_default_read_config(PCIDevice *d, 
421                                  uint32_t address, int len);
422 void pci_default_write_config(PCIDevice *d, 
423                               uint32_t address, uint32_t val, int len);
424
425 extern struct PIIX3State *piix3_state;
426
427 void i440fx_init(void);
428 void piix3_init(void);
429 void pci_bios_init(void);
430 void pci_info(void);
431
432 /* temporary: will be moved in platform specific file */
433 void pci_prep_init(void);
434 void pci_pmac_init(void);
435 void pci_ppc_bios_init(void);
436
437 /* vga.c */
438
439 #define VGA_RAM_SIZE (4096 * 1024)
440
441 typedef struct DisplayState {
442     uint8_t *data;
443     int linesize;
444     int depth;
445     void (*dpy_update)(struct DisplayState *s, int x, int y, int w, int h);
446     void (*dpy_resize)(struct DisplayState *s, int w, int h);
447     void (*dpy_refresh)(struct DisplayState *s);
448 } DisplayState;
449
450 static inline void dpy_update(DisplayState *s, int x, int y, int w, int h)
451 {
452     s->dpy_update(s, x, y, w, h);
453 }
454
455 static inline void dpy_resize(DisplayState *s, int w, int h)
456 {
457     s->dpy_resize(s, w, h);
458 }
459
460 int vga_initialize(DisplayState *ds, uint8_t *vga_ram_base, 
461                    unsigned long vga_ram_offset, int vga_ram_size, 
462                    int is_pci);
463 void vga_update_display(void);
464 void vga_screen_dump(const char *filename);
465
466 /* sdl.c */
467 void sdl_display_init(DisplayState *ds);
468
469 /* ide.c */
470 #define MAX_DISKS 4
471
472 extern BlockDriverState *bs_table[MAX_DISKS];
473
474 void isa_ide_init(int iobase, int iobase2, int irq,
475                   BlockDriverState *hd0, BlockDriverState *hd1);
476 void pci_ide_init(BlockDriverState **hd_table);
477 void pci_piix3_ide_init(BlockDriverState **hd_table);
478
479 /* oss.c */
480 typedef enum {
481   AUD_FMT_U8,
482   AUD_FMT_S8,
483   AUD_FMT_U16,
484   AUD_FMT_S16
485 } audfmt_e;
486
487 void AUD_open (int rfreq, int rnchannels, audfmt_e rfmt);
488 void AUD_reset (int rfreq, int rnchannels, audfmt_e rfmt);
489 int AUD_write (void *in_buf, int size);
490 void AUD_run (void);
491 void AUD_adjust_estimate (int _leftover);
492 int AUD_get_free (void);
493 int AUD_get_live (void);
494 int AUD_get_buffer_size (void);
495 void AUD_init (void);
496
497 /* dma.c */
498 typedef int (*DMA_transfer_handler) (void *opaque, target_ulong addr, int size);
499 int DMA_get_channel_mode (int nchan);
500 void DMA_hold_DREQ (int nchan);
501 void DMA_release_DREQ (int nchan);
502 void DMA_schedule(int nchan);
503 void DMA_run (void);
504 void DMA_init (void);
505 void DMA_register_channel (int nchan,
506                            DMA_transfer_handler transfer_handler, void *opaque);
507
508 /* sb16.c */
509 void SB16_run (void);
510 void SB16_init (void);
511  
512 /* fdc.c */
513 #define MAX_FD 2
514 extern BlockDriverState *fd_table[MAX_FD];
515
516 typedef struct fdctrl_t fdctrl_t;
517
518 fdctrl_t *fdctrl_init (int irq_lvl, int dma_chann, int mem_mapped, 
519                        uint32_t io_base,
520                        BlockDriverState **fds);
521 int fdctrl_get_drive_type(fdctrl_t *fdctrl, int drive_num);
522
523 /* ne2000.c */
524
525 void isa_ne2000_init(int base, int irq, NetDriverState *nd);
526 void pci_ne2000_init(NetDriverState *nd);
527
528 /* pckbd.c */
529
530 void kbd_put_keycode(int keycode);
531
532 #define MOUSE_EVENT_LBUTTON 0x01
533 #define MOUSE_EVENT_RBUTTON 0x02
534 #define MOUSE_EVENT_MBUTTON 0x04
535 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state);
536
537 void kbd_init(void);
538
539 /* mc146818rtc.c */
540
541 typedef struct RTCState RTCState;
542
543 RTCState *rtc_init(int base, int irq);
544 void rtc_set_memory(RTCState *s, int addr, int val);
545 void rtc_set_date(RTCState *s, const struct tm *tm);
546
547 /* serial.c */
548
549 typedef struct SerialState SerialState;
550
551 extern SerialState *serial_console;
552
553 SerialState *serial_init(int base, int irq, int fd);
554 int serial_can_receive(SerialState *s);
555 void serial_receive_byte(SerialState *s, int ch);
556 void serial_receive_break(SerialState *s);
557
558 /* i8259.c */
559
560 void pic_set_irq(int irq, int level);
561 void pic_init(void);
562 uint32_t pic_intack_read(CPUState *env);
563 void pic_info(void);
564 void irq_info(void);
565
566 /* i8254.c */
567
568 #define PIT_FREQ 1193182
569
570 typedef struct PITState PITState;
571
572 PITState *pit_init(int base, int irq);
573 void pit_set_gate(PITState *pit, int channel, int val);
574 int pit_get_gate(PITState *pit, int channel);
575 int pit_get_out(PITState *pit, int channel, int64_t current_time);
576
577 /* pc.c */
578 void pc_init(int ram_size, int vga_ram_size, int boot_device,
579              DisplayState *ds, const char **fd_filename, int snapshot,
580              const char *kernel_filename, const char *kernel_cmdline,
581              const char *initrd_filename);
582
583 /* ppc.c */
584 void ppc_init (int ram_size, int vga_ram_size, int boot_device,
585                DisplayState *ds, const char **fd_filename, int snapshot,
586                const char *kernel_filename, const char *kernel_cmdline,
587                const char *initrd_filename);
588 void ppc_prep_init (int ram_size, int vga_ram_size, int boot_device,
589                     DisplayState *ds, const char **fd_filename, int snapshot,
590                     const char *kernel_filename, const char *kernel_cmdline,
591                     const char *initrd_filename);
592 void ppc_chrp_init(int ram_size, int vga_ram_size, int boot_device,
593                    DisplayState *ds, const char **fd_filename, int snapshot,
594                    const char *kernel_filename, const char *kernel_cmdline,
595                    const char *initrd_filename);
596 ppc_tb_t *cpu_ppc_tb_init (CPUPPCState *env, uint32_t freq);
597 void PREP_debug_write (void *opaque, uint32_t addr, uint32_t val);
598
599 extern CPUWriteMemoryFunc *PPC_io_write[];
600 extern CPUReadMemoryFunc *PPC_io_read[];
601 extern int prep_enabled;
602
603 /* NVRAM helpers */
604 #include "hw/m48t59.h"
605
606 void NVRAM_set_byte (m48t59_t *nvram, uint32_t addr, uint8_t value);
607 uint8_t NVRAM_get_byte (m48t59_t *nvram, uint32_t addr);
608 void NVRAM_set_word (m48t59_t *nvram, uint32_t addr, uint16_t value);
609 uint16_t NVRAM_get_word (m48t59_t *nvram, uint32_t addr);
610 void NVRAM_set_lword (m48t59_t *nvram, uint32_t addr, uint32_t value);
611 uint32_t NVRAM_get_lword (m48t59_t *nvram, uint32_t addr);
612 void NVRAM_set_string (m48t59_t *nvram, uint32_t addr,
613                        const unsigned char *str, uint32_t max);
614 int NVRAM_get_string (m48t59_t *nvram, uint8_t *dst, uint16_t addr, int max);
615 void NVRAM_set_crc (m48t59_t *nvram, uint32_t addr,
616                     uint32_t start, uint32_t count);
617 int PPC_NVRAM_set_params (m48t59_t *nvram, uint16_t NVRAM_size,
618                           const unsigned char *arch,
619                           uint32_t RAM_size, int boot_device,
620                           uint32_t kernel_image, uint32_t kernel_size,
621                           uint32_t cmdline, uint32_t cmdline_size,
622                           uint32_t initrd_image, uint32_t initrd_size,
623                           uint32_t NVRAM_image);
624
625 /* monitor.c */
626 void monitor_init(void);
627 void term_printf(const char *fmt, ...) __attribute__ ((__format__ (__printf__, 1, 2)));
628 void term_flush(void);
629 void term_print_help(void);
630
631 /* gdbstub.c */
632
633 #define DEFAULT_GDBSTUB_PORT 1234
634
635 int gdbserver_start(int port);
636
637 #endif /* VL_H */