blob: e61a013e63fcf0916e1e5dad14eac6ba1ef06feb [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>
David Howells760285e2012-10-02 18:01:07 +010029#include <drm/drmP.h>
Jerome Glisse3ce0a232009-09-08 10:10:24 +100030#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;
Marek Olšákc116cc92012-08-19 02:22:09 +020050 u32 log_nsamples;
Jerome Glisse961fb592010-02-10 22:30:05 +000051 u32 nsamples;
52 u32 cb_color_base_last[8];
53 struct radeon_bo *cb_color_bo[8];
Alex Deucher16790562010-11-14 20:24:35 -050054 u64 cb_color_bo_mc[8];
Marek Olšákc116cc92012-08-19 02:22:09 +020055 u64 cb_color_bo_offset[8];
56 struct radeon_bo *cb_color_frag_bo[8];
57 u64 cb_color_frag_offset[8];
58 struct radeon_bo *cb_color_tile_bo[8];
59 u64 cb_color_tile_offset[8];
60 u32 cb_color_mask[8];
Jerome Glisse961fb592010-02-10 22:30:05 +000061 u32 cb_color_info[8];
Jerome Glisse285484e2011-12-16 17:03:42 -050062 u32 cb_color_view[8];
Marek Olšák3c125132012-03-19 03:09:38 +010063 u32 cb_color_size_idx[8]; /* unused */
Jerome Glisse961fb592010-02-10 22:30:05 +000064 u32 cb_target_mask;
Marek Olšák3c125132012-03-19 03:09:38 +010065 u32 cb_shader_mask; /* unused */
Marek Olšák523885d2012-08-24 14:27:36 +020066 bool is_resolve;
Jerome Glisse961fb592010-02-10 22:30:05 +000067 u32 cb_color_size[8];
68 u32 vgt_strmout_en;
69 u32 vgt_strmout_buffer_en;
Marek Olšákdd220a02012-01-27 12:17:59 -050070 struct radeon_bo *vgt_strmout_bo[4];
Marek Olšák3c125132012-03-19 03:09:38 +010071 u64 vgt_strmout_bo_mc[4]; /* unused */
Marek Olšákdd220a02012-01-27 12:17:59 -050072 u32 vgt_strmout_bo_offset[4];
73 u32 vgt_strmout_size[4];
Jerome Glisse961fb592010-02-10 22:30:05 +000074 u32 db_depth_control;
75 u32 db_depth_info;
76 u32 db_depth_size_idx;
77 u32 db_depth_view;
78 u32 db_depth_size;
79 u32 db_offset;
80 struct radeon_bo *db_bo;
Alex Deucher16790562010-11-14 20:24:35 -050081 u64 db_bo_mc;
Marek Olšák779923b2012-03-08 00:56:00 +010082 bool sx_misc_kill_all_prims;
Marek Olšák3c125132012-03-19 03:09:38 +010083 bool cb_dirty;
84 bool db_dirty;
85 bool streamout_dirty;
Jerome Glisse88f50c82012-03-21 19:18:21 -040086 struct radeon_bo *htile_bo;
87 u64 htile_offset;
88 u32 htile_surface;
Jerome Glissec8c15ff2010-01-18 13:01:36 +010089};
90
Marek Olšákfe6f0bd2011-05-07 01:09:57 +020091#define FMT_8_BIT(fmt, vc) [fmt] = { 1, 1, 1, vc, CHIP_R600 }
92#define FMT_16_BIT(fmt, vc) [fmt] = { 1, 1, 2, vc, CHIP_R600 }
Jerome Glisse285484e2011-12-16 17:03:42 -050093#define FMT_24_BIT(fmt) [fmt] = { 1, 1, 4, 0, CHIP_R600 }
Marek Olšákfe6f0bd2011-05-07 01:09:57 +020094#define FMT_32_BIT(fmt, vc) [fmt] = { 1, 1, 4, vc, CHIP_R600 }
Jerome Glisse285484e2011-12-16 17:03:42 -050095#define FMT_48_BIT(fmt) [fmt] = { 1, 1, 8, 0, CHIP_R600 }
Marek Olšákfe6f0bd2011-05-07 01:09:57 +020096#define FMT_64_BIT(fmt, vc) [fmt] = { 1, 1, 8, vc, CHIP_R600 }
97#define FMT_96_BIT(fmt) [fmt] = { 1, 1, 12, 0, CHIP_R600 }
98#define FMT_128_BIT(fmt, vc) [fmt] = { 1, 1, 16,vc, CHIP_R600 }
Dave Airlie60b212f2011-02-18 05:51:58 +000099
100struct gpu_formats {
101 unsigned blockwidth;
102 unsigned blockheight;
103 unsigned blocksize;
104 unsigned valid_color;
Marek Olšákfe6f0bd2011-05-07 01:09:57 +0200105 enum radeon_family min_family;
Dave Airlie60b212f2011-02-18 05:51:58 +0000106};
107
108static const struct gpu_formats color_formats_table[] = {
109 /* 8 bit */
110 FMT_8_BIT(V_038004_COLOR_8, 1),
111 FMT_8_BIT(V_038004_COLOR_4_4, 1),
112 FMT_8_BIT(V_038004_COLOR_3_3_2, 1),
113 FMT_8_BIT(V_038004_FMT_1, 0),
114
115 /* 16-bit */
116 FMT_16_BIT(V_038004_COLOR_16, 1),
117 FMT_16_BIT(V_038004_COLOR_16_FLOAT, 1),
118 FMT_16_BIT(V_038004_COLOR_8_8, 1),
119 FMT_16_BIT(V_038004_COLOR_5_6_5, 1),
120 FMT_16_BIT(V_038004_COLOR_6_5_5, 1),
121 FMT_16_BIT(V_038004_COLOR_1_5_5_5, 1),
122 FMT_16_BIT(V_038004_COLOR_4_4_4_4, 1),
123 FMT_16_BIT(V_038004_COLOR_5_5_5_1, 1),
124
125 /* 24-bit */
126 FMT_24_BIT(V_038004_FMT_8_8_8),
Jerome Glisse285484e2011-12-16 17:03:42 -0500127
Dave Airlie60b212f2011-02-18 05:51:58 +0000128 /* 32-bit */
129 FMT_32_BIT(V_038004_COLOR_32, 1),
130 FMT_32_BIT(V_038004_COLOR_32_FLOAT, 1),
131 FMT_32_BIT(V_038004_COLOR_16_16, 1),
132 FMT_32_BIT(V_038004_COLOR_16_16_FLOAT, 1),
133 FMT_32_BIT(V_038004_COLOR_8_24, 1),
134 FMT_32_BIT(V_038004_COLOR_8_24_FLOAT, 1),
135 FMT_32_BIT(V_038004_COLOR_24_8, 1),
136 FMT_32_BIT(V_038004_COLOR_24_8_FLOAT, 1),
137 FMT_32_BIT(V_038004_COLOR_10_11_11, 1),
138 FMT_32_BIT(V_038004_COLOR_10_11_11_FLOAT, 1),
139 FMT_32_BIT(V_038004_COLOR_11_11_10, 1),
140 FMT_32_BIT(V_038004_COLOR_11_11_10_FLOAT, 1),
141 FMT_32_BIT(V_038004_COLOR_2_10_10_10, 1),
142 FMT_32_BIT(V_038004_COLOR_8_8_8_8, 1),
143 FMT_32_BIT(V_038004_COLOR_10_10_10_2, 1),
144 FMT_32_BIT(V_038004_FMT_5_9_9_9_SHAREDEXP, 0),
145 FMT_32_BIT(V_038004_FMT_32_AS_8, 0),
146 FMT_32_BIT(V_038004_FMT_32_AS_8_8, 0),
147
148 /* 48-bit */
149 FMT_48_BIT(V_038004_FMT_16_16_16),
150 FMT_48_BIT(V_038004_FMT_16_16_16_FLOAT),
151
152 /* 64-bit */
153 FMT_64_BIT(V_038004_COLOR_X24_8_32_FLOAT, 1),
154 FMT_64_BIT(V_038004_COLOR_32_32, 1),
155 FMT_64_BIT(V_038004_COLOR_32_32_FLOAT, 1),
156 FMT_64_BIT(V_038004_COLOR_16_16_16_16, 1),
157 FMT_64_BIT(V_038004_COLOR_16_16_16_16_FLOAT, 1),
158
159 FMT_96_BIT(V_038004_FMT_32_32_32),
160 FMT_96_BIT(V_038004_FMT_32_32_32_FLOAT),
161
162 /* 128-bit */
163 FMT_128_BIT(V_038004_COLOR_32_32_32_32, 1),
164 FMT_128_BIT(V_038004_COLOR_32_32_32_32_FLOAT, 1),
165
166 [V_038004_FMT_GB_GR] = { 2, 1, 4, 0 },
167 [V_038004_FMT_BG_RG] = { 2, 1, 4, 0 },
168
169 /* block compressed formats */
170 [V_038004_FMT_BC1] = { 4, 4, 8, 0 },
171 [V_038004_FMT_BC2] = { 4, 4, 16, 0 },
172 [V_038004_FMT_BC3] = { 4, 4, 16, 0 },
173 [V_038004_FMT_BC4] = { 4, 4, 8, 0 },
174 [V_038004_FMT_BC5] = { 4, 4, 16, 0},
Marek Olšákfe6f0bd2011-05-07 01:09:57 +0200175 [V_038004_FMT_BC6] = { 4, 4, 16, 0, CHIP_CEDAR}, /* Evergreen-only */
176 [V_038004_FMT_BC7] = { 4, 4, 16, 0, CHIP_CEDAR}, /* Evergreen-only */
Dave Airlie60b212f2011-02-18 05:51:58 +0000177
Marek Olšákfe6f0bd2011-05-07 01:09:57 +0200178 /* The other Evergreen formats */
179 [V_038004_FMT_32_AS_32_32_32_32] = { 1, 1, 4, 0, CHIP_CEDAR},
Dave Airlie60b212f2011-02-18 05:51:58 +0000180};
181
Jerome Glisse285484e2011-12-16 17:03:42 -0500182bool r600_fmt_is_valid_color(u32 format)
Dave Airlie60b212f2011-02-18 05:51:58 +0000183{
Dan Carpentercf8a47d2011-02-26 04:48:18 +0300184 if (format >= ARRAY_SIZE(color_formats_table))
Dave Airlie60b212f2011-02-18 05:51:58 +0000185 return false;
Jerome Glisse285484e2011-12-16 17:03:42 -0500186
Dave Airlie60b212f2011-02-18 05:51:58 +0000187 if (color_formats_table[format].valid_color)
188 return true;
189
190 return false;
191}
192
Jerome Glisse285484e2011-12-16 17:03:42 -0500193bool r600_fmt_is_valid_texture(u32 format, enum radeon_family family)
Dave Airlie60b212f2011-02-18 05:51:58 +0000194{
Dan Carpentercf8a47d2011-02-26 04:48:18 +0300195 if (format >= ARRAY_SIZE(color_formats_table))
Dave Airlie60b212f2011-02-18 05:51:58 +0000196 return false;
Jerome Glisse285484e2011-12-16 17:03:42 -0500197
Marek Olšákfe6f0bd2011-05-07 01:09:57 +0200198 if (family < color_formats_table[format].min_family)
199 return false;
200
Dave Airlie60b212f2011-02-18 05:51:58 +0000201 if (color_formats_table[format].blockwidth > 0)
202 return true;
203
204 return false;
205}
206
Jerome Glisse285484e2011-12-16 17:03:42 -0500207int r600_fmt_get_blocksize(u32 format)
Dave Airlie60b212f2011-02-18 05:51:58 +0000208{
Dan Carpentercf8a47d2011-02-26 04:48:18 +0300209 if (format >= ARRAY_SIZE(color_formats_table))
Dave Airlie60b212f2011-02-18 05:51:58 +0000210 return 0;
211
212 return color_formats_table[format].blocksize;
213}
214
Jerome Glisse285484e2011-12-16 17:03:42 -0500215int r600_fmt_get_nblocksx(u32 format, u32 w)
Dave Airlie60b212f2011-02-18 05:51:58 +0000216{
217 unsigned bw;
Dan Carpentercf8a47d2011-02-26 04:48:18 +0300218
219 if (format >= ARRAY_SIZE(color_formats_table))
Dave Airlie60b212f2011-02-18 05:51:58 +0000220 return 0;
221
222 bw = color_formats_table[format].blockwidth;
223 if (bw == 0)
224 return 0;
225
226 return (w + bw - 1) / bw;
227}
228
Jerome Glisse285484e2011-12-16 17:03:42 -0500229int r600_fmt_get_nblocksy(u32 format, u32 h)
Dave Airlie60b212f2011-02-18 05:51:58 +0000230{
231 unsigned bh;
Dan Carpentercf8a47d2011-02-26 04:48:18 +0300232
233 if (format >= ARRAY_SIZE(color_formats_table))
Dave Airlie60b212f2011-02-18 05:51:58 +0000234 return 0;
235
236 bh = color_formats_table[format].blockheight;
237 if (bh == 0)
238 return 0;
239
240 return (h + bh - 1) / bh;
241}
242
Alex Deucher16790562010-11-14 20:24:35 -0500243struct array_mode_checker {
244 int array_mode;
245 u32 group_size;
246 u32 nbanks;
247 u32 npipes;
248 u32 nsamples;
Dave Airlie60b212f2011-02-18 05:51:58 +0000249 u32 blocksize;
Alex Deucher16790562010-11-14 20:24:35 -0500250};
251
252/* returns alignment in pixels for pitch/height/depth and bytes for base */
Andi Kleen488479e2011-10-13 16:08:41 -0700253static int r600_get_array_mode_alignment(struct array_mode_checker *values,
Alex Deucher16790562010-11-14 20:24:35 -0500254 u32 *pitch_align,
255 u32 *height_align,
256 u32 *depth_align,
257 u64 *base_align)
258{
259 u32 tile_width = 8;
260 u32 tile_height = 8;
261 u32 macro_tile_width = values->nbanks;
262 u32 macro_tile_height = values->npipes;
Dave Airlie60b212f2011-02-18 05:51:58 +0000263 u32 tile_bytes = tile_width * tile_height * values->blocksize * values->nsamples;
Alex Deucher16790562010-11-14 20:24:35 -0500264 u32 macro_tile_bytes = macro_tile_width * macro_tile_height * tile_bytes;
265
266 switch (values->array_mode) {
267 case ARRAY_LINEAR_GENERAL:
268 /* technically tile_width/_height for pitch/height */
269 *pitch_align = 1; /* tile_width */
270 *height_align = 1; /* tile_height */
271 *depth_align = 1;
272 *base_align = 1;
273 break;
274 case ARRAY_LINEAR_ALIGNED:
Dave Airlie60b212f2011-02-18 05:51:58 +0000275 *pitch_align = max((u32)64, (u32)(values->group_size / values->blocksize));
Jerome Glisse285484e2011-12-16 17:03:42 -0500276 *height_align = 1;
Alex Deucher16790562010-11-14 20:24:35 -0500277 *depth_align = 1;
278 *base_align = values->group_size;
279 break;
280 case ARRAY_1D_TILED_THIN1:
281 *pitch_align = max((u32)tile_width,
282 (u32)(values->group_size /
Dave Airlie60b212f2011-02-18 05:51:58 +0000283 (tile_height * values->blocksize * values->nsamples)));
Alex Deucher16790562010-11-14 20:24:35 -0500284 *height_align = tile_height;
285 *depth_align = 1;
286 *base_align = values->group_size;
287 break;
288 case ARRAY_2D_TILED_THIN1:
Jerome Glisse285484e2011-12-16 17:03:42 -0500289 *pitch_align = max((u32)macro_tile_width * tile_width,
290 (u32)((values->group_size * values->nbanks) /
291 (values->blocksize * values->nsamples * tile_width)));
Alex Deucher16790562010-11-14 20:24:35 -0500292 *height_align = macro_tile_height * tile_height;
293 *depth_align = 1;
294 *base_align = max(macro_tile_bytes,
Dave Airlie60b212f2011-02-18 05:51:58 +0000295 (*pitch_align) * values->blocksize * (*height_align) * values->nsamples);
Alex Deucher16790562010-11-14 20:24:35 -0500296 break;
297 default:
298 return -EINVAL;
299 }
300
301 return 0;
302}
303
Jerome Glisse961fb592010-02-10 22:30:05 +0000304static void r600_cs_track_init(struct r600_cs_track *track)
305{
306 int i;
307
Alex Deucher5f77df32010-03-26 14:52:32 -0400308 /* assume DX9 mode */
309 track->sq_config = DX9_CONSTS;
Jerome Glisse961fb592010-02-10 22:30:05 +0000310 for (i = 0; i < 8; i++) {
311 track->cb_color_base_last[i] = 0;
312 track->cb_color_size[i] = 0;
313 track->cb_color_size_idx[i] = 0;
314 track->cb_color_info[i] = 0;
Jerome Glisse285484e2011-12-16 17:03:42 -0500315 track->cb_color_view[i] = 0xFFFFFFFF;
Jerome Glisse961fb592010-02-10 22:30:05 +0000316 track->cb_color_bo[i] = NULL;
317 track->cb_color_bo_offset[i] = 0xFFFFFFFF;
Alex Deucher16790562010-11-14 20:24:35 -0500318 track->cb_color_bo_mc[i] = 0xFFFFFFFF;
Marek Olšák3b5ef592012-08-22 17:02:43 +0200319 track->cb_color_frag_bo[i] = NULL;
320 track->cb_color_frag_offset[i] = 0xFFFFFFFF;
321 track->cb_color_tile_bo[i] = NULL;
322 track->cb_color_tile_offset[i] = 0xFFFFFFFF;
323 track->cb_color_mask[i] = 0xFFFFFFFF;
Jerome Glisse961fb592010-02-10 22:30:05 +0000324 }
Marek Olšák523885d2012-08-24 14:27:36 +0200325 track->is_resolve = false;
Marek Olšák3b5ef592012-08-22 17:02:43 +0200326 track->nsamples = 16;
327 track->log_nsamples = 4;
Jerome Glisse961fb592010-02-10 22:30:05 +0000328 track->cb_target_mask = 0xFFFFFFFF;
329 track->cb_shader_mask = 0xFFFFFFFF;
Marek Olšák3c125132012-03-19 03:09:38 +0100330 track->cb_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +0000331 track->db_bo = NULL;
Alex Deucher16790562010-11-14 20:24:35 -0500332 track->db_bo_mc = 0xFFFFFFFF;
Jerome Glisse961fb592010-02-10 22:30:05 +0000333 /* assume the biggest format and that htile is enabled */
334 track->db_depth_info = 7 | (1 << 25);
335 track->db_depth_view = 0xFFFFC000;
336 track->db_depth_size = 0xFFFFFFFF;
337 track->db_depth_size_idx = 0;
338 track->db_depth_control = 0xFFFFFFFF;
Marek Olšák3c125132012-03-19 03:09:38 +0100339 track->db_dirty = true;
Jerome Glisse88f50c82012-03-21 19:18:21 -0400340 track->htile_bo = NULL;
341 track->htile_offset = 0xFFFFFFFF;
342 track->htile_surface = 0;
Marek Olšákdd220a02012-01-27 12:17:59 -0500343
344 for (i = 0; i < 4; i++) {
345 track->vgt_strmout_size[i] = 0;
346 track->vgt_strmout_bo[i] = NULL;
347 track->vgt_strmout_bo_offset[i] = 0xFFFFFFFF;
348 track->vgt_strmout_bo_mc[i] = 0xFFFFFFFF;
349 }
Marek Olšák3c125132012-03-19 03:09:38 +0100350 track->streamout_dirty = true;
Marek Olšák779923b2012-03-08 00:56:00 +0100351 track->sx_misc_kill_all_prims = false;
Jerome Glisse961fb592010-02-10 22:30:05 +0000352}
353
Andi Kleen488479e2011-10-13 16:08:41 -0700354static int r600_cs_track_validate_cb(struct radeon_cs_parser *p, int i)
Jerome Glisse961fb592010-02-10 22:30:05 +0000355{
356 struct r600_cs_track *track = p->track;
Dave Airlie60b212f2011-02-18 05:51:58 +0000357 u32 slice_tile_max, size, tmp;
Alex Deucher16790562010-11-14 20:24:35 -0500358 u32 height, height_align, pitch, pitch_align, depth_align;
359 u64 base_offset, base_align;
360 struct array_mode_checker array_check;
Jerome Glissef2e39222012-05-09 15:35:02 +0200361 volatile u32 *ib = p->ib.ptr;
Dave Airlief30df2f2010-10-21 13:55:40 +1000362 unsigned array_mode;
Dave Airlie60b212f2011-02-18 05:51:58 +0000363 u32 format;
Marek Olšák523885d2012-08-24 14:27:36 +0200364 /* When resolve is used, the second colorbuffer has always 1 sample. */
365 unsigned nsamples = track->is_resolve && i == 1 ? 1 : track->nsamples;
Jerome Glisse285484e2011-12-16 17:03:42 -0500366
Alex Deucher1729dd32010-08-06 02:54:05 -0400367 size = radeon_bo_size(track->cb_color_bo[i]) - track->cb_color_bo_offset[i];
Dave Airlie60b212f2011-02-18 05:51:58 +0000368 format = G_0280A0_FORMAT(track->cb_color_info[i]);
Jerome Glisse285484e2011-12-16 17:03:42 -0500369 if (!r600_fmt_is_valid_color(format)) {
Jerome Glisse961fb592010-02-10 22:30:05 +0000370 dev_warn(p->dev, "%s:%d cb invalid format %d for %d (0x%08X)\n",
Dave Airlie60b212f2011-02-18 05:51:58 +0000371 __func__, __LINE__, format,
Jerome Glisse961fb592010-02-10 22:30:05 +0000372 i, track->cb_color_info[i]);
373 return -EINVAL;
374 }
Alex Deucher16790562010-11-14 20:24:35 -0500375 /* pitch in pixels */
376 pitch = (G_028060_PITCH_TILE_MAX(track->cb_color_size[i]) + 1) * 8;
Jerome Glisse961fb592010-02-10 22:30:05 +0000377 slice_tile_max = G_028060_SLICE_TILE_MAX(track->cb_color_size[i]) + 1;
Dave Airlief30df2f2010-10-21 13:55:40 +1000378 slice_tile_max *= 64;
Alex Deucher16790562010-11-14 20:24:35 -0500379 height = slice_tile_max / pitch;
Jerome Glisse961fb592010-02-10 22:30:05 +0000380 if (height > 8192)
381 height = 8192;
Dave Airlief30df2f2010-10-21 13:55:40 +1000382 array_mode = G_0280A0_ARRAY_MODE(track->cb_color_info[i]);
Alex Deucher16790562010-11-14 20:24:35 -0500383
384 base_offset = track->cb_color_bo_mc[i] + track->cb_color_bo_offset[i];
385 array_check.array_mode = array_mode;
386 array_check.group_size = track->group_size;
387 array_check.nbanks = track->nbanks;
388 array_check.npipes = track->npipes;
Marek Olšák523885d2012-08-24 14:27:36 +0200389 array_check.nsamples = nsamples;
Jerome Glisse285484e2011-12-16 17:03:42 -0500390 array_check.blocksize = r600_fmt_get_blocksize(format);
Alex Deucher16790562010-11-14 20:24:35 -0500391 if (r600_get_array_mode_alignment(&array_check,
392 &pitch_align, &height_align, &depth_align, &base_align)) {
393 dev_warn(p->dev, "%s invalid tiling %d for %d (0x%08X)\n", __func__,
394 G_0280A0_ARRAY_MODE(track->cb_color_info[i]), i,
395 track->cb_color_info[i]);
396 return -EINVAL;
397 }
Dave Airlief30df2f2010-10-21 13:55:40 +1000398 switch (array_mode) {
Jerome Glisse961fb592010-02-10 22:30:05 +0000399 case V_0280A0_ARRAY_LINEAR_GENERAL:
Alex Deucher40e2a5c2010-06-04 18:41:42 -0400400 break;
Jerome Glisse961fb592010-02-10 22:30:05 +0000401 case V_0280A0_ARRAY_LINEAR_ALIGNED:
Jerome Glisse961fb592010-02-10 22:30:05 +0000402 break;
403 case V_0280A0_ARRAY_1D_TILED_THIN1:
Alex Deucher8f895da2010-10-26 20:22:42 -0400404 /* avoid breaking userspace */
405 if (height > 7)
406 height &= ~0x7;
Jerome Glisse961fb592010-02-10 22:30:05 +0000407 break;
408 case V_0280A0_ARRAY_2D_TILED_THIN1:
Jerome Glisse961fb592010-02-10 22:30:05 +0000409 break;
410 default:
411 dev_warn(p->dev, "%s invalid tiling %d for %d (0x%08X)\n", __func__,
412 G_0280A0_ARRAY_MODE(track->cb_color_info[i]), i,
413 track->cb_color_info[i]);
414 return -EINVAL;
415 }
Alex Deucher16790562010-11-14 20:24:35 -0500416
417 if (!IS_ALIGNED(pitch, pitch_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -0500418 dev_warn(p->dev, "%s:%d cb pitch (%d, 0x%x, %d) invalid\n",
419 __func__, __LINE__, pitch, pitch_align, array_mode);
Alex Deucher16790562010-11-14 20:24:35 -0500420 return -EINVAL;
421 }
422 if (!IS_ALIGNED(height, height_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -0500423 dev_warn(p->dev, "%s:%d cb height (%d, 0x%x, %d) invalid\n",
424 __func__, __LINE__, height, height_align, array_mode);
Alex Deucher16790562010-11-14 20:24:35 -0500425 return -EINVAL;
426 }
427 if (!IS_ALIGNED(base_offset, base_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -0500428 dev_warn(p->dev, "%s offset[%d] 0x%llx 0x%llx, %d not aligned\n", __func__, i,
429 base_offset, base_align, array_mode);
Alex Deucher16790562010-11-14 20:24:35 -0500430 return -EINVAL;
431 }
432
Jerome Glisse961fb592010-02-10 22:30:05 +0000433 /* check offset */
Marek Olšákfcdeefe2012-08-19 21:23:26 +0200434 tmp = r600_fmt_get_nblocksy(format, height) * r600_fmt_get_nblocksx(format, pitch) *
Marek Olšák523885d2012-08-24 14:27:36 +0200435 r600_fmt_get_blocksize(format) * nsamples;
Jerome Glisse285484e2011-12-16 17:03:42 -0500436 switch (array_mode) {
437 default:
438 case V_0280A0_ARRAY_LINEAR_GENERAL:
439 case V_0280A0_ARRAY_LINEAR_ALIGNED:
440 tmp += track->cb_color_view[i] & 0xFF;
441 break;
442 case V_0280A0_ARRAY_1D_TILED_THIN1:
443 case V_0280A0_ARRAY_2D_TILED_THIN1:
444 tmp += G_028080_SLICE_MAX(track->cb_color_view[i]) * tmp;
445 break;
446 }
Jerome Glisse961fb592010-02-10 22:30:05 +0000447 if ((tmp + track->cb_color_bo_offset[i]) > radeon_bo_size(track->cb_color_bo[i])) {
Dave Airlief30df2f2010-10-21 13:55:40 +1000448 if (array_mode == V_0280A0_ARRAY_LINEAR_GENERAL) {
449 /* the initial DDX does bad things with the CB size occasionally */
450 /* it rounds up height too far for slice tile max but the BO is smaller */
Alex Deuchera1a82132010-12-13 14:03:09 -0500451 /* r600c,g also seem to flush at bad times in some apps resulting in
452 * bogus values here. So for linear just allow anything to avoid breaking
453 * broken userspace.
454 */
Dave Airlief30df2f2010-10-21 13:55:40 +1000455 } else {
Marek Olšákc116cc92012-08-19 02:22:09 +0200456 dev_warn(p->dev, "%s offset[%d] %d %llu %d %lu too big (%d %d) (%d %d %d)\n",
Jerome Glisse285484e2011-12-16 17:03:42 -0500457 __func__, i, array_mode,
Alex Deucherc2049b32011-02-13 18:42:41 -0500458 track->cb_color_bo_offset[i], tmp,
Jerome Glisse285484e2011-12-16 17:03:42 -0500459 radeon_bo_size(track->cb_color_bo[i]),
460 pitch, height, r600_fmt_get_nblocksx(format, pitch),
461 r600_fmt_get_nblocksy(format, height),
462 r600_fmt_get_blocksize(format));
Dave Airlief30df2f2010-10-21 13:55:40 +1000463 return -EINVAL;
464 }
Alex Deucher40e2a5c2010-06-04 18:41:42 -0400465 }
Jerome Glisse961fb592010-02-10 22:30:05 +0000466 /* limit max tile */
Alex Deucher16790562010-11-14 20:24:35 -0500467 tmp = (height * pitch) >> 6;
Jerome Glisse961fb592010-02-10 22:30:05 +0000468 if (tmp < slice_tile_max)
469 slice_tile_max = tmp;
Alex Deucher16790562010-11-14 20:24:35 -0500470 tmp = S_028060_PITCH_TILE_MAX((pitch / 8) - 1) |
Jerome Glisse961fb592010-02-10 22:30:05 +0000471 S_028060_SLICE_TILE_MAX(slice_tile_max - 1);
472 ib[track->cb_color_size_idx[i]] = tmp;
Marek Olšákc116cc92012-08-19 02:22:09 +0200473
474 /* FMASK/CMASK */
475 switch (G_0280A0_TILE_MODE(track->cb_color_info[i])) {
476 case V_0280A0_TILE_DISABLE:
477 break;
478 case V_0280A0_FRAG_ENABLE:
479 if (track->nsamples > 1) {
480 uint32_t tile_max = G_028100_FMASK_TILE_MAX(track->cb_color_mask[i]);
481 /* the tile size is 8x8, but the size is in units of bits.
482 * for bytes, do just * 8. */
483 uint32_t bytes = track->nsamples * track->log_nsamples * 8 * (tile_max + 1);
484
485 if (bytes + track->cb_color_frag_offset[i] >
486 radeon_bo_size(track->cb_color_frag_bo[i])) {
487 dev_warn(p->dev, "%s FMASK_TILE_MAX too large "
488 "(tile_max=%u, bytes=%u, offset=%llu, bo_size=%lu)\n",
489 __func__, tile_max, bytes,
490 track->cb_color_frag_offset[i],
491 radeon_bo_size(track->cb_color_frag_bo[i]));
492 return -EINVAL;
493 }
494 }
495 /* fall through */
496 case V_0280A0_CLEAR_ENABLE:
497 {
498 uint32_t block_max = G_028100_CMASK_BLOCK_MAX(track->cb_color_mask[i]);
499 /* One block = 128x128 pixels, one 8x8 tile has 4 bits..
500 * (128*128) / (8*8) / 2 = 128 bytes per block. */
501 uint32_t bytes = (block_max + 1) * 128;
502
503 if (bytes + track->cb_color_tile_offset[i] >
504 radeon_bo_size(track->cb_color_tile_bo[i])) {
505 dev_warn(p->dev, "%s CMASK_BLOCK_MAX too large "
506 "(block_max=%u, bytes=%u, offset=%llu, bo_size=%lu)\n",
507 __func__, block_max, bytes,
508 track->cb_color_tile_offset[i],
509 radeon_bo_size(track->cb_color_tile_bo[i]));
510 return -EINVAL;
511 }
512 break;
513 }
514 default:
515 dev_warn(p->dev, "%s invalid tile mode\n", __func__);
516 return -EINVAL;
517 }
Jerome Glisse961fb592010-02-10 22:30:05 +0000518 return 0;
519}
520
Jerome Glisse88f50c82012-03-21 19:18:21 -0400521static int r600_cs_track_validate_db(struct radeon_cs_parser *p)
522{
523 struct r600_cs_track *track = p->track;
524 u32 nviews, bpe, ntiles, size, slice_tile_max, tmp;
525 u32 height_align, pitch_align, depth_align;
526 u32 pitch = 8192;
527 u32 height = 8192;
528 u64 base_offset, base_align;
529 struct array_mode_checker array_check;
530 int array_mode;
Jerome Glissef2e39222012-05-09 15:35:02 +0200531 volatile u32 *ib = p->ib.ptr;
Jerome Glisse88f50c82012-03-21 19:18:21 -0400532
533
534 if (track->db_bo == NULL) {
535 dev_warn(p->dev, "z/stencil with no depth buffer\n");
536 return -EINVAL;
537 }
538 switch (G_028010_FORMAT(track->db_depth_info)) {
539 case V_028010_DEPTH_16:
540 bpe = 2;
541 break;
542 case V_028010_DEPTH_X8_24:
543 case V_028010_DEPTH_8_24:
544 case V_028010_DEPTH_X8_24_FLOAT:
545 case V_028010_DEPTH_8_24_FLOAT:
546 case V_028010_DEPTH_32_FLOAT:
547 bpe = 4;
548 break;
549 case V_028010_DEPTH_X24_8_32_FLOAT:
550 bpe = 8;
551 break;
552 default:
553 dev_warn(p->dev, "z/stencil with invalid format %d\n", G_028010_FORMAT(track->db_depth_info));
554 return -EINVAL;
555 }
556 if ((track->db_depth_size & 0xFFFFFC00) == 0xFFFFFC00) {
557 if (!track->db_depth_size_idx) {
558 dev_warn(p->dev, "z/stencil buffer size not set\n");
559 return -EINVAL;
560 }
561 tmp = radeon_bo_size(track->db_bo) - track->db_offset;
562 tmp = (tmp / bpe) >> 6;
563 if (!tmp) {
564 dev_warn(p->dev, "z/stencil buffer too small (0x%08X %d %d %ld)\n",
565 track->db_depth_size, bpe, track->db_offset,
566 radeon_bo_size(track->db_bo));
567 return -EINVAL;
568 }
569 ib[track->db_depth_size_idx] = S_028000_SLICE_TILE_MAX(tmp - 1) | (track->db_depth_size & 0x3FF);
570 } else {
571 size = radeon_bo_size(track->db_bo);
572 /* pitch in pixels */
573 pitch = (G_028000_PITCH_TILE_MAX(track->db_depth_size) + 1) * 8;
574 slice_tile_max = G_028000_SLICE_TILE_MAX(track->db_depth_size) + 1;
575 slice_tile_max *= 64;
576 height = slice_tile_max / pitch;
577 if (height > 8192)
578 height = 8192;
579 base_offset = track->db_bo_mc + track->db_offset;
580 array_mode = G_028010_ARRAY_MODE(track->db_depth_info);
581 array_check.array_mode = array_mode;
582 array_check.group_size = track->group_size;
583 array_check.nbanks = track->nbanks;
584 array_check.npipes = track->npipes;
585 array_check.nsamples = track->nsamples;
586 array_check.blocksize = bpe;
587 if (r600_get_array_mode_alignment(&array_check,
588 &pitch_align, &height_align, &depth_align, &base_align)) {
589 dev_warn(p->dev, "%s invalid tiling %d (0x%08X)\n", __func__,
590 G_028010_ARRAY_MODE(track->db_depth_info),
591 track->db_depth_info);
592 return -EINVAL;
593 }
594 switch (array_mode) {
595 case V_028010_ARRAY_1D_TILED_THIN1:
596 /* don't break userspace */
597 height &= ~0x7;
598 break;
599 case V_028010_ARRAY_2D_TILED_THIN1:
600 break;
601 default:
602 dev_warn(p->dev, "%s invalid tiling %d (0x%08X)\n", __func__,
603 G_028010_ARRAY_MODE(track->db_depth_info),
604 track->db_depth_info);
605 return -EINVAL;
606 }
607
608 if (!IS_ALIGNED(pitch, pitch_align)) {
609 dev_warn(p->dev, "%s:%d db pitch (%d, 0x%x, %d) invalid\n",
610 __func__, __LINE__, pitch, pitch_align, array_mode);
611 return -EINVAL;
612 }
613 if (!IS_ALIGNED(height, height_align)) {
614 dev_warn(p->dev, "%s:%d db height (%d, 0x%x, %d) invalid\n",
615 __func__, __LINE__, height, height_align, array_mode);
616 return -EINVAL;
617 }
618 if (!IS_ALIGNED(base_offset, base_align)) {
619 dev_warn(p->dev, "%s offset 0x%llx, 0x%llx, %d not aligned\n", __func__,
620 base_offset, base_align, array_mode);
621 return -EINVAL;
622 }
623
624 ntiles = G_028000_SLICE_TILE_MAX(track->db_depth_size) + 1;
625 nviews = G_028004_SLICE_MAX(track->db_depth_view) + 1;
Marek Olšákfcdeefe2012-08-19 21:23:26 +0200626 tmp = ntiles * bpe * 64 * nviews * track->nsamples;
Jerome Glisse88f50c82012-03-21 19:18:21 -0400627 if ((tmp + track->db_offset) > radeon_bo_size(track->db_bo)) {
628 dev_warn(p->dev, "z/stencil buffer (%d) too small (0x%08X %d %d %d -> %u have %lu)\n",
629 array_mode,
630 track->db_depth_size, ntiles, nviews, bpe, tmp + track->db_offset,
631 radeon_bo_size(track->db_bo));
632 return -EINVAL;
633 }
634 }
635
636 /* hyperz */
637 if (G_028010_TILE_SURFACE_ENABLE(track->db_depth_info)) {
638 unsigned long size;
639 unsigned nbx, nby;
640
641 if (track->htile_bo == NULL) {
642 dev_warn(p->dev, "%s:%d htile enabled without htile surface 0x%08x\n",
643 __func__, __LINE__, track->db_depth_info);
644 return -EINVAL;
645 }
646 if ((track->db_depth_size & 0xFFFFFC00) == 0xFFFFFC00) {
647 dev_warn(p->dev, "%s:%d htile can't be enabled with bogus db_depth_size 0x%08x\n",
648 __func__, __LINE__, track->db_depth_size);
649 return -EINVAL;
650 }
651
652 nbx = pitch;
653 nby = height;
654 if (G_028D24_LINEAR(track->htile_surface)) {
655 /* nbx must be 16 htiles aligned == 16 * 8 pixel aligned */
656 nbx = round_up(nbx, 16 * 8);
657 /* nby is npipes htiles aligned == npipes * 8 pixel aligned */
658 nby = round_up(nby, track->npipes * 8);
659 } else {
Jerome Glisse4ac05332012-12-13 12:08:11 -0500660 /* always assume 8x8 htile */
Jerome Glisse88f50c82012-03-21 19:18:21 -0400661 /* align is htile align * 8, htile align vary according to
662 * number of pipe and tile width and nby
663 */
664 switch (track->npipes) {
665 case 8:
Jerome Glisse4ac05332012-12-13 12:08:11 -0500666 /* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
667 nbx = round_up(nbx, 64 * 8);
668 nby = round_up(nby, 64 * 8);
Jerome Glisse88f50c82012-03-21 19:18:21 -0400669 break;
670 case 4:
Jerome Glisse4ac05332012-12-13 12:08:11 -0500671 /* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
672 nbx = round_up(nbx, 64 * 8);
673 nby = round_up(nby, 32 * 8);
Jerome Glisse88f50c82012-03-21 19:18:21 -0400674 break;
675 case 2:
Jerome Glisse4ac05332012-12-13 12:08:11 -0500676 /* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
677 nbx = round_up(nbx, 32 * 8);
678 nby = round_up(nby, 32 * 8);
Jerome Glisse88f50c82012-03-21 19:18:21 -0400679 break;
680 case 1:
Jerome Glisse4ac05332012-12-13 12:08:11 -0500681 /* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
682 nbx = round_up(nbx, 32 * 8);
683 nby = round_up(nby, 16 * 8);
Jerome Glisse88f50c82012-03-21 19:18:21 -0400684 break;
685 default:
686 dev_warn(p->dev, "%s:%d invalid num pipes %d\n",
687 __func__, __LINE__, track->npipes);
688 return -EINVAL;
689 }
690 }
691 /* compute number of htile */
Jerome Glisse4ac05332012-12-13 12:08:11 -0500692 nbx = nbx >> 3;
693 nby = nby >> 3;
694 /* size must be aligned on npipes * 2K boundary */
695 size = roundup(nbx * nby * 4, track->npipes * (2 << 10));
Jerome Glisse88f50c82012-03-21 19:18:21 -0400696 size += track->htile_offset;
697
698 if (size > radeon_bo_size(track->htile_bo)) {
699 dev_warn(p->dev, "%s:%d htile surface too small %ld for %ld (%d %d)\n",
700 __func__, __LINE__, radeon_bo_size(track->htile_bo),
701 size, nbx, nby);
702 return -EINVAL;
703 }
704 }
705
706 track->db_dirty = false;
707 return 0;
708}
709
Jerome Glisse961fb592010-02-10 22:30:05 +0000710static int r600_cs_track_check(struct radeon_cs_parser *p)
711{
712 struct r600_cs_track *track = p->track;
713 u32 tmp;
714 int r, i;
Jerome Glisse961fb592010-02-10 22:30:05 +0000715
716 /* on legacy kernel we don't perform advanced check */
717 if (p->rdev == NULL)
718 return 0;
Marek Olšákdd220a02012-01-27 12:17:59 -0500719
720 /* check streamout */
Marek Olšák3c125132012-03-19 03:09:38 +0100721 if (track->streamout_dirty && track->vgt_strmout_en) {
Marek Olšákdd220a02012-01-27 12:17:59 -0500722 for (i = 0; i < 4; i++) {
723 if (track->vgt_strmout_buffer_en & (1 << i)) {
724 if (track->vgt_strmout_bo[i]) {
725 u64 offset = (u64)track->vgt_strmout_bo_offset[i] +
726 (u64)track->vgt_strmout_size[i];
727 if (offset > radeon_bo_size(track->vgt_strmout_bo[i])) {
728 DRM_ERROR("streamout %d bo too small: 0x%llx, 0x%lx\n",
729 i, offset,
730 radeon_bo_size(track->vgt_strmout_bo[i]));
731 return -EINVAL;
732 }
733 } else {
734 dev_warn(p->dev, "No buffer for streamout %d\n", i);
735 return -EINVAL;
736 }
737 }
738 }
Marek Olšák3c125132012-03-19 03:09:38 +0100739 track->streamout_dirty = false;
Jerome Glisse961fb592010-02-10 22:30:05 +0000740 }
Marek Olšákdd220a02012-01-27 12:17:59 -0500741
Marek Olšák779923b2012-03-08 00:56:00 +0100742 if (track->sx_misc_kill_all_prims)
743 return 0;
744
Jerome Glisse961fb592010-02-10 22:30:05 +0000745 /* check that we have a cb for each enabled target, we don't check
746 * shader_mask because it seems mesa isn't always setting it :(
747 */
Marek Olšák3c125132012-03-19 03:09:38 +0100748 if (track->cb_dirty) {
749 tmp = track->cb_target_mask;
Marek Olšák523885d2012-08-24 14:27:36 +0200750
751 /* We must check both colorbuffers for RESOLVE. */
752 if (track->is_resolve) {
753 tmp |= 0xff;
754 }
755
Marek Olšák3c125132012-03-19 03:09:38 +0100756 for (i = 0; i < 8; i++) {
757 if ((tmp >> (i * 4)) & 0xF) {
758 /* at least one component is enabled */
759 if (track->cb_color_bo[i] == NULL) {
760 dev_warn(p->dev, "%s:%d mask 0x%08X | 0x%08X no cb for %d\n",
761 __func__, __LINE__, track->cb_target_mask, track->cb_shader_mask, i);
762 return -EINVAL;
763 }
764 /* perform rewrite of CB_COLOR[0-7]_SIZE */
765 r = r600_cs_track_validate_cb(p, i);
766 if (r)
767 return r;
Jerome Glisse961fb592010-02-10 22:30:05 +0000768 }
Jerome Glisse961fb592010-02-10 22:30:05 +0000769 }
Marek Olšák3c125132012-03-19 03:09:38 +0100770 track->cb_dirty = false;
Jerome Glisse961fb592010-02-10 22:30:05 +0000771 }
Alex Deucher16790562010-11-14 20:24:35 -0500772
Jerome Glisse88f50c82012-03-21 19:18:21 -0400773 /* Check depth buffer */
Marek Olšák0f457e42012-07-29 16:24:57 +0200774 if (track->db_dirty &&
775 G_028010_FORMAT(track->db_depth_info) != V_028010_DEPTH_INVALID &&
776 (G_028800_STENCIL_ENABLE(track->db_depth_control) ||
777 G_028800_Z_ENABLE(track->db_depth_control))) {
Jerome Glisse88f50c82012-03-21 19:18:21 -0400778 r = r600_cs_track_validate_db(p);
779 if (r)
780 return r;
Jerome Glisse961fb592010-02-10 22:30:05 +0000781 }
Jerome Glisse88f50c82012-03-21 19:18:21 -0400782
Jerome Glisse961fb592010-02-10 22:30:05 +0000783 return 0;
784}
785
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000786/**
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000787 * r600_cs_packet_next_reloc_mm() - parse next packet which should be reloc packet3
788 * @parser: parser structure holding parsing context.
789 * @data: pointer to relocation data
790 * @offset_start: starting offset
791 * @offset_mask: offset mask (to align start offset on)
792 * @reloc: reloc informations
793 *
794 * Check next packet is relocation packet3, do bo validation and compute
795 * GPU offset using the provided start.
796 **/
797static int r600_cs_packet_next_reloc_mm(struct radeon_cs_parser *p,
798 struct radeon_cs_reloc **cs_reloc)
799{
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000800 struct radeon_cs_chunk *relocs_chunk;
801 struct radeon_cs_packet p3reloc;
802 unsigned idx;
803 int r;
804
805 if (p->chunk_relocs_idx == -1) {
806 DRM_ERROR("No relocation chunk !\n");
807 return -EINVAL;
808 }
809 *cs_reloc = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000810 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
Ilija Hadzicc38f34b2013-01-02 18:27:41 -0500811 r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000812 if (r) {
813 return r;
814 }
815 p->idx += p3reloc.count + 2;
816 if (p3reloc.type != PACKET_TYPE3 || p3reloc.opcode != PACKET3_NOP) {
817 DRM_ERROR("No packet3 for relocation for packet at %d.\n",
818 p3reloc.idx);
819 return -EINVAL;
820 }
Dave Airlie513bcb42009-09-23 16:56:27 +1000821 idx = radeon_get_ib_value(p, p3reloc.idx + 1);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000822 if (idx >= relocs_chunk->length_dw) {
823 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
824 idx, relocs_chunk->length_dw);
825 return -EINVAL;
826 }
827 /* FIXME: we assume reloc size is 4 dwords */
828 *cs_reloc = p->relocs_ptr[(idx / 4)];
829 return 0;
830}
831
832/**
833 * r600_cs_packet_next_reloc_nomm() - parse next packet which should be reloc packet3
834 * @parser: parser structure holding parsing context.
835 * @data: pointer to relocation data
836 * @offset_start: starting offset
837 * @offset_mask: offset mask (to align start offset on)
838 * @reloc: reloc informations
839 *
840 * Check next packet is relocation packet3, do bo validation and compute
841 * GPU offset using the provided start.
842 **/
843static int r600_cs_packet_next_reloc_nomm(struct radeon_cs_parser *p,
844 struct radeon_cs_reloc **cs_reloc)
845{
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000846 struct radeon_cs_chunk *relocs_chunk;
847 struct radeon_cs_packet p3reloc;
848 unsigned idx;
849 int r;
850
851 if (p->chunk_relocs_idx == -1) {
852 DRM_ERROR("No relocation chunk !\n");
853 return -EINVAL;
854 }
855 *cs_reloc = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000856 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
Ilija Hadzicc38f34b2013-01-02 18:27:41 -0500857 r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000858 if (r) {
859 return r;
860 }
861 p->idx += p3reloc.count + 2;
862 if (p3reloc.type != PACKET_TYPE3 || p3reloc.opcode != PACKET3_NOP) {
863 DRM_ERROR("No packet3 for relocation for packet at %d.\n",
864 p3reloc.idx);
865 return -EINVAL;
866 }
Dave Airlie513bcb42009-09-23 16:56:27 +1000867 idx = radeon_get_ib_value(p, p3reloc.idx + 1);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000868 if (idx >= relocs_chunk->length_dw) {
869 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
870 idx, relocs_chunk->length_dw);
871 return -EINVAL;
872 }
Julia Lawalle265f39e2009-12-19 08:16:33 +0100873 *cs_reloc = p->relocs;
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000874 (*cs_reloc)->lobj.gpu_offset = (u64)relocs_chunk->kdata[idx + 3] << 32;
875 (*cs_reloc)->lobj.gpu_offset |= relocs_chunk->kdata[idx + 0];
876 return 0;
877}
878
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400879/**
Ilija Hadzic40592a12013-01-02 18:27:43 -0500880 * r600_cs_packet_parse_vline() - parse userspace VLINE packet
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400881 * @parser: parser structure holding parsing context.
882 *
Ilija Hadzic40592a12013-01-02 18:27:43 -0500883 * This is an R600-specific function for parsing VLINE packets.
884 * Real work is done by r600_cs_common_vline_parse function.
885 * Here we just set up ASIC-specific register table and call
886 * the common implementation function.
887 */
888static int r600_cs_packet_parse_vline(struct radeon_cs_parser *p)
889{
890 static uint32_t vline_start_end[2] = {AVIVO_D1MODE_VLINE_START_END,
891 AVIVO_D2MODE_VLINE_START_END};
892 static uint32_t vline_status[2] = {AVIVO_D1MODE_VLINE_STATUS,
893 AVIVO_D2MODE_VLINE_STATUS};
894
895 return r600_cs_common_vline_parse(p, vline_start_end, vline_status);
896}
897
898/**
899 * r600_cs_common_vline_parse() - common vline parser
900 * @parser: parser structure holding parsing context.
901 * @vline_start_end: table of vline_start_end registers
902 * @vline_status: table of vline_status registers
903 *
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400904 * Userspace sends a special sequence for VLINE waits.
905 * PACKET0 - VLINE_START_END + value
906 * PACKET3 - WAIT_REG_MEM poll vline status reg
907 * RELOC (P3) - crtc_id in reloc.
908 *
909 * This function parses this and relocates the VLINE START END
910 * and WAIT_REG_MEM packets to the correct crtc.
911 * It also detects a switched off crtc and nulls out the
Ilija Hadzic40592a12013-01-02 18:27:43 -0500912 * wait in that case. This function is common for all ASICs that
913 * are R600 and newer. The parsing algorithm is the same, and only
914 * differs in which registers are used.
915 *
916 * Caller is the ASIC-specific function which passes the parser
917 * context and ASIC-specific register table
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400918 */
Ilija Hadzic40592a12013-01-02 18:27:43 -0500919int r600_cs_common_vline_parse(struct radeon_cs_parser *p,
920 uint32_t *vline_start_end,
921 uint32_t *vline_status)
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400922{
923 struct drm_mode_object *obj;
924 struct drm_crtc *crtc;
925 struct radeon_crtc *radeon_crtc;
926 struct radeon_cs_packet p3reloc, wait_reg_mem;
927 int crtc_id;
928 int r;
929 uint32_t header, h_idx, reg, wait_reg_mem_info;
930 volatile uint32_t *ib;
931
Jerome Glissef2e39222012-05-09 15:35:02 +0200932 ib = p->ib.ptr;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400933
934 /* parse the WAIT_REG_MEM */
Ilija Hadzicc38f34b2013-01-02 18:27:41 -0500935 r = radeon_cs_packet_parse(p, &wait_reg_mem, p->idx);
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400936 if (r)
937 return r;
938
939 /* check its a WAIT_REG_MEM */
940 if (wait_reg_mem.type != PACKET_TYPE3 ||
941 wait_reg_mem.opcode != PACKET3_WAIT_REG_MEM) {
942 DRM_ERROR("vline wait missing WAIT_REG_MEM segment\n");
Paul Bollea3a88a62011-03-16 22:10:06 +0100943 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400944 }
945
946 wait_reg_mem_info = radeon_get_ib_value(p, wait_reg_mem.idx + 1);
947 /* bit 4 is reg (0) or mem (1) */
948 if (wait_reg_mem_info & 0x10) {
Ilija Hadzic40592a12013-01-02 18:27:43 -0500949 DRM_ERROR("vline WAIT_REG_MEM waiting on MEM instead of REG\n");
Paul Bollea3a88a62011-03-16 22:10:06 +0100950 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400951 }
952 /* waiting for value to be equal */
953 if ((wait_reg_mem_info & 0x7) != 0x3) {
954 DRM_ERROR("vline WAIT_REG_MEM function not equal\n");
Paul Bollea3a88a62011-03-16 22:10:06 +0100955 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400956 }
Ilija Hadzic40592a12013-01-02 18:27:43 -0500957 if ((radeon_get_ib_value(p, wait_reg_mem.idx + 2) << 2) != vline_status[0]) {
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400958 DRM_ERROR("vline WAIT_REG_MEM bad reg\n");
Paul Bollea3a88a62011-03-16 22:10:06 +0100959 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400960 }
961
Ilija Hadzic40592a12013-01-02 18:27:43 -0500962 if (radeon_get_ib_value(p, wait_reg_mem.idx + 5) != RADEON_VLINE_STAT) {
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400963 DRM_ERROR("vline WAIT_REG_MEM bad bit mask\n");
Paul Bollea3a88a62011-03-16 22:10:06 +0100964 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400965 }
966
967 /* jump over the NOP */
Ilija Hadzicc38f34b2013-01-02 18:27:41 -0500968 r = radeon_cs_packet_parse(p, &p3reloc, p->idx + wait_reg_mem.count + 2);
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400969 if (r)
970 return r;
971
972 h_idx = p->idx - 2;
973 p->idx += wait_reg_mem.count + 2;
974 p->idx += p3reloc.count + 2;
975
976 header = radeon_get_ib_value(p, h_idx);
977 crtc_id = radeon_get_ib_value(p, h_idx + 2 + 7 + 1);
Dave Airlied4ac6a02009-10-08 11:32:49 +1000978 reg = CP_PACKET0_GET_REG(header);
Dave Airlie29508eb2010-07-22 09:57:13 +1000979
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400980 obj = drm_mode_object_find(p->rdev->ddev, crtc_id, DRM_MODE_OBJECT_CRTC);
981 if (!obj) {
982 DRM_ERROR("cannot find crtc %d\n", crtc_id);
Paul Bollea3a88a62011-03-16 22:10:06 +0100983 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400984 }
985 crtc = obj_to_crtc(obj);
986 radeon_crtc = to_radeon_crtc(crtc);
987 crtc_id = radeon_crtc->crtc_id;
988
989 if (!crtc->enabled) {
Ilija Hadzic40592a12013-01-02 18:27:43 -0500990 /* CRTC isn't enabled - we need to nop out the WAIT_REG_MEM */
Alex Deucher2f67c6e2009-09-25 16:35:11 -0400991 ib[h_idx + 2] = PACKET2(0);
992 ib[h_idx + 3] = PACKET2(0);
993 ib[h_idx + 4] = PACKET2(0);
994 ib[h_idx + 5] = PACKET2(0);
995 ib[h_idx + 6] = PACKET2(0);
996 ib[h_idx + 7] = PACKET2(0);
997 ib[h_idx + 8] = PACKET2(0);
Ilija Hadzic40592a12013-01-02 18:27:43 -0500998 } else if (reg == vline_start_end[0]) {
999 header &= ~R600_CP_PACKET0_REG_MASK;
1000 header |= vline_start_end[crtc_id] >> 2;
Alex Deucher2f67c6e2009-09-25 16:35:11 -04001001 ib[h_idx] = header;
Ilija Hadzic40592a12013-01-02 18:27:43 -05001002 ib[h_idx + 4] = vline_status[crtc_id] >> 2;
1003 } else {
1004 DRM_ERROR("unknown crtc reloc\n");
1005 return -EINVAL;
Alex Deucher2f67c6e2009-09-25 16:35:11 -04001006 }
Paul Bollea3a88a62011-03-16 22:10:06 +01001007 return 0;
Alex Deucher2f67c6e2009-09-25 16:35:11 -04001008}
1009
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001010static int r600_packet0_check(struct radeon_cs_parser *p,
1011 struct radeon_cs_packet *pkt,
1012 unsigned idx, unsigned reg)
1013{
Alex Deucher2f67c6e2009-09-25 16:35:11 -04001014 int r;
1015
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001016 switch (reg) {
1017 case AVIVO_D1MODE_VLINE_START_END:
Alex Deucher2f67c6e2009-09-25 16:35:11 -04001018 r = r600_cs_packet_parse_vline(p);
1019 if (r) {
1020 DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
1021 idx, reg);
1022 return r;
1023 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001024 break;
1025 default:
1026 printk(KERN_ERR "Forbidden register 0x%04X in cs at %d\n",
1027 reg, idx);
1028 return -EINVAL;
1029 }
1030 return 0;
1031}
1032
1033static int r600_cs_parse_packet0(struct radeon_cs_parser *p,
1034 struct radeon_cs_packet *pkt)
1035{
1036 unsigned reg, i;
1037 unsigned idx;
1038 int r;
1039
1040 idx = pkt->idx + 1;
1041 reg = pkt->reg;
1042 for (i = 0; i <= pkt->count; i++, idx++, reg += 4) {
1043 r = r600_packet0_check(p, pkt, idx, reg);
1044 if (r) {
1045 return r;
1046 }
1047 }
1048 return 0;
1049}
1050
Jerome Glisse961fb592010-02-10 22:30:05 +00001051/**
1052 * r600_cs_check_reg() - check if register is authorized or not
1053 * @parser: parser structure holding parsing context
1054 * @reg: register we are testing
1055 * @idx: index into the cs buffer
1056 *
1057 * This function will test against r600_reg_safe_bm and return 0
1058 * if register is safe. If register is not flag as safe this function
1059 * will test it against a list of register needind special handling.
1060 */
Andi Kleen488479e2011-10-13 16:08:41 -07001061static int r600_cs_check_reg(struct radeon_cs_parser *p, u32 reg, u32 idx)
Jerome Glisse961fb592010-02-10 22:30:05 +00001062{
1063 struct r600_cs_track *track = (struct r600_cs_track *)p->track;
1064 struct radeon_cs_reloc *reloc;
Jerome Glisse961fb592010-02-10 22:30:05 +00001065 u32 m, i, tmp, *ib;
1066 int r;
1067
1068 i = (reg >> 7);
Dan Carpenter88498832011-07-27 09:53:40 +00001069 if (i >= ARRAY_SIZE(r600_reg_safe_bm)) {
Jerome Glisse961fb592010-02-10 22:30:05 +00001070 dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
1071 return -EINVAL;
1072 }
1073 m = 1 << ((reg >> 2) & 31);
1074 if (!(r600_reg_safe_bm[i] & m))
1075 return 0;
Jerome Glissef2e39222012-05-09 15:35:02 +02001076 ib = p->ib.ptr;
Jerome Glisse961fb592010-02-10 22:30:05 +00001077 switch (reg) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001078 /* force following reg to 0 in an attempt to disable out buffer
Jerome Glisse961fb592010-02-10 22:30:05 +00001079 * which will need us to better understand how it works to perform
1080 * security check on it (Jerome)
1081 */
1082 case R_0288A8_SQ_ESGS_RING_ITEMSIZE:
1083 case R_008C44_SQ_ESGS_RING_SIZE:
1084 case R_0288B0_SQ_ESTMP_RING_ITEMSIZE:
1085 case R_008C54_SQ_ESTMP_RING_SIZE:
1086 case R_0288C0_SQ_FBUF_RING_ITEMSIZE:
1087 case R_008C74_SQ_FBUF_RING_SIZE:
1088 case R_0288B4_SQ_GSTMP_RING_ITEMSIZE:
1089 case R_008C5C_SQ_GSTMP_RING_SIZE:
1090 case R_0288AC_SQ_GSVS_RING_ITEMSIZE:
1091 case R_008C4C_SQ_GSVS_RING_SIZE:
1092 case R_0288BC_SQ_PSTMP_RING_ITEMSIZE:
1093 case R_008C6C_SQ_PSTMP_RING_SIZE:
1094 case R_0288C4_SQ_REDUC_RING_ITEMSIZE:
1095 case R_008C7C_SQ_REDUC_RING_SIZE:
1096 case R_0288B8_SQ_VSTMP_RING_ITEMSIZE:
1097 case R_008C64_SQ_VSTMP_RING_SIZE:
1098 case R_0288C8_SQ_GS_VERT_ITEMSIZE:
1099 /* get value to populate the IB don't remove */
1100 tmp =radeon_get_ib_value(p, idx);
1101 ib[idx] = 0;
1102 break;
Alex Deucher5f77df32010-03-26 14:52:32 -04001103 case SQ_CONFIG:
1104 track->sq_config = radeon_get_ib_value(p, idx);
1105 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001106 case R_028800_DB_DEPTH_CONTROL:
1107 track->db_depth_control = radeon_get_ib_value(p, idx);
Marek Olšák3c125132012-03-19 03:09:38 +01001108 track->db_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001109 break;
1110 case R_028010_DB_DEPTH_INFO:
Jerome Glisse721604a2012-01-05 22:11:05 -05001111 if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS) &&
Ilija Hadzic9ffb7a62013-01-02 18:27:42 -05001112 radeon_cs_packet_next_is_pkt3_nop(p)) {
Alex Deucher7f813372010-05-20 12:43:52 -04001113 r = r600_cs_packet_next_reloc(p, &reloc);
1114 if (r) {
1115 dev_warn(p->dev, "bad SET_CONTEXT_REG "
1116 "0x%04X\n", reg);
1117 return -EINVAL;
1118 }
1119 track->db_depth_info = radeon_get_ib_value(p, idx);
1120 ib[idx] &= C_028010_ARRAY_MODE;
1121 track->db_depth_info &= C_028010_ARRAY_MODE;
1122 if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO) {
1123 ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
1124 track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
1125 } else {
1126 ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
1127 track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
1128 }
Marek Olšák3c125132012-03-19 03:09:38 +01001129 } else {
Alex Deucher7f813372010-05-20 12:43:52 -04001130 track->db_depth_info = radeon_get_ib_value(p, idx);
Marek Olšák3c125132012-03-19 03:09:38 +01001131 }
1132 track->db_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001133 break;
1134 case R_028004_DB_DEPTH_VIEW:
1135 track->db_depth_view = radeon_get_ib_value(p, idx);
Marek Olšák3c125132012-03-19 03:09:38 +01001136 track->db_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001137 break;
1138 case R_028000_DB_DEPTH_SIZE:
1139 track->db_depth_size = radeon_get_ib_value(p, idx);
1140 track->db_depth_size_idx = idx;
Marek Olšák3c125132012-03-19 03:09:38 +01001141 track->db_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001142 break;
1143 case R_028AB0_VGT_STRMOUT_EN:
1144 track->vgt_strmout_en = radeon_get_ib_value(p, idx);
Marek Olšák3c125132012-03-19 03:09:38 +01001145 track->streamout_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001146 break;
1147 case R_028B20_VGT_STRMOUT_BUFFER_EN:
1148 track->vgt_strmout_buffer_en = radeon_get_ib_value(p, idx);
Marek Olšák3c125132012-03-19 03:09:38 +01001149 track->streamout_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001150 break;
Marek Olšákdd220a02012-01-27 12:17:59 -05001151 case VGT_STRMOUT_BUFFER_BASE_0:
1152 case VGT_STRMOUT_BUFFER_BASE_1:
1153 case VGT_STRMOUT_BUFFER_BASE_2:
1154 case VGT_STRMOUT_BUFFER_BASE_3:
1155 r = r600_cs_packet_next_reloc(p, &reloc);
1156 if (r) {
1157 dev_warn(p->dev, "bad SET_CONTEXT_REG "
1158 "0x%04X\n", reg);
1159 return -EINVAL;
1160 }
1161 tmp = (reg - VGT_STRMOUT_BUFFER_BASE_0) / 16;
1162 track->vgt_strmout_bo_offset[tmp] = radeon_get_ib_value(p, idx) << 8;
1163 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1164 track->vgt_strmout_bo[tmp] = reloc->robj;
1165 track->vgt_strmout_bo_mc[tmp] = reloc->lobj.gpu_offset;
Marek Olšák3c125132012-03-19 03:09:38 +01001166 track->streamout_dirty = true;
Marek Olšákdd220a02012-01-27 12:17:59 -05001167 break;
1168 case VGT_STRMOUT_BUFFER_SIZE_0:
1169 case VGT_STRMOUT_BUFFER_SIZE_1:
1170 case VGT_STRMOUT_BUFFER_SIZE_2:
1171 case VGT_STRMOUT_BUFFER_SIZE_3:
1172 tmp = (reg - VGT_STRMOUT_BUFFER_SIZE_0) / 16;
1173 /* size in register is DWs, convert to bytes */
1174 track->vgt_strmout_size[tmp] = radeon_get_ib_value(p, idx) * 4;
Marek Olšák3c125132012-03-19 03:09:38 +01001175 track->streamout_dirty = true;
Marek Olšákdd220a02012-01-27 12:17:59 -05001176 break;
1177 case CP_COHER_BASE:
1178 r = r600_cs_packet_next_reloc(p, &reloc);
1179 if (r) {
1180 dev_warn(p->dev, "missing reloc for CP_COHER_BASE "
1181 "0x%04X\n", reg);
1182 return -EINVAL;
1183 }
1184 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1185 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001186 case R_028238_CB_TARGET_MASK:
1187 track->cb_target_mask = radeon_get_ib_value(p, idx);
Marek Olšák3c125132012-03-19 03:09:38 +01001188 track->cb_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001189 break;
1190 case R_02823C_CB_SHADER_MASK:
1191 track->cb_shader_mask = radeon_get_ib_value(p, idx);
1192 break;
1193 case R_028C04_PA_SC_AA_CONFIG:
1194 tmp = G_028C04_MSAA_NUM_SAMPLES(radeon_get_ib_value(p, idx));
Marek Olšákc116cc92012-08-19 02:22:09 +02001195 track->log_nsamples = tmp;
Jerome Glisse961fb592010-02-10 22:30:05 +00001196 track->nsamples = 1 << tmp;
Marek Olšák3c125132012-03-19 03:09:38 +01001197 track->cb_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001198 break;
Marek Olšák523885d2012-08-24 14:27:36 +02001199 case R_028808_CB_COLOR_CONTROL:
1200 tmp = G_028808_SPECIAL_OP(radeon_get_ib_value(p, idx));
1201 track->is_resolve = tmp == V_028808_SPECIAL_RESOLVE_BOX;
1202 track->cb_dirty = true;
1203 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001204 case R_0280A0_CB_COLOR0_INFO:
1205 case R_0280A4_CB_COLOR1_INFO:
1206 case R_0280A8_CB_COLOR2_INFO:
1207 case R_0280AC_CB_COLOR3_INFO:
1208 case R_0280B0_CB_COLOR4_INFO:
1209 case R_0280B4_CB_COLOR5_INFO:
1210 case R_0280B8_CB_COLOR6_INFO:
1211 case R_0280BC_CB_COLOR7_INFO:
Jerome Glisse721604a2012-01-05 22:11:05 -05001212 if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS) &&
Ilija Hadzic9ffb7a62013-01-02 18:27:42 -05001213 radeon_cs_packet_next_is_pkt3_nop(p)) {
Alex Deucher7f813372010-05-20 12:43:52 -04001214 r = r600_cs_packet_next_reloc(p, &reloc);
1215 if (r) {
1216 dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
1217 return -EINVAL;
1218 }
1219 tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
1220 track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
1221 if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO) {
1222 ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
1223 track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
1224 } else if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO) {
1225 ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
1226 track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
1227 }
1228 } else {
1229 tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
1230 track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
1231 }
Marek Olšák3c125132012-03-19 03:09:38 +01001232 track->cb_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001233 break;
Jerome Glisse285484e2011-12-16 17:03:42 -05001234 case R_028080_CB_COLOR0_VIEW:
1235 case R_028084_CB_COLOR1_VIEW:
1236 case R_028088_CB_COLOR2_VIEW:
1237 case R_02808C_CB_COLOR3_VIEW:
1238 case R_028090_CB_COLOR4_VIEW:
1239 case R_028094_CB_COLOR5_VIEW:
1240 case R_028098_CB_COLOR6_VIEW:
1241 case R_02809C_CB_COLOR7_VIEW:
1242 tmp = (reg - R_028080_CB_COLOR0_VIEW) / 4;
1243 track->cb_color_view[tmp] = radeon_get_ib_value(p, idx);
Marek Olšák3c125132012-03-19 03:09:38 +01001244 track->cb_dirty = true;
Jerome Glisse285484e2011-12-16 17:03:42 -05001245 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001246 case R_028060_CB_COLOR0_SIZE:
1247 case R_028064_CB_COLOR1_SIZE:
1248 case R_028068_CB_COLOR2_SIZE:
1249 case R_02806C_CB_COLOR3_SIZE:
1250 case R_028070_CB_COLOR4_SIZE:
1251 case R_028074_CB_COLOR5_SIZE:
1252 case R_028078_CB_COLOR6_SIZE:
1253 case R_02807C_CB_COLOR7_SIZE:
1254 tmp = (reg - R_028060_CB_COLOR0_SIZE) / 4;
1255 track->cb_color_size[tmp] = radeon_get_ib_value(p, idx);
1256 track->cb_color_size_idx[tmp] = idx;
Marek Olšák3c125132012-03-19 03:09:38 +01001257 track->cb_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001258 break;
1259 /* This register were added late, there is userspace
1260 * which does provide relocation for those but set
1261 * 0 offset. In order to avoid breaking old userspace
1262 * we detect this and set address to point to last
1263 * CB_COLOR0_BASE, note that if userspace doesn't set
1264 * CB_COLOR0_BASE before this register we will report
1265 * error. Old userspace always set CB_COLOR0_BASE
1266 * before any of this.
1267 */
1268 case R_0280E0_CB_COLOR0_FRAG:
1269 case R_0280E4_CB_COLOR1_FRAG:
1270 case R_0280E8_CB_COLOR2_FRAG:
1271 case R_0280EC_CB_COLOR3_FRAG:
1272 case R_0280F0_CB_COLOR4_FRAG:
1273 case R_0280F4_CB_COLOR5_FRAG:
1274 case R_0280F8_CB_COLOR6_FRAG:
1275 case R_0280FC_CB_COLOR7_FRAG:
1276 tmp = (reg - R_0280E0_CB_COLOR0_FRAG) / 4;
Ilija Hadzic9ffb7a62013-01-02 18:27:42 -05001277 if (!radeon_cs_packet_next_is_pkt3_nop(p)) {
Jerome Glisse961fb592010-02-10 22:30:05 +00001278 if (!track->cb_color_base_last[tmp]) {
1279 dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
1280 return -EINVAL;
1281 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001282 track->cb_color_frag_bo[tmp] = track->cb_color_bo[tmp];
Marek Olšákc116cc92012-08-19 02:22:09 +02001283 track->cb_color_frag_offset[tmp] = track->cb_color_bo_offset[tmp];
1284 ib[idx] = track->cb_color_base_last[tmp];
Jerome Glisse961fb592010-02-10 22:30:05 +00001285 } else {
1286 r = r600_cs_packet_next_reloc(p, &reloc);
1287 if (r) {
1288 dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
1289 return -EINVAL;
1290 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001291 track->cb_color_frag_bo[tmp] = reloc->robj;
Marek Olšákc116cc92012-08-19 02:22:09 +02001292 track->cb_color_frag_offset[tmp] = (u64)ib[idx] << 8;
1293 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1294 }
1295 if (G_0280A0_TILE_MODE(track->cb_color_info[tmp])) {
1296 track->cb_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001297 }
1298 break;
1299 case R_0280C0_CB_COLOR0_TILE:
1300 case R_0280C4_CB_COLOR1_TILE:
1301 case R_0280C8_CB_COLOR2_TILE:
1302 case R_0280CC_CB_COLOR3_TILE:
1303 case R_0280D0_CB_COLOR4_TILE:
1304 case R_0280D4_CB_COLOR5_TILE:
1305 case R_0280D8_CB_COLOR6_TILE:
1306 case R_0280DC_CB_COLOR7_TILE:
1307 tmp = (reg - R_0280C0_CB_COLOR0_TILE) / 4;
Ilija Hadzic9ffb7a62013-01-02 18:27:42 -05001308 if (!radeon_cs_packet_next_is_pkt3_nop(p)) {
Jerome Glisse961fb592010-02-10 22:30:05 +00001309 if (!track->cb_color_base_last[tmp]) {
1310 dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
1311 return -EINVAL;
1312 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001313 track->cb_color_tile_bo[tmp] = track->cb_color_bo[tmp];
Marek Olšákc116cc92012-08-19 02:22:09 +02001314 track->cb_color_tile_offset[tmp] = track->cb_color_bo_offset[tmp];
1315 ib[idx] = track->cb_color_base_last[tmp];
Jerome Glisse961fb592010-02-10 22:30:05 +00001316 } else {
1317 r = r600_cs_packet_next_reloc(p, &reloc);
1318 if (r) {
1319 dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
1320 return -EINVAL;
1321 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001322 track->cb_color_tile_bo[tmp] = reloc->robj;
Marek Olšákc116cc92012-08-19 02:22:09 +02001323 track->cb_color_tile_offset[tmp] = (u64)ib[idx] << 8;
1324 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1325 }
1326 if (G_0280A0_TILE_MODE(track->cb_color_info[tmp])) {
1327 track->cb_dirty = true;
1328 }
1329 break;
1330 case R_028100_CB_COLOR0_MASK:
1331 case R_028104_CB_COLOR1_MASK:
1332 case R_028108_CB_COLOR2_MASK:
1333 case R_02810C_CB_COLOR3_MASK:
1334 case R_028110_CB_COLOR4_MASK:
1335 case R_028114_CB_COLOR5_MASK:
1336 case R_028118_CB_COLOR6_MASK:
1337 case R_02811C_CB_COLOR7_MASK:
1338 tmp = (reg - R_028100_CB_COLOR0_MASK) / 4;
Marek Olšák305a3d22012-08-22 17:02:42 +02001339 track->cb_color_mask[tmp] = radeon_get_ib_value(p, idx);
Marek Olšákc116cc92012-08-19 02:22:09 +02001340 if (G_0280A0_TILE_MODE(track->cb_color_info[tmp])) {
1341 track->cb_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001342 }
1343 break;
1344 case CB_COLOR0_BASE:
1345 case CB_COLOR1_BASE:
1346 case CB_COLOR2_BASE:
1347 case CB_COLOR3_BASE:
1348 case CB_COLOR4_BASE:
1349 case CB_COLOR5_BASE:
1350 case CB_COLOR6_BASE:
1351 case CB_COLOR7_BASE:
1352 r = r600_cs_packet_next_reloc(p, &reloc);
1353 if (r) {
1354 dev_warn(p->dev, "bad SET_CONTEXT_REG "
1355 "0x%04X\n", reg);
1356 return -EINVAL;
1357 }
Jerome Glisse7cb72ef2010-02-11 12:44:32 +01001358 tmp = (reg - CB_COLOR0_BASE) / 4;
Alex Deucher1729dd32010-08-06 02:54:05 -04001359 track->cb_color_bo_offset[tmp] = radeon_get_ib_value(p, idx) << 8;
Jerome Glisse961fb592010-02-10 22:30:05 +00001360 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
Jerome Glisse961fb592010-02-10 22:30:05 +00001361 track->cb_color_base_last[tmp] = ib[idx];
1362 track->cb_color_bo[tmp] = reloc->robj;
Alex Deucher16790562010-11-14 20:24:35 -05001363 track->cb_color_bo_mc[tmp] = reloc->lobj.gpu_offset;
Marek Olšák3c125132012-03-19 03:09:38 +01001364 track->cb_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001365 break;
1366 case DB_DEPTH_BASE:
1367 r = r600_cs_packet_next_reloc(p, &reloc);
1368 if (r) {
1369 dev_warn(p->dev, "bad SET_CONTEXT_REG "
1370 "0x%04X\n", reg);
1371 return -EINVAL;
1372 }
Alex Deucher1729dd32010-08-06 02:54:05 -04001373 track->db_offset = radeon_get_ib_value(p, idx) << 8;
Jerome Glisse961fb592010-02-10 22:30:05 +00001374 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1375 track->db_bo = reloc->robj;
Alex Deucher16790562010-11-14 20:24:35 -05001376 track->db_bo_mc = reloc->lobj.gpu_offset;
Marek Olšák3c125132012-03-19 03:09:38 +01001377 track->db_dirty = true;
Jerome Glisse961fb592010-02-10 22:30:05 +00001378 break;
1379 case DB_HTILE_DATA_BASE:
Jerome Glisse88f50c82012-03-21 19:18:21 -04001380 r = r600_cs_packet_next_reloc(p, &reloc);
1381 if (r) {
1382 dev_warn(p->dev, "bad SET_CONTEXT_REG "
1383 "0x%04X\n", reg);
1384 return -EINVAL;
1385 }
1386 track->htile_offset = radeon_get_ib_value(p, idx) << 8;
1387 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1388 track->htile_bo = reloc->robj;
1389 track->db_dirty = true;
1390 break;
1391 case DB_HTILE_SURFACE:
1392 track->htile_surface = radeon_get_ib_value(p, idx);
Jerome Glisse4ac05332012-12-13 12:08:11 -05001393 /* force 8x8 htile width and height */
1394 ib[idx] |= 3;
Jerome Glisse88f50c82012-03-21 19:18:21 -04001395 track->db_dirty = true;
1396 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001397 case SQ_PGM_START_FS:
1398 case SQ_PGM_START_ES:
1399 case SQ_PGM_START_VS:
1400 case SQ_PGM_START_GS:
1401 case SQ_PGM_START_PS:
Alex Deucher5f77df32010-03-26 14:52:32 -04001402 case SQ_ALU_CONST_CACHE_GS_0:
1403 case SQ_ALU_CONST_CACHE_GS_1:
1404 case SQ_ALU_CONST_CACHE_GS_2:
1405 case SQ_ALU_CONST_CACHE_GS_3:
1406 case SQ_ALU_CONST_CACHE_GS_4:
1407 case SQ_ALU_CONST_CACHE_GS_5:
1408 case SQ_ALU_CONST_CACHE_GS_6:
1409 case SQ_ALU_CONST_CACHE_GS_7:
1410 case SQ_ALU_CONST_CACHE_GS_8:
1411 case SQ_ALU_CONST_CACHE_GS_9:
1412 case SQ_ALU_CONST_CACHE_GS_10:
1413 case SQ_ALU_CONST_CACHE_GS_11:
1414 case SQ_ALU_CONST_CACHE_GS_12:
1415 case SQ_ALU_CONST_CACHE_GS_13:
1416 case SQ_ALU_CONST_CACHE_GS_14:
1417 case SQ_ALU_CONST_CACHE_GS_15:
1418 case SQ_ALU_CONST_CACHE_PS_0:
1419 case SQ_ALU_CONST_CACHE_PS_1:
1420 case SQ_ALU_CONST_CACHE_PS_2:
1421 case SQ_ALU_CONST_CACHE_PS_3:
1422 case SQ_ALU_CONST_CACHE_PS_4:
1423 case SQ_ALU_CONST_CACHE_PS_5:
1424 case SQ_ALU_CONST_CACHE_PS_6:
1425 case SQ_ALU_CONST_CACHE_PS_7:
1426 case SQ_ALU_CONST_CACHE_PS_8:
1427 case SQ_ALU_CONST_CACHE_PS_9:
1428 case SQ_ALU_CONST_CACHE_PS_10:
1429 case SQ_ALU_CONST_CACHE_PS_11:
1430 case SQ_ALU_CONST_CACHE_PS_12:
1431 case SQ_ALU_CONST_CACHE_PS_13:
1432 case SQ_ALU_CONST_CACHE_PS_14:
1433 case SQ_ALU_CONST_CACHE_PS_15:
1434 case SQ_ALU_CONST_CACHE_VS_0:
1435 case SQ_ALU_CONST_CACHE_VS_1:
1436 case SQ_ALU_CONST_CACHE_VS_2:
1437 case SQ_ALU_CONST_CACHE_VS_3:
1438 case SQ_ALU_CONST_CACHE_VS_4:
1439 case SQ_ALU_CONST_CACHE_VS_5:
1440 case SQ_ALU_CONST_CACHE_VS_6:
1441 case SQ_ALU_CONST_CACHE_VS_7:
1442 case SQ_ALU_CONST_CACHE_VS_8:
1443 case SQ_ALU_CONST_CACHE_VS_9:
1444 case SQ_ALU_CONST_CACHE_VS_10:
1445 case SQ_ALU_CONST_CACHE_VS_11:
1446 case SQ_ALU_CONST_CACHE_VS_12:
1447 case SQ_ALU_CONST_CACHE_VS_13:
1448 case SQ_ALU_CONST_CACHE_VS_14:
1449 case SQ_ALU_CONST_CACHE_VS_15:
Jerome Glisse961fb592010-02-10 22:30:05 +00001450 r = r600_cs_packet_next_reloc(p, &reloc);
1451 if (r) {
1452 dev_warn(p->dev, "bad SET_CONTEXT_REG "
1453 "0x%04X\n", reg);
1454 return -EINVAL;
1455 }
1456 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1457 break;
Alex Deucher033b5652011-06-08 15:26:45 -04001458 case SX_MEMORY_EXPORT_BASE:
1459 r = r600_cs_packet_next_reloc(p, &reloc);
1460 if (r) {
1461 dev_warn(p->dev, "bad SET_CONFIG_REG "
1462 "0x%04X\n", reg);
1463 return -EINVAL;
1464 }
1465 ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1466 break;
Marek Olšák779923b2012-03-08 00:56:00 +01001467 case SX_MISC:
1468 track->sx_misc_kill_all_prims = (radeon_get_ib_value(p, idx) & 0x1) != 0;
1469 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001470 default:
1471 dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
1472 return -EINVAL;
1473 }
1474 return 0;
1475}
1476
Jerome Glisse285484e2011-12-16 17:03:42 -05001477unsigned r600_mip_minify(unsigned size, unsigned level)
Jerome Glisse961fb592010-02-10 22:30:05 +00001478{
Dave Airlie60b212f2011-02-18 05:51:58 +00001479 unsigned val;
1480
1481 val = max(1U, size >> level);
1482 if (level > 0)
1483 val = roundup_pow_of_two(val);
1484 return val;
Jerome Glisse961fb592010-02-10 22:30:05 +00001485}
1486
Dave Airlie60b212f2011-02-18 05:51:58 +00001487static void r600_texture_size(unsigned nfaces, unsigned blevel, unsigned llevel,
Marek Olšákfcdeefe2012-08-19 21:23:26 +02001488 unsigned w0, unsigned h0, unsigned d0, unsigned nsamples, unsigned format,
Dave Airlie60b212f2011-02-18 05:51:58 +00001489 unsigned block_align, unsigned height_align, unsigned base_align,
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001490 unsigned *l0_size, unsigned *mipmap_size)
Jerome Glisse961fb592010-02-10 22:30:05 +00001491{
Dave Airlie60b212f2011-02-18 05:51:58 +00001492 unsigned offset, i, level;
1493 unsigned width, height, depth, size;
1494 unsigned blocksize;
1495 unsigned nbx, nby;
1496 unsigned nlevels = llevel - blevel + 1;
Jerome Glisse961fb592010-02-10 22:30:05 +00001497
Dave Airlie60b212f2011-02-18 05:51:58 +00001498 *l0_size = -1;
Jerome Glisse285484e2011-12-16 17:03:42 -05001499 blocksize = r600_fmt_get_blocksize(format);
Dave Airlie60b212f2011-02-18 05:51:58 +00001500
Jerome Glisse285484e2011-12-16 17:03:42 -05001501 w0 = r600_mip_minify(w0, 0);
1502 h0 = r600_mip_minify(h0, 0);
1503 d0 = r600_mip_minify(d0, 0);
Jerome Glisse961fb592010-02-10 22:30:05 +00001504 for(i = 0, offset = 0, level = blevel; i < nlevels; i++, level++) {
Jerome Glisse285484e2011-12-16 17:03:42 -05001505 width = r600_mip_minify(w0, i);
1506 nbx = r600_fmt_get_nblocksx(format, width);
Dave Airlie60b212f2011-02-18 05:51:58 +00001507
1508 nbx = round_up(nbx, block_align);
1509
Jerome Glisse285484e2011-12-16 17:03:42 -05001510 height = r600_mip_minify(h0, i);
1511 nby = r600_fmt_get_nblocksy(format, height);
Dave Airlie60b212f2011-02-18 05:51:58 +00001512 nby = round_up(nby, height_align);
1513
Jerome Glisse285484e2011-12-16 17:03:42 -05001514 depth = r600_mip_minify(d0, i);
Dave Airlie60b212f2011-02-18 05:51:58 +00001515
Marek Olšákfcdeefe2012-08-19 21:23:26 +02001516 size = nbx * nby * blocksize * nsamples;
Dave Airlie60b212f2011-02-18 05:51:58 +00001517 if (nfaces)
1518 size *= nfaces;
1519 else
1520 size *= depth;
1521
1522 if (i == 0)
1523 *l0_size = size;
1524
1525 if (i == 0 || i == 1)
1526 offset = round_up(offset, base_align);
1527
1528 offset += size;
Jerome Glisse961fb592010-02-10 22:30:05 +00001529 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001530 *mipmap_size = offset;
Dave Airlie60b212f2011-02-18 05:51:58 +00001531 if (llevel == 0)
Jerome Glisse961fb592010-02-10 22:30:05 +00001532 *mipmap_size = *l0_size;
Alex Deucher1729dd32010-08-06 02:54:05 -04001533 if (!blevel)
1534 *mipmap_size -= *l0_size;
Jerome Glisse961fb592010-02-10 22:30:05 +00001535}
1536
1537/**
1538 * r600_check_texture_resource() - check if register is authorized or not
1539 * @p: parser structure holding parsing context
1540 * @idx: index into the cs buffer
1541 * @texture: texture's bo structure
1542 * @mipmap: mipmap's bo structure
1543 *
1544 * This function will check that the resource has valid field and that
1545 * the texture and mipmap bo object are big enough to cover this resource.
1546 */
Andi Kleen488479e2011-10-13 16:08:41 -07001547static int r600_check_texture_resource(struct radeon_cs_parser *p, u32 idx,
Alex Deucher7f813372010-05-20 12:43:52 -04001548 struct radeon_bo *texture,
1549 struct radeon_bo *mipmap,
Alex Deucher16790562010-11-14 20:24:35 -05001550 u64 base_offset,
1551 u64 mip_offset,
Alex Deucher7f813372010-05-20 12:43:52 -04001552 u32 tiling_flags)
Jerome Glisse961fb592010-02-10 22:30:05 +00001553{
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001554 struct r600_cs_track *track = p->track;
Marek Olšákf00245f2012-08-09 16:34:15 +02001555 u32 dim, nfaces, llevel, blevel, w0, h0, d0;
1556 u32 word0, word1, l0_size, mipmap_size, word2, word3, word4, word5;
Alex Deucher16790562010-11-14 20:24:35 -05001557 u32 height_align, pitch, pitch_align, depth_align;
Marek Olšákf00245f2012-08-09 16:34:15 +02001558 u32 barray, larray;
Alex Deucher16790562010-11-14 20:24:35 -05001559 u64 base_align;
1560 struct array_mode_checker array_check;
Dave Airlie60b212f2011-02-18 05:51:58 +00001561 u32 format;
Marek Olšákf00245f2012-08-09 16:34:15 +02001562 bool is_array;
Jerome Glisse961fb592010-02-10 22:30:05 +00001563
1564 /* on legacy kernel we don't perform advanced check */
1565 if (p->rdev == NULL)
1566 return 0;
Alex Deucher7f813372010-05-20 12:43:52 -04001567
Alex Deucher16790562010-11-14 20:24:35 -05001568 /* convert to bytes */
1569 base_offset <<= 8;
1570 mip_offset <<= 8;
1571
Jerome Glisse961fb592010-02-10 22:30:05 +00001572 word0 = radeon_get_ib_value(p, idx + 0);
Jerome Glisse721604a2012-01-05 22:11:05 -05001573 if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
Marek Olšáke70f2242011-10-25 01:38:45 +02001574 if (tiling_flags & RADEON_TILING_MACRO)
1575 word0 |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
1576 else if (tiling_flags & RADEON_TILING_MICRO)
1577 word0 |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
1578 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001579 word1 = radeon_get_ib_value(p, idx + 1);
Marek Olšákf00245f2012-08-09 16:34:15 +02001580 word2 = radeon_get_ib_value(p, idx + 2) << 8;
1581 word3 = radeon_get_ib_value(p, idx + 3) << 8;
1582 word4 = radeon_get_ib_value(p, idx + 4);
1583 word5 = radeon_get_ib_value(p, idx + 5);
1584 dim = G_038000_DIM(word0);
Jerome Glisse961fb592010-02-10 22:30:05 +00001585 w0 = G_038000_TEX_WIDTH(word0) + 1;
Marek Olšákf00245f2012-08-09 16:34:15 +02001586 pitch = (G_038000_PITCH(word0) + 1) * 8;
Jerome Glisse961fb592010-02-10 22:30:05 +00001587 h0 = G_038004_TEX_HEIGHT(word1) + 1;
1588 d0 = G_038004_TEX_DEPTH(word1);
Marek Olšákf00245f2012-08-09 16:34:15 +02001589 format = G_038004_DATA_FORMAT(word1);
1590 blevel = G_038010_BASE_LEVEL(word4);
1591 llevel = G_038014_LAST_LEVEL(word5);
1592 /* pitch in texels */
1593 array_check.array_mode = G_038000_TILE_MODE(word0);
1594 array_check.group_size = track->group_size;
1595 array_check.nbanks = track->nbanks;
1596 array_check.npipes = track->npipes;
1597 array_check.nsamples = 1;
1598 array_check.blocksize = r600_fmt_get_blocksize(format);
Jerome Glisse961fb592010-02-10 22:30:05 +00001599 nfaces = 1;
Marek Olšákf00245f2012-08-09 16:34:15 +02001600 is_array = false;
1601 switch (dim) {
Jerome Glisse961fb592010-02-10 22:30:05 +00001602 case V_038000_SQ_TEX_DIM_1D:
1603 case V_038000_SQ_TEX_DIM_2D:
1604 case V_038000_SQ_TEX_DIM_3D:
1605 break;
1606 case V_038000_SQ_TEX_DIM_CUBEMAP:
Dave Airlie60b212f2011-02-18 05:51:58 +00001607 if (p->family >= CHIP_RV770)
1608 nfaces = 8;
1609 else
1610 nfaces = 6;
Jerome Glisse961fb592010-02-10 22:30:05 +00001611 break;
1612 case V_038000_SQ_TEX_DIM_1D_ARRAY:
1613 case V_038000_SQ_TEX_DIM_2D_ARRAY:
Marek Olšákf00245f2012-08-09 16:34:15 +02001614 is_array = true;
Dave Airlie60b212f2011-02-18 05:51:58 +00001615 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001616 case V_038000_SQ_TEX_DIM_2D_ARRAY_MSAA:
Marek Olšákb51ad122012-08-09 16:34:16 +02001617 is_array = true;
1618 /* fall through */
1619 case V_038000_SQ_TEX_DIM_2D_MSAA:
1620 array_check.nsamples = 1 << llevel;
1621 llevel = 0;
1622 break;
Jerome Glisse961fb592010-02-10 22:30:05 +00001623 default:
1624 dev_warn(p->dev, "this kernel doesn't support %d texture dim\n", G_038000_DIM(word0));
1625 return -EINVAL;
1626 }
Jerome Glisse285484e2011-12-16 17:03:42 -05001627 if (!r600_fmt_is_valid_texture(format, p->family)) {
Jerome Glisse961fb592010-02-10 22:30:05 +00001628 dev_warn(p->dev, "%s:%d texture invalid format %d\n",
Dave Airlie60b212f2011-02-18 05:51:58 +00001629 __func__, __LINE__, format);
Jerome Glisse961fb592010-02-10 22:30:05 +00001630 return -EINVAL;
1631 }
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001632
Alex Deucher16790562010-11-14 20:24:35 -05001633 if (r600_get_array_mode_alignment(&array_check,
1634 &pitch_align, &height_align, &depth_align, &base_align)) {
1635 dev_warn(p->dev, "%s:%d tex array mode (%d) invalid\n",
1636 __func__, __LINE__, G_038000_TILE_MODE(word0));
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001637 return -EINVAL;
1638 }
Alex Deucher16790562010-11-14 20:24:35 -05001639
1640 /* XXX check height as well... */
1641
1642 if (!IS_ALIGNED(pitch, pitch_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -05001643 dev_warn(p->dev, "%s:%d tex pitch (%d, 0x%x, %d) invalid\n",
1644 __func__, __LINE__, pitch, pitch_align, G_038000_TILE_MODE(word0));
Alex Deucher16790562010-11-14 20:24:35 -05001645 return -EINVAL;
1646 }
1647 if (!IS_ALIGNED(base_offset, base_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -05001648 dev_warn(p->dev, "%s:%d tex base offset (0x%llx, 0x%llx, %d) invalid\n",
1649 __func__, __LINE__, base_offset, base_align, G_038000_TILE_MODE(word0));
Alex Deucher16790562010-11-14 20:24:35 -05001650 return -EINVAL;
1651 }
1652 if (!IS_ALIGNED(mip_offset, base_align)) {
Alex Deucherc2049b32011-02-13 18:42:41 -05001653 dev_warn(p->dev, "%s:%d tex mip offset (0x%llx, 0x%llx, %d) invalid\n",
1654 __func__, __LINE__, mip_offset, base_align, G_038000_TILE_MODE(word0));
Alex Deucher16790562010-11-14 20:24:35 -05001655 return -EINVAL;
1656 }
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001657
Jerome Glisse285484e2011-12-16 17:03:42 -05001658 if (blevel > llevel) {
1659 dev_warn(p->dev, "texture blevel %d > llevel %d\n",
1660 blevel, llevel);
1661 }
Marek Olšákf00245f2012-08-09 16:34:15 +02001662 if (is_array) {
1663 barray = G_038014_BASE_ARRAY(word5);
1664 larray = G_038014_LAST_ARRAY(word5);
Dave Airlie60b212f2011-02-18 05:51:58 +00001665
1666 nfaces = larray - barray + 1;
1667 }
Marek Olšákfcdeefe2012-08-19 21:23:26 +02001668 r600_texture_size(nfaces, blevel, llevel, w0, h0, d0, array_check.nsamples, format,
Dave Airlie60b212f2011-02-18 05:51:58 +00001669 pitch_align, height_align, base_align,
Alex Deucher40e2a5c2010-06-04 18:41:42 -04001670 &l0_size, &mipmap_size);
Jerome Glisse961fb592010-02-10 22:30:05 +00001671 /* using get ib will give us the offset into the texture bo */
Dave Airlieaf506212011-02-28 14:27:03 +10001672 if ((l0_size + word2) > radeon_bo_size(texture)) {
Jerome Glisse285484e2011-12-16 17:03:42 -05001673 dev_warn(p->dev, "texture bo too small ((%d %d) (%d %d) %d %d %d -> %d have %ld)\n",
1674 w0, h0, pitch_align, height_align,
1675 array_check.array_mode, format, word2,
1676 l0_size, radeon_bo_size(texture));
Dave Airlie60b212f2011-02-18 05:51:58 +00001677 dev_warn(p->dev, "alignments %d %d %d %lld\n", pitch, pitch_align, height_align, base_align);
Jerome Glisse961fb592010-02-10 22:30:05 +00001678 return -EINVAL;
1679 }
1680 /* using get ib will give us the offset into the mipmap bo */
Dave Airlieaf506212011-02-28 14:27:03 +10001681 if ((mipmap_size + word3) > radeon_bo_size(mipmap)) {
Alex Deucherfe725d42010-09-14 10:10:47 -04001682 /*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 +10001683 w0, h0, format, blevel, nlevels, word3, mipmap_size, radeon_bo_size(texture));*/
Jerome Glisse961fb592010-02-10 22:30:05 +00001684 }
1685 return 0;
1686}
1687
Marek Olšákdd220a02012-01-27 12:17:59 -05001688static bool r600_is_safe_reg(struct radeon_cs_parser *p, u32 reg, u32 idx)
1689{
1690 u32 m, i;
1691
1692 i = (reg >> 7);
1693 if (i >= ARRAY_SIZE(r600_reg_safe_bm)) {
1694 dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
1695 return false;
1696 }
1697 m = 1 << ((reg >> 2) & 31);
1698 if (!(r600_reg_safe_bm[i] & m))
1699 return true;
1700 dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
1701 return false;
1702}
1703
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001704static int r600_packet3_check(struct radeon_cs_parser *p,
1705 struct radeon_cs_packet *pkt)
1706{
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001707 struct radeon_cs_reloc *reloc;
Jerome Glissec8c15ff2010-01-18 13:01:36 +01001708 struct r600_cs_track *track;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001709 volatile u32 *ib;
1710 unsigned idx;
1711 unsigned i;
1712 unsigned start_reg, end_reg, reg;
1713 int r;
Dave Airlieadea4792009-09-25 14:23:47 +10001714 u32 idx_value;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001715
Jerome Glissec8c15ff2010-01-18 13:01:36 +01001716 track = (struct r600_cs_track *)p->track;
Jerome Glissef2e39222012-05-09 15:35:02 +02001717 ib = p->ib.ptr;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001718 idx = pkt->idx + 1;
Dave Airlieadea4792009-09-25 14:23:47 +10001719 idx_value = radeon_get_ib_value(p, idx);
Dave Airlie513bcb42009-09-23 16:56:27 +10001720
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001721 switch (pkt->opcode) {
Dave Airlie2a19cac2011-02-28 16:11:48 +10001722 case PACKET3_SET_PREDICATION:
1723 {
1724 int pred_op;
1725 int tmp;
Marek Olšák63330032012-03-19 03:09:37 +01001726 uint64_t offset;
1727
Dave Airlie2a19cac2011-02-28 16:11:48 +10001728 if (pkt->count != 1) {
1729 DRM_ERROR("bad SET PREDICATION\n");
1730 return -EINVAL;
1731 }
1732
1733 tmp = radeon_get_ib_value(p, idx + 1);
1734 pred_op = (tmp >> 16) & 0x7;
1735
1736 /* for the clear predicate operation */
1737 if (pred_op == 0)
1738 return 0;
1739
1740 if (pred_op > 2) {
1741 DRM_ERROR("bad SET PREDICATION operation %d\n", pred_op);
1742 return -EINVAL;
1743 }
1744
1745 r = r600_cs_packet_next_reloc(p, &reloc);
1746 if (r) {
1747 DRM_ERROR("bad SET PREDICATION\n");
1748 return -EINVAL;
1749 }
1750
Marek Olšák63330032012-03-19 03:09:37 +01001751 offset = reloc->lobj.gpu_offset +
1752 (idx_value & 0xfffffff0) +
1753 ((u64)(tmp & 0xff) << 32);
1754
1755 ib[idx + 0] = offset;
1756 ib[idx + 1] = (tmp & 0xffffff00) | (upper_32_bits(offset) & 0xff);
Dave Airlie2a19cac2011-02-28 16:11:48 +10001757 }
1758 break;
1759
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001760 case PACKET3_START_3D_CMDBUF:
1761 if (p->family >= CHIP_RV770 || pkt->count) {
1762 DRM_ERROR("bad START_3D\n");
1763 return -EINVAL;
1764 }
1765 break;
1766 case PACKET3_CONTEXT_CONTROL:
1767 if (pkt->count != 1) {
1768 DRM_ERROR("bad CONTEXT_CONTROL\n");
1769 return -EINVAL;
1770 }
1771 break;
1772 case PACKET3_INDEX_TYPE:
1773 case PACKET3_NUM_INSTANCES:
1774 if (pkt->count) {
1775 DRM_ERROR("bad INDEX_TYPE/NUM_INSTANCES\n");
1776 return -EINVAL;
1777 }
1778 break;
1779 case PACKET3_DRAW_INDEX:
Marek Olšák63330032012-03-19 03:09:37 +01001780 {
1781 uint64_t offset;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001782 if (pkt->count != 3) {
1783 DRM_ERROR("bad DRAW_INDEX\n");
1784 return -EINVAL;
1785 }
1786 r = r600_cs_packet_next_reloc(p, &reloc);
1787 if (r) {
1788 DRM_ERROR("bad DRAW_INDEX\n");
1789 return -EINVAL;
1790 }
Marek Olšák63330032012-03-19 03:09:37 +01001791
1792 offset = reloc->lobj.gpu_offset +
1793 idx_value +
1794 ((u64)(radeon_get_ib_value(p, idx+1) & 0xff) << 32);
1795
1796 ib[idx+0] = offset;
1797 ib[idx+1] = upper_32_bits(offset) & 0xff;
1798
Jerome Glisse961fb592010-02-10 22:30:05 +00001799 r = r600_cs_track_check(p);
1800 if (r) {
1801 dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
1802 return r;
1803 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001804 break;
Marek Olšák63330032012-03-19 03:09:37 +01001805 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001806 case PACKET3_DRAW_INDEX_AUTO:
1807 if (pkt->count != 1) {
1808 DRM_ERROR("bad DRAW_INDEX_AUTO\n");
1809 return -EINVAL;
1810 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001811 r = r600_cs_track_check(p);
1812 if (r) {
1813 dev_warn(p->dev, "%s:%d invalid cmd stream %d\n", __func__, __LINE__, idx);
1814 return r;
1815 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001816 break;
1817 case PACKET3_DRAW_INDEX_IMMD_BE:
1818 case PACKET3_DRAW_INDEX_IMMD:
1819 if (pkt->count < 2) {
1820 DRM_ERROR("bad DRAW_INDEX_IMMD\n");
1821 return -EINVAL;
1822 }
Jerome Glisse961fb592010-02-10 22:30:05 +00001823 r = r600_cs_track_check(p);
1824 if (r) {
1825 dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
1826 return r;
1827 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001828 break;
1829 case PACKET3_WAIT_REG_MEM:
1830 if (pkt->count != 5) {
1831 DRM_ERROR("bad WAIT_REG_MEM\n");
1832 return -EINVAL;
1833 }
1834 /* bit 4 is reg (0) or mem (1) */
Dave Airlieadea4792009-09-25 14:23:47 +10001835 if (idx_value & 0x10) {
Marek Olšák63330032012-03-19 03:09:37 +01001836 uint64_t offset;
1837
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001838 r = r600_cs_packet_next_reloc(p, &reloc);
1839 if (r) {
1840 DRM_ERROR("bad WAIT_REG_MEM\n");
1841 return -EINVAL;
1842 }
Marek Olšák63330032012-03-19 03:09:37 +01001843
1844 offset = reloc->lobj.gpu_offset +
1845 (radeon_get_ib_value(p, idx+1) & 0xfffffff0) +
1846 ((u64)(radeon_get_ib_value(p, idx+2) & 0xff) << 32);
1847
1848 ib[idx+1] = (ib[idx+1] & 0x3) | (offset & 0xfffffff0);
1849 ib[idx+2] = upper_32_bits(offset) & 0xff;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001850 }
1851 break;
Alex Deucher6830f582012-12-03 19:03:23 -05001852 case PACKET3_CP_DMA:
1853 {
1854 u32 command, size;
1855 u64 offset, tmp;
1856 if (pkt->count != 4) {
1857 DRM_ERROR("bad CP DMA\n");
1858 return -EINVAL;
1859 }
1860 command = radeon_get_ib_value(p, idx+4);
1861 size = command & 0x1fffff;
1862 if (command & PACKET3_CP_DMA_CMD_SAS) {
1863 /* src address space is register */
1864 DRM_ERROR("CP DMA SAS not supported\n");
1865 return -EINVAL;
1866 } else {
1867 if (command & PACKET3_CP_DMA_CMD_SAIC) {
1868 DRM_ERROR("CP DMA SAIC only supported for registers\n");
1869 return -EINVAL;
1870 }
1871 /* src address space is memory */
1872 r = r600_cs_packet_next_reloc(p, &reloc);
1873 if (r) {
1874 DRM_ERROR("bad CP DMA SRC\n");
1875 return -EINVAL;
1876 }
1877
1878 tmp = radeon_get_ib_value(p, idx) +
1879 ((u64)(radeon_get_ib_value(p, idx+1) & 0xff) << 32);
1880
1881 offset = reloc->lobj.gpu_offset + tmp;
1882
1883 if ((tmp + size) > radeon_bo_size(reloc->robj)) {
1884 dev_warn(p->dev, "CP DMA src buffer too small (%llu %lu)\n",
1885 tmp + size, radeon_bo_size(reloc->robj));
1886 return -EINVAL;
1887 }
1888
1889 ib[idx] = offset;
1890 ib[idx+1] = (ib[idx+1] & 0xffffff00) | (upper_32_bits(offset) & 0xff);
1891 }
1892 if (command & PACKET3_CP_DMA_CMD_DAS) {
1893 /* dst address space is register */
1894 DRM_ERROR("CP DMA DAS not supported\n");
1895 return -EINVAL;
1896 } else {
1897 /* dst address space is memory */
1898 if (command & PACKET3_CP_DMA_CMD_DAIC) {
1899 DRM_ERROR("CP DMA DAIC only supported for registers\n");
1900 return -EINVAL;
1901 }
1902 r = r600_cs_packet_next_reloc(p, &reloc);
1903 if (r) {
1904 DRM_ERROR("bad CP DMA DST\n");
1905 return -EINVAL;
1906 }
1907
1908 tmp = radeon_get_ib_value(p, idx+2) +
1909 ((u64)(radeon_get_ib_value(p, idx+3) & 0xff) << 32);
1910
1911 offset = reloc->lobj.gpu_offset + tmp;
1912
1913 if ((tmp + size) > radeon_bo_size(reloc->robj)) {
1914 dev_warn(p->dev, "CP DMA dst buffer too small (%llu %lu)\n",
1915 tmp + size, radeon_bo_size(reloc->robj));
1916 return -EINVAL;
1917 }
1918
1919 ib[idx+2] = offset;
1920 ib[idx+3] = upper_32_bits(offset) & 0xff;
1921 }
1922 break;
1923 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001924 case PACKET3_SURFACE_SYNC:
1925 if (pkt->count != 3) {
1926 DRM_ERROR("bad SURFACE_SYNC\n");
1927 return -EINVAL;
1928 }
1929 /* 0xffffffff/0x0 is flush all cache flag */
Dave Airlie513bcb42009-09-23 16:56:27 +10001930 if (radeon_get_ib_value(p, idx + 1) != 0xffffffff ||
1931 radeon_get_ib_value(p, idx + 2) != 0) {
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001932 r = r600_cs_packet_next_reloc(p, &reloc);
1933 if (r) {
1934 DRM_ERROR("bad SURFACE_SYNC\n");
1935 return -EINVAL;
1936 }
1937 ib[idx+2] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
1938 }
1939 break;
1940 case PACKET3_EVENT_WRITE:
1941 if (pkt->count != 2 && pkt->count != 0) {
1942 DRM_ERROR("bad EVENT_WRITE\n");
1943 return -EINVAL;
1944 }
1945 if (pkt->count) {
Marek Olšák63330032012-03-19 03:09:37 +01001946 uint64_t offset;
1947
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001948 r = r600_cs_packet_next_reloc(p, &reloc);
1949 if (r) {
1950 DRM_ERROR("bad EVENT_WRITE\n");
1951 return -EINVAL;
1952 }
Marek Olšák63330032012-03-19 03:09:37 +01001953 offset = reloc->lobj.gpu_offset +
1954 (radeon_get_ib_value(p, idx+1) & 0xfffffff8) +
1955 ((u64)(radeon_get_ib_value(p, idx+2) & 0xff) << 32);
1956
1957 ib[idx+1] = offset & 0xfffffff8;
1958 ib[idx+2] = upper_32_bits(offset) & 0xff;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001959 }
1960 break;
1961 case PACKET3_EVENT_WRITE_EOP:
Marek Olšák63330032012-03-19 03:09:37 +01001962 {
1963 uint64_t offset;
1964
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001965 if (pkt->count != 4) {
1966 DRM_ERROR("bad EVENT_WRITE_EOP\n");
1967 return -EINVAL;
1968 }
1969 r = r600_cs_packet_next_reloc(p, &reloc);
1970 if (r) {
1971 DRM_ERROR("bad EVENT_WRITE\n");
1972 return -EINVAL;
1973 }
Marek Olšák63330032012-03-19 03:09:37 +01001974
1975 offset = reloc->lobj.gpu_offset +
1976 (radeon_get_ib_value(p, idx+1) & 0xfffffffc) +
1977 ((u64)(radeon_get_ib_value(p, idx+2) & 0xff) << 32);
1978
1979 ib[idx+1] = offset & 0xfffffffc;
1980 ib[idx+2] = (ib[idx+2] & 0xffffff00) | (upper_32_bits(offset) & 0xff);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001981 break;
Marek Olšák63330032012-03-19 03:09:37 +01001982 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001983 case PACKET3_SET_CONFIG_REG:
Dave Airlieadea4792009-09-25 14:23:47 +10001984 start_reg = (idx_value << 2) + PACKET3_SET_CONFIG_REG_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001985 end_reg = 4 * pkt->count + start_reg - 4;
1986 if ((start_reg < PACKET3_SET_CONFIG_REG_OFFSET) ||
1987 (start_reg >= PACKET3_SET_CONFIG_REG_END) ||
1988 (end_reg >= PACKET3_SET_CONFIG_REG_END)) {
1989 DRM_ERROR("bad PACKET3_SET_CONFIG_REG\n");
1990 return -EINVAL;
1991 }
1992 for (i = 0; i < pkt->count; i++) {
1993 reg = start_reg + (4 * i);
Jerome Glisse961fb592010-02-10 22:30:05 +00001994 r = r600_cs_check_reg(p, reg, idx+1+i);
1995 if (r)
1996 return r;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10001997 }
1998 break;
1999 case PACKET3_SET_CONTEXT_REG:
Dave Airlieadea4792009-09-25 14:23:47 +10002000 start_reg = (idx_value << 2) + PACKET3_SET_CONTEXT_REG_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002001 end_reg = 4 * pkt->count + start_reg - 4;
2002 if ((start_reg < PACKET3_SET_CONTEXT_REG_OFFSET) ||
2003 (start_reg >= PACKET3_SET_CONTEXT_REG_END) ||
2004 (end_reg >= PACKET3_SET_CONTEXT_REG_END)) {
2005 DRM_ERROR("bad PACKET3_SET_CONTEXT_REG\n");
2006 return -EINVAL;
2007 }
2008 for (i = 0; i < pkt->count; i++) {
2009 reg = start_reg + (4 * i);
Jerome Glisse961fb592010-02-10 22:30:05 +00002010 r = r600_cs_check_reg(p, reg, idx+1+i);
2011 if (r)
2012 return r;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002013 }
2014 break;
2015 case PACKET3_SET_RESOURCE:
2016 if (pkt->count % 7) {
2017 DRM_ERROR("bad SET_RESOURCE\n");
2018 return -EINVAL;
2019 }
Dave Airlieadea4792009-09-25 14:23:47 +10002020 start_reg = (idx_value << 2) + PACKET3_SET_RESOURCE_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002021 end_reg = 4 * pkt->count + start_reg - 4;
2022 if ((start_reg < PACKET3_SET_RESOURCE_OFFSET) ||
2023 (start_reg >= PACKET3_SET_RESOURCE_END) ||
2024 (end_reg >= PACKET3_SET_RESOURCE_END)) {
2025 DRM_ERROR("bad SET_RESOURCE\n");
2026 return -EINVAL;
2027 }
2028 for (i = 0; i < (pkt->count / 7); i++) {
Jerome Glisse961fb592010-02-10 22:30:05 +00002029 struct radeon_bo *texture, *mipmap;
Alex Deucher1729dd32010-08-06 02:54:05 -04002030 u32 size, offset, base_offset, mip_offset;
Jerome Glisse961fb592010-02-10 22:30:05 +00002031
Dave Airlieadea4792009-09-25 14:23:47 +10002032 switch (G__SQ_VTX_CONSTANT_TYPE(radeon_get_ib_value(p, idx+(i*7)+6+1))) {
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002033 case SQ_TEX_VTX_VALID_TEXTURE:
2034 /* tex base */
2035 r = r600_cs_packet_next_reloc(p, &reloc);
2036 if (r) {
2037 DRM_ERROR("bad SET_RESOURCE\n");
2038 return -EINVAL;
2039 }
Alex Deucher1729dd32010-08-06 02:54:05 -04002040 base_offset = (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
Jerome Glisse721604a2012-01-05 22:11:05 -05002041 if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
Marek Olšáke70f2242011-10-25 01:38:45 +02002042 if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO)
2043 ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
2044 else if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO)
2045 ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
2046 }
Jerome Glisse961fb592010-02-10 22:30:05 +00002047 texture = reloc->robj;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002048 /* tex mip base */
2049 r = r600_cs_packet_next_reloc(p, &reloc);
2050 if (r) {
2051 DRM_ERROR("bad SET_RESOURCE\n");
2052 return -EINVAL;
2053 }
Alex Deucher1729dd32010-08-06 02:54:05 -04002054 mip_offset = (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
Jerome Glisse961fb592010-02-10 22:30:05 +00002055 mipmap = reloc->robj;
2056 r = r600_check_texture_resource(p, idx+(i*7)+1,
Alex Deucher16790562010-11-14 20:24:35 -05002057 texture, mipmap,
2058 base_offset + radeon_get_ib_value(p, idx+1+(i*7)+2),
2059 mip_offset + radeon_get_ib_value(p, idx+1+(i*7)+3),
2060 reloc->lobj.tiling_flags);
Jerome Glisse961fb592010-02-10 22:30:05 +00002061 if (r)
2062 return r;
Alex Deucher1729dd32010-08-06 02:54:05 -04002063 ib[idx+1+(i*7)+2] += base_offset;
2064 ib[idx+1+(i*7)+3] += mip_offset;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002065 break;
2066 case SQ_TEX_VTX_VALID_BUFFER:
Marek Olšák63330032012-03-19 03:09:37 +01002067 {
2068 uint64_t offset64;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002069 /* vtx base */
2070 r = r600_cs_packet_next_reloc(p, &reloc);
2071 if (r) {
2072 DRM_ERROR("bad SET_RESOURCE\n");
2073 return -EINVAL;
2074 }
Jerome Glisse961fb592010-02-10 22:30:05 +00002075 offset = radeon_get_ib_value(p, idx+1+(i*7)+0);
Alex Deucher1729dd32010-08-06 02:54:05 -04002076 size = radeon_get_ib_value(p, idx+1+(i*7)+1) + 1;
Jerome Glisse961fb592010-02-10 22:30:05 +00002077 if (p->rdev && (size + offset) > radeon_bo_size(reloc->robj)) {
2078 /* force size to size of the buffer */
Alex Deucher1729dd32010-08-06 02:54:05 -04002079 dev_warn(p->dev, "vbo resource seems too big (%d) for the bo (%ld)\n",
2080 size + offset, radeon_bo_size(reloc->robj));
Marek Olšák63330032012-03-19 03:09:37 +01002081 ib[idx+1+(i*7)+1] = radeon_bo_size(reloc->robj) - offset;
Jerome Glisse961fb592010-02-10 22:30:05 +00002082 }
Marek Olšák63330032012-03-19 03:09:37 +01002083
2084 offset64 = reloc->lobj.gpu_offset + offset;
2085 ib[idx+1+(i*8)+0] = offset64;
2086 ib[idx+1+(i*8)+2] = (ib[idx+1+(i*8)+2] & 0xffffff00) |
2087 (upper_32_bits(offset64) & 0xff);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002088 break;
Marek Olšák63330032012-03-19 03:09:37 +01002089 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002090 case SQ_TEX_VTX_INVALID_TEXTURE:
2091 case SQ_TEX_VTX_INVALID_BUFFER:
2092 default:
2093 DRM_ERROR("bad SET_RESOURCE\n");
2094 return -EINVAL;
2095 }
2096 }
2097 break;
2098 case PACKET3_SET_ALU_CONST:
Alex Deucher5f77df32010-03-26 14:52:32 -04002099 if (track->sq_config & DX9_CONSTS) {
2100 start_reg = (idx_value << 2) + PACKET3_SET_ALU_CONST_OFFSET;
2101 end_reg = 4 * pkt->count + start_reg - 4;
2102 if ((start_reg < PACKET3_SET_ALU_CONST_OFFSET) ||
2103 (start_reg >= PACKET3_SET_ALU_CONST_END) ||
2104 (end_reg >= PACKET3_SET_ALU_CONST_END)) {
2105 DRM_ERROR("bad SET_ALU_CONST\n");
2106 return -EINVAL;
2107 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002108 }
2109 break;
2110 case PACKET3_SET_BOOL_CONST:
Dave Airlieadea4792009-09-25 14:23:47 +10002111 start_reg = (idx_value << 2) + PACKET3_SET_BOOL_CONST_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002112 end_reg = 4 * pkt->count + start_reg - 4;
2113 if ((start_reg < PACKET3_SET_BOOL_CONST_OFFSET) ||
2114 (start_reg >= PACKET3_SET_BOOL_CONST_END) ||
2115 (end_reg >= PACKET3_SET_BOOL_CONST_END)) {
2116 DRM_ERROR("bad SET_BOOL_CONST\n");
2117 return -EINVAL;
2118 }
2119 break;
2120 case PACKET3_SET_LOOP_CONST:
Dave Airlieadea4792009-09-25 14:23:47 +10002121 start_reg = (idx_value << 2) + PACKET3_SET_LOOP_CONST_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002122 end_reg = 4 * pkt->count + start_reg - 4;
2123 if ((start_reg < PACKET3_SET_LOOP_CONST_OFFSET) ||
2124 (start_reg >= PACKET3_SET_LOOP_CONST_END) ||
2125 (end_reg >= PACKET3_SET_LOOP_CONST_END)) {
2126 DRM_ERROR("bad SET_LOOP_CONST\n");
2127 return -EINVAL;
2128 }
2129 break;
2130 case PACKET3_SET_CTL_CONST:
Dave Airlieadea4792009-09-25 14:23:47 +10002131 start_reg = (idx_value << 2) + PACKET3_SET_CTL_CONST_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002132 end_reg = 4 * pkt->count + start_reg - 4;
2133 if ((start_reg < PACKET3_SET_CTL_CONST_OFFSET) ||
2134 (start_reg >= PACKET3_SET_CTL_CONST_END) ||
2135 (end_reg >= PACKET3_SET_CTL_CONST_END)) {
2136 DRM_ERROR("bad SET_CTL_CONST\n");
2137 return -EINVAL;
2138 }
2139 break;
2140 case PACKET3_SET_SAMPLER:
2141 if (pkt->count % 3) {
2142 DRM_ERROR("bad SET_SAMPLER\n");
2143 return -EINVAL;
2144 }
Dave Airlieadea4792009-09-25 14:23:47 +10002145 start_reg = (idx_value << 2) + PACKET3_SET_SAMPLER_OFFSET;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002146 end_reg = 4 * pkt->count + start_reg - 4;
2147 if ((start_reg < PACKET3_SET_SAMPLER_OFFSET) ||
2148 (start_reg >= PACKET3_SET_SAMPLER_END) ||
2149 (end_reg >= PACKET3_SET_SAMPLER_END)) {
2150 DRM_ERROR("bad SET_SAMPLER\n");
2151 return -EINVAL;
2152 }
2153 break;
Alex Deucher7c77bf22012-06-14 22:06:37 +02002154 case PACKET3_STRMOUT_BASE_UPDATE:
Marek Olšák46fc8782012-09-25 01:45:33 +02002155 /* RS780 and RS880 also need this */
2156 if (p->family < CHIP_RS780) {
Alex Deucher7c77bf22012-06-14 22:06:37 +02002157 DRM_ERROR("STRMOUT_BASE_UPDATE only supported on 7xx\n");
2158 return -EINVAL;
2159 }
2160 if (pkt->count != 1) {
2161 DRM_ERROR("bad STRMOUT_BASE_UPDATE packet count\n");
2162 return -EINVAL;
2163 }
2164 if (idx_value > 3) {
2165 DRM_ERROR("bad STRMOUT_BASE_UPDATE index\n");
2166 return -EINVAL;
2167 }
2168 {
2169 u64 offset;
2170
2171 r = r600_cs_packet_next_reloc(p, &reloc);
2172 if (r) {
2173 DRM_ERROR("bad STRMOUT_BASE_UPDATE reloc\n");
2174 return -EINVAL;
2175 }
2176
2177 if (reloc->robj != track->vgt_strmout_bo[idx_value]) {
2178 DRM_ERROR("bad STRMOUT_BASE_UPDATE, bo does not match\n");
2179 return -EINVAL;
2180 }
2181
2182 offset = radeon_get_ib_value(p, idx+1) << 8;
2183 if (offset != track->vgt_strmout_bo_offset[idx_value]) {
2184 DRM_ERROR("bad STRMOUT_BASE_UPDATE, bo offset does not match: 0x%llx, 0x%x\n",
2185 offset, track->vgt_strmout_bo_offset[idx_value]);
2186 return -EINVAL;
2187 }
2188
2189 if ((offset + 4) > radeon_bo_size(reloc->robj)) {
2190 DRM_ERROR("bad STRMOUT_BASE_UPDATE bo too small: 0x%llx, 0x%lx\n",
2191 offset + 4, radeon_bo_size(reloc->robj));
2192 return -EINVAL;
2193 }
2194 ib[idx+1] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
2195 }
2196 break;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002197 case PACKET3_SURFACE_BASE_UPDATE:
2198 if (p->family >= CHIP_RV770 || p->family == CHIP_R600) {
2199 DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
2200 return -EINVAL;
2201 }
2202 if (pkt->count) {
2203 DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
2204 return -EINVAL;
2205 }
2206 break;
Marek Olšákdd220a02012-01-27 12:17:59 -05002207 case PACKET3_STRMOUT_BUFFER_UPDATE:
2208 if (pkt->count != 4) {
2209 DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (invalid count)\n");
2210 return -EINVAL;
2211 }
2212 /* Updating memory at DST_ADDRESS. */
2213 if (idx_value & 0x1) {
2214 u64 offset;
2215 r = r600_cs_packet_next_reloc(p, &reloc);
2216 if (r) {
2217 DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (missing dst reloc)\n");
2218 return -EINVAL;
2219 }
2220 offset = radeon_get_ib_value(p, idx+1);
2221 offset += ((u64)(radeon_get_ib_value(p, idx+2) & 0xff)) << 32;
2222 if ((offset + 4) > radeon_bo_size(reloc->robj)) {
2223 DRM_ERROR("bad STRMOUT_BUFFER_UPDATE dst bo too small: 0x%llx, 0x%lx\n",
2224 offset + 4, radeon_bo_size(reloc->robj));
2225 return -EINVAL;
2226 }
Marek Olšák63330032012-03-19 03:09:37 +01002227 offset += reloc->lobj.gpu_offset;
2228 ib[idx+1] = offset;
2229 ib[idx+2] = upper_32_bits(offset) & 0xff;
Marek Olšákdd220a02012-01-27 12:17:59 -05002230 }
2231 /* Reading data from SRC_ADDRESS. */
2232 if (((idx_value >> 1) & 0x3) == 2) {
2233 u64 offset;
2234 r = r600_cs_packet_next_reloc(p, &reloc);
2235 if (r) {
2236 DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (missing src reloc)\n");
2237 return -EINVAL;
2238 }
2239 offset = radeon_get_ib_value(p, idx+3);
2240 offset += ((u64)(radeon_get_ib_value(p, idx+4) & 0xff)) << 32;
2241 if ((offset + 4) > radeon_bo_size(reloc->robj)) {
2242 DRM_ERROR("bad STRMOUT_BUFFER_UPDATE src bo too small: 0x%llx, 0x%lx\n",
2243 offset + 4, radeon_bo_size(reloc->robj));
2244 return -EINVAL;
2245 }
Marek Olšák63330032012-03-19 03:09:37 +01002246 offset += reloc->lobj.gpu_offset;
2247 ib[idx+3] = offset;
2248 ib[idx+4] = upper_32_bits(offset) & 0xff;
Marek Olšákdd220a02012-01-27 12:17:59 -05002249 }
2250 break;
Jerome Glisse4613ca12012-12-19 12:26:45 -05002251 case PACKET3_MEM_WRITE:
2252 {
2253 u64 offset;
2254
2255 if (pkt->count != 3) {
2256 DRM_ERROR("bad MEM_WRITE (invalid count)\n");
2257 return -EINVAL;
2258 }
2259 r = r600_cs_packet_next_reloc(p, &reloc);
2260 if (r) {
2261 DRM_ERROR("bad MEM_WRITE (missing reloc)\n");
2262 return -EINVAL;
2263 }
2264 offset = radeon_get_ib_value(p, idx+0);
2265 offset += ((u64)(radeon_get_ib_value(p, idx+1) & 0xff)) << 32UL;
2266 if (offset & 0x7) {
2267 DRM_ERROR("bad MEM_WRITE (address not qwords aligned)\n");
2268 return -EINVAL;
2269 }
2270 if ((offset + 8) > radeon_bo_size(reloc->robj)) {
2271 DRM_ERROR("bad MEM_WRITE bo too small: 0x%llx, 0x%lx\n",
2272 offset + 8, radeon_bo_size(reloc->robj));
2273 return -EINVAL;
2274 }
2275 offset += reloc->lobj.gpu_offset;
2276 ib[idx+0] = offset;
2277 ib[idx+1] = upper_32_bits(offset) & 0xff;
2278 break;
2279 }
Marek Olšákdd220a02012-01-27 12:17:59 -05002280 case PACKET3_COPY_DW:
2281 if (pkt->count != 4) {
2282 DRM_ERROR("bad COPY_DW (invalid count)\n");
2283 return -EINVAL;
2284 }
2285 if (idx_value & 0x1) {
2286 u64 offset;
2287 /* SRC is memory. */
2288 r = r600_cs_packet_next_reloc(p, &reloc);
2289 if (r) {
2290 DRM_ERROR("bad COPY_DW (missing src reloc)\n");
2291 return -EINVAL;
2292 }
2293 offset = radeon_get_ib_value(p, idx+1);
2294 offset += ((u64)(radeon_get_ib_value(p, idx+2) & 0xff)) << 32;
2295 if ((offset + 4) > radeon_bo_size(reloc->robj)) {
2296 DRM_ERROR("bad COPY_DW src bo too small: 0x%llx, 0x%lx\n",
2297 offset + 4, radeon_bo_size(reloc->robj));
2298 return -EINVAL;
2299 }
Marek Olšák63330032012-03-19 03:09:37 +01002300 offset += reloc->lobj.gpu_offset;
2301 ib[idx+1] = offset;
2302 ib[idx+2] = upper_32_bits(offset) & 0xff;
Marek Olšákdd220a02012-01-27 12:17:59 -05002303 } else {
2304 /* SRC is a reg. */
2305 reg = radeon_get_ib_value(p, idx+1) << 2;
2306 if (!r600_is_safe_reg(p, reg, idx+1))
2307 return -EINVAL;
2308 }
2309 if (idx_value & 0x2) {
2310 u64 offset;
2311 /* DST is memory. */
2312 r = r600_cs_packet_next_reloc(p, &reloc);
2313 if (r) {
2314 DRM_ERROR("bad COPY_DW (missing dst reloc)\n");
2315 return -EINVAL;
2316 }
2317 offset = radeon_get_ib_value(p, idx+3);
2318 offset += ((u64)(radeon_get_ib_value(p, idx+4) & 0xff)) << 32;
2319 if ((offset + 4) > radeon_bo_size(reloc->robj)) {
2320 DRM_ERROR("bad COPY_DW dst bo too small: 0x%llx, 0x%lx\n",
2321 offset + 4, radeon_bo_size(reloc->robj));
2322 return -EINVAL;
2323 }
Marek Olšák63330032012-03-19 03:09:37 +01002324 offset += reloc->lobj.gpu_offset;
2325 ib[idx+3] = offset;
2326 ib[idx+4] = upper_32_bits(offset) & 0xff;
Marek Olšákdd220a02012-01-27 12:17:59 -05002327 } else {
2328 /* DST is a reg. */
2329 reg = radeon_get_ib_value(p, idx+3) << 2;
2330 if (!r600_is_safe_reg(p, reg, idx+3))
2331 return -EINVAL;
2332 }
2333 break;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002334 case PACKET3_NOP:
2335 break;
2336 default:
2337 DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
2338 return -EINVAL;
2339 }
2340 return 0;
2341}
2342
2343int r600_cs_parse(struct radeon_cs_parser *p)
2344{
2345 struct radeon_cs_packet pkt;
Jerome Glissec8c15ff2010-01-18 13:01:36 +01002346 struct r600_cs_track *track;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002347 int r;
2348
Jerome Glisse961fb592010-02-10 22:30:05 +00002349 if (p->track == NULL) {
2350 /* initialize tracker, we are in kms */
2351 track = kzalloc(sizeof(*track), GFP_KERNEL);
2352 if (track == NULL)
2353 return -ENOMEM;
2354 r600_cs_track_init(track);
2355 if (p->rdev->family < CHIP_RV770) {
2356 track->npipes = p->rdev->config.r600.tiling_npipes;
2357 track->nbanks = p->rdev->config.r600.tiling_nbanks;
2358 track->group_size = p->rdev->config.r600.tiling_group_size;
2359 } else if (p->rdev->family <= CHIP_RV740) {
2360 track->npipes = p->rdev->config.rv770.tiling_npipes;
2361 track->nbanks = p->rdev->config.rv770.tiling_nbanks;
2362 track->group_size = p->rdev->config.rv770.tiling_group_size;
2363 }
2364 p->track = track;
2365 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002366 do {
Ilija Hadzicc38f34b2013-01-02 18:27:41 -05002367 r = radeon_cs_packet_parse(p, &pkt, p->idx);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002368 if (r) {
Jerome Glisse7cb72ef2010-02-11 12:44:32 +01002369 kfree(p->track);
2370 p->track = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002371 return r;
2372 }
2373 p->idx += pkt.count + 2;
2374 switch (pkt.type) {
2375 case PACKET_TYPE0:
2376 r = r600_cs_parse_packet0(p, &pkt);
2377 break;
2378 case PACKET_TYPE2:
2379 break;
2380 case PACKET_TYPE3:
2381 r = r600_packet3_check(p, &pkt);
2382 break;
2383 default:
2384 DRM_ERROR("Unknown packet type %d !\n", pkt.type);
Jerome Glisse961fb592010-02-10 22:30:05 +00002385 kfree(p->track);
Jerome Glisse7cb72ef2010-02-11 12:44:32 +01002386 p->track = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002387 return -EINVAL;
2388 }
2389 if (r) {
Jerome Glisse961fb592010-02-10 22:30:05 +00002390 kfree(p->track);
Jerome Glisse7cb72ef2010-02-11 12:44:32 +01002391 p->track = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002392 return r;
2393 }
2394 } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
2395#if 0
Jerome Glissef2e39222012-05-09 15:35:02 +02002396 for (r = 0; r < p->ib.length_dw; r++) {
2397 printk(KERN_INFO "%05d 0x%08X\n", r, p->ib.ptr[r]);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002398 mdelay(1);
2399 }
2400#endif
Jerome Glisse961fb592010-02-10 22:30:05 +00002401 kfree(p->track);
Jerome Glisse7cb72ef2010-02-11 12:44:32 +01002402 p->track = NULL;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002403 return 0;
2404}
2405
2406static int r600_cs_parser_relocs_legacy(struct radeon_cs_parser *p)
2407{
2408 if (p->chunk_relocs_idx == -1) {
2409 return 0;
2410 }
Julia Lawalle265f39e2009-12-19 08:16:33 +01002411 p->relocs = kzalloc(sizeof(struct radeon_cs_reloc), GFP_KERNEL);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002412 if (p->relocs == NULL) {
2413 return -ENOMEM;
2414 }
2415 return 0;
2416}
2417
2418/**
2419 * cs_parser_fini() - clean parser states
2420 * @parser: parser structure holding parsing context.
2421 * @error: error number
2422 *
2423 * If error is set than unvalidate buffer, otherwise just free memory
2424 * used by parsing context.
2425 **/
2426static void r600_cs_parser_fini(struct radeon_cs_parser *parser, int error)
2427{
2428 unsigned i;
2429
2430 kfree(parser->relocs);
2431 for (i = 0; i < parser->nchunks; i++) {
2432 kfree(parser->chunks[i].kdata);
Ilija Hadzica6b7e1a2013-01-07 18:21:58 -05002433 if (parser->rdev && (parser->rdev->flags & RADEON_IS_AGP)) {
2434 kfree(parser->chunks[i].kpage[0]);
2435 kfree(parser->chunks[i].kpage[1]);
2436 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002437 }
2438 kfree(parser->chunks);
2439 kfree(parser->chunks_array);
2440}
2441
2442int r600_cs_legacy(struct drm_device *dev, void *data, struct drm_file *filp,
2443 unsigned family, u32 *ib, int *l)
2444{
2445 struct radeon_cs_parser parser;
2446 struct radeon_cs_chunk *ib_chunk;
Jerome Glisse961fb592010-02-10 22:30:05 +00002447 struct r600_cs_track *track;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002448 int r;
2449
Jerome Glisse961fb592010-02-10 22:30:05 +00002450 /* initialize tracker */
2451 track = kzalloc(sizeof(*track), GFP_KERNEL);
2452 if (track == NULL)
2453 return -ENOMEM;
2454 r600_cs_track_init(track);
2455 r600_cs_legacy_get_tiling_conf(dev, &track->npipes, &track->nbanks, &track->group_size);
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002456 /* initialize parser */
2457 memset(&parser, 0, sizeof(struct radeon_cs_parser));
2458 parser.filp = filp;
Jerome Glissec8c15ff2010-01-18 13:01:36 +01002459 parser.dev = &dev->pdev->dev;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002460 parser.rdev = NULL;
2461 parser.family = family;
Jerome Glisse961fb592010-02-10 22:30:05 +00002462 parser.track = track;
Jerome Glissef2e39222012-05-09 15:35:02 +02002463 parser.ib.ptr = ib;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002464 r = radeon_cs_parser_init(&parser, data);
2465 if (r) {
2466 DRM_ERROR("Failed to initialize parser !\n");
2467 r600_cs_parser_fini(&parser, r);
2468 return r;
2469 }
2470 r = r600_cs_parser_relocs_legacy(&parser);
2471 if (r) {
2472 DRM_ERROR("Failed to parse relocation !\n");
2473 r600_cs_parser_fini(&parser, r);
2474 return r;
2475 }
2476 /* Copy the packet into the IB, the parser will read from the
2477 * input memory (cached) and write to the IB (which can be
2478 * uncached). */
2479 ib_chunk = &parser.chunks[parser.chunk_ib_idx];
Jerome Glissef2e39222012-05-09 15:35:02 +02002480 parser.ib.length_dw = ib_chunk->length_dw;
2481 *l = parser.ib.length_dw;
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002482 r = r600_cs_parse(&parser);
2483 if (r) {
2484 DRM_ERROR("Invalid command stream !\n");
2485 r600_cs_parser_fini(&parser, r);
2486 return r;
2487 }
Dave Airlie513bcb42009-09-23 16:56:27 +10002488 r = radeon_cs_finish_pages(&parser);
2489 if (r) {
2490 DRM_ERROR("Invalid command stream !\n");
2491 r600_cs_parser_fini(&parser, r);
2492 return r;
2493 }
Jerome Glisse3ce0a232009-09-08 10:10:24 +10002494 r600_cs_parser_fini(&parser, r);
2495 return r;
2496}
2497
2498void r600_cs_legacy_init(void)
2499{
2500 r600_cs_packet_next_reloc = &r600_cs_packet_next_reloc_nomm;
2501}
Alex Deuchercf4ccd02011-11-18 10:19:47 -05002502
2503/*
2504 * DMA
2505 */
2506/**
2507 * r600_dma_cs_next_reloc() - parse next reloc
2508 * @p: parser structure holding parsing context.
2509 * @cs_reloc: reloc informations
2510 *
2511 * Return the next reloc, do bo validation and compute
2512 * GPU offset using the provided start.
2513 **/
2514int r600_dma_cs_next_reloc(struct radeon_cs_parser *p,
2515 struct radeon_cs_reloc **cs_reloc)
2516{
2517 struct radeon_cs_chunk *relocs_chunk;
2518 unsigned idx;
2519
Jerome Glisse9305ede2013-01-09 16:40:42 -05002520 *cs_reloc = NULL;
Alex Deuchercf4ccd02011-11-18 10:19:47 -05002521 if (p->chunk_relocs_idx == -1) {
2522 DRM_ERROR("No relocation chunk !\n");
2523 return -EINVAL;
2524 }
Alex Deuchercf4ccd02011-11-18 10:19:47 -05002525 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
2526 idx = p->dma_reloc_idx;
Jerome Glisse9305ede2013-01-09 16:40:42 -05002527 if (idx >= p->nrelocs) {
Alex Deuchercf4ccd02011-11-18 10:19:47 -05002528 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
Jerome Glisse9305ede2013-01-09 16:40:42 -05002529 idx, p->nrelocs);
Alex Deuchercf4ccd02011-11-18 10:19:47 -05002530 return -EINVAL;
2531 }
2532 *cs_reloc = p->relocs_ptr[idx];
2533 p->dma_reloc_idx++;
2534 return 0;
2535}
2536
2537#define GET_DMA_CMD(h) (((h) & 0xf0000000) >> 28)
2538#define GET_DMA_COUNT(h) ((h) & 0x0000ffff)
2539#define GET_DMA_T(h) (((h) & 0x00800000) >> 23)
2540
2541/**
2542 * r600_dma_cs_parse() - parse the DMA IB
2543 * @p: parser structure holding parsing context.
2544 *
2545 * Parses the DMA IB from the CS ioctl and updates
2546 * the GPU addresses based on the reloc information and
2547 * checks for errors. (R6xx-R7xx)
2548 * Returns 0 for success and an error on failure.
2549 **/
2550int r600_dma_cs_parse(struct radeon_cs_parser *p)
2551{
2552 struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
2553 struct radeon_cs_reloc *src_reloc, *dst_reloc;
2554 u32 header, cmd, count, tiled;
2555 volatile u32 *ib = p->ib.ptr;
2556 u32 idx, idx_value;
2557 u64 src_offset, dst_offset;
2558 int r;
2559
2560 do {
2561 if (p->idx >= ib_chunk->length_dw) {
2562 DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
2563 p->idx, ib_chunk->length_dw);
2564 return -EINVAL;
2565 }
2566 idx = p->idx;
2567 header = radeon_get_ib_value(p, idx);
2568 cmd = GET_DMA_CMD(header);
2569 count = GET_DMA_COUNT(header);
2570 tiled = GET_DMA_T(header);
2571
2572 switch (cmd) {
2573 case DMA_PACKET_WRITE:
2574 r = r600_dma_cs_next_reloc(p, &dst_reloc);
2575 if (r) {
2576 DRM_ERROR("bad DMA_PACKET_WRITE\n");
2577 return -EINVAL;
2578 }
2579 if (tiled) {
2580 dst_offset = ib[idx+1];
2581 dst_offset <<= 8;
2582
2583 ib[idx+1] += (u32)(dst_reloc->lobj.gpu_offset >> 8);
2584 p->idx += count + 5;
2585 } else {
2586 dst_offset = ib[idx+1];
2587 dst_offset |= ((u64)(ib[idx+2] & 0xff)) << 32;
2588
2589 ib[idx+1] += (u32)(dst_reloc->lobj.gpu_offset & 0xfffffffc);
2590 ib[idx+2] += upper_32_bits(dst_reloc->lobj.gpu_offset) & 0xff;
2591 p->idx += count + 3;
2592 }
2593 if ((dst_offset + (count * 4)) > radeon_bo_size(dst_reloc->robj)) {
2594 dev_warn(p->dev, "DMA write buffer too small (%llu %lu)\n",
2595 dst_offset + (count * 4), radeon_bo_size(dst_reloc->robj));
2596 return -EINVAL;
2597 }
2598 break;
2599 case DMA_PACKET_COPY:
2600 r = r600_dma_cs_next_reloc(p, &src_reloc);
2601 if (r) {
2602 DRM_ERROR("bad DMA_PACKET_COPY\n");
2603 return -EINVAL;
2604 }
2605 r = r600_dma_cs_next_reloc(p, &dst_reloc);
2606 if (r) {
2607 DRM_ERROR("bad DMA_PACKET_COPY\n");
2608 return -EINVAL;
2609 }
2610 if (tiled) {
2611 idx_value = radeon_get_ib_value(p, idx + 2);
2612 /* detile bit */
2613 if (idx_value & (1 << 31)) {
2614 /* tiled src, linear dst */
2615 src_offset = ib[idx+1];
2616 src_offset <<= 8;
2617 ib[idx+1] += (u32)(src_reloc->lobj.gpu_offset >> 8);
2618
2619 dst_offset = ib[idx+5];
2620 dst_offset |= ((u64)(ib[idx+6] & 0xff)) << 32;
2621 ib[idx+5] += (u32)(dst_reloc->lobj.gpu_offset & 0xfffffffc);
2622 ib[idx+6] += upper_32_bits(dst_reloc->lobj.gpu_offset) & 0xff;
2623 } else {
2624 /* linear src, tiled dst */
2625 src_offset = ib[idx+5];
2626 src_offset |= ((u64)(ib[idx+6] & 0xff)) << 32;
2627 ib[idx+5] += (u32)(src_reloc->lobj.gpu_offset & 0xfffffffc);
2628 ib[idx+6] += upper_32_bits(src_reloc->lobj.gpu_offset) & 0xff;
2629
2630 dst_offset = ib[idx+1];
2631 dst_offset <<= 8;
2632 ib[idx+1] += (u32)(dst_reloc->lobj.gpu_offset >> 8);
2633 }
2634 p->idx += 7;
2635 } else {
Alex Deuchera10fbb42013-01-04 12:16:05 -05002636 if (p->family >= CHIP_RV770) {
2637 src_offset = ib[idx+2];
2638 src_offset |= ((u64)(ib[idx+4] & 0xff)) << 32;
2639 dst_offset = ib[idx+1];
2640 dst_offset |= ((u64)(ib[idx+3] & 0xff)) << 32;
Alex Deuchercf4ccd02011-11-18 10:19:47 -05002641
Alex Deuchera10fbb42013-01-04 12:16:05 -05002642 ib[idx+1] += (u32)(dst_reloc->lobj.gpu_offset & 0xfffffffc);
2643 ib[idx+2] += (u32)(src_reloc->lobj.gpu_offset & 0xfffffffc);
2644 ib[idx+3] += upper_32_bits(dst_reloc->lobj.gpu_offset) & 0xff;
2645 ib[idx+4] += upper_32_bits(src_reloc->lobj.gpu_offset) & 0xff;
2646 p->idx += 5;
2647 } else {
2648 src_offset = ib[idx+2];
2649 src_offset |= ((u64)(ib[idx+3] & 0xff)) << 32;
2650 dst_offset = ib[idx+1];
2651 dst_offset |= ((u64)(ib[idx+3] & 0xff0000)) << 16;
2652
2653 ib[idx+1] += (u32)(dst_reloc->lobj.gpu_offset & 0xfffffffc);
2654 ib[idx+2] += (u32)(src_reloc->lobj.gpu_offset & 0xfffffffc);
2655 ib[idx+3] += upper_32_bits(src_reloc->lobj.gpu_offset) & 0xff;
2656 ib[idx+3] += (upper_32_bits(dst_reloc->lobj.gpu_offset) & 0xff) << 16;
2657 p->idx += 4;
2658 }
Alex Deuchercf4ccd02011-11-18 10:19:47 -05002659 }
2660 if ((src_offset + (count * 4)) > radeon_bo_size(src_reloc->robj)) {
2661 dev_warn(p->dev, "DMA copy src buffer too small (%llu %lu)\n",
2662 src_offset + (count * 4), radeon_bo_size(src_reloc->robj));
2663 return -EINVAL;
2664 }
2665 if ((dst_offset + (count * 4)) > radeon_bo_size(dst_reloc->robj)) {
2666 dev_warn(p->dev, "DMA write dst buffer too small (%llu %lu)\n",
2667 dst_offset + (count * 4), radeon_bo_size(dst_reloc->robj));
2668 return -EINVAL;
2669 }
2670 break;
2671 case DMA_PACKET_CONSTANT_FILL:
2672 if (p->family < CHIP_RV770) {
2673 DRM_ERROR("Constant Fill is 7xx only !\n");
2674 return -EINVAL;
2675 }
2676 r = r600_dma_cs_next_reloc(p, &dst_reloc);
2677 if (r) {
2678 DRM_ERROR("bad DMA_PACKET_WRITE\n");
2679 return -EINVAL;
2680 }
2681 dst_offset = ib[idx+1];
2682 dst_offset |= ((u64)(ib[idx+3] & 0x00ff0000)) << 16;
2683 if ((dst_offset + (count * 4)) > radeon_bo_size(dst_reloc->robj)) {
2684 dev_warn(p->dev, "DMA constant fill buffer too small (%llu %lu)\n",
2685 dst_offset + (count * 4), radeon_bo_size(dst_reloc->robj));
2686 return -EINVAL;
2687 }
2688 ib[idx+1] += (u32)(dst_reloc->lobj.gpu_offset & 0xfffffffc);
2689 ib[idx+3] += (upper_32_bits(dst_reloc->lobj.gpu_offset) << 16) & 0x00ff0000;
2690 p->idx += 4;
2691 break;
2692 case DMA_PACKET_NOP:
2693 p->idx += 1;
2694 break;
2695 default:
2696 DRM_ERROR("Unknown packet type %d at %d !\n", cmd, idx);
2697 return -EINVAL;
2698 }
2699 } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
2700#if 0
2701 for (r = 0; r < p->ib->length_dw; r++) {
2702 printk(KERN_INFO "%05d 0x%08X\n", r, p->ib.ptr[r]);
2703 mdelay(1);
2704 }
2705#endif
2706 return 0;
2707}