root / hw / pci / pcie_host.c @ 5d87e4b7
History | View | Annotate | Download (5.1 kB)
1 | a9f49946 | Isaku Yamahata | /*
|
---|---|---|---|
2 | a9f49946 | Isaku Yamahata | * pcie_host.c
|
3 | a9f49946 | Isaku Yamahata | * utility functions for pci express host bridge.
|
4 | a9f49946 | Isaku Yamahata | *
|
5 | a9f49946 | Isaku Yamahata | * Copyright (c) 2009 Isaku Yamahata <yamahata at valinux co jp>
|
6 | a9f49946 | Isaku Yamahata | * VA Linux Systems Japan K.K.
|
7 | a9f49946 | Isaku Yamahata | *
|
8 | a9f49946 | Isaku Yamahata | * This program is free software; you can redistribute it and/or modify
|
9 | a9f49946 | Isaku Yamahata | * it under the terms of the GNU General Public License as published by
|
10 | a9f49946 | Isaku Yamahata | * the Free Software Foundation; either version 2 of the License, or
|
11 | a9f49946 | Isaku Yamahata | * (at your option) any later version.
|
12 | a9f49946 | Isaku Yamahata | |
13 | a9f49946 | Isaku Yamahata | * This program is distributed in the hope that it will be useful,
|
14 | a9f49946 | Isaku Yamahata | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 | a9f49946 | Isaku Yamahata | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 | a9f49946 | Isaku Yamahata | * GNU General Public License for more details.
|
17 | a9f49946 | Isaku Yamahata | |
18 | a9f49946 | Isaku Yamahata | * You should have received a copy of the GNU General Public License along
|
19 | 70539e18 | Blue Swirl | * with this program; if not, see <http://www.gnu.org/licenses/>.
|
20 | a9f49946 | Isaku Yamahata | */
|
21 | a9f49946 | Isaku Yamahata | |
22 | c759b24f | Michael S. Tsirkin | #include "hw/hw.h" |
23 | c759b24f | Michael S. Tsirkin | #include "hw/pci/pci.h" |
24 | c759b24f | Michael S. Tsirkin | #include "hw/pci/pcie_host.h" |
25 | 022c62cb | Paolo Bonzini | #include "exec/address-spaces.h" |
26 | a9f49946 | Isaku Yamahata | |
27 | a9f49946 | Isaku Yamahata | /*
|
28 | a9f49946 | Isaku Yamahata | * PCI express mmcfig address
|
29 | a9f49946 | Isaku Yamahata | * bit 20 - 28: bus number
|
30 | a9f49946 | Isaku Yamahata | * bit 15 - 19: device number
|
31 | a9f49946 | Isaku Yamahata | * bit 12 - 14: function number
|
32 | a9f49946 | Isaku Yamahata | * bit 0 - 11: offset in configuration space of a given device
|
33 | a9f49946 | Isaku Yamahata | */
|
34 | a9f49946 | Isaku Yamahata | #define PCIE_MMCFG_SIZE_MAX (1ULL << 28) |
35 | a9f49946 | Isaku Yamahata | #define PCIE_MMCFG_SIZE_MIN (1ULL << 20) |
36 | a9f49946 | Isaku Yamahata | #define PCIE_MMCFG_BUS_BIT 20 |
37 | a9f49946 | Isaku Yamahata | #define PCIE_MMCFG_BUS_MASK 0x1ff |
38 | a9f49946 | Isaku Yamahata | #define PCIE_MMCFG_DEVFN_BIT 12 |
39 | a9f49946 | Isaku Yamahata | #define PCIE_MMCFG_DEVFN_MASK 0xff |
40 | a9f49946 | Isaku Yamahata | #define PCIE_MMCFG_CONFOFFSET_MASK 0xfff |
41 | a9f49946 | Isaku Yamahata | #define PCIE_MMCFG_BUS(addr) (((addr) >> PCIE_MMCFG_BUS_BIT) & \
|
42 | a9f49946 | Isaku Yamahata | PCIE_MMCFG_BUS_MASK) |
43 | a9f49946 | Isaku Yamahata | #define PCIE_MMCFG_DEVFN(addr) (((addr) >> PCIE_MMCFG_DEVFN_BIT) & \
|
44 | a9f49946 | Isaku Yamahata | PCIE_MMCFG_DEVFN_MASK) |
45 | a9f49946 | Isaku Yamahata | #define PCIE_MMCFG_CONFOFFSET(addr) ((addr) & PCIE_MMCFG_CONFOFFSET_MASK)
|
46 | a9f49946 | Isaku Yamahata | |
47 | a9f49946 | Isaku Yamahata | |
48 | a9f49946 | Isaku Yamahata | /* a helper function to get a PCIDevice for a given mmconfig address */
|
49 | 8d6514f8 | Isaku Yamahata | static inline PCIDevice *pcie_dev_find_by_mmcfg_addr(PCIBus *s, |
50 | 8d6514f8 | Isaku Yamahata | uint32_t mmcfg_addr) |
51 | a9f49946 | Isaku Yamahata | { |
52 | a9f49946 | Isaku Yamahata | return pci_find_device(s, PCIE_MMCFG_BUS(mmcfg_addr),
|
53 | 5256d8bf | Isaku Yamahata | PCIE_MMCFG_DEVFN(mmcfg_addr)); |
54 | a9f49946 | Isaku Yamahata | } |
55 | a9f49946 | Isaku Yamahata | |
56 | a8170e5e | Avi Kivity | static void pcie_mmcfg_data_write(void *opaque, hwaddr mmcfg_addr, |
57 | c76f990e | Avi Kivity | uint64_t val, unsigned len)
|
58 | a9f49946 | Isaku Yamahata | { |
59 | c76f990e | Avi Kivity | PCIExpressHost *e = opaque; |
60 | c76f990e | Avi Kivity | PCIBus *s = e->pci.bus; |
61 | 8d6514f8 | Isaku Yamahata | PCIDevice *pci_dev = pcie_dev_find_by_mmcfg_addr(s, mmcfg_addr); |
62 | 43e86c8f | Isaku Yamahata | uint32_t addr; |
63 | 43e86c8f | Isaku Yamahata | uint32_t limit; |
64 | a9f49946 | Isaku Yamahata | |
65 | 42e4126b | Jan Kiszka | if (!pci_dev) {
|
66 | a9f49946 | Isaku Yamahata | return;
|
67 | 42e4126b | Jan Kiszka | } |
68 | 43e86c8f | Isaku Yamahata | addr = PCIE_MMCFG_CONFOFFSET(mmcfg_addr); |
69 | 43e86c8f | Isaku Yamahata | limit = pci_config_size(pci_dev); |
70 | 43e86c8f | Isaku Yamahata | if (limit <= addr) {
|
71 | 43e86c8f | Isaku Yamahata | /* conventional pci device can be behind pcie-to-pci bridge.
|
72 | 43e86c8f | Isaku Yamahata | 256 <= addr < 4K has no effects. */
|
73 | 43e86c8f | Isaku Yamahata | return;
|
74 | 43e86c8f | Isaku Yamahata | } |
75 | 43e86c8f | Isaku Yamahata | pci_host_config_write_common(pci_dev, addr, limit, val, len); |
76 | a9f49946 | Isaku Yamahata | } |
77 | a9f49946 | Isaku Yamahata | |
78 | c76f990e | Avi Kivity | static uint64_t pcie_mmcfg_data_read(void *opaque, |
79 | a8170e5e | Avi Kivity | hwaddr mmcfg_addr, |
80 | c76f990e | Avi Kivity | unsigned len)
|
81 | a9f49946 | Isaku Yamahata | { |
82 | c76f990e | Avi Kivity | PCIExpressHost *e = opaque; |
83 | c76f990e | Avi Kivity | PCIBus *s = e->pci.bus; |
84 | 43e86c8f | Isaku Yamahata | PCIDevice *pci_dev = pcie_dev_find_by_mmcfg_addr(s, mmcfg_addr); |
85 | 43e86c8f | Isaku Yamahata | uint32_t addr; |
86 | 43e86c8f | Isaku Yamahata | uint32_t limit; |
87 | a9f49946 | Isaku Yamahata | |
88 | a9f49946 | Isaku Yamahata | if (!pci_dev) {
|
89 | 4677d8ed | Michael S. Tsirkin | return ~0x0; |
90 | a9f49946 | Isaku Yamahata | } |
91 | 43e86c8f | Isaku Yamahata | addr = PCIE_MMCFG_CONFOFFSET(mmcfg_addr); |
92 | 43e86c8f | Isaku Yamahata | limit = pci_config_size(pci_dev); |
93 | 43e86c8f | Isaku Yamahata | if (limit <= addr) {
|
94 | 43e86c8f | Isaku Yamahata | /* conventional pci device can be behind pcie-to-pci bridge.
|
95 | 43e86c8f | Isaku Yamahata | 256 <= addr < 4K has no effects. */
|
96 | 43e86c8f | Isaku Yamahata | return ~0x0; |
97 | 43e86c8f | Isaku Yamahata | } |
98 | 43e86c8f | Isaku Yamahata | return pci_host_config_read_common(pci_dev, addr, limit, len);
|
99 | a9f49946 | Isaku Yamahata | } |
100 | a9f49946 | Isaku Yamahata | |
101 | c76f990e | Avi Kivity | static const MemoryRegionOps pcie_mmcfg_ops = { |
102 | c76f990e | Avi Kivity | .read = pcie_mmcfg_data_read, |
103 | c76f990e | Avi Kivity | .write = pcie_mmcfg_data_write, |
104 | c76f990e | Avi Kivity | .endianness = DEVICE_NATIVE_ENDIAN, |
105 | a9f49946 | Isaku Yamahata | }; |
106 | a9f49946 | Isaku Yamahata | |
107 | a9f49946 | Isaku Yamahata | /* pcie_host::base_addr == PCIE_BASE_ADDR_UNMAPPED when it isn't mapped. */
|
108 | a8170e5e | Avi Kivity | #define PCIE_BASE_ADDR_UNMAPPED ((hwaddr)-1ULL) |
109 | a9f49946 | Isaku Yamahata | |
110 | c702ddb8 | Jason Baron | int pcie_host_init(PCIExpressHost *e)
|
111 | a9f49946 | Isaku Yamahata | { |
112 | a9f49946 | Isaku Yamahata | e->base_addr = PCIE_BASE_ADDR_UNMAPPED; |
113 | a9f49946 | Isaku Yamahata | |
114 | a9f49946 | Isaku Yamahata | return 0; |
115 | a9f49946 | Isaku Yamahata | } |
116 | a9f49946 | Isaku Yamahata | |
117 | a9f49946 | Isaku Yamahata | void pcie_host_mmcfg_unmap(PCIExpressHost *e)
|
118 | a9f49946 | Isaku Yamahata | { |
119 | a9f49946 | Isaku Yamahata | if (e->base_addr != PCIE_BASE_ADDR_UNMAPPED) {
|
120 | c76f990e | Avi Kivity | memory_region_del_subregion(get_system_memory(), &e->mmio); |
121 | c702ddb8 | Jason Baron | memory_region_destroy(&e->mmio); |
122 | a9f49946 | Isaku Yamahata | e->base_addr = PCIE_BASE_ADDR_UNMAPPED; |
123 | a9f49946 | Isaku Yamahata | } |
124 | a9f49946 | Isaku Yamahata | } |
125 | a9f49946 | Isaku Yamahata | |
126 | c702ddb8 | Jason Baron | void pcie_host_mmcfg_map(PCIExpressHost *e, hwaddr addr,
|
127 | c702ddb8 | Jason Baron | uint32_t size) |
128 | a9f49946 | Isaku Yamahata | { |
129 | c702ddb8 | Jason Baron | assert(!(size & (size - 1))); /* power of 2 */ |
130 | c702ddb8 | Jason Baron | assert(size >= PCIE_MMCFG_SIZE_MIN); |
131 | c702ddb8 | Jason Baron | assert(size <= PCIE_MMCFG_SIZE_MAX); |
132 | c702ddb8 | Jason Baron | e->size = size; |
133 | 40c5dce9 | Paolo Bonzini | memory_region_init_io(&e->mmio, OBJECT(e), &pcie_mmcfg_ops, e, |
134 | 40c5dce9 | Paolo Bonzini | "pcie-mmcfg", e->size);
|
135 | a9f49946 | Isaku Yamahata | e->base_addr = addr; |
136 | c76f990e | Avi Kivity | memory_region_add_subregion(get_system_memory(), e->base_addr, &e->mmio); |
137 | a9f49946 | Isaku Yamahata | } |
138 | a9f49946 | Isaku Yamahata | |
139 | a9f49946 | Isaku Yamahata | void pcie_host_mmcfg_update(PCIExpressHost *e,
|
140 | a9f49946 | Isaku Yamahata | int enable,
|
141 | c702ddb8 | Jason Baron | hwaddr addr, |
142 | c702ddb8 | Jason Baron | uint32_t size) |
143 | a9f49946 | Isaku Yamahata | { |
144 | a9f49946 | Isaku Yamahata | pcie_host_mmcfg_unmap(e); |
145 | a9f49946 | Isaku Yamahata | if (enable) {
|
146 | c702ddb8 | Jason Baron | pcie_host_mmcfg_map(e, addr, size); |
147 | a9f49946 | Isaku Yamahata | } |
148 | a9f49946 | Isaku Yamahata | } |
149 | bc927e48 | Jason Baron | |
150 | bc927e48 | Jason Baron | static const TypeInfo pcie_host_type_info = { |
151 | bc927e48 | Jason Baron | .name = TYPE_PCIE_HOST_BRIDGE, |
152 | bc927e48 | Jason Baron | .parent = TYPE_PCI_HOST_BRIDGE, |
153 | bc927e48 | Jason Baron | .abstract = true,
|
154 | bc927e48 | Jason Baron | .instance_size = sizeof(PCIExpressHost),
|
155 | bc927e48 | Jason Baron | }; |
156 | bc927e48 | Jason Baron | |
157 | bc927e48 | Jason Baron | static void pcie_host_register_types(void) |
158 | bc927e48 | Jason Baron | { |
159 | bc927e48 | Jason Baron | type_register_static(&pcie_host_type_info); |
160 | bc927e48 | Jason Baron | } |
161 | bc927e48 | Jason Baron | |
162 | bc927e48 | Jason Baron | type_init(pcie_host_register_types) |