diff options
author | Jerome Glisse <jglisse@redhat.com> | 2011-09-30 10:24:48 -0400 |
---|---|---|
committer | Jerome Glisse <jglisse@redhat.com> | 2011-09-30 10:24:48 -0400 |
commit | 80799c0fa8e82822b223f0cd82cc554752d75add (patch) | |
tree | d386399a30ca5e205df1ede13e447f46ceff7e2c | |
parent | 49b32533f8008816b63c18226ac433b0bb26e1d1 (diff) |
bunch of new tools and update to previous one
-rw-r--r-- | Makefile | 22 | ||||
-rw-r--r-- | bof.c | 507 | ||||
-rw-r--r-- | bof.h | 93 | ||||
-rw-r--r-- | bofjson.c | 302 | ||||
-rw-r--r-- | jsoncs.c | 400 | ||||
-rw-r--r-- | radeon_pci.c | 15 | ||||
-rw-r--r-- | rdump.c | 35 | ||||
-rw-r--r-- | reg.c | 7 | ||||
-rw-r--r-- | testgttvram.c | 56 |
9 files changed, 1428 insertions, 9 deletions
@@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -I . -O2 -Wall -I/usr/include/drm/ +CFLAGS = -I . -O2 -Wall -I/usr/include/libdrm/ JOUJOU_SOURCES = joujou.c radeon.c radeon_pciid.c radeon_bo.c JOUJOU_OBJECTS = $(JOUJOU_SOURCES:.c=.o) @@ -13,7 +13,16 @@ CDUMP_OBJECTS = $(CDUMP_SOURCES:.c=.o) WDUMP_SOURCES = wdump.c radeon_pci.c reg.c WDUMP_OBJECTS = $(WDUMP_SOURCES:.c=.o) -TARGETS = joujou rdump cdump wdump +BOFJSON_SOURCES = bofjson.c bof.c +BOFJSON_OBJECTS = $(BOFJSON_SOURCES:.c=.o) + +JSONCS_SOURCES = jsoncs.c +JSONCS_OBJECTS = $(JSONCS_SOURCES:.c=.o) + +TESTGTTVRAM_SOURCES = testgttvram.c +TESTGTTVRAM_OBJECTS = $(TESTGTTVRAM_SOURCES:.c=.o) + +TARGETS = joujou rdump cdump wdump bofjson jsoncs testgttvram ##### RULES ##### .SUFFIXES: @@ -38,6 +47,15 @@ cdump: $(CDUMP_OBJECTS) wdump: $(WDUMP_OBJECTS) $(CC) -o $@ $(WDUMP_OBJECTS) -lpciaccess +bofjson: $(BOFJSON_OBJECTS) + $(CC) -o $@ $(BOFJSON_OBJECTS) -ljansson + +jsoncs: $(JSONCS_OBJECTS) + $(CC) -o $@ $(JSONCS_OBJECTS) -ljansson -ldrm + +testgttvram: $(TESTGTTVRAM_OBJECTS) + $(CC) -o $@ $(TESTGTTVRAM_OBJECTS) -ldrm + clean: rm -f $(TARGETS) rm -f *.o @@ -0,0 +1,507 @@ +/* + * Copyright 2010 Jerome Glisse <glisse@freedesktop.org> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: + * Jerome Glisse + */ +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include "bof.h" + +/* + * helpers + */ +static int bof_entry_grow(bof_t *bof) +{ + bof_t **array; + + if (bof->array_size < bof->nentry) + return 0; + array = realloc(bof->array, (bof->nentry + 16) * sizeof(void*)); + if (array == NULL) + return -ENOMEM; + bof->array = array; + bof->nentry += 16; + return 0; +} + +/* + * object + */ +bof_t *bof_object(void) +{ + bof_t *object; + + object = calloc(1, sizeof(bof_t)); + if (object == NULL) + return NULL; + object->refcount = 1; + object->type = BOF_TYPE_OBJECT; + object->size = 12; + return object; +} + +bof_t *bof_object_get(bof_t *object, const char *keyname) +{ + unsigned i; + + for (i = 0; i < object->array_size; i += 2) { + if (!strcmp(object->array[i]->value, keyname)) { + return object->array[i + 1]; + } + } + return NULL; +} + +int bof_object_set(bof_t *object, const char *keyname, bof_t *value) +{ + bof_t *key; + int r; + + if (object->type != BOF_TYPE_OBJECT) + return -EINVAL; + r = bof_entry_grow(object); + if (r) + return r; + key = bof_string(keyname); + if (key == NULL) + return -ENOMEM; + object->array[object->array_size++] = key; + object->array[object->array_size++] = value; + object->size += value->size; + object->size += key->size; + bof_incref(value); + return 0; +} + +/* + * array + */ +bof_t *bof_array(void) +{ + bof_t *array = bof_object(); + + if (array == NULL) + return NULL; + array->type = BOF_TYPE_ARRAY; + array->size = 12; + return array; +} + +int bof_array_append(bof_t *array, bof_t *value) +{ + int r; + if (array->type != BOF_TYPE_ARRAY) + return -EINVAL; + r = bof_entry_grow(array); + if (r) + return r; + array->array[array->array_size++] = value; + array->size += value->size; + bof_incref(value); + return 0; +} + +bof_t *bof_array_get(bof_t *bof, unsigned i) +{ + if (!bof_is_array(bof) || i >= bof->array_size) + return NULL; + return bof->array[i]; +} + +unsigned bof_array_size(bof_t *bof) +{ + if (!bof_is_array(bof)) + return 0; + return bof->array_size; +} + +/* + * blob + */ +bof_t *bof_blob(unsigned size, void *value) +{ + bof_t *blob = bof_object(); + + if (blob == NULL) + return NULL; + blob->type = BOF_TYPE_BLOB; + blob->value = calloc(1, size); + if (blob->value == NULL) { + bof_decref(blob); + return NULL; + } + blob->size = size; + if (value) { + memcpy(blob->value, value, size); + } + blob->size += 12; + return blob; +} + +unsigned bof_blob_size(bof_t *bof) +{ + if (!bof_is_blob(bof)) + return 0; + return bof->size - 12; +} + +void *bof_blob_value(bof_t *bof) +{ + if (!bof_is_blob(bof)) + return NULL; + return bof->value; +} + +/* + * string + */ +bof_t *bof_string(const char *value) +{ + bof_t *string = bof_object(); + + if (string == NULL) + return NULL; + string->type = BOF_TYPE_STRING; + string->size = strlen(value) + 1; + string->value = calloc(1, string->size); + if (string->value == NULL) { + bof_decref(string); + return NULL; + } + strcpy(string->value, value); + string->size += 12; + return string; +} + +/* + * int32 + */ +bof_t *bof_int32(int32_t value) +{ + bof_t *int32 = bof_object(); + + if (int32 == NULL) + return NULL; + int32->type = BOF_TYPE_INT32; + int32->size = 4; + int32->value = calloc(1, int32->size); + if (int32->value == NULL) { + bof_decref(int32); + return NULL; + } + memcpy(int32->value, &value, 4); + int32->size += 12; + return int32; +} + +int64_t bof_int64_value(bof_t *bof) +{ + return *((int64_t*)bof->value); +} + +bof_t *bof_int64(int64_t value) +{ + bof_t *int64 = bof_object(); + + if (int64 == NULL) + return NULL; + int64->type = BOF_TYPE_INT64; + int64->size = 8; + int64->value = calloc(1, int64->size); + if (int64->value == NULL) { + bof_decref(int64); + return NULL; + } + memcpy(int64->value, &value, 4); + int64->size += 12; + return int64; +} + +int32_t bof_int32_value(bof_t *bof) +{ + return *((uint32_t*)bof->value); +} + +/* + * common + */ +static void bof_indent(int level) +{ + int i; + + for (i = 0; i < level; i++) + fprintf(stderr, " "); +} + +static void bof_print_bof(bof_t *bof, int level, int entry) +{ + bof_indent(level); + if (bof == NULL) { + fprintf(stderr, "--NULL-- for entry %d\n", entry); + return; + } + switch (bof->type) { + case BOF_TYPE_STRING: + fprintf(stderr, "%p string [%s %d]\n", bof, (char*)bof->value, bof->size); + break; + case BOF_TYPE_INT32: + fprintf(stderr, "%p int32 [%d %d]\n", bof, *(int*)bof->value, bof->size); + break; + case BOF_TYPE_INT64: + fprintf(stderr, "%p int64 [%d %d]\n", bof, *(int*)bof->value, bof->size); + break; + case BOF_TYPE_BLOB: + fprintf(stderr, "%p blob [%d]\n", bof, bof->size); + break; + case BOF_TYPE_NULL: + fprintf(stderr, "%p null [%d]\n", bof, bof->size); + break; + case BOF_TYPE_OBJECT: + fprintf(stderr, "%p object [%d %d]\n", bof, bof->array_size / 2, bof->size); + break; + case BOF_TYPE_ARRAY: + fprintf(stderr, "%p array [%d %d]\n", bof, bof->array_size, bof->size); + break; + default: + fprintf(stderr, "%p unknown [%d]\n", bof, bof->type); + return; + } +} + +static void bof_print_rec(bof_t *bof, int level, int entry) +{ + unsigned i; + + bof_print_bof(bof, level, entry); + for (i = 0; i < bof->array_size; i++) { + bof_print_rec(bof->array[i], level + 2, i); + } +} + +void bof_print(bof_t *bof) +{ + bof_print_rec(bof, 0, 0); +} + +static int bof_read(bof_t *root, FILE *file, long end, int level) +{ + bof_t *bof = NULL; + int r; + + if (ftell(file) >= end) { + return 0; + } + r = bof_entry_grow(root); + if (r) + return r; + bof = bof_object(); + if (bof == NULL) + return -ENOMEM; + bof->offset = ftell(file); + r = fread(&bof->type, 4, 1, file); + if (r != 1) + goto out_err; + r = fread(&bof->size, 4, 1, file); + if (r != 1) + goto out_err; + r = fread(&bof->array_size, 4, 1, file); + if (r != 1) + goto out_err; + switch (bof->type) { + case BOF_TYPE_STRING: + case BOF_TYPE_INT32: + case BOF_TYPE_INT64: + case BOF_TYPE_BLOB: + bof->value = calloc(1, bof->size - 12); + if (bof->value == NULL) { + goto out_err; + } + r = fread(bof->value, bof->size - 12, 1, file); + if (r != 1) { + fprintf(stderr, "error reading %d\n", bof->size - 12); + goto out_err; + } + break; + case BOF_TYPE_NULL: + return 0; + case BOF_TYPE_OBJECT: + case BOF_TYPE_ARRAY: + r = bof_read(bof, file, bof->offset + bof->size, level + 2); + if (r) + goto out_err; + break; + default: + fprintf(stderr, "invalid type %d\n", bof->type); + goto out_err; + } + root->array[root->centry++] = bof; + return bof_read(root, file, end, level); +out_err: + bof_decref(bof); + return -EINVAL; +} + +bof_t *bof_load_file(const char *filename) +{ + bof_t *root = bof_object(); + int r; + + if (root == NULL) { + fprintf(stderr, "%s failed to create root object\n", __func__); + return NULL; + } + root->file = fopen(filename, "r"); + if (root->file == NULL) + goto out_err; + r = fseek(root->file, 0L, SEEK_SET); + if (r) { + fprintf(stderr, "%s failed to seek into file %s\n", __func__, filename); + goto out_err; + } + root->offset = ftell(root->file); + r = fread(&root->type, 4, 1, root->file); + if (r != 1) + goto out_err; + r = fread(&root->size, 4, 1, root->file); + if (r != 1) + goto out_err; + r = fread(&root->array_size, 4, 1, root->file); + if (r != 1) + goto out_err; + r = bof_read(root, root->file, root->offset + root->size, 2); + if (r) + goto out_err; + return root; +out_err: + bof_decref(root); + return NULL; +} + +void bof_incref(bof_t *bof) +{ + bof->refcount++; +} + +void bof_decref(bof_t *bof) +{ + unsigned i; + + if (bof == NULL) + return; + if (--bof->refcount > 0) + return; + for (i = 0; i < bof->array_size; i++) { + bof_decref(bof->array[i]); + bof->array[i] = NULL; + } + bof->array_size = 0; + if (bof->file) { + fclose(bof->file); + bof->file = NULL; + } + free(bof->array); + free(bof->value); + free(bof); +} + +static int bof_file_write(bof_t *bof, FILE *file) +{ + unsigned i; + int r; + + r = fwrite(&bof->type, 4, 1, file); + if (r != 1) + return -EINVAL; + r = fwrite(&bof->size, 4, 1, file); + if (r != 1) + return -EINVAL; + r = fwrite(&bof->array_size, 4, 1, file); + if (r != 1) + return -EINVAL; + switch (bof->type) { + case BOF_TYPE_NULL: + if (bof->size) + return -EINVAL; + break; + case BOF_TYPE_STRING: + case BOF_TYPE_INT32: + case BOF_TYPE_INT64: + case BOF_TYPE_BLOB: + r = fwrite(bof->value, bof->size - 12, 1, file); + if (r != 1) + return -EINVAL; + break; + case BOF_TYPE_OBJECT: + case BOF_TYPE_ARRAY: + for (i = 0; i < bof->array_size; i++) { + r = bof_file_write(bof->array[i], file); + if (r) + return r; + } + break; + default: + return -EINVAL; + } + return 0; +} + +int bof_dump_file(bof_t *bof, const char *filename) +{ + unsigned i; + int r = 0; + + if (bof->file) { + fclose(bof->file); + bof->file = NULL; + } + bof->file = fopen(filename, "w"); + if (bof->file == NULL) { + fprintf(stderr, "%s failed to open file %s\n", __func__, filename); + r = -EINVAL; + goto out_err; + } + r = fseek(bof->file, 0L, SEEK_SET); + if (r) { + fprintf(stderr, "%s failed to seek into file %s\n", __func__, filename); + goto out_err; + } + r = fwrite(&bof->type, 4, 1, bof->file); + if (r != 1) + goto out_err; + r = fwrite(&bof->size, 4, 1, bof->file); + if (r != 1) + goto out_err; + r = fwrite(&bof->array_size, 4, 1, bof->file); + if (r != 1) + goto out_err; + for (i = 0; i < bof->array_size; i++) { + r = bof_file_write(bof->array[i], bof->file); + if (r) + return r; + } +out_err: + fclose(bof->file); + bof->file = NULL; + return r; +} @@ -0,0 +1,93 @@ +/* + * Copyright 2010 Jerome Glisse <glisse@freedesktop.org> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: + * Jerome Glisse + */ +#ifndef BOF_H +#define BOF_H + +#include <stdio.h> +#include <stdint.h> + +#define BOF_TYPE_STRING 0 +#define BOF_TYPE_NULL 1 +#define BOF_TYPE_BLOB 2 +#define BOF_TYPE_OBJECT 3 +#define BOF_TYPE_ARRAY 4 +#define BOF_TYPE_INT32 5 +#define BOF_TYPE_INT64 6 + +struct bof; + +typedef struct bof { + struct bof **array; + unsigned centry; + unsigned nentry; + unsigned refcount; + FILE *file; + uint32_t type; + uint32_t size; + uint32_t array_size; + void *value; + long offset; +} bof_t; + +extern int bof_file_flush(bof_t *root); +extern bof_t *bof_file_new(const char *filename); +extern int bof_object_dump(bof_t *object, const char *filename); + +/* object */ +extern bof_t *bof_object(void); +extern bof_t *bof_object_get(bof_t *object, const char *keyname); +extern int bof_object_set(bof_t *object, const char *keyname, bof_t *value); +/* array */ +extern bof_t *bof_array(void); +extern int bof_array_append(bof_t *array, bof_t *value); +extern bof_t *bof_array_get(bof_t *bof, unsigned i); +extern unsigned bof_array_size(bof_t *bof); +/* blob */ +extern bof_t *bof_blob(unsigned size, void *value); +extern unsigned bof_blob_size(bof_t *bof); +extern void *bof_blob_value(bof_t *bof); +/* string */ +extern bof_t *bof_string(const char *value); +/* int32 */ +extern bof_t *bof_int32(int32_t value); +extern int32_t bof_int32_value(bof_t *bof); +extern bof_t *bof_int64(int64_t value); +extern int64_t bof_int64_value(bof_t *bof); +/* common functions */ +extern void bof_decref(bof_t *bof); +extern void bof_incref(bof_t *bof); +extern bof_t *bof_load_file(const char *filename); +extern int bof_dump_file(bof_t *bof, const char *filename); +extern void bof_print(bof_t *bof); + +static inline int bof_is_object(bof_t *bof){return (bof->type == BOF_TYPE_OBJECT);} +static inline int bof_is_blob(bof_t *bof){return (bof->type == BOF_TYPE_BLOB);} +static inline int bof_is_null(bof_t *bof){return (bof->type == BOF_TYPE_NULL);} +static inline int bof_is_int32(bof_t *bof){return (bof->type == BOF_TYPE_INT32);} +static inline int bof_is_array(bof_t *bof){return (bof->type == BOF_TYPE_ARRAY);} +static inline int bof_is_string(bof_t *bof){return (bof->type == BOF_TYPE_STRING);} + +#endif diff --git a/bofjson.c b/bofjson.c new file mode 100644 index 0000000..180b752 --- /dev/null +++ b/bofjson.c @@ -0,0 +1,302 @@ +/* + * Copyright 2011 Jerome Glisse <glisse@freedesktop.org> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: + * Jerome Glisse + */ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <jansson.h> +#include "bof.h" + +struct bo { + unsigned size; + uint64_t offset; + uint32_t handle; + void *data; +}; + +struct cs { + unsigned pm4_ndw; + unsigned reloc_ndw; + unsigned nbo; + struct bo *bo; + uint32_t device_id; + uint32_t *pm4; + uint32_t *reloc; +}; + +void *copy_object(void *ptr, unsigned size) +{ + void *copy; + + copy = calloc(1, size); + if (copy == NULL) + return NULL; + memcpy(copy, ptr, size); + return copy; +} + +int cs_bof_load(struct cs *cs, const char *filename) +{ + bof_t *bof; + bof_t *tmp, *array, *bo; + int r, i; + + memset(cs, 0, sizeof(struct cs)); + bof = bof_load_file(filename); + if (bof == NULL) { + fprintf(stderr, "%s:%d failed to load %s\n", __func__, __LINE__, filename); + return -EINVAL; + } + /* device id */ + tmp = bof_object_get(bof, "device_id"); + if (tmp == NULL) { + fprintf(stderr, "no device object\n"); + goto out_err; + } + cs->device_id = bof_int32_value(tmp); + /* pm4 */ + tmp = bof_object_get(bof, "pm4"); + if (tmp == NULL) { + fprintf(stderr, "no pm4 object\n"); + goto out_err; + } + cs->pm4_ndw = bof_blob_size(tmp) / 4; + cs->pm4 = copy_object(bof_blob_value(tmp), bof_blob_size(tmp)); + if (cs->pm4 == NULL) { + goto out_err; + } + /* reloc */ + tmp = bof_object_get(bof, "reloc"); + if (tmp == NULL) { + fprintf(stderr, "no reloc object\n"); + r = -EINVAL; + goto out_err; + } + cs->reloc_ndw = bof_blob_size(tmp) / 4; + cs->reloc = copy_object(bof_blob_value(tmp), bof_blob_size(tmp)); + if (cs->reloc == NULL) { + fprintf(stderr, "%s failed to allocate reloc\n", __func__); + goto out_err; + } + /* bo */ + array = bof_object_get(bof, "bo"); + if (array == NULL) { + fprintf(stderr, "no bo array object\n"); + r = -EINVAL; + goto out_err; + } + cs->nbo = bof_array_size(array); + if ((cs->reloc_ndw / 4) > cs->nbo) { + cs->reloc_ndw = cs->nbo * 4; + } + cs->bo = calloc(1, sizeof(struct bo) * cs->nbo); + if (cs->bo == NULL) { + fprintf(stderr, "%s failed to allocate bo\n", __func__); + goto out_err; + } + for (i = 0; i < bof_array_size(array); i++) { + bo = bof_array_get(array, i); + if (bo == NULL) { + fprintf(stderr, "can't get %d bo\n", i); + goto out_err; + } + tmp = bof_object_get(bo, "size"); + if (tmp == NULL) { + fprintf(stderr, "can't get %d bo size\n", i); + goto out_err; + } + cs->bo[i].size = bof_int32_value(tmp); + tmp = bof_object_get(bo, "offset"); + if (tmp == NULL) { + fprintf(stderr, "can't get %d bo offset\n", i); + goto out_err; + } + cs->bo[i].offset = bof_int64_value(tmp); + tmp = bof_object_get(bo, "handle"); + if (tmp == NULL) { + fprintf(stderr, "can't get %d bo handle\n", i); + goto out_err; + } + cs->bo[i].handle = bof_int32_value(tmp); + tmp = bof_object_get(bo, "data"); + if (tmp == NULL) { + fprintf(stderr, "can't get %d bo data from %p of %p\n", i, bo, array); + goto out_err; + } + cs->bo[i].data = copy_object(bof_blob_value(tmp), bof_blob_size(tmp)); + if (cs->bo[i].data == NULL) { + fprintf(stderr, "%s failed to allocate bo (%d)\n", __func__, cs->bo[i].size); + goto out_err; + } + } + bof_decref(bof); + return 0; +out_err: + bof_decref(bof); + for (i = 0; i < cs->nbo; i++) { + free(cs->bo[i].data); + } + free(cs->pm4); + free(cs->reloc); + free(cs->bo); + memset(cs, 0, sizeof(struct cs)); + return -EINVAL; +} + +json_t *copy_to_array(unsigned size, void *data) +{ + uint32_t *ptr = data; + unsigned i; + json_t *array, *value; + char tmp[16]; + + array = json_array(); + if (array == NULL) { + return NULL; + } + for (i = 0; i < size / 4; i++) { + sprintf(tmp, "0x%08X", ptr[i]); + value = json_string(tmp); + if (value == NULL) { + goto out_err; + } + if (json_array_append_new(array, value)) { + goto out_err; + } + } + return array; +out_err: + json_decref(array); + return NULL; +} + +int cs_json_dump(struct cs *cs, const char *filename) +{ + json_t *json, *tmp, *bo_array, *bo; + unsigned i; + int r = 0; + + json = json_object(); + if (json == NULL) { + return -ENOMEM; + } + /* device id */ + tmp = json_integer(cs->device_id); + if (tmp == NULL) { + fprintf(stderr, "%s failed to create json device_id object\n", __func__); + r = -EINVAL; + goto out_err; + } + json_object_set_new(json, "device_id", tmp); + /* pm4 */ + tmp = copy_to_array(cs->pm4_ndw * 4, cs->pm4); + if (tmp == NULL) { + fprintf(stderr, "%s failed to create json pm4 object\n", __func__); + r = -EINVAL; + goto out_err; + } + json_object_set_new(json, "pm4", tmp); + /* reloc */ + tmp = copy_to_array(cs->reloc_ndw * 4, cs->reloc); + if (tmp == NULL) { + fprintf(stderr, "%s failed to create json reloc object\n", __func__); + r = -EINVAL; + goto out_err; + } + json_object_set_new(json, "reloc", tmp); + /* draw array */ + bo_array = json_array(); + if (bo_array == NULL) { + fprintf(stderr, "%s failed to create json bo array\n", __func__); + r = -EINVAL; + goto out_err; + } + json_object_set_new(json, "bo", bo_array); + for (i = 0; i < cs->nbo; i++) { + bo = json_object(); + if (bo == NULL) { + fprintf(stderr, "%s failed to create json bo[%d]\n", __func__, i); + r = -EINVAL; + goto out_err; + } + if (json_array_append_new(bo_array, bo)) { + json_decref(bo); + r = -EINVAL; + goto out_err; + } + tmp = json_integer(cs->bo[i].size); + if (tmp == NULL) { + fprintf(stderr, "%s failed to create bo[%d] size object\n", __func__, i); + r = -EINVAL; + goto out_err; + } + json_object_set_new(bo, "size", tmp); + tmp = json_integer(cs->bo[i].offset); + if (tmp == NULL) { + fprintf(stderr, "%s failed to create bo[%d] offset object\n", __func__, i); + r = -EINVAL; + goto out_err; + } + json_object_set_new(bo, "offset", tmp); + tmp = json_integer(cs->bo[i].handle); + if (tmp == NULL) { + fprintf(stderr, "%s failed to create bo[%d] size object\n", __func__, i); + r = -EINVAL; + goto out_err; + } + json_object_set_new(bo, "handle", tmp); + tmp = copy_to_array(cs->bo[i].size, cs->bo[i].data); + if (tmp == NULL) { + fprintf(stderr, "%s failed to create bo[%d] data object\n", __func__, i); + r = -EINVAL; + goto out_err; + } + json_object_set_new(bo, "data", tmp); + } + r = json_dump_file(json, filename, JSON_INDENT(2) | JSON_SORT_KEYS); +out_err: + json_decref(json); + return r; +} + +int main(int argc, char *argv[]) +{ + struct cs cs; + + if (argc != 3) { + fprintf(stderr, "usage : %s bofile jsonfile\n", argv[0]); + return -EINVAL; + } + + if (cs_bof_load(&cs, argv[1])) { + return -EINVAL; + } + + if (cs_json_dump(&cs, argv[2])) { + return -EINVAL; + } + return 0; +} diff --git a/jsoncs.c b/jsoncs.c new file mode 100644 index 0000000..08dd424 --- /dev/null +++ b/jsoncs.c @@ -0,0 +1,400 @@ +/* + * Copyright 2011 Jerome Glisse <glisse@freedesktop.org> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: + * Jerome Glisse + */ +#define _FILE_OFFSET_BITS 64 +#include <sys/mman.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <errno.h> +#include <jansson.h> +#include "xf86drm.h" +#include "radeon_drm.h" + +#pragma pack(1) +struct radeon_cs_reloc { + uint32_t handle; + uint32_t read_domain; + uint32_t write_domain; + uint32_t flags; +}; +#pragma pack() + +struct rbo { + unsigned refcount; + uint32_t handle; + unsigned size; + unsigned alignment; + void *data; +}; + +struct bo { + unsigned size; + uint32_t handle; + void *data; + struct rbo *rbo; +}; + +struct cs { + unsigned pm4_ndw; + unsigned reloc_ndw; + unsigned nbo; + struct bo *bo; + uint32_t device_id; + uint32_t *pm4; + uint32_t *reloc; +}; + +struct rbo *rbo(int fd, unsigned handle, unsigned size, unsigned alignment, void *ptr); +int rbo_map(int fd, struct rbo *bo); +void rbo_unmap(int fd, struct rbo *bo); +struct rbo *rbo_decref(int fd, struct rbo *bo); + +int copy_from_array(json_t *array, void *data) +{ + uint32_t *ptr = data; + unsigned i; + json_t *value; + + for (i = 0; i < json_array_size(array); i++) { + value = json_array_get(array, i); + if (value == NULL) { + return -EINVAL; + } + ptr[i] = strtoul(json_string_value(value), NULL, 0); + } + return 0; +} + +int cs_json_load(struct cs *cs, const char *filename) +{ + json_error_t error; + json_t *json, *tmp, *bo_array, *bo; + int i; + + memset(cs, 0, sizeof(struct cs)); + json = json_load_file(filename, &error); + if (json == NULL) { + return -EINVAL; + } + /* device */ + tmp = json_object_get(json, "device_id"); + if (tmp == NULL) { + fprintf(stderr, "no device id object\n"); + goto out_err; + } + cs->device_id = json_integer_value(tmp); + /* pm4 */ + tmp = json_object_get(json, "pm4"); + if (tmp == NULL) { + fprintf(stderr, "no pm4 object\n"); + goto out_err; + } + cs->pm4_ndw = json_array_size(tmp); + cs->pm4 = calloc(1, cs->pm4_ndw * 4); + if (copy_from_array(tmp, cs->pm4)) { + fprintf(stderr, "failed reading pm4 object\n"); + goto out_err; + } + /* reloc */ + tmp = json_object_get(json, "reloc"); + if (tmp == NULL) { + fprintf(stderr, "no reloc object\n"); + goto out_err; + } + cs->reloc_ndw = json_array_size(tmp); + cs->reloc = calloc(1, cs->reloc_ndw * 4); + if (copy_from_array(tmp, cs->reloc)) { + fprintf(stderr, "failed reading pm4 object\n"); + goto out_err; + } + /* bo */ + bo_array = json_object_get(json, "bo"); + if (bo_array == NULL) { + fprintf(stderr, "no bo object\n"); + goto out_err; + } + cs->nbo = json_array_size(bo_array); + cs->bo = calloc(1, sizeof(struct bo) * cs->nbo); + if (cs->bo == NULL) { + fprintf(stderr, "%s failed to allocate bo\n", __func__); + goto out_err; + } + for (i = 0; i < cs->nbo; i++) { + bo = json_array_get(bo_array, i); + if (bo == NULL) { + fprintf(stderr, "no bo[%d] object\n", i); + goto out_err; + } + tmp = json_object_get(bo, "size"); + if (tmp == NULL) { + fprintf(stderr, "no size object for bo[%d]\n", i); + goto out_err; + } + cs->bo[i].size = json_integer_value(tmp); + tmp = json_object_get(bo, "handle"); + if (tmp == NULL) { + fprintf(stderr, "no handle object for bo[%d]\n", i); + goto out_err; + } + cs->bo[i].handle = json_integer_value(tmp); + cs->bo[i].data = calloc(1, cs->bo[i].size); + if (cs->bo[i].data == NULL) { + fprintf(stderr, "failed to allocate bo[%d]\n", i); + goto out_err; + } + tmp = json_object_get(bo, "data"); + if (tmp == NULL) { + fprintf(stderr, "no data object for bo[%d]\n", i); + goto out_err; + } + if (copy_from_array(tmp, cs->bo[i].data)) { + fprintf(stderr, "failed reading pm4 object\n"); + goto out_err; + } + } + json_decref(json); + return 0; +out_err: + json_decref(json); + for (i = 0; i < cs->nbo; i++) { + free(cs->bo[i].data); + } + free(cs->pm4); + free(cs->reloc); + free(cs->bo); + memset(cs, 0, sizeof(struct cs)); + return -EINVAL; +} + +struct rbo *rbo(int fd, unsigned handle, unsigned size, unsigned alignment, void *ptr) +{ + struct rbo *bo; + int r; + + bo = calloc(1, sizeof(*bo)); + if (bo == NULL) { + return NULL; + } + bo->refcount = 1; + bo->size = size; + bo->handle = handle; + bo->alignment = alignment; + + if (handle) { + struct drm_gem_open open_arg; + + memset(&open_arg, 0, sizeof(open_arg)); + open_arg.name = handle; + r = drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &open_arg); + if (r != 0) { + free(bo); + return NULL; + } + bo->handle = open_arg.handle; + } else { + struct drm_radeon_gem_create args; + + args.size = size; + args.alignment = alignment; + args.initial_domain = RADEON_GEM_DOMAIN_CPU; + args.flags = 0; + args.handle = 0; + r = drmCommandWriteRead(fd, DRM_RADEON_GEM_CREATE, &args, sizeof(args)); + bo->handle = args.handle; + if (r) { + fprintf(stderr, "Failed to allocate :\n"); + fprintf(stderr, " size : %d bytes\n", size); + fprintf(stderr, " alignment : %d bytes\n", alignment); + free(bo); + return NULL; + } + } + if (ptr) { + if (rbo_map(fd, bo)) { + fprintf(stderr, "%s failed to copy data into bo\n", __func__); + return rbo_decref(fd, bo); + } + memcpy(bo->data, ptr, size); + rbo_unmap(fd, bo); + } + return bo; +} + +int rbo_map(int fd, struct rbo *bo) +{ + struct drm_radeon_gem_mmap args; + void *ptr; + int r; + + /* Zero out args to make valgrind happy */ + memset(&args, 0, sizeof(args)); + args.handle = bo->handle; + args.offset = 0; + args.size = (uint64_t)bo->size; + r = drmCommandWriteRead(fd, DRM_RADEON_GEM_MMAP, &args, sizeof(args)); + if (r) { + fprintf(stderr, "error mapping %p 0x%08X (error = %d)\n", + bo, bo->handle, r); + return r; + } + ptr = mmap(0, args.size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, args.addr_ptr); + if (ptr == MAP_FAILED) { + fprintf(stderr, "%s failed to map bo\n", __func__); + return -errno; + } + bo->data = ptr; + return 0; +} + +void rbo_unmap(int fd, struct rbo *bo) +{ + munmap(bo->data, bo->size); + bo->data = NULL; +} + +struct rbo *rbo_decref(int fd, struct rbo *bo) +{ + struct drm_gem_close args; + + if (bo == NULL) + return NULL; + if (--bo->refcount > 0) { + return NULL; + } + + if (bo->data) { + munmap(bo->data, bo->size); + } + memset(&args, 0, sizeof(args)); + args.handle = bo->handle; + drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &args); + memset(bo, 0, sizeof(struct rbo)); + free(bo); + return NULL; +} + +int radeon_open_fd(void) +{ + return drmOpen("radeon", NULL); +} + +int cs_submit(int fd, struct cs *cs) +{ + struct drm_radeon_cs drmib; + struct drm_radeon_cs_chunk chunks[2]; + uint64_t chunk_array[2]; + int r = 0; + + drmib.num_chunks = 2; + drmib.chunks = (uint64_t)(uintptr_t)chunk_array; + chunks[0].chunk_id = RADEON_CHUNK_ID_IB; + chunks[0].length_dw = cs->pm4_ndw; + chunks[0].chunk_data = (uintptr_t)cs->pm4; + chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS; + chunks[1].length_dw = cs->reloc_ndw; + chunks[1].chunk_data = (uintptr_t)cs->reloc; + chunk_array[0] = (uintptr_t)&chunks[0]; + chunk_array[1] = (uintptr_t)&chunks[1]; +#if 1 + r = drmCommandWriteRead(fd, DRM_RADEON_CS, &drmib, sizeof(struct drm_radeon_cs)); +#endif + return r; +} + +int main(int argc, char *argv[]) +{ + unsigned i, j; + struct cs cs; + int radeonfd; + struct radeon_cs_reloc *reloc; + + if (argc != 2) { + fprintf(stderr, "usage : %s jsonfile\n", argv[0]); + return -EINVAL; + } + + radeonfd = radeon_open_fd(); + if (radeonfd < 0) { + fprintf(stderr, "failed to radeon fd\n"); + return -1; + } + + if (cs_json_load(&cs, argv[1])) { + fprintf(stderr, "failed to load json\n"); + return -EINVAL; + } + + /* allocate bo */ + for (i = 0; i < cs.nbo; i++) { + cs.bo[i].rbo = rbo(radeonfd, 0, cs.bo[i].size, 4096, cs.bo[i].data); + if (cs.bo[i].rbo == NULL) { + fprintf(stderr, "failed to allocate hw bo[%d]\n", i); + return -EINVAL; + } + if (!rbo_map(radeonfd, cs.bo[i].rbo)) { + fprintf(stderr, "bo[%d][0x%08X][0] =", i, cs.bo[i].rbo->size); + for (j = 0; j < 4; j++) { + fprintf(stderr, " 0x%08x", ((unsigned*)cs.bo[i].rbo->data)[j]); + } + fprintf(stderr, "\n"); + rbo_unmap(radeonfd, cs.bo[i].rbo); + } + } + /* update reloc */ + reloc = (struct radeon_cs_reloc *)cs.reloc; + for (i = 0; i < (cs.reloc_ndw / 4); i++) { + for (j = 0; j < cs.nbo; j++) { + if (reloc[i].handle == cs.bo[j].handle) { + reloc[i].handle = cs.bo[j].rbo->handle; + break; + } + } + if (j == cs.nbo) { + fprintf(stderr, "failed to find bo for handle %d\n", reloc[i].handle); + return -EINVAL; + } + } + + printf("ready to submit, press a key to proceed\n"); + getchar(); + + if (cs_submit(radeonfd, &cs)) { + fprintf(stderr, "cs submission failed\n"); + return -EINVAL; + } + for (i = 0; i < cs.nbo; i++) { + if (!rbo_map(radeonfd, cs.bo[i].rbo)) { + fprintf(stderr, "bo[%d][0x%08X][0] =", i, cs.bo[i].rbo->size); + for (j = 0; j < 4; j++) { + fprintf(stderr, " 0x%08x", ((unsigned*)cs.bo[i].rbo->data)[j]); + } + fprintf(stderr, "\n"); + rbo_unmap(radeonfd, cs.bo[i].rbo); + } + } + + return 0; +} diff --git a/radeon_pci.c b/radeon_pci.c index 7a1edef..f348472 100644 --- a/radeon_pci.c +++ b/radeon_pci.c @@ -36,6 +36,7 @@ int radeon_pci_init(struct radeon_pci *pradeon) struct pci_device *device; int r = 0; + pradeon->dev = NULL; if (pci_system_init()) { fprintf(stderr, "Failed to initialize libpciaccess\n"); return -1; @@ -48,6 +49,11 @@ int radeon_pci_init(struct radeon_pci *pradeon) iter = pci_slot_match_iterator_create(&match); while ((device = pci_device_next(iter))) { pci_device_probe(device); + + if ((device->device_class & 0x00FF0000) != 0x30000) { + continue; + } + /* ATI vendor id 0x1002 and we are looking for a video * card class 0x03 with subclass 0x00 (VGA) */ @@ -55,6 +61,10 @@ int radeon_pci_init(struct radeon_pci *pradeon) pradeon->mmio_base = device->regions[2].base_addr; pradeon->mmio_size = device->regions[2].size; pradeon->dev = device; + fprintf(stderr, "probed %02d:%02d.%d 0x%08X (0x%08X %d|0x%08X)\n", + device->bus, device->dev, device->func, device->device_class, + (unsigned)pradeon->mmio_base, (unsigned)pradeon->mmio_size, + (unsigned)pradeon->mmio_size); break; } } @@ -63,9 +73,10 @@ int radeon_pci_init(struct radeon_pci *pradeon) fprintf(stderr, "Cannot find radeon device\n"); return -1; } - if (pci_device_map_range(pradeon->dev, pradeon->mmio_base, pradeon->mmio_size, + r = pci_device_map_range(pradeon->dev, pradeon->mmio_base, pradeon->mmio_size, PCI_DEV_MAP_FLAG_WRITABLE, - (void**)&pradeon->mmio)) { + (void**)&pradeon->mmio); + if (r) { fprintf(stderr, "Mapping mmio failed (are you root ?)\n"); return -1; } @@ -40,13 +40,37 @@ int main(int argc, char *argv[]) return -1; } + printf("0x0E50_SRBM_STATUS 0x%08x\n", radeon_mmio_rd32(&pradeon, 0x0e50)); + printf("0x8010_GRBM_STATUS 0x%08x\n", radeon_mmio_rd32(&pradeon, 0x8010)); + printf("0x8014_GRBM_STATUS_SE0 0x%08x\n", radeon_mmio_rd32(&pradeon, 0x8014)); + printf("0x8018_GRBM_STATUS_SE1 0x%08x\n", radeon_mmio_rd32(&pradeon, 0x8018)); + + printf("0x98F4_CC_RB_BACKEND_DISABLE 0x%08x\n", radeon_mmio_rd32(&pradeon, 0x98F4)); + printf("0x98F8_GB_ADDR_CONFIG 0x%08x\n", radeon_mmio_rd32(&pradeon, 0x98F8)); + printf("0x98FC_GB_BACKEND_MAP 0x%08x\n", radeon_mmio_rd32(&pradeon, 0x98FC)); + + printf("0x8950_CC_GC_SHADER_PIPE_CONFIG 0x%08x\n", radeon_mmio_rd32(&pradeon, 0x8950)); + printf("0x8954_GC_USER_SHADER_PIPE_CONFIG 0x%08x\n", radeon_mmio_rd32(&pradeon, 0x8954)); + + /* general reg */ +#if 0 + range.foffset = 0x0008; + range.loffset = 0x0800; + range.stride = 4; + dump_range(&dump, &pradeon, &range); + dump_print(&dump); + dump_fini(&dump); +#endif /* general reg */ - range.foffset = 0x0004; - range.loffset = 0x8000; +#if 0 + range.foffset = 0x0c00; + range.loffset = 0x1000; range.stride = 4; dump_range(&dump, &pradeon, &range); dump_print(&dump); dump_fini(&dump); +#endif +#if 0 /* config reg */ range.foffset = 0x8000; range.loffset = 0xac00; @@ -54,13 +78,16 @@ int main(int argc, char *argv[]) dump_range(&dump, &pradeon, &range); dump_print(&dump); dump_fini(&dump); +#endif +#if 0 /* context reg */ - range.foffset = 0x28000; - range.loffset = 0x29000; + range.foffset = 0x29000; + range.loffset = 0x2A000; range.stride = 4; dump_range(&dump, &pradeon, &range); dump_print(&dump); dump_fini(&dump); +#endif radeon_pci_fini(&pradeon); return 0; @@ -42,7 +42,12 @@ int dump_range(struct dump *dump, struct radeon_pci *pradeon, struct range *rang for (i = 0; i < dump->nregs; i++) { dump->regs[i].domain = range->domain; dump->regs[i].offset = range->foffset + range->stride * i; - dump->regs[i].value = radeon_mmio_rd32(pradeon, dump->regs[i].offset); + if (dump->regs[i].offset > pradeon->mmio_size) { + radeon_mmio_wr32(pradeon, 0x0, dump->regs[i].offset); + dump->regs[i].value = radeon_mmio_rd32(pradeon, 0x4); + } else { + dump->regs[i].value = radeon_mmio_rd32(pradeon, dump->regs[i].offset); + } } return 0; } diff --git a/testgttvram.c b/testgttvram.c new file mode 100644 index 0000000..47978ed --- /dev/null +++ b/testgttvram.c @@ -0,0 +1,56 @@ +/* + * Copyright 2011 Jerome Glisse <glisse@freedesktop.org> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * on the rights to use, copy, modify, merge, publish, distribute, sub + * license, and/or sell copies of the Software, and to permit persons to whom + * the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL + * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: + * Jerome Glisse + */ +#include <stdio.h> +#include <stdlib.h> +#include "xf86drm.h" +#include "radeon_drm.h" + +int test(int fd) +{ + struct drm_radeon_info info; + + info.request = 0x100; + return drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(struct drm_radeon_info)); +} + +int radeon_open_fd(void) +{ + return drmOpen("radeon", NULL); +} + +int main(void) +{ + int radeonfd; + + radeonfd = radeon_open_fd(); + if (radeonfd < 0) { + fprintf(stderr, "failed to radeon fd\n"); + return -1; + } + test(radeonfd); + drmClose(radeonfd); + return 0; +} |