r600_cs.c 42 KB

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