2 * Copyright 2008 Jerome Glisse.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Jerome Glisse <glisse@freedesktop.org>
28 #include "radeon_drm.h"
29 #include "radeon_reg.h"
32 void r100_cs_dump_packet(struct radeon_cs_parser
*p
,
33 struct radeon_cs_packet
*pkt
);
35 int radeon_cs_parser_relocs(struct radeon_cs_parser
*p
)
37 struct drm_device
*ddev
= p
->rdev
->ddev
;
38 struct radeon_cs_chunk
*chunk
;
42 if (p
->chunk_relocs_idx
== -1) {
45 chunk
= &p
->chunks
[p
->chunk_relocs_idx
];
46 /* FIXME: we assume that each relocs use 4 dwords */
47 p
->nrelocs
= chunk
->length_dw
/ 4;
48 p
->relocs_ptr
= kcalloc(p
->nrelocs
, sizeof(void *), GFP_KERNEL
);
49 if (p
->relocs_ptr
== NULL
) {
52 p
->relocs
= kcalloc(p
->nrelocs
, sizeof(struct radeon_cs_reloc
), GFP_KERNEL
);
53 if (p
->relocs
== NULL
) {
56 for (i
= 0; i
< p
->nrelocs
; i
++) {
57 struct drm_radeon_cs_reloc
*r
;
60 r
= (struct drm_radeon_cs_reloc
*)&chunk
->kdata
[i
*4];
61 for (j
= 0; j
< p
->nrelocs
; j
++) {
62 if (r
->handle
== p
->relocs
[j
].handle
) {
63 p
->relocs_ptr
[i
] = &p
->relocs
[j
];
69 p
->relocs
[i
].gobj
= drm_gem_object_lookup(ddev
,
72 if (p
->relocs
[i
].gobj
== NULL
) {
73 DRM_ERROR("gem object lookup failed 0x%x\n",
77 p
->relocs_ptr
[i
] = &p
->relocs
[i
];
78 p
->relocs
[i
].robj
= p
->relocs
[i
].gobj
->driver_private
;
79 p
->relocs
[i
].lobj
.robj
= p
->relocs
[i
].robj
;
80 p
->relocs
[i
].lobj
.rdomain
= r
->read_domains
;
81 p
->relocs
[i
].lobj
.wdomain
= r
->write_domain
;
82 p
->relocs
[i
].handle
= r
->handle
;
83 p
->relocs
[i
].flags
= r
->flags
;
84 INIT_LIST_HEAD(&p
->relocs
[i
].lobj
.list
);
85 radeon_object_list_add_object(&p
->relocs
[i
].lobj
,
89 return radeon_object_list_validate(&p
->validated
, p
->ib
->fence
);
92 int radeon_cs_parser_init(struct radeon_cs_parser
*p
, void *data
)
94 struct drm_radeon_cs
*cs
= data
;
95 uint64_t *chunk_array_ptr
;
98 if (!cs
->num_chunks
) {
102 INIT_LIST_HEAD(&p
->validated
);
104 p
->chunk_ib_idx
= -1;
105 p
->chunk_relocs_idx
= -1;
106 p
->chunks_array
= kcalloc(cs
->num_chunks
, sizeof(uint64_t), GFP_KERNEL
);
107 if (p
->chunks_array
== NULL
) {
110 chunk_array_ptr
= (uint64_t *)(unsigned long)(cs
->chunks
);
111 if (DRM_COPY_FROM_USER(p
->chunks_array
, chunk_array_ptr
,
112 sizeof(uint64_t)*cs
->num_chunks
)) {
115 p
->nchunks
= cs
->num_chunks
;
116 p
->chunks
= kcalloc(p
->nchunks
, sizeof(struct radeon_cs_chunk
), GFP_KERNEL
);
117 if (p
->chunks
== NULL
) {
120 for (i
= 0; i
< p
->nchunks
; i
++) {
121 struct drm_radeon_cs_chunk __user
**chunk_ptr
= NULL
;
122 struct drm_radeon_cs_chunk user_chunk
;
123 uint32_t __user
*cdata
;
125 chunk_ptr
= (void __user
*)(unsigned long)p
->chunks_array
[i
];
126 if (DRM_COPY_FROM_USER(&user_chunk
, chunk_ptr
,
127 sizeof(struct drm_radeon_cs_chunk
))) {
130 p
->chunks
[i
].length_dw
= user_chunk
.length_dw
;
131 p
->chunks
[i
].kdata
= NULL
;
132 p
->chunks
[i
].chunk_id
= user_chunk
.chunk_id
;
134 if (p
->chunks
[i
].chunk_id
== RADEON_CHUNK_ID_RELOCS
) {
135 p
->chunk_relocs_idx
= i
;
137 if (p
->chunks
[i
].chunk_id
== RADEON_CHUNK_ID_IB
) {
139 /* zero length IB isn't useful */
140 if (p
->chunks
[i
].length_dw
== 0)
144 p
->chunks
[i
].length_dw
= user_chunk
.length_dw
;
145 cdata
= (uint32_t *)(unsigned long)user_chunk
.chunk_data
;
147 size
= p
->chunks
[i
].length_dw
* sizeof(uint32_t);
148 p
->chunks
[i
].kdata
= kzalloc(size
, GFP_KERNEL
);
149 if (p
->chunks
[i
].kdata
== NULL
) {
152 if (DRM_COPY_FROM_USER(p
->chunks
[i
].kdata
, cdata
, size
)) {
156 if (p
->chunks
[p
->chunk_ib_idx
].length_dw
> (16 * 1024)) {
157 DRM_ERROR("cs IB too big: %d\n",
158 p
->chunks
[p
->chunk_ib_idx
].length_dw
);
165 * cs_parser_fini() - clean parser states
166 * @parser: parser structure holding parsing context.
167 * @error: error number
169 * If error is set than unvalidate buffer, otherwise just free memory
170 * used by parsing context.
172 static void radeon_cs_parser_fini(struct radeon_cs_parser
*parser
, int error
)
177 radeon_object_list_unvalidate(&parser
->validated
);
179 radeon_object_list_clean(&parser
->validated
);
181 for (i
= 0; i
< parser
->nrelocs
; i
++) {
182 if (parser
->relocs
[i
].gobj
) {
183 mutex_lock(&parser
->rdev
->ddev
->struct_mutex
);
184 drm_gem_object_unreference(parser
->relocs
[i
].gobj
);
185 mutex_unlock(&parser
->rdev
->ddev
->struct_mutex
);
188 kfree(parser
->relocs
);
189 kfree(parser
->relocs_ptr
);
190 for (i
= 0; i
< parser
->nchunks
; i
++) {
191 kfree(parser
->chunks
[i
].kdata
);
193 kfree(parser
->chunks
);
194 kfree(parser
->chunks_array
);
195 radeon_ib_free(parser
->rdev
, &parser
->ib
);
198 int radeon_cs_ioctl(struct drm_device
*dev
, void *data
, struct drm_file
*filp
)
200 struct radeon_device
*rdev
= dev
->dev_private
;
201 struct radeon_cs_parser parser
;
202 struct radeon_cs_chunk
*ib_chunk
;
205 mutex_lock(&rdev
->cs_mutex
);
206 if (rdev
->gpu_lockup
) {
207 mutex_unlock(&rdev
->cs_mutex
);
210 /* initialize parser */
211 memset(&parser
, 0, sizeof(struct radeon_cs_parser
));
214 r
= radeon_cs_parser_init(&parser
, data
);
216 DRM_ERROR("Failed to initialize parser !\n");
217 radeon_cs_parser_fini(&parser
, r
);
218 mutex_unlock(&rdev
->cs_mutex
);
221 r
= radeon_ib_get(rdev
, &parser
.ib
);
223 DRM_ERROR("Failed to get ib !\n");
224 radeon_cs_parser_fini(&parser
, r
);
225 mutex_unlock(&rdev
->cs_mutex
);
228 r
= radeon_cs_parser_relocs(&parser
);
230 DRM_ERROR("Failed to parse relocation !\n");
231 radeon_cs_parser_fini(&parser
, r
);
232 mutex_unlock(&rdev
->cs_mutex
);
235 /* Copy the packet into the IB, the parser will read from the
236 * input memory (cached) and write to the IB (which can be
238 ib_chunk
= &parser
.chunks
[parser
.chunk_ib_idx
];
239 parser
.ib
->length_dw
= ib_chunk
->length_dw
;
240 memcpy((void *)parser
.ib
->ptr
, ib_chunk
->kdata
, ib_chunk
->length_dw
*4);
241 r
= radeon_cs_parse(&parser
);
243 DRM_ERROR("Invalid command stream !\n");
244 radeon_cs_parser_fini(&parser
, r
);
245 mutex_unlock(&rdev
->cs_mutex
);
248 r
= radeon_ib_schedule(rdev
, parser
.ib
);
250 DRM_ERROR("Faild to schedule IB !\n");
252 radeon_cs_parser_fini(&parser
, r
);
253 mutex_unlock(&rdev
->cs_mutex
);