4 * Copyright (c) 2009 Isaku Yamahata <yamahata at valinux co jp>
5 * VA Linux Systems Japan K.K.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #define PCI_DPRINTF(fmt, ...) \
30 do { printf("pci_host_data: " fmt , ## __VA_ARGS__); } while (0)
32 #define PCI_DPRINTF(fmt, ...)
37 * bit 16 - 24: bus number
38 * bit 8 - 15: devfun number
39 * bit 0 - 7: offset in configuration space of a given pci device
42 /* the helper functio to get a PCIDeice* for a given pci address */
43 static inline PCIDevice
*pci_addr_to_dev(PCIBus
*bus
, uint32_t addr
)
45 uint8_t bus_num
= (addr
>> 16) & 0xff;
46 uint8_t devfn
= (addr
>> 8) & 0xff;
47 return pci_find_device(bus
, bus_num
, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
50 static inline uint32_t pci_addr_to_config(uint32_t addr
)
52 return addr
& (PCI_CONFIG_SPACE_SIZE
- 1);
55 void pci_data_write(PCIBus
*s
, uint32_t addr
, uint32_t val
, int len
)
57 PCIDevice
*pci_dev
= pci_addr_to_dev(s
, addr
);
58 uint32_t config_addr
= pci_addr_to_config(addr
);
63 PCI_DPRINTF("%s: %s: addr=%02"PRIx32
" val=%08"PRI32x
" len=%d\n",
64 __func__
, pci_dev
->name
, config_addr
, val
, len
);
65 pci_dev
->config_write(pci_dev
, config_addr
, val
, len
);
68 uint32_t pci_data_read(PCIBus
*s
, uint32_t addr
, int len
)
70 PCIDevice
*pci_dev
= pci_addr_to_dev(s
, addr
);
71 uint32_t config_addr
= pci_addr_to_config(addr
);
88 val
= pci_dev
->config_read(pci_dev
, config_addr
, len
);
89 PCI_DPRINTF("%s: %s: addr=%02"PRIx32
" val=%08"PRIx32
" len=%d\n",
90 __func__
, pci_dev
->name
, config_addr
, val
, len
);
96 static void pci_host_config_writel(void *opaque
, target_phys_addr_t addr
,
99 PCIHostState
*s
= opaque
;
101 #ifdef TARGET_WORDS_BIGENDIAN
104 PCI_DPRINTF("%s addr " TARGET_FMT_plx
" val %"PRIx32
"\n",
105 __func__
, addr
, val
);
109 static uint32_t pci_host_config_readl(void *opaque
, target_phys_addr_t addr
)
111 PCIHostState
*s
= opaque
;
112 uint32_t val
= s
->config_reg
;
114 #ifdef TARGET_WORDS_BIGENDIAN
117 PCI_DPRINTF("%s addr " TARGET_FMT_plx
" val %"PRIx32
"\n",
118 __func__
, addr
, val
);
122 static CPUWriteMemoryFunc
* const pci_host_config_write
[] = {
123 &pci_host_config_writel
,
124 &pci_host_config_writel
,
125 &pci_host_config_writel
,
128 static CPUReadMemoryFunc
* const pci_host_config_read
[] = {
129 &pci_host_config_readl
,
130 &pci_host_config_readl
,
131 &pci_host_config_readl
,
134 int pci_host_config_register_io_memory(PCIHostState
*s
)
136 return cpu_register_io_memory(pci_host_config_read
,
137 pci_host_config_write
, s
);
140 static void pci_host_config_writel_noswap(void *opaque
,
141 target_phys_addr_t addr
,
144 PCIHostState
*s
= opaque
;
146 PCI_DPRINTF("%s addr " TARGET_FMT_plx
" val %"PRIx32
"\n",
147 __func__
, addr
, val
);
151 static uint32_t pci_host_config_readl_noswap(void *opaque
,
152 target_phys_addr_t addr
)
154 PCIHostState
*s
= opaque
;
155 uint32_t val
= s
->config_reg
;
157 PCI_DPRINTF("%s addr " TARGET_FMT_plx
" val %"PRIx32
"\n",
158 __func__
, addr
, val
);
162 static CPUWriteMemoryFunc
* const pci_host_config_write_noswap
[] = {
163 &pci_host_config_writel_noswap
,
164 &pci_host_config_writel_noswap
,
165 &pci_host_config_writel_noswap
,
168 static CPUReadMemoryFunc
* const pci_host_config_read_noswap
[] = {
169 &pci_host_config_readl_noswap
,
170 &pci_host_config_readl_noswap
,
171 &pci_host_config_readl_noswap
,
174 int pci_host_config_register_io_memory_noswap(PCIHostState
*s
)
176 return cpu_register_io_memory(pci_host_config_read_noswap
,
177 pci_host_config_write_noswap
, s
);
180 static void pci_host_config_writel_ioport(void *opaque
,
181 uint32_t addr
, uint32_t val
)
183 PCIHostState
*s
= opaque
;
185 PCI_DPRINTF("%s addr %"PRIx32
" val %"PRIx32
"\n", __func__
, addr
, val
);
189 static uint32_t pci_host_config_readl_ioport(void *opaque
, uint32_t addr
)
191 PCIHostState
*s
= opaque
;
192 uint32_t val
= s
->config_reg
;
194 PCI_DPRINTF("%s addr %"PRIx32
" val %"PRIx32
"\n", __func__
, addr
, val
);
198 void pci_host_config_register_ioport(pio_addr_t ioport
, PCIHostState
*s
)
200 register_ioport_write(ioport
, 4, 4, pci_host_config_writel_ioport
, s
);
201 register_ioport_read(ioport
, 4, 4, pci_host_config_readl_ioport
, s
);
204 #define PCI_ADDR_T target_phys_addr_t
205 #define PCI_HOST_SUFFIX _mmio
207 #include "pci_host_template.h"
209 static CPUWriteMemoryFunc
* const pci_host_data_write_mmio
[] = {
210 pci_host_data_writeb_mmio
,
211 pci_host_data_writew_mmio
,
212 pci_host_data_writel_mmio
,
215 static CPUReadMemoryFunc
* const pci_host_data_read_mmio
[] = {
216 pci_host_data_readb_mmio
,
217 pci_host_data_readw_mmio
,
218 pci_host_data_readl_mmio
,
221 int pci_host_data_register_io_memory(PCIHostState
*s
)
223 return cpu_register_io_memory(pci_host_data_read_mmio
,
224 pci_host_data_write_mmio
,
229 #undef PCI_HOST_SUFFIX
231 #define PCI_ADDR_T uint32_t
232 #define PCI_HOST_SUFFIX _ioport
234 #include "pci_host_template.h"
236 void pci_host_data_register_ioport(pio_addr_t ioport
, PCIHostState
*s
)
238 register_ioport_write(ioport
, 4, 1, pci_host_data_writeb_ioport
, s
);
239 register_ioport_write(ioport
, 4, 2, pci_host_data_writew_ioport
, s
);
240 register_ioport_write(ioport
, 4, 4, pci_host_data_writel_ioport
, s
);
241 register_ioport_read(ioport
, 4, 1, pci_host_data_readb_ioport
, s
);
242 register_ioport_read(ioport
, 4, 2, pci_host_data_readw_ioport
, s
);
243 register_ioport_read(ioport
, 4, 4, pci_host_data_readl_ioport
, s
);