-
Notifications
You must be signed in to change notification settings - Fork 0
/
pimon_os.c
563 lines (495 loc) · 15.6 KB
/
pimon_os.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/******************************************************************************\
* Native ESXi on Arm driver for hardware monitoring on the Raspberry Pi.
* Copyright (c) 2020 Tom Hebel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
\******************************************************************************/
// TODO:
// - Implement a fail-safe for when the requested DMA heap is above 1GB, and
// program the DMA controller to move the DMA aperture upwards to match the
// heap start MA.
/*
* pimon_os.c --
*
* Module initialization and other OS stuff.
*/
#include "pimon.h"
#include "pimon_types.h"
#include "rpiq_drv.h"
#include "pimon_charDev.h"
/***********************************************************************/
static pimon_Driver_t pimon_Driver;
static rpiq_Device_t rpiq_Device;
/* For RPIQ char dev */
static pimon_CharDev_t rpiq_CharDev;
static vmk_BusType rpiq_logicalBusType;
VMK_ReturnStatus pimon_attachDevice(vmk_Device device);
VMK_ReturnStatus pimon_scanDevice(vmk_Device device);
VMK_ReturnStatus pimon_detachDevice(vmk_Device device);
VMK_ReturnStatus pimon_quiesceDevice(vmk_Device device);
VMK_ReturnStatus pimon_startDevice(vmk_Device device);
void pimon_forgetDevice(vmk_Device device);
static vmk_DriverOps pimon_DriverOps = {
.attachDevice = pimon_attachDevice,
.scanDevice = pimon_scanDevice,
.detachDevice = pimon_detachDevice,
.quiesceDevice = pimon_quiesceDevice,
.startDevice = pimon_startDevice,
.forgetDevice = pimon_forgetDevice,
};
/*
* Callbacks gluing the chardev driver to the RPIQ driver.
*/
static pimon_CharDevCallbacks_t rpiq_CharDevCBs = {
.open = rpiq_mmioOpenCB,
.close = rpiq_mmioCloseCB,
.ioctl = rpiq_mmioIoctlCB,
.read = rpiq_mmioReadCB,
.write = rpiq_mmioWriteCB,
};
/*
***********************************************************************
* init_module --
*
* Entry point for vmkernel module.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None
***********************************************************************
*/
int
init_module(void)
{
VMK_ReturnStatus status = VMK_OK;
vmk_HeapID heapID;
vmk_HeapCreateProps heapProps;
vmk_DriverProps driverProps;
vmk_LogProperties logProps;
vmk_Name charDevBusName;
status = vmk_NameInitialize(&pimon_Driver.driverName, PIMON_DRIVER_NAME);
VMK_ASSERT(status == VMK_OK);
pimon_Driver.moduleID = vmk_ModuleCurrentID;
rpiq_Device.initialized = VMK_FALSE;
/*
* Create module heap
*/
heapProps.type = VMK_HEAP_TYPE_SIMPLE;
status = vmk_NameInitialize(&heapProps.name, PIMON_DRIVER_NAME);
VMK_ASSERT(status == VMK_OK);
heapProps.module = pimon_Driver.moduleID;
heapProps.initial = PIMON_HEAP_INITIAL_SIZE;
heapProps.creationTimeoutMS = VMK_TIMEOUT_NONBLOCKING;
heapProps.max = PIMON_HEAP_MAX_SIZE;
status = vmk_HeapCreate(&heapProps, &heapID);
if (status != VMK_OK) {
vmk_WarningMessage("%s: %s: failed to create heap: %s",
PIMON_DRIVER_NAME,
__FUNCTION__,
vmk_StatusToString(status));
goto heap_create_failed;
}
vmk_ModuleSetHeapID(pimon_Driver.moduleID, heapID);
VMK_ASSERT(vmk_ModuleGetHeapID(pimon_Driver.moduleID) == heapID);
pimon_Driver.heapID = heapID;
/*
* Set up logger
*/
status = vmk_NameInitialize(&logProps.name, PIMON_DRIVER_NAME);
VMK_ASSERT(status == VMK_OK);
logProps.module = pimon_Driver.moduleID;
logProps.heap = pimon_Driver.heapID;
logProps.defaultLevel = 0;
logProps.throttle = NULL;
status = vmk_LogRegister(&logProps, &pimon_Driver.logger);
if (status != VMK_OK) {
vmk_WarningMessage("%s: %s: failed to register logger: %s",
PIMON_DRIVER_NAME,
__FUNCTION__,
vmk_StatusToString(status));
goto logger_reg_failed;
}
/*
* Init logical bus for char dev
*/
status = vmk_NameInitialize(&charDevBusName, VMK_LOGICAL_BUS_NAME);
status = vmk_BusTypeFind(&charDevBusName, &rpiq_logicalBusType);
if (status != VMK_OK) {
vmk_Warning(pimon_Driver.logger,
"failed to find logical bus: %s",
vmk_StatusToString(status));
goto chardev_bus_failed;
}
/*
* Register driver
*/
vmk_Memset(&driverProps, 0, sizeof(driverProps));
status = vmk_NameInitialize(&driverProps.name, PIMON_DRIVER_NAME);
VMK_ASSERT(status == VMK_OK);
driverProps.moduleID = pimon_Driver.moduleID;
driverProps.ops = &pimon_DriverOps;
status = vmk_DriverRegister(&driverProps,
&(pimon_Driver.driverHandle));
if (status == VMK_OK) {
vmk_Log(pimon_Driver.logger,
"driver registration successful",
PIMON_DRIVER_NAME,
__FUNCTION__);
}
else {
vmk_Warning(pimon_Driver.logger,
"driver registration failed: %s",
vmk_StatusToString(status));
goto driver_register_failed;
}
/*
* Init char dev driver
*/
pimon_charDevInit(pimon_Driver.moduleID,
pimon_Driver.heapID,
pimon_Driver.logger);
return VMK_OK;
driver_register_failed:
chardev_bus_failed:
vmk_LogUnregister(pimon_Driver.logger);
logger_reg_failed:
vmk_HeapDestroy(pimon_Driver.heapID);
heap_create_failed:
return 0;
}
/*
***********************************************************************
* cleanup_module --
*
* Cleanup code for when module is unloaded.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None
***********************************************************************
*/
void
cleanup_module(void)
{
rpiq_drvCleanUp();
vmk_HeapDestroy(rpiq_Device.dmaHeapID);
vmk_ACPIUnmapIOResource(pimon_Driver.moduleID, rpiq_Device.acpiDevice, 0);
vmk_LogUnregister(pimon_Driver.logger);
vmk_BusTypeRelease(rpiq_logicalBusType);
vmk_HeapDestroy(pimon_Driver.heapID);
vmk_DriverUnregister(pimon_Driver.driverHandle);
}
/*
***********************************************************************
* pimon_attachDevice --
*
* Driver callback that attaches RPIQ logical device to the driver.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None
***********************************************************************
*/
VMK_ReturnStatus
pimon_attachDevice(vmk_Device device)
{
VMK_ReturnStatus status = VMK_OK;
vmk_ACPIDevice acpiDev;
vmk_ACPIInfo acpiInfo;
vmk_MappedResourceAddress mappedAddr;
vmk_HeapAllocationDescriptor heapAllocDesc;
vmk_ByteCount dmaHeapSize;
vmk_HeapCreateProps heapProps;
vmk_HeapID dmaHeapID;
rpiq_Device_t *adapter = &rpiq_Device;
/*
* Since there is only one RPIQ device on the system board, we're making
* sure we're not initializing more.
*/
if (adapter->initialized != VMK_FALSE) {
vmk_Warning(pimon_Driver.logger,
"RPIQ device %p already initialized as %p",
device,
adapter->vmkDevice);
status = VMK_EXISTS;
goto device_already_exists;
}
/*
* Get vmk ACPI dev
*/
status = vmk_DeviceGetRegistrationData(device, (vmk_AddrCookie *)&acpiDev);
if (status != VMK_OK) {
vmk_Warning(pimon_Driver.logger,
"failed to get ACPI dev for vmk dev %p: %s",
device,
vmk_StatusToString(status));
goto get_acpi_dev_failed;
}
/*
* Find out if this is an ACPI dev
*/
status = vmk_ACPIQueryInfo(acpiDev, &acpiInfo);
if (status != VMK_OK) {
goto not_an_acpi_dev;
}
vmk_Log(pimon_Driver.logger,
"retrieved ACPI dev %p for vmk dev %p",
acpiDev,
device);
/*
* Map io resources
*/
status = vmk_ACPIMapIOResource(pimon_Driver.moduleID,
acpiDev,
0,
&mappedAddr);
if (status != VMK_OK) {
vmk_Warning(pimon_Driver.logger,
"unable to acquire io resources"
" for device %p: %s",
device,
vmk_StatusToString(status));
goto iores_map_failed;
}
else if (mappedAddr.type != VMK_IORESOURCE_MEM) {
vmk_Warning(pimon_Driver.logger,
"mapped io space %p of type %d, expecting %d",
mappedAddr,
mappedAddr.type,
VMK_IORESOURCE_MEM);
goto iores_map_failed;
}
vmk_Log(pimon_Driver.logger,
"mapped io space at %p of length %d for device %p",
(void*)mappedAddr.address.vaddr,
mappedAddr.len,
device);
/*
* Create DMA heap for device
*/
heapProps.type = VMK_HEAP_TYPE_CUSTOM;
heapProps.module = pimon_Driver.moduleID;
heapProps.creationTimeoutMS = VMK_TIMEOUT_NONBLOCKING;
heapProps.typeSpecific.custom.physContiguity = VMK_MEM_PHYS_CONTIGUOUS;
/*
* The DMA addresses accessible by the DMA engine are below 1GB by default,
* so the below may not be sufficient.
*/
// TODO: Make more robust by programming DMA controller to move aperture.
heapProps.typeSpecific.custom.physRange = VMK_PHYS_ADDR_BELOW_2GB;
status = vmk_NameInitialize(&heapProps.name, RPIQ_DMA_HEAP_NAME);
VMK_ASSERT(status == VMK_OK);
heapAllocDesc.size = sizeof(rpiq_MboxBuffer_t);
heapAllocDesc.alignment = RPIQ_DMA_MBOX_ALIGNMENT;
heapAllocDesc.count = RPIQ_DMA_MBOX_OBJ_MAX;
status = vmk_HeapDetermineMaxSize(&heapAllocDesc, 1, &dmaHeapSize);
if (status != VMK_OK) {
vmk_Warning(pimon_Driver.logger,
"failed to determine DMA max heap size: %s",
vmk_StatusToString(status));
goto dma_heap_size_failed;
}
heapProps.initial = dmaHeapSize;
heapProps.max = dmaHeapSize;
status = vmk_HeapCreate(&heapProps, &dmaHeapID);
if (status != VMK_OK) {
vmk_Warning(pimon_Driver.logger,
"failed to create DMA heap: %s",
vmk_StatusToString(status));
goto dma_heap_create_failed;
}
/*
* Init adapter
*/
vmk_Memset(adapter, 0, sizeof(*adapter));
adapter->vmkDevice = device;
adapter->acpiDevice = acpiDev;
adapter->acpiInfo = acpiInfo;
vmk_AtomicWrite64(&adapter->refCount, 0);
vmk_Memcpy(&adapter->mmioMappedAddr,
&mappedAddr,
sizeof(adapter->mmioMappedAddr));
adapter->mmioBase = (void *)mappedAddr.address.vaddr;
adapter->mmioLen = mappedAddr.len;
adapter->dmaHeapID = dmaHeapID;
adapter->initialized = VMK_TRUE;
/*
* Init RPIQ driver
*/
status = rpiq_drvInit(&pimon_Driver, &rpiq_Device);
if (status != VMK_OK) {
vmk_Warning(pimon_Driver.logger,
"failed to initialize RPIQ driver: %s",
vmk_StatusToString(status));
goto rpiq_drv_init_failed;
}
/*
* Attach device
*/
status = vmk_DeviceSetAttachedDriverData(adapter->vmkDevice, adapter);
if (status != VMK_OK) {
vmk_Warning(pimon_Driver.logger,
"failed to attach device %p: %s",
adapter->vmkDevice,
vmk_StatusToString(status));
goto device_attach_failed;
}
#ifdef PIMON_DEBUG
{
vmk_Log(pimon_Driver.logger,
"DMA heap %p",
adapter->dmaHeapID);
}
#endif /* PIMON_DEBUG */
return VMK_OK;
device_attach_failed:
rpiq_drv_init_failed:
vmk_HeapDestroy(rpiq_Device.dmaHeapID);
dma_heap_create_failed:
dma_heap_size_failed:
vmk_ACPIUnmapIOResource(pimon_Driver.moduleID, acpiDev, 0);
iores_map_failed:
not_an_acpi_dev:
get_acpi_dev_failed:
device_already_exists:
vmk_Warning(pimon_Driver.logger,
"no device attached",
PIMON_DRIVER_NAME,
__FUNCTION__);
return status;
}
/*
***********************************************************************
* pimon_scanDevice --
*
* Register char dev.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None
***********************************************************************
*/
VMK_ReturnStatus
pimon_scanDevice(vmk_Device device)
{
VMK_ReturnStatus status = VMK_OK;
pimon_CharDevPriv_t *privData;
pimon_CharDevProps_t charDevProps;
/* We only allow data to be transmitted in a particular size */
privData = vmk_HeapAlloc(pimon_Driver.heapID, sizeof(*privData));
if (privData == NULL) {
status = VMK_NO_MEMORY;
vmk_Warning(pimon_Driver.logger, "unable to allocate char dev priv data");
goto priv_alloc_failed;
}
privData->ioctlDataLen = sizeof(rpiq_MboxBuffer_t);
charDevProps.driverHandle = pimon_Driver.driverHandle;
charDevProps.logicalBusType = rpiq_logicalBusType;
charDevProps.parentDevice = device;
charDevProps.charDev = &rpiq_CharDev;
charDevProps.logicalPort = 0;
charDevProps.privData = privData;
charDevProps.callbacks = &rpiq_CharDevCBs;
status = pimon_charDevRegister(&charDevProps);
priv_alloc_failed:
return status;
}
/*
***********************************************************************
* pimon_detachDevice --
*
* Detaches the device from the driver.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None
***********************************************************************
*/
VMK_ReturnStatus
pimon_detachDevice(vmk_Device device)
{
VMK_ReturnStatus status = VMK_OK;
return status;
}
/*
***********************************************************************
* pimon_quiesceDevice --
*
* Do nothing.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None
***********************************************************************
*/
VMK_ReturnStatus
pimon_quiesceDevice(vmk_Device device)
{
VMK_ReturnStatus status = VMK_OK;
return status;
}
/*
***********************************************************************
* pimon_startDevice --
*
* Start the GPIO device.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None
***********************************************************************
*/
VMK_ReturnStatus
pimon_startDevice(vmk_Device device)
{
VMK_ReturnStatus status = VMK_OK;
return status;
}
/*
***********************************************************************
* pimon_forgetDevice --
*
* Do nothing.
*
* Results:
* None.
*
* Side Effects:
* None
***********************************************************************
*/
void
pimon_forgetDevice(vmk_Device device)
{
return;
}