File: radeonreg.c

package info (click to toggle)
radeontool 1.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 2,064 kB
  • ctags: 7,197
  • sloc: ansic: 15,599; sh: 11,054; makefile: 48
file content (484 lines) | stat: -rw-r--r-- 14,797 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* 
 * avivotool v0.1
 * by Daniel Stone <daniel@fooishbar.org>
 *
 * based on:
 * Radeontool   v1.4
 * by Frederick Dean <software@fdd.com>
 *
 * Copyright 2002-2004 Frederick Dean
 * Use hereby granted under the zlib license.
 *
 * Warning: I do not have the Radeon documents, so this was engineered from 
 * the radeon_reg.h header file.  
 *
 * USE RADEONTOOL AT YOUR OWN RISK
 *
 * Thanks to Deepak Chawla, Erno Kuusela, Rolf Offermanns, and Soos Peter
 * for patches.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <fnmatch.h>
#include <errno.h>
#include <pciaccess.h>

#include "radeon.h"
#include "radeon_reg.h"

int skip;

/* *ctrl_mem is mapped to the actual device's memory mapped control area. */
/* Not the address but what it points to is volatile. */
RADEONCardInfo *card_info = NULL;
unsigned char * volatile ctrl_mem;
unsigned char * volatile fb_mem;


static void die(const char *why)
{
    fprintf(stderr, "fatal error: %s\n", why);
    pci_system_cleanup();
    exit(-1);
}

static void die_error(int err, const char *why)
{
    fprintf(stderr, "fatal error: %s: %s\n", why, strerror(err));
    pci_system_cleanup();
    exit(-1);
}

static void radeon_set(unsigned long offset, const char *name, unsigned int value)
{

    if (ctrl_mem == NULL)
        die("internal error");

#ifdef __powerpc__
    __asm__ __volatile__ ("stwbrx %1,%2,%3\n\t"
                          "eieio"
                          : "=m" (*((volatile unsigned int *)ctrl_mem+offset))
                          : "r"(value), "b"(ctrl_mem), "r"(offset));
#else
    *(unsigned int * volatile)(ctrl_mem + offset) = value;  
#endif
}

static void radeon_set_indexed(unsigned long index_offset,
                               unsigned long data_offset, unsigned long offset,
                               const char *name, unsigned int value)
{
    radeon_set(index_offset, "index", offset);
    radeon_set(data_offset, name, value);
}

static unsigned int radeon_get(unsigned long offset, const char *name)
{
    unsigned int value;

    if (ctrl_mem == NULL)
        die("internal error");

#ifdef __powerpc__
    __asm__ __volatile__ ("lwbrx %0,%1,%2\n\t"
                          "eieio"
                          : "=r" (value)
                          : "b" (ctrl_mem), "r"(offset),
                          "m" (*((volatile unsigned int *)ctrl_mem+offset)));
#else
    value = *(unsigned int * volatile)(ctrl_mem + offset);
#endif

    return value;
}

static unsigned int radeon_get_indexed(unsigned long index_offset,
                                       unsigned long data_offset,
                                       unsigned long offset, const char *name)
{
    radeon_set(index_offset, "index", offset);
    return radeon_get(data_offset, name);
}

static unsigned int radeon_get_clk(unsigned long offset, const char *name)
{
    return radeon_get_indexed(RADEON_CLOCK_CNTL_INDEX,
			      RADEON_CLOCK_CNTL_DATA,
                              offset, name);
}

static void radeon_set_clk(unsigned long offset, const char *name,
                          unsigned int value)
{
    return radeon_set_indexed(RADEON_CLOCK_CNTL_INDEX, RADEON_CLOCK_CNTL_DATA,
                              offset | RADEON_PLL_WR_EN, name, value);
}

static unsigned int radeon_get_mc(unsigned long offset, const char *name)
{
    return radeon_get_indexed(AVIVO_MC_INDEX, AVIVO_MC_DATA,
                              offset | 0x007f0000, name);
}

static void radeon_set_mc(unsigned long offset, const char *name,
                          unsigned int value)
{
    return radeon_set_indexed(AVIVO_MC_INDEX, AVIVO_MC_DATA,
                              offset | 0x00ff0000, name, value);
}

static unsigned int radeon_get_pcie(unsigned long offset, const char *name)
{
    return radeon_get_indexed(0x30, 0x34, offset, name);
}

static void radeon_set_pcie(unsigned long offset, const char *name, unsigned int value)
{
    return radeon_set_indexed(0x30, 0x34, offset, name, value);
}

static void rs690_set_mc(unsigned long offset, const char *name, unsigned int value)
{
    return radeon_set_indexed(RS690_NB_MC_INDEX, RS690_NB_MC_DATA,
			      (offset & RS690_MC_INDEX_MASK) | RS690_MC_INDEX_WR_EN, name, value);
}

static unsigned int rs690_get_mc(unsigned long offset, const char *name)
{
    return radeon_get_indexed(RS690_NB_MC_INDEX, RS690_NB_MC_DATA, offset, name);
}

static void usage(void)
{
    printf("usage: radeonreg [options] [command]\n");
    printf("         --skip=1           - use the second radeon card\n");
    printf("         regs <set>         - show a listing of some random registers\n");
    printf("         regmatch <pattern> - show registers matching wildcard pattern\n");
    printf("         regset <pattern> <value> - set registers matching wildcard pattern\n");
    exit(-1);
}

#define GET_REG(r) radeon_get(r, #r)
#define SET_REG(r, v) radeon_set(r, #r, v)

#define GET_REG(r) radeon_get(r, #r)
#define SHOW_REG(r) printf("%s\t%08x\n", #r, radeon_get(r, #r))
#define SHOW_UNKNOWN_REG(r) { tmp = radeon_get(r, "byhand"); printf("%08lx\t%08x (%d)\n", r, tmp, tmp); }
#define REG_TYPE_NAME(r) ((r == REG_TYPE_STATIC) ? "static" : ((r == REG_TYPE_SEMI_STATIC) ? "semi-static" : "random"))
#define SHOW_STATIC_REG(r) { tmp = get_reg_type(r); printf("%s (%08lx)\t%s\n", get_reg_name(r, ""), r, REG_TYPE_NAME(tmp)); }
#define SHOW_REG_DECIMAL(r) printf("%s\t%d (decimal)\n", #r, radeon_get(r, #r))
#define SHOW_REG_BITS(r, ...) radeon_show_reg_bits(NULL, #r, 0, 0, r, __VA_ARGS__)
#define SHOW_MC_REG(r) printf("%s\t%08x\n", #r, radeon_get_indexed(AVIVO_MC_INDEX, AVIVO_MC_DATA, (r | 0x007f0000), #r))
#define SHOW_MC_REG_BITS(r, ...) radeon_show_reg_bits(NULL, AVIVO_MC_INDEX, AVIVO_MC_DATA, #r, (r | 0x007f0000), __VA_ARGS__)
#define SHOW_PCIE_REG(r) printf("%s\t%08x\n", #r, radeon_get_indexed(0x30, 0x34, (r), #r))

#define SHOW_UNK_MC_REG(r) printf("%02x\t%08x\n", r, radeon_get_indexed(AVIVO_MC_INDEX, AVIVO_MC_DATA, (r | 0x007f0000), #r))

#define SHOW_UNK_CLK_REG(r) printf("%02x\t%08x\n", r, radeon_get_indexed(RADEON_CLOCK_CNTL_INDEX, RADEON_CLOCK_CNTL_DATA, (r), #r))

static void radeon_show_radeon_display_regs(const char *type)
{
    int tmp; /* may be stomped at any moment. */
    unsigned long i;

    for (i = 0x0000; i < 0x00fc; i += 4)
            SHOW_UNKNOWN_REG(i);
    for (i = 0x0200; i < 0x060c; i += 4)
            SHOW_UNKNOWN_REG(i);
    for (i = 0x0800; i < 0x0904; i += 4)
            SHOW_UNKNOWN_REG(i);
    for (i = 0x0d00; i < 0x0d78; i += 4)
            SHOW_UNKNOWN_REG(i);
    for (i = 0; i < 0x2f; i++)
	    SHOW_UNK_CLK_REG(i);
}

static void radeon_show_avivo_display_regs(const char *type)
{
    int tmp; /* may be stomped at any moment. */
    unsigned long i;

    for (i = 0x0000; i < 0x5b4; i += 4)
            SHOW_UNKNOWN_REG(i);
    for (i = 0x5dfc; i < 0x7ff0; i += 4)
            SHOW_UNKNOWN_REG(i);
    for (i = 0; i < 0x3f; i++)
	    SHOW_UNK_CLK_REG(i);
}

static void radeon_show_dce3_display_regs(const char *type)
{
    int tmp; /* may be stomped at any moment. */
    unsigned long i;

    for (i = 0x0000; i < 0x5bc; i += 4)
            SHOW_UNKNOWN_REG(i);
    for (i = 0x5dfc; i < 0x8000; i += 4)
            SHOW_UNKNOWN_REG(i);
}

static void radeon_show_dce4_display_regs(const char *type)
{
    int tmp; /* may be stomped at any moment. */
    unsigned long i;

    for (i = 0x0000; i < 0x5e8; i += 4)
            SHOW_UNKNOWN_REG(i);
    for (i = 0x5c6c; i < 0x7f64; i += 4)
            SHOW_UNKNOWN_REG(i);
    for (i = 0x10000; i < 0x12f64; i += 4)
            SHOW_UNKNOWN_REG(i);
}

static void radeon_show_dce5_display_regs(const char *type)
{
    int tmp; /* may be stomped at any moment. */
    unsigned long i;

    for (i = 0x0000; i < 0x5e8; i += 4)
            SHOW_UNKNOWN_REG(i);
    for (i = 0x5800; i < 0x7fa4; i += 4)
            SHOW_UNKNOWN_REG(i);
    for (i = 0x10000; i < 0x12fa4; i += 4)
            SHOW_UNKNOWN_REG(i);
}

void radeon_cmd_regs(const char *type)
{

    /* Dump all registers that we can read. */
    if (strcmp(type, "radeon") == 0) {
	radeon_show_radeon_display_regs(type);
        return;
    }
    if (strcmp(type, "avivo") == 0) {
	radeon_show_avivo_display_regs(type);
        return;
    }
    if (strcmp(type, "dce3") == 0) {
	radeon_show_dce3_display_regs(type);
        return;
    }
    if (strcmp(type, "dce4") == 0) {
	radeon_show_dce4_display_regs(type);
        return;
    }
    if (strcmp(type, "dce5") == 0) {
	radeon_show_dce5_display_regs(type);
        return;
    }
    if (strcmp(type, "all") == 0) {
	    if (IS_DISPLAY_RADEON(card_info))
		    radeon_show_radeon_display_regs(type);
	    else if (IS_DISPLAY_AVIVO(card_info))
		    radeon_show_avivo_display_regs(type);
	    else if (IS_DISPLAY_DCE3(card_info))
		    radeon_show_dce3_display_regs(type);
	    else if (IS_DISPLAY_DCE4(card_info))
		    radeon_show_dce4_display_regs(type);
	    else if (IS_DISPLAY_DCE5(card_info))
		    radeon_show_dce5_display_regs(type);
	    else
		    printf("unknown chipset, specify the regs to dump\n");
	    return;
    }
}

void radeon_reg_match(const char *pattern)
{
    int i;
    unsigned long address;
    unsigned int value;

    if (pattern[0] == '0' && pattern[1] == 'x') {
        address = strtol(&(pattern[2]), NULL, 16);
        value = radeon_get(address, pattern);
        printf("%s\t0x%08x (%d)\n", pattern, value, value);
    }
    else if (pattern[0] == 'M' && pattern[1] == 'C' && pattern[2] == ':') {
        address = strtol(&(pattern[3]), NULL, 16);
        value = radeon_get_mc(address, pattern);
        printf("%s\t0x%08x (%d)\n", pattern, value, value);
    }
    else if (pattern[0] == 'C' && pattern[1] == 'L' && pattern[2] == ':') {
        address = strtol(&(pattern[3]), NULL, 16);
        value = radeon_get_clk(address, pattern);
        printf("%s\t0x%08x (%d)\n", pattern, value, value);
    }
    else if (pattern[0] == 'P' && pattern[1] == 'C' && pattern[2] == ':') {
        address = strtol(&(pattern[3]), NULL, 16);
        value = radeon_get_pcie(address, pattern);
        printf("%s\t0x%08x (%d)\n", pattern, value, value);
    }
}

void set_reg(const char *name, const char *type, unsigned long address,
             unsigned int value,
             unsigned int (*get)(unsigned long, const char *),
             void (*set)(unsigned long, const char *, unsigned int))
{
    unsigned int readback;

    readback = get(address, name);
    printf("OLD: %s (%s%04lx)\t0x%08x (%d)\n", name, type, address,
           readback, readback);
    set(address, name, value);
    readback = get(address, name);
    printf("NEW: %s (%s%04lx)\t0x%08x (%d)\n", name, type, address,
           readback, readback);
}

void radeon_reg_set(const char *inname, unsigned int value)
{
    int i;
    unsigned long address;

    if (inname[0] == '0' && inname[1] == 'x') {
        address = strtol(&(inname[2]), NULL, 16);
        set_reg(inname, "", address, value, radeon_get, radeon_set);
    }
    else if (inname[0] == 'M' && inname[1] == 'C' && inname[2] == ':') {
        address = strtol(&(inname[3]), NULL, 16);
        set_reg(inname, "MC: ", address, value, radeon_get_mc, radeon_set_mc);
    }
    else if (inname[0] == 'C' && inname[1] == 'L' && inname[2] == ':') {
        address = strtol(&(inname[3]), NULL, 16);
        set_reg(inname, "CL: ", address, value, radeon_get_clk, radeon_set_clk);
    }
    else if (inname[0] == 'P' && inname[1] == 'C' && inname[2] == ':') {
        address = strtol(&(inname[3]), NULL, 16);
        set_reg(inname, "PCIE: ", address, value, radeon_get_pcie, radeon_set_pcie);
    }
}

/* Find and map the buffers. */
static int map_radeon_mem(void)
{
#if 0
    struct pci_id_match match;
#else
    struct pci_slot_match match;
#endif
    struct pci_device_iterator *iter;
    struct pci_device *device;
    pciaddr_t fb_base, fb_size, ctrl_base, ctrl_size;
    int i = 0, ret;

    ret = pci_system_init();
    if (ret)
        die_error(ret, "failed to initialise libpciaccess");

#if 0
    match.vendor_id = 0x1002;
    match.device_id = PCI_MATCH_ANY;
    match.subvendor_id = PCI_MATCH_ANY;
    match.subdevice_id = PCI_MATCH_ANY;
    match.device_class = (0x03 << 16);
    match.device_class_mask = 0x00ff0000;
    match.match_data = 0;
    iter = pci_id_match_iterator_create(&match);
#else
    match.domain = PCI_MATCH_ANY;
    match.bus = PCI_MATCH_ANY;
    match.dev = PCI_MATCH_ANY;

    match.func = 0;
    match.match_data = 0;
    iter = pci_slot_match_iterator_create(&match);
#endif

    while ((device = pci_device_next(iter))) {
        pci_device_probe(device);
        if (device->vendor_id != 0x1002)
            continue;
        if ((device->device_class & 0x00ffff00) != 0x00030000 &&
            (device->device_class & 0x00ffff00) != 0x00038000)
            continue;
        if (skip--)
            continue;
        break;
    }

    if (!device) {
        printf("cannot find Radeon device\n");
        return -1;
    }

    for (i = 0; i < sizeof(RADEONCards) / sizeof(RADEONCardInfo); i++) {
        if (RADEONCards[i].pci_device_id == device->device_id)
            card_info = &RADEONCards[i];
    }

    ctrl_base = device->regions[2].base_addr;
    ctrl_size = device->regions[2].size;
    if (!ctrl_size)
        die("missing ctrl region");
    ret = pci_device_map_range(device, ctrl_base, ctrl_size,
			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &ctrl_mem);
    if (ret)
        die_error(ret, "cannot map ctrl region");

    fb_base = device->regions[0].base_addr;
    fb_size = device->regions[0].size;
    if (!fb_size || pci_device_map_range(device, fb_base, fb_size,
			     PCI_DEV_MAP_FLAG_WRITABLE, (void **) &fb_mem))
        fb_mem = NULL;

    pci_iterator_destroy(iter);

    return 0;
}

int main(int argc, char *argv[])
{
    int ret;
    if (argc == 1)
        usage();

    skip = 0;
    if (argc > 1 && strncmp(argv[1], "--skip=", 7) == 0) {
        skip = atoi(argv[1] + 7);
        argv++;
        argc--;
    }

    ret = map_radeon_mem();
    if (ret!=0)
	    die("Unable to see card");

    if (argc == 2) {
	    if (strcmp(argv[1], "regs") == 0) {
		    radeon_cmd_regs("default");
		    return 0;
	    }
    } else if (argc == 3) {
	    if (strcmp(argv[1], "regmatch") == 0) {
		    radeon_reg_match(argv[2]);
		    return 0;
	    } else if (strcmp(argv[1], "regs") == 0) {
		    radeon_cmd_regs(argv[2]);
		    return 0;
	    }

    } else if (argc == 4) {
        if (strcmp(argv[1], "regset") == 0) {
            radeon_reg_set(argv[2], strtoul(argv[3], NULL, 0));
            return 0;
        }
    }

    usage();

    pci_system_cleanup();

    return 1;
}