forked from LordRafa/Parallella-cgminer
/
driver-epiphany.c
287 lines (225 loc) · 7.03 KB
/
driver-epiphany.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
/*
* Copyright 2013-2013 Rafael Waldo Delgado Doblas
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include "driver-epiphany.h"
#ifdef WANT_EPIPHANYMINING
/* TODO: resolve externals */
extern void submit_work_async(struct work *work_in, struct timeval *tv_work_found);
extern int dev_from_id(int thr_id);
/*
* Encode a length len/4 vector of (uint32_t) into a length len vector of
* (unsigned char) in big-endian form. Assumes len is a multiple of 4.
*/
static inline void
be32enc_vect(uint32_t *dst, const uint32_t *src, uint32_t len)
{
uint32_t i;
for (i = 0; i < len; i++)
dst[i] = htobe32(src[i]);
}
static void epiphany_detect()
{
e_platform_t platform;
if (e_init(NULL) == E_ERR)
return;
if (e_reset_system() == E_ERR)
return;
if (e_get_platform_info(&platform) == E_ERR)
return;
struct cgpu_info *epiphany = malloc(sizeof(struct cgpu_info));
if (unlikely(!epiphany))
quit(1, "Failed to malloc epiphany");
epiphany->drv = &epiphany_drv;
epiphany->deven = DEV_ENABLED;
epiphany->threads = 1;
epiphany->epiphany_rows = platform.rows;
epiphany->epiphany_cols = platform.cols;
epiphany->kname = "Epiphany Scrypt";
add_cgpu(epiphany);
}
static bool epiphany_thread_prepare(struct thr_info *thr)
{
e_epiphany_t *dev = &thr->cgpu->epiphany_dev;
e_mem_t *emem = &thr->cgpu->epiphany_emem;
unsigned rows = thr->cgpu->epiphany_rows;
unsigned cols = thr->cgpu->epiphany_cols;
char *fullpath = alloca(PATH_MAX);
if (e_alloc(emem, _BufOffset, rows * cols * sizeof(shared_buf_t)) == E_ERR) {
applog(LOG_ERR, "Error: Could not alloc shared Epiphany memory.");
return false;
}
if (e_open(dev, 0, 0, rows, cols) == E_ERR) {
applog(LOG_ERR, "Error: Could not start Epiphany cores.");
return false;
}
strcpy(fullpath, cgminer_path);
strcat(fullpath, "epiphany-scrypt.srec");
FILE* checkf = fopen(fullpath, "r");
if (!checkf) {
thr->cgpu->status = LIFE_SICK;
applog(LOG_ERR, "Error: Could not find epiphany-scrypt.srec.");
applog(LOG_ERR, " Is epiphany-scrypt.srec in cgminer directory?.");
return false;
}
fclose(checkf);
if (e_load_group(fullpath, dev, 0, 0, rows, cols, E_FALSE) == E_ERR) {
applog(LOG_ERR, "Error: Could not load epiphany-scrypt.srec on Epiphany.");
return false;
}
thread_reportin(thr);
return true;
}
#ifdef EPIPHANY_DEBUG
extern void scrypt_1024_1_1_256_sp(const uint32_t* input, char* scratchpad, uint32_t *ostate);
#endif
static bool epiphany_scrypt(struct thr_info *thr, const unsigned char __maybe_unused *pmidstate,
unsigned char *pdata, unsigned char __maybe_unused *phash1,
unsigned char __maybe_unused *phash, const unsigned char *ptarget,
uint32_t max_nonce, uint32_t *last_nonce, uint32_t n)
{
uint32_t i;
e_epiphany_t *dev = &thr->cgpu->epiphany_dev;
e_mem_t *emem = &thr->cgpu->epiphany_emem;
unsigned rows = thr->cgpu->epiphany_rows;
unsigned cols = thr->cgpu->epiphany_cols;
uint8_t *core_working = calloc(rows*cols, sizeof(uint8_t));
uint32_t *core_nonce = calloc(rows*cols, sizeof(uint32_t));
uint32_t cores_working = 0;
uint32_t *nonce = (uint32_t *)(pdata + 76);
uint32_t ostate;
uint32_t data[20];
const uint8_t core_go = 1;
uint32_t tmp_hash7;
uint32_t Htarg = ((const uint32_t *)ptarget)[7];
bool ret = false;
be32enc_vect(data, (const uint32_t *)pdata, 19);
if (e_start_group(dev) == E_ERR) {
applog(LOG_ERR, "Error: Could not start Epiphany cores.");
return false;
}
off_t offdata = offsetof(shared_buf_t, data);
off_t offostate = offsetof(shared_buf_t, ostate);
off_t offcorego = offsetof(shared_buf_t, go);
off_t offcoreend = offsetof(shared_buf_t, working);
off_t offcore;
#ifdef EPIPHANY_DEBUG
#define SCRATCHBUF_SIZE (131584)
uint32_t ostate2[8];
char *scratchbuf = malloc(SCRATCHBUF_SIZE);
uint32_t *ostatearm = calloc(rows*cols, sizeof(uint32_t));
#endif
i = 0;
while(1) {
offcore = i * sizeof(shared_buf_t);
if ((!core_working[i]) && (n < max_nonce)) {
*nonce = ++n;
data[19] = n;
core_working[i] = 1;
cores_working++;
core_nonce[i] = n;
#ifdef EPIPHANY_DEBUG
scrypt_1024_1_1_256_sp(data, scratchbuf, ostate2);
ostatearm[i] = ostate2[7];
#endif
e_write(emem, 0, 0, offcore + offdata, (void *) data, sizeof(data));
e_write(emem, 0, 0, offcore + offcoreend, (void *) &core_working[i], sizeof(core_working[i]));
e_write(emem, 0, 0, offcore + offcorego, (void *) &core_go, sizeof(core_go));
}
e_read(emem, 0, 0, offcore + offcoreend, (void *) &(core_working[i]), sizeof(core_working[i]));
if (!core_working[i]) {
e_read(emem, 0, 0, offcore + offostate, (void *) &(ostate), sizeof(ostate));
#ifdef EPIPHANY_DEBUG
applog(LOG_DEBUG, "CORE %u - EPI HASH %u - ARM HASH %u - %s", i, ostate, ostatearm[i], ((ostate==ostatearm[i])? "OK":"FAIL"));
#endif
tmp_hash7 = be32toh(ostate);
cores_working--;
if (unlikely(tmp_hash7 <= Htarg)) {
((uint32_t *)pdata)[19] = htobe32(core_nonce[i]);
*last_nonce = core_nonce[i];
#ifdef EPIPHANY_DEBUG
free(scratchbuf);
#endif
return true;
}
}
if (unlikely(((n >= max_nonce) && !cores_working) || thr->work_restart)) {
*last_nonce = n;
#ifdef EPIPHANY_DEBUG
free(scratchbuf);
#endif
return false;
}
i++;
i %= rows * cols;
}
#ifdef EPIPHANY_DEBUG
free(scratchbuf);
#endif
return false;
}
static int64_t epiphany_scanhash(struct thr_info *thr, struct work *work, int64_t max_nonce)
{
const int thr_id = thr->id;
unsigned char hash1[64];
uint32_t first_nonce = work->blk.nonce;
uint32_t last_nonce;
bool rc;
hex2bin(hash1, "00000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000010000", 64);
EPIPHANYSearch:
last_nonce = first_nonce;
rc = false;
/* scan nonces for a proof-of-work hash */
{
rc = epiphany_scrypt(
thr,
work->midstate,
work->data,
hash1,
work->hash,
work->target,
max_nonce,
&last_nonce,
work->blk.nonce
);
}
/* if nonce found, submit work */
if (unlikely(rc)) {
applog(LOG_DEBUG, "EPIPHANY %d found something?", dev_from_id(thr_id));
submit_work_async(work, NULL);
work->blk.nonce = last_nonce + 1;
goto EPIPHANYSearch;
}
else
if (unlikely(last_nonce == first_nonce))
return 0;
work->blk.nonce = last_nonce + 1;
return last_nonce - first_nonce + 1;
}
static void epiphany_thread_shutdown(__maybe_unused struct thr_info *thr)
{
e_epiphany_t *dev = &thr->cgpu->epiphany_dev;
e_mem_t *emem = &thr->cgpu->epiphany_emem;
e_close(dev);
e_free(emem);
e_finalize();
}
static uint64_t epiphany_can_limit_work(struct thr_info __maybe_unused *thr)
{
return 0x1ff;
}
struct device_drv epiphany_drv = {
.drv_id = DRIVER_EPIPHANY,
.dname = "epi",
.name = "EPI",
.drv_detect = epiphany_detect,
.thread_prepare = epiphany_thread_prepare,
.thread_shutdown = epiphany_thread_shutdown,
.can_limit_work = epiphany_can_limit_work,
.scanhash = epiphany_scanhash,
};
#endif