VirtualBox

source: vbox/trunk/src/VBox/Devices/Bus/MsixCommon.cpp@ 84111

Last change on this file since 84111 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.1 KB
Line 
1/* $Id: MsixCommon.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * MSI-X support routines
4 */
5
6/*
7 * Copyright (C) 2010-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19#define LOG_GROUP LOG_GROUP_DEV_PCI
20#define PDMPCIDEV_INCLUDE_PRIVATE /* Hack to get pdmpcidevint.h included at the right point. */
21#include <VBox/pci.h>
22#include <VBox/msi.h>
23#include <VBox/vmm/pdmdev.h>
24#include <VBox/log.h>
25#include <VBox/vmm/mm.h>
26#include <VBox/AssertGuest.h>
27
28#include <iprt/assert.h>
29
30#include "MsiCommon.h"
31#include "PciInline.h"
32
33typedef struct
34{
35 uint32_t u32MsgAddressLo;
36 uint32_t u32MsgAddressHi;
37 uint32_t u32MsgData;
38 uint32_t u32VectorControl;
39} MsixTableRecord;
40AssertCompileSize(MsixTableRecord, VBOX_MSIX_ENTRY_SIZE);
41
42
43/** @todo use accessors so that raw PCI devices work correctly with MSI-X. */
44DECLINLINE(uint16_t) msixGetMessageControl(PPDMPCIDEV pDev)
45{
46 return PCIDevGetWord(pDev, pDev->Int.s.u8MsixCapOffset + VBOX_MSIX_CAP_MESSAGE_CONTROL);
47}
48
49DECLINLINE(bool) msixIsEnabled(PPDMPCIDEV pDev)
50{
51 return (msixGetMessageControl(pDev) & VBOX_PCI_MSIX_FLAGS_ENABLE) != 0;
52}
53
54DECLINLINE(bool) msixIsMasked(PPDMPCIDEV pDev)
55{
56 return (msixGetMessageControl(pDev) & VBOX_PCI_MSIX_FLAGS_FUNCMASK) != 0;
57}
58
59#ifdef IN_RING3
60DECLINLINE(uint16_t) msixTableSize(PPDMPCIDEV pDev)
61{
62 return (msixGetMessageControl(pDev) & 0x3ff) + 1;
63}
64#endif
65
66DECLINLINE(uint8_t *) msixGetPageOffset(PPDMPCIDEV pDev, uint32_t off)
67{
68 return &pDev->abMsixState[off];
69}
70
71DECLINLINE(MsixTableRecord *) msixGetVectorRecord(PPDMPCIDEV pDev, uint32_t iVector)
72{
73 return (MsixTableRecord *)msixGetPageOffset(pDev, iVector * VBOX_MSIX_ENTRY_SIZE);
74}
75
76DECLINLINE(RTGCPHYS) msixGetMsiAddress(PPDMPCIDEV pDev, uint32_t iVector)
77{
78 MsixTableRecord *pRec = msixGetVectorRecord(pDev, iVector);
79 return RT_MAKE_U64(pRec->u32MsgAddressLo & ~UINT32_C(0x3), pRec->u32MsgAddressHi);
80}
81
82DECLINLINE(uint32_t) msixGetMsiData(PPDMPCIDEV pDev, uint32_t iVector)
83{
84 return msixGetVectorRecord(pDev, iVector)->u32MsgData;
85}
86
87DECLINLINE(uint32_t) msixIsVectorMasked(PPDMPCIDEV pDev, uint32_t iVector)
88{
89 return (msixGetVectorRecord(pDev, iVector)->u32VectorControl & 0x1) != 0;
90}
91
92DECLINLINE(uint8_t *) msixPendingByte(PPDMPCIDEV pDev, uint32_t iVector)
93{
94 return msixGetPageOffset(pDev, pDev->Int.s.offMsixPba + iVector / 8);
95}
96
97DECLINLINE(void) msixSetPending(PPDMPCIDEV pDev, uint32_t iVector)
98{
99 *msixPendingByte(pDev, iVector) |= (1 << (iVector & 0x7));
100}
101
102DECLINLINE(void) msixClearPending(PPDMPCIDEV pDev, uint32_t iVector)
103{
104 *msixPendingByte(pDev, iVector) &= ~(1 << (iVector & 0x7));
105}
106
107#ifdef IN_RING3
108
109DECLINLINE(bool) msixR3IsPending(PPDMPCIDEV pDev, uint32_t iVector)
110{
111 return (*msixPendingByte(pDev, iVector) & (1 << (iVector & 0x7))) != 0;
112}
113
114static void msixR3CheckPendingVector(PPDMDEVINS pDevIns, PCPDMPCIHLP pPciHlp, PPDMPCIDEV pDev, uint32_t iVector)
115{
116 if (msixR3IsPending(pDev, iVector) && !msixIsVectorMasked(pDev, iVector))
117 MsixNotify(pDevIns, pPciHlp, pDev, iVector, 1 /* iLevel */, 0 /*uTagSrc*/);
118}
119
120/**
121 * @callback_method_impl{FNIOMMMIONEWREAD}
122 */
123static DECLCALLBACK(VBOXSTRICTRC) msixR3MMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void *pv, unsigned cb)
124{
125 PPDMPCIDEV pPciDev = (PPDMPCIDEV)pvUser;
126 RT_NOREF(pDevIns);
127
128 /* Validate IOM behaviour. */
129 Assert(cb == 4);
130 Assert((off & 3) == 0);
131
132 /* Do the read if it's within the MSI state. */
133 ASSERT_GUEST_MSG_RETURN(off + cb <= pPciDev->Int.s.cbMsixRegion, ("Out of bounds access for the MSI-X region\n"),
134 VINF_IOM_MMIO_UNUSED_FF);
135 *(uint32_t *)pv = *(uint32_t *)&pPciDev->abMsixState[off];
136
137 LogFlowFunc(("off=%RGp cb=%d -> %#010RX32\n", off, cb, *(uint32_t *)pv));
138 return VINF_SUCCESS;
139}
140
141/**
142 * @callback_method_impl{FNIOMMMIONEWWRITE}
143 */
144static DECLCALLBACK(VBOXSTRICTRC) msixR3MMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS off, void const *pv, unsigned cb)
145{
146 PPDMPCIDEV pPciDev = (PPDMPCIDEV)pvUser;
147 LogFlowFunc(("off=%RGp cb=%d %#010RX32\n", off, cb, *(uint32_t *)pv));
148
149 /* Validate IOM behaviour. */
150 Assert(cb == 4);
151 Assert((off & 3) == 0);
152
153 /* Do the write if it's within the MSI state. */
154 ASSERT_GUEST_MSG_RETURN(off + cb <= pPciDev->Int.s.offMsixPba, ("Trying to write to PBA\n"),
155 VINF_SUCCESS);
156 *(uint32_t *)&pPciDev->abMsixState[off] = *(uint32_t *)pv;
157
158 /* (See MsixR3Init the setting up of pvPciBusPtrR3.) */
159 msixR3CheckPendingVector(pDevIns, (PCPDMPCIHLP)pPciDev->Int.s.pvPciBusPtrR3, pPciDev, off / VBOX_MSIX_ENTRY_SIZE);
160 return VINF_SUCCESS;
161}
162
163/**
164 * Initalizes MSI-X support for the given PCI device.
165 */
166int MsixR3Init(PCPDMPCIHLP pPciHlp, PPDMPCIDEV pDev, PPDMMSIREG pMsiReg)
167{
168 if (pMsiReg->cMsixVectors == 0)
169 return VINF_SUCCESS;
170
171 /* We cannot init MSI-X on raw devices yet. */
172 Assert(!pciDevIsPassthrough(pDev));
173
174 uint16_t cVectors = pMsiReg->cMsixVectors;
175 uint8_t iCapOffset = pMsiReg->iMsixCapOffset;
176 uint8_t iNextOffset = pMsiReg->iMsixNextOffset;
177 uint8_t iBar = pMsiReg->iMsixBar;
178
179 AssertMsgReturn(cVectors <= VBOX_MSIX_MAX_ENTRIES, ("Too many MSI-X vectors: %d\n", cVectors), VERR_TOO_MUCH_DATA);
180 AssertMsgReturn(iBar <= 5, ("Using wrong BAR for MSI-X: %d\n", iBar), VERR_INVALID_PARAMETER);
181 Assert(iCapOffset != 0 && iCapOffset < 0xff && iNextOffset < 0xff);
182
183 uint16_t cbPba = cVectors / 8;
184 if (cVectors % 8)
185 cbPba++;
186 uint16_t cbMsixRegion = RT_ALIGN_T(cVectors * sizeof(MsixTableRecord) + cbPba, _4K, uint16_t);
187 AssertLogRelMsgReturn(cbMsixRegion <= pDev->cbMsixState,
188 ("%#x vs %#x (%s)\n", cbMsixRegion, pDev->cbMsixState, pDev->pszNameR3),
189 VERR_MISMATCH);
190
191 /* If device is passthrough, BAR is registered using common mechanism. */
192 if (!pciDevIsPassthrough(pDev))
193 {
194 /** @todo r=bird: This used to be IOMMMIO_FLAGS_READ_PASSTHRU |
195 * IOMMMIO_FLAGS_WRITE_PASSTHRU with the callbacks asserting and
196 * returning VERR_INTERNAL_ERROR on non-dword reads. That is of
197 * course certifiable insane behaviour. So, instead I've changed it
198 * so the callbacks only see dword reads and writes. I'm not at all
199 * sure about the read-missing behaviour, but it seems like a good
200 * idea for now. */
201 /** @todo r=bird: Shouldn't we at least handle writes in ring-0? */
202 int rc = PDMDevHlpPCIIORegionCreateMmio(pDev->Int.s.CTX_SUFF(pDevIns), iBar, cbMsixRegion, PCI_ADDRESS_SPACE_MEM,
203 msixR3MMIOWrite, msixR3MMIORead, pDev,
204 IOMMMIO_FLAGS_READ_DWORD | IOMMMIO_FLAGS_WRITE_DWORD_READ_MISSING,
205 "MSI-X tables", &pDev->Int.s.hMmioMsix);
206 AssertRCReturn(rc, rc);
207 }
208
209 uint16_t offTable = 0;
210 uint16_t offPBA = cVectors * sizeof(MsixTableRecord);
211
212 pDev->Int.s.u8MsixCapOffset = iCapOffset;
213 pDev->Int.s.u8MsixCapSize = VBOX_MSIX_CAP_SIZE;
214 pDev->Int.s.cbMsixRegion = cbMsixRegion;
215 pDev->Int.s.offMsixPba = offPBA;
216
217 /* R3 PCI helper */
218 pDev->Int.s.pvPciBusPtrR3 = pPciHlp;
219
220 PCIDevSetByte(pDev, iCapOffset + 0, VBOX_PCI_CAP_ID_MSIX);
221 PCIDevSetByte(pDev, iCapOffset + 1, iNextOffset); /* next */
222 PCIDevSetWord(pDev, iCapOffset + VBOX_MSIX_CAP_MESSAGE_CONTROL, cVectors - 1);
223
224 PCIDevSetDWord(pDev, iCapOffset + VBOX_MSIX_TABLE_BIROFFSET, offTable | iBar);
225 PCIDevSetDWord(pDev, iCapOffset + VBOX_MSIX_PBA_BIROFFSET, offPBA | iBar);
226
227 pciDevSetMsixCapable(pDev);
228
229 return VINF_SUCCESS;
230}
231
232#endif /* IN_RING3 */
233
234/**
235 * Checks if MSI-X is enabled for the tiven PCI device.
236 *
237 * (Must use MSIXNotify() for notifications when true.)
238 */
239bool MsixIsEnabled(PPDMPCIDEV pDev)
240{
241 return pciDevIsMsixCapable(pDev) && msixIsEnabled(pDev);
242}
243
244/**
245 * Device notification (aka interrupt).
246 */
247void MsixNotify(PPDMDEVINS pDevIns, PCPDMPCIHLP pPciHlp, PPDMPCIDEV pDev, int iVector, int iLevel, uint32_t uTagSrc)
248{
249 AssertMsg(msixIsEnabled(pDev), ("Must be enabled to use that"));
250
251 Assert(pPciHlp->pfnIoApicSendMsi != NULL);
252
253 /* We only trigger MSI-X on level up */
254 if ((iLevel & PDM_IRQ_LEVEL_HIGH) == 0)
255 {
256 return;
257 }
258
259 // if this vector is somehow disabled
260 if (msixIsMasked(pDev) || msixIsVectorMasked(pDev, iVector))
261 {
262 // mark pending bit
263 msixSetPending(pDev, iVector);
264 return;
265 }
266
267 // clear pending bit
268 msixClearPending(pDev, iVector);
269
270 RTGCPHYS GCAddr = msixGetMsiAddress(pDev, iVector);
271 uint32_t u32Value = msixGetMsiData(pDev, iVector);
272
273 pPciHlp->pfnIoApicSendMsi(pDevIns, GCAddr, u32Value, uTagSrc);
274}
275
276#ifdef IN_RING3
277
278DECLINLINE(bool) msixR3BitJustCleared(uint32_t uOldValue, uint32_t uNewValue, uint32_t uMask)
279{
280 return !!(uOldValue & uMask) && !(uNewValue & uMask);
281}
282
283
284static void msixR3CheckPendingVectors(PPDMDEVINS pDevIns, PCPDMPCIHLP pPciHlp, PPDMPCIDEV pDev)
285{
286 for (uint32_t i = 0; i < msixTableSize(pDev); i++)
287 msixR3CheckPendingVector(pDevIns, pPciHlp, pDev, i);
288}
289
290/**
291 * PCI config space accessors for MSI-X.
292 */
293void MsixR3PciConfigWrite(PPDMDEVINS pDevIns, PCPDMPCIHLP pPciHlp, PPDMPCIDEV pDev, uint32_t u32Address, uint32_t val, unsigned len)
294{
295 int32_t iOff = u32Address - pDev->Int.s.u8MsixCapOffset;
296 Assert(iOff >= 0 && (pciDevIsMsixCapable(pDev) && iOff < pDev->Int.s.u8MsixCapSize));
297
298 Log2(("MsixR3PciConfigWrite: %d <- %x (%d)\n", iOff, val, len));
299
300 uint32_t uAddr = u32Address;
301 uint8_t u8NewVal;
302 bool fJustEnabled = false;
303
304 for (uint32_t i = 0; i < len; i++)
305 {
306 uint32_t reg = i + iOff;
307 uint8_t u8Val = (uint8_t)val;
308 switch (reg)
309 {
310 case 0: /* Capability ID, ro */
311 case 1: /* Next pointer, ro */
312 break;
313 case VBOX_MSIX_CAP_MESSAGE_CONTROL:
314 /* don't change read-only bits: 0-7 */
315 break;
316 case VBOX_MSIX_CAP_MESSAGE_CONTROL + 1:
317 {
318 /* don't change read-only bits 8-13 */
319 u8NewVal = (u8Val & UINT8_C(~0x3f)) | (pDev->abConfig[uAddr] & UINT8_C(0x3f));
320 /* If just enabled globally - check pending vectors */
321 fJustEnabled |= msixR3BitJustCleared(pDev->abConfig[uAddr], u8NewVal, VBOX_PCI_MSIX_FLAGS_ENABLE >> 8);
322 fJustEnabled |= msixR3BitJustCleared(pDev->abConfig[uAddr], u8NewVal, VBOX_PCI_MSIX_FLAGS_FUNCMASK >> 8);
323 pDev->abConfig[uAddr] = u8NewVal;
324 break;
325 }
326 default:
327 /* other fields read-only too */
328 break;
329 }
330 uAddr++;
331 val >>= 8;
332 }
333
334 if (fJustEnabled)
335 msixR3CheckPendingVectors(pDevIns, pPciHlp, pDev);
336}
337
338#endif /* IN_RING3 */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette