r600_cs.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/kernel.h>
  29. #include "drmP.h"
  30. #include "radeon.h"
  31. #include "r600d.h"
  32. #include "r600_reg_safe.h"
  33. static int r600_cs_packet_next_reloc_mm(struct radeon_cs_parser *p,
  34. struct radeon_cs_reloc **cs_reloc);
  35. static int r600_cs_packet_next_reloc_nomm(struct radeon_cs_parser *p,
  36. struct radeon_cs_reloc **cs_reloc);
  37. typedef int (*next_reloc_t)(struct radeon_cs_parser*, struct radeon_cs_reloc**);
  38. static next_reloc_t r600_cs_packet_next_reloc = &r600_cs_packet_next_reloc_mm;
  39. extern void r600_cs_legacy_get_tiling_conf(struct drm_device *dev, u32 *npipes, u32 *nbanks, u32 *group_size);
  40. struct r600_cs_track {
  41. /* configuration we miror so that we use same code btw kms/ums */
  42. u32 group_size;
  43. u32 nbanks;
  44. u32 npipes;
  45. /* value we track */
  46. u32 sq_config;
  47. u32 nsamples;
  48. u32 cb_color_base_last[8];
  49. struct radeon_bo *cb_color_bo[8];
  50. u32 cb_color_bo_offset[8];
  51. struct radeon_bo *cb_color_frag_bo[8];
  52. struct radeon_bo *cb_color_tile_bo[8];
  53. u32 cb_color_info[8];
  54. u32 cb_color_size_idx[8];
  55. u32 cb_target_mask;
  56. u32 cb_shader_mask;
  57. u32 cb_color_size[8];
  58. u32 vgt_strmout_en;
  59. u32 vgt_strmout_buffer_en;
  60. u32 db_depth_control;
  61. u32 db_depth_info;
  62. u32 db_depth_size_idx;
  63. u32 db_depth_view;
  64. u32 db_depth_size;
  65. u32 db_offset;
  66. struct radeon_bo *db_bo;
  67. };
  68. static inline int r600_bpe_from_format(u32 *bpe, u32 format)
  69. {
  70. switch (format) {
  71. case V_038004_COLOR_8:
  72. case V_038004_COLOR_4_4:
  73. case V_038004_COLOR_3_3_2:
  74. case V_038004_FMT_1:
  75. *bpe = 1;
  76. break;
  77. case V_038004_COLOR_16:
  78. case V_038004_COLOR_16_FLOAT:
  79. case V_038004_COLOR_8_8:
  80. case V_038004_COLOR_5_6_5:
  81. case V_038004_COLOR_6_5_5:
  82. case V_038004_COLOR_1_5_5_5:
  83. case V_038004_COLOR_4_4_4_4:
  84. case V_038004_COLOR_5_5_5_1:
  85. *bpe = 2;
  86. break;
  87. case V_038004_FMT_8_8_8:
  88. *bpe = 3;
  89. break;
  90. case V_038004_COLOR_32:
  91. case V_038004_COLOR_32_FLOAT:
  92. case V_038004_COLOR_16_16:
  93. case V_038004_COLOR_16_16_FLOAT:
  94. case V_038004_COLOR_8_24:
  95. case V_038004_COLOR_8_24_FLOAT:
  96. case V_038004_COLOR_24_8:
  97. case V_038004_COLOR_24_8_FLOAT:
  98. case V_038004_COLOR_10_11_11:
  99. case V_038004_COLOR_10_11_11_FLOAT:
  100. case V_038004_COLOR_11_11_10:
  101. case V_038004_COLOR_11_11_10_FLOAT:
  102. case V_038004_COLOR_2_10_10_10:
  103. case V_038004_COLOR_8_8_8_8:
  104. case V_038004_COLOR_10_10_10_2:
  105. case V_038004_FMT_5_9_9_9_SHAREDEXP:
  106. case V_038004_FMT_32_AS_8:
  107. case V_038004_FMT_32_AS_8_8:
  108. *bpe = 4;
  109. break;
  110. case V_038004_COLOR_X24_8_32_FLOAT:
  111. case V_038004_COLOR_32_32:
  112. case V_038004_COLOR_32_32_FLOAT:
  113. case V_038004_COLOR_16_16_16_16:
  114. case V_038004_COLOR_16_16_16_16_FLOAT:
  115. *bpe = 8;
  116. break;
  117. case V_038004_FMT_16_16_16:
  118. case V_038004_FMT_16_16_16_FLOAT:
  119. *bpe = 6;
  120. break;
  121. case V_038004_FMT_32_32_32:
  122. case V_038004_FMT_32_32_32_FLOAT:
  123. *bpe = 12;
  124. break;
  125. case V_038004_COLOR_32_32_32_32:
  126. case V_038004_COLOR_32_32_32_32_FLOAT:
  127. *bpe = 16;
  128. break;
  129. case V_038004_FMT_GB_GR:
  130. case V_038004_FMT_BG_RG:
  131. case V_038004_COLOR_INVALID:
  132. *bpe = 16;
  133. return -EINVAL;
  134. }
  135. return 0;
  136. }
  137. static void r600_cs_track_init(struct r600_cs_track *track)
  138. {
  139. int i;
  140. /* assume DX9 mode */
  141. track->sq_config = DX9_CONSTS;
  142. for (i = 0; i < 8; i++) {
  143. track->cb_color_base_last[i] = 0;
  144. track->cb_color_size[i] = 0;
  145. track->cb_color_size_idx[i] = 0;
  146. track->cb_color_info[i] = 0;
  147. track->cb_color_bo[i] = NULL;
  148. track->cb_color_bo_offset[i] = 0xFFFFFFFF;
  149. }
  150. track->cb_target_mask = 0xFFFFFFFF;
  151. track->cb_shader_mask = 0xFFFFFFFF;
  152. track->db_bo = NULL;
  153. /* assume the biggest format and that htile is enabled */
  154. track->db_depth_info = 7 | (1 << 25);
  155. track->db_depth_view = 0xFFFFC000;
  156. track->db_depth_size = 0xFFFFFFFF;
  157. track->db_depth_size_idx = 0;
  158. track->db_depth_control = 0xFFFFFFFF;
  159. }
  160. static inline int r600_cs_track_validate_cb(struct radeon_cs_parser *p, int i)
  161. {
  162. struct r600_cs_track *track = p->track;
  163. u32 bpe = 0, pitch, slice_tile_max, size, tmp, height, pitch_align;
  164. volatile u32 *ib = p->ib->ptr;
  165. if (G_0280A0_TILE_MODE(track->cb_color_info[i])) {
  166. dev_warn(p->dev, "FMASK or CMASK buffer are not supported by this kernel\n");
  167. return -EINVAL;
  168. }
  169. size = radeon_bo_size(track->cb_color_bo[i]);
  170. if (r600_bpe_from_format(&bpe, G_0280A0_FORMAT(track->cb_color_info[i]))) {
  171. dev_warn(p->dev, "%s:%d cb invalid format %d for %d (0x%08X)\n",
  172. __func__, __LINE__, G_0280A0_FORMAT(track->cb_color_info[i]),
  173. i, track->cb_color_info[i]);
  174. return -EINVAL;
  175. }
  176. /* pitch is the number of 8x8 tiles per row */
  177. pitch = G_028060_PITCH_TILE_MAX(track->cb_color_size[i]) + 1;
  178. slice_tile_max = G_028060_SLICE_TILE_MAX(track->cb_color_size[i]) + 1;
  179. height = size / (pitch * 8 * bpe);
  180. if (height > 8192)
  181. height = 8192;
  182. if (height > 7)
  183. height &= ~0x7;
  184. switch (G_0280A0_ARRAY_MODE(track->cb_color_info[i])) {
  185. case V_0280A0_ARRAY_LINEAR_GENERAL:
  186. /* technically height & 0x7 */
  187. break;
  188. case V_0280A0_ARRAY_LINEAR_ALIGNED:
  189. pitch_align = max((u32)64, (u32)(track->group_size / bpe)) / 8;
  190. if (!IS_ALIGNED(pitch, pitch_align)) {
  191. dev_warn(p->dev, "%s:%d cb pitch (%d) invalid\n",
  192. __func__, __LINE__, pitch);
  193. return -EINVAL;
  194. }
  195. if (!IS_ALIGNED(height, 8)) {
  196. dev_warn(p->dev, "%s:%d cb height (%d) invalid\n",
  197. __func__, __LINE__, height);
  198. return -EINVAL;
  199. }
  200. break;
  201. case V_0280A0_ARRAY_1D_TILED_THIN1:
  202. pitch_align = max((u32)8, (u32)(track->group_size / (8 * bpe * track->nsamples))) / 8;
  203. if (!IS_ALIGNED(pitch, pitch_align)) {
  204. dev_warn(p->dev, "%s:%d cb pitch (%d) invalid\n",
  205. __func__, __LINE__, pitch);
  206. return -EINVAL;
  207. }
  208. if (!IS_ALIGNED(height, 8)) {
  209. dev_warn(p->dev, "%s:%d cb height (%d) invalid\n",
  210. __func__, __LINE__, height);
  211. return -EINVAL;
  212. }
  213. break;
  214. case V_0280A0_ARRAY_2D_TILED_THIN1:
  215. pitch_align = max((u32)track->nbanks,
  216. (u32)(((track->group_size / 8) / (bpe * track->nsamples)) * track->nbanks));
  217. if (!IS_ALIGNED(pitch, pitch_align)) {
  218. dev_warn(p->dev, "%s:%d cb pitch (%d) invalid\n",
  219. __func__, __LINE__, pitch);
  220. return -EINVAL;
  221. }
  222. if (!IS_ALIGNED((height / 8), track->nbanks)) {
  223. dev_warn(p->dev, "%s:%d cb height (%d) invalid\n",
  224. __func__, __LINE__, height);
  225. return -EINVAL;
  226. }
  227. break;
  228. default:
  229. dev_warn(p->dev, "%s invalid tiling %d for %d (0x%08X)\n", __func__,
  230. G_0280A0_ARRAY_MODE(track->cb_color_info[i]), i,
  231. track->cb_color_info[i]);
  232. return -EINVAL;
  233. }
  234. /* check offset */
  235. tmp = height * pitch * 8 * bpe;
  236. if ((tmp + track->cb_color_bo_offset[i]) > radeon_bo_size(track->cb_color_bo[i])) {
  237. dev_warn(p->dev, "%s offset[%d] %d too big\n", __func__, i, track->cb_color_bo_offset[i]);
  238. return -EINVAL;
  239. }
  240. if (!IS_ALIGNED(track->cb_color_bo_offset[i], track->group_size)) {
  241. dev_warn(p->dev, "%s offset[%d] %d not aligned\n", __func__, i, track->cb_color_bo_offset[i]);
  242. return -EINVAL;
  243. }
  244. /* limit max tile */
  245. tmp = (height * pitch * 8) >> 6;
  246. if (tmp < slice_tile_max)
  247. slice_tile_max = tmp;
  248. tmp = S_028060_PITCH_TILE_MAX(pitch - 1) |
  249. S_028060_SLICE_TILE_MAX(slice_tile_max - 1);
  250. ib[track->cb_color_size_idx[i]] = tmp;
  251. return 0;
  252. }
  253. static int r600_cs_track_check(struct radeon_cs_parser *p)
  254. {
  255. struct r600_cs_track *track = p->track;
  256. u32 tmp;
  257. int r, i;
  258. volatile u32 *ib = p->ib->ptr;
  259. /* on legacy kernel we don't perform advanced check */
  260. if (p->rdev == NULL)
  261. return 0;
  262. /* we don't support out buffer yet */
  263. if (track->vgt_strmout_en || track->vgt_strmout_buffer_en) {
  264. dev_warn(p->dev, "this kernel doesn't support SMX output buffer\n");
  265. return -EINVAL;
  266. }
  267. /* check that we have a cb for each enabled target, we don't check
  268. * shader_mask because it seems mesa isn't always setting it :(
  269. */
  270. tmp = track->cb_target_mask;
  271. for (i = 0; i < 8; i++) {
  272. if ((tmp >> (i * 4)) & 0xF) {
  273. /* at least one component is enabled */
  274. if (track->cb_color_bo[i] == NULL) {
  275. dev_warn(p->dev, "%s:%d mask 0x%08X | 0x%08X no cb for %d\n",
  276. __func__, __LINE__, track->cb_target_mask, track->cb_shader_mask, i);
  277. return -EINVAL;
  278. }
  279. /* perform rewrite of CB_COLOR[0-7]_SIZE */
  280. r = r600_cs_track_validate_cb(p, i);
  281. if (r)
  282. return r;
  283. }
  284. }
  285. /* Check depth buffer */
  286. if (G_028800_STENCIL_ENABLE(track->db_depth_control) ||
  287. G_028800_Z_ENABLE(track->db_depth_control)) {
  288. u32 nviews, bpe, ntiles, pitch, pitch_align, height, size;
  289. if (track->db_bo == NULL) {
  290. dev_warn(p->dev, "z/stencil with no depth buffer\n");
  291. return -EINVAL;
  292. }
  293. if (G_028010_TILE_SURFACE_ENABLE(track->db_depth_info)) {
  294. dev_warn(p->dev, "this kernel doesn't support z/stencil htile\n");
  295. return -EINVAL;
  296. }
  297. switch (G_028010_FORMAT(track->db_depth_info)) {
  298. case V_028010_DEPTH_16:
  299. bpe = 2;
  300. break;
  301. case V_028010_DEPTH_X8_24:
  302. case V_028010_DEPTH_8_24:
  303. case V_028010_DEPTH_X8_24_FLOAT:
  304. case V_028010_DEPTH_8_24_FLOAT:
  305. case V_028010_DEPTH_32_FLOAT:
  306. bpe = 4;
  307. break;
  308. case V_028010_DEPTH_X24_8_32_FLOAT:
  309. bpe = 8;
  310. break;
  311. default:
  312. dev_warn(p->dev, "z/stencil with invalid format %d\n", G_028010_FORMAT(track->db_depth_info));
  313. return -EINVAL;
  314. }
  315. if ((track->db_depth_size & 0xFFFFFC00) == 0xFFFFFC00) {
  316. if (!track->db_depth_size_idx) {
  317. dev_warn(p->dev, "z/stencil buffer size not set\n");
  318. return -EINVAL;
  319. }
  320. printk_once(KERN_WARNING "You have old & broken userspace please consider updating mesa\n");
  321. tmp = radeon_bo_size(track->db_bo) - track->db_offset;
  322. tmp = (tmp / bpe) >> 6;
  323. if (!tmp) {
  324. dev_warn(p->dev, "z/stencil buffer too small (0x%08X %d %d %ld)\n",
  325. track->db_depth_size, bpe, track->db_offset,
  326. radeon_bo_size(track->db_bo));
  327. return -EINVAL;
  328. }
  329. ib[track->db_depth_size_idx] = S_028000_SLICE_TILE_MAX(tmp - 1) | (track->db_depth_size & 0x3FF);
  330. } else {
  331. size = radeon_bo_size(track->db_bo);
  332. pitch = G_028000_PITCH_TILE_MAX(track->db_depth_size) + 1;
  333. height = size / (pitch * 8 * bpe);
  334. height &= ~0x7;
  335. if (!height)
  336. height = 8;
  337. switch (G_028010_ARRAY_MODE(track->db_depth_info)) {
  338. case V_028010_ARRAY_1D_TILED_THIN1:
  339. pitch_align = (max((u32)8, (u32)(track->group_size / (8 * bpe))) / 8);
  340. if (!IS_ALIGNED(pitch, pitch_align)) {
  341. dev_warn(p->dev, "%s:%d db pitch (%d) invalid\n",
  342. __func__, __LINE__, pitch);
  343. return -EINVAL;
  344. }
  345. if (!IS_ALIGNED(height, 8)) {
  346. dev_warn(p->dev, "%s:%d db height (%d) invalid\n",
  347. __func__, __LINE__, height);
  348. return -EINVAL;
  349. }
  350. break;
  351. case V_028010_ARRAY_2D_TILED_THIN1:
  352. pitch_align = max((u32)track->nbanks,
  353. (u32)(((track->group_size / 8) / bpe) * track->nbanks));
  354. if (!IS_ALIGNED(pitch, pitch_align)) {
  355. dev_warn(p->dev, "%s:%d db pitch (%d) invalid\n",
  356. __func__, __LINE__, pitch);
  357. return -EINVAL;
  358. }
  359. if ((height / 8) & (track->nbanks - 1)) {
  360. dev_warn(p->dev, "%s:%d db height (%d) invalid\n",
  361. __func__, __LINE__, height);
  362. return -EINVAL;
  363. }
  364. break;
  365. default:
  366. dev_warn(p->dev, "%s invalid tiling %d (0x%08X)\n", __func__,
  367. G_028010_ARRAY_MODE(track->db_depth_info),
  368. track->db_depth_info);
  369. return -EINVAL;
  370. }
  371. if (!IS_ALIGNED(track->db_offset, track->group_size)) {
  372. dev_warn(p->dev, "%s offset[%d] %d not aligned\n", __func__, i, track->db_offset);
  373. return -EINVAL;
  374. }
  375. ntiles = G_028000_SLICE_TILE_MAX(track->db_depth_size) + 1;
  376. nviews = G_028004_SLICE_MAX(track->db_depth_view) + 1;
  377. tmp = ntiles * bpe * 64 * nviews;
  378. if ((tmp + track->db_offset) > radeon_bo_size(track->db_bo)) {
  379. dev_warn(p->dev, "z/stencil buffer too small (0x%08X %d %d %d -> %d have %ld)\n",
  380. track->db_depth_size, ntiles, nviews, bpe, tmp + track->db_offset,
  381. radeon_bo_size(track->db_bo));
  382. return -EINVAL;
  383. }
  384. }
  385. }
  386. return 0;
  387. }
  388. /**
  389. * r600_cs_packet_parse() - parse cp packet and point ib index to next packet
  390. * @parser: parser structure holding parsing context.
  391. * @pkt: where to store packet informations
  392. *
  393. * Assume that chunk_ib_index is properly set. Will return -EINVAL
  394. * if packet is bigger than remaining ib size. or if packets is unknown.
  395. **/
  396. int r600_cs_packet_parse(struct radeon_cs_parser *p,
  397. struct radeon_cs_packet *pkt,
  398. unsigned idx)
  399. {
  400. struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
  401. uint32_t header;
  402. if (idx >= ib_chunk->length_dw) {
  403. DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
  404. idx, ib_chunk->length_dw);
  405. return -EINVAL;
  406. }
  407. header = radeon_get_ib_value(p, idx);
  408. pkt->idx = idx;
  409. pkt->type = CP_PACKET_GET_TYPE(header);
  410. pkt->count = CP_PACKET_GET_COUNT(header);
  411. pkt->one_reg_wr = 0;
  412. switch (pkt->type) {
  413. case PACKET_TYPE0:
  414. pkt->reg = CP_PACKET0_GET_REG(header);
  415. break;
  416. case PACKET_TYPE3:
  417. pkt->opcode = CP_PACKET3_GET_OPCODE(header);
  418. break;
  419. case PACKET_TYPE2:
  420. pkt->count = -1;
  421. break;
  422. default:
  423. DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
  424. return -EINVAL;
  425. }
  426. if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
  427. DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
  428. pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
  429. return -EINVAL;
  430. }
  431. return 0;
  432. }
  433. /**
  434. * r600_cs_packet_next_reloc_mm() - parse next packet which should be reloc packet3
  435. * @parser: parser structure holding parsing context.
  436. * @data: pointer to relocation data
  437. * @offset_start: starting offset
  438. * @offset_mask: offset mask (to align start offset on)
  439. * @reloc: reloc informations
  440. *
  441. * Check next packet is relocation packet3, do bo validation and compute
  442. * GPU offset using the provided start.
  443. **/
  444. static int r600_cs_packet_next_reloc_mm(struct radeon_cs_parser *p,
  445. struct radeon_cs_reloc **cs_reloc)
  446. {
  447. struct radeon_cs_chunk *relocs_chunk;
  448. struct radeon_cs_packet p3reloc;
  449. unsigned idx;
  450. int r;
  451. if (p->chunk_relocs_idx == -1) {
  452. DRM_ERROR("No relocation chunk !\n");
  453. return -EINVAL;
  454. }
  455. *cs_reloc = NULL;
  456. relocs_chunk = &p->chunks[p->chunk_relocs_idx];
  457. r = r600_cs_packet_parse(p, &p3reloc, p->idx);
  458. if (r) {
  459. return r;
  460. }
  461. p->idx += p3reloc.count + 2;
  462. if (p3reloc.type != PACKET_TYPE3 || p3reloc.opcode != PACKET3_NOP) {
  463. DRM_ERROR("No packet3 for relocation for packet at %d.\n",
  464. p3reloc.idx);
  465. return -EINVAL;
  466. }
  467. idx = radeon_get_ib_value(p, p3reloc.idx + 1);
  468. if (idx >= relocs_chunk->length_dw) {
  469. DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
  470. idx, relocs_chunk->length_dw);
  471. return -EINVAL;
  472. }
  473. /* FIXME: we assume reloc size is 4 dwords */
  474. *cs_reloc = p->relocs_ptr[(idx / 4)];
  475. return 0;
  476. }
  477. /**
  478. * r600_cs_packet_next_reloc_nomm() - parse next packet which should be reloc packet3
  479. * @parser: parser structure holding parsing context.
  480. * @data: pointer to relocation data
  481. * @offset_start: starting offset
  482. * @offset_mask: offset mask (to align start offset on)
  483. * @reloc: reloc informations
  484. *
  485. * Check next packet is relocation packet3, do bo validation and compute
  486. * GPU offset using the provided start.
  487. **/
  488. static int r600_cs_packet_next_reloc_nomm(struct radeon_cs_parser *p,
  489. struct radeon_cs_reloc **cs_reloc)
  490. {
  491. struct radeon_cs_chunk *relocs_chunk;
  492. struct radeon_cs_packet p3reloc;
  493. unsigned idx;
  494. int r;
  495. if (p->chunk_relocs_idx == -1) {
  496. DRM_ERROR("No relocation chunk !\n");
  497. return -EINVAL;
  498. }
  499. *cs_reloc = NULL;
  500. relocs_chunk = &p->chunks[p->chunk_relocs_idx];
  501. r = r600_cs_packet_parse(p, &p3reloc, p->idx);
  502. if (r) {
  503. return r;
  504. }
  505. p->idx += p3reloc.count + 2;
  506. if (p3reloc.type != PACKET_TYPE3 || p3reloc.opcode != PACKET3_NOP) {
  507. DRM_ERROR("No packet3 for relocation for packet at %d.\n",
  508. p3reloc.idx);
  509. return -EINVAL;
  510. }
  511. idx = radeon_get_ib_value(p, p3reloc.idx + 1);
  512. if (idx >= relocs_chunk->length_dw) {
  513. DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
  514. idx, relocs_chunk->length_dw);
  515. return -EINVAL;
  516. }
  517. *cs_reloc = p->relocs;
  518. (*cs_reloc)->lobj.gpu_offset = (u64)relocs_chunk->kdata[idx + 3] << 32;
  519. (*cs_reloc)->lobj.gpu_offset |= relocs_chunk->kdata[idx + 0];
  520. return 0;
  521. }
  522. /**
  523. * r600_cs_packet_next_is_pkt3_nop() - test if next packet is packet3 nop for reloc
  524. * @parser: parser structure holding parsing context.
  525. *
  526. * Check next packet is relocation packet3, do bo validation and compute
  527. * GPU offset using the provided start.
  528. **/
  529. static inline int r600_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p)
  530. {
  531. struct radeon_cs_packet p3reloc;
  532. int r;
  533. r = r600_cs_packet_parse(p, &p3reloc, p->idx);
  534. if (r) {
  535. return 0;
  536. }
  537. if (p3reloc.type != PACKET_TYPE3 || p3reloc.opcode != PACKET3_NOP) {
  538. return 0;
  539. }
  540. return 1;
  541. }
  542. /**
  543. * r600_cs_packet_next_vline() - parse userspace VLINE packet
  544. * @parser: parser structure holding parsing context.
  545. *
  546. * Userspace sends a special sequence for VLINE waits.
  547. * PACKET0 - VLINE_START_END + value
  548. * PACKET3 - WAIT_REG_MEM poll vline status reg
  549. * RELOC (P3) - crtc_id in reloc.
  550. *
  551. * This function parses this and relocates the VLINE START END
  552. * and WAIT_REG_MEM packets to the correct crtc.
  553. * It also detects a switched off crtc and nulls out the
  554. * wait in that case.
  555. */
  556. static int r600_cs_packet_parse_vline(struct radeon_cs_parser *p)
  557. {
  558. struct drm_mode_object *obj;
  559. struct drm_crtc *crtc;
  560. struct radeon_crtc *radeon_crtc;
  561. struct radeon_cs_packet p3reloc, wait_reg_mem;
  562. int crtc_id;
  563. int r;
  564. uint32_t header, h_idx, reg, wait_reg_mem_info;
  565. volatile uint32_t *ib;
  566. ib = p->ib->ptr;
  567. /* parse the WAIT_REG_MEM */
  568. r = r600_cs_packet_parse(p, &wait_reg_mem, p->idx);
  569. if (r)
  570. return r;
  571. /* check its a WAIT_REG_MEM */
  572. if (wait_reg_mem.type != PACKET_TYPE3 ||
  573. wait_reg_mem.opcode != PACKET3_WAIT_REG_MEM) {
  574. DRM_ERROR("vline wait missing WAIT_REG_MEM segment\n");
  575. r = -EINVAL;
  576. return r;
  577. }
  578. wait_reg_mem_info = radeon_get_ib_value(p, wait_reg_mem.idx + 1);
  579. /* bit 4 is reg (0) or mem (1) */
  580. if (wait_reg_mem_info & 0x10) {
  581. DRM_ERROR("vline WAIT_REG_MEM waiting on MEM rather than REG\n");
  582. r = -EINVAL;
  583. return r;
  584. }
  585. /* waiting for value to be equal */
  586. if ((wait_reg_mem_info & 0x7) != 0x3) {
  587. DRM_ERROR("vline WAIT_REG_MEM function not equal\n");
  588. r = -EINVAL;
  589. return r;
  590. }
  591. if ((radeon_get_ib_value(p, wait_reg_mem.idx + 2) << 2) != AVIVO_D1MODE_VLINE_STATUS) {
  592. DRM_ERROR("vline WAIT_REG_MEM bad reg\n");
  593. r = -EINVAL;
  594. return r;
  595. }
  596. if (radeon_get_ib_value(p, wait_reg_mem.idx + 5) != AVIVO_D1MODE_VLINE_STAT) {
  597. DRM_ERROR("vline WAIT_REG_MEM bad bit mask\n");
  598. r = -EINVAL;
  599. return r;
  600. }
  601. /* jump over the NOP */
  602. r = r600_cs_packet_parse(p, &p3reloc, p->idx + wait_reg_mem.count + 2);
  603. if (r)
  604. return r;
  605. h_idx = p->idx - 2;
  606. p->idx += wait_reg_mem.count + 2;
  607. p->idx += p3reloc.count + 2;
  608. header = radeon_get_ib_value(p, h_idx);
  609. crtc_id = radeon_get_ib_value(p, h_idx + 2 + 7 + 1);
  610. reg = CP_PACKET0_GET_REG(header);
  611. mutex_lock(&p->rdev->ddev->mode_config.mutex);
  612. obj = drm_mode_object_find(p->rdev->ddev, crtc_id, DRM_MODE_OBJECT_CRTC);
  613. if (!obj) {
  614. DRM_ERROR("cannot find crtc %d\n", crtc_id);
  615. r = -EINVAL;
  616. goto out;
  617. }
  618. crtc = obj_to_crtc(obj);
  619. radeon_crtc = to_radeon_crtc(crtc);
  620. crtc_id = radeon_crtc->crtc_id;
  621. if (!crtc->enabled) {
  622. /* if the CRTC isn't enabled - we need to nop out the WAIT_REG_MEM */
  623. ib[h_idx + 2] = PACKET2(0);
  624. ib[h_idx + 3] = PACKET2(0);
  625. ib[h_idx + 4] = PACKET2(0);
  626. ib[h_idx + 5] = PACKET2(0);
  627. ib[h_idx + 6] = PACKET2(0);
  628. ib[h_idx + 7] = PACKET2(0);
  629. ib[h_idx + 8] = PACKET2(0);
  630. } else if (crtc_id == 1) {
  631. switch (reg) {
  632. case AVIVO_D1MODE_VLINE_START_END:
  633. header &= ~R600_CP_PACKET0_REG_MASK;
  634. header |= AVIVO_D2MODE_VLINE_START_END >> 2;
  635. break;
  636. default:
  637. DRM_ERROR("unknown crtc reloc\n");
  638. r = -EINVAL;
  639. goto out;
  640. }
  641. ib[h_idx] = header;
  642. ib[h_idx + 4] = AVIVO_D2MODE_VLINE_STATUS >> 2;
  643. }
  644. out:
  645. mutex_unlock(&p->rdev->ddev->mode_config.mutex);
  646. return r;
  647. }
  648. static int r600_packet0_check(struct radeon_cs_parser *p,
  649. struct radeon_cs_packet *pkt,
  650. unsigned idx, unsigned reg)
  651. {
  652. int r;
  653. switch (reg) {
  654. case AVIVO_D1MODE_VLINE_START_END:
  655. r = r600_cs_packet_parse_vline(p);
  656. if (r) {
  657. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  658. idx, reg);
  659. return r;
  660. }
  661. break;
  662. default:
  663. printk(KERN_ERR "Forbidden register 0x%04X in cs at %d\n",
  664. reg, idx);
  665. return -EINVAL;
  666. }
  667. return 0;
  668. }
  669. static int r600_cs_parse_packet0(struct radeon_cs_parser *p,
  670. struct radeon_cs_packet *pkt)
  671. {
  672. unsigned reg, i;
  673. unsigned idx;
  674. int r;
  675. idx = pkt->idx + 1;
  676. reg = pkt->reg;
  677. for (i = 0; i <= pkt->count; i++, idx++, reg += 4) {
  678. r = r600_packet0_check(p, pkt, idx, reg);
  679. if (r) {
  680. return r;
  681. }
  682. }
  683. return 0;
  684. }
  685. /**
  686. * r600_cs_check_reg() - check if register is authorized or not
  687. * @parser: parser structure holding parsing context
  688. * @reg: register we are testing
  689. * @idx: index into the cs buffer
  690. *
  691. * This function will test against r600_reg_safe_bm and return 0
  692. * if register is safe. If register is not flag as safe this function
  693. * will test it against a list of register needind special handling.
  694. */
  695. static inline int r600_cs_check_reg(struct radeon_cs_parser *p, u32 reg, u32 idx)
  696. {
  697. struct r600_cs_track *track = (struct r600_cs_track *)p->track;
  698. struct radeon_cs_reloc *reloc;
  699. u32 last_reg = ARRAY_SIZE(r600_reg_safe_bm);
  700. u32 m, i, tmp, *ib;
  701. int r;
  702. i = (reg >> 7);
  703. if (i > last_reg) {
  704. dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
  705. return -EINVAL;
  706. }
  707. m = 1 << ((reg >> 2) & 31);
  708. if (!(r600_reg_safe_bm[i] & m))
  709. return 0;
  710. ib = p->ib->ptr;
  711. switch (reg) {
  712. /* force following reg to 0 in an attemp to disable out buffer
  713. * which will need us to better understand how it works to perform
  714. * security check on it (Jerome)
  715. */
  716. case R_0288A8_SQ_ESGS_RING_ITEMSIZE:
  717. case R_008C44_SQ_ESGS_RING_SIZE:
  718. case R_0288B0_SQ_ESTMP_RING_ITEMSIZE:
  719. case R_008C54_SQ_ESTMP_RING_SIZE:
  720. case R_0288C0_SQ_FBUF_RING_ITEMSIZE:
  721. case R_008C74_SQ_FBUF_RING_SIZE:
  722. case R_0288B4_SQ_GSTMP_RING_ITEMSIZE:
  723. case R_008C5C_SQ_GSTMP_RING_SIZE:
  724. case R_0288AC_SQ_GSVS_RING_ITEMSIZE:
  725. case R_008C4C_SQ_GSVS_RING_SIZE:
  726. case R_0288BC_SQ_PSTMP_RING_ITEMSIZE:
  727. case R_008C6C_SQ_PSTMP_RING_SIZE:
  728. case R_0288C4_SQ_REDUC_RING_ITEMSIZE:
  729. case R_008C7C_SQ_REDUC_RING_SIZE:
  730. case R_0288B8_SQ_VSTMP_RING_ITEMSIZE:
  731. case R_008C64_SQ_VSTMP_RING_SIZE:
  732. case R_0288C8_SQ_GS_VERT_ITEMSIZE:
  733. /* get value to populate the IB don't remove */
  734. tmp =radeon_get_ib_value(p, idx);
  735. ib[idx] = 0;
  736. break;
  737. case SQ_CONFIG:
  738. track->sq_config = radeon_get_ib_value(p, idx);
  739. break;
  740. case R_028800_DB_DEPTH_CONTROL:
  741. track->db_depth_control = radeon_get_ib_value(p, idx);
  742. break;
  743. case R_028010_DB_DEPTH_INFO:
  744. if (r600_cs_packet_next_is_pkt3_nop(p)) {
  745. r = r600_cs_packet_next_reloc(p, &reloc);
  746. if (r) {
  747. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  748. "0x%04X\n", reg);
  749. return -EINVAL;
  750. }
  751. track->db_depth_info = radeon_get_ib_value(p, idx);
  752. ib[idx] &= C_028010_ARRAY_MODE;
  753. track->db_depth_info &= C_028010_ARRAY_MODE;
  754. if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO) {
  755. ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
  756. track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
  757. } else {
  758. ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
  759. track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
  760. }
  761. } else
  762. track->db_depth_info = radeon_get_ib_value(p, idx);
  763. break;
  764. case R_028004_DB_DEPTH_VIEW:
  765. track->db_depth_view = radeon_get_ib_value(p, idx);
  766. break;
  767. case R_028000_DB_DEPTH_SIZE:
  768. track->db_depth_size = radeon_get_ib_value(p, idx);
  769. track->db_depth_size_idx = idx;
  770. break;
  771. case R_028AB0_VGT_STRMOUT_EN:
  772. track->vgt_strmout_en = radeon_get_ib_value(p, idx);
  773. break;
  774. case R_028B20_VGT_STRMOUT_BUFFER_EN:
  775. track->vgt_strmout_buffer_en = radeon_get_ib_value(p, idx);
  776. break;
  777. case R_028238_CB_TARGET_MASK:
  778. track->cb_target_mask = radeon_get_ib_value(p, idx);
  779. break;
  780. case R_02823C_CB_SHADER_MASK:
  781. track->cb_shader_mask = radeon_get_ib_value(p, idx);
  782. break;
  783. case R_028C04_PA_SC_AA_CONFIG:
  784. tmp = G_028C04_MSAA_NUM_SAMPLES(radeon_get_ib_value(p, idx));
  785. track->nsamples = 1 << tmp;
  786. break;
  787. case R_0280A0_CB_COLOR0_INFO:
  788. case R_0280A4_CB_COLOR1_INFO:
  789. case R_0280A8_CB_COLOR2_INFO:
  790. case R_0280AC_CB_COLOR3_INFO:
  791. case R_0280B0_CB_COLOR4_INFO:
  792. case R_0280B4_CB_COLOR5_INFO:
  793. case R_0280B8_CB_COLOR6_INFO:
  794. case R_0280BC_CB_COLOR7_INFO:
  795. if (r600_cs_packet_next_is_pkt3_nop(p)) {
  796. r = r600_cs_packet_next_reloc(p, &reloc);
  797. if (r) {
  798. dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
  799. return -EINVAL;
  800. }
  801. tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
  802. track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
  803. if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO) {
  804. ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
  805. track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
  806. } else if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO) {
  807. ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
  808. track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
  809. }
  810. } else {
  811. tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
  812. track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
  813. }
  814. break;
  815. case R_028060_CB_COLOR0_SIZE:
  816. case R_028064_CB_COLOR1_SIZE:
  817. case R_028068_CB_COLOR2_SIZE:
  818. case R_02806C_CB_COLOR3_SIZE:
  819. case R_028070_CB_COLOR4_SIZE:
  820. case R_028074_CB_COLOR5_SIZE:
  821. case R_028078_CB_COLOR6_SIZE:
  822. case R_02807C_CB_COLOR7_SIZE:
  823. tmp = (reg - R_028060_CB_COLOR0_SIZE) / 4;
  824. track->cb_color_size[tmp] = radeon_get_ib_value(p, idx);
  825. track->cb_color_size_idx[tmp] = idx;
  826. break;
  827. /* This register were added late, there is userspace
  828. * which does provide relocation for those but set
  829. * 0 offset. In order to avoid breaking old userspace
  830. * we detect this and set address to point to last
  831. * CB_COLOR0_BASE, note that if userspace doesn't set
  832. * CB_COLOR0_BASE before this register we will report
  833. * error. Old userspace always set CB_COLOR0_BASE
  834. * before any of this.
  835. */
  836. case R_0280E0_CB_COLOR0_FRAG:
  837. case R_0280E4_CB_COLOR1_FRAG:
  838. case R_0280E8_CB_COLOR2_FRAG:
  839. case R_0280EC_CB_COLOR3_FRAG:
  840. case R_0280F0_CB_COLOR4_FRAG:
  841. case R_0280F4_CB_COLOR5_FRAG:
  842. case R_0280F8_CB_COLOR6_FRAG:
  843. case R_0280FC_CB_COLOR7_FRAG:
  844. tmp = (reg - R_0280E0_CB_COLOR0_FRAG) / 4;
  845. if (!r600_cs_packet_next_is_pkt3_nop(p)) {
  846. if (!track->cb_color_base_last[tmp]) {
  847. dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
  848. return -EINVAL;
  849. }
  850. ib[idx] = track->cb_color_base_last[tmp];
  851. printk_once(KERN_WARNING "You have old & broken userspace "
  852. "please consider updating mesa & xf86-video-ati\n");
  853. track->cb_color_frag_bo[tmp] = track->cb_color_bo[tmp];
  854. } else {
  855. r = r600_cs_packet_next_reloc(p, &reloc);
  856. if (r) {
  857. dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
  858. return -EINVAL;
  859. }
  860. ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  861. track->cb_color_frag_bo[tmp] = reloc->robj;
  862. }
  863. break;
  864. case R_0280C0_CB_COLOR0_TILE:
  865. case R_0280C4_CB_COLOR1_TILE:
  866. case R_0280C8_CB_COLOR2_TILE:
  867. case R_0280CC_CB_COLOR3_TILE:
  868. case R_0280D0_CB_COLOR4_TILE:
  869. case R_0280D4_CB_COLOR5_TILE:
  870. case R_0280D8_CB_COLOR6_TILE:
  871. case R_0280DC_CB_COLOR7_TILE:
  872. tmp = (reg - R_0280C0_CB_COLOR0_TILE) / 4;
  873. if (!r600_cs_packet_next_is_pkt3_nop(p)) {
  874. if (!track->cb_color_base_last[tmp]) {
  875. dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
  876. return -EINVAL;
  877. }
  878. ib[idx] = track->cb_color_base_last[tmp];
  879. printk_once(KERN_WARNING "You have old & broken userspace "
  880. "please consider updating mesa & xf86-video-ati\n");
  881. track->cb_color_tile_bo[tmp] = track->cb_color_bo[tmp];
  882. } else {
  883. r = r600_cs_packet_next_reloc(p, &reloc);
  884. if (r) {
  885. dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
  886. return -EINVAL;
  887. }
  888. ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  889. track->cb_color_tile_bo[tmp] = reloc->robj;
  890. }
  891. break;
  892. case CB_COLOR0_BASE:
  893. case CB_COLOR1_BASE:
  894. case CB_COLOR2_BASE:
  895. case CB_COLOR3_BASE:
  896. case CB_COLOR4_BASE:
  897. case CB_COLOR5_BASE:
  898. case CB_COLOR6_BASE:
  899. case CB_COLOR7_BASE:
  900. r = r600_cs_packet_next_reloc(p, &reloc);
  901. if (r) {
  902. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  903. "0x%04X\n", reg);
  904. return -EINVAL;
  905. }
  906. tmp = (reg - CB_COLOR0_BASE) / 4;
  907. track->cb_color_bo_offset[tmp] = radeon_get_ib_value(p, idx);
  908. ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  909. track->cb_color_base_last[tmp] = ib[idx];
  910. track->cb_color_bo[tmp] = reloc->robj;
  911. break;
  912. case DB_DEPTH_BASE:
  913. r = r600_cs_packet_next_reloc(p, &reloc);
  914. if (r) {
  915. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  916. "0x%04X\n", reg);
  917. return -EINVAL;
  918. }
  919. track->db_offset = radeon_get_ib_value(p, idx);
  920. ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  921. track->db_bo = reloc->robj;
  922. break;
  923. case DB_HTILE_DATA_BASE:
  924. case SQ_PGM_START_FS:
  925. case SQ_PGM_START_ES:
  926. case SQ_PGM_START_VS:
  927. case SQ_PGM_START_GS:
  928. case SQ_PGM_START_PS:
  929. case SQ_ALU_CONST_CACHE_GS_0:
  930. case SQ_ALU_CONST_CACHE_GS_1:
  931. case SQ_ALU_CONST_CACHE_GS_2:
  932. case SQ_ALU_CONST_CACHE_GS_3:
  933. case SQ_ALU_CONST_CACHE_GS_4:
  934. case SQ_ALU_CONST_CACHE_GS_5:
  935. case SQ_ALU_CONST_CACHE_GS_6:
  936. case SQ_ALU_CONST_CACHE_GS_7:
  937. case SQ_ALU_CONST_CACHE_GS_8:
  938. case SQ_ALU_CONST_CACHE_GS_9:
  939. case SQ_ALU_CONST_CACHE_GS_10:
  940. case SQ_ALU_CONST_CACHE_GS_11:
  941. case SQ_ALU_CONST_CACHE_GS_12:
  942. case SQ_ALU_CONST_CACHE_GS_13:
  943. case SQ_ALU_CONST_CACHE_GS_14:
  944. case SQ_ALU_CONST_CACHE_GS_15:
  945. case SQ_ALU_CONST_CACHE_PS_0:
  946. case SQ_ALU_CONST_CACHE_PS_1:
  947. case SQ_ALU_CONST_CACHE_PS_2:
  948. case SQ_ALU_CONST_CACHE_PS_3:
  949. case SQ_ALU_CONST_CACHE_PS_4:
  950. case SQ_ALU_CONST_CACHE_PS_5:
  951. case SQ_ALU_CONST_CACHE_PS_6:
  952. case SQ_ALU_CONST_CACHE_PS_7:
  953. case SQ_ALU_CONST_CACHE_PS_8:
  954. case SQ_ALU_CONST_CACHE_PS_9:
  955. case SQ_ALU_CONST_CACHE_PS_10:
  956. case SQ_ALU_CONST_CACHE_PS_11:
  957. case SQ_ALU_CONST_CACHE_PS_12:
  958. case SQ_ALU_CONST_CACHE_PS_13:
  959. case SQ_ALU_CONST_CACHE_PS_14:
  960. case SQ_ALU_CONST_CACHE_PS_15:
  961. case SQ_ALU_CONST_CACHE_VS_0:
  962. case SQ_ALU_CONST_CACHE_VS_1:
  963. case SQ_ALU_CONST_CACHE_VS_2:
  964. case SQ_ALU_CONST_CACHE_VS_3:
  965. case SQ_ALU_CONST_CACHE_VS_4:
  966. case SQ_ALU_CONST_CACHE_VS_5:
  967. case SQ_ALU_CONST_CACHE_VS_6:
  968. case SQ_ALU_CONST_CACHE_VS_7:
  969. case SQ_ALU_CONST_CACHE_VS_8:
  970. case SQ_ALU_CONST_CACHE_VS_9:
  971. case SQ_ALU_CONST_CACHE_VS_10:
  972. case SQ_ALU_CONST_CACHE_VS_11:
  973. case SQ_ALU_CONST_CACHE_VS_12:
  974. case SQ_ALU_CONST_CACHE_VS_13:
  975. case SQ_ALU_CONST_CACHE_VS_14:
  976. case SQ_ALU_CONST_CACHE_VS_15:
  977. r = r600_cs_packet_next_reloc(p, &reloc);
  978. if (r) {
  979. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  980. "0x%04X\n", reg);
  981. return -EINVAL;
  982. }
  983. ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  984. break;
  985. default:
  986. dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
  987. return -EINVAL;
  988. }
  989. return 0;
  990. }
  991. static inline unsigned minify(unsigned size, unsigned levels)
  992. {
  993. size = size >> levels;
  994. if (size < 1)
  995. size = 1;
  996. return size;
  997. }
  998. static void r600_texture_size(unsigned nfaces, unsigned blevel, unsigned nlevels,
  999. unsigned w0, unsigned h0, unsigned d0, unsigned bpe,
  1000. unsigned pitch_align,
  1001. unsigned *l0_size, unsigned *mipmap_size)
  1002. {
  1003. unsigned offset, i, level, face;
  1004. unsigned width, height, depth, rowstride, size;
  1005. w0 = minify(w0, 0);
  1006. h0 = minify(h0, 0);
  1007. d0 = minify(d0, 0);
  1008. for(i = 0, offset = 0, level = blevel; i < nlevels; i++, level++) {
  1009. width = minify(w0, i);
  1010. height = minify(h0, i);
  1011. depth = minify(d0, i);
  1012. for(face = 0; face < nfaces; face++) {
  1013. rowstride = ALIGN((width * bpe), pitch_align);
  1014. size = height * rowstride * depth;
  1015. offset += size;
  1016. offset = (offset + 0x1f) & ~0x1f;
  1017. }
  1018. }
  1019. *l0_size = ALIGN((w0 * bpe), pitch_align) * h0 * d0;
  1020. *mipmap_size = offset;
  1021. if (!blevel)
  1022. *mipmap_size -= *l0_size;
  1023. if (!nlevels)
  1024. *mipmap_size = *l0_size;
  1025. }
  1026. /**
  1027. * r600_check_texture_resource() - check if register is authorized or not
  1028. * @p: parser structure holding parsing context
  1029. * @idx: index into the cs buffer
  1030. * @texture: texture's bo structure
  1031. * @mipmap: mipmap's bo structure
  1032. *
  1033. * This function will check that the resource has valid field and that
  1034. * the texture and mipmap bo object are big enough to cover this resource.
  1035. */
  1036. static inline int r600_check_texture_resource(struct radeon_cs_parser *p, u32 idx,
  1037. struct radeon_bo *texture,
  1038. struct radeon_bo *mipmap,
  1039. u32 tiling_flags)
  1040. {
  1041. struct r600_cs_track *track = p->track;
  1042. u32 nfaces, nlevels, blevel, w0, h0, d0, bpe = 0;
  1043. u32 word0, word1, l0_size, mipmap_size, pitch, pitch_align;
  1044. /* on legacy kernel we don't perform advanced check */
  1045. if (p->rdev == NULL)
  1046. return 0;
  1047. word0 = radeon_get_ib_value(p, idx + 0);
  1048. if (tiling_flags & RADEON_TILING_MACRO)
  1049. word0 |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
  1050. else if (tiling_flags & RADEON_TILING_MICRO)
  1051. word0 |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
  1052. word1 = radeon_get_ib_value(p, idx + 1);
  1053. w0 = G_038000_TEX_WIDTH(word0) + 1;
  1054. h0 = G_038004_TEX_HEIGHT(word1) + 1;
  1055. d0 = G_038004_TEX_DEPTH(word1);
  1056. nfaces = 1;
  1057. switch (G_038000_DIM(word0)) {
  1058. case V_038000_SQ_TEX_DIM_1D:
  1059. case V_038000_SQ_TEX_DIM_2D:
  1060. case V_038000_SQ_TEX_DIM_3D:
  1061. break;
  1062. case V_038000_SQ_TEX_DIM_CUBEMAP:
  1063. nfaces = 6;
  1064. break;
  1065. case V_038000_SQ_TEX_DIM_1D_ARRAY:
  1066. case V_038000_SQ_TEX_DIM_2D_ARRAY:
  1067. case V_038000_SQ_TEX_DIM_2D_MSAA:
  1068. case V_038000_SQ_TEX_DIM_2D_ARRAY_MSAA:
  1069. default:
  1070. dev_warn(p->dev, "this kernel doesn't support %d texture dim\n", G_038000_DIM(word0));
  1071. return -EINVAL;
  1072. }
  1073. if (r600_bpe_from_format(&bpe, G_038004_DATA_FORMAT(word1))) {
  1074. dev_warn(p->dev, "%s:%d texture invalid format %d\n",
  1075. __func__, __LINE__, G_038004_DATA_FORMAT(word1));
  1076. return -EINVAL;
  1077. }
  1078. pitch = G_038000_PITCH(word0) + 1;
  1079. switch (G_038000_TILE_MODE(word0)) {
  1080. case V_038000_ARRAY_LINEAR_GENERAL:
  1081. pitch_align = 1;
  1082. /* XXX check height align */
  1083. break;
  1084. case V_038000_ARRAY_LINEAR_ALIGNED:
  1085. pitch_align = max((u32)64, (u32)(track->group_size / bpe)) / 8;
  1086. if (!IS_ALIGNED(pitch, pitch_align)) {
  1087. dev_warn(p->dev, "%s:%d tex pitch (%d) invalid\n",
  1088. __func__, __LINE__, pitch);
  1089. return -EINVAL;
  1090. }
  1091. /* XXX check height align */
  1092. break;
  1093. case V_038000_ARRAY_1D_TILED_THIN1:
  1094. pitch_align = max((u32)8, (u32)(track->group_size / (8 * bpe))) / 8;
  1095. if (!IS_ALIGNED(pitch, pitch_align)) {
  1096. dev_warn(p->dev, "%s:%d tex pitch (%d) invalid\n",
  1097. __func__, __LINE__, pitch);
  1098. return -EINVAL;
  1099. }
  1100. /* XXX check height align */
  1101. break;
  1102. case V_038000_ARRAY_2D_TILED_THIN1:
  1103. pitch_align = max((u32)track->nbanks,
  1104. (u32)(((track->group_size / 8) / bpe) * track->nbanks));
  1105. if (!IS_ALIGNED(pitch, pitch_align)) {
  1106. dev_warn(p->dev, "%s:%d tex pitch (%d) invalid\n",
  1107. __func__, __LINE__, pitch);
  1108. return -EINVAL;
  1109. }
  1110. /* XXX check height align */
  1111. break;
  1112. default:
  1113. dev_warn(p->dev, "%s invalid tiling %d (0x%08X)\n", __func__,
  1114. G_038000_TILE_MODE(word0), word0);
  1115. return -EINVAL;
  1116. }
  1117. /* XXX check offset align */
  1118. word0 = radeon_get_ib_value(p, idx + 4);
  1119. word1 = radeon_get_ib_value(p, idx + 5);
  1120. blevel = G_038010_BASE_LEVEL(word0);
  1121. nlevels = G_038014_LAST_LEVEL(word1);
  1122. r600_texture_size(nfaces, blevel, nlevels, w0, h0, d0, bpe,
  1123. (pitch_align * bpe),
  1124. &l0_size, &mipmap_size);
  1125. /* using get ib will give us the offset into the texture bo */
  1126. word0 = radeon_get_ib_value(p, idx + 2);
  1127. if ((l0_size + word0) > radeon_bo_size(texture)) {
  1128. dev_warn(p->dev, "texture bo too small (%d %d %d %d -> %d have %ld)\n",
  1129. w0, h0, bpe, word0, l0_size, radeon_bo_size(texture));
  1130. return -EINVAL;
  1131. }
  1132. /* using get ib will give us the offset into the mipmap bo */
  1133. word0 = radeon_get_ib_value(p, idx + 3);
  1134. if ((mipmap_size + word0) > radeon_bo_size(mipmap)) {
  1135. dev_warn(p->dev, "mipmap bo too small (%d %d %d %d %d %d -> %d have %ld)\n",
  1136. w0, h0, bpe, blevel, nlevels, word0, mipmap_size, radeon_bo_size(texture));
  1137. return -EINVAL;
  1138. }
  1139. return 0;
  1140. }
  1141. static int r600_packet3_check(struct radeon_cs_parser *p,
  1142. struct radeon_cs_packet *pkt)
  1143. {
  1144. struct radeon_cs_reloc *reloc;
  1145. struct r600_cs_track *track;
  1146. volatile u32 *ib;
  1147. unsigned idx;
  1148. unsigned i;
  1149. unsigned start_reg, end_reg, reg;
  1150. int r;
  1151. u32 idx_value;
  1152. track = (struct r600_cs_track *)p->track;
  1153. ib = p->ib->ptr;
  1154. idx = pkt->idx + 1;
  1155. idx_value = radeon_get_ib_value(p, idx);
  1156. switch (pkt->opcode) {
  1157. case PACKET3_START_3D_CMDBUF:
  1158. if (p->family >= CHIP_RV770 || pkt->count) {
  1159. DRM_ERROR("bad START_3D\n");
  1160. return -EINVAL;
  1161. }
  1162. break;
  1163. case PACKET3_CONTEXT_CONTROL:
  1164. if (pkt->count != 1) {
  1165. DRM_ERROR("bad CONTEXT_CONTROL\n");
  1166. return -EINVAL;
  1167. }
  1168. break;
  1169. case PACKET3_INDEX_TYPE:
  1170. case PACKET3_NUM_INSTANCES:
  1171. if (pkt->count) {
  1172. DRM_ERROR("bad INDEX_TYPE/NUM_INSTANCES\n");
  1173. return -EINVAL;
  1174. }
  1175. break;
  1176. case PACKET3_DRAW_INDEX:
  1177. if (pkt->count != 3) {
  1178. DRM_ERROR("bad DRAW_INDEX\n");
  1179. return -EINVAL;
  1180. }
  1181. r = r600_cs_packet_next_reloc(p, &reloc);
  1182. if (r) {
  1183. DRM_ERROR("bad DRAW_INDEX\n");
  1184. return -EINVAL;
  1185. }
  1186. ib[idx+0] = idx_value + (u32)(reloc->lobj.gpu_offset & 0xffffffff);
  1187. ib[idx+1] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
  1188. r = r600_cs_track_check(p);
  1189. if (r) {
  1190. dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
  1191. return r;
  1192. }
  1193. break;
  1194. case PACKET3_DRAW_INDEX_AUTO:
  1195. if (pkt->count != 1) {
  1196. DRM_ERROR("bad DRAW_INDEX_AUTO\n");
  1197. return -EINVAL;
  1198. }
  1199. r = r600_cs_track_check(p);
  1200. if (r) {
  1201. dev_warn(p->dev, "%s:%d invalid cmd stream %d\n", __func__, __LINE__, idx);
  1202. return r;
  1203. }
  1204. break;
  1205. case PACKET3_DRAW_INDEX_IMMD_BE:
  1206. case PACKET3_DRAW_INDEX_IMMD:
  1207. if (pkt->count < 2) {
  1208. DRM_ERROR("bad DRAW_INDEX_IMMD\n");
  1209. return -EINVAL;
  1210. }
  1211. r = r600_cs_track_check(p);
  1212. if (r) {
  1213. dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
  1214. return r;
  1215. }
  1216. break;
  1217. case PACKET3_WAIT_REG_MEM:
  1218. if (pkt->count != 5) {
  1219. DRM_ERROR("bad WAIT_REG_MEM\n");
  1220. return -EINVAL;
  1221. }
  1222. /* bit 4 is reg (0) or mem (1) */
  1223. if (idx_value & 0x10) {
  1224. r = r600_cs_packet_next_reloc(p, &reloc);
  1225. if (r) {
  1226. DRM_ERROR("bad WAIT_REG_MEM\n");
  1227. return -EINVAL;
  1228. }
  1229. ib[idx+1] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
  1230. ib[idx+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
  1231. }
  1232. break;
  1233. case PACKET3_SURFACE_SYNC:
  1234. if (pkt->count != 3) {
  1235. DRM_ERROR("bad SURFACE_SYNC\n");
  1236. return -EINVAL;
  1237. }
  1238. /* 0xffffffff/0x0 is flush all cache flag */
  1239. if (radeon_get_ib_value(p, idx + 1) != 0xffffffff ||
  1240. radeon_get_ib_value(p, idx + 2) != 0) {
  1241. r = r600_cs_packet_next_reloc(p, &reloc);
  1242. if (r) {
  1243. DRM_ERROR("bad SURFACE_SYNC\n");
  1244. return -EINVAL;
  1245. }
  1246. ib[idx+2] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  1247. }
  1248. break;
  1249. case PACKET3_EVENT_WRITE:
  1250. if (pkt->count != 2 && pkt->count != 0) {
  1251. DRM_ERROR("bad EVENT_WRITE\n");
  1252. return -EINVAL;
  1253. }
  1254. if (pkt->count) {
  1255. r = r600_cs_packet_next_reloc(p, &reloc);
  1256. if (r) {
  1257. DRM_ERROR("bad EVENT_WRITE\n");
  1258. return -EINVAL;
  1259. }
  1260. ib[idx+1] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
  1261. ib[idx+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
  1262. }
  1263. break;
  1264. case PACKET3_EVENT_WRITE_EOP:
  1265. if (pkt->count != 4) {
  1266. DRM_ERROR("bad EVENT_WRITE_EOP\n");
  1267. return -EINVAL;
  1268. }
  1269. r = r600_cs_packet_next_reloc(p, &reloc);
  1270. if (r) {
  1271. DRM_ERROR("bad EVENT_WRITE\n");
  1272. return -EINVAL;
  1273. }
  1274. ib[idx+1] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
  1275. ib[idx+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
  1276. break;
  1277. case PACKET3_SET_CONFIG_REG:
  1278. start_reg = (idx_value << 2) + PACKET3_SET_CONFIG_REG_OFFSET;
  1279. end_reg = 4 * pkt->count + start_reg - 4;
  1280. if ((start_reg < PACKET3_SET_CONFIG_REG_OFFSET) ||
  1281. (start_reg >= PACKET3_SET_CONFIG_REG_END) ||
  1282. (end_reg >= PACKET3_SET_CONFIG_REG_END)) {
  1283. DRM_ERROR("bad PACKET3_SET_CONFIG_REG\n");
  1284. return -EINVAL;
  1285. }
  1286. for (i = 0; i < pkt->count; i++) {
  1287. reg = start_reg + (4 * i);
  1288. r = r600_cs_check_reg(p, reg, idx+1+i);
  1289. if (r)
  1290. return r;
  1291. }
  1292. break;
  1293. case PACKET3_SET_CONTEXT_REG:
  1294. start_reg = (idx_value << 2) + PACKET3_SET_CONTEXT_REG_OFFSET;
  1295. end_reg = 4 * pkt->count + start_reg - 4;
  1296. if ((start_reg < PACKET3_SET_CONTEXT_REG_OFFSET) ||
  1297. (start_reg >= PACKET3_SET_CONTEXT_REG_END) ||
  1298. (end_reg >= PACKET3_SET_CONTEXT_REG_END)) {
  1299. DRM_ERROR("bad PACKET3_SET_CONTEXT_REG\n");
  1300. return -EINVAL;
  1301. }
  1302. for (i = 0; i < pkt->count; i++) {
  1303. reg = start_reg + (4 * i);
  1304. r = r600_cs_check_reg(p, reg, idx+1+i);
  1305. if (r)
  1306. return r;
  1307. }
  1308. break;
  1309. case PACKET3_SET_RESOURCE:
  1310. if (pkt->count % 7) {
  1311. DRM_ERROR("bad SET_RESOURCE\n");
  1312. return -EINVAL;
  1313. }
  1314. start_reg = (idx_value << 2) + PACKET3_SET_RESOURCE_OFFSET;
  1315. end_reg = 4 * pkt->count + start_reg - 4;
  1316. if ((start_reg < PACKET3_SET_RESOURCE_OFFSET) ||
  1317. (start_reg >= PACKET3_SET_RESOURCE_END) ||
  1318. (end_reg >= PACKET3_SET_RESOURCE_END)) {
  1319. DRM_ERROR("bad SET_RESOURCE\n");
  1320. return -EINVAL;
  1321. }
  1322. for (i = 0; i < (pkt->count / 7); i++) {
  1323. struct radeon_bo *texture, *mipmap;
  1324. u32 size, offset;
  1325. switch (G__SQ_VTX_CONSTANT_TYPE(radeon_get_ib_value(p, idx+(i*7)+6+1))) {
  1326. case SQ_TEX_VTX_VALID_TEXTURE:
  1327. /* tex base */
  1328. r = r600_cs_packet_next_reloc(p, &reloc);
  1329. if (r) {
  1330. DRM_ERROR("bad SET_RESOURCE\n");
  1331. return -EINVAL;
  1332. }
  1333. ib[idx+1+(i*7)+2] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  1334. if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO)
  1335. ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
  1336. else if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO)
  1337. ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
  1338. texture = reloc->robj;
  1339. /* tex mip base */
  1340. r = r600_cs_packet_next_reloc(p, &reloc);
  1341. if (r) {
  1342. DRM_ERROR("bad SET_RESOURCE\n");
  1343. return -EINVAL;
  1344. }
  1345. ib[idx+1+(i*7)+3] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  1346. mipmap = reloc->robj;
  1347. r = r600_check_texture_resource(p, idx+(i*7)+1,
  1348. texture, mipmap, reloc->lobj.tiling_flags);
  1349. if (r)
  1350. return r;
  1351. break;
  1352. case SQ_TEX_VTX_VALID_BUFFER:
  1353. /* vtx base */
  1354. r = r600_cs_packet_next_reloc(p, &reloc);
  1355. if (r) {
  1356. DRM_ERROR("bad SET_RESOURCE\n");
  1357. return -EINVAL;
  1358. }
  1359. offset = radeon_get_ib_value(p, idx+1+(i*7)+0);
  1360. size = radeon_get_ib_value(p, idx+1+(i*7)+1);
  1361. if (p->rdev && (size + offset) > radeon_bo_size(reloc->robj)) {
  1362. /* force size to size of the buffer */
  1363. dev_warn(p->dev, "vbo resource seems too big for the bo\n");
  1364. ib[idx+1+(i*7)+1] = radeon_bo_size(reloc->robj);
  1365. }
  1366. ib[idx+1+(i*7)+0] += (u32)((reloc->lobj.gpu_offset) & 0xffffffff);
  1367. ib[idx+1+(i*7)+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
  1368. break;
  1369. case SQ_TEX_VTX_INVALID_TEXTURE:
  1370. case SQ_TEX_VTX_INVALID_BUFFER:
  1371. default:
  1372. DRM_ERROR("bad SET_RESOURCE\n");
  1373. return -EINVAL;
  1374. }
  1375. }
  1376. break;
  1377. case PACKET3_SET_ALU_CONST:
  1378. if (track->sq_config & DX9_CONSTS) {
  1379. start_reg = (idx_value << 2) + PACKET3_SET_ALU_CONST_OFFSET;
  1380. end_reg = 4 * pkt->count + start_reg - 4;
  1381. if ((start_reg < PACKET3_SET_ALU_CONST_OFFSET) ||
  1382. (start_reg >= PACKET3_SET_ALU_CONST_END) ||
  1383. (end_reg >= PACKET3_SET_ALU_CONST_END)) {
  1384. DRM_ERROR("bad SET_ALU_CONST\n");
  1385. return -EINVAL;
  1386. }
  1387. }
  1388. break;
  1389. case PACKET3_SET_BOOL_CONST:
  1390. start_reg = (idx_value << 2) + PACKET3_SET_BOOL_CONST_OFFSET;
  1391. end_reg = 4 * pkt->count + start_reg - 4;
  1392. if ((start_reg < PACKET3_SET_BOOL_CONST_OFFSET) ||
  1393. (start_reg >= PACKET3_SET_BOOL_CONST_END) ||
  1394. (end_reg >= PACKET3_SET_BOOL_CONST_END)) {
  1395. DRM_ERROR("bad SET_BOOL_CONST\n");
  1396. return -EINVAL;
  1397. }
  1398. break;
  1399. case PACKET3_SET_LOOP_CONST:
  1400. start_reg = (idx_value << 2) + PACKET3_SET_LOOP_CONST_OFFSET;
  1401. end_reg = 4 * pkt->count + start_reg - 4;
  1402. if ((start_reg < PACKET3_SET_LOOP_CONST_OFFSET) ||
  1403. (start_reg >= PACKET3_SET_LOOP_CONST_END) ||
  1404. (end_reg >= PACKET3_SET_LOOP_CONST_END)) {
  1405. DRM_ERROR("bad SET_LOOP_CONST\n");
  1406. return -EINVAL;
  1407. }
  1408. break;
  1409. case PACKET3_SET_CTL_CONST:
  1410. start_reg = (idx_value << 2) + PACKET3_SET_CTL_CONST_OFFSET;
  1411. end_reg = 4 * pkt->count + start_reg - 4;
  1412. if ((start_reg < PACKET3_SET_CTL_CONST_OFFSET) ||
  1413. (start_reg >= PACKET3_SET_CTL_CONST_END) ||
  1414. (end_reg >= PACKET3_SET_CTL_CONST_END)) {
  1415. DRM_ERROR("bad SET_CTL_CONST\n");
  1416. return -EINVAL;
  1417. }
  1418. break;
  1419. case PACKET3_SET_SAMPLER:
  1420. if (pkt->count % 3) {
  1421. DRM_ERROR("bad SET_SAMPLER\n");
  1422. return -EINVAL;
  1423. }
  1424. start_reg = (idx_value << 2) + PACKET3_SET_SAMPLER_OFFSET;
  1425. end_reg = 4 * pkt->count + start_reg - 4;
  1426. if ((start_reg < PACKET3_SET_SAMPLER_OFFSET) ||
  1427. (start_reg >= PACKET3_SET_SAMPLER_END) ||
  1428. (end_reg >= PACKET3_SET_SAMPLER_END)) {
  1429. DRM_ERROR("bad SET_SAMPLER\n");
  1430. return -EINVAL;
  1431. }
  1432. break;
  1433. case PACKET3_SURFACE_BASE_UPDATE:
  1434. if (p->family >= CHIP_RV770 || p->family == CHIP_R600) {
  1435. DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
  1436. return -EINVAL;
  1437. }
  1438. if (pkt->count) {
  1439. DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
  1440. return -EINVAL;
  1441. }
  1442. break;
  1443. case PACKET3_NOP:
  1444. break;
  1445. default:
  1446. DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
  1447. return -EINVAL;
  1448. }
  1449. return 0;
  1450. }
  1451. int r600_cs_parse(struct radeon_cs_parser *p)
  1452. {
  1453. struct radeon_cs_packet pkt;
  1454. struct r600_cs_track *track;
  1455. int r;
  1456. if (p->track == NULL) {
  1457. /* initialize tracker, we are in kms */
  1458. track = kzalloc(sizeof(*track), GFP_KERNEL);
  1459. if (track == NULL)
  1460. return -ENOMEM;
  1461. r600_cs_track_init(track);
  1462. if (p->rdev->family < CHIP_RV770) {
  1463. track->npipes = p->rdev->config.r600.tiling_npipes;
  1464. track->nbanks = p->rdev->config.r600.tiling_nbanks;
  1465. track->group_size = p->rdev->config.r600.tiling_group_size;
  1466. } else if (p->rdev->family <= CHIP_RV740) {
  1467. track->npipes = p->rdev->config.rv770.tiling_npipes;
  1468. track->nbanks = p->rdev->config.rv770.tiling_nbanks;
  1469. track->group_size = p->rdev->config.rv770.tiling_group_size;
  1470. }
  1471. p->track = track;
  1472. }
  1473. do {
  1474. r = r600_cs_packet_parse(p, &pkt, p->idx);
  1475. if (r) {
  1476. kfree(p->track);
  1477. p->track = NULL;
  1478. return r;
  1479. }
  1480. p->idx += pkt.count + 2;
  1481. switch (pkt.type) {
  1482. case PACKET_TYPE0:
  1483. r = r600_cs_parse_packet0(p, &pkt);
  1484. break;
  1485. case PACKET_TYPE2:
  1486. break;
  1487. case PACKET_TYPE3:
  1488. r = r600_packet3_check(p, &pkt);
  1489. break;
  1490. default:
  1491. DRM_ERROR("Unknown packet type %d !\n", pkt.type);
  1492. kfree(p->track);
  1493. p->track = NULL;
  1494. return -EINVAL;
  1495. }
  1496. if (r) {
  1497. kfree(p->track);
  1498. p->track = NULL;
  1499. return r;
  1500. }
  1501. } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
  1502. #if 0
  1503. for (r = 0; r < p->ib->length_dw; r++) {
  1504. printk(KERN_INFO "%05d 0x%08X\n", r, p->ib->ptr[r]);
  1505. mdelay(1);
  1506. }
  1507. #endif
  1508. kfree(p->track);
  1509. p->track = NULL;
  1510. return 0;
  1511. }
  1512. static int r600_cs_parser_relocs_legacy(struct radeon_cs_parser *p)
  1513. {
  1514. if (p->chunk_relocs_idx == -1) {
  1515. return 0;
  1516. }
  1517. p->relocs = kzalloc(sizeof(struct radeon_cs_reloc), GFP_KERNEL);
  1518. if (p->relocs == NULL) {
  1519. return -ENOMEM;
  1520. }
  1521. return 0;
  1522. }
  1523. /**
  1524. * cs_parser_fini() - clean parser states
  1525. * @parser: parser structure holding parsing context.
  1526. * @error: error number
  1527. *
  1528. * If error is set than unvalidate buffer, otherwise just free memory
  1529. * used by parsing context.
  1530. **/
  1531. static void r600_cs_parser_fini(struct radeon_cs_parser *parser, int error)
  1532. {
  1533. unsigned i;
  1534. kfree(parser->relocs);
  1535. for (i = 0; i < parser->nchunks; i++) {
  1536. kfree(parser->chunks[i].kdata);
  1537. kfree(parser->chunks[i].kpage[0]);
  1538. kfree(parser->chunks[i].kpage[1]);
  1539. }
  1540. kfree(parser->chunks);
  1541. kfree(parser->chunks_array);
  1542. }
  1543. int r600_cs_legacy(struct drm_device *dev, void *data, struct drm_file *filp,
  1544. unsigned family, u32 *ib, int *l)
  1545. {
  1546. struct radeon_cs_parser parser;
  1547. struct radeon_cs_chunk *ib_chunk;
  1548. struct radeon_ib fake_ib;
  1549. struct r600_cs_track *track;
  1550. int r;
  1551. /* initialize tracker */
  1552. track = kzalloc(sizeof(*track), GFP_KERNEL);
  1553. if (track == NULL)
  1554. return -ENOMEM;
  1555. r600_cs_track_init(track);
  1556. r600_cs_legacy_get_tiling_conf(dev, &track->npipes, &track->nbanks, &track->group_size);
  1557. /* initialize parser */
  1558. memset(&parser, 0, sizeof(struct radeon_cs_parser));
  1559. parser.filp = filp;
  1560. parser.dev = &dev->pdev->dev;
  1561. parser.rdev = NULL;
  1562. parser.family = family;
  1563. parser.ib = &fake_ib;
  1564. parser.track = track;
  1565. fake_ib.ptr = ib;
  1566. r = radeon_cs_parser_init(&parser, data);
  1567. if (r) {
  1568. DRM_ERROR("Failed to initialize parser !\n");
  1569. r600_cs_parser_fini(&parser, r);
  1570. return r;
  1571. }
  1572. r = r600_cs_parser_relocs_legacy(&parser);
  1573. if (r) {
  1574. DRM_ERROR("Failed to parse relocation !\n");
  1575. r600_cs_parser_fini(&parser, r);
  1576. return r;
  1577. }
  1578. /* Copy the packet into the IB, the parser will read from the
  1579. * input memory (cached) and write to the IB (which can be
  1580. * uncached). */
  1581. ib_chunk = &parser.chunks[parser.chunk_ib_idx];
  1582. parser.ib->length_dw = ib_chunk->length_dw;
  1583. *l = parser.ib->length_dw;
  1584. r = r600_cs_parse(&parser);
  1585. if (r) {
  1586. DRM_ERROR("Invalid command stream !\n");
  1587. r600_cs_parser_fini(&parser, r);
  1588. return r;
  1589. }
  1590. r = radeon_cs_finish_pages(&parser);
  1591. if (r) {
  1592. DRM_ERROR("Invalid command stream !\n");
  1593. r600_cs_parser_fini(&parser, r);
  1594. return r;
  1595. }
  1596. r600_cs_parser_fini(&parser, r);
  1597. return r;
  1598. }
  1599. void r600_cs_legacy_init(void)
  1600. {
  1601. r600_cs_packet_next_reloc = &r600_cs_packet_next_reloc_nomm;
  1602. }