]>
Commit | Line | Data |
---|---|---|
771fe6b9 JG |
1 | /* |
2 | * Copyright 2008 Jerome Glisse. | |
3 | * All Rights Reserved. | |
4 | * | |
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: | |
11 | * | |
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 | |
14 | * Software. | |
15 | * | |
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. | |
23 | * | |
24 | * Authors: | |
25 | * Jerome Glisse <glisse@freedesktop.org> | |
26 | */ | |
27 | #include "drmP.h" | |
28 | #include "radeon_drm.h" | |
29 | #include "radeon_reg.h" | |
30 | #include "radeon.h" | |
31 | ||
32 | void r100_cs_dump_packet(struct radeon_cs_parser *p, | |
33 | struct radeon_cs_packet *pkt); | |
34 | ||
35 | int radeon_cs_parser_relocs(struct radeon_cs_parser *p) | |
36 | { | |
37 | struct drm_device *ddev = p->rdev->ddev; | |
38 | struct radeon_cs_chunk *chunk; | |
39 | unsigned i, j; | |
40 | bool duplicate; | |
41 | ||
42 | if (p->chunk_relocs_idx == -1) { | |
43 | return 0; | |
44 | } | |
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) { | |
50 | return -ENOMEM; | |
51 | } | |
52 | p->relocs = kcalloc(p->nrelocs, sizeof(struct radeon_cs_reloc), GFP_KERNEL); | |
53 | if (p->relocs == NULL) { | |
54 | return -ENOMEM; | |
55 | } | |
56 | for (i = 0; i < p->nrelocs; i++) { | |
57 | struct drm_radeon_cs_reloc *r; | |
58 | ||
59 | duplicate = false; | |
60 | r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4]; | |
16557f1e | 61 | for (j = 0; j < i; j++) { |
771fe6b9 JG |
62 | if (r->handle == p->relocs[j].handle) { |
63 | p->relocs_ptr[i] = &p->relocs[j]; | |
64 | duplicate = true; | |
65 | break; | |
66 | } | |
67 | } | |
68 | if (!duplicate) { | |
69 | p->relocs[i].gobj = drm_gem_object_lookup(ddev, | |
70 | p->filp, | |
71 | r->handle); | |
72 | if (p->relocs[i].gobj == NULL) { | |
73 | DRM_ERROR("gem object lookup failed 0x%x\n", | |
74 | r->handle); | |
bf79cb91 | 75 | return -ENOENT; |
771fe6b9 JG |
76 | } |
77 | p->relocs_ptr[i] = &p->relocs[i]; | |
7e4d15d9 | 78 | p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj); |
4c788679 | 79 | p->relocs[i].lobj.bo = p->relocs[i].robj; |
771fe6b9 | 80 | p->relocs[i].lobj.wdomain = r->write_domain; |
147666fb TH |
81 | p->relocs[i].lobj.rdomain = r->read_domains; |
82 | p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo; | |
771fe6b9 JG |
83 | p->relocs[i].handle = r->handle; |
84 | p->relocs[i].flags = r->flags; | |
4c788679 | 85 | radeon_bo_list_add_object(&p->relocs[i].lobj, |
147666fb | 86 | &p->validated); |
16557f1e CK |
87 | } else |
88 | p->relocs[i].handle = 0; | |
771fe6b9 | 89 | } |
94429bb6 | 90 | return radeon_bo_list_validate(&p->validated); |
771fe6b9 JG |
91 | } |
92 | ||
93 | int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data) | |
94 | { | |
95 | struct drm_radeon_cs *cs = data; | |
96 | uint64_t *chunk_array_ptr; | |
e70f224c | 97 | unsigned size, i, flags = 0; |
771fe6b9 JG |
98 | |
99 | if (!cs->num_chunks) { | |
100 | return 0; | |
101 | } | |
102 | /* get chunks */ | |
103 | INIT_LIST_HEAD(&p->validated); | |
104 | p->idx = 0; | |
105 | p->chunk_ib_idx = -1; | |
106 | p->chunk_relocs_idx = -1; | |
107 | p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL); | |
108 | if (p->chunks_array == NULL) { | |
109 | return -ENOMEM; | |
110 | } | |
111 | chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks); | |
112 | if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr, | |
113 | sizeof(uint64_t)*cs->num_chunks)) { | |
114 | return -EFAULT; | |
115 | } | |
116 | p->nchunks = cs->num_chunks; | |
117 | p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL); | |
118 | if (p->chunks == NULL) { | |
119 | return -ENOMEM; | |
120 | } | |
121 | for (i = 0; i < p->nchunks; i++) { | |
122 | struct drm_radeon_cs_chunk __user **chunk_ptr = NULL; | |
123 | struct drm_radeon_cs_chunk user_chunk; | |
124 | uint32_t __user *cdata; | |
125 | ||
126 | chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i]; | |
127 | if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr, | |
128 | sizeof(struct drm_radeon_cs_chunk))) { | |
129 | return -EFAULT; | |
130 | } | |
5176fdc4 DA |
131 | p->chunks[i].length_dw = user_chunk.length_dw; |
132 | p->chunks[i].kdata = NULL; | |
771fe6b9 | 133 | p->chunks[i].chunk_id = user_chunk.chunk_id; |
5176fdc4 | 134 | |
771fe6b9 JG |
135 | if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) { |
136 | p->chunk_relocs_idx = i; | |
137 | } | |
138 | if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) { | |
139 | p->chunk_ib_idx = i; | |
5176fdc4 DA |
140 | /* zero length IB isn't useful */ |
141 | if (p->chunks[i].length_dw == 0) | |
142 | return -EINVAL; | |
771fe6b9 | 143 | } |
e70f224c MO |
144 | if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS && |
145 | !p->chunks[i].length_dw) { | |
146 | return -EINVAL; | |
147 | } | |
5176fdc4 | 148 | |
771fe6b9 | 149 | p->chunks[i].length_dw = user_chunk.length_dw; |
513bcb46 | 150 | p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data; |
771fe6b9 | 151 | |
513bcb46 DA |
152 | cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data; |
153 | if (p->chunks[i].chunk_id != RADEON_CHUNK_ID_IB) { | |
154 | size = p->chunks[i].length_dw * sizeof(uint32_t); | |
155 | p->chunks[i].kdata = kmalloc(size, GFP_KERNEL); | |
156 | if (p->chunks[i].kdata == NULL) { | |
157 | return -ENOMEM; | |
158 | } | |
159 | if (DRM_COPY_FROM_USER(p->chunks[i].kdata, | |
160 | p->chunks[i].user_ptr, size)) { | |
161 | return -EFAULT; | |
162 | } | |
e70f224c MO |
163 | if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) { |
164 | flags = p->chunks[i].kdata[0]; | |
165 | } | |
513bcb46 DA |
166 | } else { |
167 | p->chunks[i].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
168 | p->chunks[i].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
169 | if (p->chunks[i].kpage[0] == NULL || p->chunks[i].kpage[1] == NULL) { | |
170 | kfree(p->chunks[i].kpage[0]); | |
171 | kfree(p->chunks[i].kpage[1]); | |
172 | return -ENOMEM; | |
173 | } | |
174 | p->chunks[i].kpage_idx[0] = -1; | |
175 | p->chunks[i].kpage_idx[1] = -1; | |
176 | p->chunks[i].last_copied_page = -1; | |
177 | p->chunks[i].last_page_index = ((p->chunks[i].length_dw * 4) - 1) / PAGE_SIZE; | |
771fe6b9 JG |
178 | } |
179 | } | |
180 | if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) { | |
181 | DRM_ERROR("cs IB too big: %d\n", | |
182 | p->chunks[p->chunk_ib_idx].length_dw); | |
183 | return -EINVAL; | |
184 | } | |
e70f224c MO |
185 | |
186 | p->keep_tiling_flags = (flags & RADEON_CS_KEEP_TILING_FLAGS) != 0; | |
771fe6b9 JG |
187 | return 0; |
188 | } | |
189 | ||
190 | /** | |
191 | * cs_parser_fini() - clean parser states | |
192 | * @parser: parser structure holding parsing context. | |
193 | * @error: error number | |
194 | * | |
195 | * If error is set than unvalidate buffer, otherwise just free memory | |
196 | * used by parsing context. | |
197 | **/ | |
198 | static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error) | |
199 | { | |
200 | unsigned i; | |
201 | ||
147666fb TH |
202 | |
203 | if (!error && parser->ib) | |
204 | ttm_eu_fence_buffer_objects(&parser->validated, | |
205 | parser->ib->fence); | |
206 | else | |
207 | ttm_eu_backoff_reservation(&parser->validated); | |
208 | ||
fcbc451b PN |
209 | if (parser->relocs != NULL) { |
210 | for (i = 0; i < parser->nrelocs; i++) { | |
211 | if (parser->relocs[i].gobj) | |
212 | drm_gem_object_unreference_unlocked(parser->relocs[i].gobj); | |
213 | } | |
771fe6b9 | 214 | } |
48e113e5 | 215 | kfree(parser->track); |
771fe6b9 JG |
216 | kfree(parser->relocs); |
217 | kfree(parser->relocs_ptr); | |
218 | for (i = 0; i < parser->nchunks; i++) { | |
219 | kfree(parser->chunks[i].kdata); | |
513bcb46 DA |
220 | kfree(parser->chunks[i].kpage[0]); |
221 | kfree(parser->chunks[i].kpage[1]); | |
771fe6b9 JG |
222 | } |
223 | kfree(parser->chunks); | |
224 | kfree(parser->chunks_array); | |
225 | radeon_ib_free(parser->rdev, &parser->ib); | |
226 | } | |
227 | ||
228 | int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) | |
229 | { | |
230 | struct radeon_device *rdev = dev->dev_private; | |
231 | struct radeon_cs_parser parser; | |
232 | struct radeon_cs_chunk *ib_chunk; | |
233 | int r; | |
234 | ||
7a1619b9 | 235 | radeon_mutex_lock(&rdev->cs_mutex); |
771fe6b9 JG |
236 | /* initialize parser */ |
237 | memset(&parser, 0, sizeof(struct radeon_cs_parser)); | |
238 | parser.filp = filp; | |
239 | parser.rdev = rdev; | |
c8c15ff1 | 240 | parser.dev = rdev->dev; |
428c6e36 | 241 | parser.family = rdev->family; |
771fe6b9 JG |
242 | r = radeon_cs_parser_init(&parser, data); |
243 | if (r) { | |
244 | DRM_ERROR("Failed to initialize parser !\n"); | |
245 | radeon_cs_parser_fini(&parser, r); | |
7a1619b9 | 246 | radeon_mutex_unlock(&rdev->cs_mutex); |
771fe6b9 JG |
247 | return r; |
248 | } | |
69e130a6 JG |
249 | ib_chunk = &parser.chunks[parser.chunk_ib_idx]; |
250 | r = radeon_ib_get(rdev, RADEON_RING_TYPE_GFX_INDEX, &parser.ib, | |
251 | ib_chunk->length_dw * 4); | |
771fe6b9 JG |
252 | if (r) { |
253 | DRM_ERROR("Failed to get ib !\n"); | |
254 | radeon_cs_parser_fini(&parser, r); | |
7a1619b9 | 255 | radeon_mutex_unlock(&rdev->cs_mutex); |
771fe6b9 JG |
256 | return r; |
257 | } | |
258 | r = radeon_cs_parser_relocs(&parser); | |
259 | if (r) { | |
97f23b3d DA |
260 | if (r != -ERESTARTSYS) |
261 | DRM_ERROR("Failed to parse relocation %d!\n", r); | |
771fe6b9 | 262 | radeon_cs_parser_fini(&parser, r); |
7a1619b9 | 263 | radeon_mutex_unlock(&rdev->cs_mutex); |
771fe6b9 JG |
264 | return r; |
265 | } | |
266 | /* Copy the packet into the IB, the parser will read from the | |
267 | * input memory (cached) and write to the IB (which can be | |
268 | * uncached). */ | |
771fe6b9 | 269 | parser.ib->length_dw = ib_chunk->length_dw; |
771fe6b9 | 270 | r = radeon_cs_parse(&parser); |
513bcb46 DA |
271 | if (r || parser.parser_error) { |
272 | DRM_ERROR("Invalid command stream !\n"); | |
273 | radeon_cs_parser_fini(&parser, r); | |
7a1619b9 | 274 | radeon_mutex_unlock(&rdev->cs_mutex); |
513bcb46 DA |
275 | return r; |
276 | } | |
277 | r = radeon_cs_finish_pages(&parser); | |
771fe6b9 JG |
278 | if (r) { |
279 | DRM_ERROR("Invalid command stream !\n"); | |
280 | radeon_cs_parser_fini(&parser, r); | |
7a1619b9 | 281 | radeon_mutex_unlock(&rdev->cs_mutex); |
771fe6b9 JG |
282 | return r; |
283 | } | |
284 | r = radeon_ib_schedule(rdev, parser.ib); | |
285 | if (r) { | |
426d3107 | 286 | DRM_ERROR("Failed to schedule IB !\n"); |
771fe6b9 JG |
287 | } |
288 | radeon_cs_parser_fini(&parser, r); | |
7a1619b9 | 289 | radeon_mutex_unlock(&rdev->cs_mutex); |
771fe6b9 JG |
290 | return r; |
291 | } | |
513bcb46 DA |
292 | |
293 | int radeon_cs_finish_pages(struct radeon_cs_parser *p) | |
294 | { | |
295 | struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx]; | |
296 | int i; | |
297 | int size = PAGE_SIZE; | |
298 | ||
299 | for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) { | |
300 | if (i == ibc->last_page_index) { | |
301 | size = (ibc->length_dw * 4) % PAGE_SIZE; | |
302 | if (size == 0) | |
303 | size = PAGE_SIZE; | |
304 | } | |
305 | ||
306 | if (DRM_COPY_FROM_USER(p->ib->ptr + (i * (PAGE_SIZE/4)), | |
307 | ibc->user_ptr + (i * PAGE_SIZE), | |
308 | size)) | |
309 | return -EFAULT; | |
310 | } | |
311 | return 0; | |
312 | } | |
313 | ||
314 | int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx) | |
315 | { | |
316 | int new_page; | |
513bcb46 DA |
317 | struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx]; |
318 | int i; | |
319 | int size = PAGE_SIZE; | |
320 | ||
c5e617e2 | 321 | for (i = ibc->last_copied_page + 1; i < pg_idx; i++) { |
513bcb46 DA |
322 | if (DRM_COPY_FROM_USER(p->ib->ptr + (i * (PAGE_SIZE/4)), |
323 | ibc->user_ptr + (i * PAGE_SIZE), | |
324 | PAGE_SIZE)) { | |
325 | p->parser_error = -EFAULT; | |
326 | return 0; | |
327 | } | |
328 | } | |
329 | ||
330 | new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1; | |
331 | ||
332 | if (pg_idx == ibc->last_page_index) { | |
333 | size = (ibc->length_dw * 4) % PAGE_SIZE; | |
334 | if (size == 0) | |
335 | size = PAGE_SIZE; | |
336 | } | |
337 | ||
338 | if (DRM_COPY_FROM_USER(ibc->kpage[new_page], | |
339 | ibc->user_ptr + (pg_idx * PAGE_SIZE), | |
340 | size)) { | |
341 | p->parser_error = -EFAULT; | |
342 | return 0; | |
343 | } | |
344 | ||
345 | /* copy to IB here */ | |
346 | memcpy((void *)(p->ib->ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size); | |
347 | ||
348 | ibc->last_copied_page = pg_idx; | |
349 | ibc->kpage_idx[new_page] = pg_idx; | |
350 | ||
351 | return new_page; | |
352 | } |