miner.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. bool enabled;
  206. int accepted;
  207. int rejected;
  208. int hw_errors;
  209. double rolling;
  210. double total_mhashes;
  211. double utility;
  212. enum alive status;
  213. char init[40];
  214. struct timeval last_message_tv;
  215. int threads;
  216. bool dynamic;
  217. int intensity;
  218. #ifdef HAVE_ADL
  219. bool has_adl;
  220. struct gpu_adl adl;
  221. int gpu_engine;
  222. int min_engine;
  223. int gpu_fan;
  224. int min_fan;
  225. int gpu_memclock;
  226. int gpu_memdiff;
  227. int gpu_powertune;
  228. float gpu_vddc;
  229. #endif
  230. };
  231. #ifndef WIN32
  232. #define PTH(thr) ((thr)->pth)
  233. #else
  234. #define PTH(thr) ((thr)->pth.p)
  235. #endif
  236. struct thread_q {
  237. struct list_head q;
  238. bool frozen;
  239. pthread_mutex_t mutex;
  240. pthread_cond_t cond;
  241. };
  242. struct thr_info {
  243. int id;
  244. pthread_t pth;
  245. struct thread_q *q;
  246. struct cgpu_info *cgpu;
  247. void *cgpu_data;
  248. struct timeval last;
  249. struct timeval sick;
  250. bool pause;
  251. bool getwork;
  252. double rolling;
  253. };
  254. extern int thr_info_create(struct thr_info *thr, pthread_attr_t *attr, void *(*start) (void *), void *arg);
  255. extern void thr_info_cancel(struct thr_info *thr);
  256. static inline uint32_t swab32(uint32_t v)
  257. {
  258. return bswap_32(v);
  259. }
  260. static inline void swap256(void *dest_p, const void *src_p)
  261. {
  262. uint32_t *dest = dest_p;
  263. const uint32_t *src = src_p;
  264. dest[0] = src[7];
  265. dest[1] = src[6];
  266. dest[2] = src[5];
  267. dest[3] = src[4];
  268. dest[4] = src[3];
  269. dest[5] = src[2];
  270. dest[6] = src[1];
  271. dest[7] = src[0];
  272. }
  273. extern void quit(int status, const char *format, ...);
  274. static inline void mutex_lock(pthread_mutex_t *lock)
  275. {
  276. if (unlikely(pthread_mutex_lock(lock)))
  277. quit(1, "WTF MUTEX ERROR ON LOCK!");
  278. }
  279. static inline void mutex_unlock(pthread_mutex_t *lock)
  280. {
  281. if (unlikely(pthread_mutex_unlock(lock)))
  282. quit(1, "WTF MUTEX ERROR ON UNLOCK!");
  283. }
  284. static inline void wr_lock(pthread_rwlock_t *lock)
  285. {
  286. if (unlikely(pthread_rwlock_wrlock(lock)))
  287. quit(1, "WTF WRLOCK ERROR ON LOCK!");
  288. }
  289. static inline void rd_lock(pthread_rwlock_t *lock)
  290. {
  291. if (unlikely(pthread_rwlock_rdlock(lock)))
  292. quit(1, "WTF RDLOCK ERROR ON LOCK!");
  293. }
  294. static inline void rw_unlock(pthread_rwlock_t *lock)
  295. {
  296. if (unlikely(pthread_rwlock_unlock(lock)))
  297. quit(1, "WTF RWLOCK ERROR ON UNLOCK!");
  298. }
  299. static inline void rd_unlock(pthread_rwlock_t *lock)
  300. {
  301. rw_unlock(lock);
  302. }
  303. static inline void wr_unlock(pthread_rwlock_t *lock)
  304. {
  305. rw_unlock(lock);
  306. }
  307. struct pool;
  308. extern bool opt_debug;
  309. extern bool opt_protocol;
  310. extern bool opt_log_output;
  311. extern char *opt_kernel_path;
  312. extern char *cgminer_path;
  313. extern bool opt_autofan;
  314. extern bool opt_autoengine;
  315. extern bool use_curses;
  316. extern char *opt_api_description;
  317. extern int opt_api_port;
  318. extern bool opt_api_listen;
  319. extern bool opt_api_network;
  320. extern const uint32_t sha256_init_state[];
  321. extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
  322. const char *rpc_req, bool, bool, bool *,
  323. struct pool *pool);
  324. extern char *bin2hex(const unsigned char *p, size_t len);
  325. extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
  326. typedef bool (*sha256_func)(int thr_id, const unsigned char *pmidstate,
  327. unsigned char *pdata,
  328. unsigned char *phash1, unsigned char *phash,
  329. const unsigned char *ptarget,
  330. uint32_t max_nonce,
  331. uint32_t *last_nonce,
  332. uint32_t nonce);
  333. extern bool ScanHash_4WaySSE2(int, const unsigned char *pmidstate,
  334. unsigned char *pdata, unsigned char *phash1, unsigned char *phash,
  335. const unsigned char *ptarget,
  336. uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
  337. extern bool ScanHash_altivec_4way(int thr_id, const unsigned char *pmidstate,
  338. unsigned char *pdata,
  339. unsigned char *phash1, unsigned char *phash,
  340. const unsigned char *ptarget,
  341. uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
  342. extern bool scanhash_via(int, const unsigned char *pmidstate,
  343. unsigned char *pdata,
  344. unsigned char *phash1, unsigned char *phash,
  345. const unsigned char *target,
  346. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
  347. extern bool scanhash_c(int, const unsigned char *midstate, unsigned char *data,
  348. unsigned char *hash1, unsigned char *hash,
  349. const unsigned char *target,
  350. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
  351. extern bool scanhash_cryptopp(int, const unsigned char *midstate,unsigned char *data,
  352. unsigned char *hash1, unsigned char *hash,
  353. const unsigned char *target,
  354. uint32_t max_nonce, uint32_t *last_nonce, uint32_t n);
  355. extern bool scanhash_asm32(int, const unsigned char *midstate,unsigned char *data,
  356. unsigned char *hash1, unsigned char *hash,
  357. const unsigned char *target,
  358. uint32_t max_nonce, uint32_t *last_nonce, uint32_t nonce);
  359. extern bool scanhash_sse2_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  360. unsigned char *phash1, unsigned char *phash,
  361. const unsigned char *ptarget,
  362. uint32_t max_nonce, uint32_t *last_nonce,
  363. uint32_t nonce);
  364. extern bool scanhash_sse4_64(int, const unsigned char *pmidstate, unsigned char *pdata,
  365. unsigned char *phash1, unsigned char *phash,
  366. const unsigned char *ptarget,
  367. uint32_t max_nonce, uint32_t *last_nonce,
  368. uint32_t nonce);
  369. extern bool scanhash_sse2_32(int, const unsigned char *pmidstate, unsigned char *pdata,
  370. unsigned char *phash1, unsigned char *phash,
  371. const unsigned char *ptarget,
  372. uint32_t max_nonce, uint32_t *last_nonce,
  373. uint32_t nonce);
  374. extern int
  375. timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y);
  376. extern bool fulltest(const unsigned char *hash, const unsigned char *target);
  377. extern int opt_scantime;
  378. struct work_restart {
  379. volatile unsigned long restart;
  380. char padding[128 - sizeof(unsigned long)];
  381. };
  382. extern void kill_work(void);
  383. extern void reinit_device(struct cgpu_info *cgpu);
  384. #ifdef HAVE_ADL
  385. extern float gpu_temp(int gpu);
  386. extern int gpu_fanspeed(int gpu);
  387. extern int gpu_fanpercent(int gpu);
  388. extern bool gpu_stats(int gpu, float *temp, int *engineclock, int *memclock, float *vddc, int *activity, int *fanspeed, int *fanpercent, int *powertune);
  389. #endif
  390. extern void api(void);
  391. #define MAX_GPUDEVICES 16
  392. #define MAX_DEVICES 32
  393. #define MAX_POOLS (32)
  394. extern int nDevs;
  395. extern int opt_n_threads;
  396. extern int num_processors;
  397. extern int hw_errors;
  398. extern bool use_syslog;
  399. extern struct thr_info *thr_info;
  400. extern int longpoll_thr_id;
  401. extern struct work_restart *work_restart;
  402. extern struct cgpu_info gpus[MAX_GPUDEVICES];
  403. extern int gpu_threads;
  404. extern double total_secs;
  405. extern int mining_threads;
  406. extern struct cgpu_info *cpus;
  407. extern int total_devices;
  408. extern struct cgpu_info *devices[];
  409. extern int total_pools;
  410. extern struct pool *pools[MAX_POOLS];
  411. extern const char *algo_names[];
  412. extern enum sha256_algos opt_algo;
  413. extern double total_mhashes_done;
  414. extern unsigned int new_blocks;
  415. extern unsigned int found_blocks;
  416. extern int total_accepted, total_rejected;
  417. extern int total_getworks, total_stale, total_discarded;
  418. extern unsigned int local_work;
  419. extern unsigned int total_go, total_ro;
  420. extern int opt_log_interval;
  421. #ifdef HAVE_OPENCL
  422. typedef struct {
  423. cl_uint ctx_a; cl_uint ctx_b; cl_uint ctx_c; cl_uint ctx_d;
  424. cl_uint ctx_e; cl_uint ctx_f; cl_uint ctx_g; cl_uint ctx_h;
  425. cl_uint cty_a; cl_uint cty_b; cl_uint cty_c; cl_uint cty_d;
  426. cl_uint cty_e; cl_uint cty_f; cl_uint cty_g; cl_uint cty_h;
  427. cl_uint merkle; cl_uint ntime; cl_uint nbits; cl_uint nonce;
  428. cl_uint fW0; cl_uint fW1; cl_uint fW2; cl_uint fW3; cl_uint fW15;
  429. cl_uint fW01r; cl_uint fcty_e; cl_uint fcty_e2;
  430. cl_uint W16; cl_uint W17; cl_uint W2;
  431. cl_uint PreVal4; cl_uint T1;
  432. cl_uint C1addK5; cl_uint D1A; cl_uint W2A; cl_uint W17_2;
  433. cl_uint PreVal4addT1; cl_uint T1substate0;
  434. cl_uint PreVal4_2;
  435. cl_uint PreVal0;
  436. cl_uint PreW18;
  437. cl_uint PreW19;
  438. cl_uint PreW31;
  439. cl_uint PreW32;
  440. } dev_blk_ctx;
  441. #else
  442. typedef struct {
  443. uint32_t nonce;
  444. } dev_blk_ctx;
  445. #endif
  446. struct pool {
  447. int pool_no;
  448. int prio;
  449. int accepted, rejected;
  450. bool submit_fail;
  451. bool idle;
  452. bool lagging;
  453. bool probed;
  454. bool enabled;
  455. char *hdr_path;
  456. unsigned int getwork_requested;
  457. unsigned int stale_shares;
  458. unsigned int discarded_work;
  459. unsigned int getfail_occasions;
  460. unsigned int remotefail_occasions;
  461. struct timeval tv_idle;
  462. char *rpc_url;
  463. char *rpc_userpass;
  464. char *rpc_user, *rpc_pass;
  465. pthread_mutex_t pool_lock;
  466. };
  467. struct work {
  468. unsigned char data[128];
  469. unsigned char hash1[64];
  470. unsigned char midstate[32];
  471. unsigned char target[32];
  472. unsigned char hash[32];
  473. int rolls;
  474. uint32_t output[1];
  475. uint32_t valid;
  476. dev_blk_ctx blk;
  477. struct thr_info *thr;
  478. int thr_id;
  479. struct pool *pool;
  480. struct timeval tv_staged;
  481. bool mined;
  482. bool clone;
  483. bool cloned;
  484. bool rolltime;
  485. unsigned int work_block;
  486. int id;
  487. UT_hash_handle hh;
  488. };
  489. enum cl_kernel {
  490. KL_NONE,
  491. KL_POCLBM,
  492. KL_PHATK,
  493. };
  494. bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
  495. extern void wlogprint(const char *f, ...);
  496. extern int curses_int(const char *query);
  497. extern char *curses_input(const char *query);
  498. extern void kill_work(void);
  499. extern void log_curses(int prio, const char *f, va_list ap);
  500. extern void clear_logwin(void);
  501. extern void vapplog(int prio, const char *fmt, va_list ap);
  502. extern void applog(int prio, const char *fmt, ...);
  503. extern struct thread_q *tq_new(void);
  504. extern void tq_free(struct thread_q *tq);
  505. extern bool tq_push(struct thread_q *tq, void *data);
  506. extern void *tq_pop(struct thread_q *tq, const struct timespec *abstime);
  507. extern void tq_freeze(struct thread_q *tq);
  508. extern void tq_thaw(struct thread_q *tq);
  509. extern bool successful_connect;
  510. extern enum cl_kernel chosen_kernel;
  511. extern void adl(void);
  512. extern bool get_dondata(char **url, char **userpass);
  513. #endif /* __MINER_H__ */