r600_cs.c 80 KB

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