1 |
dpavlin |
1 |
/* |
2 |
dpavlin |
7 |
* Cisco router simulation platform. |
3 |
dpavlin |
1 |
* Copyright (c) 2005,2006 Christophe Fillot (cf@utc.fr) |
4 |
|
|
* |
5 |
|
|
* Packet SRAM. This is a fast memory zone for packets on NPE150/NPE200. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#include <stdio.h> |
9 |
|
|
#include <stdlib.h> |
10 |
|
|
#include <string.h> |
11 |
|
|
#include <unistd.h> |
12 |
|
|
#include <assert.h> |
13 |
|
|
|
14 |
dpavlin |
7 |
#include "cpu.h" |
15 |
|
|
#include "vm.h" |
16 |
dpavlin |
1 |
#include "dynamips.h" |
17 |
|
|
#include "memory.h" |
18 |
|
|
#include "device.h" |
19 |
|
|
|
20 |
|
|
#define PCI_VENDOR_SRAM 0x1137 |
21 |
|
|
#define PCI_PRODUCT_SRAM 0x0005 |
22 |
|
|
|
23 |
|
|
/* SRAM structure */ |
24 |
|
|
struct sram_data { |
25 |
|
|
/* VM object info */ |
26 |
|
|
vm_obj_t vm_obj; |
27 |
|
|
|
28 |
|
|
/* SRAM main device */ |
29 |
|
|
struct vdevice *dev; |
30 |
|
|
|
31 |
|
|
/* Aliased device */ |
32 |
|
|
char *alias_dev_name; |
33 |
|
|
struct vdevice *alias_dev; |
34 |
|
|
|
35 |
|
|
/* Byte-swapped device */ |
36 |
|
|
char *bs_dev_name; |
37 |
dpavlin |
7 |
vm_obj_t *bs_obj; |
38 |
dpavlin |
1 |
|
39 |
|
|
/* PCI device */ |
40 |
|
|
struct pci_device *pci_dev; |
41 |
|
|
|
42 |
|
|
/* Filename used to virtualize SRAM */ |
43 |
|
|
char *filename; |
44 |
|
|
}; |
45 |
|
|
|
46 |
|
|
/* Shutdown an SRAM device */ |
47 |
|
|
void dev_c7200_sram_shutdown(vm_instance_t *vm,struct sram_data *d) |
48 |
|
|
{ |
49 |
|
|
if (d != NULL) { |
50 |
|
|
/* Remove the PCI device */ |
51 |
|
|
pci_dev_remove(d->pci_dev); |
52 |
|
|
|
53 |
dpavlin |
7 |
/* Remove the byte-swapped device */ |
54 |
|
|
vm_object_remove(vm,d->bs_obj); |
55 |
|
|
|
56 |
|
|
/* Remove the alias and the main device */ |
57 |
dpavlin |
1 |
dev_remove(vm,d->alias_dev); |
58 |
|
|
dev_remove(vm,d->dev); |
59 |
|
|
|
60 |
|
|
/* Free devices */ |
61 |
|
|
free(d->alias_dev); |
62 |
|
|
free(d->dev); |
63 |
|
|
|
64 |
|
|
/* Free device names */ |
65 |
|
|
free(d->alias_dev_name); |
66 |
|
|
free(d->bs_dev_name); |
67 |
|
|
|
68 |
|
|
/* Remove filename used to virtualize SRAM */ |
69 |
|
|
if (d->filename) { |
70 |
|
|
unlink(d->filename); |
71 |
|
|
free(d->filename); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
/* Free the structure itself */ |
75 |
|
|
free(d); |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
/* Initialize an SRAM device */ |
80 |
|
|
int dev_c7200_sram_init(vm_instance_t *vm,char *name, |
81 |
|
|
m_uint64_t paddr,m_uint32_t len, |
82 |
|
|
struct pci_bus *pci_bus,int pci_device) |
83 |
|
|
{ |
84 |
|
|
m_uint64_t alias_paddr; |
85 |
|
|
struct sram_data *d; |
86 |
|
|
|
87 |
|
|
/* Allocate the private data structure for SRAM */ |
88 |
|
|
if (!(d = malloc(sizeof(*d)))) { |
89 |
|
|
fprintf(stderr,"dev_c7200_sram_init (%s): out of memory\n",name); |
90 |
|
|
return(-1); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
memset(d,0,sizeof(*d)); |
94 |
|
|
|
95 |
|
|
vm_object_init(&d->vm_obj); |
96 |
|
|
d->vm_obj.name = name; |
97 |
|
|
d->vm_obj.data = d; |
98 |
|
|
d->vm_obj.shutdown = (vm_shutdown_t)dev_c7200_sram_shutdown; |
99 |
|
|
|
100 |
|
|
if (!(d->filename = vm_build_filename(vm,name))) |
101 |
|
|
return(-1); |
102 |
|
|
|
103 |
|
|
/* add as a pci device */ |
104 |
|
|
d->pci_dev = pci_dev_add_basic(pci_bus,name, |
105 |
|
|
PCI_VENDOR_SRAM,PCI_PRODUCT_SRAM, |
106 |
|
|
pci_device,0); |
107 |
|
|
|
108 |
|
|
alias_paddr = 0x100000000ULL + paddr; |
109 |
|
|
|
110 |
|
|
/* create the standard RAM zone */ |
111 |
dpavlin |
7 |
if (!(d->dev = dev_create_ram(vm,name,FALSE,d->filename,paddr,len))) { |
112 |
dpavlin |
1 |
fprintf(stderr,"dev_c7200_sram_init: unable to create '%s' file.\n", |
113 |
|
|
d->filename); |
114 |
|
|
return(-1); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
/* create the RAM alias */ |
118 |
|
|
if (!(d->alias_dev_name = dyn_sprintf("%s_alias",name))) { |
119 |
|
|
fprintf(stderr,"dev_c7200_sram_init: unable to create alias name.\n"); |
120 |
|
|
return(-1); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
d->alias_dev = dev_create_ram_alias(vm,d->alias_dev_name,name, |
124 |
|
|
alias_paddr,len); |
125 |
|
|
|
126 |
|
|
if (!d->alias_dev) { |
127 |
|
|
fprintf(stderr,"dev_c7200_sram_init: unable to create alias device.\n"); |
128 |
|
|
return(-1); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
/* create the byte-swapped zone (used with Galileo DMA) */ |
132 |
|
|
if (!(d->bs_dev_name = dyn_sprintf("%s_bswap",name))) { |
133 |
|
|
fprintf(stderr,"dev_c7200_sram_init: unable to create BS name.\n"); |
134 |
|
|
return(-1); |
135 |
|
|
} |
136 |
|
|
|
137 |
dpavlin |
7 |
if (dev_bswap_init(vm,d->bs_dev_name,paddr+0x800000,len,paddr) == -1) { |
138 |
dpavlin |
1 |
fprintf(stderr,"dev_c7200_sram_init: unable to create BS device.\n"); |
139 |
|
|
return(-1); |
140 |
|
|
} |
141 |
dpavlin |
7 |
|
142 |
|
|
d->bs_obj = vm_object_find(vm,d->bs_dev_name); |
143 |
dpavlin |
1 |
return(0); |
144 |
|
|
} |