driver-titan.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. 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_titan_clean_flush(proc->device->dev_repr, knc->ctx, knccore->asicno, knccore->dieno);
  202. proc = proc->next_proc;
  203. if ((!proc) || proc->device == proc)
  204. break;
  205. }
  206. knc->cores = total_cores;
  207. }
  208. cgpu_set_defaults(cgpu);
  209. if (0 >= total_cores)
  210. return false;
  211. /* Init nonce ranges for cores */
  212. double nonce_step = 4294967296.0 / total_cores;
  213. double nonce_f = 0.0;
  214. struct titan_setup_core_params setup_params = {
  215. .bad_address_mask = {0, 0},
  216. .bad_address_match = {0x3FF, 0x3FF},
  217. .difficulty = DEFAULT_DIFF_FILTERING_ZEROES,
  218. .thread_enable = 0xFF,
  219. .thread_base_address = {0, 1, 2, 3, 4, 5, 6, 7},
  220. .lookup_gap_mask = {0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7, 0x7},
  221. .N_mask = {0, 0, 0, 0, 0, 0, 0, 0},
  222. .N_shift = {0, 0, 0, 0, 0, 0, 0, 0},
  223. .nonce_bottom = 0,
  224. .nonce_top = 0xFFFFFFFF,
  225. };
  226. for (proc = cgpu; proc; proc = proc->next_proc) {
  227. nonce_f += nonce_step;
  228. setup_params.nonce_bottom = setup_params.nonce_top + 1;
  229. if (NULL != proc->next_proc)
  230. setup_params.nonce_top = nonce_f;
  231. else
  232. setup_params.nonce_top = 0xFFFFFFFF;
  233. knc = proc->device_data;
  234. mythr = proc->thr[0];
  235. knccore = mythr->cgpu_data;
  236. 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);
  237. knc_titan_setup_core_local(proc->device->dev_repr, knc->ctx, knccore->asicno, knccore->dieno, knccore->coreno, &setup_params);
  238. }
  239. timer_set_now(&thr->tv_poll);
  240. return true;
  241. }
  242. static bool knc_titan_prepare_work(struct thr_info *thr, struct work *work)
  243. {
  244. struct cgpu_info * const cgpu = thr->cgpu;
  245. work->nonce_diff = cgpu->min_nonce_diff;
  246. return true;
  247. }
  248. static void knc_titan_clear_last_nonce(struct knc_titan_info * const knc)
  249. {
  250. struct thr_info * mythr;
  251. struct cgpu_info *proc;
  252. struct knc_titan_core *knccore;
  253. for (proc = knc->cgpu; proc; proc = proc->next_proc) {
  254. mythr = proc->thr[0];
  255. knccore = mythr->cgpu_data;
  256. knccore->last_nonce.slot = 0;
  257. knccore->last_nonce.nonce = 0;
  258. }
  259. }
  260. static void knc_titan_set_queue_full(struct knc_titan_info * const knc)
  261. {
  262. const bool full = (knc->workqueue_size >= knc->workqueue_max);
  263. struct cgpu_info *proc;
  264. for (proc = knc->cgpu; proc; proc = proc->next_proc) {
  265. struct thr_info * const thr = proc->thr[0];
  266. thr->queue_full = full;
  267. }
  268. }
  269. static void knc_titan_remove_local_queue(struct knc_titan_info * const knc, struct work * const work)
  270. {
  271. DL_DELETE(knc->workqueue, work);
  272. free_work(work);
  273. --knc->workqueue_size;
  274. }
  275. static void knc_titan_prune_local_queue(struct thr_info *thr)
  276. {
  277. struct cgpu_info * const cgpu = thr->cgpu;
  278. struct knc_titan_info * const knc = cgpu->device_data;
  279. struct work *work, *tmp;
  280. DL_FOREACH_SAFE(knc->workqueue, work, tmp) {
  281. if (stale_work(work, false))
  282. knc_titan_remove_local_queue(knc, work);
  283. }
  284. knc_titan_set_queue_full(knc);
  285. }
  286. static bool knc_titan_queue_append(struct thr_info * const thr, struct work * const work)
  287. {
  288. struct cgpu_info * const cgpu = thr->cgpu;
  289. struct knc_titan_info * const knc = cgpu->device_data;
  290. if (knc->workqueue_size >= knc->workqueue_max) {
  291. knc_titan_prune_local_queue(thr);
  292. if (thr->queue_full)
  293. return false;
  294. }
  295. DL_APPEND(knc->workqueue, work);
  296. ++knc->workqueue_size;
  297. knc_titan_set_queue_full(knc);
  298. if (thr->queue_full)
  299. knc_titan_prune_local_queue(thr);
  300. return true;
  301. }
  302. #define HASH_LAST_ADDED(head, out) \
  303. (out = (head) ? (ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail)) : NULL)
  304. static void knc_titan_queue_flush(struct thr_info * const thr)
  305. {
  306. struct cgpu_info * const cgpu = thr->cgpu;
  307. struct knc_titan_info * const knc = cgpu->device_data;
  308. struct work *work, *tmp;
  309. if (knc->cgpu != cgpu)
  310. return;
  311. DL_FOREACH_SAFE(knc->workqueue, work, tmp){
  312. knc_titan_remove_local_queue(knc, work);
  313. }
  314. knc_titan_set_queue_full(knc);
  315. HASH_LAST_ADDED(knc->devicework, work);
  316. if (work && stale_work(work, true)) {
  317. knc->need_flush = true;
  318. timer_set_now(&thr->tv_poll);
  319. }
  320. }
  321. static void knc_titan_poll(struct thr_info * const thr)
  322. {
  323. struct thr_info *mythr;
  324. struct cgpu_info * const cgpu = thr->cgpu, *proc;
  325. struct knc_titan_info * const knc = cgpu->device_data;
  326. struct knc_titan_core *knccore;
  327. struct work *work, *tmp;
  328. int workaccept = 0;
  329. unsigned long delay_usecs = KNC_POLL_INTERVAL_US;
  330. struct knc_report report;
  331. struct knc_die_info die_info;
  332. int asic;
  333. int die;
  334. int i, tmp_int;
  335. knc_titan_prune_local_queue(thr);
  336. if (knc->need_flush)
  337. knc_titan_clear_last_nonce(knc);
  338. knccore = cgpu->thr[0]->cgpu_data;
  339. DL_FOREACH_SAFE(knc->workqueue, work, tmp) {
  340. bool work_accepted = false;
  341. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  342. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  343. if (0 >= knc->dies[asic][die].cores)
  344. continue;
  345. bool die_work_accepted = false;
  346. 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))
  347. die_work_accepted = false;
  348. if (die_work_accepted)
  349. work_accepted = true;
  350. }
  351. }
  352. if (!work_accepted)
  353. break;
  354. if (knc->need_flush) {
  355. struct work *work1, *tmp1;
  356. knc->need_flush = false;
  357. applog(LOG_NOTICE, "%s: Flushing stale works", knc_titan_drv.dname);
  358. HASH_ITER(hh, knc->devicework, work1, tmp1) {
  359. HASH_DEL(knc->devicework, work1);
  360. free_work(work1);
  361. }
  362. delay_usecs = 0;
  363. }
  364. --knc->workqueue_size;
  365. DL_DELETE(knc->workqueue, work);
  366. work->device_id = knccore->next_slot;
  367. HASH_ADD(hh, knc->devicework, device_id, sizeof(work->device_id), work);
  368. if (++(knccore->next_slot) >= 16)
  369. knccore->next_slot = 1;
  370. ++workaccept;
  371. }
  372. applog(LOG_DEBUG, "%s: %d jobs accepted to queue (max=%d)", knc_titan_drv.dname, workaccept, knc->workqueue_max);
  373. for (asic = 0; asic < KNC_TITAN_MAX_ASICS; ++asic) {
  374. for (die = 0; die < KNC_TITAN_DIES_PER_ASIC; ++die) {
  375. if (0 >= knc->dies[asic][die].cores)
  376. continue;
  377. die_info.cores = knc->dies[asic][die].cores; /* core hint */
  378. die_info.version = KNC_VERSION_TITAN;
  379. if (!knc_titan_get_info(cgpu->dev_repr, knc->ctx, asic, die, &die_info))
  380. continue;
  381. for (proc = knc->dies[asic][die].first_proc; proc; proc = proc->next_proc) {
  382. mythr = proc->thr[0];
  383. knccore = mythr->cgpu_data;
  384. if ((knccore->dieno != die) || (knccore->asicno != asic))
  385. break;
  386. if (!die_info.has_report[knccore->coreno])
  387. continue;
  388. if (!knc_titan_get_report(proc->proc_repr, knc->ctx, asic, die, knccore->coreno, &report))
  389. continue;
  390. /* if last_nonce.slot == 0, then there was a flush and all reports are stale */
  391. if (0 != knccore->last_nonce.slot) {
  392. for (i = 0; i < KNC_TITAN_NONCES_PER_REPORT; ++i) {
  393. if ((report.nonce[i].slot == knccore->last_nonce.slot) &&
  394. (report.nonce[i].nonce == knccore->last_nonce.nonce))
  395. break;
  396. tmp_int = report.nonce[i].slot;
  397. HASH_FIND_INT(knc->devicework, &tmp_int, work);
  398. if (!work) {
  399. applog(LOG_WARNING, "%"PRIpreprv": Got nonce for unknown work in slot %u", proc->proc_repr, tmp_int);
  400. continue;
  401. }
  402. if (submit_nonce(mythr, work, report.nonce[i].nonce)) {
  403. hashes_done2(mythr, DEFAULT_DIFF_HASHES_PER_NONCE, NULL);
  404. knccore->hwerr_in_row = 0;
  405. }
  406. }
  407. }
  408. knccore->last_nonce.slot = report.nonce[0].slot;
  409. knccore->last_nonce.nonce = report.nonce[0].nonce;
  410. }
  411. }
  412. }
  413. if (workaccept) {
  414. if (workaccept >= knc->workqueue_max) {
  415. knc->workqueue_max = workaccept;
  416. delay_usecs = 0;
  417. }
  418. knc_titan_set_queue_full(knc);
  419. }
  420. timer_set_delay_from_now(&thr->tv_poll, delay_usecs);
  421. }
  422. /*
  423. * specify settings / options via RPC or command line
  424. */
  425. /* support for --set-device
  426. * must be set before probing the device
  427. */
  428. static void knc_titan_set_clock_freq(struct cgpu_info * const device, int const val)
  429. {
  430. }
  431. 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)
  432. {
  433. knc_titan_set_clock_freq(device, atoi(setting));
  434. return NULL;
  435. }
  436. static const struct bfg_set_device_definition knc_titan_set_device_funcs[] = {
  437. { "clock", knc_titan_set_clock, NULL },
  438. { NULL },
  439. };
  440. /*
  441. * specify settings / options via TUI
  442. */
  443. #ifdef HAVE_CURSES
  444. static void knc_titan_tui_wlogprint_choices(struct cgpu_info * const proc)
  445. {
  446. wlogprint("[C]lock speed ");
  447. }
  448. static const char *knc_titan_tui_handle_choice(struct cgpu_info * const proc, const int input)
  449. {
  450. static char buf[0x100]; /* Static for replies */
  451. switch (input)
  452. {
  453. case 'c': case 'C':
  454. {
  455. sprintf(buf, "Set clock speed");
  456. char * const setting = curses_input(buf);
  457. knc_titan_set_clock_freq(proc->device, atoi(setting));
  458. return "Clock speed changed\n";
  459. }
  460. }
  461. return NULL;
  462. }
  463. static void knc_titan_wlogprint_status(struct cgpu_info * const proc)
  464. {
  465. wlogprint("Clock speed: N/A\n");
  466. }
  467. #endif
  468. struct device_drv knc_titan_drv =
  469. {
  470. /* metadata */
  471. .dname = "titan",
  472. .name = "KNC",
  473. .supported_algos = POW_SCRYPT,
  474. .drv_detect = knc_titan_detect,
  475. .thread_init = knc_titan_init,
  476. /* specify mining type - queue */
  477. .minerloop = minerloop_queue,
  478. .queue_append = knc_titan_queue_append,
  479. .queue_flush = knc_titan_queue_flush,
  480. .poll = knc_titan_poll,
  481. .prepare_work = knc_titan_prepare_work,
  482. /* TUI support - e.g. setting clock via UI */
  483. #ifdef HAVE_CURSES
  484. .proc_wlogprint_status = knc_titan_wlogprint_status,
  485. .proc_tui_wlogprint_choices = knc_titan_tui_wlogprint_choices,
  486. .proc_tui_handle_choice = knc_titan_tui_handle_choice,
  487. #endif
  488. };