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