VirtualBox

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

最後變更 在這個檔案從39091是 39091,由 vboxsync 提交於 13 年 前

More parameter warning fixes; made PciIch9 check the saved state version.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 10.9 KB
 
1/* $Id: MsixCommon.cpp 39091 2011-10-24 13:58:22Z vboxsync $ */
2/** @file
3 * MSI-X support routines
4 */
5
6/*
7 * Copyright (C) 2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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#define LOG_GROUP LOG_GROUP_DEV_PCI
18/* Hack to get PCIDEVICEINT declare at the right point - include "PCIInternal.h". */
19#define PCI_INCLUDE_PRIVATE
20#include <VBox/pci.h>
21#include <VBox/msi.h>
22#include <VBox/vmm/pdmdev.h>
23#include <VBox/log.h>
24#include <VBox/vmm/mm.h>
25
26#include <iprt/assert.h>
27
28#include "MsiCommon.h"
29
30#pragma pack(1)
31typedef struct
32{
33 uint32_t u32MsgAddressLo;
34 uint32_t u32MsgAddressHi;
35 uint32_t u32MsgData;
36 uint32_t u32VectorControl;
37} MsixTableRecord;
38AssertCompileSize(MsixTableRecord, VBOX_MSIX_ENTRY_SIZE);
39#pragma pack()
40
41/** @todo: use accessors so that raw PCI devices work correctly with MSI-X. */
42DECLINLINE(uint16_t) msixGetMessageControl(PPCIDEVICE pDev)
43{
44 return PCIDevGetWord(pDev, pDev->Int.s.u8MsixCapOffset + VBOX_MSIX_CAP_MESSAGE_CONTROL);
45}
46
47DECLINLINE(bool) msixIsEnabled(PPCIDEVICE pDev)
48{
49 return (msixGetMessageControl(pDev) & VBOX_PCI_MSIX_FLAGS_ENABLE) != 0;
50}
51
52DECLINLINE(bool) msixIsMasked(PPCIDEVICE pDev)
53{
54 return (msixGetMessageControl(pDev) & VBOX_PCI_MSIX_FLAGS_FUNCMASK) != 0;
55}
56
57DECLINLINE(uint16_t) msixTableSize(PPCIDEVICE pDev)
58{
59 return (msixGetMessageControl(pDev) & 0x3ff) + 1;
60}
61
62DECLINLINE(uint8_t*) msixGetPageOffset(PPCIDEVICE pDev, uint32_t off)
63{
64 return (uint8_t*)pDev->Int.s.CTX_SUFF(pMsixPage) + off;
65}
66
67DECLINLINE(MsixTableRecord*) msixGetVectorRecord(PPCIDEVICE pDev, uint32_t iVector)
68{
69 return (MsixTableRecord*)msixGetPageOffset(pDev, iVector * VBOX_MSIX_ENTRY_SIZE);
70}
71
72DECLINLINE(RTGCPHYS) msixGetMsiAddress(PPCIDEVICE pDev, uint32_t iVector)
73{
74 MsixTableRecord* pRec = msixGetVectorRecord(pDev, iVector);
75 return RT_MAKE_U64(pRec->u32MsgAddressLo & ~UINT32_C(0x3), pRec->u32MsgAddressHi);
76}
77
78DECLINLINE(uint32_t) msixGetMsiData(PPCIDEVICE pDev, uint32_t iVector)
79{
80 return msixGetVectorRecord(pDev, iVector)->u32MsgData;
81}
82
83DECLINLINE(uint32_t) msixIsVectorMasked(PPCIDEVICE pDev, uint32_t iVector)
84{
85 return (msixGetVectorRecord(pDev, iVector)->u32VectorControl & 0x1) != 0;
86}
87
88DECLINLINE(uint8_t*) msixPendingByte(PPCIDEVICE pDev, uint32_t iVector)
89{
90 return msixGetPageOffset(pDev, 0x800 + iVector / 8);
91}
92
93DECLINLINE(void) msixSetPending(PPCIDEVICE pDev, uint32_t iVector)
94{
95 *msixPendingByte(pDev, iVector) |= (1 << (iVector & 0x7));
96}
97
98DECLINLINE(void) msixClearPending(PPCIDEVICE pDev, uint32_t iVector)
99{
100 *msixPendingByte(pDev, iVector) &= ~(1 << (iVector & 0x7));
101}
102
103DECLINLINE(bool) msixIsPending(PPCIDEVICE pDev, uint32_t iVector)
104{
105 return (*msixPendingByte(pDev, iVector) & (1 << (iVector & 0x7))) != 0;
106}
107
108static void msixCheckPendingVector(PPDMDEVINS pDevIns, PCPDMPCIHLP pPciHlp, PPCIDEVICE pDev, uint32_t iVector)
109{
110 if (msixIsPending(pDev, iVector) && !msixIsVectorMasked(pDev, iVector))
111 MsixNotify(pDevIns, pPciHlp, pDev, iVector, 1 /* iLevel */);
112}
113
114#ifdef IN_RING3
115
116PDMBOTHCBDECL(int) msixMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
117{
118 /// @todo qword accesses?
119 NOREF(pDevIns);
120 AssertMsgReturn(cb == 4,
121 ("MSI-X must be accessed with 4-byte reads"),
122 VERR_INTERNAL_ERROR);
123
124 uint32_t off = (uint32_t)(GCPhysAddr & 0xfff);
125 PPCIDEVICE pPciDev = (PPCIDEVICE)pvUser;
126
127 *(uint32_t*)pv = *(uint32_t*)msixGetPageOffset(pPciDev, off);
128
129 return VINF_SUCCESS;
130}
131
132PDMBOTHCBDECL(int) msixMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
133{
134 /// @todo: qword accesses?
135 AssertMsgReturn(cb == 4,
136 ("MSI-X must be accessed with 4-byte reads"),
137 VERR_INTERNAL_ERROR);
138 PPCIDEVICE pPciDev = (PPCIDEVICE)pvUser;
139
140 uint32_t off = (uint32_t)(GCPhysAddr & 0xfff);
141
142 AssertMsgReturn(off < 0x800, ("Trying to write to PBA\n"), VINF_SUCCESS);
143
144 *(uint32_t*)msixGetPageOffset(pPciDev, off) = *(uint32_t*)pv;
145
146 msixCheckPendingVector(pDevIns, (PCPDMPCIHLP)pPciDev->Int.s.pPciBusPtrR3, pPciDev, off / VBOX_MSIX_ENTRY_SIZE);
147
148 return VINF_SUCCESS;
149}
150
151static DECLCALLBACK(int) msixMap (PPCIDEVICE pPciDev, int iRegion,
152 RTGCPHYS GCPhysAddress, uint32_t cb,
153 PCIADDRESSSPACE enmType)
154{
155 Assert(enmType == PCI_ADDRESS_SPACE_MEM);
156 NOREF(iRegion); NOREF(enmType);
157
158 int rc = PDMDevHlpMMIORegister(pPciDev->pDevIns, GCPhysAddress, cb, pPciDev,
159 msixMMIOWrite, msixMMIORead, NULL, "MSI-X tables");
160
161 if (RT_FAILURE(rc))
162 return rc;
163
164 return VINF_SUCCESS;
165}
166
167int MsixInit(PCPDMPCIHLP pPciHlp, PPCIDEVICE 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 if (cVectors > VBOX_MSIX_MAX_ENTRIES)
181 {
182 AssertMsgFailed(("Too many MSI-X vectors: %d\n", cVectors));
183 return VERR_TOO_MUCH_DATA;
184 }
185
186 if (iBar > 5)
187 {
188 AssertMsgFailed(("Using wrong BAR for MSI-X: %d\n", iBar));
189 return VERR_INVALID_PARAMETER;
190 }
191
192 Assert(iCapOffset != 0 && iCapOffset < 0xff && iNextOffset < 0xff);
193
194 int rc = VINF_SUCCESS;
195
196 /* If device is passthrough, BAR is registered using common mechanism. */
197 if (!pciDevIsPassthrough(pDev))
198 {
199 rc = PDMDevHlpPCIIORegionRegister (pDev->pDevIns, iBar, 0x1000, PCI_ADDRESS_SPACE_MEM, msixMap);
200 if (RT_FAILURE (rc))
201 return rc;
202 }
203
204 pDev->Int.s.u8MsixCapOffset = iCapOffset;
205 pDev->Int.s.u8MsixCapSize = VBOX_MSIX_CAP_SIZE;
206 PVM pVM = PDMDevHlpGetVM(pDev->pDevIns);
207
208 pDev->Int.s.pMsixPageR3 = NULL;
209
210 rc = MMHyperAlloc(pVM, 0x1000, 1, MM_TAG_PDM_DEVICE_USER, (void **)&pDev->Int.s.pMsixPageR3);
211 if (RT_FAILURE(rc) || (pDev->Int.s.pMsixPageR3 == NULL))
212 return VERR_NO_VM_MEMORY;
213 RT_BZERO(pDev->Int.s.pMsixPageR3, 0x1000);
214 pDev->Int.s.pMsixPageR0 = MMHyperR3ToR0(pVM, pDev->Int.s.pMsixPageR3);
215 pDev->Int.s.pMsixPageRC = MMHyperR3ToRC(pVM, pDev->Int.s.pMsixPageR3);
216
217 /* R3 PCI helper */
218 pDev->Int.s.pPciBusPtrR3 = 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 uint32_t offTable = 0, offPBA = 0x800;
225
226 PCIDevSetDWord(pDev, iCapOffset + VBOX_MSIX_TABLE_BIROFFSET, offTable | iBar);
227 PCIDevSetDWord(pDev, iCapOffset + VBOX_MSIX_PBA_BIROFFSET, offPBA | iBar);
228
229 pciDevSetMsixCapable(pDev);
230
231 return VINF_SUCCESS;
232}
233#endif
234
235bool MsixIsEnabled(PPCIDEVICE pDev)
236{
237 return pciDevIsMsixCapable(pDev) && msixIsEnabled(pDev);
238}
239
240void MsixNotify(PPDMDEVINS pDevIns, PCPDMPCIHLP pPciHlp, PPCIDEVICE pDev, int iVector, int iLevel)
241{
242 AssertMsg(msixIsEnabled(pDev), ("Must be enabled to use that"));
243
244 Assert(pPciHlp->pfnIoApicSendMsi != NULL);
245
246 /* We only trigger MSI-X on level up */
247 if ((iLevel & PDM_IRQ_LEVEL_HIGH) == 0)
248 {
249 return;
250 }
251
252 // if this vector is somehow disabled
253 if (msixIsMasked(pDev) || msixIsVectorMasked(pDev, iVector))
254 {
255 // mark pending bit
256 msixSetPending(pDev, iVector);
257 return;
258 }
259
260 // clear pending bit
261 msixClearPending(pDev, iVector);
262
263 RTGCPHYS GCAddr = msixGetMsiAddress(pDev, iVector);
264 uint32_t u32Value = msixGetMsiData(pDev, iVector);
265
266 pPciHlp->pfnIoApicSendMsi(pDevIns, GCAddr, u32Value);
267}
268
269DECLINLINE(bool) msixBitJustCleared(uint32_t uOldValue,
270 uint32_t uNewValue,
271 uint32_t uMask)
272{
273 return (!!(uOldValue & uMask) && !(uNewValue & uMask));
274}
275
276static void msixCheckPendingVectors(PPDMDEVINS pDevIns, PCPDMPCIHLP pPciHlp, PPCIDEVICE pDev)
277{
278 for (uint32_t i = 0; i < msixTableSize(pDev); i++)
279 msixCheckPendingVector(pDevIns, pPciHlp, pDev, i);
280}
281
282
283void MsixPciConfigWrite(PPDMDEVINS pDevIns, PCPDMPCIHLP pPciHlp, PPCIDEVICE pDev, uint32_t u32Address, uint32_t val, unsigned len)
284{
285 int32_t iOff = u32Address - pDev->Int.s.u8MsixCapOffset;
286 Assert(iOff >= 0 && (pciDevIsMsixCapable(pDev) && iOff < pDev->Int.s.u8MsixCapSize));
287
288 Log2(("MsixPciConfigWrite: %d <- %x (%d)\n", iOff, val, len));
289
290 uint32_t uAddr = u32Address;
291 uint8_t u8NewVal;
292 bool fJustEnabled = false;
293
294 for (uint32_t i = 0; i < len; i++)
295 {
296 uint32_t reg = i + iOff;
297 uint8_t u8Val = (uint8_t)val;
298 switch (reg)
299 {
300 case 0: /* Capability ID, ro */
301 case 1: /* Next pointer, ro */
302 break;
303 case VBOX_MSIX_CAP_MESSAGE_CONTROL:
304 /* don't change read-only bits: 0-7 */
305 break;
306 case VBOX_MSIX_CAP_MESSAGE_CONTROL + 1:
307 {
308 /* don't change read-only bits 8-13 */
309 u8NewVal = (u8Val & UINT8_C(~0x3f)) | (pDev->config[uAddr] & UINT8_C(0x3f));
310 /* If just enabled globally - check pending vectors */
311 fJustEnabled |= msixBitJustCleared(pDev->config[uAddr], u8NewVal, VBOX_PCI_MSIX_FLAGS_ENABLE >> 8);
312 fJustEnabled |= msixBitJustCleared(pDev->config[uAddr], u8NewVal, VBOX_PCI_MSIX_FLAGS_FUNCMASK >> 8);
313 pDev->config[uAddr] = u8NewVal;
314 break;
315 }
316 default:
317 /* other fields read-only too */
318 break;
319 }
320 uAddr++;
321 val >>= 8;
322 }
323
324 if (fJustEnabled)
325 msixCheckPendingVectors(pDevIns, pPciHlp, pDev);
326}
327
328
329uint32_t MsixPciConfigRead(PPDMDEVINS pDevIns, PPCIDEVICE pDev, uint32_t u32Address, unsigned len)
330{
331 int32_t iOff = u32Address - pDev->Int.s.u8MsixCapOffset;
332 NOREF(pDevIns);
333
334 Assert(iOff >= 0 && (pciDevIsMsixCapable(pDev) && iOff < pDev->Int.s.u8MsixCapSize));
335 uint32_t rv = 0;
336
337 switch (len)
338 {
339 case 1:
340 rv = PCIDevGetByte(pDev, u32Address);
341 break;
342 case 2:
343 rv = PCIDevGetWord(pDev, u32Address);
344 break;
345 case 4:
346 rv = PCIDevGetDWord(pDev, u32Address);
347 break;
348 default:
349 Assert(false);
350 }
351
352 Log2(("MsixPciConfigRead: %d (%d) -> %x\n", iOff, len, rv));
353
354 return rv;
355}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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