/[dynamips]/upstream/dynamips-0.2.5/dev_c7200_sram.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /upstream/dynamips-0.2.5/dev_c7200_sram.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (hide annotations)
Sat Oct 6 16:01:44 2007 UTC (13 years, 1 month ago) by dpavlin
File MIME type: text/plain
File size: 4909 byte(s)
import 0.2.5 from upstream

1 dpavlin 1 /*
2     * Cisco 7200 (Predator) simulation platform.
3     * 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     #include "mips64.h"
15     #include "dynamips.h"
16     #include "memory.h"
17     #include "device.h"
18    
19     #define PCI_VENDOR_SRAM 0x1137
20     #define PCI_PRODUCT_SRAM 0x0005
21    
22     /* SRAM structure */
23     struct sram_data {
24     /* VM object info */
25     vm_obj_t vm_obj;
26    
27     /* SRAM main device */
28     struct vdevice *dev;
29    
30     /* Aliased device */
31     char *alias_dev_name;
32     struct vdevice *alias_dev;
33    
34     /* Byte-swapped device */
35     char *bs_dev_name;
36     struct vdevice *bs_dev;
37    
38     /* PCI device */
39     struct pci_device *pci_dev;
40    
41     /* Filename used to virtualize SRAM */
42     char *filename;
43     };
44    
45     /*
46     * SRAM byte swapped access.
47     */
48     static void *dev_sram_bs_access(cpu_mips_t *cpu,struct vdevice *dev,
49     m_uint32_t offset,u_int op_size,u_int op_type,
50     m_uint64_t *data)
51     {
52     void *ptr = (u_char *)dev->host_addr + offset;
53    
54     switch(op_size) {
55     case 1:
56     return(ptr);
57    
58     case 2:
59     if (op_type == MTS_READ)
60     *data = swap16(htovm16(*(m_uint16_t *)ptr));
61     else
62     *(m_uint16_t *)ptr = vmtoh16(swap16(*data));
63     break;
64    
65     case 4:
66     if (op_type == MTS_READ)
67     *data = swap32(htovm32(*(m_uint32_t *)ptr));
68     else
69     *(m_uint32_t *)ptr = vmtoh32(swap32(*data));
70     break;
71    
72     case 8:
73     if (op_type == MTS_READ)
74     *data = swap64(htovm64(*(m_uint64_t *)ptr));
75     else
76     *(m_uint64_t *)ptr = vmtoh64(swap64(*data));
77     break;
78     }
79    
80     return NULL;
81     }
82    
83     /* Shutdown an SRAM device */
84     void dev_c7200_sram_shutdown(vm_instance_t *vm,struct sram_data *d)
85     {
86     if (d != NULL) {
87     /* Remove the PCI device */
88     pci_dev_remove(d->pci_dev);
89    
90     /* Remove the alias, the byte-swapped and the main device */
91     dev_remove(vm,d->alias_dev);
92     dev_remove(vm,d->bs_dev);
93     dev_remove(vm,d->dev);
94    
95     /* Free devices */
96     free(d->alias_dev);
97     free(d->bs_dev);
98     free(d->dev);
99    
100     /* Free device names */
101     free(d->alias_dev_name);
102     free(d->bs_dev_name);
103    
104     /* Remove filename used to virtualize SRAM */
105     if (d->filename) {
106     unlink(d->filename);
107     free(d->filename);
108     }
109    
110     /* Free the structure itself */
111     free(d);
112     }
113     }
114    
115     /* Initialize an SRAM device */
116     int dev_c7200_sram_init(vm_instance_t *vm,char *name,
117     m_uint64_t paddr,m_uint32_t len,
118     struct pci_bus *pci_bus,int pci_device)
119     {
120     m_uint64_t alias_paddr;
121     struct sram_data *d;
122    
123     /* Allocate the private data structure for SRAM */
124     if (!(d = malloc(sizeof(*d)))) {
125     fprintf(stderr,"dev_c7200_sram_init (%s): out of memory\n",name);
126     return(-1);
127     }
128    
129     memset(d,0,sizeof(*d));
130    
131     vm_object_init(&d->vm_obj);
132     d->vm_obj.name = name;
133     d->vm_obj.data = d;
134     d->vm_obj.shutdown = (vm_shutdown_t)dev_c7200_sram_shutdown;
135    
136     if (!(d->filename = vm_build_filename(vm,name)))
137     return(-1);
138    
139     /* add as a pci device */
140     d->pci_dev = pci_dev_add_basic(pci_bus,name,
141     PCI_VENDOR_SRAM,PCI_PRODUCT_SRAM,
142     pci_device,0);
143    
144     alias_paddr = 0x100000000ULL + paddr;
145    
146     /* create the standard RAM zone */
147     if (!(d->dev = dev_create_ram(vm,name,d->filename,paddr,len))) {
148     fprintf(stderr,"dev_c7200_sram_init: unable to create '%s' file.\n",
149     d->filename);
150     return(-1);
151     }
152    
153     /* create the RAM alias */
154     if (!(d->alias_dev_name = dyn_sprintf("%s_alias",name))) {
155     fprintf(stderr,"dev_c7200_sram_init: unable to create alias name.\n");
156     return(-1);
157     }
158    
159     d->alias_dev = dev_create_ram_alias(vm,d->alias_dev_name,name,
160     alias_paddr,len);
161    
162     if (!d->alias_dev) {
163     fprintf(stderr,"dev_c7200_sram_init: unable to create alias device.\n");
164     return(-1);
165     }
166    
167     /* create the byte-swapped zone (used with Galileo DMA) */
168     if (!(d->bs_dev_name = dyn_sprintf("%s_bswap",name))) {
169     fprintf(stderr,"dev_c7200_sram_init: unable to create BS name.\n");
170     return(-1);
171     }
172    
173     if (!(d->bs_dev = dev_create(d->bs_dev_name))) {
174     fprintf(stderr,"dev_c7200_sram_init: unable to create BS device.\n");
175     return(-1);
176     }
177    
178     d->bs_dev->phys_addr = paddr + 0x800000;
179     d->bs_dev->phys_len = len;
180     d->bs_dev->handler = dev_sram_bs_access;
181     d->bs_dev->host_addr = (m_iptr_t)d->dev->host_addr;
182     d->bs_dev->flags = VDEVICE_FLAG_NO_MTS_MMAP|VDEVICE_FLAG_CACHING;
183     d->bs_dev->flags |= VDEVICE_FLAG_REMAP;
184     vm_bind_device(vm,d->bs_dev);
185     vm_object_add(vm,&d->vm_obj);
186     return(0);
187     }

  ViewVC Help
Powered by ViewVC 1.1.26