Revert "r600g: don't use dynamic state allocation for states"
[mesa.git] / src / gallium / winsys / r600 / drm / radeon_state.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse
25 */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include "radeon_priv.h"
31
32 /*
33 * state core functions
34 */
35 struct radeon_state *radeon_state(struct radeon *radeon, u32 type, u32 id)
36 {
37 struct radeon_state *state;
38
39 if (type > radeon->ntype) {
40 fprintf(stderr, "%s invalid type %d\n", __func__, type);
41 return NULL;
42 }
43 if (id > radeon->nstate) {
44 fprintf(stderr, "%s invalid state id %d\n", __func__, id);
45 return NULL;
46 }
47 state = calloc(1, sizeof(*state));
48 if (state == NULL)
49 return NULL;
50 state->radeon = radeon;
51 state->type = type;
52 state->id = id;
53 state->refcount = 1;
54 state->npm4 = radeon->type[type].npm4;
55 state->nstates = radeon->type[type].nstates;
56 state->states = calloc(1, state->nstates * 4);
57 state->pm4 = calloc(1, radeon->type[type].npm4 * 4);
58 if (state->states == NULL || state->pm4 == NULL) {
59 radeon_state_decref(state);
60 return NULL;
61 }
62 return state;
63 }
64
65 struct radeon_state *radeon_state_duplicate(struct radeon_state *state)
66 {
67 struct radeon_state *nstate = radeon_state(state->radeon, state->type, state->id);
68 unsigned i;
69
70 if (state == NULL)
71 return NULL;
72 nstate->cpm4 = state->cpm4;
73 nstate->nbo = state->nbo;
74 nstate->nreloc = state->nreloc;
75 memcpy(nstate->states, state->states, state->nstates * 4);
76 memcpy(nstate->pm4, state->pm4, state->npm4 * 4);
77 memcpy(nstate->placement, state->placement, 8 * 4);
78 memcpy(nstate->reloc_pm4_id, state->reloc_pm4_id, 8 * 4);
79 memcpy(nstate->reloc_bo_id, state->reloc_bo_id, 8 * 4);
80 memcpy(nstate->bo_dirty, state->bo_dirty, 4 * 4);
81 for (i = 0; i < state->nbo; i++) {
82 nstate->bo[i] = radeon_bo_incref(state->radeon, state->bo[i]);
83 }
84 return nstate;
85 }
86
87 struct radeon_state *radeon_state_incref(struct radeon_state *state)
88 {
89 state->refcount++;
90 return state;
91 }
92
93 struct radeon_state *radeon_state_decref(struct radeon_state *state)
94 {
95 unsigned i;
96
97 if (state == NULL)
98 return NULL;
99 if (--state->refcount > 0) {
100 return NULL;
101 }
102 for (i = 0; i < state->nbo; i++) {
103 state->bo[i] = radeon_bo_decref(state->radeon, state->bo[i]);
104 }
105 free(state->immd);
106 free(state->states);
107 free(state->pm4);
108 memset(state, 0, sizeof(*state));
109 free(state);
110 return NULL;
111 }
112
113 int radeon_state_replace_always(struct radeon_state *ostate,
114 struct radeon_state *nstate)
115 {
116 return 1;
117 }
118
119 int radeon_state_pm4_generic(struct radeon_state *state)
120 {
121 return -EINVAL;
122 }
123
124 static u32 crc32(void *d, size_t len)
125 {
126 u16 *data = (uint16_t*)d;
127 u32 sum1 = 0xffff, sum2 = 0xffff;
128
129 len = len >> 1;
130 while (len) {
131 unsigned tlen = len > 360 ? 360 : len;
132 len -= tlen;
133 do {
134 sum1 += *data++;
135 sum2 += sum1;
136 } while (--tlen);
137 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
138 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
139 }
140 /* Second reduction step to reduce sums to 16 bits */
141 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
142 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
143 return sum2 << 16 | sum1;
144 }
145
146 int radeon_state_pm4(struct radeon_state *state)
147 {
148 int r;
149
150 if (state == NULL || state->cpm4)
151 return 0;
152 r = state->radeon->type[state->type].pm4(state);
153 if (r) {
154 fprintf(stderr, "%s failed to build PM4 for state(%d %d)\n",
155 __func__, state->type, state->id);
156 return r;
157 }
158 state->pm4_crc = crc32(state->pm4, state->cpm4 * 4);
159 return 0;
160 }
161
162 int radeon_state_reloc(struct radeon_state *state, unsigned id, unsigned bo_id)
163 {
164 state->reloc_pm4_id[state->nreloc] = id;
165 state->reloc_bo_id[state->nreloc] = bo_id;
166 state->nreloc++;
167 return 0;
168 }