root / hw / pci / pcie_host.c @ ddf5636d
History | View | Annotate | Download (4.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 | /* a helper function to get a PCIDevice for a given mmconfig address */
|
28 | 8d6514f8 | Isaku Yamahata | static inline PCIDevice *pcie_dev_find_by_mmcfg_addr(PCIBus *s, |
29 | 8d6514f8 | Isaku Yamahata | uint32_t mmcfg_addr) |
30 | a9f49946 | Isaku Yamahata | { |
31 | a9f49946 | Isaku Yamahata | return pci_find_device(s, PCIE_MMCFG_BUS(mmcfg_addr),
|
32 | 5256d8bf | Isaku Yamahata | PCIE_MMCFG_DEVFN(mmcfg_addr)); |
33 | a9f49946 | Isaku Yamahata | } |
34 | a9f49946 | Isaku Yamahata | |
35 | a8170e5e | Avi Kivity | static void pcie_mmcfg_data_write(void *opaque, hwaddr mmcfg_addr, |
36 | c76f990e | Avi Kivity | uint64_t val, unsigned len)
|
37 | a9f49946 | Isaku Yamahata | { |
38 | c76f990e | Avi Kivity | PCIExpressHost *e = opaque; |
39 | c76f990e | Avi Kivity | PCIBus *s = e->pci.bus; |
40 | 8d6514f8 | Isaku Yamahata | PCIDevice *pci_dev = pcie_dev_find_by_mmcfg_addr(s, mmcfg_addr); |
41 | 43e86c8f | Isaku Yamahata | uint32_t addr; |
42 | 43e86c8f | Isaku Yamahata | uint32_t limit; |
43 | a9f49946 | Isaku Yamahata | |
44 | 42e4126b | Jan Kiszka | if (!pci_dev) {
|
45 | a9f49946 | Isaku Yamahata | return;
|
46 | 42e4126b | Jan Kiszka | } |
47 | 43e86c8f | Isaku Yamahata | addr = PCIE_MMCFG_CONFOFFSET(mmcfg_addr); |
48 | 43e86c8f | Isaku Yamahata | limit = pci_config_size(pci_dev); |
49 | 43e86c8f | Isaku Yamahata | if (limit <= addr) {
|
50 | 43e86c8f | Isaku Yamahata | /* conventional pci device can be behind pcie-to-pci bridge.
|
51 | 43e86c8f | Isaku Yamahata | 256 <= addr < 4K has no effects. */
|
52 | 43e86c8f | Isaku Yamahata | return;
|
53 | 43e86c8f | Isaku Yamahata | } |
54 | 43e86c8f | Isaku Yamahata | pci_host_config_write_common(pci_dev, addr, limit, val, len); |
55 | a9f49946 | Isaku Yamahata | } |
56 | a9f49946 | Isaku Yamahata | |
57 | c76f990e | Avi Kivity | static uint64_t pcie_mmcfg_data_read(void *opaque, |
58 | a8170e5e | Avi Kivity | hwaddr mmcfg_addr, |
59 | c76f990e | Avi Kivity | unsigned len)
|
60 | a9f49946 | Isaku Yamahata | { |
61 | c76f990e | Avi Kivity | PCIExpressHost *e = opaque; |
62 | c76f990e | Avi Kivity | PCIBus *s = e->pci.bus; |
63 | 43e86c8f | Isaku Yamahata | PCIDevice *pci_dev = pcie_dev_find_by_mmcfg_addr(s, mmcfg_addr); |
64 | 43e86c8f | Isaku Yamahata | uint32_t addr; |
65 | 43e86c8f | Isaku Yamahata | uint32_t limit; |
66 | a9f49946 | Isaku Yamahata | |
67 | a9f49946 | Isaku Yamahata | if (!pci_dev) {
|
68 | 4677d8ed | Michael S. Tsirkin | return ~0x0; |
69 | a9f49946 | Isaku Yamahata | } |
70 | 43e86c8f | Isaku Yamahata | addr = PCIE_MMCFG_CONFOFFSET(mmcfg_addr); |
71 | 43e86c8f | Isaku Yamahata | limit = pci_config_size(pci_dev); |
72 | 43e86c8f | Isaku Yamahata | if (limit <= addr) {
|
73 | 43e86c8f | Isaku Yamahata | /* conventional pci device can be behind pcie-to-pci bridge.
|
74 | 43e86c8f | Isaku Yamahata | 256 <= addr < 4K has no effects. */
|
75 | 43e86c8f | Isaku Yamahata | return ~0x0; |
76 | 43e86c8f | Isaku Yamahata | } |
77 | 43e86c8f | Isaku Yamahata | return pci_host_config_read_common(pci_dev, addr, limit, len);
|
78 | a9f49946 | Isaku Yamahata | } |
79 | a9f49946 | Isaku Yamahata | |
80 | c76f990e | Avi Kivity | static const MemoryRegionOps pcie_mmcfg_ops = { |
81 | c76f990e | Avi Kivity | .read = pcie_mmcfg_data_read, |
82 | c76f990e | Avi Kivity | .write = pcie_mmcfg_data_write, |
83 | c76f990e | Avi Kivity | .endianness = DEVICE_NATIVE_ENDIAN, |
84 | a9f49946 | Isaku Yamahata | }; |
85 | a9f49946 | Isaku Yamahata | |
86 | c702ddb8 | Jason Baron | int pcie_host_init(PCIExpressHost *e)
|
87 | a9f49946 | Isaku Yamahata | { |
88 | a9f49946 | Isaku Yamahata | e->base_addr = PCIE_BASE_ADDR_UNMAPPED; |
89 | a9f49946 | Isaku Yamahata | |
90 | a9f49946 | Isaku Yamahata | return 0; |
91 | a9f49946 | Isaku Yamahata | } |
92 | a9f49946 | Isaku Yamahata | |
93 | a9f49946 | Isaku Yamahata | void pcie_host_mmcfg_unmap(PCIExpressHost *e)
|
94 | a9f49946 | Isaku Yamahata | { |
95 | a9f49946 | Isaku Yamahata | if (e->base_addr != PCIE_BASE_ADDR_UNMAPPED) {
|
96 | c76f990e | Avi Kivity | memory_region_del_subregion(get_system_memory(), &e->mmio); |
97 | c702ddb8 | Jason Baron | memory_region_destroy(&e->mmio); |
98 | a9f49946 | Isaku Yamahata | e->base_addr = PCIE_BASE_ADDR_UNMAPPED; |
99 | a9f49946 | Isaku Yamahata | } |
100 | a9f49946 | Isaku Yamahata | } |
101 | a9f49946 | Isaku Yamahata | |
102 | c702ddb8 | Jason Baron | void pcie_host_mmcfg_map(PCIExpressHost *e, hwaddr addr,
|
103 | c702ddb8 | Jason Baron | uint32_t size) |
104 | a9f49946 | Isaku Yamahata | { |
105 | c702ddb8 | Jason Baron | assert(!(size & (size - 1))); /* power of 2 */ |
106 | c702ddb8 | Jason Baron | assert(size >= PCIE_MMCFG_SIZE_MIN); |
107 | c702ddb8 | Jason Baron | assert(size <= PCIE_MMCFG_SIZE_MAX); |
108 | c702ddb8 | Jason Baron | e->size = size; |
109 | 40c5dce9 | Paolo Bonzini | memory_region_init_io(&e->mmio, OBJECT(e), &pcie_mmcfg_ops, e, |
110 | 40c5dce9 | Paolo Bonzini | "pcie-mmcfg", e->size);
|
111 | a9f49946 | Isaku Yamahata | e->base_addr = addr; |
112 | c76f990e | Avi Kivity | memory_region_add_subregion(get_system_memory(), e->base_addr, &e->mmio); |
113 | a9f49946 | Isaku Yamahata | } |
114 | a9f49946 | Isaku Yamahata | |
115 | a9f49946 | Isaku Yamahata | void pcie_host_mmcfg_update(PCIExpressHost *e,
|
116 | a9f49946 | Isaku Yamahata | int enable,
|
117 | c702ddb8 | Jason Baron | hwaddr addr, |
118 | c702ddb8 | Jason Baron | uint32_t size) |
119 | a9f49946 | Isaku Yamahata | { |
120 | a9f49946 | Isaku Yamahata | pcie_host_mmcfg_unmap(e); |
121 | a9f49946 | Isaku Yamahata | if (enable) {
|
122 | c702ddb8 | Jason Baron | pcie_host_mmcfg_map(e, addr, size); |
123 | a9f49946 | Isaku Yamahata | } |
124 | a9f49946 | Isaku Yamahata | } |
125 | bc927e48 | Jason Baron | |
126 | bc927e48 | Jason Baron | static const TypeInfo pcie_host_type_info = { |
127 | bc927e48 | Jason Baron | .name = TYPE_PCIE_HOST_BRIDGE, |
128 | bc927e48 | Jason Baron | .parent = TYPE_PCI_HOST_BRIDGE, |
129 | bc927e48 | Jason Baron | .abstract = true,
|
130 | bc927e48 | Jason Baron | .instance_size = sizeof(PCIExpressHost),
|
131 | bc927e48 | Jason Baron | }; |
132 | bc927e48 | Jason Baron | |
133 | bc927e48 | Jason Baron | static void pcie_host_register_types(void) |
134 | bc927e48 | Jason Baron | { |
135 | bc927e48 | Jason Baron | type_register_static(&pcie_host_type_info); |
136 | bc927e48 | Jason Baron | } |
137 | bc927e48 | Jason Baron | |
138 | bc927e48 | Jason Baron | type_init(pcie_host_register_types) |