d7cd1d7a9465985c6826102aa31c67c13a4d146b
[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 int radeon_state_init(struct radeon_state *state, struct radeon *radeon, u32 type, u32 id)
36 {
37 if (type > radeon->ntype) {
38 fprintf(stderr, "%s invalid type %d\n", __func__, type);
39 return -EINVAL;
40 }
41 if (id > radeon->nstate) {
42 fprintf(stderr, "%s invalid state id %d\n", __func__, id);
43 return -EINVAL;
44 }
45 memset(state, 0, sizeof(struct radeon_state));
46 state->radeon = radeon;
47 state->type = type;
48 state->id = id;
49 state->npm4 = radeon->type[type].npm4;
50 state->nstates = radeon->type[type].nstates;
51 return 0;
52 }
53
54 int radeon_state_replace_always(struct radeon_state *ostate,
55 struct radeon_state *nstate)
56 {
57 return 1;
58 }
59
60 int radeon_state_pm4_generic(struct radeon_state *state)
61 {
62 return -EINVAL;
63 }
64
65 static u32 crc32(void *d, size_t len)
66 {
67 u16 *data = (uint16_t*)d;
68 u32 sum1 = 0xffff, sum2 = 0xffff;
69
70 len = len >> 1;
71 while (len) {
72 unsigned tlen = len > 360 ? 360 : len;
73 len -= tlen;
74 do {
75 sum1 += *data++;
76 sum2 += sum1;
77 } while (--tlen);
78 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
79 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
80 }
81 /* Second reduction step to reduce sums to 16 bits */
82 sum1 = (sum1 & 0xffff) + (sum1 >> 16);
83 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
84 return sum2 << 16 | sum1;
85 }
86
87 int radeon_state_pm4(struct radeon_state *state)
88 {
89 int r;
90
91 if (state == NULL || state->cpm4)
92 return 0;
93 r = state->radeon->type[state->type].pm4(state);
94 if (r) {
95 fprintf(stderr, "%s failed to build PM4 for state(%d %d)\n",
96 __func__, state->type, state->id);
97 return r;
98 }
99 state->pm4_crc = crc32(state->pm4, state->cpm4 * 4);
100 state->valid = 1;
101 return 0;
102 }
103
104 int radeon_state_reloc(struct radeon_state *state, unsigned id, unsigned bo_id)
105 {
106 state->reloc_pm4_id[state->nreloc] = id;
107 state->reloc_bo_id[state->nreloc] = bo_id;
108 state->nreloc++;
109 return 0;
110 }