Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / openhackware / src / bios.h
1 /*
2  * <bios.h>
3  *
4  * header for Open Hack'Ware
5  * 
6  * Copyright (c) 2004-2005 Jocelyn Mayer
7  * 
8  *   This program is free software; you can redistribute it and/or
9  *   modify it under the terms of the GNU General Public License V2
10  *   as published by the Free Software Foundation
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 #if !defined (__BIOS_H__)
22 #define __BIOS_H__
23
24 #define USE_OPENFIRMWARE
25 //#define DEBUG_BIOS 1
26
27 #define BIOS_VERSION "0.4.1"
28
29 #define DSISR 18
30 #define DAR   19
31 #define SRR0  26
32 #define SRR1  27
33
34 #define _tostring(s) #s
35 #define stringify(s) _tostring(s)
36
37 #if !defined (ASSEMBLY_CODE)
38
39 #ifdef DEBUG_BIOS
40 #define DPRINTF(fmt, args...) do { dprintf(fmt , ##args); } while (0)
41 #else
42 #define DPRINTF(fmt, args...) do { } while (0)
43 #endif
44 #define ERROR(fmt, args...) do { printf("ERROR: " fmt , ##args); } while (0)
45 #define MSG(fmt, args...) do { printf(fmt , ##args); } while (0)
46
47 #define offsetof(_struct, field)                                      \
48 ({                                                                    \
49     typeof(_struct) __tmp_struct;                                     \
50     int __off;                                                        \
51     __off = (char *)(&__tmp_struct.field) - (char *)(&__tmp_struct);  \
52     __off;                                                            \
53 })
54
55 #define unused __attribute__ (( unused)) 
56
57 /* Useful macro in C code */
58 #define MTSPR(num, value)                                            \
59 __asm__ __volatile__ ("mtspr " stringify(num) ", %0" :: "r"(value));
60
61 /* Architectures */
62 enum {
63     ARCH_PREP = 0,
64     ARCH_CHRP,
65     ARCH_MAC99,
66     ARCH_POP,
67     ARCH_HEATHROW,
68 };
69
70 /* Hardware definition(s) */
71 extern uint32_t isa_io_base;
72 #define ISA_IO_BASE 0x80000000
73 extern int arch;
74
75 /*****************************************************************************/
76 /* From start.S : BIOS start code and asm helpers */
77 void transfer_handler (void *residual, void *load_addr,
78                        void *OF_entry, void *bootinfos,
79                        void *cmdline, void *not_used,
80                        void *nip, void *stack_base);
81 void bug (void);
82
83 /* PPC helpers */
84 uint32_t mfmsr (void);
85 void mtmsr (uint32_t msr);
86 uint32_t mfpvr (void);
87 void mftb (uint32_t *tb);
88 void MMU_on (void);
89 void MMU_off (void);
90 /* IO helpers */
91 uint32_t inb (uint16_t port);
92 void outb (uint16_t port, uint32_t val);
93 uint32_t inw (uint16_t port);
94 void outw (uint16_t port, uint32_t val);
95 uint32_t inl (uint16_t port);
96 void outl (uint16_t port, uint32_t val);
97 void eieio (void);
98 /* Misc helpers */
99 uint16_t ldswap16 (uint16_t *addr);
100 void stswap16 (void *addr, uint16_t val);
101 uint32_t ldswap32 (uint32_t *addr);
102 void stswap32 (void *addr, uint32_t val);
103 void mul64 (uint32_t *ret, uint32_t a, uint32_t b);
104 void add64 (uint32_t *ret, uint32_t *a, uint32_t *b);
105
106 typedef struct jmp_buf {
107     uint32_t gpr[32];
108     uint32_t lr;
109     uint32_t ctr;
110     uint32_t xer;
111     uint32_t ccr;
112 } jmp_buf;
113 int setjmp (jmp_buf env);
114 void longjmp (jmp_buf env);
115
116 /*****************************************************************************/
117 /* PCI BIOS                                                                  */
118 typedef struct pci_common_t pci_common_t;
119 typedef struct pci_host_t pci_host_t;
120 typedef struct pci_device_t pci_device_t;
121 typedef struct pci_bridge_t pci_bridge_t;
122 typedef struct pci_ops_t pci_ops_t;
123 typedef union pci_u_t pci_u_t;
124
125 typedef struct pci_dev_t pci_dev_t;
126 struct pci_dev_t {
127     uint16_t vendor;
128     uint16_t product;
129     const unsigned char *type;
130     const unsigned char *name;
131     const unsigned char *model;
132     const unsigned char *compat;
133     int acells;
134     int scells;
135     int icells;
136     int (*config_cb)(pci_device_t *device);
137     const void *private;
138 };
139
140 pci_host_t *pci_init (void);
141 void pci_get_mem_range (pci_host_t *host, uint32_t *start, uint32_t *len);
142
143 /*****************************************************************************/
144 /* nvram.c : NVRAM management routines */
145 typedef struct nvram_t nvram_t;
146 extern nvram_t *nvram;
147
148 uint8_t NVRAM_read (nvram_t *nvram, uint32_t addr);
149 void NVRAM_write (nvram_t *nvram, uint32_t addr, uint8_t value);
150 uint16_t NVRAM_get_size (nvram_t *nvram);
151 int NVRAM_format (nvram_t *nvram);
152 nvram_t *NVRAM_get_config (uint32_t *RAM_size, int *boot_device,
153                            void **boot_image, uint32_t *boot_size,
154                            void **cmdline, uint32_t *cmdline_size,
155                            void **ramdisk, uint32_t *ramdisk_size);
156
157 /*****************************************************************************/
158 /* bloc.c : bloc devices management */
159 typedef struct pos_t {
160     uint32_t bloc;
161     uint32_t offset;
162 } pos_t;
163
164 typedef struct bloc_device_t bloc_device_t;
165 typedef struct part_t part_t;
166 typedef struct fs_t fs_t;
167
168 bloc_device_t *bd_open (int device);
169 int bd_seek (bloc_device_t *bd, uint32_t bloc, uint32_t pos);
170 int bd_read (bloc_device_t *bd, void *buffer, int len);
171 int bd_write (bloc_device_t *bd, const void *buffer, int len);
172 #define _IOCTL(a, b) (((a) << 16) | (b))
173 #define MEM_SET_ADDR _IOCTL('M', 0x00)
174 #define MEM_SET_SIZE _IOCTL('M', 0x01)
175 int bd_ioctl (bloc_device_t *bd, int func, void *args);
176 uint32_t bd_seclen (bloc_device_t *bd);
177 void bd_close (bloc_device_t *bd);
178 void bd_reset_all(void);
179 uint32_t bd_seclen (bloc_device_t *bd);
180 uint32_t bd_maxbloc (bloc_device_t *bd);
181 void bd_sect2CHS (bloc_device_t *bd, uint32_t secnum,
182                   int *cyl, int *head, int *sect);
183 uint32_t bd_CHS2sect (bloc_device_t *bd,
184                       int cyl, int head, int sect);
185 part_t *bd_probe (int boot_device);
186 bloc_device_t *bd_get (int device);
187 void bd_put (bloc_device_t *bd);
188 void bd_set_boot_part (bloc_device_t *bd, part_t *partition, int partnum);
189 part_t **_bd_parts (bloc_device_t *bd);
190
191 void ide_pci_pc_register (uint32_t io_base0, uint32_t io_base1,
192                           uint32_t io_base2, uint32_t io_base3,
193                           void *OF_private0, void *OF_private1);
194 void ide_pci_pmac_register (uint32_t io_base0, uint32_t io_base1,
195                             void *OF_private);
196
197 /*****************************************************************************/
198 /* part.c : partitions management */
199 enum part_flags_t {
200     PART_TYPE_RAW     = 0x0000,
201     PART_TYPE_PREP    = 0x0001,
202     PART_TYPE_APPLE   = 0x0002,
203     PART_TYPE_ISO9660 = 0x0004,
204     PART_FLAG_DUMMY   = 0x0010,
205     PART_FLAG_DRIVER  = 0x0020,
206     PART_FLAG_PATCH   = 0x0040,
207     PART_FLAG_FS      = 0x0080,
208     PART_FLAG_BOOT    = 0x0100,
209 };
210
211 enum {
212     PART_PREP = 0x01,
213     PART_CHRP = 0x02,
214 };
215
216 part_t *part_open (bloc_device_t *bd,
217                    uint32_t start, uint32_t size, uint32_t spb);
218 int part_seek (part_t *part, uint32_t bloc, uint32_t pos);
219 int part_read (part_t *part, void *buffer, int len);
220 int part_write (part_t *part, const void *buffer, int len);
221 void part_close (part_t *part);
222 uint32_t part_blocsize (part_t *part);
223 uint32_t part_flags (part_t *part);
224 uint32_t part_size (part_t *part);
225 fs_t *part_fs (part_t *part);
226
227 part_t *part_get (bloc_device_t *bd, int partnum);
228 part_t *part_probe (bloc_device_t *bd, int set_raw);
229 int part_set_boot_file (part_t *part, uint32_t start, uint32_t offset,
230                         uint32_t size);
231
232 /*****************************************************************************/
233 /* fs.c : file system management */
234 typedef struct dir_t dir_t;
235 typedef struct dirent_t dirent_t;
236 typedef struct inode_t inode_t;
237
238 struct dirent_t {
239     dir_t *dir;
240     inode_t *inode;
241     const unsigned char *dname;
242 };
243
244 enum {
245     INODE_TYPE_UNKNOWN = 0x00FF,
246     INODE_TYPE_DIR     = 0x0000,
247     INODE_TYPE_FILE    = 0x0001,
248     INODE_TYPE_OTHER   = 0x0002,
249     INODE_TYPE_MASK    = 0x00FF,
250     INODE_FLAG_EXEC    = 0x0100,
251     INODE_FLAG_BOOT    = 0x0200,
252     INODE_FLAG_MASK    = 0xFF00,
253 };
254
255 /* probe a filesystem from a partition */
256 fs_t *fs_probe (part_t *part, int set_raw);
257 part_t *fs_part (fs_t *fs);
258 /* Recurse thru directories */
259 dir_t *fs_opendir (fs_t *fs, const unsigned char *name);
260 dirent_t *fs_readdir (dir_t *dir);
261 unsigned char *fs_get_path (dirent_t *dirent);
262 void fs_closedir (dir_t *dir);
263 /* Play with files */
264 inode_t *fs_open (fs_t *fs, const unsigned char *name);
265 int fs_seek (inode_t *inode, uint32_t bloc, uint32_t pos);
266 int fs_read (inode_t *inode, void *buffer, int len);
267 int fs_write (inode_t *inode, const void *buffer, unused int len);
268 void fs_close (inode_t *inode);
269 uint32_t fs_get_type (fs_t *fs);
270 uint32_t fs_inode_get_type (inode_t *inode);
271 uint32_t fs_inode_get_flags (inode_t *inode);
272 part_t *fs_inode_get_part (inode_t *inode);
273
274 /* Bootfile */
275 unsigned char *fs_get_boot_dirname (fs_t *fs);
276 inode_t *fs_get_bootfile (fs_t *fs);
277 int fs_raw_set_bootfile (part_t *part,
278                          uint32_t start_bloc, uint32_t start_offset,
279                          uint32_t size_bloc, uint32_t size_offset);
280
281 /*****************************************************************************/
282 /* file.c : file management */
283 #define DEFAULT_LOAD_DEST 0x00100000
284
285 uint32_t file_seek (inode_t *file, uint32_t pos);
286
287 /* Executable files loader */
288 int bootfile_load (void **dest, void **entry, void **end,
289                    part_t *part, int type, const unsigned char *fname,
290                    uint32_t offset);
291
292 /*****************************************************************************/
293 /* char.c : char devices */
294 typedef struct chardev_t chardev_t;
295 typedef struct cops_t cops_t;
296
297 struct cops_t {
298     int (*open)(void *private);
299     int (*close)(void *private);
300     int (*read)(void *private);
301     int (*write)(void *private, int c);
302     /* Won't implement seek for now */
303 };
304
305 enum {
306     CHARDEV_KBD = 0,
307     CHARDEV_MOUSE,
308     CHARDEV_SERIAL,
309     CHARDEV_DISPLAY,
310     CHARDEV_LAST,
311 };
312
313 int chardev_register (int type, cops_t *ops, void *private);
314 int chardev_open (chardev_t *dev);
315 int chardev_close (chardev_t *dev);
316 int chardev_read (chardev_t *dev, void *buffer, int maxlen);
317 int chardev_write (chardev_t *dev, const void *buffer, int maxlen);
318 int chardev_type (chardev_t *dev);
319
320 /* Console driver */
321 int console_open (void);
322 int console_read (void *buffer, int maxlen);
323 int console_write (const void *buffer, int len);
324 void console_close (void);
325
326 /* PC serial port */
327 #define SERIAL_OUT_PORT (0x03F8)
328 int pc_serial_register (uint16_t base);
329
330 /* CUDA host */
331 typedef struct cuda_t cuda_t;
332 cuda_t *cuda_init (uint32_t base);
333 void cuda_reset (cuda_t *cuda);
334
335 /*****************************************************************************/
336 /* vga.c : VGA console */
337 extern unsigned long vga_fb_phys_addr;
338 extern int vga_fb_width;
339 extern int vga_fb_height;
340 extern int vga_fb_linesize;
341 extern int vga_fb_bpp;
342 extern int vga_fb_depth;
343 void vga_prep_init(void);
344 void vga_set_address (uint32_t address);
345 void vga_set_mode(int width, int height, int depth);
346 void vga_set_palette(int i, unsigned int rgba);
347 #define RGBA(r, g, b, a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
348 #define RGB(r, g, b) RGBA(r, g, b, 0xff)
349 unsigned int vga_get_color(unsigned int rgba);
350
351 void vga_draw_buf (const void *buf, int buf_linesize,
352                    int posx, int posy, int width, int height);
353 void vga_fill_rect (int posx, int posy, int width, int height, uint32_t color);
354 void vga_bitblt(int xs, int ys, int xd, int yd, int w, int h);
355 void vga_check_mode(int width, int height, int depth);
356
357 /* text primitives */
358 void vga_text_set_fgcol(unsigned int rgba);
359 void vga_text_set_bgcol(unsigned int rgba);
360 void vga_putcharxy(int x, int y, int ch,
361                    unsigned int fgcol, unsigned int bgcol);
362 void vga_putchar(int ch);
363 void vga_puts(const char *s);
364
365 /*****************************************************************************/
366 /* bootinfos.c : build structures needed by kernels to boot */
367 void prepare_bootinfos (void *p, uint32_t memsize,
368                         void *cmdline, void *initrd, uint32_t initrd_size);
369 void residual_build (void *p, uint32_t memsize,
370                      uint32_t load_base, uint32_t load_size,
371                      uint32_t last_alloc);
372
373 /*****************************************************************************/
374 /* of.c : Open-firmware emulation */
375 #define OF_NAMELEN_MAX 1024
376 #define OF_PROPLEN_MAX 256
377
378 int OF_init (void);
379 int OF_register_mb (const unsigned char *model, const unsigned char **compats);
380 int OF_register_cpu (const unsigned char *name, int num, uint32_t pvr,
381                      uint32_t min_freq, uint32_t max_freq, uint32_t bus_freq,
382                      uint32_t tb_freq, uint32_t reset_io);
383 #if 0
384 int OF_register_translations (int nb, OF_transl_t *translations);
385 #endif
386 uint32_t OF_claim_virt (uint32_t virt, uint32_t size, int *range);
387 int OF_register_memory (uint32_t memsize, uint32_t bios_size);
388 int OF_register_bootargs (const unsigned char *bootargs);
389 void *OF_register_pci_host (pci_dev_t *dev, uint16_t rev, uint32_t ccode,
390                             uint32_t cfg_base, uint32_t cfg_len,
391                             uint32_t mem_base, uint32_t mem_len,
392                             uint32_t io_base, uint32_t io_len,
393                             uint32_t rbase, uint32_t rlen,
394                             uint16_t min_grant, uint16_t max_latency);
395 void *OF_register_pci_bridge (void *parent, pci_dev_t *dev,
396                               uint32_t cfg_base, uint32_t cfg_len,
397                               uint8_t devfn, uint8_t rev, uint32_t ccode,
398                               uint16_t min_grant, uint16_t max_latency);
399 void *OF_register_pci_device (void *parent, pci_dev_t *dev,
400                               uint8_t devfn, uint8_t rev, uint32_t ccode,
401                               uint16_t min_grant, uint16_t max_latency);
402 void OF_finalize_pci_host (void *dev, int first_bus, int nb_busses);
403 void OF_finalize_pci_device (void *dev, uint8_t bus, uint8_t devfn,
404                              uint32_t *regions, uint32_t *sizes,
405                              int irq_line);
406 void OF_finalize_pci_macio (void *dev, uint32_t base_address, uint32_t size,
407                             void *private_data);
408 void OF_finalize_pci_ide (void *dev, 
409                           uint32_t io_base0, uint32_t io_base1,
410                           uint32_t io_base2, uint32_t io_base3);
411 int OF_register_bus (const unsigned char *name, uint32_t address,
412                      const unsigned char *type);
413 int OF_register_serial (const unsigned char *bus, const unsigned char *name,
414                         uint32_t io_base, int irq);
415 int OF_register_stdio (const unsigned char *dev_in,
416                        const unsigned char *dev_out);
417 void OF_vga_register (const unsigned char *name, unused uint32_t address,
418                       int width, int height, int depth,
419                       unsigned long vga_bios_addr, 
420                       unsigned long vga_bios_size);
421 void *OF_blockdev_register (void *parent, void *private,
422                             const unsigned char *type,
423                             const unsigned char *name, int devnum,
424                             const char *alias);
425 void OF_blockdev_set_boot_device (void *disk, int partnum,
426                                   const unsigned char *file);
427
428 int OF_entry (void *p);
429 int OF_client_entry (void *p);
430 void RTAS_init (void);
431
432 /*****************************************************************************/
433 /* main.c : main BIOS code */
434 /* Memory management */
435 /* Memory areas */
436 extern uint32_t _data_start, _data_end;
437 extern uint32_t _OF_vars_start, _OF_vars_end;
438 extern uint32_t _sdata_start, _sdata_end;
439 extern uint32_t _ro_start, _ro_end;
440 extern uint32_t _RTAS_start, _RTAS_end;
441 extern uint32_t _RTAS_data_start, _RTAS_data_end;
442 extern uint32_t _bss_start, _bss_end;
443 extern uint32_t _ram_start;
444 extern const unsigned char *BIOS_str;
445 extern const unsigned char *copyright;
446 void *mem_align (int align);
447 void freep (void *p);
448
449 /* Endian-safe memory read/write */
450 static inline void put_be64 (void *addr, uint64_t l)
451 {
452     *(uint64_t *)addr = l;
453 }
454
455 static inline uint64_t get_be64 (void *addr)
456 {
457     return *(uint64_t *)addr;
458 }
459
460 static inline void put_le64 (void *addr, uint64_t l)
461 {
462     uint32_t *p;
463
464     p = addr;
465     stswap32(p, l);
466     stswap32(p + 1, l >> 32);
467 }
468
469 static inline uint64_t get_le64 (void *addr)
470 {
471     uint64_t val;
472     uint32_t *p;
473
474     p = addr;
475     val = ldswap32(p);
476     val |= (uint64_t)ldswap32(p + 1) << 32;
477     
478     return val;
479 }
480
481 static inline void put_be32 (void *addr, uint32_t l)
482 {
483     *(uint32_t *)addr = l;
484 }
485
486 static inline uint32_t get_be32 (void *addr)
487 {
488     return *(uint32_t *)addr;
489 }
490
491 static inline void put_le32 (void *addr, uint32_t l)
492 {
493     stswap32(addr, l);
494 }
495
496 static inline uint32_t get_le32 (void *addr)
497 {
498     return ldswap32(addr);
499 }
500
501 static inline void put_be16 (void *addr, uint16_t l)
502 {
503     *(uint16_t *)addr = l;
504 }
505
506 static inline uint16_t get_be16 (void *addr)
507 {
508     return *(uint16_t *)addr;
509 }
510
511 static inline void put_le16 (void *addr, uint16_t l)
512 {
513     stswap16(addr, l);
514 }
515
516 static inline uint16_t get_le16 (void *addr)
517 {
518     return ldswap16(addr);
519 }
520
521 /* String functions */
522 long strtol (const unsigned char *str, unsigned char **end, int base);
523
524 int write_buf (const unsigned char *buf, int len);
525
526 /* Misc */
527 void usleep (uint32_t usec);
528 void sleep (int sec);
529 uint32_t crc32 (uint32_t crc, const uint8_t *p, int len);
530 void set_loadinfo (void *load_base, uint32_t size);
531 void set_check (int do_it);
532 void check_location (const void *buf, const char *func, const char *name);
533
534 static inline void pokeb (void *location, uint8_t val)
535 {
536 #ifdef DEBUG_BIOS
537     check_location(location, __func__, "location");
538 #endif
539     *((uint8_t *)location) = val;
540 }
541
542 static inline uint8_t peekb (void *location)
543 {
544 #ifdef DEBUG_BIOS
545     check_location(location, __func__, "location");
546 #endif
547     return *((uint8_t *)location);
548 }
549
550 static inline void pokew (void *location, uint16_t val)
551 {
552 #ifdef DEBUG_BIOS
553     check_location(location, __func__, "location");
554 #endif
555     *((uint8_t *)location) = val;
556 }
557
558 static inline uint16_t peekw (void *location)
559 {
560 #ifdef DEBUG_BIOS
561     check_location(location, __func__, "location");
562 #endif
563     return *((uint16_t *)location);
564 }
565
566 static inline void pokel (void *location, uint32_t val)
567 {
568 #ifdef DEBUG_BIOS
569     check_location(location, __func__, "location");
570 #endif
571     *((uint32_t *)location) = val;
572 }
573
574 static inline uint32_t peekl (void *location)
575 {
576 #ifdef DEBUG_BIOS
577     check_location(location, __func__, "location");
578 #endif
579     return *((uint32_t *)location);
580 }
581
582 /* Console */
583 int cs_write (const unsigned char *buf, int len);
584
585 #endif /* !defined (ASSEMBLY_CODE) */
586
587
588 #endif /* !defined (__BIOS_H__) */