18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2008 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci * Copyright 2008 Red Hat Inc.
48c2ecf20Sopenharmony_ci * Copyright 2009 Jerome Glisse.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
78c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
88c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
98c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
108c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
118c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
148c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
178c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
188c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
198c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
208c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
218c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
228c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Authors: Dave Airlie
258c2ecf20Sopenharmony_ci *          Alex Deucher
268c2ecf20Sopenharmony_ci *          Jerome Glisse
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci#include <linux/kernel.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include "radeon.h"
318c2ecf20Sopenharmony_ci#include "radeon_asic.h"
328c2ecf20Sopenharmony_ci#include "r600d.h"
338c2ecf20Sopenharmony_ci#include "r600_reg_safe.h"
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic int r600_nomm;
368c2ecf20Sopenharmony_ciextern void r600_cs_legacy_get_tiling_conf(struct drm_device *dev, u32 *npipes, u32 *nbanks, u32 *group_size);
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct r600_cs_track {
408c2ecf20Sopenharmony_ci	/* configuration we miror so that we use same code btw kms/ums */
418c2ecf20Sopenharmony_ci	u32			group_size;
428c2ecf20Sopenharmony_ci	u32			nbanks;
438c2ecf20Sopenharmony_ci	u32			npipes;
448c2ecf20Sopenharmony_ci	/* value we track */
458c2ecf20Sopenharmony_ci	u32			sq_config;
468c2ecf20Sopenharmony_ci	u32			log_nsamples;
478c2ecf20Sopenharmony_ci	u32			nsamples;
488c2ecf20Sopenharmony_ci	u32			cb_color_base_last[8];
498c2ecf20Sopenharmony_ci	struct radeon_bo	*cb_color_bo[8];
508c2ecf20Sopenharmony_ci	u64			cb_color_bo_mc[8];
518c2ecf20Sopenharmony_ci	u64			cb_color_bo_offset[8];
528c2ecf20Sopenharmony_ci	struct radeon_bo	*cb_color_frag_bo[8];
538c2ecf20Sopenharmony_ci	u64			cb_color_frag_offset[8];
548c2ecf20Sopenharmony_ci	struct radeon_bo	*cb_color_tile_bo[8];
558c2ecf20Sopenharmony_ci	u64			cb_color_tile_offset[8];
568c2ecf20Sopenharmony_ci	u32			cb_color_mask[8];
578c2ecf20Sopenharmony_ci	u32			cb_color_info[8];
588c2ecf20Sopenharmony_ci	u32			cb_color_view[8];
598c2ecf20Sopenharmony_ci	u32			cb_color_size_idx[8]; /* unused */
608c2ecf20Sopenharmony_ci	u32			cb_target_mask;
618c2ecf20Sopenharmony_ci	u32			cb_shader_mask;  /* unused */
628c2ecf20Sopenharmony_ci	bool			is_resolve;
638c2ecf20Sopenharmony_ci	u32			cb_color_size[8];
648c2ecf20Sopenharmony_ci	u32			vgt_strmout_en;
658c2ecf20Sopenharmony_ci	u32			vgt_strmout_buffer_en;
668c2ecf20Sopenharmony_ci	struct radeon_bo	*vgt_strmout_bo[4];
678c2ecf20Sopenharmony_ci	u64			vgt_strmout_bo_mc[4]; /* unused */
688c2ecf20Sopenharmony_ci	u32			vgt_strmout_bo_offset[4];
698c2ecf20Sopenharmony_ci	u32			vgt_strmout_size[4];
708c2ecf20Sopenharmony_ci	u32			db_depth_control;
718c2ecf20Sopenharmony_ci	u32			db_depth_info;
728c2ecf20Sopenharmony_ci	u32			db_depth_size_idx;
738c2ecf20Sopenharmony_ci	u32			db_depth_view;
748c2ecf20Sopenharmony_ci	u32			db_depth_size;
758c2ecf20Sopenharmony_ci	u32			db_offset;
768c2ecf20Sopenharmony_ci	struct radeon_bo	*db_bo;
778c2ecf20Sopenharmony_ci	u64			db_bo_mc;
788c2ecf20Sopenharmony_ci	bool			sx_misc_kill_all_prims;
798c2ecf20Sopenharmony_ci	bool			cb_dirty;
808c2ecf20Sopenharmony_ci	bool			db_dirty;
818c2ecf20Sopenharmony_ci	bool			streamout_dirty;
828c2ecf20Sopenharmony_ci	struct radeon_bo	*htile_bo;
838c2ecf20Sopenharmony_ci	u64			htile_offset;
848c2ecf20Sopenharmony_ci	u32			htile_surface;
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define FMT_8_BIT(fmt, vc)   [fmt] = { 1, 1, 1, vc, CHIP_R600 }
888c2ecf20Sopenharmony_ci#define FMT_16_BIT(fmt, vc)  [fmt] = { 1, 1, 2, vc, CHIP_R600 }
898c2ecf20Sopenharmony_ci#define FMT_24_BIT(fmt)      [fmt] = { 1, 1, 4,  0, CHIP_R600 }
908c2ecf20Sopenharmony_ci#define FMT_32_BIT(fmt, vc)  [fmt] = { 1, 1, 4, vc, CHIP_R600 }
918c2ecf20Sopenharmony_ci#define FMT_48_BIT(fmt)      [fmt] = { 1, 1, 8,  0, CHIP_R600 }
928c2ecf20Sopenharmony_ci#define FMT_64_BIT(fmt, vc)  [fmt] = { 1, 1, 8, vc, CHIP_R600 }
938c2ecf20Sopenharmony_ci#define FMT_96_BIT(fmt)      [fmt] = { 1, 1, 12, 0, CHIP_R600 }
948c2ecf20Sopenharmony_ci#define FMT_128_BIT(fmt, vc) [fmt] = { 1, 1, 16,vc, CHIP_R600 }
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistruct gpu_formats {
978c2ecf20Sopenharmony_ci	unsigned blockwidth;
988c2ecf20Sopenharmony_ci	unsigned blockheight;
998c2ecf20Sopenharmony_ci	unsigned blocksize;
1008c2ecf20Sopenharmony_ci	unsigned valid_color;
1018c2ecf20Sopenharmony_ci	enum radeon_family min_family;
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic const struct gpu_formats color_formats_table[] = {
1058c2ecf20Sopenharmony_ci	/* 8 bit */
1068c2ecf20Sopenharmony_ci	FMT_8_BIT(V_038004_COLOR_8, 1),
1078c2ecf20Sopenharmony_ci	FMT_8_BIT(V_038004_COLOR_4_4, 1),
1088c2ecf20Sopenharmony_ci	FMT_8_BIT(V_038004_COLOR_3_3_2, 1),
1098c2ecf20Sopenharmony_ci	FMT_8_BIT(V_038004_FMT_1, 0),
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	/* 16-bit */
1128c2ecf20Sopenharmony_ci	FMT_16_BIT(V_038004_COLOR_16, 1),
1138c2ecf20Sopenharmony_ci	FMT_16_BIT(V_038004_COLOR_16_FLOAT, 1),
1148c2ecf20Sopenharmony_ci	FMT_16_BIT(V_038004_COLOR_8_8, 1),
1158c2ecf20Sopenharmony_ci	FMT_16_BIT(V_038004_COLOR_5_6_5, 1),
1168c2ecf20Sopenharmony_ci	FMT_16_BIT(V_038004_COLOR_6_5_5, 1),
1178c2ecf20Sopenharmony_ci	FMT_16_BIT(V_038004_COLOR_1_5_5_5, 1),
1188c2ecf20Sopenharmony_ci	FMT_16_BIT(V_038004_COLOR_4_4_4_4, 1),
1198c2ecf20Sopenharmony_ci	FMT_16_BIT(V_038004_COLOR_5_5_5_1, 1),
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	/* 24-bit */
1228c2ecf20Sopenharmony_ci	FMT_24_BIT(V_038004_FMT_8_8_8),
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/* 32-bit */
1258c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_32, 1),
1268c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_32_FLOAT, 1),
1278c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_16_16, 1),
1288c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_16_16_FLOAT, 1),
1298c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_8_24, 1),
1308c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_8_24_FLOAT, 1),
1318c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_24_8, 1),
1328c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_24_8_FLOAT, 1),
1338c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_10_11_11, 1),
1348c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_10_11_11_FLOAT, 1),
1358c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_11_11_10, 1),
1368c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_11_11_10_FLOAT, 1),
1378c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_2_10_10_10, 1),
1388c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_8_8_8_8, 1),
1398c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_COLOR_10_10_10_2, 1),
1408c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_FMT_5_9_9_9_SHAREDEXP, 0),
1418c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_FMT_32_AS_8, 0),
1428c2ecf20Sopenharmony_ci	FMT_32_BIT(V_038004_FMT_32_AS_8_8, 0),
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* 48-bit */
1458c2ecf20Sopenharmony_ci	FMT_48_BIT(V_038004_FMT_16_16_16),
1468c2ecf20Sopenharmony_ci	FMT_48_BIT(V_038004_FMT_16_16_16_FLOAT),
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	/* 64-bit */
1498c2ecf20Sopenharmony_ci	FMT_64_BIT(V_038004_COLOR_X24_8_32_FLOAT, 1),
1508c2ecf20Sopenharmony_ci	FMT_64_BIT(V_038004_COLOR_32_32, 1),
1518c2ecf20Sopenharmony_ci	FMT_64_BIT(V_038004_COLOR_32_32_FLOAT, 1),
1528c2ecf20Sopenharmony_ci	FMT_64_BIT(V_038004_COLOR_16_16_16_16, 1),
1538c2ecf20Sopenharmony_ci	FMT_64_BIT(V_038004_COLOR_16_16_16_16_FLOAT, 1),
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	FMT_96_BIT(V_038004_FMT_32_32_32),
1568c2ecf20Sopenharmony_ci	FMT_96_BIT(V_038004_FMT_32_32_32_FLOAT),
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	/* 128-bit */
1598c2ecf20Sopenharmony_ci	FMT_128_BIT(V_038004_COLOR_32_32_32_32, 1),
1608c2ecf20Sopenharmony_ci	FMT_128_BIT(V_038004_COLOR_32_32_32_32_FLOAT, 1),
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	[V_038004_FMT_GB_GR] = { 2, 1, 4, 0 },
1638c2ecf20Sopenharmony_ci	[V_038004_FMT_BG_RG] = { 2, 1, 4, 0 },
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	/* block compressed formats */
1668c2ecf20Sopenharmony_ci	[V_038004_FMT_BC1] = { 4, 4, 8, 0 },
1678c2ecf20Sopenharmony_ci	[V_038004_FMT_BC2] = { 4, 4, 16, 0 },
1688c2ecf20Sopenharmony_ci	[V_038004_FMT_BC3] = { 4, 4, 16, 0 },
1698c2ecf20Sopenharmony_ci	[V_038004_FMT_BC4] = { 4, 4, 8, 0 },
1708c2ecf20Sopenharmony_ci	[V_038004_FMT_BC5] = { 4, 4, 16, 0},
1718c2ecf20Sopenharmony_ci	[V_038004_FMT_BC6] = { 4, 4, 16, 0, CHIP_CEDAR}, /* Evergreen-only */
1728c2ecf20Sopenharmony_ci	[V_038004_FMT_BC7] = { 4, 4, 16, 0, CHIP_CEDAR}, /* Evergreen-only */
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	/* The other Evergreen formats */
1758c2ecf20Sopenharmony_ci	[V_038004_FMT_32_AS_32_32_32_32] = { 1, 1, 4, 0, CHIP_CEDAR},
1768c2ecf20Sopenharmony_ci};
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cibool r600_fmt_is_valid_color(u32 format)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	if (format >= ARRAY_SIZE(color_formats_table))
1818c2ecf20Sopenharmony_ci		return false;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (color_formats_table[format].valid_color)
1848c2ecf20Sopenharmony_ci		return true;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	return false;
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cibool r600_fmt_is_valid_texture(u32 format, enum radeon_family family)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	if (format >= ARRAY_SIZE(color_formats_table))
1928c2ecf20Sopenharmony_ci		return false;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	if (family < color_formats_table[format].min_family)
1958c2ecf20Sopenharmony_ci		return false;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	if (color_formats_table[format].blockwidth > 0)
1988c2ecf20Sopenharmony_ci		return true;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return false;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ciint r600_fmt_get_blocksize(u32 format)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	if (format >= ARRAY_SIZE(color_formats_table))
2068c2ecf20Sopenharmony_ci		return 0;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	return color_formats_table[format].blocksize;
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ciint r600_fmt_get_nblocksx(u32 format, u32 w)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	unsigned bw;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	if (format >= ARRAY_SIZE(color_formats_table))
2168c2ecf20Sopenharmony_ci		return 0;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	bw = color_formats_table[format].blockwidth;
2198c2ecf20Sopenharmony_ci	if (bw == 0)
2208c2ecf20Sopenharmony_ci		return 0;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	return (w + bw - 1) / bw;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ciint r600_fmt_get_nblocksy(u32 format, u32 h)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	unsigned bh;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (format >= ARRAY_SIZE(color_formats_table))
2308c2ecf20Sopenharmony_ci		return 0;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	bh = color_formats_table[format].blockheight;
2338c2ecf20Sopenharmony_ci	if (bh == 0)
2348c2ecf20Sopenharmony_ci		return 0;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	return (h + bh - 1) / bh;
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistruct array_mode_checker {
2408c2ecf20Sopenharmony_ci	int array_mode;
2418c2ecf20Sopenharmony_ci	u32 group_size;
2428c2ecf20Sopenharmony_ci	u32 nbanks;
2438c2ecf20Sopenharmony_ci	u32 npipes;
2448c2ecf20Sopenharmony_ci	u32 nsamples;
2458c2ecf20Sopenharmony_ci	u32 blocksize;
2468c2ecf20Sopenharmony_ci};
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci/* returns alignment in pixels for pitch/height/depth and bytes for base */
2498c2ecf20Sopenharmony_cistatic int r600_get_array_mode_alignment(struct array_mode_checker *values,
2508c2ecf20Sopenharmony_ci						u32 *pitch_align,
2518c2ecf20Sopenharmony_ci						u32 *height_align,
2528c2ecf20Sopenharmony_ci						u32 *depth_align,
2538c2ecf20Sopenharmony_ci						u64 *base_align)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	u32 tile_width = 8;
2568c2ecf20Sopenharmony_ci	u32 tile_height = 8;
2578c2ecf20Sopenharmony_ci	u32 macro_tile_width = values->nbanks;
2588c2ecf20Sopenharmony_ci	u32 macro_tile_height = values->npipes;
2598c2ecf20Sopenharmony_ci	u32 tile_bytes = tile_width * tile_height * values->blocksize * values->nsamples;
2608c2ecf20Sopenharmony_ci	u32 macro_tile_bytes = macro_tile_width * macro_tile_height * tile_bytes;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	switch (values->array_mode) {
2638c2ecf20Sopenharmony_ci	case ARRAY_LINEAR_GENERAL:
2648c2ecf20Sopenharmony_ci		/* technically tile_width/_height for pitch/height */
2658c2ecf20Sopenharmony_ci		*pitch_align = 1; /* tile_width */
2668c2ecf20Sopenharmony_ci		*height_align = 1; /* tile_height */
2678c2ecf20Sopenharmony_ci		*depth_align = 1;
2688c2ecf20Sopenharmony_ci		*base_align = 1;
2698c2ecf20Sopenharmony_ci		break;
2708c2ecf20Sopenharmony_ci	case ARRAY_LINEAR_ALIGNED:
2718c2ecf20Sopenharmony_ci		*pitch_align = max((u32)64, (u32)(values->group_size / values->blocksize));
2728c2ecf20Sopenharmony_ci		*height_align = 1;
2738c2ecf20Sopenharmony_ci		*depth_align = 1;
2748c2ecf20Sopenharmony_ci		*base_align = values->group_size;
2758c2ecf20Sopenharmony_ci		break;
2768c2ecf20Sopenharmony_ci	case ARRAY_1D_TILED_THIN1:
2778c2ecf20Sopenharmony_ci		*pitch_align = max((u32)tile_width,
2788c2ecf20Sopenharmony_ci				   (u32)(values->group_size /
2798c2ecf20Sopenharmony_ci					 (tile_height * values->blocksize * values->nsamples)));
2808c2ecf20Sopenharmony_ci		*height_align = tile_height;
2818c2ecf20Sopenharmony_ci		*depth_align = 1;
2828c2ecf20Sopenharmony_ci		*base_align = values->group_size;
2838c2ecf20Sopenharmony_ci		break;
2848c2ecf20Sopenharmony_ci	case ARRAY_2D_TILED_THIN1:
2858c2ecf20Sopenharmony_ci		*pitch_align = max((u32)macro_tile_width * tile_width,
2868c2ecf20Sopenharmony_ci				(u32)((values->group_size * values->nbanks) /
2878c2ecf20Sopenharmony_ci				(values->blocksize * values->nsamples * tile_width)));
2888c2ecf20Sopenharmony_ci		*height_align = macro_tile_height * tile_height;
2898c2ecf20Sopenharmony_ci		*depth_align = 1;
2908c2ecf20Sopenharmony_ci		*base_align = max(macro_tile_bytes,
2918c2ecf20Sopenharmony_ci				  (*pitch_align) * values->blocksize * (*height_align) * values->nsamples);
2928c2ecf20Sopenharmony_ci		break;
2938c2ecf20Sopenharmony_ci	default:
2948c2ecf20Sopenharmony_ci		return -EINVAL;
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	return 0;
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_cistatic void r600_cs_track_init(struct r600_cs_track *track)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	int i;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	/* assume DX9 mode */
3058c2ecf20Sopenharmony_ci	track->sq_config = DX9_CONSTS;
3068c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++) {
3078c2ecf20Sopenharmony_ci		track->cb_color_base_last[i] = 0;
3088c2ecf20Sopenharmony_ci		track->cb_color_size[i] = 0;
3098c2ecf20Sopenharmony_ci		track->cb_color_size_idx[i] = 0;
3108c2ecf20Sopenharmony_ci		track->cb_color_info[i] = 0;
3118c2ecf20Sopenharmony_ci		track->cb_color_view[i] = 0xFFFFFFFF;
3128c2ecf20Sopenharmony_ci		track->cb_color_bo[i] = NULL;
3138c2ecf20Sopenharmony_ci		track->cb_color_bo_offset[i] = 0xFFFFFFFF;
3148c2ecf20Sopenharmony_ci		track->cb_color_bo_mc[i] = 0xFFFFFFFF;
3158c2ecf20Sopenharmony_ci		track->cb_color_frag_bo[i] = NULL;
3168c2ecf20Sopenharmony_ci		track->cb_color_frag_offset[i] = 0xFFFFFFFF;
3178c2ecf20Sopenharmony_ci		track->cb_color_tile_bo[i] = NULL;
3188c2ecf20Sopenharmony_ci		track->cb_color_tile_offset[i] = 0xFFFFFFFF;
3198c2ecf20Sopenharmony_ci		track->cb_color_mask[i] = 0xFFFFFFFF;
3208c2ecf20Sopenharmony_ci	}
3218c2ecf20Sopenharmony_ci	track->is_resolve = false;
3228c2ecf20Sopenharmony_ci	track->nsamples = 16;
3238c2ecf20Sopenharmony_ci	track->log_nsamples = 4;
3248c2ecf20Sopenharmony_ci	track->cb_target_mask = 0xFFFFFFFF;
3258c2ecf20Sopenharmony_ci	track->cb_shader_mask = 0xFFFFFFFF;
3268c2ecf20Sopenharmony_ci	track->cb_dirty = true;
3278c2ecf20Sopenharmony_ci	track->db_bo = NULL;
3288c2ecf20Sopenharmony_ci	track->db_bo_mc = 0xFFFFFFFF;
3298c2ecf20Sopenharmony_ci	/* assume the biggest format and that htile is enabled */
3308c2ecf20Sopenharmony_ci	track->db_depth_info = 7 | (1 << 25);
3318c2ecf20Sopenharmony_ci	track->db_depth_view = 0xFFFFC000;
3328c2ecf20Sopenharmony_ci	track->db_depth_size = 0xFFFFFFFF;
3338c2ecf20Sopenharmony_ci	track->db_depth_size_idx = 0;
3348c2ecf20Sopenharmony_ci	track->db_depth_control = 0xFFFFFFFF;
3358c2ecf20Sopenharmony_ci	track->db_dirty = true;
3368c2ecf20Sopenharmony_ci	track->htile_bo = NULL;
3378c2ecf20Sopenharmony_ci	track->htile_offset = 0xFFFFFFFF;
3388c2ecf20Sopenharmony_ci	track->htile_surface = 0;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
3418c2ecf20Sopenharmony_ci		track->vgt_strmout_size[i] = 0;
3428c2ecf20Sopenharmony_ci		track->vgt_strmout_bo[i] = NULL;
3438c2ecf20Sopenharmony_ci		track->vgt_strmout_bo_offset[i] = 0xFFFFFFFF;
3448c2ecf20Sopenharmony_ci		track->vgt_strmout_bo_mc[i] = 0xFFFFFFFF;
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci	track->streamout_dirty = true;
3478c2ecf20Sopenharmony_ci	track->sx_misc_kill_all_prims = false;
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic int r600_cs_track_validate_cb(struct radeon_cs_parser *p, int i)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	struct r600_cs_track *track = p->track;
3538c2ecf20Sopenharmony_ci	u32 slice_tile_max, tmp;
3548c2ecf20Sopenharmony_ci	u32 height, height_align, pitch, pitch_align, depth_align;
3558c2ecf20Sopenharmony_ci	u64 base_offset, base_align;
3568c2ecf20Sopenharmony_ci	struct array_mode_checker array_check;
3578c2ecf20Sopenharmony_ci	volatile u32 *ib = p->ib.ptr;
3588c2ecf20Sopenharmony_ci	unsigned array_mode;
3598c2ecf20Sopenharmony_ci	u32 format;
3608c2ecf20Sopenharmony_ci	/* When resolve is used, the second colorbuffer has always 1 sample. */
3618c2ecf20Sopenharmony_ci	unsigned nsamples = track->is_resolve && i == 1 ? 1 : track->nsamples;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	format = G_0280A0_FORMAT(track->cb_color_info[i]);
3648c2ecf20Sopenharmony_ci	if (!r600_fmt_is_valid_color(format)) {
3658c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s:%d cb invalid format %d for %d (0x%08X)\n",
3668c2ecf20Sopenharmony_ci			 __func__, __LINE__, format,
3678c2ecf20Sopenharmony_ci			i, track->cb_color_info[i]);
3688c2ecf20Sopenharmony_ci		return -EINVAL;
3698c2ecf20Sopenharmony_ci	}
3708c2ecf20Sopenharmony_ci	/* pitch in pixels */
3718c2ecf20Sopenharmony_ci	pitch = (G_028060_PITCH_TILE_MAX(track->cb_color_size[i]) + 1) * 8;
3728c2ecf20Sopenharmony_ci	slice_tile_max = G_028060_SLICE_TILE_MAX(track->cb_color_size[i]) + 1;
3738c2ecf20Sopenharmony_ci	slice_tile_max *= 64;
3748c2ecf20Sopenharmony_ci	height = slice_tile_max / pitch;
3758c2ecf20Sopenharmony_ci	if (height > 8192)
3768c2ecf20Sopenharmony_ci		height = 8192;
3778c2ecf20Sopenharmony_ci	array_mode = G_0280A0_ARRAY_MODE(track->cb_color_info[i]);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	base_offset = track->cb_color_bo_mc[i] + track->cb_color_bo_offset[i];
3808c2ecf20Sopenharmony_ci	array_check.array_mode = array_mode;
3818c2ecf20Sopenharmony_ci	array_check.group_size = track->group_size;
3828c2ecf20Sopenharmony_ci	array_check.nbanks = track->nbanks;
3838c2ecf20Sopenharmony_ci	array_check.npipes = track->npipes;
3848c2ecf20Sopenharmony_ci	array_check.nsamples = nsamples;
3858c2ecf20Sopenharmony_ci	array_check.blocksize = r600_fmt_get_blocksize(format);
3868c2ecf20Sopenharmony_ci	if (r600_get_array_mode_alignment(&array_check,
3878c2ecf20Sopenharmony_ci					  &pitch_align, &height_align, &depth_align, &base_align)) {
3888c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s invalid tiling %d for %d (0x%08X)\n", __func__,
3898c2ecf20Sopenharmony_ci			 G_0280A0_ARRAY_MODE(track->cb_color_info[i]), i,
3908c2ecf20Sopenharmony_ci			 track->cb_color_info[i]);
3918c2ecf20Sopenharmony_ci		return -EINVAL;
3928c2ecf20Sopenharmony_ci	}
3938c2ecf20Sopenharmony_ci	switch (array_mode) {
3948c2ecf20Sopenharmony_ci	case V_0280A0_ARRAY_LINEAR_GENERAL:
3958c2ecf20Sopenharmony_ci		break;
3968c2ecf20Sopenharmony_ci	case V_0280A0_ARRAY_LINEAR_ALIGNED:
3978c2ecf20Sopenharmony_ci		break;
3988c2ecf20Sopenharmony_ci	case V_0280A0_ARRAY_1D_TILED_THIN1:
3998c2ecf20Sopenharmony_ci		/* avoid breaking userspace */
4008c2ecf20Sopenharmony_ci		if (height > 7)
4018c2ecf20Sopenharmony_ci			height &= ~0x7;
4028c2ecf20Sopenharmony_ci		break;
4038c2ecf20Sopenharmony_ci	case V_0280A0_ARRAY_2D_TILED_THIN1:
4048c2ecf20Sopenharmony_ci		break;
4058c2ecf20Sopenharmony_ci	default:
4068c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s invalid tiling %d for %d (0x%08X)\n", __func__,
4078c2ecf20Sopenharmony_ci			G_0280A0_ARRAY_MODE(track->cb_color_info[i]), i,
4088c2ecf20Sopenharmony_ci			track->cb_color_info[i]);
4098c2ecf20Sopenharmony_ci		return -EINVAL;
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	if (!IS_ALIGNED(pitch, pitch_align)) {
4138c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s:%d cb pitch (%d, 0x%x, %d) invalid\n",
4148c2ecf20Sopenharmony_ci			 __func__, __LINE__, pitch, pitch_align, array_mode);
4158c2ecf20Sopenharmony_ci		return -EINVAL;
4168c2ecf20Sopenharmony_ci	}
4178c2ecf20Sopenharmony_ci	if (!IS_ALIGNED(height, height_align)) {
4188c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s:%d cb height (%d, 0x%x, %d) invalid\n",
4198c2ecf20Sopenharmony_ci			 __func__, __LINE__, height, height_align, array_mode);
4208c2ecf20Sopenharmony_ci		return -EINVAL;
4218c2ecf20Sopenharmony_ci	}
4228c2ecf20Sopenharmony_ci	if (!IS_ALIGNED(base_offset, base_align)) {
4238c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s offset[%d] 0x%llx 0x%llx, %d not aligned\n", __func__, i,
4248c2ecf20Sopenharmony_ci			 base_offset, base_align, array_mode);
4258c2ecf20Sopenharmony_ci		return -EINVAL;
4268c2ecf20Sopenharmony_ci	}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	/* check offset */
4298c2ecf20Sopenharmony_ci	tmp = r600_fmt_get_nblocksy(format, height) * r600_fmt_get_nblocksx(format, pitch) *
4308c2ecf20Sopenharmony_ci	      r600_fmt_get_blocksize(format) * nsamples;
4318c2ecf20Sopenharmony_ci	switch (array_mode) {
4328c2ecf20Sopenharmony_ci	default:
4338c2ecf20Sopenharmony_ci	case V_0280A0_ARRAY_LINEAR_GENERAL:
4348c2ecf20Sopenharmony_ci	case V_0280A0_ARRAY_LINEAR_ALIGNED:
4358c2ecf20Sopenharmony_ci		tmp += track->cb_color_view[i] & 0xFF;
4368c2ecf20Sopenharmony_ci		break;
4378c2ecf20Sopenharmony_ci	case V_0280A0_ARRAY_1D_TILED_THIN1:
4388c2ecf20Sopenharmony_ci	case V_0280A0_ARRAY_2D_TILED_THIN1:
4398c2ecf20Sopenharmony_ci		tmp += G_028080_SLICE_MAX(track->cb_color_view[i]) * tmp;
4408c2ecf20Sopenharmony_ci		break;
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci	if ((tmp + track->cb_color_bo_offset[i]) > radeon_bo_size(track->cb_color_bo[i])) {
4438c2ecf20Sopenharmony_ci		if (array_mode == V_0280A0_ARRAY_LINEAR_GENERAL) {
4448c2ecf20Sopenharmony_ci			/* the initial DDX does bad things with the CB size occasionally */
4458c2ecf20Sopenharmony_ci			/* it rounds up height too far for slice tile max but the BO is smaller */
4468c2ecf20Sopenharmony_ci			/* r600c,g also seem to flush at bad times in some apps resulting in
4478c2ecf20Sopenharmony_ci			 * bogus values here. So for linear just allow anything to avoid breaking
4488c2ecf20Sopenharmony_ci			 * broken userspace.
4498c2ecf20Sopenharmony_ci			 */
4508c2ecf20Sopenharmony_ci		} else {
4518c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s offset[%d] %d %llu %d %lu too big (%d %d) (%d %d %d)\n",
4528c2ecf20Sopenharmony_ci				 __func__, i, array_mode,
4538c2ecf20Sopenharmony_ci				 track->cb_color_bo_offset[i], tmp,
4548c2ecf20Sopenharmony_ci				 radeon_bo_size(track->cb_color_bo[i]),
4558c2ecf20Sopenharmony_ci				 pitch, height, r600_fmt_get_nblocksx(format, pitch),
4568c2ecf20Sopenharmony_ci				 r600_fmt_get_nblocksy(format, height),
4578c2ecf20Sopenharmony_ci				 r600_fmt_get_blocksize(format));
4588c2ecf20Sopenharmony_ci			return -EINVAL;
4598c2ecf20Sopenharmony_ci		}
4608c2ecf20Sopenharmony_ci	}
4618c2ecf20Sopenharmony_ci	/* limit max tile */
4628c2ecf20Sopenharmony_ci	tmp = (height * pitch) >> 6;
4638c2ecf20Sopenharmony_ci	if (tmp < slice_tile_max)
4648c2ecf20Sopenharmony_ci		slice_tile_max = tmp;
4658c2ecf20Sopenharmony_ci	tmp = S_028060_PITCH_TILE_MAX((pitch / 8) - 1) |
4668c2ecf20Sopenharmony_ci		S_028060_SLICE_TILE_MAX(slice_tile_max - 1);
4678c2ecf20Sopenharmony_ci	ib[track->cb_color_size_idx[i]] = tmp;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	/* FMASK/CMASK */
4708c2ecf20Sopenharmony_ci	switch (G_0280A0_TILE_MODE(track->cb_color_info[i])) {
4718c2ecf20Sopenharmony_ci	case V_0280A0_TILE_DISABLE:
4728c2ecf20Sopenharmony_ci		break;
4738c2ecf20Sopenharmony_ci	case V_0280A0_FRAG_ENABLE:
4748c2ecf20Sopenharmony_ci		if (track->nsamples > 1) {
4758c2ecf20Sopenharmony_ci			uint32_t tile_max = G_028100_FMASK_TILE_MAX(track->cb_color_mask[i]);
4768c2ecf20Sopenharmony_ci			/* the tile size is 8x8, but the size is in units of bits.
4778c2ecf20Sopenharmony_ci			 * for bytes, do just * 8. */
4788c2ecf20Sopenharmony_ci			uint32_t bytes = track->nsamples * track->log_nsamples * 8 * (tile_max + 1);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci			if (bytes + track->cb_color_frag_offset[i] >
4818c2ecf20Sopenharmony_ci			    radeon_bo_size(track->cb_color_frag_bo[i])) {
4828c2ecf20Sopenharmony_ci				dev_warn(p->dev, "%s FMASK_TILE_MAX too large "
4838c2ecf20Sopenharmony_ci					 "(tile_max=%u, bytes=%u, offset=%llu, bo_size=%lu)\n",
4848c2ecf20Sopenharmony_ci					 __func__, tile_max, bytes,
4858c2ecf20Sopenharmony_ci					 track->cb_color_frag_offset[i],
4868c2ecf20Sopenharmony_ci					 radeon_bo_size(track->cb_color_frag_bo[i]));
4878c2ecf20Sopenharmony_ci				return -EINVAL;
4888c2ecf20Sopenharmony_ci			}
4898c2ecf20Sopenharmony_ci		}
4908c2ecf20Sopenharmony_ci		fallthrough;
4918c2ecf20Sopenharmony_ci	case V_0280A0_CLEAR_ENABLE:
4928c2ecf20Sopenharmony_ci	{
4938c2ecf20Sopenharmony_ci		uint32_t block_max = G_028100_CMASK_BLOCK_MAX(track->cb_color_mask[i]);
4948c2ecf20Sopenharmony_ci		/* One block = 128x128 pixels, one 8x8 tile has 4 bits..
4958c2ecf20Sopenharmony_ci		 * (128*128) / (8*8) / 2 = 128 bytes per block. */
4968c2ecf20Sopenharmony_ci		uint32_t bytes = (block_max + 1) * 128;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci		if (bytes + track->cb_color_tile_offset[i] >
4998c2ecf20Sopenharmony_ci		    radeon_bo_size(track->cb_color_tile_bo[i])) {
5008c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s CMASK_BLOCK_MAX too large "
5018c2ecf20Sopenharmony_ci				 "(block_max=%u, bytes=%u, offset=%llu, bo_size=%lu)\n",
5028c2ecf20Sopenharmony_ci				 __func__, block_max, bytes,
5038c2ecf20Sopenharmony_ci				 track->cb_color_tile_offset[i],
5048c2ecf20Sopenharmony_ci				 radeon_bo_size(track->cb_color_tile_bo[i]));
5058c2ecf20Sopenharmony_ci			return -EINVAL;
5068c2ecf20Sopenharmony_ci		}
5078c2ecf20Sopenharmony_ci		break;
5088c2ecf20Sopenharmony_ci	}
5098c2ecf20Sopenharmony_ci	default:
5108c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s invalid tile mode\n", __func__);
5118c2ecf20Sopenharmony_ci		return -EINVAL;
5128c2ecf20Sopenharmony_ci	}
5138c2ecf20Sopenharmony_ci	return 0;
5148c2ecf20Sopenharmony_ci}
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_cistatic int r600_cs_track_validate_db(struct radeon_cs_parser *p)
5178c2ecf20Sopenharmony_ci{
5188c2ecf20Sopenharmony_ci	struct r600_cs_track *track = p->track;
5198c2ecf20Sopenharmony_ci	u32 nviews, bpe, ntiles, slice_tile_max, tmp;
5208c2ecf20Sopenharmony_ci	u32 height_align, pitch_align, depth_align;
5218c2ecf20Sopenharmony_ci	u32 pitch = 8192;
5228c2ecf20Sopenharmony_ci	u32 height = 8192;
5238c2ecf20Sopenharmony_ci	u64 base_offset, base_align;
5248c2ecf20Sopenharmony_ci	struct array_mode_checker array_check;
5258c2ecf20Sopenharmony_ci	int array_mode;
5268c2ecf20Sopenharmony_ci	volatile u32 *ib = p->ib.ptr;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	if (track->db_bo == NULL) {
5308c2ecf20Sopenharmony_ci		dev_warn(p->dev, "z/stencil with no depth buffer\n");
5318c2ecf20Sopenharmony_ci		return -EINVAL;
5328c2ecf20Sopenharmony_ci	}
5338c2ecf20Sopenharmony_ci	switch (G_028010_FORMAT(track->db_depth_info)) {
5348c2ecf20Sopenharmony_ci	case V_028010_DEPTH_16:
5358c2ecf20Sopenharmony_ci		bpe = 2;
5368c2ecf20Sopenharmony_ci		break;
5378c2ecf20Sopenharmony_ci	case V_028010_DEPTH_X8_24:
5388c2ecf20Sopenharmony_ci	case V_028010_DEPTH_8_24:
5398c2ecf20Sopenharmony_ci	case V_028010_DEPTH_X8_24_FLOAT:
5408c2ecf20Sopenharmony_ci	case V_028010_DEPTH_8_24_FLOAT:
5418c2ecf20Sopenharmony_ci	case V_028010_DEPTH_32_FLOAT:
5428c2ecf20Sopenharmony_ci		bpe = 4;
5438c2ecf20Sopenharmony_ci		break;
5448c2ecf20Sopenharmony_ci	case V_028010_DEPTH_X24_8_32_FLOAT:
5458c2ecf20Sopenharmony_ci		bpe = 8;
5468c2ecf20Sopenharmony_ci		break;
5478c2ecf20Sopenharmony_ci	default:
5488c2ecf20Sopenharmony_ci		dev_warn(p->dev, "z/stencil with invalid format %d\n", G_028010_FORMAT(track->db_depth_info));
5498c2ecf20Sopenharmony_ci		return -EINVAL;
5508c2ecf20Sopenharmony_ci	}
5518c2ecf20Sopenharmony_ci	if ((track->db_depth_size & 0xFFFFFC00) == 0xFFFFFC00) {
5528c2ecf20Sopenharmony_ci		if (!track->db_depth_size_idx) {
5538c2ecf20Sopenharmony_ci			dev_warn(p->dev, "z/stencil buffer size not set\n");
5548c2ecf20Sopenharmony_ci			return -EINVAL;
5558c2ecf20Sopenharmony_ci		}
5568c2ecf20Sopenharmony_ci		tmp = radeon_bo_size(track->db_bo) - track->db_offset;
5578c2ecf20Sopenharmony_ci		tmp = (tmp / bpe) >> 6;
5588c2ecf20Sopenharmony_ci		if (!tmp) {
5598c2ecf20Sopenharmony_ci			dev_warn(p->dev, "z/stencil buffer too small (0x%08X %d %d %ld)\n",
5608c2ecf20Sopenharmony_ci					track->db_depth_size, bpe, track->db_offset,
5618c2ecf20Sopenharmony_ci					radeon_bo_size(track->db_bo));
5628c2ecf20Sopenharmony_ci			return -EINVAL;
5638c2ecf20Sopenharmony_ci		}
5648c2ecf20Sopenharmony_ci		ib[track->db_depth_size_idx] = S_028000_SLICE_TILE_MAX(tmp - 1) | (track->db_depth_size & 0x3FF);
5658c2ecf20Sopenharmony_ci	} else {
5668c2ecf20Sopenharmony_ci		/* pitch in pixels */
5678c2ecf20Sopenharmony_ci		pitch = (G_028000_PITCH_TILE_MAX(track->db_depth_size) + 1) * 8;
5688c2ecf20Sopenharmony_ci		slice_tile_max = G_028000_SLICE_TILE_MAX(track->db_depth_size) + 1;
5698c2ecf20Sopenharmony_ci		slice_tile_max *= 64;
5708c2ecf20Sopenharmony_ci		height = slice_tile_max / pitch;
5718c2ecf20Sopenharmony_ci		if (height > 8192)
5728c2ecf20Sopenharmony_ci			height = 8192;
5738c2ecf20Sopenharmony_ci		base_offset = track->db_bo_mc + track->db_offset;
5748c2ecf20Sopenharmony_ci		array_mode = G_028010_ARRAY_MODE(track->db_depth_info);
5758c2ecf20Sopenharmony_ci		array_check.array_mode = array_mode;
5768c2ecf20Sopenharmony_ci		array_check.group_size = track->group_size;
5778c2ecf20Sopenharmony_ci		array_check.nbanks = track->nbanks;
5788c2ecf20Sopenharmony_ci		array_check.npipes = track->npipes;
5798c2ecf20Sopenharmony_ci		array_check.nsamples = track->nsamples;
5808c2ecf20Sopenharmony_ci		array_check.blocksize = bpe;
5818c2ecf20Sopenharmony_ci		if (r600_get_array_mode_alignment(&array_check,
5828c2ecf20Sopenharmony_ci					&pitch_align, &height_align, &depth_align, &base_align)) {
5838c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s invalid tiling %d (0x%08X)\n", __func__,
5848c2ecf20Sopenharmony_ci					G_028010_ARRAY_MODE(track->db_depth_info),
5858c2ecf20Sopenharmony_ci					track->db_depth_info);
5868c2ecf20Sopenharmony_ci			return -EINVAL;
5878c2ecf20Sopenharmony_ci		}
5888c2ecf20Sopenharmony_ci		switch (array_mode) {
5898c2ecf20Sopenharmony_ci		case V_028010_ARRAY_1D_TILED_THIN1:
5908c2ecf20Sopenharmony_ci			/* don't break userspace */
5918c2ecf20Sopenharmony_ci			height &= ~0x7;
5928c2ecf20Sopenharmony_ci			break;
5938c2ecf20Sopenharmony_ci		case V_028010_ARRAY_2D_TILED_THIN1:
5948c2ecf20Sopenharmony_ci			break;
5958c2ecf20Sopenharmony_ci		default:
5968c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s invalid tiling %d (0x%08X)\n", __func__,
5978c2ecf20Sopenharmony_ci					G_028010_ARRAY_MODE(track->db_depth_info),
5988c2ecf20Sopenharmony_ci					track->db_depth_info);
5998c2ecf20Sopenharmony_ci			return -EINVAL;
6008c2ecf20Sopenharmony_ci		}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci		if (!IS_ALIGNED(pitch, pitch_align)) {
6038c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s:%d db pitch (%d, 0x%x, %d) invalid\n",
6048c2ecf20Sopenharmony_ci					__func__, __LINE__, pitch, pitch_align, array_mode);
6058c2ecf20Sopenharmony_ci			return -EINVAL;
6068c2ecf20Sopenharmony_ci		}
6078c2ecf20Sopenharmony_ci		if (!IS_ALIGNED(height, height_align)) {
6088c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s:%d db height (%d, 0x%x, %d) invalid\n",
6098c2ecf20Sopenharmony_ci					__func__, __LINE__, height, height_align, array_mode);
6108c2ecf20Sopenharmony_ci			return -EINVAL;
6118c2ecf20Sopenharmony_ci		}
6128c2ecf20Sopenharmony_ci		if (!IS_ALIGNED(base_offset, base_align)) {
6138c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s offset 0x%llx, 0x%llx, %d not aligned\n", __func__,
6148c2ecf20Sopenharmony_ci					base_offset, base_align, array_mode);
6158c2ecf20Sopenharmony_ci			return -EINVAL;
6168c2ecf20Sopenharmony_ci		}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci		ntiles = G_028000_SLICE_TILE_MAX(track->db_depth_size) + 1;
6198c2ecf20Sopenharmony_ci		nviews = G_028004_SLICE_MAX(track->db_depth_view) + 1;
6208c2ecf20Sopenharmony_ci		tmp = ntiles * bpe * 64 * nviews * track->nsamples;
6218c2ecf20Sopenharmony_ci		if ((tmp + track->db_offset) > radeon_bo_size(track->db_bo)) {
6228c2ecf20Sopenharmony_ci			dev_warn(p->dev, "z/stencil buffer (%d) too small (0x%08X %d %d %d -> %u have %lu)\n",
6238c2ecf20Sopenharmony_ci					array_mode,
6248c2ecf20Sopenharmony_ci					track->db_depth_size, ntiles, nviews, bpe, tmp + track->db_offset,
6258c2ecf20Sopenharmony_ci					radeon_bo_size(track->db_bo));
6268c2ecf20Sopenharmony_ci			return -EINVAL;
6278c2ecf20Sopenharmony_ci		}
6288c2ecf20Sopenharmony_ci	}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	/* hyperz */
6318c2ecf20Sopenharmony_ci	if (G_028010_TILE_SURFACE_ENABLE(track->db_depth_info)) {
6328c2ecf20Sopenharmony_ci		unsigned long size;
6338c2ecf20Sopenharmony_ci		unsigned nbx, nby;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci		if (track->htile_bo == NULL) {
6368c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s:%d htile enabled without htile surface 0x%08x\n",
6378c2ecf20Sopenharmony_ci				 __func__, __LINE__, track->db_depth_info);
6388c2ecf20Sopenharmony_ci			return -EINVAL;
6398c2ecf20Sopenharmony_ci		}
6408c2ecf20Sopenharmony_ci		if ((track->db_depth_size & 0xFFFFFC00) == 0xFFFFFC00) {
6418c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s:%d htile can't be enabled with bogus db_depth_size 0x%08x\n",
6428c2ecf20Sopenharmony_ci				 __func__, __LINE__, track->db_depth_size);
6438c2ecf20Sopenharmony_ci			return -EINVAL;
6448c2ecf20Sopenharmony_ci		}
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci		nbx = pitch;
6478c2ecf20Sopenharmony_ci		nby = height;
6488c2ecf20Sopenharmony_ci		if (G_028D24_LINEAR(track->htile_surface)) {
6498c2ecf20Sopenharmony_ci			/* nbx must be 16 htiles aligned == 16 * 8 pixel aligned */
6508c2ecf20Sopenharmony_ci			nbx = round_up(nbx, 16 * 8);
6518c2ecf20Sopenharmony_ci			/* nby is npipes htiles aligned == npipes * 8 pixel aligned */
6528c2ecf20Sopenharmony_ci			nby = round_up(nby, track->npipes * 8);
6538c2ecf20Sopenharmony_ci		} else {
6548c2ecf20Sopenharmony_ci			/* always assume 8x8 htile */
6558c2ecf20Sopenharmony_ci			/* align is htile align * 8, htile align vary according to
6568c2ecf20Sopenharmony_ci			 * number of pipe and tile width and nby
6578c2ecf20Sopenharmony_ci			 */
6588c2ecf20Sopenharmony_ci			switch (track->npipes) {
6598c2ecf20Sopenharmony_ci			case 8:
6608c2ecf20Sopenharmony_ci				/* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
6618c2ecf20Sopenharmony_ci				nbx = round_up(nbx, 64 * 8);
6628c2ecf20Sopenharmony_ci				nby = round_up(nby, 64 * 8);
6638c2ecf20Sopenharmony_ci				break;
6648c2ecf20Sopenharmony_ci			case 4:
6658c2ecf20Sopenharmony_ci				/* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
6668c2ecf20Sopenharmony_ci				nbx = round_up(nbx, 64 * 8);
6678c2ecf20Sopenharmony_ci				nby = round_up(nby, 32 * 8);
6688c2ecf20Sopenharmony_ci				break;
6698c2ecf20Sopenharmony_ci			case 2:
6708c2ecf20Sopenharmony_ci				/* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
6718c2ecf20Sopenharmony_ci				nbx = round_up(nbx, 32 * 8);
6728c2ecf20Sopenharmony_ci				nby = round_up(nby, 32 * 8);
6738c2ecf20Sopenharmony_ci				break;
6748c2ecf20Sopenharmony_ci			case 1:
6758c2ecf20Sopenharmony_ci				/* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
6768c2ecf20Sopenharmony_ci				nbx = round_up(nbx, 32 * 8);
6778c2ecf20Sopenharmony_ci				nby = round_up(nby, 16 * 8);
6788c2ecf20Sopenharmony_ci				break;
6798c2ecf20Sopenharmony_ci			default:
6808c2ecf20Sopenharmony_ci				dev_warn(p->dev, "%s:%d invalid num pipes %d\n",
6818c2ecf20Sopenharmony_ci					 __func__, __LINE__, track->npipes);
6828c2ecf20Sopenharmony_ci				return -EINVAL;
6838c2ecf20Sopenharmony_ci			}
6848c2ecf20Sopenharmony_ci		}
6858c2ecf20Sopenharmony_ci		/* compute number of htile */
6868c2ecf20Sopenharmony_ci		nbx = nbx >> 3;
6878c2ecf20Sopenharmony_ci		nby = nby >> 3;
6888c2ecf20Sopenharmony_ci		/* size must be aligned on npipes * 2K boundary */
6898c2ecf20Sopenharmony_ci		size = roundup(nbx * nby * 4, track->npipes * (2 << 10));
6908c2ecf20Sopenharmony_ci		size += track->htile_offset;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci		if (size > radeon_bo_size(track->htile_bo)) {
6938c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s:%d htile surface too small %ld for %ld (%d %d)\n",
6948c2ecf20Sopenharmony_ci				 __func__, __LINE__, radeon_bo_size(track->htile_bo),
6958c2ecf20Sopenharmony_ci				 size, nbx, nby);
6968c2ecf20Sopenharmony_ci			return -EINVAL;
6978c2ecf20Sopenharmony_ci		}
6988c2ecf20Sopenharmony_ci	}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	track->db_dirty = false;
7018c2ecf20Sopenharmony_ci	return 0;
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic int r600_cs_track_check(struct radeon_cs_parser *p)
7058c2ecf20Sopenharmony_ci{
7068c2ecf20Sopenharmony_ci	struct r600_cs_track *track = p->track;
7078c2ecf20Sopenharmony_ci	u32 tmp;
7088c2ecf20Sopenharmony_ci	int r, i;
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/* on legacy kernel we don't perform advanced check */
7118c2ecf20Sopenharmony_ci	if (p->rdev == NULL)
7128c2ecf20Sopenharmony_ci		return 0;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	/* check streamout */
7158c2ecf20Sopenharmony_ci	if (track->streamout_dirty && track->vgt_strmout_en) {
7168c2ecf20Sopenharmony_ci		for (i = 0; i < 4; i++) {
7178c2ecf20Sopenharmony_ci			if (track->vgt_strmout_buffer_en & (1 << i)) {
7188c2ecf20Sopenharmony_ci				if (track->vgt_strmout_bo[i]) {
7198c2ecf20Sopenharmony_ci					u64 offset = (u64)track->vgt_strmout_bo_offset[i] +
7208c2ecf20Sopenharmony_ci						(u64)track->vgt_strmout_size[i];
7218c2ecf20Sopenharmony_ci					if (offset > radeon_bo_size(track->vgt_strmout_bo[i])) {
7228c2ecf20Sopenharmony_ci						DRM_ERROR("streamout %d bo too small: 0x%llx, 0x%lx\n",
7238c2ecf20Sopenharmony_ci							  i, offset,
7248c2ecf20Sopenharmony_ci							  radeon_bo_size(track->vgt_strmout_bo[i]));
7258c2ecf20Sopenharmony_ci						return -EINVAL;
7268c2ecf20Sopenharmony_ci					}
7278c2ecf20Sopenharmony_ci				} else {
7288c2ecf20Sopenharmony_ci					dev_warn(p->dev, "No buffer for streamout %d\n", i);
7298c2ecf20Sopenharmony_ci					return -EINVAL;
7308c2ecf20Sopenharmony_ci				}
7318c2ecf20Sopenharmony_ci			}
7328c2ecf20Sopenharmony_ci		}
7338c2ecf20Sopenharmony_ci		track->streamout_dirty = false;
7348c2ecf20Sopenharmony_ci	}
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	if (track->sx_misc_kill_all_prims)
7378c2ecf20Sopenharmony_ci		return 0;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	/* check that we have a cb for each enabled target, we don't check
7408c2ecf20Sopenharmony_ci	 * shader_mask because it seems mesa isn't always setting it :(
7418c2ecf20Sopenharmony_ci	 */
7428c2ecf20Sopenharmony_ci	if (track->cb_dirty) {
7438c2ecf20Sopenharmony_ci		tmp = track->cb_target_mask;
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci		/* We must check both colorbuffers for RESOLVE. */
7468c2ecf20Sopenharmony_ci		if (track->is_resolve) {
7478c2ecf20Sopenharmony_ci			tmp |= 0xff;
7488c2ecf20Sopenharmony_ci		}
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci		for (i = 0; i < 8; i++) {
7518c2ecf20Sopenharmony_ci			u32 format = G_0280A0_FORMAT(track->cb_color_info[i]);
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci			if (format != V_0280A0_COLOR_INVALID &&
7548c2ecf20Sopenharmony_ci			    (tmp >> (i * 4)) & 0xF) {
7558c2ecf20Sopenharmony_ci				/* at least one component is enabled */
7568c2ecf20Sopenharmony_ci				if (track->cb_color_bo[i] == NULL) {
7578c2ecf20Sopenharmony_ci					dev_warn(p->dev, "%s:%d mask 0x%08X | 0x%08X no cb for %d\n",
7588c2ecf20Sopenharmony_ci						__func__, __LINE__, track->cb_target_mask, track->cb_shader_mask, i);
7598c2ecf20Sopenharmony_ci					return -EINVAL;
7608c2ecf20Sopenharmony_ci				}
7618c2ecf20Sopenharmony_ci				/* perform rewrite of CB_COLOR[0-7]_SIZE */
7628c2ecf20Sopenharmony_ci				r = r600_cs_track_validate_cb(p, i);
7638c2ecf20Sopenharmony_ci				if (r)
7648c2ecf20Sopenharmony_ci					return r;
7658c2ecf20Sopenharmony_ci			}
7668c2ecf20Sopenharmony_ci		}
7678c2ecf20Sopenharmony_ci		track->cb_dirty = false;
7688c2ecf20Sopenharmony_ci	}
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci	/* Check depth buffer */
7718c2ecf20Sopenharmony_ci	if (track->db_dirty &&
7728c2ecf20Sopenharmony_ci	    G_028010_FORMAT(track->db_depth_info) != V_028010_DEPTH_INVALID &&
7738c2ecf20Sopenharmony_ci	    (G_028800_STENCIL_ENABLE(track->db_depth_control) ||
7748c2ecf20Sopenharmony_ci	     G_028800_Z_ENABLE(track->db_depth_control))) {
7758c2ecf20Sopenharmony_ci		r = r600_cs_track_validate_db(p);
7768c2ecf20Sopenharmony_ci		if (r)
7778c2ecf20Sopenharmony_ci			return r;
7788c2ecf20Sopenharmony_ci	}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	return 0;
7818c2ecf20Sopenharmony_ci}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci/**
7848c2ecf20Sopenharmony_ci * r600_cs_packet_parse_vline() - parse userspace VLINE packet
7858c2ecf20Sopenharmony_ci * @parser:		parser structure holding parsing context.
7868c2ecf20Sopenharmony_ci *
7878c2ecf20Sopenharmony_ci * This is an R600-specific function for parsing VLINE packets.
7888c2ecf20Sopenharmony_ci * Real work is done by r600_cs_common_vline_parse function.
7898c2ecf20Sopenharmony_ci * Here we just set up ASIC-specific register table and call
7908c2ecf20Sopenharmony_ci * the common implementation function.
7918c2ecf20Sopenharmony_ci */
7928c2ecf20Sopenharmony_cistatic int r600_cs_packet_parse_vline(struct radeon_cs_parser *p)
7938c2ecf20Sopenharmony_ci{
7948c2ecf20Sopenharmony_ci	static uint32_t vline_start_end[2] = {AVIVO_D1MODE_VLINE_START_END,
7958c2ecf20Sopenharmony_ci					      AVIVO_D2MODE_VLINE_START_END};
7968c2ecf20Sopenharmony_ci	static uint32_t vline_status[2] = {AVIVO_D1MODE_VLINE_STATUS,
7978c2ecf20Sopenharmony_ci					   AVIVO_D2MODE_VLINE_STATUS};
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	return r600_cs_common_vline_parse(p, vline_start_end, vline_status);
8008c2ecf20Sopenharmony_ci}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci/**
8038c2ecf20Sopenharmony_ci * r600_cs_common_vline_parse() - common vline parser
8048c2ecf20Sopenharmony_ci * @parser:		parser structure holding parsing context.
8058c2ecf20Sopenharmony_ci * @vline_start_end:    table of vline_start_end registers
8068c2ecf20Sopenharmony_ci * @vline_status:       table of vline_status registers
8078c2ecf20Sopenharmony_ci *
8088c2ecf20Sopenharmony_ci * Userspace sends a special sequence for VLINE waits.
8098c2ecf20Sopenharmony_ci * PACKET0 - VLINE_START_END + value
8108c2ecf20Sopenharmony_ci * PACKET3 - WAIT_REG_MEM poll vline status reg
8118c2ecf20Sopenharmony_ci * RELOC (P3) - crtc_id in reloc.
8128c2ecf20Sopenharmony_ci *
8138c2ecf20Sopenharmony_ci * This function parses this and relocates the VLINE START END
8148c2ecf20Sopenharmony_ci * and WAIT_REG_MEM packets to the correct crtc.
8158c2ecf20Sopenharmony_ci * It also detects a switched off crtc and nulls out the
8168c2ecf20Sopenharmony_ci * wait in that case. This function is common for all ASICs that
8178c2ecf20Sopenharmony_ci * are R600 and newer. The parsing algorithm is the same, and only
8188c2ecf20Sopenharmony_ci * differs in which registers are used.
8198c2ecf20Sopenharmony_ci *
8208c2ecf20Sopenharmony_ci * Caller is the ASIC-specific function which passes the parser
8218c2ecf20Sopenharmony_ci * context and ASIC-specific register table
8228c2ecf20Sopenharmony_ci */
8238c2ecf20Sopenharmony_ciint r600_cs_common_vline_parse(struct radeon_cs_parser *p,
8248c2ecf20Sopenharmony_ci			       uint32_t *vline_start_end,
8258c2ecf20Sopenharmony_ci			       uint32_t *vline_status)
8268c2ecf20Sopenharmony_ci{
8278c2ecf20Sopenharmony_ci	struct drm_crtc *crtc;
8288c2ecf20Sopenharmony_ci	struct radeon_crtc *radeon_crtc;
8298c2ecf20Sopenharmony_ci	struct radeon_cs_packet p3reloc, wait_reg_mem;
8308c2ecf20Sopenharmony_ci	int crtc_id;
8318c2ecf20Sopenharmony_ci	int r;
8328c2ecf20Sopenharmony_ci	uint32_t header, h_idx, reg, wait_reg_mem_info;
8338c2ecf20Sopenharmony_ci	volatile uint32_t *ib;
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	ib = p->ib.ptr;
8368c2ecf20Sopenharmony_ci
8378c2ecf20Sopenharmony_ci	/* parse the WAIT_REG_MEM */
8388c2ecf20Sopenharmony_ci	r = radeon_cs_packet_parse(p, &wait_reg_mem, p->idx);
8398c2ecf20Sopenharmony_ci	if (r)
8408c2ecf20Sopenharmony_ci		return r;
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	/* check its a WAIT_REG_MEM */
8438c2ecf20Sopenharmony_ci	if (wait_reg_mem.type != RADEON_PACKET_TYPE3 ||
8448c2ecf20Sopenharmony_ci	    wait_reg_mem.opcode != PACKET3_WAIT_REG_MEM) {
8458c2ecf20Sopenharmony_ci		DRM_ERROR("vline wait missing WAIT_REG_MEM segment\n");
8468c2ecf20Sopenharmony_ci		return -EINVAL;
8478c2ecf20Sopenharmony_ci	}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	wait_reg_mem_info = radeon_get_ib_value(p, wait_reg_mem.idx + 1);
8508c2ecf20Sopenharmony_ci	/* bit 4 is reg (0) or mem (1) */
8518c2ecf20Sopenharmony_ci	if (wait_reg_mem_info & 0x10) {
8528c2ecf20Sopenharmony_ci		DRM_ERROR("vline WAIT_REG_MEM waiting on MEM instead of REG\n");
8538c2ecf20Sopenharmony_ci		return -EINVAL;
8548c2ecf20Sopenharmony_ci	}
8558c2ecf20Sopenharmony_ci	/* bit 8 is me (0) or pfp (1) */
8568c2ecf20Sopenharmony_ci	if (wait_reg_mem_info & 0x100) {
8578c2ecf20Sopenharmony_ci		DRM_ERROR("vline WAIT_REG_MEM waiting on PFP instead of ME\n");
8588c2ecf20Sopenharmony_ci		return -EINVAL;
8598c2ecf20Sopenharmony_ci	}
8608c2ecf20Sopenharmony_ci	/* waiting for value to be equal */
8618c2ecf20Sopenharmony_ci	if ((wait_reg_mem_info & 0x7) != 0x3) {
8628c2ecf20Sopenharmony_ci		DRM_ERROR("vline WAIT_REG_MEM function not equal\n");
8638c2ecf20Sopenharmony_ci		return -EINVAL;
8648c2ecf20Sopenharmony_ci	}
8658c2ecf20Sopenharmony_ci	if ((radeon_get_ib_value(p, wait_reg_mem.idx + 2) << 2) != vline_status[0]) {
8668c2ecf20Sopenharmony_ci		DRM_ERROR("vline WAIT_REG_MEM bad reg\n");
8678c2ecf20Sopenharmony_ci		return -EINVAL;
8688c2ecf20Sopenharmony_ci	}
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	if (radeon_get_ib_value(p, wait_reg_mem.idx + 5) != RADEON_VLINE_STAT) {
8718c2ecf20Sopenharmony_ci		DRM_ERROR("vline WAIT_REG_MEM bad bit mask\n");
8728c2ecf20Sopenharmony_ci		return -EINVAL;
8738c2ecf20Sopenharmony_ci	}
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	/* jump over the NOP */
8768c2ecf20Sopenharmony_ci	r = radeon_cs_packet_parse(p, &p3reloc, p->idx + wait_reg_mem.count + 2);
8778c2ecf20Sopenharmony_ci	if (r)
8788c2ecf20Sopenharmony_ci		return r;
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	h_idx = p->idx - 2;
8818c2ecf20Sopenharmony_ci	p->idx += wait_reg_mem.count + 2;
8828c2ecf20Sopenharmony_ci	p->idx += p3reloc.count + 2;
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	header = radeon_get_ib_value(p, h_idx);
8858c2ecf20Sopenharmony_ci	crtc_id = radeon_get_ib_value(p, h_idx + 2 + 7 + 1);
8868c2ecf20Sopenharmony_ci	reg = R600_CP_PACKET0_GET_REG(header);
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	crtc = drm_crtc_find(p->rdev->ddev, p->filp, crtc_id);
8898c2ecf20Sopenharmony_ci	if (!crtc) {
8908c2ecf20Sopenharmony_ci		DRM_ERROR("cannot find crtc %d\n", crtc_id);
8918c2ecf20Sopenharmony_ci		return -ENOENT;
8928c2ecf20Sopenharmony_ci	}
8938c2ecf20Sopenharmony_ci	radeon_crtc = to_radeon_crtc(crtc);
8948c2ecf20Sopenharmony_ci	crtc_id = radeon_crtc->crtc_id;
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	if (!crtc->enabled) {
8978c2ecf20Sopenharmony_ci		/* CRTC isn't enabled - we need to nop out the WAIT_REG_MEM */
8988c2ecf20Sopenharmony_ci		ib[h_idx + 2] = PACKET2(0);
8998c2ecf20Sopenharmony_ci		ib[h_idx + 3] = PACKET2(0);
9008c2ecf20Sopenharmony_ci		ib[h_idx + 4] = PACKET2(0);
9018c2ecf20Sopenharmony_ci		ib[h_idx + 5] = PACKET2(0);
9028c2ecf20Sopenharmony_ci		ib[h_idx + 6] = PACKET2(0);
9038c2ecf20Sopenharmony_ci		ib[h_idx + 7] = PACKET2(0);
9048c2ecf20Sopenharmony_ci		ib[h_idx + 8] = PACKET2(0);
9058c2ecf20Sopenharmony_ci	} else if (reg == vline_start_end[0]) {
9068c2ecf20Sopenharmony_ci		header &= ~R600_CP_PACKET0_REG_MASK;
9078c2ecf20Sopenharmony_ci		header |= vline_start_end[crtc_id] >> 2;
9088c2ecf20Sopenharmony_ci		ib[h_idx] = header;
9098c2ecf20Sopenharmony_ci		ib[h_idx + 4] = vline_status[crtc_id] >> 2;
9108c2ecf20Sopenharmony_ci	} else {
9118c2ecf20Sopenharmony_ci		DRM_ERROR("unknown crtc reloc\n");
9128c2ecf20Sopenharmony_ci		return -EINVAL;
9138c2ecf20Sopenharmony_ci	}
9148c2ecf20Sopenharmony_ci	return 0;
9158c2ecf20Sopenharmony_ci}
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_cistatic int r600_packet0_check(struct radeon_cs_parser *p,
9188c2ecf20Sopenharmony_ci				struct radeon_cs_packet *pkt,
9198c2ecf20Sopenharmony_ci				unsigned idx, unsigned reg)
9208c2ecf20Sopenharmony_ci{
9218c2ecf20Sopenharmony_ci	int r;
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci	switch (reg) {
9248c2ecf20Sopenharmony_ci	case AVIVO_D1MODE_VLINE_START_END:
9258c2ecf20Sopenharmony_ci		r = r600_cs_packet_parse_vline(p);
9268c2ecf20Sopenharmony_ci		if (r) {
9278c2ecf20Sopenharmony_ci			DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
9288c2ecf20Sopenharmony_ci					idx, reg);
9298c2ecf20Sopenharmony_ci			return r;
9308c2ecf20Sopenharmony_ci		}
9318c2ecf20Sopenharmony_ci		break;
9328c2ecf20Sopenharmony_ci	default:
9338c2ecf20Sopenharmony_ci		pr_err("Forbidden register 0x%04X in cs at %d\n", reg, idx);
9348c2ecf20Sopenharmony_ci		return -EINVAL;
9358c2ecf20Sopenharmony_ci	}
9368c2ecf20Sopenharmony_ci	return 0;
9378c2ecf20Sopenharmony_ci}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_cistatic int r600_cs_parse_packet0(struct radeon_cs_parser *p,
9408c2ecf20Sopenharmony_ci				struct radeon_cs_packet *pkt)
9418c2ecf20Sopenharmony_ci{
9428c2ecf20Sopenharmony_ci	unsigned reg, i;
9438c2ecf20Sopenharmony_ci	unsigned idx;
9448c2ecf20Sopenharmony_ci	int r;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	idx = pkt->idx + 1;
9478c2ecf20Sopenharmony_ci	reg = pkt->reg;
9488c2ecf20Sopenharmony_ci	for (i = 0; i <= pkt->count; i++, idx++, reg += 4) {
9498c2ecf20Sopenharmony_ci		r = r600_packet0_check(p, pkt, idx, reg);
9508c2ecf20Sopenharmony_ci		if (r) {
9518c2ecf20Sopenharmony_ci			return r;
9528c2ecf20Sopenharmony_ci		}
9538c2ecf20Sopenharmony_ci	}
9548c2ecf20Sopenharmony_ci	return 0;
9558c2ecf20Sopenharmony_ci}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci/**
9588c2ecf20Sopenharmony_ci * r600_cs_check_reg() - check if register is authorized or not
9598c2ecf20Sopenharmony_ci * @parser: parser structure holding parsing context
9608c2ecf20Sopenharmony_ci * @reg: register we are testing
9618c2ecf20Sopenharmony_ci * @idx: index into the cs buffer
9628c2ecf20Sopenharmony_ci *
9638c2ecf20Sopenharmony_ci * This function will test against r600_reg_safe_bm and return 0
9648c2ecf20Sopenharmony_ci * if register is safe. If register is not flag as safe this function
9658c2ecf20Sopenharmony_ci * will test it against a list of register needind special handling.
9668c2ecf20Sopenharmony_ci */
9678c2ecf20Sopenharmony_cistatic int r600_cs_check_reg(struct radeon_cs_parser *p, u32 reg, u32 idx)
9688c2ecf20Sopenharmony_ci{
9698c2ecf20Sopenharmony_ci	struct r600_cs_track *track = (struct r600_cs_track *)p->track;
9708c2ecf20Sopenharmony_ci	struct radeon_bo_list *reloc;
9718c2ecf20Sopenharmony_ci	u32 m, i, tmp, *ib;
9728c2ecf20Sopenharmony_ci	int r;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	i = (reg >> 7);
9758c2ecf20Sopenharmony_ci	if (i >= ARRAY_SIZE(r600_reg_safe_bm)) {
9768c2ecf20Sopenharmony_ci		dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
9778c2ecf20Sopenharmony_ci		return -EINVAL;
9788c2ecf20Sopenharmony_ci	}
9798c2ecf20Sopenharmony_ci	m = 1 << ((reg >> 2) & 31);
9808c2ecf20Sopenharmony_ci	if (!(r600_reg_safe_bm[i] & m))
9818c2ecf20Sopenharmony_ci		return 0;
9828c2ecf20Sopenharmony_ci	ib = p->ib.ptr;
9838c2ecf20Sopenharmony_ci	switch (reg) {
9848c2ecf20Sopenharmony_ci	/* force following reg to 0 in an attempt to disable out buffer
9858c2ecf20Sopenharmony_ci	 * which will need us to better understand how it works to perform
9868c2ecf20Sopenharmony_ci	 * security check on it (Jerome)
9878c2ecf20Sopenharmony_ci	 */
9888c2ecf20Sopenharmony_ci	case R_0288A8_SQ_ESGS_RING_ITEMSIZE:
9898c2ecf20Sopenharmony_ci	case R_008C44_SQ_ESGS_RING_SIZE:
9908c2ecf20Sopenharmony_ci	case R_0288B0_SQ_ESTMP_RING_ITEMSIZE:
9918c2ecf20Sopenharmony_ci	case R_008C54_SQ_ESTMP_RING_SIZE:
9928c2ecf20Sopenharmony_ci	case R_0288C0_SQ_FBUF_RING_ITEMSIZE:
9938c2ecf20Sopenharmony_ci	case R_008C74_SQ_FBUF_RING_SIZE:
9948c2ecf20Sopenharmony_ci	case R_0288B4_SQ_GSTMP_RING_ITEMSIZE:
9958c2ecf20Sopenharmony_ci	case R_008C5C_SQ_GSTMP_RING_SIZE:
9968c2ecf20Sopenharmony_ci	case R_0288AC_SQ_GSVS_RING_ITEMSIZE:
9978c2ecf20Sopenharmony_ci	case R_008C4C_SQ_GSVS_RING_SIZE:
9988c2ecf20Sopenharmony_ci	case R_0288BC_SQ_PSTMP_RING_ITEMSIZE:
9998c2ecf20Sopenharmony_ci	case R_008C6C_SQ_PSTMP_RING_SIZE:
10008c2ecf20Sopenharmony_ci	case R_0288C4_SQ_REDUC_RING_ITEMSIZE:
10018c2ecf20Sopenharmony_ci	case R_008C7C_SQ_REDUC_RING_SIZE:
10028c2ecf20Sopenharmony_ci	case R_0288B8_SQ_VSTMP_RING_ITEMSIZE:
10038c2ecf20Sopenharmony_ci	case R_008C64_SQ_VSTMP_RING_SIZE:
10048c2ecf20Sopenharmony_ci	case R_0288C8_SQ_GS_VERT_ITEMSIZE:
10058c2ecf20Sopenharmony_ci		/* get value to populate the IB don't remove */
10068c2ecf20Sopenharmony_ci		/*tmp =radeon_get_ib_value(p, idx);
10078c2ecf20Sopenharmony_ci		  ib[idx] = 0;*/
10088c2ecf20Sopenharmony_ci		break;
10098c2ecf20Sopenharmony_ci	case SQ_ESGS_RING_BASE:
10108c2ecf20Sopenharmony_ci	case SQ_GSVS_RING_BASE:
10118c2ecf20Sopenharmony_ci	case SQ_ESTMP_RING_BASE:
10128c2ecf20Sopenharmony_ci	case SQ_GSTMP_RING_BASE:
10138c2ecf20Sopenharmony_ci	case SQ_PSTMP_RING_BASE:
10148c2ecf20Sopenharmony_ci	case SQ_VSTMP_RING_BASE:
10158c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, 0);
10168c2ecf20Sopenharmony_ci		if (r) {
10178c2ecf20Sopenharmony_ci			dev_warn(p->dev, "bad SET_CONTEXT_REG "
10188c2ecf20Sopenharmony_ci					"0x%04X\n", reg);
10198c2ecf20Sopenharmony_ci			return -EINVAL;
10208c2ecf20Sopenharmony_ci		}
10218c2ecf20Sopenharmony_ci		ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
10228c2ecf20Sopenharmony_ci		break;
10238c2ecf20Sopenharmony_ci	case SQ_CONFIG:
10248c2ecf20Sopenharmony_ci		track->sq_config = radeon_get_ib_value(p, idx);
10258c2ecf20Sopenharmony_ci		break;
10268c2ecf20Sopenharmony_ci	case R_028800_DB_DEPTH_CONTROL:
10278c2ecf20Sopenharmony_ci		track->db_depth_control = radeon_get_ib_value(p, idx);
10288c2ecf20Sopenharmony_ci		track->db_dirty = true;
10298c2ecf20Sopenharmony_ci		break;
10308c2ecf20Sopenharmony_ci	case R_028010_DB_DEPTH_INFO:
10318c2ecf20Sopenharmony_ci		if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS) &&
10328c2ecf20Sopenharmony_ci		    radeon_cs_packet_next_is_pkt3_nop(p)) {
10338c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
10348c2ecf20Sopenharmony_ci			if (r) {
10358c2ecf20Sopenharmony_ci				dev_warn(p->dev, "bad SET_CONTEXT_REG "
10368c2ecf20Sopenharmony_ci					 "0x%04X\n", reg);
10378c2ecf20Sopenharmony_ci				return -EINVAL;
10388c2ecf20Sopenharmony_ci			}
10398c2ecf20Sopenharmony_ci			track->db_depth_info = radeon_get_ib_value(p, idx);
10408c2ecf20Sopenharmony_ci			ib[idx] &= C_028010_ARRAY_MODE;
10418c2ecf20Sopenharmony_ci			track->db_depth_info &= C_028010_ARRAY_MODE;
10428c2ecf20Sopenharmony_ci			if (reloc->tiling_flags & RADEON_TILING_MACRO) {
10438c2ecf20Sopenharmony_ci				ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
10448c2ecf20Sopenharmony_ci				track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
10458c2ecf20Sopenharmony_ci			} else {
10468c2ecf20Sopenharmony_ci				ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
10478c2ecf20Sopenharmony_ci				track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
10488c2ecf20Sopenharmony_ci			}
10498c2ecf20Sopenharmony_ci		} else {
10508c2ecf20Sopenharmony_ci			track->db_depth_info = radeon_get_ib_value(p, idx);
10518c2ecf20Sopenharmony_ci		}
10528c2ecf20Sopenharmony_ci		track->db_dirty = true;
10538c2ecf20Sopenharmony_ci		break;
10548c2ecf20Sopenharmony_ci	case R_028004_DB_DEPTH_VIEW:
10558c2ecf20Sopenharmony_ci		track->db_depth_view = radeon_get_ib_value(p, idx);
10568c2ecf20Sopenharmony_ci		track->db_dirty = true;
10578c2ecf20Sopenharmony_ci		break;
10588c2ecf20Sopenharmony_ci	case R_028000_DB_DEPTH_SIZE:
10598c2ecf20Sopenharmony_ci		track->db_depth_size = radeon_get_ib_value(p, idx);
10608c2ecf20Sopenharmony_ci		track->db_depth_size_idx = idx;
10618c2ecf20Sopenharmony_ci		track->db_dirty = true;
10628c2ecf20Sopenharmony_ci		break;
10638c2ecf20Sopenharmony_ci	case R_028AB0_VGT_STRMOUT_EN:
10648c2ecf20Sopenharmony_ci		track->vgt_strmout_en = radeon_get_ib_value(p, idx);
10658c2ecf20Sopenharmony_ci		track->streamout_dirty = true;
10668c2ecf20Sopenharmony_ci		break;
10678c2ecf20Sopenharmony_ci	case R_028B20_VGT_STRMOUT_BUFFER_EN:
10688c2ecf20Sopenharmony_ci		track->vgt_strmout_buffer_en = radeon_get_ib_value(p, idx);
10698c2ecf20Sopenharmony_ci		track->streamout_dirty = true;
10708c2ecf20Sopenharmony_ci		break;
10718c2ecf20Sopenharmony_ci	case VGT_STRMOUT_BUFFER_BASE_0:
10728c2ecf20Sopenharmony_ci	case VGT_STRMOUT_BUFFER_BASE_1:
10738c2ecf20Sopenharmony_ci	case VGT_STRMOUT_BUFFER_BASE_2:
10748c2ecf20Sopenharmony_ci	case VGT_STRMOUT_BUFFER_BASE_3:
10758c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
10768c2ecf20Sopenharmony_ci		if (r) {
10778c2ecf20Sopenharmony_ci			dev_warn(p->dev, "bad SET_CONTEXT_REG "
10788c2ecf20Sopenharmony_ci					"0x%04X\n", reg);
10798c2ecf20Sopenharmony_ci			return -EINVAL;
10808c2ecf20Sopenharmony_ci		}
10818c2ecf20Sopenharmony_ci		tmp = (reg - VGT_STRMOUT_BUFFER_BASE_0) / 16;
10828c2ecf20Sopenharmony_ci		track->vgt_strmout_bo_offset[tmp] = radeon_get_ib_value(p, idx) << 8;
10838c2ecf20Sopenharmony_ci		ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
10848c2ecf20Sopenharmony_ci		track->vgt_strmout_bo[tmp] = reloc->robj;
10858c2ecf20Sopenharmony_ci		track->vgt_strmout_bo_mc[tmp] = reloc->gpu_offset;
10868c2ecf20Sopenharmony_ci		track->streamout_dirty = true;
10878c2ecf20Sopenharmony_ci		break;
10888c2ecf20Sopenharmony_ci	case VGT_STRMOUT_BUFFER_SIZE_0:
10898c2ecf20Sopenharmony_ci	case VGT_STRMOUT_BUFFER_SIZE_1:
10908c2ecf20Sopenharmony_ci	case VGT_STRMOUT_BUFFER_SIZE_2:
10918c2ecf20Sopenharmony_ci	case VGT_STRMOUT_BUFFER_SIZE_3:
10928c2ecf20Sopenharmony_ci		tmp = (reg - VGT_STRMOUT_BUFFER_SIZE_0) / 16;
10938c2ecf20Sopenharmony_ci		/* size in register is DWs, convert to bytes */
10948c2ecf20Sopenharmony_ci		track->vgt_strmout_size[tmp] = radeon_get_ib_value(p, idx) * 4;
10958c2ecf20Sopenharmony_ci		track->streamout_dirty = true;
10968c2ecf20Sopenharmony_ci		break;
10978c2ecf20Sopenharmony_ci	case CP_COHER_BASE:
10988c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
10998c2ecf20Sopenharmony_ci		if (r) {
11008c2ecf20Sopenharmony_ci			dev_warn(p->dev, "missing reloc for CP_COHER_BASE "
11018c2ecf20Sopenharmony_ci					"0x%04X\n", reg);
11028c2ecf20Sopenharmony_ci			return -EINVAL;
11038c2ecf20Sopenharmony_ci		}
11048c2ecf20Sopenharmony_ci		ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
11058c2ecf20Sopenharmony_ci		break;
11068c2ecf20Sopenharmony_ci	case R_028238_CB_TARGET_MASK:
11078c2ecf20Sopenharmony_ci		track->cb_target_mask = radeon_get_ib_value(p, idx);
11088c2ecf20Sopenharmony_ci		track->cb_dirty = true;
11098c2ecf20Sopenharmony_ci		break;
11108c2ecf20Sopenharmony_ci	case R_02823C_CB_SHADER_MASK:
11118c2ecf20Sopenharmony_ci		track->cb_shader_mask = radeon_get_ib_value(p, idx);
11128c2ecf20Sopenharmony_ci		break;
11138c2ecf20Sopenharmony_ci	case R_028C04_PA_SC_AA_CONFIG:
11148c2ecf20Sopenharmony_ci		tmp = G_028C04_MSAA_NUM_SAMPLES(radeon_get_ib_value(p, idx));
11158c2ecf20Sopenharmony_ci		track->log_nsamples = tmp;
11168c2ecf20Sopenharmony_ci		track->nsamples = 1 << tmp;
11178c2ecf20Sopenharmony_ci		track->cb_dirty = true;
11188c2ecf20Sopenharmony_ci		break;
11198c2ecf20Sopenharmony_ci	case R_028808_CB_COLOR_CONTROL:
11208c2ecf20Sopenharmony_ci		tmp = G_028808_SPECIAL_OP(radeon_get_ib_value(p, idx));
11218c2ecf20Sopenharmony_ci		track->is_resolve = tmp == V_028808_SPECIAL_RESOLVE_BOX;
11228c2ecf20Sopenharmony_ci		track->cb_dirty = true;
11238c2ecf20Sopenharmony_ci		break;
11248c2ecf20Sopenharmony_ci	case R_0280A0_CB_COLOR0_INFO:
11258c2ecf20Sopenharmony_ci	case R_0280A4_CB_COLOR1_INFO:
11268c2ecf20Sopenharmony_ci	case R_0280A8_CB_COLOR2_INFO:
11278c2ecf20Sopenharmony_ci	case R_0280AC_CB_COLOR3_INFO:
11288c2ecf20Sopenharmony_ci	case R_0280B0_CB_COLOR4_INFO:
11298c2ecf20Sopenharmony_ci	case R_0280B4_CB_COLOR5_INFO:
11308c2ecf20Sopenharmony_ci	case R_0280B8_CB_COLOR6_INFO:
11318c2ecf20Sopenharmony_ci	case R_0280BC_CB_COLOR7_INFO:
11328c2ecf20Sopenharmony_ci		if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS) &&
11338c2ecf20Sopenharmony_ci		     radeon_cs_packet_next_is_pkt3_nop(p)) {
11348c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
11358c2ecf20Sopenharmony_ci			if (r) {
11368c2ecf20Sopenharmony_ci				dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
11378c2ecf20Sopenharmony_ci				return -EINVAL;
11388c2ecf20Sopenharmony_ci			}
11398c2ecf20Sopenharmony_ci			tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
11408c2ecf20Sopenharmony_ci			track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
11418c2ecf20Sopenharmony_ci			if (reloc->tiling_flags & RADEON_TILING_MACRO) {
11428c2ecf20Sopenharmony_ci				ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
11438c2ecf20Sopenharmony_ci				track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
11448c2ecf20Sopenharmony_ci			} else if (reloc->tiling_flags & RADEON_TILING_MICRO) {
11458c2ecf20Sopenharmony_ci				ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
11468c2ecf20Sopenharmony_ci				track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
11478c2ecf20Sopenharmony_ci			}
11488c2ecf20Sopenharmony_ci		} else {
11498c2ecf20Sopenharmony_ci			tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
11508c2ecf20Sopenharmony_ci			track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
11518c2ecf20Sopenharmony_ci		}
11528c2ecf20Sopenharmony_ci		track->cb_dirty = true;
11538c2ecf20Sopenharmony_ci		break;
11548c2ecf20Sopenharmony_ci	case R_028080_CB_COLOR0_VIEW:
11558c2ecf20Sopenharmony_ci	case R_028084_CB_COLOR1_VIEW:
11568c2ecf20Sopenharmony_ci	case R_028088_CB_COLOR2_VIEW:
11578c2ecf20Sopenharmony_ci	case R_02808C_CB_COLOR3_VIEW:
11588c2ecf20Sopenharmony_ci	case R_028090_CB_COLOR4_VIEW:
11598c2ecf20Sopenharmony_ci	case R_028094_CB_COLOR5_VIEW:
11608c2ecf20Sopenharmony_ci	case R_028098_CB_COLOR6_VIEW:
11618c2ecf20Sopenharmony_ci	case R_02809C_CB_COLOR7_VIEW:
11628c2ecf20Sopenharmony_ci		tmp = (reg - R_028080_CB_COLOR0_VIEW) / 4;
11638c2ecf20Sopenharmony_ci		track->cb_color_view[tmp] = radeon_get_ib_value(p, idx);
11648c2ecf20Sopenharmony_ci		track->cb_dirty = true;
11658c2ecf20Sopenharmony_ci		break;
11668c2ecf20Sopenharmony_ci	case R_028060_CB_COLOR0_SIZE:
11678c2ecf20Sopenharmony_ci	case R_028064_CB_COLOR1_SIZE:
11688c2ecf20Sopenharmony_ci	case R_028068_CB_COLOR2_SIZE:
11698c2ecf20Sopenharmony_ci	case R_02806C_CB_COLOR3_SIZE:
11708c2ecf20Sopenharmony_ci	case R_028070_CB_COLOR4_SIZE:
11718c2ecf20Sopenharmony_ci	case R_028074_CB_COLOR5_SIZE:
11728c2ecf20Sopenharmony_ci	case R_028078_CB_COLOR6_SIZE:
11738c2ecf20Sopenharmony_ci	case R_02807C_CB_COLOR7_SIZE:
11748c2ecf20Sopenharmony_ci		tmp = (reg - R_028060_CB_COLOR0_SIZE) / 4;
11758c2ecf20Sopenharmony_ci		track->cb_color_size[tmp] = radeon_get_ib_value(p, idx);
11768c2ecf20Sopenharmony_ci		track->cb_color_size_idx[tmp] = idx;
11778c2ecf20Sopenharmony_ci		track->cb_dirty = true;
11788c2ecf20Sopenharmony_ci		break;
11798c2ecf20Sopenharmony_ci		/* This register were added late, there is userspace
11808c2ecf20Sopenharmony_ci		 * which does provide relocation for those but set
11818c2ecf20Sopenharmony_ci		 * 0 offset. In order to avoid breaking old userspace
11828c2ecf20Sopenharmony_ci		 * we detect this and set address to point to last
11838c2ecf20Sopenharmony_ci		 * CB_COLOR0_BASE, note that if userspace doesn't set
11848c2ecf20Sopenharmony_ci		 * CB_COLOR0_BASE before this register we will report
11858c2ecf20Sopenharmony_ci		 * error. Old userspace always set CB_COLOR0_BASE
11868c2ecf20Sopenharmony_ci		 * before any of this.
11878c2ecf20Sopenharmony_ci		 */
11888c2ecf20Sopenharmony_ci	case R_0280E0_CB_COLOR0_FRAG:
11898c2ecf20Sopenharmony_ci	case R_0280E4_CB_COLOR1_FRAG:
11908c2ecf20Sopenharmony_ci	case R_0280E8_CB_COLOR2_FRAG:
11918c2ecf20Sopenharmony_ci	case R_0280EC_CB_COLOR3_FRAG:
11928c2ecf20Sopenharmony_ci	case R_0280F0_CB_COLOR4_FRAG:
11938c2ecf20Sopenharmony_ci	case R_0280F4_CB_COLOR5_FRAG:
11948c2ecf20Sopenharmony_ci	case R_0280F8_CB_COLOR6_FRAG:
11958c2ecf20Sopenharmony_ci	case R_0280FC_CB_COLOR7_FRAG:
11968c2ecf20Sopenharmony_ci		tmp = (reg - R_0280E0_CB_COLOR0_FRAG) / 4;
11978c2ecf20Sopenharmony_ci		if (!radeon_cs_packet_next_is_pkt3_nop(p)) {
11988c2ecf20Sopenharmony_ci			if (!track->cb_color_base_last[tmp]) {
11998c2ecf20Sopenharmony_ci				dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
12008c2ecf20Sopenharmony_ci				return -EINVAL;
12018c2ecf20Sopenharmony_ci			}
12028c2ecf20Sopenharmony_ci			track->cb_color_frag_bo[tmp] = track->cb_color_bo[tmp];
12038c2ecf20Sopenharmony_ci			track->cb_color_frag_offset[tmp] = track->cb_color_bo_offset[tmp];
12048c2ecf20Sopenharmony_ci			ib[idx] = track->cb_color_base_last[tmp];
12058c2ecf20Sopenharmony_ci		} else {
12068c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
12078c2ecf20Sopenharmony_ci			if (r) {
12088c2ecf20Sopenharmony_ci				dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
12098c2ecf20Sopenharmony_ci				return -EINVAL;
12108c2ecf20Sopenharmony_ci			}
12118c2ecf20Sopenharmony_ci			track->cb_color_frag_bo[tmp] = reloc->robj;
12128c2ecf20Sopenharmony_ci			track->cb_color_frag_offset[tmp] = (u64)ib[idx] << 8;
12138c2ecf20Sopenharmony_ci			ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
12148c2ecf20Sopenharmony_ci		}
12158c2ecf20Sopenharmony_ci		if (G_0280A0_TILE_MODE(track->cb_color_info[tmp])) {
12168c2ecf20Sopenharmony_ci			track->cb_dirty = true;
12178c2ecf20Sopenharmony_ci		}
12188c2ecf20Sopenharmony_ci		break;
12198c2ecf20Sopenharmony_ci	case R_0280C0_CB_COLOR0_TILE:
12208c2ecf20Sopenharmony_ci	case R_0280C4_CB_COLOR1_TILE:
12218c2ecf20Sopenharmony_ci	case R_0280C8_CB_COLOR2_TILE:
12228c2ecf20Sopenharmony_ci	case R_0280CC_CB_COLOR3_TILE:
12238c2ecf20Sopenharmony_ci	case R_0280D0_CB_COLOR4_TILE:
12248c2ecf20Sopenharmony_ci	case R_0280D4_CB_COLOR5_TILE:
12258c2ecf20Sopenharmony_ci	case R_0280D8_CB_COLOR6_TILE:
12268c2ecf20Sopenharmony_ci	case R_0280DC_CB_COLOR7_TILE:
12278c2ecf20Sopenharmony_ci		tmp = (reg - R_0280C0_CB_COLOR0_TILE) / 4;
12288c2ecf20Sopenharmony_ci		if (!radeon_cs_packet_next_is_pkt3_nop(p)) {
12298c2ecf20Sopenharmony_ci			if (!track->cb_color_base_last[tmp]) {
12308c2ecf20Sopenharmony_ci				dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
12318c2ecf20Sopenharmony_ci				return -EINVAL;
12328c2ecf20Sopenharmony_ci			}
12338c2ecf20Sopenharmony_ci			track->cb_color_tile_bo[tmp] = track->cb_color_bo[tmp];
12348c2ecf20Sopenharmony_ci			track->cb_color_tile_offset[tmp] = track->cb_color_bo_offset[tmp];
12358c2ecf20Sopenharmony_ci			ib[idx] = track->cb_color_base_last[tmp];
12368c2ecf20Sopenharmony_ci		} else {
12378c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
12388c2ecf20Sopenharmony_ci			if (r) {
12398c2ecf20Sopenharmony_ci				dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
12408c2ecf20Sopenharmony_ci				return -EINVAL;
12418c2ecf20Sopenharmony_ci			}
12428c2ecf20Sopenharmony_ci			track->cb_color_tile_bo[tmp] = reloc->robj;
12438c2ecf20Sopenharmony_ci			track->cb_color_tile_offset[tmp] = (u64)ib[idx] << 8;
12448c2ecf20Sopenharmony_ci			ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
12458c2ecf20Sopenharmony_ci		}
12468c2ecf20Sopenharmony_ci		if (G_0280A0_TILE_MODE(track->cb_color_info[tmp])) {
12478c2ecf20Sopenharmony_ci			track->cb_dirty = true;
12488c2ecf20Sopenharmony_ci		}
12498c2ecf20Sopenharmony_ci		break;
12508c2ecf20Sopenharmony_ci	case R_028100_CB_COLOR0_MASK:
12518c2ecf20Sopenharmony_ci	case R_028104_CB_COLOR1_MASK:
12528c2ecf20Sopenharmony_ci	case R_028108_CB_COLOR2_MASK:
12538c2ecf20Sopenharmony_ci	case R_02810C_CB_COLOR3_MASK:
12548c2ecf20Sopenharmony_ci	case R_028110_CB_COLOR4_MASK:
12558c2ecf20Sopenharmony_ci	case R_028114_CB_COLOR5_MASK:
12568c2ecf20Sopenharmony_ci	case R_028118_CB_COLOR6_MASK:
12578c2ecf20Sopenharmony_ci	case R_02811C_CB_COLOR7_MASK:
12588c2ecf20Sopenharmony_ci		tmp = (reg - R_028100_CB_COLOR0_MASK) / 4;
12598c2ecf20Sopenharmony_ci		track->cb_color_mask[tmp] = radeon_get_ib_value(p, idx);
12608c2ecf20Sopenharmony_ci		if (G_0280A0_TILE_MODE(track->cb_color_info[tmp])) {
12618c2ecf20Sopenharmony_ci			track->cb_dirty = true;
12628c2ecf20Sopenharmony_ci		}
12638c2ecf20Sopenharmony_ci		break;
12648c2ecf20Sopenharmony_ci	case CB_COLOR0_BASE:
12658c2ecf20Sopenharmony_ci	case CB_COLOR1_BASE:
12668c2ecf20Sopenharmony_ci	case CB_COLOR2_BASE:
12678c2ecf20Sopenharmony_ci	case CB_COLOR3_BASE:
12688c2ecf20Sopenharmony_ci	case CB_COLOR4_BASE:
12698c2ecf20Sopenharmony_ci	case CB_COLOR5_BASE:
12708c2ecf20Sopenharmony_ci	case CB_COLOR6_BASE:
12718c2ecf20Sopenharmony_ci	case CB_COLOR7_BASE:
12728c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
12738c2ecf20Sopenharmony_ci		if (r) {
12748c2ecf20Sopenharmony_ci			dev_warn(p->dev, "bad SET_CONTEXT_REG "
12758c2ecf20Sopenharmony_ci					"0x%04X\n", reg);
12768c2ecf20Sopenharmony_ci			return -EINVAL;
12778c2ecf20Sopenharmony_ci		}
12788c2ecf20Sopenharmony_ci		tmp = (reg - CB_COLOR0_BASE) / 4;
12798c2ecf20Sopenharmony_ci		track->cb_color_bo_offset[tmp] = (u64)radeon_get_ib_value(p, idx) << 8;
12808c2ecf20Sopenharmony_ci		ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
12818c2ecf20Sopenharmony_ci		track->cb_color_base_last[tmp] = ib[idx];
12828c2ecf20Sopenharmony_ci		track->cb_color_bo[tmp] = reloc->robj;
12838c2ecf20Sopenharmony_ci		track->cb_color_bo_mc[tmp] = reloc->gpu_offset;
12848c2ecf20Sopenharmony_ci		track->cb_dirty = true;
12858c2ecf20Sopenharmony_ci		break;
12868c2ecf20Sopenharmony_ci	case DB_DEPTH_BASE:
12878c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
12888c2ecf20Sopenharmony_ci		if (r) {
12898c2ecf20Sopenharmony_ci			dev_warn(p->dev, "bad SET_CONTEXT_REG "
12908c2ecf20Sopenharmony_ci					"0x%04X\n", reg);
12918c2ecf20Sopenharmony_ci			return -EINVAL;
12928c2ecf20Sopenharmony_ci		}
12938c2ecf20Sopenharmony_ci		track->db_offset = radeon_get_ib_value(p, idx) << 8;
12948c2ecf20Sopenharmony_ci		ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
12958c2ecf20Sopenharmony_ci		track->db_bo = reloc->robj;
12968c2ecf20Sopenharmony_ci		track->db_bo_mc = reloc->gpu_offset;
12978c2ecf20Sopenharmony_ci		track->db_dirty = true;
12988c2ecf20Sopenharmony_ci		break;
12998c2ecf20Sopenharmony_ci	case DB_HTILE_DATA_BASE:
13008c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
13018c2ecf20Sopenharmony_ci		if (r) {
13028c2ecf20Sopenharmony_ci			dev_warn(p->dev, "bad SET_CONTEXT_REG "
13038c2ecf20Sopenharmony_ci					"0x%04X\n", reg);
13048c2ecf20Sopenharmony_ci			return -EINVAL;
13058c2ecf20Sopenharmony_ci		}
13068c2ecf20Sopenharmony_ci		track->htile_offset = (u64)radeon_get_ib_value(p, idx) << 8;
13078c2ecf20Sopenharmony_ci		ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
13088c2ecf20Sopenharmony_ci		track->htile_bo = reloc->robj;
13098c2ecf20Sopenharmony_ci		track->db_dirty = true;
13108c2ecf20Sopenharmony_ci		break;
13118c2ecf20Sopenharmony_ci	case DB_HTILE_SURFACE:
13128c2ecf20Sopenharmony_ci		track->htile_surface = radeon_get_ib_value(p, idx);
13138c2ecf20Sopenharmony_ci		/* force 8x8 htile width and height */
13148c2ecf20Sopenharmony_ci		ib[idx] |= 3;
13158c2ecf20Sopenharmony_ci		track->db_dirty = true;
13168c2ecf20Sopenharmony_ci		break;
13178c2ecf20Sopenharmony_ci	case SQ_PGM_START_FS:
13188c2ecf20Sopenharmony_ci	case SQ_PGM_START_ES:
13198c2ecf20Sopenharmony_ci	case SQ_PGM_START_VS:
13208c2ecf20Sopenharmony_ci	case SQ_PGM_START_GS:
13218c2ecf20Sopenharmony_ci	case SQ_PGM_START_PS:
13228c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_0:
13238c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_1:
13248c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_2:
13258c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_3:
13268c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_4:
13278c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_5:
13288c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_6:
13298c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_7:
13308c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_8:
13318c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_9:
13328c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_10:
13338c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_11:
13348c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_12:
13358c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_13:
13368c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_14:
13378c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_GS_15:
13388c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_0:
13398c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_1:
13408c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_2:
13418c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_3:
13428c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_4:
13438c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_5:
13448c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_6:
13458c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_7:
13468c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_8:
13478c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_9:
13488c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_10:
13498c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_11:
13508c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_12:
13518c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_13:
13528c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_14:
13538c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_PS_15:
13548c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_0:
13558c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_1:
13568c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_2:
13578c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_3:
13588c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_4:
13598c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_5:
13608c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_6:
13618c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_7:
13628c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_8:
13638c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_9:
13648c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_10:
13658c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_11:
13668c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_12:
13678c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_13:
13688c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_14:
13698c2ecf20Sopenharmony_ci	case SQ_ALU_CONST_CACHE_VS_15:
13708c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
13718c2ecf20Sopenharmony_ci		if (r) {
13728c2ecf20Sopenharmony_ci			dev_warn(p->dev, "bad SET_CONTEXT_REG "
13738c2ecf20Sopenharmony_ci					"0x%04X\n", reg);
13748c2ecf20Sopenharmony_ci			return -EINVAL;
13758c2ecf20Sopenharmony_ci		}
13768c2ecf20Sopenharmony_ci		ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
13778c2ecf20Sopenharmony_ci		break;
13788c2ecf20Sopenharmony_ci	case SX_MEMORY_EXPORT_BASE:
13798c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
13808c2ecf20Sopenharmony_ci		if (r) {
13818c2ecf20Sopenharmony_ci			dev_warn(p->dev, "bad SET_CONFIG_REG "
13828c2ecf20Sopenharmony_ci					"0x%04X\n", reg);
13838c2ecf20Sopenharmony_ci			return -EINVAL;
13848c2ecf20Sopenharmony_ci		}
13858c2ecf20Sopenharmony_ci		ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
13868c2ecf20Sopenharmony_ci		break;
13878c2ecf20Sopenharmony_ci	case SX_MISC:
13888c2ecf20Sopenharmony_ci		track->sx_misc_kill_all_prims = (radeon_get_ib_value(p, idx) & 0x1) != 0;
13898c2ecf20Sopenharmony_ci		break;
13908c2ecf20Sopenharmony_ci	default:
13918c2ecf20Sopenharmony_ci		dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
13928c2ecf20Sopenharmony_ci		return -EINVAL;
13938c2ecf20Sopenharmony_ci	}
13948c2ecf20Sopenharmony_ci	return 0;
13958c2ecf20Sopenharmony_ci}
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ciunsigned r600_mip_minify(unsigned size, unsigned level)
13988c2ecf20Sopenharmony_ci{
13998c2ecf20Sopenharmony_ci	unsigned val;
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	val = max(1U, size >> level);
14028c2ecf20Sopenharmony_ci	if (level > 0)
14038c2ecf20Sopenharmony_ci		val = roundup_pow_of_two(val);
14048c2ecf20Sopenharmony_ci	return val;
14058c2ecf20Sopenharmony_ci}
14068c2ecf20Sopenharmony_ci
14078c2ecf20Sopenharmony_cistatic void r600_texture_size(unsigned nfaces, unsigned blevel, unsigned llevel,
14088c2ecf20Sopenharmony_ci			      unsigned w0, unsigned h0, unsigned d0, unsigned nsamples, unsigned format,
14098c2ecf20Sopenharmony_ci			      unsigned block_align, unsigned height_align, unsigned base_align,
14108c2ecf20Sopenharmony_ci			      unsigned *l0_size, unsigned *mipmap_size)
14118c2ecf20Sopenharmony_ci{
14128c2ecf20Sopenharmony_ci	unsigned offset, i, level;
14138c2ecf20Sopenharmony_ci	unsigned width, height, depth, size;
14148c2ecf20Sopenharmony_ci	unsigned blocksize;
14158c2ecf20Sopenharmony_ci	unsigned nbx, nby;
14168c2ecf20Sopenharmony_ci	unsigned nlevels = llevel - blevel + 1;
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci	*l0_size = -1;
14198c2ecf20Sopenharmony_ci	blocksize = r600_fmt_get_blocksize(format);
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci	w0 = r600_mip_minify(w0, 0);
14228c2ecf20Sopenharmony_ci	h0 = r600_mip_minify(h0, 0);
14238c2ecf20Sopenharmony_ci	d0 = r600_mip_minify(d0, 0);
14248c2ecf20Sopenharmony_ci	for(i = 0, offset = 0, level = blevel; i < nlevels; i++, level++) {
14258c2ecf20Sopenharmony_ci		width = r600_mip_minify(w0, i);
14268c2ecf20Sopenharmony_ci		nbx = r600_fmt_get_nblocksx(format, width);
14278c2ecf20Sopenharmony_ci
14288c2ecf20Sopenharmony_ci		nbx = round_up(nbx, block_align);
14298c2ecf20Sopenharmony_ci
14308c2ecf20Sopenharmony_ci		height = r600_mip_minify(h0, i);
14318c2ecf20Sopenharmony_ci		nby = r600_fmt_get_nblocksy(format, height);
14328c2ecf20Sopenharmony_ci		nby = round_up(nby, height_align);
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci		depth = r600_mip_minify(d0, i);
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci		size = nbx * nby * blocksize * nsamples;
14378c2ecf20Sopenharmony_ci		if (nfaces)
14388c2ecf20Sopenharmony_ci			size *= nfaces;
14398c2ecf20Sopenharmony_ci		else
14408c2ecf20Sopenharmony_ci			size *= depth;
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci		if (i == 0)
14438c2ecf20Sopenharmony_ci			*l0_size = size;
14448c2ecf20Sopenharmony_ci
14458c2ecf20Sopenharmony_ci		if (i == 0 || i == 1)
14468c2ecf20Sopenharmony_ci			offset = round_up(offset, base_align);
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci		offset += size;
14498c2ecf20Sopenharmony_ci	}
14508c2ecf20Sopenharmony_ci	*mipmap_size = offset;
14518c2ecf20Sopenharmony_ci	if (llevel == 0)
14528c2ecf20Sopenharmony_ci		*mipmap_size = *l0_size;
14538c2ecf20Sopenharmony_ci	if (!blevel)
14548c2ecf20Sopenharmony_ci		*mipmap_size -= *l0_size;
14558c2ecf20Sopenharmony_ci}
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_ci/**
14588c2ecf20Sopenharmony_ci * r600_check_texture_resource() - check if register is authorized or not
14598c2ecf20Sopenharmony_ci * @p: parser structure holding parsing context
14608c2ecf20Sopenharmony_ci * @idx: index into the cs buffer
14618c2ecf20Sopenharmony_ci * @texture: texture's bo structure
14628c2ecf20Sopenharmony_ci * @mipmap: mipmap's bo structure
14638c2ecf20Sopenharmony_ci *
14648c2ecf20Sopenharmony_ci * This function will check that the resource has valid field and that
14658c2ecf20Sopenharmony_ci * the texture and mipmap bo object are big enough to cover this resource.
14668c2ecf20Sopenharmony_ci */
14678c2ecf20Sopenharmony_cistatic int r600_check_texture_resource(struct radeon_cs_parser *p,  u32 idx,
14688c2ecf20Sopenharmony_ci					      struct radeon_bo *texture,
14698c2ecf20Sopenharmony_ci					      struct radeon_bo *mipmap,
14708c2ecf20Sopenharmony_ci					      u64 base_offset,
14718c2ecf20Sopenharmony_ci					      u64 mip_offset,
14728c2ecf20Sopenharmony_ci					      u32 tiling_flags)
14738c2ecf20Sopenharmony_ci{
14748c2ecf20Sopenharmony_ci	struct r600_cs_track *track = p->track;
14758c2ecf20Sopenharmony_ci	u32 dim, nfaces, llevel, blevel, w0, h0, d0;
14768c2ecf20Sopenharmony_ci	u32 word0, word1, l0_size, mipmap_size, word2, word3, word4, word5;
14778c2ecf20Sopenharmony_ci	u32 height_align, pitch, pitch_align, depth_align;
14788c2ecf20Sopenharmony_ci	u32 barray, larray;
14798c2ecf20Sopenharmony_ci	u64 base_align;
14808c2ecf20Sopenharmony_ci	struct array_mode_checker array_check;
14818c2ecf20Sopenharmony_ci	u32 format;
14828c2ecf20Sopenharmony_ci	bool is_array;
14838c2ecf20Sopenharmony_ci
14848c2ecf20Sopenharmony_ci	/* on legacy kernel we don't perform advanced check */
14858c2ecf20Sopenharmony_ci	if (p->rdev == NULL)
14868c2ecf20Sopenharmony_ci		return 0;
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci	/* convert to bytes */
14898c2ecf20Sopenharmony_ci	base_offset <<= 8;
14908c2ecf20Sopenharmony_ci	mip_offset <<= 8;
14918c2ecf20Sopenharmony_ci
14928c2ecf20Sopenharmony_ci	word0 = radeon_get_ib_value(p, idx + 0);
14938c2ecf20Sopenharmony_ci	if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
14948c2ecf20Sopenharmony_ci		if (tiling_flags & RADEON_TILING_MACRO)
14958c2ecf20Sopenharmony_ci			word0 |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
14968c2ecf20Sopenharmony_ci		else if (tiling_flags & RADEON_TILING_MICRO)
14978c2ecf20Sopenharmony_ci			word0 |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
14988c2ecf20Sopenharmony_ci	}
14998c2ecf20Sopenharmony_ci	word1 = radeon_get_ib_value(p, idx + 1);
15008c2ecf20Sopenharmony_ci	word2 = radeon_get_ib_value(p, idx + 2) << 8;
15018c2ecf20Sopenharmony_ci	word3 = radeon_get_ib_value(p, idx + 3) << 8;
15028c2ecf20Sopenharmony_ci	word4 = radeon_get_ib_value(p, idx + 4);
15038c2ecf20Sopenharmony_ci	word5 = radeon_get_ib_value(p, idx + 5);
15048c2ecf20Sopenharmony_ci	dim = G_038000_DIM(word0);
15058c2ecf20Sopenharmony_ci	w0 = G_038000_TEX_WIDTH(word0) + 1;
15068c2ecf20Sopenharmony_ci	pitch = (G_038000_PITCH(word0) + 1) * 8;
15078c2ecf20Sopenharmony_ci	h0 = G_038004_TEX_HEIGHT(word1) + 1;
15088c2ecf20Sopenharmony_ci	d0 = G_038004_TEX_DEPTH(word1);
15098c2ecf20Sopenharmony_ci	format = G_038004_DATA_FORMAT(word1);
15108c2ecf20Sopenharmony_ci	blevel = G_038010_BASE_LEVEL(word4);
15118c2ecf20Sopenharmony_ci	llevel = G_038014_LAST_LEVEL(word5);
15128c2ecf20Sopenharmony_ci	/* pitch in texels */
15138c2ecf20Sopenharmony_ci	array_check.array_mode = G_038000_TILE_MODE(word0);
15148c2ecf20Sopenharmony_ci	array_check.group_size = track->group_size;
15158c2ecf20Sopenharmony_ci	array_check.nbanks = track->nbanks;
15168c2ecf20Sopenharmony_ci	array_check.npipes = track->npipes;
15178c2ecf20Sopenharmony_ci	array_check.nsamples = 1;
15188c2ecf20Sopenharmony_ci	array_check.blocksize = r600_fmt_get_blocksize(format);
15198c2ecf20Sopenharmony_ci	nfaces = 1;
15208c2ecf20Sopenharmony_ci	is_array = false;
15218c2ecf20Sopenharmony_ci	switch (dim) {
15228c2ecf20Sopenharmony_ci	case V_038000_SQ_TEX_DIM_1D:
15238c2ecf20Sopenharmony_ci	case V_038000_SQ_TEX_DIM_2D:
15248c2ecf20Sopenharmony_ci	case V_038000_SQ_TEX_DIM_3D:
15258c2ecf20Sopenharmony_ci		break;
15268c2ecf20Sopenharmony_ci	case V_038000_SQ_TEX_DIM_CUBEMAP:
15278c2ecf20Sopenharmony_ci		if (p->family >= CHIP_RV770)
15288c2ecf20Sopenharmony_ci			nfaces = 8;
15298c2ecf20Sopenharmony_ci		else
15308c2ecf20Sopenharmony_ci			nfaces = 6;
15318c2ecf20Sopenharmony_ci		break;
15328c2ecf20Sopenharmony_ci	case V_038000_SQ_TEX_DIM_1D_ARRAY:
15338c2ecf20Sopenharmony_ci	case V_038000_SQ_TEX_DIM_2D_ARRAY:
15348c2ecf20Sopenharmony_ci		is_array = true;
15358c2ecf20Sopenharmony_ci		break;
15368c2ecf20Sopenharmony_ci	case V_038000_SQ_TEX_DIM_2D_ARRAY_MSAA:
15378c2ecf20Sopenharmony_ci		is_array = true;
15388c2ecf20Sopenharmony_ci		fallthrough;
15398c2ecf20Sopenharmony_ci	case V_038000_SQ_TEX_DIM_2D_MSAA:
15408c2ecf20Sopenharmony_ci		array_check.nsamples = 1 << llevel;
15418c2ecf20Sopenharmony_ci		llevel = 0;
15428c2ecf20Sopenharmony_ci		break;
15438c2ecf20Sopenharmony_ci	default:
15448c2ecf20Sopenharmony_ci		dev_warn(p->dev, "this kernel doesn't support %d texture dim\n", G_038000_DIM(word0));
15458c2ecf20Sopenharmony_ci		return -EINVAL;
15468c2ecf20Sopenharmony_ci	}
15478c2ecf20Sopenharmony_ci	if (!r600_fmt_is_valid_texture(format, p->family)) {
15488c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s:%d texture invalid format %d\n",
15498c2ecf20Sopenharmony_ci			 __func__, __LINE__, format);
15508c2ecf20Sopenharmony_ci		return -EINVAL;
15518c2ecf20Sopenharmony_ci	}
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci	if (r600_get_array_mode_alignment(&array_check,
15548c2ecf20Sopenharmony_ci					  &pitch_align, &height_align, &depth_align, &base_align)) {
15558c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s:%d tex array mode (%d) invalid\n",
15568c2ecf20Sopenharmony_ci			 __func__, __LINE__, G_038000_TILE_MODE(word0));
15578c2ecf20Sopenharmony_ci		return -EINVAL;
15588c2ecf20Sopenharmony_ci	}
15598c2ecf20Sopenharmony_ci
15608c2ecf20Sopenharmony_ci	/* XXX check height as well... */
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_ci	if (!IS_ALIGNED(pitch, pitch_align)) {
15638c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s:%d tex pitch (%d, 0x%x, %d) invalid\n",
15648c2ecf20Sopenharmony_ci			 __func__, __LINE__, pitch, pitch_align, G_038000_TILE_MODE(word0));
15658c2ecf20Sopenharmony_ci		return -EINVAL;
15668c2ecf20Sopenharmony_ci	}
15678c2ecf20Sopenharmony_ci	if (!IS_ALIGNED(base_offset, base_align)) {
15688c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s:%d tex base offset (0x%llx, 0x%llx, %d) invalid\n",
15698c2ecf20Sopenharmony_ci			 __func__, __LINE__, base_offset, base_align, G_038000_TILE_MODE(word0));
15708c2ecf20Sopenharmony_ci		return -EINVAL;
15718c2ecf20Sopenharmony_ci	}
15728c2ecf20Sopenharmony_ci	if (!IS_ALIGNED(mip_offset, base_align)) {
15738c2ecf20Sopenharmony_ci		dev_warn(p->dev, "%s:%d tex mip offset (0x%llx, 0x%llx, %d) invalid\n",
15748c2ecf20Sopenharmony_ci			 __func__, __LINE__, mip_offset, base_align, G_038000_TILE_MODE(word0));
15758c2ecf20Sopenharmony_ci		return -EINVAL;
15768c2ecf20Sopenharmony_ci	}
15778c2ecf20Sopenharmony_ci
15788c2ecf20Sopenharmony_ci	if (blevel > llevel) {
15798c2ecf20Sopenharmony_ci		dev_warn(p->dev, "texture blevel %d > llevel %d\n",
15808c2ecf20Sopenharmony_ci			 blevel, llevel);
15818c2ecf20Sopenharmony_ci	}
15828c2ecf20Sopenharmony_ci	if (is_array) {
15838c2ecf20Sopenharmony_ci		barray = G_038014_BASE_ARRAY(word5);
15848c2ecf20Sopenharmony_ci		larray = G_038014_LAST_ARRAY(word5);
15858c2ecf20Sopenharmony_ci
15868c2ecf20Sopenharmony_ci		nfaces = larray - barray + 1;
15878c2ecf20Sopenharmony_ci	}
15888c2ecf20Sopenharmony_ci	r600_texture_size(nfaces, blevel, llevel, w0, h0, d0, array_check.nsamples, format,
15898c2ecf20Sopenharmony_ci			  pitch_align, height_align, base_align,
15908c2ecf20Sopenharmony_ci			  &l0_size, &mipmap_size);
15918c2ecf20Sopenharmony_ci	/* using get ib will give us the offset into the texture bo */
15928c2ecf20Sopenharmony_ci	if ((l0_size + word2) > radeon_bo_size(texture)) {
15938c2ecf20Sopenharmony_ci		dev_warn(p->dev, "texture bo too small ((%d %d) (%d %d) %d %d %d -> %d have %ld)\n",
15948c2ecf20Sopenharmony_ci			 w0, h0, pitch_align, height_align,
15958c2ecf20Sopenharmony_ci			 array_check.array_mode, format, word2,
15968c2ecf20Sopenharmony_ci			 l0_size, radeon_bo_size(texture));
15978c2ecf20Sopenharmony_ci		dev_warn(p->dev, "alignments %d %d %d %lld\n", pitch, pitch_align, height_align, base_align);
15988c2ecf20Sopenharmony_ci		return -EINVAL;
15998c2ecf20Sopenharmony_ci	}
16008c2ecf20Sopenharmony_ci	/* using get ib will give us the offset into the mipmap bo */
16018c2ecf20Sopenharmony_ci	if ((mipmap_size + word3) > radeon_bo_size(mipmap)) {
16028c2ecf20Sopenharmony_ci		/*dev_warn(p->dev, "mipmap bo too small (%d %d %d %d %d %d -> %d have %ld)\n",
16038c2ecf20Sopenharmony_ci		  w0, h0, format, blevel, nlevels, word3, mipmap_size, radeon_bo_size(texture));*/
16048c2ecf20Sopenharmony_ci	}
16058c2ecf20Sopenharmony_ci	return 0;
16068c2ecf20Sopenharmony_ci}
16078c2ecf20Sopenharmony_ci
16088c2ecf20Sopenharmony_cistatic bool r600_is_safe_reg(struct radeon_cs_parser *p, u32 reg, u32 idx)
16098c2ecf20Sopenharmony_ci{
16108c2ecf20Sopenharmony_ci	u32 m, i;
16118c2ecf20Sopenharmony_ci
16128c2ecf20Sopenharmony_ci	i = (reg >> 7);
16138c2ecf20Sopenharmony_ci	if (i >= ARRAY_SIZE(r600_reg_safe_bm)) {
16148c2ecf20Sopenharmony_ci		dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
16158c2ecf20Sopenharmony_ci		return false;
16168c2ecf20Sopenharmony_ci	}
16178c2ecf20Sopenharmony_ci	m = 1 << ((reg >> 2) & 31);
16188c2ecf20Sopenharmony_ci	if (!(r600_reg_safe_bm[i] & m))
16198c2ecf20Sopenharmony_ci		return true;
16208c2ecf20Sopenharmony_ci	dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
16218c2ecf20Sopenharmony_ci	return false;
16228c2ecf20Sopenharmony_ci}
16238c2ecf20Sopenharmony_ci
16248c2ecf20Sopenharmony_cistatic int r600_packet3_check(struct radeon_cs_parser *p,
16258c2ecf20Sopenharmony_ci				struct radeon_cs_packet *pkt)
16268c2ecf20Sopenharmony_ci{
16278c2ecf20Sopenharmony_ci	struct radeon_bo_list *reloc;
16288c2ecf20Sopenharmony_ci	struct r600_cs_track *track;
16298c2ecf20Sopenharmony_ci	volatile u32 *ib;
16308c2ecf20Sopenharmony_ci	unsigned idx;
16318c2ecf20Sopenharmony_ci	unsigned i;
16328c2ecf20Sopenharmony_ci	unsigned start_reg, end_reg, reg;
16338c2ecf20Sopenharmony_ci	int r;
16348c2ecf20Sopenharmony_ci	u32 idx_value;
16358c2ecf20Sopenharmony_ci
16368c2ecf20Sopenharmony_ci	track = (struct r600_cs_track *)p->track;
16378c2ecf20Sopenharmony_ci	ib = p->ib.ptr;
16388c2ecf20Sopenharmony_ci	idx = pkt->idx + 1;
16398c2ecf20Sopenharmony_ci	idx_value = radeon_get_ib_value(p, idx);
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_ci	switch (pkt->opcode) {
16428c2ecf20Sopenharmony_ci	case PACKET3_SET_PREDICATION:
16438c2ecf20Sopenharmony_ci	{
16448c2ecf20Sopenharmony_ci		int pred_op;
16458c2ecf20Sopenharmony_ci		int tmp;
16468c2ecf20Sopenharmony_ci		uint64_t offset;
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_ci		if (pkt->count != 1) {
16498c2ecf20Sopenharmony_ci			DRM_ERROR("bad SET PREDICATION\n");
16508c2ecf20Sopenharmony_ci			return -EINVAL;
16518c2ecf20Sopenharmony_ci		}
16528c2ecf20Sopenharmony_ci
16538c2ecf20Sopenharmony_ci		tmp = radeon_get_ib_value(p, idx + 1);
16548c2ecf20Sopenharmony_ci		pred_op = (tmp >> 16) & 0x7;
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_ci		/* for the clear predicate operation */
16578c2ecf20Sopenharmony_ci		if (pred_op == 0)
16588c2ecf20Sopenharmony_ci			return 0;
16598c2ecf20Sopenharmony_ci
16608c2ecf20Sopenharmony_ci		if (pred_op > 2) {
16618c2ecf20Sopenharmony_ci			DRM_ERROR("bad SET PREDICATION operation %d\n", pred_op);
16628c2ecf20Sopenharmony_ci			return -EINVAL;
16638c2ecf20Sopenharmony_ci		}
16648c2ecf20Sopenharmony_ci
16658c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
16668c2ecf20Sopenharmony_ci		if (r) {
16678c2ecf20Sopenharmony_ci			DRM_ERROR("bad SET PREDICATION\n");
16688c2ecf20Sopenharmony_ci			return -EINVAL;
16698c2ecf20Sopenharmony_ci		}
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci		offset = reloc->gpu_offset +
16728c2ecf20Sopenharmony_ci			 (idx_value & 0xfffffff0) +
16738c2ecf20Sopenharmony_ci			 ((u64)(tmp & 0xff) << 32);
16748c2ecf20Sopenharmony_ci
16758c2ecf20Sopenharmony_ci		ib[idx + 0] = offset;
16768c2ecf20Sopenharmony_ci		ib[idx + 1] = (tmp & 0xffffff00) | (upper_32_bits(offset) & 0xff);
16778c2ecf20Sopenharmony_ci	}
16788c2ecf20Sopenharmony_ci	break;
16798c2ecf20Sopenharmony_ci
16808c2ecf20Sopenharmony_ci	case PACKET3_START_3D_CMDBUF:
16818c2ecf20Sopenharmony_ci		if (p->family >= CHIP_RV770 || pkt->count) {
16828c2ecf20Sopenharmony_ci			DRM_ERROR("bad START_3D\n");
16838c2ecf20Sopenharmony_ci			return -EINVAL;
16848c2ecf20Sopenharmony_ci		}
16858c2ecf20Sopenharmony_ci		break;
16868c2ecf20Sopenharmony_ci	case PACKET3_CONTEXT_CONTROL:
16878c2ecf20Sopenharmony_ci		if (pkt->count != 1) {
16888c2ecf20Sopenharmony_ci			DRM_ERROR("bad CONTEXT_CONTROL\n");
16898c2ecf20Sopenharmony_ci			return -EINVAL;
16908c2ecf20Sopenharmony_ci		}
16918c2ecf20Sopenharmony_ci		break;
16928c2ecf20Sopenharmony_ci	case PACKET3_INDEX_TYPE:
16938c2ecf20Sopenharmony_ci	case PACKET3_NUM_INSTANCES:
16948c2ecf20Sopenharmony_ci		if (pkt->count) {
16958c2ecf20Sopenharmony_ci			DRM_ERROR("bad INDEX_TYPE/NUM_INSTANCES\n");
16968c2ecf20Sopenharmony_ci			return -EINVAL;
16978c2ecf20Sopenharmony_ci		}
16988c2ecf20Sopenharmony_ci		break;
16998c2ecf20Sopenharmony_ci	case PACKET3_DRAW_INDEX:
17008c2ecf20Sopenharmony_ci	{
17018c2ecf20Sopenharmony_ci		uint64_t offset;
17028c2ecf20Sopenharmony_ci		if (pkt->count != 3) {
17038c2ecf20Sopenharmony_ci			DRM_ERROR("bad DRAW_INDEX\n");
17048c2ecf20Sopenharmony_ci			return -EINVAL;
17058c2ecf20Sopenharmony_ci		}
17068c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
17078c2ecf20Sopenharmony_ci		if (r) {
17088c2ecf20Sopenharmony_ci			DRM_ERROR("bad DRAW_INDEX\n");
17098c2ecf20Sopenharmony_ci			return -EINVAL;
17108c2ecf20Sopenharmony_ci		}
17118c2ecf20Sopenharmony_ci
17128c2ecf20Sopenharmony_ci		offset = reloc->gpu_offset +
17138c2ecf20Sopenharmony_ci			 idx_value +
17148c2ecf20Sopenharmony_ci			 ((u64)(radeon_get_ib_value(p, idx+1) & 0xff) << 32);
17158c2ecf20Sopenharmony_ci
17168c2ecf20Sopenharmony_ci		ib[idx+0] = offset;
17178c2ecf20Sopenharmony_ci		ib[idx+1] = upper_32_bits(offset) & 0xff;
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_ci		r = r600_cs_track_check(p);
17208c2ecf20Sopenharmony_ci		if (r) {
17218c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
17228c2ecf20Sopenharmony_ci			return r;
17238c2ecf20Sopenharmony_ci		}
17248c2ecf20Sopenharmony_ci		break;
17258c2ecf20Sopenharmony_ci	}
17268c2ecf20Sopenharmony_ci	case PACKET3_DRAW_INDEX_AUTO:
17278c2ecf20Sopenharmony_ci		if (pkt->count != 1) {
17288c2ecf20Sopenharmony_ci			DRM_ERROR("bad DRAW_INDEX_AUTO\n");
17298c2ecf20Sopenharmony_ci			return -EINVAL;
17308c2ecf20Sopenharmony_ci		}
17318c2ecf20Sopenharmony_ci		r = r600_cs_track_check(p);
17328c2ecf20Sopenharmony_ci		if (r) {
17338c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s:%d invalid cmd stream %d\n", __func__, __LINE__, idx);
17348c2ecf20Sopenharmony_ci			return r;
17358c2ecf20Sopenharmony_ci		}
17368c2ecf20Sopenharmony_ci		break;
17378c2ecf20Sopenharmony_ci	case PACKET3_DRAW_INDEX_IMMD_BE:
17388c2ecf20Sopenharmony_ci	case PACKET3_DRAW_INDEX_IMMD:
17398c2ecf20Sopenharmony_ci		if (pkt->count < 2) {
17408c2ecf20Sopenharmony_ci			DRM_ERROR("bad DRAW_INDEX_IMMD\n");
17418c2ecf20Sopenharmony_ci			return -EINVAL;
17428c2ecf20Sopenharmony_ci		}
17438c2ecf20Sopenharmony_ci		r = r600_cs_track_check(p);
17448c2ecf20Sopenharmony_ci		if (r) {
17458c2ecf20Sopenharmony_ci			dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
17468c2ecf20Sopenharmony_ci			return r;
17478c2ecf20Sopenharmony_ci		}
17488c2ecf20Sopenharmony_ci		break;
17498c2ecf20Sopenharmony_ci	case PACKET3_WAIT_REG_MEM:
17508c2ecf20Sopenharmony_ci		if (pkt->count != 5) {
17518c2ecf20Sopenharmony_ci			DRM_ERROR("bad WAIT_REG_MEM\n");
17528c2ecf20Sopenharmony_ci			return -EINVAL;
17538c2ecf20Sopenharmony_ci		}
17548c2ecf20Sopenharmony_ci		/* bit 4 is reg (0) or mem (1) */
17558c2ecf20Sopenharmony_ci		if (idx_value & 0x10) {
17568c2ecf20Sopenharmony_ci			uint64_t offset;
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
17598c2ecf20Sopenharmony_ci			if (r) {
17608c2ecf20Sopenharmony_ci				DRM_ERROR("bad WAIT_REG_MEM\n");
17618c2ecf20Sopenharmony_ci				return -EINVAL;
17628c2ecf20Sopenharmony_ci			}
17638c2ecf20Sopenharmony_ci
17648c2ecf20Sopenharmony_ci			offset = reloc->gpu_offset +
17658c2ecf20Sopenharmony_ci				 (radeon_get_ib_value(p, idx+1) & 0xfffffff0) +
17668c2ecf20Sopenharmony_ci				 ((u64)(radeon_get_ib_value(p, idx+2) & 0xff) << 32);
17678c2ecf20Sopenharmony_ci
17688c2ecf20Sopenharmony_ci			ib[idx+1] = (ib[idx+1] & 0x3) | (offset & 0xfffffff0);
17698c2ecf20Sopenharmony_ci			ib[idx+2] = upper_32_bits(offset) & 0xff;
17708c2ecf20Sopenharmony_ci		} else if (idx_value & 0x100) {
17718c2ecf20Sopenharmony_ci			DRM_ERROR("cannot use PFP on REG wait\n");
17728c2ecf20Sopenharmony_ci			return -EINVAL;
17738c2ecf20Sopenharmony_ci		}
17748c2ecf20Sopenharmony_ci		break;
17758c2ecf20Sopenharmony_ci	case PACKET3_CP_DMA:
17768c2ecf20Sopenharmony_ci	{
17778c2ecf20Sopenharmony_ci		u32 command, size;
17788c2ecf20Sopenharmony_ci		u64 offset, tmp;
17798c2ecf20Sopenharmony_ci		if (pkt->count != 4) {
17808c2ecf20Sopenharmony_ci			DRM_ERROR("bad CP DMA\n");
17818c2ecf20Sopenharmony_ci			return -EINVAL;
17828c2ecf20Sopenharmony_ci		}
17838c2ecf20Sopenharmony_ci		command = radeon_get_ib_value(p, idx+4);
17848c2ecf20Sopenharmony_ci		size = command & 0x1fffff;
17858c2ecf20Sopenharmony_ci		if (command & PACKET3_CP_DMA_CMD_SAS) {
17868c2ecf20Sopenharmony_ci			/* src address space is register */
17878c2ecf20Sopenharmony_ci			DRM_ERROR("CP DMA SAS not supported\n");
17888c2ecf20Sopenharmony_ci			return -EINVAL;
17898c2ecf20Sopenharmony_ci		} else {
17908c2ecf20Sopenharmony_ci			if (command & PACKET3_CP_DMA_CMD_SAIC) {
17918c2ecf20Sopenharmony_ci				DRM_ERROR("CP DMA SAIC only supported for registers\n");
17928c2ecf20Sopenharmony_ci				return -EINVAL;
17938c2ecf20Sopenharmony_ci			}
17948c2ecf20Sopenharmony_ci			/* src address space is memory */
17958c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
17968c2ecf20Sopenharmony_ci			if (r) {
17978c2ecf20Sopenharmony_ci				DRM_ERROR("bad CP DMA SRC\n");
17988c2ecf20Sopenharmony_ci				return -EINVAL;
17998c2ecf20Sopenharmony_ci			}
18008c2ecf20Sopenharmony_ci
18018c2ecf20Sopenharmony_ci			tmp = radeon_get_ib_value(p, idx) +
18028c2ecf20Sopenharmony_ci				((u64)(radeon_get_ib_value(p, idx+1) & 0xff) << 32);
18038c2ecf20Sopenharmony_ci
18048c2ecf20Sopenharmony_ci			offset = reloc->gpu_offset + tmp;
18058c2ecf20Sopenharmony_ci
18068c2ecf20Sopenharmony_ci			if ((tmp + size) > radeon_bo_size(reloc->robj)) {
18078c2ecf20Sopenharmony_ci				dev_warn(p->dev, "CP DMA src buffer too small (%llu %lu)\n",
18088c2ecf20Sopenharmony_ci					 tmp + size, radeon_bo_size(reloc->robj));
18098c2ecf20Sopenharmony_ci				return -EINVAL;
18108c2ecf20Sopenharmony_ci			}
18118c2ecf20Sopenharmony_ci
18128c2ecf20Sopenharmony_ci			ib[idx] = offset;
18138c2ecf20Sopenharmony_ci			ib[idx+1] = (ib[idx+1] & 0xffffff00) | (upper_32_bits(offset) & 0xff);
18148c2ecf20Sopenharmony_ci		}
18158c2ecf20Sopenharmony_ci		if (command & PACKET3_CP_DMA_CMD_DAS) {
18168c2ecf20Sopenharmony_ci			/* dst address space is register */
18178c2ecf20Sopenharmony_ci			DRM_ERROR("CP DMA DAS not supported\n");
18188c2ecf20Sopenharmony_ci			return -EINVAL;
18198c2ecf20Sopenharmony_ci		} else {
18208c2ecf20Sopenharmony_ci			/* dst address space is memory */
18218c2ecf20Sopenharmony_ci			if (command & PACKET3_CP_DMA_CMD_DAIC) {
18228c2ecf20Sopenharmony_ci				DRM_ERROR("CP DMA DAIC only supported for registers\n");
18238c2ecf20Sopenharmony_ci				return -EINVAL;
18248c2ecf20Sopenharmony_ci			}
18258c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
18268c2ecf20Sopenharmony_ci			if (r) {
18278c2ecf20Sopenharmony_ci				DRM_ERROR("bad CP DMA DST\n");
18288c2ecf20Sopenharmony_ci				return -EINVAL;
18298c2ecf20Sopenharmony_ci			}
18308c2ecf20Sopenharmony_ci
18318c2ecf20Sopenharmony_ci			tmp = radeon_get_ib_value(p, idx+2) +
18328c2ecf20Sopenharmony_ci				((u64)(radeon_get_ib_value(p, idx+3) & 0xff) << 32);
18338c2ecf20Sopenharmony_ci
18348c2ecf20Sopenharmony_ci			offset = reloc->gpu_offset + tmp;
18358c2ecf20Sopenharmony_ci
18368c2ecf20Sopenharmony_ci			if ((tmp + size) > radeon_bo_size(reloc->robj)) {
18378c2ecf20Sopenharmony_ci				dev_warn(p->dev, "CP DMA dst buffer too small (%llu %lu)\n",
18388c2ecf20Sopenharmony_ci					 tmp + size, radeon_bo_size(reloc->robj));
18398c2ecf20Sopenharmony_ci				return -EINVAL;
18408c2ecf20Sopenharmony_ci			}
18418c2ecf20Sopenharmony_ci
18428c2ecf20Sopenharmony_ci			ib[idx+2] = offset;
18438c2ecf20Sopenharmony_ci			ib[idx+3] = upper_32_bits(offset) & 0xff;
18448c2ecf20Sopenharmony_ci		}
18458c2ecf20Sopenharmony_ci		break;
18468c2ecf20Sopenharmony_ci	}
18478c2ecf20Sopenharmony_ci	case PACKET3_SURFACE_SYNC:
18488c2ecf20Sopenharmony_ci		if (pkt->count != 3) {
18498c2ecf20Sopenharmony_ci			DRM_ERROR("bad SURFACE_SYNC\n");
18508c2ecf20Sopenharmony_ci			return -EINVAL;
18518c2ecf20Sopenharmony_ci		}
18528c2ecf20Sopenharmony_ci		/* 0xffffffff/0x0 is flush all cache flag */
18538c2ecf20Sopenharmony_ci		if (radeon_get_ib_value(p, idx + 1) != 0xffffffff ||
18548c2ecf20Sopenharmony_ci		    radeon_get_ib_value(p, idx + 2) != 0) {
18558c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
18568c2ecf20Sopenharmony_ci			if (r) {
18578c2ecf20Sopenharmony_ci				DRM_ERROR("bad SURFACE_SYNC\n");
18588c2ecf20Sopenharmony_ci				return -EINVAL;
18598c2ecf20Sopenharmony_ci			}
18608c2ecf20Sopenharmony_ci			ib[idx+2] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
18618c2ecf20Sopenharmony_ci		}
18628c2ecf20Sopenharmony_ci		break;
18638c2ecf20Sopenharmony_ci	case PACKET3_EVENT_WRITE:
18648c2ecf20Sopenharmony_ci		if (pkt->count != 2 && pkt->count != 0) {
18658c2ecf20Sopenharmony_ci			DRM_ERROR("bad EVENT_WRITE\n");
18668c2ecf20Sopenharmony_ci			return -EINVAL;
18678c2ecf20Sopenharmony_ci		}
18688c2ecf20Sopenharmony_ci		if (pkt->count) {
18698c2ecf20Sopenharmony_ci			uint64_t offset;
18708c2ecf20Sopenharmony_ci
18718c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
18728c2ecf20Sopenharmony_ci			if (r) {
18738c2ecf20Sopenharmony_ci				DRM_ERROR("bad EVENT_WRITE\n");
18748c2ecf20Sopenharmony_ci				return -EINVAL;
18758c2ecf20Sopenharmony_ci			}
18768c2ecf20Sopenharmony_ci			offset = reloc->gpu_offset +
18778c2ecf20Sopenharmony_ci				 (radeon_get_ib_value(p, idx+1) & 0xfffffff8) +
18788c2ecf20Sopenharmony_ci				 ((u64)(radeon_get_ib_value(p, idx+2) & 0xff) << 32);
18798c2ecf20Sopenharmony_ci
18808c2ecf20Sopenharmony_ci			ib[idx+1] = offset & 0xfffffff8;
18818c2ecf20Sopenharmony_ci			ib[idx+2] = upper_32_bits(offset) & 0xff;
18828c2ecf20Sopenharmony_ci		}
18838c2ecf20Sopenharmony_ci		break;
18848c2ecf20Sopenharmony_ci	case PACKET3_EVENT_WRITE_EOP:
18858c2ecf20Sopenharmony_ci	{
18868c2ecf20Sopenharmony_ci		uint64_t offset;
18878c2ecf20Sopenharmony_ci
18888c2ecf20Sopenharmony_ci		if (pkt->count != 4) {
18898c2ecf20Sopenharmony_ci			DRM_ERROR("bad EVENT_WRITE_EOP\n");
18908c2ecf20Sopenharmony_ci			return -EINVAL;
18918c2ecf20Sopenharmony_ci		}
18928c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
18938c2ecf20Sopenharmony_ci		if (r) {
18948c2ecf20Sopenharmony_ci			DRM_ERROR("bad EVENT_WRITE\n");
18958c2ecf20Sopenharmony_ci			return -EINVAL;
18968c2ecf20Sopenharmony_ci		}
18978c2ecf20Sopenharmony_ci
18988c2ecf20Sopenharmony_ci		offset = reloc->gpu_offset +
18998c2ecf20Sopenharmony_ci			 (radeon_get_ib_value(p, idx+1) & 0xfffffffc) +
19008c2ecf20Sopenharmony_ci			 ((u64)(radeon_get_ib_value(p, idx+2) & 0xff) << 32);
19018c2ecf20Sopenharmony_ci
19028c2ecf20Sopenharmony_ci		ib[idx+1] = offset & 0xfffffffc;
19038c2ecf20Sopenharmony_ci		ib[idx+2] = (ib[idx+2] & 0xffffff00) | (upper_32_bits(offset) & 0xff);
19048c2ecf20Sopenharmony_ci		break;
19058c2ecf20Sopenharmony_ci	}
19068c2ecf20Sopenharmony_ci	case PACKET3_SET_CONFIG_REG:
19078c2ecf20Sopenharmony_ci		start_reg = (idx_value << 2) + PACKET3_SET_CONFIG_REG_OFFSET;
19088c2ecf20Sopenharmony_ci		end_reg = 4 * pkt->count + start_reg - 4;
19098c2ecf20Sopenharmony_ci		if ((start_reg < PACKET3_SET_CONFIG_REG_OFFSET) ||
19108c2ecf20Sopenharmony_ci		    (start_reg >= PACKET3_SET_CONFIG_REG_END) ||
19118c2ecf20Sopenharmony_ci		    (end_reg >= PACKET3_SET_CONFIG_REG_END)) {
19128c2ecf20Sopenharmony_ci			DRM_ERROR("bad PACKET3_SET_CONFIG_REG\n");
19138c2ecf20Sopenharmony_ci			return -EINVAL;
19148c2ecf20Sopenharmony_ci		}
19158c2ecf20Sopenharmony_ci		for (i = 0; i < pkt->count; i++) {
19168c2ecf20Sopenharmony_ci			reg = start_reg + (4 * i);
19178c2ecf20Sopenharmony_ci			r = r600_cs_check_reg(p, reg, idx+1+i);
19188c2ecf20Sopenharmony_ci			if (r)
19198c2ecf20Sopenharmony_ci				return r;
19208c2ecf20Sopenharmony_ci		}
19218c2ecf20Sopenharmony_ci		break;
19228c2ecf20Sopenharmony_ci	case PACKET3_SET_CONTEXT_REG:
19238c2ecf20Sopenharmony_ci		start_reg = (idx_value << 2) + PACKET3_SET_CONTEXT_REG_OFFSET;
19248c2ecf20Sopenharmony_ci		end_reg = 4 * pkt->count + start_reg - 4;
19258c2ecf20Sopenharmony_ci		if ((start_reg < PACKET3_SET_CONTEXT_REG_OFFSET) ||
19268c2ecf20Sopenharmony_ci		    (start_reg >= PACKET3_SET_CONTEXT_REG_END) ||
19278c2ecf20Sopenharmony_ci		    (end_reg >= PACKET3_SET_CONTEXT_REG_END)) {
19288c2ecf20Sopenharmony_ci			DRM_ERROR("bad PACKET3_SET_CONTEXT_REG\n");
19298c2ecf20Sopenharmony_ci			return -EINVAL;
19308c2ecf20Sopenharmony_ci		}
19318c2ecf20Sopenharmony_ci		for (i = 0; i < pkt->count; i++) {
19328c2ecf20Sopenharmony_ci			reg = start_reg + (4 * i);
19338c2ecf20Sopenharmony_ci			r = r600_cs_check_reg(p, reg, idx+1+i);
19348c2ecf20Sopenharmony_ci			if (r)
19358c2ecf20Sopenharmony_ci				return r;
19368c2ecf20Sopenharmony_ci		}
19378c2ecf20Sopenharmony_ci		break;
19388c2ecf20Sopenharmony_ci	case PACKET3_SET_RESOURCE:
19398c2ecf20Sopenharmony_ci		if (pkt->count % 7) {
19408c2ecf20Sopenharmony_ci			DRM_ERROR("bad SET_RESOURCE\n");
19418c2ecf20Sopenharmony_ci			return -EINVAL;
19428c2ecf20Sopenharmony_ci		}
19438c2ecf20Sopenharmony_ci		start_reg = (idx_value << 2) + PACKET3_SET_RESOURCE_OFFSET;
19448c2ecf20Sopenharmony_ci		end_reg = 4 * pkt->count + start_reg - 4;
19458c2ecf20Sopenharmony_ci		if ((start_reg < PACKET3_SET_RESOURCE_OFFSET) ||
19468c2ecf20Sopenharmony_ci		    (start_reg >= PACKET3_SET_RESOURCE_END) ||
19478c2ecf20Sopenharmony_ci		    (end_reg >= PACKET3_SET_RESOURCE_END)) {
19488c2ecf20Sopenharmony_ci			DRM_ERROR("bad SET_RESOURCE\n");
19498c2ecf20Sopenharmony_ci			return -EINVAL;
19508c2ecf20Sopenharmony_ci		}
19518c2ecf20Sopenharmony_ci		for (i = 0; i < (pkt->count / 7); i++) {
19528c2ecf20Sopenharmony_ci			struct radeon_bo *texture, *mipmap;
19538c2ecf20Sopenharmony_ci			u32 size, offset, base_offset, mip_offset;
19548c2ecf20Sopenharmony_ci
19558c2ecf20Sopenharmony_ci			switch (G__SQ_VTX_CONSTANT_TYPE(radeon_get_ib_value(p, idx+(i*7)+6+1))) {
19568c2ecf20Sopenharmony_ci			case SQ_TEX_VTX_VALID_TEXTURE:
19578c2ecf20Sopenharmony_ci				/* tex base */
19588c2ecf20Sopenharmony_ci				r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
19598c2ecf20Sopenharmony_ci				if (r) {
19608c2ecf20Sopenharmony_ci					DRM_ERROR("bad SET_RESOURCE\n");
19618c2ecf20Sopenharmony_ci					return -EINVAL;
19628c2ecf20Sopenharmony_ci				}
19638c2ecf20Sopenharmony_ci				base_offset = (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
19648c2ecf20Sopenharmony_ci				if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
19658c2ecf20Sopenharmony_ci					if (reloc->tiling_flags & RADEON_TILING_MACRO)
19668c2ecf20Sopenharmony_ci						ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
19678c2ecf20Sopenharmony_ci					else if (reloc->tiling_flags & RADEON_TILING_MICRO)
19688c2ecf20Sopenharmony_ci						ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
19698c2ecf20Sopenharmony_ci				}
19708c2ecf20Sopenharmony_ci				texture = reloc->robj;
19718c2ecf20Sopenharmony_ci				/* tex mip base */
19728c2ecf20Sopenharmony_ci				r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
19738c2ecf20Sopenharmony_ci				if (r) {
19748c2ecf20Sopenharmony_ci					DRM_ERROR("bad SET_RESOURCE\n");
19758c2ecf20Sopenharmony_ci					return -EINVAL;
19768c2ecf20Sopenharmony_ci				}
19778c2ecf20Sopenharmony_ci				mip_offset = (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
19788c2ecf20Sopenharmony_ci				mipmap = reloc->robj;
19798c2ecf20Sopenharmony_ci				r = r600_check_texture_resource(p,  idx+(i*7)+1,
19808c2ecf20Sopenharmony_ci								texture, mipmap,
19818c2ecf20Sopenharmony_ci								base_offset + radeon_get_ib_value(p, idx+1+(i*7)+2),
19828c2ecf20Sopenharmony_ci								mip_offset + radeon_get_ib_value(p, idx+1+(i*7)+3),
19838c2ecf20Sopenharmony_ci								reloc->tiling_flags);
19848c2ecf20Sopenharmony_ci				if (r)
19858c2ecf20Sopenharmony_ci					return r;
19868c2ecf20Sopenharmony_ci				ib[idx+1+(i*7)+2] += base_offset;
19878c2ecf20Sopenharmony_ci				ib[idx+1+(i*7)+3] += mip_offset;
19888c2ecf20Sopenharmony_ci				break;
19898c2ecf20Sopenharmony_ci			case SQ_TEX_VTX_VALID_BUFFER:
19908c2ecf20Sopenharmony_ci			{
19918c2ecf20Sopenharmony_ci				uint64_t offset64;
19928c2ecf20Sopenharmony_ci				/* vtx base */
19938c2ecf20Sopenharmony_ci				r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
19948c2ecf20Sopenharmony_ci				if (r) {
19958c2ecf20Sopenharmony_ci					DRM_ERROR("bad SET_RESOURCE\n");
19968c2ecf20Sopenharmony_ci					return -EINVAL;
19978c2ecf20Sopenharmony_ci				}
19988c2ecf20Sopenharmony_ci				offset = radeon_get_ib_value(p, idx+1+(i*7)+0);
19998c2ecf20Sopenharmony_ci				size = radeon_get_ib_value(p, idx+1+(i*7)+1) + 1;
20008c2ecf20Sopenharmony_ci				if (p->rdev && (size + offset) > radeon_bo_size(reloc->robj)) {
20018c2ecf20Sopenharmony_ci					/* force size to size of the buffer */
20028c2ecf20Sopenharmony_ci					dev_warn(p->dev, "vbo resource seems too big (%d) for the bo (%ld)\n",
20038c2ecf20Sopenharmony_ci						 size + offset, radeon_bo_size(reloc->robj));
20048c2ecf20Sopenharmony_ci					ib[idx+1+(i*7)+1] = radeon_bo_size(reloc->robj) - offset;
20058c2ecf20Sopenharmony_ci				}
20068c2ecf20Sopenharmony_ci
20078c2ecf20Sopenharmony_ci				offset64 = reloc->gpu_offset + offset;
20088c2ecf20Sopenharmony_ci				ib[idx+1+(i*8)+0] = offset64;
20098c2ecf20Sopenharmony_ci				ib[idx+1+(i*8)+2] = (ib[idx+1+(i*8)+2] & 0xffffff00) |
20108c2ecf20Sopenharmony_ci						    (upper_32_bits(offset64) & 0xff);
20118c2ecf20Sopenharmony_ci				break;
20128c2ecf20Sopenharmony_ci			}
20138c2ecf20Sopenharmony_ci			case SQ_TEX_VTX_INVALID_TEXTURE:
20148c2ecf20Sopenharmony_ci			case SQ_TEX_VTX_INVALID_BUFFER:
20158c2ecf20Sopenharmony_ci			default:
20168c2ecf20Sopenharmony_ci				DRM_ERROR("bad SET_RESOURCE\n");
20178c2ecf20Sopenharmony_ci				return -EINVAL;
20188c2ecf20Sopenharmony_ci			}
20198c2ecf20Sopenharmony_ci		}
20208c2ecf20Sopenharmony_ci		break;
20218c2ecf20Sopenharmony_ci	case PACKET3_SET_ALU_CONST:
20228c2ecf20Sopenharmony_ci		if (track->sq_config & DX9_CONSTS) {
20238c2ecf20Sopenharmony_ci			start_reg = (idx_value << 2) + PACKET3_SET_ALU_CONST_OFFSET;
20248c2ecf20Sopenharmony_ci			end_reg = 4 * pkt->count + start_reg - 4;
20258c2ecf20Sopenharmony_ci			if ((start_reg < PACKET3_SET_ALU_CONST_OFFSET) ||
20268c2ecf20Sopenharmony_ci			    (start_reg >= PACKET3_SET_ALU_CONST_END) ||
20278c2ecf20Sopenharmony_ci			    (end_reg >= PACKET3_SET_ALU_CONST_END)) {
20288c2ecf20Sopenharmony_ci				DRM_ERROR("bad SET_ALU_CONST\n");
20298c2ecf20Sopenharmony_ci				return -EINVAL;
20308c2ecf20Sopenharmony_ci			}
20318c2ecf20Sopenharmony_ci		}
20328c2ecf20Sopenharmony_ci		break;
20338c2ecf20Sopenharmony_ci	case PACKET3_SET_BOOL_CONST:
20348c2ecf20Sopenharmony_ci		start_reg = (idx_value << 2) + PACKET3_SET_BOOL_CONST_OFFSET;
20358c2ecf20Sopenharmony_ci		end_reg = 4 * pkt->count + start_reg - 4;
20368c2ecf20Sopenharmony_ci		if ((start_reg < PACKET3_SET_BOOL_CONST_OFFSET) ||
20378c2ecf20Sopenharmony_ci		    (start_reg >= PACKET3_SET_BOOL_CONST_END) ||
20388c2ecf20Sopenharmony_ci		    (end_reg >= PACKET3_SET_BOOL_CONST_END)) {
20398c2ecf20Sopenharmony_ci			DRM_ERROR("bad SET_BOOL_CONST\n");
20408c2ecf20Sopenharmony_ci			return -EINVAL;
20418c2ecf20Sopenharmony_ci		}
20428c2ecf20Sopenharmony_ci		break;
20438c2ecf20Sopenharmony_ci	case PACKET3_SET_LOOP_CONST:
20448c2ecf20Sopenharmony_ci		start_reg = (idx_value << 2) + PACKET3_SET_LOOP_CONST_OFFSET;
20458c2ecf20Sopenharmony_ci		end_reg = 4 * pkt->count + start_reg - 4;
20468c2ecf20Sopenharmony_ci		if ((start_reg < PACKET3_SET_LOOP_CONST_OFFSET) ||
20478c2ecf20Sopenharmony_ci		    (start_reg >= PACKET3_SET_LOOP_CONST_END) ||
20488c2ecf20Sopenharmony_ci		    (end_reg >= PACKET3_SET_LOOP_CONST_END)) {
20498c2ecf20Sopenharmony_ci			DRM_ERROR("bad SET_LOOP_CONST\n");
20508c2ecf20Sopenharmony_ci			return -EINVAL;
20518c2ecf20Sopenharmony_ci		}
20528c2ecf20Sopenharmony_ci		break;
20538c2ecf20Sopenharmony_ci	case PACKET3_SET_CTL_CONST:
20548c2ecf20Sopenharmony_ci		start_reg = (idx_value << 2) + PACKET3_SET_CTL_CONST_OFFSET;
20558c2ecf20Sopenharmony_ci		end_reg = 4 * pkt->count + start_reg - 4;
20568c2ecf20Sopenharmony_ci		if ((start_reg < PACKET3_SET_CTL_CONST_OFFSET) ||
20578c2ecf20Sopenharmony_ci		    (start_reg >= PACKET3_SET_CTL_CONST_END) ||
20588c2ecf20Sopenharmony_ci		    (end_reg >= PACKET3_SET_CTL_CONST_END)) {
20598c2ecf20Sopenharmony_ci			DRM_ERROR("bad SET_CTL_CONST\n");
20608c2ecf20Sopenharmony_ci			return -EINVAL;
20618c2ecf20Sopenharmony_ci		}
20628c2ecf20Sopenharmony_ci		break;
20638c2ecf20Sopenharmony_ci	case PACKET3_SET_SAMPLER:
20648c2ecf20Sopenharmony_ci		if (pkt->count % 3) {
20658c2ecf20Sopenharmony_ci			DRM_ERROR("bad SET_SAMPLER\n");
20668c2ecf20Sopenharmony_ci			return -EINVAL;
20678c2ecf20Sopenharmony_ci		}
20688c2ecf20Sopenharmony_ci		start_reg = (idx_value << 2) + PACKET3_SET_SAMPLER_OFFSET;
20698c2ecf20Sopenharmony_ci		end_reg = 4 * pkt->count + start_reg - 4;
20708c2ecf20Sopenharmony_ci		if ((start_reg < PACKET3_SET_SAMPLER_OFFSET) ||
20718c2ecf20Sopenharmony_ci		    (start_reg >= PACKET3_SET_SAMPLER_END) ||
20728c2ecf20Sopenharmony_ci		    (end_reg >= PACKET3_SET_SAMPLER_END)) {
20738c2ecf20Sopenharmony_ci			DRM_ERROR("bad SET_SAMPLER\n");
20748c2ecf20Sopenharmony_ci			return -EINVAL;
20758c2ecf20Sopenharmony_ci		}
20768c2ecf20Sopenharmony_ci		break;
20778c2ecf20Sopenharmony_ci	case PACKET3_STRMOUT_BASE_UPDATE:
20788c2ecf20Sopenharmony_ci		/* RS780 and RS880 also need this */
20798c2ecf20Sopenharmony_ci		if (p->family < CHIP_RS780) {
20808c2ecf20Sopenharmony_ci			DRM_ERROR("STRMOUT_BASE_UPDATE only supported on 7xx\n");
20818c2ecf20Sopenharmony_ci			return -EINVAL;
20828c2ecf20Sopenharmony_ci		}
20838c2ecf20Sopenharmony_ci		if (pkt->count != 1) {
20848c2ecf20Sopenharmony_ci			DRM_ERROR("bad STRMOUT_BASE_UPDATE packet count\n");
20858c2ecf20Sopenharmony_ci			return -EINVAL;
20868c2ecf20Sopenharmony_ci		}
20878c2ecf20Sopenharmony_ci		if (idx_value > 3) {
20888c2ecf20Sopenharmony_ci			DRM_ERROR("bad STRMOUT_BASE_UPDATE index\n");
20898c2ecf20Sopenharmony_ci			return -EINVAL;
20908c2ecf20Sopenharmony_ci		}
20918c2ecf20Sopenharmony_ci		{
20928c2ecf20Sopenharmony_ci			u64 offset;
20938c2ecf20Sopenharmony_ci
20948c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
20958c2ecf20Sopenharmony_ci			if (r) {
20968c2ecf20Sopenharmony_ci				DRM_ERROR("bad STRMOUT_BASE_UPDATE reloc\n");
20978c2ecf20Sopenharmony_ci				return -EINVAL;
20988c2ecf20Sopenharmony_ci			}
20998c2ecf20Sopenharmony_ci
21008c2ecf20Sopenharmony_ci			if (reloc->robj != track->vgt_strmout_bo[idx_value]) {
21018c2ecf20Sopenharmony_ci				DRM_ERROR("bad STRMOUT_BASE_UPDATE, bo does not match\n");
21028c2ecf20Sopenharmony_ci				return -EINVAL;
21038c2ecf20Sopenharmony_ci			}
21048c2ecf20Sopenharmony_ci
21058c2ecf20Sopenharmony_ci			offset = radeon_get_ib_value(p, idx+1) << 8;
21068c2ecf20Sopenharmony_ci			if (offset != track->vgt_strmout_bo_offset[idx_value]) {
21078c2ecf20Sopenharmony_ci				DRM_ERROR("bad STRMOUT_BASE_UPDATE, bo offset does not match: 0x%llx, 0x%x\n",
21088c2ecf20Sopenharmony_ci					  offset, track->vgt_strmout_bo_offset[idx_value]);
21098c2ecf20Sopenharmony_ci				return -EINVAL;
21108c2ecf20Sopenharmony_ci			}
21118c2ecf20Sopenharmony_ci
21128c2ecf20Sopenharmony_ci			if ((offset + 4) > radeon_bo_size(reloc->robj)) {
21138c2ecf20Sopenharmony_ci				DRM_ERROR("bad STRMOUT_BASE_UPDATE bo too small: 0x%llx, 0x%lx\n",
21148c2ecf20Sopenharmony_ci					  offset + 4, radeon_bo_size(reloc->robj));
21158c2ecf20Sopenharmony_ci				return -EINVAL;
21168c2ecf20Sopenharmony_ci			}
21178c2ecf20Sopenharmony_ci			ib[idx+1] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
21188c2ecf20Sopenharmony_ci		}
21198c2ecf20Sopenharmony_ci		break;
21208c2ecf20Sopenharmony_ci	case PACKET3_SURFACE_BASE_UPDATE:
21218c2ecf20Sopenharmony_ci		if (p->family >= CHIP_RV770 || p->family == CHIP_R600) {
21228c2ecf20Sopenharmony_ci			DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
21238c2ecf20Sopenharmony_ci			return -EINVAL;
21248c2ecf20Sopenharmony_ci		}
21258c2ecf20Sopenharmony_ci		if (pkt->count) {
21268c2ecf20Sopenharmony_ci			DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
21278c2ecf20Sopenharmony_ci			return -EINVAL;
21288c2ecf20Sopenharmony_ci		}
21298c2ecf20Sopenharmony_ci		break;
21308c2ecf20Sopenharmony_ci	case PACKET3_STRMOUT_BUFFER_UPDATE:
21318c2ecf20Sopenharmony_ci		if (pkt->count != 4) {
21328c2ecf20Sopenharmony_ci			DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (invalid count)\n");
21338c2ecf20Sopenharmony_ci			return -EINVAL;
21348c2ecf20Sopenharmony_ci		}
21358c2ecf20Sopenharmony_ci		/* Updating memory at DST_ADDRESS. */
21368c2ecf20Sopenharmony_ci		if (idx_value & 0x1) {
21378c2ecf20Sopenharmony_ci			u64 offset;
21388c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
21398c2ecf20Sopenharmony_ci			if (r) {
21408c2ecf20Sopenharmony_ci				DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (missing dst reloc)\n");
21418c2ecf20Sopenharmony_ci				return -EINVAL;
21428c2ecf20Sopenharmony_ci			}
21438c2ecf20Sopenharmony_ci			offset = radeon_get_ib_value(p, idx+1);
21448c2ecf20Sopenharmony_ci			offset += ((u64)(radeon_get_ib_value(p, idx+2) & 0xff)) << 32;
21458c2ecf20Sopenharmony_ci			if ((offset + 4) > radeon_bo_size(reloc->robj)) {
21468c2ecf20Sopenharmony_ci				DRM_ERROR("bad STRMOUT_BUFFER_UPDATE dst bo too small: 0x%llx, 0x%lx\n",
21478c2ecf20Sopenharmony_ci					  offset + 4, radeon_bo_size(reloc->robj));
21488c2ecf20Sopenharmony_ci				return -EINVAL;
21498c2ecf20Sopenharmony_ci			}
21508c2ecf20Sopenharmony_ci			offset += reloc->gpu_offset;
21518c2ecf20Sopenharmony_ci			ib[idx+1] = offset;
21528c2ecf20Sopenharmony_ci			ib[idx+2] = upper_32_bits(offset) & 0xff;
21538c2ecf20Sopenharmony_ci		}
21548c2ecf20Sopenharmony_ci		/* Reading data from SRC_ADDRESS. */
21558c2ecf20Sopenharmony_ci		if (((idx_value >> 1) & 0x3) == 2) {
21568c2ecf20Sopenharmony_ci			u64 offset;
21578c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
21588c2ecf20Sopenharmony_ci			if (r) {
21598c2ecf20Sopenharmony_ci				DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (missing src reloc)\n");
21608c2ecf20Sopenharmony_ci				return -EINVAL;
21618c2ecf20Sopenharmony_ci			}
21628c2ecf20Sopenharmony_ci			offset = radeon_get_ib_value(p, idx+3);
21638c2ecf20Sopenharmony_ci			offset += ((u64)(radeon_get_ib_value(p, idx+4) & 0xff)) << 32;
21648c2ecf20Sopenharmony_ci			if ((offset + 4) > radeon_bo_size(reloc->robj)) {
21658c2ecf20Sopenharmony_ci				DRM_ERROR("bad STRMOUT_BUFFER_UPDATE src bo too small: 0x%llx, 0x%lx\n",
21668c2ecf20Sopenharmony_ci					  offset + 4, radeon_bo_size(reloc->robj));
21678c2ecf20Sopenharmony_ci				return -EINVAL;
21688c2ecf20Sopenharmony_ci			}
21698c2ecf20Sopenharmony_ci			offset += reloc->gpu_offset;
21708c2ecf20Sopenharmony_ci			ib[idx+3] = offset;
21718c2ecf20Sopenharmony_ci			ib[idx+4] = upper_32_bits(offset) & 0xff;
21728c2ecf20Sopenharmony_ci		}
21738c2ecf20Sopenharmony_ci		break;
21748c2ecf20Sopenharmony_ci	case PACKET3_MEM_WRITE:
21758c2ecf20Sopenharmony_ci	{
21768c2ecf20Sopenharmony_ci		u64 offset;
21778c2ecf20Sopenharmony_ci
21788c2ecf20Sopenharmony_ci		if (pkt->count != 3) {
21798c2ecf20Sopenharmony_ci			DRM_ERROR("bad MEM_WRITE (invalid count)\n");
21808c2ecf20Sopenharmony_ci			return -EINVAL;
21818c2ecf20Sopenharmony_ci		}
21828c2ecf20Sopenharmony_ci		r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
21838c2ecf20Sopenharmony_ci		if (r) {
21848c2ecf20Sopenharmony_ci			DRM_ERROR("bad MEM_WRITE (missing reloc)\n");
21858c2ecf20Sopenharmony_ci			return -EINVAL;
21868c2ecf20Sopenharmony_ci		}
21878c2ecf20Sopenharmony_ci		offset = radeon_get_ib_value(p, idx+0);
21888c2ecf20Sopenharmony_ci		offset += ((u64)(radeon_get_ib_value(p, idx+1) & 0xff)) << 32UL;
21898c2ecf20Sopenharmony_ci		if (offset & 0x7) {
21908c2ecf20Sopenharmony_ci			DRM_ERROR("bad MEM_WRITE (address not qwords aligned)\n");
21918c2ecf20Sopenharmony_ci			return -EINVAL;
21928c2ecf20Sopenharmony_ci		}
21938c2ecf20Sopenharmony_ci		if ((offset + 8) > radeon_bo_size(reloc->robj)) {
21948c2ecf20Sopenharmony_ci			DRM_ERROR("bad MEM_WRITE bo too small: 0x%llx, 0x%lx\n",
21958c2ecf20Sopenharmony_ci				  offset + 8, radeon_bo_size(reloc->robj));
21968c2ecf20Sopenharmony_ci			return -EINVAL;
21978c2ecf20Sopenharmony_ci		}
21988c2ecf20Sopenharmony_ci		offset += reloc->gpu_offset;
21998c2ecf20Sopenharmony_ci		ib[idx+0] = offset;
22008c2ecf20Sopenharmony_ci		ib[idx+1] = upper_32_bits(offset) & 0xff;
22018c2ecf20Sopenharmony_ci		break;
22028c2ecf20Sopenharmony_ci	}
22038c2ecf20Sopenharmony_ci	case PACKET3_COPY_DW:
22048c2ecf20Sopenharmony_ci		if (pkt->count != 4) {
22058c2ecf20Sopenharmony_ci			DRM_ERROR("bad COPY_DW (invalid count)\n");
22068c2ecf20Sopenharmony_ci			return -EINVAL;
22078c2ecf20Sopenharmony_ci		}
22088c2ecf20Sopenharmony_ci		if (idx_value & 0x1) {
22098c2ecf20Sopenharmony_ci			u64 offset;
22108c2ecf20Sopenharmony_ci			/* SRC is memory. */
22118c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
22128c2ecf20Sopenharmony_ci			if (r) {
22138c2ecf20Sopenharmony_ci				DRM_ERROR("bad COPY_DW (missing src reloc)\n");
22148c2ecf20Sopenharmony_ci				return -EINVAL;
22158c2ecf20Sopenharmony_ci			}
22168c2ecf20Sopenharmony_ci			offset = radeon_get_ib_value(p, idx+1);
22178c2ecf20Sopenharmony_ci			offset += ((u64)(radeon_get_ib_value(p, idx+2) & 0xff)) << 32;
22188c2ecf20Sopenharmony_ci			if ((offset + 4) > radeon_bo_size(reloc->robj)) {
22198c2ecf20Sopenharmony_ci				DRM_ERROR("bad COPY_DW src bo too small: 0x%llx, 0x%lx\n",
22208c2ecf20Sopenharmony_ci					  offset + 4, radeon_bo_size(reloc->robj));
22218c2ecf20Sopenharmony_ci				return -EINVAL;
22228c2ecf20Sopenharmony_ci			}
22238c2ecf20Sopenharmony_ci			offset += reloc->gpu_offset;
22248c2ecf20Sopenharmony_ci			ib[idx+1] = offset;
22258c2ecf20Sopenharmony_ci			ib[idx+2] = upper_32_bits(offset) & 0xff;
22268c2ecf20Sopenharmony_ci		} else {
22278c2ecf20Sopenharmony_ci			/* SRC is a reg. */
22288c2ecf20Sopenharmony_ci			reg = radeon_get_ib_value(p, idx+1) << 2;
22298c2ecf20Sopenharmony_ci			if (!r600_is_safe_reg(p, reg, idx+1))
22308c2ecf20Sopenharmony_ci				return -EINVAL;
22318c2ecf20Sopenharmony_ci		}
22328c2ecf20Sopenharmony_ci		if (idx_value & 0x2) {
22338c2ecf20Sopenharmony_ci			u64 offset;
22348c2ecf20Sopenharmony_ci			/* DST is memory. */
22358c2ecf20Sopenharmony_ci			r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
22368c2ecf20Sopenharmony_ci			if (r) {
22378c2ecf20Sopenharmony_ci				DRM_ERROR("bad COPY_DW (missing dst reloc)\n");
22388c2ecf20Sopenharmony_ci				return -EINVAL;
22398c2ecf20Sopenharmony_ci			}
22408c2ecf20Sopenharmony_ci			offset = radeon_get_ib_value(p, idx+3);
22418c2ecf20Sopenharmony_ci			offset += ((u64)(radeon_get_ib_value(p, idx+4) & 0xff)) << 32;
22428c2ecf20Sopenharmony_ci			if ((offset + 4) > radeon_bo_size(reloc->robj)) {
22438c2ecf20Sopenharmony_ci				DRM_ERROR("bad COPY_DW dst bo too small: 0x%llx, 0x%lx\n",
22448c2ecf20Sopenharmony_ci					  offset + 4, radeon_bo_size(reloc->robj));
22458c2ecf20Sopenharmony_ci				return -EINVAL;
22468c2ecf20Sopenharmony_ci			}
22478c2ecf20Sopenharmony_ci			offset += reloc->gpu_offset;
22488c2ecf20Sopenharmony_ci			ib[idx+3] = offset;
22498c2ecf20Sopenharmony_ci			ib[idx+4] = upper_32_bits(offset) & 0xff;
22508c2ecf20Sopenharmony_ci		} else {
22518c2ecf20Sopenharmony_ci			/* DST is a reg. */
22528c2ecf20Sopenharmony_ci			reg = radeon_get_ib_value(p, idx+3) << 2;
22538c2ecf20Sopenharmony_ci			if (!r600_is_safe_reg(p, reg, idx+3))
22548c2ecf20Sopenharmony_ci				return -EINVAL;
22558c2ecf20Sopenharmony_ci		}
22568c2ecf20Sopenharmony_ci		break;
22578c2ecf20Sopenharmony_ci	case PACKET3_NOP:
22588c2ecf20Sopenharmony_ci		break;
22598c2ecf20Sopenharmony_ci	default:
22608c2ecf20Sopenharmony_ci		DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
22618c2ecf20Sopenharmony_ci		return -EINVAL;
22628c2ecf20Sopenharmony_ci	}
22638c2ecf20Sopenharmony_ci	return 0;
22648c2ecf20Sopenharmony_ci}
22658c2ecf20Sopenharmony_ci
22668c2ecf20Sopenharmony_ciint r600_cs_parse(struct radeon_cs_parser *p)
22678c2ecf20Sopenharmony_ci{
22688c2ecf20Sopenharmony_ci	struct radeon_cs_packet pkt;
22698c2ecf20Sopenharmony_ci	struct r600_cs_track *track;
22708c2ecf20Sopenharmony_ci	int r;
22718c2ecf20Sopenharmony_ci
22728c2ecf20Sopenharmony_ci	if (p->track == NULL) {
22738c2ecf20Sopenharmony_ci		/* initialize tracker, we are in kms */
22748c2ecf20Sopenharmony_ci		track = kzalloc(sizeof(*track), GFP_KERNEL);
22758c2ecf20Sopenharmony_ci		if (track == NULL)
22768c2ecf20Sopenharmony_ci			return -ENOMEM;
22778c2ecf20Sopenharmony_ci		r600_cs_track_init(track);
22788c2ecf20Sopenharmony_ci		if (p->rdev->family < CHIP_RV770) {
22798c2ecf20Sopenharmony_ci			track->npipes = p->rdev->config.r600.tiling_npipes;
22808c2ecf20Sopenharmony_ci			track->nbanks = p->rdev->config.r600.tiling_nbanks;
22818c2ecf20Sopenharmony_ci			track->group_size = p->rdev->config.r600.tiling_group_size;
22828c2ecf20Sopenharmony_ci		} else if (p->rdev->family <= CHIP_RV740) {
22838c2ecf20Sopenharmony_ci			track->npipes = p->rdev->config.rv770.tiling_npipes;
22848c2ecf20Sopenharmony_ci			track->nbanks = p->rdev->config.rv770.tiling_nbanks;
22858c2ecf20Sopenharmony_ci			track->group_size = p->rdev->config.rv770.tiling_group_size;
22868c2ecf20Sopenharmony_ci		}
22878c2ecf20Sopenharmony_ci		p->track = track;
22888c2ecf20Sopenharmony_ci	}
22898c2ecf20Sopenharmony_ci	do {
22908c2ecf20Sopenharmony_ci		r = radeon_cs_packet_parse(p, &pkt, p->idx);
22918c2ecf20Sopenharmony_ci		if (r) {
22928c2ecf20Sopenharmony_ci			kfree(p->track);
22938c2ecf20Sopenharmony_ci			p->track = NULL;
22948c2ecf20Sopenharmony_ci			return r;
22958c2ecf20Sopenharmony_ci		}
22968c2ecf20Sopenharmony_ci		p->idx += pkt.count + 2;
22978c2ecf20Sopenharmony_ci		switch (pkt.type) {
22988c2ecf20Sopenharmony_ci		case RADEON_PACKET_TYPE0:
22998c2ecf20Sopenharmony_ci			r = r600_cs_parse_packet0(p, &pkt);
23008c2ecf20Sopenharmony_ci			break;
23018c2ecf20Sopenharmony_ci		case RADEON_PACKET_TYPE2:
23028c2ecf20Sopenharmony_ci			break;
23038c2ecf20Sopenharmony_ci		case RADEON_PACKET_TYPE3:
23048c2ecf20Sopenharmony_ci			r = r600_packet3_check(p, &pkt);
23058c2ecf20Sopenharmony_ci			break;
23068c2ecf20Sopenharmony_ci		default:
23078c2ecf20Sopenharmony_ci			DRM_ERROR("Unknown packet type %d !\n", pkt.type);
23088c2ecf20Sopenharmony_ci			kfree(p->track);
23098c2ecf20Sopenharmony_ci			p->track = NULL;
23108c2ecf20Sopenharmony_ci			return -EINVAL;
23118c2ecf20Sopenharmony_ci		}
23128c2ecf20Sopenharmony_ci		if (r) {
23138c2ecf20Sopenharmony_ci			kfree(p->track);
23148c2ecf20Sopenharmony_ci			p->track = NULL;
23158c2ecf20Sopenharmony_ci			return r;
23168c2ecf20Sopenharmony_ci		}
23178c2ecf20Sopenharmony_ci	} while (p->idx < p->chunk_ib->length_dw);
23188c2ecf20Sopenharmony_ci#if 0
23198c2ecf20Sopenharmony_ci	for (r = 0; r < p->ib.length_dw; r++) {
23208c2ecf20Sopenharmony_ci		pr_info("%05d  0x%08X\n", r, p->ib.ptr[r]);
23218c2ecf20Sopenharmony_ci		mdelay(1);
23228c2ecf20Sopenharmony_ci	}
23238c2ecf20Sopenharmony_ci#endif
23248c2ecf20Sopenharmony_ci	kfree(p->track);
23258c2ecf20Sopenharmony_ci	p->track = NULL;
23268c2ecf20Sopenharmony_ci	return 0;
23278c2ecf20Sopenharmony_ci}
23288c2ecf20Sopenharmony_ci
23298c2ecf20Sopenharmony_ci/*
23308c2ecf20Sopenharmony_ci *  DMA
23318c2ecf20Sopenharmony_ci */
23328c2ecf20Sopenharmony_ci/**
23338c2ecf20Sopenharmony_ci * r600_dma_cs_next_reloc() - parse next reloc
23348c2ecf20Sopenharmony_ci * @p:		parser structure holding parsing context.
23358c2ecf20Sopenharmony_ci * @cs_reloc:		reloc informations
23368c2ecf20Sopenharmony_ci *
23378c2ecf20Sopenharmony_ci * Return the next reloc, do bo validation and compute
23388c2ecf20Sopenharmony_ci * GPU offset using the provided start.
23398c2ecf20Sopenharmony_ci **/
23408c2ecf20Sopenharmony_ciint r600_dma_cs_next_reloc(struct radeon_cs_parser *p,
23418c2ecf20Sopenharmony_ci			   struct radeon_bo_list **cs_reloc)
23428c2ecf20Sopenharmony_ci{
23438c2ecf20Sopenharmony_ci	unsigned idx;
23448c2ecf20Sopenharmony_ci
23458c2ecf20Sopenharmony_ci	*cs_reloc = NULL;
23468c2ecf20Sopenharmony_ci	if (p->chunk_relocs == NULL) {
23478c2ecf20Sopenharmony_ci		DRM_ERROR("No relocation chunk !\n");
23488c2ecf20Sopenharmony_ci		return -EINVAL;
23498c2ecf20Sopenharmony_ci	}
23508c2ecf20Sopenharmony_ci	idx = p->dma_reloc_idx;
23518c2ecf20Sopenharmony_ci	if (idx >= p->nrelocs) {
23528c2ecf20Sopenharmony_ci		DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
23538c2ecf20Sopenharmony_ci			  idx, p->nrelocs);
23548c2ecf20Sopenharmony_ci		return -EINVAL;
23558c2ecf20Sopenharmony_ci	}
23568c2ecf20Sopenharmony_ci	*cs_reloc = &p->relocs[idx];
23578c2ecf20Sopenharmony_ci	p->dma_reloc_idx++;
23588c2ecf20Sopenharmony_ci	return 0;
23598c2ecf20Sopenharmony_ci}
23608c2ecf20Sopenharmony_ci
23618c2ecf20Sopenharmony_ci#define GET_DMA_CMD(h) (((h) & 0xf0000000) >> 28)
23628c2ecf20Sopenharmony_ci#define GET_DMA_COUNT(h) ((h) & 0x0000ffff)
23638c2ecf20Sopenharmony_ci#define GET_DMA_T(h) (((h) & 0x00800000) >> 23)
23648c2ecf20Sopenharmony_ci
23658c2ecf20Sopenharmony_ci/**
23668c2ecf20Sopenharmony_ci * r600_dma_cs_parse() - parse the DMA IB
23678c2ecf20Sopenharmony_ci * @p:		parser structure holding parsing context.
23688c2ecf20Sopenharmony_ci *
23698c2ecf20Sopenharmony_ci * Parses the DMA IB from the CS ioctl and updates
23708c2ecf20Sopenharmony_ci * the GPU addresses based on the reloc information and
23718c2ecf20Sopenharmony_ci * checks for errors. (R6xx-R7xx)
23728c2ecf20Sopenharmony_ci * Returns 0 for success and an error on failure.
23738c2ecf20Sopenharmony_ci **/
23748c2ecf20Sopenharmony_ciint r600_dma_cs_parse(struct radeon_cs_parser *p)
23758c2ecf20Sopenharmony_ci{
23768c2ecf20Sopenharmony_ci	struct radeon_cs_chunk *ib_chunk = p->chunk_ib;
23778c2ecf20Sopenharmony_ci	struct radeon_bo_list *src_reloc, *dst_reloc;
23788c2ecf20Sopenharmony_ci	u32 header, cmd, count, tiled;
23798c2ecf20Sopenharmony_ci	volatile u32 *ib = p->ib.ptr;
23808c2ecf20Sopenharmony_ci	u32 idx, idx_value;
23818c2ecf20Sopenharmony_ci	u64 src_offset, dst_offset;
23828c2ecf20Sopenharmony_ci	int r;
23838c2ecf20Sopenharmony_ci
23848c2ecf20Sopenharmony_ci	do {
23858c2ecf20Sopenharmony_ci		if (p->idx >= ib_chunk->length_dw) {
23868c2ecf20Sopenharmony_ci			DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
23878c2ecf20Sopenharmony_ci				  p->idx, ib_chunk->length_dw);
23888c2ecf20Sopenharmony_ci			return -EINVAL;
23898c2ecf20Sopenharmony_ci		}
23908c2ecf20Sopenharmony_ci		idx = p->idx;
23918c2ecf20Sopenharmony_ci		header = radeon_get_ib_value(p, idx);
23928c2ecf20Sopenharmony_ci		cmd = GET_DMA_CMD(header);
23938c2ecf20Sopenharmony_ci		count = GET_DMA_COUNT(header);
23948c2ecf20Sopenharmony_ci		tiled = GET_DMA_T(header);
23958c2ecf20Sopenharmony_ci
23968c2ecf20Sopenharmony_ci		switch (cmd) {
23978c2ecf20Sopenharmony_ci		case DMA_PACKET_WRITE:
23988c2ecf20Sopenharmony_ci			r = r600_dma_cs_next_reloc(p, &dst_reloc);
23998c2ecf20Sopenharmony_ci			if (r) {
24008c2ecf20Sopenharmony_ci				DRM_ERROR("bad DMA_PACKET_WRITE\n");
24018c2ecf20Sopenharmony_ci				return -EINVAL;
24028c2ecf20Sopenharmony_ci			}
24038c2ecf20Sopenharmony_ci			if (tiled) {
24048c2ecf20Sopenharmony_ci				dst_offset = radeon_get_ib_value(p, idx+1);
24058c2ecf20Sopenharmony_ci				dst_offset <<= 8;
24068c2ecf20Sopenharmony_ci
24078c2ecf20Sopenharmony_ci				ib[idx+1] += (u32)(dst_reloc->gpu_offset >> 8);
24088c2ecf20Sopenharmony_ci				p->idx += count + 5;
24098c2ecf20Sopenharmony_ci			} else {
24108c2ecf20Sopenharmony_ci				dst_offset = radeon_get_ib_value(p, idx+1);
24118c2ecf20Sopenharmony_ci				dst_offset |= ((u64)(radeon_get_ib_value(p, idx+2) & 0xff)) << 32;
24128c2ecf20Sopenharmony_ci
24138c2ecf20Sopenharmony_ci				ib[idx+1] += (u32)(dst_reloc->gpu_offset & 0xfffffffc);
24148c2ecf20Sopenharmony_ci				ib[idx+2] += upper_32_bits(dst_reloc->gpu_offset) & 0xff;
24158c2ecf20Sopenharmony_ci				p->idx += count + 3;
24168c2ecf20Sopenharmony_ci			}
24178c2ecf20Sopenharmony_ci			if ((dst_offset + (count * 4)) > radeon_bo_size(dst_reloc->robj)) {
24188c2ecf20Sopenharmony_ci				dev_warn(p->dev, "DMA write buffer too small (%llu %lu)\n",
24198c2ecf20Sopenharmony_ci					 dst_offset + (count * 4), radeon_bo_size(dst_reloc->robj));
24208c2ecf20Sopenharmony_ci				return -EINVAL;
24218c2ecf20Sopenharmony_ci			}
24228c2ecf20Sopenharmony_ci			break;
24238c2ecf20Sopenharmony_ci		case DMA_PACKET_COPY:
24248c2ecf20Sopenharmony_ci			r = r600_dma_cs_next_reloc(p, &src_reloc);
24258c2ecf20Sopenharmony_ci			if (r) {
24268c2ecf20Sopenharmony_ci				DRM_ERROR("bad DMA_PACKET_COPY\n");
24278c2ecf20Sopenharmony_ci				return -EINVAL;
24288c2ecf20Sopenharmony_ci			}
24298c2ecf20Sopenharmony_ci			r = r600_dma_cs_next_reloc(p, &dst_reloc);
24308c2ecf20Sopenharmony_ci			if (r) {
24318c2ecf20Sopenharmony_ci				DRM_ERROR("bad DMA_PACKET_COPY\n");
24328c2ecf20Sopenharmony_ci				return -EINVAL;
24338c2ecf20Sopenharmony_ci			}
24348c2ecf20Sopenharmony_ci			if (tiled) {
24358c2ecf20Sopenharmony_ci				idx_value = radeon_get_ib_value(p, idx + 2);
24368c2ecf20Sopenharmony_ci				/* detile bit */
24378c2ecf20Sopenharmony_ci				if (idx_value & (1 << 31)) {
24388c2ecf20Sopenharmony_ci					/* tiled src, linear dst */
24398c2ecf20Sopenharmony_ci					src_offset = radeon_get_ib_value(p, idx+1);
24408c2ecf20Sopenharmony_ci					src_offset <<= 8;
24418c2ecf20Sopenharmony_ci					ib[idx+1] += (u32)(src_reloc->gpu_offset >> 8);
24428c2ecf20Sopenharmony_ci
24438c2ecf20Sopenharmony_ci					dst_offset = radeon_get_ib_value(p, idx+5);
24448c2ecf20Sopenharmony_ci					dst_offset |= ((u64)(radeon_get_ib_value(p, idx+6) & 0xff)) << 32;
24458c2ecf20Sopenharmony_ci					ib[idx+5] += (u32)(dst_reloc->gpu_offset & 0xfffffffc);
24468c2ecf20Sopenharmony_ci					ib[idx+6] += upper_32_bits(dst_reloc->gpu_offset) & 0xff;
24478c2ecf20Sopenharmony_ci				} else {
24488c2ecf20Sopenharmony_ci					/* linear src, tiled dst */
24498c2ecf20Sopenharmony_ci					src_offset = radeon_get_ib_value(p, idx+5);
24508c2ecf20Sopenharmony_ci					src_offset |= ((u64)(radeon_get_ib_value(p, idx+6) & 0xff)) << 32;
24518c2ecf20Sopenharmony_ci					ib[idx+5] += (u32)(src_reloc->gpu_offset & 0xfffffffc);
24528c2ecf20Sopenharmony_ci					ib[idx+6] += upper_32_bits(src_reloc->gpu_offset) & 0xff;
24538c2ecf20Sopenharmony_ci
24548c2ecf20Sopenharmony_ci					dst_offset = radeon_get_ib_value(p, idx+1);
24558c2ecf20Sopenharmony_ci					dst_offset <<= 8;
24568c2ecf20Sopenharmony_ci					ib[idx+1] += (u32)(dst_reloc->gpu_offset >> 8);
24578c2ecf20Sopenharmony_ci				}
24588c2ecf20Sopenharmony_ci				p->idx += 7;
24598c2ecf20Sopenharmony_ci			} else {
24608c2ecf20Sopenharmony_ci				if (p->family >= CHIP_RV770) {
24618c2ecf20Sopenharmony_ci					src_offset = radeon_get_ib_value(p, idx+2);
24628c2ecf20Sopenharmony_ci					src_offset |= ((u64)(radeon_get_ib_value(p, idx+4) & 0xff)) << 32;
24638c2ecf20Sopenharmony_ci					dst_offset = radeon_get_ib_value(p, idx+1);
24648c2ecf20Sopenharmony_ci					dst_offset |= ((u64)(radeon_get_ib_value(p, idx+3) & 0xff)) << 32;
24658c2ecf20Sopenharmony_ci
24668c2ecf20Sopenharmony_ci					ib[idx+1] += (u32)(dst_reloc->gpu_offset & 0xfffffffc);
24678c2ecf20Sopenharmony_ci					ib[idx+2] += (u32)(src_reloc->gpu_offset & 0xfffffffc);
24688c2ecf20Sopenharmony_ci					ib[idx+3] += upper_32_bits(dst_reloc->gpu_offset) & 0xff;
24698c2ecf20Sopenharmony_ci					ib[idx+4] += upper_32_bits(src_reloc->gpu_offset) & 0xff;
24708c2ecf20Sopenharmony_ci					p->idx += 5;
24718c2ecf20Sopenharmony_ci				} else {
24728c2ecf20Sopenharmony_ci					src_offset = radeon_get_ib_value(p, idx+2);
24738c2ecf20Sopenharmony_ci					src_offset |= ((u64)(radeon_get_ib_value(p, idx+3) & 0xff)) << 32;
24748c2ecf20Sopenharmony_ci					dst_offset = radeon_get_ib_value(p, idx+1);
24758c2ecf20Sopenharmony_ci					dst_offset |= ((u64)(radeon_get_ib_value(p, idx+3) & 0xff0000)) << 16;
24768c2ecf20Sopenharmony_ci
24778c2ecf20Sopenharmony_ci					ib[idx+1] += (u32)(dst_reloc->gpu_offset & 0xfffffffc);
24788c2ecf20Sopenharmony_ci					ib[idx+2] += (u32)(src_reloc->gpu_offset & 0xfffffffc);
24798c2ecf20Sopenharmony_ci					ib[idx+3] += upper_32_bits(src_reloc->gpu_offset) & 0xff;
24808c2ecf20Sopenharmony_ci					ib[idx+3] += (upper_32_bits(dst_reloc->gpu_offset) & 0xff) << 16;
24818c2ecf20Sopenharmony_ci					p->idx += 4;
24828c2ecf20Sopenharmony_ci				}
24838c2ecf20Sopenharmony_ci			}
24848c2ecf20Sopenharmony_ci			if ((src_offset + (count * 4)) > radeon_bo_size(src_reloc->robj)) {
24858c2ecf20Sopenharmony_ci				dev_warn(p->dev, "DMA copy src buffer too small (%llu %lu)\n",
24868c2ecf20Sopenharmony_ci					 src_offset + (count * 4), radeon_bo_size(src_reloc->robj));
24878c2ecf20Sopenharmony_ci				return -EINVAL;
24888c2ecf20Sopenharmony_ci			}
24898c2ecf20Sopenharmony_ci			if ((dst_offset + (count * 4)) > radeon_bo_size(dst_reloc->robj)) {
24908c2ecf20Sopenharmony_ci				dev_warn(p->dev, "DMA write dst buffer too small (%llu %lu)\n",
24918c2ecf20Sopenharmony_ci					 dst_offset + (count * 4), radeon_bo_size(dst_reloc->robj));
24928c2ecf20Sopenharmony_ci				return -EINVAL;
24938c2ecf20Sopenharmony_ci			}
24948c2ecf20Sopenharmony_ci			break;
24958c2ecf20Sopenharmony_ci		case DMA_PACKET_CONSTANT_FILL:
24968c2ecf20Sopenharmony_ci			if (p->family < CHIP_RV770) {
24978c2ecf20Sopenharmony_ci				DRM_ERROR("Constant Fill is 7xx only !\n");
24988c2ecf20Sopenharmony_ci				return -EINVAL;
24998c2ecf20Sopenharmony_ci			}
25008c2ecf20Sopenharmony_ci			r = r600_dma_cs_next_reloc(p, &dst_reloc);
25018c2ecf20Sopenharmony_ci			if (r) {
25028c2ecf20Sopenharmony_ci				DRM_ERROR("bad DMA_PACKET_WRITE\n");
25038c2ecf20Sopenharmony_ci				return -EINVAL;
25048c2ecf20Sopenharmony_ci			}
25058c2ecf20Sopenharmony_ci			dst_offset = radeon_get_ib_value(p, idx+1);
25068c2ecf20Sopenharmony_ci			dst_offset |= ((u64)(radeon_get_ib_value(p, idx+3) & 0x00ff0000)) << 16;
25078c2ecf20Sopenharmony_ci			if ((dst_offset + (count * 4)) > radeon_bo_size(dst_reloc->robj)) {
25088c2ecf20Sopenharmony_ci				dev_warn(p->dev, "DMA constant fill buffer too small (%llu %lu)\n",
25098c2ecf20Sopenharmony_ci					 dst_offset + (count * 4), radeon_bo_size(dst_reloc->robj));
25108c2ecf20Sopenharmony_ci				return -EINVAL;
25118c2ecf20Sopenharmony_ci			}
25128c2ecf20Sopenharmony_ci			ib[idx+1] += (u32)(dst_reloc->gpu_offset & 0xfffffffc);
25138c2ecf20Sopenharmony_ci			ib[idx+3] += (upper_32_bits(dst_reloc->gpu_offset) << 16) & 0x00ff0000;
25148c2ecf20Sopenharmony_ci			p->idx += 4;
25158c2ecf20Sopenharmony_ci			break;
25168c2ecf20Sopenharmony_ci		case DMA_PACKET_NOP:
25178c2ecf20Sopenharmony_ci			p->idx += 1;
25188c2ecf20Sopenharmony_ci			break;
25198c2ecf20Sopenharmony_ci		default:
25208c2ecf20Sopenharmony_ci			DRM_ERROR("Unknown packet type %d at %d !\n", cmd, idx);
25218c2ecf20Sopenharmony_ci			return -EINVAL;
25228c2ecf20Sopenharmony_ci		}
25238c2ecf20Sopenharmony_ci	} while (p->idx < p->chunk_ib->length_dw);
25248c2ecf20Sopenharmony_ci#if 0
25258c2ecf20Sopenharmony_ci	for (r = 0; r < p->ib->length_dw; r++) {
25268c2ecf20Sopenharmony_ci		pr_info("%05d  0x%08X\n", r, p->ib.ptr[r]);
25278c2ecf20Sopenharmony_ci		mdelay(1);
25288c2ecf20Sopenharmony_ci	}
25298c2ecf20Sopenharmony_ci#endif
25308c2ecf20Sopenharmony_ci	return 0;
25318c2ecf20Sopenharmony_ci}
2532