/
videocore.go
147 lines (112 loc) · 3.58 KB
/
videocore.go
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
// BCM2835 SoC VideoCore support
// https://github.com/f-secure-foundry/tamago
//
// Copyright (c) the bcm2835 package authors
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
package bcm2835
import "encoding/binary"
const (
GPU_MEMORY_FLAG_DISCARDABLE = 1 << 0
GPU_MEMORY_FLAG_NORMAL = 0 << 2
GPU_MEMORY_FLAG_DIRECT = 1 << 2
GPU_MEMORY_FLAG_COHERENT = 2 << 2
GPU_MEMORY_FLAG_L1_NONALLOCATING = 3 << 2
GPU_MEMORY_FLAG_ZERO = 1 << 4
GPU_MEMORY_FLAG_NO_INIT = 1 << 5
GPU_MEMORY_FLAG_HINT_PERMALOCK = 1 << 6
)
// FirmwareRevision gets the firmware rev of the VideoCore GPU
func FirmwareRevision() uint32 {
buf := exchangeSingleTagMessage(VC_BOARD_GET_REV, make([]byte, VC_BOARD_GET_REV_LEN))
if len(buf) < 4 {
return 0
}
return binary.LittleEndian.Uint32(buf)
}
// BoardModel gets the board model
func BoardModel() uint32 {
buf := exchangeSingleTagMessage(VC_BOARD_GET_MODEL, make([]byte, VC_BOARD_GET_MODEL_LEN))
if len(buf) < 4 {
return 0
}
return binary.LittleEndian.Uint32(buf)
}
// MACAddress gets the board's MAC address
func MACAddress() []byte {
return exchangeSingleTagMessage(VC_BOARD_GET_MAC, make([]byte, VC_BOARD_GET_MAC_LEN))
}
// Serial gets the board's serial number
func Serial() uint32 {
buf := exchangeSingleTagMessage(VC_BOARD_GET_SERIAL, make([]byte, VC_BOARD_GET_SERIAL_LEN))
if len(buf) < 4 {
return 0
}
return binary.LittleEndian.Uint32(buf)
}
// CPUMemory gets the memory ranges allocated to the ARM core(s)
func CPUMemory() (start uint32, size uint32) {
buf := exchangeSingleTagMessage(VC_BOARD_GET_ARM_MEMORY, make([]byte, VC_BOARD_GET_ARM_MEMORY_LEN))
if len(buf) < 8 {
return 0, 0
}
return binary.LittleEndian.Uint32(buf[0:]), binary.LittleEndian.Uint32(buf[4:])
}
// GPUMemory gets the memory ranges allocated to VideoCore
func GPUMemory() (start uint32, size uint32) {
buf := exchangeSingleTagMessage(VC_BOARD_GET_VC_MEMORY, make([]byte, VC_BOARD_GET_VC_MEMORY_LEN))
if len(buf) < 8 {
return 0, 0
}
return binary.LittleEndian.Uint32(buf[0:]), binary.LittleEndian.Uint32(buf[4:])
}
// CPUAvailableDMAChannels gets the DMA channels available to the ARM core(s)
func CPUAvailableDMAChannels() (bitmask uint32) {
buf := exchangeSingleTagMessage(VC_RES_GET_DMACHANNELS, make([]byte, VC_RES_GET_DMACHANNELS_LEN))
if len(buf) < 4 {
return 0
}
return binary.LittleEndian.Uint32(buf)
}
// AllocateGPUMemory allocates space from the GPU address space
//
// The returned value is a handle, use LockMemory to convert
// to an address.
func AllocateGPUMemory(size uint32, alignment uint32, flags uint32) (handle uint32) {
buf := make([]byte, VC_MEM_ALLOCATE_LEN)
binary.LittleEndian.PutUint32(buf[0:], size)
binary.LittleEndian.PutUint32(buf[4:], alignment)
binary.LittleEndian.PutUint32(buf[8:], uint32(flags))
buf = exchangeSingleTagMessage(VC_MEM_ALLOCATE, buf)
if len(buf) < 4 {
return 0
}
return binary.LittleEndian.Uint32(buf)
}
// LockGPUMemory provides the address of previously allocated memory
func LockGPUMemory(handle uint32) (addr uint32) {
buf := make([]byte, VC_MEM_LOCK_LEN)
binary.LittleEndian.PutUint32(buf[0:], handle)
buf = exchangeSingleTagMessage(VC_MEM_LOCK, buf)
if len(buf) < 4 {
return 0
}
return binary.LittleEndian.Uint32(buf)
}
func exchangeSingleTagMessage(code uint32, buf []byte) []byte {
msg := &MailboxMessage{
Tags: []MailboxTag{
{
ID: code,
Buffer: buf,
},
},
}
Mailbox.Call(VC_CH_PROPERTYTAGS_A_TO_VC, msg)
tag := msg.Tag(code)
if tag == nil {
return nil
}
return tag.Buffer
}