radeon_state.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261
  1. /* radeon_state.c -- State support for Radeon -*- linux-c -*- */
  2. /*
  3. * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
  4. * All Rights Reserved.
  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 (including the next
  14. * paragraph) shall be included in all copies or substantial portions of the
  15. * Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. *
  25. * Authors:
  26. * Gareth Hughes <gareth@valinux.com>
  27. * Kevin E. Martin <martin@valinux.com>
  28. *
  29. * ------------------------ This file is DEPRECATED! -------------------------
  30. */
  31. #include <drm/drmP.h>
  32. #include <drm/radeon_drm.h>
  33. #include "radeon_drv.h"
  34. #include "drm_buffer.h"
  35. /* ================================================================
  36. * Helper functions for client state checking and fixup
  37. */
  38. static __inline__ int radeon_check_and_fixup_offset(drm_radeon_private_t *
  39. dev_priv,
  40. struct drm_file * file_priv,
  41. u32 *offset)
  42. {
  43. u64 off = *offset;
  44. u32 fb_end = dev_priv->fb_location + dev_priv->fb_size - 1;
  45. struct drm_radeon_driver_file_fields *radeon_priv;
  46. /* Hrm ... the story of the offset ... So this function converts
  47. * the various ideas of what userland clients might have for an
  48. * offset in the card address space into an offset into the card
  49. * address space :) So with a sane client, it should just keep
  50. * the value intact and just do some boundary checking. However,
  51. * not all clients are sane. Some older clients pass us 0 based
  52. * offsets relative to the start of the framebuffer and some may
  53. * assume the AGP aperture it appended to the framebuffer, so we
  54. * try to detect those cases and fix them up.
  55. *
  56. * Note: It might be a good idea here to make sure the offset lands
  57. * in some "allowed" area to protect things like the PCIE GART...
  58. */
  59. /* First, the best case, the offset already lands in either the
  60. * framebuffer or the GART mapped space
  61. */
  62. if (radeon_check_offset(dev_priv, off))
  63. return 0;
  64. /* Ok, that didn't happen... now check if we have a zero based
  65. * offset that fits in the framebuffer + gart space, apply the
  66. * magic offset we get from SETPARAM or calculated from fb_location
  67. */
  68. if (off < (dev_priv->fb_size + dev_priv->gart_size)) {
  69. radeon_priv = file_priv->driver_priv;
  70. off += radeon_priv->radeon_fb_delta;
  71. }
  72. /* Finally, assume we aimed at a GART offset if beyond the fb */
  73. if (off > fb_end)
  74. off = off - fb_end - 1 + dev_priv->gart_vm_start;
  75. /* Now recheck and fail if out of bounds */
  76. if (radeon_check_offset(dev_priv, off)) {
  77. DRM_DEBUG("offset fixed up to 0x%x\n", (unsigned int)off);
  78. *offset = off;
  79. return 0;
  80. }
  81. return -EINVAL;
  82. }
  83. static __inline__ int radeon_check_and_fixup_packets(drm_radeon_private_t *
  84. dev_priv,
  85. struct drm_file *file_priv,
  86. int id, struct drm_buffer *buf)
  87. {
  88. u32 *data;
  89. switch (id) {
  90. case RADEON_EMIT_PP_MISC:
  91. data = drm_buffer_pointer_to_dword(buf,
  92. (RADEON_RB3D_DEPTHOFFSET - RADEON_PP_MISC) / 4);
  93. if (radeon_check_and_fixup_offset(dev_priv, file_priv, data)) {
  94. DRM_ERROR("Invalid depth buffer offset\n");
  95. return -EINVAL;
  96. }
  97. dev_priv->have_z_offset = 1;
  98. break;
  99. case RADEON_EMIT_PP_CNTL:
  100. data = drm_buffer_pointer_to_dword(buf,
  101. (RADEON_RB3D_COLOROFFSET - RADEON_PP_CNTL) / 4);
  102. if (radeon_check_and_fixup_offset(dev_priv, file_priv, data)) {
  103. DRM_ERROR("Invalid colour buffer offset\n");
  104. return -EINVAL;
  105. }
  106. break;
  107. case R200_EMIT_PP_TXOFFSET_0:
  108. case R200_EMIT_PP_TXOFFSET_1:
  109. case R200_EMIT_PP_TXOFFSET_2:
  110. case R200_EMIT_PP_TXOFFSET_3:
  111. case R200_EMIT_PP_TXOFFSET_4:
  112. case R200_EMIT_PP_TXOFFSET_5:
  113. data = drm_buffer_pointer_to_dword(buf, 0);
  114. if (radeon_check_and_fixup_offset(dev_priv, file_priv, data)) {
  115. DRM_ERROR("Invalid R200 texture offset\n");
  116. return -EINVAL;
  117. }
  118. break;
  119. case RADEON_EMIT_PP_TXFILTER_0:
  120. case RADEON_EMIT_PP_TXFILTER_1:
  121. case RADEON_EMIT_PP_TXFILTER_2:
  122. data = drm_buffer_pointer_to_dword(buf,
  123. (RADEON_PP_TXOFFSET_0 - RADEON_PP_TXFILTER_0) / 4);
  124. if (radeon_check_and_fixup_offset(dev_priv, file_priv, data)) {
  125. DRM_ERROR("Invalid R100 texture offset\n");
  126. return -EINVAL;
  127. }
  128. break;
  129. case R200_EMIT_PP_CUBIC_OFFSETS_0:
  130. case R200_EMIT_PP_CUBIC_OFFSETS_1:
  131. case R200_EMIT_PP_CUBIC_OFFSETS_2:
  132. case R200_EMIT_PP_CUBIC_OFFSETS_3:
  133. case R200_EMIT_PP_CUBIC_OFFSETS_4:
  134. case R200_EMIT_PP_CUBIC_OFFSETS_5:{
  135. int i;
  136. for (i = 0; i < 5; i++) {
  137. data = drm_buffer_pointer_to_dword(buf, i);
  138. if (radeon_check_and_fixup_offset(dev_priv,
  139. file_priv,
  140. data)) {
  141. DRM_ERROR
  142. ("Invalid R200 cubic texture offset\n");
  143. return -EINVAL;
  144. }
  145. }
  146. break;
  147. }
  148. case RADEON_EMIT_PP_CUBIC_OFFSETS_T0:
  149. case RADEON_EMIT_PP_CUBIC_OFFSETS_T1:
  150. case RADEON_EMIT_PP_CUBIC_OFFSETS_T2:{
  151. int i;
  152. for (i = 0; i < 5; i++) {
  153. data = drm_buffer_pointer_to_dword(buf, i);
  154. if (radeon_check_and_fixup_offset(dev_priv,
  155. file_priv,
  156. data)) {
  157. DRM_ERROR
  158. ("Invalid R100 cubic texture offset\n");
  159. return -EINVAL;
  160. }
  161. }
  162. }
  163. break;
  164. case R200_EMIT_VAP_CTL:{
  165. RING_LOCALS;
  166. BEGIN_RING(2);
  167. OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
  168. ADVANCE_RING();
  169. }
  170. break;
  171. case RADEON_EMIT_RB3D_COLORPITCH:
  172. case RADEON_EMIT_RE_LINE_PATTERN:
  173. case RADEON_EMIT_SE_LINE_WIDTH:
  174. case RADEON_EMIT_PP_LUM_MATRIX:
  175. case RADEON_EMIT_PP_ROT_MATRIX_0:
  176. case RADEON_EMIT_RB3D_STENCILREFMASK:
  177. case RADEON_EMIT_SE_VPORT_XSCALE:
  178. case RADEON_EMIT_SE_CNTL:
  179. case RADEON_EMIT_SE_CNTL_STATUS:
  180. case RADEON_EMIT_RE_MISC:
  181. case RADEON_EMIT_PP_BORDER_COLOR_0:
  182. case RADEON_EMIT_PP_BORDER_COLOR_1:
  183. case RADEON_EMIT_PP_BORDER_COLOR_2:
  184. case RADEON_EMIT_SE_ZBIAS_FACTOR:
  185. case RADEON_EMIT_SE_TCL_OUTPUT_VTX_FMT:
  186. case RADEON_EMIT_SE_TCL_MATERIAL_EMMISSIVE_RED:
  187. case R200_EMIT_PP_TXCBLEND_0:
  188. case R200_EMIT_PP_TXCBLEND_1:
  189. case R200_EMIT_PP_TXCBLEND_2:
  190. case R200_EMIT_PP_TXCBLEND_3:
  191. case R200_EMIT_PP_TXCBLEND_4:
  192. case R200_EMIT_PP_TXCBLEND_5:
  193. case R200_EMIT_PP_TXCBLEND_6:
  194. case R200_EMIT_PP_TXCBLEND_7:
  195. case R200_EMIT_TCL_LIGHT_MODEL_CTL_0:
  196. case R200_EMIT_TFACTOR_0:
  197. case R200_EMIT_VTX_FMT_0:
  198. case R200_EMIT_MATRIX_SELECT_0:
  199. case R200_EMIT_TEX_PROC_CTL_2:
  200. case R200_EMIT_TCL_UCP_VERT_BLEND_CTL:
  201. case R200_EMIT_PP_TXFILTER_0:
  202. case R200_EMIT_PP_TXFILTER_1:
  203. case R200_EMIT_PP_TXFILTER_2:
  204. case R200_EMIT_PP_TXFILTER_3:
  205. case R200_EMIT_PP_TXFILTER_4:
  206. case R200_EMIT_PP_TXFILTER_5:
  207. case R200_EMIT_VTE_CNTL:
  208. case R200_EMIT_OUTPUT_VTX_COMP_SEL:
  209. case R200_EMIT_PP_TAM_DEBUG3:
  210. case R200_EMIT_PP_CNTL_X:
  211. case R200_EMIT_RB3D_DEPTHXY_OFFSET:
  212. case R200_EMIT_RE_AUX_SCISSOR_CNTL:
  213. case R200_EMIT_RE_SCISSOR_TL_0:
  214. case R200_EMIT_RE_SCISSOR_TL_1:
  215. case R200_EMIT_RE_SCISSOR_TL_2:
  216. case R200_EMIT_SE_VAP_CNTL_STATUS:
  217. case R200_EMIT_SE_VTX_STATE_CNTL:
  218. case R200_EMIT_RE_POINTSIZE:
  219. case R200_EMIT_TCL_INPUT_VTX_VECTOR_ADDR_0:
  220. case R200_EMIT_PP_CUBIC_FACES_0:
  221. case R200_EMIT_PP_CUBIC_FACES_1:
  222. case R200_EMIT_PP_CUBIC_FACES_2:
  223. case R200_EMIT_PP_CUBIC_FACES_3:
  224. case R200_EMIT_PP_CUBIC_FACES_4:
  225. case R200_EMIT_PP_CUBIC_FACES_5:
  226. case RADEON_EMIT_PP_TEX_SIZE_0:
  227. case RADEON_EMIT_PP_TEX_SIZE_1:
  228. case RADEON_EMIT_PP_TEX_SIZE_2:
  229. case R200_EMIT_RB3D_BLENDCOLOR:
  230. case R200_EMIT_TCL_POINT_SPRITE_CNTL:
  231. case RADEON_EMIT_PP_CUBIC_FACES_0:
  232. case RADEON_EMIT_PP_CUBIC_FACES_1:
  233. case RADEON_EMIT_PP_CUBIC_FACES_2:
  234. case R200_EMIT_PP_TRI_PERF_CNTL:
  235. case R200_EMIT_PP_AFS_0:
  236. case R200_EMIT_PP_AFS_1:
  237. case R200_EMIT_ATF_TFACTOR:
  238. case R200_EMIT_PP_TXCTLALL_0:
  239. case R200_EMIT_PP_TXCTLALL_1:
  240. case R200_EMIT_PP_TXCTLALL_2:
  241. case R200_EMIT_PP_TXCTLALL_3:
  242. case R200_EMIT_PP_TXCTLALL_4:
  243. case R200_EMIT_PP_TXCTLALL_5:
  244. case R200_EMIT_VAP_PVS_CNTL:
  245. /* These packets don't contain memory offsets */
  246. break;
  247. default:
  248. DRM_ERROR("Unknown state packet ID %d\n", id);
  249. return -EINVAL;
  250. }
  251. return 0;
  252. }
  253. static int radeon_check_and_fixup_packet3(drm_radeon_private_t *
  254. dev_priv,
  255. struct drm_file *file_priv,
  256. drm_radeon_kcmd_buffer_t *
  257. cmdbuf,
  258. unsigned int *cmdsz)
  259. {
  260. u32 *cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 0);
  261. u32 offset, narrays;
  262. int count, i, k;
  263. count = ((*cmd & RADEON_CP_PACKET_COUNT_MASK) >> 16);
  264. *cmdsz = 2 + count;
  265. if ((*cmd & 0xc0000000) != RADEON_CP_PACKET3) {
  266. DRM_ERROR("Not a type 3 packet\n");
  267. return -EINVAL;
  268. }
  269. if (4 * *cmdsz > drm_buffer_unprocessed(cmdbuf->buffer)) {
  270. DRM_ERROR("Packet size larger than size of data provided\n");
  271. return -EINVAL;
  272. }
  273. switch (*cmd & 0xff00) {
  274. /* XXX Are there old drivers needing other packets? */
  275. case RADEON_3D_DRAW_IMMD:
  276. case RADEON_3D_DRAW_VBUF:
  277. case RADEON_3D_DRAW_INDX:
  278. case RADEON_WAIT_FOR_IDLE:
  279. case RADEON_CP_NOP:
  280. case RADEON_3D_CLEAR_ZMASK:
  281. /* case RADEON_CP_NEXT_CHAR:
  282. case RADEON_CP_PLY_NEXTSCAN:
  283. case RADEON_CP_SET_SCISSORS: */ /* probably safe but will never need them? */
  284. /* these packets are safe */
  285. break;
  286. case RADEON_CP_3D_DRAW_IMMD_2:
  287. case RADEON_CP_3D_DRAW_VBUF_2:
  288. case RADEON_CP_3D_DRAW_INDX_2:
  289. case RADEON_3D_CLEAR_HIZ:
  290. /* safe but r200 only */
  291. if (dev_priv->microcode_version != UCODE_R200) {
  292. DRM_ERROR("Invalid 3d packet for r100-class chip\n");
  293. return -EINVAL;
  294. }
  295. break;
  296. case RADEON_3D_LOAD_VBPNTR:
  297. if (count > 18) { /* 12 arrays max */
  298. DRM_ERROR("Too large payload in 3D_LOAD_VBPNTR (count=%d)\n",
  299. count);
  300. return -EINVAL;
  301. }
  302. /* carefully check packet contents */
  303. cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
  304. narrays = *cmd & ~0xc000;
  305. k = 0;
  306. i = 2;
  307. while ((k < narrays) && (i < (count + 2))) {
  308. i++; /* skip attribute field */
  309. cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, i);
  310. if (radeon_check_and_fixup_offset(dev_priv, file_priv,
  311. cmd)) {
  312. DRM_ERROR
  313. ("Invalid offset (k=%d i=%d) in 3D_LOAD_VBPNTR packet.\n",
  314. k, i);
  315. return -EINVAL;
  316. }
  317. k++;
  318. i++;
  319. if (k == narrays)
  320. break;
  321. /* have one more to process, they come in pairs */
  322. cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, i);
  323. if (radeon_check_and_fixup_offset(dev_priv,
  324. file_priv, cmd))
  325. {
  326. DRM_ERROR
  327. ("Invalid offset (k=%d i=%d) in 3D_LOAD_VBPNTR packet.\n",
  328. k, i);
  329. return -EINVAL;
  330. }
  331. k++;
  332. i++;
  333. }
  334. /* do the counts match what we expect ? */
  335. if ((k != narrays) || (i != (count + 2))) {
  336. DRM_ERROR
  337. ("Malformed 3D_LOAD_VBPNTR packet (k=%d i=%d narrays=%d count+1=%d).\n",
  338. k, i, narrays, count + 1);
  339. return -EINVAL;
  340. }
  341. break;
  342. case RADEON_3D_RNDR_GEN_INDX_PRIM:
  343. if (dev_priv->microcode_version != UCODE_R100) {
  344. DRM_ERROR("Invalid 3d packet for r200-class chip\n");
  345. return -EINVAL;
  346. }
  347. cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
  348. if (radeon_check_and_fixup_offset(dev_priv, file_priv, cmd)) {
  349. DRM_ERROR("Invalid rndr_gen_indx offset\n");
  350. return -EINVAL;
  351. }
  352. break;
  353. case RADEON_CP_INDX_BUFFER:
  354. if (dev_priv->microcode_version != UCODE_R200) {
  355. DRM_ERROR("Invalid 3d packet for r100-class chip\n");
  356. return -EINVAL;
  357. }
  358. cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
  359. if ((*cmd & 0x8000ffff) != 0x80000810) {
  360. DRM_ERROR("Invalid indx_buffer reg address %08X\n", *cmd);
  361. return -EINVAL;
  362. }
  363. cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 2);
  364. if (radeon_check_and_fixup_offset(dev_priv, file_priv, cmd)) {
  365. DRM_ERROR("Invalid indx_buffer offset is %08X\n", *cmd);
  366. return -EINVAL;
  367. }
  368. break;
  369. case RADEON_CNTL_HOSTDATA_BLT:
  370. case RADEON_CNTL_PAINT_MULTI:
  371. case RADEON_CNTL_BITBLT_MULTI:
  372. /* MSB of opcode: next DWORD GUI_CNTL */
  373. cmd = drm_buffer_pointer_to_dword(cmdbuf->buffer, 1);
  374. if (*cmd & (RADEON_GMC_SRC_PITCH_OFFSET_CNTL
  375. | RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
  376. u32 *cmd2 = drm_buffer_pointer_to_dword(cmdbuf->buffer, 2);
  377. offset = *cmd2 << 10;
  378. if (radeon_check_and_fixup_offset
  379. (dev_priv, file_priv, &offset)) {
  380. DRM_ERROR("Invalid first packet offset\n");
  381. return -EINVAL;
  382. }
  383. *cmd2 = (*cmd2 & 0xffc00000) | offset >> 10;
  384. }
  385. if ((*cmd & RADEON_GMC_SRC_PITCH_OFFSET_CNTL) &&
  386. (*cmd & RADEON_GMC_DST_PITCH_OFFSET_CNTL)) {
  387. u32 *cmd3 = drm_buffer_pointer_to_dword(cmdbuf->buffer, 3);
  388. offset = *cmd3 << 10;
  389. if (radeon_check_and_fixup_offset
  390. (dev_priv, file_priv, &offset)) {
  391. DRM_ERROR("Invalid second packet offset\n");
  392. return -EINVAL;
  393. }
  394. *cmd3 = (*cmd3 & 0xffc00000) | offset >> 10;
  395. }
  396. break;
  397. default:
  398. DRM_ERROR("Invalid packet type %x\n", *cmd & 0xff00);
  399. return -EINVAL;
  400. }
  401. return 0;
  402. }
  403. /* ================================================================
  404. * CP hardware state programming functions
  405. */
  406. static void radeon_emit_clip_rect(drm_radeon_private_t * dev_priv,
  407. struct drm_clip_rect * box)
  408. {
  409. RING_LOCALS;
  410. DRM_DEBUG(" box: x1=%d y1=%d x2=%d y2=%d\n",
  411. box->x1, box->y1, box->x2, box->y2);
  412. BEGIN_RING(4);
  413. OUT_RING(CP_PACKET0(RADEON_RE_TOP_LEFT, 0));
  414. OUT_RING((box->y1 << 16) | box->x1);
  415. OUT_RING(CP_PACKET0(RADEON_RE_WIDTH_HEIGHT, 0));
  416. OUT_RING(((box->y2 - 1) << 16) | (box->x2 - 1));
  417. ADVANCE_RING();
  418. }
  419. /* Emit 1.1 state
  420. */
  421. static int radeon_emit_state(drm_radeon_private_t * dev_priv,
  422. struct drm_file *file_priv,
  423. drm_radeon_context_regs_t * ctx,
  424. drm_radeon_texture_regs_t * tex,
  425. unsigned int dirty)
  426. {
  427. RING_LOCALS;
  428. DRM_DEBUG("dirty=0x%08x\n", dirty);
  429. if (dirty & RADEON_UPLOAD_CONTEXT) {
  430. if (radeon_check_and_fixup_offset(dev_priv, file_priv,
  431. &ctx->rb3d_depthoffset)) {
  432. DRM_ERROR("Invalid depth buffer offset\n");
  433. return -EINVAL;
  434. }
  435. if (radeon_check_and_fixup_offset(dev_priv, file_priv,
  436. &ctx->rb3d_coloroffset)) {
  437. DRM_ERROR("Invalid depth buffer offset\n");
  438. return -EINVAL;
  439. }
  440. BEGIN_RING(14);
  441. OUT_RING(CP_PACKET0(RADEON_PP_MISC, 6));
  442. OUT_RING(ctx->pp_misc);
  443. OUT_RING(ctx->pp_fog_color);
  444. OUT_RING(ctx->re_solid_color);
  445. OUT_RING(ctx->rb3d_blendcntl);
  446. OUT_RING(ctx->rb3d_depthoffset);
  447. OUT_RING(ctx->rb3d_depthpitch);
  448. OUT_RING(ctx->rb3d_zstencilcntl);
  449. OUT_RING(CP_PACKET0(RADEON_PP_CNTL, 2));
  450. OUT_RING(ctx->pp_cntl);
  451. OUT_RING(ctx->rb3d_cntl);
  452. OUT_RING(ctx->rb3d_coloroffset);
  453. OUT_RING(CP_PACKET0(RADEON_RB3D_COLORPITCH, 0));
  454. OUT_RING(ctx->rb3d_colorpitch);
  455. ADVANCE_RING();
  456. }
  457. if (dirty & RADEON_UPLOAD_VERTFMT) {
  458. BEGIN_RING(2);
  459. OUT_RING(CP_PACKET0(RADEON_SE_COORD_FMT, 0));
  460. OUT_RING(ctx->se_coord_fmt);
  461. ADVANCE_RING();
  462. }
  463. if (dirty & RADEON_UPLOAD_LINE) {
  464. BEGIN_RING(5);
  465. OUT_RING(CP_PACKET0(RADEON_RE_LINE_PATTERN, 1));
  466. OUT_RING(ctx->re_line_pattern);
  467. OUT_RING(ctx->re_line_state);
  468. OUT_RING(CP_PACKET0(RADEON_SE_LINE_WIDTH, 0));
  469. OUT_RING(ctx->se_line_width);
  470. ADVANCE_RING();
  471. }
  472. if (dirty & RADEON_UPLOAD_BUMPMAP) {
  473. BEGIN_RING(5);
  474. OUT_RING(CP_PACKET0(RADEON_PP_LUM_MATRIX, 0));
  475. OUT_RING(ctx->pp_lum_matrix);
  476. OUT_RING(CP_PACKET0(RADEON_PP_ROT_MATRIX_0, 1));
  477. OUT_RING(ctx->pp_rot_matrix_0);
  478. OUT_RING(ctx->pp_rot_matrix_1);
  479. ADVANCE_RING();
  480. }
  481. if (dirty & RADEON_UPLOAD_MASKS) {
  482. BEGIN_RING(4);
  483. OUT_RING(CP_PACKET0(RADEON_RB3D_STENCILREFMASK, 2));
  484. OUT_RING(ctx->rb3d_stencilrefmask);
  485. OUT_RING(ctx->rb3d_ropcntl);
  486. OUT_RING(ctx->rb3d_planemask);
  487. ADVANCE_RING();
  488. }
  489. if (dirty & RADEON_UPLOAD_VIEWPORT) {
  490. BEGIN_RING(7);
  491. OUT_RING(CP_PACKET0(RADEON_SE_VPORT_XSCALE, 5));
  492. OUT_RING(ctx->se_vport_xscale);
  493. OUT_RING(ctx->se_vport_xoffset);
  494. OUT_RING(ctx->se_vport_yscale);
  495. OUT_RING(ctx->se_vport_yoffset);
  496. OUT_RING(ctx->se_vport_zscale);
  497. OUT_RING(ctx->se_vport_zoffset);
  498. ADVANCE_RING();
  499. }
  500. if (dirty & RADEON_UPLOAD_SETUP) {
  501. BEGIN_RING(4);
  502. OUT_RING(CP_PACKET0(RADEON_SE_CNTL, 0));
  503. OUT_RING(ctx->se_cntl);
  504. OUT_RING(CP_PACKET0(RADEON_SE_CNTL_STATUS, 0));
  505. OUT_RING(ctx->se_cntl_status);
  506. ADVANCE_RING();
  507. }
  508. if (dirty & RADEON_UPLOAD_MISC) {
  509. BEGIN_RING(2);
  510. OUT_RING(CP_PACKET0(RADEON_RE_MISC, 0));
  511. OUT_RING(ctx->re_misc);
  512. ADVANCE_RING();
  513. }
  514. if (dirty & RADEON_UPLOAD_TEX0) {
  515. if (radeon_check_and_fixup_offset(dev_priv, file_priv,
  516. &tex[0].pp_txoffset)) {
  517. DRM_ERROR("Invalid texture offset for unit 0\n");
  518. return -EINVAL;
  519. }
  520. BEGIN_RING(9);
  521. OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_0, 5));
  522. OUT_RING(tex[0].pp_txfilter);
  523. OUT_RING(tex[0].pp_txformat);
  524. OUT_RING(tex[0].pp_txoffset);
  525. OUT_RING(tex[0].pp_txcblend);
  526. OUT_RING(tex[0].pp_txablend);
  527. OUT_RING(tex[0].pp_tfactor);
  528. OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_0, 0));
  529. OUT_RING(tex[0].pp_border_color);
  530. ADVANCE_RING();
  531. }
  532. if (dirty & RADEON_UPLOAD_TEX1) {
  533. if (radeon_check_and_fixup_offset(dev_priv, file_priv,
  534. &tex[1].pp_txoffset)) {
  535. DRM_ERROR("Invalid texture offset for unit 1\n");
  536. return -EINVAL;
  537. }
  538. BEGIN_RING(9);
  539. OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_1, 5));
  540. OUT_RING(tex[1].pp_txfilter);
  541. OUT_RING(tex[1].pp_txformat);
  542. OUT_RING(tex[1].pp_txoffset);
  543. OUT_RING(tex[1].pp_txcblend);
  544. OUT_RING(tex[1].pp_txablend);
  545. OUT_RING(tex[1].pp_tfactor);
  546. OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_1, 0));
  547. OUT_RING(tex[1].pp_border_color);
  548. ADVANCE_RING();
  549. }
  550. if (dirty & RADEON_UPLOAD_TEX2) {
  551. if (radeon_check_and_fixup_offset(dev_priv, file_priv,
  552. &tex[2].pp_txoffset)) {
  553. DRM_ERROR("Invalid texture offset for unit 2\n");
  554. return -EINVAL;
  555. }
  556. BEGIN_RING(9);
  557. OUT_RING(CP_PACKET0(RADEON_PP_TXFILTER_2, 5));
  558. OUT_RING(tex[2].pp_txfilter);
  559. OUT_RING(tex[2].pp_txformat);
  560. OUT_RING(tex[2].pp_txoffset);
  561. OUT_RING(tex[2].pp_txcblend);
  562. OUT_RING(tex[2].pp_txablend);
  563. OUT_RING(tex[2].pp_tfactor);
  564. OUT_RING(CP_PACKET0(RADEON_PP_BORDER_COLOR_2, 0));
  565. OUT_RING(tex[2].pp_border_color);
  566. ADVANCE_RING();
  567. }
  568. return 0;
  569. }
  570. /* Emit 1.2 state
  571. */
  572. static int radeon_emit_state2(drm_radeon_private_t * dev_priv,
  573. struct drm_file *file_priv,
  574. drm_radeon_state_t * state)
  575. {
  576. RING_LOCALS;
  577. if (state->dirty & RADEON_UPLOAD_ZBIAS) {
  578. BEGIN_RING(3);
  579. OUT_RING(CP_PACKET0(RADEON_SE_ZBIAS_FACTOR, 1));
  580. OUT_RING(state->context2.se_zbias_factor);
  581. OUT_RING(state->context2.se_zbias_constant);
  582. ADVANCE_RING();
  583. }
  584. return radeon_emit_state(dev_priv, file_priv, &state->context,
  585. state->tex, state->dirty);
  586. }
  587. /* New (1.3) state mechanism. 3 commands (packet, scalar, vector) in
  588. * 1.3 cmdbuffers allow all previous state to be updated as well as
  589. * the tcl scalar and vector areas.
  590. */
  591. static struct {
  592. int start;
  593. int len;
  594. const char *name;
  595. } packet[RADEON_MAX_STATE_PACKETS] = {
  596. {RADEON_PP_MISC, 7, "RADEON_PP_MISC"},
  597. {RADEON_PP_CNTL, 3, "RADEON_PP_CNTL"},
  598. {RADEON_RB3D_COLORPITCH, 1, "RADEON_RB3D_COLORPITCH"},
  599. {RADEON_RE_LINE_PATTERN, 2, "RADEON_RE_LINE_PATTERN"},
  600. {RADEON_SE_LINE_WIDTH, 1, "RADEON_SE_LINE_WIDTH"},
  601. {RADEON_PP_LUM_MATRIX, 1, "RADEON_PP_LUM_MATRIX"},
  602. {RADEON_PP_ROT_MATRIX_0, 2, "RADEON_PP_ROT_MATRIX_0"},
  603. {RADEON_RB3D_STENCILREFMASK, 3, "RADEON_RB3D_STENCILREFMASK"},
  604. {RADEON_SE_VPORT_XSCALE, 6, "RADEON_SE_VPORT_XSCALE"},
  605. {RADEON_SE_CNTL, 2, "RADEON_SE_CNTL"},
  606. {RADEON_SE_CNTL_STATUS, 1, "RADEON_SE_CNTL_STATUS"},
  607. {RADEON_RE_MISC, 1, "RADEON_RE_MISC"},
  608. {RADEON_PP_TXFILTER_0, 6, "RADEON_PP_TXFILTER_0"},
  609. {RADEON_PP_BORDER_COLOR_0, 1, "RADEON_PP_BORDER_COLOR_0"},
  610. {RADEON_PP_TXFILTER_1, 6, "RADEON_PP_TXFILTER_1"},
  611. {RADEON_PP_BORDER_COLOR_1, 1, "RADEON_PP_BORDER_COLOR_1"},
  612. {RADEON_PP_TXFILTER_2, 6, "RADEON_PP_TXFILTER_2"},
  613. {RADEON_PP_BORDER_COLOR_2, 1, "RADEON_PP_BORDER_COLOR_2"},
  614. {RADEON_SE_ZBIAS_FACTOR, 2, "RADEON_SE_ZBIAS_FACTOR"},
  615. {RADEON_SE_TCL_OUTPUT_VTX_FMT, 11, "RADEON_SE_TCL_OUTPUT_VTX_FMT"},
  616. {RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED, 17,
  617. "RADEON_SE_TCL_MATERIAL_EMMISSIVE_RED"},
  618. {R200_PP_TXCBLEND_0, 4, "R200_PP_TXCBLEND_0"},
  619. {R200_PP_TXCBLEND_1, 4, "R200_PP_TXCBLEND_1"},
  620. {R200_PP_TXCBLEND_2, 4, "R200_PP_TXCBLEND_2"},
  621. {R200_PP_TXCBLEND_3, 4, "R200_PP_TXCBLEND_3"},
  622. {R200_PP_TXCBLEND_4, 4, "R200_PP_TXCBLEND_4"},
  623. {R200_PP_TXCBLEND_5, 4, "R200_PP_TXCBLEND_5"},
  624. {R200_PP_TXCBLEND_6, 4, "R200_PP_TXCBLEND_6"},
  625. {R200_PP_TXCBLEND_7, 4, "R200_PP_TXCBLEND_7"},
  626. {R200_SE_TCL_LIGHT_MODEL_CTL_0, 6, "R200_SE_TCL_LIGHT_MODEL_CTL_0"},
  627. {R200_PP_TFACTOR_0, 6, "R200_PP_TFACTOR_0"},
  628. {R200_SE_VTX_FMT_0, 4, "R200_SE_VTX_FMT_0"},
  629. {R200_SE_VAP_CNTL, 1, "R200_SE_VAP_CNTL"},
  630. {R200_SE_TCL_MATRIX_SEL_0, 5, "R200_SE_TCL_MATRIX_SEL_0"},
  631. {R200_SE_TCL_TEX_PROC_CTL_2, 5, "R200_SE_TCL_TEX_PROC_CTL_2"},
  632. {R200_SE_TCL_UCP_VERT_BLEND_CTL, 1, "R200_SE_TCL_UCP_VERT_BLEND_CTL"},
  633. {R200_PP_TXFILTER_0, 6, "R200_PP_TXFILTER_0"},
  634. {R200_PP_TXFILTER_1, 6, "R200_PP_TXFILTER_1"},
  635. {R200_PP_TXFILTER_2, 6, "R200_PP_TXFILTER_2"},
  636. {R200_PP_TXFILTER_3, 6, "R200_PP_TXFILTER_3"},
  637. {R200_PP_TXFILTER_4, 6, "R200_PP_TXFILTER_4"},
  638. {R200_PP_TXFILTER_5, 6, "R200_PP_TXFILTER_5"},
  639. {R200_PP_TXOFFSET_0, 1, "R200_PP_TXOFFSET_0"},
  640. {R200_PP_TXOFFSET_1, 1, "R200_PP_TXOFFSET_1"},
  641. {R200_PP_TXOFFSET_2, 1, "R200_PP_TXOFFSET_2"},
  642. {R200_PP_TXOFFSET_3, 1, "R200_PP_TXOFFSET_3"},
  643. {R200_PP_TXOFFSET_4, 1, "R200_PP_TXOFFSET_4"},
  644. {R200_PP_TXOFFSET_5, 1, "R200_PP_TXOFFSET_5"},
  645. {R200_SE_VTE_CNTL, 1, "R200_SE_VTE_CNTL"},
  646. {R200_SE_TCL_OUTPUT_VTX_COMP_SEL, 1,
  647. "R200_SE_TCL_OUTPUT_VTX_COMP_SEL"},
  648. {R200_PP_TAM_DEBUG3, 1, "R200_PP_TAM_DEBUG3"},
  649. {R200_PP_CNTL_X, 1, "R200_PP_CNTL_X"},
  650. {R200_RB3D_DEPTHXY_OFFSET, 1, "R200_RB3D_DEPTHXY_OFFSET"},
  651. {R200_RE_AUX_SCISSOR_CNTL, 1, "R200_RE_AUX_SCISSOR_CNTL"},
  652. {R200_RE_SCISSOR_TL_0, 2, "R200_RE_SCISSOR_TL_0"},
  653. {R200_RE_SCISSOR_TL_1, 2, "R200_RE_SCISSOR_TL_1"},
  654. {R200_RE_SCISSOR_TL_2, 2, "R200_RE_SCISSOR_TL_2"},
  655. {R200_SE_VAP_CNTL_STATUS, 1, "R200_SE_VAP_CNTL_STATUS"},
  656. {R200_SE_VTX_STATE_CNTL, 1, "R200_SE_VTX_STATE_CNTL"},
  657. {R200_RE_POINTSIZE, 1, "R200_RE_POINTSIZE"},
  658. {R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0, 4,
  659. "R200_SE_TCL_INPUT_VTX_VECTOR_ADDR_0"},
  660. {R200_PP_CUBIC_FACES_0, 1, "R200_PP_CUBIC_FACES_0"}, /* 61 */
  661. {R200_PP_CUBIC_OFFSET_F1_0, 5, "R200_PP_CUBIC_OFFSET_F1_0"}, /* 62 */
  662. {R200_PP_CUBIC_FACES_1, 1, "R200_PP_CUBIC_FACES_1"},
  663. {R200_PP_CUBIC_OFFSET_F1_1, 5, "R200_PP_CUBIC_OFFSET_F1_1"},
  664. {R200_PP_CUBIC_FACES_2, 1, "R200_PP_CUBIC_FACES_2"},
  665. {R200_PP_CUBIC_OFFSET_F1_2, 5, "R200_PP_CUBIC_OFFSET_F1_2"},
  666. {R200_PP_CUBIC_FACES_3, 1, "R200_PP_CUBIC_FACES_3"},
  667. {R200_PP_CUBIC_OFFSET_F1_3, 5, "R200_PP_CUBIC_OFFSET_F1_3"},
  668. {R200_PP_CUBIC_FACES_4, 1, "R200_PP_CUBIC_FACES_4"},
  669. {R200_PP_CUBIC_OFFSET_F1_4, 5, "R200_PP_CUBIC_OFFSET_F1_4"},
  670. {R200_PP_CUBIC_FACES_5, 1, "R200_PP_CUBIC_FACES_5"},
  671. {R200_PP_CUBIC_OFFSET_F1_5, 5, "R200_PP_CUBIC_OFFSET_F1_5"},
  672. {RADEON_PP_TEX_SIZE_0, 2, "RADEON_PP_TEX_SIZE_0"},
  673. {RADEON_PP_TEX_SIZE_1, 2, "RADEON_PP_TEX_SIZE_1"},
  674. {RADEON_PP_TEX_SIZE_2, 2, "RADEON_PP_TEX_SIZE_2"},
  675. {R200_RB3D_BLENDCOLOR, 3, "R200_RB3D_BLENDCOLOR"},
  676. {R200_SE_TCL_POINT_SPRITE_CNTL, 1, "R200_SE_TCL_POINT_SPRITE_CNTL"},
  677. {RADEON_PP_CUBIC_FACES_0, 1, "RADEON_PP_CUBIC_FACES_0"},
  678. {RADEON_PP_CUBIC_OFFSET_T0_0, 5, "RADEON_PP_CUBIC_OFFSET_T0_0"},
  679. {RADEON_PP_CUBIC_FACES_1, 1, "RADEON_PP_CUBIC_FACES_1"},
  680. {RADEON_PP_CUBIC_OFFSET_T1_0, 5, "RADEON_PP_CUBIC_OFFSET_T1_0"},
  681. {RADEON_PP_CUBIC_FACES_2, 1, "RADEON_PP_CUBIC_FACES_2"},
  682. {RADEON_PP_CUBIC_OFFSET_T2_0, 5, "RADEON_PP_CUBIC_OFFSET_T2_0"},
  683. {R200_PP_TRI_PERF, 2, "R200_PP_TRI_PERF"},
  684. {R200_PP_AFS_0, 32, "R200_PP_AFS_0"}, /* 85 */
  685. {R200_PP_AFS_1, 32, "R200_PP_AFS_1"},
  686. {R200_PP_TFACTOR_0, 8, "R200_ATF_TFACTOR"},
  687. {R200_PP_TXFILTER_0, 8, "R200_PP_TXCTLALL_0"},
  688. {R200_PP_TXFILTER_1, 8, "R200_PP_TXCTLALL_1"},
  689. {R200_PP_TXFILTER_2, 8, "R200_PP_TXCTLALL_2"},
  690. {R200_PP_TXFILTER_3, 8, "R200_PP_TXCTLALL_3"},
  691. {R200_PP_TXFILTER_4, 8, "R200_PP_TXCTLALL_4"},
  692. {R200_PP_TXFILTER_5, 8, "R200_PP_TXCTLALL_5"},
  693. {R200_VAP_PVS_CNTL_1, 2, "R200_VAP_PVS_CNTL"},
  694. };
  695. /* ================================================================
  696. * Performance monitoring functions
  697. */
  698. static void radeon_clear_box(drm_radeon_private_t * dev_priv,
  699. struct drm_radeon_master_private *master_priv,
  700. int x, int y, int w, int h, int r, int g, int b)
  701. {
  702. u32 color;
  703. RING_LOCALS;
  704. x += master_priv->sarea_priv->boxes[0].x1;
  705. y += master_priv->sarea_priv->boxes[0].y1;
  706. switch (dev_priv->color_fmt) {
  707. case RADEON_COLOR_FORMAT_RGB565:
  708. color = (((r & 0xf8) << 8) |
  709. ((g & 0xfc) << 3) | ((b & 0xf8) >> 3));
  710. break;
  711. case RADEON_COLOR_FORMAT_ARGB8888:
  712. default:
  713. color = (((0xff) << 24) | (r << 16) | (g << 8) | b);
  714. break;
  715. }
  716. BEGIN_RING(4);
  717. RADEON_WAIT_UNTIL_3D_IDLE();
  718. OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
  719. OUT_RING(0xffffffff);
  720. ADVANCE_RING();
  721. BEGIN_RING(6);
  722. OUT_RING(CP_PACKET3(RADEON_CNTL_PAINT_MULTI, 4));
  723. OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  724. RADEON_GMC_BRUSH_SOLID_COLOR |
  725. (dev_priv->color_fmt << 8) |
  726. RADEON_GMC_SRC_DATATYPE_COLOR |
  727. RADEON_ROP3_P | RADEON_GMC_CLR_CMP_CNTL_DIS);
  728. if (master_priv->sarea_priv->pfCurrentPage == 1) {
  729. OUT_RING(dev_priv->front_pitch_offset);
  730. } else {
  731. OUT_RING(dev_priv->back_pitch_offset);
  732. }
  733. OUT_RING(color);
  734. OUT_RING((x << 16) | y);
  735. OUT_RING((w << 16) | h);
  736. ADVANCE_RING();
  737. }
  738. static void radeon_cp_performance_boxes(drm_radeon_private_t *dev_priv, struct drm_radeon_master_private *master_priv)
  739. {
  740. /* Collapse various things into a wait flag -- trying to
  741. * guess if userspase slept -- better just to have them tell us.
  742. */
  743. if (dev_priv->stats.last_frame_reads > 1 ||
  744. dev_priv->stats.last_clear_reads > dev_priv->stats.clears) {
  745. dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
  746. }
  747. if (dev_priv->stats.freelist_loops) {
  748. dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
  749. }
  750. /* Purple box for page flipping
  751. */
  752. if (dev_priv->stats.boxes & RADEON_BOX_FLIP)
  753. radeon_clear_box(dev_priv, master_priv, 4, 4, 8, 8, 255, 0, 255);
  754. /* Red box if we have to wait for idle at any point
  755. */
  756. if (dev_priv->stats.boxes & RADEON_BOX_WAIT_IDLE)
  757. radeon_clear_box(dev_priv, master_priv, 16, 4, 8, 8, 255, 0, 0);
  758. /* Blue box: lost context?
  759. */
  760. /* Yellow box for texture swaps
  761. */
  762. if (dev_priv->stats.boxes & RADEON_BOX_TEXTURE_LOAD)
  763. radeon_clear_box(dev_priv, master_priv, 40, 4, 8, 8, 255, 255, 0);
  764. /* Green box if hardware never idles (as far as we can tell)
  765. */
  766. if (!(dev_priv->stats.boxes & RADEON_BOX_DMA_IDLE))
  767. radeon_clear_box(dev_priv, master_priv, 64, 4, 8, 8, 0, 255, 0);
  768. /* Draw bars indicating number of buffers allocated
  769. * (not a great measure, easily confused)
  770. */
  771. if (dev_priv->stats.requested_bufs) {
  772. if (dev_priv->stats.requested_bufs > 100)
  773. dev_priv->stats.requested_bufs = 100;
  774. radeon_clear_box(dev_priv, master_priv, 4, 16,
  775. dev_priv->stats.requested_bufs, 4,
  776. 196, 128, 128);
  777. }
  778. memset(&dev_priv->stats, 0, sizeof(dev_priv->stats));
  779. }
  780. /* ================================================================
  781. * CP command dispatch functions
  782. */
  783. static void radeon_cp_dispatch_clear(struct drm_device * dev,
  784. struct drm_master *master,
  785. drm_radeon_clear_t * clear,
  786. drm_radeon_clear_rect_t * depth_boxes)
  787. {
  788. drm_radeon_private_t *dev_priv = dev->dev_private;
  789. struct drm_radeon_master_private *master_priv = master->driver_priv;
  790. drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
  791. drm_radeon_depth_clear_t *depth_clear = &dev_priv->depth_clear;
  792. int nbox = sarea_priv->nbox;
  793. struct drm_clip_rect *pbox = sarea_priv->boxes;
  794. unsigned int flags = clear->flags;
  795. u32 rb3d_cntl = 0, rb3d_stencilrefmask = 0;
  796. int i;
  797. RING_LOCALS;
  798. DRM_DEBUG("flags = 0x%x\n", flags);
  799. dev_priv->stats.clears++;
  800. if (sarea_priv->pfCurrentPage == 1) {
  801. unsigned int tmp = flags;
  802. flags &= ~(RADEON_FRONT | RADEON_BACK);
  803. if (tmp & RADEON_FRONT)
  804. flags |= RADEON_BACK;
  805. if (tmp & RADEON_BACK)
  806. flags |= RADEON_FRONT;
  807. }
  808. if (flags & (RADEON_DEPTH|RADEON_STENCIL)) {
  809. if (!dev_priv->have_z_offset) {
  810. printk_once(KERN_ERR "radeon: illegal depth clear request. Buggy mesa detected - please update.\n");
  811. flags &= ~(RADEON_DEPTH | RADEON_STENCIL);
  812. }
  813. }
  814. if (flags & (RADEON_FRONT | RADEON_BACK)) {
  815. BEGIN_RING(4);
  816. /* Ensure the 3D stream is idle before doing a
  817. * 2D fill to clear the front or back buffer.
  818. */
  819. RADEON_WAIT_UNTIL_3D_IDLE();
  820. OUT_RING(CP_PACKET0(RADEON_DP_WRITE_MASK, 0));
  821. OUT_RING(clear->color_mask);
  822. ADVANCE_RING();
  823. /* Make sure we restore the 3D state next time.
  824. */
  825. sarea_priv->ctx_owner = 0;
  826. for (i = 0; i < nbox; i++) {
  827. int x = pbox[i].x1;
  828. int y = pbox[i].y1;
  829. int w = pbox[i].x2 - x;
  830. int h = pbox[i].y2 - y;
  831. DRM_DEBUG("%d,%d-%d,%d flags 0x%x\n",
  832. x, y, w, h, flags);
  833. if (flags & RADEON_FRONT) {
  834. BEGIN_RING(6);
  835. OUT_RING(CP_PACKET3
  836. (RADEON_CNTL_PAINT_MULTI, 4));
  837. OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  838. RADEON_GMC_BRUSH_SOLID_COLOR |
  839. (dev_priv->
  840. color_fmt << 8) |
  841. RADEON_GMC_SRC_DATATYPE_COLOR |
  842. RADEON_ROP3_P |
  843. RADEON_GMC_CLR_CMP_CNTL_DIS);
  844. OUT_RING(dev_priv->front_pitch_offset);
  845. OUT_RING(clear->clear_color);
  846. OUT_RING((x << 16) | y);
  847. OUT_RING((w << 16) | h);
  848. ADVANCE_RING();
  849. }
  850. if (flags & RADEON_BACK) {
  851. BEGIN_RING(6);
  852. OUT_RING(CP_PACKET3
  853. (RADEON_CNTL_PAINT_MULTI, 4));
  854. OUT_RING(RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  855. RADEON_GMC_BRUSH_SOLID_COLOR |
  856. (dev_priv->
  857. color_fmt << 8) |
  858. RADEON_GMC_SRC_DATATYPE_COLOR |
  859. RADEON_ROP3_P |
  860. RADEON_GMC_CLR_CMP_CNTL_DIS);
  861. OUT_RING(dev_priv->back_pitch_offset);
  862. OUT_RING(clear->clear_color);
  863. OUT_RING((x << 16) | y);
  864. OUT_RING((w << 16) | h);
  865. ADVANCE_RING();
  866. }
  867. }
  868. }
  869. /* hyper z clear */
  870. /* no docs available, based on reverse engineering by Stephane Marchesin */
  871. if ((flags & (RADEON_DEPTH | RADEON_STENCIL))
  872. && (flags & RADEON_CLEAR_FASTZ)) {
  873. int i;
  874. int depthpixperline =
  875. dev_priv->depth_fmt ==
  876. RADEON_DEPTH_FORMAT_16BIT_INT_Z ? (dev_priv->depth_pitch /
  877. 2) : (dev_priv->
  878. depth_pitch / 4);
  879. u32 clearmask;
  880. u32 tempRB3D_DEPTHCLEARVALUE = clear->clear_depth |
  881. ((clear->depth_mask & 0xff) << 24);
  882. /* Make sure we restore the 3D state next time.
  883. * we haven't touched any "normal" state - still need this?
  884. */
  885. sarea_priv->ctx_owner = 0;
  886. if ((dev_priv->flags & RADEON_HAS_HIERZ)
  887. && (flags & RADEON_USE_HIERZ)) {
  888. /* FIXME : reverse engineer that for Rx00 cards */
  889. /* FIXME : the mask supposedly contains low-res z values. So can't set
  890. just to the max (0xff? or actually 0x3fff?), need to take z clear
  891. value into account? */
  892. /* pattern seems to work for r100, though get slight
  893. rendering errors with glxgears. If hierz is not enabled for r100,
  894. only 4 bits which indicate clear (15,16,31,32, all zero) matter, the
  895. other ones are ignored, and the same clear mask can be used. That's
  896. very different behaviour than R200 which needs different clear mask
  897. and different number of tiles to clear if hierz is enabled or not !?!
  898. */
  899. clearmask = (0xff << 22) | (0xff << 6) | 0x003f003f;
  900. } else {
  901. /* clear mask : chooses the clearing pattern.
  902. rv250: could be used to clear only parts of macrotiles
  903. (but that would get really complicated...)?
  904. bit 0 and 1 (either or both of them ?!?!) are used to
  905. not clear tile (or maybe one of the bits indicates if the tile is
  906. compressed or not), bit 2 and 3 to not clear tile 1,...,.
  907. Pattern is as follows:
  908. | 0,1 | 4,5 | 8,9 |12,13|16,17|20,21|24,25|28,29|
  909. bits -------------------------------------------------
  910. | 2,3 | 6,7 |10,11|14,15|18,19|22,23|26,27|30,31|
  911. rv100: clearmask covers 2x8 4x1 tiles, but one clear still
  912. covers 256 pixels ?!?
  913. */
  914. clearmask = 0x0;
  915. }
  916. BEGIN_RING(8);
  917. RADEON_WAIT_UNTIL_2D_IDLE();
  918. OUT_RING_REG(RADEON_RB3D_DEPTHCLEARVALUE,
  919. tempRB3D_DEPTHCLEARVALUE);
  920. /* what offset is this exactly ? */
  921. OUT_RING_REG(RADEON_RB3D_ZMASKOFFSET, 0);
  922. /* need ctlstat, otherwise get some strange black flickering */
  923. OUT_RING_REG(RADEON_RB3D_ZCACHE_CTLSTAT,
  924. RADEON_RB3D_ZC_FLUSH_ALL);
  925. ADVANCE_RING();
  926. for (i = 0; i < nbox; i++) {
  927. int tileoffset, nrtilesx, nrtilesy, j;
  928. /* it looks like r200 needs rv-style clears, at least if hierz is not enabled? */
  929. if ((dev_priv->flags & RADEON_HAS_HIERZ)
  930. && !(dev_priv->microcode_version == UCODE_R200)) {
  931. /* FIXME : figure this out for r200 (when hierz is enabled). Or
  932. maybe r200 actually doesn't need to put the low-res z value into
  933. the tile cache like r100, but just needs to clear the hi-level z-buffer?
  934. Works for R100, both with hierz and without.
  935. R100 seems to operate on 2x1 8x8 tiles, but...
  936. odd: offset/nrtiles need to be 64 pix (4 block) aligned? Potentially
  937. problematic with resolutions which are not 64 pix aligned? */
  938. tileoffset =
  939. ((pbox[i].y1 >> 3) * depthpixperline +
  940. pbox[i].x1) >> 6;
  941. nrtilesx =
  942. ((pbox[i].x2 & ~63) -
  943. (pbox[i].x1 & ~63)) >> 4;
  944. nrtilesy =
  945. (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
  946. for (j = 0; j <= nrtilesy; j++) {
  947. BEGIN_RING(4);
  948. OUT_RING(CP_PACKET3
  949. (RADEON_3D_CLEAR_ZMASK, 2));
  950. /* first tile */
  951. OUT_RING(tileoffset * 8);
  952. /* the number of tiles to clear */
  953. OUT_RING(nrtilesx + 4);
  954. /* clear mask : chooses the clearing pattern. */
  955. OUT_RING(clearmask);
  956. ADVANCE_RING();
  957. tileoffset += depthpixperline >> 6;
  958. }
  959. } else if (dev_priv->microcode_version == UCODE_R200) {
  960. /* works for rv250. */
  961. /* find first macro tile (8x2 4x4 z-pixels on rv250) */
  962. tileoffset =
  963. ((pbox[i].y1 >> 3) * depthpixperline +
  964. pbox[i].x1) >> 5;
  965. nrtilesx =
  966. (pbox[i].x2 >> 5) - (pbox[i].x1 >> 5);
  967. nrtilesy =
  968. (pbox[i].y2 >> 3) - (pbox[i].y1 >> 3);
  969. for (j = 0; j <= nrtilesy; j++) {
  970. BEGIN_RING(4);
  971. OUT_RING(CP_PACKET3
  972. (RADEON_3D_CLEAR_ZMASK, 2));
  973. /* first tile */
  974. /* judging by the first tile offset needed, could possibly
  975. directly address/clear 4x4 tiles instead of 8x2 * 4x4
  976. macro tiles, though would still need clear mask for
  977. right/bottom if truly 4x4 granularity is desired ? */
  978. OUT_RING(tileoffset * 16);
  979. /* the number of tiles to clear */
  980. OUT_RING(nrtilesx + 1);
  981. /* clear mask : chooses the clearing pattern. */
  982. OUT_RING(clearmask);
  983. ADVANCE_RING();
  984. tileoffset += depthpixperline >> 5;
  985. }
  986. } else { /* rv 100 */
  987. /* rv100 might not need 64 pix alignment, who knows */
  988. /* offsets are, hmm, weird */
  989. tileoffset =
  990. ((pbox[i].y1 >> 4) * depthpixperline +
  991. pbox[i].x1) >> 6;
  992. nrtilesx =
  993. ((pbox[i].x2 & ~63) -
  994. (pbox[i].x1 & ~63)) >> 4;
  995. nrtilesy =
  996. (pbox[i].y2 >> 4) - (pbox[i].y1 >> 4);
  997. for (j = 0; j <= nrtilesy; j++) {
  998. BEGIN_RING(4);
  999. OUT_RING(CP_PACKET3
  1000. (RADEON_3D_CLEAR_ZMASK, 2));
  1001. OUT_RING(tileoffset * 128);
  1002. /* the number of tiles to clear */
  1003. OUT_RING(nrtilesx + 4);
  1004. /* clear mask : chooses the clearing pattern. */
  1005. OUT_RING(clearmask);
  1006. ADVANCE_RING();
  1007. tileoffset += depthpixperline >> 6;
  1008. }
  1009. }
  1010. }
  1011. /* TODO don't always clear all hi-level z tiles */
  1012. if ((dev_priv->flags & RADEON_HAS_HIERZ)
  1013. && (dev_priv->microcode_version == UCODE_R200)
  1014. && (flags & RADEON_USE_HIERZ))
  1015. /* r100 and cards without hierarchical z-buffer have no high-level z-buffer */
  1016. /* FIXME : the mask supposedly contains low-res z values. So can't set
  1017. just to the max (0xff? or actually 0x3fff?), need to take z clear
  1018. value into account? */
  1019. {
  1020. BEGIN_RING(4);
  1021. OUT_RING(CP_PACKET3(RADEON_3D_CLEAR_HIZ, 2));
  1022. OUT_RING(0x0); /* First tile */
  1023. OUT_RING(0x3cc0);
  1024. OUT_RING((0xff << 22) | (0xff << 6) | 0x003f003f);
  1025. ADVANCE_RING();
  1026. }
  1027. }
  1028. /* We have to clear the depth and/or stencil buffers by
  1029. * rendering a quad into just those buffers. Thus, we have to
  1030. * make sure the 3D engine is configured correctly.
  1031. */
  1032. else if ((dev_priv->microcode_version == UCODE_R200) &&
  1033. (flags & (RADEON_DEPTH | RADEON_STENCIL))) {
  1034. int tempPP_CNTL;
  1035. int tempRE_CNTL;
  1036. int tempRB3D_CNTL;
  1037. int tempRB3D_ZSTENCILCNTL;
  1038. int tempRB3D_STENCILREFMASK;
  1039. int tempRB3D_PLANEMASK;
  1040. int tempSE_CNTL;
  1041. int tempSE_VTE_CNTL;
  1042. int tempSE_VTX_FMT_0;
  1043. int tempSE_VTX_FMT_1;
  1044. int tempSE_VAP_CNTL;
  1045. int tempRE_AUX_SCISSOR_CNTL;
  1046. tempPP_CNTL = 0;
  1047. tempRE_CNTL = 0;
  1048. tempRB3D_CNTL = depth_clear->rb3d_cntl;
  1049. tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;
  1050. tempRB3D_STENCILREFMASK = 0x0;
  1051. tempSE_CNTL = depth_clear->se_cntl;
  1052. /* Disable TCL */
  1053. tempSE_VAP_CNTL = ( /* SE_VAP_CNTL__FORCE_W_TO_ONE_MASK | */
  1054. (0x9 <<
  1055. SE_VAP_CNTL__VF_MAX_VTX_NUM__SHIFT));
  1056. tempRB3D_PLANEMASK = 0x0;
  1057. tempRE_AUX_SCISSOR_CNTL = 0x0;
  1058. tempSE_VTE_CNTL =
  1059. SE_VTE_CNTL__VTX_XY_FMT_MASK | SE_VTE_CNTL__VTX_Z_FMT_MASK;
  1060. /* Vertex format (X, Y, Z, W) */
  1061. tempSE_VTX_FMT_0 =
  1062. SE_VTX_FMT_0__VTX_Z0_PRESENT_MASK |
  1063. SE_VTX_FMT_0__VTX_W0_PRESENT_MASK;
  1064. tempSE_VTX_FMT_1 = 0x0;
  1065. /*
  1066. * Depth buffer specific enables
  1067. */
  1068. if (flags & RADEON_DEPTH) {
  1069. /* Enable depth buffer */
  1070. tempRB3D_CNTL |= RADEON_Z_ENABLE;
  1071. } else {
  1072. /* Disable depth buffer */
  1073. tempRB3D_CNTL &= ~RADEON_Z_ENABLE;
  1074. }
  1075. /*
  1076. * Stencil buffer specific enables
  1077. */
  1078. if (flags & RADEON_STENCIL) {
  1079. tempRB3D_CNTL |= RADEON_STENCIL_ENABLE;
  1080. tempRB3D_STENCILREFMASK = clear->depth_mask;
  1081. } else {
  1082. tempRB3D_CNTL &= ~RADEON_STENCIL_ENABLE;
  1083. tempRB3D_STENCILREFMASK = 0x00000000;
  1084. }
  1085. if (flags & RADEON_USE_COMP_ZBUF) {
  1086. tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
  1087. RADEON_Z_DECOMPRESSION_ENABLE;
  1088. }
  1089. if (flags & RADEON_USE_HIERZ) {
  1090. tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
  1091. }
  1092. BEGIN_RING(26);
  1093. RADEON_WAIT_UNTIL_2D_IDLE();
  1094. OUT_RING_REG(RADEON_PP_CNTL, tempPP_CNTL);
  1095. OUT_RING_REG(R200_RE_CNTL, tempRE_CNTL);
  1096. OUT_RING_REG(RADEON_RB3D_CNTL, tempRB3D_CNTL);
  1097. OUT_RING_REG(RADEON_RB3D_ZSTENCILCNTL, tempRB3D_ZSTENCILCNTL);
  1098. OUT_RING_REG(RADEON_RB3D_STENCILREFMASK,
  1099. tempRB3D_STENCILREFMASK);
  1100. OUT_RING_REG(RADEON_RB3D_PLANEMASK, tempRB3D_PLANEMASK);
  1101. OUT_RING_REG(RADEON_SE_CNTL, tempSE_CNTL);
  1102. OUT_RING_REG(R200_SE_VTE_CNTL, tempSE_VTE_CNTL);
  1103. OUT_RING_REG(R200_SE_VTX_FMT_0, tempSE_VTX_FMT_0);
  1104. OUT_RING_REG(R200_SE_VTX_FMT_1, tempSE_VTX_FMT_1);
  1105. OUT_RING_REG(R200_SE_VAP_CNTL, tempSE_VAP_CNTL);
  1106. OUT_RING_REG(R200_RE_AUX_SCISSOR_CNTL, tempRE_AUX_SCISSOR_CNTL);
  1107. ADVANCE_RING();
  1108. /* Make sure we restore the 3D state next time.
  1109. */
  1110. sarea_priv->ctx_owner = 0;
  1111. for (i = 0; i < nbox; i++) {
  1112. /* Funny that this should be required --
  1113. * sets top-left?
  1114. */
  1115. radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
  1116. BEGIN_RING(14);
  1117. OUT_RING(CP_PACKET3(R200_3D_DRAW_IMMD_2, 12));
  1118. OUT_RING((RADEON_PRIM_TYPE_RECT_LIST |
  1119. RADEON_PRIM_WALK_RING |
  1120. (3 << RADEON_NUM_VERTICES_SHIFT)));
  1121. OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
  1122. OUT_RING(depth_boxes[i].ui[CLEAR_Y1]);
  1123. OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
  1124. OUT_RING(0x3f800000);
  1125. OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
  1126. OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
  1127. OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
  1128. OUT_RING(0x3f800000);
  1129. OUT_RING(depth_boxes[i].ui[CLEAR_X2]);
  1130. OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
  1131. OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
  1132. OUT_RING(0x3f800000);
  1133. ADVANCE_RING();
  1134. }
  1135. } else if ((flags & (RADEON_DEPTH | RADEON_STENCIL))) {
  1136. int tempRB3D_ZSTENCILCNTL = depth_clear->rb3d_zstencilcntl;
  1137. rb3d_cntl = depth_clear->rb3d_cntl;
  1138. if (flags & RADEON_DEPTH) {
  1139. rb3d_cntl |= RADEON_Z_ENABLE;
  1140. } else {
  1141. rb3d_cntl &= ~RADEON_Z_ENABLE;
  1142. }
  1143. if (flags & RADEON_STENCIL) {
  1144. rb3d_cntl |= RADEON_STENCIL_ENABLE;
  1145. rb3d_stencilrefmask = clear->depth_mask; /* misnamed field */
  1146. } else {
  1147. rb3d_cntl &= ~RADEON_STENCIL_ENABLE;
  1148. rb3d_stencilrefmask = 0x00000000;
  1149. }
  1150. if (flags & RADEON_USE_COMP_ZBUF) {
  1151. tempRB3D_ZSTENCILCNTL |= RADEON_Z_COMPRESSION_ENABLE |
  1152. RADEON_Z_DECOMPRESSION_ENABLE;
  1153. }
  1154. if (flags & RADEON_USE_HIERZ) {
  1155. tempRB3D_ZSTENCILCNTL |= RADEON_Z_HIERARCHY_ENABLE;
  1156. }
  1157. BEGIN_RING(13);
  1158. RADEON_WAIT_UNTIL_2D_IDLE();
  1159. OUT_RING(CP_PACKET0(RADEON_PP_CNTL, 1));
  1160. OUT_RING(0x00000000);
  1161. OUT_RING(rb3d_cntl);
  1162. OUT_RING_REG(RADEON_RB3D_ZSTENCILCNTL, tempRB3D_ZSTENCILCNTL);
  1163. OUT_RING_REG(RADEON_RB3D_STENCILREFMASK, rb3d_stencilrefmask);
  1164. OUT_RING_REG(RADEON_RB3D_PLANEMASK, 0x00000000);
  1165. OUT_RING_REG(RADEON_SE_CNTL, depth_clear->se_cntl);
  1166. ADVANCE_RING();
  1167. /* Make sure we restore the 3D state next time.
  1168. */
  1169. sarea_priv->ctx_owner = 0;
  1170. for (i = 0; i < nbox; i++) {
  1171. /* Funny that this should be required --
  1172. * sets top-left?
  1173. */
  1174. radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
  1175. BEGIN_RING(15);
  1176. OUT_RING(CP_PACKET3(RADEON_3D_DRAW_IMMD, 13));
  1177. OUT_RING(RADEON_VTX_Z_PRESENT |
  1178. RADEON_VTX_PKCOLOR_PRESENT);
  1179. OUT_RING((RADEON_PRIM_TYPE_RECT_LIST |
  1180. RADEON_PRIM_WALK_RING |
  1181. RADEON_MAOS_ENABLE |
  1182. RADEON_VTX_FMT_RADEON_MODE |
  1183. (3 << RADEON_NUM_VERTICES_SHIFT)));
  1184. OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
  1185. OUT_RING(depth_boxes[i].ui[CLEAR_Y1]);
  1186. OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
  1187. OUT_RING(0x0);
  1188. OUT_RING(depth_boxes[i].ui[CLEAR_X1]);
  1189. OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
  1190. OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
  1191. OUT_RING(0x0);
  1192. OUT_RING(depth_boxes[i].ui[CLEAR_X2]);
  1193. OUT_RING(depth_boxes[i].ui[CLEAR_Y2]);
  1194. OUT_RING(depth_boxes[i].ui[CLEAR_DEPTH]);
  1195. OUT_RING(0x0);
  1196. ADVANCE_RING();
  1197. }
  1198. }
  1199. /* Increment the clear counter. The client-side 3D driver must
  1200. * wait on this value before performing the clear ioctl. We
  1201. * need this because the card's so damned fast...
  1202. */
  1203. sarea_priv->last_clear++;
  1204. BEGIN_RING(4);
  1205. RADEON_CLEAR_AGE(sarea_priv->last_clear);
  1206. RADEON_WAIT_UNTIL_IDLE();
  1207. ADVANCE_RING();
  1208. }
  1209. static void radeon_cp_dispatch_swap(struct drm_device *dev, struct drm_master *master)
  1210. {
  1211. drm_radeon_private_t *dev_priv = dev->dev_private;
  1212. struct drm_radeon_master_private *master_priv = master->driver_priv;
  1213. drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
  1214. int nbox = sarea_priv->nbox;
  1215. struct drm_clip_rect *pbox = sarea_priv->boxes;
  1216. int i;
  1217. RING_LOCALS;
  1218. DRM_DEBUG("\n");
  1219. /* Do some trivial performance monitoring...
  1220. */
  1221. if (dev_priv->do_boxes)
  1222. radeon_cp_performance_boxes(dev_priv, master_priv);
  1223. /* Wait for the 3D stream to idle before dispatching the bitblt.
  1224. * This will prevent data corruption between the two streams.
  1225. */
  1226. BEGIN_RING(2);
  1227. RADEON_WAIT_UNTIL_3D_IDLE();
  1228. ADVANCE_RING();
  1229. for (i = 0; i < nbox; i++) {
  1230. int x = pbox[i].x1;
  1231. int y = pbox[i].y1;
  1232. int w = pbox[i].x2 - x;
  1233. int h = pbox[i].y2 - y;
  1234. DRM_DEBUG("%d,%d-%d,%d\n", x, y, w, h);
  1235. BEGIN_RING(9);
  1236. OUT_RING(CP_PACKET0(RADEON_DP_GUI_MASTER_CNTL, 0));
  1237. OUT_RING(RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
  1238. RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  1239. RADEON_GMC_BRUSH_NONE |
  1240. (dev_priv->color_fmt << 8) |
  1241. RADEON_GMC_SRC_DATATYPE_COLOR |
  1242. RADEON_ROP3_S |
  1243. RADEON_DP_SRC_SOURCE_MEMORY |
  1244. RADEON_GMC_CLR_CMP_CNTL_DIS | RADEON_GMC_WR_MSK_DIS);
  1245. /* Make this work even if front & back are flipped:
  1246. */
  1247. OUT_RING(CP_PACKET0(RADEON_SRC_PITCH_OFFSET, 1));
  1248. if (sarea_priv->pfCurrentPage == 0) {
  1249. OUT_RING(dev_priv->back_pitch_offset);
  1250. OUT_RING(dev_priv->front_pitch_offset);
  1251. } else {
  1252. OUT_RING(dev_priv->front_pitch_offset);
  1253. OUT_RING(dev_priv->back_pitch_offset);
  1254. }
  1255. OUT_RING(CP_PACKET0(RADEON_SRC_X_Y, 2));
  1256. OUT_RING((x << 16) | y);
  1257. OUT_RING((x << 16) | y);
  1258. OUT_RING((w << 16) | h);
  1259. ADVANCE_RING();
  1260. }
  1261. /* Increment the frame counter. The client-side 3D driver must
  1262. * throttle the framerate by waiting for this value before
  1263. * performing the swapbuffer ioctl.
  1264. */
  1265. sarea_priv->last_frame++;
  1266. BEGIN_RING(4);
  1267. RADEON_FRAME_AGE(sarea_priv->last_frame);
  1268. RADEON_WAIT_UNTIL_2D_IDLE();
  1269. ADVANCE_RING();
  1270. }
  1271. void radeon_cp_dispatch_flip(struct drm_device *dev, struct drm_master *master)
  1272. {
  1273. drm_radeon_private_t *dev_priv = dev->dev_private;
  1274. struct drm_radeon_master_private *master_priv = master->driver_priv;
  1275. struct drm_sarea *sarea = (struct drm_sarea *)master_priv->sarea->handle;
  1276. int offset = (master_priv->sarea_priv->pfCurrentPage == 1)
  1277. ? dev_priv->front_offset : dev_priv->back_offset;
  1278. RING_LOCALS;
  1279. DRM_DEBUG("pfCurrentPage=%d\n",
  1280. master_priv->sarea_priv->pfCurrentPage);
  1281. /* Do some trivial performance monitoring...
  1282. */
  1283. if (dev_priv->do_boxes) {
  1284. dev_priv->stats.boxes |= RADEON_BOX_FLIP;
  1285. radeon_cp_performance_boxes(dev_priv, master_priv);
  1286. }
  1287. /* Update the frame offsets for both CRTCs
  1288. */
  1289. BEGIN_RING(6);
  1290. RADEON_WAIT_UNTIL_3D_IDLE();
  1291. OUT_RING_REG(RADEON_CRTC_OFFSET,
  1292. ((sarea->frame.y * dev_priv->front_pitch +
  1293. sarea->frame.x * (dev_priv->color_fmt - 2)) & ~7)
  1294. + offset);
  1295. OUT_RING_REG(RADEON_CRTC2_OFFSET, master_priv->sarea_priv->crtc2_base
  1296. + offset);
  1297. ADVANCE_RING();
  1298. /* Increment the frame counter. The client-side 3D driver must
  1299. * throttle the framerate by waiting for this value before
  1300. * performing the swapbuffer ioctl.
  1301. */
  1302. master_priv->sarea_priv->last_frame++;
  1303. master_priv->sarea_priv->pfCurrentPage =
  1304. 1 - master_priv->sarea_priv->pfCurrentPage;
  1305. BEGIN_RING(2);
  1306. RADEON_FRAME_AGE(master_priv->sarea_priv->last_frame);
  1307. ADVANCE_RING();
  1308. }
  1309. static int bad_prim_vertex_nr(int primitive, int nr)
  1310. {
  1311. switch (primitive & RADEON_PRIM_TYPE_MASK) {
  1312. case RADEON_PRIM_TYPE_NONE:
  1313. case RADEON_PRIM_TYPE_POINT:
  1314. return nr < 1;
  1315. case RADEON_PRIM_TYPE_LINE:
  1316. return (nr & 1) || nr == 0;
  1317. case RADEON_PRIM_TYPE_LINE_STRIP:
  1318. return nr < 2;
  1319. case RADEON_PRIM_TYPE_TRI_LIST:
  1320. case RADEON_PRIM_TYPE_3VRT_POINT_LIST:
  1321. case RADEON_PRIM_TYPE_3VRT_LINE_LIST:
  1322. case RADEON_PRIM_TYPE_RECT_LIST:
  1323. return nr % 3 || nr == 0;
  1324. case RADEON_PRIM_TYPE_TRI_FAN:
  1325. case RADEON_PRIM_TYPE_TRI_STRIP:
  1326. return nr < 3;
  1327. default:
  1328. return 1;
  1329. }
  1330. }
  1331. typedef struct {
  1332. unsigned int start;
  1333. unsigned int finish;
  1334. unsigned int prim;
  1335. unsigned int numverts;
  1336. unsigned int offset;
  1337. unsigned int vc_format;
  1338. } drm_radeon_tcl_prim_t;
  1339. static void radeon_cp_dispatch_vertex(struct drm_device * dev,
  1340. struct drm_file *file_priv,
  1341. struct drm_buf * buf,
  1342. drm_radeon_tcl_prim_t * prim)
  1343. {
  1344. drm_radeon_private_t *dev_priv = dev->dev_private;
  1345. struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
  1346. drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
  1347. int offset = dev_priv->gart_buffers_offset + buf->offset + prim->start;
  1348. int numverts = (int)prim->numverts;
  1349. int nbox = sarea_priv->nbox;
  1350. int i = 0;
  1351. RING_LOCALS;
  1352. DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d %d verts\n",
  1353. prim->prim,
  1354. prim->vc_format, prim->start, prim->finish, prim->numverts);
  1355. if (bad_prim_vertex_nr(prim->prim, prim->numverts)) {
  1356. DRM_ERROR("bad prim %x numverts %d\n",
  1357. prim->prim, prim->numverts);
  1358. return;
  1359. }
  1360. do {
  1361. /* Emit the next cliprect */
  1362. if (i < nbox) {
  1363. radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
  1364. }
  1365. /* Emit the vertex buffer rendering commands */
  1366. BEGIN_RING(5);
  1367. OUT_RING(CP_PACKET3(RADEON_3D_RNDR_GEN_INDX_PRIM, 3));
  1368. OUT_RING(offset);
  1369. OUT_RING(numverts);
  1370. OUT_RING(prim->vc_format);
  1371. OUT_RING(prim->prim | RADEON_PRIM_WALK_LIST |
  1372. RADEON_COLOR_ORDER_RGBA |
  1373. RADEON_VTX_FMT_RADEON_MODE |
  1374. (numverts << RADEON_NUM_VERTICES_SHIFT));
  1375. ADVANCE_RING();
  1376. i++;
  1377. } while (i < nbox);
  1378. }
  1379. void radeon_cp_discard_buffer(struct drm_device *dev, struct drm_master *master, struct drm_buf *buf)
  1380. {
  1381. drm_radeon_private_t *dev_priv = dev->dev_private;
  1382. struct drm_radeon_master_private *master_priv = master->driver_priv;
  1383. drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
  1384. RING_LOCALS;
  1385. buf_priv->age = ++master_priv->sarea_priv->last_dispatch;
  1386. /* Emit the vertex buffer age */
  1387. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
  1388. BEGIN_RING(3);
  1389. R600_DISPATCH_AGE(buf_priv->age);
  1390. ADVANCE_RING();
  1391. } else {
  1392. BEGIN_RING(2);
  1393. RADEON_DISPATCH_AGE(buf_priv->age);
  1394. ADVANCE_RING();
  1395. }
  1396. buf->pending = 1;
  1397. buf->used = 0;
  1398. }
  1399. static void radeon_cp_dispatch_indirect(struct drm_device * dev,
  1400. struct drm_buf * buf, int start, int end)
  1401. {
  1402. drm_radeon_private_t *dev_priv = dev->dev_private;
  1403. RING_LOCALS;
  1404. DRM_DEBUG("buf=%d s=0x%x e=0x%x\n", buf->idx, start, end);
  1405. if (start != end) {
  1406. int offset = (dev_priv->gart_buffers_offset
  1407. + buf->offset + start);
  1408. int dwords = (end - start + 3) / sizeof(u32);
  1409. /* Indirect buffer data must be an even number of
  1410. * dwords, so if we've been given an odd number we must
  1411. * pad the data with a Type-2 CP packet.
  1412. */
  1413. if (dwords & 1) {
  1414. u32 *data = (u32 *)
  1415. ((char *)dev->agp_buffer_map->handle
  1416. + buf->offset + start);
  1417. data[dwords++] = RADEON_CP_PACKET2;
  1418. }
  1419. /* Fire off the indirect buffer */
  1420. BEGIN_RING(3);
  1421. OUT_RING(CP_PACKET0(RADEON_CP_IB_BASE, 1));
  1422. OUT_RING(offset);
  1423. OUT_RING(dwords);
  1424. ADVANCE_RING();
  1425. }
  1426. }
  1427. static void radeon_cp_dispatch_indices(struct drm_device *dev,
  1428. struct drm_master *master,
  1429. struct drm_buf * elt_buf,
  1430. drm_radeon_tcl_prim_t * prim)
  1431. {
  1432. drm_radeon_private_t *dev_priv = dev->dev_private;
  1433. struct drm_radeon_master_private *master_priv = master->driver_priv;
  1434. drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
  1435. int offset = dev_priv->gart_buffers_offset + prim->offset;
  1436. u32 *data;
  1437. int dwords;
  1438. int i = 0;
  1439. int start = prim->start + RADEON_INDEX_PRIM_OFFSET;
  1440. int count = (prim->finish - start) / sizeof(u16);
  1441. int nbox = sarea_priv->nbox;
  1442. DRM_DEBUG("hwprim 0x%x vfmt 0x%x %d..%d offset: %x nr %d\n",
  1443. prim->prim,
  1444. prim->vc_format,
  1445. prim->start, prim->finish, prim->offset, prim->numverts);
  1446. if (bad_prim_vertex_nr(prim->prim, count)) {
  1447. DRM_ERROR("bad prim %x count %d\n", prim->prim, count);
  1448. return;
  1449. }
  1450. if (start >= prim->finish || (prim->start & 0x7)) {
  1451. DRM_ERROR("buffer prim %d\n", prim->prim);
  1452. return;
  1453. }
  1454. dwords = (prim->finish - prim->start + 3) / sizeof(u32);
  1455. data = (u32 *) ((char *)dev->agp_buffer_map->handle +
  1456. elt_buf->offset + prim->start);
  1457. data[0] = CP_PACKET3(RADEON_3D_RNDR_GEN_INDX_PRIM, dwords - 2);
  1458. data[1] = offset;
  1459. data[2] = prim->numverts;
  1460. data[3] = prim->vc_format;
  1461. data[4] = (prim->prim |
  1462. RADEON_PRIM_WALK_IND |
  1463. RADEON_COLOR_ORDER_RGBA |
  1464. RADEON_VTX_FMT_RADEON_MODE |
  1465. (count << RADEON_NUM_VERTICES_SHIFT));
  1466. do {
  1467. if (i < nbox)
  1468. radeon_emit_clip_rect(dev_priv, &sarea_priv->boxes[i]);
  1469. radeon_cp_dispatch_indirect(dev, elt_buf,
  1470. prim->start, prim->finish);
  1471. i++;
  1472. } while (i < nbox);
  1473. }
  1474. #define RADEON_MAX_TEXTURE_SIZE RADEON_BUFFER_SIZE
  1475. static int radeon_cp_dispatch_texture(struct drm_device * dev,
  1476. struct drm_file *file_priv,
  1477. drm_radeon_texture_t * tex,
  1478. drm_radeon_tex_image_t * image)
  1479. {
  1480. drm_radeon_private_t *dev_priv = dev->dev_private;
  1481. struct drm_buf *buf;
  1482. u32 format;
  1483. u32 *buffer;
  1484. const u8 __user *data;
  1485. unsigned int size, dwords, tex_width, blit_width, spitch;
  1486. u32 height;
  1487. int i;
  1488. u32 texpitch, microtile;
  1489. u32 offset, byte_offset;
  1490. RING_LOCALS;
  1491. if (radeon_check_and_fixup_offset(dev_priv, file_priv, &tex->offset)) {
  1492. DRM_ERROR("Invalid destination offset\n");
  1493. return -EINVAL;
  1494. }
  1495. dev_priv->stats.boxes |= RADEON_BOX_TEXTURE_LOAD;
  1496. /* Flush the pixel cache. This ensures no pixel data gets mixed
  1497. * up with the texture data from the host data blit, otherwise
  1498. * part of the texture image may be corrupted.
  1499. */
  1500. BEGIN_RING(4);
  1501. RADEON_FLUSH_CACHE();
  1502. RADEON_WAIT_UNTIL_IDLE();
  1503. ADVANCE_RING();
  1504. /* The compiler won't optimize away a division by a variable,
  1505. * even if the only legal values are powers of two. Thus, we'll
  1506. * use a shift instead.
  1507. */
  1508. switch (tex->format) {
  1509. case RADEON_TXFORMAT_ARGB8888:
  1510. case RADEON_TXFORMAT_RGBA8888:
  1511. format = RADEON_COLOR_FORMAT_ARGB8888;
  1512. tex_width = tex->width * 4;
  1513. blit_width = image->width * 4;
  1514. break;
  1515. case RADEON_TXFORMAT_AI88:
  1516. case RADEON_TXFORMAT_ARGB1555:
  1517. case RADEON_TXFORMAT_RGB565:
  1518. case RADEON_TXFORMAT_ARGB4444:
  1519. case RADEON_TXFORMAT_VYUY422:
  1520. case RADEON_TXFORMAT_YVYU422:
  1521. format = RADEON_COLOR_FORMAT_RGB565;
  1522. tex_width = tex->width * 2;
  1523. blit_width = image->width * 2;
  1524. break;
  1525. case RADEON_TXFORMAT_I8:
  1526. case RADEON_TXFORMAT_RGB332:
  1527. format = RADEON_COLOR_FORMAT_CI8;
  1528. tex_width = tex->width * 1;
  1529. blit_width = image->width * 1;
  1530. break;
  1531. default:
  1532. DRM_ERROR("invalid texture format %d\n", tex->format);
  1533. return -EINVAL;
  1534. }
  1535. spitch = blit_width >> 6;
  1536. if (spitch == 0 && image->height > 1)
  1537. return -EINVAL;
  1538. texpitch = tex->pitch;
  1539. if ((texpitch << 22) & RADEON_DST_TILE_MICRO) {
  1540. microtile = 1;
  1541. if (tex_width < 64) {
  1542. texpitch &= ~(RADEON_DST_TILE_MICRO >> 22);
  1543. /* we got tiled coordinates, untile them */
  1544. image->x *= 2;
  1545. }
  1546. } else
  1547. microtile = 0;
  1548. /* this might fail for zero-sized uploads - are those illegal? */
  1549. if (!radeon_check_offset(dev_priv, tex->offset + image->height *
  1550. blit_width - 1)) {
  1551. DRM_ERROR("Invalid final destination offset\n");
  1552. return -EINVAL;
  1553. }
  1554. DRM_DEBUG("tex=%dx%d blit=%d\n", tex_width, tex->height, blit_width);
  1555. do {
  1556. DRM_DEBUG("tex: ofs=0x%x p=%d f=%d x=%hd y=%hd w=%hd h=%hd\n",
  1557. tex->offset >> 10, tex->pitch, tex->format,
  1558. image->x, image->y, image->width, image->height);
  1559. /* Make a copy of some parameters in case we have to
  1560. * update them for a multi-pass texture blit.
  1561. */
  1562. height = image->height;
  1563. data = (const u8 __user *)image->data;
  1564. size = height * blit_width;
  1565. if (size > RADEON_MAX_TEXTURE_SIZE) {
  1566. height = RADEON_MAX_TEXTURE_SIZE / blit_width;
  1567. size = height * blit_width;
  1568. } else if (size < 4 && size > 0) {
  1569. size = 4;
  1570. } else if (size == 0) {
  1571. return 0;
  1572. }
  1573. buf = radeon_freelist_get(dev);
  1574. if (0 && !buf) {
  1575. radeon_do_cp_idle(dev_priv);
  1576. buf = radeon_freelist_get(dev);
  1577. }
  1578. if (!buf) {
  1579. DRM_DEBUG("EAGAIN\n");
  1580. if (copy_to_user(tex->image, image, sizeof(*image)))
  1581. return -EFAULT;
  1582. return -EAGAIN;
  1583. }
  1584. /* Dispatch the indirect buffer.
  1585. */
  1586. buffer =
  1587. (u32 *) ((char *)dev->agp_buffer_map->handle + buf->offset);
  1588. dwords = size / 4;
  1589. #define RADEON_COPY_MT(_buf, _data, _width) \
  1590. do { \
  1591. if (copy_from_user(_buf, _data, (_width))) {\
  1592. DRM_ERROR("EFAULT on pad, %d bytes\n", (_width)); \
  1593. return -EFAULT; \
  1594. } \
  1595. } while(0)
  1596. if (microtile) {
  1597. /* texture micro tiling in use, minimum texture width is thus 16 bytes.
  1598. however, we cannot use blitter directly for texture width < 64 bytes,
  1599. since minimum tex pitch is 64 bytes and we need this to match
  1600. the texture width, otherwise the blitter will tile it wrong.
  1601. Thus, tiling manually in this case. Additionally, need to special
  1602. case tex height = 1, since our actual image will have height 2
  1603. and we need to ensure we don't read beyond the texture size
  1604. from user space. */
  1605. if (tex->height == 1) {
  1606. if (tex_width >= 64 || tex_width <= 16) {
  1607. RADEON_COPY_MT(buffer, data,
  1608. (int)(tex_width * sizeof(u32)));
  1609. } else if (tex_width == 32) {
  1610. RADEON_COPY_MT(buffer, data, 16);
  1611. RADEON_COPY_MT(buffer + 8,
  1612. data + 16, 16);
  1613. }
  1614. } else if (tex_width >= 64 || tex_width == 16) {
  1615. RADEON_COPY_MT(buffer, data,
  1616. (int)(dwords * sizeof(u32)));
  1617. } else if (tex_width < 16) {
  1618. for (i = 0; i < tex->height; i++) {
  1619. RADEON_COPY_MT(buffer, data, tex_width);
  1620. buffer += 4;
  1621. data += tex_width;
  1622. }
  1623. } else if (tex_width == 32) {
  1624. /* TODO: make sure this works when not fitting in one buffer
  1625. (i.e. 32bytes x 2048...) */
  1626. for (i = 0; i < tex->height; i += 2) {
  1627. RADEON_COPY_MT(buffer, data, 16);
  1628. data += 16;
  1629. RADEON_COPY_MT(buffer + 8, data, 16);
  1630. data += 16;
  1631. RADEON_COPY_MT(buffer + 4, data, 16);
  1632. data += 16;
  1633. RADEON_COPY_MT(buffer + 12, data, 16);
  1634. data += 16;
  1635. buffer += 16;
  1636. }
  1637. }
  1638. } else {
  1639. if (tex_width >= 32) {
  1640. /* Texture image width is larger than the minimum, so we
  1641. * can upload it directly.
  1642. */
  1643. RADEON_COPY_MT(buffer, data,
  1644. (int)(dwords * sizeof(u32)));
  1645. } else {
  1646. /* Texture image width is less than the minimum, so we
  1647. * need to pad out each image scanline to the minimum
  1648. * width.
  1649. */
  1650. for (i = 0; i < tex->height; i++) {
  1651. RADEON_COPY_MT(buffer, data, tex_width);
  1652. buffer += 8;
  1653. data += tex_width;
  1654. }
  1655. }
  1656. }
  1657. #undef RADEON_COPY_MT
  1658. byte_offset = (image->y & ~2047) * blit_width;
  1659. buf->file_priv = file_priv;
  1660. buf->used = size;
  1661. offset = dev_priv->gart_buffers_offset + buf->offset;
  1662. BEGIN_RING(9);
  1663. OUT_RING(CP_PACKET3(RADEON_CNTL_BITBLT_MULTI, 5));
  1664. OUT_RING(RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
  1665. RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  1666. RADEON_GMC_BRUSH_NONE |
  1667. (format << 8) |
  1668. RADEON_GMC_SRC_DATATYPE_COLOR |
  1669. RADEON_ROP3_S |
  1670. RADEON_DP_SRC_SOURCE_MEMORY |
  1671. RADEON_GMC_CLR_CMP_CNTL_DIS | RADEON_GMC_WR_MSK_DIS);
  1672. OUT_RING((spitch << 22) | (offset >> 10));
  1673. OUT_RING((texpitch << 22) | ((tex->offset >> 10) + (byte_offset >> 10)));
  1674. OUT_RING(0);
  1675. OUT_RING((image->x << 16) | (image->y % 2048));
  1676. OUT_RING((image->width << 16) | height);
  1677. RADEON_WAIT_UNTIL_2D_IDLE();
  1678. ADVANCE_RING();
  1679. COMMIT_RING();
  1680. radeon_cp_discard_buffer(dev, file_priv->master, buf);
  1681. /* Update the input parameters for next time */
  1682. image->y += height;
  1683. image->height -= height;
  1684. image->data = (const u8 __user *)image->data + size;
  1685. } while (image->height > 0);
  1686. /* Flush the pixel cache after the blit completes. This ensures
  1687. * the texture data is written out to memory before rendering
  1688. * continues.
  1689. */
  1690. BEGIN_RING(4);
  1691. RADEON_FLUSH_CACHE();
  1692. RADEON_WAIT_UNTIL_2D_IDLE();
  1693. ADVANCE_RING();
  1694. COMMIT_RING();
  1695. return 0;
  1696. }
  1697. static void radeon_cp_dispatch_stipple(struct drm_device * dev, u32 * stipple)
  1698. {
  1699. drm_radeon_private_t *dev_priv = dev->dev_private;
  1700. int i;
  1701. RING_LOCALS;
  1702. DRM_DEBUG("\n");
  1703. BEGIN_RING(35);
  1704. OUT_RING(CP_PACKET0(RADEON_RE_STIPPLE_ADDR, 0));
  1705. OUT_RING(0x00000000);
  1706. OUT_RING(CP_PACKET0_TABLE(RADEON_RE_STIPPLE_DATA, 31));
  1707. for (i = 0; i < 32; i++) {
  1708. OUT_RING(stipple[i]);
  1709. }
  1710. ADVANCE_RING();
  1711. }
  1712. static void radeon_apply_surface_regs(int surf_index,
  1713. drm_radeon_private_t *dev_priv)
  1714. {
  1715. if (!dev_priv->mmio)
  1716. return;
  1717. radeon_do_cp_idle(dev_priv);
  1718. RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * surf_index,
  1719. dev_priv->surfaces[surf_index].flags);
  1720. RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND + 16 * surf_index,
  1721. dev_priv->surfaces[surf_index].lower);
  1722. RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND + 16 * surf_index,
  1723. dev_priv->surfaces[surf_index].upper);
  1724. }
  1725. /* Allocates a virtual surface
  1726. * doesn't always allocate a real surface, will stretch an existing
  1727. * surface when possible.
  1728. *
  1729. * Note that refcount can be at most 2, since during a free refcount=3
  1730. * might mean we have to allocate a new surface which might not always
  1731. * be available.
  1732. * For example : we allocate three contiguous surfaces ABC. If B is
  1733. * freed, we suddenly need two surfaces to store A and C, which might
  1734. * not always be available.
  1735. */
  1736. static int alloc_surface(drm_radeon_surface_alloc_t *new,
  1737. drm_radeon_private_t *dev_priv,
  1738. struct drm_file *file_priv)
  1739. {
  1740. struct radeon_virt_surface *s;
  1741. int i;
  1742. int virt_surface_index;
  1743. uint32_t new_upper, new_lower;
  1744. new_lower = new->address;
  1745. new_upper = new_lower + new->size - 1;
  1746. /* sanity check */
  1747. if ((new_lower >= new_upper) || (new->flags == 0) || (new->size == 0) ||
  1748. ((new_upper & RADEON_SURF_ADDRESS_FIXED_MASK) !=
  1749. RADEON_SURF_ADDRESS_FIXED_MASK)
  1750. || ((new_lower & RADEON_SURF_ADDRESS_FIXED_MASK) != 0))
  1751. return -1;
  1752. /* make sure there is no overlap with existing surfaces */
  1753. for (i = 0; i < RADEON_MAX_SURFACES; i++) {
  1754. if ((dev_priv->surfaces[i].refcount != 0) &&
  1755. (((new_lower >= dev_priv->surfaces[i].lower) &&
  1756. (new_lower < dev_priv->surfaces[i].upper)) ||
  1757. ((new_lower < dev_priv->surfaces[i].lower) &&
  1758. (new_upper > dev_priv->surfaces[i].lower)))) {
  1759. return -1;
  1760. }
  1761. }
  1762. /* find a virtual surface */
  1763. for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++)
  1764. if (dev_priv->virt_surfaces[i].file_priv == NULL)
  1765. break;
  1766. if (i == 2 * RADEON_MAX_SURFACES) {
  1767. return -1;
  1768. }
  1769. virt_surface_index = i;
  1770. /* try to reuse an existing surface */
  1771. for (i = 0; i < RADEON_MAX_SURFACES; i++) {
  1772. /* extend before */
  1773. if ((dev_priv->surfaces[i].refcount == 1) &&
  1774. (new->flags == dev_priv->surfaces[i].flags) &&
  1775. (new_upper + 1 == dev_priv->surfaces[i].lower)) {
  1776. s = &(dev_priv->virt_surfaces[virt_surface_index]);
  1777. s->surface_index = i;
  1778. s->lower = new_lower;
  1779. s->upper = new_upper;
  1780. s->flags = new->flags;
  1781. s->file_priv = file_priv;
  1782. dev_priv->surfaces[i].refcount++;
  1783. dev_priv->surfaces[i].lower = s->lower;
  1784. radeon_apply_surface_regs(s->surface_index, dev_priv);
  1785. return virt_surface_index;
  1786. }
  1787. /* extend after */
  1788. if ((dev_priv->surfaces[i].refcount == 1) &&
  1789. (new->flags == dev_priv->surfaces[i].flags) &&
  1790. (new_lower == dev_priv->surfaces[i].upper + 1)) {
  1791. s = &(dev_priv->virt_surfaces[virt_surface_index]);
  1792. s->surface_index = i;
  1793. s->lower = new_lower;
  1794. s->upper = new_upper;
  1795. s->flags = new->flags;
  1796. s->file_priv = file_priv;
  1797. dev_priv->surfaces[i].refcount++;
  1798. dev_priv->surfaces[i].upper = s->upper;
  1799. radeon_apply_surface_regs(s->surface_index, dev_priv);
  1800. return virt_surface_index;
  1801. }
  1802. }
  1803. /* okay, we need a new one */
  1804. for (i = 0; i < RADEON_MAX_SURFACES; i++) {
  1805. if (dev_priv->surfaces[i].refcount == 0) {
  1806. s = &(dev_priv->virt_surfaces[virt_surface_index]);
  1807. s->surface_index = i;
  1808. s->lower = new_lower;
  1809. s->upper = new_upper;
  1810. s->flags = new->flags;
  1811. s->file_priv = file_priv;
  1812. dev_priv->surfaces[i].refcount = 1;
  1813. dev_priv->surfaces[i].lower = s->lower;
  1814. dev_priv->surfaces[i].upper = s->upper;
  1815. dev_priv->surfaces[i].flags = s->flags;
  1816. radeon_apply_surface_regs(s->surface_index, dev_priv);
  1817. return virt_surface_index;
  1818. }
  1819. }
  1820. /* we didn't find anything */
  1821. return -1;
  1822. }
  1823. static int free_surface(struct drm_file *file_priv,
  1824. drm_radeon_private_t * dev_priv,
  1825. int lower)
  1826. {
  1827. struct radeon_virt_surface *s;
  1828. int i;
  1829. /* find the virtual surface */
  1830. for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
  1831. s = &(dev_priv->virt_surfaces[i]);
  1832. if (s->file_priv) {
  1833. if ((lower == s->lower) && (file_priv == s->file_priv))
  1834. {
  1835. if (dev_priv->surfaces[s->surface_index].
  1836. lower == s->lower)
  1837. dev_priv->surfaces[s->surface_index].
  1838. lower = s->upper;
  1839. if (dev_priv->surfaces[s->surface_index].
  1840. upper == s->upper)
  1841. dev_priv->surfaces[s->surface_index].
  1842. upper = s->lower;
  1843. dev_priv->surfaces[s->surface_index].refcount--;
  1844. if (dev_priv->surfaces[s->surface_index].
  1845. refcount == 0)
  1846. dev_priv->surfaces[s->surface_index].
  1847. flags = 0;
  1848. s->file_priv = NULL;
  1849. radeon_apply_surface_regs(s->surface_index,
  1850. dev_priv);
  1851. return 0;
  1852. }
  1853. }
  1854. }
  1855. return 1;
  1856. }
  1857. static void radeon_surfaces_release(struct drm_file *file_priv,
  1858. drm_radeon_private_t * dev_priv)
  1859. {
  1860. int i;
  1861. for (i = 0; i < 2 * RADEON_MAX_SURFACES; i++) {
  1862. if (dev_priv->virt_surfaces[i].file_priv == file_priv)
  1863. free_surface(file_priv, dev_priv,
  1864. dev_priv->virt_surfaces[i].lower);
  1865. }
  1866. }
  1867. /* ================================================================
  1868. * IOCTL functions
  1869. */
  1870. static int radeon_surface_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1871. {
  1872. drm_radeon_private_t *dev_priv = dev->dev_private;
  1873. drm_radeon_surface_alloc_t *alloc = data;
  1874. if (alloc_surface(alloc, dev_priv, file_priv) == -1)
  1875. return -EINVAL;
  1876. else
  1877. return 0;
  1878. }
  1879. static int radeon_surface_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1880. {
  1881. drm_radeon_private_t *dev_priv = dev->dev_private;
  1882. drm_radeon_surface_free_t *memfree = data;
  1883. if (free_surface(file_priv, dev_priv, memfree->address))
  1884. return -EINVAL;
  1885. else
  1886. return 0;
  1887. }
  1888. static int radeon_cp_clear(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1889. {
  1890. drm_radeon_private_t *dev_priv = dev->dev_private;
  1891. struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
  1892. drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
  1893. drm_radeon_clear_t *clear = data;
  1894. drm_radeon_clear_rect_t depth_boxes[RADEON_NR_SAREA_CLIPRECTS];
  1895. DRM_DEBUG("\n");
  1896. LOCK_TEST_WITH_RETURN(dev, file_priv);
  1897. RING_SPACE_TEST_WITH_RETURN(dev_priv);
  1898. if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
  1899. sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;
  1900. if (copy_from_user(&depth_boxes, clear->depth_boxes,
  1901. sarea_priv->nbox * sizeof(depth_boxes[0])))
  1902. return -EFAULT;
  1903. radeon_cp_dispatch_clear(dev, file_priv->master, clear, depth_boxes);
  1904. COMMIT_RING();
  1905. return 0;
  1906. }
  1907. /* Not sure why this isn't set all the time:
  1908. */
  1909. static int radeon_do_init_pageflip(struct drm_device *dev, struct drm_master *master)
  1910. {
  1911. drm_radeon_private_t *dev_priv = dev->dev_private;
  1912. struct drm_radeon_master_private *master_priv = master->driver_priv;
  1913. RING_LOCALS;
  1914. DRM_DEBUG("\n");
  1915. BEGIN_RING(6);
  1916. RADEON_WAIT_UNTIL_3D_IDLE();
  1917. OUT_RING(CP_PACKET0(RADEON_CRTC_OFFSET_CNTL, 0));
  1918. OUT_RING(RADEON_READ(RADEON_CRTC_OFFSET_CNTL) |
  1919. RADEON_CRTC_OFFSET_FLIP_CNTL);
  1920. OUT_RING(CP_PACKET0(RADEON_CRTC2_OFFSET_CNTL, 0));
  1921. OUT_RING(RADEON_READ(RADEON_CRTC2_OFFSET_CNTL) |
  1922. RADEON_CRTC_OFFSET_FLIP_CNTL);
  1923. ADVANCE_RING();
  1924. dev_priv->page_flipping = 1;
  1925. if (master_priv->sarea_priv->pfCurrentPage != 1)
  1926. master_priv->sarea_priv->pfCurrentPage = 0;
  1927. return 0;
  1928. }
  1929. /* Swapping and flipping are different operations, need different ioctls.
  1930. * They can & should be intermixed to support multiple 3d windows.
  1931. */
  1932. static int radeon_cp_flip(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1933. {
  1934. drm_radeon_private_t *dev_priv = dev->dev_private;
  1935. DRM_DEBUG("\n");
  1936. LOCK_TEST_WITH_RETURN(dev, file_priv);
  1937. RING_SPACE_TEST_WITH_RETURN(dev_priv);
  1938. if (!dev_priv->page_flipping)
  1939. radeon_do_init_pageflip(dev, file_priv->master);
  1940. radeon_cp_dispatch_flip(dev, file_priv->master);
  1941. COMMIT_RING();
  1942. return 0;
  1943. }
  1944. static int radeon_cp_swap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1945. {
  1946. drm_radeon_private_t *dev_priv = dev->dev_private;
  1947. struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
  1948. drm_radeon_sarea_t *sarea_priv = master_priv->sarea_priv;
  1949. DRM_DEBUG("\n");
  1950. LOCK_TEST_WITH_RETURN(dev, file_priv);
  1951. RING_SPACE_TEST_WITH_RETURN(dev_priv);
  1952. if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
  1953. sarea_priv->nbox = RADEON_NR_SAREA_CLIPRECTS;
  1954. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  1955. r600_cp_dispatch_swap(dev, file_priv);
  1956. else
  1957. radeon_cp_dispatch_swap(dev, file_priv->master);
  1958. sarea_priv->ctx_owner = 0;
  1959. COMMIT_RING();
  1960. return 0;
  1961. }
  1962. static int radeon_cp_vertex(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1963. {
  1964. drm_radeon_private_t *dev_priv = dev->dev_private;
  1965. struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
  1966. drm_radeon_sarea_t *sarea_priv;
  1967. struct drm_device_dma *dma = dev->dma;
  1968. struct drm_buf *buf;
  1969. drm_radeon_vertex_t *vertex = data;
  1970. drm_radeon_tcl_prim_t prim;
  1971. LOCK_TEST_WITH_RETURN(dev, file_priv);
  1972. sarea_priv = master_priv->sarea_priv;
  1973. DRM_DEBUG("pid=%d index=%d count=%d discard=%d\n",
  1974. DRM_CURRENTPID, vertex->idx, vertex->count, vertex->discard);
  1975. if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
  1976. DRM_ERROR("buffer index %d (of %d max)\n",
  1977. vertex->idx, dma->buf_count - 1);
  1978. return -EINVAL;
  1979. }
  1980. if (vertex->prim < 0 || vertex->prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
  1981. DRM_ERROR("buffer prim %d\n", vertex->prim);
  1982. return -EINVAL;
  1983. }
  1984. RING_SPACE_TEST_WITH_RETURN(dev_priv);
  1985. VB_AGE_TEST_WITH_RETURN(dev_priv);
  1986. buf = dma->buflist[vertex->idx];
  1987. if (buf->file_priv != file_priv) {
  1988. DRM_ERROR("process %d using buffer owned by %p\n",
  1989. DRM_CURRENTPID, buf->file_priv);
  1990. return -EINVAL;
  1991. }
  1992. if (buf->pending) {
  1993. DRM_ERROR("sending pending buffer %d\n", vertex->idx);
  1994. return -EINVAL;
  1995. }
  1996. /* Build up a prim_t record:
  1997. */
  1998. if (vertex->count) {
  1999. buf->used = vertex->count; /* not used? */
  2000. if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
  2001. if (radeon_emit_state(dev_priv, file_priv,
  2002. &sarea_priv->context_state,
  2003. sarea_priv->tex_state,
  2004. sarea_priv->dirty)) {
  2005. DRM_ERROR("radeon_emit_state failed\n");
  2006. return -EINVAL;
  2007. }
  2008. sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
  2009. RADEON_UPLOAD_TEX1IMAGES |
  2010. RADEON_UPLOAD_TEX2IMAGES |
  2011. RADEON_REQUIRE_QUIESCENCE);
  2012. }
  2013. prim.start = 0;
  2014. prim.finish = vertex->count; /* unused */
  2015. prim.prim = vertex->prim;
  2016. prim.numverts = vertex->count;
  2017. prim.vc_format = sarea_priv->vc_format;
  2018. radeon_cp_dispatch_vertex(dev, file_priv, buf, &prim);
  2019. }
  2020. if (vertex->discard) {
  2021. radeon_cp_discard_buffer(dev, file_priv->master, buf);
  2022. }
  2023. COMMIT_RING();
  2024. return 0;
  2025. }
  2026. static int radeon_cp_indices(struct drm_device *dev, void *data, struct drm_file *file_priv)
  2027. {
  2028. drm_radeon_private_t *dev_priv = dev->dev_private;
  2029. struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
  2030. drm_radeon_sarea_t *sarea_priv;
  2031. struct drm_device_dma *dma = dev->dma;
  2032. struct drm_buf *buf;
  2033. drm_radeon_indices_t *elts = data;
  2034. drm_radeon_tcl_prim_t prim;
  2035. int count;
  2036. LOCK_TEST_WITH_RETURN(dev, file_priv);
  2037. sarea_priv = master_priv->sarea_priv;
  2038. DRM_DEBUG("pid=%d index=%d start=%d end=%d discard=%d\n",
  2039. DRM_CURRENTPID, elts->idx, elts->start, elts->end,
  2040. elts->discard);
  2041. if (elts->idx < 0 || elts->idx >= dma->buf_count) {
  2042. DRM_ERROR("buffer index %d (of %d max)\n",
  2043. elts->idx, dma->buf_count - 1);
  2044. return -EINVAL;
  2045. }
  2046. if (elts->prim < 0 || elts->prim > RADEON_PRIM_TYPE_3VRT_LINE_LIST) {
  2047. DRM_ERROR("buffer prim %d\n", elts->prim);
  2048. return -EINVAL;
  2049. }
  2050. RING_SPACE_TEST_WITH_RETURN(dev_priv);
  2051. VB_AGE_TEST_WITH_RETURN(dev_priv);
  2052. buf = dma->buflist[elts->idx];
  2053. if (buf->file_priv != file_priv) {
  2054. DRM_ERROR("process %d using buffer owned by %p\n",
  2055. DRM_CURRENTPID, buf->file_priv);
  2056. return -EINVAL;
  2057. }
  2058. if (buf->pending) {
  2059. DRM_ERROR("sending pending buffer %d\n", elts->idx);
  2060. return -EINVAL;
  2061. }
  2062. count = (elts->end - elts->start) / sizeof(u16);
  2063. elts->start -= RADEON_INDEX_PRIM_OFFSET;
  2064. if (elts->start & 0x7) {
  2065. DRM_ERROR("misaligned buffer 0x%x\n", elts->start);
  2066. return -EINVAL;
  2067. }
  2068. if (elts->start < buf->used) {
  2069. DRM_ERROR("no header 0x%x - 0x%x\n", elts->start, buf->used);
  2070. return -EINVAL;
  2071. }
  2072. buf->used = elts->end;
  2073. if (sarea_priv->dirty & ~RADEON_UPLOAD_CLIPRECTS) {
  2074. if (radeon_emit_state(dev_priv, file_priv,
  2075. &sarea_priv->context_state,
  2076. sarea_priv->tex_state,
  2077. sarea_priv->dirty)) {
  2078. DRM_ERROR("radeon_emit_state failed\n");
  2079. return -EINVAL;
  2080. }
  2081. sarea_priv->dirty &= ~(RADEON_UPLOAD_TEX0IMAGES |
  2082. RADEON_UPLOAD_TEX1IMAGES |
  2083. RADEON_UPLOAD_TEX2IMAGES |
  2084. RADEON_REQUIRE_QUIESCENCE);
  2085. }
  2086. /* Build up a prim_t record:
  2087. */
  2088. prim.start = elts->start;
  2089. prim.finish = elts->end;
  2090. prim.prim = elts->prim;
  2091. prim.offset = 0; /* offset from start of dma buffers */
  2092. prim.numverts = RADEON_MAX_VB_VERTS; /* duh */
  2093. prim.vc_format = sarea_priv->vc_format;
  2094. radeon_cp_dispatch_indices(dev, file_priv->master, buf, &prim);
  2095. if (elts->discard) {
  2096. radeon_cp_discard_buffer(dev, file_priv->master, buf);
  2097. }
  2098. COMMIT_RING();
  2099. return 0;
  2100. }
  2101. static int radeon_cp_texture(struct drm_device *dev, void *data, struct drm_file *file_priv)
  2102. {
  2103. drm_radeon_private_t *dev_priv = dev->dev_private;
  2104. drm_radeon_texture_t *tex = data;
  2105. drm_radeon_tex_image_t image;
  2106. int ret;
  2107. LOCK_TEST_WITH_RETURN(dev, file_priv);
  2108. if (tex->image == NULL) {
  2109. DRM_ERROR("null texture image!\n");
  2110. return -EINVAL;
  2111. }
  2112. if (copy_from_user(&image,
  2113. (drm_radeon_tex_image_t __user *) tex->image,
  2114. sizeof(image)))
  2115. return -EFAULT;
  2116. RING_SPACE_TEST_WITH_RETURN(dev_priv);
  2117. VB_AGE_TEST_WITH_RETURN(dev_priv);
  2118. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  2119. ret = r600_cp_dispatch_texture(dev, file_priv, tex, &image);
  2120. else
  2121. ret = radeon_cp_dispatch_texture(dev, file_priv, tex, &image);
  2122. return ret;
  2123. }
  2124. static int radeon_cp_stipple(struct drm_device *dev, void *data, struct drm_file *file_priv)
  2125. {
  2126. drm_radeon_private_t *dev_priv = dev->dev_private;
  2127. drm_radeon_stipple_t *stipple = data;
  2128. u32 mask[32];
  2129. LOCK_TEST_WITH_RETURN(dev, file_priv);
  2130. if (copy_from_user(&mask, stipple->mask, 32 * sizeof(u32)))
  2131. return -EFAULT;
  2132. RING_SPACE_TEST_WITH_RETURN(dev_priv);
  2133. radeon_cp_dispatch_stipple(dev, mask);
  2134. COMMIT_RING();
  2135. return 0;
  2136. }
  2137. static int radeon_cp_indirect(struct drm_device *dev, void *data, struct drm_file *file_priv)
  2138. {
  2139. drm_radeon_private_t *dev_priv = dev->dev_private;
  2140. struct drm_device_dma *dma = dev->dma;
  2141. struct drm_buf *buf;
  2142. drm_radeon_indirect_t *indirect = data;
  2143. RING_LOCALS;
  2144. LOCK_TEST_WITH_RETURN(dev, file_priv);
  2145. DRM_DEBUG("idx=%d s=%d e=%d d=%d\n",
  2146. indirect->idx, indirect->start, indirect->end,
  2147. indirect->discard);
  2148. if (indirect->idx < 0 || indirect->idx >= dma->buf_count) {
  2149. DRM_ERROR("buffer index %d (of %d max)\n",
  2150. indirect->idx, dma->buf_count - 1);
  2151. return -EINVAL;
  2152. }
  2153. buf = dma->buflist[indirect->idx];
  2154. if (buf->file_priv != file_priv) {
  2155. DRM_ERROR("process %d using buffer owned by %p\n",
  2156. DRM_CURRENTPID, buf->file_priv);
  2157. return -EINVAL;
  2158. }
  2159. if (buf->pending) {
  2160. DRM_ERROR("sending pending buffer %d\n", indirect->idx);
  2161. return -EINVAL;
  2162. }
  2163. if (indirect->start < buf->used) {
  2164. DRM_ERROR("reusing indirect: start=0x%x actual=0x%x\n",
  2165. indirect->start, buf->used);
  2166. return -EINVAL;
  2167. }
  2168. RING_SPACE_TEST_WITH_RETURN(dev_priv);
  2169. VB_AGE_TEST_WITH_RETURN(dev_priv);
  2170. buf->used = indirect->end;
  2171. /* Dispatch the indirect buffer full of commands from the
  2172. * X server. This is insecure and is thus only available to
  2173. * privileged clients.
  2174. */
  2175. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  2176. r600_cp_dispatch_indirect(dev, buf, indirect->start, indirect->end);
  2177. else {
  2178. /* Wait for the 3D stream to idle before the indirect buffer
  2179. * containing 2D acceleration commands is processed.
  2180. */
  2181. BEGIN_RING(2);
  2182. RADEON_WAIT_UNTIL_3D_IDLE();
  2183. ADVANCE_RING();
  2184. radeon_cp_dispatch_indirect(dev, buf, indirect->start, indirect->end);
  2185. }
  2186. if (indirect->discard) {
  2187. radeon_cp_discard_buffer(dev, file_priv->master, buf);
  2188. }
  2189. COMMIT_RING();
  2190. return 0;
  2191. }
  2192. static int radeon_cp_vertex2(struct drm_device *dev, void *data, struct drm_file *file_priv)
  2193. {
  2194. drm_radeon_private_t *dev_priv = dev->dev_private;
  2195. struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
  2196. drm_radeon_sarea_t *sarea_priv;
  2197. struct drm_device_dma *dma = dev->dma;
  2198. struct drm_buf *buf;
  2199. drm_radeon_vertex2_t *vertex = data;
  2200. int i;
  2201. unsigned char laststate;
  2202. LOCK_TEST_WITH_RETURN(dev, file_priv);
  2203. sarea_priv = master_priv->sarea_priv;
  2204. DRM_DEBUG("pid=%d index=%d discard=%d\n",
  2205. DRM_CURRENTPID, vertex->idx, vertex->discard);
  2206. if (vertex->idx < 0 || vertex->idx >= dma->buf_count) {
  2207. DRM_ERROR("buffer index %d (of %d max)\n",
  2208. vertex->idx, dma->buf_count - 1);
  2209. return -EINVAL;
  2210. }
  2211. RING_SPACE_TEST_WITH_RETURN(dev_priv);
  2212. VB_AGE_TEST_WITH_RETURN(dev_priv);
  2213. buf = dma->buflist[vertex->idx];
  2214. if (buf->file_priv != file_priv) {
  2215. DRM_ERROR("process %d using buffer owned by %p\n",
  2216. DRM_CURRENTPID, buf->file_priv);
  2217. return -EINVAL;
  2218. }
  2219. if (buf->pending) {
  2220. DRM_ERROR("sending pending buffer %d\n", vertex->idx);
  2221. return -EINVAL;
  2222. }
  2223. if (sarea_priv->nbox > RADEON_NR_SAREA_CLIPRECTS)
  2224. return -EINVAL;
  2225. for (laststate = 0xff, i = 0; i < vertex->nr_prims; i++) {
  2226. drm_radeon_prim_t prim;
  2227. drm_radeon_tcl_prim_t tclprim;
  2228. if (copy_from_user(&prim, &vertex->prim[i], sizeof(prim)))
  2229. return -EFAULT;
  2230. if (prim.stateidx != laststate) {
  2231. drm_radeon_state_t state;
  2232. if (copy_from_user(&state,
  2233. &vertex->state[prim.stateidx],
  2234. sizeof(state)))
  2235. return -EFAULT;
  2236. if (radeon_emit_state2(dev_priv, file_priv, &state)) {
  2237. DRM_ERROR("radeon_emit_state2 failed\n");
  2238. return -EINVAL;
  2239. }
  2240. laststate = prim.stateidx;
  2241. }
  2242. tclprim.start = prim.start;
  2243. tclprim.finish = prim.finish;
  2244. tclprim.prim = prim.prim;
  2245. tclprim.vc_format = prim.vc_format;
  2246. if (prim.prim & RADEON_PRIM_WALK_IND) {
  2247. tclprim.offset = prim.numverts * 64;
  2248. tclprim.numverts = RADEON_MAX_VB_VERTS; /* duh */
  2249. radeon_cp_dispatch_indices(dev, file_priv->master, buf, &tclprim);
  2250. } else {
  2251. tclprim.numverts = prim.numverts;
  2252. tclprim.offset = 0; /* not used */
  2253. radeon_cp_dispatch_vertex(dev, file_priv, buf, &tclprim);
  2254. }
  2255. if (sarea_priv->nbox == 1)
  2256. sarea_priv->nbox = 0;
  2257. }
  2258. if (vertex->discard) {
  2259. radeon_cp_discard_buffer(dev, file_priv->master, buf);
  2260. }
  2261. COMMIT_RING();
  2262. return 0;
  2263. }
  2264. static int radeon_emit_packets(drm_radeon_private_t * dev_priv,
  2265. struct drm_file *file_priv,
  2266. drm_radeon_cmd_header_t header,
  2267. drm_radeon_kcmd_buffer_t *cmdbuf)
  2268. {
  2269. int id = (int)header.packet.packet_id;
  2270. int sz, reg;
  2271. RING_LOCALS;
  2272. if (id >= RADEON_MAX_STATE_PACKETS)
  2273. return -EINVAL;
  2274. sz = packet[id].len;
  2275. reg = packet[id].start;
  2276. if (sz * sizeof(u32) > drm_buffer_unprocessed(cmdbuf->buffer)) {
  2277. DRM_ERROR("Packet size provided larger than data provided\n");
  2278. return -EINVAL;
  2279. }
  2280. if (radeon_check_and_fixup_packets(dev_priv, file_priv, id,
  2281. cmdbuf->buffer)) {
  2282. DRM_ERROR("Packet verification failed\n");
  2283. return -EINVAL;
  2284. }
  2285. BEGIN_RING(sz + 1);
  2286. OUT_RING(CP_PACKET0(reg, (sz - 1)));
  2287. OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
  2288. ADVANCE_RING();
  2289. return 0;
  2290. }
  2291. static __inline__ int radeon_emit_scalars(drm_radeon_private_t *dev_priv,
  2292. drm_radeon_cmd_header_t header,
  2293. drm_radeon_kcmd_buffer_t *cmdbuf)
  2294. {
  2295. int sz = header.scalars.count;
  2296. int start = header.scalars.offset;
  2297. int stride = header.scalars.stride;
  2298. RING_LOCALS;
  2299. BEGIN_RING(3 + sz);
  2300. OUT_RING(CP_PACKET0(RADEON_SE_TCL_SCALAR_INDX_REG, 0));
  2301. OUT_RING(start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
  2302. OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_SCALAR_DATA_REG, sz - 1));
  2303. OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
  2304. ADVANCE_RING();
  2305. return 0;
  2306. }
  2307. /* God this is ugly
  2308. */
  2309. static __inline__ int radeon_emit_scalars2(drm_radeon_private_t *dev_priv,
  2310. drm_radeon_cmd_header_t header,
  2311. drm_radeon_kcmd_buffer_t *cmdbuf)
  2312. {
  2313. int sz = header.scalars.count;
  2314. int start = ((unsigned int)header.scalars.offset) + 0x100;
  2315. int stride = header.scalars.stride;
  2316. RING_LOCALS;
  2317. BEGIN_RING(3 + sz);
  2318. OUT_RING(CP_PACKET0(RADEON_SE_TCL_SCALAR_INDX_REG, 0));
  2319. OUT_RING(start | (stride << RADEON_SCAL_INDX_DWORD_STRIDE_SHIFT));
  2320. OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_SCALAR_DATA_REG, sz - 1));
  2321. OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
  2322. ADVANCE_RING();
  2323. return 0;
  2324. }
  2325. static __inline__ int radeon_emit_vectors(drm_radeon_private_t *dev_priv,
  2326. drm_radeon_cmd_header_t header,
  2327. drm_radeon_kcmd_buffer_t *cmdbuf)
  2328. {
  2329. int sz = header.vectors.count;
  2330. int start = header.vectors.offset;
  2331. int stride = header.vectors.stride;
  2332. RING_LOCALS;
  2333. BEGIN_RING(5 + sz);
  2334. OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
  2335. OUT_RING(CP_PACKET0(RADEON_SE_TCL_VECTOR_INDX_REG, 0));
  2336. OUT_RING(start | (stride << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT));
  2337. OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_VECTOR_DATA_REG, (sz - 1)));
  2338. OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
  2339. ADVANCE_RING();
  2340. return 0;
  2341. }
  2342. static __inline__ int radeon_emit_veclinear(drm_radeon_private_t *dev_priv,
  2343. drm_radeon_cmd_header_t header,
  2344. drm_radeon_kcmd_buffer_t *cmdbuf)
  2345. {
  2346. int sz = header.veclinear.count * 4;
  2347. int start = header.veclinear.addr_lo | (header.veclinear.addr_hi << 8);
  2348. RING_LOCALS;
  2349. if (!sz)
  2350. return 0;
  2351. if (sz * 4 > drm_buffer_unprocessed(cmdbuf->buffer))
  2352. return -EINVAL;
  2353. BEGIN_RING(5 + sz);
  2354. OUT_RING_REG(RADEON_SE_TCL_STATE_FLUSH, 0);
  2355. OUT_RING(CP_PACKET0(RADEON_SE_TCL_VECTOR_INDX_REG, 0));
  2356. OUT_RING(start | (1 << RADEON_VEC_INDX_OCTWORD_STRIDE_SHIFT));
  2357. OUT_RING(CP_PACKET0_TABLE(RADEON_SE_TCL_VECTOR_DATA_REG, (sz - 1)));
  2358. OUT_RING_DRM_BUFFER(cmdbuf->buffer, sz);
  2359. ADVANCE_RING();
  2360. return 0;
  2361. }
  2362. static int radeon_emit_packet3(struct drm_device * dev,
  2363. struct drm_file *file_priv,
  2364. drm_radeon_kcmd_buffer_t *cmdbuf)
  2365. {
  2366. drm_radeon_private_t *dev_priv = dev->dev_private;
  2367. unsigned int cmdsz;
  2368. int ret;
  2369. RING_LOCALS;
  2370. DRM_DEBUG("\n");
  2371. if ((ret = radeon_check_and_fixup_packet3(dev_priv, file_priv,
  2372. cmdbuf, &cmdsz))) {
  2373. DRM_ERROR("Packet verification failed\n");
  2374. return ret;
  2375. }
  2376. BEGIN_RING(cmdsz);
  2377. OUT_RING_DRM_BUFFER(cmdbuf->buffer, cmdsz);
  2378. ADVANCE_RING();
  2379. return 0;
  2380. }
  2381. static int radeon_emit_packet3_cliprect(struct drm_device *dev,
  2382. struct drm_file *file_priv,
  2383. drm_radeon_kcmd_buffer_t *cmdbuf,
  2384. int orig_nbox)
  2385. {
  2386. drm_radeon_private_t *dev_priv = dev->dev_private;
  2387. struct drm_clip_rect box;
  2388. unsigned int cmdsz;
  2389. int ret;
  2390. struct drm_clip_rect __user *boxes = cmdbuf->boxes;
  2391. int i = 0;
  2392. RING_LOCALS;
  2393. DRM_DEBUG("\n");
  2394. if ((ret = radeon_check_and_fixup_packet3(dev_priv, file_priv,
  2395. cmdbuf, &cmdsz))) {
  2396. DRM_ERROR("Packet verification failed\n");
  2397. return ret;
  2398. }
  2399. if (!orig_nbox)
  2400. goto out;
  2401. do {
  2402. if (i < cmdbuf->nbox) {
  2403. if (copy_from_user(&box, &boxes[i], sizeof(box)))
  2404. return -EFAULT;
  2405. /* FIXME The second and subsequent times round
  2406. * this loop, send a WAIT_UNTIL_3D_IDLE before
  2407. * calling emit_clip_rect(). This fixes a
  2408. * lockup on fast machines when sending
  2409. * several cliprects with a cmdbuf, as when
  2410. * waving a 2D window over a 3D
  2411. * window. Something in the commands from user
  2412. * space seems to hang the card when they're
  2413. * sent several times in a row. That would be
  2414. * the correct place to fix it but this works
  2415. * around it until I can figure that out - Tim
  2416. * Smith */
  2417. if (i) {
  2418. BEGIN_RING(2);
  2419. RADEON_WAIT_UNTIL_3D_IDLE();
  2420. ADVANCE_RING();
  2421. }
  2422. radeon_emit_clip_rect(dev_priv, &box);
  2423. }
  2424. BEGIN_RING(cmdsz);
  2425. OUT_RING_DRM_BUFFER(cmdbuf->buffer, cmdsz);
  2426. ADVANCE_RING();
  2427. } while (++i < cmdbuf->nbox);
  2428. if (cmdbuf->nbox == 1)
  2429. cmdbuf->nbox = 0;
  2430. return 0;
  2431. out:
  2432. drm_buffer_advance(cmdbuf->buffer, cmdsz * 4);
  2433. return 0;
  2434. }
  2435. static int radeon_emit_wait(struct drm_device * dev, int flags)
  2436. {
  2437. drm_radeon_private_t *dev_priv = dev->dev_private;
  2438. RING_LOCALS;
  2439. DRM_DEBUG("%x\n", flags);
  2440. switch (flags) {
  2441. case RADEON_WAIT_2D:
  2442. BEGIN_RING(2);
  2443. RADEON_WAIT_UNTIL_2D_IDLE();
  2444. ADVANCE_RING();
  2445. break;
  2446. case RADEON_WAIT_3D:
  2447. BEGIN_RING(2);
  2448. RADEON_WAIT_UNTIL_3D_IDLE();
  2449. ADVANCE_RING();
  2450. break;
  2451. case RADEON_WAIT_2D | RADEON_WAIT_3D:
  2452. BEGIN_RING(2);
  2453. RADEON_WAIT_UNTIL_IDLE();
  2454. ADVANCE_RING();
  2455. break;
  2456. default:
  2457. return -EINVAL;
  2458. }
  2459. return 0;
  2460. }
  2461. static int radeon_cp_cmdbuf(struct drm_device *dev, void *data,
  2462. struct drm_file *file_priv)
  2463. {
  2464. drm_radeon_private_t *dev_priv = dev->dev_private;
  2465. struct drm_device_dma *dma = dev->dma;
  2466. struct drm_buf *buf = NULL;
  2467. drm_radeon_cmd_header_t stack_header;
  2468. int idx;
  2469. drm_radeon_kcmd_buffer_t *cmdbuf = data;
  2470. int orig_nbox;
  2471. LOCK_TEST_WITH_RETURN(dev, file_priv);
  2472. RING_SPACE_TEST_WITH_RETURN(dev_priv);
  2473. VB_AGE_TEST_WITH_RETURN(dev_priv);
  2474. if (cmdbuf->bufsz > 64 * 1024 || cmdbuf->bufsz < 0) {
  2475. return -EINVAL;
  2476. }
  2477. /* Allocate an in-kernel area and copy in the cmdbuf. Do this to avoid
  2478. * races between checking values and using those values in other code,
  2479. * and simply to avoid a lot of function calls to copy in data.
  2480. */
  2481. if (cmdbuf->bufsz != 0) {
  2482. int rv;
  2483. void __user *buffer = cmdbuf->buffer;
  2484. rv = drm_buffer_alloc(&cmdbuf->buffer, cmdbuf->bufsz);
  2485. if (rv)
  2486. return rv;
  2487. rv = drm_buffer_copy_from_user(cmdbuf->buffer, buffer,
  2488. cmdbuf->bufsz);
  2489. if (rv) {
  2490. drm_buffer_free(cmdbuf->buffer);
  2491. return rv;
  2492. }
  2493. } else
  2494. goto done;
  2495. orig_nbox = cmdbuf->nbox;
  2496. if (dev_priv->microcode_version == UCODE_R300) {
  2497. int temp;
  2498. temp = r300_do_cp_cmdbuf(dev, file_priv, cmdbuf);
  2499. drm_buffer_free(cmdbuf->buffer);
  2500. return temp;
  2501. }
  2502. /* microcode_version != r300 */
  2503. while (drm_buffer_unprocessed(cmdbuf->buffer) >= sizeof(stack_header)) {
  2504. drm_radeon_cmd_header_t *header;
  2505. header = drm_buffer_read_object(cmdbuf->buffer,
  2506. sizeof(stack_header), &stack_header);
  2507. switch (header->header.cmd_type) {
  2508. case RADEON_CMD_PACKET:
  2509. DRM_DEBUG("RADEON_CMD_PACKET\n");
  2510. if (radeon_emit_packets
  2511. (dev_priv, file_priv, *header, cmdbuf)) {
  2512. DRM_ERROR("radeon_emit_packets failed\n");
  2513. goto err;
  2514. }
  2515. break;
  2516. case RADEON_CMD_SCALARS:
  2517. DRM_DEBUG("RADEON_CMD_SCALARS\n");
  2518. if (radeon_emit_scalars(dev_priv, *header, cmdbuf)) {
  2519. DRM_ERROR("radeon_emit_scalars failed\n");
  2520. goto err;
  2521. }
  2522. break;
  2523. case RADEON_CMD_VECTORS:
  2524. DRM_DEBUG("RADEON_CMD_VECTORS\n");
  2525. if (radeon_emit_vectors(dev_priv, *header, cmdbuf)) {
  2526. DRM_ERROR("radeon_emit_vectors failed\n");
  2527. goto err;
  2528. }
  2529. break;
  2530. case RADEON_CMD_DMA_DISCARD:
  2531. DRM_DEBUG("RADEON_CMD_DMA_DISCARD\n");
  2532. idx = header->dma.buf_idx;
  2533. if (idx < 0 || idx >= dma->buf_count) {
  2534. DRM_ERROR("buffer index %d (of %d max)\n",
  2535. idx, dma->buf_count - 1);
  2536. goto err;
  2537. }
  2538. buf = dma->buflist[idx];
  2539. if (buf->file_priv != file_priv || buf->pending) {
  2540. DRM_ERROR("bad buffer %p %p %d\n",
  2541. buf->file_priv, file_priv,
  2542. buf->pending);
  2543. goto err;
  2544. }
  2545. radeon_cp_discard_buffer(dev, file_priv->master, buf);
  2546. break;
  2547. case RADEON_CMD_PACKET3:
  2548. DRM_DEBUG("RADEON_CMD_PACKET3\n");
  2549. if (radeon_emit_packet3(dev, file_priv, cmdbuf)) {
  2550. DRM_ERROR("radeon_emit_packet3 failed\n");
  2551. goto err;
  2552. }
  2553. break;
  2554. case RADEON_CMD_PACKET3_CLIP:
  2555. DRM_DEBUG("RADEON_CMD_PACKET3_CLIP\n");
  2556. if (radeon_emit_packet3_cliprect
  2557. (dev, file_priv, cmdbuf, orig_nbox)) {
  2558. DRM_ERROR("radeon_emit_packet3_clip failed\n");
  2559. goto err;
  2560. }
  2561. break;
  2562. case RADEON_CMD_SCALARS2:
  2563. DRM_DEBUG("RADEON_CMD_SCALARS2\n");
  2564. if (radeon_emit_scalars2(dev_priv, *header, cmdbuf)) {
  2565. DRM_ERROR("radeon_emit_scalars2 failed\n");
  2566. goto err;
  2567. }
  2568. break;
  2569. case RADEON_CMD_WAIT:
  2570. DRM_DEBUG("RADEON_CMD_WAIT\n");
  2571. if (radeon_emit_wait(dev, header->wait.flags)) {
  2572. DRM_ERROR("radeon_emit_wait failed\n");
  2573. goto err;
  2574. }
  2575. break;
  2576. case RADEON_CMD_VECLINEAR:
  2577. DRM_DEBUG("RADEON_CMD_VECLINEAR\n");
  2578. if (radeon_emit_veclinear(dev_priv, *header, cmdbuf)) {
  2579. DRM_ERROR("radeon_emit_veclinear failed\n");
  2580. goto err;
  2581. }
  2582. break;
  2583. default:
  2584. DRM_ERROR("bad cmd_type %d at byte %d\n",
  2585. header->header.cmd_type,
  2586. cmdbuf->buffer->iterator);
  2587. goto err;
  2588. }
  2589. }
  2590. drm_buffer_free(cmdbuf->buffer);
  2591. done:
  2592. DRM_DEBUG("DONE\n");
  2593. COMMIT_RING();
  2594. return 0;
  2595. err:
  2596. drm_buffer_free(cmdbuf->buffer);
  2597. return -EINVAL;
  2598. }
  2599. static int radeon_cp_getparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
  2600. {
  2601. drm_radeon_private_t *dev_priv = dev->dev_private;
  2602. drm_radeon_getparam_t *param = data;
  2603. int value;
  2604. DRM_DEBUG("pid=%d\n", DRM_CURRENTPID);
  2605. switch (param->param) {
  2606. case RADEON_PARAM_GART_BUFFER_OFFSET:
  2607. value = dev_priv->gart_buffers_offset;
  2608. break;
  2609. case RADEON_PARAM_LAST_FRAME:
  2610. dev_priv->stats.last_frame_reads++;
  2611. value = GET_SCRATCH(dev_priv, 0);
  2612. break;
  2613. case RADEON_PARAM_LAST_DISPATCH:
  2614. value = GET_SCRATCH(dev_priv, 1);
  2615. break;
  2616. case RADEON_PARAM_LAST_CLEAR:
  2617. dev_priv->stats.last_clear_reads++;
  2618. value = GET_SCRATCH(dev_priv, 2);
  2619. break;
  2620. case RADEON_PARAM_IRQ_NR:
  2621. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  2622. value = 0;
  2623. else
  2624. value = dev->pdev->irq;
  2625. break;
  2626. case RADEON_PARAM_GART_BASE:
  2627. value = dev_priv->gart_vm_start;
  2628. break;
  2629. case RADEON_PARAM_REGISTER_HANDLE:
  2630. value = dev_priv->mmio->offset;
  2631. break;
  2632. case RADEON_PARAM_STATUS_HANDLE:
  2633. value = dev_priv->ring_rptr_offset;
  2634. break;
  2635. #if BITS_PER_LONG == 32
  2636. /*
  2637. * This ioctl() doesn't work on 64-bit platforms because hw_lock is a
  2638. * pointer which can't fit into an int-sized variable. According to
  2639. * Michel Dänzer, the ioctl() is only used on embedded platforms, so
  2640. * not supporting it shouldn't be a problem. If the same functionality
  2641. * is needed on 64-bit platforms, a new ioctl() would have to be added,
  2642. * so backwards-compatibility for the embedded platforms can be
  2643. * maintained. --davidm 4-Feb-2004.
  2644. */
  2645. case RADEON_PARAM_SAREA_HANDLE:
  2646. /* The lock is the first dword in the sarea. */
  2647. /* no users of this parameter */
  2648. break;
  2649. #endif
  2650. case RADEON_PARAM_GART_TEX_HANDLE:
  2651. value = dev_priv->gart_textures_offset;
  2652. break;
  2653. case RADEON_PARAM_SCRATCH_OFFSET:
  2654. if (!dev_priv->writeback_works)
  2655. return -EINVAL;
  2656. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  2657. value = R600_SCRATCH_REG_OFFSET;
  2658. else
  2659. value = RADEON_SCRATCH_REG_OFFSET;
  2660. break;
  2661. case RADEON_PARAM_CARD_TYPE:
  2662. if (dev_priv->flags & RADEON_IS_PCIE)
  2663. value = RADEON_CARD_PCIE;
  2664. else if (dev_priv->flags & RADEON_IS_AGP)
  2665. value = RADEON_CARD_AGP;
  2666. else
  2667. value = RADEON_CARD_PCI;
  2668. break;
  2669. case RADEON_PARAM_VBLANK_CRTC:
  2670. value = radeon_vblank_crtc_get(dev);
  2671. break;
  2672. case RADEON_PARAM_FB_LOCATION:
  2673. value = radeon_read_fb_location(dev_priv);
  2674. break;
  2675. case RADEON_PARAM_NUM_GB_PIPES:
  2676. value = dev_priv->num_gb_pipes;
  2677. break;
  2678. case RADEON_PARAM_NUM_Z_PIPES:
  2679. value = dev_priv->num_z_pipes;
  2680. break;
  2681. default:
  2682. DRM_DEBUG("Invalid parameter %d\n", param->param);
  2683. return -EINVAL;
  2684. }
  2685. if (copy_to_user(param->value, &value, sizeof(int))) {
  2686. DRM_ERROR("copy_to_user\n");
  2687. return -EFAULT;
  2688. }
  2689. return 0;
  2690. }
  2691. static int radeon_cp_setparam(struct drm_device *dev, void *data, struct drm_file *file_priv)
  2692. {
  2693. drm_radeon_private_t *dev_priv = dev->dev_private;
  2694. struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
  2695. drm_radeon_setparam_t *sp = data;
  2696. struct drm_radeon_driver_file_fields *radeon_priv;
  2697. switch (sp->param) {
  2698. case RADEON_SETPARAM_FB_LOCATION:
  2699. radeon_priv = file_priv->driver_priv;
  2700. radeon_priv->radeon_fb_delta = dev_priv->fb_location -
  2701. sp->value;
  2702. break;
  2703. case RADEON_SETPARAM_SWITCH_TILING:
  2704. if (sp->value == 0) {
  2705. DRM_DEBUG("color tiling disabled\n");
  2706. dev_priv->front_pitch_offset &= ~RADEON_DST_TILE_MACRO;
  2707. dev_priv->back_pitch_offset &= ~RADEON_DST_TILE_MACRO;
  2708. if (master_priv->sarea_priv)
  2709. master_priv->sarea_priv->tiling_enabled = 0;
  2710. } else if (sp->value == 1) {
  2711. DRM_DEBUG("color tiling enabled\n");
  2712. dev_priv->front_pitch_offset |= RADEON_DST_TILE_MACRO;
  2713. dev_priv->back_pitch_offset |= RADEON_DST_TILE_MACRO;
  2714. if (master_priv->sarea_priv)
  2715. master_priv->sarea_priv->tiling_enabled = 1;
  2716. }
  2717. break;
  2718. case RADEON_SETPARAM_PCIGART_LOCATION:
  2719. dev_priv->pcigart_offset = sp->value;
  2720. dev_priv->pcigart_offset_set = 1;
  2721. break;
  2722. case RADEON_SETPARAM_NEW_MEMMAP:
  2723. dev_priv->new_memmap = sp->value;
  2724. break;
  2725. case RADEON_SETPARAM_PCIGART_TABLE_SIZE:
  2726. dev_priv->gart_info.table_size = sp->value;
  2727. if (dev_priv->gart_info.table_size < RADEON_PCIGART_TABLE_SIZE)
  2728. dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
  2729. break;
  2730. case RADEON_SETPARAM_VBLANK_CRTC:
  2731. return radeon_vblank_crtc_set(dev, sp->value);
  2732. break;
  2733. default:
  2734. DRM_DEBUG("Invalid parameter %d\n", sp->param);
  2735. return -EINVAL;
  2736. }
  2737. return 0;
  2738. }
  2739. /* When a client dies:
  2740. * - Check for and clean up flipped page state
  2741. * - Free any alloced GART memory.
  2742. * - Free any alloced radeon surfaces.
  2743. *
  2744. * DRM infrastructure takes care of reclaiming dma buffers.
  2745. */
  2746. void radeon_driver_preclose(struct drm_device *dev, struct drm_file *file_priv)
  2747. {
  2748. if (dev->dev_private) {
  2749. drm_radeon_private_t *dev_priv = dev->dev_private;
  2750. dev_priv->page_flipping = 0;
  2751. radeon_mem_release(file_priv, dev_priv->gart_heap);
  2752. radeon_mem_release(file_priv, dev_priv->fb_heap);
  2753. radeon_surfaces_release(file_priv, dev_priv);
  2754. }
  2755. }
  2756. void radeon_driver_lastclose(struct drm_device *dev)
  2757. {
  2758. radeon_surfaces_release(PCIGART_FILE_PRIV, dev->dev_private);
  2759. radeon_do_release(dev);
  2760. }
  2761. int radeon_driver_open(struct drm_device *dev, struct drm_file *file_priv)
  2762. {
  2763. drm_radeon_private_t *dev_priv = dev->dev_private;
  2764. struct drm_radeon_driver_file_fields *radeon_priv;
  2765. DRM_DEBUG("\n");
  2766. radeon_priv = kmalloc(sizeof(*radeon_priv), GFP_KERNEL);
  2767. if (!radeon_priv)
  2768. return -ENOMEM;
  2769. file_priv->driver_priv = radeon_priv;
  2770. if (dev_priv)
  2771. radeon_priv->radeon_fb_delta = dev_priv->fb_location;
  2772. else
  2773. radeon_priv->radeon_fb_delta = 0;
  2774. return 0;
  2775. }
  2776. void radeon_driver_postclose(struct drm_device *dev, struct drm_file *file_priv)
  2777. {
  2778. struct drm_radeon_driver_file_fields *radeon_priv =
  2779. file_priv->driver_priv;
  2780. kfree(radeon_priv);
  2781. }
  2782. struct drm_ioctl_desc radeon_ioctls[] = {
  2783. DRM_IOCTL_DEF_DRV(RADEON_CP_INIT, radeon_cp_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2784. DRM_IOCTL_DEF_DRV(RADEON_CP_START, radeon_cp_start, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2785. DRM_IOCTL_DEF_DRV(RADEON_CP_STOP, radeon_cp_stop, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2786. DRM_IOCTL_DEF_DRV(RADEON_CP_RESET, radeon_cp_reset, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2787. DRM_IOCTL_DEF_DRV(RADEON_CP_IDLE, radeon_cp_idle, DRM_AUTH),
  2788. DRM_IOCTL_DEF_DRV(RADEON_CP_RESUME, radeon_cp_resume, DRM_AUTH),
  2789. DRM_IOCTL_DEF_DRV(RADEON_RESET, radeon_engine_reset, DRM_AUTH),
  2790. DRM_IOCTL_DEF_DRV(RADEON_FULLSCREEN, radeon_fullscreen, DRM_AUTH),
  2791. DRM_IOCTL_DEF_DRV(RADEON_SWAP, radeon_cp_swap, DRM_AUTH),
  2792. DRM_IOCTL_DEF_DRV(RADEON_CLEAR, radeon_cp_clear, DRM_AUTH),
  2793. DRM_IOCTL_DEF_DRV(RADEON_VERTEX, radeon_cp_vertex, DRM_AUTH),
  2794. DRM_IOCTL_DEF_DRV(RADEON_INDICES, radeon_cp_indices, DRM_AUTH),
  2795. DRM_IOCTL_DEF_DRV(RADEON_TEXTURE, radeon_cp_texture, DRM_AUTH),
  2796. DRM_IOCTL_DEF_DRV(RADEON_STIPPLE, radeon_cp_stipple, DRM_AUTH),
  2797. DRM_IOCTL_DEF_DRV(RADEON_INDIRECT, radeon_cp_indirect, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2798. DRM_IOCTL_DEF_DRV(RADEON_VERTEX2, radeon_cp_vertex2, DRM_AUTH),
  2799. DRM_IOCTL_DEF_DRV(RADEON_CMDBUF, radeon_cp_cmdbuf, DRM_AUTH),
  2800. DRM_IOCTL_DEF_DRV(RADEON_GETPARAM, radeon_cp_getparam, DRM_AUTH),
  2801. DRM_IOCTL_DEF_DRV(RADEON_FLIP, radeon_cp_flip, DRM_AUTH),
  2802. DRM_IOCTL_DEF_DRV(RADEON_ALLOC, radeon_mem_alloc, DRM_AUTH),
  2803. DRM_IOCTL_DEF_DRV(RADEON_FREE, radeon_mem_free, DRM_AUTH),
  2804. DRM_IOCTL_DEF_DRV(RADEON_INIT_HEAP, radeon_mem_init_heap, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  2805. DRM_IOCTL_DEF_DRV(RADEON_IRQ_EMIT, radeon_irq_emit, DRM_AUTH),
  2806. DRM_IOCTL_DEF_DRV(RADEON_IRQ_WAIT, radeon_irq_wait, DRM_AUTH),
  2807. DRM_IOCTL_DEF_DRV(RADEON_SETPARAM, radeon_cp_setparam, DRM_AUTH),
  2808. DRM_IOCTL_DEF_DRV(RADEON_SURF_ALLOC, radeon_surface_alloc, DRM_AUTH),
  2809. DRM_IOCTL_DEF_DRV(RADEON_SURF_FREE, radeon_surface_free, DRM_AUTH),
  2810. DRM_IOCTL_DEF_DRV(RADEON_CS, r600_cs_legacy_ioctl, DRM_AUTH)
  2811. };
  2812. int radeon_max_ioctl = ARRAY_SIZE(radeon_ioctls);