Statistics
| Branch: | Revision:

root / hw / pci_host.c @ 92f562ec

History | View | Annotate | Download (5.5 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 4f5e19e6 Isaku Yamahata
#include "pci.h"
22 4f5e19e6 Isaku Yamahata
#include "pci_host.h"
23 4f5e19e6 Isaku Yamahata
24 4f5e19e6 Isaku Yamahata
/* debug PCI */
25 4f5e19e6 Isaku Yamahata
//#define DEBUG_PCI
26 4f5e19e6 Isaku Yamahata
27 4f5e19e6 Isaku Yamahata
#ifdef DEBUG_PCI
28 4f5e19e6 Isaku Yamahata
#define PCI_DPRINTF(fmt, ...) \
29 4f5e19e6 Isaku Yamahata
do { printf("pci_host_data: " fmt , ## __VA_ARGS__); } while (0)
30 4f5e19e6 Isaku Yamahata
#else
31 4f5e19e6 Isaku Yamahata
#define PCI_DPRINTF(fmt, ...)
32 4f5e19e6 Isaku Yamahata
#endif
33 4f5e19e6 Isaku Yamahata
34 766347cc Isaku Yamahata
/*
35 766347cc Isaku Yamahata
 * PCI address
36 766347cc Isaku Yamahata
 * bit 16 - 24: bus number
37 766347cc Isaku Yamahata
 * bit  8 - 15: devfun number
38 766347cc Isaku Yamahata
 * bit  0 -  7: offset in configuration space of a given pci device
39 766347cc Isaku Yamahata
 */
40 766347cc Isaku Yamahata
41 766347cc Isaku Yamahata
/* the helper functio to get a PCIDeice* for a given pci address */
42 8d6514f8 Isaku Yamahata
static inline PCIDevice *pci_dev_find_by_addr(PCIBus *bus, uint32_t addr)
43 766347cc Isaku Yamahata
{
44 42331e9f Isaku Yamahata
    uint8_t bus_num = addr >> 16;
45 42331e9f Isaku Yamahata
    uint8_t devfn = addr >> 8;
46 42331e9f Isaku Yamahata
47 5256d8bf Isaku Yamahata
    return pci_find_device(bus, bus_num, devfn);
48 766347cc Isaku Yamahata
}
49 766347cc Isaku Yamahata
50 42e4126b Jan Kiszka
void pci_host_config_write_common(PCIDevice *pci_dev, uint32_t addr,
51 42e4126b Jan Kiszka
                                  uint32_t limit, uint32_t val, uint32_t len)
52 42e4126b Jan Kiszka
{
53 42e4126b Jan Kiszka
    assert(len <= 4);
54 42e4126b Jan Kiszka
    pci_dev->config_write(pci_dev, addr, val, MIN(len, limit - addr));
55 42e4126b Jan Kiszka
}
56 42e4126b Jan Kiszka
57 42e4126b Jan Kiszka
uint32_t pci_host_config_read_common(PCIDevice *pci_dev, uint32_t addr,
58 42e4126b Jan Kiszka
                                     uint32_t limit, uint32_t len)
59 42e4126b Jan Kiszka
{
60 42e4126b Jan Kiszka
    assert(len <= 4);
61 42e4126b Jan Kiszka
    return pci_dev->config_read(pci_dev, addr, MIN(len, limit - addr));
62 42e4126b Jan Kiszka
}
63 42e4126b Jan Kiszka
64 ce195fb5 Isaku Yamahata
void pci_data_write(PCIBus *s, uint32_t addr, uint32_t val, int len)
65 766347cc Isaku Yamahata
{
66 8d6514f8 Isaku Yamahata
    PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
67 7ac901cd Isaku Yamahata
    uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
68 766347cc Isaku Yamahata
69 42e4126b Jan Kiszka
    if (!pci_dev) {
70 766347cc Isaku Yamahata
        return;
71 42e4126b Jan Kiszka
    }
72 766347cc Isaku Yamahata
73 0b987f19 Blue Swirl
    PCI_DPRINTF("%s: %s: addr=%02" PRIx32 " val=%08" PRIx32 " len=%d\n",
74 766347cc Isaku Yamahata
                __func__, pci_dev->name, config_addr, val, len);
75 42e4126b Jan Kiszka
    pci_host_config_write_common(pci_dev, config_addr, PCI_CONFIG_SPACE_SIZE,
76 42e4126b Jan Kiszka
                                 val, len);
77 766347cc Isaku Yamahata
}
78 766347cc Isaku Yamahata
79 ce195fb5 Isaku Yamahata
uint32_t pci_data_read(PCIBus *s, uint32_t addr, int len)
80 766347cc Isaku Yamahata
{
81 8d6514f8 Isaku Yamahata
    PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
82 7ac901cd Isaku Yamahata
    uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
83 766347cc Isaku Yamahata
    uint32_t val;
84 766347cc Isaku Yamahata
85 766347cc Isaku Yamahata
    if (!pci_dev) {
86 4677d8ed Michael S. Tsirkin
        return ~0x0;
87 766347cc Isaku Yamahata
    }
88 766347cc Isaku Yamahata
89 42e4126b Jan Kiszka
    val = pci_host_config_read_common(pci_dev, config_addr,
90 42e4126b Jan Kiszka
                                      PCI_CONFIG_SPACE_SIZE, len);
91 4677d8ed Michael S. Tsirkin
    PCI_DPRINTF("%s: %s: addr=%02"PRIx32" val=%08"PRIx32" len=%d\n",
92 4677d8ed Michael S. Tsirkin
                __func__, pci_dev->name, config_addr, val, len);
93 4677d8ed Michael S. Tsirkin
94 766347cc Isaku Yamahata
    return val;
95 766347cc Isaku Yamahata
}
96 766347cc Isaku Yamahata
97 6ebf5905 Alexander Graf
static void pci_host_config_write(ReadWriteHandler *handler,
98 6ebf5905 Alexander Graf
                                  pcibus_t addr, uint32_t val, int len)
99 a455783b Isaku Yamahata
{
100 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, conf_handler);
101 a455783b Isaku Yamahata
102 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("%s addr %" FMT_PCIBUS " %d val %"PRIx32"\n",
103 9f6f0423 Michael S. Tsirkin
                __func__, addr, len, val);
104 a455783b Isaku Yamahata
    s->config_reg = val;
105 a455783b Isaku Yamahata
}
106 a455783b Isaku Yamahata
107 6ebf5905 Alexander Graf
static uint32_t pci_host_config_read(ReadWriteHandler *handler,
108 6ebf5905 Alexander Graf
                                     pcibus_t addr, int len)
109 a455783b Isaku Yamahata
{
110 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, conf_handler);
111 a455783b Isaku Yamahata
    uint32_t val = s->config_reg;
112 952760bb Blue Swirl
113 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("%s addr %" FMT_PCIBUS " len %d val %"PRIx32"\n",
114 9f6f0423 Michael S. Tsirkin
                __func__, addr, len, val);
115 a455783b Isaku Yamahata
    return val;
116 a455783b Isaku Yamahata
}
117 a455783b Isaku Yamahata
118 6ebf5905 Alexander Graf
static void pci_host_data_write(ReadWriteHandler *handler,
119 6ebf5905 Alexander Graf
                                pcibus_t addr, uint32_t val, int len)
120 a455783b Isaku Yamahata
{
121 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, data_handler);
122 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("write addr %" FMT_PCIBUS " len %d val %x\n",
123 9f6f0423 Michael S. Tsirkin
                addr, len, val);
124 9f6f0423 Michael S. Tsirkin
    if (s->config_reg & (1u << 31))
125 9f6f0423 Michael S. Tsirkin
        pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
126 a455783b Isaku Yamahata
}
127 a455783b Isaku Yamahata
128 6ebf5905 Alexander Graf
static uint32_t pci_host_data_read(ReadWriteHandler *handler,
129 6ebf5905 Alexander Graf
                                   pcibus_t addr, int len)
130 a455783b Isaku Yamahata
{
131 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, data_handler);
132 9f6f0423 Michael S. Tsirkin
    uint32_t val;
133 9f6f0423 Michael S. Tsirkin
    if (!(s->config_reg & (1 << 31)))
134 9f6f0423 Michael S. Tsirkin
        return 0xffffffff;
135 9f6f0423 Michael S. Tsirkin
    val = pci_data_read(s->bus, s->config_reg | (addr & 3), len);
136 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("read addr %" FMT_PCIBUS " len %d val %x\n",
137 9f6f0423 Michael S. Tsirkin
                addr, len, val);
138 952760bb Blue Swirl
    return val;
139 9f6f0423 Michael S. Tsirkin
}
140 a455783b Isaku Yamahata
141 952760bb Blue Swirl
static void pci_host_init(PCIHostState *s)
142 952760bb Blue Swirl
{
143 6ebf5905 Alexander Graf
    s->conf_handler.write = pci_host_config_write;
144 6ebf5905 Alexander Graf
    s->conf_handler.read = pci_host_config_read;
145 6ebf5905 Alexander Graf
    s->data_handler.write = pci_host_data_write;
146 6ebf5905 Alexander Graf
    s->data_handler.read = pci_host_data_read;
147 952760bb Blue Swirl
}
148 952760bb Blue Swirl
149 6ebf5905 Alexander Graf
int pci_host_conf_register_mmio(PCIHostState *s, int endian)
150 9f6f0423 Michael S. Tsirkin
{
151 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
152 6ebf5905 Alexander Graf
    return cpu_register_io_memory_simple(&s->conf_handler, endian);
153 a455783b Isaku Yamahata
}
154 a455783b Isaku Yamahata
155 f08b32fe Isaku Yamahata
void pci_host_conf_register_ioport(pio_addr_t ioport, PCIHostState *s)
156 a455783b Isaku Yamahata
{
157 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
158 6ebf5905 Alexander Graf
    register_ioport_simple(&s->conf_handler, ioport, 4, 4);
159 c646f74f Gleb Natapov
    sysbus_init_ioports(&s->busdev, ioport, 4);
160 a455783b Isaku Yamahata
}
161 a455783b Isaku Yamahata
162 6ebf5905 Alexander Graf
int pci_host_data_register_mmio(PCIHostState *s, int endian)
163 4f5e19e6 Isaku Yamahata
{
164 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
165 6ebf5905 Alexander Graf
    return cpu_register_io_memory_simple(&s->data_handler, endian);
166 4f5e19e6 Isaku Yamahata
}
167 4f5e19e6 Isaku Yamahata
168 4f5e19e6 Isaku Yamahata
void pci_host_data_register_ioport(pio_addr_t ioport, PCIHostState *s)
169 4f5e19e6 Isaku Yamahata
{
170 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
171 6ebf5905 Alexander Graf
    register_ioport_simple(&s->data_handler, ioport, 4, 1);
172 6ebf5905 Alexander Graf
    register_ioport_simple(&s->data_handler, ioport, 4, 2);
173 6ebf5905 Alexander Graf
    register_ioport_simple(&s->data_handler, ioport, 4, 4);
174 c646f74f Gleb Natapov
    sysbus_init_ioports(&s->busdev, ioport, 4);
175 4f5e19e6 Isaku Yamahata
}