summaryrefslogtreecommitdiff
path: root/tools/null_state_gen/intel_null_state_gen.c
blob: 14f45d3aa1d974dfc4aca72304d0389439ab5b9c (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>

#include "intel_batchbuffer.h"

#define STATE_ALIGN 64

extern int gen6_setup_null_render_state(struct intel_batchbuffer *batch);
extern int gen7_setup_null_render_state(struct intel_batchbuffer *batch);
extern int gen8_setup_null_render_state(struct intel_batchbuffer *batch);

static void print_usage(char *s)
{
	fprintf(stderr, "%s: <gen>\n"
		"     gen:     gen to generate for (6,7,8)\n",
	       s);
}

static int is_reloc(struct intel_batchbuffer *batch, uint32_t offset)
{
	int i;

	for (i = 0; i < batch->num_relocs; i++)
		if (batch->relocs[i] == offset)
			return 1;

	return 0;
}

static int print_state(int gen, struct intel_batchbuffer *batch)
{
	int i;

	printf("#include \"intel_renderstate.h\"\n\n");

	printf("static const u32 gen%d_null_state_relocs[] = {\n", gen);
	for (i = 0; i < batch->num_relocs; i++) {
		printf("\t0x%08x,\n", batch->relocs[i]);
	}
	printf("};\n\n");

	printf("static const u32 gen%d_null_state_batch[] = {\n", gen);
	for (i = 0; i < batch->size; i += 4) {
		const uint32_t *p = (void *)batch->base + i;
		printf("\t0x%08x,", *p);

		if (i == intel_batch_cmds_used(batch) - 4)
			printf("\t /* cmds end */");

		if (i == intel_batch_state_start(batch))
			printf("\t /* state start */");


		if (i == intel_batch_state_start(batch) +
		    intel_batch_state_used(batch) - 4)
			printf("\t /* state end */");

		if (is_reloc(batch, i))
			printf("\t /* reloc */");

		printf("\n");
	}
	printf("};\n\nRO_RENDERSTATE(%d);\n", gen);

	return 0;
}

static int do_generate(int gen)
{
	int initial_size = 8192;
	struct intel_batchbuffer batch;
	void *p;
	int ret = -EINVAL;
	uint32_t cmd_len, state_len, size;
	int (*null_state_gen)(struct intel_batchbuffer *batch) = NULL;

	p = malloc(initial_size);
	if (p == NULL)
		return -ENOMEM;

	assert(ALIGN(initial_size/2, STATE_ALIGN) == initial_size/2);

	ret = intel_batch_reset(&batch, p, initial_size, initial_size/2);
	if (ret)
		goto out;

	switch (gen) {
	case 6:
		null_state_gen = gen6_setup_null_render_state;
		break;

	case 7:
		null_state_gen = gen7_setup_null_render_state;
		break;

	case 8:
		null_state_gen = gen8_setup_null_render_state;
		break;
	}

	if (null_state_gen == NULL) {
		printf("no generator found for %d\n", gen);
		ret = -EINVAL;
		goto out;
	}

	ret = null_state_gen(&batch);
	if (ret < 0)
		goto out;

	cmd_len = intel_batch_cmds_used(&batch);
	state_len = intel_batch_state_used(&batch);

	size = cmd_len + state_len + ALIGN(cmd_len, STATE_ALIGN) - cmd_len;

	ret = intel_batch_reset(&batch, p, size, ALIGN(cmd_len, STATE_ALIGN));
	if (ret)
		goto out;

	ret = null_state_gen(&batch);
	if (ret < 0)
		goto out;

	assert(cmd_len == intel_batch_cmds_used(&batch));
	assert(state_len == intel_batch_state_used(&batch));
	assert(size == ret);

	/* Batch buffer needs to end */
	assert(*(uint32_t *)(p + cmd_len - 4) == (0xA << 23));

	ret = print_state(gen, &batch);
out:
	free(p);

	if (ret < 0)
		return ret;

	return 0;
}

int main(int argc, char *argv[])
{
	if (argc != 2) {
		print_usage(argv[0]);
		return 1;
	}

	return do_generate(atoi(argv[1]));
}