root / hw / msix.c @ cf03eb2c
History | View | Annotate | Download (11.9 kB)
1 | 02eb84d0 | Michael S. Tsirkin | /*
|
---|---|---|---|
2 | 02eb84d0 | Michael S. Tsirkin | * MSI-X device support
|
3 | 02eb84d0 | Michael S. Tsirkin | *
|
4 | 02eb84d0 | Michael S. Tsirkin | * This module includes support for MSI-X in pci devices.
|
5 | 02eb84d0 | Michael S. Tsirkin | *
|
6 | 02eb84d0 | Michael S. Tsirkin | * Author: Michael S. Tsirkin <mst@redhat.com>
|
7 | 02eb84d0 | Michael S. Tsirkin | *
|
8 | 02eb84d0 | Michael S. Tsirkin | * Copyright (c) 2009, Red Hat Inc, Michael S. Tsirkin (mst@redhat.com)
|
9 | 02eb84d0 | Michael S. Tsirkin | *
|
10 | 02eb84d0 | Michael S. Tsirkin | * This work is licensed under the terms of the GNU GPL, version 2. See
|
11 | 02eb84d0 | Michael S. Tsirkin | * the COPYING file in the top-level directory.
|
12 | 02eb84d0 | Michael S. Tsirkin | */
|
13 | 02eb84d0 | Michael S. Tsirkin | |
14 | 02eb84d0 | Michael S. Tsirkin | #include "hw.h" |
15 | 02eb84d0 | Michael S. Tsirkin | #include "msix.h" |
16 | 02eb84d0 | Michael S. Tsirkin | #include "pci.h" |
17 | 02eb84d0 | Michael S. Tsirkin | |
18 | 02eb84d0 | Michael S. Tsirkin | /* MSI-X capability structure */
|
19 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_TABLE_OFFSET 4 |
20 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_PBA_OFFSET 8 |
21 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_CAP_LENGTH 12 |
22 | 02eb84d0 | Michael S. Tsirkin | |
23 | 2760952b | Michael S. Tsirkin | /* MSI enable bit and maskall bit are in byte 1 in FLAGS register */
|
24 | 2760952b | Michael S. Tsirkin | #define MSIX_CONTROL_OFFSET (PCI_MSIX_FLAGS + 1) |
25 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_ENABLE_MASK (PCI_MSIX_FLAGS_ENABLE >> 8) |
26 | 5b5cb086 | Michael S. Tsirkin | #define MSIX_MASKALL_MASK (PCI_MSIX_FLAGS_MASKALL >> 8) |
27 | 02eb84d0 | Michael S. Tsirkin | |
28 | 02eb84d0 | Michael S. Tsirkin | /* MSI-X table format */
|
29 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_MSG_ADDR 0 |
30 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_MSG_UPPER_ADDR 4 |
31 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_MSG_DATA 8 |
32 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_VECTOR_CTRL 12 |
33 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_ENTRY_SIZE 16 |
34 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_VECTOR_MASK 0x1 |
35 | 5a1fc5e8 | Michael S. Tsirkin | |
36 | 5a1fc5e8 | Michael S. Tsirkin | /* How much space does an MSIX table need. */
|
37 | 5a1fc5e8 | Michael S. Tsirkin | /* The spec requires giving the table structure
|
38 | 5a1fc5e8 | Michael S. Tsirkin | * a 4K aligned region all by itself. */
|
39 | 5a1fc5e8 | Michael S. Tsirkin | #define MSIX_PAGE_SIZE 0x1000 |
40 | 5a1fc5e8 | Michael S. Tsirkin | /* Reserve second half of the page for pending bits */
|
41 | 5a1fc5e8 | Michael S. Tsirkin | #define MSIX_PAGE_PENDING (MSIX_PAGE_SIZE / 2) |
42 | 02eb84d0 | Michael S. Tsirkin | #define MSIX_MAX_ENTRIES 32 |
43 | 02eb84d0 | Michael S. Tsirkin | |
44 | 02eb84d0 | Michael S. Tsirkin | |
45 | 02eb84d0 | Michael S. Tsirkin | /* Flag for interrupt controller to declare MSI-X support */
|
46 | 02eb84d0 | Michael S. Tsirkin | int msix_supported;
|
47 | 02eb84d0 | Michael S. Tsirkin | |
48 | 02eb84d0 | Michael S. Tsirkin | /* Add MSI-X capability to the config space for the device. */
|
49 | 02eb84d0 | Michael S. Tsirkin | /* Given a bar and its size, add MSI-X table on top of it
|
50 | 02eb84d0 | Michael S. Tsirkin | * and fill MSI-X capability in the config space.
|
51 | 02eb84d0 | Michael S. Tsirkin | * Original bar size must be a power of 2 or 0.
|
52 | 02eb84d0 | Michael S. Tsirkin | * New bar size is returned. */
|
53 | 02eb84d0 | Michael S. Tsirkin | static int msix_add_config(struct PCIDevice *pdev, unsigned short nentries, |
54 | 02eb84d0 | Michael S. Tsirkin | unsigned bar_nr, unsigned bar_size) |
55 | 02eb84d0 | Michael S. Tsirkin | { |
56 | 02eb84d0 | Michael S. Tsirkin | int config_offset;
|
57 | 02eb84d0 | Michael S. Tsirkin | uint8_t *config; |
58 | 02eb84d0 | Michael S. Tsirkin | uint32_t new_size; |
59 | 02eb84d0 | Michael S. Tsirkin | |
60 | 02eb84d0 | Michael S. Tsirkin | if (nentries < 1 || nentries > PCI_MSIX_FLAGS_QSIZE + 1) |
61 | 02eb84d0 | Michael S. Tsirkin | return -EINVAL;
|
62 | 02eb84d0 | Michael S. Tsirkin | if (bar_size > 0x80000000) |
63 | 02eb84d0 | Michael S. Tsirkin | return -ENOSPC;
|
64 | 02eb84d0 | Michael S. Tsirkin | |
65 | 02eb84d0 | Michael S. Tsirkin | /* Add space for MSI-X structures */
|
66 | 5e520a7d | Blue Swirl | if (!bar_size) {
|
67 | 5a1fc5e8 | Michael S. Tsirkin | new_size = MSIX_PAGE_SIZE; |
68 | 5a1fc5e8 | Michael S. Tsirkin | } else if (bar_size < MSIX_PAGE_SIZE) { |
69 | 5a1fc5e8 | Michael S. Tsirkin | bar_size = MSIX_PAGE_SIZE; |
70 | 5a1fc5e8 | Michael S. Tsirkin | new_size = MSIX_PAGE_SIZE * 2;
|
71 | 5a1fc5e8 | Michael S. Tsirkin | } else {
|
72 | 02eb84d0 | Michael S. Tsirkin | new_size = bar_size * 2;
|
73 | 5a1fc5e8 | Michael S. Tsirkin | } |
74 | 02eb84d0 | Michael S. Tsirkin | |
75 | 02eb84d0 | Michael S. Tsirkin | pdev->msix_bar_size = new_size; |
76 | 02eb84d0 | Michael S. Tsirkin | config_offset = pci_add_capability(pdev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH); |
77 | 02eb84d0 | Michael S. Tsirkin | if (config_offset < 0) |
78 | 02eb84d0 | Michael S. Tsirkin | return config_offset;
|
79 | 02eb84d0 | Michael S. Tsirkin | config = pdev->config + config_offset; |
80 | 02eb84d0 | Michael S. Tsirkin | |
81 | 02eb84d0 | Michael S. Tsirkin | pci_set_word(config + PCI_MSIX_FLAGS, nentries - 1);
|
82 | 02eb84d0 | Michael S. Tsirkin | /* Table on top of BAR */
|
83 | 02eb84d0 | Michael S. Tsirkin | pci_set_long(config + MSIX_TABLE_OFFSET, bar_size | bar_nr); |
84 | 02eb84d0 | Michael S. Tsirkin | /* Pending bits on top of that */
|
85 | 5a1fc5e8 | Michael S. Tsirkin | pci_set_long(config + MSIX_PBA_OFFSET, (bar_size + MSIX_PAGE_PENDING) | |
86 | 5a1fc5e8 | Michael S. Tsirkin | bar_nr); |
87 | 02eb84d0 | Michael S. Tsirkin | pdev->msix_cap = config_offset; |
88 | 02eb84d0 | Michael S. Tsirkin | /* Make flags bit writeable. */
|
89 | 5b5cb086 | Michael S. Tsirkin | pdev->wmask[config_offset + MSIX_CONTROL_OFFSET] |= MSIX_ENABLE_MASK | |
90 | 5b5cb086 | Michael S. Tsirkin | MSIX_MASKALL_MASK; |
91 | 02eb84d0 | Michael S. Tsirkin | return 0; |
92 | 02eb84d0 | Michael S. Tsirkin | } |
93 | 02eb84d0 | Michael S. Tsirkin | |
94 | c227f099 | Anthony Liguori | static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr) |
95 | 02eb84d0 | Michael S. Tsirkin | { |
96 | 02eb84d0 | Michael S. Tsirkin | PCIDevice *dev = opaque; |
97 | 76f5159d | Michael S. Tsirkin | unsigned int offset = addr & (MSIX_PAGE_SIZE - 1) & ~0x3; |
98 | 02eb84d0 | Michael S. Tsirkin | void *page = dev->msix_table_page;
|
99 | 02eb84d0 | Michael S. Tsirkin | |
100 | 76f5159d | Michael S. Tsirkin | return pci_get_long(page + offset);
|
101 | 02eb84d0 | Michael S. Tsirkin | } |
102 | 02eb84d0 | Michael S. Tsirkin | |
103 | c227f099 | Anthony Liguori | static uint32_t msix_mmio_read_unallowed(void *opaque, target_phys_addr_t addr) |
104 | 02eb84d0 | Michael S. Tsirkin | { |
105 | 02eb84d0 | Michael S. Tsirkin | fprintf(stderr, "MSI-X: only dword read is allowed!\n");
|
106 | 02eb84d0 | Michael S. Tsirkin | return 0; |
107 | 02eb84d0 | Michael S. Tsirkin | } |
108 | 02eb84d0 | Michael S. Tsirkin | |
109 | 02eb84d0 | Michael S. Tsirkin | static uint8_t msix_pending_mask(int vector) |
110 | 02eb84d0 | Michael S. Tsirkin | { |
111 | 02eb84d0 | Michael S. Tsirkin | return 1 << (vector % 8); |
112 | 02eb84d0 | Michael S. Tsirkin | } |
113 | 02eb84d0 | Michael S. Tsirkin | |
114 | 02eb84d0 | Michael S. Tsirkin | static uint8_t *msix_pending_byte(PCIDevice *dev, int vector) |
115 | 02eb84d0 | Michael S. Tsirkin | { |
116 | 5a1fc5e8 | Michael S. Tsirkin | return dev->msix_table_page + MSIX_PAGE_PENDING + vector / 8; |
117 | 02eb84d0 | Michael S. Tsirkin | } |
118 | 02eb84d0 | Michael S. Tsirkin | |
119 | 02eb84d0 | Michael S. Tsirkin | static int msix_is_pending(PCIDevice *dev, int vector) |
120 | 02eb84d0 | Michael S. Tsirkin | { |
121 | 02eb84d0 | Michael S. Tsirkin | return *msix_pending_byte(dev, vector) & msix_pending_mask(vector);
|
122 | 02eb84d0 | Michael S. Tsirkin | } |
123 | 02eb84d0 | Michael S. Tsirkin | |
124 | 02eb84d0 | Michael S. Tsirkin | static void msix_set_pending(PCIDevice *dev, int vector) |
125 | 02eb84d0 | Michael S. Tsirkin | { |
126 | 02eb84d0 | Michael S. Tsirkin | *msix_pending_byte(dev, vector) |= msix_pending_mask(vector); |
127 | 02eb84d0 | Michael S. Tsirkin | } |
128 | 02eb84d0 | Michael S. Tsirkin | |
129 | 02eb84d0 | Michael S. Tsirkin | static void msix_clr_pending(PCIDevice *dev, int vector) |
130 | 02eb84d0 | Michael S. Tsirkin | { |
131 | 02eb84d0 | Michael S. Tsirkin | *msix_pending_byte(dev, vector) &= ~msix_pending_mask(vector); |
132 | 02eb84d0 | Michael S. Tsirkin | } |
133 | 02eb84d0 | Michael S. Tsirkin | |
134 | 5b5cb086 | Michael S. Tsirkin | static int msix_function_masked(PCIDevice *dev) |
135 | 5b5cb086 | Michael S. Tsirkin | { |
136 | 5b5cb086 | Michael S. Tsirkin | return dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] & MSIX_MASKALL_MASK;
|
137 | 5b5cb086 | Michael S. Tsirkin | } |
138 | 5b5cb086 | Michael S. Tsirkin | |
139 | 02eb84d0 | Michael S. Tsirkin | static int msix_is_masked(PCIDevice *dev, int vector) |
140 | 02eb84d0 | Michael S. Tsirkin | { |
141 | 02eb84d0 | Michael S. Tsirkin | unsigned offset = vector * MSIX_ENTRY_SIZE + MSIX_VECTOR_CTRL;
|
142 | 5b5cb086 | Michael S. Tsirkin | return msix_function_masked(dev) ||
|
143 | 5b5cb086 | Michael S. Tsirkin | dev->msix_table_page[offset] & MSIX_VECTOR_MASK; |
144 | 5b5cb086 | Michael S. Tsirkin | } |
145 | 5b5cb086 | Michael S. Tsirkin | |
146 | 5b5cb086 | Michael S. Tsirkin | static void msix_handle_mask_update(PCIDevice *dev, int vector) |
147 | 5b5cb086 | Michael S. Tsirkin | { |
148 | 5b5cb086 | Michael S. Tsirkin | if (!msix_is_masked(dev, vector) && msix_is_pending(dev, vector)) {
|
149 | 5b5cb086 | Michael S. Tsirkin | msix_clr_pending(dev, vector); |
150 | 5b5cb086 | Michael S. Tsirkin | msix_notify(dev, vector); |
151 | 5b5cb086 | Michael S. Tsirkin | } |
152 | 5b5cb086 | Michael S. Tsirkin | } |
153 | 5b5cb086 | Michael S. Tsirkin | |
154 | 5b5cb086 | Michael S. Tsirkin | /* Handle MSI-X capability config write. */
|
155 | 5b5cb086 | Michael S. Tsirkin | void msix_write_config(PCIDevice *dev, uint32_t addr,
|
156 | 5b5cb086 | Michael S. Tsirkin | uint32_t val, int len)
|
157 | 5b5cb086 | Michael S. Tsirkin | { |
158 | 5b5cb086 | Michael S. Tsirkin | unsigned enable_pos = dev->msix_cap + MSIX_CONTROL_OFFSET;
|
159 | 5b5cb086 | Michael S. Tsirkin | int vector;
|
160 | 5b5cb086 | Michael S. Tsirkin | |
161 | 98a3cb02 | Isaku Yamahata | if (!range_covers_byte(addr, len, enable_pos)) {
|
162 | 5b5cb086 | Michael S. Tsirkin | return;
|
163 | 5b5cb086 | Michael S. Tsirkin | } |
164 | 5b5cb086 | Michael S. Tsirkin | |
165 | 5b5cb086 | Michael S. Tsirkin | if (!msix_enabled(dev)) {
|
166 | 5b5cb086 | Michael S. Tsirkin | return;
|
167 | 5b5cb086 | Michael S. Tsirkin | } |
168 | 5b5cb086 | Michael S. Tsirkin | |
169 | 5b5cb086 | Michael S. Tsirkin | qemu_set_irq(dev->irq[0], 0); |
170 | 5b5cb086 | Michael S. Tsirkin | |
171 | 5b5cb086 | Michael S. Tsirkin | if (msix_function_masked(dev)) {
|
172 | 5b5cb086 | Michael S. Tsirkin | return;
|
173 | 5b5cb086 | Michael S. Tsirkin | } |
174 | 5b5cb086 | Michael S. Tsirkin | |
175 | 5b5cb086 | Michael S. Tsirkin | for (vector = 0; vector < dev->msix_entries_nr; ++vector) { |
176 | 5b5cb086 | Michael S. Tsirkin | msix_handle_mask_update(dev, vector); |
177 | 5b5cb086 | Michael S. Tsirkin | } |
178 | 02eb84d0 | Michael S. Tsirkin | } |
179 | 02eb84d0 | Michael S. Tsirkin | |
180 | c227f099 | Anthony Liguori | static void msix_mmio_writel(void *opaque, target_phys_addr_t addr, |
181 | 02eb84d0 | Michael S. Tsirkin | uint32_t val) |
182 | 02eb84d0 | Michael S. Tsirkin | { |
183 | 02eb84d0 | Michael S. Tsirkin | PCIDevice *dev = opaque; |
184 | 76f5159d | Michael S. Tsirkin | unsigned int offset = addr & (MSIX_PAGE_SIZE - 1) & ~0x3; |
185 | 02eb84d0 | Michael S. Tsirkin | int vector = offset / MSIX_ENTRY_SIZE;
|
186 | 76f5159d | Michael S. Tsirkin | pci_set_long(dev->msix_table_page + offset, val); |
187 | 5b5cb086 | Michael S. Tsirkin | msix_handle_mask_update(dev, vector); |
188 | 02eb84d0 | Michael S. Tsirkin | } |
189 | 02eb84d0 | Michael S. Tsirkin | |
190 | c227f099 | Anthony Liguori | static void msix_mmio_write_unallowed(void *opaque, target_phys_addr_t addr, |
191 | 02eb84d0 | Michael S. Tsirkin | uint32_t val) |
192 | 02eb84d0 | Michael S. Tsirkin | { |
193 | 02eb84d0 | Michael S. Tsirkin | fprintf(stderr, "MSI-X: only dword write is allowed!\n");
|
194 | 02eb84d0 | Michael S. Tsirkin | } |
195 | 02eb84d0 | Michael S. Tsirkin | |
196 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const msix_mmio_write[] = { |
197 | 02eb84d0 | Michael S. Tsirkin | msix_mmio_write_unallowed, msix_mmio_write_unallowed, msix_mmio_writel |
198 | 02eb84d0 | Michael S. Tsirkin | }; |
199 | 02eb84d0 | Michael S. Tsirkin | |
200 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const msix_mmio_read[] = { |
201 | 02eb84d0 | Michael S. Tsirkin | msix_mmio_read_unallowed, msix_mmio_read_unallowed, msix_mmio_readl |
202 | 02eb84d0 | Michael S. Tsirkin | }; |
203 | 02eb84d0 | Michael S. Tsirkin | |
204 | 02eb84d0 | Michael S. Tsirkin | /* Should be called from device's map method. */
|
205 | 02eb84d0 | Michael S. Tsirkin | void msix_mmio_map(PCIDevice *d, int region_num, |
206 | 6e355d90 | Isaku Yamahata | pcibus_t addr, pcibus_t size, int type)
|
207 | 02eb84d0 | Michael S. Tsirkin | { |
208 | 02eb84d0 | Michael S. Tsirkin | uint8_t *config = d->config + d->msix_cap; |
209 | 02eb84d0 | Michael S. Tsirkin | uint32_t table = pci_get_long(config + MSIX_TABLE_OFFSET); |
210 | 5a1fc5e8 | Michael S. Tsirkin | uint32_t offset = table & ~(MSIX_PAGE_SIZE - 1);
|
211 | 02eb84d0 | Michael S. Tsirkin | /* TODO: for assigned devices, we'll want to make it possible to map
|
212 | 02eb84d0 | Michael S. Tsirkin | * pending bits separately in case they are in a separate bar. */
|
213 | 02eb84d0 | Michael S. Tsirkin | int table_bir = table & PCI_MSIX_FLAGS_BIRMASK;
|
214 | 02eb84d0 | Michael S. Tsirkin | |
215 | 02eb84d0 | Michael S. Tsirkin | if (table_bir != region_num)
|
216 | 02eb84d0 | Michael S. Tsirkin | return;
|
217 | 02eb84d0 | Michael S. Tsirkin | if (size <= offset)
|
218 | 02eb84d0 | Michael S. Tsirkin | return;
|
219 | 02eb84d0 | Michael S. Tsirkin | cpu_register_physical_memory(addr + offset, size - offset, |
220 | 02eb84d0 | Michael S. Tsirkin | d->msix_mmio_index); |
221 | 02eb84d0 | Michael S. Tsirkin | } |
222 | 02eb84d0 | Michael S. Tsirkin | |
223 | ae1be0bb | Michael S. Tsirkin | static void msix_mask_all(struct PCIDevice *dev, unsigned nentries) |
224 | ae1be0bb | Michael S. Tsirkin | { |
225 | ae1be0bb | Michael S. Tsirkin | int vector;
|
226 | ae1be0bb | Michael S. Tsirkin | for (vector = 0; vector < nentries; ++vector) { |
227 | ae1be0bb | Michael S. Tsirkin | unsigned offset = vector * MSIX_ENTRY_SIZE + MSIX_VECTOR_CTRL;
|
228 | ae1be0bb | Michael S. Tsirkin | dev->msix_table_page[offset] |= MSIX_VECTOR_MASK; |
229 | ae1be0bb | Michael S. Tsirkin | } |
230 | ae1be0bb | Michael S. Tsirkin | } |
231 | ae1be0bb | Michael S. Tsirkin | |
232 | 02eb84d0 | Michael S. Tsirkin | /* Initialize the MSI-X structures. Note: if MSI-X is supported, BAR size is
|
233 | 02eb84d0 | Michael S. Tsirkin | * modified, it should be retrieved with msix_bar_size. */
|
234 | 02eb84d0 | Michael S. Tsirkin | int msix_init(struct PCIDevice *dev, unsigned short nentries, |
235 | 5a1fc5e8 | Michael S. Tsirkin | unsigned bar_nr, unsigned bar_size) |
236 | 02eb84d0 | Michael S. Tsirkin | { |
237 | 02eb84d0 | Michael S. Tsirkin | int ret;
|
238 | 02eb84d0 | Michael S. Tsirkin | /* Nothing to do if MSI is not supported by interrupt controller */
|
239 | 02eb84d0 | Michael S. Tsirkin | if (!msix_supported)
|
240 | 02eb84d0 | Michael S. Tsirkin | return -ENOTSUP;
|
241 | 02eb84d0 | Michael S. Tsirkin | |
242 | 02eb84d0 | Michael S. Tsirkin | if (nentries > MSIX_MAX_ENTRIES)
|
243 | 02eb84d0 | Michael S. Tsirkin | return -EINVAL;
|
244 | 02eb84d0 | Michael S. Tsirkin | |
245 | 02eb84d0 | Michael S. Tsirkin | dev->msix_entry_used = qemu_mallocz(MSIX_MAX_ENTRIES * |
246 | 02eb84d0 | Michael S. Tsirkin | sizeof *dev->msix_entry_used);
|
247 | 02eb84d0 | Michael S. Tsirkin | |
248 | 5a1fc5e8 | Michael S. Tsirkin | dev->msix_table_page = qemu_mallocz(MSIX_PAGE_SIZE); |
249 | ae1be0bb | Michael S. Tsirkin | msix_mask_all(dev, nentries); |
250 | 02eb84d0 | Michael S. Tsirkin | |
251 | 02eb84d0 | Michael S. Tsirkin | dev->msix_mmio_index = cpu_register_io_memory(msix_mmio_read, |
252 | 02eb84d0 | Michael S. Tsirkin | msix_mmio_write, dev); |
253 | 02eb84d0 | Michael S. Tsirkin | if (dev->msix_mmio_index == -1) { |
254 | 02eb84d0 | Michael S. Tsirkin | ret = -EBUSY; |
255 | 02eb84d0 | Michael S. Tsirkin | goto err_index;
|
256 | 02eb84d0 | Michael S. Tsirkin | } |
257 | 02eb84d0 | Michael S. Tsirkin | |
258 | 02eb84d0 | Michael S. Tsirkin | dev->msix_entries_nr = nentries; |
259 | 02eb84d0 | Michael S. Tsirkin | ret = msix_add_config(dev, nentries, bar_nr, bar_size); |
260 | 02eb84d0 | Michael S. Tsirkin | if (ret)
|
261 | 02eb84d0 | Michael S. Tsirkin | goto err_config;
|
262 | 02eb84d0 | Michael S. Tsirkin | |
263 | 02eb84d0 | Michael S. Tsirkin | dev->cap_present |= QEMU_PCI_CAP_MSIX; |
264 | 02eb84d0 | Michael S. Tsirkin | return 0; |
265 | 02eb84d0 | Michael S. Tsirkin | |
266 | 02eb84d0 | Michael S. Tsirkin | err_config:
|
267 | 3174ecd1 | Michael S. Tsirkin | dev->msix_entries_nr = 0;
|
268 | 02eb84d0 | Michael S. Tsirkin | cpu_unregister_io_memory(dev->msix_mmio_index); |
269 | 02eb84d0 | Michael S. Tsirkin | err_index:
|
270 | 02eb84d0 | Michael S. Tsirkin | qemu_free(dev->msix_table_page); |
271 | 02eb84d0 | Michael S. Tsirkin | dev->msix_table_page = NULL;
|
272 | 02eb84d0 | Michael S. Tsirkin | qemu_free(dev->msix_entry_used); |
273 | 02eb84d0 | Michael S. Tsirkin | dev->msix_entry_used = NULL;
|
274 | 02eb84d0 | Michael S. Tsirkin | return ret;
|
275 | 02eb84d0 | Michael S. Tsirkin | } |
276 | 02eb84d0 | Michael S. Tsirkin | |
277 | 98304c84 | Michael S. Tsirkin | static void msix_free_irq_entries(PCIDevice *dev) |
278 | 98304c84 | Michael S. Tsirkin | { |
279 | 98304c84 | Michael S. Tsirkin | int vector;
|
280 | 98304c84 | Michael S. Tsirkin | |
281 | 98304c84 | Michael S. Tsirkin | for (vector = 0; vector < dev->msix_entries_nr; ++vector) { |
282 | 98304c84 | Michael S. Tsirkin | dev->msix_entry_used[vector] = 0;
|
283 | 98304c84 | Michael S. Tsirkin | msix_clr_pending(dev, vector); |
284 | 98304c84 | Michael S. Tsirkin | } |
285 | 98304c84 | Michael S. Tsirkin | } |
286 | 98304c84 | Michael S. Tsirkin | |
287 | 02eb84d0 | Michael S. Tsirkin | /* Clean up resources for the device. */
|
288 | 02eb84d0 | Michael S. Tsirkin | int msix_uninit(PCIDevice *dev)
|
289 | 02eb84d0 | Michael S. Tsirkin | { |
290 | 02eb84d0 | Michael S. Tsirkin | if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
|
291 | 02eb84d0 | Michael S. Tsirkin | return 0; |
292 | 02eb84d0 | Michael S. Tsirkin | pci_del_capability(dev, PCI_CAP_ID_MSIX, MSIX_CAP_LENGTH); |
293 | 02eb84d0 | Michael S. Tsirkin | dev->msix_cap = 0;
|
294 | 02eb84d0 | Michael S. Tsirkin | msix_free_irq_entries(dev); |
295 | 02eb84d0 | Michael S. Tsirkin | dev->msix_entries_nr = 0;
|
296 | 02eb84d0 | Michael S. Tsirkin | cpu_unregister_io_memory(dev->msix_mmio_index); |
297 | 02eb84d0 | Michael S. Tsirkin | qemu_free(dev->msix_table_page); |
298 | 02eb84d0 | Michael S. Tsirkin | dev->msix_table_page = NULL;
|
299 | 02eb84d0 | Michael S. Tsirkin | qemu_free(dev->msix_entry_used); |
300 | 02eb84d0 | Michael S. Tsirkin | dev->msix_entry_used = NULL;
|
301 | 02eb84d0 | Michael S. Tsirkin | dev->cap_present &= ~QEMU_PCI_CAP_MSIX; |
302 | 02eb84d0 | Michael S. Tsirkin | return 0; |
303 | 02eb84d0 | Michael S. Tsirkin | } |
304 | 02eb84d0 | Michael S. Tsirkin | |
305 | 02eb84d0 | Michael S. Tsirkin | void msix_save(PCIDevice *dev, QEMUFile *f)
|
306 | 02eb84d0 | Michael S. Tsirkin | { |
307 | 9a3e12c8 | Michael S. Tsirkin | unsigned n = dev->msix_entries_nr;
|
308 | 9a3e12c8 | Michael S. Tsirkin | |
309 | 72755a70 | Michael S. Tsirkin | if (!(dev->cap_present & QEMU_PCI_CAP_MSIX)) {
|
310 | 9a3e12c8 | Michael S. Tsirkin | return;
|
311 | 72755a70 | Michael S. Tsirkin | } |
312 | 9a3e12c8 | Michael S. Tsirkin | |
313 | 9a3e12c8 | Michael S. Tsirkin | qemu_put_buffer(f, dev->msix_table_page, n * MSIX_ENTRY_SIZE); |
314 | 5a1fc5e8 | Michael S. Tsirkin | qemu_put_buffer(f, dev->msix_table_page + MSIX_PAGE_PENDING, (n + 7) / 8); |
315 | 02eb84d0 | Michael S. Tsirkin | } |
316 | 02eb84d0 | Michael S. Tsirkin | |
317 | 02eb84d0 | Michael S. Tsirkin | /* Should be called after restoring the config space. */
|
318 | 02eb84d0 | Michael S. Tsirkin | void msix_load(PCIDevice *dev, QEMUFile *f)
|
319 | 02eb84d0 | Michael S. Tsirkin | { |
320 | 02eb84d0 | Michael S. Tsirkin | unsigned n = dev->msix_entries_nr;
|
321 | 02eb84d0 | Michael S. Tsirkin | |
322 | 98846d73 | Blue Swirl | if (!(dev->cap_present & QEMU_PCI_CAP_MSIX)) {
|
323 | 02eb84d0 | Michael S. Tsirkin | return;
|
324 | 98846d73 | Blue Swirl | } |
325 | 02eb84d0 | Michael S. Tsirkin | |
326 | 4bfd1712 | Michael S. Tsirkin | msix_free_irq_entries(dev); |
327 | 02eb84d0 | Michael S. Tsirkin | qemu_get_buffer(f, dev->msix_table_page, n * MSIX_ENTRY_SIZE); |
328 | 5a1fc5e8 | Michael S. Tsirkin | qemu_get_buffer(f, dev->msix_table_page + MSIX_PAGE_PENDING, (n + 7) / 8); |
329 | 02eb84d0 | Michael S. Tsirkin | } |
330 | 02eb84d0 | Michael S. Tsirkin | |
331 | 02eb84d0 | Michael S. Tsirkin | /* Does device support MSI-X? */
|
332 | 02eb84d0 | Michael S. Tsirkin | int msix_present(PCIDevice *dev)
|
333 | 02eb84d0 | Michael S. Tsirkin | { |
334 | 02eb84d0 | Michael S. Tsirkin | return dev->cap_present & QEMU_PCI_CAP_MSIX;
|
335 | 02eb84d0 | Michael S. Tsirkin | } |
336 | 02eb84d0 | Michael S. Tsirkin | |
337 | 02eb84d0 | Michael S. Tsirkin | /* Is MSI-X enabled? */
|
338 | 02eb84d0 | Michael S. Tsirkin | int msix_enabled(PCIDevice *dev)
|
339 | 02eb84d0 | Michael S. Tsirkin | { |
340 | 02eb84d0 | Michael S. Tsirkin | return (dev->cap_present & QEMU_PCI_CAP_MSIX) &&
|
341 | 2760952b | Michael S. Tsirkin | (dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] & |
342 | 02eb84d0 | Michael S. Tsirkin | MSIX_ENABLE_MASK); |
343 | 02eb84d0 | Michael S. Tsirkin | } |
344 | 02eb84d0 | Michael S. Tsirkin | |
345 | 02eb84d0 | Michael S. Tsirkin | /* Size of bar where MSI-X table resides, or 0 if MSI-X not supported. */
|
346 | 02eb84d0 | Michael S. Tsirkin | uint32_t msix_bar_size(PCIDevice *dev) |
347 | 02eb84d0 | Michael S. Tsirkin | { |
348 | 02eb84d0 | Michael S. Tsirkin | return (dev->cap_present & QEMU_PCI_CAP_MSIX) ?
|
349 | 02eb84d0 | Michael S. Tsirkin | dev->msix_bar_size : 0;
|
350 | 02eb84d0 | Michael S. Tsirkin | } |
351 | 02eb84d0 | Michael S. Tsirkin | |
352 | 02eb84d0 | Michael S. Tsirkin | /* Send an MSI-X message */
|
353 | 02eb84d0 | Michael S. Tsirkin | void msix_notify(PCIDevice *dev, unsigned vector) |
354 | 02eb84d0 | Michael S. Tsirkin | { |
355 | 02eb84d0 | Michael S. Tsirkin | uint8_t *table_entry = dev->msix_table_page + vector * MSIX_ENTRY_SIZE; |
356 | 02eb84d0 | Michael S. Tsirkin | uint64_t address; |
357 | 02eb84d0 | Michael S. Tsirkin | uint32_t data; |
358 | 02eb84d0 | Michael S. Tsirkin | |
359 | 02eb84d0 | Michael S. Tsirkin | if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector])
|
360 | 02eb84d0 | Michael S. Tsirkin | return;
|
361 | 02eb84d0 | Michael S. Tsirkin | if (msix_is_masked(dev, vector)) {
|
362 | 02eb84d0 | Michael S. Tsirkin | msix_set_pending(dev, vector); |
363 | 02eb84d0 | Michael S. Tsirkin | return;
|
364 | 02eb84d0 | Michael S. Tsirkin | } |
365 | 02eb84d0 | Michael S. Tsirkin | |
366 | 02eb84d0 | Michael S. Tsirkin | address = pci_get_long(table_entry + MSIX_MSG_UPPER_ADDR); |
367 | 02eb84d0 | Michael S. Tsirkin | address = (address << 32) | pci_get_long(table_entry + MSIX_MSG_ADDR);
|
368 | 02eb84d0 | Michael S. Tsirkin | data = pci_get_long(table_entry + MSIX_MSG_DATA); |
369 | 02eb84d0 | Michael S. Tsirkin | stl_phys(address, data); |
370 | 02eb84d0 | Michael S. Tsirkin | } |
371 | 02eb84d0 | Michael S. Tsirkin | |
372 | 02eb84d0 | Michael S. Tsirkin | void msix_reset(PCIDevice *dev)
|
373 | 02eb84d0 | Michael S. Tsirkin | { |
374 | 02eb84d0 | Michael S. Tsirkin | if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
|
375 | 02eb84d0 | Michael S. Tsirkin | return;
|
376 | 02eb84d0 | Michael S. Tsirkin | msix_free_irq_entries(dev); |
377 | 2760952b | Michael S. Tsirkin | dev->config[dev->msix_cap + MSIX_CONTROL_OFFSET] &= |
378 | 2760952b | Michael S. Tsirkin | ~dev->wmask[dev->msix_cap + MSIX_CONTROL_OFFSET]; |
379 | 5a1fc5e8 | Michael S. Tsirkin | memset(dev->msix_table_page, 0, MSIX_PAGE_SIZE);
|
380 | ae1be0bb | Michael S. Tsirkin | msix_mask_all(dev, dev->msix_entries_nr); |
381 | 02eb84d0 | Michael S. Tsirkin | } |
382 | 02eb84d0 | Michael S. Tsirkin | |
383 | 02eb84d0 | Michael S. Tsirkin | /* PCI spec suggests that devices make it possible for software to configure
|
384 | 02eb84d0 | Michael S. Tsirkin | * less vectors than supported by the device, but does not specify a standard
|
385 | 02eb84d0 | Michael S. Tsirkin | * mechanism for devices to do so.
|
386 | 02eb84d0 | Michael S. Tsirkin | *
|
387 | 02eb84d0 | Michael S. Tsirkin | * We support this by asking devices to declare vectors software is going to
|
388 | 02eb84d0 | Michael S. Tsirkin | * actually use, and checking this on the notification path. Devices that
|
389 | 02eb84d0 | Michael S. Tsirkin | * don't want to follow the spec suggestion can declare all vectors as used. */
|
390 | 02eb84d0 | Michael S. Tsirkin | |
391 | 02eb84d0 | Michael S. Tsirkin | /* Mark vector as used. */
|
392 | 02eb84d0 | Michael S. Tsirkin | int msix_vector_use(PCIDevice *dev, unsigned vector) |
393 | 02eb84d0 | Michael S. Tsirkin | { |
394 | 02eb84d0 | Michael S. Tsirkin | if (vector >= dev->msix_entries_nr)
|
395 | 02eb84d0 | Michael S. Tsirkin | return -EINVAL;
|
396 | 02eb84d0 | Michael S. Tsirkin | dev->msix_entry_used[vector]++; |
397 | 02eb84d0 | Michael S. Tsirkin | return 0; |
398 | 02eb84d0 | Michael S. Tsirkin | } |
399 | 02eb84d0 | Michael S. Tsirkin | |
400 | 02eb84d0 | Michael S. Tsirkin | /* Mark vector as unused. */
|
401 | 02eb84d0 | Michael S. Tsirkin | void msix_vector_unuse(PCIDevice *dev, unsigned vector) |
402 | 02eb84d0 | Michael S. Tsirkin | { |
403 | 98304c84 | Michael S. Tsirkin | if (vector >= dev->msix_entries_nr || !dev->msix_entry_used[vector]) {
|
404 | 98304c84 | Michael S. Tsirkin | return;
|
405 | 98304c84 | Michael S. Tsirkin | } |
406 | 98304c84 | Michael S. Tsirkin | if (--dev->msix_entry_used[vector]) {
|
407 | 98304c84 | Michael S. Tsirkin | return;
|
408 | 98304c84 | Michael S. Tsirkin | } |
409 | 98304c84 | Michael S. Tsirkin | msix_clr_pending(dev, vector); |
410 | 02eb84d0 | Michael S. Tsirkin | } |
411 | b5f28bca | Michael S. Tsirkin | |
412 | b5f28bca | Michael S. Tsirkin | void msix_unuse_all_vectors(PCIDevice *dev)
|
413 | b5f28bca | Michael S. Tsirkin | { |
414 | b5f28bca | Michael S. Tsirkin | if (!(dev->cap_present & QEMU_PCI_CAP_MSIX))
|
415 | b5f28bca | Michael S. Tsirkin | return;
|
416 | b5f28bca | Michael S. Tsirkin | msix_free_irq_entries(dev); |
417 | b5f28bca | Michael S. Tsirkin | } |