-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrpiq_drv.c
550 lines (486 loc) · 14.9 KB
/
rpiq_drv.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
/******************************************************************************\
* 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:
// - Figure out if it's possible to support arbitrary length mbox buffers.
// - Currently, the mbox buffer is copied four times: (1) from UW to kernel
// space, (2) from driver heap to DMA heap, (3) from DMA heap to driver heap,
// and (4) from kernel space to UW. Perhaps this could be simplified.
/*
* rpiq_drv.c --
*
* Implementation of RPIQ hardware interface.
*/
#include "rpiq_drv.h"
/***********************************************************************/
static pimon_Driver_t *pimon_Driver;
static rpiq_Device_t *rpiq_Device;
static rpiq_MboxDMABuffer_t rpiq_MboxDMABuffer;
/*
***********************************************************************
* rpiq_drvInit --
*
* Initialize the RPIQ driver.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None
***********************************************************************
*/
VMK_ReturnStatus
rpiq_drvInit(pimon_Driver_t *driver,
rpiq_Device_t *adapter)
{
VMK_ReturnStatus status = VMK_OK;
vmk_MA dmaBufPtrMA;
vmk_SpinlockCreateProps lockProps;
pimon_Driver = driver;
rpiq_Device = adapter;
/*
* Allocate mbox DMA buffer. We allocate once and re-use it as a work-around,
* since repeated allocations will eventually allocate beyond 1GB. This in
* turn causes the VC to be unable to access it.
*
* TODO: Program the DMA controller & move the aperture to match the
* allocation.
*/
rpiq_MboxDMABuffer.ptr = vmk_HeapAlign(rpiq_Device->dmaHeapID,
sizeof(*rpiq_MboxDMABuffer.ptr),
RPIQ_DMA_MBOX_ALIGNMENT);
/* Verify that mbox buffer is below 2GB */
vmk_VA2MA((vmk_VA)rpiq_MboxDMABuffer.ptr, 0, &dmaBufPtrMA);
if (dmaBufPtrMA > RPIQ_DMA_MAX_ADDR) {
status = VMK_NO_MEMORY;
vmk_Warning(pimon_Driver->logger,
"unable to allocate below 2GB");
goto dma_addr_invalid;
}
/*
* Init lock
*/
lockProps.moduleID = pimon_Driver->moduleID;
lockProps.heapID = pimon_Driver->heapID;
status = vmk_NameInitialize(&lockProps.name, RPIQ_DMA_LOCK_NAME);
if (status != VMK_OK) {
vmk_Warning(pimon_Driver->logger,
"failed to init DMA buffer lock name: %s",
vmk_StatusToString(status));
goto lock_init_failed;
}
lockProps.type = VMK_SPINLOCK;
lockProps.domain = VMK_LOCKDOMAIN_INVALID;
lockProps.rank = VMK_SPINLOCK_UNRANKED;
status = vmk_SpinlockCreate(&lockProps, &rpiq_MboxDMABuffer.lock);
if (status != VMK_OK) {
vmk_Warning(pimon_Driver->logger,
"failed to create DMA buffer spinlock: %s",
vmk_StatusToString(status));
goto lock_init_failed;
}
lock_init_failed:
dma_addr_invalid:
vmk_HeapFree(rpiq_Device->dmaHeapID, rpiq_MboxDMABuffer.ptr);
return status;
}
/*
***********************************************************************
* rpiq_drvCleanUp --
*
* Clean up the RPIQ driver.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None
***********************************************************************
*/
void
rpiq_drvCleanUp()
{
vmk_SpinlockDestroy(rpiq_MboxDMABuffer.lock);
vmk_HeapFree(rpiq_Device->dmaHeapID, rpiq_MboxDMABuffer.ptr);
}
/*
***********************************************************************
* rpiq_mboxDrain --
*
* Drain the VideoCore mailbox.
*
* Results:
* VMK_OK on success
* VMK_TIMEOUT otherwise
*
* Side Effects:
* None.
***********************************************************************
*/
VMK_ReturnStatus
rpiq_mboxDrain()
{
int retries = 0;
vmk_uint32 mboxStatus, mboxVal;
do {
vmk_MappedResourceRead32(&rpiq_Device->mmioMappedAddr,
RPIQ_MBOX_STATUS,
&mboxStatus);
if (mboxStatus == RPIQ_MBOX_EMPTY) {
return VMK_OK;
}
RPIQ_MEM_BARRIER();
vmk_MappedResourceRead32(&rpiq_Device->mmioMappedAddr,
RPIQ_MBOX_READ,
&mboxVal);
} while (++retries < RPIQ_MBOX_MAX_RETRIES);
return VMK_TIMEOUT;
}
/*
***********************************************************************
* rpiq_mboxStatusCleared --
*
* Spin until VC mailbox status flag is clear.
*
* Results:
* VMK_OK on success
* VMK_TIMEOUT otherwise
*
* Side Effects:
* None.
***********************************************************************
*/
VMK_ReturnStatus
rpiq_mboxStatusCleared(vmk_uint32 status)
{
int retries = 0;
vmk_uint32 mboxStatus;
do {
vmk_MappedResourceRead32(&rpiq_Device->mmioMappedAddr,
RPIQ_MBOX_STATUS,
&mboxStatus);
if ((mboxStatus & status) == 0) {
return VMK_OK;
}
RPIQ_MEM_BARRIER();
} while (++retries < RPIQ_MBOX_MAX_RETRIES);
return VMK_TIMEOUT;
}
/*
***********************************************************************
* rpiq_mboxSend --
*
* Send a request to a VideoCore mailbox and receive its response.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None.
***********************************************************************
*/
VMK_ReturnStatus
rpiq_mboxSend(rpiq_MboxChannel_t channel, // IN
rpiq_MboxBuffer_t *buffer) // IN/OUT
{
VMK_ReturnStatus status = VMK_OK;
rpiq_MboxDMABuffer_t *dmaBuf = &rpiq_MboxDMABuffer;
rpiq_MboxBuffer_t *dmaBufPtr = dmaBuf->ptr;
vmk_MA dmaBufPtrMA;
vmk_uint32 mboxIn;
vmk_uint32 mboxOut;
vmk_uint32 mboxReadRetries = 0;
/*
* Drain mbox
*/
if (rpiq_mboxDrain() != VMK_OK) {
status = VMK_TIMEOUT;
vmk_Warning(pimon_Driver->logger, "failed to drain mailbox");
goto mbox_drain_timeout;
}
/* Lock the DMA buffer */
vmk_SpinlockLock(dmaBuf->lock);
/*
*-------------------------------------------------------------------
* MBOX WRITE
*-------------------------------------------------------------------
*/
/*
* Wait for mbox to become empty
*/
if (rpiq_mboxStatusCleared(RPIQ_MBOX_FULL) != VMK_OK) {
status = VMK_TIMEOUT;
vmk_Warning(pimon_Driver->logger,
"timeout waiting for mbox to become empty");
goto mbox_empty_timeout;
}
/*
* Copy buffer to location accessible by VC DMA
*/
vmk_Memcpy(dmaBufPtr, buffer, sizeof(*dmaBufPtr));
/*
* Prepare mbox input data
*/
vmk_VA2MA((vmk_VA)dmaBufPtr, 0, &dmaBufPtrMA);
mboxIn = ((vmk_uint32)dmaBufPtrMA
| RPIQ_DMA_COHERENT_ADDR
| (vmk_uint32)channel);
RPIQ_MEM_BARRIER();
vmk_MappedResourceWrite32(&rpiq_Device->mmioMappedAddr,
RPIQ_MBOX_WRITE,
mboxIn);
RPIQ_MEM_BARRIER();
/*
* This needs to be here.
*/
RPIQ_INVAL_DCACHE((void *)dmaBufPtr);
/*
*-------------------------------------------------------------------
* MBOX READ
*-------------------------------------------------------------------
*/
/*
* Wait for mbox to fill up
*/
if (rpiq_mboxStatusCleared(RPIQ_MBOX_EMPTY) != VMK_OK) {
status = VMK_TIMEOUT;
vmk_Warning(pimon_Driver->logger,
"timeout waiting for mbox to become full");
goto mbox_full_timeout;
}
RPIQ_MEM_BARRIER();
/*
* Perform read
*/
do {
vmk_MappedResourceRead32(&rpiq_Device->mmioMappedAddr,
RPIQ_MBOX_READ,
&mboxOut);
RPIQ_MEM_BARRIER();
++mboxReadRetries;
if (mboxReadRetries >= RPIQ_MBOX_MAX_RETRIES) {
status = VMK_TIMEOUT;
vmk_Warning(pimon_Driver->logger,
"RPIQ mailbox read failed after %d retries",
mboxReadRetries);
goto mbox_read_attempts;
}
} while ((mboxOut & RPIQ_MBOX_CHAN_MASK) != channel);
RPIQ_MEM_BARRIER();
/*
* Something went wrong, in & out buffers don't match
*/
if (mboxOut != mboxIn) {
status = VMK_FAILURE;
vmk_Warning(pimon_Driver->logger,
"mismatch in mbox input %p and output %p",
mboxIn,
mboxOut);
goto mbox_response_mismatch;
}
/*
* Invalidate cache and copy data back to in/out buffer
*/
RPIQ_INVAL_DCACHE((void *)dmaBufPtr);
vmk_Memcpy(buffer, dmaBufPtr, sizeof(*buffer));
if (buffer->header.requestResponse != RPIQ_MBOX_SUCCESS) {
status = VMK_FAILURE;
vmk_Warning(pimon_Driver->logger,
"no response data received");
}
/* Unlock the DMA buffer */
vmk_SpinlockUnlock(dmaBuf->lock);
return VMK_OK;
mbox_response_mismatch:
mbox_read_attempts:
mbox_full_timeout:
mbox_empty_timeout:
mbox_drain_timeout:
vmk_SpinlockUnlock(dmaBuf->lock);
return status;
}
/*
***********************************************************************
* rpiq_mmioOpenCB --
*
* Callback used by char dev driver when the /dev/vmgfx32 file is
* opened.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None.
***********************************************************************
*/
VMK_ReturnStatus
rpiq_mmioOpenCB(vmk_CharDevFdAttr *attr)
{
VMK_ReturnStatus status = VMK_OK;
vmk_Log(pimon_Driver->logger, "Opening file.");
return status;
}
/*
***********************************************************************
* rpiq_mmioCloseCB --
*
* Callback used by char dev driver when the /dev/vmgfx32 file is
* closed.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None.
***********************************************************************
*/
VMK_ReturnStatus
rpiq_mmioCloseCB(vmk_CharDevFdAttr *attr)
{
VMK_ReturnStatus status = VMK_OK;
vmk_Log(pimon_Driver->logger, "Closing file.");
return status;
}
/*
***********************************************************************
* rpiq_mmioIoctlCB --
*
* Callback used by char dev driver for I/O control. The RPIQ char dev
* file only supports access via ioctl, since it allows for sending
* and receiving data structures easily.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None.
***********************************************************************
*/
VMK_ReturnStatus
rpiq_mmioIoctlCB(unsigned int channel, // IN
void *data, // IN/OUT
vmk_ByteCount ioctlDataLen) // IN
{
VMK_ReturnStatus status = VMK_OK;
rpiq_MboxBuffer_t *buffer;
// TODO: add support for arbitrary length mailbox data, to allow for things
// like requesting console data.
/*
* Sanity checks
*/
if (data == NULL || ioctlDataLen != sizeof(*buffer)) {
status = VMK_BAD_PARAM;
vmk_Warning(pimon_Driver->logger, "invalid ioctl data");
goto invalid_ioctl_data;
}
if (channel < RPIQ_CHAN_MBOX_MIN
|| channel > RPIQ_CHAN_MBOX_MAX) {
status = VMK_BAD_PARAM;
vmk_Warning(pimon_Driver->logger, "ioctl command %d invalid", channel);
goto invalid_ioct_channel;
}
// TODO: Should we check the tag validity too?
buffer = (rpiq_MboxBuffer_t *)data;
if (buffer->endTag != 0
|| buffer->padding[0] != 0
|| buffer->padding[1] != 0
|| buffer->header.bufLen != sizeof(*buffer)) {
status = VMK_BAD_PARAM;
vmk_Warning(pimon_Driver->logger, "invalid mailbox buffer");
goto invalid_mbox_buffer;
}
#ifdef RPIQ_DEBUG
{
vmk_Log(pimon_Driver->logger,
"bufLen=%d rqRp=%d tag=0x%x rpLen=%d rqLen=%d endTag=%d"
" padding[0]=%d padding[1]=%d",
buffer->header.bufLen,
buffer->header.requestResponse,
buffer->header.tag,
buffer->header.responseLen,
buffer->header.requestLen,
buffer->endTag,
buffer->padding[0],
buffer->padding[1]);
}
#endif /* RPIQ_DEBUG */
/*
* Mbox I/O
*/
status = rpiq_mboxSend(channel, buffer);
if (status != VMK_OK) {
vmk_Warning(pimon_Driver->logger,
"unable to send to mailbox: %s",
vmk_StatusToString(status));
goto mbox_send_failed;
}
return VMK_OK;
mbox_send_failed:
invalid_mbox_buffer:
invalid_ioct_channel:
invalid_ioctl_data:
return status;
}
/*
***********************************************************************
* rpiq_mmioReadCB --
*
* Callback used by char dev driver when the file is read. Currently
* not supported, as all I/O is done via ioctl.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None.
***********************************************************************
*/
VMK_ReturnStatus
rpiq_mmioReadCB(char *buffer,
vmk_ByteCount nbytes,
vmk_loff_t *ppos,
vmk_ByteCountSigned *nread)
{
return VMK_NOT_SUPPORTED;
}
/*
***********************************************************************
* rpiq_mmioWrite --
*
* Callback used by char dev driver when the file is written to.
* Currently not supported, as all I/O is done via ioctl.
*
* Results:
* VMK_OK on success, error code otherwise
*
* Side Effects:
* None.
***********************************************************************
*/
VMK_ReturnStatus
rpiq_mmioWriteCB(char *buffer,
vmk_ByteCount nbytes,
vmk_loff_t *ppos,
vmk_ByteCountSigned *nwritten)
{
return VMK_NOT_SUPPORTED;
}