root / hw / pcie_host.c @ 4677d8ed
History | View | Annotate | Download (5.5 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 | a9f49946 | Isaku Yamahata | * with this program; if not, write to the Free Software Foundation, Inc.,
|
20 | a9f49946 | Isaku Yamahata | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
21 | a9f49946 | Isaku Yamahata | */
|
22 | a9f49946 | Isaku Yamahata | |
23 | a9f49946 | Isaku Yamahata | #include "hw.h" |
24 | a9f49946 | Isaku Yamahata | #include "pci.h" |
25 | a9f49946 | Isaku Yamahata | #include "pcie_host.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 | a9f49946 | Isaku Yamahata | static inline PCIDevice *pcie_mmcfg_addr_to_dev(PCIBus *s, uint32_t mmcfg_addr) |
50 | a9f49946 | Isaku Yamahata | { |
51 | a9f49946 | Isaku Yamahata | return pci_find_device(s, PCIE_MMCFG_BUS(mmcfg_addr),
|
52 | a9f49946 | Isaku Yamahata | PCI_SLOT(PCIE_MMCFG_DEVFN(mmcfg_addr)), |
53 | a9f49946 | Isaku Yamahata | PCI_FUNC(PCIE_MMCFG_DEVFN(mmcfg_addr))); |
54 | a9f49946 | Isaku Yamahata | } |
55 | a9f49946 | Isaku Yamahata | |
56 | a9f49946 | Isaku Yamahata | static void pcie_mmcfg_data_write(PCIBus *s, |
57 | a9f49946 | Isaku Yamahata | uint32_t mmcfg_addr, uint32_t val, int len)
|
58 | a9f49946 | Isaku Yamahata | { |
59 | a9f49946 | Isaku Yamahata | PCIDevice *pci_dev = pcie_mmcfg_addr_to_dev(s, mmcfg_addr); |
60 | a9f49946 | Isaku Yamahata | |
61 | a9f49946 | Isaku Yamahata | if (!pci_dev)
|
62 | a9f49946 | Isaku Yamahata | return;
|
63 | a9f49946 | Isaku Yamahata | |
64 | a9f49946 | Isaku Yamahata | pci_dev->config_write(pci_dev, |
65 | a9f49946 | Isaku Yamahata | PCIE_MMCFG_CONFOFFSET(mmcfg_addr), val, len); |
66 | a9f49946 | Isaku Yamahata | } |
67 | a9f49946 | Isaku Yamahata | |
68 | 4677d8ed | Michael S. Tsirkin | static uint32_t pcie_mmcfg_data_read(PCIBus *s, uint32_t addr, int len) |
69 | a9f49946 | Isaku Yamahata | { |
70 | 4677d8ed | Michael S. Tsirkin | PCIDevice *pci_dev = pcie_mmcfg_addr_to_dev(s, addr); |
71 | a9f49946 | Isaku Yamahata | |
72 | 4677d8ed | Michael S. Tsirkin | assert(len == 1 || len == 2 || len == 4); |
73 | a9f49946 | Isaku Yamahata | if (!pci_dev) {
|
74 | 4677d8ed | Michael S. Tsirkin | return ~0x0; |
75 | a9f49946 | Isaku Yamahata | } |
76 | 4677d8ed | Michael S. Tsirkin | return pci_dev->config_read(pci_dev, PCIE_MMCFG_CONFOFFSET(addr), len);
|
77 | a9f49946 | Isaku Yamahata | } |
78 | a9f49946 | Isaku Yamahata | |
79 | a9f49946 | Isaku Yamahata | static void pcie_mmcfg_data_writeb(void *opaque, |
80 | a9f49946 | Isaku Yamahata | target_phys_addr_t addr, uint32_t value) |
81 | a9f49946 | Isaku Yamahata | { |
82 | a9f49946 | Isaku Yamahata | PCIExpressHost *e = opaque; |
83 | a9f49946 | Isaku Yamahata | pcie_mmcfg_data_write(e->pci.bus, addr - e->base_addr, value, 1);
|
84 | a9f49946 | Isaku Yamahata | } |
85 | a9f49946 | Isaku Yamahata | |
86 | a9f49946 | Isaku Yamahata | static void pcie_mmcfg_data_writew(void *opaque, |
87 | a9f49946 | Isaku Yamahata | target_phys_addr_t addr, uint32_t value) |
88 | a9f49946 | Isaku Yamahata | { |
89 | a9f49946 | Isaku Yamahata | PCIExpressHost *e = opaque; |
90 | a9f49946 | Isaku Yamahata | pcie_mmcfg_data_write(e->pci.bus, addr - e->base_addr, value, 2);
|
91 | a9f49946 | Isaku Yamahata | } |
92 | a9f49946 | Isaku Yamahata | |
93 | a9f49946 | Isaku Yamahata | static void pcie_mmcfg_data_writel(void *opaque, |
94 | a9f49946 | Isaku Yamahata | target_phys_addr_t addr, uint32_t value) |
95 | a9f49946 | Isaku Yamahata | { |
96 | a9f49946 | Isaku Yamahata | PCIExpressHost *e = opaque; |
97 | a9f49946 | Isaku Yamahata | pcie_mmcfg_data_write(e->pci.bus, addr - e->base_addr, value, 4);
|
98 | a9f49946 | Isaku Yamahata | } |
99 | a9f49946 | Isaku Yamahata | |
100 | a9f49946 | Isaku Yamahata | static uint32_t pcie_mmcfg_data_readb(void *opaque, target_phys_addr_t addr) |
101 | a9f49946 | Isaku Yamahata | { |
102 | a9f49946 | Isaku Yamahata | PCIExpressHost *e = opaque; |
103 | a9f49946 | Isaku Yamahata | return pcie_mmcfg_data_read(e->pci.bus, addr - e->base_addr, 1); |
104 | a9f49946 | Isaku Yamahata | } |
105 | a9f49946 | Isaku Yamahata | |
106 | a9f49946 | Isaku Yamahata | static uint32_t pcie_mmcfg_data_readw(void *opaque, target_phys_addr_t addr) |
107 | a9f49946 | Isaku Yamahata | { |
108 | a9f49946 | Isaku Yamahata | PCIExpressHost *e = opaque; |
109 | a9f49946 | Isaku Yamahata | return pcie_mmcfg_data_read(e->pci.bus, addr - e->base_addr, 2); |
110 | a9f49946 | Isaku Yamahata | } |
111 | a9f49946 | Isaku Yamahata | |
112 | a9f49946 | Isaku Yamahata | static uint32_t pcie_mmcfg_data_readl(void *opaque, target_phys_addr_t addr) |
113 | a9f49946 | Isaku Yamahata | { |
114 | a9f49946 | Isaku Yamahata | PCIExpressHost *e = opaque; |
115 | a9f49946 | Isaku Yamahata | return pcie_mmcfg_data_read(e->pci.bus, addr - e->base_addr, 4); |
116 | a9f49946 | Isaku Yamahata | } |
117 | a9f49946 | Isaku Yamahata | |
118 | a9f49946 | Isaku Yamahata | |
119 | a9f49946 | Isaku Yamahata | static CPUWriteMemoryFunc * const pcie_mmcfg_write[] = |
120 | a9f49946 | Isaku Yamahata | { |
121 | a9f49946 | Isaku Yamahata | pcie_mmcfg_data_writeb, |
122 | a9f49946 | Isaku Yamahata | pcie_mmcfg_data_writew, |
123 | a9f49946 | Isaku Yamahata | pcie_mmcfg_data_writel, |
124 | a9f49946 | Isaku Yamahata | }; |
125 | a9f49946 | Isaku Yamahata | |
126 | a9f49946 | Isaku Yamahata | static CPUReadMemoryFunc * const pcie_mmcfg_read[] = |
127 | a9f49946 | Isaku Yamahata | { |
128 | a9f49946 | Isaku Yamahata | pcie_mmcfg_data_readb, |
129 | a9f49946 | Isaku Yamahata | pcie_mmcfg_data_readw, |
130 | a9f49946 | Isaku Yamahata | pcie_mmcfg_data_readl, |
131 | a9f49946 | Isaku Yamahata | }; |
132 | a9f49946 | Isaku Yamahata | |
133 | a9f49946 | Isaku Yamahata | /* pcie_host::base_addr == PCIE_BASE_ADDR_UNMAPPED when it isn't mapped. */
|
134 | a9f49946 | Isaku Yamahata | #define PCIE_BASE_ADDR_UNMAPPED ((target_phys_addr_t)-1ULL) |
135 | a9f49946 | Isaku Yamahata | |
136 | a9f49946 | Isaku Yamahata | int pcie_host_init(PCIExpressHost *e)
|
137 | a9f49946 | Isaku Yamahata | { |
138 | a9f49946 | Isaku Yamahata | e->base_addr = PCIE_BASE_ADDR_UNMAPPED; |
139 | a9f49946 | Isaku Yamahata | e->mmio_index = |
140 | a9f49946 | Isaku Yamahata | cpu_register_io_memory(pcie_mmcfg_read, pcie_mmcfg_write, e); |
141 | a9f49946 | Isaku Yamahata | if (e->mmio_index < 0) { |
142 | a9f49946 | Isaku Yamahata | return -1; |
143 | a9f49946 | Isaku Yamahata | } |
144 | a9f49946 | Isaku Yamahata | |
145 | a9f49946 | Isaku Yamahata | return 0; |
146 | a9f49946 | Isaku Yamahata | } |
147 | a9f49946 | Isaku Yamahata | |
148 | a9f49946 | Isaku Yamahata | void pcie_host_mmcfg_unmap(PCIExpressHost *e)
|
149 | a9f49946 | Isaku Yamahata | { |
150 | a9f49946 | Isaku Yamahata | if (e->base_addr != PCIE_BASE_ADDR_UNMAPPED) {
|
151 | a9f49946 | Isaku Yamahata | cpu_register_physical_memory(e->base_addr, e->size, IO_MEM_UNASSIGNED); |
152 | a9f49946 | Isaku Yamahata | e->base_addr = PCIE_BASE_ADDR_UNMAPPED; |
153 | a9f49946 | Isaku Yamahata | } |
154 | a9f49946 | Isaku Yamahata | } |
155 | a9f49946 | Isaku Yamahata | |
156 | a9f49946 | Isaku Yamahata | void pcie_host_mmcfg_map(PCIExpressHost *e,
|
157 | a9f49946 | Isaku Yamahata | target_phys_addr_t addr, uint32_t size) |
158 | a9f49946 | Isaku Yamahata | { |
159 | a9f49946 | Isaku Yamahata | assert(!(size & (size - 1))); /* power of 2 */ |
160 | a9f49946 | Isaku Yamahata | assert(size >= PCIE_MMCFG_SIZE_MIN); |
161 | a9f49946 | Isaku Yamahata | assert(size <= PCIE_MMCFG_SIZE_MAX); |
162 | a9f49946 | Isaku Yamahata | |
163 | a9f49946 | Isaku Yamahata | e->base_addr = addr; |
164 | a9f49946 | Isaku Yamahata | e->size = size; |
165 | a9f49946 | Isaku Yamahata | cpu_register_physical_memory(e->base_addr, e->size, e->mmio_index); |
166 | a9f49946 | Isaku Yamahata | } |
167 | a9f49946 | Isaku Yamahata | |
168 | a9f49946 | Isaku Yamahata | void pcie_host_mmcfg_update(PCIExpressHost *e,
|
169 | a9f49946 | Isaku Yamahata | int enable,
|
170 | a9f49946 | Isaku Yamahata | target_phys_addr_t addr, uint32_t size) |
171 | a9f49946 | Isaku Yamahata | { |
172 | a9f49946 | Isaku Yamahata | pcie_host_mmcfg_unmap(e); |
173 | a9f49946 | Isaku Yamahata | if (enable) {
|
174 | a9f49946 | Isaku Yamahata | pcie_host_mmcfg_map(e, addr, size); |
175 | a9f49946 | Isaku Yamahata | } |
176 | a9f49946 | Isaku Yamahata | } |