driver-titan.c 15 KB

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