driver-titan.c 24 KB

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