forked from ztachip/ztachip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathztalib.c
169 lines (141 loc) · 4.52 KB
/
ztalib.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
//----------------------------------------------------------------------------
// Copyright [2014] [Ztachip Technologies Inc]
//
// Author: Vuong Nguyen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except IN compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to IN writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
#include <stdbool.h>
#include "ztalib.h"
// This file contains supporting functions for codes running on mcore
static bool taskStatus=false;
// Task entry point...
static void taskEntry(int thisFunc, void(*func)(int, int), int p1, int p2) {
ZTAM_GREG(0,REG_DP_VM_TOGGLE,0)=0;
_taskYield();
if (func)
(*func)(p1, p2);
taskStatus = false;
for (;;) {
ZTAM_GREG(0,REG_DP_VM_TOGGLE,0)=0;
_taskYield();
}
}
// Perform initialization for code running on mcore
void ztaInit() {
taskStatus = false;
_taskSpawn((uint32_t)taskEntry,0,0,0);
}
// Start execution by spawning 2 threads
void ztaDualHartExecute(void(*func)(void *,int),void *pparm) {
// Launch a thread to execute on tensor processor's first HART
taskStatus = true;
_taskSpawn((uint32_t)taskEntry,(uint32_t)func,(uint32_t)pparm,1);
ZTAM_GREG(0,REG_DP_VM_TOGGLE,0)=0;
_taskYield();
(*func)(pparm,0);
// Wait for both threads to be finished
while(taskStatus)
ztaTaskYield();
}
// All information to launch a kernel is packed into 32 bit word
uint32_t ztaBuildKernelFunc(uint32_t _func,int num_pcore,int num_tid) {
uint32_t func,p0,p1,dataModel;
func=EXE_FUNC_FIELD(_func); // First instruction address to kernel functions
p0=EXE_P0_FIELD(_func); // Firt parameter
p1=EXE_P1_FIELD(_func); // Second parameter
dataModel=EXE_MODEL_FIELD(_func); // Kernel memory model (large/small)
return DP_EXE_CMD(1,func,num_pcore-1,0,p0,p1,num_tid-1,dataModel);
}
// Allocate shared memory from non-cached data region
ZTA_SHARED_MEM ztaAllocSharedMem(int _size) {
return (ZTA_SHARED_MEM)malloc(_size);
}
// Free a previously allocated shared memory
void ztaFreeSharedMem(ZTA_SHARED_MEM p) {
free((void *)p);
}
// Build ztachip lookup table
static ZTA_SHARED_MEM buildSpu(SPU_FUNC func,void *pparm,uint32_t parm,uint32_t parm2) {
uint16_t v;
int16_t v2,v4,slope;
int16_t *p;
ZTA_SHARED_MEM shm;
shm=ztaAllocSharedMem(SPU_SIZE*2*sizeof(int16_t));
p=(int16_t *)ZTA_SHARED_MEM_VIRTUAL(shm);
for(int i=0;i < SPU_SIZE;i++) {
v=((i*SPU_REMAINDER)&0xFFF);
if(v & 0x800)
v |= 0xF800;
v2=(*func)(v,(i==0)?pparm:0,parm,parm2);
v=((i*SPU_REMAINDER+(SPU_REMAINDER-1))&0xFFF);
if(v & 0x800)
v |= 0xF800;
v4=(*func)(v,0,parm,parm2);
slope=(int16_t)(((((int)v4-(int)v2)))*SPU_REMAINDER)/(SPU_REMAINDER-1);
p[2*i]=slope;
p[2*i+1]=v2;
}
return shm;
}
ZTA_SHARED_MEM ztaBuildSpuBundle(int numSpuImg,...) {
ZTA_SHARED_MEM bundle,spu;
SPU_FUNC func;
void *pparm;
uint32_t parm,parm2;
int16_t *pp;
int i;
va_list args;
va_start(args,numSpuImg);
bundle=ztaAllocSharedMem((numSpuImg*SPU_SIZE*2+1)*sizeof(int16_t));
pp=(int16_t *)ZTA_SHARED_MEM_VIRTUAL(bundle);
pp[0]=numSpuImg;
pp++;
for(i=0;i < numSpuImg;i++,pp+=SPU_SIZE*2) {
func = va_arg(args,SPU_FUNC);
pparm = va_arg(args,void *);
parm = va_arg(args,uint32_t);
parm2 = va_arg(args,uint32_t);
spu=buildSpu(func,pparm,parm,parm2);
memcpy(pp,ZTA_SHARED_MEM_VIRTUAL(spu),SPU_SIZE*2*sizeof(int16_t));
ztaFreeSharedMem(spu);
}
va_end(args);
return bundle;
}
// Reading response message from ztachip core
bool ztaReadResponse(uint32_t *resp) {
if(ZTAM_GREG(0,REG_DP_READ_INDICATION_AVAIL,0)==0) {
*resp=0;
return false;
}
ZTAM_GREG(0,REG_DP_READ_INDICATION,0);
*resp=ZTAM_GREG(0,REG_DP_READ_INDICATION_PARM,0);
ZTAM_GREG(0,REG_DP_READ_SYNC,0);
return true;
}
// Abort all excution
void ztaAbort(int _errorCode) {
extern void _exit(int);
_exit(_errorCode);
}