r600_cs.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  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. obj = drm_mode_object_find(p->rdev->ddev, crtc_id, DRM_MODE_OBJECT_CRTC);
  612. if (!obj) {
  613. DRM_ERROR("cannot find crtc %d\n", crtc_id);
  614. r = -EINVAL;
  615. goto out;
  616. }
  617. crtc = obj_to_crtc(obj);
  618. radeon_crtc = to_radeon_crtc(crtc);
  619. crtc_id = radeon_crtc->crtc_id;
  620. if (!crtc->enabled) {
  621. /* if the CRTC isn't enabled - we need to nop out the WAIT_REG_MEM */
  622. ib[h_idx + 2] = PACKET2(0);
  623. ib[h_idx + 3] = PACKET2(0);
  624. ib[h_idx + 4] = PACKET2(0);
  625. ib[h_idx + 5] = PACKET2(0);
  626. ib[h_idx + 6] = PACKET2(0);
  627. ib[h_idx + 7] = PACKET2(0);
  628. ib[h_idx + 8] = PACKET2(0);
  629. } else if (crtc_id == 1) {
  630. switch (reg) {
  631. case AVIVO_D1MODE_VLINE_START_END:
  632. header &= ~R600_CP_PACKET0_REG_MASK;
  633. header |= AVIVO_D2MODE_VLINE_START_END >> 2;
  634. break;
  635. default:
  636. DRM_ERROR("unknown crtc reloc\n");
  637. r = -EINVAL;
  638. goto out;
  639. }
  640. ib[h_idx] = header;
  641. ib[h_idx + 4] = AVIVO_D2MODE_VLINE_STATUS >> 2;
  642. }
  643. out:
  644. return r;
  645. }
  646. static int r600_packet0_check(struct radeon_cs_parser *p,
  647. struct radeon_cs_packet *pkt,
  648. unsigned idx, unsigned reg)
  649. {
  650. int r;
  651. switch (reg) {
  652. case AVIVO_D1MODE_VLINE_START_END:
  653. r = r600_cs_packet_parse_vline(p);
  654. if (r) {
  655. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  656. idx, reg);
  657. return r;
  658. }
  659. break;
  660. default:
  661. printk(KERN_ERR "Forbidden register 0x%04X in cs at %d\n",
  662. reg, idx);
  663. return -EINVAL;
  664. }
  665. return 0;
  666. }
  667. static int r600_cs_parse_packet0(struct radeon_cs_parser *p,
  668. struct radeon_cs_packet *pkt)
  669. {
  670. unsigned reg, i;
  671. unsigned idx;
  672. int r;
  673. idx = pkt->idx + 1;
  674. reg = pkt->reg;
  675. for (i = 0; i <= pkt->count; i++, idx++, reg += 4) {
  676. r = r600_packet0_check(p, pkt, idx, reg);
  677. if (r) {
  678. return r;
  679. }
  680. }
  681. return 0;
  682. }
  683. /**
  684. * r600_cs_check_reg() - check if register is authorized or not
  685. * @parser: parser structure holding parsing context
  686. * @reg: register we are testing
  687. * @idx: index into the cs buffer
  688. *
  689. * This function will test against r600_reg_safe_bm and return 0
  690. * if register is safe. If register is not flag as safe this function
  691. * will test it against a list of register needind special handling.
  692. */
  693. static inline int r600_cs_check_reg(struct radeon_cs_parser *p, u32 reg, u32 idx)
  694. {
  695. struct r600_cs_track *track = (struct r600_cs_track *)p->track;
  696. struct radeon_cs_reloc *reloc;
  697. u32 last_reg = ARRAY_SIZE(r600_reg_safe_bm);
  698. u32 m, i, tmp, *ib;
  699. int r;
  700. i = (reg >> 7);
  701. if (i > last_reg) {
  702. dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
  703. return -EINVAL;
  704. }
  705. m = 1 << ((reg >> 2) & 31);
  706. if (!(r600_reg_safe_bm[i] & m))
  707. return 0;
  708. ib = p->ib->ptr;
  709. switch (reg) {
  710. /* force following reg to 0 in an attemp to disable out buffer
  711. * which will need us to better understand how it works to perform
  712. * security check on it (Jerome)
  713. */
  714. case R_0288A8_SQ_ESGS_RING_ITEMSIZE:
  715. case R_008C44_SQ_ESGS_RING_SIZE:
  716. case R_0288B0_SQ_ESTMP_RING_ITEMSIZE:
  717. case R_008C54_SQ_ESTMP_RING_SIZE:
  718. case R_0288C0_SQ_FBUF_RING_ITEMSIZE:
  719. case R_008C74_SQ_FBUF_RING_SIZE:
  720. case R_0288B4_SQ_GSTMP_RING_ITEMSIZE:
  721. case R_008C5C_SQ_GSTMP_RING_SIZE:
  722. case R_0288AC_SQ_GSVS_RING_ITEMSIZE:
  723. case R_008C4C_SQ_GSVS_RING_SIZE:
  724. case R_0288BC_SQ_PSTMP_RING_ITEMSIZE:
  725. case R_008C6C_SQ_PSTMP_RING_SIZE:
  726. case R_0288C4_SQ_REDUC_RING_ITEMSIZE:
  727. case R_008C7C_SQ_REDUC_RING_SIZE:
  728. case R_0288B8_SQ_VSTMP_RING_ITEMSIZE:
  729. case R_008C64_SQ_VSTMP_RING_SIZE:
  730. case R_0288C8_SQ_GS_VERT_ITEMSIZE:
  731. /* get value to populate the IB don't remove */
  732. tmp =radeon_get_ib_value(p, idx);
  733. ib[idx] = 0;
  734. break;
  735. case SQ_CONFIG:
  736. track->sq_config = radeon_get_ib_value(p, idx);
  737. break;
  738. case R_028800_DB_DEPTH_CONTROL:
  739. track->db_depth_control = radeon_get_ib_value(p, idx);
  740. break;
  741. case R_028010_DB_DEPTH_INFO:
  742. if (r600_cs_packet_next_is_pkt3_nop(p)) {
  743. r = r600_cs_packet_next_reloc(p, &reloc);
  744. if (r) {
  745. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  746. "0x%04X\n", reg);
  747. return -EINVAL;
  748. }
  749. track->db_depth_info = radeon_get_ib_value(p, idx);
  750. ib[idx] &= C_028010_ARRAY_MODE;
  751. track->db_depth_info &= C_028010_ARRAY_MODE;
  752. if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO) {
  753. ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
  754. track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
  755. } else {
  756. ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
  757. track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
  758. }
  759. } else
  760. track->db_depth_info = radeon_get_ib_value(p, idx);
  761. break;
  762. case R_028004_DB_DEPTH_VIEW:
  763. track->db_depth_view = radeon_get_ib_value(p, idx);
  764. break;
  765. case R_028000_DB_DEPTH_SIZE:
  766. track->db_depth_size = radeon_get_ib_value(p, idx);
  767. track->db_depth_size_idx = idx;
  768. break;
  769. case R_028AB0_VGT_STRMOUT_EN:
  770. track->vgt_strmout_en = radeon_get_ib_value(p, idx);
  771. break;
  772. case R_028B20_VGT_STRMOUT_BUFFER_EN:
  773. track->vgt_strmout_buffer_en = radeon_get_ib_value(p, idx);
  774. break;
  775. case R_028238_CB_TARGET_MASK:
  776. track->cb_target_mask = radeon_get_ib_value(p, idx);
  777. break;
  778. case R_02823C_CB_SHADER_MASK:
  779. track->cb_shader_mask = radeon_get_ib_value(p, idx);
  780. break;
  781. case R_028C04_PA_SC_AA_CONFIG:
  782. tmp = G_028C04_MSAA_NUM_SAMPLES(radeon_get_ib_value(p, idx));
  783. track->nsamples = 1 << tmp;
  784. break;
  785. case R_0280A0_CB_COLOR0_INFO:
  786. case R_0280A4_CB_COLOR1_INFO:
  787. case R_0280A8_CB_COLOR2_INFO:
  788. case R_0280AC_CB_COLOR3_INFO:
  789. case R_0280B0_CB_COLOR4_INFO:
  790. case R_0280B4_CB_COLOR5_INFO:
  791. case R_0280B8_CB_COLOR6_INFO:
  792. case R_0280BC_CB_COLOR7_INFO:
  793. if (r600_cs_packet_next_is_pkt3_nop(p)) {
  794. r = r600_cs_packet_next_reloc(p, &reloc);
  795. if (r) {
  796. dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
  797. return -EINVAL;
  798. }
  799. tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
  800. track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
  801. if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO) {
  802. ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
  803. track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
  804. } else if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO) {
  805. ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
  806. track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
  807. }
  808. } else {
  809. tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
  810. track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
  811. }
  812. break;
  813. case R_028060_CB_COLOR0_SIZE:
  814. case R_028064_CB_COLOR1_SIZE:
  815. case R_028068_CB_COLOR2_SIZE:
  816. case R_02806C_CB_COLOR3_SIZE:
  817. case R_028070_CB_COLOR4_SIZE:
  818. case R_028074_CB_COLOR5_SIZE:
  819. case R_028078_CB_COLOR6_SIZE:
  820. case R_02807C_CB_COLOR7_SIZE:
  821. tmp = (reg - R_028060_CB_COLOR0_SIZE) / 4;
  822. track->cb_color_size[tmp] = radeon_get_ib_value(p, idx);
  823. track->cb_color_size_idx[tmp] = idx;
  824. break;
  825. /* This register were added late, there is userspace
  826. * which does provide relocation for those but set
  827. * 0 offset. In order to avoid breaking old userspace
  828. * we detect this and set address to point to last
  829. * CB_COLOR0_BASE, note that if userspace doesn't set
  830. * CB_COLOR0_BASE before this register we will report
  831. * error. Old userspace always set CB_COLOR0_BASE
  832. * before any of this.
  833. */
  834. case R_0280E0_CB_COLOR0_FRAG:
  835. case R_0280E4_CB_COLOR1_FRAG:
  836. case R_0280E8_CB_COLOR2_FRAG:
  837. case R_0280EC_CB_COLOR3_FRAG:
  838. case R_0280F0_CB_COLOR4_FRAG:
  839. case R_0280F4_CB_COLOR5_FRAG:
  840. case R_0280F8_CB_COLOR6_FRAG:
  841. case R_0280FC_CB_COLOR7_FRAG:
  842. tmp = (reg - R_0280E0_CB_COLOR0_FRAG) / 4;
  843. if (!r600_cs_packet_next_is_pkt3_nop(p)) {
  844. if (!track->cb_color_base_last[tmp]) {
  845. dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
  846. return -EINVAL;
  847. }
  848. ib[idx] = track->cb_color_base_last[tmp];
  849. printk_once(KERN_WARNING "You have old & broken userspace "
  850. "please consider updating mesa & xf86-video-ati\n");
  851. track->cb_color_frag_bo[tmp] = track->cb_color_bo[tmp];
  852. } else {
  853. r = r600_cs_packet_next_reloc(p, &reloc);
  854. if (r) {
  855. dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
  856. return -EINVAL;
  857. }
  858. ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  859. track->cb_color_frag_bo[tmp] = reloc->robj;
  860. }
  861. break;
  862. case R_0280C0_CB_COLOR0_TILE:
  863. case R_0280C4_CB_COLOR1_TILE:
  864. case R_0280C8_CB_COLOR2_TILE:
  865. case R_0280CC_CB_COLOR3_TILE:
  866. case R_0280D0_CB_COLOR4_TILE:
  867. case R_0280D4_CB_COLOR5_TILE:
  868. case R_0280D8_CB_COLOR6_TILE:
  869. case R_0280DC_CB_COLOR7_TILE:
  870. tmp = (reg - R_0280C0_CB_COLOR0_TILE) / 4;
  871. if (!r600_cs_packet_next_is_pkt3_nop(p)) {
  872. if (!track->cb_color_base_last[tmp]) {
  873. dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
  874. return -EINVAL;
  875. }
  876. ib[idx] = track->cb_color_base_last[tmp];
  877. printk_once(KERN_WARNING "You have old & broken userspace "
  878. "please consider updating mesa & xf86-video-ati\n");
  879. track->cb_color_tile_bo[tmp] = track->cb_color_bo[tmp];
  880. } else {
  881. r = r600_cs_packet_next_reloc(p, &reloc);
  882. if (r) {
  883. dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
  884. return -EINVAL;
  885. }
  886. ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  887. track->cb_color_tile_bo[tmp] = reloc->robj;
  888. }
  889. break;
  890. case CB_COLOR0_BASE:
  891. case CB_COLOR1_BASE:
  892. case CB_COLOR2_BASE:
  893. case CB_COLOR3_BASE:
  894. case CB_COLOR4_BASE:
  895. case CB_COLOR5_BASE:
  896. case CB_COLOR6_BASE:
  897. case CB_COLOR7_BASE:
  898. r = r600_cs_packet_next_reloc(p, &reloc);
  899. if (r) {
  900. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  901. "0x%04X\n", reg);
  902. return -EINVAL;
  903. }
  904. tmp = (reg - CB_COLOR0_BASE) / 4;
  905. track->cb_color_bo_offset[tmp] = radeon_get_ib_value(p, idx);
  906. ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  907. track->cb_color_base_last[tmp] = ib[idx];
  908. track->cb_color_bo[tmp] = reloc->robj;
  909. break;
  910. case DB_DEPTH_BASE:
  911. r = r600_cs_packet_next_reloc(p, &reloc);
  912. if (r) {
  913. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  914. "0x%04X\n", reg);
  915. return -EINVAL;
  916. }
  917. track->db_offset = radeon_get_ib_value(p, idx);
  918. ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  919. track->db_bo = reloc->robj;
  920. break;
  921. case DB_HTILE_DATA_BASE:
  922. case SQ_PGM_START_FS:
  923. case SQ_PGM_START_ES:
  924. case SQ_PGM_START_VS:
  925. case SQ_PGM_START_GS:
  926. case SQ_PGM_START_PS:
  927. case SQ_ALU_CONST_CACHE_GS_0:
  928. case SQ_ALU_CONST_CACHE_GS_1:
  929. case SQ_ALU_CONST_CACHE_GS_2:
  930. case SQ_ALU_CONST_CACHE_GS_3:
  931. case SQ_ALU_CONST_CACHE_GS_4:
  932. case SQ_ALU_CONST_CACHE_GS_5:
  933. case SQ_ALU_CONST_CACHE_GS_6:
  934. case SQ_ALU_CONST_CACHE_GS_7:
  935. case SQ_ALU_CONST_CACHE_GS_8:
  936. case SQ_ALU_CONST_CACHE_GS_9:
  937. case SQ_ALU_CONST_CACHE_GS_10:
  938. case SQ_ALU_CONST_CACHE_GS_11:
  939. case SQ_ALU_CONST_CACHE_GS_12:
  940. case SQ_ALU_CONST_CACHE_GS_13:
  941. case SQ_ALU_CONST_CACHE_GS_14:
  942. case SQ_ALU_CONST_CACHE_GS_15:
  943. case SQ_ALU_CONST_CACHE_PS_0:
  944. case SQ_ALU_CONST_CACHE_PS_1:
  945. case SQ_ALU_CONST_CACHE_PS_2:
  946. case SQ_ALU_CONST_CACHE_PS_3:
  947. case SQ_ALU_CONST_CACHE_PS_4:
  948. case SQ_ALU_CONST_CACHE_PS_5:
  949. case SQ_ALU_CONST_CACHE_PS_6:
  950. case SQ_ALU_CONST_CACHE_PS_7:
  951. case SQ_ALU_CONST_CACHE_PS_8:
  952. case SQ_ALU_CONST_CACHE_PS_9:
  953. case SQ_ALU_CONST_CACHE_PS_10:
  954. case SQ_ALU_CONST_CACHE_PS_11:
  955. case SQ_ALU_CONST_CACHE_PS_12:
  956. case SQ_ALU_CONST_CACHE_PS_13:
  957. case SQ_ALU_CONST_CACHE_PS_14:
  958. case SQ_ALU_CONST_CACHE_PS_15:
  959. case SQ_ALU_CONST_CACHE_VS_0:
  960. case SQ_ALU_CONST_CACHE_VS_1:
  961. case SQ_ALU_CONST_CACHE_VS_2:
  962. case SQ_ALU_CONST_CACHE_VS_3:
  963. case SQ_ALU_CONST_CACHE_VS_4:
  964. case SQ_ALU_CONST_CACHE_VS_5:
  965. case SQ_ALU_CONST_CACHE_VS_6:
  966. case SQ_ALU_CONST_CACHE_VS_7:
  967. case SQ_ALU_CONST_CACHE_VS_8:
  968. case SQ_ALU_CONST_CACHE_VS_9:
  969. case SQ_ALU_CONST_CACHE_VS_10:
  970. case SQ_ALU_CONST_CACHE_VS_11:
  971. case SQ_ALU_CONST_CACHE_VS_12:
  972. case SQ_ALU_CONST_CACHE_VS_13:
  973. case SQ_ALU_CONST_CACHE_VS_14:
  974. case SQ_ALU_CONST_CACHE_VS_15:
  975. r = r600_cs_packet_next_reloc(p, &reloc);
  976. if (r) {
  977. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  978. "0x%04X\n", reg);
  979. return -EINVAL;
  980. }
  981. ib[idx] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  982. break;
  983. default:
  984. dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
  985. return -EINVAL;
  986. }
  987. return 0;
  988. }
  989. static inline unsigned minify(unsigned size, unsigned levels)
  990. {
  991. size = size >> levels;
  992. if (size < 1)
  993. size = 1;
  994. return size;
  995. }
  996. static void r600_texture_size(unsigned nfaces, unsigned blevel, unsigned nlevels,
  997. unsigned w0, unsigned h0, unsigned d0, unsigned bpe,
  998. unsigned pitch_align,
  999. unsigned *l0_size, unsigned *mipmap_size)
  1000. {
  1001. unsigned offset, i, level, face;
  1002. unsigned width, height, depth, rowstride, size;
  1003. w0 = minify(w0, 0);
  1004. h0 = minify(h0, 0);
  1005. d0 = minify(d0, 0);
  1006. for(i = 0, offset = 0, level = blevel; i < nlevels; i++, level++) {
  1007. width = minify(w0, i);
  1008. height = minify(h0, i);
  1009. depth = minify(d0, i);
  1010. for(face = 0; face < nfaces; face++) {
  1011. rowstride = ALIGN((width * bpe), pitch_align);
  1012. size = height * rowstride * depth;
  1013. offset += size;
  1014. offset = (offset + 0x1f) & ~0x1f;
  1015. }
  1016. }
  1017. *l0_size = ALIGN((w0 * bpe), pitch_align) * h0 * d0;
  1018. *mipmap_size = offset;
  1019. if (!blevel)
  1020. *mipmap_size -= *l0_size;
  1021. if (!nlevels)
  1022. *mipmap_size = *l0_size;
  1023. }
  1024. /**
  1025. * r600_check_texture_resource() - check if register is authorized or not
  1026. * @p: parser structure holding parsing context
  1027. * @idx: index into the cs buffer
  1028. * @texture: texture's bo structure
  1029. * @mipmap: mipmap's bo structure
  1030. *
  1031. * This function will check that the resource has valid field and that
  1032. * the texture and mipmap bo object are big enough to cover this resource.
  1033. */
  1034. static inline int r600_check_texture_resource(struct radeon_cs_parser *p, u32 idx,
  1035. struct radeon_bo *texture,
  1036. struct radeon_bo *mipmap,
  1037. u32 tiling_flags)
  1038. {
  1039. struct r600_cs_track *track = p->track;
  1040. u32 nfaces, nlevels, blevel, w0, h0, d0, bpe = 0;
  1041. u32 word0, word1, l0_size, mipmap_size, pitch, pitch_align;
  1042. /* on legacy kernel we don't perform advanced check */
  1043. if (p->rdev == NULL)
  1044. return 0;
  1045. word0 = radeon_get_ib_value(p, idx + 0);
  1046. if (tiling_flags & RADEON_TILING_MACRO)
  1047. word0 |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
  1048. else if (tiling_flags & RADEON_TILING_MICRO)
  1049. word0 |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
  1050. word1 = radeon_get_ib_value(p, idx + 1);
  1051. w0 = G_038000_TEX_WIDTH(word0) + 1;
  1052. h0 = G_038004_TEX_HEIGHT(word1) + 1;
  1053. d0 = G_038004_TEX_DEPTH(word1);
  1054. nfaces = 1;
  1055. switch (G_038000_DIM(word0)) {
  1056. case V_038000_SQ_TEX_DIM_1D:
  1057. case V_038000_SQ_TEX_DIM_2D:
  1058. case V_038000_SQ_TEX_DIM_3D:
  1059. break;
  1060. case V_038000_SQ_TEX_DIM_CUBEMAP:
  1061. nfaces = 6;
  1062. break;
  1063. case V_038000_SQ_TEX_DIM_1D_ARRAY:
  1064. case V_038000_SQ_TEX_DIM_2D_ARRAY:
  1065. case V_038000_SQ_TEX_DIM_2D_MSAA:
  1066. case V_038000_SQ_TEX_DIM_2D_ARRAY_MSAA:
  1067. default:
  1068. dev_warn(p->dev, "this kernel doesn't support %d texture dim\n", G_038000_DIM(word0));
  1069. return -EINVAL;
  1070. }
  1071. if (r600_bpe_from_format(&bpe, G_038004_DATA_FORMAT(word1))) {
  1072. dev_warn(p->dev, "%s:%d texture invalid format %d\n",
  1073. __func__, __LINE__, G_038004_DATA_FORMAT(word1));
  1074. return -EINVAL;
  1075. }
  1076. pitch = G_038000_PITCH(word0) + 1;
  1077. switch (G_038000_TILE_MODE(word0)) {
  1078. case V_038000_ARRAY_LINEAR_GENERAL:
  1079. pitch_align = 1;
  1080. /* XXX check height align */
  1081. break;
  1082. case V_038000_ARRAY_LINEAR_ALIGNED:
  1083. pitch_align = max((u32)64, (u32)(track->group_size / bpe)) / 8;
  1084. if (!IS_ALIGNED(pitch, pitch_align)) {
  1085. dev_warn(p->dev, "%s:%d tex pitch (%d) invalid\n",
  1086. __func__, __LINE__, pitch);
  1087. return -EINVAL;
  1088. }
  1089. /* XXX check height align */
  1090. break;
  1091. case V_038000_ARRAY_1D_TILED_THIN1:
  1092. pitch_align = max((u32)8, (u32)(track->group_size / (8 * bpe))) / 8;
  1093. if (!IS_ALIGNED(pitch, pitch_align)) {
  1094. dev_warn(p->dev, "%s:%d tex pitch (%d) invalid\n",
  1095. __func__, __LINE__, pitch);
  1096. return -EINVAL;
  1097. }
  1098. /* XXX check height align */
  1099. break;
  1100. case V_038000_ARRAY_2D_TILED_THIN1:
  1101. pitch_align = max((u32)track->nbanks,
  1102. (u32)(((track->group_size / 8) / bpe) * track->nbanks));
  1103. if (!IS_ALIGNED(pitch, pitch_align)) {
  1104. dev_warn(p->dev, "%s:%d tex pitch (%d) invalid\n",
  1105. __func__, __LINE__, pitch);
  1106. return -EINVAL;
  1107. }
  1108. /* XXX check height align */
  1109. break;
  1110. default:
  1111. dev_warn(p->dev, "%s invalid tiling %d (0x%08X)\n", __func__,
  1112. G_038000_TILE_MODE(word0), word0);
  1113. return -EINVAL;
  1114. }
  1115. /* XXX check offset align */
  1116. word0 = radeon_get_ib_value(p, idx + 4);
  1117. word1 = radeon_get_ib_value(p, idx + 5);
  1118. blevel = G_038010_BASE_LEVEL(word0);
  1119. nlevels = G_038014_LAST_LEVEL(word1);
  1120. r600_texture_size(nfaces, blevel, nlevels, w0, h0, d0, bpe,
  1121. (pitch_align * bpe),
  1122. &l0_size, &mipmap_size);
  1123. /* using get ib will give us the offset into the texture bo */
  1124. word0 = radeon_get_ib_value(p, idx + 2);
  1125. if ((l0_size + word0) > radeon_bo_size(texture)) {
  1126. dev_warn(p->dev, "texture bo too small (%d %d %d %d -> %d have %ld)\n",
  1127. w0, h0, bpe, word0, l0_size, radeon_bo_size(texture));
  1128. return -EINVAL;
  1129. }
  1130. /* using get ib will give us the offset into the mipmap bo */
  1131. word0 = radeon_get_ib_value(p, idx + 3);
  1132. if ((mipmap_size + word0) > radeon_bo_size(mipmap)) {
  1133. dev_warn(p->dev, "mipmap bo too small (%d %d %d %d %d %d -> %d have %ld)\n",
  1134. w0, h0, bpe, blevel, nlevels, word0, mipmap_size, radeon_bo_size(texture));
  1135. return -EINVAL;
  1136. }
  1137. return 0;
  1138. }
  1139. static int r600_packet3_check(struct radeon_cs_parser *p,
  1140. struct radeon_cs_packet *pkt)
  1141. {
  1142. struct radeon_cs_reloc *reloc;
  1143. struct r600_cs_track *track;
  1144. volatile u32 *ib;
  1145. unsigned idx;
  1146. unsigned i;
  1147. unsigned start_reg, end_reg, reg;
  1148. int r;
  1149. u32 idx_value;
  1150. track = (struct r600_cs_track *)p->track;
  1151. ib = p->ib->ptr;
  1152. idx = pkt->idx + 1;
  1153. idx_value = radeon_get_ib_value(p, idx);
  1154. switch (pkt->opcode) {
  1155. case PACKET3_START_3D_CMDBUF:
  1156. if (p->family >= CHIP_RV770 || pkt->count) {
  1157. DRM_ERROR("bad START_3D\n");
  1158. return -EINVAL;
  1159. }
  1160. break;
  1161. case PACKET3_CONTEXT_CONTROL:
  1162. if (pkt->count != 1) {
  1163. DRM_ERROR("bad CONTEXT_CONTROL\n");
  1164. return -EINVAL;
  1165. }
  1166. break;
  1167. case PACKET3_INDEX_TYPE:
  1168. case PACKET3_NUM_INSTANCES:
  1169. if (pkt->count) {
  1170. DRM_ERROR("bad INDEX_TYPE/NUM_INSTANCES\n");
  1171. return -EINVAL;
  1172. }
  1173. break;
  1174. case PACKET3_DRAW_INDEX:
  1175. if (pkt->count != 3) {
  1176. DRM_ERROR("bad DRAW_INDEX\n");
  1177. return -EINVAL;
  1178. }
  1179. r = r600_cs_packet_next_reloc(p, &reloc);
  1180. if (r) {
  1181. DRM_ERROR("bad DRAW_INDEX\n");
  1182. return -EINVAL;
  1183. }
  1184. ib[idx+0] = idx_value + (u32)(reloc->lobj.gpu_offset & 0xffffffff);
  1185. ib[idx+1] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
  1186. r = r600_cs_track_check(p);
  1187. if (r) {
  1188. dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
  1189. return r;
  1190. }
  1191. break;
  1192. case PACKET3_DRAW_INDEX_AUTO:
  1193. if (pkt->count != 1) {
  1194. DRM_ERROR("bad DRAW_INDEX_AUTO\n");
  1195. return -EINVAL;
  1196. }
  1197. r = r600_cs_track_check(p);
  1198. if (r) {
  1199. dev_warn(p->dev, "%s:%d invalid cmd stream %d\n", __func__, __LINE__, idx);
  1200. return r;
  1201. }
  1202. break;
  1203. case PACKET3_DRAW_INDEX_IMMD_BE:
  1204. case PACKET3_DRAW_INDEX_IMMD:
  1205. if (pkt->count < 2) {
  1206. DRM_ERROR("bad DRAW_INDEX_IMMD\n");
  1207. return -EINVAL;
  1208. }
  1209. r = r600_cs_track_check(p);
  1210. if (r) {
  1211. dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
  1212. return r;
  1213. }
  1214. break;
  1215. case PACKET3_WAIT_REG_MEM:
  1216. if (pkt->count != 5) {
  1217. DRM_ERROR("bad WAIT_REG_MEM\n");
  1218. return -EINVAL;
  1219. }
  1220. /* bit 4 is reg (0) or mem (1) */
  1221. if (idx_value & 0x10) {
  1222. r = r600_cs_packet_next_reloc(p, &reloc);
  1223. if (r) {
  1224. DRM_ERROR("bad WAIT_REG_MEM\n");
  1225. return -EINVAL;
  1226. }
  1227. ib[idx+1] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
  1228. ib[idx+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
  1229. }
  1230. break;
  1231. case PACKET3_SURFACE_SYNC:
  1232. if (pkt->count != 3) {
  1233. DRM_ERROR("bad SURFACE_SYNC\n");
  1234. return -EINVAL;
  1235. }
  1236. /* 0xffffffff/0x0 is flush all cache flag */
  1237. if (radeon_get_ib_value(p, idx + 1) != 0xffffffff ||
  1238. radeon_get_ib_value(p, idx + 2) != 0) {
  1239. r = r600_cs_packet_next_reloc(p, &reloc);
  1240. if (r) {
  1241. DRM_ERROR("bad SURFACE_SYNC\n");
  1242. return -EINVAL;
  1243. }
  1244. ib[idx+2] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  1245. }
  1246. break;
  1247. case PACKET3_EVENT_WRITE:
  1248. if (pkt->count != 2 && pkt->count != 0) {
  1249. DRM_ERROR("bad EVENT_WRITE\n");
  1250. return -EINVAL;
  1251. }
  1252. if (pkt->count) {
  1253. r = r600_cs_packet_next_reloc(p, &reloc);
  1254. if (r) {
  1255. DRM_ERROR("bad EVENT_WRITE\n");
  1256. return -EINVAL;
  1257. }
  1258. ib[idx+1] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
  1259. ib[idx+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
  1260. }
  1261. break;
  1262. case PACKET3_EVENT_WRITE_EOP:
  1263. if (pkt->count != 4) {
  1264. DRM_ERROR("bad EVENT_WRITE_EOP\n");
  1265. return -EINVAL;
  1266. }
  1267. r = r600_cs_packet_next_reloc(p, &reloc);
  1268. if (r) {
  1269. DRM_ERROR("bad EVENT_WRITE\n");
  1270. return -EINVAL;
  1271. }
  1272. ib[idx+1] += (u32)(reloc->lobj.gpu_offset & 0xffffffff);
  1273. ib[idx+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
  1274. break;
  1275. case PACKET3_SET_CONFIG_REG:
  1276. start_reg = (idx_value << 2) + PACKET3_SET_CONFIG_REG_OFFSET;
  1277. end_reg = 4 * pkt->count + start_reg - 4;
  1278. if ((start_reg < PACKET3_SET_CONFIG_REG_OFFSET) ||
  1279. (start_reg >= PACKET3_SET_CONFIG_REG_END) ||
  1280. (end_reg >= PACKET3_SET_CONFIG_REG_END)) {
  1281. DRM_ERROR("bad PACKET3_SET_CONFIG_REG\n");
  1282. return -EINVAL;
  1283. }
  1284. for (i = 0; i < pkt->count; i++) {
  1285. reg = start_reg + (4 * i);
  1286. r = r600_cs_check_reg(p, reg, idx+1+i);
  1287. if (r)
  1288. return r;
  1289. }
  1290. break;
  1291. case PACKET3_SET_CONTEXT_REG:
  1292. start_reg = (idx_value << 2) + PACKET3_SET_CONTEXT_REG_OFFSET;
  1293. end_reg = 4 * pkt->count + start_reg - 4;
  1294. if ((start_reg < PACKET3_SET_CONTEXT_REG_OFFSET) ||
  1295. (start_reg >= PACKET3_SET_CONTEXT_REG_END) ||
  1296. (end_reg >= PACKET3_SET_CONTEXT_REG_END)) {
  1297. DRM_ERROR("bad PACKET3_SET_CONTEXT_REG\n");
  1298. return -EINVAL;
  1299. }
  1300. for (i = 0; i < pkt->count; i++) {
  1301. reg = start_reg + (4 * i);
  1302. r = r600_cs_check_reg(p, reg, idx+1+i);
  1303. if (r)
  1304. return r;
  1305. }
  1306. break;
  1307. case PACKET3_SET_RESOURCE:
  1308. if (pkt->count % 7) {
  1309. DRM_ERROR("bad SET_RESOURCE\n");
  1310. return -EINVAL;
  1311. }
  1312. start_reg = (idx_value << 2) + PACKET3_SET_RESOURCE_OFFSET;
  1313. end_reg = 4 * pkt->count + start_reg - 4;
  1314. if ((start_reg < PACKET3_SET_RESOURCE_OFFSET) ||
  1315. (start_reg >= PACKET3_SET_RESOURCE_END) ||
  1316. (end_reg >= PACKET3_SET_RESOURCE_END)) {
  1317. DRM_ERROR("bad SET_RESOURCE\n");
  1318. return -EINVAL;
  1319. }
  1320. for (i = 0; i < (pkt->count / 7); i++) {
  1321. struct radeon_bo *texture, *mipmap;
  1322. u32 size, offset;
  1323. switch (G__SQ_VTX_CONSTANT_TYPE(radeon_get_ib_value(p, idx+(i*7)+6+1))) {
  1324. case SQ_TEX_VTX_VALID_TEXTURE:
  1325. /* tex base */
  1326. r = r600_cs_packet_next_reloc(p, &reloc);
  1327. if (r) {
  1328. DRM_ERROR("bad SET_RESOURCE\n");
  1329. return -EINVAL;
  1330. }
  1331. ib[idx+1+(i*7)+2] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  1332. if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO)
  1333. ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
  1334. else if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO)
  1335. ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
  1336. texture = reloc->robj;
  1337. /* tex mip base */
  1338. r = r600_cs_packet_next_reloc(p, &reloc);
  1339. if (r) {
  1340. DRM_ERROR("bad SET_RESOURCE\n");
  1341. return -EINVAL;
  1342. }
  1343. ib[idx+1+(i*7)+3] += (u32)((reloc->lobj.gpu_offset >> 8) & 0xffffffff);
  1344. mipmap = reloc->robj;
  1345. r = r600_check_texture_resource(p, idx+(i*7)+1,
  1346. texture, mipmap, reloc->lobj.tiling_flags);
  1347. if (r)
  1348. return r;
  1349. break;
  1350. case SQ_TEX_VTX_VALID_BUFFER:
  1351. /* vtx base */
  1352. r = r600_cs_packet_next_reloc(p, &reloc);
  1353. if (r) {
  1354. DRM_ERROR("bad SET_RESOURCE\n");
  1355. return -EINVAL;
  1356. }
  1357. offset = radeon_get_ib_value(p, idx+1+(i*7)+0);
  1358. size = radeon_get_ib_value(p, idx+1+(i*7)+1);
  1359. if (p->rdev && (size + offset) > radeon_bo_size(reloc->robj)) {
  1360. /* force size to size of the buffer */
  1361. dev_warn(p->dev, "vbo resource seems too big for the bo\n");
  1362. ib[idx+1+(i*7)+1] = radeon_bo_size(reloc->robj);
  1363. }
  1364. ib[idx+1+(i*7)+0] += (u32)((reloc->lobj.gpu_offset) & 0xffffffff);
  1365. ib[idx+1+(i*7)+2] += upper_32_bits(reloc->lobj.gpu_offset) & 0xff;
  1366. break;
  1367. case SQ_TEX_VTX_INVALID_TEXTURE:
  1368. case SQ_TEX_VTX_INVALID_BUFFER:
  1369. default:
  1370. DRM_ERROR("bad SET_RESOURCE\n");
  1371. return -EINVAL;
  1372. }
  1373. }
  1374. break;
  1375. case PACKET3_SET_ALU_CONST:
  1376. if (track->sq_config & DX9_CONSTS) {
  1377. start_reg = (idx_value << 2) + PACKET3_SET_ALU_CONST_OFFSET;
  1378. end_reg = 4 * pkt->count + start_reg - 4;
  1379. if ((start_reg < PACKET3_SET_ALU_CONST_OFFSET) ||
  1380. (start_reg >= PACKET3_SET_ALU_CONST_END) ||
  1381. (end_reg >= PACKET3_SET_ALU_CONST_END)) {
  1382. DRM_ERROR("bad SET_ALU_CONST\n");
  1383. return -EINVAL;
  1384. }
  1385. }
  1386. break;
  1387. case PACKET3_SET_BOOL_CONST:
  1388. start_reg = (idx_value << 2) + PACKET3_SET_BOOL_CONST_OFFSET;
  1389. end_reg = 4 * pkt->count + start_reg - 4;
  1390. if ((start_reg < PACKET3_SET_BOOL_CONST_OFFSET) ||
  1391. (start_reg >= PACKET3_SET_BOOL_CONST_END) ||
  1392. (end_reg >= PACKET3_SET_BOOL_CONST_END)) {
  1393. DRM_ERROR("bad SET_BOOL_CONST\n");
  1394. return -EINVAL;
  1395. }
  1396. break;
  1397. case PACKET3_SET_LOOP_CONST:
  1398. start_reg = (idx_value << 2) + PACKET3_SET_LOOP_CONST_OFFSET;
  1399. end_reg = 4 * pkt->count + start_reg - 4;
  1400. if ((start_reg < PACKET3_SET_LOOP_CONST_OFFSET) ||
  1401. (start_reg >= PACKET3_SET_LOOP_CONST_END) ||
  1402. (end_reg >= PACKET3_SET_LOOP_CONST_END)) {
  1403. DRM_ERROR("bad SET_LOOP_CONST\n");
  1404. return -EINVAL;
  1405. }
  1406. break;
  1407. case PACKET3_SET_CTL_CONST:
  1408. start_reg = (idx_value << 2) + PACKET3_SET_CTL_CONST_OFFSET;
  1409. end_reg = 4 * pkt->count + start_reg - 4;
  1410. if ((start_reg < PACKET3_SET_CTL_CONST_OFFSET) ||
  1411. (start_reg >= PACKET3_SET_CTL_CONST_END) ||
  1412. (end_reg >= PACKET3_SET_CTL_CONST_END)) {
  1413. DRM_ERROR("bad SET_CTL_CONST\n");
  1414. return -EINVAL;
  1415. }
  1416. break;
  1417. case PACKET3_SET_SAMPLER:
  1418. if (pkt->count % 3) {
  1419. DRM_ERROR("bad SET_SAMPLER\n");
  1420. return -EINVAL;
  1421. }
  1422. start_reg = (idx_value << 2) + PACKET3_SET_SAMPLER_OFFSET;
  1423. end_reg = 4 * pkt->count + start_reg - 4;
  1424. if ((start_reg < PACKET3_SET_SAMPLER_OFFSET) ||
  1425. (start_reg >= PACKET3_SET_SAMPLER_END) ||
  1426. (end_reg >= PACKET3_SET_SAMPLER_END)) {
  1427. DRM_ERROR("bad SET_SAMPLER\n");
  1428. return -EINVAL;
  1429. }
  1430. break;
  1431. case PACKET3_SURFACE_BASE_UPDATE:
  1432. if (p->family >= CHIP_RV770 || p->family == CHIP_R600) {
  1433. DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
  1434. return -EINVAL;
  1435. }
  1436. if (pkt->count) {
  1437. DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
  1438. return -EINVAL;
  1439. }
  1440. break;
  1441. case PACKET3_NOP:
  1442. break;
  1443. default:
  1444. DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
  1445. return -EINVAL;
  1446. }
  1447. return 0;
  1448. }
  1449. int r600_cs_parse(struct radeon_cs_parser *p)
  1450. {
  1451. struct radeon_cs_packet pkt;
  1452. struct r600_cs_track *track;
  1453. int r;
  1454. if (p->track == NULL) {
  1455. /* initialize tracker, we are in kms */
  1456. track = kzalloc(sizeof(*track), GFP_KERNEL);
  1457. if (track == NULL)
  1458. return -ENOMEM;
  1459. r600_cs_track_init(track);
  1460. if (p->rdev->family < CHIP_RV770) {
  1461. track->npipes = p->rdev->config.r600.tiling_npipes;
  1462. track->nbanks = p->rdev->config.r600.tiling_nbanks;
  1463. track->group_size = p->rdev->config.r600.tiling_group_size;
  1464. } else if (p->rdev->family <= CHIP_RV740) {
  1465. track->npipes = p->rdev->config.rv770.tiling_npipes;
  1466. track->nbanks = p->rdev->config.rv770.tiling_nbanks;
  1467. track->group_size = p->rdev->config.rv770.tiling_group_size;
  1468. }
  1469. p->track = track;
  1470. }
  1471. do {
  1472. r = r600_cs_packet_parse(p, &pkt, p->idx);
  1473. if (r) {
  1474. kfree(p->track);
  1475. p->track = NULL;
  1476. return r;
  1477. }
  1478. p->idx += pkt.count + 2;
  1479. switch (pkt.type) {
  1480. case PACKET_TYPE0:
  1481. r = r600_cs_parse_packet0(p, &pkt);
  1482. break;
  1483. case PACKET_TYPE2:
  1484. break;
  1485. case PACKET_TYPE3:
  1486. r = r600_packet3_check(p, &pkt);
  1487. break;
  1488. default:
  1489. DRM_ERROR("Unknown packet type %d !\n", pkt.type);
  1490. kfree(p->track);
  1491. p->track = NULL;
  1492. return -EINVAL;
  1493. }
  1494. if (r) {
  1495. kfree(p->track);
  1496. p->track = NULL;
  1497. return r;
  1498. }
  1499. } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
  1500. #if 0
  1501. for (r = 0; r < p->ib->length_dw; r++) {
  1502. printk(KERN_INFO "%05d 0x%08X\n", r, p->ib->ptr[r]);
  1503. mdelay(1);
  1504. }
  1505. #endif
  1506. kfree(p->track);
  1507. p->track = NULL;
  1508. return 0;
  1509. }
  1510. static int r600_cs_parser_relocs_legacy(struct radeon_cs_parser *p)
  1511. {
  1512. if (p->chunk_relocs_idx == -1) {
  1513. return 0;
  1514. }
  1515. p->relocs = kzalloc(sizeof(struct radeon_cs_reloc), GFP_KERNEL);
  1516. if (p->relocs == NULL) {
  1517. return -ENOMEM;
  1518. }
  1519. return 0;
  1520. }
  1521. /**
  1522. * cs_parser_fini() - clean parser states
  1523. * @parser: parser structure holding parsing context.
  1524. * @error: error number
  1525. *
  1526. * If error is set than unvalidate buffer, otherwise just free memory
  1527. * used by parsing context.
  1528. **/
  1529. static void r600_cs_parser_fini(struct radeon_cs_parser *parser, int error)
  1530. {
  1531. unsigned i;
  1532. kfree(parser->relocs);
  1533. for (i = 0; i < parser->nchunks; i++) {
  1534. kfree(parser->chunks[i].kdata);
  1535. kfree(parser->chunks[i].kpage[0]);
  1536. kfree(parser->chunks[i].kpage[1]);
  1537. }
  1538. kfree(parser->chunks);
  1539. kfree(parser->chunks_array);
  1540. }
  1541. int r600_cs_legacy(struct drm_device *dev, void *data, struct drm_file *filp,
  1542. unsigned family, u32 *ib, int *l)
  1543. {
  1544. struct radeon_cs_parser parser;
  1545. struct radeon_cs_chunk *ib_chunk;
  1546. struct radeon_ib fake_ib;
  1547. struct r600_cs_track *track;
  1548. int r;
  1549. /* initialize tracker */
  1550. track = kzalloc(sizeof(*track), GFP_KERNEL);
  1551. if (track == NULL)
  1552. return -ENOMEM;
  1553. r600_cs_track_init(track);
  1554. r600_cs_legacy_get_tiling_conf(dev, &track->npipes, &track->nbanks, &track->group_size);
  1555. /* initialize parser */
  1556. memset(&parser, 0, sizeof(struct radeon_cs_parser));
  1557. parser.filp = filp;
  1558. parser.dev = &dev->pdev->dev;
  1559. parser.rdev = NULL;
  1560. parser.family = family;
  1561. parser.ib = &fake_ib;
  1562. parser.track = track;
  1563. fake_ib.ptr = ib;
  1564. r = radeon_cs_parser_init(&parser, data);
  1565. if (r) {
  1566. DRM_ERROR("Failed to initialize parser !\n");
  1567. r600_cs_parser_fini(&parser, r);
  1568. return r;
  1569. }
  1570. r = r600_cs_parser_relocs_legacy(&parser);
  1571. if (r) {
  1572. DRM_ERROR("Failed to parse relocation !\n");
  1573. r600_cs_parser_fini(&parser, r);
  1574. return r;
  1575. }
  1576. /* Copy the packet into the IB, the parser will read from the
  1577. * input memory (cached) and write to the IB (which can be
  1578. * uncached). */
  1579. ib_chunk = &parser.chunks[parser.chunk_ib_idx];
  1580. parser.ib->length_dw = ib_chunk->length_dw;
  1581. *l = parser.ib->length_dw;
  1582. r = r600_cs_parse(&parser);
  1583. if (r) {
  1584. DRM_ERROR("Invalid command stream !\n");
  1585. r600_cs_parser_fini(&parser, r);
  1586. return r;
  1587. }
  1588. r = radeon_cs_finish_pages(&parser);
  1589. if (r) {
  1590. DRM_ERROR("Invalid command stream !\n");
  1591. r600_cs_parser_fini(&parser, r);
  1592. return r;
  1593. }
  1594. r600_cs_parser_fini(&parser, r);
  1595. return r;
  1596. }
  1597. void r600_cs_legacy_init(void)
  1598. {
  1599. r600_cs_packet_next_reloc = &r600_cs_packet_next_reloc_nomm;
  1600. }