summaryrefslogtreecommitdiff
path: root/src/gallium/winsys/r600/drm/radeon_state.c
blob: c60b12ef67c950d6005028e9880523ffd962ac81 (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
/*
 * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "radeon_priv.h"

/*
 * state core functions
 */
struct radeon_state *radeon_state(struct radeon *radeon, u32 id)
{
	struct radeon_state *state;

	state = calloc(1, sizeof(*state));
	if (state == NULL)
		return NULL;
	state->radeon = radeon;
	state->id = id;
	state->refcount = 1;
	state->cpm4 = radeon->type[id].state_cpm4;
	memcpy(state->states, radeon->type[id].state_pm4, radeon->type[id].state_cpm4 * 4);
	return state;
}

struct radeon_state *radeon_state_duplicate(struct radeon_state *state)
{
	struct radeon_state *nstate = radeon_state(state->radeon, state->id);
	unsigned i;

	if (state == NULL)
		return NULL;
	*nstate = *state;
	for (i = 0; i < state->nbo; i++) {
		nstate->bo[i] = radeon_bo_incref(state->radeon, state->bo[i]);
	}
	return nstate;
}

struct radeon_state *radeon_state_incref(struct radeon_state *state)
{
	state->refcount++;
	return state;
}

struct radeon_state *radeon_state_decref(struct radeon_state *state)
{
	unsigned i;

	if (state == NULL)
		return NULL;
	if (--state->refcount > 0) {
		return NULL;
	}
	for (i = 0; i < state->nbo; i++) {
		state->bo[i] = radeon_bo_decref(state->radeon, state->bo[i]);
	}
	memset(state, 0, sizeof(*state));
	free(state);
	return NULL;
}

int radeon_state_replace_always(struct radeon_state *ostate,
				struct radeon_state *nstate)
{
	return 1;
}

int radeon_state_pm4_generic(struct radeon_state *state)
{
	return -EINVAL;
}

static u32 crc32(void *d, size_t len)
{
	u16 *data = (uint16_t*)d;
	u32 sum1 = 0xffff, sum2 = 0xffff;

	len = len >> 1;
	while (len) {
		unsigned tlen = len > 360 ? 360 : len;
		len -= tlen;
		do {
			sum1 += *data++;
			sum2 += sum1;
		} while (--tlen);
		sum1 = (sum1 & 0xffff) + (sum1 >> 16);
		sum2 = (sum2 & 0xffff) + (sum2 >> 16);
	}
	/* Second reduction step to reduce sums to 16 bits */
	sum1 = (sum1 & 0xffff) + (sum1 >> 16);
	sum2 = (sum2 & 0xffff) + (sum2 >> 16);
	return sum2 << 16 | sum1;
}

int radeon_state_pm4(struct radeon_state *state)
{
	if (state == NULL)
		return 0;
	state->pm4_crc = crc32(state->states, state->cpm4 * 4);
	return 0;
}