root / hw / pci_host.c @ 2be24aaa
History | View | Annotate | Download (7.1 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 | 766347cc | Isaku Yamahata | return pci_find_device(bus, bus_num, PCI_SLOT(devfn), PCI_FUNC(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 | 952760bb | Blue Swirl | static void pci_host_config_write_swap(ReadWriteHandler *handler, |
82 | 952760bb | Blue Swirl | 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 | 9f6f0423 | Michael S. Tsirkin | val = qemu_bswap_len(val, len); |
89 | a455783b | Isaku Yamahata | s->config_reg = val; |
90 | a455783b | Isaku Yamahata | } |
91 | a455783b | Isaku Yamahata | |
92 | 952760bb | Blue Swirl | static uint32_t pci_host_config_read_swap(ReadWriteHandler *handler,
|
93 | 952760bb | Blue Swirl | pcibus_t addr, int len)
|
94 | a455783b | Isaku Yamahata | { |
95 | 9f6f0423 | Michael S. Tsirkin | PCIHostState *s = container_of(handler, PCIHostState, conf_handler); |
96 | a455783b | Isaku Yamahata | uint32_t val = s->config_reg; |
97 | 952760bb | Blue Swirl | |
98 | 9f6f0423 | Michael S. Tsirkin | val = qemu_bswap_len(val, len); |
99 | 9f6f0423 | Michael S. Tsirkin | PCI_DPRINTF("%s addr %" FMT_PCIBUS " len %d val %"PRIx32"\n", |
100 | 9f6f0423 | Michael S. Tsirkin | __func__, addr, len, val); |
101 | a455783b | Isaku Yamahata | return val;
|
102 | a455783b | Isaku Yamahata | } |
103 | a455783b | Isaku Yamahata | |
104 | 9f6f0423 | Michael S. Tsirkin | static void pci_host_config_write_noswap(ReadWriteHandler *handler, |
105 | 9f6f0423 | Michael S. Tsirkin | pcibus_t addr, uint32_t val, int len)
|
106 | a455783b | Isaku Yamahata | { |
107 | 9f6f0423 | Michael S. Tsirkin | PCIHostState *s = container_of(handler, PCIHostState, conf_noswap_handler); |
108 | a455783b | Isaku Yamahata | |
109 | 9f6f0423 | Michael S. Tsirkin | PCI_DPRINTF("%s addr %" FMT_PCIBUS " %d val %"PRIx32"\n", |
110 | 9f6f0423 | Michael S. Tsirkin | __func__, addr, len, val); |
111 | a455783b | Isaku Yamahata | s->config_reg = val; |
112 | a455783b | Isaku Yamahata | } |
113 | a455783b | Isaku Yamahata | |
114 | 9f6f0423 | Michael S. Tsirkin | static uint32_t pci_host_config_read_noswap(ReadWriteHandler *handler,
|
115 | 9f6f0423 | Michael S. Tsirkin | pcibus_t addr, int len)
|
116 | a455783b | Isaku Yamahata | { |
117 | 9f6f0423 | Michael S. Tsirkin | PCIHostState *s = container_of(handler, PCIHostState, conf_noswap_handler); |
118 | a455783b | Isaku Yamahata | uint32_t val = s->config_reg; |
119 | a455783b | Isaku Yamahata | |
120 | 9f6f0423 | Michael S. Tsirkin | PCI_DPRINTF("%s addr %" FMT_PCIBUS " len %d val %"PRIx32"\n", |
121 | 9f6f0423 | Michael S. Tsirkin | __func__, addr, len, val); |
122 | a455783b | Isaku Yamahata | return val;
|
123 | a455783b | Isaku Yamahata | } |
124 | a455783b | Isaku Yamahata | |
125 | 952760bb | Blue Swirl | static void pci_host_data_write_swap(ReadWriteHandler *handler, |
126 | 952760bb | Blue Swirl | pcibus_t addr, uint32_t val, int len)
|
127 | a455783b | Isaku Yamahata | { |
128 | 9f6f0423 | Michael S. Tsirkin | PCIHostState *s = container_of(handler, PCIHostState, data_handler); |
129 | 952760bb | Blue Swirl | |
130 | 9f6f0423 | Michael S. Tsirkin | val = qemu_bswap_len(val, len); |
131 | 9f6f0423 | Michael S. Tsirkin | PCI_DPRINTF("write addr %" FMT_PCIBUS " len %d val %x\n", |
132 | 9f6f0423 | Michael S. Tsirkin | addr, len, val); |
133 | 9f6f0423 | Michael S. Tsirkin | if (s->config_reg & (1u << 31)) |
134 | 9f6f0423 | Michael S. Tsirkin | pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
|
135 | a455783b | Isaku Yamahata | } |
136 | a455783b | Isaku Yamahata | |
137 | 952760bb | Blue Swirl | static uint32_t pci_host_data_read_swap(ReadWriteHandler *handler,
|
138 | 952760bb | Blue Swirl | pcibus_t addr, int len)
|
139 | a455783b | Isaku Yamahata | { |
140 | 9f6f0423 | Michael S. Tsirkin | PCIHostState *s = container_of(handler, PCIHostState, data_handler); |
141 | 9f6f0423 | Michael S. Tsirkin | uint32_t val; |
142 | 9f6f0423 | Michael S. Tsirkin | if (!(s->config_reg & (1 << 31))) |
143 | 9f6f0423 | Michael S. Tsirkin | return 0xffffffff; |
144 | 9f6f0423 | Michael S. Tsirkin | val = pci_data_read(s->bus, s->config_reg | (addr & 3), len);
|
145 | 9f6f0423 | Michael S. Tsirkin | PCI_DPRINTF("read addr %" FMT_PCIBUS " len %d val %x\n", |
146 | 9f6f0423 | Michael S. Tsirkin | addr, len, val); |
147 | 9f6f0423 | Michael S. Tsirkin | val = qemu_bswap_len(val, len); |
148 | 9f6f0423 | Michael S. Tsirkin | return val;
|
149 | 9f6f0423 | Michael S. Tsirkin | } |
150 | a455783b | Isaku Yamahata | |
151 | 952760bb | Blue Swirl | static void pci_host_data_write_noswap(ReadWriteHandler *handler, |
152 | 952760bb | Blue Swirl | pcibus_t addr, uint32_t val, int len)
|
153 | 9f6f0423 | Michael S. Tsirkin | { |
154 | 4dcf7d87 | Aurelien Jarno | PCIHostState *s = container_of(handler, PCIHostState, data_noswap_handler); |
155 | 952760bb | Blue Swirl | PCI_DPRINTF("write addr %" FMT_PCIBUS " len %d val %x\n", |
156 | 952760bb | Blue Swirl | addr, len, val); |
157 | 952760bb | Blue Swirl | if (s->config_reg & (1u << 31)) |
158 | 952760bb | Blue Swirl | pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
|
159 | a455783b | Isaku Yamahata | } |
160 | a455783b | Isaku Yamahata | |
161 | 952760bb | Blue Swirl | static uint32_t pci_host_data_read_noswap(ReadWriteHandler *handler,
|
162 | 952760bb | Blue Swirl | pcibus_t addr, int len)
|
163 | a455783b | Isaku Yamahata | { |
164 | 4dcf7d87 | Aurelien Jarno | PCIHostState *s = container_of(handler, PCIHostState, data_noswap_handler); |
165 | 952760bb | Blue Swirl | uint32_t val; |
166 | 952760bb | Blue Swirl | if (!(s->config_reg & (1 << 31))) |
167 | 952760bb | Blue Swirl | return 0xffffffff; |
168 | 952760bb | Blue Swirl | val = pci_data_read(s->bus, s->config_reg | (addr & 3), len);
|
169 | 952760bb | Blue Swirl | PCI_DPRINTF("read addr %" FMT_PCIBUS " len %d val %x\n", |
170 | 952760bb | Blue Swirl | addr, len, val); |
171 | 952760bb | Blue Swirl | return val;
|
172 | 9f6f0423 | Michael S. Tsirkin | } |
173 | a455783b | Isaku Yamahata | |
174 | 952760bb | Blue Swirl | static void pci_host_init(PCIHostState *s) |
175 | 952760bb | Blue Swirl | { |
176 | 952760bb | Blue Swirl | s->conf_handler.write = pci_host_config_write_swap; |
177 | 952760bb | Blue Swirl | s->conf_handler.read = pci_host_config_read_swap; |
178 | 952760bb | Blue Swirl | s->conf_noswap_handler.write = pci_host_config_write_noswap; |
179 | 952760bb | Blue Swirl | s->conf_noswap_handler.read = pci_host_config_read_noswap; |
180 | 952760bb | Blue Swirl | s->data_handler.write = pci_host_data_write_swap; |
181 | 952760bb | Blue Swirl | s->data_handler.read = pci_host_data_read_swap; |
182 | 952760bb | Blue Swirl | s->data_noswap_handler.write = pci_host_data_write_noswap; |
183 | 952760bb | Blue Swirl | s->data_noswap_handler.read = pci_host_data_read_noswap; |
184 | 952760bb | Blue Swirl | } |
185 | 952760bb | Blue Swirl | |
186 | 952760bb | Blue Swirl | int pci_host_conf_register_mmio(PCIHostState *s, int swap) |
187 | 9f6f0423 | Michael S. Tsirkin | { |
188 | 9f6f0423 | Michael S. Tsirkin | pci_host_init(s); |
189 | 952760bb | Blue Swirl | if (swap) {
|
190 | 952760bb | Blue Swirl | return cpu_register_io_memory_simple(&s->conf_handler);
|
191 | 952760bb | Blue Swirl | } else {
|
192 | 952760bb | Blue Swirl | return cpu_register_io_memory_simple(&s->conf_noswap_handler);
|
193 | 952760bb | Blue Swirl | } |
194 | a455783b | Isaku Yamahata | } |
195 | a455783b | Isaku Yamahata | |
196 | f08b32fe | Isaku Yamahata | void pci_host_conf_register_ioport(pio_addr_t ioport, PCIHostState *s)
|
197 | a455783b | Isaku Yamahata | { |
198 | 9f6f0423 | Michael S. Tsirkin | pci_host_init(s); |
199 | 9f6f0423 | Michael S. Tsirkin | register_ioport_simple(&s->conf_noswap_handler, ioport, 4, 4); |
200 | a455783b | Isaku Yamahata | } |
201 | a455783b | Isaku Yamahata | |
202 | 952760bb | Blue Swirl | int pci_host_data_register_mmio(PCIHostState *s, int swap) |
203 | 4f5e19e6 | Isaku Yamahata | { |
204 | 9f6f0423 | Michael S. Tsirkin | pci_host_init(s); |
205 | 952760bb | Blue Swirl | if (swap) {
|
206 | 952760bb | Blue Swirl | return cpu_register_io_memory_simple(&s->data_handler);
|
207 | 952760bb | Blue Swirl | } else {
|
208 | 952760bb | Blue Swirl | return cpu_register_io_memory_simple(&s->data_noswap_handler);
|
209 | 952760bb | Blue Swirl | } |
210 | 4f5e19e6 | Isaku Yamahata | } |
211 | 4f5e19e6 | Isaku Yamahata | |
212 | 4f5e19e6 | Isaku Yamahata | void pci_host_data_register_ioport(pio_addr_t ioport, PCIHostState *s)
|
213 | 4f5e19e6 | Isaku Yamahata | { |
214 | 9f6f0423 | Michael S. Tsirkin | pci_host_init(s); |
215 | 4dcf7d87 | Aurelien Jarno | register_ioport_simple(&s->data_noswap_handler, ioport, 4, 1); |
216 | 4dcf7d87 | Aurelien Jarno | register_ioport_simple(&s->data_noswap_handler, ioport, 4, 2); |
217 | 4dcf7d87 | Aurelien Jarno | register_ioport_simple(&s->data_noswap_handler, ioport, 4, 4); |
218 | 4f5e19e6 | Isaku Yamahata | } |