VirtualBox

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

Last change on this file since 87293 was 84827, checked in by vboxsync, 4 years ago

AMD IOMMU: bugref:9654 PDM interface changes for supplying bus:device:function for devices' initiating PCI interrupts and MSIs. [build fix]

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