root / hw / pci_host.c @ 37952117
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 | 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 | d0ed8076 | Avi Kivity | static void pci_host_config_write(void *opaque, target_phys_addr_t addr, |
98 | d0ed8076 | Avi Kivity | uint64_t val, unsigned len)
|
99 | a455783b | Isaku Yamahata | { |
100 | d0ed8076 | Avi Kivity | PCIHostState *s = opaque; |
101 | a455783b | Isaku Yamahata | |
102 | d0ed8076 | Avi Kivity | PCI_DPRINTF("%s addr " TARGET_FMT_plx " len %d val %"PRIx64"\n", |
103 | 9f6f0423 | Michael S. Tsirkin | __func__, addr, len, val); |
104 | cdde6ffc | Avi Kivity | if (addr != 0 || len != 4) { |
105 | cdde6ffc | Avi Kivity | return;
|
106 | cdde6ffc | Avi Kivity | } |
107 | a455783b | Isaku Yamahata | s->config_reg = val; |
108 | a455783b | Isaku Yamahata | } |
109 | a455783b | Isaku Yamahata | |
110 | d0ed8076 | Avi Kivity | static uint64_t pci_host_config_read(void *opaque, target_phys_addr_t addr, |
111 | d0ed8076 | Avi Kivity | unsigned len)
|
112 | a455783b | Isaku Yamahata | { |
113 | d0ed8076 | Avi Kivity | PCIHostState *s = opaque; |
114 | a455783b | Isaku Yamahata | uint32_t val = s->config_reg; |
115 | 952760bb | Blue Swirl | |
116 | d0ed8076 | Avi Kivity | PCI_DPRINTF("%s addr " TARGET_FMT_plx " len %d val %"PRIx32"\n", |
117 | 9f6f0423 | Michael S. Tsirkin | __func__, addr, len, val); |
118 | a455783b | Isaku Yamahata | return val;
|
119 | a455783b | Isaku Yamahata | } |
120 | a455783b | Isaku Yamahata | |
121 | d0ed8076 | Avi Kivity | static void pci_host_data_write(void *opaque, target_phys_addr_t addr, |
122 | d0ed8076 | Avi Kivity | uint64_t val, unsigned len)
|
123 | a455783b | Isaku Yamahata | { |
124 | d0ed8076 | Avi Kivity | PCIHostState *s = opaque; |
125 | d0ed8076 | Avi Kivity | PCI_DPRINTF("write addr " TARGET_FMT_plx " len %d val %x\n", |
126 | d0ed8076 | Avi Kivity | addr, len, (unsigned)val);
|
127 | 9f6f0423 | Michael S. Tsirkin | if (s->config_reg & (1u << 31)) |
128 | 9f6f0423 | Michael S. Tsirkin | pci_data_write(s->bus, s->config_reg | (addr & 3), val, len);
|
129 | a455783b | Isaku Yamahata | } |
130 | a455783b | Isaku Yamahata | |
131 | d0ed8076 | Avi Kivity | static uint64_t pci_host_data_read(void *opaque, |
132 | d0ed8076 | Avi Kivity | target_phys_addr_t addr, unsigned len)
|
133 | a455783b | Isaku Yamahata | { |
134 | d0ed8076 | Avi Kivity | PCIHostState *s = opaque; |
135 | 9f6f0423 | Michael S. Tsirkin | uint32_t val; |
136 | 9f6f0423 | Michael S. Tsirkin | if (!(s->config_reg & (1 << 31))) |
137 | 9f6f0423 | Michael S. Tsirkin | return 0xffffffff; |
138 | 9f6f0423 | Michael S. Tsirkin | val = pci_data_read(s->bus, s->config_reg | (addr & 3), len);
|
139 | d0ed8076 | Avi Kivity | PCI_DPRINTF("read addr " TARGET_FMT_plx " len %d val %x\n", |
140 | 9f6f0423 | Michael S. Tsirkin | addr, len, val); |
141 | 952760bb | Blue Swirl | return val;
|
142 | 9f6f0423 | Michael S. Tsirkin | } |
143 | a455783b | Isaku Yamahata | |
144 | d0ed8076 | Avi Kivity | const MemoryRegionOps pci_host_conf_le_ops = {
|
145 | d0ed8076 | Avi Kivity | .read = pci_host_config_read, |
146 | d0ed8076 | Avi Kivity | .write = pci_host_config_write, |
147 | d0ed8076 | Avi Kivity | .endianness = DEVICE_LITTLE_ENDIAN, |
148 | d0ed8076 | Avi Kivity | }; |
149 | a455783b | Isaku Yamahata | |
150 | d0ed8076 | Avi Kivity | const MemoryRegionOps pci_host_conf_be_ops = {
|
151 | d0ed8076 | Avi Kivity | .read = pci_host_config_read, |
152 | d0ed8076 | Avi Kivity | .write = pci_host_config_write, |
153 | d0ed8076 | Avi Kivity | .endianness = DEVICE_BIG_ENDIAN, |
154 | d0ed8076 | Avi Kivity | }; |
155 | d2c33733 | Avi Kivity | |
156 | d0ed8076 | Avi Kivity | const MemoryRegionOps pci_host_data_le_ops = {
|
157 | d0ed8076 | Avi Kivity | .read = pci_host_data_read, |
158 | d0ed8076 | Avi Kivity | .write = pci_host_data_write, |
159 | d0ed8076 | Avi Kivity | .endianness = DEVICE_LITTLE_ENDIAN, |
160 | d0ed8076 | Avi Kivity | }; |
161 | d0ed8076 | Avi Kivity | |
162 | d0ed8076 | Avi Kivity | const MemoryRegionOps pci_host_data_be_ops = {
|
163 | d0ed8076 | Avi Kivity | .read = pci_host_data_read, |
164 | d0ed8076 | Avi Kivity | .write = pci_host_data_write, |
165 | d0ed8076 | Avi Kivity | .endianness = DEVICE_BIG_ENDIAN, |
166 | d0ed8076 | Avi Kivity | }; |
167 | a455783b | Isaku Yamahata |