driver-SPI-dragonmint-t1.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. /*
  2. * cgminer SPI driver for Dragonmint T1 devices
  3. *
  4. * Copyright 2013, 2014 Zefir Kurtisi <zefir.kurtisi@gmail.com>
  5. * Copyright 2018 Con Kolivas <kernel@kolivas.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 3 of the License, or (at your option)
  10. * any later version. See COPYING for more details.
  11. */
  12. #include <stdlib.h>
  13. #include <assert.h>
  14. #include <fcntl.h>
  15. #include <limits.h>
  16. #include <unistd.h>
  17. #include <stdbool.h>
  18. #include <pthread.h>
  19. #include "logging.h"
  20. #include "miner.h"
  21. #include "util.h"
  22. #include "dragonmint_t1.h"
  23. #include "dm_temp_ctrl.h"
  24. #include "dm_fan_ctrl.h"
  25. #include "sys/time.h"
  26. #define T1_FANSPEED_INIT (opt_T1_target)
  27. #define T1_TEMP_TARGET_INIT (60)
  28. #define T1_TEMP_TARGET_RUN (75)
  29. struct T1_chain *chain[MAX_CHAIN_NUM];
  30. uint8_t chain_mask;
  31. uint16_t T1Pll[MCOMPAT_CONFIG_MAX_CHAIN_NUM];
  32. /* FAN CTRL */
  33. //dragonmint_fan_temp_s g_fan_ctrl;
  34. static volatile uint8_t g_debug_stats[MAX_CHAIN_NUM];
  35. static int total_chains;
  36. static int chains_tuned;
  37. static dragonmint_reg_ctrl_t s_reg_ctrl;
  38. hardware_version_e g_hwver;
  39. //dragonmint_type_e g_type;
  40. int g_reset_delay = 0xffff;
  41. char volShowLog[MAX_CHAIN_NUM][256];
  42. /* one global board_selector and spi context is enough */
  43. //static struct board_selector *board_selector;
  44. static int spi_status_err_cnt = 0;
  45. static pthread_t fan_tid;
  46. /*
  47. * for now, we have one global config, defaulting values:
  48. * - ref_clk 16MHz / sys_clk 800MHz
  49. * - 2000 kHz SPI clock
  50. */
  51. struct T1_config_options T1_config_options = {
  52. .ref_clk_khz = 16000, .sys_clk_khz = 800000, .spi_clk_khz = 2000,
  53. };
  54. /* override values with --bitmine-t1-options ref:sys:spi: - use 0 for default */
  55. static struct T1_config_options *parsed_config_options;
  56. int chain_plug[MAX_CHAIN_NUM];
  57. int chain_flag[MAX_CHAIN_NUM];
  58. #define LOG_VOL_PREFIX "/tmp/log/volAnalys"
  59. void dragonmint_log_record(int cid, void* log, int len)
  60. {
  61. FILE* fd;
  62. char fileName[128] = {0};
  63. sprintf(fileName, "%s%d.log", LOG_VOL_PREFIX, cid);
  64. fd = fopen(fileName, "w+");
  65. if (fd == NULL){
  66. //applog(LOG_ERR, "Open log File%d Failed!%d", cid, errno);
  67. applog(LOG_ERR, "Open log File%d Failed!%s", cid, strerror(errno));
  68. return;
  69. }
  70. fwrite(log, len, 1, fd);
  71. fflush(fd);
  72. fclose(fd);
  73. }
  74. static void wq_enqueue(struct thr_info *thr, struct T1_chain *t1)
  75. {
  76. struct work *work = get_work(thr, thr->id);
  77. struct work_queue *wq;
  78. struct work_ent *we;
  79. int rolls = 0;
  80. wq = &t1->active_wq;
  81. while (42) {
  82. we = cgmalloc(sizeof(*we));
  83. we->work = work;
  84. INIT_LIST_HEAD(&we->head);
  85. mutex_lock(&t1->lock);
  86. list_add_tail(&we->head, &wq->head);
  87. wq->num_elems++;
  88. mutex_unlock(&t1->lock);
  89. if (wq->num_elems >= t1->num_active_chips * 2) {
  90. break;
  91. }
  92. if (rolls > work->drv_rolllimit) {
  93. work = get_work(thr, thr->id);
  94. continue;
  95. }
  96. work = make_clone(work);
  97. roll_work(work);
  98. }
  99. }
  100. static struct work *wq_dequeue(struct T1_chain *t1, bool sig)
  101. {
  102. struct work_ent *we;
  103. struct work *work = NULL;
  104. struct work_queue *wq = &t1->active_wq;
  105. if (wq == NULL)
  106. return NULL;
  107. /* Sleep only a small duration if there is no work queued in case it's
  108. * still refilling rather than we have no upstream work. */
  109. if (unlikely(!wq->num_elems && sig))
  110. cgsleep_ms(10);
  111. mutex_lock(&t1->lock);
  112. if (likely(wq->num_elems > 0)) {
  113. we = list_entry(wq->head.next, struct work_ent, head);
  114. work = we->work;
  115. list_del(&we->head);
  116. free(we);
  117. wq->num_elems--;
  118. }
  119. if (sig)
  120. pthread_cond_signal(&t1->cond);
  121. mutex_unlock(&t1->lock);
  122. return work;
  123. }
  124. /********** driver interface */
  125. void exit_T1_chain(struct T1_chain *t1)
  126. {
  127. if (t1 == NULL)
  128. return;
  129. free(t1->chips);
  130. t1->chips = NULL;
  131. chain[t1->chain_id] = NULL;
  132. chain_flag[t1->chain_id] = 0;
  133. mcompat_set_led(t1->chain_id, LED_OFF);
  134. mcompat_set_power_en(t1->chain_id, 0);
  135. free(t1);
  136. }
  137. static void get_temperatures(struct T1_chain *t1)
  138. {
  139. int i;
  140. int temp[MAX_CHIP_NUM] = {0};
  141. mcompat_get_chip_temp(t1->chain_id, temp);
  142. for (i = 0; i < t1->num_active_chips; i++)
  143. t1->chips[i].temp = temp[i];
  144. }
  145. static void get_voltages(struct T1_chain *t1)
  146. {
  147. int i;
  148. //configure for vsensor
  149. mcompat_configure_tvsensor(t1->chain_id, CMD_ADDR_BROADCAST, 0);
  150. for (i = 0; i < t1->num_active_chips; i++)
  151. dragonmint_check_voltage(t1, i + 1, &s_reg_ctrl);
  152. //configure for tsensor
  153. mcompat_configure_tvsensor(t1->chain_id, CMD_ADDR_BROADCAST, 1);
  154. dragonmint_get_voltage_stats(t1, &s_reg_ctrl);
  155. }
  156. static bool prechain_detect(struct T1_chain *t1, int idxpll)
  157. {
  158. int pll_lv_to_setspi;
  159. int pll_lv_to_setvid;
  160. int chain_id = t1->chain_id;
  161. assert(pll_lv_to_setvid < idxpll);
  162. cgsleep_us(1000);
  163. t1->pll = 0;
  164. t1->base_pll = idxpll;
  165. if (opt_T1auto) {
  166. /* Start tuning at a different voltage depending on tuning
  167. * strategy. */
  168. if (opt_T1_performance)
  169. opt_T1Vol[chain_id] = TUNE_VOLT_START_PER;
  170. else if (opt_T1_efficient)
  171. opt_T1Vol[chain_id] = TUNE_VOLT_START_EFF;
  172. else
  173. opt_T1Vol[chain_id] = TUNE_VOLT_START_BAL;
  174. }
  175. pll_lv_to_setspi = T1_ConfigT1PLLClock(T1_PLL_SETSPI);
  176. if (!t1_set_pll(t1, CMD_ADDR_BROADCAST, pll_lv_to_setspi))
  177. return false;
  178. /* Using 390K spi speed at first and raising to 1.5M at 310M PLL
  179. * to avoid spi failure on 150M PLL */
  180. applog(LOG_NOTICE, "chain%d: spi speed set to 1.5M", chain_id);
  181. mcompat_set_spi_speed(chain_id, SPI_SPEED_1562K);
  182. cgsleep_ms(10);
  183. pll_lv_to_setvid = T1_ConfigT1PLLClock(T1_PLL_SETVID);
  184. if (!t1_set_pll(t1, CMD_ADDR_BROADCAST, pll_lv_to_setvid))
  185. return false;
  186. /* Set voltage down at this point to avoid massive power draw as we
  187. * increase frequency */
  188. if (!opt_T1auto && opt_T1VID[chain_id]) {
  189. /* If opt_T1VID values are set in non-auto mode, we use those
  190. * from the config. */
  191. mcompat_set_vid_by_step(chain_id, t1->iVid, opt_T1VID[chain_id]);
  192. t1->iVid = opt_T1VID[chain_id];
  193. } else {
  194. t1->iVid = mcompat_find_chain_vid(chain_id, t1->num_active_chips,
  195. STARTUP_VID, opt_T1Vol[chain_id]);
  196. }
  197. if (!t1_set_pll(t1, CMD_ADDR_BROADCAST, idxpll))
  198. return false;
  199. /* Now fine tune voltage to the target level as voltage will have
  200. * changed due to changing frequency */
  201. if (opt_T1auto || !opt_T1VID[chain_id]) {
  202. t1->iVid = mcompat_find_chain_vid(chain_id, t1->num_active_chips,
  203. t1->iVid, opt_T1Vol[chain_id]);
  204. }
  205. /* Read chip voltages */
  206. get_voltages(t1);
  207. applog(LOG_NOTICE, "chain%d: volt = %.1f, vid = %d after calibration", chain_id,
  208. s_reg_ctrl.average_vol[chain_id], t1->iVid);
  209. return true;
  210. }
  211. /*
  212. * BIST_START works only once after HW reset, on subsequent calls it
  213. * returns 0 as number of chips.
  214. */
  215. static int chain_detect(struct T1_chain *t1)
  216. {
  217. int cid = t1->chain_id;
  218. uint8_t n_chips = mcompat_cmd_bist_start(cid, CMD_ADDR_BROADCAST);
  219. if (unlikely(n_chips == 0 || n_chips > MAX_CHIP_NUM)){
  220. write_miner_ageing_status(AGEING_BIST_START_FAILED);
  221. return 0;
  222. }
  223. applog(LOG_WARNING, "%d: detected %d chips", cid, n_chips);
  224. cgsleep_ms(10);
  225. /*
  226. if (!mcompat_cmd_bist_collect(cid, CMD_ADDR_BROADCAST))
  227. {
  228. applog(LOG_WARNING, "bist collect fail");
  229. return 0;
  230. }
  231. */
  232. applog(LOG_WARNING, "collect core success");
  233. return n_chips;
  234. }
  235. static bool prepare_T1(struct T1_chain *t1, int chain_id)
  236. {
  237. uint8_t buffer[4] = {};
  238. bool ret = false;
  239. //spi speed init
  240. applog(LOG_NOTICE, "chain%d: spi speed set to 390K", chain_id);
  241. mcompat_set_spi_speed(chain_id, T1_SPI_SPEED_DEF);
  242. cgsleep_ms(10);
  243. if (!dm_cmd_resetall(chain_id, CMD_ADDR_BROADCAST, buffer)) {
  244. applog(LOG_ERR, "failed to reset chain %d!", chain_id);
  245. goto out;
  246. }
  247. if (CMD_TYPE_T1 != (buffer[0] & 0xf0)) {
  248. applog(LOG_ERR, "incompatible chip type %02X for chain %d!", buffer[0] & 0xf0, chain_id);
  249. goto out;
  250. }
  251. t1->num_chips = chain_detect(t1);
  252. cgsleep_ms(10);
  253. if ((t1->num_chips <= 0) || (t1->num_chips > MAX_CHIP_NUM)){
  254. spi_status_err_cnt++;
  255. if (chain_id == (MAX_CHAIN_NUM - 1)){
  256. if (spi_status_err_cnt >= MAX_CHAIN_NUM){
  257. write_miner_ageing_status(AGEING_ALL_SPI_STATUS_ERROR);
  258. }
  259. if ((spi_status_err_cnt >= 1) && (spi_status_err_cnt < MAX_CHAIN_NUM)){
  260. write_miner_ageing_status(AGEING_SPI_STATUS_ERROR);
  261. }
  262. }
  263. goto out;
  264. }
  265. if (chain_id == (MAX_CHAIN_NUM - 1)){
  266. if (spi_status_err_cnt >= MAX_CHAIN_NUM){
  267. write_miner_ageing_status(AGEING_ALL_SPI_STATUS_ERROR);
  268. }
  269. if ((spi_status_err_cnt >= 1) && (spi_status_err_cnt < MAX_CHAIN_NUM)){
  270. write_miner_ageing_status(AGEING_SPI_STATUS_ERROR);
  271. }
  272. }
  273. /* override max number of active chips if requested */
  274. t1->num_active_chips = t1->num_chips;
  275. if (T1_config_options.override_chip_num > 0 &&
  276. t1->num_chips > T1_config_options.override_chip_num) {
  277. t1->num_active_chips = T1_config_options.override_chip_num;
  278. applog(LOG_WARNING, "%d: limiting chain to %d chips",
  279. chain_id, t1->num_active_chips);
  280. }
  281. /* Free this in case we are re-initialising a chain */
  282. free(t1->chips);
  283. t1->chips = cgcalloc(t1->num_active_chips, sizeof(struct T1_chip));
  284. ret = true;
  285. out:
  286. return ret;
  287. }
  288. static struct T1_chain *pre_init_T1_chain(int chain_id)
  289. {
  290. struct T1_chain *t1 = cgcalloc(sizeof(*t1), 1);
  291. applog(LOG_INFO, "pre %d: T1 init chain", chain_id);
  292. t1->chain_id = chain_id;
  293. if (!prepare_T1(t1, chain_id)) {
  294. exit_T1_chain(t1);
  295. t1 = NULL;
  296. }
  297. return t1;
  298. }
  299. static bool init_T1_chain(struct T1_chain *t1)
  300. {
  301. int i;
  302. uint8_t src_reg[REG_LENGTH] = {0};
  303. uint8_t reg[REG_LENGTH] = {0};
  304. int chain_id = t1->chain_id;
  305. int num_chips;
  306. bool ret = false;
  307. applog(LOG_INFO, "%d: T1 init chain", chain_id);
  308. applog(LOG_NOTICE, "chain%d: spi speed set to 6.25M", chain_id);
  309. mcompat_set_spi_speed(chain_id, SPI_SPEED_6250K);
  310. cgsleep_ms(1);
  311. #ifdef USE_BISTMASK
  312. dm_cmd_resetbist(chain_id, CMD_ADDR_BROADCAST, reg);
  313. //cgsleep_ms(120);
  314. sleep(1);
  315. //bist mask
  316. mcompat_cmd_read_register(chain_id, 0x01, reg, REG_LENGTH);
  317. memcpy(src_reg, reg, REG_LENGTH);
  318. src_reg[7] = src_reg[7] | 0x10;
  319. mcompat_cmd_write_register(chain_id, CMD_ADDR_BROADCAST, src_reg, REG_LENGTH);
  320. cgsleep_us(200);
  321. #endif
  322. applog(LOG_DEBUG, "%d: T1 init chain", chain_id);
  323. num_chips = chain_detect(t1);
  324. cgsleep_ms(10);
  325. if (num_chips != 0 && num_chips != t1->num_chips) {
  326. applog(LOG_WARNING, "T1 %d: Num chips failure", chain_id);
  327. goto out;
  328. }
  329. if (!mcompat_cmd_bist_fix(chain_id, CMD_ADDR_BROADCAST)) {
  330. write_miner_ageing_status(AGEING_BIST_FIX_FAILED);
  331. goto out;
  332. }
  333. cgsleep_us(200);
  334. #if 0
  335. sprintf(volShowLog[chain_id], "+ %2d | %8f | %8f | %8f |",chain_id, \
  336. s_reg_ctrl.highest_vol[chain_id],s_reg_ctrl.average_vol[chain_id],s_reg_ctrl.lowest_vol[chain_id]);
  337. dragonmint_log_record(chain_id, volShowLog[chain_id], strlen(volShowLog[0]));
  338. #endif
  339. applog(LOG_WARNING,
  340. "Chain %d Voltage information. Highest Vol:%.0f, Average Vol:%.0f, Lowest Vol:%.0f",
  341. chain_id, s_reg_ctrl.highest_vol[chain_id], s_reg_ctrl.average_vol[chain_id],
  342. s_reg_ctrl.lowest_vol[chain_id]);
  343. /* Reset value in case we are re-initialising */
  344. t1->num_cores = 0;
  345. for (i = 0; i < t1->num_active_chips; i++)
  346. check_chip(t1, i);
  347. applog(LOG_WARNING, "%d: found %d chips with total %d active cores",
  348. chain_id, t1->num_active_chips, t1->num_cores);
  349. if (!opt_T1auto)
  350. t1->VidOptimal = t1->pllOptimal = true;
  351. ret = true;
  352. out:
  353. return ret;
  354. }
  355. /* Asynchronous work generation since get_work is a blocking function */
  356. static void *T1_work_thread(void *arg)
  357. {
  358. struct cgpu_info *cgpu = arg;
  359. struct T1_chain *t1 = cgpu->device_data;
  360. char tname[16];
  361. sprintf(tname, "T1_%dwork", t1->chain_id);
  362. RenameThread(tname);
  363. mutex_lock(&t1->lock);
  364. while (!pthread_cond_wait(&t1->cond, &t1->lock)) {
  365. mutex_unlock(&t1->lock);
  366. /* Only start filling the queue once we're 1/3 empty */
  367. if (t1->active_wq.num_elems < t1->num_active_chips * 4 / 3)
  368. wq_enqueue(cgpu->thr[0], t1);
  369. mutex_lock(&t1->lock);
  370. }
  371. return NULL;
  372. }
  373. static void start_T1_chain(int cid, int retries)
  374. {
  375. mcompat_set_reset(cid, 1);
  376. sleep(retries);
  377. mcompat_set_power_en(cid, 1);
  378. sleep(retries);
  379. mcompat_set_reset(cid, 0);
  380. sleep(retries);
  381. mcompat_set_start_en(cid, 1);
  382. sleep(retries);
  383. mcompat_set_reset(cid, 1);
  384. sleep(retries);
  385. }
  386. static bool detect_T1_chain(void)
  387. {
  388. int i, retries, chain_num = 0, chip_num = 0, iPll;
  389. c_temp_cfg tmp_cfg;
  390. applog(LOG_NOTICE, "T1: checking T1 chain");
  391. for(i = 0; i < MAX_CHAIN_NUM; i++) {
  392. if (chain_plug[i] != 1)
  393. continue;
  394. chain_num++;
  395. }
  396. /* Go back and try chains that have failed after cycling through all of
  397. * them. */
  398. for (retries = 0; retries < 3; retries++) {
  399. for (i = 0; i < MAX_CHAIN_NUM; i++) {
  400. if (chain_plug[i] != 1)
  401. continue;
  402. if (chain[i])
  403. continue;
  404. start_T1_chain(i, retries);
  405. /* pre-init chain */
  406. if ((chain[i] = pre_init_T1_chain(i))) {
  407. chain_flag[i] = 1;
  408. if (chain[i]->num_chips > chip_num)
  409. chip_num = chain[i]->num_chips;
  410. }
  411. }
  412. }
  413. // reinit platform with real chain number and chip number
  414. applog(LOG_NOTICE, "platform re-init: chain_num(%d), chip_num(%d)", chain_num, chip_num);
  415. sys_platform_exit();
  416. sys_platform_init(PLATFORM_ZYNQ_HUB_G19, MCOMPAT_LIB_MINER_TYPE_T1, chain_num, chip_num);
  417. for (i = 0; i < MAX_CHAIN_NUM; i++) {
  418. if (chain_plug[i] != 1)
  419. continue;
  420. if (chain[i] == NULL){
  421. applog(LOG_ERR, "init %d T1 chain fail", i);
  422. continue;
  423. }
  424. // re-config spi speed after platform init
  425. mcompat_set_spi_speed(i, T1_SPI_SPEED_DEF);
  426. cgsleep_ms(10);
  427. mcompat_cfg_tsadc_divider(i, PLL_Clk_12Mhz[0].speedMHz);
  428. }
  429. // init temp ctrl
  430. dm_tempctrl_get_defcfg(&tmp_cfg);
  431. /* Set initial target temperature lower for more reliable startup */
  432. tmp_cfg.tmp_target = T1_TEMP_TARGET_INIT; // target temperature
  433. dm_tempctrl_init(&tmp_cfg);
  434. // start fan ctrl thread
  435. c_fan_cfg fan_cfg;
  436. dm_fanctrl_get_defcfg(&fan_cfg);
  437. fan_cfg.preheat = false; // disable preheat
  438. fan_cfg.fan_speed = T1_FANSPEED_INIT;
  439. dm_fanctrl_init(&fan_cfg);
  440. // dm_fanctrl_init(NULL); // using default cfg
  441. pthread_create(&fan_tid, NULL, dm_fanctrl_thread, NULL);
  442. for(i = 0; i < MAX_CHAIN_NUM; i++) {
  443. if (chain_flag[i] != 1)
  444. continue;
  445. if (!prechain_detect(chain[i], T1Pll[i])) {
  446. chain_flag[i] = 0;
  447. exit_T1_chain(chain[i]);
  448. }
  449. }
  450. for(i = 0; i < MAX_CHAIN_NUM; i++) {
  451. if (chain_flag[i] != 1)
  452. continue;
  453. if (!init_T1_chain(chain[i])) {
  454. exit_T1_chain(chain[i]);
  455. applog(LOG_ERR, "init %d T1 chain fail", i);
  456. chain_flag[i] = 0;
  457. continue;
  458. }
  459. }
  460. for(i = 0; i < MAX_CHAIN_NUM; i++) {
  461. struct cgpu_info *cgpu;
  462. struct T1_chain *t1;
  463. pthread_t pth;
  464. if (chain_flag[i] != 1)
  465. continue;
  466. total_chains++;
  467. cgpu = cgcalloc(sizeof(*cgpu), 1);
  468. cgpu->drv = &dragonmintT1_drv;
  469. cgpu->name = "DragonmintT1.SingleChain";
  470. cgpu->threads = 1;
  471. cgpu->chainNum = i;
  472. cgpu->device_data = t1 = chain[i];
  473. cgtime(&cgpu->dev_start_tv);
  474. t1->lastshare = cgpu->dev_start_tv.tv_sec;
  475. iPll = T1Pll[i];
  476. if ((chain[i]->num_chips <= MAX_CHIP_NUM) && (chain[i]->num_cores <= MAX_CORES)) {
  477. cgpu->mhs_av = (double)PLL_Clk_12Mhz[iPll].speedMHz * 2ull * (chain[i]->num_cores);
  478. } else {
  479. cgpu->mhs_av = 0;
  480. chain_flag[i] = 0;
  481. }
  482. chain[i]->cgpu = cgpu;
  483. cgpu->device_id = i;
  484. add_cgpu(cgpu);
  485. mcompat_set_led(i, LED_ON);
  486. applog(LOG_WARNING, "Detected the %d T1 chain with %d chips / %d cores",
  487. i, chain[i]->num_active_chips, chain[i]->num_cores);
  488. INIT_LIST_HEAD(&t1->active_wq.head);
  489. mutex_init(&t1->lock);
  490. pthread_cond_init(&t1->cond, NULL);
  491. pthread_create(&pth, NULL, T1_work_thread, cgpu);
  492. }
  493. if (!total_chains)
  494. return false;
  495. /* Now adjust target temperature for runtime setting */
  496. tmp_cfg.tmp_target = T1_TEMP_TARGET_RUN;
  497. dm_tempctrl_set(&tmp_cfg);
  498. return true;
  499. }
  500. /* Probe SPI channel and register chip chain */
  501. void T1_detect(bool hotplug)
  502. {
  503. int i;
  504. if (hotplug)
  505. return;
  506. /* parse bimine-t1-options */
  507. if (opt_dragonmint_t1_options != NULL && parsed_config_options == NULL) {
  508. int ref_clk = 0;
  509. int sys_clk = 0;
  510. int spi_clk = 0;
  511. int override_chip_num = 0;
  512. int wiper = 0;
  513. sscanf(opt_dragonmint_t1_options, "%d:%d:%d:%d:%d",
  514. &ref_clk, &sys_clk, &spi_clk, &override_chip_num,
  515. &wiper);
  516. if (ref_clk != 0)
  517. T1_config_options.ref_clk_khz = ref_clk;
  518. if (sys_clk != 0) {
  519. if (sys_clk < 100000)
  520. quit(1, "system clock must be above 100MHz");
  521. T1_config_options.sys_clk_khz = sys_clk;
  522. }
  523. if (spi_clk != 0)
  524. T1_config_options.spi_clk_khz = spi_clk;
  525. if (override_chip_num != 0)
  526. T1_config_options.override_chip_num = override_chip_num;
  527. if (wiper != 0)
  528. T1_config_options.wiper = wiper;
  529. /* config options are global, scan them once */
  530. parsed_config_options = &T1_config_options;
  531. }
  532. applog(LOG_DEBUG, "T1 detect");
  533. memset(&s_reg_ctrl,0,sizeof(s_reg_ctrl));
  534. g_hwver = dragonmint_get_hwver();
  535. // g_type = dragonmint_get_miner_type();
  536. // FIXME: get correct hwver and chain num to init platform
  537. sys_platform_init(PLATFORM_ZYNQ_HUB_G19, MCOMPAT_LIB_MINER_TYPE_T1, MAX_CHAIN_NUM, MAX_CHIP_NUM);
  538. applog(LOG_NOTICE, "vid type detected: %d", misc_get_vid_type());
  539. // set fan speed high to get to a lower startup temperature
  540. dm_fanctrl_set_fan_speed(T1_FANSPEED_INIT);
  541. //dragonmint_miner_init_voltage_flag();
  542. for (i = MAX_CHAIN_NUM - 1; i >= 0; i--) {
  543. if (mcompat_get_plug(i) == 0) {
  544. chain_plug[i] = 1;
  545. applog(LOG_INFO, "chain:%d the plat is inserted", i);
  546. } else {
  547. applog(LOG_INFO, "chain:%d the plat is not inserted", i);
  548. write_miner_ageing_status(AGEING_PLUG_STATUS_ERROR);
  549. }
  550. }
  551. /* If hardware version is g19, continue init cgminer. Else power off*/
  552. if (HARDWARE_VERSION_G19 == g_hwver) {
  553. applog(LOG_INFO, "The hardware version is G19");
  554. for (i = 0; i < MAX_CHAIN_NUM; i++) {
  555. if (chain_plug[i] != 1)
  556. continue;
  557. /* Sets initial voltage to very high to get chips
  558. * initialised. */
  559. mcompat_set_vid(i, STARTUP_VID);
  560. }
  561. } else if (HARDWARE_VERSION_G9 == g_hwver) {
  562. applog(LOG_INFO, "The hardware version is G9");
  563. mcompat_set_vid(0, STARTUP_VID);
  564. } else {
  565. for(i = 0; i < MAX_CHAIN_NUM; i++) {
  566. applog(LOG_ERR, "Unknown hwver, chain%d power down", i);
  567. mcompat_chain_power_down(i);
  568. }
  569. write_miner_ageing_status(AGEING_HW_VERSION_ERROR);
  570. return;
  571. }
  572. for(i = 0; i < MAX_CHAIN_NUM; ++i) {
  573. int pll = DEFAULT_PLL;
  574. /* Tune voltage to highest frequency in Performance mode, and
  575. * lowest frequency in efficient mode. */
  576. if (opt_T1auto) {
  577. if (opt_T1_performance)
  578. pll = MAX_PLL;
  579. else if (opt_T1_efficient)
  580. pll = MIN_PLL;
  581. } else
  582. pll = opt_T1Pll[i];
  583. T1Pll[i] = T1_ConfigT1PLLClock(pll);
  584. }
  585. if (detect_T1_chain()) {
  586. if (misc_get_vid_type() == MCOMPAT_LIB_VID_I2C_TYPE)
  587. set_timeout_on_i2c(30);
  588. applog(LOG_WARNING, "T1 detect finish");
  589. }
  590. }
  591. /* Exit cgminer on failure, allowing systemd watchdog to restart */
  592. static void reinit_T1_chain(struct T1_chain *t1, int cid)
  593. {
  594. bool success = false;
  595. struct timeval now;
  596. int i;
  597. applog(LOG_WARNING, "T1: %d attempting to re-initialise!", cid);
  598. for (i = 0; i < 3; i++) {
  599. start_T1_chain(cid, i);
  600. if (prepare_T1(t1, cid)) {
  601. success = true;
  602. break;
  603. }
  604. }
  605. if (!success) {
  606. applog(LOG_EMERG, "T1: %d FAILED TO PREPARE, SHUTTING DOWN", cid);
  607. raise_cgminer();
  608. }
  609. if (!prechain_detect(t1, T1Pll[cid])) {
  610. applog(LOG_EMERG, "T1: %d FAILED TO PRECHAIN DETECT, SHUTTING DOWN", cid);
  611. raise_cgminer();
  612. }
  613. if (!init_T1_chain(t1)) {
  614. applog(LOG_EMERG, "T1: %d FAILED TO INIT, SHUTTING DOWN", cid);
  615. raise_cgminer();
  616. }
  617. cgtime(&now);
  618. t1->lastshare = now.tv_sec;
  619. }
  620. #define VOLTAGE_UPDATE_INT 121
  621. #define WRITE_CONFIG_TIME 60
  622. #define CHECK_DISABLE_TIME 59
  623. #if 0
  624. char szShowLog[MAX_CHAIN_NUM][MAX_CHIP_NUM][256] = {0};
  625. #define LOG_FILE_PREFIX "/tmp/log/analys"
  626. char cLevelError1[3] = "!";
  627. char cLevelError2[3] = "#";
  628. char cLevelError3[3] = "$";
  629. char cLevelError4[3] = "%";
  630. char cLevelError5[3] = "*";
  631. char cLevelNormal[3] = "+";
  632. void Dragonmint_Log_Save(struct T1_chip *chip,int nChip,int nChain)
  633. {
  634. char szInNormal[8] = {};
  635. if (chip->hw_errors > 0){
  636. strcat(szInNormal,cLevelError1);
  637. }
  638. if (chip->stales > 0){
  639. strcat(szInNormal,cLevelError2);
  640. }
  641. if (chip->num_cores < 32){
  642. strcat(szInNormal,cLevelError4);
  643. }
  644. if ((chip->nVol > 440) || (chip->nVol < 360)){
  645. strcat(szInNormal,cLevelError5);
  646. }
  647. if ((chip->hw_errors == 0) && (chip->stales == 0) && ((chip->nVol < 440) && (chip->nVol > 360)) && (chip->num_cores == 32)){
  648. strcat(szInNormal,cLevelNormal);
  649. }
  650. sprintf(szShowLog[nChain][nChip], "\n%-8s|%32d|%8d|%8d|%8d|%8d|%8d|%8d|%8d",szInNormal,chip->nonces_found,
  651. chip->hw_errors, chip->stales,chip->temp,chip->nVol,chip->num_cores,nChip,nChain);
  652. }
  653. void dragonmint_log_print(int cid, void* log, int len)
  654. {
  655. FILE* fd;
  656. char fileName[128] = {0};
  657. sprintf(fileName, "%s%d.log", LOG_FILE_PREFIX, cid);
  658. fd = fopen(fileName, "w+");
  659. if (fd == NULL){
  660. applog(LOG_ERR, "Open log File%d Failed!", cid);
  661. return;
  662. }
  663. fwrite(log, len, 1, fd);
  664. fflush(fd);
  665. fclose(fd);
  666. }
  667. #endif
  668. /* Invalidate all statistics during buffer underruns and while hardware isn't
  669. * running at its optimal temperature. */
  670. static void reset_tune(struct T1_chain *t1)
  671. {
  672. struct timeval now;
  673. cgtime(&now);
  674. copy_time(&t1->cycle_start, &now);
  675. t1->cycles = 0;
  676. /* Reset last share time since the hardware could genuinely not be
  677. * able to generate shares during extended underruns. */
  678. t1->lastshare = now.tv_sec;
  679. }
  680. static void T1_set_optimal_vid(struct T1_chain *t1, int cid)
  681. {
  682. double best = 0, product[T1_VID_TUNE_RANGE] = {};
  683. int i, vid = t1->iVid;
  684. for (i = 0; i < T1_VID_TUNE_RANGE; i++) {
  685. product[i] = t1->vidproduct[i];
  686. /* In efficient mode divide by the square of the voltage */
  687. if (opt_T1_efficient && t1->vidvol[i])
  688. product[i] /= (double)(t1->vidvol[i] * t1->vidvol[i]);
  689. }
  690. for (i = 0; i < T1_VID_TUNE_RANGE; i++) {
  691. if (!t1->vidproduct[i])
  692. continue;
  693. applog(LOG_ERR, "vid%d: product=%.5f, hwerr=%.5f",
  694. i, t1->vidproduct[i], t1->vidhwerr[i]);
  695. /* Allow up to 1% drop for the sake of lower voltage */
  696. if (!best || (product[i] > best * 0.99 && t1->vidhwerr[i] < 0.2)) {
  697. best = product[i];
  698. vid = i;
  699. }
  700. /* Reset values for clean reuse */
  701. t1->vidproduct[i] = 0;
  702. }
  703. t1->optimalVid = vid;
  704. t1->VidOptimal = true;
  705. mcompat_set_vid_by_step(cid, t1->iVid, vid);
  706. t1->iVid = vid;
  707. get_voltages(t1);
  708. /* Store the optimal voltage for readjusting after PLL changes */
  709. t1->optimal_vol = s_reg_ctrl.average_vol[cid];
  710. /* Set opt_T1VID for saving to config file */
  711. opt_T1VID[cid] = t1->iVid;
  712. for (i = 0; i < T1_PLL_TUNE_RANGE; i++)
  713. t1->pllvid[i] = t1->iVid;
  714. }
  715. /* This returns the best absolute product */
  716. static double T1_best_vid_product(struct T1_chain *t1)
  717. {
  718. double best = 0;
  719. int i;
  720. for (i = 0; i < T1_VID_TUNE_RANGE; i++) {
  721. if (t1->vidproduct[i] > best)
  722. best = t1->vidproduct[i];
  723. }
  724. return best;
  725. }
  726. static void T1_set_optimal_pll(struct T1_chain *t1, int cid)
  727. {
  728. double best = 0, product[T1_PLL_TUNE_RANGE] = {};
  729. int i, pll = t1->pll, best_offset = 0;
  730. for (i = 0; i < T1_PLL_TUNE_RANGE; i++) {
  731. product[i] = t1->pllproduct[i];
  732. /* In efficient mode divide by the frequency */
  733. if (opt_T1_efficient)
  734. product[i] /= (double)PLL_Clk_12Mhz[i + T1_PLL_TUNE_MIN].speedMHz;
  735. }
  736. for (i = 0; i < T1_PLL_TUNE_RANGE; i++) {
  737. if (!t1->pllproduct[i])
  738. continue;
  739. applog(LOG_ERR, "pll%d: product=%.5f, hwerr=%.5f",
  740. i + T1_PLL_TUNE_MIN, t1->pllproduct[i], t1->pllhwerr[i]);
  741. if (!best || (product[i] > best && t1->pllhwerr[i] < 0.2)) {
  742. best = product[i];
  743. pll = i + T1_PLL_TUNE_MIN;
  744. best_offset = i;
  745. }
  746. t1->pllproduct[i] = 0;
  747. }
  748. t1->pllOptimal = true;
  749. t1_set_pll(t1, CMD_ADDR_BROADCAST, pll);
  750. t1->base_pll = t1->pll;
  751. /* Set opt_T1Pll for saving to config file */
  752. opt_T1Pll[cid] = PLL_Clk_12Mhz[t1->pll].speedMHz;
  753. /* Readjust iVid if we changed it during tuning */
  754. if (t1->iVid != t1->pllvid[best_offset]) {
  755. mcompat_set_vid_by_step(cid, t1->iVid, t1->pllvid[best_offset]);
  756. t1->iVid = t1->pllvid[best_offset];
  757. opt_T1VID[cid] = t1->iVid;
  758. }
  759. }
  760. static double T1_best_pll_product(struct T1_chain *t1)
  761. {
  762. double best = 0;
  763. int i;
  764. for (i = 0; i < T1_PLL_TUNE_RANGE; i++) {
  765. if (t1->pllproduct[i] > best)
  766. best = t1->pllproduct[i];
  767. }
  768. return best;
  769. }
  770. static void T1_save_config(void)
  771. {
  772. FILE *fcfg;
  773. fcfg = fopen("/config/cgminer.conf", "w");
  774. if (unlikely(fcfg == NULL)) {
  775. applog(LOG_ERR, "Failed to open /config/cgminer.conf for writing!");
  776. return;
  777. }
  778. write_config(fcfg);
  779. fflush(fcfg);
  780. fclose(fcfg);
  781. }
  782. static void T1_tune_complete(struct T1_chain *t1, int cid)
  783. {
  784. int i;
  785. applog(LOG_WARNING, "T1 %d tuning complete, optimal VID %d PLL %d", cid,
  786. t1->iVid, t1->pll);
  787. t1->sampling = false;
  788. /* Reset hw error count to ignore noise during tuning. */
  789. for(i = 0; i < t1->num_active_chips; ++i)
  790. t1->chips[i].hw_errors = 0;
  791. if (++chains_tuned < total_chains)
  792. return;
  793. applog(LOG_WARNING, "Tuning complete, saving results to config file");
  794. /* Change t1auto to false to save to config to disable tuning
  795. * on next run. */
  796. opt_T1auto = false;
  797. T1_save_config();
  798. /* Reset all stats after tuning */
  799. zero_stats();
  800. }
  801. static void T1_tune(struct T1_chain *t1, int cid)
  802. {
  803. double product, tdiff, best, hw_rate;
  804. int offset, i, hwerr, hw_diff;
  805. struct timeval now;
  806. cgtime(&now);
  807. if (t1->pllOptimal)
  808. return;
  809. if (unlikely(!t1->cycle_start.tv_sec)) {
  810. copy_time(&t1->cycle_start, &now);
  811. t1->cycles = 0;
  812. return;
  813. }
  814. if (t1->cycles < T1_CYCLES_CHAIN)
  815. return;
  816. tdiff = ms_tdiff(&now, &t1->cycle_start);
  817. product = (double)t1->cycles / tdiff;
  818. // hwerr stat.
  819. hwerr = 0;
  820. for(i = 0; i < t1->num_active_chips; ++i)
  821. hwerr += t1->chips[i].hw_errors;
  822. hw_diff = hwerr - t1->hw_errors;
  823. t1->hw_errors = hwerr;
  824. hw_rate = (double) hw_diff / tdiff;
  825. applog(LOG_NOTICE, "Chain %d cycles %d, hw %d, vid %d, pll %d, %.1fms product %f, hwrate %f",
  826. cid, t1->cycles, hw_diff, t1->iVid, t1->pll, tdiff, product, hw_rate);
  827. reset_tune(t1);
  828. if (!t1->sampling) {
  829. /* Discard the first lot of samples due to changing diff on
  830. * startup and possible init times invalidating data. */
  831. t1->sampling = true;
  832. return;
  833. }
  834. if (t1->VidOptimal)
  835. goto tune_freq;
  836. best = T1_best_vid_product(t1);
  837. t1->vidproduct[t1->iVid] = product;
  838. t1->vidhwerr[t1->iVid] = hw_rate;
  839. /* Plenty of time has passed since we set this VID so reading the
  840. * voltage will be accurate here */
  841. get_voltages(t1);
  842. t1->vidvol[t1->iVid] = s_reg_ctrl.average_vol[cid];
  843. /* Don't keep going lower voltage in Performance mode if there's been
  844. * a large drop in product as any further may be unstable */
  845. if (t1->iVid < T1_VID_MAX && (!opt_T1_performance || product > best * 0.9)) {
  846. /* We don't need great accuracy here so no need to delay after
  847. * setting VID */
  848. mcompat_set_vid(cid, ++t1->iVid);
  849. get_voltages(t1);
  850. if (s_reg_ctrl.average_vol[cid] > TUNE_VOLT_STOP) {
  851. applog(LOG_NOTICE, "Chain% d testing iVid %d avg voltage %.0f",
  852. cid, t1->iVid, s_reg_ctrl.average_vol[cid]);
  853. return;
  854. }
  855. }
  856. /* Now find the iVid that corresponds with highest product */
  857. T1_set_optimal_vid(t1, cid);
  858. applog(LOG_WARNING, "T1 %d optimal iVid set to %d, beginning freq tune",
  859. cid, t1->iVid);
  860. return;
  861. tune_freq:
  862. best = T1_best_pll_product(t1);
  863. offset = t1->pll - T1_PLL_TUNE_MIN;
  864. t1->pllproduct[offset] = product;
  865. t1->pllhwerr[offset] = hw_rate;
  866. if (t1->pll < T1_PLL_TUNE_MAX && product > best) {
  867. /* Only keep increasing frequency if product has been
  868. * increasing. */
  869. t1->base_pll = ++t1->pll;
  870. applog(LOG_NOTICE, "Chain %d testing pll %d", cid, t1->pll);
  871. T1_SetT1PLLClock(t1, t1->pll, 0);
  872. if (t1->iVid > T1_VID_MIN) {
  873. /* Vid was set a long time ago so it should be
  874. * accurate to read voltages. */
  875. get_voltages(t1);
  876. if (s_reg_ctrl.average_vol[cid] < t1->optimal_vol - 2) {
  877. applog(LOG_WARNING, "Chain %d dropping VID to %d due to PLL %d lowering voltage to %.0f",
  878. cid, --t1->iVid, t1->pll, s_reg_ctrl.average_vol[cid]);
  879. mcompat_set_vid(cid, t1->iVid);
  880. }
  881. }
  882. /* Store the iVid associated with this pll */
  883. t1->pllvid[offset] = t1->iVid;
  884. return;
  885. }
  886. T1_set_optimal_pll(t1, cid);
  887. applog(LOG_WARNING, "Chain %d optimal pll set to %d Mhz",
  888. cid, PLL_Clk_12Mhz[t1->pll].speedMHz);
  889. T1_tune_complete(t1, cid);
  890. }
  891. #define MAX_NONCE_SLEEP (100)
  892. #define T1_THROTTLE_INTERVAL (5)
  893. #define T1_RAISE_INTERVAL (15)
  894. static void t1_throttle(struct T1_chain *t1, int cid)
  895. {
  896. time_t now;
  897. /* Chain will have been shut down by the time we get to zero but it's
  898. * possible with complete fan failures. */
  899. if (t1->pll <= T1_PLL_MIN)
  900. return;
  901. /* Only throttle further after 5 second intervals */
  902. now = time(NULL);
  903. if (now - t1->throttled < T1_THROTTLE_INTERVAL)
  904. return;
  905. t1->throttled = now;
  906. applog(LOG_WARNING, "T1 %d Chain throttling to %d MHz for overheat!", cid ,
  907. PLL_Clk_12Mhz[--t1->pll].speedMHz);
  908. T1_SetT1PLLClock(t1, t1->pll, 0);
  909. }
  910. static void t1_raise(struct T1_chain *t1, int cid)
  911. {
  912. time_t now = time(NULL);
  913. /* Same as throttling, but wait 15s before increasing frequency */
  914. if (now - t1->throttled < T1_RAISE_INTERVAL)
  915. return;
  916. t1->throttled = now;
  917. applog(LOG_WARNING, "T1 %d Chain increasing frequency to %d MHz from throttle due to cooldown",
  918. cid, PLL_Clk_12Mhz[++t1->pll].speedMHz);
  919. T1_SetT1PLLClock(t1, t1->pll, 0);
  920. /* If we're back to base pll then throttling has ceased */
  921. if (t1->pll >= t1->base_pll) {
  922. t1->throttled = 0;
  923. /* Reset all the values in case we started throttling in the
  924. * middle of tuning, rendering all the values inval. */
  925. reset_tune(t1);
  926. }
  927. }
  928. #define MAX_CMD_FAILS (0)
  929. #define MAX_CMD_RESETS (50)
  930. static int g_cmd_fails[MAX_CHAIN_NUM];
  931. static int g_cmd_resets[MAX_CHAIN_NUM];
  932. static void T1_overheated_blinking(int cid)
  933. {
  934. // block thread and blink led
  935. while (42) {
  936. mcompat_set_led(cid, LED_OFF);
  937. cgsleep_ms(500);
  938. mcompat_set_led(cid, LED_ON);
  939. cgsleep_ms(500);
  940. }
  941. }
  942. static int64_t T1_scanwork(struct thr_info *thr)
  943. {
  944. struct cgpu_info *cgpu = thr->cgpu;
  945. struct T1_chain *t1 = cgpu->device_data;
  946. int cid = t1->chain_id, i;
  947. struct T1_chip *chip;
  948. int64_t hashes = 0;
  949. uint32_t nonce;
  950. uint8_t chip_id;
  951. uint8_t job_id;
  952. uint16_t micro_job_id;
  953. uint8_t reg[REG_LENGTH] = {0};
  954. int timer_sleep = 10;
  955. int slept = 0;
  956. bool nononce = false;
  957. int chain_temp_status;
  958. #ifdef USE_AUTONONCE
  959. bool autononce = true;
  960. #endif
  961. struct timeval now;
  962. if (unlikely((t1->num_cores == 0) || (t1->num_cores > MAX_CORES))) {
  963. cgpu->deven = DEV_DISABLED;
  964. return -1;
  965. }
  966. /* Spurious wakeups are harmless */
  967. pthread_cond_signal(&t1->cond);
  968. cgtime(&now);
  969. /* Poll queued results. A full nonce range takes about 200ms to scan so
  970. * we're unlikely to need more work until then. Poll every 10ms for up
  971. * to 100ms using a reentrant function to avoid having any latency from
  972. * processing received nonces and avoid sleeping too long. */
  973. while (true) {
  974. struct work *work;
  975. if (!get_nonce(t1, (uint8_t*)&nonce, &chip_id, &job_id, (uint8_t*)&micro_job_id)) {
  976. if (timer_sleep < MAX_NONCE_SLEEP && !thr->work_restart && !nononce) {
  977. /* Do not sleep more than twice if no results */
  978. nononce = true;
  979. slept += cgsleep_ms_r(&t1->cgt, timer_sleep);
  980. timer_sleep += 10;
  981. continue;
  982. }
  983. /* Disable any further sleeps */
  984. timer_sleep = MAX_NONCE_SLEEP;
  985. #ifdef USE_AUTONONCE
  986. if (autononce) {
  987. /* Check once more after autononce is disabled
  988. * in case we just missed the last one */
  989. mcompat_cmd_auto_nonce(cid, 0, REG_LENGTH); // disable auto get nonce
  990. autononce = false;
  991. continue;
  992. } else
  993. #endif
  994. break;
  995. }
  996. nononce = false;
  997. nonce = bswap_32(nonce);
  998. chip = &t1->chips[chip_id - 1];
  999. if (nonce == chip->last_nonce) {
  1000. applog(LOG_INFO, "%d: chip %d: duplicate nonce.", cid, chip_id);
  1001. chip->dupes++;
  1002. continue;
  1003. }
  1004. if (chip_id < 1 || chip_id > t1->num_active_chips) {
  1005. applog(LOG_WARNING, "%d: wrong chip_id %d", cid, chip_id);
  1006. continue;
  1007. }
  1008. if (job_id < 1 || job_id > 4) {
  1009. applog(LOG_WARNING, "%d: chip %d: result has wrong ""job_id %d", cid, chip_id, job_id);
  1010. continue;
  1011. }
  1012. chip->last_nonce = nonce;
  1013. work = chip->work[job_id - 1];
  1014. if (work == NULL) {
  1015. /* already been flushed => stale */
  1016. applog(LOG_INFO, "%d: chip %d: stale nonce 0x%08x", cid, chip_id, nonce);
  1017. chip->stales++;
  1018. continue;
  1019. }
  1020. work->micro_job_id = micro_job_id;
  1021. memcpy(work->data, &(work->pool->vmask_001[micro_job_id]), 4);
  1022. if (!submit_nonce(thr, work, nonce)) {
  1023. applog(LOG_INFO, "%d: chip %d: invalid nonce 0x%08x", cid, chip_id, nonce);
  1024. applog(LOG_INFO, "micro_job_id %d", micro_job_id);
  1025. chip->hw_errors++;
  1026. continue;
  1027. }
  1028. applog(LOG_INFO, "YEAH: %d: chip %d / job_id %d: nonce 0x%08x", cid, chip_id, job_id, nonce);
  1029. chip->nonces_found++;
  1030. hashes += work->device_diff;
  1031. t1->cycles++;
  1032. t1->lastshare = now.tv_sec;
  1033. }
  1034. if (unlikely(now.tv_sec - t1->lastshare > 300)) {
  1035. applog(LOG_EMERG, "T1 chain %d not producing shares for more than 5 mins.",
  1036. cid);
  1037. reinit_T1_chain(t1, cid);
  1038. }
  1039. cgsleep_prepare_r(&t1->cgt);
  1040. if (thr->work_restart) {
  1041. if (!dm_cmd_resetjob(cid, CMD_ADDR_BROADCAST, reg))
  1042. applog(LOG_WARNING, "T1 %d clear work failed", cid);
  1043. /* Flush the work chips were currently hashing */
  1044. for (i = 0; i < t1->num_active_chips; i++) {
  1045. int j;
  1046. struct T1_chip *chip = &t1->chips[i];
  1047. for (j = 0; j < 2; j++) {
  1048. if (!chip->work[j])
  1049. continue;
  1050. //applog(LOG_DEBUG, "%d: flushing chip %d, work %d: 0x%p",
  1051. // cid, i, j + 1, work);
  1052. free_work(chip->work[j]);
  1053. }
  1054. chip->last_queued_id = 0;
  1055. }
  1056. /* Flush any work in the driver queue */
  1057. while (t1->active_wq.num_elems > 0) {
  1058. /* We don't want to signal wq_dequeue to get more
  1059. * work until we've emptied the queue */
  1060. struct work *work = wq_dequeue(t1, false);
  1061. free_work(work);
  1062. }
  1063. pthread_cond_signal(&t1->cond);
  1064. /* Reset tuning parameters since dropping work on block change
  1065. * can adversely affect hashrate */
  1066. reset_tune(t1);
  1067. }
  1068. /* Clean spi buffer before read 0a reg */
  1069. hub_spi_clean_chain(cid);
  1070. if (thr->work_restart || mcompat_cmd_read_register(cid, MAX_CHIP_NUM >> 1, reg, REG_LENGTH)) {
  1071. uint8_t qstate = reg[9] & 0x03;
  1072. /* Clear counter */
  1073. g_cmd_fails[cid] = 0;
  1074. g_cmd_resets[cid] = 0;
  1075. /* qstate will always be 0x0 when work_restart is set */
  1076. if (qstate != 0x03) {
  1077. if (qstate == 0x0) {
  1078. for (i = t1->num_active_chips; i > 0; i--) {
  1079. struct T1_chip *chip = &t1->chips[i - 1];
  1080. struct work *work = wq_dequeue(t1, true);
  1081. uint8_t c = i;
  1082. if (unlikely(!work)) {
  1083. reset_tune(t1);
  1084. applog(LOG_WARNING, "T1 %d main work underrun", cid);
  1085. cgsleep_ms(10);
  1086. break;
  1087. }
  1088. if (set_work(t1, c, work, 0))
  1089. chip->nonce_ranges_done++;
  1090. }
  1091. }
  1092. //applog(LOG_NOTICE, "qstate is not 0x0,the number of work is %d. \t", t1->active_wq.num_elems);
  1093. for (i = t1->num_active_chips; i > 0; i--) {
  1094. struct T1_chip *chip = &t1->chips[i - 1];
  1095. struct work *work = wq_dequeue(t1, true);
  1096. uint8_t c = i;
  1097. if (unlikely(!work)) {
  1098. /* Demote this message since it's the
  1099. * backup queue and not critical */
  1100. applog(LOG_INFO, "T1 %d backup work underrun", cid);
  1101. break;
  1102. }
  1103. if (set_work(t1, c, work, 0))
  1104. chip->nonce_ranges_done++;
  1105. }
  1106. }
  1107. } else {
  1108. g_cmd_fails[cid]++;
  1109. if (g_cmd_fails[cid] > MAX_CMD_FAILS) {
  1110. // TODO: replaced with mcompat_spi_reset()
  1111. applog(LOG_ERR, "Chain %d reset spihub", cid);
  1112. hub_spi_clean_chain(cid);
  1113. g_cmd_resets[cid]++;
  1114. if (g_cmd_resets[cid] > MAX_CMD_RESETS) {
  1115. applog(LOG_ERR, "Chain %d is not working due to multiple resets.",
  1116. cid);
  1117. reinit_T1_chain(t1, cid);
  1118. }
  1119. }
  1120. }
  1121. /* Temperature control */
  1122. chain_temp_status = dm_tempctrl_update_chain_temp(cid);
  1123. cgpu->temp_min = (double)g_chain_tmp[cid].tmp_lo;
  1124. cgpu->temp_max = (double)g_chain_tmp[cid].tmp_hi;
  1125. cgpu->temp = (double)g_chain_tmp[cid].tmp_avg;
  1126. if (chain_temp_status == TEMP_SHUTDOWN) {
  1127. // shut down chain
  1128. applog(LOG_ERR, "DANGEROUS TEMPERATURE(%.0f): power down chain %d",
  1129. cgpu->temp_max, cid);
  1130. /* Only time we power down is for dangerous overheat */
  1131. mcompat_chain_power_down(cid);
  1132. cgpu->status = LIFE_DEAD;
  1133. cgtime(&thr->sick);
  1134. /* Function doesn't currently return */
  1135. T1_overheated_blinking(cid);
  1136. } else if (chain_temp_status == TEMP_WARNING ||
  1137. (g_chain_tmp[cid].optimal && g_fan_cfg.fan_speed > opt_T1_target))
  1138. t1_throttle(t1, cid);
  1139. else if (t1->throttled && chain_temp_status == TEMP_NORMAL &&
  1140. g_fan_cfg.fan_speed < opt_T1_target)
  1141. t1_raise(t1, cid);
  1142. /* Tuning */
  1143. if (!thr->work_restart && !t1->throttled && opt_T1auto)
  1144. T1_tune(t1, cid);
  1145. /* read chip temperatures and voltages */
  1146. if (g_debug_stats[cid]) {
  1147. cgsleep_ms(1);
  1148. get_temperatures(t1);
  1149. get_voltages(t1);
  1150. g_debug_stats[cid] = 0;
  1151. }
  1152. #ifdef USE_AUTONONCE
  1153. mcompat_cmd_auto_nonce(cid, 1, REG_LENGTH); // enable auto get nonce
  1154. #endif
  1155. /* in case of no progress, prevent busy looping */
  1156. pthread_cond_signal(&t1->cond);
  1157. /* If we haven't slept at least 10ms we are at risk of polling too
  1158. * often and being CPU bound. Either the chain is too busy or internal
  1159. * clock based sleep behaviour may be off so revert to simple usleep. */
  1160. if (slept < 10)
  1161. usleep((10 - slept) * 1000);
  1162. return hashes * 0x100000000ull;
  1163. }
  1164. static int chains_shutdown;
  1165. /* Shut down the chains gracefully. We do not want to power them down as it
  1166. * makes the next start unreliable, so we decrease power usage to a minimum. */
  1167. static void T1_shutdown(struct thr_info *thr)
  1168. {
  1169. struct cgpu_info *cgpu = thr->cgpu;
  1170. struct T1_chain *t1 = cgpu->device_data;
  1171. int cid = t1->chain_id;
  1172. mcompat_set_spi_speed(cid, T1_SPI_SPEED_DEF);
  1173. /* Set a very low frequency. */
  1174. t1_set_pll(t1, CMD_ADDR_BROADCAST, 0);
  1175. /* Set a very low voltage */
  1176. mcompat_set_vid_by_step(cid, t1->iVid, T1_VID_MAX);
  1177. /* Confirm we have actually reset the chains */
  1178. if (!mcompat_set_reset(cid, 0)) {
  1179. applog(LOG_ERR, "Failed to reset chain %d on shutdown", cid);
  1180. return;
  1181. }
  1182. /* Only once all chains are successfully shut down is it safe to turn
  1183. * down fan speed */
  1184. if (++chains_shutdown < total_chains)
  1185. return;
  1186. pthread_cancel(fan_tid);
  1187. dm_fanctrl_set_fan_speed(FAN_SPEED_PREHEAT);
  1188. }
  1189. static void T1_get_statline_before(char *buf, size_t len, struct cgpu_info *cgpu)
  1190. {
  1191. struct T1_chain *t1 = cgpu->device_data;
  1192. char temp[10];
  1193. if (cgpu->temp != 0)
  1194. snprintf(temp, 9, "%2.0fC", cgpu->temp);
  1195. tailsprintf(buf, len, " %2d:%2d/%3d %s",
  1196. t1->chain_id, t1->num_active_chips, t1->num_cores,
  1197. cgpu->temp == 0 ? " " : temp);
  1198. }
  1199. static struct api_data *T1_api_stats(struct cgpu_info *cgpu)
  1200. {
  1201. struct T1_chain *t1 = cgpu->device_data;
  1202. int fan_speed = g_fan_cfg.fan_speed;
  1203. unsigned long long int chipmap = 0;
  1204. struct api_data *root = NULL;
  1205. bool fake;
  1206. int i;
  1207. char s[32];
  1208. ROOT_ADD_API(int, "Chain ID", t1->chain_id, false);
  1209. ROOT_ADD_API(int, "Num chips", t1->num_chips, false);
  1210. ROOT_ADD_API(int, "Num cores", t1->num_cores, false);
  1211. ROOT_ADD_API(int, "Num active chips", t1->num_active_chips, false);
  1212. ROOT_ADD_API(int, "Chain skew", t1->chain_skew, false);
  1213. ROOT_ADD_API(double, "Temp max", cgpu->temp_max, false);
  1214. ROOT_ADD_API(double, "Temp min", cgpu->temp_min, false);
  1215. ROOT_ADD_API(int, "Fan duty", fan_speed, true);
  1216. ROOT_ADD_API(int, "iVid", t1->iVid, false);
  1217. ROOT_ADD_API(int, "PLL", t1->pll, false);
  1218. ROOT_ADD_API(double, "Voltage Max", s_reg_ctrl.highest_vol[t1->chain_id], false);
  1219. ROOT_ADD_API(double, "Voltage Min", s_reg_ctrl.lowest_vol[t1->chain_id], false);
  1220. ROOT_ADD_API(double, "Voltage Avg", s_reg_ctrl.average_vol[t1->chain_id], false);
  1221. ROOT_ADD_API(bool, "VidOptimal", t1->VidOptimal, false);
  1222. ROOT_ADD_API(bool, "pllOptimal", t1->pllOptimal, false);
  1223. ROOT_ADD_API(int, "Chain num", cgpu->chainNum, false);
  1224. ROOT_ADD_API(double, "MHS av", cgpu->mhs_av, false);
  1225. ROOT_ADD_API(bool, "Disabled", t1->disabled, false);
  1226. fake = !!t1->throttled;
  1227. ROOT_ADD_API(bool, "Throttled", fake, true);
  1228. for (i = 0; i < t1->num_chips; i++) {
  1229. if (!t1->chips[i].disabled)
  1230. chipmap |= 1 << i;
  1231. }
  1232. sprintf(s, "%Lx", chipmap);
  1233. ROOT_ADD_API(string, "Enabled chips", s[0], true);
  1234. ROOT_ADD_API(double, "Temp", cgpu->temp, false);
  1235. for (i = 0; i < t1->num_chips; i++) {
  1236. sprintf(s, "%02d HW errors", i);
  1237. ROOT_ADD_API(int, s, t1->chips[i].hw_errors, true);
  1238. sprintf(s, "%02d Stales", i);
  1239. ROOT_ADD_API(int, s, t1->chips[i].stales, true);
  1240. sprintf(s, "%02d Duplicates", i);
  1241. ROOT_ADD_API(int, s, t1->chips[i].dupes, true);
  1242. sprintf(s, "%02d Nonces found", i);
  1243. ROOT_ADD_API(int, s, t1->chips[i].nonces_found, true);
  1244. sprintf(s, "%02d Nonce ranges", i);
  1245. ROOT_ADD_API(int, s, t1->chips[i].nonce_ranges_done, true);
  1246. sprintf(s, "%02d Cooldown", i);
  1247. ROOT_ADD_API(int, s, t1->chips[i].cooldown_begin, true);
  1248. sprintf(s, "%02d Fail count", i);
  1249. ROOT_ADD_API(int, s, t1->chips[i].fail_count, true);
  1250. sprintf(s, "%02d Fail reset", i);
  1251. ROOT_ADD_API(int, s, t1->chips[i].fail_reset, true);
  1252. sprintf(s, "%02d Temp", i);
  1253. ROOT_ADD_API(int, s, t1->chips[i].temp, true);
  1254. sprintf(s, "%02d nVol", i);
  1255. ROOT_ADD_API(int, s, t1->chips[i].nVol, true);
  1256. }
  1257. return root;
  1258. }
  1259. static struct api_data *T1_api_debug(struct cgpu_info *cgpu)
  1260. {
  1261. struct T1_chain *t1 = cgpu->device_data;
  1262. int timeout = 1000;
  1263. g_debug_stats[t1->chain_id] = 1;
  1264. // Wait for g_debug_stats cleared or timeout
  1265. while (g_debug_stats[t1->chain_id] && timeout) {
  1266. timeout -= 10;
  1267. cgsleep_ms(10);
  1268. }
  1269. return T1_api_stats(cgpu);
  1270. }
  1271. struct device_drv dragonmintT1_drv = {
  1272. .drv_id = DRIVER_dragonmintT1,
  1273. .dname = "DragonmintT1",
  1274. .name = "DT1",
  1275. .drv_detect = T1_detect,
  1276. /* Set to lowest diff we can reliably use to get accurate hashrates. */
  1277. .max_diff = 129,
  1278. .hash_work = hash_driver_work,
  1279. .scanwork = T1_scanwork,
  1280. .thread_shutdown = T1_shutdown,
  1281. .get_api_stats = T1_api_stats,
  1282. .get_api_debug = T1_api_debug,
  1283. .get_statline_before = T1_get_statline_before,
  1284. };