driver-titan.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. * Copyright 2014 Vitalii Demianets
  3. * Copyright 2014 KnCMiner
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include <fcntl.h>
  11. #include <sys/ioctl.h>
  12. #include "deviceapi.h"
  13. #include "logging.h"
  14. #include "miner.h"
  15. #include "util.h"
  16. #include "titan-asic.h"
  17. #define KNC_TITAN_DEFAULT_FREQUENCY 275
  18. #define KNC_TITAN_HWERR_DISABLE_SECS 10
  19. #define KNC_POLL_INTERVAL_US 10000
  20. #define DIE_HEALTH_INTERVAL_SEC 20
  21. /* Broadcast address to all cores in a die */
  22. #define ALL_CORES 0xFFFF
  23. /* Work queue pre-fill level.
  24. * Must be high enough to supply all ASICs with works after a flush */
  25. #define WORK_QUEUE_PREFILL 20
  26. #define MANUAL_CHECK_CORES_PER_POLL 100
  27. /* Specify here minimum number of leading zeroes in hash */
  28. #define DEFAULT_DIFF_FILTERING_ZEROES 24
  29. #define DEFAULT_DIFF_FILTERING_FLOAT (1. / ((double)(0x00000000FFFFFFFF >> DEFAULT_DIFF_FILTERING_ZEROES)))
  30. #define DEFAULT_DIFF_HASHES_PER_NONCE (1 << DEFAULT_DIFF_FILTERING_ZEROES)
  31. BFG_REGISTER_DRIVER(knc_titan_drv)
  32. /* 3 - default number of threads per core */
  33. static int opt_knc_threads_per_core = 3;
  34. static const struct bfg_set_device_definition knc_titan_set_device_funcs[];
  35. struct knc_titan_core {
  36. int asicno;
  37. int dieno; /* inside asic */
  38. int coreno; /* inside die */
  39. struct knc_titan_die *die;
  40. struct cgpu_info *proc;
  41. int hwerr_in_row;
  42. int hwerr_disable_time;
  43. struct timeval enable_at;
  44. struct timeval first_hwerr;
  45. struct nonce_report last_nonce;
  46. bool need_manual_check;
  47. };
  48. struct knc_titan_die {
  49. int asicno;
  50. int dieno; /* inside asic */
  51. int cores;
  52. struct cgpu_info *first_proc;
  53. bool need_flush;
  54. int next_slot;
  55. /* First slot after flush. If next_slot reaches this, then
  56. * we need to re-flush all the cores to avoid duplicating slot numbers
  57. * for different works */
  58. int first_slot;
  59. struct timeval last_share;
  60. /* Don't use this! DC/DCs don't like broadcast urgent setworks */
  61. bool broadcast_flushes;
  62. uint64_t hashes_done;
  63. /* We store history of hash counters for the last minute (12*5 = 60 s) */
  64. #define HASHES_BUF_ONE_ENTRY_TIME 5
  65. #define HASHES_BUF_ENTRIES 12
  66. uint64_t hashes_buf[HASHES_BUF_ENTRIES];
  67. int hashes_buf_idx; /* next write position === the oldest data stored */
  68. int freq;
  69. int manual_check_count;
  70. };
  71. struct knc_titan_info {
  72. void *ctx;
  73. struct cgpu_info *cgpu;
  74. int cores;
  75. struct knc_titan_die dies[KNC_TITAN_MAX_ASICS][KNC_TITAN_DIES_PER_ASIC];
  76. bool asic_served_by_fpga[KNC_TITAN_MAX_ASICS];
  77. struct timeval tv_prev;
  78. struct work *workqueue;
  79. int workqueue_size;
  80. int workqueue_max;
  81. int next_id;
  82. struct work *devicework;
  83. };
  84. static void knc_titan_zero_stats(struct cgpu_info *cgpu)
  85. {
  86. if (cgpu->device != cgpu)
  87. return;
  88. struct knc_titan_core *knccore = cgpu->thr[0]->cgpu_data;
  89. struct knc_titan_info *knc = cgpu->device_data;
  90. struct knc_titan_die *die;
  91. int dieno;
  92. for (dieno = 0; dieno < KNC_TITAN_DIES_PER_ASIC; ++dieno) {
  93. die = &(knc->dies[knccore->asicno][dieno]);
  94. die->hashes_done = 0;
  95. memset(die->hashes_buf, 0, sizeof(die->hashes_buf));
  96. }
  97. }
  98. static double knc_titan_get_device_rolling_hashrate(struct cgpu_info *device)
  99. {
  100. struct knc_titan_core *knccore = device->thr[0]->cgpu_data;
  101. struct knc_titan_info *knc = device->device_data;
  102. double hashrate = 0.0;
  103. int dieno, i;
  104. for (dieno = 0; dieno < KNC_TITAN_DIES_PER_ASIC; ++dieno) {
  105. for (i = 0; i < HASHES_BUF_ENTRIES; ++i) {
  106. hashrate += (double)knc->dies[knccore->asicno][dieno].hashes_buf[i] / 1.0e6;
  107. }
  108. }
  109. return hashrate / ((double)(HASHES_BUF_ENTRIES * HASHES_BUF_ONE_ENTRY_TIME));
  110. }
  111. static void knc_titan_die_hashmeter(struct knc_titan_die *die, uint64_t hashes_done)
  112. {
  113. struct timeval tv_now;
  114. cgtime(&tv_now);
  115. int cur_idx = (tv_now.tv_sec / HASHES_BUF_ONE_ENTRY_TIME) % HASHES_BUF_ENTRIES;
  116. if (die->hashes_buf_idx == cur_idx) {
  117. die->hashes_done += hashes_done;
  118. return;
  119. }
  120. die->hashes_buf[die->hashes_buf_idx] = die->hashes_done;
  121. die->hashes_done = hashes_done;
  122. die->hashes_buf_idx = (die->hashes_buf_idx + 1) % HASHES_BUF_ENTRIES;
  123. while (die->hashes_buf_idx != cur_idx) {
  124. die->hashes_buf[die->hashes_buf_idx] = 0;
  125. die->hashes_buf_idx = (die->hashes_buf_idx + 1) % HASHES_BUF_ENTRIES;
  126. }
  127. }
  128. static bool knc_titan_detect_one(const char *devpath)
  129. {
  130. static struct cgpu_info *prev_cgpu = NULL;
  131. struct cgpu_info *cgpu;
  132. void *ctx;
  133. struct knc_titan_info *knc;
  134. int cores = 0, asic, die;
  135. struct knc_die_info die_info;
  136. char repr[6];
  137. cgpu = malloc(sizeof(*cgpu));
  138. if (unlikely(!cgpu))
  139. quit(1, "Failed to alloc cgpu_info");
  140. if (!prev_cgpu) {
  141. if (NULL == (ctx = knc_trnsp_new(NULL))) {
  142. free(cgpu);
  143. return false;
  144. }
  145. knc = calloc(1, sizeof(*knc));
  146. if (unlikely(!knc))
  147. quit(1, "Failed to alloc knc_titan_info");
  148. knc->ctx = ctx;
  149. knc->cgpu = cgpu;
  150. knc->workqueue_max = WORK_QUEUE_PREFILL;
  151. } else {
  152. knc = prev_cgpu->device_data;
  153. ctx = knc->ctx;
  154. }
  155. snprintf(repr, sizeof(repr), "%s %s", knc_titan_drv.name, devpath);
  156. asic = atoi(devpath);
  157. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  158. die_info.cores = KNC_TITAN_CORES_PER_DIE; /* core hint */
  159. die_info.version = KNC_VERSION_TITAN;
  160. if (!knc_titan_get_info(repr, ctx, asic, die, &die_info))
  161. die_info.cores = -1;
  162. if (0 < die_info.cores) {
  163. knc->dies[asic][die] = (struct knc_titan_die) {
  164. .asicno = asic,
  165. .dieno = die,
  166. .cores = die_info.cores,
  167. .first_proc = cgpu,
  168. .freq = KNC_TITAN_DEFAULT_FREQUENCY,
  169. };
  170. cores += die_info.cores;
  171. } else {
  172. knc->dies[asic][die] = (struct knc_titan_die) {
  173. .asicno = -INT_MAX,
  174. .dieno = -INT_MAX,
  175. .cores = 0,
  176. .first_proc = NULL,
  177. };
  178. }
  179. }
  180. if (0 == cores) {
  181. free(cgpu);
  182. if (!prev_cgpu) {
  183. free(knc);
  184. knc_trnsp_free(ctx);
  185. }
  186. return false;
  187. }
  188. applog(LOG_NOTICE, "%s: Found ASIC with %d cores", repr, cores);
  189. *cgpu = (struct cgpu_info) {
  190. .drv = &knc_titan_drv,
  191. .device_path = strdup(devpath),
  192. .set_device_funcs = knc_titan_set_device_funcs,
  193. .deven = DEV_ENABLED,
  194. .procs = cores,
  195. .threads = prev_cgpu ? 0 : 1,
  196. .extra_work_queue = -1,
  197. .device_data = knc,
  198. };
  199. const bool rv = add_cgpu_slave(cgpu, prev_cgpu);
  200. if (!prev_cgpu) {
  201. cgpu->extra_work_queue += WORK_QUEUE_PREFILL - opt_queue;
  202. if (0 > cgpu->extra_work_queue)
  203. cgpu->extra_work_queue = 0;
  204. }
  205. prev_cgpu = cgpu;
  206. return rv;
  207. }
  208. static int knc_titan_detect_auto(void)
  209. {
  210. const int first = 0, last = KNC_TITAN_MAX_ASICS - 1;
  211. char devpath[256];
  212. int found = 0, i;
  213. for (i = first; i <= last; ++i) {
  214. sprintf(devpath, "%d", i);
  215. if (knc_titan_detect_one(devpath))
  216. ++found;
  217. }
  218. return found;
  219. }
  220. static void knc_titan_detect(void)
  221. {
  222. generic_detect(&knc_titan_drv, knc_titan_detect_one, knc_titan_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  223. }
  224. static void knc_titan_clean_flush(const char *repr, void * const ctx, struct knc_titan_core *knccore)
  225. {
  226. struct knc_report report;
  227. bool unused;
  228. if (knc_titan_set_work(repr, ctx, knccore->asicno, knccore->dieno, knccore->coreno, 0, NULL, true, &unused, &report)) {
  229. knccore->last_nonce.slot = report.nonce[0].slot;
  230. knccore->last_nonce.nonce = report.nonce[0].nonce;
  231. }
  232. }
  233. static uint32_t nonce_tops[KNC_TITAN_CORES_PER_DIE];
  234. static bool nonce_tops_inited = false;
  235. static void get_nonce_range(int dieno, int coreno, uint32_t *nonce_bottom, uint32_t *nonce_top)
  236. {
  237. if (!nonce_tops_inited) {
  238. uint32_t top;
  239. double nonce_f, nonce_step;
  240. int core;
  241. nonce_f = 0.0;
  242. nonce_step = 4294967296.0 / KNC_TITAN_CORES_PER_DIE;
  243. for (core = 0; core < KNC_TITAN_CORES_PER_DIE; ++core) {
  244. nonce_f += nonce_step;
  245. if (core < (KNC_TITAN_CORES_PER_DIE - 1))
  246. top = nonce_f;
  247. else
  248. top = 0xFFFFFFFF;
  249. nonce_tops[core] = top;
  250. }
  251. nonce_tops_inited = true;
  252. }
  253. *nonce_top = nonce_tops[coreno];
  254. if (coreno > 0) {
  255. *nonce_bottom = nonce_tops[coreno - 1] + 1;
  256. return;
  257. }
  258. *nonce_bottom = 0;
  259. }
  260. static bool configure_one_die(struct knc_titan_info *knc, int asic, int die)
  261. {
  262. struct cgpu_info *proc, *first_proc;
  263. struct thr_info *mythr;
  264. struct knc_titan_core *knccore;
  265. char *repr;
  266. struct knc_titan_die *die_p;
  267. if ((0 > asic) || (KNC_TITAN_MAX_ASICS <= asic) || (0 > die) || (KNC_TITAN_DIES_PER_ASIC <= die))
  268. return false;
  269. die_p = &(knc->dies[asic][die]);
  270. if (0 >= die_p->cores)
  271. return false;
  272. /* Init nonce ranges for cores */
  273. struct titan_setup_core_params setup_params = {
  274. .bad_address_mask = {0, 0},
  275. .bad_address_match = {0x3FF, 0x3FF},
  276. .difficulty = DEFAULT_DIFF_FILTERING_ZEROES - 1,
  277. .thread_enable = 0xFF,
  278. .thread_base_address = {0, 1, 2, 3, 4, 5, 6, 7},
  279. .lookup_gap_mask = {0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7},
  280. .N_mask = {0, 0, 0, 0, 0, 0, 0, 0},
  281. .N_shift = {0, 0, 0, 0, 0, 0, 0, 0},
  282. .nonce_bottom = 0,
  283. .nonce_top = 0xFFFFFFFF,
  284. };
  285. fill_in_thread_params(opt_knc_threads_per_core, &setup_params);
  286. first_proc = die_p->first_proc;
  287. repr = first_proc->device->dev_repr;
  288. bool success = true;
  289. for (proc = first_proc; proc; proc = proc->next_proc) {
  290. mythr = proc->thr[0];
  291. knccore = mythr->cgpu_data;
  292. if ((asic != knccore->asicno) || (die != knccore->dieno))
  293. break;
  294. knc_titan_clean_flush(repr, knc->ctx, knccore);
  295. get_nonce_range(knccore->dieno, knccore->coreno, &setup_params.nonce_bottom, &setup_params.nonce_top);
  296. applog(LOG_DEBUG, "%s[%d:%d:%d]: Setup core, nonces 0x%08X - 0x%08X", repr, knccore->asicno, knccore->dieno, knccore->coreno, setup_params.nonce_bottom, setup_params.nonce_top);
  297. if (!knc_titan_setup_core_local(repr, knc->ctx, knccore->asicno, knccore->dieno, knccore->coreno, &setup_params))
  298. success = false;
  299. }
  300. applog(LOG_NOTICE, "%s[%d-%d] Die configur%s", repr, asic, die, success ? "ed successfully" : "ation failed");
  301. die_p->need_flush = true;
  302. timer_set_now(&(die_p->last_share));
  303. die_p->broadcast_flushes = false;
  304. die_p->manual_check_count = 0;
  305. return true;
  306. }
  307. static
  308. float titan_min_nonce_diff(struct cgpu_info * const proc, const struct mining_algorithm * const malgo)
  309. {
  310. return (malgo->algo == POW_SCRYPT) ? DEFAULT_DIFF_FILTERING_FLOAT : -1.;
  311. }
  312. static bool knc_titan_init(struct thr_info * const thr)
  313. {
  314. const int max_cores = KNC_TITAN_CORES_PER_ASIC;
  315. struct thr_info *mythr;
  316. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  317. struct knc_titan_core *knccore;
  318. struct knc_titan_info *knc;
  319. int i, asic, die, core_base;
  320. int total_cores = 0;
  321. int asic_cores[KNC_TITAN_MAX_ASICS] = {0};
  322. for (proc = cgpu; proc; ) {
  323. if (proc->device != proc) {
  324. applog(LOG_WARNING, "%"PRIpreprv": Extra processor?", proc->proc_repr);
  325. proc = proc->next_proc;
  326. continue;
  327. }
  328. asic = atoi(proc->device_path);
  329. knc = proc->device_data;
  330. die = 0;
  331. core_base = 0;
  332. for (i = 0; i < max_cores; ++i) {
  333. while (i >= (core_base + knc->dies[asic][die].cores)) {
  334. core_base += knc->dies[asic][die].cores;
  335. if (++die >= KNC_TITAN_DIES_PER_ASIC)
  336. break;
  337. }
  338. if (die >= KNC_TITAN_DIES_PER_ASIC)
  339. break;
  340. mythr = proc->thr[0];
  341. mythr->cgpu_data = knccore = malloc(sizeof(*knccore));
  342. if (unlikely(!knccore))
  343. quit(1, "Failed to alloc knc_titan_core");
  344. *knccore = (struct knc_titan_core) {
  345. .asicno = asic,
  346. .dieno = die,
  347. .coreno = i - core_base,
  348. .die = &(knc->dies[asic][die]),
  349. .proc = proc,
  350. .hwerr_in_row = 0,
  351. .hwerr_disable_time = KNC_TITAN_HWERR_DISABLE_SECS,
  352. .need_manual_check = false,
  353. };
  354. timer_set_now(&knccore->enable_at);
  355. proc->device_data = knc;
  356. ++total_cores;
  357. ++(asic_cores[asic]);
  358. applog(LOG_DEBUG, "%s Allocated core %d:%d:%d", proc->device->dev_repr, asic, die, (i - core_base));
  359. if (0 == knccore->coreno) {
  360. knc->dies[asic][die].first_proc = proc;
  361. }
  362. proc = proc->next_proc;
  363. if ((!proc) || proc->device == proc)
  364. break;
  365. }
  366. knc->cores = total_cores;
  367. knc->asic_served_by_fpga[asic] = true;
  368. }
  369. cgpu_set_defaults(cgpu);
  370. cgpu_setup_control_requests(cgpu);
  371. if (0 >= total_cores)
  372. return false;
  373. knc = cgpu->device_data;
  374. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  375. knc_titan_setup_spi("ASIC", knc->ctx, asic, KNC_TITAN_FPGA_SPI_DIVIDER,
  376. KNC_TITAN_FPGA_SPI_PRECLK, KNC_TITAN_FPGA_SPI_DECLK,
  377. KNC_TITAN_FPGA_SPI_SSLOWMIN);
  378. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  379. configure_one_die(knc, asic, die);
  380. knc->dies[asic][die].next_slot = KNC_TITAN_MIN_WORK_SLOT_NUM;
  381. knc->dies[asic][die].first_slot = KNC_TITAN_MIN_WORK_SLOT_NUM;
  382. }
  383. }
  384. timer_set_now(&thr->tv_poll);
  385. return true;
  386. }
  387. static bool die_test_and_add(struct knc_titan_info * const knc, int asic, int die, char * const errbuf)
  388. {
  389. struct knc_die_info die_info;
  390. char repr[6];
  391. snprintf(repr, sizeof(repr), "%s %d", knc_titan_drv.name, asic);
  392. die_info.cores = KNC_TITAN_CORES_PER_DIE; /* core hint */
  393. die_info.version = KNC_VERSION_TITAN;
  394. if (!knc_titan_get_info(repr, knc->ctx, asic, die, &die_info))
  395. die_info.cores = -1;
  396. if (0 < die_info.cores) {
  397. sprintf(errbuf, "Die[%d:%d] not detected", asic, die);
  398. return false;
  399. }
  400. /* TODO: add procs */
  401. sprintf(errbuf, "Die[%d:%d] has %d cores; was not added (addition not implemented)", asic, die, die_info.cores);
  402. return false;
  403. }
  404. static bool die_enable(struct knc_titan_info * const knc, int asic, int die, char * const errbuf)
  405. {
  406. bool res = true;
  407. cgpu_request_control(knc->cgpu);
  408. if (0 >= knc->dies[asic][die].cores)
  409. res = die_test_and_add(knc, asic, die, errbuf);
  410. if (res) {
  411. res = configure_one_die(knc, asic, die);
  412. }
  413. cgpu_release_control(knc->cgpu);
  414. return res;
  415. }
  416. static bool die_disable(struct knc_titan_info * const knc, int asic, int die, char * const errbuf)
  417. {
  418. cgpu_request_control(knc->cgpu);
  419. /* TODO: delete procs */
  420. cgpu_release_control(knc->cgpu);
  421. sprintf(errbuf, "die_disable[%d:%d] not imnplemented", asic, die);
  422. return false;
  423. }
  424. static bool die_reconfigure(struct knc_titan_info * const knc, int asic, int die, char * const errbuf)
  425. {
  426. return die_enable(knc, asic, die, errbuf);
  427. }
  428. static bool knc_titan_prepare_work(struct thr_info *thr, struct work *work)
  429. {
  430. work->nonce_diff = DEFAULT_DIFF_FILTERING_FLOAT;
  431. return true;
  432. }
  433. static void knc_titan_set_queue_full(struct knc_titan_info * const knc)
  434. {
  435. const bool full = (knc->workqueue_size >= knc->workqueue_max);
  436. struct cgpu_info *proc;
  437. for (proc = knc->cgpu; proc; proc = proc->next_proc) {
  438. struct thr_info * const thr = proc->thr[0];
  439. thr->queue_full = full;
  440. }
  441. }
  442. static void knc_titan_remove_local_queue(struct knc_titan_info * const knc, struct work * const work)
  443. {
  444. DL_DELETE(knc->workqueue, work);
  445. free_work(work);
  446. --knc->workqueue_size;
  447. }
  448. static void knc_titan_prune_local_queue(struct thr_info *thr)
  449. {
  450. struct cgpu_info * const cgpu = thr->cgpu;
  451. struct knc_titan_info * const knc = cgpu->device_data;
  452. struct work *work, *tmp;
  453. DL_FOREACH_SAFE(knc->workqueue, work, tmp) {
  454. if (stale_work(work, false))
  455. knc_titan_remove_local_queue(knc, work);
  456. }
  457. knc_titan_set_queue_full(knc);
  458. }
  459. static bool knc_titan_queue_append(struct thr_info * const thr, struct work * const work)
  460. {
  461. struct cgpu_info * const cgpu = thr->cgpu;
  462. struct knc_titan_info * const knc = cgpu->device_data;
  463. if (knc->workqueue_size >= knc->workqueue_max) {
  464. knc_titan_prune_local_queue(thr);
  465. if (thr->queue_full)
  466. return false;
  467. }
  468. DL_APPEND(knc->workqueue, work);
  469. ++knc->workqueue_size;
  470. knc_titan_set_queue_full(knc);
  471. if (thr->queue_full)
  472. knc_titan_prune_local_queue(thr);
  473. return true;
  474. }
  475. #define HASH_LAST_ADDED(head, out) \
  476. (out = (head) ? (ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail)) : NULL)
  477. static void knc_titan_queue_flush(struct thr_info * const thr)
  478. {
  479. struct cgpu_info * const cgpu = thr->cgpu;
  480. struct knc_titan_info * const knc = cgpu->device_data;
  481. struct work *work, *tmp;
  482. if (knc->cgpu != cgpu)
  483. return;
  484. DL_FOREACH_SAFE(knc->workqueue, work, tmp){
  485. knc_titan_remove_local_queue(knc, work);
  486. }
  487. knc_titan_set_queue_full(knc);
  488. HASH_LAST_ADDED(knc->devicework, work);
  489. if (work && stale_work(work, true)) {
  490. int asic, die;
  491. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  492. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  493. knc->dies[asic][die].need_flush = true;
  494. }
  495. knc->asic_served_by_fpga[asic] = true;
  496. }
  497. timer_set_now(&thr->tv_poll);
  498. }
  499. }
  500. #define MAKE_WORKID(asic, die, slot) ((((uint32_t)(asic)) << 16) | ((uint32_t)(die) << 8) | ((uint32_t)(slot)))
  501. #define ASIC_FROM_WORKID(workid) ((((uint32_t)(workid)) >> 16) & 0xFF)
  502. #define DIE_FROM_WORKID(workid) ((((uint32_t)(workid)) >> 8) & 0xFF)
  503. #define SLOT_FROM_WORKID(workid) (((uint32_t)(workid)) & 0xFF)
  504. static bool knc_titan_process_report(struct knc_titan_info * const knc, struct knc_titan_core * const knccore, struct knc_report * const report)
  505. {
  506. int i, tmp_int;
  507. struct work *work;
  508. struct cgpu_info * const proc = knccore->proc;
  509. bool ret = false;
  510. for (i = 0; i < KNC_TITAN_NONCES_PER_REPORT; ++i) {
  511. if ((report->nonce[i].slot == knccore->last_nonce.slot) &&
  512. (report->nonce[i].nonce == knccore->last_nonce.nonce))
  513. break;
  514. ret = true;
  515. tmp_int = MAKE_WORKID(knccore->asicno, knccore->dieno, report->nonce[i].slot);
  516. HASH_FIND_INT(knc->devicework, &tmp_int, work);
  517. if (!work) {
  518. applog(LOG_WARNING, "%"PRIpreprv"[%d:%d:%d]: Got nonce for unknown work in slot %u", proc->proc_repr, knccore->asicno, knccore->dieno, knccore->coreno, (unsigned)report->nonce[i].slot);
  519. continue;
  520. }
  521. if (submit_nonce(proc->thr[0], work, report->nonce[i].nonce)) {
  522. hashes_done2(proc->thr[0], DEFAULT_DIFF_HASHES_PER_NONCE, NULL);
  523. knc_titan_die_hashmeter(knccore->die, DEFAULT_DIFF_HASHES_PER_NONCE);
  524. knccore->hwerr_in_row = 0;
  525. }
  526. }
  527. knccore->last_nonce.slot = report->nonce[0].slot;
  528. knccore->last_nonce.nonce = report->nonce[0].nonce;
  529. knccore->need_manual_check = false;
  530. return ret;
  531. }
  532. static void knc_titan_poll(struct thr_info * const thr)
  533. {
  534. struct thr_info *mythr;
  535. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  536. struct knc_titan_info * const knc = cgpu->device_data;
  537. struct knc_titan_core *knccore;
  538. struct work *work, *tmp;
  539. int workaccept = 0;
  540. unsigned long delay_usecs = KNC_POLL_INTERVAL_US;
  541. struct knc_report report;
  542. struct knc_die_info die_info;
  543. int asic;
  544. int die;
  545. struct knc_titan_die *die_p;
  546. struct timeval tv_now;
  547. int num_request_busy;
  548. int num_status_byte_error[4];
  549. bool fpga_status_checked;
  550. knc_titan_prune_local_queue(thr);
  551. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  552. fpga_status_checked = false;
  553. num_request_busy = KNC_TITAN_DIES_PER_ASIC;
  554. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  555. die_p = &(knc->dies[asic][die]);
  556. /* make sure the running average buffers are updated even in the absence of found nonces */
  557. knc_titan_die_hashmeter(die_p, 0);
  558. if (0 >= die_p->cores)
  559. continue;
  560. struct cgpu_info *first_proc = die_p->first_proc;
  561. DL_FOREACH_SAFE(knc->workqueue, work, tmp) {
  562. bool work_accepted = false;
  563. bool need_replace;
  564. if (die_p->first_slot > KNC_TITAN_MIN_WORK_SLOT_NUM)
  565. need_replace = ((die_p->next_slot + 1) == die_p->first_slot);
  566. else
  567. need_replace = (die_p->next_slot == KNC_TITAN_MAX_WORK_SLOT_NUM);
  568. if (die_p->need_flush || need_replace) {
  569. bool unused;
  570. if (die_p->broadcast_flushes) {
  571. /* Use broadcast */
  572. if (knc_titan_set_work(first_proc->device->dev_repr, knc->ctx, asic, die, ALL_CORES, die_p->next_slot, work, true, &unused, &report)) {
  573. work_accepted = true;
  574. }
  575. } else {
  576. /* Use FPGA accelerated unicasts */
  577. if (!fpga_status_checked) {
  578. timer_set_now(&knc->tv_prev);
  579. knc_titan_get_work_status(first_proc->device->dev_repr, knc->ctx, asic, &num_request_busy, num_status_byte_error);
  580. fpga_status_checked = true;
  581. }
  582. if (num_request_busy == 0) {
  583. if (knc_titan_set_work_parallel(first_proc->device->dev_repr, knc->ctx, asic, 1 << die, 0, die_p->next_slot, work, true, die_p->cores, KNC_TITAN_FPGA_RETRIES)) {
  584. work_accepted = true;
  585. }
  586. }
  587. }
  588. } else {
  589. if (knc->asic_served_by_fpga[asic]) {
  590. knc_titan_get_work_status(first_proc->device->dev_repr, knc->ctx, asic, &num_request_busy, num_status_byte_error);
  591. if (num_request_busy == 0) {
  592. timer_set_now(&tv_now);
  593. double diff = ((tv_now.tv_sec - knc->tv_prev.tv_sec) * 1000000.0 + (tv_now.tv_usec - knc->tv_prev.tv_usec)) / 1000000.0;
  594. applog(LOG_INFO, "%s: Flush took %f secs for ASIC %d", knc_titan_drv.dname, diff, asic);
  595. applog(LOG_DEBUG, "FPGA CRC error counters: %d %d %d %d", num_status_byte_error[0], num_status_byte_error[1], num_status_byte_error[2], num_status_byte_error[3]);
  596. knc->asic_served_by_fpga[asic] = false;
  597. for (int die2 = 0; die2 < KNC_TITAN_DIES_PER_ASIC; ++die2) {
  598. knc->dies[asic][die2].manual_check_count = KNC_TITAN_CORES_PER_DIE - MANUAL_CHECK_CORES_PER_POLL;
  599. for (proc = knc->dies[asic][die2].first_proc; proc; proc = proc->next_proc) {
  600. mythr = proc->thr[0];
  601. knccore = mythr->cgpu_data;
  602. knccore->need_manual_check = true;
  603. }
  604. }
  605. }
  606. }
  607. if (knc->asic_served_by_fpga[asic] || !knc_titan_set_work(first_proc->dev_repr, knc->ctx, asic, die, ALL_CORES, die_p->next_slot, work, false, &work_accepted, &report))
  608. work_accepted = false;
  609. }
  610. knccore = first_proc->thr[0]->cgpu_data;
  611. if ((!work_accepted) || (NULL == knccore))
  612. break;
  613. bool was_flushed = false;
  614. if (die_p->need_flush || need_replace) {
  615. applog(LOG_NOTICE, "%s[%d-%d] Flushing stale works (%s)", first_proc->dev_repr, asic, die,
  616. die_p->need_flush ? "New work" : "Slot collision");
  617. die_p->need_flush = false;
  618. die_p->first_slot = die_p->next_slot;
  619. delay_usecs = 0;
  620. was_flushed = true;
  621. }
  622. --knc->workqueue_size;
  623. DL_DELETE(knc->workqueue, work);
  624. work->device_id = MAKE_WORKID(asic, die, die_p->next_slot);
  625. struct work *work1, *tmp1;
  626. HASH_ITER(hh, knc->devicework, work1, tmp1) {
  627. if (work->device_id == work1->device_id) {
  628. HASH_DEL(knc->devicework, work1);
  629. free_work(work1);
  630. }
  631. }
  632. HASH_ADD(hh, knc->devicework, device_id, sizeof(work->device_id), work);
  633. if (++(die_p->next_slot) > KNC_TITAN_MAX_WORK_SLOT_NUM)
  634. die_p->next_slot = KNC_TITAN_MIN_WORK_SLOT_NUM;
  635. ++workaccept;
  636. /* If we know for sure that this work was urgent, then we don't need to hurry up
  637. * with filling next slot, we have plenty of time until current work completes.
  638. * So, better to proceed with other ASICs/dies. */
  639. if (was_flushed)
  640. break;
  641. }
  642. }
  643. }
  644. applog(LOG_DEBUG, "%s: %d jobs accepted to queue (max=%d)", knc_titan_drv.dname, workaccept, knc->workqueue_max);
  645. timer_set_now(&tv_now);
  646. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  647. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  648. die_p = &(knc->dies[asic][die]);
  649. if (0 >= die_p->cores)
  650. continue;
  651. die_info.cores = die_p->cores; /* core hint */
  652. die_info.version = KNC_VERSION_TITAN;
  653. if (knc->asic_served_by_fpga[asic] || !knc_titan_get_info(cgpu->dev_repr, knc->ctx, asic, die, &die_info))
  654. continue;
  655. for (proc = die_p->first_proc; proc; proc = proc->next_proc) {
  656. mythr = proc->thr[0];
  657. knccore = mythr->cgpu_data;
  658. thread_reportin(mythr);
  659. if ((knccore->dieno != die) || (knccore->asicno != asic))
  660. break;
  661. if (!die_info.has_report[knccore->coreno])
  662. continue;
  663. if (!knc_titan_get_report(proc->proc_repr, knc->ctx, asic, die, knccore->coreno, &report))
  664. continue;
  665. if (knc_titan_process_report(knc, knccore, &report))
  666. timer_set_now(&(die_p->last_share));
  667. }
  668. }
  669. /* Check die health */
  670. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  671. die_p = &(knc->dies[asic][die]);
  672. if (0 >= die_p->cores)
  673. continue;
  674. if (timer_elapsed(&(die_p->last_share), &tv_now) < DIE_HEALTH_INTERVAL_SEC)
  675. continue;
  676. /* Reconfigure die */
  677. configure_one_die(knc, asic, die);
  678. }
  679. }
  680. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  681. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  682. die_p = &(knc->dies[asic][die]);
  683. if (0 >= die_p->cores || die_p->manual_check_count < 0)
  684. continue;
  685. for (proc = die_p->first_proc; proc; proc = proc->next_proc) {
  686. mythr = proc->thr[0];
  687. knccore = mythr->cgpu_data;
  688. int core = knccore->coreno;
  689. if (core < die_p->manual_check_count)
  690. continue;
  691. if (core >= die_p->manual_check_count + MANUAL_CHECK_CORES_PER_POLL)
  692. break;
  693. if ((knccore->dieno != die) || (knccore->asicno != asic))
  694. break;
  695. if (!knccore->need_manual_check)
  696. continue;
  697. if (!knc_titan_get_report(proc->proc_repr, knc->ctx, asic, die, knccore->coreno, &report))
  698. continue;
  699. if (knc_titan_process_report(knc, knccore, &report))
  700. timer_set_now(&(die_p->last_share));
  701. }
  702. if (die_p->manual_check_count == 0) {
  703. die_p->manual_check_count = -1;
  704. } else {
  705. die_p->manual_check_count -= MANUAL_CHECK_CORES_PER_POLL;
  706. if (die_p->manual_check_count < 0)
  707. die_p->manual_check_count = 0;
  708. }
  709. }
  710. }
  711. if (workaccept) {
  712. if (workaccept >= knc->workqueue_max) {
  713. knc->workqueue_max = workaccept;
  714. delay_usecs = 0;
  715. }
  716. knc_titan_set_queue_full(knc);
  717. }
  718. timer_set_delay_from_now(&thr->tv_poll, delay_usecs);
  719. }
  720. /*
  721. * specify settings / options via RPC or command line
  722. */
  723. /* support for --set-device
  724. * must be set before probing the device
  725. */
  726. static void knc_titan_set_clock_freq(struct cgpu_info * const device, int const val)
  727. {
  728. }
  729. static const char *knc_titan_set_clock(struct cgpu_info * const device, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  730. {
  731. knc_titan_set_clock_freq(device, atoi(setting));
  732. return NULL;
  733. }
  734. static const char *knc_titan_die_ena(struct cgpu_info * const device, const char * const option, const char * const setting, char * const replybuf, enum bfg_set_device_replytype * const success)
  735. {
  736. int asic, die;
  737. char str[256];
  738. /* command format: ASIC:N;DIE:N;MODE:ENABLE|DISABLE|RECONFIGURE */
  739. if (3 != sscanf(setting, "ASIC:%d;DIE:%d;MODE:%255s", &asic, &die, str)) {
  740. error_bad_params:
  741. sprintf(replybuf, "Die setup failed, bad parameters");
  742. return replybuf;
  743. }
  744. if (0 == strncasecmp(str, "enable", sizeof(str) - 1)) {
  745. if (!die_enable(device->device_data, asic, die, replybuf))
  746. return replybuf;
  747. } else if (0 == strncasecmp(str, "disable", sizeof(str) - 1)) {
  748. if (!die_disable(device->device_data, asic, die, replybuf))
  749. return replybuf;
  750. } else if (0 == strncasecmp(str, "reconfigure", sizeof(str) - 1)) {
  751. if (!die_reconfigure(device->device_data, asic, die, replybuf)) {
  752. /* Do not return error on reconfigure command!
  753. * (or the whole bfgminer will be restarted) */
  754. *success = SDR_OK;
  755. return replybuf;
  756. }
  757. } else
  758. goto error_bad_params;
  759. sprintf(replybuf, "Die setup Ok; asic %d die %d cmd %s", asic, die, str);
  760. *success = SDR_OK;
  761. return replybuf;
  762. }
  763. static const struct bfg_set_device_definition knc_titan_set_device_funcs[] = {
  764. { "clock", knc_titan_set_clock, NULL },
  765. { "die", knc_titan_die_ena, NULL },
  766. { NULL },
  767. };
  768. /*
  769. * specify settings / options via TUI
  770. */
  771. #ifdef HAVE_CURSES
  772. static void knc_titan_tui_wlogprint_choices(struct cgpu_info * const proc)
  773. {
  774. wlogprint("[C]lock speed ");
  775. }
  776. static const char *knc_titan_tui_handle_choice(struct cgpu_info * const proc, const int input)
  777. {
  778. static char buf[0x100]; /* Static for replies */
  779. switch (input)
  780. {
  781. case 'c': case 'C':
  782. {
  783. sprintf(buf, "Set clock speed");
  784. char * const setting = curses_input(buf);
  785. knc_titan_set_clock_freq(proc->device, atoi(setting));
  786. return "Clock speed changed\n";
  787. }
  788. }
  789. return NULL;
  790. }
  791. static void knc_titan_wlogprint_status(struct cgpu_info * const proc)
  792. {
  793. wlogprint("Clock speed: N/A\n");
  794. }
  795. #endif
  796. struct device_drv knc_titan_drv =
  797. {
  798. /* metadata */
  799. .dname = "titan",
  800. .name = "KNC",
  801. .drv_min_nonce_diff = titan_min_nonce_diff,
  802. .drv_detect = knc_titan_detect,
  803. .thread_init = knc_titan_init,
  804. /* specify mining type - queue */
  805. .minerloop = minerloop_queue,
  806. .queue_append = knc_titan_queue_append,
  807. .queue_flush = knc_titan_queue_flush,
  808. .poll = knc_titan_poll,
  809. .prepare_work = knc_titan_prepare_work,
  810. /* additional statistics */
  811. .get_master_rolling_hashrate = knc_titan_get_device_rolling_hashrate,
  812. .zero_stats = knc_titan_zero_stats,
  813. /* TUI support - e.g. setting clock via UI */
  814. #ifdef HAVE_CURSES
  815. .proc_wlogprint_status = knc_titan_wlogprint_status,
  816. .proc_tui_wlogprint_choices = knc_titan_tui_wlogprint_choices,
  817. .proc_tui_handle_choice = knc_titan_tui_handle_choice,
  818. #endif
  819. };