VirtualBox

source: vbox/trunk/src/VBox/Devices/Samples/DevPlayground.cpp@ 65291

Last change on this file since 65291 was 65291, checked in by vboxsync, 8 years ago

DevPlayground: the big mem BARs need to be prefetchable in the real world

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
1/* $Id: DevPlayground.cpp 65291 2017-01-13 18:00:28Z vboxsync $ */
2/** @file
3 * DevPlayground - Device for making PDM/PCI/... experiments.
4 *
5 * This device uses big PCI BAR64 resources, which needs the ICH9 chipset.
6 * The device works without any PCI config (because the default setup with the
7 * ICH9 chipset doesn't have anything at bus=0, device=0, function=0.
8 *
9 * To enable this device for a particular VM:
10 * VBoxManage setextradata vmname VBoxInternal/PDM/Devices/playground/Path .../obj/VBoxSampleDevice/VBoxSampleDevice
11 * VBoxManage setextradata vmname VBoxInternal/Devices/playground/0/Config/Whatever1 0
12 */
13
14/*
15 * Copyright (C) 2009-2016 Oracle Corporation
16 *
17 * This file is part of VirtualBox Open Source Edition (OSE), as
18 * available from http://www.215389.xyz. This file is free software;
19 * you can redistribute it and/or modify it under the terms of the GNU
20 * General Public License (GPL) as published by the Free Software
21 * Foundation, in version 2 as it comes in the "COPYING" file of the
22 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
23 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
24 */
25
26
27/*********************************************************************************************************************************
28* Header Files *
29*********************************************************************************************************************************/
30#define LOG_GROUP LOG_GROUP_MISC
31#include <VBox/vmm/pdmdev.h>
32#include <VBox/version.h>
33#include <VBox/err.h>
34#include <VBox/log.h>
35
36#include <iprt/assert.h>
37
38
39/*********************************************************************************************************************************
40* Structures and Typedefs *
41*********************************************************************************************************************************/
42/**
43 * Playground device per function (sub-device) data.
44 */
45typedef struct VBOXPLAYGROUNDDEVICEFUNCTION
46{
47 /** The PCI devices. */
48 PDMPCIDEV PciDev;
49 /** The function number. */
50 uint8_t iFun;
51 /** Device function name. */
52 char szName[31];
53} VBOXPLAYGROUNDDEVICEFUNCTION;
54/** Pointer to a PCI function of the playground device. */
55typedef VBOXPLAYGROUNDDEVICEFUNCTION *PVBOXPLAYGROUNDDEVICEFUNCTION;
56
57/**
58 * Playground device instance data.
59 */
60typedef struct VBOXPLAYGROUNDDEVICE
61{
62 /** PCI device functions. */
63 VBOXPLAYGROUNDDEVICEFUNCTION aPciFuns[8];
64} VBOXPLAYGROUNDDEVICE;
65/** Pointer to the instance data of a playground device instance. */
66typedef VBOXPLAYGROUNDDEVICE *PVBOXPLAYGROUNDDEVICE;
67
68
69/*********************************************************************************************************************************
70* Device Functions *
71*********************************************************************************************************************************/
72
73PDMBOTHCBDECL(int) devPlaygroundMMIORead(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void *pv, unsigned cb)
74{
75 NOREF(pDevIns);
76 NOREF(pvUser);
77 NOREF(GCPhysAddr);
78 NOREF(pv);
79 NOREF(cb);
80 return VINF_SUCCESS;
81}
82
83
84PDMBOTHCBDECL(int) devPlaygroundMMIOWrite(PPDMDEVINS pDevIns, void *pvUser, RTGCPHYS GCPhysAddr, void const *pv, unsigned cb)
85{
86 NOREF(pDevIns);
87 NOREF(pvUser);
88 NOREF(GCPhysAddr);
89 NOREF(pv);
90 NOREF(cb);
91 return VINF_SUCCESS;
92}
93
94
95/**
96 * @callback_method_impl{FNPCIIOREGIONMAP}
97 */
98static DECLCALLBACK(int) devPlaygroundMap(PPDMDEVINS pDevIns, PPDMPCIDEV pPciDev, uint32_t iRegion,
99 RTGCPHYS GCPhysAddress, RTGCPHYS cb, PCIADDRESSSPACE enmType)
100{
101 RT_NOREF(pPciDev, enmType, cb);
102
103 switch (iRegion)
104 {
105 case 0:
106 case 2:
107 Assert(enmType == (PCIADDRESSSPACE)(PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64));
108 if (GCPhysAddress == NIL_RTGCPHYS)
109 return VINF_SUCCESS; /* We ignore the unmap notification. */
110 return PDMDevHlpMMIOExMap(pDevIns, pPciDev, iRegion, GCPhysAddress);
111
112 default:
113 /* We should never get here */
114 AssertMsgFailedReturn(("Invalid PCI region param in map callback"), VERR_INTERNAL_ERROR);
115 }
116}
117
118
119/**
120 * @interface_method_impl{PDMDEVREG,pfnDestruct}
121 */
122static DECLCALLBACK(int) devPlaygroundDestruct(PPDMDEVINS pDevIns)
123{
124 /*
125 * Check the versions here as well since the destructor is *always* called.
126 */
127 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
128
129 return VINF_SUCCESS;
130}
131
132
133/**
134 * @interface_method_impl{PDMDEVREG,pfnConstruct}
135 */
136static DECLCALLBACK(int) devPlaygroundConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
137{
138 RT_NOREF(iInstance, pCfg);
139
140 /*
141 * Check that the device instance and device helper structures are compatible.
142 */
143 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
144
145 /*
146 * Initialize the instance data so that the destructor won't mess up.
147 */
148 PVBOXPLAYGROUNDDEVICE pThis = PDMINS_2_DATA(pDevIns, PVBOXPLAYGROUNDDEVICE);
149
150 /*
151 * Validate and read the configuration.
152 */
153 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "Whatever1|Whatever2", "");
154
155 /*
156 * PCI device setup.
157 */
158 uint32_t iPciDevNo = PDMPCIDEVREG_DEV_NO_FIRST_UNUSED;
159 for (uint32_t iPciFun = 0; iPciFun < RT_ELEMENTS(pThis->aPciFuns); iPciFun++)
160 {
161 PVBOXPLAYGROUNDDEVICEFUNCTION pFun = &pThis->aPciFuns[iPciFun];
162 RTStrPrintf(pFun->szName, sizeof(pThis->aPciFuns[iPciFun].PciDev), "playground%u", iPciFun);
163 pFun->iFun = iPciFun;
164
165 PCIDevSetVendorId( &pFun->PciDev, 0x80ee);
166 PCIDevSetDeviceId( &pFun->PciDev, 0xde4e);
167 PCIDevSetClassBase(&pFun->PciDev, 0x07); /* communications device */
168 PCIDevSetClassSub( &pFun->PciDev, 0x80); /* other communications device */
169 if (iPciFun == 0) /* only for the primary function */
170 PCIDevSetHeaderType(&pFun->PciDev, 0x80); /* normal, multifunction device */
171
172 int rc = PDMDevHlpPCIRegisterEx(pDevIns, &pFun->PciDev, iPciFun, 0 /*fFlags*/, iPciDevNo, iPciFun,
173 pThis->aPciFuns[iPciFun].szName);
174 AssertLogRelRCReturn(rc, rc);
175
176 /* First region. */
177 RTGCPHYS const cbFirst = iPciFun == 0 ? 8*_1G64 : iPciFun * _4K;
178 rc = PDMDevHlpPCIIORegionRegisterEx(pDevIns, &pFun->PciDev, 0, cbFirst,
179 (PCIADDRESSSPACE)( PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64
180 | (iPciFun == 0 ? PCI_ADDRESS_SPACE_MEM_PREFETCH : 0)),
181 devPlaygroundMap);
182 AssertLogRelRCReturn(rc, rc);
183 rc = PDMDevHlpMMIOExPreRegister(pDevIns, &pFun->PciDev, 0, cbFirst,
184 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU, "PG-BAR0",
185 NULL /*pvUser*/, devPlaygroundMMIOWrite, devPlaygroundMMIORead, NULL /*pfnFill*/,
186 NIL_RTR0PTR /*pvUserR0*/, NULL /*pszWriteR0*/, NULL /*pszReadR0*/, NULL /*pszFillR0*/,
187 NIL_RTRCPTR /*pvUserRC*/, NULL /*pszWriteRC*/, NULL /*pszReadRC*/, NULL /*pszFillRC*/);
188 AssertLogRelRCReturn(rc, rc);
189
190 /* Second region. */
191 RTGCPHYS const cbSecond = iPciFun == 0 ? 256*_1G64 : iPciFun * _32K;
192 rc = PDMDevHlpPCIIORegionRegisterEx(pDevIns, &pFun->PciDev, 2, cbSecond,
193 (PCIADDRESSSPACE)( PCI_ADDRESS_SPACE_MEM | PCI_ADDRESS_SPACE_BAR64
194 | (iPciFun == 0 ? PCI_ADDRESS_SPACE_MEM_PREFETCH : 0)),
195 devPlaygroundMap);
196 AssertLogRelRCReturn(rc, rc);
197 rc = PDMDevHlpMMIOExPreRegister(pDevIns, &pFun->PciDev, 2, cbSecond,
198 IOMMMIO_FLAGS_READ_PASSTHRU | IOMMMIO_FLAGS_WRITE_PASSTHRU, "PG-BAR2",
199 NULL /*pvUser*/, devPlaygroundMMIOWrite, devPlaygroundMMIORead, NULL /*pfnFill*/,
200 NIL_RTR0PTR /*pvUserR0*/, NULL /*pszWriteR0*/, NULL /*pszReadR0*/, NULL /*pszFillR0*/,
201 NIL_RTRCPTR /*pvUserRC*/, NULL /*pszWriteRC*/, NULL /*pszReadRC*/, NULL /*pszFillRC*/);
202 AssertLogRelRCReturn(rc, rc);
203
204 /* Subsequent function should use the same major as the previous one. */
205 iPciDevNo = PDMPCIDEVREG_DEV_NO_SAME_AS_PREV;
206 }
207
208 return VINF_SUCCESS;
209}
210
211RT_C_DECLS_BEGIN
212extern const PDMDEVREG g_DevicePlayground;
213RT_C_DECLS_END
214
215/**
216 * The device registration structure.
217 */
218const PDMDEVREG g_DevicePlayground =
219{
220 /* u32Version */
221 PDM_DEVREG_VERSION,
222 /* szName */
223 "playground",
224 /* szRCMod */
225 "",
226 /* szR0Mod */
227 "",
228 /* pszDescription */
229 "VBox Playground Device.",
230 /* fFlags */
231 PDM_DEVREG_FLAGS_DEFAULT_BITS,
232 /* fClass */
233 PDM_DEVREG_CLASS_MISC,
234 /* cMaxInstances */
235 1,
236 /* cbInstance */
237 sizeof(VBOXPLAYGROUNDDEVICE),
238 /* pfnConstruct */
239 devPlaygroundConstruct,
240 /* pfnDestruct */
241 devPlaygroundDestruct,
242 /* pfnRelocate */
243 NULL,
244 /* pfnMemSetup */
245 NULL,
246 /* pfnPowerOn */
247 NULL,
248 /* pfnReset */
249 NULL,
250 /* pfnSuspend */
251 NULL,
252 /* pfnResume */
253 NULL,
254 /* pfnAttach */
255 NULL,
256 /* pfnDetach */
257 NULL,
258 /* pfnQueryInterface */
259 NULL,
260 /* pfnInitComplete */
261 NULL,
262 /* pfnPowerOff */
263 NULL,
264 /* pfnSoftReset */
265 NULL,
266 /* u32VersionEnd */
267 PDM_DEVREG_VERSION
268};
Note: See TracBrowser for help on using the repository browser.

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