blob: 5cbe948ef16e6f9e0d070acc5025ecfe526553d3 [file] [log] [blame]
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the 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 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Alex Deucher40e2a5c2010-06-04 18:41:42 -040028#include <linux/kernel.h>
Jerome Glisse3ce0a232009-09-08 10:10:24 +100029#include "drmP.h"
30#include "radeon.h"
Jerome Glisse3ce0a232009-09-08 10:10:24 +100031#include "r600d.h"
Jerome Glisse961fb592010-02-10 22:30:05 +000032#include "r600_reg_safe.h"
Jerome Glisse3ce0a232009-09-08 10:10:24 +100033
34static int r600_cs_packet_next_reloc_mm(struct radeon_cs_parser *p,
35 struct radeon_cs_reloc **cs_reloc);
36static int r600_cs_packet_next_reloc_nomm(struct radeon_cs_parser *p,
37 struct radeon_cs_reloc **cs_reloc);
38typedef int (*next_reloc_t)(struct radeon_cs_parser*, struct radeon_cs_reloc**);
39static next_reloc_t r600_cs_packet_next_reloc = &r600_cs_packet_next_reloc_mm;
Jerome Glisse961fb592010-02-10 22:30:05 +000040extern void r600_cs_legacy_get_tiling_conf(struct drm_device *dev, u32 *npipes, u32 *nbanks, u32 *group_size);
41
Jerome Glisse3ce0a232009-09-08 10:10:24 +100042
Jerome Glissec8c15ff2010-01-18 13:01:36 +010043struct r600_cs_track {
Jerome Glisse961fb592010-02-10 22:30:05 +000044 /* configuration we miror so that we use same code btw kms/ums */
45 u32 group_size;
46 u32 nbanks;
47 u32 npipes;
48 /* value we track */
Alex Deucher5f77df32010-03-26 14:52:32 -040049 u32 sq_config;
Jerome Glisse961fb592010-02-10 22:30:05 +000050 u32 nsamples;
51 u32 cb_color_base_last[8];
52 struct radeon_bo *cb_color_bo[8];
Alex Deucher16790562010-11-14 20:24:35 -050053 u64 cb_color_bo_mc[8];
Jerome Glisse961fb592010-02-10 22:30:05 +000054 u32 cb_color_bo_offset[8];
55 struct radeon_bo *cb_color_frag_bo[8];
56 struct radeon_bo *cb_color_tile_bo[8];
57 u32 cb_color_info[8];
Jerome Glisse285484e2011-12-16 17:03:42 -050058 u32 cb_color_view[8];
Jerome Glisse961fb592010-02-10 22:30:05 +000059 u32 cb_color_size_idx[8];
60 u32 cb_target_mask;
61 u32 cb_shader_mask;
62 u32 cb_color_size[8];
63 u32 vgt_strmout_en;
64 u32 vgt_strmout_buffer_en;
Marek Olšákdd220a02012-01-27 12:17:59 -050065 struct radeon_bo *vgt_strmout_bo[4];
66 u64 vgt_strmout_bo_mc[4];
67 u32 vgt_strmout_bo_offset[4];
68 u32 vgt_strmout_size[4];
Jerome Glisse961fb592010-02-10 22:30:05 +000069 u32 db_depth_control;
70 u32 db_depth_info;
71 u32 db_depth_size_idx;
72 u32 db_depth_view;
73 u32 db_depth_size;
74 u32 db_offset;
75 struct radeon_bo *db_bo;
Alex Deucher16790562010-11-14 20:24:35 -050076 u64 db_bo_mc;
Jerome Glissec8c15ff2010-01-18 13:01:36 +010077};
78
Marek Olšákfe6f0bd2011-05-07 01:09:57 +020079#define FMT_8_BIT(fmt, vc) [fmt] = { 1, 1, 1, vc, CHIP_R600 }
80#define FMT_16_BIT(fmt, vc) [fmt] = { 1, 1, 2, vc, CHIP_R600 }
Jerome Glisse285484e2011-12-16 17:03:42 -050081#define FMT_24_BIT(fmt) [fmt] = { 1, 1, 4, 0, CHIP_R600 }
Marek Olšákfe6f0bd2011-05-07 01:09:57 +020082#define FMT_32_BIT(fmt, vc) [fmt] = { 1, 1, 4, vc, CHIP_R600 }
Jerome Glisse285484e2011-12-16 17:03:42 -050083#define FMT_48_BIT(fmt) [fmt] = { 1, 1, 8, 0, CHIP_R600 }
Marek Olšákfe6f0bd2011-05-07 01:09:57 +020084#define FMT_64_BIT(fmt, vc) [fmt] = { 1, 1, 8, vc, CHIP_R600 }
85#define FMT_96_BIT(fmt) [fmt] = { 1, 1, 12, 0, CHIP_R600 }
86#define FMT_128_BIT(fmt, vc) [fmt] = { 1, 1, 16,vc, CHIP_R600 }
Dave Airlie60b212f2011-02-18 05:51:58 +000087
88struct gpu_formats {
89 unsigned blockwidth;
90 unsigned blockheight;
91 unsigned blocksize;
92 unsigned valid_color;
Marek Olšákfe6f0bd2011-05-07 01:09:57 +020093 enum radeon_family min_family;
Dave Airlie60b212f2011-02-18 05:51:58 +000094};
95
96static const struct gpu_formats color_formats_table[] = {
97 /* 8 bit */
98 FMT_8_BIT(V_038004_COLOR_8, 1),
99 FMT_8_BIT(V_038004_COLOR_4_4, 1),
100 FMT_8_BIT(V_038004_COLOR_3_3_2, 1),
101 FMT_8_BIT(V_038004_FMT_1, 0),
102
103 /* 16-bit */
104 FMT_16_BIT(V_038004_COLOR_16, 1),
105 FMT_16_BIT(V_038004_COLOR_16_FLOAT, 1),
106 FMT_16_BIT(V_038004_COLOR_8_8, 1),
107 FMT_16_BIT(V_038004_COLOR_5_6_5, 1),
108 FMT_16_BIT(V_038004_COLOR_6_5_5, 1),
109 FMT_16_BIT(V_038004_COLOR_1_5_5_5, 1),
110 FMT_16_BIT(V_038004_COLOR_4_4_4_4, 1),
111 FMT_16_BIT(V_038004_COLOR_5_5_5_1, 1),
112
113 /* 24-bit */
114 FMT_24_BIT(V_038004_FMT_8_8_8),
Jerome Glisse285484e2011-12-16 17:03:42 -0500115
Dave Airlie60b212f2011-02-18 05:51:58 +0000116 /* 32-bit */
117 FMT_32_BIT(V_038004_COLOR_32, 1),
118 FMT_32_BIT(V_038004_COLOR_32_FLOAT, 1),
119 FMT_32_BIT(V_038004_COLOR_16_16, 1),
120 FMT_32_BIT(V_038004_COLOR_16_16_FLOAT, 1),
121 FMT_32_BIT(V_038004_COLOR_8_24, 1),
122 FMT_32_BIT(V_038004_COLOR_8_24_FLOAT, 1),
123 FMT_32_BIT(V_038004_COLOR_24_8, 1),
124 FMT_32_BIT(V_038004_COLOR_24_8_FLOAT, 1),
125 FMT_32_BIT(V_038004_COLOR_10_11_11, 1),
126 FMT_32_BIT(V_038004_COLOR_10_11_11_FLOAT, 1),
127 FMT_32_BIT(V_038004_COLOR_11_11_10, 1),
128 FMT_32_BIT(V_038004_COLOR_11_11_10_FLOAT, 1),
129 FMT_32_BIT(V_038004_COLOR_2_10_10_10, 1),
130 FMT_32_BIT(V_038004_COLOR_8_8_8_8, 1),
131 FMT_32_BIT(V_038004_COLOR_10_10_10_2, 1),
132 FMT_32_BIT(V_038004_FMT_5_9_9_9_SHAREDEXP, 0),
133 FMT_32_BIT(V_038004_FMT_32_AS_8, 0),
134 FMT_32_BIT(V_038004_FMT_32_AS_8_8, 0),
135
136 /* 48-bit */
137 FMT_48_BIT(V_038004_FMT_16_16_16),
138 FMT_48_BIT(V_038004_FMT_16_16_16_FLOAT),
139
140 /* 64-bit */
141 FMT_64_BIT(V_038004_COLOR_X24_8_32_FLOAT, 1),
142 FMT_64_BIT(V_038004_COLOR_32_32, 1),
143 FMT_64_BIT(V_038004_COLOR_32_32_FLOAT, 1),
144 FMT_64_BIT(V_038004_COLOR_16_16_16_16, 1),
145 FMT_64_BIT(V_038004_COLOR_16_16_16_16_FLOAT, 1),
146
147 FMT_96_BIT(V_038004_FMT_32_32_32),
148 FMT_96_BIT(V_038004_FMT_32_32_32_FLOAT),
149
150 /* 128-bit */
151 FMT_128_BIT(V_038004_COLOR_32_32_32_32, 1),
152 FMT_128_BIT(V_038004_COLOR_32_32_32_32_FLOAT, 1),
153
154 [V_038004_FMT_GB_GR] = { 2, 1, 4, 0 },
155 [V_038004_FMT_BG_RG] = { 2, 1, 4, 0 },
156
157 /* block compressed formats */
158 [V_038004_FMT_BC1] = { 4, 4, 8, 0 },
159 [V_038004_FMT_BC2] = { 4, 4, 16, 0 },
160 [V_038004_FMT_BC3] = { 4, 4, 16, 0 },
161 [V_038004_FMT_BC4] = { 4, 4, 8, 0 },
162 [V_038004_FMT_BC5] = { 4, 4, 16, 0},
Marek Olšákfe6f0bd2011-05-07 01:09:57 +0200163 [V_038004_FMT_BC6] = { 4, 4, 16, 0, CHIP_CEDAR}, /* Evergreen-only */
164 [V_038004_FMT_BC7] = { 4, 4, 16, 0, CHIP_CEDAR}, /* Evergreen-only */
Dave Airlie60b212f2011-02-18 05:51:58 +0000165
Marek Olšákfe6f0bd2011-05-07 01:09:57 +0200166 /* The other Evergreen formats */
167 [V_038004_FMT_32_AS_32_32_32_32] = { 1, 1, 4, 0, CHIP_CEDAR},
Dave Airlie60b212f2011-02-18 05:51:58 +0000168};
169
Jerome Glisse285484e2011-12-16 17:03:42 -0500170bool r600_fmt_is_valid_color(u32 format)
Dave Airlie60b212f2011-02-18 05:51:58 +0000171{
Dan Carpentercf8a47d2011-02-26 04:48:18 +0300172 if (format >= ARRAY_SIZE(color_formats_table))
Dave Airlie60b212f2011-02-18 05:51:58 +0000173 return false;
Jerome Glisse285484e2011-12-16 17:03:42 -0500174
Dave Airlie60b212f2011-02-18 05:51:58 +0000175 if (color_formats_table[format].valid_color)
176 return true;
177
178 return false;
179}
180
Jerome Glisse285484e2011-12-16 17:03:42 -0500181bool r600_fmt_is_valid_texture(u32 format, enum radeon_family family)
Dave Airlie60b212f2011-02-18 05:51:58 +0000182{
Dan Carpentercf8a47d2011-02-26 04:48:18 +0300183 if (format >= ARRAY_SIZE(color_formats_table))
Dave Airlie60b212f2011-02-18 05:51:58 +0000184 return false;
Jerome Glisse285484e2011-12-16 17:03:42 -0500185
Marek Olšákfe6f0bd2011-05-07 01:09:57 +0200186 if (family < color_formats_table[format].min_family)
187 return false;
188
Dave Airlie60b212f2011-02-18 05:51:58 +0000189 if (color_formats_table[format].blockwidth > 0)
190 return true;
191
192 return false;
193}
194
Jerome Glisse285484e2011-12-16 17:03:42 -0500195int r600_fmt_get_blocksize(u32 format)
Dave Airlie60b212f2011-02-18 05:51:58 +0000196{
Dan Carpentercf8a47d2011-02-26 04:48:18 +0300197 if (format >= ARRAY_SIZE(color_formats_table))
Dave Airlie60b212f2011-02-18 05:51:58 +0000198 return 0;
199
200 return color_formats_table[format].blocksize;
201}
202
Jerome Glisse285484e2011-12-16 17:03:42 -0500203int r600_fmt_get_nblocksx(u32 format, u32 w)
Dave Airlie60b212f2011-02-18 05:51:58 +0000204{
205 unsigned bw;
Dan Carpentercf8a47d2011-02-26 04:48:18 +0300206
207 if (format >= ARRAY_SIZE(color_formats_table))
Dave Airlie60b212f2011-02-18 05:51:58 +0000208 return 0;
209
210 bw = color_formats_table[format].blockwidth;
211 if (bw == 0)
212 return 0;
213
214 return (w + bw - 1) / bw;
215}
216
Jerome Glisse285484e2011-12-16 17:03:42 -0500217int r600_fmt_get_nblocksy(u32 format, u32 h)
Dave Airlie60b212f2011-02-18 05:51:58 +0000218{
219 unsigned bh;
Dan Carpentercf8a47d2011-02-26 04:48:18 +0300220
221 if (format >= ARRAY_SIZE(color_formats_table))
Dave Airlie60b212f2011-02-18 05:51:58 +0000222 return 0;
223
224 bh = color_formats_table[format].blockheight;
225 if (bh == 0)
226 return 0;
227
228 return (h + bh - 1) / bh;
229}
230
Alex Deucher16790562010-11-14 20:24:35 -0500231struct array_mode_checker {
232 int array_mode;
233 u32 group_size;
234 u32 nbanks;
235 u32 npipes;
236 u32 nsamples;
Dave Airlie60b212f2011-02-18 05:51:58 +0000237 u32 blocksize;
Alex Deucher16790562010-11-14 20:24:35 -0500238};
239
240/* returns alignment in pixels for pitch/height/depth and bytes for base */
Andi Kleen488479e2011-10-13 16:08:41 -0700241static int r600_get_array_mode_alignment(struct array_mode_checker *values,
Alex Deucher16790562010-11-14 20:24:35 -0500242 u32 *pitch_align,
243 u32 *height_align,
244 u32 *depth_align,
245 u64 *base_align)
246{
247 u32 tile_width = 8;
248 u32 tile_height = 8;
249 u32 macro_tile_width = values->nbanks;
250 u32 macro_tile_height = values->npipes;
Dave Airlie60b212f2011-02-18 05:51:58 +0000251 u32 tile_bytes = tile_width * tile_height * values->blocksize * values->nsamples;
Alex Deucher16790562010-11-14 20:24:35 -0500252 u32 macro_tile_bytes = macro_tile_width * macro_tile_height * tile_bytes;
253
254 switch (values->array_mode) {
255 case ARRAY_LINEAR_GENERAL:
256 /* technically tile_width/_height for pitch/height */
257 *pitch_align = 1; /* tile_width */
258 *height_align = 1; /* tile_height */
259 *depth_align = 1;
260 *base_align = 1;
261 break;
262 case ARRAY_LINEAR_ALIGNED:
Dave Airlie60b212f2011-02-18 05:51:58 +0000263 *pitch_align = max((u32)64, (u32)(values->group_size / values->blocksize));
Jerome Glisse285484e2011-12-16 17:03:42 -0500264 *height_align = 1;
Alex Deucher16790562010-11-14 20:24:35 -0500265 *depth_align = 1;
266 *base_align = values->group_size;
267 break;
268 case ARRAY_1D_TILED_THIN1:
269 *pitch_align = max((u32)tile_width,
270 (u32)(values->group_size /
Dave Airlie60b212f2011-02-18 05:51:58 +0000271 (tile_height * values->blocksize * values->nsamples)));
Alex Deucher16790562010-11-14 20:24:35 -0500272 *height_align = tile_height;
273 *depth_align = 1;
274 *base_align = values->group_size;
275 break;
276 case ARRAY_2D_TILED_THIN1:
Jerome Glisse285484e2011-12-16 17:03:42 -0500277 *pitch_align = max((u32)macro_tile_width * tile_width,
278 (u32)((values->group_size * values->nbanks) /
279 (values->blocksize * values->nsamples * tile_width)));
Alex Deucher16790562010-11-14 20:24:35 -0500280 *height_align = macro_tile_height * tile_height;
281 *depth_align = 1;
282 *base_align = max(macro_tile_bytes,
Dave Airlie60b212f2011-02-18 05:51:58 +0000283 (*pitch_align) * values->blocksize * (*height_align) * values->nsamples);
Alex Deucher16790562010-11-14 20:24:35 -0500284 break;
285 default:
286 return -EINVAL;
287 }
288
289 return 0;
290}
291
Jerome Glisse961fb592010-02-10 22:30:05 +0000292static void r600_cs_track_init(struct r600_cs_track *track)
293{
294 int i;
295
Alex Deucher5f77df32010-03-26 14:52:32 -0400296 /* assume DX9 mode */
297 track->sq_config = DX9_CONSTS;
Jerome Glisse961fb592010-02-10 22:30:05 +0000298 for (i = 0; i < 8; i++) {
299 track->cb_color_base_last[i] = 0;
300 track->cb_color_size[i] = 0;
301 track->cb_color_size_idx[i] = 0;
302 track->cb_color_info[i] = 0;
Jerome Glisse285484e2011-12-16 17:03:42 -0500303 track->cb_color_view[i] = 0xFFFFFFFF;
Jerome Glisse961fb592010-02-10 22:30:05 +0000304 track->cb_color_bo[i] = NULL;
305 track->cb_color_bo_offset[i] = 0xFFFFFFFF;
Alex Deucher16790562010-11-14 20:24:35 -0500306 track->cb_color_bo_mc[i] = 0xFFFFFFFF;
Jerome Glisse961fb592010-02-10 22:30:05 +0000307 }
308 track->cb_target_mask = 0xFFFFFFFF;
309 track->cb_shader_mask = 0xFFFFFFFF;
310 track->db_bo = NULL;
Alex Deucher16790562010-11-14 20:24:35 -0500311 track->db_bo_mc = 0xFFFFFFFF;
Jerome Glisse961fb592010-02-10 22:30:05 +0000312 /* assume the biggest format and that htile is enabled */
313 track->db_depth_info = 7 | (1 << 25);
314 track->db_depth_view = 0xFFFFC000;
315 track->db_depth_size = 0xFFFFFFFF;
316 track->db_depth_size_idx = 0;
317 track->db_depth_control = 0xFFFFFFFF;
Marek Olšákdd220a02012-01-27 12:17:59 -0500318
319 for (i = 0; i < 4; i++) {
320 track->vgt_strmout_size[i] = 0;
321 track->vgt_strmout_bo[i] = NULL;
322 track->vgt_strmout_bo_offset[i] = 0xFFFFFFFF;
323 track->vgt_strmout_bo_mc[i] = 0xFFFFFFFF;
324 }
Jerome Glisse961fb592010-02-10 22:30:05 +0000325}
326
Andi Kleen488479e2011-10-13 16:08:41 -0700327static int r600_cs_track_validate_cb(struct radeon_cs_parser *p, int i)
Jerome Glisse961fb592010-02-10 22:30:05 +0000328{
329 struct r600_cs_track *track = p->track;
Dave Airlie60b212f2011-02-18 05:51:58 +0000330 u32 slice_tile_max, size, tmp;
Alex Deucher16790562010-11-14 20:24:35 -0500331 u32 height, height_align, pitch, pitch_align, depth_align;
332 u64 base_offset, base_align;
333 struct array_mode_checker array_check;
Jerome Glisse961fb592010-02-10 22:30:05 +0000334 volatile u32 *ib = p->ib->ptr;
Dave Airlief30df2f2010-10-21 13:55:40 +1000335 unsigned array_mode;
Dave Airlie60b212f2011-02-18 05:51:58 +0000336 u32 format;
Jerome Glisse285484e2011-12-16 17:03:42 -0500337
Jerome Glisse961fb592010-02-10 22:30:05 +0000338 if (G_0280A0_TILE_MODE(track->cb_color_info[i])) {
339 dev_warn(p->dev, "FMASK or CMASK buffer are not supported by this kernel\n");
340 return -EINVAL;
341 }
Alex Deucher1729dd32010-08-06 02:54:05 -0400342 size = radeon_bo_size(track->cb_color_bo[i]) - track->cb_color_bo_offset[i];
Dave Airlie60b212f2011-02-18 05:51:58 +0000343 format = G_0280A0_FORMAT(track->cb_color_info[i]);
Jerome Glisse285484e2011-12-16 17:03:42 -0500344 if (!r600_fmt_is_valid_color(format)) {
Jerome Glisse961fb592010-02-10 22:30:05 +0000345 dev_warn(p->dev, "%s:%d cb invalid format %d for %d (0x%08X)\n",
Dave Airlie60b212f2011-02-18 05:51:58 +0000346 __func__, __LINE__, format,
Jerome Glisse961fb592010-02-10 22:30:05 +0000347 i, track->cb_color_info[i]);
348 return -EINVAL;
349 }
Alex Deucher16790562010-11-14 20:24:35 -0500350 /* pitch in pixels */
351 pitch = (G_028060_PITCH_TILE_MAX(track->cb_color_size[i]) + 1) * 8;
Jerome Glisse961fb592010-02-10 22:30:05 +0000352 slice_tile_max = G_028060_SLICE_TILE_MAX(track->cb_color_size[i]) + 1;
Dave Airlief30df2f2010-10-21 13:55:40 +1000353 slice_tile_max *= 64;
Alex Deucher16790562010-11-14 20:24:35 -0500354 height = slice_tile_max / pitch;
Jerome Glisse961fb592010-02-10 22:30:05 +0000355 if (height > 8192)
356 height = 8192;
Dave Airlief30df2f2010-10-21 13:55:40 +1000357 array_mode = G_0280A0_ARRAY_MODE(track->cb_color_info[i]);
Alex Deucher16790562010-11-14 20:24:35 -0500358
359 base_offset = track->cb_color_bo_mc[i] + track->cb_color_bo_offset[i];
360 array_check.array_mode = array_mode;
361 array_check.group_size = track->group_size;
362 array_check.nbanks = track->nbanks;
363 array_check.npipes = track->npipes;
364 array_check.nsamples = track->nsamples;
Jerome Glisse285484e2011-12-16 17:03:42 -0500365 array_check.blocksize = r600_fmt_get_blocksize(format);
Alex Deucher16790562010-11-14 20:24:35 -0500366 if (r600_get_array_mode_alignment(&array_check,
367 &pitch_align, &height_align, &depth_align, &base_align)) {
368 dev_warn(p->dev, "%s invalid tiling %d for %d (0x%08X)\n", __func__,
369 G_0280A0_ARRAY_MODE(track->cb_color_info[i]), i,
370 track->cb_color_info[i]);
371 return -EINVAL;
372 }
Dave Airlief30df2f2010-10-21 13:55:40 +1000373 switch (array_mode) {
Jerome Glisse961fb592010-02-10 22:30:05 +0000374 case V_0280A0_ARRAY_LINEAR_GENERAL:
Alex Deucher40e2a5c2010-06-04 18:41:42 -0400375 break;
Jerome Glisse961fb592010-02-10 22:30:05 +0000376 case V_0280A0_ARRAY_LINEAR_ALIGNED:
Jerome Glisse961fb592010-02-10 22:30:05 +0000377 break;
378 case V_0280A0_ARRAY_1D_TILED_THIN1:
Alex Deucher8f895da2010-10-26 20:22:42 -0400379 /* avoid breaking userspace */
380 if (height > 7)
381 height &= ~0x7;
Jerome Glisse961fb592010-02-10 22:30:05 +0000382 break;
383 case V_0280A0_ARRAY_2D_TILED_THIN1:
Jerome Glisse961fb592010-02-10 22:30:05 +0000384 break;
385 default:
386 dev_warn(p->dev, "%s invalid tiling %d for %d (0x%08X)\n", __func__,
387 G_0280A0_ARRAY_MODE(track->cb_color_info[i]), i,
388 track->cb_color_info[i]);
389 return -EINVAL;
390 }
Alex Deucher16790562010-11-14 20:24:35 -0500391
392 if (!IS_ALIGNED(pitch, pitch_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -0500393 dev_warn(p->dev, "%s:%d cb pitch (%d, 0x%x, %d) invalid\n",
394 __func__, __LINE__, pitch, pitch_align, array_mode);
Alex Deucher16790562010-11-14 20:24:35 -0500395 return -EINVAL;
396 }
397 if (!IS_ALIGNED(height, height_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -0500398 dev_warn(p->dev, "%s:%d cb height (%d, 0x%x, %d) invalid\n",
399 __func__, __LINE__, height, height_align, array_mode);
Alex Deucher16790562010-11-14 20:24:35 -0500400 return -EINVAL;
401 }
402 if (!IS_ALIGNED(base_offset, base_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -0500403 dev_warn(p->dev, "%s offset[%d] 0x%llx 0x%llx, %d not aligned\n", __func__, i,
404 base_offset, base_align, array_mode);
Alex Deucher16790562010-11-14 20:24:35 -0500405 return -EINVAL;
406 }
407
Jerome Glisse961fb592010-02-10 22:30:05 +0000408 /* check offset */
Jerome Glisse285484e2011-12-16 17:03:42 -0500409 tmp = r600_fmt_get_nblocksy(format, height) * r600_fmt_get_nblocksx(format, pitch) * r600_fmt_get_blocksize(format);
410 switch (array_mode) {
411 default:
412 case V_0280A0_ARRAY_LINEAR_GENERAL:
413 case V_0280A0_ARRAY_LINEAR_ALIGNED:
414 tmp += track->cb_color_view[i] & 0xFF;
415 break;
416 case V_0280A0_ARRAY_1D_TILED_THIN1:
417 case V_0280A0_ARRAY_2D_TILED_THIN1:
418 tmp += G_028080_SLICE_MAX(track->cb_color_view[i]) * tmp;
419 break;
420 }
Jerome Glisse961fb592010-02-10 22:30:05 +0000421 if ((tmp + track->cb_color_bo_offset[i]) > radeon_bo_size(track->cb_color_bo[i])) {
Dave Airlief30df2f2010-10-21 13:55:40 +1000422 if (array_mode == V_0280A0_ARRAY_LINEAR_GENERAL) {
423 /* the initial DDX does bad things with the CB size occasionally */
424 /* it rounds up height too far for slice tile max but the BO is smaller */
Alex Deuchera1a82132010-12-13 14:03:09 -0500425 /* r600c,g also seem to flush at bad times in some apps resulting in
426 * bogus values here. So for linear just allow anything to avoid breaking
427 * broken userspace.
428 */
Dave Airlief30df2f2010-10-21 13:55:40 +1000429 } else {
Jerome Glisse285484e2011-12-16 17:03:42 -0500430 dev_warn(p->dev, "%s offset[%d] %d %d %d %lu too big (%d %d) (%d %d %d)\n",
431 __func__, i, array_mode,
Alex Deucherc2049b32011-02-13 18:42:41 -0500432 track->cb_color_bo_offset[i], tmp,
Jerome Glisse285484e2011-12-16 17:03:42 -0500433 radeon_bo_size(track->cb_color_bo[i]),
434 pitch, height, r600_fmt_get_nblocksx(format, pitch),
435 r600_fmt_get_nblocksy(format, height),
436 r600_fmt_get_blocksize(format));
Dave Airlief30df2f2010-10-21 13:55:40 +1000437 return -EINVAL;
438 }
Alex Deucher40e2a5c2010-06-04 18:41:42 -0400439 }
Jerome Glisse961fb592010-02-10 22:30:05 +0000440 /* limit max tile */
Alex Deucher16790562010-11-14 20:24:35 -0500441 tmp = (height * pitch) >> 6;
Jerome Glisse961fb592010-02-10 22:30:05 +0000442 if (tmp < slice_tile_max)
443 slice_tile_max = tmp;
Alex Deucher16790562010-11-14 20:24:35 -0500444 tmp = S_028060_PITCH_TILE_MAX((pitch / 8) - 1) |
Jerome Glisse961fb592010-02-10 22:30:05 +0000445 S_028060_SLICE_TILE_MAX(slice_tile_max - 1);
446 ib[track->cb_color_size_idx[i]] = tmp;
447 return 0;
448}
449
450static int r600_cs_track_check(struct radeon_cs_parser *p)
451{
452 struct r600_cs_track *track = p->track;
453 u32 tmp;
454 int r, i;
455 volatile u32 *ib = p->ib->ptr;
456
457 /* on legacy kernel we don't perform advanced check */
458 if (p->rdev == NULL)
459 return 0;
Marek Olšákdd220a02012-01-27 12:17:59 -0500460
461 /* check streamout */
462 if (track->vgt_strmout_en) {
463 for (i = 0; i < 4; i++) {
464 if (track->vgt_strmout_buffer_en & (1 << i)) {
465 if (track->vgt_strmout_bo[i]) {
466 u64 offset = (u64)track->vgt_strmout_bo_offset[i] +
467 (u64)track->vgt_strmout_size[i];
468 if (offset > radeon_bo_size(track->vgt_strmout_bo[i])) {
469 DRM_ERROR("streamout %d bo too small: 0x%llx, 0x%lx\n",
470 i, offset,
471 radeon_bo_size(track->vgt_strmout_bo[i]));
472 return -EINVAL;
473 }
474 } else {
475 dev_warn(p->dev, "No buffer for streamout %d\n", i);
476 return -EINVAL;
477 }
478 }
479 }
Jerome Glisse961fb592010-02-10 22:30:05 +0000480 }
Marek Olšákdd220a02012-01-27 12:17:59 -0500481
Jerome Glisse961fb592010-02-10 22:30:05 +0000482 /* check that we have a cb for each enabled target, we don't check
483 * shader_mask because it seems mesa isn't always setting it :(
484 */
485 tmp = track->cb_target_mask;
486 for (i = 0; i < 8; i++) {
487 if ((tmp >> (i * 4)) & 0xF) {
488 /* at least one component is enabled */
489 if (track->cb_color_bo[i] == NULL) {
490 dev_warn(p->dev, "%s:%d mask 0x%08X | 0x%08X no cb for %d\n",
491 __func__, __LINE__, track->cb_target_mask, track->cb_shader_mask, i);
492 return -EINVAL;
493 }
494 /* perform rewrite of CB_COLOR[0-7]_SIZE */
495 r = r600_cs_track_validate_cb(p, i);
496 if (r)
497 return r;
498 }
499 }
500 /* Check depth buffer */
501 if (G_028800_STENCIL_ENABLE(track->db_depth_control) ||
502 G_028800_Z_ENABLE(track->db_depth_control)) {
Alex Deucher16790562010-11-14 20:24:35 -0500503 u32 nviews, bpe, ntiles, size, slice_tile_max;
504 u32 height, height_align, pitch, pitch_align, depth_align;
505 u64 base_offset, base_align;
506 struct array_mode_checker array_check;
507 int array_mode;
508
Jerome Glisse961fb592010-02-10 22:30:05 +0000509 if (track->db_bo == NULL) {
510 dev_warn(p->dev, "z/stencil with no depth buffer\n");
511 return -EINVAL;
512 }
513 if (G_028010_TILE_SURFACE_ENABLE(track->db_depth_info)) {
514 dev_warn(p->dev, "this kernel doesn't support z/stencil htile\n");
515 return -EINVAL;
516 }
517 switch (G_028010_FORMAT(track->db_depth_info)) {
518 case V_028010_DEPTH_16:
519 bpe = 2;
520 break;
521 case V_028010_DEPTH_X8_24:
522 case V_028010_DEPTH_8_24:
523 case V_028010_DEPTH_X8_24_FLOAT:
524 case V_028010_DEPTH_8_24_FLOAT:
525 case V_028010_DEPTH_32_FLOAT:
526 bpe = 4;
527 break;
528 case V_028010_DEPTH_X24_8_32_FLOAT:
529 bpe = 8;
530 break;
531 default:
532 dev_warn(p->dev, "z/stencil with invalid format %d\n", G_028010_FORMAT(track->db_depth_info));
533 return -EINVAL;
534 }
535 if ((track->db_depth_size & 0xFFFFFC00) == 0xFFFFFC00) {
536 if (!track->db_depth_size_idx) {
537 dev_warn(p->dev, "z/stencil buffer size not set\n");
538 return -EINVAL;
539 }
Jerome Glisse961fb592010-02-10 22:30:05 +0000540 tmp = radeon_bo_size(track->db_bo) - track->db_offset;
541 tmp = (tmp / bpe) >> 6;
542 if (!tmp) {
543 dev_warn(p->dev, "z/stencil buffer too small (0x%08X %d %d %ld)\n",
544 track->db_depth_size, bpe, track->db_offset,
545 radeon_bo_size(track->db_bo));
546 return -EINVAL;
547 }
548 ib[track->db_depth_size_idx] = S_028000_SLICE_TILE_MAX(tmp - 1) | (track->db_depth_size & 0x3FF);
549 } else {
Alex Deucher40e2a5c2010-06-04 18:41:42 -0400550 size = radeon_bo_size(track->db_bo);
Alex Deucher16790562010-11-14 20:24:35 -0500551 /* pitch in pixels */
552 pitch = (G_028000_PITCH_TILE_MAX(track->db_depth_size) + 1) * 8;
Alex Deucher2c7d81a2010-10-27 01:44:35 -0400553 slice_tile_max = G_028000_SLICE_TILE_MAX(track->db_depth_size) + 1;
554 slice_tile_max *= 64;
Alex Deucher16790562010-11-14 20:24:35 -0500555 height = slice_tile_max / pitch;
Alex Deucher2c7d81a2010-10-27 01:44:35 -0400556 if (height > 8192)
557 height = 8192;
Alex Deucher16790562010-11-14 20:24:35 -0500558 base_offset = track->db_bo_mc + track->db_offset;
559 array_mode = G_028010_ARRAY_MODE(track->db_depth_info);
560 array_check.array_mode = array_mode;
561 array_check.group_size = track->group_size;
562 array_check.nbanks = track->nbanks;
563 array_check.npipes = track->npipes;
564 array_check.nsamples = track->nsamples;
Dave Airlie60b212f2011-02-18 05:51:58 +0000565 array_check.blocksize = bpe;
Alex Deucher16790562010-11-14 20:24:35 -0500566 if (r600_get_array_mode_alignment(&array_check,
567 &pitch_align, &height_align, &depth_align, &base_align)) {
568 dev_warn(p->dev, "%s invalid tiling %d (0x%08X)\n", __func__,
569 G_028010_ARRAY_MODE(track->db_depth_info),
570 track->db_depth_info);
571 return -EINVAL;
572 }
573 switch (array_mode) {
Alex Deucher40e2a5c2010-06-04 18:41:42 -0400574 case V_028010_ARRAY_1D_TILED_THIN1:
Alex Deucher2c7d81a2010-10-27 01:44:35 -0400575 /* don't break userspace */
576 height &= ~0x7;
Alex Deucher40e2a5c2010-06-04 18:41:42 -0400577 break;
578 case V_028010_ARRAY_2D_TILED_THIN1:
Alex Deucher40e2a5c2010-06-04 18:41:42 -0400579 break;
580 default:
581 dev_warn(p->dev, "%s invalid tiling %d (0x%08X)\n", __func__,
582 G_028010_ARRAY_MODE(track->db_depth_info),
583 track->db_depth_info);
584 return -EINVAL;
585 }
Alex Deucher16790562010-11-14 20:24:35 -0500586
587 if (!IS_ALIGNED(pitch, pitch_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -0500588 dev_warn(p->dev, "%s:%d db pitch (%d, 0x%x, %d) invalid\n",
589 __func__, __LINE__, pitch, pitch_align, array_mode);
Alex Deucher40e2a5c2010-06-04 18:41:42 -0400590 return -EINVAL;
591 }
Alex Deucher16790562010-11-14 20:24:35 -0500592 if (!IS_ALIGNED(height, height_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -0500593 dev_warn(p->dev, "%s:%d db height (%d, 0x%x, %d) invalid\n",
594 __func__, __LINE__, height, height_align, array_mode);
Alex Deucher16790562010-11-14 20:24:35 -0500595 return -EINVAL;
596 }
597 if (!IS_ALIGNED(base_offset, base_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -0500598 dev_warn(p->dev, "%s offset[%d] 0x%llx, 0x%llx, %d not aligned\n", __func__, i,
599 base_offset, base_align, array_mode);
Alex Deucher16790562010-11-14 20:24:35 -0500600 return -EINVAL;
601 }
602
Jerome Glisse961fb592010-02-10 22:30:05 +0000603 ntiles = G_028000_SLICE_TILE_MAX(track->db_depth_size) + 1;
604 nviews = G_028004_SLICE_MAX(track->db_depth_view) + 1;
605 tmp = ntiles * bpe * 64 * nviews;
606 if ((tmp + track->db_offset) > radeon_bo_size(track->db_bo)) {
Alex Deucherc2049b32011-02-13 18:42:41 -0500607 dev_warn(p->dev, "z/stencil buffer (%d) too small (0x%08X %d %d %d -> %u have %lu)\n",
608 array_mode,
609 track->db_depth_size, ntiles, nviews, bpe, tmp + track->db_offset,
610 radeon_bo_size(track->db_bo));
Jerome Glisse961fb592010-02-10 22:30:05 +0000611 return -EINVAL;
612 }
613 }
614 }
615 return 0;
616}
617
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000618/**
619 * r600_cs_packet_parse() - parse cp packet and point ib index to next packet
620 * @parser: parser structure holding parsing context.
621 * @pkt: where to store packet informations
622 *
623 * Assume that chunk_ib_index is properly set. Will return -EINVAL
624 * if packet is bigger than remaining ib size. or if packets is unknown.
625 **/
626int r600_cs_packet_parse(struct radeon_cs_parser *p,
627 struct radeon_cs_packet *pkt,
628 unsigned idx)
629{
630 struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
631 uint32_t header;
632
633 if (idx >= ib_chunk->length_dw) {
634 DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
635 idx, ib_chunk->length_dw);
636 return -EINVAL;
637 }
Dave Airlie513bcb42009-09-23 16:56:27 +1000638 header = radeon_get_ib_value(p, idx);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000639 pkt->idx = idx;
640 pkt->type = CP_PACKET_GET_TYPE(header);
641 pkt->count = CP_PACKET_GET_COUNT(header);
642 pkt->one_reg_wr = 0;
643 switch (pkt->type) {
644 case PACKET_TYPE0:
645 pkt->reg = CP_PACKET0_GET_REG(header);
646 break;
647 case PACKET_TYPE3:
648 pkt->opcode = CP_PACKET3_GET_OPCODE(header);
649 break;
650 case PACKET_TYPE2:
651 pkt->count = -1;
652 break;
653 default:
654 DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
655 return -EINVAL;
656 }
657 if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
658 DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
659 pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
660 return -EINVAL;
661 }
662 return 0;
663}
664
665/**
666 * r600_cs_packet_next_reloc_mm() - parse next packet which should be reloc packet3
667 * @parser: parser structure holding parsing context.
668 * @data: pointer to relocation data
669 * @offset_start: starting offset
670 * @offset_mask: offset mask (to align start offset on)
671 * @reloc: reloc informations
672 *
673 * Check next packet is relocation packet3, do bo validation and compute
674 * GPU offset using the provided start.
675 **/
676static int r600_cs_packet_next_reloc_mm(struct radeon_cs_parser *p,
677 struct radeon_cs_reloc **cs_reloc)
678{
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000679 struct radeon_cs_chunk *relocs_chunk;
680 struct radeon_cs_packet p3reloc;
681 unsigned idx;
682 int r;
683
684 if (p->chunk_relocs_idx == -1) {
685 DRM_ERROR("No relocation chunk !\n");
686 return -EINVAL;
687 }
688 *cs_reloc = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000689 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
690 r = r600_cs_packet_parse(p, &p3reloc, p->idx);
691 if (r) {
692 return r;
693 }
694 p->idx += p3reloc.count + 2;
695 if (p3reloc.type != PACKET_TYPE3 || p3reloc.opcode != PACKET3_NOP) {
696 DRM_ERROR("No packet3 for relocation for packet at %d.\n",
697 p3reloc.idx);
698 return -EINVAL;
699 }
Dave Airlie513bcb42009-09-23 16:56:27 +1000700 idx = radeon_get_ib_value(p, p3reloc.idx + 1);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000701 if (idx >= relocs_chunk->length_dw) {
702 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
703 idx, relocs_chunk->length_dw);
704 return -EINVAL;
705 }
706 /* FIXME: we assume reloc size is 4 dwords */
707 *cs_reloc = p->relocs_ptr[(idx / 4)];
708 return 0;
709}
710
711/**
712 * r600_cs_packet_next_reloc_nomm() - parse next packet which should be reloc packet3
713 * @parser: parser structure holding parsing context.
714 * @data: pointer to relocation data
715 * @offset_start: starting offset
716 * @offset_mask: offset mask (to align start offset on)
717 * @reloc: reloc informations
718 *
719 * Check next packet is relocation packet3, do bo validation and compute
720 * GPU offset using the provided start.
721 **/
722static int r600_cs_packet_next_reloc_nomm(struct radeon_cs_parser *p,
723 struct radeon_cs_reloc **cs_reloc)
724{
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000725 struct radeon_cs_chunk *relocs_chunk;
726 struct radeon_cs_packet p3reloc;
727 unsigned idx;
728 int r;
729
730 if (p->chunk_relocs_idx == -1) {
731 DRM_ERROR("No relocation chunk !\n");
732 return -EINVAL;
733 }
734 *cs_reloc = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000735 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
736 r = r600_cs_packet_parse(p, &p3reloc, p->idx);
737 if (r) {
738 return r;
739 }
740 p->idx += p3reloc.count + 2;
741 if (p3reloc.type != PACKET_TYPE3 || p3reloc.opcode != PACKET3_NOP) {
742 DRM_ERROR("No packet3 for relocation for packet at %d.\n",
743 p3reloc.idx);
744 return -EINVAL;
745 }
Dave Airlie513bcb42009-09-23 16:56:27 +1000746 idx = radeon_get_ib_value(p, p3reloc.idx + 1);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000747 if (idx >= relocs_chunk->length_dw) {
748 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
749 idx, relocs_chunk->length_dw);
750 return -EINVAL;
751 }
Julia Lawalle265f39e2009-12-19 08:16:33 +0100752 *cs_reloc = p->relocs;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000753 (*cs_reloc)->lobj.gpu_offset = (u64)relocs_chunk->kdata[idx + 3] << 32;
754 (*cs_reloc)->lobj.gpu_offset |= relocs_chunk->kdata[idx + 0];
755 return 0;
756}
757
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400758/**
Jerome Glissec8c15ff2010-01-18 13:01:36 +0100759 * r600_cs_packet_next_is_pkt3_nop() - test if next packet is packet3 nop for reloc
760 * @parser: parser structure holding parsing context.
761 *
762 * Check next packet is relocation packet3, do bo validation and compute
763 * GPU offset using the provided start.
764 **/
Andi Kleen488479e2011-10-13 16:08:41 -0700765static int r600_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p)
Jerome Glissec8c15ff2010-01-18 13:01:36 +0100766{
767 struct radeon_cs_packet p3reloc;
768 int r;
769
770 r = r600_cs_packet_parse(p, &p3reloc, p->idx);
771 if (r) {
772 return 0;
773 }
774 if (p3reloc.type != PACKET_TYPE3 || p3reloc.opcode != PACKET3_NOP) {
775 return 0;
776 }
777 return 1;
778}
779
780/**
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400781 * r600_cs_packet_next_vline() - parse userspace VLINE packet
782 * @parser: parser structure holding parsing context.
783 *
784 * Userspace sends a special sequence for VLINE waits.
785 * PACKET0 - VLINE_START_END + value
786 * PACKET3 - WAIT_REG_MEM poll vline status reg
787 * RELOC (P3) - crtc_id in reloc.
788 *
789 * This function parses this and relocates the VLINE START END
790 * and WAIT_REG_MEM packets to the correct crtc.
791 * It also detects a switched off crtc and nulls out the
792 * wait in that case.
793 */
794static int r600_cs_packet_parse_vline(struct radeon_cs_parser *p)
795{
796 struct drm_mode_object *obj;
797 struct drm_crtc *crtc;
798 struct radeon_crtc *radeon_crtc;
799 struct radeon_cs_packet p3reloc, wait_reg_mem;
800 int crtc_id;
801 int r;
802 uint32_t header, h_idx, reg, wait_reg_mem_info;
803 volatile uint32_t *ib;
804
805 ib = p->ib->ptr;
806
807 /* parse the WAIT_REG_MEM */
808 r = r600_cs_packet_parse(p, &wait_reg_mem, p->idx);
809 if (r)
810 return r;
811
812 /* check its a WAIT_REG_MEM */
813 if (wait_reg_mem.type != PACKET_TYPE3 ||
814 wait_reg_mem.opcode != PACKET3_WAIT_REG_MEM) {
815 DRM_ERROR("vline wait missing WAIT_REG_MEM segment\n");
Paul Bollea3a88a62011-03-16 22:10:06 +0100816 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400817 }
818
819 wait_reg_mem_info = radeon_get_ib_value(p, wait_reg_mem.idx + 1);
820 /* bit 4 is reg (0) or mem (1) */
821 if (wait_reg_mem_info & 0x10) {
822 DRM_ERROR("vline WAIT_REG_MEM waiting on MEM rather than REG\n");
Paul Bollea3a88a62011-03-16 22:10:06 +0100823 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400824 }
825 /* waiting for value to be equal */
826 if ((wait_reg_mem_info & 0x7) != 0x3) {
827 DRM_ERROR("vline WAIT_REG_MEM function not equal\n");
Paul Bollea3a88a62011-03-16 22:10:06 +0100828 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400829 }
830 if ((radeon_get_ib_value(p, wait_reg_mem.idx + 2) << 2) != AVIVO_D1MODE_VLINE_STATUS) {
831 DRM_ERROR("vline WAIT_REG_MEM bad reg\n");
Paul Bollea3a88a62011-03-16 22:10:06 +0100832 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400833 }
834
835 if (radeon_get_ib_value(p, wait_reg_mem.idx + 5) != AVIVO_D1MODE_VLINE_STAT) {
836 DRM_ERROR("vline WAIT_REG_MEM bad bit mask\n");
Paul Bollea3a88a62011-03-16 22:10:06 +0100837 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400838 }
839
840 /* jump over the NOP */
841 r = r600_cs_packet_parse(p, &p3reloc, p->idx + wait_reg_mem.count + 2);
842 if (r)
843 return r;
844
845 h_idx = p->idx - 2;
846 p->idx += wait_reg_mem.count + 2;
847 p->idx += p3reloc.count + 2;
848
849 header = radeon_get_ib_value(p, h_idx);
850 crtc_id = radeon_get_ib_value(p, h_idx + 2 + 7 + 1);
Dave Airlied4ac6a02009-10-08 11:32:49 +1000851 reg = CP_PACKET0_GET_REG(header);
Dave Airlie29508eb2010-07-22 09:57:13 +1000852
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400853 obj = drm_mode_object_find(p->rdev->ddev, crtc_id, DRM_MODE_OBJECT_CRTC);
854 if (!obj) {
855 DRM_ERROR("cannot find crtc %d\n", crtc_id);
Paul Bollea3a88a62011-03-16 22:10:06 +0100856 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400857 }
858 crtc = obj_to_crtc(obj);
859 radeon_crtc = to_radeon_crtc(crtc);
860 crtc_id = radeon_crtc->crtc_id;
861
862 if (!crtc->enabled) {
863 /* if the CRTC isn't enabled - we need to nop out the WAIT_REG_MEM */
864 ib[h_idx + 2] = PACKET2(0);
865 ib[h_idx + 3] = PACKET2(0);
866 ib[h_idx + 4] = PACKET2(0);
867 ib[h_idx + 5] = PACKET2(0);
868 ib[h_idx + 6] = PACKET2(0);
869 ib[h_idx + 7] = PACKET2(0);
870 ib[h_idx + 8] = PACKET2(0);
871 } else if (crtc_id == 1) {
872 switch (reg) {
873 case AVIVO_D1MODE_VLINE_START_END:
874 header &= ~R600_CP_PACKET0_REG_MASK;
875 header |= AVIVO_D2MODE_VLINE_START_END >> 2;
876 break;
877 default:
878 DRM_ERROR("unknown crtc reloc\n");
Paul Bollea3a88a62011-03-16 22:10:06 +0100879 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400880 }
881 ib[h_idx] = header;
882 ib[h_idx + 4] = AVIVO_D2MODE_VLINE_STATUS >> 2;
883 }
Paul Bollea3a88a62011-03-16 22:10:06 +0100884
885 return 0;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400886}
887
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000888static int r600_packet0_check(struct radeon_cs_parser *p,
889 struct radeon_cs_packet *pkt,
890 unsigned idx, unsigned reg)
891{
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400892 int r;
893
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000894 switch (reg) {
895 case AVIVO_D1MODE_VLINE_START_END:
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400896 r = r600_cs_packet_parse_vline(p);
897 if (r) {
898 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
899 idx, reg);
900 return r;
901 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000902 break;
903 default:
904 printk(KERN_ERR "Forbidden register 0x%04X in cs at %d\n",
905 reg, idx);
906 return -EINVAL;
907 }
908 return 0;
909}
910
911static int r600_cs_parse_packet0(struct radeon_cs_parser *p,
912 struct radeon_cs_packet *pkt)
913{
914 unsigned reg, i;
915 unsigned idx;
916 int r;
917
918 idx = pkt->idx + 1;
919 reg = pkt->reg;
920 for (i = 0; i <= pkt->count; i++, idx++, reg += 4) {
921 r = r600_packet0_check(p, pkt, idx, reg);
922 if (r) {
923 return r;
924 }
925 }
926 return 0;
927}
928
Jerome Glisse961fb592010-02-10 22:30:05 +0000929/**
930 * r600_cs_check_reg() - check if register is authorized or not
931 * @parser: parser structure holding parsing context
932 * @reg: register we are testing
933 * @idx: index into the cs buffer
934 *
935 * This function will test against r600_reg_safe_bm and return 0
936 * if register is safe. If register is not flag as safe this function
937 * will test it against a list of register needind special handling.
938 */
Andi Kleen488479e2011-10-13 16:08:41 -0700939static int r600_cs_check_reg(struct radeon_cs_parser *p, u32 reg, u32 idx)
Jerome Glisse961fb592010-02-10 22:30:05 +0000940{
941 struct r600_cs_track *track = (struct r600_cs_track *)p->track;
942 struct radeon_cs_reloc *reloc;
Jerome Glisse961fb592010-02-10 22:30:05 +0000943 u32 m, i, tmp, *ib;
944 int r;
945
946 i = (reg >> 7);
Dan Carpenter88498832011-07-27 09:53:40 +0000947 if (i >= ARRAY_SIZE(r600_reg_safe_bm)) {
Jerome Glisse961fb592010-02-10 22:30:05 +0000948 dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
949 return -EINVAL;
950 }
951 m = 1 << ((reg >> 2) & 31);
952 if (!(r600_reg_safe_bm[i] & m))
953 return 0;
954 ib = p->ib->ptr;
955 switch (reg) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300956 /* force following reg to 0 in an attempt to disable out buffer
Jerome Glisse961fb592010-02-10 22:30:05 +0000957 * which will need us to better understand how it works to perform
958 * security check on it (Jerome)
959 */
960 case R_0288A8_SQ_ESGS_RING_ITEMSIZE:
961 case R_008C44_SQ_ESGS_RING_SIZE:
962 case R_0288B0_SQ_ESTMP_RING_ITEMSIZE:
963 case R_008C54_SQ_ESTMP_RING_SIZE:
964 case R_0288C0_SQ_FBUF_RING_ITEMSIZE:
965 case R_008C74_SQ_FBUF_RING_SIZE:
966 case R_0288B4_SQ_GSTMP_RING_ITEMSIZE:
967 case R_008C5C_SQ_GSTMP_RING_SIZE:
968 case R_0288AC_SQ_GSVS_RING_ITEMSIZE:
969 case R_008C4C_SQ_GSVS_RING_SIZE:
970 case R_0288BC_SQ_PSTMP_RING_ITEMSIZE:
971 case R_008C6C_SQ_PSTMP_RING_SIZE:
972 case R_0288C4_SQ_REDUC_RING_ITEMSIZE:
973 case R_008C7C_SQ_REDUC_RING_SIZE:
974 case R_0288B8_SQ_VSTMP_RING_ITEMSIZE:
975 case R_008C64_SQ_VSTMP_RING_SIZE:
976 case R_0288C8_SQ_GS_VERT_ITEMSIZE:
977 /* get value to populate the IB don't remove */
978 tmp =radeon_get_ib_value(p, idx);
979 ib[idx] = 0;
980 break;
Alex Deucher5f77df32010-03-26 14:52:32 -0400981 case SQ_CONFIG:
982 track->sq_config = radeon_get_ib_value(p, idx);
983 break;
Jerome Glisse961fb592010-02-10 22:30:05 +0000984 case R_028800_DB_DEPTH_CONTROL:
985 track->db_depth_control = radeon_get_ib_value(p, idx);
986 break;
987 case R_028010_DB_DEPTH_INFO:
Jerome Glisse721604a2012-01-05 22:11:05 -0500988 if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS) &&
Marek Olšáke70f2242011-10-25 01:38:45 +0200989 r600_cs_packet_next_is_pkt3_nop(p)) {
Alex Deucher7f813372010-05-20 12:43:52 -0400990 r = r600_cs_packet_next_reloc(p, &reloc);
991 if (r) {
992 dev_warn(p->dev, "bad SET_CONTEXT_REG "
993 "0x%04X\n", reg);
994 return -EINVAL;
995 }
996 track->db_depth_info = radeon_get_ib_value(p, idx);
997 ib[idx] &= C_028010_ARRAY_MODE;
998 track->db_depth_info &= C_028010_ARRAY_MODE;
999 if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO) {
1000 ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
1001 track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
1002 } else {
1003 ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
1004 track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
1005 }
1006 } else
1007 track->db_depth_info = radeon_get_ib_value(p, idx);
Jerome Glisse961fb592010-02-10 22:30:05 +00001008 break;
1009 case R_028004_DB_DEPTH_VIEW:
1010 track->db_depth_view = radeon_get_ib_value(p, idx);
1011 break;
1012 case R_028000_DB_DEPTH_SIZE:
1013 track->db_depth_size = radeon_get_ib_value(p, idx);
1014 track->db_depth_size_idx = idx;
1015 break;
1016 case R_028AB0_VGT_STRMOUT_EN:
1017 track->vgt_strmout_en = radeon_get_ib_value(p, idx);
1018 break;
1019 case R_028B20_VGT_STRMOUT_BUFFER_EN:
1020 track->vgt_strmout_buffer_en = radeon_get_ib_value(p, idx);
1021 break;
Marek Olšákdd220a02012-01-27 12:17:59 -05001022 case VGT_STRMOUT_BUFFER_BASE_0:
1023 case VGT_STRMOUT_BUFFER_BASE_1:
1024 case VGT_STRMOUT_BUFFER_BASE_2:
1025 case VGT_STRMOUT_BUFFER_BASE_3:
1026 r = r600_cs_packet_next_reloc(p, &reloc);
1027 if (r) {
1028 dev_warn(p->dev, "bad SET_CONTEXT_REG "
1029 "0x%04X\n", reg);
1030 return -EINVAL;
1031 }
1032 tmp = (reg - VGT_STRMOUT_BUFFER_BASE_0) / 16;
1033 track->vgt_strmout_bo_offset[tmp] = radeon_get_ib_value(p, idx) << 8;
1034 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1035 track->vgt_strmout_bo[tmp] = reloc->robj;
1036 track->vgt_strmout_bo_mc[tmp] = reloc->lobj.gpu_offset;
1037 break;
1038 case VGT_STRMOUT_BUFFER_SIZE_0:
1039 case VGT_STRMOUT_BUFFER_SIZE_1:
1040 case VGT_STRMOUT_BUFFER_SIZE_2:
1041 case VGT_STRMOUT_BUFFER_SIZE_3:
1042 tmp = (reg - VGT_STRMOUT_BUFFER_SIZE_0) / 16;
1043 /* size in register is DWs, convert to bytes */
1044 track->vgt_strmout_size[tmp] = radeon_get_ib_value(p, idx) * 4;
1045 break;
1046 case CP_COHER_BASE:
1047 r = r600_cs_packet_next_reloc(p, &reloc);
1048 if (r) {
1049 dev_warn(p->dev, "missing reloc for CP_COHER_BASE "
1050 "0x%04X\n", reg);
1051 return -EINVAL;
1052 }
1053 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1054 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001055 case R_028238_CB_TARGET_MASK:
1056 track->cb_target_mask = radeon_get_ib_value(p, idx);
1057 break;
1058 case R_02823C_CB_SHADER_MASK:
1059 track->cb_shader_mask = radeon_get_ib_value(p, idx);
1060 break;
1061 case R_028C04_PA_SC_AA_CONFIG:
1062 tmp = G_028C04_MSAA_NUM_SAMPLES(radeon_get_ib_value(p, idx));
1063 track->nsamples = 1 << tmp;
1064 break;
1065 case R_0280A0_CB_COLOR0_INFO:
1066 case R_0280A4_CB_COLOR1_INFO:
1067 case R_0280A8_CB_COLOR2_INFO:
1068 case R_0280AC_CB_COLOR3_INFO:
1069 case R_0280B0_CB_COLOR4_INFO:
1070 case R_0280B4_CB_COLOR5_INFO:
1071 case R_0280B8_CB_COLOR6_INFO:
1072 case R_0280BC_CB_COLOR7_INFO:
Jerome Glisse721604a2012-01-05 22:11:05 -05001073 if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS) &&
Marek Olšáke70f2242011-10-25 01:38:45 +02001074 r600_cs_packet_next_is_pkt3_nop(p)) {
Alex Deucher7f813372010-05-20 12:43:52 -04001075 r = r600_cs_packet_next_reloc(p, &reloc);
1076 if (r) {
1077 dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
1078 return -EINVAL;
1079 }
1080 tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
1081 track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
1082 if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO) {
1083 ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
1084 track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
1085 } else if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO) {
1086 ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
1087 track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
1088 }
1089 } else {
1090 tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
1091 track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
1092 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001093 break;
Jerome Glisse285484e2011-12-16 17:03:42 -05001094 case R_028080_CB_COLOR0_VIEW:
1095 case R_028084_CB_COLOR1_VIEW:
1096 case R_028088_CB_COLOR2_VIEW:
1097 case R_02808C_CB_COLOR3_VIEW:
1098 case R_028090_CB_COLOR4_VIEW:
1099 case R_028094_CB_COLOR5_VIEW:
1100 case R_028098_CB_COLOR6_VIEW:
1101 case R_02809C_CB_COLOR7_VIEW:
1102 tmp = (reg - R_028080_CB_COLOR0_VIEW) / 4;
1103 track->cb_color_view[tmp] = radeon_get_ib_value(p, idx);
1104 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001105 case R_028060_CB_COLOR0_SIZE:
1106 case R_028064_CB_COLOR1_SIZE:
1107 case R_028068_CB_COLOR2_SIZE:
1108 case R_02806C_CB_COLOR3_SIZE:
1109 case R_028070_CB_COLOR4_SIZE:
1110 case R_028074_CB_COLOR5_SIZE:
1111 case R_028078_CB_COLOR6_SIZE:
1112 case R_02807C_CB_COLOR7_SIZE:
1113 tmp = (reg - R_028060_CB_COLOR0_SIZE) / 4;
1114 track->cb_color_size[tmp] = radeon_get_ib_value(p, idx);
1115 track->cb_color_size_idx[tmp] = idx;
1116 break;
1117 /* This register were added late, there is userspace
1118 * which does provide relocation for those but set
1119 * 0 offset. In order to avoid breaking old userspace
1120 * we detect this and set address to point to last
1121 * CB_COLOR0_BASE, note that if userspace doesn't set
1122 * CB_COLOR0_BASE before this register we will report
1123 * error. Old userspace always set CB_COLOR0_BASE
1124 * before any of this.
1125 */
1126 case R_0280E0_CB_COLOR0_FRAG:
1127 case R_0280E4_CB_COLOR1_FRAG:
1128 case R_0280E8_CB_COLOR2_FRAG:
1129 case R_0280EC_CB_COLOR3_FRAG:
1130 case R_0280F0_CB_COLOR4_FRAG:
1131 case R_0280F4_CB_COLOR5_FRAG:
1132 case R_0280F8_CB_COLOR6_FRAG:
1133 case R_0280FC_CB_COLOR7_FRAG:
1134 tmp = (reg - R_0280E0_CB_COLOR0_FRAG) / 4;
1135 if (!r600_cs_packet_next_is_pkt3_nop(p)) {
1136 if (!track->cb_color_base_last[tmp]) {
1137 dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
1138 return -EINVAL;
1139 }
1140 ib[idx] = track->cb_color_base_last[tmp];
Jerome Glisse961fb592010-02-10 22:30:05 +00001141 track->cb_color_frag_bo[tmp] = track->cb_color_bo[tmp];
1142 } else {
1143 r = r600_cs_packet_next_reloc(p, &reloc);
1144 if (r) {
1145 dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
1146 return -EINVAL;
1147 }
1148 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1149 track->cb_color_frag_bo[tmp] = reloc->robj;
1150 }
1151 break;
1152 case R_0280C0_CB_COLOR0_TILE:
1153 case R_0280C4_CB_COLOR1_TILE:
1154 case R_0280C8_CB_COLOR2_TILE:
1155 case R_0280CC_CB_COLOR3_TILE:
1156 case R_0280D0_CB_COLOR4_TILE:
1157 case R_0280D4_CB_COLOR5_TILE:
1158 case R_0280D8_CB_COLOR6_TILE:
1159 case R_0280DC_CB_COLOR7_TILE:
1160 tmp = (reg - R_0280C0_CB_COLOR0_TILE) / 4;
1161 if (!r600_cs_packet_next_is_pkt3_nop(p)) {
1162 if (!track->cb_color_base_last[tmp]) {
1163 dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
1164 return -EINVAL;
1165 }
1166 ib[idx] = track->cb_color_base_last[tmp];
Jerome Glisse961fb592010-02-10 22:30:05 +00001167 track->cb_color_tile_bo[tmp] = track->cb_color_bo[tmp];
1168 } else {
1169 r = r600_cs_packet_next_reloc(p, &reloc);
1170 if (r) {
1171 dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
1172 return -EINVAL;
1173 }
1174 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1175 track->cb_color_tile_bo[tmp] = reloc->robj;
1176 }
1177 break;
1178 case CB_COLOR0_BASE:
1179 case CB_COLOR1_BASE:
1180 case CB_COLOR2_BASE:
1181 case CB_COLOR3_BASE:
1182 case CB_COLOR4_BASE:
1183 case CB_COLOR5_BASE:
1184 case CB_COLOR6_BASE:
1185 case CB_COLOR7_BASE:
1186 r = r600_cs_packet_next_reloc(p, &reloc);
1187 if (r) {
1188 dev_warn(p->dev, "bad SET_CONTEXT_REG "
1189 "0x%04X\n", reg);
1190 return -EINVAL;
1191 }
Jerome Glisse7cb72ef2010-02-11 12:44:32 +01001192 tmp = (reg - CB_COLOR0_BASE) / 4;
Alex Deucher1729dd32010-08-06 02:54:05 -04001193 track->cb_color_bo_offset[tmp] = radeon_get_ib_value(p, idx) << 8;
Jerome Glisse961fb592010-02-10 22:30:05 +00001194 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
Jerome Glisse961fb592010-02-10 22:30:05 +00001195 track->cb_color_base_last[tmp] = ib[idx];
1196 track->cb_color_bo[tmp] = reloc->robj;
Alex Deucher16790562010-11-14 20:24:35 -05001197 track->cb_color_bo_mc[tmp] = reloc->lobj.gpu_offset;
Jerome Glisse961fb592010-02-10 22:30:05 +00001198 break;
1199 case DB_DEPTH_BASE:
1200 r = r600_cs_packet_next_reloc(p, &reloc);
1201 if (r) {
1202 dev_warn(p->dev, "bad SET_CONTEXT_REG "
1203 "0x%04X\n", reg);
1204 return -EINVAL;
1205 }
Alex Deucher1729dd32010-08-06 02:54:05 -04001206 track->db_offset = radeon_get_ib_value(p, idx) << 8;
Jerome Glisse961fb592010-02-10 22:30:05 +00001207 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1208 track->db_bo = reloc->robj;
Alex Deucher16790562010-11-14 20:24:35 -05001209 track->db_bo_mc = reloc->lobj.gpu_offset;
Jerome Glisse961fb592010-02-10 22:30:05 +00001210 break;
1211 case DB_HTILE_DATA_BASE:
1212 case SQ_PGM_START_FS:
1213 case SQ_PGM_START_ES:
1214 case SQ_PGM_START_VS:
1215 case SQ_PGM_START_GS:
1216 case SQ_PGM_START_PS:
Alex Deucher5f77df32010-03-26 14:52:32 -04001217 case SQ_ALU_CONST_CACHE_GS_0:
1218 case SQ_ALU_CONST_CACHE_GS_1:
1219 case SQ_ALU_CONST_CACHE_GS_2:
1220 case SQ_ALU_CONST_CACHE_GS_3:
1221 case SQ_ALU_CONST_CACHE_GS_4:
1222 case SQ_ALU_CONST_CACHE_GS_5:
1223 case SQ_ALU_CONST_CACHE_GS_6:
1224 case SQ_ALU_CONST_CACHE_GS_7:
1225 case SQ_ALU_CONST_CACHE_GS_8:
1226 case SQ_ALU_CONST_CACHE_GS_9:
1227 case SQ_ALU_CONST_CACHE_GS_10:
1228 case SQ_ALU_CONST_CACHE_GS_11:
1229 case SQ_ALU_CONST_CACHE_GS_12:
1230 case SQ_ALU_CONST_CACHE_GS_13:
1231 case SQ_ALU_CONST_CACHE_GS_14:
1232 case SQ_ALU_CONST_CACHE_GS_15:
1233 case SQ_ALU_CONST_CACHE_PS_0:
1234 case SQ_ALU_CONST_CACHE_PS_1:
1235 case SQ_ALU_CONST_CACHE_PS_2:
1236 case SQ_ALU_CONST_CACHE_PS_3:
1237 case SQ_ALU_CONST_CACHE_PS_4:
1238 case SQ_ALU_CONST_CACHE_PS_5:
1239 case SQ_ALU_CONST_CACHE_PS_6:
1240 case SQ_ALU_CONST_CACHE_PS_7:
1241 case SQ_ALU_CONST_CACHE_PS_8:
1242 case SQ_ALU_CONST_CACHE_PS_9:
1243 case SQ_ALU_CONST_CACHE_PS_10:
1244 case SQ_ALU_CONST_CACHE_PS_11:
1245 case SQ_ALU_CONST_CACHE_PS_12:
1246 case SQ_ALU_CONST_CACHE_PS_13:
1247 case SQ_ALU_CONST_CACHE_PS_14:
1248 case SQ_ALU_CONST_CACHE_PS_15:
1249 case SQ_ALU_CONST_CACHE_VS_0:
1250 case SQ_ALU_CONST_CACHE_VS_1:
1251 case SQ_ALU_CONST_CACHE_VS_2:
1252 case SQ_ALU_CONST_CACHE_VS_3:
1253 case SQ_ALU_CONST_CACHE_VS_4:
1254 case SQ_ALU_CONST_CACHE_VS_5:
1255 case SQ_ALU_CONST_CACHE_VS_6:
1256 case SQ_ALU_CONST_CACHE_VS_7:
1257 case SQ_ALU_CONST_CACHE_VS_8:
1258 case SQ_ALU_CONST_CACHE_VS_9:
1259 case SQ_ALU_CONST_CACHE_VS_10:
1260 case SQ_ALU_CONST_CACHE_VS_11:
1261 case SQ_ALU_CONST_CACHE_VS_12:
1262 case SQ_ALU_CONST_CACHE_VS_13:
1263 case SQ_ALU_CONST_CACHE_VS_14:
1264 case SQ_ALU_CONST_CACHE_VS_15:
Jerome Glisse961fb592010-02-10 22:30:05 +00001265 r = r600_cs_packet_next_reloc(p, &reloc);
1266 if (r) {
1267 dev_warn(p->dev, "bad SET_CONTEXT_REG "
1268 "0x%04X\n", reg);
1269 return -EINVAL;
1270 }
1271 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1272 break;
Alex Deucher033b5652011-06-08 15:26:45 -04001273 case SX_MEMORY_EXPORT_BASE:
1274 r = r600_cs_packet_next_reloc(p, &reloc);
1275 if (r) {
1276 dev_warn(p->dev, "bad SET_CONFIG_REG "
1277 "0x%04X\n", reg);
1278 return -EINVAL;
1279 }
1280 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1281 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001282 default:
1283 dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
1284 return -EINVAL;
1285 }
1286 return 0;
1287}
1288
Jerome Glisse285484e2011-12-16 17:03:42 -05001289unsigned r600_mip_minify(unsigned size, unsigned level)
Jerome Glisse961fb592010-02-10 22:30:05 +00001290{
Dave Airlie60b212f2011-02-18 05:51:58 +00001291 unsigned val;
1292
1293 val = max(1U, size >> level);
1294 if (level > 0)
1295 val = roundup_pow_of_two(val);
1296 return val;
Jerome Glisse961fb592010-02-10 22:30:05 +00001297}
1298
Dave Airlie60b212f2011-02-18 05:51:58 +00001299static void r600_texture_size(unsigned nfaces, unsigned blevel, unsigned llevel,
1300 unsigned w0, unsigned h0, unsigned d0, unsigned format,
1301 unsigned block_align, unsigned height_align, unsigned base_align,
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001302 unsigned *l0_size, unsigned *mipmap_size)
Jerome Glisse961fb592010-02-10 22:30:05 +00001303{
Dave Airlie60b212f2011-02-18 05:51:58 +00001304 unsigned offset, i, level;
1305 unsigned width, height, depth, size;
1306 unsigned blocksize;
1307 unsigned nbx, nby;
1308 unsigned nlevels = llevel - blevel + 1;
Jerome Glisse961fb592010-02-10 22:30:05 +00001309
Dave Airlie60b212f2011-02-18 05:51:58 +00001310 *l0_size = -1;
Jerome Glisse285484e2011-12-16 17:03:42 -05001311 blocksize = r600_fmt_get_blocksize(format);
Dave Airlie60b212f2011-02-18 05:51:58 +00001312
Jerome Glisse285484e2011-12-16 17:03:42 -05001313 w0 = r600_mip_minify(w0, 0);
1314 h0 = r600_mip_minify(h0, 0);
1315 d0 = r600_mip_minify(d0, 0);
Jerome Glisse961fb592010-02-10 22:30:05 +00001316 for(i = 0, offset = 0, level = blevel; i < nlevels; i++, level++) {
Jerome Glisse285484e2011-12-16 17:03:42 -05001317 width = r600_mip_minify(w0, i);
1318 nbx = r600_fmt_get_nblocksx(format, width);
Dave Airlie60b212f2011-02-18 05:51:58 +00001319
1320 nbx = round_up(nbx, block_align);
1321
Jerome Glisse285484e2011-12-16 17:03:42 -05001322 height = r600_mip_minify(h0, i);
1323 nby = r600_fmt_get_nblocksy(format, height);
Dave Airlie60b212f2011-02-18 05:51:58 +00001324 nby = round_up(nby, height_align);
1325
Jerome Glisse285484e2011-12-16 17:03:42 -05001326 depth = r600_mip_minify(d0, i);
Dave Airlie60b212f2011-02-18 05:51:58 +00001327
1328 size = nbx * nby * blocksize;
1329 if (nfaces)
1330 size *= nfaces;
1331 else
1332 size *= depth;
1333
1334 if (i == 0)
1335 *l0_size = size;
1336
1337 if (i == 0 || i == 1)
1338 offset = round_up(offset, base_align);
1339
1340 offset += size;
Jerome Glisse961fb592010-02-10 22:30:05 +00001341 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001342 *mipmap_size = offset;
Dave Airlie60b212f2011-02-18 05:51:58 +00001343 if (llevel == 0)
Jerome Glisse961fb592010-02-10 22:30:05 +00001344 *mipmap_size = *l0_size;
Alex Deucher1729dd32010-08-06 02:54:05 -04001345 if (!blevel)
1346 *mipmap_size -= *l0_size;
Jerome Glisse961fb592010-02-10 22:30:05 +00001347}
1348
1349/**
1350 * r600_check_texture_resource() - check if register is authorized or not
1351 * @p: parser structure holding parsing context
1352 * @idx: index into the cs buffer
1353 * @texture: texture's bo structure
1354 * @mipmap: mipmap's bo structure
1355 *
1356 * This function will check that the resource has valid field and that
1357 * the texture and mipmap bo object are big enough to cover this resource.
1358 */
Andi Kleen488479e2011-10-13 16:08:41 -07001359static int r600_check_texture_resource(struct radeon_cs_parser *p, u32 idx,
Alex Deucher7f813372010-05-20 12:43:52 -04001360 struct radeon_bo *texture,
1361 struct radeon_bo *mipmap,
Alex Deucher16790562010-11-14 20:24:35 -05001362 u64 base_offset,
1363 u64 mip_offset,
Alex Deucher7f813372010-05-20 12:43:52 -04001364 u32 tiling_flags)
Jerome Glisse961fb592010-02-10 22:30:05 +00001365{
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001366 struct r600_cs_track *track = p->track;
Dave Airlie60b212f2011-02-18 05:51:58 +00001367 u32 nfaces, llevel, blevel, w0, h0, d0;
Dave Airlieaf506212011-02-28 14:27:03 +10001368 u32 word0, word1, l0_size, mipmap_size, word2, word3;
Alex Deucher16790562010-11-14 20:24:35 -05001369 u32 height_align, pitch, pitch_align, depth_align;
Dave Airlie60b212f2011-02-18 05:51:58 +00001370 u32 array, barray, larray;
Alex Deucher16790562010-11-14 20:24:35 -05001371 u64 base_align;
1372 struct array_mode_checker array_check;
Dave Airlie60b212f2011-02-18 05:51:58 +00001373 u32 format;
Jerome Glisse961fb592010-02-10 22:30:05 +00001374
1375 /* on legacy kernel we don't perform advanced check */
1376 if (p->rdev == NULL)
1377 return 0;
Alex Deucher7f813372010-05-20 12:43:52 -04001378
Alex Deucher16790562010-11-14 20:24:35 -05001379 /* convert to bytes */
1380 base_offset <<= 8;
1381 mip_offset <<= 8;
1382
Jerome Glisse961fb592010-02-10 22:30:05 +00001383 word0 = radeon_get_ib_value(p, idx + 0);
Jerome Glisse721604a2012-01-05 22:11:05 -05001384 if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
Marek Olšáke70f2242011-10-25 01:38:45 +02001385 if (tiling_flags & RADEON_TILING_MACRO)
1386 word0 |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
1387 else if (tiling_flags & RADEON_TILING_MICRO)
1388 word0 |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
1389 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001390 word1 = radeon_get_ib_value(p, idx + 1);
1391 w0 = G_038000_TEX_WIDTH(word0) + 1;
1392 h0 = G_038004_TEX_HEIGHT(word1) + 1;
1393 d0 = G_038004_TEX_DEPTH(word1);
1394 nfaces = 1;
1395 switch (G_038000_DIM(word0)) {
1396 case V_038000_SQ_TEX_DIM_1D:
1397 case V_038000_SQ_TEX_DIM_2D:
1398 case V_038000_SQ_TEX_DIM_3D:
1399 break;
1400 case V_038000_SQ_TEX_DIM_CUBEMAP:
Dave Airlie60b212f2011-02-18 05:51:58 +00001401 if (p->family >= CHIP_RV770)
1402 nfaces = 8;
1403 else
1404 nfaces = 6;
Jerome Glisse961fb592010-02-10 22:30:05 +00001405 break;
1406 case V_038000_SQ_TEX_DIM_1D_ARRAY:
1407 case V_038000_SQ_TEX_DIM_2D_ARRAY:
Dave Airlie60b212f2011-02-18 05:51:58 +00001408 array = 1;
1409 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001410 case V_038000_SQ_TEX_DIM_2D_MSAA:
1411 case V_038000_SQ_TEX_DIM_2D_ARRAY_MSAA:
1412 default:
1413 dev_warn(p->dev, "this kernel doesn't support %d texture dim\n", G_038000_DIM(word0));
1414 return -EINVAL;
1415 }
Dave Airlie60b212f2011-02-18 05:51:58 +00001416 format = G_038004_DATA_FORMAT(word1);
Jerome Glisse285484e2011-12-16 17:03:42 -05001417 if (!r600_fmt_is_valid_texture(format, p->family)) {
Jerome Glisse961fb592010-02-10 22:30:05 +00001418 dev_warn(p->dev, "%s:%d texture invalid format %d\n",
Dave Airlie60b212f2011-02-18 05:51:58 +00001419 __func__, __LINE__, format);
Jerome Glisse961fb592010-02-10 22:30:05 +00001420 return -EINVAL;
1421 }
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001422
Alex Deucher16790562010-11-14 20:24:35 -05001423 /* pitch in texels */
1424 pitch = (G_038000_PITCH(word0) + 1) * 8;
1425 array_check.array_mode = G_038000_TILE_MODE(word0);
1426 array_check.group_size = track->group_size;
1427 array_check.nbanks = track->nbanks;
1428 array_check.npipes = track->npipes;
1429 array_check.nsamples = 1;
Jerome Glisse285484e2011-12-16 17:03:42 -05001430 array_check.blocksize = r600_fmt_get_blocksize(format);
Alex Deucher16790562010-11-14 20:24:35 -05001431 if (r600_get_array_mode_alignment(&array_check,
1432 &pitch_align, &height_align, &depth_align, &base_align)) {
1433 dev_warn(p->dev, "%s:%d tex array mode (%d) invalid\n",
1434 __func__, __LINE__, G_038000_TILE_MODE(word0));
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001435 return -EINVAL;
1436 }
Alex Deucher16790562010-11-14 20:24:35 -05001437
1438 /* XXX check height as well... */
1439
1440 if (!IS_ALIGNED(pitch, pitch_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -05001441 dev_warn(p->dev, "%s:%d tex pitch (%d, 0x%x, %d) invalid\n",
1442 __func__, __LINE__, pitch, pitch_align, G_038000_TILE_MODE(word0));
Alex Deucher16790562010-11-14 20:24:35 -05001443 return -EINVAL;
1444 }
1445 if (!IS_ALIGNED(base_offset, base_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -05001446 dev_warn(p->dev, "%s:%d tex base offset (0x%llx, 0x%llx, %d) invalid\n",
1447 __func__, __LINE__, base_offset, base_align, G_038000_TILE_MODE(word0));
Alex Deucher16790562010-11-14 20:24:35 -05001448 return -EINVAL;
1449 }
1450 if (!IS_ALIGNED(mip_offset, base_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -05001451 dev_warn(p->dev, "%s:%d tex mip offset (0x%llx, 0x%llx, %d) invalid\n",
1452 __func__, __LINE__, mip_offset, base_align, G_038000_TILE_MODE(word0));
Alex Deucher16790562010-11-14 20:24:35 -05001453 return -EINVAL;
1454 }
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001455
Dave Airlieaf506212011-02-28 14:27:03 +10001456 word2 = radeon_get_ib_value(p, idx + 2) << 8;
1457 word3 = radeon_get_ib_value(p, idx + 3) << 8;
1458
Jerome Glisse961fb592010-02-10 22:30:05 +00001459 word0 = radeon_get_ib_value(p, idx + 4);
1460 word1 = radeon_get_ib_value(p, idx + 5);
1461 blevel = G_038010_BASE_LEVEL(word0);
Dave Airlie60b212f2011-02-18 05:51:58 +00001462 llevel = G_038014_LAST_LEVEL(word1);
Jerome Glisse285484e2011-12-16 17:03:42 -05001463 if (blevel > llevel) {
1464 dev_warn(p->dev, "texture blevel %d > llevel %d\n",
1465 blevel, llevel);
1466 }
Dave Airlie60b212f2011-02-18 05:51:58 +00001467 if (array == 1) {
1468 barray = G_038014_BASE_ARRAY(word1);
1469 larray = G_038014_LAST_ARRAY(word1);
1470
1471 nfaces = larray - barray + 1;
1472 }
1473 r600_texture_size(nfaces, blevel, llevel, w0, h0, d0, format,
1474 pitch_align, height_align, base_align,
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001475 &l0_size, &mipmap_size);
Jerome Glisse961fb592010-02-10 22:30:05 +00001476 /* using get ib will give us the offset into the texture bo */
Dave Airlieaf506212011-02-28 14:27:03 +10001477 if ((l0_size + word2) > radeon_bo_size(texture)) {
Jerome Glisse285484e2011-12-16 17:03:42 -05001478 dev_warn(p->dev, "texture bo too small ((%d %d) (%d %d) %d %d %d -> %d have %ld)\n",
1479 w0, h0, pitch_align, height_align,
1480 array_check.array_mode, format, word2,
1481 l0_size, radeon_bo_size(texture));
Dave Airlie60b212f2011-02-18 05:51:58 +00001482 dev_warn(p->dev, "alignments %d %d %d %lld\n", pitch, pitch_align, height_align, base_align);
Jerome Glisse961fb592010-02-10 22:30:05 +00001483 return -EINVAL;
1484 }
1485 /* using get ib will give us the offset into the mipmap bo */
Dave Airlieaf506212011-02-28 14:27:03 +10001486 word3 = radeon_get_ib_value(p, idx + 3) << 8;
1487 if ((mipmap_size + word3) > radeon_bo_size(mipmap)) {
Alex Deucherfe725d42010-09-14 10:10:47 -04001488 /*dev_warn(p->dev, "mipmap bo too small (%d %d %d %d %d %d -> %d have %ld)\n",
Dave Airlieaf506212011-02-28 14:27:03 +10001489 w0, h0, format, blevel, nlevels, word3, mipmap_size, radeon_bo_size(texture));*/
Jerome Glisse961fb592010-02-10 22:30:05 +00001490 }
1491 return 0;
1492}
1493
Marek Olšákdd220a02012-01-27 12:17:59 -05001494static bool r600_is_safe_reg(struct radeon_cs_parser *p, u32 reg, u32 idx)
1495{
1496 u32 m, i;
1497
1498 i = (reg >> 7);
1499 if (i >= ARRAY_SIZE(r600_reg_safe_bm)) {
1500 dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
1501 return false;
1502 }
1503 m = 1 << ((reg >> 2) & 31);
1504 if (!(r600_reg_safe_bm[i] & m))
1505 return true;
1506 dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
1507 return false;
1508}
1509
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001510static int r600_packet3_check(struct radeon_cs_parser *p,
1511 struct radeon_cs_packet *pkt)
1512{
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001513 struct radeon_cs_reloc *reloc;
Jerome Glissec8c15ff2010-01-18 13:01:36 +01001514 struct r600_cs_track *track;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001515 volatile u32 *ib;
1516 unsigned idx;
1517 unsigned i;
1518 unsigned start_reg, end_reg, reg;
1519 int r;
Dave Airlieadea4792009-09-25 14:23:47 +10001520 u32 idx_value;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001521
Jerome Glissec8c15ff2010-01-18 13:01:36 +01001522 track = (struct r600_cs_track *)p->track;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001523 ib = p->ib->ptr;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001524 idx = pkt->idx + 1;
Dave Airlieadea4792009-09-25 14:23:47 +10001525 idx_value = radeon_get_ib_value(p, idx);
Dave Airlie513bcb42009-09-23 16:56:27 +10001526
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001527 switch (pkt->opcode) {
Dave Airlie2a19cac2011-02-28 16:11:48 +10001528 case PACKET3_SET_PREDICATION:
1529 {
1530 int pred_op;
1531 int tmp;
1532 if (pkt->count != 1) {
1533 DRM_ERROR("bad SET PREDICATION\n");
1534 return -EINVAL;
1535 }
1536
1537 tmp = radeon_get_ib_value(p, idx + 1);
1538 pred_op = (tmp >> 16) & 0x7;
1539
1540 /* for the clear predicate operation */
1541 if (pred_op == 0)
1542 return 0;
1543
1544 if (pred_op > 2) {
1545 DRM_ERROR("bad SET PREDICATION operation %d\n", pred_op);
1546 return -EINVAL;
1547 }
1548
1549 r = r600_cs_packet_next_reloc(p, &reloc);
1550 if (r) {
1551 DRM_ERROR("bad SET PREDICATION\n");
1552 return -EINVAL;
1553 }
1554
1555 ib[idx + 0] = idx_value + (u32)(reloc->lobj.gpu_offset & 0xffffffff);
1556 ib[idx + 1] = tmp + (upper_32_bits(reloc->lobj.gpu_offset) & 0xff);
1557 }
1558 break;
1559
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001560 case PACKET3_START_3D_CMDBUF:
1561 if (p->family >= CHIP_RV770 || pkt->count) {
1562 DRM_ERROR("bad START_3D\n");
1563 return -EINVAL;
1564 }
1565 break;
1566 case PACKET3_CONTEXT_CONTROL:
1567 if (pkt->count != 1) {
1568 DRM_ERROR("bad CONTEXT_CONTROL\n");
1569 return -EINVAL;
1570 }
1571 break;
1572 case PACKET3_INDEX_TYPE:
1573 case PACKET3_NUM_INSTANCES:
1574 if (pkt->count) {
1575 DRM_ERROR("bad INDEX_TYPE/NUM_INSTANCES\n");
1576 return -EINVAL;
1577 }
1578 break;
1579 case PACKET3_DRAW_INDEX:
1580 if (pkt->count != 3) {
1581 DRM_ERROR("bad DRAW_INDEX\n");
1582 return -EINVAL;
1583 }
1584 r = r600_cs_packet_next_reloc(p, &reloc);
1585 if (r) {
1586 DRM_ERROR("bad DRAW_INDEX\n");
1587 return -EINVAL;
1588 }
Dave Airlieadea4792009-09-25 14:23:47 +10001589 ib[idx+0] = idx_value + (u32)(reloc->lobj.gpu_offset & 0xffffffff);
Alex Deucher210bed82009-09-25 18:33:08 -04001590 ib[idx+1] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
Jerome Glisse961fb592010-02-10 22:30:05 +00001591 r = r600_cs_track_check(p);
1592 if (r) {
1593 dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
1594 return r;
1595 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001596 break;
1597 case PACKET3_DRAW_INDEX_AUTO:
1598 if (pkt->count != 1) {
1599 DRM_ERROR("bad DRAW_INDEX_AUTO\n");
1600 return -EINVAL;
1601 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001602 r = r600_cs_track_check(p);
1603 if (r) {
1604 dev_warn(p->dev, "%s:%d invalid cmd stream %d\n", __func__, __LINE__, idx);
1605 return r;
1606 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001607 break;
1608 case PACKET3_DRAW_INDEX_IMMD_BE:
1609 case PACKET3_DRAW_INDEX_IMMD:
1610 if (pkt->count < 2) {
1611 DRM_ERROR("bad DRAW_INDEX_IMMD\n");
1612 return -EINVAL;
1613 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001614 r = r600_cs_track_check(p);
1615 if (r) {
1616 dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
1617 return r;
1618 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001619 break;
1620 case PACKET3_WAIT_REG_MEM:
1621 if (pkt->count != 5) {
1622 DRM_ERROR("bad WAIT_REG_MEM\n");
1623 return -EINVAL;
1624 }
1625 /* bit 4 is reg (0) or mem (1) */
Dave Airlieadea4792009-09-25 14:23:47 +10001626 if (idx_value & 0x10) {
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001627 r = r600_cs_packet_next_reloc(p, &reloc);
1628 if (r) {
1629 DRM_ERROR("bad WAIT_REG_MEM\n");
1630 return -EINVAL;
1631 }
1632 ib[idx+1] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
Alex Deucher210bed82009-09-25 18:33:08 -04001633 ib[idx+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001634 }
1635 break;
1636 case PACKET3_SURFACE_SYNC:
1637 if (pkt->count != 3) {
1638 DRM_ERROR("bad SURFACE_SYNC\n");
1639 return -EINVAL;
1640 }
1641 /* 0xffffffff/0x0 is flush all cache flag */
Dave Airlie513bcb42009-09-23 16:56:27 +10001642 if (radeon_get_ib_value(p, idx + 1) != 0xffffffff ||
1643 radeon_get_ib_value(p, idx + 2) != 0) {
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001644 r = r600_cs_packet_next_reloc(p, &reloc);
1645 if (r) {
1646 DRM_ERROR("bad SURFACE_SYNC\n");
1647 return -EINVAL;
1648 }
1649 ib[idx+2] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1650 }
1651 break;
1652 case PACKET3_EVENT_WRITE:
1653 if (pkt->count != 2 && pkt->count != 0) {
1654 DRM_ERROR("bad EVENT_WRITE\n");
1655 return -EINVAL;
1656 }
1657 if (pkt->count) {
1658 r = r600_cs_packet_next_reloc(p, &reloc);
1659 if (r) {
1660 DRM_ERROR("bad EVENT_WRITE\n");
1661 return -EINVAL;
1662 }
1663 ib[idx+1] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
Alex Deucher210bed82009-09-25 18:33:08 -04001664 ib[idx+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001665 }
1666 break;
1667 case PACKET3_EVENT_WRITE_EOP:
1668 if (pkt->count != 4) {
1669 DRM_ERROR("bad EVENT_WRITE_EOP\n");
1670 return -EINVAL;
1671 }
1672 r = r600_cs_packet_next_reloc(p, &reloc);
1673 if (r) {
1674 DRM_ERROR("bad EVENT_WRITE\n");
1675 return -EINVAL;
1676 }
1677 ib[idx+1] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
Alex Deucher210bed82009-09-25 18:33:08 -04001678 ib[idx+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001679 break;
1680 case PACKET3_SET_CONFIG_REG:
Dave Airlieadea4792009-09-25 14:23:47 +10001681 start_reg = (idx_value << 2) + PACKET3_SET_CONFIG_REG_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001682 end_reg = 4 * pkt->count + start_reg - 4;
1683 if ((start_reg < PACKET3_SET_CONFIG_REG_OFFSET) ||
1684 (start_reg >= PACKET3_SET_CONFIG_REG_END) ||
1685 (end_reg >= PACKET3_SET_CONFIG_REG_END)) {
1686 DRM_ERROR("bad PACKET3_SET_CONFIG_REG\n");
1687 return -EINVAL;
1688 }
1689 for (i = 0; i < pkt->count; i++) {
1690 reg = start_reg + (4 * i);
Jerome Glisse961fb592010-02-10 22:30:05 +00001691 r = r600_cs_check_reg(p, reg, idx+1+i);
1692 if (r)
1693 return r;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001694 }
1695 break;
1696 case PACKET3_SET_CONTEXT_REG:
Dave Airlieadea4792009-09-25 14:23:47 +10001697 start_reg = (idx_value << 2) + PACKET3_SET_CONTEXT_REG_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001698 end_reg = 4 * pkt->count + start_reg - 4;
1699 if ((start_reg < PACKET3_SET_CONTEXT_REG_OFFSET) ||
1700 (start_reg >= PACKET3_SET_CONTEXT_REG_END) ||
1701 (end_reg >= PACKET3_SET_CONTEXT_REG_END)) {
1702 DRM_ERROR("bad PACKET3_SET_CONTEXT_REG\n");
1703 return -EINVAL;
1704 }
1705 for (i = 0; i < pkt->count; i++) {
1706 reg = start_reg + (4 * i);
Jerome Glisse961fb592010-02-10 22:30:05 +00001707 r = r600_cs_check_reg(p, reg, idx+1+i);
1708 if (r)
1709 return r;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001710 }
1711 break;
1712 case PACKET3_SET_RESOURCE:
1713 if (pkt->count % 7) {
1714 DRM_ERROR("bad SET_RESOURCE\n");
1715 return -EINVAL;
1716 }
Dave Airlieadea4792009-09-25 14:23:47 +10001717 start_reg = (idx_value << 2) + PACKET3_SET_RESOURCE_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001718 end_reg = 4 * pkt->count + start_reg - 4;
1719 if ((start_reg < PACKET3_SET_RESOURCE_OFFSET) ||
1720 (start_reg >= PACKET3_SET_RESOURCE_END) ||
1721 (end_reg >= PACKET3_SET_RESOURCE_END)) {
1722 DRM_ERROR("bad SET_RESOURCE\n");
1723 return -EINVAL;
1724 }
1725 for (i = 0; i < (pkt->count / 7); i++) {
Jerome Glisse961fb592010-02-10 22:30:05 +00001726 struct radeon_bo *texture, *mipmap;
Alex Deucher1729dd32010-08-06 02:54:05 -04001727 u32 size, offset, base_offset, mip_offset;
Jerome Glisse961fb592010-02-10 22:30:05 +00001728
Dave Airlieadea4792009-09-25 14:23:47 +10001729 switch (G__SQ_VTX_CONSTANT_TYPE(radeon_get_ib_value(p, idx+(i*7)+6+1))) {
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001730 case SQ_TEX_VTX_VALID_TEXTURE:
1731 /* tex base */
1732 r = r600_cs_packet_next_reloc(p, &reloc);
1733 if (r) {
1734 DRM_ERROR("bad SET_RESOURCE\n");
1735 return -EINVAL;
1736 }
Alex Deucher1729dd32010-08-06 02:54:05 -04001737 base_offset = (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
Jerome Glisse721604a2012-01-05 22:11:05 -05001738 if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
Marek Olšáke70f2242011-10-25 01:38:45 +02001739 if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO)
1740 ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
1741 else if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO)
1742 ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
1743 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001744 texture = reloc->robj;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001745 /* tex mip base */
1746 r = r600_cs_packet_next_reloc(p, &reloc);
1747 if (r) {
1748 DRM_ERROR("bad SET_RESOURCE\n");
1749 return -EINVAL;
1750 }
Alex Deucher1729dd32010-08-06 02:54:05 -04001751 mip_offset = (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
Jerome Glisse961fb592010-02-10 22:30:05 +00001752 mipmap = reloc->robj;
1753 r = r600_check_texture_resource(p, idx+(i*7)+1,
Alex Deucher16790562010-11-14 20:24:35 -05001754 texture, mipmap,
1755 base_offset + radeon_get_ib_value(p, idx+1+(i*7)+2),
1756 mip_offset + radeon_get_ib_value(p, idx+1+(i*7)+3),
1757 reloc->lobj.tiling_flags);
Jerome Glisse961fb592010-02-10 22:30:05 +00001758 if (r)
1759 return r;
Alex Deucher1729dd32010-08-06 02:54:05 -04001760 ib[idx+1+(i*7)+2] += base_offset;
1761 ib[idx+1+(i*7)+3] += mip_offset;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001762 break;
1763 case SQ_TEX_VTX_VALID_BUFFER:
1764 /* vtx base */
1765 r = r600_cs_packet_next_reloc(p, &reloc);
1766 if (r) {
1767 DRM_ERROR("bad SET_RESOURCE\n");
1768 return -EINVAL;
1769 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001770 offset = radeon_get_ib_value(p, idx+1+(i*7)+0);
Alex Deucher1729dd32010-08-06 02:54:05 -04001771 size = radeon_get_ib_value(p, idx+1+(i*7)+1) + 1;
Jerome Glisse961fb592010-02-10 22:30:05 +00001772 if (p->rdev && (size + offset) > radeon_bo_size(reloc->robj)) {
1773 /* force size to size of the buffer */
Alex Deucher1729dd32010-08-06 02:54:05 -04001774 dev_warn(p->dev, "vbo resource seems too big (%d) for the bo (%ld)\n",
1775 size + offset, radeon_bo_size(reloc->robj));
Jerome Glisse961fb592010-02-10 22:30:05 +00001776 ib[idx+1+(i*7)+1] = radeon_bo_size(reloc->robj);
1777 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001778 ib[idx+1+(i*7)+0] += (u32)((reloc->lobj.gpu_offset) & 0xffffffff);
Alex Deucher210bed82009-09-25 18:33:08 -04001779 ib[idx+1+(i*7)+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001780 break;
1781 case SQ_TEX_VTX_INVALID_TEXTURE:
1782 case SQ_TEX_VTX_INVALID_BUFFER:
1783 default:
1784 DRM_ERROR("bad SET_RESOURCE\n");
1785 return -EINVAL;
1786 }
1787 }
1788 break;
1789 case PACKET3_SET_ALU_CONST:
Alex Deucher5f77df32010-03-26 14:52:32 -04001790 if (track->sq_config & DX9_CONSTS) {
1791 start_reg = (idx_value << 2) + PACKET3_SET_ALU_CONST_OFFSET;
1792 end_reg = 4 * pkt->count + start_reg - 4;
1793 if ((start_reg < PACKET3_SET_ALU_CONST_OFFSET) ||
1794 (start_reg >= PACKET3_SET_ALU_CONST_END) ||
1795 (end_reg >= PACKET3_SET_ALU_CONST_END)) {
1796 DRM_ERROR("bad SET_ALU_CONST\n");
1797 return -EINVAL;
1798 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001799 }
1800 break;
1801 case PACKET3_SET_BOOL_CONST:
Dave Airlieadea4792009-09-25 14:23:47 +10001802 start_reg = (idx_value << 2) + PACKET3_SET_BOOL_CONST_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001803 end_reg = 4 * pkt->count + start_reg - 4;
1804 if ((start_reg < PACKET3_SET_BOOL_CONST_OFFSET) ||
1805 (start_reg >= PACKET3_SET_BOOL_CONST_END) ||
1806 (end_reg >= PACKET3_SET_BOOL_CONST_END)) {
1807 DRM_ERROR("bad SET_BOOL_CONST\n");
1808 return -EINVAL;
1809 }
1810 break;
1811 case PACKET3_SET_LOOP_CONST:
Dave Airlieadea4792009-09-25 14:23:47 +10001812 start_reg = (idx_value << 2) + PACKET3_SET_LOOP_CONST_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001813 end_reg = 4 * pkt->count + start_reg - 4;
1814 if ((start_reg < PACKET3_SET_LOOP_CONST_OFFSET) ||
1815 (start_reg >= PACKET3_SET_LOOP_CONST_END) ||
1816 (end_reg >= PACKET3_SET_LOOP_CONST_END)) {
1817 DRM_ERROR("bad SET_LOOP_CONST\n");
1818 return -EINVAL;
1819 }
1820 break;
1821 case PACKET3_SET_CTL_CONST:
Dave Airlieadea4792009-09-25 14:23:47 +10001822 start_reg = (idx_value << 2) + PACKET3_SET_CTL_CONST_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001823 end_reg = 4 * pkt->count + start_reg - 4;
1824 if ((start_reg < PACKET3_SET_CTL_CONST_OFFSET) ||
1825 (start_reg >= PACKET3_SET_CTL_CONST_END) ||
1826 (end_reg >= PACKET3_SET_CTL_CONST_END)) {
1827 DRM_ERROR("bad SET_CTL_CONST\n");
1828 return -EINVAL;
1829 }
1830 break;
1831 case PACKET3_SET_SAMPLER:
1832 if (pkt->count % 3) {
1833 DRM_ERROR("bad SET_SAMPLER\n");
1834 return -EINVAL;
1835 }
Dave Airlieadea4792009-09-25 14:23:47 +10001836 start_reg = (idx_value << 2) + PACKET3_SET_SAMPLER_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001837 end_reg = 4 * pkt->count + start_reg - 4;
1838 if ((start_reg < PACKET3_SET_SAMPLER_OFFSET) ||
1839 (start_reg >= PACKET3_SET_SAMPLER_END) ||
1840 (end_reg >= PACKET3_SET_SAMPLER_END)) {
1841 DRM_ERROR("bad SET_SAMPLER\n");
1842 return -EINVAL;
1843 }
1844 break;
1845 case PACKET3_SURFACE_BASE_UPDATE:
1846 if (p->family >= CHIP_RV770 || p->family == CHIP_R600) {
1847 DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
1848 return -EINVAL;
1849 }
1850 if (pkt->count) {
1851 DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
1852 return -EINVAL;
1853 }
1854 break;
Marek Olšákdd220a02012-01-27 12:17:59 -05001855 case PACKET3_STRMOUT_BUFFER_UPDATE:
1856 if (pkt->count != 4) {
1857 DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (invalid count)\n");
1858 return -EINVAL;
1859 }
1860 /* Updating memory at DST_ADDRESS. */
1861 if (idx_value & 0x1) {
1862 u64 offset;
1863 r = r600_cs_packet_next_reloc(p, &reloc);
1864 if (r) {
1865 DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (missing dst reloc)\n");
1866 return -EINVAL;
1867 }
1868 offset = radeon_get_ib_value(p, idx+1);
1869 offset += ((u64)(radeon_get_ib_value(p, idx+2) & 0xff)) << 32;
1870 if ((offset + 4) > radeon_bo_size(reloc->robj)) {
1871 DRM_ERROR("bad STRMOUT_BUFFER_UPDATE dst bo too small: 0x%llx, 0x%lx\n",
1872 offset + 4, radeon_bo_size(reloc->robj));
1873 return -EINVAL;
1874 }
1875 ib[idx+1] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
1876 ib[idx+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
1877 }
1878 /* Reading data from SRC_ADDRESS. */
1879 if (((idx_value >> 1) & 0x3) == 2) {
1880 u64 offset;
1881 r = r600_cs_packet_next_reloc(p, &reloc);
1882 if (r) {
1883 DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (missing src reloc)\n");
1884 return -EINVAL;
1885 }
1886 offset = radeon_get_ib_value(p, idx+3);
1887 offset += ((u64)(radeon_get_ib_value(p, idx+4) & 0xff)) << 32;
1888 if ((offset + 4) > radeon_bo_size(reloc->robj)) {
1889 DRM_ERROR("bad STRMOUT_BUFFER_UPDATE src bo too small: 0x%llx, 0x%lx\n",
1890 offset + 4, radeon_bo_size(reloc->robj));
1891 return -EINVAL;
1892 }
1893 ib[idx+3] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
1894 ib[idx+4] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
1895 }
1896 break;
1897 case PACKET3_COPY_DW:
1898 if (pkt->count != 4) {
1899 DRM_ERROR("bad COPY_DW (invalid count)\n");
1900 return -EINVAL;
1901 }
1902 if (idx_value & 0x1) {
1903 u64 offset;
1904 /* SRC is memory. */
1905 r = r600_cs_packet_next_reloc(p, &reloc);
1906 if (r) {
1907 DRM_ERROR("bad COPY_DW (missing src reloc)\n");
1908 return -EINVAL;
1909 }
1910 offset = radeon_get_ib_value(p, idx+1);
1911 offset += ((u64)(radeon_get_ib_value(p, idx+2) & 0xff)) << 32;
1912 if ((offset + 4) > radeon_bo_size(reloc->robj)) {
1913 DRM_ERROR("bad COPY_DW src bo too small: 0x%llx, 0x%lx\n",
1914 offset + 4, radeon_bo_size(reloc->robj));
1915 return -EINVAL;
1916 }
1917 ib[idx+1] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
1918 ib[idx+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
1919 } else {
1920 /* SRC is a reg. */
1921 reg = radeon_get_ib_value(p, idx+1) << 2;
1922 if (!r600_is_safe_reg(p, reg, idx+1))
1923 return -EINVAL;
1924 }
1925 if (idx_value & 0x2) {
1926 u64 offset;
1927 /* DST is memory. */
1928 r = r600_cs_packet_next_reloc(p, &reloc);
1929 if (r) {
1930 DRM_ERROR("bad COPY_DW (missing dst reloc)\n");
1931 return -EINVAL;
1932 }
1933 offset = radeon_get_ib_value(p, idx+3);
1934 offset += ((u64)(radeon_get_ib_value(p, idx+4) & 0xff)) << 32;
1935 if ((offset + 4) > radeon_bo_size(reloc->robj)) {
1936 DRM_ERROR("bad COPY_DW dst bo too small: 0x%llx, 0x%lx\n",
1937 offset + 4, radeon_bo_size(reloc->robj));
1938 return -EINVAL;
1939 }
1940 ib[idx+3] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
1941 ib[idx+4] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
1942 } else {
1943 /* DST is a reg. */
1944 reg = radeon_get_ib_value(p, idx+3) << 2;
1945 if (!r600_is_safe_reg(p, reg, idx+3))
1946 return -EINVAL;
1947 }
1948 break;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001949 case PACKET3_NOP:
1950 break;
1951 default:
1952 DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
1953 return -EINVAL;
1954 }
1955 return 0;
1956}
1957
1958int r600_cs_parse(struct radeon_cs_parser *p)
1959{
1960 struct radeon_cs_packet pkt;
Jerome Glissec8c15ff2010-01-18 13:01:36 +01001961 struct r600_cs_track *track;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001962 int r;
1963
Jerome Glisse961fb592010-02-10 22:30:05 +00001964 if (p->track == NULL) {
1965 /* initialize tracker, we are in kms */
1966 track = kzalloc(sizeof(*track), GFP_KERNEL);
1967 if (track == NULL)
1968 return -ENOMEM;
1969 r600_cs_track_init(track);
1970 if (p->rdev->family < CHIP_RV770) {
1971 track->npipes = p->rdev->config.r600.tiling_npipes;
1972 track->nbanks = p->rdev->config.r600.tiling_nbanks;
1973 track->group_size = p->rdev->config.r600.tiling_group_size;
1974 } else if (p->rdev->family <= CHIP_RV740) {
1975 track->npipes = p->rdev->config.rv770.tiling_npipes;
1976 track->nbanks = p->rdev->config.rv770.tiling_nbanks;
1977 track->group_size = p->rdev->config.rv770.tiling_group_size;
1978 }
1979 p->track = track;
1980 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001981 do {
1982 r = r600_cs_packet_parse(p, &pkt, p->idx);
1983 if (r) {
Jerome Glisse7cb72ef2010-02-11 12:44:32 +01001984 kfree(p->track);
1985 p->track = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001986 return r;
1987 }
1988 p->idx += pkt.count + 2;
1989 switch (pkt.type) {
1990 case PACKET_TYPE0:
1991 r = r600_cs_parse_packet0(p, &pkt);
1992 break;
1993 case PACKET_TYPE2:
1994 break;
1995 case PACKET_TYPE3:
1996 r = r600_packet3_check(p, &pkt);
1997 break;
1998 default:
1999 DRM_ERROR("Unknown packet type %d !\n", pkt.type);
Jerome Glisse961fb592010-02-10 22:30:05 +00002000 kfree(p->track);
Jerome Glisse7cb72ef2010-02-11 12:44:32 +01002001 p->track = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002002 return -EINVAL;
2003 }
2004 if (r) {
Jerome Glisse961fb592010-02-10 22:30:05 +00002005 kfree(p->track);
Jerome Glisse7cb72ef2010-02-11 12:44:32 +01002006 p->track = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002007 return r;
2008 }
2009 } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
2010#if 0
2011 for (r = 0; r < p->ib->length_dw; r++) {
2012 printk(KERN_INFO "%05d 0x%08X\n", r, p->ib->ptr[r]);
2013 mdelay(1);
2014 }
2015#endif
Jerome Glisse961fb592010-02-10 22:30:05 +00002016 kfree(p->track);
Jerome Glisse7cb72ef2010-02-11 12:44:32 +01002017 p->track = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002018 return 0;
2019}
2020
2021static int r600_cs_parser_relocs_legacy(struct radeon_cs_parser *p)
2022{
2023 if (p->chunk_relocs_idx == -1) {
2024 return 0;
2025 }
Julia Lawalle265f39e2009-12-19 08:16:33 +01002026 p->relocs = kzalloc(sizeof(struct radeon_cs_reloc), GFP_KERNEL);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002027 if (p->relocs == NULL) {
2028 return -ENOMEM;
2029 }
2030 return 0;
2031}
2032
2033/**
2034 * cs_parser_fini() - clean parser states
2035 * @parser: parser structure holding parsing context.
2036 * @error: error number
2037 *
2038 * If error is set than unvalidate buffer, otherwise just free memory
2039 * used by parsing context.
2040 **/
2041static void r600_cs_parser_fini(struct radeon_cs_parser *parser, int error)
2042{
2043 unsigned i;
2044
2045 kfree(parser->relocs);
2046 for (i = 0; i < parser->nchunks; i++) {
2047 kfree(parser->chunks[i].kdata);
Dave Airlie4c57edba2009-09-28 15:37:25 +10002048 kfree(parser->chunks[i].kpage[0]);
2049 kfree(parser->chunks[i].kpage[1]);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002050 }
2051 kfree(parser->chunks);
2052 kfree(parser->chunks_array);
2053}
2054
2055int r600_cs_legacy(struct drm_device *dev, void *data, struct drm_file *filp,
2056 unsigned family, u32 *ib, int *l)
2057{
2058 struct radeon_cs_parser parser;
2059 struct radeon_cs_chunk *ib_chunk;
Jerome Glisse961fb592010-02-10 22:30:05 +00002060 struct radeon_ib fake_ib;
2061 struct r600_cs_track *track;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002062 int r;
2063
Jerome Glisse961fb592010-02-10 22:30:05 +00002064 /* initialize tracker */
2065 track = kzalloc(sizeof(*track), GFP_KERNEL);
2066 if (track == NULL)
2067 return -ENOMEM;
2068 r600_cs_track_init(track);
2069 r600_cs_legacy_get_tiling_conf(dev, &track->npipes, &track->nbanks, &track->group_size);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002070 /* initialize parser */
2071 memset(&parser, 0, sizeof(struct radeon_cs_parser));
2072 parser.filp = filp;
Jerome Glissec8c15ff2010-01-18 13:01:36 +01002073 parser.dev = &dev->pdev->dev;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002074 parser.rdev = NULL;
2075 parser.family = family;
2076 parser.ib = &fake_ib;
Jerome Glisse961fb592010-02-10 22:30:05 +00002077 parser.track = track;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002078 fake_ib.ptr = ib;
2079 r = radeon_cs_parser_init(&parser, data);
2080 if (r) {
2081 DRM_ERROR("Failed to initialize parser !\n");
2082 r600_cs_parser_fini(&parser, r);
2083 return r;
2084 }
2085 r = r600_cs_parser_relocs_legacy(&parser);
2086 if (r) {
2087 DRM_ERROR("Failed to parse relocation !\n");
2088 r600_cs_parser_fini(&parser, r);
2089 return r;
2090 }
2091 /* Copy the packet into the IB, the parser will read from the
2092 * input memory (cached) and write to the IB (which can be
2093 * uncached). */
2094 ib_chunk = &parser.chunks[parser.chunk_ib_idx];
2095 parser.ib->length_dw = ib_chunk->length_dw;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002096 *l = parser.ib->length_dw;
2097 r = r600_cs_parse(&parser);
2098 if (r) {
2099 DRM_ERROR("Invalid command stream !\n");
2100 r600_cs_parser_fini(&parser, r);
2101 return r;
2102 }
Dave Airlie513bcb42009-09-23 16:56:27 +10002103 r = radeon_cs_finish_pages(&parser);
2104 if (r) {
2105 DRM_ERROR("Invalid command stream !\n");
2106 r600_cs_parser_fini(&parser, r);
2107 return r;
2108 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002109 r600_cs_parser_fini(&parser, r);
2110 return r;
2111}
2112
2113void r600_cs_legacy_init(void)
2114{
2115 r600_cs_packet_next_reloc = &r600_cs_packet_next_reloc_nomm;
2116}