driver-titan.c 25 KB

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