Debian Codegraph @

common/dev_c2600.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot ([email protected])
4 * Patched by Jeremy Grossmann for the GNS3 project (www.gns3.net)
5 *
6 * Generic Cisco 2600 routines and definitions (EEPROM,...).
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <assert.h>
15
16 #include "cpu.h"
17 #include "vm.h"
18 #include "dynamips.h"
19 #include "memory.h"
20 #include "device.h"
21 #include "ppc32_mem.h"
22 #include "pci_io.h"
23 #include "cisco_eeprom.h"
24 #include "dev_mpc860.h"
25 #include "dev_rom.h"
26 #include "dev_c2600.h"
27 #include "dev_c2600_iofpga.h"
28 #include "dev_vtty.h"
29 #include "registry.h"
30 #include "fs_nvram.h"
31
32 /* ======================================================================== */
33 /* EEPROM definitions */
34 /* ======================================================================== */
35
36 /* Cisco 2600 mainboard EEPROM */
37 static m_uint16_t eeprom_c2600_mb_data[] = {
38 0x0101, 0x0404, 0x0000, 0x0000, 0x4320, 0x00FF, 0x0091, 0x0020,
39 0x0000, 0x0000, 0x0000, 0x0000, 0x4654, 0x5809, 0x4557, 0x304D,
40 0x5902, 0x0200, 0x0000, 0x0000, 0x00FF, 0xFFFF, 0x5006, 0x490B,
41 0x1709, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
42 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
43 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
44 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
45 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
46 };
47
48 struct c2600_mb_id {
49 char *name;
50 char *mb_driver;
51 m_uint16_t id;
52 int xm_model;
53 int supported;
54 };
55
56 struct c2600_mb_id c2600_mainboard_id[] = {
57 { "2610" , "CISCO2600-MB-1E" , 0x0091, FALSE, TRUE },
58 { "2611" , "CISCO2600-MB-2E" , 0x0092, FALSE, TRUE },
59 { "2620" , "CISCO2600-MB-1FE" , 0x0094, FALSE, TRUE },
60 { "2621" , "CISCO2600-MB-2FE" , 0x00a2, FALSE, TRUE },
61 { "2610XM" , "CISCO2600-MB-1FE" , 0x036a, TRUE, TRUE },
62 { "2611XM" , "CISCO2600-MB-2FE" , 0x036b, TRUE, FALSE },
63 { "2620XM" , "CISCO2600-MB-1FE" , 0x036c, TRUE, TRUE },
64 { "2621XM" , "CISCO2600-MB-2FE" , 0x036d, TRUE, FALSE },
65 { "2650XM" , "CISCO2600-MB-1FE" , 0x036e, TRUE, TRUE },
66 { "2651XM" , "CISCO2600-MB-2FE" , 0x036f, TRUE, FALSE },
67 { NULL , NULL , 0x0000, FALSE, FALSE },
68 };
69
70 /* ======================================================================== */
71 /* Network Module Drivers */
72 /* ======================================================================== */
73 static struct cisco_card_driver *nm_drivers[] = {
74 &dev_c2600_mb1e_eth_driver,
75 &dev_c2600_mb2e_eth_driver,
76 &dev_c2600_mb1fe_eth_driver,
77 &dev_c2600_mb2fe_eth_driver,
78
79 &dev_c2600_nm_1e_driver,
80 &dev_c2600_nm_4e_driver,
81 &dev_c2600_nm_1fe_tx_driver,
82 &dev_c2600_nm_16esw_driver,
83
84 &dev_c2600_nm_nam_driver,
85 &dev_c2600_nm_cids_driver,
86
87 NULL,
88 };
89
90 /* ======================================================================== */
91 /* Cisco 2600 router instances */
92 /* ======================================================================== */
93
94 /* Initialize default parameters for a C2600 */
95 static void c2600_init_defaults(c2600_t *router);
96
97 /* Directly extract the configuration from the NVRAM device */
98 static int c2600_nvram_extract_config(vm_instance_t *vm,u_char **startup_config,size_t *startup_len,u_char **private_config,size_t *private_len)
99 {
100 int ret;
101
102 ret = generic_nvram_extract_config(vm, "nvram", vm->nvram_rom_space*4, 0, 0, FS_NVRAM_FORMAT_SCALE_4, startup_config, startup_len, private_config, private_len);
103
104 return(ret);
105 }
106
107 /* Directly push the IOS configuration to the NVRAM device */
108 static int c2600_nvram_push_config(vm_instance_t *vm,u_char *startup_config,size_t startup_len,u_char *private_config,size_t private_len)
109 {
110 int ret;
111
112 ret = generic_nvram_push_config(vm, "nvram", vm->nvram_size*4096, vm->nvram_rom_space*4, 0, 0, FS_NVRAM_FORMAT_SCALE_4, startup_config, startup_len, private_config, private_len);
113
114 return(ret);
115 }
116
117 /* Check for empty config */
118 int c2600_nvram_check_empty_config(vm_instance_t *vm)
119 {
120 struct vdevice *dev;
121 m_uint64_t addr;
122 m_uint32_t len;
123
124 if (!(dev = dev_get_by_name(vm,"nvram")))
125 return(-1);
126
127 addr = dev->phys_addr + (vm->nvram_rom_space << 2);
128 len = dev->phys_len - (vm->nvram_rom_space << 2);
129
130 while(len > 0) {
131 if (physmem_copy_u32_from_vm(vm,addr) != 0)
132 return(0);
133
134 addr += sizeof(m_uint32_t);
135 len -= sizeof(m_uint32_t);
136 }
137
138 /* Empty NVRAM */
139 vm->conf_reg |= 0x0040;
140 printf("NVRAM is empty, setting config register to 0x%x\n",vm->conf_reg);
141 return(0);
142 }
143
144 /* Create a new router instance */
145 static int c2600_create_instance(vm_instance_t *vm)
146 {
147 c2600_t *router;
148
149 if (!(router = malloc(sizeof(*router)))) {
150 fprintf(stderr,"C2600 '%s': Unable to create new instance!\n",vm->name);
151 return(-1);
152 }
153
154 memset(router,0,sizeof(*router));
155 router->vm = vm;
156 vm->hw_data = router;
157
158 c2600_init_defaults(router);
159 return(0);
160 }
161
162 /* Free resources used by a router instance */
163 static int c2600_delete_instance(vm_instance_t *vm)
164 {
165 c2600_t *router = VM_C2600(vm);
166 int i;
167
168 /* Stop all CPUs */
169 if (vm->cpu_group != NULL) {
170 vm_stop(vm);
171
172 if (cpu_group_sync_state(vm->cpu_group) == -1) {
173 vm_error(vm,"unable to sync with system CPUs.\n");
174 return(FALSE);
175 }
176 }
177
178 /* Remove NIO bindings */
179 for(i=0;i<vm->nr_slots;i++)
180 vm_slot_remove_all_nio_bindings(vm,i);
181
182 /* Shutdown all Network Modules */
183 vm_slot_shutdown_all(vm);
184
185 /* Free mainboard EEPROM */
186 cisco_eeprom_free(&router->mb_eeprom);
187
188 /* Free all resources used by VM */
189 vm_free(vm);
190
191 /* Free the router structure */
192 free(router);
193 return(TRUE);
194 }
195
196 /* Save configuration of a C2600 instance */
197 void c2600_save_config(vm_instance_t *vm,FILE *fd)
198 {
199 c2600_t *router = VM_C2600(vm);
200
201 fprintf(fd,"c2600 set_chassis %s %s\n\n",vm->name,router->mainboard_type);
202 }
203
204 /* Get WIC device address for the specified onboard port */
205 int c2600_get_onboard_wic_addr(u_int slot,m_uint64_t *phys_addr)
206 {
207 if (slot >= C2600_MAX_WIC_BAYS)
208 return(-1);
209
210 *phys_addr = C2600_WIC_ADDR + (slot * C2600_WIC_SIZE);
211 return(0);
212 }
213
214 /* Set EEPROM for the specified slot */
215 int c2600_set_slot_eeprom(c2600_t *router,u_int slot,
216 struct cisco_eeprom *eeprom)
217 {
218 switch(slot) {
219 case 1:
220 router->nm_eeprom_group.eeprom[0] = eeprom;
221 return(0);
222 default:
223 return(-1);
224 }
225 }
226
227 /* Get slot/port corresponding to specified network IRQ */
228 static inline void
229 c2600_net_irq_get_slot_port(u_int irq,u_int *slot,u_int *port)
230 {
231 irq -= C2600_NETIO_IRQ_BASE;
232 *port = irq & C2600_NETIO_IRQ_PORT_MASK;
233 *slot = irq >> C2600_NETIO_IRQ_PORT_BITS;
234 }
235
236 /* Get network IRQ for specified slot/port */
237 u_int c2600_net_irq_for_slot_port(u_int slot,u_int port)
238 {
239 u_int irq;
240
241 irq = (slot << C2600_NETIO_IRQ_PORT_BITS) + port;
242 irq += C2600_NETIO_IRQ_BASE;
243
244 return(irq);
245 }
246
247 /* Find Cisco 2600 Mainboard info */
248 static struct c2600_mb_id *c2600_get_mb_info(char *mainboard_type)
249 {
250 int i;
251
252 for(i=0;c2600_mainboard_id[i].name;i++)
253 if (!strcmp(c2600_mainboard_id[i].name,mainboard_type))
254 return(&c2600_mainboard_id[i]);
255
256 return NULL;
257 }
258
259 /* Show all available mainboards */
260 void c2600_mainboard_show_drivers(void)
261 {
262 int i;
263
264 printf("Available C2600 chassis drivers:\n");
265
266 for(i=0;c2600_mainboard_id[i].name;i++)
267 printf(" * %s %s\n",
268 c2600_mainboard_id[i].name,
269 !c2600_mainboard_id[i].supported ? "(NOT WORKING)" : "");
270
271 printf("\n");
272 }
273
274 /* Show the list of available NM drivers */
275 void c2600_nm_show_drivers(void)
276 {
277 int i;
278
279 printf("Available C2600 Network Module drivers:\n");
280
281 for(i=0;nm_drivers[i];i++) {
282 printf(" * %s %s\n",
283 nm_drivers[i]->dev_type,
284 !nm_drivers[i]->supported ? "(NOT WORKING)" : "");
285 }
286
287 printf("\n");
288 }
289
290 /* Set the system id or processor board id in the eeprom */
291 int c2600_set_system_id(c2600_t *router,char *id)
292 {
293 /* 11 characters is enough. Array is 20 long */
294 strncpy(router->board_id,id,13);
295 /* Make sure it is null terminated */
296 router->board_id[13] = 0x00;
297 c2600_refresh_systemid(router);
298 return 0;
299 }
300
301 int c2600_refresh_systemid(c2600_t *router)
302 {
303 if (router->board_id[0] == 0x00) return(0);
304 m_uint8_t buf[11];
305 parse_board_id(buf,router->board_id,9);
306 // Does not use the cisco_eeprom libraries.. do it by hand
307 int i;
308 for(i=0;i<4;i++) {
309 router->vm->chassis_cookie[i+12] = buf[i*2] << 8;
310 router->vm->chassis_cookie[i+12] |= buf[(i*2)+1];
311 }
312 router->vm->chassis_cookie[i+12] &= 0x00ff;
313 router->vm->chassis_cookie[i+12] |= buf[(i*2)]<<8;
314
315 return (0);
316 }
317
318 /* Set the base MAC address of the chassis */
319 static int c2600_burn_mac_addr(c2600_t *router,n_eth_addr_t *addr)
320 {
321 int i;
322
323 for(i=0;i<3;i++) {
324 router->vm->chassis_cookie[i+1] = addr->eth_addr_byte[i*2] << 8;
325 router->vm->chassis_cookie[i+1] |= addr->eth_addr_byte[(i*2)+1];
326 }
327
328 return(0);
329 }
330
331 /* Set mainboard type */
332 int c2600_mainboard_set_type(c2600_t *router,char *mainboard_type)
333 {
334 struct c2600_mb_id *mb_info;
335
336 if (router->vm->status == VM_STATUS_RUNNING) {
337 vm_error(router->vm,"unable to change mainboard type when online.\n");
338 return(-1);
339 }
340
341 if (!(mb_info = c2600_get_mb_info(mainboard_type))) {
342 vm_error(router->vm,"unknown mainboard '%s'\n",mainboard_type);
343 return(-1);
344 }
345
346 router->mainboard_type = mainboard_type;
347 router->xm_model = mb_info->xm_model;
348
349 /* Set the cookie */
350 memcpy(router->vm->chassis_cookie,
351 eeprom_c2600_mb_data,sizeof(eeprom_c2600_mb_data));
352
353 router->vm->chassis_cookie[6] = mb_info->id;
354
355 /* Set the chassis base MAC address */
356 c2600_burn_mac_addr(router,&router->mac_addr);
357
358 /* Set the mainboard driver */
359 if (vm_slot_active(router->vm,0,0))
360 vm_slot_remove_binding(router->vm,0,0);
361
362 vm_slot_add_binding(router->vm,mb_info->mb_driver,0,0);
363 c2600_refresh_systemid(router);
364 return(0);
365 }
366
367 /* Set chassis MAC address */
368 int c2600_chassis_set_mac_addr(c2600_t *router,char *mac_addr)
369 {
370 if (parse_mac_addr(&router->mac_addr,mac_addr) == -1) {
371 vm_error(router->vm,"unable to parse MAC address '%s'.\n",mac_addr);
372 return(-1);
373 }
374
375 /* Set the chassis base MAC address */
376 c2600_burn_mac_addr(router,&router->mac_addr);
377 return(0);
378 }
379
380 /* Initialize a Cisco 2600 */
381 static int c2600_init(c2600_t *router)
382 {
383 vm_instance_t *vm = router->vm;
384
385 /* Create the PCI bus */
386 if (!(vm->pci_bus[0] = pci_bus_create("PCI0",0))) {
387 vm_error(vm,"unable to create PCI data.\n");
388 return(-1);
389 }
390
391 /* Create the PCI controller */
392 if (dev_c2600_pci_init(vm,"c2600_pci",C2600_PCICTRL_ADDR,0x10000,
393 vm->pci_bus[0]) == -1)
394 return(-1);
395
396 /* Bind PCI bus to slots 0 and 1 */
397 vm->slots_pci_bus[0] = vm->pci_bus[0];
398 vm->slots_pci_bus[1] = vm->pci_bus[0];
399
400 vm->elf_machine_id = C2600_ELF_MACHINE_ID;
401 return(0);
402 }
403
404 /* Show C2600 hardware info */
405 void c2600_show_hardware(c2600_t *router)
406 {
407 vm_instance_t *vm = router->vm;
408
409 printf("C2600 instance '%s' (id %d):\n",vm->name,vm->instance_id);
410
411 printf(" VM Status : %d\n",vm->status);
412 printf(" RAM size : %u Mb\n",vm->ram_size);
413 printf(" NVRAM size : %u Kb\n",vm->nvram_size);
414 printf(" IOS image : %s\n\n",vm->ios_image);
415
416 if (vm->debug_level > 0) {
417 dev_show_list(vm);
418 pci_dev_show_list(vm->pci_bus[0]);
419 pci_dev_show_list(vm->pci_bus[1]);
420 printf("\n");
421 }
422 }
423
424 /* Initialize default parameters for a C2600 */
425 static void c2600_init_defaults(c2600_t *router)
426 {
427 vm_instance_t *vm = router->vm;
428 n_eth_addr_t *m;
429 m_uint16_t pid;
430
431 /* Set platform slots characteristics */
432 vm->nr_slots = C2600_MAX_NM_BAYS;
433 vm->slots_type = CISCO_CARD_TYPE_NM;
434 vm->slots_drivers = nm_drivers;
435
436 pid = (m_uint16_t)getpid();
437
438 /* Generate a chassis MAC address based on the instance ID */
439 m = &router->mac_addr;
440 m->eth_addr_byte[0] = vm_get_mac_addr_msb(vm);
441 m->eth_addr_byte[1] = vm->instance_id & 0xFF;
442 m->eth_addr_byte[2] = pid >> 8;
443 m->eth_addr_byte[3] = pid & 0xFF;
444 m->eth_addr_byte[4] = 0x00;
445 m->eth_addr_byte[5] = 0x00;
446 router->board_id[0] = 0x00;
447
448 c2600_init_eeprom_groups(router);
449 c2600_mainboard_set_type(router,C2600_DEFAULT_MAINBOARD);
450 c2600_burn_mac_addr(router,&router->mac_addr);
451
452 vm->ram_mmap = C2600_DEFAULT_RAM_MMAP;
453 vm->ram_size = C2600_DEFAULT_RAM_SIZE;
454 vm->rom_size = C2600_DEFAULT_ROM_SIZE;
455 vm->nvram_size = C2600_DEFAULT_NVRAM_SIZE;
456 vm->conf_reg_setup = C2600_DEFAULT_CONF_REG;
457 vm->clock_divisor = C2600_DEFAULT_CLOCK_DIV;
458 vm->nvram_rom_space = C2600_NVRAM_ROM_RES_SIZE;
459 vm->nm_iomem_size = C2600_DEFAULT_IOMEM_SIZE;
460
461 vm->pcmcia_disk_size[0] = C2600_DEFAULT_DISK0_SIZE;
462 vm->pcmcia_disk_size[1] = C2600_DEFAULT_DISK1_SIZE;
463 }
464
465 /* Set an IRQ */
466 static void c2600_set_irq(vm_instance_t *vm,u_int irq)
467 {
468 c2600_t *router = VM_C2600(vm);
469 cpu_ppc_t *cpu = CPU_PPC32(vm->boot_cpu);
470 u_int slot,port;
471
472 switch(irq) {
473 case C2600_VTIMER_IRQ:
474 mpc860_set_pending_irq(router->mpc_data,30);
475 break;
476 case C2600_DUART_IRQ:
477 mpc860_set_pending_irq(router->mpc_data,29);
478 break;
479 case C2600_NETIO_IRQ:
480 mpc860_set_pending_irq(router->mpc_data,25);
481 break;
482 case C2600_PA_MGMT_IRQ:
483 mpc860_set_pending_irq(router->mpc_data,27);
484 break;
485
486 case C2600_NETIO_IRQ_BASE ... C2600_NETIO_IRQ_END:
487 c2600_net_irq_get_slot_port(irq,&slot,&port);
488 dev_c2600_iofpga_net_set_irq(router->iofpga_data,slot,port);
489 break;
490
491 /* IRQ test */
492 case 255:
493 mpc860_set_pending_irq(router->mpc_data,24);
494 break;
495 }
496
497 if (vm->irq_idle_preempt[irq])
498 cpu_idle_break_wait(cpu->gen);
499 }
500
501 /* Clear an IRQ */
502 static void c2600_clear_irq(vm_instance_t *vm,u_int irq)
503 {
504 c2600_t *router = VM_C2600(vm);
505 u_int slot,port;
506
507 switch(irq) {
508 case C2600_VTIMER_IRQ:
509 mpc860_clear_pending_irq(router->mpc_data,30);
510 break;
511 case C2600_DUART_IRQ:
512 mpc860_clear_pending_irq(router->mpc_data,29);
513 break;
514 case C2600_NETIO_IRQ:
515 mpc860_clear_pending_irq(router->mpc_data,25);
516 break;
517 case C2600_PA_MGMT_IRQ:
518 mpc860_clear_pending_irq(router->mpc_data,27);
519 break;
520
521 case C2600_NETIO_IRQ_BASE ... C2600_NETIO_IRQ_END:
522 c2600_net_irq_get_slot_port(irq,&slot,&port);
523 dev_c2600_iofpga_net_clear_irq(router->iofpga_data,slot,port);
524 break;
525
526 /* IRQ test */
527 case 255:
528 mpc860_clear_pending_irq(router->mpc_data,24);
529 break;
530 }
531 }
532
533 /* Initialize the C2600 Platform */
534 static int c2600_init_platform(c2600_t *router)
535 {
536 vm_instance_t *vm = router->vm;
537 vm_obj_t *obj;
538 cpu_ppc_t *cpu;
539 cpu_gen_t *gen;
540
541 /* Copy config register setup into "active" config register */
542 vm->conf_reg = vm->conf_reg_setup;
543
544 /* Create Console and AUX ports */
545 vm_init_vtty(vm);
546
547 /* Create a CPU group */
548 vm->cpu_group = cpu_group_create("System CPU");
549
550 /* Initialize the virtual PowerPC processor */
551 if (!(gen = cpu_create(vm,CPU_TYPE_PPC32,0))) {
552 vm_error(vm,"unable to create CPU!\n");
553 return(-1);
554 }
555
556 cpu = CPU_PPC32(gen);
557
558 /* Add this CPU to the system CPU group */
559 cpu_group_add(vm->cpu_group,gen);
560 vm->boot_cpu = gen;
561
562 /* Set processor ID */
563 ppc32_set_pvr(cpu,0x00500202);
564
565 /* Mark the Network IO interrupt as high priority */
566 vm->irq_idle_preempt[C2600_NETIO_IRQ] = TRUE;
567 vm->irq_idle_preempt[C2600_DUART_IRQ] = TRUE;
568
569 /* Copy some parameters from VM to CPU (idle PC, ...) */
570 cpu->idle_pc = vm->idle_pc;
571
572 if (vm->timer_irq_check_itv)
573 cpu->timer_irq_check_itv = vm->timer_irq_check_itv;
574
575 /* Remote emulator control */
576 dev_remote_control_init(vm,0xf6000000,0x1000);
577
578 /* MPC860 */
579 cpu->mpc860_immr = C2600_MPC860_ADDR;
580
581 if (dev_mpc860_init(vm,"MPC860",C2600_MPC860_ADDR,0x10000) == -1)
582 return(-1);
583
584 if (!(obj = vm_object_find(router->vm,"MPC860")))
585 return(-1);
586
587 router->mpc_data = obj->data;
588
589 /* IO FPGA */
590 if (dev_c2600_iofpga_init(router,C2600_IOFPGA_ADDR,0x10000) == -1)
591 return(-1);
592
593 if (!(obj = vm_object_find(router->vm,"io_fpga")))
594 return(-1);
595
596 router->iofpga_data = obj->data;
597
598 /* Initialize the chassis */
599 if (c2600_init(router) == -1)
600 return(-1);
601
602 /* Initialize RAM */
603 vm_ram_init(vm,0x00000000ULL);
604
605 /* Initialize ROM */
606 if (!vm->rom_filename) {
607 /* use embedded ROM */
608 dev_rom_init(vm,"rom",C2600_ROM_ADDR,512*1024,
609 ppc32_microcode,ppc32_microcode_len);
610 } else {
611 /* use alternate ROM */
612 dev_ram_init(vm,"rom",TRUE,TRUE,NULL,FALSE,C2600_ROM_ADDR,512*1024);
613 }
614
615 /* RAM aliasing */
616 dev_create_ram_alias(vm,"ram_alias","ram",0x80000000,vm->ram_size*1048576);
617
618 /* NVRAM */
619 dev_ram_init(vm,"nvram",TRUE,FALSE,NULL,FALSE,
620 C2600_NVRAM_ADDR,vm->nvram_size*4096);
621 c2600_nvram_check_empty_config(vm);
622
623 /* Bootflash */
624 dev_bootflash_init(vm,"flash0","c2600-bootflash-8mb",
625 C2600_FLASH_ADDR);
626 dev_bootflash_init(vm,"flash1","c2600-bootflash-8mb",
627 C2600_FLASH_ADDR+0x800000);
628
629 /* Initialize the NS16552 DUART */
630 dev_ns16552_init(vm,C2600_DUART_ADDR,0x1000,0,C2600_DUART_IRQ,
631 vm->vtty_con,vm->vtty_aux);
632
633 /* Initialize Network Modules */
634 if (vm_slot_init_all(vm) == -1)
635 return(-1);
636
637 /* Show device list */
638 c2600_show_hardware(router);
639 return(0);
640 }
641
642 static struct ppc32_bat_prog bat_array[] = {
643 { PPC32_IBAT_IDX, 0, 0xfff0001e, 0xfff00001 },
644 { PPC32_IBAT_IDX, 1, 0x00001ffe, 0x00000001 },
645 { PPC32_IBAT_IDX, 2, 0x00000000, 0xee3e0072 },
646 { PPC32_IBAT_IDX, 3, 0x80001ffe, 0x00000001 },
647
648 { PPC32_DBAT_IDX, 0, 0x80001ffe, 0x00000042 },
649 { PPC32_DBAT_IDX, 1, 0x00001ffe, 0x0000002a },
650 { PPC32_DBAT_IDX, 2, 0x40007ffe, 0x4000002a },
651 { PPC32_DBAT_IDX, 3, 0xf0001ffe, 0xf000002a },
652 { -1, -1, 0, 0 },
653 };
654
655 /* Boot the IOS image */
656 int c2600_boot_ios(c2600_t *router)
657 {
658 vm_instance_t *vm = router->vm;
659 cpu_ppc_t *cpu;
660
661 if (!vm->boot_cpu)
662 return(-1);
663
664 /* Suspend CPU activity since we will restart directly from ROM */
665 vm_suspend(vm);
666
667 /* Check that CPU activity is really suspended */
668 if (cpu_group_sync_state(vm->cpu_group) == -1) {
669 vm_error(vm,"unable to sync with system CPUs.\n");
670 return(-1);
671 }
672
673 /* Reset the boot CPU */
674 cpu = CPU_PPC32(vm->boot_cpu);
675 ppc32_reset(cpu);
676
677 /* Adjust stack pointer */
678 cpu->gpr[1] |= 0x80000000;
679
680 /* Load BAT registers */
681 printf("Loading BAT registers\n");
682 ppc32_load_bat_array(cpu,bat_array);
683 cpu->msr |= PPC32_MSR_IR|PPC32_MSR_DR;
684
685 /* IRQ routing */
686 vm->set_irq = c2600_set_irq;
687 vm->clear_irq = c2600_clear_irq;
688
689 /* Load IOS image */
690 if (ppc32_load_elf_image(cpu,vm->ios_image,
691 (vm->ghost_status == VM_GHOST_RAM_USE),
692 &vm->ios_entry_point) < 0)
693 {
694 vm_error(vm,"failed to load Cisco IOS image '%s'.\n",vm->ios_image);
695 return(-1);
696 }
697
698 /* Launch the simulation */
699 printf("\nC2600 '%s': starting simulation (CPU0 IA=0x%8.8x), "
700 "JIT %sabled.\n",
701 vm->name,cpu->ia,vm->jit_use ? "en":"dis");
702
703 vm_log(vm,"C2600_BOOT",
704 "starting instance (CPU0 PC=0x%8.8x,idle_pc=0x%8.8x,JIT %s)\n",
705 cpu->ia,cpu->idle_pc,vm->jit_use ? "on":"off");
706
707 /* Start main CPU */
708 if (vm->ghost_status != VM_GHOST_RAM_GENERATE) {
709 vm->status = VM_STATUS_RUNNING;
710 cpu_start(vm->boot_cpu);
711 } else {
712 vm->status = VM_STATUS_SHUTDOWN;
713 }
714 return(0);
715 }
716
717 /* Initialize a Cisco 2600 instance */
718 static int c2600_init_instance(vm_instance_t *vm)
719 {
720 c2600_t *router = VM_C2600(vm);
721 m_uint32_t rom_entry_point;
722 cpu_ppc_t *cpu0;
723
724 if (!vm->ios_image) {
725 vm_error(vm,"no Cisco IOS image defined.");
726 return(-1);
727 }
728
729 /* Initialize the C2600 platform */
730 if (c2600_init_platform(router) == -1) {
731 vm_error(vm,"unable to initialize the platform hardware.\n");
732 return(-1);
733 }
734
735 /* Load IOS configuration files */
736 if (vm->ios_startup_config != NULL || vm->ios_private_config != NULL) {
737 vm_nvram_push_config(vm,vm->ios_startup_config,vm->ios_private_config);
738 vm->conf_reg &= ~0x40;
739 }
740
741 /* Load ROM (ELF image or embedded) */
742 cpu0 = CPU_PPC32(vm->boot_cpu);
743 rom_entry_point = (m_uint32_t)PPC32_ROM_START;
744
745 if ((vm->rom_filename != NULL) &&
746 (ppc32_load_elf_image(cpu0,vm->rom_filename,0,&rom_entry_point) < 0))
747 {
748 vm_error(vm,"unable to load alternate ROM '%s', "
749 "fallback to embedded ROM.\n\n",vm->rom_filename);
750 vm->rom_filename = NULL;
751 }
752
753 return(c2600_boot_ios(router));
754 }
755
756 /* Stop a Cisco 2600 instance */
757 int c2600_stop_instance(vm_instance_t *vm)
758 {
759 printf("\nC2600 '%s': stopping simulation.\n",vm->name);
760 vm_log(vm,"C2600_STOP","stopping simulation.\n");
761
762 /* Stop all CPUs */
763 if (vm->cpu_group != NULL) {
764 vm_stop(vm);
765
766 if (cpu_group_sync_state(vm->cpu_group) == -1) {
767 vm_error(vm,"unable to sync with system CPUs.\n");
768 return(-1);
769 }
770 }
771
772 /* Free resources that were used during execution to emulate hardware */
773 vm_slot_shutdown_all(vm);
774 vm_hardware_shutdown(vm);
775
776 /* Cleanup */
777 VM_C2600(vm)->iofpga_data = NULL;
778 VM_C2600(vm)->mpc_data = NULL;
779 return(0);
780 }
781
782 /* Get MAC address MSB */
783 static u_int c2600_get_mac_addr_msb(void)
784 {
785 return(0xC8);
786 }
787
788 /* Parse specific options for the Cisco 2600 platform */
789 static int c2600_cli_parse_options(vm_instance_t *vm,int option)
790 {
791 c2600_t *router = VM_C2600(vm);
792
793 switch(option) {
794 /* IO memory reserved for NMs (in percents!) */
795 case OPT_IOMEM_SIZE:
796 vm->nm_iomem_size = 0x8000 | atoi(optarg);
797 break;
798
799 /* Mainboard type */
800 case 't':
801 c2600_mainboard_set_type(router,optarg);
802 break;
803
804 /* Set the base MAC address */
805 case 'm':
806 if (!c2600_chassis_set_mac_addr(router,optarg))
807 printf("MAC address set to '%s'.\n",optarg);
808 break;
809
810 /* Set the System ID */
811 case 'I':
812 if (!c2600_set_system_id(router,optarg))
813 printf("System ID set to '%s'.\n",optarg);
814 break;
815
816 /* Unknown option */
817 default:
818 return(-1);
819 }
820
821 return(0);
822 }
823
824 /* Show specific CLI options */
825 static void c2600_cli_show_options(vm_instance_t *vm)
826 {
827 printf(" --iomem-size <val> : IO memory (in percents, default: %u)\n"
828 " -p <nm_desc> : Define a Network Module\n"
829 " -I <serialno> : Set Processor Board Serial Number\n"
830 " -s <nm_nio> : Bind a Network IO interface to a "
831 "Network Module\n",
832 vm->nm_iomem_size);
833 }
834
835 /* Platform definition */
836 static vm_platform_t c2600_platform = {
837 "c2600", "C2600", "2600",
838 c2600_create_instance,
839 c2600_delete_instance,
840 c2600_init_instance,
841 c2600_stop_instance,
842 NULL,
843 NULL,
844 c2600_nvram_extract_config,
845 c2600_nvram_push_config,
846 c2600_get_mac_addr_msb,
847 c2600_save_config,
848 c2600_cli_parse_options,
849 c2600_cli_show_options,
850 c2600_mainboard_show_drivers,
851 };
852
853 /* Register the c2600 platform */
854 int c2600_platform_register(void)
855 {
856 if (vm_platform_register(&c2600_platform) == -1)
857 return(-1);
858
859 return(hypervisor_c2600_init(&c2600_platform));
860 }