r600_cs.c 77 KB

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