| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005,2006 Christophe Fillot ([email protected]) |
| 4 |
*/ |
| 5 |
|
| 6 |
#ifndef __DEVICE_H__ |
| 7 |
#define __DEVICE_H__ |
| 8 |
|
| 9 |
#include <sys/types.h> |
| 10 |
#include "utils.h" |
| 11 |
#include "cpu.h" |
| 12 |
#include "net_io.h" |
| 13 |
#include "vm.h" |
| 14 |
|
| 15 |
/* Device Flags */ |
| 16 |
#define VDEVICE_FLAG_NO_MTS_MMAP 0x01 /* Prevent MMAPed access by MTS */ |
| 17 |
#define VDEVICE_FLAG_CACHING 0x02 /* Device does support caching */ |
| 18 |
#define VDEVICE_FLAG_REMAP 0x04 /* Physical address remapping */ |
| 19 |
#define VDEVICE_FLAG_SYNC 0x08 /* Forced sync */ |
| 20 |
#define VDEVICE_FLAG_SPARSE 0x10 /* Sparse device */ |
| 21 |
#define VDEVICE_FLAG_GHOST 0x20 /* Ghost device */ |
| 22 |
|
| 23 |
#define VDEVICE_PTE_DIRTY 0x01 |
| 24 |
|
| 25 |
typedef void *(*dev_handler_t)(cpu_gen_t *cpu,struct vdevice *dev, |
| 26 |
m_uint32_t offset,u_int op_size,u_int op_type, |
| 27 |
m_uint64_t *data); |
| 28 |
|
| 29 |
/* Virtual Device */ |
| 30 |
struct vdevice { |
| 31 |
char *name; |
| 32 |
u_int id; |
| 33 |
m_uint64_t phys_addr; |
| 34 |
m_uint32_t phys_len; |
| 35 |
m_iptr_t host_addr; |
| 36 |
void *priv_data; |
| 37 |
int flags; |
| 38 |
int fd; |
| 39 |
dev_handler_t handler; |
| 40 |
m_iptr_t *sparse_map; |
| 41 |
struct vdevice *next,**pprev; |
| 42 |
}; |
| 43 |
|
| 44 |
/* PCI part */ |
| 45 |
#include "pci_dev.h" |
| 46 |
|
| 47 |
/* device access function */ |
| 48 |
#ifdef MAC64HACK |
| 49 |
static void *__dev_access_fast(cpu_gen_t *cpu,u_int dev_id,m_uint32_t offset, |
| 50 |
u_int op_size,u_int op_type,m_uint64_t *data) |
| 51 |
{ |
| 52 |
struct vdevice *dev = cpu->vm->dev_array[dev_id]; |
| 53 |
|
| 54 |
if (unlikely(!dev)) { |
| 55 |
cpu_log(cpu,"dev_access_fast","null handler (dev_id=%u,offset=0x%x)\n", |
| 56 |
dev_id,offset); |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
|
| 60 |
#if DEBUG_DEV_PERF_CNT |
| 61 |
cpu->dev_access_counter++; |
| 62 |
#endif |
| 63 |
|
| 64 |
return(dev->handler(cpu,dev,offset,op_size,op_type,data)); |
| 65 |
} |
| 66 |
|
| 67 |
static forced_inline |
| 68 |
void *dev_access_fast(cpu_gen_t *cpu,u_int dev_id,m_uint32_t offset, |
| 69 |
u_int op_size,u_int op_type,m_uint64_t *data) |
| 70 |
{ |
| 71 |
asm("sub $8, %rsp"); |
| 72 |
void* ret = __dev_access_fast(cpu, dev_id, offset, op_size, op_type, data); |
| 73 |
asm("add $8, %rsp"); |
| 74 |
return ret; |
| 75 |
} |
| 76 |
|
| 77 |
#else |
| 78 |
static forced_inline |
| 79 |
void *dev_access_fast(cpu_gen_t *cpu,u_int dev_id,m_uint32_t offset, |
| 80 |
u_int op_size,u_int op_type,m_uint64_t *data) |
| 81 |
{ |
| 82 |
struct vdevice *dev = cpu->vm->dev_array[dev_id]; |
| 83 |
|
| 84 |
if (unlikely(!dev)) { |
| 85 |
cpu_log(cpu,"dev_access_fast","null handler (dev_id=%u,offset=0x%x)\n", |
| 86 |
dev_id,offset); |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
|
| 90 |
#if DEBUG_DEV_PERF_CNT |
| 91 |
cpu->dev_access_counter++; |
| 92 |
#endif |
| 93 |
|
| 94 |
return(dev->handler(cpu,dev,offset,op_size,op_type,data)); |
| 95 |
} |
| 96 |
#endif |
| 97 |
|
| 98 |
/* Get device by ID */ |
| 99 |
struct vdevice *dev_get_by_id(vm_instance_t *vm,u_int dev_id); |
| 100 |
|
| 101 |
/* Get device by name */ |
| 102 |
struct vdevice *dev_get_by_name(vm_instance_t *vm,char *name); |
| 103 |
|
| 104 |
/* Device lookup by physical address */ |
| 105 |
struct vdevice *dev_lookup(vm_instance_t *vm,m_uint64_t phys_addr,int cached); |
| 106 |
|
| 107 |
/* Find the next device after the specified address */ |
| 108 |
struct vdevice *dev_lookup_next(vm_instance_t *vm,m_uint64_t phys_addr, |
| 109 |
struct vdevice *dev_start,int cached); |
| 110 |
|
| 111 |
/* Initialize a device */ |
| 112 |
void dev_init(struct vdevice *dev); |
| 113 |
|
| 114 |
/* Allocate a device */ |
| 115 |
struct vdevice *dev_create(char *name); |
| 116 |
|
| 117 |
/* Remove a device */ |
| 118 |
void dev_remove(vm_instance_t *vm,struct vdevice *dev); |
| 119 |
|
| 120 |
/* Show properties of a device */ |
| 121 |
void dev_show(struct vdevice *dev); |
| 122 |
|
| 123 |
/* Show the device list */ |
| 124 |
void dev_show_list(vm_instance_t *vm); |
| 125 |
|
| 126 |
/* device access function */ |
| 127 |
void *dev_access(cpu_gen_t *cpu,u_int dev_id,m_uint32_t offset, |
| 128 |
u_int op_size,u_int op_type,m_uint64_t *data); |
| 129 |
|
| 130 |
/* Synchronize memory for a memory-mapped (mmap) device */ |
| 131 |
int dev_sync(struct vdevice *dev); |
| 132 |
|
| 133 |
/* Remap a device at specified physical address */ |
| 134 |
struct vdevice *dev_remap(char *name,struct vdevice *orig, |
| 135 |
m_uint64_t paddr,m_uint32_t len); |
| 136 |
|
| 137 |
/* Create a RAM device */ |
| 138 |
struct vdevice *dev_create_ram(vm_instance_t *vm,char *name, |
| 139 |
int sparse,char *filename, |
| 140 |
m_uint64_t paddr,m_uint32_t len); |
| 141 |
|
| 142 |
/* Create a ghosted RAM device */ |
| 143 |
struct vdevice * |
| 144 |
dev_create_ghost_ram(vm_instance_t *vm,char *name,int sparse,char *filename, |
| 145 |
m_uint64_t paddr,m_uint32_t len); |
| 146 |
|
| 147 |
/* Create a memory alias */ |
| 148 |
struct vdevice *dev_create_ram_alias(vm_instance_t *vm,char *name,char *orig, |
| 149 |
m_uint64_t paddr,m_uint32_t len); |
| 150 |
|
| 151 |
/* Initialize a sparse device */ |
| 152 |
int dev_sparse_init(struct vdevice *dev); |
| 153 |
|
| 154 |
/* Shutdown sparse device structures */ |
| 155 |
int dev_sparse_shutdown(struct vdevice *dev); |
| 156 |
|
| 157 |
/* Get an host address for a sparse device */ |
| 158 |
m_iptr_t dev_sparse_get_host_addr(vm_instance_t *vm,struct vdevice *dev, |
| 159 |
m_uint64_t paddr,u_int op_type,int *cow); |
| 160 |
|
| 161 |
/* Get virtual address space used on host for the specified device */ |
| 162 |
size_t dev_get_vspace_size(struct vdevice *dev); |
| 163 |
|
| 164 |
/* Create a dummy console */ |
| 165 |
int dev_create_dummy_console(vm_instance_t *vm); |
| 166 |
|
| 167 |
/* Initialized a zeroed memory zone */ |
| 168 |
int dev_zero_init(vm_instance_t *vm,char *name, |
| 169 |
m_uint64_t paddr,m_uint32_t len); |
| 170 |
|
| 171 |
/* Initialized a byte-swap device */ |
| 172 |
int dev_bswap_init(vm_instance_t *vm,char *name, |
| 173 |
m_uint64_t paddr,m_uint32_t len,m_uint64_t remap_addr); |
| 174 |
|
| 175 |
/* Initialize a RAM zone */ |
| 176 |
int dev_ram_init(vm_instance_t *vm,char *name,int use_mmap,int delete_file, |
| 177 |
char *alternate_name,int sparse, |
| 178 |
m_uint64_t paddr,m_uint32_t len); |
| 179 |
|
| 180 |
/* Initialize a ghosted RAM zone */ |
| 181 |
int dev_ram_ghost_init(vm_instance_t *vm,char *name,int sparse,char *filename, |
| 182 |
m_uint64_t paddr,m_uint32_t len); |
| 183 |
|
| 184 |
/* Create the NVRAM device */ |
| 185 |
int dev_nvram_init(vm_instance_t *vm,char *name, |
| 186 |
m_uint64_t paddr,m_uint32_t len, |
| 187 |
u_int *conf_reg); |
| 188 |
|
| 189 |
/* Create a 8 Mb bootflash */ |
| 190 |
int dev_bootflash_init(vm_instance_t *vm,char *name,char *model, |
| 191 |
m_uint64_t paddr); |
| 192 |
|
| 193 |
/* Create a Flash device */ |
| 194 |
vm_obj_t *dev_flash_init(vm_instance_t *vm,char *name, |
| 195 |
m_uint64_t paddr,m_uint32_t len); |
| 196 |
|
| 197 |
/* Copy data directly to a flash device */ |
| 198 |
int dev_flash_copy_data(vm_obj_t *obj,m_uint32_t offset, |
| 199 |
u_char *ptr,ssize_t len); |
| 200 |
|
| 201 |
/* dev_dec21050_init() */ |
| 202 |
int dev_dec21050_init(struct pci_bus *pci_bus,int pci_device, |
| 203 |
struct pci_bus *sec_bus); |
| 204 |
|
| 205 |
/* dev_dec21052_init() */ |
| 206 |
int dev_dec21052_init(struct pci_bus *pci_bus,int pci_device, |
| 207 |
struct pci_bus *sec_bus); |
| 208 |
|
| 209 |
/* dev_dec21150_init() */ |
| 210 |
int dev_dec21150_init(struct pci_bus *pci_bus,int pci_device, |
| 211 |
struct pci_bus *sec_bus); |
| 212 |
|
| 213 |
/* dev_dec21152_init() */ |
| 214 |
int dev_dec21152_init(struct pci_bus *pci_bus,int pci_device, |
| 215 |
struct pci_bus *sec_bus); |
| 216 |
|
| 217 |
/* dev_dec21154_init() */ |
| 218 |
int dev_dec21154_init(struct pci_bus *pci_bus,int pci_device, |
| 219 |
struct pci_bus *sec_bus); |
| 220 |
|
| 221 |
/* dev_pericom_init() */ |
| 222 |
int dev_pericom_init(struct pci_bus *pci_bus,int pci_device, |
| 223 |
struct pci_bus *sec_bus); |
| 224 |
|
| 225 |
/* dev_ti2050b_init() */ |
| 226 |
int dev_ti2050b_init(struct pci_bus *pci_bus,int pci_device, |
| 227 |
struct pci_bus *sec_bus); |
| 228 |
|
| 229 |
/* Create an AP1011 Sturgeon HyperTransport-PCI Bridge */ |
| 230 |
int dev_ap1011_init(struct pci_bus *pci_bus,int pci_device, |
| 231 |
struct pci_bus *sec_bus); |
| 232 |
|
| 233 |
/* dev_plx6520cb_init() */ |
| 234 |
int dev_plx6520cb_init(struct pci_bus *pci_bus,int pci_device, |
| 235 |
struct pci_bus *sec_bus); |
| 236 |
|
| 237 |
/* dev_clpd6729_init() */ |
| 238 |
int dev_clpd6729_init(vm_instance_t *vm, |
| 239 |
struct pci_bus *pci_bus,int pci_device, |
| 240 |
struct pci_io_data *pci_io_data, |
| 241 |
m_uint32_t io_start,m_uint32_t io_end); |
| 242 |
|
| 243 |
/* Create a NS16552 device */ |
| 244 |
int dev_ns16552_init(vm_instance_t *vm,m_uint64_t paddr,m_uint32_t len, |
| 245 |
u_int reg_div,u_int irq,vtty_t *vtty_A,vtty_t *vtty_B); |
| 246 |
|
| 247 |
/* Initialize an SRAM device */ |
| 248 |
int dev_c7200_sram_init(vm_instance_t *vm,char *name, |
| 249 |
m_uint64_t paddr,m_uint32_t len, |
| 250 |
struct pci_bus *pci_bus,int pci_device); |
| 251 |
|
| 252 |
/* Initialize a PCMCIA disk */ |
| 253 |
vm_obj_t *dev_pcmcia_disk_init(vm_instance_t *vm,char *name, |
| 254 |
m_uint64_t paddr,m_uint32_t len, |
| 255 |
u_int disk_size,int mode); |
| 256 |
|
| 257 |
/* Get the device associated with a PCMCIA disk object */ |
| 258 |
struct vdevice *dev_pcmcia_disk_get_device(vm_obj_t *obj); |
| 259 |
|
| 260 |
/* Create SB-1 system control devices */ |
| 261 |
int dev_sb1_init(vm_instance_t *vm); |
| 262 |
|
| 263 |
/* Create SB-1 I/O devices */ |
| 264 |
int dev_sb1_io_init(vm_instance_t *vm,u_int duart_irq); |
| 265 |
|
| 266 |
/* Create the SB-1 PCI bus configuration zone */ |
| 267 |
int dev_sb1_pci_init(vm_instance_t *vm,char *name,m_uint64_t paddr); |
| 268 |
|
| 269 |
/* dev_sb1_duart_init() */ |
| 270 |
int dev_sb1_duart_init(vm_instance_t *vm,m_uint64_t paddr,m_uint32_t len); |
| 271 |
|
| 272 |
/* remote control device */ |
| 273 |
int dev_remote_control_init(vm_instance_t *vm,m_uint64_t paddr,m_uint32_t len); |
| 274 |
|
| 275 |
int generic_nvram_extract_config(vm_instance_t *vm, char *dev_name, size_t nvram_offset, size_t nvram_size, m_uint32_t addr, u_int format, |
| 276 |
u_char **startup_config, size_t *startup_len, u_char **private_config, size_t *private_len); |
| 277 |
|
| 278 |
int generic_nvram_push_config(vm_instance_t *vm, char *dev_name, size_t file_size, size_t nvram_offset, size_t nvram_size, m_uint32_t addr, u_int format, |
| 279 |
u_char *startup_config, size_t startup_len, u_char *private_config, size_t private_len); |
| 280 |
|
| 281 |
#endif |