Statistics
| Branch: | Revision:

root / hw / pci / pci_host.c @ ddf5636d

History | View | Annotate | Download (5.6 kB)

1 4f5e19e6 Isaku Yamahata
/*
2 4f5e19e6 Isaku Yamahata
 * pci_host.c
3 4f5e19e6 Isaku Yamahata
 *
4 4f5e19e6 Isaku Yamahata
 * Copyright (c) 2009 Isaku Yamahata <yamahata at valinux co jp>
5 4f5e19e6 Isaku Yamahata
 *                    VA Linux Systems Japan K.K.
6 4f5e19e6 Isaku Yamahata
 *
7 4f5e19e6 Isaku Yamahata
 * This program is free software; you can redistribute it and/or modify
8 4f5e19e6 Isaku Yamahata
 * it under the terms of the GNU General Public License as published by
9 4f5e19e6 Isaku Yamahata
 * the Free Software Foundation; either version 2 of the License, or
10 4f5e19e6 Isaku Yamahata
 * (at your option) any later version.
11 4f5e19e6 Isaku Yamahata

12 4f5e19e6 Isaku Yamahata
 * This program is distributed in the hope that it will be useful,
13 4f5e19e6 Isaku Yamahata
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 4f5e19e6 Isaku Yamahata
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 4f5e19e6 Isaku Yamahata
 * GNU General Public License for more details.
16 4f5e19e6 Isaku Yamahata

17 4f5e19e6 Isaku Yamahata
 * You should have received a copy of the GNU General Public License along
18 70539e18 Blue Swirl
 * with this program; if not, see <http://www.gnu.org/licenses/>.
19 4f5e19e6 Isaku Yamahata
 */
20 4f5e19e6 Isaku Yamahata
21 c759b24f Michael S. Tsirkin
#include "hw/pci/pci.h"
22 c759b24f Michael S. Tsirkin
#include "hw/pci/pci_host.h"
23 3bf4dfdd Alexey Kardashevskiy
#include "trace.h"
24 4f5e19e6 Isaku Yamahata
25 4f5e19e6 Isaku Yamahata
/* debug PCI */
26 4f5e19e6 Isaku Yamahata
//#define DEBUG_PCI
27 4f5e19e6 Isaku Yamahata
28 4f5e19e6 Isaku Yamahata
#ifdef DEBUG_PCI
29 4f5e19e6 Isaku Yamahata
#define PCI_DPRINTF(fmt, ...) \
30 4f5e19e6 Isaku Yamahata
do { printf("pci_host_data: " fmt , ## __VA_ARGS__); } while (0)
31 4f5e19e6 Isaku Yamahata
#else
32 4f5e19e6 Isaku Yamahata
#define PCI_DPRINTF(fmt, ...)
33 4f5e19e6 Isaku Yamahata
#endif
34 4f5e19e6 Isaku Yamahata
35 766347cc Isaku Yamahata
/*
36 766347cc Isaku Yamahata
 * PCI address
37 766347cc Isaku Yamahata
 * bit 16 - 24: bus number
38 766347cc Isaku Yamahata
 * bit  8 - 15: devfun number
39 766347cc Isaku Yamahata
 * bit  0 -  7: offset in configuration space of a given pci device
40 766347cc Isaku Yamahata
 */
41 766347cc Isaku Yamahata
42 085d8134 Peter Maydell
/* the helper function to get a PCIDevice* for a given pci address */
43 8d6514f8 Isaku Yamahata
static inline PCIDevice *pci_dev_find_by_addr(PCIBus *bus, uint32_t addr)
44 766347cc Isaku Yamahata
{
45 42331e9f Isaku Yamahata
    uint8_t bus_num = addr >> 16;
46 42331e9f Isaku Yamahata
    uint8_t devfn = addr >> 8;
47 42331e9f Isaku Yamahata
48 5256d8bf Isaku Yamahata
    return pci_find_device(bus, bus_num, devfn);
49 766347cc Isaku Yamahata
}
50 766347cc Isaku Yamahata
51 42e4126b Jan Kiszka
void pci_host_config_write_common(PCIDevice *pci_dev, uint32_t addr,
52 42e4126b Jan Kiszka
                                  uint32_t limit, uint32_t val, uint32_t len)
53 42e4126b Jan Kiszka
{
54 42e4126b Jan Kiszka
    assert(len <= 4);
55 3bf4dfdd Alexey Kardashevskiy
    trace_pci_cfg_write(pci_dev->name, PCI_SLOT(pci_dev->devfn),
56 3bf4dfdd Alexey Kardashevskiy
                        PCI_FUNC(pci_dev->devfn), addr, val);
57 42e4126b Jan Kiszka
    pci_dev->config_write(pci_dev, addr, val, MIN(len, limit - addr));
58 42e4126b Jan Kiszka
}
59 42e4126b Jan Kiszka
60 42e4126b Jan Kiszka
uint32_t pci_host_config_read_common(PCIDevice *pci_dev, uint32_t addr,
61 42e4126b Jan Kiszka
                                     uint32_t limit, uint32_t len)
62 42e4126b Jan Kiszka
{
63 3bf4dfdd Alexey Kardashevskiy
    uint32_t ret;
64 3bf4dfdd Alexey Kardashevskiy
65 42e4126b Jan Kiszka
    assert(len <= 4);
66 3bf4dfdd Alexey Kardashevskiy
    ret = pci_dev->config_read(pci_dev, addr, MIN(len, limit - addr));
67 3bf4dfdd Alexey Kardashevskiy
    trace_pci_cfg_read(pci_dev->name, PCI_SLOT(pci_dev->devfn),
68 3bf4dfdd Alexey Kardashevskiy
                       PCI_FUNC(pci_dev->devfn), addr, ret);
69 3bf4dfdd Alexey Kardashevskiy
70 3bf4dfdd Alexey Kardashevskiy
    return ret;
71 42e4126b Jan Kiszka
}
72 42e4126b Jan Kiszka
73 ce195fb5 Isaku Yamahata
void pci_data_write(PCIBus *s, uint32_t addr, uint32_t val, int len)
74 766347cc Isaku Yamahata
{
75 8d6514f8 Isaku Yamahata
    PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
76 7ac901cd Isaku Yamahata
    uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
77 766347cc Isaku Yamahata
78 42e4126b Jan Kiszka
    if (!pci_dev) {
79 766347cc Isaku Yamahata
        return;
80 42e4126b Jan Kiszka
    }
81 766347cc Isaku Yamahata
82 0b987f19 Blue Swirl
    PCI_DPRINTF("%s: %s: addr=%02" PRIx32 " val=%08" PRIx32 " len=%d\n",
83 766347cc Isaku Yamahata
                __func__, pci_dev->name, config_addr, val, len);
84 42e4126b Jan Kiszka
    pci_host_config_write_common(pci_dev, config_addr, PCI_CONFIG_SPACE_SIZE,
85 42e4126b Jan Kiszka
                                 val, len);
86 766347cc Isaku Yamahata
}
87 766347cc Isaku Yamahata
88 ce195fb5 Isaku Yamahata
uint32_t pci_data_read(PCIBus *s, uint32_t addr, int len)
89 766347cc Isaku Yamahata
{
90 8d6514f8 Isaku Yamahata
    PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
91 7ac901cd Isaku Yamahata
    uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
92 766347cc Isaku Yamahata
    uint32_t val;
93 766347cc Isaku Yamahata
94 766347cc Isaku Yamahata
    if (!pci_dev) {
95 4677d8ed Michael S. Tsirkin
        return ~0x0;
96 766347cc Isaku Yamahata
    }
97 766347cc Isaku Yamahata
98 42e4126b Jan Kiszka
    val = pci_host_config_read_common(pci_dev, config_addr,
99 42e4126b Jan Kiszka
                                      PCI_CONFIG_SPACE_SIZE, len);
100 4677d8ed Michael S. Tsirkin
    PCI_DPRINTF("%s: %s: addr=%02"PRIx32" val=%08"PRIx32" len=%d\n",
101 4677d8ed Michael S. Tsirkin
                __func__, pci_dev->name, config_addr, val, len);
102 4677d8ed Michael S. Tsirkin
103 766347cc Isaku Yamahata
    return val;
104 766347cc Isaku Yamahata
}
105 766347cc Isaku Yamahata
106 a8170e5e Avi Kivity
static void pci_host_config_write(void *opaque, hwaddr addr,
107 d0ed8076 Avi Kivity
                                  uint64_t val, unsigned len)
108 a455783b Isaku Yamahata
{
109 d0ed8076 Avi Kivity
    PCIHostState *s = opaque;
110 a455783b Isaku Yamahata
111 d0ed8076 Avi Kivity
    PCI_DPRINTF("%s addr " TARGET_FMT_plx " len %d val %"PRIx64"\n",
112 9f6f0423 Michael S. Tsirkin
                __func__, addr, len, val);
113 cdde6ffc Avi Kivity
    if (addr != 0 || len != 4) {
114 cdde6ffc Avi Kivity
        return;
115 cdde6ffc Avi Kivity
    }
116 a455783b Isaku Yamahata
    s->config_reg = val;
117 a455783b Isaku Yamahata
}
118 a455783b Isaku Yamahata
119 a8170e5e Avi Kivity
static uint64_t pci_host_config_read(void *opaque, hwaddr addr,
120 d0ed8076 Avi Kivity
                                     unsigned len)
121 a455783b Isaku Yamahata
{
122 d0ed8076 Avi Kivity
    PCIHostState *s = opaque;
123 a455783b Isaku Yamahata
    uint32_t val = s->config_reg;
124 952760bb Blue Swirl
125 d0ed8076 Avi Kivity
    PCI_DPRINTF("%s addr " TARGET_FMT_plx " len %d val %"PRIx32"\n",
126 9f6f0423 Michael S. Tsirkin
                __func__, addr, len, val);
127 a455783b Isaku Yamahata
    return val;
128 a455783b Isaku Yamahata
}
129 a455783b Isaku Yamahata
130 a8170e5e Avi Kivity
static void pci_host_data_write(void *opaque, hwaddr addr,
131 d0ed8076 Avi Kivity
                                uint64_t val, unsigned len)
132 a455783b Isaku Yamahata
{
133 d0ed8076 Avi Kivity
    PCIHostState *s = opaque;
134 d0ed8076 Avi Kivity
    PCI_DPRINTF("write addr " TARGET_FMT_plx " len %d val %x\n",
135 d0ed8076 Avi Kivity
                addr, len, (unsigned)val);
136 9f6f0423 Michael S. Tsirkin
    if (s->config_reg & (1u << 31))
137 9f6f0423 Michael S. Tsirkin
        pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
138 a455783b Isaku Yamahata
}
139 a455783b Isaku Yamahata
140 d0ed8076 Avi Kivity
static uint64_t pci_host_data_read(void *opaque,
141 a8170e5e Avi Kivity
                                   hwaddr addr, unsigned len)
142 a455783b Isaku Yamahata
{
143 d0ed8076 Avi Kivity
    PCIHostState *s = opaque;
144 9f6f0423 Michael S. Tsirkin
    uint32_t val;
145 9f6f0423 Michael S. Tsirkin
    if (!(s->config_reg & (1 << 31)))
146 9f6f0423 Michael S. Tsirkin
        return 0xffffffff;
147 9f6f0423 Michael S. Tsirkin
    val = pci_data_read(s->bus, s->config_reg | (addr & 3), len);
148 d0ed8076 Avi Kivity
    PCI_DPRINTF("read addr " TARGET_FMT_plx " len %d val %x\n",
149 9f6f0423 Michael S. Tsirkin
                addr, len, val);
150 952760bb Blue Swirl
    return val;
151 9f6f0423 Michael S. Tsirkin
}
152 a455783b Isaku Yamahata
153 d0ed8076 Avi Kivity
const MemoryRegionOps pci_host_conf_le_ops = {
154 d0ed8076 Avi Kivity
    .read = pci_host_config_read,
155 d0ed8076 Avi Kivity
    .write = pci_host_config_write,
156 d0ed8076 Avi Kivity
    .endianness = DEVICE_LITTLE_ENDIAN,
157 d0ed8076 Avi Kivity
};
158 a455783b Isaku Yamahata
159 d0ed8076 Avi Kivity
const MemoryRegionOps pci_host_conf_be_ops = {
160 d0ed8076 Avi Kivity
    .read = pci_host_config_read,
161 d0ed8076 Avi Kivity
    .write = pci_host_config_write,
162 d0ed8076 Avi Kivity
    .endianness = DEVICE_BIG_ENDIAN,
163 d0ed8076 Avi Kivity
};
164 d2c33733 Avi Kivity
165 d0ed8076 Avi Kivity
const MemoryRegionOps pci_host_data_le_ops = {
166 d0ed8076 Avi Kivity
    .read = pci_host_data_read,
167 d0ed8076 Avi Kivity
    .write = pci_host_data_write,
168 d0ed8076 Avi Kivity
    .endianness = DEVICE_LITTLE_ENDIAN,
169 d0ed8076 Avi Kivity
};
170 d0ed8076 Avi Kivity
171 d0ed8076 Avi Kivity
const MemoryRegionOps pci_host_data_be_ops = {
172 d0ed8076 Avi Kivity
    .read = pci_host_data_read,
173 d0ed8076 Avi Kivity
    .write = pci_host_data_write,
174 d0ed8076 Avi Kivity
    .endianness = DEVICE_BIG_ENDIAN,
175 d0ed8076 Avi Kivity
};
176 a455783b Isaku Yamahata
177 b44ff9d4 Andreas Färber
static const TypeInfo pci_host_type_info = {
178 b44ff9d4 Andreas Färber
    .name = TYPE_PCI_HOST_BRIDGE,
179 b44ff9d4 Andreas Färber
    .parent = TYPE_SYS_BUS_DEVICE,
180 b44ff9d4 Andreas Färber
    .abstract = true,
181 568f0690 David Gibson
    .class_size = sizeof(PCIHostBridgeClass),
182 b44ff9d4 Andreas Färber
    .instance_size = sizeof(PCIHostState),
183 b44ff9d4 Andreas Färber
};
184 b44ff9d4 Andreas Färber
185 b44ff9d4 Andreas Färber
static void pci_host_register_types(void)
186 b44ff9d4 Andreas Färber
{
187 b44ff9d4 Andreas Färber
    type_register_static(&pci_host_type_info);
188 b44ff9d4 Andreas Färber
}
189 4f5e19e6 Isaku Yamahata
190 b44ff9d4 Andreas Färber
type_init(pci_host_register_types)