driver-titan.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 600
  18. #define KNC_TITAN_HWERR_DISABLE_SECS 10
  19. #define KNC_POLL_INTERVAL_US 10000
  20. /* Specify here minimum number of leading zeroes in hash */
  21. #define DEFAULT_DIFF_FILTERING_ZEROES 12
  22. #define DEFAULT_DIFF_FILTERING_FLOAT (1. / ((double)(0x00000000FFFFFFFF >> DEFAULT_DIFF_FILTERING_ZEROES)))
  23. #define DEFAULT_DIFF_HASHES_PER_NONCE (1 << DEFAULT_DIFF_FILTERING_ZEROES)
  24. BFG_REGISTER_DRIVER(knc_titan_drv)
  25. static const struct bfg_set_device_definition knc_titan_set_device_funcs[];
  26. struct knc_titan_core {
  27. int asicno;
  28. int dieno; /* inside asic */
  29. int coreno; /* inside die */
  30. struct knc_titan_die *die;
  31. struct cgpu_info *proc;
  32. int next_slot;
  33. int hwerr_in_row;
  34. int hwerr_disable_time;
  35. struct timeval enable_at;
  36. struct timeval first_hwerr;
  37. struct nonce_report last_nonce;
  38. };
  39. struct knc_titan_die {
  40. int asicno;
  41. int dieno; /* inside asic */
  42. int cores;
  43. struct cgpu_info *first_proc;
  44. int freq;
  45. };
  46. struct knc_titan_info {
  47. void *ctx;
  48. struct cgpu_info *cgpu;
  49. int cores;
  50. struct knc_titan_die dies[KNC_TITAN_MAX_ASICS][KNC_TITAN_DIES_PER_ASIC];
  51. bool need_flush;
  52. struct work *workqueue;
  53. int workqueue_size;
  54. int workqueue_max;
  55. int next_id;
  56. struct work *devicework;
  57. };
  58. static bool knc_titan_detect_one(const char *devpath)
  59. {
  60. static struct cgpu_info *prev_cgpu = NULL;
  61. struct cgpu_info *cgpu;
  62. void *ctx;
  63. struct knc_titan_info *knc;
  64. int cores = 0, asic, die;
  65. struct knc_die_info die_info;
  66. char repr[6];
  67. cgpu = malloc(sizeof(*cgpu));
  68. if (unlikely(!cgpu))
  69. quit(1, "Failed to alloc cgpu_info");
  70. if (!prev_cgpu) {
  71. if (NULL == (ctx = knc_trnsp_new(NULL))) {
  72. free(cgpu);
  73. return false;
  74. }
  75. knc = calloc(1, sizeof(*knc));
  76. if (unlikely(!knc))
  77. quit(1, "Failed to alloc knc_titan_info");
  78. knc->ctx = ctx;
  79. knc->cgpu = cgpu;
  80. knc->workqueue_max = KNC_TITAN_WORKSLOTS_PER_CORE + 1;
  81. } else {
  82. knc = prev_cgpu->device_data;
  83. ctx = knc->ctx;
  84. }
  85. snprintf(repr, sizeof(repr), "%s %s", knc_titan_drv.name, devpath);
  86. asic = atoi(devpath);
  87. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  88. die_info.cores = KNC_TITAN_CORES_PER_DIE; /* core hint */
  89. if (!knc_titan_get_info(repr, ctx, asic, die, &die_info))
  90. continue;
  91. if (0 < die_info.cores) {
  92. knc->dies[asic][die] = (struct knc_titan_die) {
  93. .asicno = asic,
  94. .dieno = die,
  95. .cores = die_info.cores,
  96. .first_proc = cgpu,
  97. .freq = KNC_TITAN_DEFAULT_FREQUENCY,
  98. };
  99. cores += die_info.cores;
  100. } else {
  101. knc->dies[asic][die] = (struct knc_titan_die) {
  102. .asicno = -INT_MAX,
  103. .dieno = -INT_MAX,
  104. .cores = 0,
  105. .first_proc = NULL,
  106. };
  107. }
  108. }
  109. if (0 == cores) {
  110. free(cgpu);
  111. if (!prev_cgpu) {
  112. free(knc);
  113. knc_trnsp_free(ctx);
  114. }
  115. return false;
  116. }
  117. applog(LOG_NOTICE, "%s: Found ASIC with %d cores", repr, cores);
  118. *cgpu = (struct cgpu_info) {
  119. .drv = &knc_titan_drv,
  120. .device_path = strdup(devpath),
  121. .set_device_funcs = knc_titan_set_device_funcs,
  122. .deven = DEV_ENABLED,
  123. .procs = cores,
  124. .threads = prev_cgpu ? 0 : 1,
  125. .device_data = knc,
  126. };
  127. const bool rv = add_cgpu_slave(cgpu, prev_cgpu);
  128. prev_cgpu = cgpu;
  129. return rv;
  130. }
  131. static int knc_titan_detect_auto(void)
  132. {
  133. const int first = 0, last = KNC_TITAN_MAX_ASICS - 1;
  134. char devpath[256];
  135. int found = 0, i;
  136. for (i = first; i <= last; ++i) {
  137. sprintf(devpath, "%d", i);
  138. if (knc_titan_detect_one(devpath))
  139. ++found;
  140. }
  141. return found;
  142. }
  143. static void knc_titan_detect(void)
  144. {
  145. generic_detect(&knc_titan_drv, knc_titan_detect_one, knc_titan_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  146. }
  147. static void knc_titan_clean_flush(const char *repr, void * const ctx, int asic, int die)
  148. {
  149. struct knc_report report;
  150. bool unused;
  151. knc_titan_set_work(repr, ctx, asic, die, 0xFFFF, 0, NULL, true, &unused, &report);
  152. }
  153. static bool knc_titan_init(struct thr_info * const thr)
  154. {
  155. const int max_cores = KNC_TITAN_CORES_PER_ASIC;
  156. struct thr_info *mythr;
  157. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  158. struct knc_titan_core *knccore;
  159. struct knc_titan_info *knc;
  160. int i, asic, die, core_base;
  161. int total_cores = 0;
  162. for (proc = cgpu; proc; ) {
  163. proc->min_nonce_diff = DEFAULT_DIFF_FILTERING_FLOAT;
  164. if (proc->device != proc) {
  165. applog(LOG_WARNING, "%"PRIpreprv": Extra processor?", proc->proc_repr);
  166. proc = proc->next_proc;
  167. continue;
  168. }
  169. asic = atoi(proc->device_path);
  170. knc = proc->device_data;
  171. die = 0;
  172. core_base = 0;
  173. for (i = 0; i < max_cores; ++i) {
  174. while (i >= (core_base + knc->dies[asic][die].cores)) {
  175. core_base += knc->dies[asic][die].cores;
  176. if (++die >= KNC_TITAN_DIES_PER_ASIC)
  177. break;
  178. }
  179. if (die >= KNC_TITAN_DIES_PER_ASIC)
  180. break;
  181. mythr = proc->thr[0];
  182. mythr->cgpu_data = knccore = malloc(sizeof(*knccore));
  183. if (unlikely(!knccore))
  184. quit(1, "Failed to alloc knc_titan_core");
  185. *knccore = (struct knc_titan_core) {
  186. .asicno = asic,
  187. .dieno = die,
  188. .coreno = i - core_base,
  189. .next_slot = 1,
  190. .die = &(knc->dies[asic][die]),
  191. .proc = proc,
  192. .hwerr_in_row = 0,
  193. .hwerr_disable_time = KNC_TITAN_HWERR_DISABLE_SECS,
  194. };
  195. timer_set_now(&knccore->enable_at);
  196. proc->device_data = knc;
  197. ++total_cores;
  198. applog(LOG_DEBUG, "%s Allocated core %d:%d:%d", proc->device->dev_repr, asic, die, (i - core_base));
  199. if (0 == knccore->coreno)
  200. knc_titan_clean_flush(proc->device->dev_repr, knc->ctx, knccore->asicno, knccore->dieno);
  201. proc = proc->next_proc;
  202. if ((!proc) || proc->device == proc)
  203. break;
  204. }
  205. knc->cores = total_cores;
  206. }
  207. cgpu_set_defaults(cgpu);
  208. if (0 >= total_cores)
  209. return false;
  210. /* Init nonce ranges for cores */
  211. double nonce_step = 4294967296.0 / total_cores;
  212. double nonce_f = 0.0;
  213. struct titan_setup_core_params setup_params = {
  214. .bad_address_mask = {0, 0},
  215. .bad_address_match = {0x3FF, 0x3FF},
  216. .difficulty = DEFAULT_DIFF_FILTERING_ZEROES,
  217. .thread_enable = 0xFF,
  218. .thread_base_address = {0, 1, 2, 3, 4, 5, 6, 7},
  219. .lookup_gap_mask = {0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7},
  220. .N_mask = {0, 0, 0, 0, 0, 0, 0, 0},
  221. .N_shift = {0, 0, 0, 0, 0, 0, 0, 0},
  222. .nonce_bottom = 0,
  223. .nonce_top = 0xFFFFFFFF,
  224. };
  225. for (proc = cgpu; proc; proc = proc->next_proc) {
  226. nonce_f += nonce_step;
  227. setup_params.nonce_bottom = setup_params.nonce_top + 1;
  228. if (NULL != proc->next_proc)
  229. setup_params.nonce_top = nonce_f;
  230. else
  231. setup_params.nonce_top = 0xFFFFFFFF;
  232. knc = proc->device_data;
  233. mythr = proc->thr[0];
  234. knccore = mythr->cgpu_data;
  235. applog(LOG_DEBUG, "%s Setup core %d:%d:%d, nonces 0x%08X - 0x%08X", proc->device->dev_repr, knccore->asicno, knccore->dieno, knccore->coreno, setup_params.nonce_bottom, setup_params.nonce_top);
  236. knc_titan_setup_core(proc->device->dev_repr, knc->ctx, knccore->asicno, knccore->dieno, knccore->coreno, &setup_params);
  237. }
  238. timer_set_now(&thr->tv_poll);
  239. return true;
  240. }
  241. static bool knc_titan_prepare_work(struct thr_info *thr, struct work *work)
  242. {
  243. struct cgpu_info * const cgpu = thr->cgpu;
  244. work->nonce_diff = cgpu->min_nonce_diff;
  245. return true;
  246. }
  247. static void knc_titan_clear_last_nonce(struct knc_titan_info * const knc)
  248. {
  249. struct thr_info * mythr;
  250. struct cgpu_info *proc;
  251. struct knc_titan_core *knccore;
  252. for (proc = knc->cgpu; proc; proc = proc->next_proc) {
  253. mythr = proc->thr[0];
  254. knccore = mythr->cgpu_data;
  255. knccore->last_nonce.slot = 0;
  256. knccore->last_nonce.nonce = 0;
  257. }
  258. }
  259. static void knc_titan_set_queue_full(struct knc_titan_info * const knc)
  260. {
  261. const bool full = (knc->workqueue_size >= knc->workqueue_max);
  262. struct cgpu_info *proc;
  263. for (proc = knc->cgpu; proc; proc = proc->next_proc) {
  264. struct thr_info * const thr = proc->thr[0];
  265. thr->queue_full = full;
  266. }
  267. }
  268. static void knc_titan_remove_local_queue(struct knc_titan_info * const knc, struct work * const work)
  269. {
  270. DL_DELETE(knc->workqueue, work);
  271. free_work(work);
  272. --knc->workqueue_size;
  273. }
  274. static void knc_titan_prune_local_queue(struct thr_info *thr)
  275. {
  276. struct cgpu_info * const cgpu = thr->cgpu;
  277. struct knc_titan_info * const knc = cgpu->device_data;
  278. struct work *work, *tmp;
  279. DL_FOREACH_SAFE(knc->workqueue, work, tmp) {
  280. if (stale_work(work, false))
  281. knc_titan_remove_local_queue(knc, work);
  282. }
  283. knc_titan_set_queue_full(knc);
  284. }
  285. static bool knc_titan_queue_append(struct thr_info * const thr, struct work * const work)
  286. {
  287. struct cgpu_info * const cgpu = thr->cgpu;
  288. struct knc_titan_info * const knc = cgpu->device_data;
  289. if (knc->workqueue_size >= knc->workqueue_max) {
  290. knc_titan_prune_local_queue(thr);
  291. if (thr->queue_full)
  292. return false;
  293. }
  294. DL_APPEND(knc->workqueue, work);
  295. ++knc->workqueue_size;
  296. knc_titan_set_queue_full(knc);
  297. if (thr->queue_full)
  298. knc_titan_prune_local_queue(thr);
  299. return true;
  300. }
  301. #define HASH_LAST_ADDED(head, out) \
  302. (out = (head) ? (ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail)) : NULL)
  303. static void knc_titan_queue_flush(struct thr_info * const thr)
  304. {
  305. struct cgpu_info * const cgpu = thr->cgpu;
  306. struct knc_titan_info * const knc = cgpu->device_data;
  307. struct work *work, *tmp;
  308. if (knc->cgpu != cgpu)
  309. return;
  310. DL_FOREACH_SAFE(knc->workqueue, work, tmp){
  311. knc_titan_remove_local_queue(knc, work);
  312. }
  313. knc_titan_set_queue_full(knc);
  314. HASH_LAST_ADDED(knc->devicework, work);
  315. if (work && stale_work(work, true)) {
  316. knc->need_flush = true;
  317. timer_set_now(&thr->tv_poll);
  318. }
  319. }
  320. static void knc_titan_poll(struct thr_info * const thr)
  321. {
  322. struct thr_info *mythr;
  323. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  324. struct knc_titan_info * const knc = cgpu->device_data;
  325. struct knc_titan_core *knccore;
  326. struct work *work, *tmp;
  327. int workaccept = 0;
  328. unsigned long delay_usecs = KNC_POLL_INTERVAL_US;
  329. struct knc_report report;
  330. struct knc_die_info die_info;
  331. int asic = 0; /* TODO: the asic number must iterate from 0 to 5 */
  332. int die = 0; /* TODO: the die number must iterate from 0 to 3 */
  333. int i, tmp_int;
  334. knc_titan_prune_local_queue(thr);
  335. if (knc->need_flush)
  336. knc_titan_clear_last_nonce(knc);
  337. knccore = cgpu->thr[0]->cgpu_data;
  338. DL_FOREACH_SAFE(knc->workqueue, work, tmp) {
  339. bool work_accepted;
  340. if (!knc_titan_set_work(cgpu->dev_repr, knc->ctx, asic, die, 0xFFFF, knccore->next_slot, work, knc->need_flush, &work_accepted, &report))
  341. work_accepted = false;
  342. if (!work_accepted)
  343. break;
  344. if (knc->need_flush) {
  345. struct work *work1, *tmp1;
  346. knc->need_flush = false;
  347. applog(LOG_NOTICE, "%s: Flushing stale works", knc_titan_drv.dname);
  348. HASH_ITER(hh, knc->devicework, work1, tmp1) {
  349. HASH_DEL(knc->devicework, work1);
  350. free_work(work1);
  351. }
  352. delay_usecs = 0;
  353. }
  354. --knc->workqueue_size;
  355. DL_DELETE(knc->workqueue, work);
  356. work->device_id = knccore->next_slot;
  357. HASH_ADD(hh, knc->devicework, device_id, sizeof(work->device_id), work);
  358. if (++(knccore->next_slot) >= 16)
  359. knccore->next_slot = 1;
  360. ++workaccept;
  361. }
  362. applog(LOG_DEBUG, "%s: %d jobs accepted to queue (max=%d)", knc_titan_drv.dname, workaccept, knc->workqueue_max);
  363. while (true) {
  364. if (0 >= knc->dies[asic][die].cores)
  365. break;
  366. die_info.cores = knc->dies[asic][die].cores; /* core hint */
  367. if (!knc_titan_get_info(cgpu->dev_repr, knc->ctx, asic, die, &die_info))
  368. break;
  369. for (proc = knc->dies[asic][die].first_proc; proc; proc = proc->next_proc) {
  370. mythr = proc->thr[0];
  371. knccore = mythr->cgpu_data;
  372. if ((knccore->dieno != die) || (knccore->asicno != asic))
  373. break;
  374. if (!die_info.has_report[knccore->coreno])
  375. continue;
  376. if (!knc_titan_get_report(proc->proc_repr, knc->ctx, asic, die, knccore->coreno, &report))
  377. continue;
  378. /* if last_nonce.slot == 0, then there was a flush and all reports are stale */
  379. if (0 != knccore->last_nonce.slot) {
  380. for (i = 0; i < KNC_TITAN_NONCES_PER_REPORT; ++i) {
  381. if ((report.nonce[i].slot == knccore->last_nonce.slot) &&
  382. (report.nonce[i].nonce == knccore->last_nonce.nonce))
  383. break;
  384. tmp_int = report.nonce[i].slot;
  385. HASH_FIND_INT(knc->devicework, &tmp_int, work);
  386. if (!work) {
  387. applog(LOG_WARNING, "%"PRIpreprv": Got nonce for unknown work in slot %u", proc->proc_repr, tmp_int);
  388. continue;
  389. }
  390. if (submit_nonce(mythr, work, report.nonce[i].nonce)) {
  391. hashes_done2(mythr, DEFAULT_DIFF_HASHES_PER_NONCE, NULL);
  392. knccore->hwerr_in_row = 0;
  393. }
  394. }
  395. }
  396. knccore->last_nonce.slot = report.nonce[0].slot;
  397. knccore->last_nonce.nonce = report.nonce[0].nonce;
  398. }
  399. /* TODO: switch to next asic/die */
  400. break;
  401. }
  402. if (workaccept) {
  403. if (workaccept >= knc->workqueue_max) {
  404. knc->workqueue_max = workaccept;
  405. delay_usecs = 0;
  406. }
  407. knc_titan_set_queue_full(knc);
  408. }
  409. timer_set_delay_from_now(&thr->tv_poll, delay_usecs);
  410. }
  411. /*
  412. * specify settings / options via RPC or command line
  413. */
  414. /* support for --set-device
  415. * must be set before probing the device
  416. */
  417. static void knc_titan_set_clock_freq(struct cgpu_info * const device, int const val)
  418. {
  419. }
  420. 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)
  421. {
  422. knc_titan_set_clock_freq(device, atoi(setting));
  423. return NULL;
  424. }
  425. static const struct bfg_set_device_definition knc_titan_set_device_funcs[] = {
  426. { "clock", knc_titan_set_clock, NULL },
  427. { NULL },
  428. };
  429. /*
  430. * specify settings / options via TUI
  431. */
  432. #ifdef HAVE_CURSES
  433. static void knc_titan_tui_wlogprint_choices(struct cgpu_info * const proc)
  434. {
  435. wlogprint("[C]lock speed ");
  436. }
  437. static const char *knc_titan_tui_handle_choice(struct cgpu_info * const proc, const int input)
  438. {
  439. static char buf[0x100]; /* Static for replies */
  440. switch (input)
  441. {
  442. case 'c': case 'C':
  443. {
  444. sprintf(buf, "Set clock speed");
  445. char * const setting = curses_input(buf);
  446. knc_titan_set_clock_freq(proc->device, atoi(setting));
  447. return "Clock speed changed\n";
  448. }
  449. }
  450. return NULL;
  451. }
  452. static void knc_titan_wlogprint_status(struct cgpu_info * const proc)
  453. {
  454. wlogprint("Clock speed: N/A\n");
  455. }
  456. #endif
  457. struct device_drv knc_titan_drv =
  458. {
  459. /* metadata */
  460. .dname = "titan",
  461. .name = "KNC",
  462. .supported_algos = POW_SCRYPT,
  463. .drv_detect = knc_titan_detect,
  464. .thread_init = knc_titan_init,
  465. /* specify mining type - queue */
  466. .minerloop = minerloop_queue,
  467. .queue_append = knc_titan_queue_append,
  468. .queue_flush = knc_titan_queue_flush,
  469. .poll = knc_titan_poll,
  470. .prepare_work = knc_titan_prepare_work,
  471. /* TUI support - e.g. setting clock via UI */
  472. #ifdef HAVE_CURSES
  473. .proc_wlogprint_status = knc_titan_wlogprint_status,
  474. .proc_tui_wlogprint_choices = knc_titan_tui_wlogprint_choices,
  475. .proc_tui_handle_choice = knc_titan_tui_handle_choice,
  476. #endif
  477. };