miner.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. #ifndef __MINER_H__
  2. #define __MINER_H__
  3. #include "config.h"
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include <sys/time.h>
  7. #include <pthread.h>
  8. #include <jansson.h>
  9. #include <curl/curl.h>
  10. #include "elist.h"
  11. #include "uthash.h"
  12. #ifdef HAVE_OPENCL
  13. #ifdef __APPLE_CC__
  14. #include <OpenCL/opencl.h>
  15. #else
  16. #include <CL/cl.h>
  17. #endif
  18. #endif /* HAVE_OPENCL */
  19. #ifdef STDC_HEADERS
  20. # include <stdlib.h>
  21. # include <stddef.h>
  22. #else
  23. # ifdef HAVE_STDLIB_H
  24. # include <stdlib.h>
  25. # endif
  26. #endif
  27. #ifdef HAVE_ALLOCA_H
  28. # include <alloca.h>
  29. #elif defined __GNUC__
  30. # ifndef WIN32
  31. # define alloca __builtin_alloca
  32. # else
  33. # include <malloc.h>
  34. # endif
  35. #elif defined _AIX
  36. # define alloca __alloca
  37. #elif defined _MSC_VER
  38. # include <malloc.h>
  39. # define alloca _alloca
  40. #else
  41. # ifndef HAVE_ALLOCA
  42. # ifdef __cplusplus
  43. extern "C"
  44. # endif
  45. void *alloca (size_t);
  46. # endif
  47. #endif
  48. #if defined (__linux)
  49. #ifndef LINUX
  50. #define LINUX
  51. #endif
  52. #endif
  53. #ifdef HAVE_ADL
  54. #include "ADL_SDK/adl_sdk.h"
  55. #endif
  56. #ifdef __SSE2__
  57. #define WANT_SSE2_4WAY 1
  58. #endif
  59. #ifdef __ALTIVEC__
  60. #define WANT_ALTIVEC_4WAY 1
  61. #endif
  62. #if defined(__i386__) && defined(HAS_YASM) && defined(__SSE2__)
  63. #define WANT_X8632_SSE2 1
  64. #endif
  65. #if (defined(__i386__) || defined(__x86_64__)) && !defined(__APPLE__)
  66. #define WANT_VIA_PADLOCK 1
  67. #endif
  68. #if defined(__x86_64__) && defined(HAS_YASM)
  69. #define WANT_X8664_SSE2 1
  70. #endif
  71. #if defined(__x86_64__) && defined(HAS_YASM)
  72. #define WANT_X8664_SSE4 1
  73. #endif
  74. #if !defined(WIN32) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
  75. #define bswap_16 __builtin_bswap16
  76. #define bswap_32 __builtin_bswap32
  77. #define bswap_64 __builtin_bswap64
  78. #else
  79. #if HAVE_BYTESWAP_H
  80. #include <byteswap.h>
  81. #elif defined(USE_SYS_ENDIAN_H)
  82. #include <sys/endian.h>
  83. #elif defined(__APPLE__)
  84. #include <libkern/OSByteOrder.h>
  85. #define bswap_16 OSSwapInt16
  86. #define bswap_32 OSSwapInt32
  87. #define bswap_64 OSSwapInt64
  88. #else
  89. #define bswap_16(value) \
  90. ((((value) & 0xff) << 8) | ((value) >> 8))
  91. #define bswap_32(value) \
  92. (((uint32_t)bswap_16((uint16_t)((value) & 0xffff)) << 16) | \
  93. (uint32_t)bswap_16((uint16_t)((value) >> 16)))
  94. #define bswap_64(value) \
  95. (((uint64_t)bswap_32((uint32_t)((value) & 0xffffffff)) \
  96. << 32) | \
  97. (uint64_t)bswap_32((uint32_t)((value) >> 32)))
  98. #endif
  99. #endif /* !defined(__GLXBYTEORDER_H__) */
  100. /* This assumes htobe32 is a macro in endian.h */
  101. #ifndef htobe32
  102. # if __BYTE_ORDER == __LITTLE_ENDIAN
  103. # define be32toh(x) bswap_32(x)
  104. # define htobe32(x) bswap_32(x)
  105. # elif __BYTE_ORDER == __BIG_ENDIAN
  106. # define be32toh(x) (x)
  107. # define htobe32(x) (x)
  108. #else
  109. #error UNKNOWN BYTE ORDER
  110. #endif
  111. #endif
  112. #ifdef HAVE_SYSLOG_H
  113. #include <syslog.h>
  114. #else
  115. enum {
  116. LOG_ERR,
  117. LOG_WARNING,
  118. LOG_NOTICE,
  119. LOG_INFO,
  120. LOG_DEBUG,
  121. };
  122. #endif
  123. #undef unlikely
  124. #undef likely
  125. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  126. #define unlikely(expr) (__builtin_expect(!!(expr), 0))
  127. #define likely(expr) (__builtin_expect(!!(expr), 1))
  128. #else
  129. #define unlikely(expr) (expr)
  130. #define likely(expr) (expr)
  131. #endif
  132. #if defined(__i386__)
  133. #define WANT_CRYPTOPP_ASM32
  134. #endif
  135. #ifndef ARRAY_SIZE
  136. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  137. #endif
  138. enum sha256_algos {
  139. ALGO_C, /* plain C */
  140. ALGO_4WAY, /* parallel SSE2 */
  141. ALGO_VIA, /* VIA padlock */
  142. ALGO_CRYPTOPP, /* Crypto++ (C) */
  143. ALGO_CRYPTOPP_ASM32, /* Crypto++ 32-bit assembly */
  144. ALGO_SSE2_32, /* SSE2 for x86_32 */
  145. ALGO_SSE2_64, /* SSE2 for x86_64 */
  146. ALGO_SSE4_64, /* SSE4 for x86_64 */
  147. ALGO_ALTIVEC_4WAY, /* parallel Altivec */
  148. };
  149. enum alive {
  150. LIFE_WELL,
  151. LIFE_SICK,
  152. LIFE_DEAD,
  153. LIFE_NOSTART
  154. };
  155. #ifdef HAVE_ADL
  156. struct gpu_adl {
  157. ADLTemperature lpTemperature;
  158. int iAdapterIndex;
  159. int lpAdapterID;
  160. ADLPMActivity lpActivity;
  161. ADLODParameters lpOdParameters;
  162. ADLODPerformanceLevels *DefPerfLev;
  163. ADLFanSpeedInfo lpFanSpeedInfo;
  164. ADLFanSpeedValue lpFanSpeedValue;
  165. ADLFanSpeedValue DefFanSpeedValue;
  166. int iEngineClock;
  167. int iMemoryClock;
  168. int iVddc;
  169. int iPercentage;
  170. bool autofan;
  171. bool autoengine;
  172. bool managed; /* Were the values ever changed on this card */
  173. int lasttemp;
  174. int targetfan;
  175. int targettemp;
  176. int overtemp;
  177. int cutofftemp;
  178. int minspeed;
  179. int maxspeed;
  180. };
  181. #endif
  182. struct cgpu_info;
  183. struct thr_info;
  184. struct work;
  185. struct device_api {
  186. char*name;
  187. // API-global functions
  188. void (*api_detect)();
  189. // Device-specific functions
  190. void (*reinit_device)(struct cgpu_info*);
  191. void (*get_statline)(char*, struct cgpu_info*);
  192. // Thread-specific functions
  193. bool (*thread_prepare)(struct thr_info*);
  194. uint64_t (*can_limit_work)(struct thr_info*);
  195. bool (*thread_init)(struct thr_info*);
  196. void (*free_work)(struct thr_info*, struct work*);
  197. bool (*prepare_work)(struct thr_info*, struct work*);
  198. uint64_t (*scanhash)(struct thr_info*, struct work*, uint64_t);
  199. void (*thread_shutdown)(struct thr_info*);
  200. };
  201. struct cgpu_info {
  202. int cgminer_id;
  203. struct device_api *api;
  204. int device_id;
  205. char *device_path;
  206. FILE *device_file;
  207. bool enabled;
  208. int accepted;
  209. int rejected;
  210. int hw_errors;
  211. double rolling;
  212. double total_mhashes;
  213. double utility;
  214. enum alive status;
  215. char init[40];
  216. struct timeval last_message_tv;
  217. int threads;
  218. bool dynamic;
  219. int intensity;
  220. #ifdef HAVE_ADL
  221. bool has_adl;
  222. struct gpu_adl adl;
  223. int gpu_engine;
  224. int min_engine;
  225. int gpu_fan;
  226. int min_fan;
  227. int gpu_memclock;
  228. int gpu_memdiff;
  229. int gpu_powertune;
  230. float gpu_vddc;
  231. #endif
  232. };
  233. #ifndef WIN32
  234. #define PTH(thr) ((thr)->pth)
  235. #else
  236. #define PTH(thr) ((thr)->pth.p)
  237. #endif
  238. struct thread_q {
  239. struct list_head q;
  240. bool frozen;
  241. pthread_mutex_t mutex;
  242. pthread_cond_t cond;
  243. };
  244. struct thr_info {
  245. int id;
  246. pthread_t pth;
  247. struct thread_q *q;
  248. struct cgpu_info *cgpu;
  249. void *cgpu_data;
  250. struct timeval last;
  251. struct timeval sick;
  252. bool pause;
  253. bool getwork;
  254. double rolling;
  255. };
  256. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  257. extern void thr_info_cancel(struct thr_info *thr);
  258. struct string_elist {
  259. char *string;
  260. bool free_me;
  261. struct list_head list;
  262. };
  263. static inline void string_elist_add(const char *s, struct list_head *head)
  264. {
  265. struct string_elist *n;
  266. n = calloc(1, sizeof(*n));
  267. n->string = strdup(s);
  268. n->free_me = true;
  269. list_add_tail(&n->list, head);
  270. }
  271. static inline void string_elist_del(struct string_elist *item)
  272. {
  273. if (item->free_me)
  274. free(item->string);
  275. list_del(&item->list);
  276. }
  277. static inline uint32_t swab32(uint32_t v)
  278. {
  279. return bswap_32(v);
  280. }
  281. static inline void swap256(void *dest_p, const void *src_p)
  282. {
  283. uint32_t *dest = dest_p;
  284. const uint32_t *src = src_p;
  285. dest[0] = src[7];
  286. dest[1] = src[6];
  287. dest[2] = src[5];
  288. dest[3] = src[4];
  289. dest[4] = src[3];
  290. dest[5] = src[2];
  291. dest[6] = src[1];
  292. dest[7] = src[0];
  293. }
  294. extern void quit(int status, const char *format, ...);
  295. static inline void mutex_lock(pthread_mutex_t *lock)
  296. {
  297. if (unlikely(pthread_mutex_lock(lock)))
  298. quit(1, "WTF MUTEX ERROR ON LOCK!");
  299. }
  300. static inline void mutex_unlock(pthread_mutex_t *lock)
  301. {
  302. if (unlikely(pthread_mutex_unlock(lock)))
  303. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  304. }
  305. static inline void wr_lock(pthread_rwlock_t *lock)
  306. {
  307. if (unlikely(pthread_rwlock_wrlock(lock)))
  308. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  309. }
  310. static inline void rd_lock(pthread_rwlock_t *lock)
  311. {
  312. if (unlikely(pthread_rwlock_rdlock(lock)))
  313. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  314. }
  315. static inline void rw_unlock(pthread_rwlock_t *lock)
  316. {
  317. if (unlikely(pthread_rwlock_unlock(lock)))
  318. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  319. }
  320. static inline void rd_unlock(pthread_rwlock_t *lock)
  321. {
  322. rw_unlock(lock);
  323. }
  324. static inline void wr_unlock(pthread_rwlock_t *lock)
  325. {
  326. rw_unlock(lock);
  327. }
  328. struct pool;
  329. extern bool opt_debug;
  330. extern bool opt_protocol;
  331. extern bool opt_log_output;
  332. extern char *opt_kernel_path;
  333. extern char *cgminer_path;
  334. extern bool opt_autofan;
  335. extern bool opt_autoengine;
  336. extern bool use_curses;
  337. extern char *opt_api_description;
  338. extern int opt_api_port;
  339. extern bool opt_api_listen;
  340. extern bool opt_api_network;
  341. extern const uint32_t sha256_init_state[];
  342. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  343. const char *rpc_req, bool, bool, bool *,
  344. struct pool *pool);
  345. extern char *bin2hex(const unsigned char *p, size_t len);
  346. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  347. typedef bool (*sha256_func)(int thr_id, const unsigned char *pmidstate,
  348. unsigned char *pdata,
  349. unsigned char *phash1, unsigned char *phash,
  350. const unsigned char *ptarget,
  351. uint32_t max_nonce,
  352. uint32_t *last_nonce,
  353. uint32_t nonce);
  354. extern bool ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
  355. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  356. const unsigned char *ptarget,
  357. uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
  358. extern bool ScanHash_altivec_4way(int thr_id, const unsigned char *pmidstate,
  359. unsigned char *pdata,
  360. unsigned char *phash1, unsigned char *phash,
  361. const unsigned char *ptarget,
  362. uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
  363. extern bool scanhash_via(int, const unsigned char *pmidstate,
  364. unsigned char *pdata,
  365. unsigned char *phash1, unsigned char *phash,
  366. const unsigned char *target,
  367. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
  368. extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
  369. unsigned char *hash1, unsigned char *hash,
  370. const unsigned char *target,
  371. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
  372. extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
  373. unsigned char *hash1, unsigned char *hash,
  374. const unsigned char *target,
  375. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
  376. extern bool scanhash_asm32(int, const unsigned char *midstate,unsigned char *data,
  377. unsigned char *hash1, unsigned char *hash,
  378. const unsigned char *target,
  379. uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
  380. extern bool scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  381. unsigned char *phash1, unsigned char *phash,
  382. const unsigned char *ptarget,
  383. uint32_t max_nonce, uint32_t *last_nonce,
  384. uint32_t nonce);
  385. extern bool scanhash_sse4_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  386. unsigned char *phash1, unsigned char *phash,
  387. const unsigned char *ptarget,
  388. uint32_t max_nonce, uint32_t *last_nonce,
  389. uint32_t nonce);
  390. extern bool scanhash_sse2_32(int, const unsigned char *pmidstate, unsigned char *pdata,
  391. unsigned char *phash1, unsigned char *phash,
  392. const unsigned char *ptarget,
  393. uint32_t max_nonce, uint32_t *last_nonce,
  394. uint32_t nonce);
  395. extern int
  396. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  397. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  398. extern int opt_scantime;
  399. struct work_restart {
  400. volatile unsigned long restart;
  401. char padding[128 - sizeof(unsigned long)];
  402. };
  403. extern void kill_work(void);
  404. extern void reinit_device(struct cgpu_info *cgpu);
  405. #ifdef HAVE_ADL
  406. extern float gpu_temp(int gpu);
  407. extern int gpu_fanspeed(int gpu);
  408. extern int gpu_fanpercent(int gpu);
  409. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  410. #endif
  411. extern void api(void);
  412. #define MAX_GPUDEVICES 16
  413. #define MAX_DEVICES 32
  414. #define MAX_POOLS (32)
  415. extern struct list_head scan_devices;
  416. extern int nDevs;
  417. extern int opt_n_threads;
  418. extern int num_processors;
  419. extern int hw_errors;
  420. extern bool use_syslog;
  421. extern struct thr_info *thr_info;
  422. extern int longpoll_thr_id;
  423. extern struct work_restart *work_restart;
  424. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  425. extern int gpu_threads;
  426. extern double total_secs;
  427. extern int mining_threads;
  428. extern struct cgpu_info *cpus;
  429. extern int total_devices;
  430. extern struct cgpu_info *devices[];
  431. extern int total_pools;
  432. extern struct pool *pools[MAX_POOLS];
  433. extern const char *algo_names[];
  434. extern enum sha256_algos opt_algo;
  435. extern double total_mhashes_done;
  436. extern unsigned int new_blocks;
  437. extern unsigned int found_blocks;
  438. extern int total_accepted, total_rejected;
  439. extern int total_getworks, total_stale, total_discarded;
  440. extern unsigned int local_work;
  441. extern unsigned int total_go, total_ro;
  442. extern int opt_log_interval;
  443. #ifdef HAVE_OPENCL
  444. typedef struct {
  445. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  446. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  447. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  448. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  449. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  450. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  451. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  452. cl_uint W16; cl_uint W17; cl_uint W2;
  453. cl_uint PreVal4; cl_uint T1;
  454. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  455. cl_uint PreVal4addT1; cl_uint T1substate0;
  456. cl_uint PreVal4_2;
  457. cl_uint PreVal0;
  458. cl_uint PreW18;
  459. cl_uint PreW19;
  460. cl_uint PreW31;
  461. cl_uint PreW32;
  462. } dev_blk_ctx;
  463. #else
  464. typedef struct {
  465. uint32_t nonce;
  466. } dev_blk_ctx;
  467. #endif
  468. struct pool {
  469. int pool_no;
  470. int prio;
  471. int accepted, rejected;
  472. bool submit_fail;
  473. bool idle;
  474. bool lagging;
  475. bool probed;
  476. bool enabled;
  477. char *hdr_path;
  478. unsigned int getwork_requested;
  479. unsigned int stale_shares;
  480. unsigned int discarded_work;
  481. unsigned int getfail_occasions;
  482. unsigned int remotefail_occasions;
  483. struct timeval tv_idle;
  484. char *rpc_url;
  485. char *rpc_userpass;
  486. char *rpc_user, *rpc_pass;
  487. pthread_mutex_t pool_lock;
  488. };
  489. struct work {
  490. unsigned char data[128];
  491. unsigned char hash1[64];
  492. unsigned char midstate[32];
  493. unsigned char target[32];
  494. unsigned char hash[32];
  495. int rolls;
  496. uint32_t output[1];
  497. uint32_t valid;
  498. dev_blk_ctx blk;
  499. struct thr_info *thr;
  500. int thr_id;
  501. struct pool *pool;
  502. struct timeval tv_staged;
  503. bool mined;
  504. bool clone;
  505. bool cloned;
  506. bool rolltime;
  507. unsigned int work_block;
  508. int id;
  509. UT_hash_handle hh;
  510. };
  511. enum cl_kernel {
  512. KL_NONE,
  513. KL_POCLBM,
  514. KL_PHATK,
  515. };
  516. extern void get_datestamp(char *, struct timeval *);
  517. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  518. extern void wlogprint(const char *f, ...);
  519. extern int curses_int(const char *query);
  520. extern char *curses_input(const char *query);
  521. extern void kill_work(void);
  522. extern void log_curses(int prio, const char *f, va_list ap);
  523. extern void clear_logwin(void);
  524. extern void vapplog(int prio, const char *fmt, va_list ap);
  525. extern void applog(int prio, const char *fmt, ...);
  526. extern struct thread_q *tq_new(void);
  527. extern void tq_free(struct thread_q *tq);
  528. extern bool tq_push(struct thread_q *tq, void *data);
  529. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  530. extern void tq_freeze(struct thread_q *tq);
  531. extern void tq_thaw(struct thread_q *tq);
  532. extern bool successful_connect;
  533. extern enum cl_kernel chosen_kernel;
  534. extern void adl(void);
  535. extern bool get_dondata(char **url, char **userpass);
  536. #endif /* __MINER_H__ */