r600_cs.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632
  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 <drm/drmP.h>
  30. #include "radeon.h"
  31. #include "r600d.h"
  32. #include "r600_reg_safe.h"
  33. static int r600_nomm;
  34. extern void r600_cs_legacy_get_tiling_conf(struct drm_device *dev, u32 *npipes, u32 *nbanks, u32 *group_size);
  35. struct r600_cs_track {
  36. /* configuration we miror so that we use same code btw kms/ums */
  37. u32 group_size;
  38. u32 nbanks;
  39. u32 npipes;
  40. /* value we track */
  41. u32 sq_config;
  42. u32 log_nsamples;
  43. u32 nsamples;
  44. u32 cb_color_base_last[8];
  45. struct radeon_bo *cb_color_bo[8];
  46. u64 cb_color_bo_mc[8];
  47. u64 cb_color_bo_offset[8];
  48. struct radeon_bo *cb_color_frag_bo[8];
  49. u64 cb_color_frag_offset[8];
  50. struct radeon_bo *cb_color_tile_bo[8];
  51. u64 cb_color_tile_offset[8];
  52. u32 cb_color_mask[8];
  53. u32 cb_color_info[8];
  54. u32 cb_color_view[8];
  55. u32 cb_color_size_idx[8]; /* unused */
  56. u32 cb_target_mask;
  57. u32 cb_shader_mask; /* unused */
  58. bool is_resolve;
  59. u32 cb_color_size[8];
  60. u32 vgt_strmout_en;
  61. u32 vgt_strmout_buffer_en;
  62. struct radeon_bo *vgt_strmout_bo[4];
  63. u64 vgt_strmout_bo_mc[4]; /* unused */
  64. u32 vgt_strmout_bo_offset[4];
  65. u32 vgt_strmout_size[4];
  66. u32 db_depth_control;
  67. u32 db_depth_info;
  68. u32 db_depth_size_idx;
  69. u32 db_depth_view;
  70. u32 db_depth_size;
  71. u32 db_offset;
  72. struct radeon_bo *db_bo;
  73. u64 db_bo_mc;
  74. bool sx_misc_kill_all_prims;
  75. bool cb_dirty;
  76. bool db_dirty;
  77. bool streamout_dirty;
  78. struct radeon_bo *htile_bo;
  79. u64 htile_offset;
  80. u32 htile_surface;
  81. };
  82. #define FMT_8_BIT(fmt, vc) [fmt] = { 1, 1, 1, vc, CHIP_R600 }
  83. #define FMT_16_BIT(fmt, vc) [fmt] = { 1, 1, 2, vc, CHIP_R600 }
  84. #define FMT_24_BIT(fmt) [fmt] = { 1, 1, 4, 0, CHIP_R600 }
  85. #define FMT_32_BIT(fmt, vc) [fmt] = { 1, 1, 4, vc, CHIP_R600 }
  86. #define FMT_48_BIT(fmt) [fmt] = { 1, 1, 8, 0, CHIP_R600 }
  87. #define FMT_64_BIT(fmt, vc) [fmt] = { 1, 1, 8, vc, CHIP_R600 }
  88. #define FMT_96_BIT(fmt) [fmt] = { 1, 1, 12, 0, CHIP_R600 }
  89. #define FMT_128_BIT(fmt, vc) [fmt] = { 1, 1, 16,vc, CHIP_R600 }
  90. struct gpu_formats {
  91. unsigned blockwidth;
  92. unsigned blockheight;
  93. unsigned blocksize;
  94. unsigned valid_color;
  95. enum radeon_family min_family;
  96. };
  97. static const struct gpu_formats color_formats_table[] = {
  98. /* 8 bit */
  99. FMT_8_BIT(V_038004_COLOR_8, 1),
  100. FMT_8_BIT(V_038004_COLOR_4_4, 1),
  101. FMT_8_BIT(V_038004_COLOR_3_3_2, 1),
  102. FMT_8_BIT(V_038004_FMT_1, 0),
  103. /* 16-bit */
  104. FMT_16_BIT(V_038004_COLOR_16, 1),
  105. FMT_16_BIT(V_038004_COLOR_16_FLOAT, 1),
  106. FMT_16_BIT(V_038004_COLOR_8_8, 1),
  107. FMT_16_BIT(V_038004_COLOR_5_6_5, 1),
  108. FMT_16_BIT(V_038004_COLOR_6_5_5, 1),
  109. FMT_16_BIT(V_038004_COLOR_1_5_5_5, 1),
  110. FMT_16_BIT(V_038004_COLOR_4_4_4_4, 1),
  111. FMT_16_BIT(V_038004_COLOR_5_5_5_1, 1),
  112. /* 24-bit */
  113. FMT_24_BIT(V_038004_FMT_8_8_8),
  114. /* 32-bit */
  115. FMT_32_BIT(V_038004_COLOR_32, 1),
  116. FMT_32_BIT(V_038004_COLOR_32_FLOAT, 1),
  117. FMT_32_BIT(V_038004_COLOR_16_16, 1),
  118. FMT_32_BIT(V_038004_COLOR_16_16_FLOAT, 1),
  119. FMT_32_BIT(V_038004_COLOR_8_24, 1),
  120. FMT_32_BIT(V_038004_COLOR_8_24_FLOAT, 1),
  121. FMT_32_BIT(V_038004_COLOR_24_8, 1),
  122. FMT_32_BIT(V_038004_COLOR_24_8_FLOAT, 1),
  123. FMT_32_BIT(V_038004_COLOR_10_11_11, 1),
  124. FMT_32_BIT(V_038004_COLOR_10_11_11_FLOAT, 1),
  125. FMT_32_BIT(V_038004_COLOR_11_11_10, 1),
  126. FMT_32_BIT(V_038004_COLOR_11_11_10_FLOAT, 1),
  127. FMT_32_BIT(V_038004_COLOR_2_10_10_10, 1),
  128. FMT_32_BIT(V_038004_COLOR_8_8_8_8, 1),
  129. FMT_32_BIT(V_038004_COLOR_10_10_10_2, 1),
  130. FMT_32_BIT(V_038004_FMT_5_9_9_9_SHAREDEXP, 0),
  131. FMT_32_BIT(V_038004_FMT_32_AS_8, 0),
  132. FMT_32_BIT(V_038004_FMT_32_AS_8_8, 0),
  133. /* 48-bit */
  134. FMT_48_BIT(V_038004_FMT_16_16_16),
  135. FMT_48_BIT(V_038004_FMT_16_16_16_FLOAT),
  136. /* 64-bit */
  137. FMT_64_BIT(V_038004_COLOR_X24_8_32_FLOAT, 1),
  138. FMT_64_BIT(V_038004_COLOR_32_32, 1),
  139. FMT_64_BIT(V_038004_COLOR_32_32_FLOAT, 1),
  140. FMT_64_BIT(V_038004_COLOR_16_16_16_16, 1),
  141. FMT_64_BIT(V_038004_COLOR_16_16_16_16_FLOAT, 1),
  142. FMT_96_BIT(V_038004_FMT_32_32_32),
  143. FMT_96_BIT(V_038004_FMT_32_32_32_FLOAT),
  144. /* 128-bit */
  145. FMT_128_BIT(V_038004_COLOR_32_32_32_32, 1),
  146. FMT_128_BIT(V_038004_COLOR_32_32_32_32_FLOAT, 1),
  147. [V_038004_FMT_GB_GR] = { 2, 1, 4, 0 },
  148. [V_038004_FMT_BG_RG] = { 2, 1, 4, 0 },
  149. /* block compressed formats */
  150. [V_038004_FMT_BC1] = { 4, 4, 8, 0 },
  151. [V_038004_FMT_BC2] = { 4, 4, 16, 0 },
  152. [V_038004_FMT_BC3] = { 4, 4, 16, 0 },
  153. [V_038004_FMT_BC4] = { 4, 4, 8, 0 },
  154. [V_038004_FMT_BC5] = { 4, 4, 16, 0},
  155. [V_038004_FMT_BC6] = { 4, 4, 16, 0, CHIP_CEDAR}, /* Evergreen-only */
  156. [V_038004_FMT_BC7] = { 4, 4, 16, 0, CHIP_CEDAR}, /* Evergreen-only */
  157. /* The other Evergreen formats */
  158. [V_038004_FMT_32_AS_32_32_32_32] = { 1, 1, 4, 0, CHIP_CEDAR},
  159. };
  160. bool r600_fmt_is_valid_color(u32 format)
  161. {
  162. if (format >= ARRAY_SIZE(color_formats_table))
  163. return false;
  164. if (color_formats_table[format].valid_color)
  165. return true;
  166. return false;
  167. }
  168. bool r600_fmt_is_valid_texture(u32 format, enum radeon_family family)
  169. {
  170. if (format >= ARRAY_SIZE(color_formats_table))
  171. return false;
  172. if (family < color_formats_table[format].min_family)
  173. return false;
  174. if (color_formats_table[format].blockwidth > 0)
  175. return true;
  176. return false;
  177. }
  178. int r600_fmt_get_blocksize(u32 format)
  179. {
  180. if (format >= ARRAY_SIZE(color_formats_table))
  181. return 0;
  182. return color_formats_table[format].blocksize;
  183. }
  184. int r600_fmt_get_nblocksx(u32 format, u32 w)
  185. {
  186. unsigned bw;
  187. if (format >= ARRAY_SIZE(color_formats_table))
  188. return 0;
  189. bw = color_formats_table[format].blockwidth;
  190. if (bw == 0)
  191. return 0;
  192. return (w + bw - 1) / bw;
  193. }
  194. int r600_fmt_get_nblocksy(u32 format, u32 h)
  195. {
  196. unsigned bh;
  197. if (format >= ARRAY_SIZE(color_formats_table))
  198. return 0;
  199. bh = color_formats_table[format].blockheight;
  200. if (bh == 0)
  201. return 0;
  202. return (h + bh - 1) / bh;
  203. }
  204. struct array_mode_checker {
  205. int array_mode;
  206. u32 group_size;
  207. u32 nbanks;
  208. u32 npipes;
  209. u32 nsamples;
  210. u32 blocksize;
  211. };
  212. /* returns alignment in pixels for pitch/height/depth and bytes for base */
  213. static int r600_get_array_mode_alignment(struct array_mode_checker *values,
  214. u32 *pitch_align,
  215. u32 *height_align,
  216. u32 *depth_align,
  217. u64 *base_align)
  218. {
  219. u32 tile_width = 8;
  220. u32 tile_height = 8;
  221. u32 macro_tile_width = values->nbanks;
  222. u32 macro_tile_height = values->npipes;
  223. u32 tile_bytes = tile_width * tile_height * values->blocksize * values->nsamples;
  224. u32 macro_tile_bytes = macro_tile_width * macro_tile_height * tile_bytes;
  225. switch (values->array_mode) {
  226. case ARRAY_LINEAR_GENERAL:
  227. /* technically tile_width/_height for pitch/height */
  228. *pitch_align = 1; /* tile_width */
  229. *height_align = 1; /* tile_height */
  230. *depth_align = 1;
  231. *base_align = 1;
  232. break;
  233. case ARRAY_LINEAR_ALIGNED:
  234. *pitch_align = max((u32)64, (u32)(values->group_size / values->blocksize));
  235. *height_align = 1;
  236. *depth_align = 1;
  237. *base_align = values->group_size;
  238. break;
  239. case ARRAY_1D_TILED_THIN1:
  240. *pitch_align = max((u32)tile_width,
  241. (u32)(values->group_size /
  242. (tile_height * values->blocksize * values->nsamples)));
  243. *height_align = tile_height;
  244. *depth_align = 1;
  245. *base_align = values->group_size;
  246. break;
  247. case ARRAY_2D_TILED_THIN1:
  248. *pitch_align = max((u32)macro_tile_width * tile_width,
  249. (u32)((values->group_size * values->nbanks) /
  250. (values->blocksize * values->nsamples * tile_width)));
  251. *height_align = macro_tile_height * tile_height;
  252. *depth_align = 1;
  253. *base_align = max(macro_tile_bytes,
  254. (*pitch_align) * values->blocksize * (*height_align) * values->nsamples);
  255. break;
  256. default:
  257. return -EINVAL;
  258. }
  259. return 0;
  260. }
  261. static void r600_cs_track_init(struct r600_cs_track *track)
  262. {
  263. int i;
  264. /* assume DX9 mode */
  265. track->sq_config = DX9_CONSTS;
  266. for (i = 0; i < 8; i++) {
  267. track->cb_color_base_last[i] = 0;
  268. track->cb_color_size[i] = 0;
  269. track->cb_color_size_idx[i] = 0;
  270. track->cb_color_info[i] = 0;
  271. track->cb_color_view[i] = 0xFFFFFFFF;
  272. track->cb_color_bo[i] = NULL;
  273. track->cb_color_bo_offset[i] = 0xFFFFFFFF;
  274. track->cb_color_bo_mc[i] = 0xFFFFFFFF;
  275. track->cb_color_frag_bo[i] = NULL;
  276. track->cb_color_frag_offset[i] = 0xFFFFFFFF;
  277. track->cb_color_tile_bo[i] = NULL;
  278. track->cb_color_tile_offset[i] = 0xFFFFFFFF;
  279. track->cb_color_mask[i] = 0xFFFFFFFF;
  280. }
  281. track->is_resolve = false;
  282. track->nsamples = 16;
  283. track->log_nsamples = 4;
  284. track->cb_target_mask = 0xFFFFFFFF;
  285. track->cb_shader_mask = 0xFFFFFFFF;
  286. track->cb_dirty = true;
  287. track->db_bo = NULL;
  288. track->db_bo_mc = 0xFFFFFFFF;
  289. /* assume the biggest format and that htile is enabled */
  290. track->db_depth_info = 7 | (1 << 25);
  291. track->db_depth_view = 0xFFFFC000;
  292. track->db_depth_size = 0xFFFFFFFF;
  293. track->db_depth_size_idx = 0;
  294. track->db_depth_control = 0xFFFFFFFF;
  295. track->db_dirty = true;
  296. track->htile_bo = NULL;
  297. track->htile_offset = 0xFFFFFFFF;
  298. track->htile_surface = 0;
  299. for (i = 0; i < 4; i++) {
  300. track->vgt_strmout_size[i] = 0;
  301. track->vgt_strmout_bo[i] = NULL;
  302. track->vgt_strmout_bo_offset[i] = 0xFFFFFFFF;
  303. track->vgt_strmout_bo_mc[i] = 0xFFFFFFFF;
  304. }
  305. track->streamout_dirty = true;
  306. track->sx_misc_kill_all_prims = false;
  307. }
  308. static int r600_cs_track_validate_cb(struct radeon_cs_parser *p, int i)
  309. {
  310. struct r600_cs_track *track = p->track;
  311. u32 slice_tile_max, size, tmp;
  312. u32 height, height_align, pitch, pitch_align, depth_align;
  313. u64 base_offset, base_align;
  314. struct array_mode_checker array_check;
  315. volatile u32 *ib = p->ib.ptr;
  316. unsigned array_mode;
  317. u32 format;
  318. /* When resolve is used, the second colorbuffer has always 1 sample. */
  319. unsigned nsamples = track->is_resolve && i == 1 ? 1 : track->nsamples;
  320. size = radeon_bo_size(track->cb_color_bo[i]) - track->cb_color_bo_offset[i];
  321. format = G_0280A0_FORMAT(track->cb_color_info[i]);
  322. if (!r600_fmt_is_valid_color(format)) {
  323. dev_warn(p->dev, "%s:%d cb invalid format %d for %d (0x%08X)\n",
  324. __func__, __LINE__, format,
  325. i, track->cb_color_info[i]);
  326. return -EINVAL;
  327. }
  328. /* pitch in pixels */
  329. pitch = (G_028060_PITCH_TILE_MAX(track->cb_color_size[i]) + 1) * 8;
  330. slice_tile_max = G_028060_SLICE_TILE_MAX(track->cb_color_size[i]) + 1;
  331. slice_tile_max *= 64;
  332. height = slice_tile_max / pitch;
  333. if (height > 8192)
  334. height = 8192;
  335. array_mode = G_0280A0_ARRAY_MODE(track->cb_color_info[i]);
  336. base_offset = track->cb_color_bo_mc[i] + track->cb_color_bo_offset[i];
  337. array_check.array_mode = array_mode;
  338. array_check.group_size = track->group_size;
  339. array_check.nbanks = track->nbanks;
  340. array_check.npipes = track->npipes;
  341. array_check.nsamples = nsamples;
  342. array_check.blocksize = r600_fmt_get_blocksize(format);
  343. if (r600_get_array_mode_alignment(&array_check,
  344. &pitch_align, &height_align, &depth_align, &base_align)) {
  345. dev_warn(p->dev, "%s invalid tiling %d for %d (0x%08X)\n", __func__,
  346. G_0280A0_ARRAY_MODE(track->cb_color_info[i]), i,
  347. track->cb_color_info[i]);
  348. return -EINVAL;
  349. }
  350. switch (array_mode) {
  351. case V_0280A0_ARRAY_LINEAR_GENERAL:
  352. break;
  353. case V_0280A0_ARRAY_LINEAR_ALIGNED:
  354. break;
  355. case V_0280A0_ARRAY_1D_TILED_THIN1:
  356. /* avoid breaking userspace */
  357. if (height > 7)
  358. height &= ~0x7;
  359. break;
  360. case V_0280A0_ARRAY_2D_TILED_THIN1:
  361. break;
  362. default:
  363. dev_warn(p->dev, "%s invalid tiling %d for %d (0x%08X)\n", __func__,
  364. G_0280A0_ARRAY_MODE(track->cb_color_info[i]), i,
  365. track->cb_color_info[i]);
  366. return -EINVAL;
  367. }
  368. if (!IS_ALIGNED(pitch, pitch_align)) {
  369. dev_warn(p->dev, "%s:%d cb pitch (%d, 0x%x, %d) invalid\n",
  370. __func__, __LINE__, pitch, pitch_align, array_mode);
  371. return -EINVAL;
  372. }
  373. if (!IS_ALIGNED(height, height_align)) {
  374. dev_warn(p->dev, "%s:%d cb height (%d, 0x%x, %d) invalid\n",
  375. __func__, __LINE__, height, height_align, array_mode);
  376. return -EINVAL;
  377. }
  378. if (!IS_ALIGNED(base_offset, base_align)) {
  379. dev_warn(p->dev, "%s offset[%d] 0x%llx 0x%llx, %d not aligned\n", __func__, i,
  380. base_offset, base_align, array_mode);
  381. return -EINVAL;
  382. }
  383. /* check offset */
  384. tmp = r600_fmt_get_nblocksy(format, height) * r600_fmt_get_nblocksx(format, pitch) *
  385. r600_fmt_get_blocksize(format) * nsamples;
  386. switch (array_mode) {
  387. default:
  388. case V_0280A0_ARRAY_LINEAR_GENERAL:
  389. case V_0280A0_ARRAY_LINEAR_ALIGNED:
  390. tmp += track->cb_color_view[i] & 0xFF;
  391. break;
  392. case V_0280A0_ARRAY_1D_TILED_THIN1:
  393. case V_0280A0_ARRAY_2D_TILED_THIN1:
  394. tmp += G_028080_SLICE_MAX(track->cb_color_view[i]) * tmp;
  395. break;
  396. }
  397. if ((tmp + track->cb_color_bo_offset[i]) > radeon_bo_size(track->cb_color_bo[i])) {
  398. if (array_mode == V_0280A0_ARRAY_LINEAR_GENERAL) {
  399. /* the initial DDX does bad things with the CB size occasionally */
  400. /* it rounds up height too far for slice tile max but the BO is smaller */
  401. /* r600c,g also seem to flush at bad times in some apps resulting in
  402. * bogus values here. So for linear just allow anything to avoid breaking
  403. * broken userspace.
  404. */
  405. } else {
  406. dev_warn(p->dev, "%s offset[%d] %d %llu %d %lu too big (%d %d) (%d %d %d)\n",
  407. __func__, i, array_mode,
  408. track->cb_color_bo_offset[i], tmp,
  409. radeon_bo_size(track->cb_color_bo[i]),
  410. pitch, height, r600_fmt_get_nblocksx(format, pitch),
  411. r600_fmt_get_nblocksy(format, height),
  412. r600_fmt_get_blocksize(format));
  413. return -EINVAL;
  414. }
  415. }
  416. /* limit max tile */
  417. tmp = (height * pitch) >> 6;
  418. if (tmp < slice_tile_max)
  419. slice_tile_max = tmp;
  420. tmp = S_028060_PITCH_TILE_MAX((pitch / 8) - 1) |
  421. S_028060_SLICE_TILE_MAX(slice_tile_max - 1);
  422. ib[track->cb_color_size_idx[i]] = tmp;
  423. /* FMASK/CMASK */
  424. switch (G_0280A0_TILE_MODE(track->cb_color_info[i])) {
  425. case V_0280A0_TILE_DISABLE:
  426. break;
  427. case V_0280A0_FRAG_ENABLE:
  428. if (track->nsamples > 1) {
  429. uint32_t tile_max = G_028100_FMASK_TILE_MAX(track->cb_color_mask[i]);
  430. /* the tile size is 8x8, but the size is in units of bits.
  431. * for bytes, do just * 8. */
  432. uint32_t bytes = track->nsamples * track->log_nsamples * 8 * (tile_max + 1);
  433. if (bytes + track->cb_color_frag_offset[i] >
  434. radeon_bo_size(track->cb_color_frag_bo[i])) {
  435. dev_warn(p->dev, "%s FMASK_TILE_MAX too large "
  436. "(tile_max=%u, bytes=%u, offset=%llu, bo_size=%lu)\n",
  437. __func__, tile_max, bytes,
  438. track->cb_color_frag_offset[i],
  439. radeon_bo_size(track->cb_color_frag_bo[i]));
  440. return -EINVAL;
  441. }
  442. }
  443. /* fall through */
  444. case V_0280A0_CLEAR_ENABLE:
  445. {
  446. uint32_t block_max = G_028100_CMASK_BLOCK_MAX(track->cb_color_mask[i]);
  447. /* One block = 128x128 pixels, one 8x8 tile has 4 bits..
  448. * (128*128) / (8*8) / 2 = 128 bytes per block. */
  449. uint32_t bytes = (block_max + 1) * 128;
  450. if (bytes + track->cb_color_tile_offset[i] >
  451. radeon_bo_size(track->cb_color_tile_bo[i])) {
  452. dev_warn(p->dev, "%s CMASK_BLOCK_MAX too large "
  453. "(block_max=%u, bytes=%u, offset=%llu, bo_size=%lu)\n",
  454. __func__, block_max, bytes,
  455. track->cb_color_tile_offset[i],
  456. radeon_bo_size(track->cb_color_tile_bo[i]));
  457. return -EINVAL;
  458. }
  459. break;
  460. }
  461. default:
  462. dev_warn(p->dev, "%s invalid tile mode\n", __func__);
  463. return -EINVAL;
  464. }
  465. return 0;
  466. }
  467. static int r600_cs_track_validate_db(struct radeon_cs_parser *p)
  468. {
  469. struct r600_cs_track *track = p->track;
  470. u32 nviews, bpe, ntiles, size, slice_tile_max, tmp;
  471. u32 height_align, pitch_align, depth_align;
  472. u32 pitch = 8192;
  473. u32 height = 8192;
  474. u64 base_offset, base_align;
  475. struct array_mode_checker array_check;
  476. int array_mode;
  477. volatile u32 *ib = p->ib.ptr;
  478. if (track->db_bo == NULL) {
  479. dev_warn(p->dev, "z/stencil with no depth buffer\n");
  480. return -EINVAL;
  481. }
  482. switch (G_028010_FORMAT(track->db_depth_info)) {
  483. case V_028010_DEPTH_16:
  484. bpe = 2;
  485. break;
  486. case V_028010_DEPTH_X8_24:
  487. case V_028010_DEPTH_8_24:
  488. case V_028010_DEPTH_X8_24_FLOAT:
  489. case V_028010_DEPTH_8_24_FLOAT:
  490. case V_028010_DEPTH_32_FLOAT:
  491. bpe = 4;
  492. break;
  493. case V_028010_DEPTH_X24_8_32_FLOAT:
  494. bpe = 8;
  495. break;
  496. default:
  497. dev_warn(p->dev, "z/stencil with invalid format %d\n", G_028010_FORMAT(track->db_depth_info));
  498. return -EINVAL;
  499. }
  500. if ((track->db_depth_size & 0xFFFFFC00) == 0xFFFFFC00) {
  501. if (!track->db_depth_size_idx) {
  502. dev_warn(p->dev, "z/stencil buffer size not set\n");
  503. return -EINVAL;
  504. }
  505. tmp = radeon_bo_size(track->db_bo) - track->db_offset;
  506. tmp = (tmp / bpe) >> 6;
  507. if (!tmp) {
  508. dev_warn(p->dev, "z/stencil buffer too small (0x%08X %d %d %ld)\n",
  509. track->db_depth_size, bpe, track->db_offset,
  510. radeon_bo_size(track->db_bo));
  511. return -EINVAL;
  512. }
  513. ib[track->db_depth_size_idx] = S_028000_SLICE_TILE_MAX(tmp - 1) | (track->db_depth_size & 0x3FF);
  514. } else {
  515. size = radeon_bo_size(track->db_bo);
  516. /* pitch in pixels */
  517. pitch = (G_028000_PITCH_TILE_MAX(track->db_depth_size) + 1) * 8;
  518. slice_tile_max = G_028000_SLICE_TILE_MAX(track->db_depth_size) + 1;
  519. slice_tile_max *= 64;
  520. height = slice_tile_max / pitch;
  521. if (height > 8192)
  522. height = 8192;
  523. base_offset = track->db_bo_mc + track->db_offset;
  524. array_mode = G_028010_ARRAY_MODE(track->db_depth_info);
  525. array_check.array_mode = array_mode;
  526. array_check.group_size = track->group_size;
  527. array_check.nbanks = track->nbanks;
  528. array_check.npipes = track->npipes;
  529. array_check.nsamples = track->nsamples;
  530. array_check.blocksize = bpe;
  531. if (r600_get_array_mode_alignment(&array_check,
  532. &pitch_align, &height_align, &depth_align, &base_align)) {
  533. dev_warn(p->dev, "%s invalid tiling %d (0x%08X)\n", __func__,
  534. G_028010_ARRAY_MODE(track->db_depth_info),
  535. track->db_depth_info);
  536. return -EINVAL;
  537. }
  538. switch (array_mode) {
  539. case V_028010_ARRAY_1D_TILED_THIN1:
  540. /* don't break userspace */
  541. height &= ~0x7;
  542. break;
  543. case V_028010_ARRAY_2D_TILED_THIN1:
  544. break;
  545. default:
  546. dev_warn(p->dev, "%s invalid tiling %d (0x%08X)\n", __func__,
  547. G_028010_ARRAY_MODE(track->db_depth_info),
  548. track->db_depth_info);
  549. return -EINVAL;
  550. }
  551. if (!IS_ALIGNED(pitch, pitch_align)) {
  552. dev_warn(p->dev, "%s:%d db pitch (%d, 0x%x, %d) invalid\n",
  553. __func__, __LINE__, pitch, pitch_align, array_mode);
  554. return -EINVAL;
  555. }
  556. if (!IS_ALIGNED(height, height_align)) {
  557. dev_warn(p->dev, "%s:%d db height (%d, 0x%x, %d) invalid\n",
  558. __func__, __LINE__, height, height_align, array_mode);
  559. return -EINVAL;
  560. }
  561. if (!IS_ALIGNED(base_offset, base_align)) {
  562. dev_warn(p->dev, "%s offset 0x%llx, 0x%llx, %d not aligned\n", __func__,
  563. base_offset, base_align, array_mode);
  564. return -EINVAL;
  565. }
  566. ntiles = G_028000_SLICE_TILE_MAX(track->db_depth_size) + 1;
  567. nviews = G_028004_SLICE_MAX(track->db_depth_view) + 1;
  568. tmp = ntiles * bpe * 64 * nviews * track->nsamples;
  569. if ((tmp + track->db_offset) > radeon_bo_size(track->db_bo)) {
  570. dev_warn(p->dev, "z/stencil buffer (%d) too small (0x%08X %d %d %d -> %u have %lu)\n",
  571. array_mode,
  572. track->db_depth_size, ntiles, nviews, bpe, tmp + track->db_offset,
  573. radeon_bo_size(track->db_bo));
  574. return -EINVAL;
  575. }
  576. }
  577. /* hyperz */
  578. if (G_028010_TILE_SURFACE_ENABLE(track->db_depth_info)) {
  579. unsigned long size;
  580. unsigned nbx, nby;
  581. if (track->htile_bo == NULL) {
  582. dev_warn(p->dev, "%s:%d htile enabled without htile surface 0x%08x\n",
  583. __func__, __LINE__, track->db_depth_info);
  584. return -EINVAL;
  585. }
  586. if ((track->db_depth_size & 0xFFFFFC00) == 0xFFFFFC00) {
  587. dev_warn(p->dev, "%s:%d htile can't be enabled with bogus db_depth_size 0x%08x\n",
  588. __func__, __LINE__, track->db_depth_size);
  589. return -EINVAL;
  590. }
  591. nbx = pitch;
  592. nby = height;
  593. if (G_028D24_LINEAR(track->htile_surface)) {
  594. /* nbx must be 16 htiles aligned == 16 * 8 pixel aligned */
  595. nbx = round_up(nbx, 16 * 8);
  596. /* nby is npipes htiles aligned == npipes * 8 pixel aligned */
  597. nby = round_up(nby, track->npipes * 8);
  598. } else {
  599. /* always assume 8x8 htile */
  600. /* align is htile align * 8, htile align vary according to
  601. * number of pipe and tile width and nby
  602. */
  603. switch (track->npipes) {
  604. case 8:
  605. /* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
  606. nbx = round_up(nbx, 64 * 8);
  607. nby = round_up(nby, 64 * 8);
  608. break;
  609. case 4:
  610. /* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
  611. nbx = round_up(nbx, 64 * 8);
  612. nby = round_up(nby, 32 * 8);
  613. break;
  614. case 2:
  615. /* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
  616. nbx = round_up(nbx, 32 * 8);
  617. nby = round_up(nby, 32 * 8);
  618. break;
  619. case 1:
  620. /* HTILE_WIDTH = 8 & HTILE_HEIGHT = 8*/
  621. nbx = round_up(nbx, 32 * 8);
  622. nby = round_up(nby, 16 * 8);
  623. break;
  624. default:
  625. dev_warn(p->dev, "%s:%d invalid num pipes %d\n",
  626. __func__, __LINE__, track->npipes);
  627. return -EINVAL;
  628. }
  629. }
  630. /* compute number of htile */
  631. nbx = nbx >> 3;
  632. nby = nby >> 3;
  633. /* size must be aligned on npipes * 2K boundary */
  634. size = roundup(nbx * nby * 4, track->npipes * (2 << 10));
  635. size += track->htile_offset;
  636. if (size > radeon_bo_size(track->htile_bo)) {
  637. dev_warn(p->dev, "%s:%d htile surface too small %ld for %ld (%d %d)\n",
  638. __func__, __LINE__, radeon_bo_size(track->htile_bo),
  639. size, nbx, nby);
  640. return -EINVAL;
  641. }
  642. }
  643. track->db_dirty = false;
  644. return 0;
  645. }
  646. static int r600_cs_track_check(struct radeon_cs_parser *p)
  647. {
  648. struct r600_cs_track *track = p->track;
  649. u32 tmp;
  650. int r, i;
  651. /* on legacy kernel we don't perform advanced check */
  652. if (p->rdev == NULL)
  653. return 0;
  654. /* check streamout */
  655. if (track->streamout_dirty && track->vgt_strmout_en) {
  656. for (i = 0; i < 4; i++) {
  657. if (track->vgt_strmout_buffer_en & (1 << i)) {
  658. if (track->vgt_strmout_bo[i]) {
  659. u64 offset = (u64)track->vgt_strmout_bo_offset[i] +
  660. (u64)track->vgt_strmout_size[i];
  661. if (offset > radeon_bo_size(track->vgt_strmout_bo[i])) {
  662. DRM_ERROR("streamout %d bo too small: 0x%llx, 0x%lx\n",
  663. i, offset,
  664. radeon_bo_size(track->vgt_strmout_bo[i]));
  665. return -EINVAL;
  666. }
  667. } else {
  668. dev_warn(p->dev, "No buffer for streamout %d\n", i);
  669. return -EINVAL;
  670. }
  671. }
  672. }
  673. track->streamout_dirty = false;
  674. }
  675. if (track->sx_misc_kill_all_prims)
  676. return 0;
  677. /* check that we have a cb for each enabled target, we don't check
  678. * shader_mask because it seems mesa isn't always setting it :(
  679. */
  680. if (track->cb_dirty) {
  681. tmp = track->cb_target_mask;
  682. /* We must check both colorbuffers for RESOLVE. */
  683. if (track->is_resolve) {
  684. tmp |= 0xff;
  685. }
  686. for (i = 0; i < 8; i++) {
  687. u32 format = G_0280A0_FORMAT(track->cb_color_info[i]);
  688. if (format != V_0280A0_COLOR_INVALID &&
  689. (tmp >> (i * 4)) & 0xF) {
  690. /* at least one component is enabled */
  691. if (track->cb_color_bo[i] == NULL) {
  692. dev_warn(p->dev, "%s:%d mask 0x%08X | 0x%08X no cb for %d\n",
  693. __func__, __LINE__, track->cb_target_mask, track->cb_shader_mask, i);
  694. return -EINVAL;
  695. }
  696. /* perform rewrite of CB_COLOR[0-7]_SIZE */
  697. r = r600_cs_track_validate_cb(p, i);
  698. if (r)
  699. return r;
  700. }
  701. }
  702. track->cb_dirty = false;
  703. }
  704. /* Check depth buffer */
  705. if (track->db_dirty &&
  706. G_028010_FORMAT(track->db_depth_info) != V_028010_DEPTH_INVALID &&
  707. (G_028800_STENCIL_ENABLE(track->db_depth_control) ||
  708. G_028800_Z_ENABLE(track->db_depth_control))) {
  709. r = r600_cs_track_validate_db(p);
  710. if (r)
  711. return r;
  712. }
  713. return 0;
  714. }
  715. /**
  716. * r600_cs_packet_parse_vline() - parse userspace VLINE packet
  717. * @parser: parser structure holding parsing context.
  718. *
  719. * This is an R600-specific function for parsing VLINE packets.
  720. * Real work is done by r600_cs_common_vline_parse function.
  721. * Here we just set up ASIC-specific register table and call
  722. * the common implementation function.
  723. */
  724. static int r600_cs_packet_parse_vline(struct radeon_cs_parser *p)
  725. {
  726. static uint32_t vline_start_end[2] = {AVIVO_D1MODE_VLINE_START_END,
  727. AVIVO_D2MODE_VLINE_START_END};
  728. static uint32_t vline_status[2] = {AVIVO_D1MODE_VLINE_STATUS,
  729. AVIVO_D2MODE_VLINE_STATUS};
  730. return r600_cs_common_vline_parse(p, vline_start_end, vline_status);
  731. }
  732. /**
  733. * r600_cs_common_vline_parse() - common vline parser
  734. * @parser: parser structure holding parsing context.
  735. * @vline_start_end: table of vline_start_end registers
  736. * @vline_status: table of vline_status registers
  737. *
  738. * Userspace sends a special sequence for VLINE waits.
  739. * PACKET0 - VLINE_START_END + value
  740. * PACKET3 - WAIT_REG_MEM poll vline status reg
  741. * RELOC (P3) - crtc_id in reloc.
  742. *
  743. * This function parses this and relocates the VLINE START END
  744. * and WAIT_REG_MEM packets to the correct crtc.
  745. * It also detects a switched off crtc and nulls out the
  746. * wait in that case. This function is common for all ASICs that
  747. * are R600 and newer. The parsing algorithm is the same, and only
  748. * differs in which registers are used.
  749. *
  750. * Caller is the ASIC-specific function which passes the parser
  751. * context and ASIC-specific register table
  752. */
  753. int r600_cs_common_vline_parse(struct radeon_cs_parser *p,
  754. uint32_t *vline_start_end,
  755. uint32_t *vline_status)
  756. {
  757. struct drm_mode_object *obj;
  758. struct drm_crtc *crtc;
  759. struct radeon_crtc *radeon_crtc;
  760. struct radeon_cs_packet p3reloc, wait_reg_mem;
  761. int crtc_id;
  762. int r;
  763. uint32_t header, h_idx, reg, wait_reg_mem_info;
  764. volatile uint32_t *ib;
  765. ib = p->ib.ptr;
  766. /* parse the WAIT_REG_MEM */
  767. r = radeon_cs_packet_parse(p, &wait_reg_mem, p->idx);
  768. if (r)
  769. return r;
  770. /* check its a WAIT_REG_MEM */
  771. if (wait_reg_mem.type != RADEON_PACKET_TYPE3 ||
  772. wait_reg_mem.opcode != PACKET3_WAIT_REG_MEM) {
  773. DRM_ERROR("vline wait missing WAIT_REG_MEM segment\n");
  774. return -EINVAL;
  775. }
  776. wait_reg_mem_info = radeon_get_ib_value(p, wait_reg_mem.idx + 1);
  777. /* bit 4 is reg (0) or mem (1) */
  778. if (wait_reg_mem_info & 0x10) {
  779. DRM_ERROR("vline WAIT_REG_MEM waiting on MEM instead of REG\n");
  780. return -EINVAL;
  781. }
  782. /* bit 8 is me (0) or pfp (1) */
  783. if (wait_reg_mem_info & 0x100) {
  784. DRM_ERROR("vline WAIT_REG_MEM waiting on PFP instead of ME\n");
  785. return -EINVAL;
  786. }
  787. /* waiting for value to be equal */
  788. if ((wait_reg_mem_info & 0x7) != 0x3) {
  789. DRM_ERROR("vline WAIT_REG_MEM function not equal\n");
  790. return -EINVAL;
  791. }
  792. if ((radeon_get_ib_value(p, wait_reg_mem.idx + 2) << 2) != vline_status[0]) {
  793. DRM_ERROR("vline WAIT_REG_MEM bad reg\n");
  794. return -EINVAL;
  795. }
  796. if (radeon_get_ib_value(p, wait_reg_mem.idx + 5) != RADEON_VLINE_STAT) {
  797. DRM_ERROR("vline WAIT_REG_MEM bad bit mask\n");
  798. return -EINVAL;
  799. }
  800. /* jump over the NOP */
  801. r = radeon_cs_packet_parse(p, &p3reloc, p->idx + wait_reg_mem.count + 2);
  802. if (r)
  803. return r;
  804. h_idx = p->idx - 2;
  805. p->idx += wait_reg_mem.count + 2;
  806. p->idx += p3reloc.count + 2;
  807. header = radeon_get_ib_value(p, h_idx);
  808. crtc_id = radeon_get_ib_value(p, h_idx + 2 + 7 + 1);
  809. reg = R600_CP_PACKET0_GET_REG(header);
  810. obj = drm_mode_object_find(p->rdev->ddev, crtc_id, DRM_MODE_OBJECT_CRTC);
  811. if (!obj) {
  812. DRM_ERROR("cannot find crtc %d\n", crtc_id);
  813. return -ENOENT;
  814. }
  815. crtc = obj_to_crtc(obj);
  816. radeon_crtc = to_radeon_crtc(crtc);
  817. crtc_id = radeon_crtc->crtc_id;
  818. if (!crtc->enabled) {
  819. /* CRTC isn't enabled - we need to nop out the WAIT_REG_MEM */
  820. ib[h_idx + 2] = PACKET2(0);
  821. ib[h_idx + 3] = PACKET2(0);
  822. ib[h_idx + 4] = PACKET2(0);
  823. ib[h_idx + 5] = PACKET2(0);
  824. ib[h_idx + 6] = PACKET2(0);
  825. ib[h_idx + 7] = PACKET2(0);
  826. ib[h_idx + 8] = PACKET2(0);
  827. } else if (reg == vline_start_end[0]) {
  828. header &= ~R600_CP_PACKET0_REG_MASK;
  829. header |= vline_start_end[crtc_id] >> 2;
  830. ib[h_idx] = header;
  831. ib[h_idx + 4] = vline_status[crtc_id] >> 2;
  832. } else {
  833. DRM_ERROR("unknown crtc reloc\n");
  834. return -EINVAL;
  835. }
  836. return 0;
  837. }
  838. static int r600_packet0_check(struct radeon_cs_parser *p,
  839. struct radeon_cs_packet *pkt,
  840. unsigned idx, unsigned reg)
  841. {
  842. int r;
  843. switch (reg) {
  844. case AVIVO_D1MODE_VLINE_START_END:
  845. r = r600_cs_packet_parse_vline(p);
  846. if (r) {
  847. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  848. idx, reg);
  849. return r;
  850. }
  851. break;
  852. default:
  853. printk(KERN_ERR "Forbidden register 0x%04X in cs at %d\n",
  854. reg, idx);
  855. return -EINVAL;
  856. }
  857. return 0;
  858. }
  859. static int r600_cs_parse_packet0(struct radeon_cs_parser *p,
  860. struct radeon_cs_packet *pkt)
  861. {
  862. unsigned reg, i;
  863. unsigned idx;
  864. int r;
  865. idx = pkt->idx + 1;
  866. reg = pkt->reg;
  867. for (i = 0; i <= pkt->count; i++, idx++, reg += 4) {
  868. r = r600_packet0_check(p, pkt, idx, reg);
  869. if (r) {
  870. return r;
  871. }
  872. }
  873. return 0;
  874. }
  875. /**
  876. * r600_cs_check_reg() - check if register is authorized or not
  877. * @parser: parser structure holding parsing context
  878. * @reg: register we are testing
  879. * @idx: index into the cs buffer
  880. *
  881. * This function will test against r600_reg_safe_bm and return 0
  882. * if register is safe. If register is not flag as safe this function
  883. * will test it against a list of register needind special handling.
  884. */
  885. static int r600_cs_check_reg(struct radeon_cs_parser *p, u32 reg, u32 idx)
  886. {
  887. struct r600_cs_track *track = (struct r600_cs_track *)p->track;
  888. struct radeon_cs_reloc *reloc;
  889. u32 m, i, tmp, *ib;
  890. int r;
  891. i = (reg >> 7);
  892. if (i >= ARRAY_SIZE(r600_reg_safe_bm)) {
  893. dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
  894. return -EINVAL;
  895. }
  896. m = 1 << ((reg >> 2) & 31);
  897. if (!(r600_reg_safe_bm[i] & m))
  898. return 0;
  899. ib = p->ib.ptr;
  900. switch (reg) {
  901. /* force following reg to 0 in an attempt to disable out buffer
  902. * which will need us to better understand how it works to perform
  903. * security check on it (Jerome)
  904. */
  905. case R_0288A8_SQ_ESGS_RING_ITEMSIZE:
  906. case R_008C44_SQ_ESGS_RING_SIZE:
  907. case R_0288B0_SQ_ESTMP_RING_ITEMSIZE:
  908. case R_008C54_SQ_ESTMP_RING_SIZE:
  909. case R_0288C0_SQ_FBUF_RING_ITEMSIZE:
  910. case R_008C74_SQ_FBUF_RING_SIZE:
  911. case R_0288B4_SQ_GSTMP_RING_ITEMSIZE:
  912. case R_008C5C_SQ_GSTMP_RING_SIZE:
  913. case R_0288AC_SQ_GSVS_RING_ITEMSIZE:
  914. case R_008C4C_SQ_GSVS_RING_SIZE:
  915. case R_0288BC_SQ_PSTMP_RING_ITEMSIZE:
  916. case R_008C6C_SQ_PSTMP_RING_SIZE:
  917. case R_0288C4_SQ_REDUC_RING_ITEMSIZE:
  918. case R_008C7C_SQ_REDUC_RING_SIZE:
  919. case R_0288B8_SQ_VSTMP_RING_ITEMSIZE:
  920. case R_008C64_SQ_VSTMP_RING_SIZE:
  921. case R_0288C8_SQ_GS_VERT_ITEMSIZE:
  922. /* get value to populate the IB don't remove */
  923. /*tmp =radeon_get_ib_value(p, idx);
  924. ib[idx] = 0;*/
  925. break;
  926. case SQ_ESGS_RING_BASE:
  927. case SQ_GSVS_RING_BASE:
  928. case SQ_ESTMP_RING_BASE:
  929. case SQ_GSTMP_RING_BASE:
  930. case SQ_PSTMP_RING_BASE:
  931. case SQ_VSTMP_RING_BASE:
  932. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  933. if (r) {
  934. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  935. "0x%04X\n", reg);
  936. return -EINVAL;
  937. }
  938. ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  939. break;
  940. case SQ_CONFIG:
  941. track->sq_config = radeon_get_ib_value(p, idx);
  942. break;
  943. case R_028800_DB_DEPTH_CONTROL:
  944. track->db_depth_control = radeon_get_ib_value(p, idx);
  945. track->db_dirty = true;
  946. break;
  947. case R_028010_DB_DEPTH_INFO:
  948. if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS) &&
  949. radeon_cs_packet_next_is_pkt3_nop(p)) {
  950. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  951. if (r) {
  952. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  953. "0x%04X\n", reg);
  954. return -EINVAL;
  955. }
  956. track->db_depth_info = radeon_get_ib_value(p, idx);
  957. ib[idx] &= C_028010_ARRAY_MODE;
  958. track->db_depth_info &= C_028010_ARRAY_MODE;
  959. if (reloc->tiling_flags & RADEON_TILING_MACRO) {
  960. ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
  961. track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_2D_TILED_THIN1);
  962. } else {
  963. ib[idx] |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
  964. track->db_depth_info |= S_028010_ARRAY_MODE(V_028010_ARRAY_1D_TILED_THIN1);
  965. }
  966. } else {
  967. track->db_depth_info = radeon_get_ib_value(p, idx);
  968. }
  969. track->db_dirty = true;
  970. break;
  971. case R_028004_DB_DEPTH_VIEW:
  972. track->db_depth_view = radeon_get_ib_value(p, idx);
  973. track->db_dirty = true;
  974. break;
  975. case R_028000_DB_DEPTH_SIZE:
  976. track->db_depth_size = radeon_get_ib_value(p, idx);
  977. track->db_depth_size_idx = idx;
  978. track->db_dirty = true;
  979. break;
  980. case R_028AB0_VGT_STRMOUT_EN:
  981. track->vgt_strmout_en = radeon_get_ib_value(p, idx);
  982. track->streamout_dirty = true;
  983. break;
  984. case R_028B20_VGT_STRMOUT_BUFFER_EN:
  985. track->vgt_strmout_buffer_en = radeon_get_ib_value(p, idx);
  986. track->streamout_dirty = true;
  987. break;
  988. case VGT_STRMOUT_BUFFER_BASE_0:
  989. case VGT_STRMOUT_BUFFER_BASE_1:
  990. case VGT_STRMOUT_BUFFER_BASE_2:
  991. case VGT_STRMOUT_BUFFER_BASE_3:
  992. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  993. if (r) {
  994. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  995. "0x%04X\n", reg);
  996. return -EINVAL;
  997. }
  998. tmp = (reg - VGT_STRMOUT_BUFFER_BASE_0) / 16;
  999. track->vgt_strmout_bo_offset[tmp] = radeon_get_ib_value(p, idx) << 8;
  1000. ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1001. track->vgt_strmout_bo[tmp] = reloc->robj;
  1002. track->vgt_strmout_bo_mc[tmp] = reloc->gpu_offset;
  1003. track->streamout_dirty = true;
  1004. break;
  1005. case VGT_STRMOUT_BUFFER_SIZE_0:
  1006. case VGT_STRMOUT_BUFFER_SIZE_1:
  1007. case VGT_STRMOUT_BUFFER_SIZE_2:
  1008. case VGT_STRMOUT_BUFFER_SIZE_3:
  1009. tmp = (reg - VGT_STRMOUT_BUFFER_SIZE_0) / 16;
  1010. /* size in register is DWs, convert to bytes */
  1011. track->vgt_strmout_size[tmp] = radeon_get_ib_value(p, idx) * 4;
  1012. track->streamout_dirty = true;
  1013. break;
  1014. case CP_COHER_BASE:
  1015. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1016. if (r) {
  1017. dev_warn(p->dev, "missing reloc for CP_COHER_BASE "
  1018. "0x%04X\n", reg);
  1019. return -EINVAL;
  1020. }
  1021. ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1022. break;
  1023. case R_028238_CB_TARGET_MASK:
  1024. track->cb_target_mask = radeon_get_ib_value(p, idx);
  1025. track->cb_dirty = true;
  1026. break;
  1027. case R_02823C_CB_SHADER_MASK:
  1028. track->cb_shader_mask = radeon_get_ib_value(p, idx);
  1029. break;
  1030. case R_028C04_PA_SC_AA_CONFIG:
  1031. tmp = G_028C04_MSAA_NUM_SAMPLES(radeon_get_ib_value(p, idx));
  1032. track->log_nsamples = tmp;
  1033. track->nsamples = 1 << tmp;
  1034. track->cb_dirty = true;
  1035. break;
  1036. case R_028808_CB_COLOR_CONTROL:
  1037. tmp = G_028808_SPECIAL_OP(radeon_get_ib_value(p, idx));
  1038. track->is_resolve = tmp == V_028808_SPECIAL_RESOLVE_BOX;
  1039. track->cb_dirty = true;
  1040. break;
  1041. case R_0280A0_CB_COLOR0_INFO:
  1042. case R_0280A4_CB_COLOR1_INFO:
  1043. case R_0280A8_CB_COLOR2_INFO:
  1044. case R_0280AC_CB_COLOR3_INFO:
  1045. case R_0280B0_CB_COLOR4_INFO:
  1046. case R_0280B4_CB_COLOR5_INFO:
  1047. case R_0280B8_CB_COLOR6_INFO:
  1048. case R_0280BC_CB_COLOR7_INFO:
  1049. if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS) &&
  1050. radeon_cs_packet_next_is_pkt3_nop(p)) {
  1051. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1052. if (r) {
  1053. dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
  1054. return -EINVAL;
  1055. }
  1056. tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
  1057. track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
  1058. if (reloc->tiling_flags & RADEON_TILING_MACRO) {
  1059. ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
  1060. track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_2D_TILED_THIN1);
  1061. } else if (reloc->tiling_flags & RADEON_TILING_MICRO) {
  1062. ib[idx] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
  1063. track->cb_color_info[tmp] |= S_0280A0_ARRAY_MODE(V_0280A0_ARRAY_1D_TILED_THIN1);
  1064. }
  1065. } else {
  1066. tmp = (reg - R_0280A0_CB_COLOR0_INFO) / 4;
  1067. track->cb_color_info[tmp] = radeon_get_ib_value(p, idx);
  1068. }
  1069. track->cb_dirty = true;
  1070. break;
  1071. case R_028080_CB_COLOR0_VIEW:
  1072. case R_028084_CB_COLOR1_VIEW:
  1073. case R_028088_CB_COLOR2_VIEW:
  1074. case R_02808C_CB_COLOR3_VIEW:
  1075. case R_028090_CB_COLOR4_VIEW:
  1076. case R_028094_CB_COLOR5_VIEW:
  1077. case R_028098_CB_COLOR6_VIEW:
  1078. case R_02809C_CB_COLOR7_VIEW:
  1079. tmp = (reg - R_028080_CB_COLOR0_VIEW) / 4;
  1080. track->cb_color_view[tmp] = radeon_get_ib_value(p, idx);
  1081. track->cb_dirty = true;
  1082. break;
  1083. case R_028060_CB_COLOR0_SIZE:
  1084. case R_028064_CB_COLOR1_SIZE:
  1085. case R_028068_CB_COLOR2_SIZE:
  1086. case R_02806C_CB_COLOR3_SIZE:
  1087. case R_028070_CB_COLOR4_SIZE:
  1088. case R_028074_CB_COLOR5_SIZE:
  1089. case R_028078_CB_COLOR6_SIZE:
  1090. case R_02807C_CB_COLOR7_SIZE:
  1091. tmp = (reg - R_028060_CB_COLOR0_SIZE) / 4;
  1092. track->cb_color_size[tmp] = radeon_get_ib_value(p, idx);
  1093. track->cb_color_size_idx[tmp] = idx;
  1094. track->cb_dirty = true;
  1095. break;
  1096. /* This register were added late, there is userspace
  1097. * which does provide relocation for those but set
  1098. * 0 offset. In order to avoid breaking old userspace
  1099. * we detect this and set address to point to last
  1100. * CB_COLOR0_BASE, note that if userspace doesn't set
  1101. * CB_COLOR0_BASE before this register we will report
  1102. * error. Old userspace always set CB_COLOR0_BASE
  1103. * before any of this.
  1104. */
  1105. case R_0280E0_CB_COLOR0_FRAG:
  1106. case R_0280E4_CB_COLOR1_FRAG:
  1107. case R_0280E8_CB_COLOR2_FRAG:
  1108. case R_0280EC_CB_COLOR3_FRAG:
  1109. case R_0280F0_CB_COLOR4_FRAG:
  1110. case R_0280F4_CB_COLOR5_FRAG:
  1111. case R_0280F8_CB_COLOR6_FRAG:
  1112. case R_0280FC_CB_COLOR7_FRAG:
  1113. tmp = (reg - R_0280E0_CB_COLOR0_FRAG) / 4;
  1114. if (!radeon_cs_packet_next_is_pkt3_nop(p)) {
  1115. if (!track->cb_color_base_last[tmp]) {
  1116. dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
  1117. return -EINVAL;
  1118. }
  1119. track->cb_color_frag_bo[tmp] = track->cb_color_bo[tmp];
  1120. track->cb_color_frag_offset[tmp] = track->cb_color_bo_offset[tmp];
  1121. ib[idx] = track->cb_color_base_last[tmp];
  1122. } else {
  1123. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1124. if (r) {
  1125. dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
  1126. return -EINVAL;
  1127. }
  1128. track->cb_color_frag_bo[tmp] = reloc->robj;
  1129. track->cb_color_frag_offset[tmp] = (u64)ib[idx] << 8;
  1130. ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1131. }
  1132. if (G_0280A0_TILE_MODE(track->cb_color_info[tmp])) {
  1133. track->cb_dirty = true;
  1134. }
  1135. break;
  1136. case R_0280C0_CB_COLOR0_TILE:
  1137. case R_0280C4_CB_COLOR1_TILE:
  1138. case R_0280C8_CB_COLOR2_TILE:
  1139. case R_0280CC_CB_COLOR3_TILE:
  1140. case R_0280D0_CB_COLOR4_TILE:
  1141. case R_0280D4_CB_COLOR5_TILE:
  1142. case R_0280D8_CB_COLOR6_TILE:
  1143. case R_0280DC_CB_COLOR7_TILE:
  1144. tmp = (reg - R_0280C0_CB_COLOR0_TILE) / 4;
  1145. if (!radeon_cs_packet_next_is_pkt3_nop(p)) {
  1146. if (!track->cb_color_base_last[tmp]) {
  1147. dev_err(p->dev, "Broken old userspace ? no cb_color0_base supplied before trying to write 0x%08X\n", reg);
  1148. return -EINVAL;
  1149. }
  1150. track->cb_color_tile_bo[tmp] = track->cb_color_bo[tmp];
  1151. track->cb_color_tile_offset[tmp] = track->cb_color_bo_offset[tmp];
  1152. ib[idx] = track->cb_color_base_last[tmp];
  1153. } else {
  1154. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1155. if (r) {
  1156. dev_err(p->dev, "bad SET_CONTEXT_REG 0x%04X\n", reg);
  1157. return -EINVAL;
  1158. }
  1159. track->cb_color_tile_bo[tmp] = reloc->robj;
  1160. track->cb_color_tile_offset[tmp] = (u64)ib[idx] << 8;
  1161. ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1162. }
  1163. if (G_0280A0_TILE_MODE(track->cb_color_info[tmp])) {
  1164. track->cb_dirty = true;
  1165. }
  1166. break;
  1167. case R_028100_CB_COLOR0_MASK:
  1168. case R_028104_CB_COLOR1_MASK:
  1169. case R_028108_CB_COLOR2_MASK:
  1170. case R_02810C_CB_COLOR3_MASK:
  1171. case R_028110_CB_COLOR4_MASK:
  1172. case R_028114_CB_COLOR5_MASK:
  1173. case R_028118_CB_COLOR6_MASK:
  1174. case R_02811C_CB_COLOR7_MASK:
  1175. tmp = (reg - R_028100_CB_COLOR0_MASK) / 4;
  1176. track->cb_color_mask[tmp] = radeon_get_ib_value(p, idx);
  1177. if (G_0280A0_TILE_MODE(track->cb_color_info[tmp])) {
  1178. track->cb_dirty = true;
  1179. }
  1180. break;
  1181. case CB_COLOR0_BASE:
  1182. case CB_COLOR1_BASE:
  1183. case CB_COLOR2_BASE:
  1184. case CB_COLOR3_BASE:
  1185. case CB_COLOR4_BASE:
  1186. case CB_COLOR5_BASE:
  1187. case CB_COLOR6_BASE:
  1188. case CB_COLOR7_BASE:
  1189. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1190. if (r) {
  1191. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  1192. "0x%04X\n", reg);
  1193. return -EINVAL;
  1194. }
  1195. tmp = (reg - CB_COLOR0_BASE) / 4;
  1196. track->cb_color_bo_offset[tmp] = radeon_get_ib_value(p, idx) << 8;
  1197. ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1198. track->cb_color_base_last[tmp] = ib[idx];
  1199. track->cb_color_bo[tmp] = reloc->robj;
  1200. track->cb_color_bo_mc[tmp] = reloc->gpu_offset;
  1201. track->cb_dirty = true;
  1202. break;
  1203. case DB_DEPTH_BASE:
  1204. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1205. if (r) {
  1206. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  1207. "0x%04X\n", reg);
  1208. return -EINVAL;
  1209. }
  1210. track->db_offset = radeon_get_ib_value(p, idx) << 8;
  1211. ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1212. track->db_bo = reloc->robj;
  1213. track->db_bo_mc = reloc->gpu_offset;
  1214. track->db_dirty = true;
  1215. break;
  1216. case DB_HTILE_DATA_BASE:
  1217. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1218. if (r) {
  1219. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  1220. "0x%04X\n", reg);
  1221. return -EINVAL;
  1222. }
  1223. track->htile_offset = radeon_get_ib_value(p, idx) << 8;
  1224. ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1225. track->htile_bo = reloc->robj;
  1226. track->db_dirty = true;
  1227. break;
  1228. case DB_HTILE_SURFACE:
  1229. track->htile_surface = radeon_get_ib_value(p, idx);
  1230. /* force 8x8 htile width and height */
  1231. ib[idx] |= 3;
  1232. track->db_dirty = true;
  1233. break;
  1234. case SQ_PGM_START_FS:
  1235. case SQ_PGM_START_ES:
  1236. case SQ_PGM_START_VS:
  1237. case SQ_PGM_START_GS:
  1238. case SQ_PGM_START_PS:
  1239. case SQ_ALU_CONST_CACHE_GS_0:
  1240. case SQ_ALU_CONST_CACHE_GS_1:
  1241. case SQ_ALU_CONST_CACHE_GS_2:
  1242. case SQ_ALU_CONST_CACHE_GS_3:
  1243. case SQ_ALU_CONST_CACHE_GS_4:
  1244. case SQ_ALU_CONST_CACHE_GS_5:
  1245. case SQ_ALU_CONST_CACHE_GS_6:
  1246. case SQ_ALU_CONST_CACHE_GS_7:
  1247. case SQ_ALU_CONST_CACHE_GS_8:
  1248. case SQ_ALU_CONST_CACHE_GS_9:
  1249. case SQ_ALU_CONST_CACHE_GS_10:
  1250. case SQ_ALU_CONST_CACHE_GS_11:
  1251. case SQ_ALU_CONST_CACHE_GS_12:
  1252. case SQ_ALU_CONST_CACHE_GS_13:
  1253. case SQ_ALU_CONST_CACHE_GS_14:
  1254. case SQ_ALU_CONST_CACHE_GS_15:
  1255. case SQ_ALU_CONST_CACHE_PS_0:
  1256. case SQ_ALU_CONST_CACHE_PS_1:
  1257. case SQ_ALU_CONST_CACHE_PS_2:
  1258. case SQ_ALU_CONST_CACHE_PS_3:
  1259. case SQ_ALU_CONST_CACHE_PS_4:
  1260. case SQ_ALU_CONST_CACHE_PS_5:
  1261. case SQ_ALU_CONST_CACHE_PS_6:
  1262. case SQ_ALU_CONST_CACHE_PS_7:
  1263. case SQ_ALU_CONST_CACHE_PS_8:
  1264. case SQ_ALU_CONST_CACHE_PS_9:
  1265. case SQ_ALU_CONST_CACHE_PS_10:
  1266. case SQ_ALU_CONST_CACHE_PS_11:
  1267. case SQ_ALU_CONST_CACHE_PS_12:
  1268. case SQ_ALU_CONST_CACHE_PS_13:
  1269. case SQ_ALU_CONST_CACHE_PS_14:
  1270. case SQ_ALU_CONST_CACHE_PS_15:
  1271. case SQ_ALU_CONST_CACHE_VS_0:
  1272. case SQ_ALU_CONST_CACHE_VS_1:
  1273. case SQ_ALU_CONST_CACHE_VS_2:
  1274. case SQ_ALU_CONST_CACHE_VS_3:
  1275. case SQ_ALU_CONST_CACHE_VS_4:
  1276. case SQ_ALU_CONST_CACHE_VS_5:
  1277. case SQ_ALU_CONST_CACHE_VS_6:
  1278. case SQ_ALU_CONST_CACHE_VS_7:
  1279. case SQ_ALU_CONST_CACHE_VS_8:
  1280. case SQ_ALU_CONST_CACHE_VS_9:
  1281. case SQ_ALU_CONST_CACHE_VS_10:
  1282. case SQ_ALU_CONST_CACHE_VS_11:
  1283. case SQ_ALU_CONST_CACHE_VS_12:
  1284. case SQ_ALU_CONST_CACHE_VS_13:
  1285. case SQ_ALU_CONST_CACHE_VS_14:
  1286. case SQ_ALU_CONST_CACHE_VS_15:
  1287. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1288. if (r) {
  1289. dev_warn(p->dev, "bad SET_CONTEXT_REG "
  1290. "0x%04X\n", reg);
  1291. return -EINVAL;
  1292. }
  1293. ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1294. break;
  1295. case SX_MEMORY_EXPORT_BASE:
  1296. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1297. if (r) {
  1298. dev_warn(p->dev, "bad SET_CONFIG_REG "
  1299. "0x%04X\n", reg);
  1300. return -EINVAL;
  1301. }
  1302. ib[idx] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1303. break;
  1304. case SX_MISC:
  1305. track->sx_misc_kill_all_prims = (radeon_get_ib_value(p, idx) & 0x1) != 0;
  1306. break;
  1307. default:
  1308. dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
  1309. return -EINVAL;
  1310. }
  1311. return 0;
  1312. }
  1313. unsigned r600_mip_minify(unsigned size, unsigned level)
  1314. {
  1315. unsigned val;
  1316. val = max(1U, size >> level);
  1317. if (level > 0)
  1318. val = roundup_pow_of_two(val);
  1319. return val;
  1320. }
  1321. static void r600_texture_size(unsigned nfaces, unsigned blevel, unsigned llevel,
  1322. unsigned w0, unsigned h0, unsigned d0, unsigned nsamples, unsigned format,
  1323. unsigned block_align, unsigned height_align, unsigned base_align,
  1324. unsigned *l0_size, unsigned *mipmap_size)
  1325. {
  1326. unsigned offset, i, level;
  1327. unsigned width, height, depth, size;
  1328. unsigned blocksize;
  1329. unsigned nbx, nby;
  1330. unsigned nlevels = llevel - blevel + 1;
  1331. *l0_size = -1;
  1332. blocksize = r600_fmt_get_blocksize(format);
  1333. w0 = r600_mip_minify(w0, 0);
  1334. h0 = r600_mip_minify(h0, 0);
  1335. d0 = r600_mip_minify(d0, 0);
  1336. for(i = 0, offset = 0, level = blevel; i < nlevels; i++, level++) {
  1337. width = r600_mip_minify(w0, i);
  1338. nbx = r600_fmt_get_nblocksx(format, width);
  1339. nbx = round_up(nbx, block_align);
  1340. height = r600_mip_minify(h0, i);
  1341. nby = r600_fmt_get_nblocksy(format, height);
  1342. nby = round_up(nby, height_align);
  1343. depth = r600_mip_minify(d0, i);
  1344. size = nbx * nby * blocksize * nsamples;
  1345. if (nfaces)
  1346. size *= nfaces;
  1347. else
  1348. size *= depth;
  1349. if (i == 0)
  1350. *l0_size = size;
  1351. if (i == 0 || i == 1)
  1352. offset = round_up(offset, base_align);
  1353. offset += size;
  1354. }
  1355. *mipmap_size = offset;
  1356. if (llevel == 0)
  1357. *mipmap_size = *l0_size;
  1358. if (!blevel)
  1359. *mipmap_size -= *l0_size;
  1360. }
  1361. /**
  1362. * r600_check_texture_resource() - check if register is authorized or not
  1363. * @p: parser structure holding parsing context
  1364. * @idx: index into the cs buffer
  1365. * @texture: texture's bo structure
  1366. * @mipmap: mipmap's bo structure
  1367. *
  1368. * This function will check that the resource has valid field and that
  1369. * the texture and mipmap bo object are big enough to cover this resource.
  1370. */
  1371. static int r600_check_texture_resource(struct radeon_cs_parser *p, u32 idx,
  1372. struct radeon_bo *texture,
  1373. struct radeon_bo *mipmap,
  1374. u64 base_offset,
  1375. u64 mip_offset,
  1376. u32 tiling_flags)
  1377. {
  1378. struct r600_cs_track *track = p->track;
  1379. u32 dim, nfaces, llevel, blevel, w0, h0, d0;
  1380. u32 word0, word1, l0_size, mipmap_size, word2, word3, word4, word5;
  1381. u32 height_align, pitch, pitch_align, depth_align;
  1382. u32 barray, larray;
  1383. u64 base_align;
  1384. struct array_mode_checker array_check;
  1385. u32 format;
  1386. bool is_array;
  1387. /* on legacy kernel we don't perform advanced check */
  1388. if (p->rdev == NULL)
  1389. return 0;
  1390. /* convert to bytes */
  1391. base_offset <<= 8;
  1392. mip_offset <<= 8;
  1393. word0 = radeon_get_ib_value(p, idx + 0);
  1394. if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
  1395. if (tiling_flags & RADEON_TILING_MACRO)
  1396. word0 |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
  1397. else if (tiling_flags & RADEON_TILING_MICRO)
  1398. word0 |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
  1399. }
  1400. word1 = radeon_get_ib_value(p, idx + 1);
  1401. word2 = radeon_get_ib_value(p, idx + 2) << 8;
  1402. word3 = radeon_get_ib_value(p, idx + 3) << 8;
  1403. word4 = radeon_get_ib_value(p, idx + 4);
  1404. word5 = radeon_get_ib_value(p, idx + 5);
  1405. dim = G_038000_DIM(word0);
  1406. w0 = G_038000_TEX_WIDTH(word0) + 1;
  1407. pitch = (G_038000_PITCH(word0) + 1) * 8;
  1408. h0 = G_038004_TEX_HEIGHT(word1) + 1;
  1409. d0 = G_038004_TEX_DEPTH(word1);
  1410. format = G_038004_DATA_FORMAT(word1);
  1411. blevel = G_038010_BASE_LEVEL(word4);
  1412. llevel = G_038014_LAST_LEVEL(word5);
  1413. /* pitch in texels */
  1414. array_check.array_mode = G_038000_TILE_MODE(word0);
  1415. array_check.group_size = track->group_size;
  1416. array_check.nbanks = track->nbanks;
  1417. array_check.npipes = track->npipes;
  1418. array_check.nsamples = 1;
  1419. array_check.blocksize = r600_fmt_get_blocksize(format);
  1420. nfaces = 1;
  1421. is_array = false;
  1422. switch (dim) {
  1423. case V_038000_SQ_TEX_DIM_1D:
  1424. case V_038000_SQ_TEX_DIM_2D:
  1425. case V_038000_SQ_TEX_DIM_3D:
  1426. break;
  1427. case V_038000_SQ_TEX_DIM_CUBEMAP:
  1428. if (p->family >= CHIP_RV770)
  1429. nfaces = 8;
  1430. else
  1431. nfaces = 6;
  1432. break;
  1433. case V_038000_SQ_TEX_DIM_1D_ARRAY:
  1434. case V_038000_SQ_TEX_DIM_2D_ARRAY:
  1435. is_array = true;
  1436. break;
  1437. case V_038000_SQ_TEX_DIM_2D_ARRAY_MSAA:
  1438. is_array = true;
  1439. /* fall through */
  1440. case V_038000_SQ_TEX_DIM_2D_MSAA:
  1441. array_check.nsamples = 1 << llevel;
  1442. llevel = 0;
  1443. break;
  1444. default:
  1445. dev_warn(p->dev, "this kernel doesn't support %d texture dim\n", G_038000_DIM(word0));
  1446. return -EINVAL;
  1447. }
  1448. if (!r600_fmt_is_valid_texture(format, p->family)) {
  1449. dev_warn(p->dev, "%s:%d texture invalid format %d\n",
  1450. __func__, __LINE__, format);
  1451. return -EINVAL;
  1452. }
  1453. if (r600_get_array_mode_alignment(&array_check,
  1454. &pitch_align, &height_align, &depth_align, &base_align)) {
  1455. dev_warn(p->dev, "%s:%d tex array mode (%d) invalid\n",
  1456. __func__, __LINE__, G_038000_TILE_MODE(word0));
  1457. return -EINVAL;
  1458. }
  1459. /* XXX check height as well... */
  1460. if (!IS_ALIGNED(pitch, pitch_align)) {
  1461. dev_warn(p->dev, "%s:%d tex pitch (%d, 0x%x, %d) invalid\n",
  1462. __func__, __LINE__, pitch, pitch_align, G_038000_TILE_MODE(word0));
  1463. return -EINVAL;
  1464. }
  1465. if (!IS_ALIGNED(base_offset, base_align)) {
  1466. dev_warn(p->dev, "%s:%d tex base offset (0x%llx, 0x%llx, %d) invalid\n",
  1467. __func__, __LINE__, base_offset, base_align, G_038000_TILE_MODE(word0));
  1468. return -EINVAL;
  1469. }
  1470. if (!IS_ALIGNED(mip_offset, base_align)) {
  1471. dev_warn(p->dev, "%s:%d tex mip offset (0x%llx, 0x%llx, %d) invalid\n",
  1472. __func__, __LINE__, mip_offset, base_align, G_038000_TILE_MODE(word0));
  1473. return -EINVAL;
  1474. }
  1475. if (blevel > llevel) {
  1476. dev_warn(p->dev, "texture blevel %d > llevel %d\n",
  1477. blevel, llevel);
  1478. }
  1479. if (is_array) {
  1480. barray = G_038014_BASE_ARRAY(word5);
  1481. larray = G_038014_LAST_ARRAY(word5);
  1482. nfaces = larray - barray + 1;
  1483. }
  1484. r600_texture_size(nfaces, blevel, llevel, w0, h0, d0, array_check.nsamples, format,
  1485. pitch_align, height_align, base_align,
  1486. &l0_size, &mipmap_size);
  1487. /* using get ib will give us the offset into the texture bo */
  1488. if ((l0_size + word2) > radeon_bo_size(texture)) {
  1489. dev_warn(p->dev, "texture bo too small ((%d %d) (%d %d) %d %d %d -> %d have %ld)\n",
  1490. w0, h0, pitch_align, height_align,
  1491. array_check.array_mode, format, word2,
  1492. l0_size, radeon_bo_size(texture));
  1493. dev_warn(p->dev, "alignments %d %d %d %lld\n", pitch, pitch_align, height_align, base_align);
  1494. return -EINVAL;
  1495. }
  1496. /* using get ib will give us the offset into the mipmap bo */
  1497. if ((mipmap_size + word3) > radeon_bo_size(mipmap)) {
  1498. /*dev_warn(p->dev, "mipmap bo too small (%d %d %d %d %d %d -> %d have %ld)\n",
  1499. w0, h0, format, blevel, nlevels, word3, mipmap_size, radeon_bo_size(texture));*/
  1500. }
  1501. return 0;
  1502. }
  1503. static bool r600_is_safe_reg(struct radeon_cs_parser *p, u32 reg, u32 idx)
  1504. {
  1505. u32 m, i;
  1506. i = (reg >> 7);
  1507. if (i >= ARRAY_SIZE(r600_reg_safe_bm)) {
  1508. dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
  1509. return false;
  1510. }
  1511. m = 1 << ((reg >> 2) & 31);
  1512. if (!(r600_reg_safe_bm[i] & m))
  1513. return true;
  1514. dev_warn(p->dev, "forbidden register 0x%08x at %d\n", reg, idx);
  1515. return false;
  1516. }
  1517. static int r600_packet3_check(struct radeon_cs_parser *p,
  1518. struct radeon_cs_packet *pkt)
  1519. {
  1520. struct radeon_cs_reloc *reloc;
  1521. struct r600_cs_track *track;
  1522. volatile u32 *ib;
  1523. unsigned idx;
  1524. unsigned i;
  1525. unsigned start_reg, end_reg, reg;
  1526. int r;
  1527. u32 idx_value;
  1528. track = (struct r600_cs_track *)p->track;
  1529. ib = p->ib.ptr;
  1530. idx = pkt->idx + 1;
  1531. idx_value = radeon_get_ib_value(p, idx);
  1532. switch (pkt->opcode) {
  1533. case PACKET3_SET_PREDICATION:
  1534. {
  1535. int pred_op;
  1536. int tmp;
  1537. uint64_t offset;
  1538. if (pkt->count != 1) {
  1539. DRM_ERROR("bad SET PREDICATION\n");
  1540. return -EINVAL;
  1541. }
  1542. tmp = radeon_get_ib_value(p, idx + 1);
  1543. pred_op = (tmp >> 16) & 0x7;
  1544. /* for the clear predicate operation */
  1545. if (pred_op == 0)
  1546. return 0;
  1547. if (pred_op > 2) {
  1548. DRM_ERROR("bad SET PREDICATION operation %d\n", pred_op);
  1549. return -EINVAL;
  1550. }
  1551. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1552. if (r) {
  1553. DRM_ERROR("bad SET PREDICATION\n");
  1554. return -EINVAL;
  1555. }
  1556. offset = reloc->gpu_offset +
  1557. (idx_value & 0xfffffff0) +
  1558. ((u64)(tmp & 0xff) << 32);
  1559. ib[idx + 0] = offset;
  1560. ib[idx + 1] = (tmp & 0xffffff00) | (upper_32_bits(offset) & 0xff);
  1561. }
  1562. break;
  1563. case PACKET3_START_3D_CMDBUF:
  1564. if (p->family >= CHIP_RV770 || pkt->count) {
  1565. DRM_ERROR("bad START_3D\n");
  1566. return -EINVAL;
  1567. }
  1568. break;
  1569. case PACKET3_CONTEXT_CONTROL:
  1570. if (pkt->count != 1) {
  1571. DRM_ERROR("bad CONTEXT_CONTROL\n");
  1572. return -EINVAL;
  1573. }
  1574. break;
  1575. case PACKET3_INDEX_TYPE:
  1576. case PACKET3_NUM_INSTANCES:
  1577. if (pkt->count) {
  1578. DRM_ERROR("bad INDEX_TYPE/NUM_INSTANCES\n");
  1579. return -EINVAL;
  1580. }
  1581. break;
  1582. case PACKET3_DRAW_INDEX:
  1583. {
  1584. uint64_t offset;
  1585. if (pkt->count != 3) {
  1586. DRM_ERROR("bad DRAW_INDEX\n");
  1587. return -EINVAL;
  1588. }
  1589. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1590. if (r) {
  1591. DRM_ERROR("bad DRAW_INDEX\n");
  1592. return -EINVAL;
  1593. }
  1594. offset = reloc->gpu_offset +
  1595. idx_value +
  1596. ((u64)(radeon_get_ib_value(p, idx+1) & 0xff) << 32);
  1597. ib[idx+0] = offset;
  1598. ib[idx+1] = upper_32_bits(offset) & 0xff;
  1599. r = r600_cs_track_check(p);
  1600. if (r) {
  1601. dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
  1602. return r;
  1603. }
  1604. break;
  1605. }
  1606. case PACKET3_DRAW_INDEX_AUTO:
  1607. if (pkt->count != 1) {
  1608. DRM_ERROR("bad DRAW_INDEX_AUTO\n");
  1609. return -EINVAL;
  1610. }
  1611. r = r600_cs_track_check(p);
  1612. if (r) {
  1613. dev_warn(p->dev, "%s:%d invalid cmd stream %d\n", __func__, __LINE__, idx);
  1614. return r;
  1615. }
  1616. break;
  1617. case PACKET3_DRAW_INDEX_IMMD_BE:
  1618. case PACKET3_DRAW_INDEX_IMMD:
  1619. if (pkt->count < 2) {
  1620. DRM_ERROR("bad DRAW_INDEX_IMMD\n");
  1621. return -EINVAL;
  1622. }
  1623. r = r600_cs_track_check(p);
  1624. if (r) {
  1625. dev_warn(p->dev, "%s:%d invalid cmd stream\n", __func__, __LINE__);
  1626. return r;
  1627. }
  1628. break;
  1629. case PACKET3_WAIT_REG_MEM:
  1630. if (pkt->count != 5) {
  1631. DRM_ERROR("bad WAIT_REG_MEM\n");
  1632. return -EINVAL;
  1633. }
  1634. /* bit 4 is reg (0) or mem (1) */
  1635. if (idx_value & 0x10) {
  1636. uint64_t offset;
  1637. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1638. if (r) {
  1639. DRM_ERROR("bad WAIT_REG_MEM\n");
  1640. return -EINVAL;
  1641. }
  1642. offset = reloc->gpu_offset +
  1643. (radeon_get_ib_value(p, idx+1) & 0xfffffff0) +
  1644. ((u64)(radeon_get_ib_value(p, idx+2) & 0xff) << 32);
  1645. ib[idx+1] = (ib[idx+1] & 0x3) | (offset & 0xfffffff0);
  1646. ib[idx+2] = upper_32_bits(offset) & 0xff;
  1647. } else if (idx_value & 0x100) {
  1648. DRM_ERROR("cannot use PFP on REG wait\n");
  1649. return -EINVAL;
  1650. }
  1651. break;
  1652. case PACKET3_CP_DMA:
  1653. {
  1654. u32 command, size;
  1655. u64 offset, tmp;
  1656. if (pkt->count != 4) {
  1657. DRM_ERROR("bad CP DMA\n");
  1658. return -EINVAL;
  1659. }
  1660. command = radeon_get_ib_value(p, idx+4);
  1661. size = command & 0x1fffff;
  1662. if (command & PACKET3_CP_DMA_CMD_SAS) {
  1663. /* src address space is register */
  1664. DRM_ERROR("CP DMA SAS not supported\n");
  1665. return -EINVAL;
  1666. } else {
  1667. if (command & PACKET3_CP_DMA_CMD_SAIC) {
  1668. DRM_ERROR("CP DMA SAIC only supported for registers\n");
  1669. return -EINVAL;
  1670. }
  1671. /* src address space is memory */
  1672. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1673. if (r) {
  1674. DRM_ERROR("bad CP DMA SRC\n");
  1675. return -EINVAL;
  1676. }
  1677. tmp = radeon_get_ib_value(p, idx) +
  1678. ((u64)(radeon_get_ib_value(p, idx+1) & 0xff) << 32);
  1679. offset = reloc->gpu_offset + tmp;
  1680. if ((tmp + size) > radeon_bo_size(reloc->robj)) {
  1681. dev_warn(p->dev, "CP DMA src buffer too small (%llu %lu)\n",
  1682. tmp + size, radeon_bo_size(reloc->robj));
  1683. return -EINVAL;
  1684. }
  1685. ib[idx] = offset;
  1686. ib[idx+1] = (ib[idx+1] & 0xffffff00) | (upper_32_bits(offset) & 0xff);
  1687. }
  1688. if (command & PACKET3_CP_DMA_CMD_DAS) {
  1689. /* dst address space is register */
  1690. DRM_ERROR("CP DMA DAS not supported\n");
  1691. return -EINVAL;
  1692. } else {
  1693. /* dst address space is memory */
  1694. if (command & PACKET3_CP_DMA_CMD_DAIC) {
  1695. DRM_ERROR("CP DMA DAIC only supported for registers\n");
  1696. return -EINVAL;
  1697. }
  1698. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1699. if (r) {
  1700. DRM_ERROR("bad CP DMA DST\n");
  1701. return -EINVAL;
  1702. }
  1703. tmp = radeon_get_ib_value(p, idx+2) +
  1704. ((u64)(radeon_get_ib_value(p, idx+3) & 0xff) << 32);
  1705. offset = reloc->gpu_offset + tmp;
  1706. if ((tmp + size) > radeon_bo_size(reloc->robj)) {
  1707. dev_warn(p->dev, "CP DMA dst buffer too small (%llu %lu)\n",
  1708. tmp + size, radeon_bo_size(reloc->robj));
  1709. return -EINVAL;
  1710. }
  1711. ib[idx+2] = offset;
  1712. ib[idx+3] = upper_32_bits(offset) & 0xff;
  1713. }
  1714. break;
  1715. }
  1716. case PACKET3_SURFACE_SYNC:
  1717. if (pkt->count != 3) {
  1718. DRM_ERROR("bad SURFACE_SYNC\n");
  1719. return -EINVAL;
  1720. }
  1721. /* 0xffffffff/0x0 is flush all cache flag */
  1722. if (radeon_get_ib_value(p, idx + 1) != 0xffffffff ||
  1723. radeon_get_ib_value(p, idx + 2) != 0) {
  1724. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1725. if (r) {
  1726. DRM_ERROR("bad SURFACE_SYNC\n");
  1727. return -EINVAL;
  1728. }
  1729. ib[idx+2] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1730. }
  1731. break;
  1732. case PACKET3_EVENT_WRITE:
  1733. if (pkt->count != 2 && pkt->count != 0) {
  1734. DRM_ERROR("bad EVENT_WRITE\n");
  1735. return -EINVAL;
  1736. }
  1737. if (pkt->count) {
  1738. uint64_t offset;
  1739. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1740. if (r) {
  1741. DRM_ERROR("bad EVENT_WRITE\n");
  1742. return -EINVAL;
  1743. }
  1744. offset = reloc->gpu_offset +
  1745. (radeon_get_ib_value(p, idx+1) & 0xfffffff8) +
  1746. ((u64)(radeon_get_ib_value(p, idx+2) & 0xff) << 32);
  1747. ib[idx+1] = offset & 0xfffffff8;
  1748. ib[idx+2] = upper_32_bits(offset) & 0xff;
  1749. }
  1750. break;
  1751. case PACKET3_EVENT_WRITE_EOP:
  1752. {
  1753. uint64_t offset;
  1754. if (pkt->count != 4) {
  1755. DRM_ERROR("bad EVENT_WRITE_EOP\n");
  1756. return -EINVAL;
  1757. }
  1758. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1759. if (r) {
  1760. DRM_ERROR("bad EVENT_WRITE\n");
  1761. return -EINVAL;
  1762. }
  1763. offset = reloc->gpu_offset +
  1764. (radeon_get_ib_value(p, idx+1) & 0xfffffffc) +
  1765. ((u64)(radeon_get_ib_value(p, idx+2) & 0xff) << 32);
  1766. ib[idx+1] = offset & 0xfffffffc;
  1767. ib[idx+2] = (ib[idx+2] & 0xffffff00) | (upper_32_bits(offset) & 0xff);
  1768. break;
  1769. }
  1770. case PACKET3_SET_CONFIG_REG:
  1771. start_reg = (idx_value << 2) + PACKET3_SET_CONFIG_REG_OFFSET;
  1772. end_reg = 4 * pkt->count + start_reg - 4;
  1773. if ((start_reg < PACKET3_SET_CONFIG_REG_OFFSET) ||
  1774. (start_reg >= PACKET3_SET_CONFIG_REG_END) ||
  1775. (end_reg >= PACKET3_SET_CONFIG_REG_END)) {
  1776. DRM_ERROR("bad PACKET3_SET_CONFIG_REG\n");
  1777. return -EINVAL;
  1778. }
  1779. for (i = 0; i < pkt->count; i++) {
  1780. reg = start_reg + (4 * i);
  1781. r = r600_cs_check_reg(p, reg, idx+1+i);
  1782. if (r)
  1783. return r;
  1784. }
  1785. break;
  1786. case PACKET3_SET_CONTEXT_REG:
  1787. start_reg = (idx_value << 2) + PACKET3_SET_CONTEXT_REG_OFFSET;
  1788. end_reg = 4 * pkt->count + start_reg - 4;
  1789. if ((start_reg < PACKET3_SET_CONTEXT_REG_OFFSET) ||
  1790. (start_reg >= PACKET3_SET_CONTEXT_REG_END) ||
  1791. (end_reg >= PACKET3_SET_CONTEXT_REG_END)) {
  1792. DRM_ERROR("bad PACKET3_SET_CONTEXT_REG\n");
  1793. return -EINVAL;
  1794. }
  1795. for (i = 0; i < pkt->count; i++) {
  1796. reg = start_reg + (4 * i);
  1797. r = r600_cs_check_reg(p, reg, idx+1+i);
  1798. if (r)
  1799. return r;
  1800. }
  1801. break;
  1802. case PACKET3_SET_RESOURCE:
  1803. if (pkt->count % 7) {
  1804. DRM_ERROR("bad SET_RESOURCE\n");
  1805. return -EINVAL;
  1806. }
  1807. start_reg = (idx_value << 2) + PACKET3_SET_RESOURCE_OFFSET;
  1808. end_reg = 4 * pkt->count + start_reg - 4;
  1809. if ((start_reg < PACKET3_SET_RESOURCE_OFFSET) ||
  1810. (start_reg >= PACKET3_SET_RESOURCE_END) ||
  1811. (end_reg >= PACKET3_SET_RESOURCE_END)) {
  1812. DRM_ERROR("bad SET_RESOURCE\n");
  1813. return -EINVAL;
  1814. }
  1815. for (i = 0; i < (pkt->count / 7); i++) {
  1816. struct radeon_bo *texture, *mipmap;
  1817. u32 size, offset, base_offset, mip_offset;
  1818. switch (G__SQ_VTX_CONSTANT_TYPE(radeon_get_ib_value(p, idx+(i*7)+6+1))) {
  1819. case SQ_TEX_VTX_VALID_TEXTURE:
  1820. /* tex base */
  1821. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1822. if (r) {
  1823. DRM_ERROR("bad SET_RESOURCE\n");
  1824. return -EINVAL;
  1825. }
  1826. base_offset = (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1827. if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
  1828. if (reloc->tiling_flags & RADEON_TILING_MACRO)
  1829. ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_2D_TILED_THIN1);
  1830. else if (reloc->tiling_flags & RADEON_TILING_MICRO)
  1831. ib[idx+1+(i*7)+0] |= S_038000_TILE_MODE(V_038000_ARRAY_1D_TILED_THIN1);
  1832. }
  1833. texture = reloc->robj;
  1834. /* tex mip base */
  1835. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1836. if (r) {
  1837. DRM_ERROR("bad SET_RESOURCE\n");
  1838. return -EINVAL;
  1839. }
  1840. mip_offset = (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1841. mipmap = reloc->robj;
  1842. r = r600_check_texture_resource(p, idx+(i*7)+1,
  1843. texture, mipmap,
  1844. base_offset + radeon_get_ib_value(p, idx+1+(i*7)+2),
  1845. mip_offset + radeon_get_ib_value(p, idx+1+(i*7)+3),
  1846. reloc->tiling_flags);
  1847. if (r)
  1848. return r;
  1849. ib[idx+1+(i*7)+2] += base_offset;
  1850. ib[idx+1+(i*7)+3] += mip_offset;
  1851. break;
  1852. case SQ_TEX_VTX_VALID_BUFFER:
  1853. {
  1854. uint64_t offset64;
  1855. /* vtx base */
  1856. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1857. if (r) {
  1858. DRM_ERROR("bad SET_RESOURCE\n");
  1859. return -EINVAL;
  1860. }
  1861. offset = radeon_get_ib_value(p, idx+1+(i*7)+0);
  1862. size = radeon_get_ib_value(p, idx+1+(i*7)+1) + 1;
  1863. if (p->rdev && (size + offset) > radeon_bo_size(reloc->robj)) {
  1864. /* force size to size of the buffer */
  1865. dev_warn(p->dev, "vbo resource seems too big (%d) for the bo (%ld)\n",
  1866. size + offset, radeon_bo_size(reloc->robj));
  1867. ib[idx+1+(i*7)+1] = radeon_bo_size(reloc->robj) - offset;
  1868. }
  1869. offset64 = reloc->gpu_offset + offset;
  1870. ib[idx+1+(i*8)+0] = offset64;
  1871. ib[idx+1+(i*8)+2] = (ib[idx+1+(i*8)+2] & 0xffffff00) |
  1872. (upper_32_bits(offset64) & 0xff);
  1873. break;
  1874. }
  1875. case SQ_TEX_VTX_INVALID_TEXTURE:
  1876. case SQ_TEX_VTX_INVALID_BUFFER:
  1877. default:
  1878. DRM_ERROR("bad SET_RESOURCE\n");
  1879. return -EINVAL;
  1880. }
  1881. }
  1882. break;
  1883. case PACKET3_SET_ALU_CONST:
  1884. if (track->sq_config & DX9_CONSTS) {
  1885. start_reg = (idx_value << 2) + PACKET3_SET_ALU_CONST_OFFSET;
  1886. end_reg = 4 * pkt->count + start_reg - 4;
  1887. if ((start_reg < PACKET3_SET_ALU_CONST_OFFSET) ||
  1888. (start_reg >= PACKET3_SET_ALU_CONST_END) ||
  1889. (end_reg >= PACKET3_SET_ALU_CONST_END)) {
  1890. DRM_ERROR("bad SET_ALU_CONST\n");
  1891. return -EINVAL;
  1892. }
  1893. }
  1894. break;
  1895. case PACKET3_SET_BOOL_CONST:
  1896. start_reg = (idx_value << 2) + PACKET3_SET_BOOL_CONST_OFFSET;
  1897. end_reg = 4 * pkt->count + start_reg - 4;
  1898. if ((start_reg < PACKET3_SET_BOOL_CONST_OFFSET) ||
  1899. (start_reg >= PACKET3_SET_BOOL_CONST_END) ||
  1900. (end_reg >= PACKET3_SET_BOOL_CONST_END)) {
  1901. DRM_ERROR("bad SET_BOOL_CONST\n");
  1902. return -EINVAL;
  1903. }
  1904. break;
  1905. case PACKET3_SET_LOOP_CONST:
  1906. start_reg = (idx_value << 2) + PACKET3_SET_LOOP_CONST_OFFSET;
  1907. end_reg = 4 * pkt->count + start_reg - 4;
  1908. if ((start_reg < PACKET3_SET_LOOP_CONST_OFFSET) ||
  1909. (start_reg >= PACKET3_SET_LOOP_CONST_END) ||
  1910. (end_reg >= PACKET3_SET_LOOP_CONST_END)) {
  1911. DRM_ERROR("bad SET_LOOP_CONST\n");
  1912. return -EINVAL;
  1913. }
  1914. break;
  1915. case PACKET3_SET_CTL_CONST:
  1916. start_reg = (idx_value << 2) + PACKET3_SET_CTL_CONST_OFFSET;
  1917. end_reg = 4 * pkt->count + start_reg - 4;
  1918. if ((start_reg < PACKET3_SET_CTL_CONST_OFFSET) ||
  1919. (start_reg >= PACKET3_SET_CTL_CONST_END) ||
  1920. (end_reg >= PACKET3_SET_CTL_CONST_END)) {
  1921. DRM_ERROR("bad SET_CTL_CONST\n");
  1922. return -EINVAL;
  1923. }
  1924. break;
  1925. case PACKET3_SET_SAMPLER:
  1926. if (pkt->count % 3) {
  1927. DRM_ERROR("bad SET_SAMPLER\n");
  1928. return -EINVAL;
  1929. }
  1930. start_reg = (idx_value << 2) + PACKET3_SET_SAMPLER_OFFSET;
  1931. end_reg = 4 * pkt->count + start_reg - 4;
  1932. if ((start_reg < PACKET3_SET_SAMPLER_OFFSET) ||
  1933. (start_reg >= PACKET3_SET_SAMPLER_END) ||
  1934. (end_reg >= PACKET3_SET_SAMPLER_END)) {
  1935. DRM_ERROR("bad SET_SAMPLER\n");
  1936. return -EINVAL;
  1937. }
  1938. break;
  1939. case PACKET3_STRMOUT_BASE_UPDATE:
  1940. /* RS780 and RS880 also need this */
  1941. if (p->family < CHIP_RS780) {
  1942. DRM_ERROR("STRMOUT_BASE_UPDATE only supported on 7xx\n");
  1943. return -EINVAL;
  1944. }
  1945. if (pkt->count != 1) {
  1946. DRM_ERROR("bad STRMOUT_BASE_UPDATE packet count\n");
  1947. return -EINVAL;
  1948. }
  1949. if (idx_value > 3) {
  1950. DRM_ERROR("bad STRMOUT_BASE_UPDATE index\n");
  1951. return -EINVAL;
  1952. }
  1953. {
  1954. u64 offset;
  1955. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1956. if (r) {
  1957. DRM_ERROR("bad STRMOUT_BASE_UPDATE reloc\n");
  1958. return -EINVAL;
  1959. }
  1960. if (reloc->robj != track->vgt_strmout_bo[idx_value]) {
  1961. DRM_ERROR("bad STRMOUT_BASE_UPDATE, bo does not match\n");
  1962. return -EINVAL;
  1963. }
  1964. offset = radeon_get_ib_value(p, idx+1) << 8;
  1965. if (offset != track->vgt_strmout_bo_offset[idx_value]) {
  1966. DRM_ERROR("bad STRMOUT_BASE_UPDATE, bo offset does not match: 0x%llx, 0x%x\n",
  1967. offset, track->vgt_strmout_bo_offset[idx_value]);
  1968. return -EINVAL;
  1969. }
  1970. if ((offset + 4) > radeon_bo_size(reloc->robj)) {
  1971. DRM_ERROR("bad STRMOUT_BASE_UPDATE bo too small: 0x%llx, 0x%lx\n",
  1972. offset + 4, radeon_bo_size(reloc->robj));
  1973. return -EINVAL;
  1974. }
  1975. ib[idx+1] += (u32)((reloc->gpu_offset >> 8) & 0xffffffff);
  1976. }
  1977. break;
  1978. case PACKET3_SURFACE_BASE_UPDATE:
  1979. if (p->family >= CHIP_RV770 || p->family == CHIP_R600) {
  1980. DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
  1981. return -EINVAL;
  1982. }
  1983. if (pkt->count) {
  1984. DRM_ERROR("bad SURFACE_BASE_UPDATE\n");
  1985. return -EINVAL;
  1986. }
  1987. break;
  1988. case PACKET3_STRMOUT_BUFFER_UPDATE:
  1989. if (pkt->count != 4) {
  1990. DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (invalid count)\n");
  1991. return -EINVAL;
  1992. }
  1993. /* Updating memory at DST_ADDRESS. */
  1994. if (idx_value & 0x1) {
  1995. u64 offset;
  1996. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  1997. if (r) {
  1998. DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (missing dst reloc)\n");
  1999. return -EINVAL;
  2000. }
  2001. offset = radeon_get_ib_value(p, idx+1);
  2002. offset += ((u64)(radeon_get_ib_value(p, idx+2) & 0xff)) << 32;
  2003. if ((offset + 4) > radeon_bo_size(reloc->robj)) {
  2004. DRM_ERROR("bad STRMOUT_BUFFER_UPDATE dst bo too small: 0x%llx, 0x%lx\n",
  2005. offset + 4, radeon_bo_size(reloc->robj));
  2006. return -EINVAL;
  2007. }
  2008. offset += reloc->gpu_offset;
  2009. ib[idx+1] = offset;
  2010. ib[idx+2] = upper_32_bits(offset) & 0xff;
  2011. }
  2012. /* Reading data from SRC_ADDRESS. */
  2013. if (((idx_value >> 1) & 0x3) == 2) {
  2014. u64 offset;
  2015. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  2016. if (r) {
  2017. DRM_ERROR("bad STRMOUT_BUFFER_UPDATE (missing src reloc)\n");
  2018. return -EINVAL;
  2019. }
  2020. offset = radeon_get_ib_value(p, idx+3);
  2021. offset += ((u64)(radeon_get_ib_value(p, idx+4) & 0xff)) << 32;
  2022. if ((offset + 4) > radeon_bo_size(reloc->robj)) {
  2023. DRM_ERROR("bad STRMOUT_BUFFER_UPDATE src bo too small: 0x%llx, 0x%lx\n",
  2024. offset + 4, radeon_bo_size(reloc->robj));
  2025. return -EINVAL;
  2026. }
  2027. offset += reloc->gpu_offset;
  2028. ib[idx+3] = offset;
  2029. ib[idx+4] = upper_32_bits(offset) & 0xff;
  2030. }
  2031. break;
  2032. case PACKET3_MEM_WRITE:
  2033. {
  2034. u64 offset;
  2035. if (pkt->count != 3) {
  2036. DRM_ERROR("bad MEM_WRITE (invalid count)\n");
  2037. return -EINVAL;
  2038. }
  2039. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  2040. if (r) {
  2041. DRM_ERROR("bad MEM_WRITE (missing reloc)\n");
  2042. return -EINVAL;
  2043. }
  2044. offset = radeon_get_ib_value(p, idx+0);
  2045. offset += ((u64)(radeon_get_ib_value(p, idx+1) & 0xff)) << 32UL;
  2046. if (offset & 0x7) {
  2047. DRM_ERROR("bad MEM_WRITE (address not qwords aligned)\n");
  2048. return -EINVAL;
  2049. }
  2050. if ((offset + 8) > radeon_bo_size(reloc->robj)) {
  2051. DRM_ERROR("bad MEM_WRITE bo too small: 0x%llx, 0x%lx\n",
  2052. offset + 8, radeon_bo_size(reloc->robj));
  2053. return -EINVAL;
  2054. }
  2055. offset += reloc->gpu_offset;
  2056. ib[idx+0] = offset;
  2057. ib[idx+1] = upper_32_bits(offset) & 0xff;
  2058. break;
  2059. }
  2060. case PACKET3_COPY_DW:
  2061. if (pkt->count != 4) {
  2062. DRM_ERROR("bad COPY_DW (invalid count)\n");
  2063. return -EINVAL;
  2064. }
  2065. if (idx_value & 0x1) {
  2066. u64 offset;
  2067. /* SRC is memory. */
  2068. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  2069. if (r) {
  2070. DRM_ERROR("bad COPY_DW (missing src reloc)\n");
  2071. return -EINVAL;
  2072. }
  2073. offset = radeon_get_ib_value(p, idx+1);
  2074. offset += ((u64)(radeon_get_ib_value(p, idx+2) & 0xff)) << 32;
  2075. if ((offset + 4) > radeon_bo_size(reloc->robj)) {
  2076. DRM_ERROR("bad COPY_DW src bo too small: 0x%llx, 0x%lx\n",
  2077. offset + 4, radeon_bo_size(reloc->robj));
  2078. return -EINVAL;
  2079. }
  2080. offset += reloc->gpu_offset;
  2081. ib[idx+1] = offset;
  2082. ib[idx+2] = upper_32_bits(offset) & 0xff;
  2083. } else {
  2084. /* SRC is a reg. */
  2085. reg = radeon_get_ib_value(p, idx+1) << 2;
  2086. if (!r600_is_safe_reg(p, reg, idx+1))
  2087. return -EINVAL;
  2088. }
  2089. if (idx_value & 0x2) {
  2090. u64 offset;
  2091. /* DST is memory. */
  2092. r = radeon_cs_packet_next_reloc(p, &reloc, r600_nomm);
  2093. if (r) {
  2094. DRM_ERROR("bad COPY_DW (missing dst reloc)\n");
  2095. return -EINVAL;
  2096. }
  2097. offset = radeon_get_ib_value(p, idx+3);
  2098. offset += ((u64)(radeon_get_ib_value(p, idx+4) & 0xff)) << 32;
  2099. if ((offset + 4) > radeon_bo_size(reloc->robj)) {
  2100. DRM_ERROR("bad COPY_DW dst bo too small: 0x%llx, 0x%lx\n",
  2101. offset + 4, radeon_bo_size(reloc->robj));
  2102. return -EINVAL;
  2103. }
  2104. offset += reloc->gpu_offset;
  2105. ib[idx+3] = offset;
  2106. ib[idx+4] = upper_32_bits(offset) & 0xff;
  2107. } else {
  2108. /* DST is a reg. */
  2109. reg = radeon_get_ib_value(p, idx+3) << 2;
  2110. if (!r600_is_safe_reg(p, reg, idx+3))
  2111. return -EINVAL;
  2112. }
  2113. break;
  2114. case PACKET3_NOP:
  2115. break;
  2116. default:
  2117. DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
  2118. return -EINVAL;
  2119. }
  2120. return 0;
  2121. }
  2122. int r600_cs_parse(struct radeon_cs_parser *p)
  2123. {
  2124. struct radeon_cs_packet pkt;
  2125. struct r600_cs_track *track;
  2126. int r;
  2127. if (p->track == NULL) {
  2128. /* initialize tracker, we are in kms */
  2129. track = kzalloc(sizeof(*track), GFP_KERNEL);
  2130. if (track == NULL)
  2131. return -ENOMEM;
  2132. r600_cs_track_init(track);
  2133. if (p->rdev->family < CHIP_RV770) {
  2134. track->npipes = p->rdev->config.r600.tiling_npipes;
  2135. track->nbanks = p->rdev->config.r600.tiling_nbanks;
  2136. track->group_size = p->rdev->config.r600.tiling_group_size;
  2137. } else if (p->rdev->family <= CHIP_RV740) {
  2138. track->npipes = p->rdev->config.rv770.tiling_npipes;
  2139. track->nbanks = p->rdev->config.rv770.tiling_nbanks;
  2140. track->group_size = p->rdev->config.rv770.tiling_group_size;
  2141. }
  2142. p->track = track;
  2143. }
  2144. do {
  2145. r = radeon_cs_packet_parse(p, &pkt, p->idx);
  2146. if (r) {
  2147. kfree(p->track);
  2148. p->track = NULL;
  2149. return r;
  2150. }
  2151. p->idx += pkt.count + 2;
  2152. switch (pkt.type) {
  2153. case RADEON_PACKET_TYPE0:
  2154. r = r600_cs_parse_packet0(p, &pkt);
  2155. break;
  2156. case RADEON_PACKET_TYPE2:
  2157. break;
  2158. case RADEON_PACKET_TYPE3:
  2159. r = r600_packet3_check(p, &pkt);
  2160. break;
  2161. default:
  2162. DRM_ERROR("Unknown packet type %d !\n", pkt.type);
  2163. kfree(p->track);
  2164. p->track = NULL;
  2165. return -EINVAL;
  2166. }
  2167. if (r) {
  2168. kfree(p->track);
  2169. p->track = NULL;
  2170. return r;
  2171. }
  2172. } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
  2173. #if 0
  2174. for (r = 0; r < p->ib.length_dw; r++) {
  2175. printk(KERN_INFO "%05d 0x%08X\n", r, p->ib.ptr[r]);
  2176. mdelay(1);
  2177. }
  2178. #endif
  2179. kfree(p->track);
  2180. p->track = NULL;
  2181. return 0;
  2182. }
  2183. #ifdef CONFIG_DRM_RADEON_UMS
  2184. /**
  2185. * cs_parser_fini() - clean parser states
  2186. * @parser: parser structure holding parsing context.
  2187. * @error: error number
  2188. *
  2189. * If error is set than unvalidate buffer, otherwise just free memory
  2190. * used by parsing context.
  2191. **/
  2192. static void r600_cs_parser_fini(struct radeon_cs_parser *parser, int error)
  2193. {
  2194. unsigned i;
  2195. kfree(parser->relocs);
  2196. for (i = 0; i < parser->nchunks; i++)
  2197. drm_free_large(parser->chunks[i].kdata);
  2198. kfree(parser->chunks);
  2199. kfree(parser->chunks_array);
  2200. }
  2201. static int r600_cs_parser_relocs_legacy(struct radeon_cs_parser *p)
  2202. {
  2203. if (p->chunk_relocs_idx == -1) {
  2204. return 0;
  2205. }
  2206. p->relocs = kzalloc(sizeof(struct radeon_cs_reloc), GFP_KERNEL);
  2207. if (p->relocs == NULL) {
  2208. return -ENOMEM;
  2209. }
  2210. return 0;
  2211. }
  2212. int r600_cs_legacy(struct drm_device *dev, void *data, struct drm_file *filp,
  2213. unsigned family, u32 *ib, int *l)
  2214. {
  2215. struct radeon_cs_parser parser;
  2216. struct radeon_cs_chunk *ib_chunk;
  2217. struct r600_cs_track *track;
  2218. int r;
  2219. /* initialize tracker */
  2220. track = kzalloc(sizeof(*track), GFP_KERNEL);
  2221. if (track == NULL)
  2222. return -ENOMEM;
  2223. r600_cs_track_init(track);
  2224. r600_cs_legacy_get_tiling_conf(dev, &track->npipes, &track->nbanks, &track->group_size);
  2225. /* initialize parser */
  2226. memset(&parser, 0, sizeof(struct radeon_cs_parser));
  2227. parser.filp = filp;
  2228. parser.dev = &dev->pdev->dev;
  2229. parser.rdev = NULL;
  2230. parser.family = family;
  2231. parser.track = track;
  2232. parser.ib.ptr = ib;
  2233. r = radeon_cs_parser_init(&parser, data);
  2234. if (r) {
  2235. DRM_ERROR("Failed to initialize parser !\n");
  2236. r600_cs_parser_fini(&parser, r);
  2237. return r;
  2238. }
  2239. r = r600_cs_parser_relocs_legacy(&parser);
  2240. if (r) {
  2241. DRM_ERROR("Failed to parse relocation !\n");
  2242. r600_cs_parser_fini(&parser, r);
  2243. return r;
  2244. }
  2245. /* Copy the packet into the IB, the parser will read from the
  2246. * input memory (cached) and write to the IB (which can be
  2247. * uncached). */
  2248. ib_chunk = &parser.chunks[parser.chunk_ib_idx];
  2249. parser.ib.length_dw = ib_chunk->length_dw;
  2250. *l = parser.ib.length_dw;
  2251. if (copy_from_user(ib, ib_chunk->user_ptr, ib_chunk->length_dw * 4)) {
  2252. r = -EFAULT;
  2253. r600_cs_parser_fini(&parser, r);
  2254. return r;
  2255. }
  2256. r = r600_cs_parse(&parser);
  2257. if (r) {
  2258. DRM_ERROR("Invalid command stream !\n");
  2259. r600_cs_parser_fini(&parser, r);
  2260. return r;
  2261. }
  2262. r600_cs_parser_fini(&parser, r);
  2263. return r;
  2264. }
  2265. void r600_cs_legacy_init(void)
  2266. {
  2267. r600_nomm = 1;
  2268. }
  2269. #endif
  2270. /*
  2271. * DMA
  2272. */
  2273. /**
  2274. * r600_dma_cs_next_reloc() - parse next reloc
  2275. * @p: parser structure holding parsing context.
  2276. * @cs_reloc: reloc informations
  2277. *
  2278. * Return the next reloc, do bo validation and compute
  2279. * GPU offset using the provided start.
  2280. **/
  2281. int r600_dma_cs_next_reloc(struct radeon_cs_parser *p,
  2282. struct radeon_cs_reloc **cs_reloc)
  2283. {
  2284. struct radeon_cs_chunk *relocs_chunk;
  2285. unsigned idx;
  2286. *cs_reloc = NULL;
  2287. if (p->chunk_relocs_idx == -1) {
  2288. DRM_ERROR("No relocation chunk !\n");
  2289. return -EINVAL;
  2290. }
  2291. relocs_chunk = &p->chunks[p->chunk_relocs_idx];
  2292. idx = p->dma_reloc_idx;
  2293. if (idx >= p->nrelocs) {
  2294. DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
  2295. idx, p->nrelocs);
  2296. return -EINVAL;
  2297. }
  2298. *cs_reloc = p->relocs_ptr[idx];
  2299. p->dma_reloc_idx++;
  2300. return 0;
  2301. }
  2302. #define GET_DMA_CMD(h) (((h) & 0xf0000000) >> 28)
  2303. #define GET_DMA_COUNT(h) ((h) & 0x0000ffff)
  2304. #define GET_DMA_T(h) (((h) & 0x00800000) >> 23)
  2305. /**
  2306. * r600_dma_cs_parse() - parse the DMA IB
  2307. * @p: parser structure holding parsing context.
  2308. *
  2309. * Parses the DMA IB from the CS ioctl and updates
  2310. * the GPU addresses based on the reloc information and
  2311. * checks for errors. (R6xx-R7xx)
  2312. * Returns 0 for success and an error on failure.
  2313. **/
  2314. int r600_dma_cs_parse(struct radeon_cs_parser *p)
  2315. {
  2316. struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
  2317. struct radeon_cs_reloc *src_reloc, *dst_reloc;
  2318. u32 header, cmd, count, tiled;
  2319. volatile u32 *ib = p->ib.ptr;
  2320. u32 idx, idx_value;
  2321. u64 src_offset, dst_offset;
  2322. int r;
  2323. do {
  2324. if (p->idx >= ib_chunk->length_dw) {
  2325. DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
  2326. p->idx, ib_chunk->length_dw);
  2327. return -EINVAL;
  2328. }
  2329. idx = p->idx;
  2330. header = radeon_get_ib_value(p, idx);
  2331. cmd = GET_DMA_CMD(header);
  2332. count = GET_DMA_COUNT(header);
  2333. tiled = GET_DMA_T(header);
  2334. switch (cmd) {
  2335. case DMA_PACKET_WRITE:
  2336. r = r600_dma_cs_next_reloc(p, &dst_reloc);
  2337. if (r) {
  2338. DRM_ERROR("bad DMA_PACKET_WRITE\n");
  2339. return -EINVAL;
  2340. }
  2341. if (tiled) {
  2342. dst_offset = radeon_get_ib_value(p, idx+1);
  2343. dst_offset <<= 8;
  2344. ib[idx+1] += (u32)(dst_reloc->gpu_offset >> 8);
  2345. p->idx += count + 5;
  2346. } else {
  2347. dst_offset = radeon_get_ib_value(p, idx+1);
  2348. dst_offset |= ((u64)(radeon_get_ib_value(p, idx+2) & 0xff)) << 32;
  2349. ib[idx+1] += (u32)(dst_reloc->gpu_offset & 0xfffffffc);
  2350. ib[idx+2] += upper_32_bits(dst_reloc->gpu_offset) & 0xff;
  2351. p->idx += count + 3;
  2352. }
  2353. if ((dst_offset + (count * 4)) > radeon_bo_size(dst_reloc->robj)) {
  2354. dev_warn(p->dev, "DMA write buffer too small (%llu %lu)\n",
  2355. dst_offset + (count * 4), radeon_bo_size(dst_reloc->robj));
  2356. return -EINVAL;
  2357. }
  2358. break;
  2359. case DMA_PACKET_COPY:
  2360. r = r600_dma_cs_next_reloc(p, &src_reloc);
  2361. if (r) {
  2362. DRM_ERROR("bad DMA_PACKET_COPY\n");
  2363. return -EINVAL;
  2364. }
  2365. r = r600_dma_cs_next_reloc(p, &dst_reloc);
  2366. if (r) {
  2367. DRM_ERROR("bad DMA_PACKET_COPY\n");
  2368. return -EINVAL;
  2369. }
  2370. if (tiled) {
  2371. idx_value = radeon_get_ib_value(p, idx + 2);
  2372. /* detile bit */
  2373. if (idx_value & (1 << 31)) {
  2374. /* tiled src, linear dst */
  2375. src_offset = radeon_get_ib_value(p, idx+1);
  2376. src_offset <<= 8;
  2377. ib[idx+1] += (u32)(src_reloc->gpu_offset >> 8);
  2378. dst_offset = radeon_get_ib_value(p, idx+5);
  2379. dst_offset |= ((u64)(radeon_get_ib_value(p, idx+6) & 0xff)) << 32;
  2380. ib[idx+5] += (u32)(dst_reloc->gpu_offset & 0xfffffffc);
  2381. ib[idx+6] += upper_32_bits(dst_reloc->gpu_offset) & 0xff;
  2382. } else {
  2383. /* linear src, tiled dst */
  2384. src_offset = radeon_get_ib_value(p, idx+5);
  2385. src_offset |= ((u64)(radeon_get_ib_value(p, idx+6) & 0xff)) << 32;
  2386. ib[idx+5] += (u32)(src_reloc->gpu_offset & 0xfffffffc);
  2387. ib[idx+6] += upper_32_bits(src_reloc->gpu_offset) & 0xff;
  2388. dst_offset = radeon_get_ib_value(p, idx+1);
  2389. dst_offset <<= 8;
  2390. ib[idx+1] += (u32)(dst_reloc->gpu_offset >> 8);
  2391. }
  2392. p->idx += 7;
  2393. } else {
  2394. if (p->family >= CHIP_RV770) {
  2395. src_offset = radeon_get_ib_value(p, idx+2);
  2396. src_offset |= ((u64)(radeon_get_ib_value(p, idx+4) & 0xff)) << 32;
  2397. dst_offset = radeon_get_ib_value(p, idx+1);
  2398. dst_offset |= ((u64)(radeon_get_ib_value(p, idx+3) & 0xff)) << 32;
  2399. ib[idx+1] += (u32)(dst_reloc->gpu_offset & 0xfffffffc);
  2400. ib[idx+2] += (u32)(src_reloc->gpu_offset & 0xfffffffc);
  2401. ib[idx+3] += upper_32_bits(dst_reloc->gpu_offset) & 0xff;
  2402. ib[idx+4] += upper_32_bits(src_reloc->gpu_offset) & 0xff;
  2403. p->idx += 5;
  2404. } else {
  2405. src_offset = radeon_get_ib_value(p, idx+2);
  2406. src_offset |= ((u64)(radeon_get_ib_value(p, idx+3) & 0xff)) << 32;
  2407. dst_offset = radeon_get_ib_value(p, idx+1);
  2408. dst_offset |= ((u64)(radeon_get_ib_value(p, idx+3) & 0xff0000)) << 16;
  2409. ib[idx+1] += (u32)(dst_reloc->gpu_offset & 0xfffffffc);
  2410. ib[idx+2] += (u32)(src_reloc->gpu_offset & 0xfffffffc);
  2411. ib[idx+3] += upper_32_bits(src_reloc->gpu_offset) & 0xff;
  2412. ib[idx+3] += (upper_32_bits(dst_reloc->gpu_offset) & 0xff) << 16;
  2413. p->idx += 4;
  2414. }
  2415. }
  2416. if ((src_offset + (count * 4)) > radeon_bo_size(src_reloc->robj)) {
  2417. dev_warn(p->dev, "DMA copy src buffer too small (%llu %lu)\n",
  2418. src_offset + (count * 4), radeon_bo_size(src_reloc->robj));
  2419. return -EINVAL;
  2420. }
  2421. if ((dst_offset + (count * 4)) > radeon_bo_size(dst_reloc->robj)) {
  2422. dev_warn(p->dev, "DMA write dst buffer too small (%llu %lu)\n",
  2423. dst_offset + (count * 4), radeon_bo_size(dst_reloc->robj));
  2424. return -EINVAL;
  2425. }
  2426. break;
  2427. case DMA_PACKET_CONSTANT_FILL:
  2428. if (p->family < CHIP_RV770) {
  2429. DRM_ERROR("Constant Fill is 7xx only !\n");
  2430. return -EINVAL;
  2431. }
  2432. r = r600_dma_cs_next_reloc(p, &dst_reloc);
  2433. if (r) {
  2434. DRM_ERROR("bad DMA_PACKET_WRITE\n");
  2435. return -EINVAL;
  2436. }
  2437. dst_offset = radeon_get_ib_value(p, idx+1);
  2438. dst_offset |= ((u64)(radeon_get_ib_value(p, idx+3) & 0x00ff0000)) << 16;
  2439. if ((dst_offset + (count * 4)) > radeon_bo_size(dst_reloc->robj)) {
  2440. dev_warn(p->dev, "DMA constant fill buffer too small (%llu %lu)\n",
  2441. dst_offset + (count * 4), radeon_bo_size(dst_reloc->robj));
  2442. return -EINVAL;
  2443. }
  2444. ib[idx+1] += (u32)(dst_reloc->gpu_offset & 0xfffffffc);
  2445. ib[idx+3] += (upper_32_bits(dst_reloc->gpu_offset) << 16) & 0x00ff0000;
  2446. p->idx += 4;
  2447. break;
  2448. case DMA_PACKET_NOP:
  2449. p->idx += 1;
  2450. break;
  2451. default:
  2452. DRM_ERROR("Unknown packet type %d at %d !\n", cmd, idx);
  2453. return -EINVAL;
  2454. }
  2455. } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
  2456. #if 0
  2457. for (r = 0; r < p->ib->length_dw; r++) {
  2458. printk(KERN_INFO "%05d 0x%08X\n", r, p->ib.ptr[r]);
  2459. mdelay(1);
  2460. }
  2461. #endif
  2462. return 0;
  2463. }