~braiampe/+junk/cgminer

« back to all changes in this revision

Viewing changes to miner.h

  • Committer: Braiam Peguero
  • Date: 2011-10-09 06:47:47 UTC
  • Revision ID: braiamp@gmail.com-20111009064747-iju2nhzrh6ya56zs
* Forgot adding files to the branch.

Show diffs side-by-side

added added

removed removed

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