File: pci_io.c

package info (click to toggle)
dynamips 0.2.7-0.2.8RC2-5.1
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 4,184 kB
  • sloc: ansic: 70,972; makefile: 1,639; perl: 20
file content (130 lines) | stat: -rw-r--r-- 2,965 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * Cisco router simulation platform.
 * Copyright (c) 2006 Christophe Fillot (cf@utc.fr)
 *
 * PCI I/O space.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

#include "cpu.h"
#include "vm.h"
#include "dynamips.h"
#include "memory.h"
#include "device.h"
#include "pci_io.h"

/* Debugging flags */
#define DEBUG_ACCESS  0

/* Add a new PCI I/O device */
struct pci_io_device *pci_io_add(struct pci_io_data *d,
                                 m_uint32_t start,m_uint32_t end,
                                 struct vdevice *dev,dev_handler_t handler)
{
   struct pci_io_device *p;

   if (!(p = malloc(sizeof(*p)))) {
      fprintf(stderr,"pci_io_add: unable to create a new device.\n");
      return NULL;
   }

   p->start    = start;
   p->end      = end;
   p->real_dev = dev;
   p->handler  = handler;

   p->next = d->dev_list;
   p->pprev = &d->dev_list;

   if (d->dev_list != NULL)
      d->dev_list->pprev = &p->next;

   d->dev_list = p;
   return p;
}            

/* Remove a PCI I/O device */
void pci_io_remove(struct pci_io_device *dev)
{
   if (dev != NULL) {
      if (dev->next)
         dev->next->pprev = dev->pprev;

      *(dev->pprev) = dev->next;
      free(dev);
   }
}

/*
 * pci_io_access()
 */
static void *pci_io_access(cpu_gen_t *cpu,struct vdevice *dev,
                           m_uint32_t offset,u_int op_size,u_int op_type,
                           m_uint64_t *data)
{
   struct pci_io_data *d = dev->priv_data;
   struct pci_io_device *p;

#if DEBUG_ACCESS
   if (op_type == MTS_READ) {
      cpu_log(cpu,"PCI_IO","read request at pc=0x%llx, offset=0x%x\n",
              cpu_get_pc(cpu),offset);
   } else {
      cpu_log(cpu,"PCI_IO",
              "write request (data=0x%llx) at pc=0x%llx, offset=0x%x\n",
              *data,cpu_get_pc(cpu),offset);
   }
#endif

   if (op_type == MTS_READ)
      *data = 0;

   for(p=d->dev_list;p;p=p->next)
      if ((offset >= p->start) && (offset <= p->end)) {
         return(p->handler(cpu,p->real_dev,(offset - p->start),
                           op_size,op_type,data));
      }

   return NULL;
}

/* Remove PCI I/O space */
void pci_io_data_remove(vm_instance_t *vm,struct pci_io_data *d)
{
   if (d != NULL) {
      /* Remove the device */
      dev_remove(vm,&d->dev);

      /* Free the structure itself */
      free(d);
   }
}

/* Initialize PCI I/O space */
struct pci_io_data *pci_io_data_init(vm_instance_t *vm,m_uint64_t paddr)
{
   struct pci_io_data *d;

   /* Allocate the PCI I/O data structure */
   if (!(d = malloc(sizeof(*d)))) {
      fprintf(stderr,"PCI_IO: out of memory\n");
      return NULL;
   }

   memset(d,0,sizeof(*d));
   dev_init(&d->dev);
   d->dev.name      = "pci_io";
   d->dev.priv_data = d;
   d->dev.phys_addr = paddr;
   d->dev.phys_len  = 2 * 1048576;
   d->dev.handler   = pci_io_access;
   
   /* Map this device to the VM */
   vm_bind_device(vm,&d->dev);
   return(d);
}