2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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:
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
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.
30 #include "radeon_priv.h"
33 * state core functions
35 struct radeon_state
*radeon_state(struct radeon
*radeon
, u32 type
, u32 id
)
37 struct radeon_state
*state
;
39 if (type
> radeon
->ntype
) {
40 fprintf(stderr
, "%s invalid type %d\n", __func__
, type
);
43 if (id
> radeon
->nstate
) {
44 fprintf(stderr
, "%s invalid state id %d\n", __func__
, id
);
47 state
= calloc(1, sizeof(*state
));
50 state
->radeon
= radeon
;
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
);
65 struct radeon_state
*radeon_state_duplicate(struct radeon_state
*state
)
67 struct radeon_state
*nstate
= radeon_state(state
->radeon
, state
->type
, state
->id
);
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
]);
87 struct radeon_state
*radeon_state_incref(struct radeon_state
*state
)
93 struct radeon_state
*radeon_state_decref(struct radeon_state
*state
)
99 if (--state
->refcount
> 0) {
102 for (i
= 0; i
< state
->nbo
; i
++) {
103 state
->bo
[i
] = radeon_bo_decref(state
->radeon
, state
->bo
[i
]);
108 memset(state
, 0, sizeof(*state
));
113 int radeon_state_replace_always(struct radeon_state
*ostate
,
114 struct radeon_state
*nstate
)
119 int radeon_state_pm4_generic(struct radeon_state
*state
)
124 static u32
crc32(void *d
, size_t len
)
126 u16
*data
= (uint16_t*)d
;
127 u32 sum1
= 0xffff, sum2
= 0xffff;
131 unsigned tlen
= len
> 360 ? 360 : len
;
137 sum1
= (sum1
& 0xffff) + (sum1
>> 16);
138 sum2
= (sum2
& 0xffff) + (sum2
>> 16);
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
;
146 int radeon_state_pm4(struct radeon_state
*state
)
150 if (state
== NULL
|| state
->cpm4
)
152 r
= state
->radeon
->type
[state
->type
].pm4(state
);
154 fprintf(stderr
, "%s failed to build PM4 for state(%d %d)\n",
155 __func__
, state
->type
, state
->id
);
158 state
->pm4_crc
= crc32(state
->pm4
, state
->cpm4
* 4);
162 int radeon_state_reloc(struct radeon_state
*state
, unsigned id
, unsigned bo_id
)
164 state
->reloc_pm4_id
[state
->nreloc
] = id
;
165 state
->reloc_bo_id
[state
->nreloc
] = bo_id
;