Statistics
| Branch: | Revision:

root / hw / pci_host.c @ 366c9332

History | View | Annotate | Download (4.9 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 ce195fb5 Isaku Yamahata
void pci_data_write(PCIBus *s, uint32_t addr, uint32_t val, int len)
51 766347cc Isaku Yamahata
{
52 8d6514f8 Isaku Yamahata
    PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
53 7ac901cd Isaku Yamahata
    uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
54 766347cc Isaku Yamahata
55 766347cc Isaku Yamahata
    if (!pci_dev)
56 766347cc Isaku Yamahata
        return;
57 766347cc Isaku Yamahata
58 0b987f19 Blue Swirl
    PCI_DPRINTF("%s: %s: addr=%02" PRIx32 " val=%08" PRIx32 " len=%d\n",
59 766347cc Isaku Yamahata
                __func__, pci_dev->name, config_addr, val, len);
60 766347cc Isaku Yamahata
    pci_dev->config_write(pci_dev, config_addr, val, len);
61 766347cc Isaku Yamahata
}
62 766347cc Isaku Yamahata
63 ce195fb5 Isaku Yamahata
uint32_t pci_data_read(PCIBus *s, uint32_t addr, int len)
64 766347cc Isaku Yamahata
{
65 8d6514f8 Isaku Yamahata
    PCIDevice *pci_dev = pci_dev_find_by_addr(s, addr);
66 7ac901cd Isaku Yamahata
    uint32_t config_addr = addr & (PCI_CONFIG_SPACE_SIZE - 1);
67 766347cc Isaku Yamahata
    uint32_t val;
68 766347cc Isaku Yamahata
69 4677d8ed Michael S. Tsirkin
    assert(len == 1 || len == 2 || len == 4);
70 766347cc Isaku Yamahata
    if (!pci_dev) {
71 4677d8ed Michael S. Tsirkin
        return ~0x0;
72 766347cc Isaku Yamahata
    }
73 766347cc Isaku Yamahata
74 4677d8ed Michael S. Tsirkin
    val = pci_dev->config_read(pci_dev, config_addr, len);
75 4677d8ed Michael S. Tsirkin
    PCI_DPRINTF("%s: %s: addr=%02"PRIx32" val=%08"PRIx32" len=%d\n",
76 4677d8ed Michael S. Tsirkin
                __func__, pci_dev->name, config_addr, val, len);
77 4677d8ed Michael S. Tsirkin
78 766347cc Isaku Yamahata
    return val;
79 766347cc Isaku Yamahata
}
80 766347cc Isaku Yamahata
81 6ebf5905 Alexander Graf
static void pci_host_config_write(ReadWriteHandler *handler,
82 6ebf5905 Alexander Graf
                                  pcibus_t addr, uint32_t val, int len)
83 a455783b Isaku Yamahata
{
84 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, conf_handler);
85 a455783b Isaku Yamahata
86 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("%s addr %" FMT_PCIBUS " %d val %"PRIx32"\n",
87 9f6f0423 Michael S. Tsirkin
                __func__, addr, len, val);
88 a455783b Isaku Yamahata
    s->config_reg = val;
89 a455783b Isaku Yamahata
}
90 a455783b Isaku Yamahata
91 6ebf5905 Alexander Graf
static uint32_t pci_host_config_read(ReadWriteHandler *handler,
92 6ebf5905 Alexander Graf
                                     pcibus_t addr, int len)
93 a455783b Isaku Yamahata
{
94 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, conf_handler);
95 a455783b Isaku Yamahata
    uint32_t val = s->config_reg;
96 952760bb Blue Swirl
97 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("%s addr %" FMT_PCIBUS " len %d val %"PRIx32"\n",
98 9f6f0423 Michael S. Tsirkin
                __func__, addr, len, val);
99 a455783b Isaku Yamahata
    return val;
100 a455783b Isaku Yamahata
}
101 a455783b Isaku Yamahata
102 6ebf5905 Alexander Graf
static void pci_host_data_write(ReadWriteHandler *handler,
103 6ebf5905 Alexander Graf
                                pcibus_t addr, uint32_t val, int len)
104 a455783b Isaku Yamahata
{
105 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, data_handler);
106 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("write addr %" FMT_PCIBUS " len %d val %x\n",
107 9f6f0423 Michael S. Tsirkin
                addr, len, val);
108 9f6f0423 Michael S. Tsirkin
    if (s->config_reg & (1u << 31))
109 9f6f0423 Michael S. Tsirkin
        pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
110 a455783b Isaku Yamahata
}
111 a455783b Isaku Yamahata
112 6ebf5905 Alexander Graf
static uint32_t pci_host_data_read(ReadWriteHandler *handler,
113 6ebf5905 Alexander Graf
                                   pcibus_t addr, int len)
114 a455783b Isaku Yamahata
{
115 9f6f0423 Michael S. Tsirkin
    PCIHostState *s = container_of(handler, PCIHostState, data_handler);
116 9f6f0423 Michael S. Tsirkin
    uint32_t val;
117 9f6f0423 Michael S. Tsirkin
    if (!(s->config_reg & (1 << 31)))
118 9f6f0423 Michael S. Tsirkin
        return 0xffffffff;
119 9f6f0423 Michael S. Tsirkin
    val = pci_data_read(s->bus, s->config_reg | (addr & 3), len);
120 9f6f0423 Michael S. Tsirkin
    PCI_DPRINTF("read addr %" FMT_PCIBUS " len %d val %x\n",
121 9f6f0423 Michael S. Tsirkin
                addr, len, val);
122 952760bb Blue Swirl
    return val;
123 9f6f0423 Michael S. Tsirkin
}
124 a455783b Isaku Yamahata
125 952760bb Blue Swirl
static void pci_host_init(PCIHostState *s)
126 952760bb Blue Swirl
{
127 6ebf5905 Alexander Graf
    s->conf_handler.write = pci_host_config_write;
128 6ebf5905 Alexander Graf
    s->conf_handler.read = pci_host_config_read;
129 6ebf5905 Alexander Graf
    s->data_handler.write = pci_host_data_write;
130 6ebf5905 Alexander Graf
    s->data_handler.read = pci_host_data_read;
131 952760bb Blue Swirl
}
132 952760bb Blue Swirl
133 6ebf5905 Alexander Graf
int pci_host_conf_register_mmio(PCIHostState *s, int endian)
134 9f6f0423 Michael S. Tsirkin
{
135 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
136 6ebf5905 Alexander Graf
    return cpu_register_io_memory_simple(&s->conf_handler, endian);
137 a455783b Isaku Yamahata
}
138 a455783b Isaku Yamahata
139 f08b32fe Isaku Yamahata
void pci_host_conf_register_ioport(pio_addr_t ioport, PCIHostState *s)
140 a455783b Isaku Yamahata
{
141 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
142 6ebf5905 Alexander Graf
    register_ioport_simple(&s->conf_handler, ioport, 4, 4);
143 c646f74f Gleb Natapov
    sysbus_init_ioports(&s->busdev, ioport, 4);
144 a455783b Isaku Yamahata
}
145 a455783b Isaku Yamahata
146 6ebf5905 Alexander Graf
int pci_host_data_register_mmio(PCIHostState *s, int endian)
147 4f5e19e6 Isaku Yamahata
{
148 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
149 6ebf5905 Alexander Graf
    return cpu_register_io_memory_simple(&s->data_handler, endian);
150 4f5e19e6 Isaku Yamahata
}
151 4f5e19e6 Isaku Yamahata
152 4f5e19e6 Isaku Yamahata
void pci_host_data_register_ioport(pio_addr_t ioport, PCIHostState *s)
153 4f5e19e6 Isaku Yamahata
{
154 9f6f0423 Michael S. Tsirkin
    pci_host_init(s);
155 6ebf5905 Alexander Graf
    register_ioport_simple(&s->data_handler, ioport, 4, 1);
156 6ebf5905 Alexander Graf
    register_ioport_simple(&s->data_handler, ioport, 4, 2);
157 6ebf5905 Alexander Graf
    register_ioport_simple(&s->data_handler, ioport, 4, 4);
158 c646f74f Gleb Natapov
    sysbus_init_ioports(&s->busdev, ioport, 4);
159 4f5e19e6 Isaku Yamahata
}