r100.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/seq_file.h>
  29. #include <linux/slab.h>
  30. #include <drm/drmP.h>
  31. #include <drm/radeon_drm.h>
  32. #include "radeon_reg.h"
  33. #include "radeon.h"
  34. #include "radeon_asic.h"
  35. #include "r100d.h"
  36. #include "rs100d.h"
  37. #include "rv200d.h"
  38. #include "rv250d.h"
  39. #include "atom.h"
  40. #include <linux/firmware.h>
  41. #include <linux/module.h>
  42. #include "r100_reg_safe.h"
  43. #include "rn50_reg_safe.h"
  44. /* Firmware Names */
  45. #define FIRMWARE_R100 "radeon/R100_cp.bin"
  46. #define FIRMWARE_R200 "radeon/R200_cp.bin"
  47. #define FIRMWARE_R300 "radeon/R300_cp.bin"
  48. #define FIRMWARE_R420 "radeon/R420_cp.bin"
  49. #define FIRMWARE_RS690 "radeon/RS690_cp.bin"
  50. #define FIRMWARE_RS600 "radeon/RS600_cp.bin"
  51. #define FIRMWARE_R520 "radeon/R520_cp.bin"
  52. MODULE_FIRMWARE(FIRMWARE_R100);
  53. MODULE_FIRMWARE(FIRMWARE_R200);
  54. MODULE_FIRMWARE(FIRMWARE_R300);
  55. MODULE_FIRMWARE(FIRMWARE_R420);
  56. MODULE_FIRMWARE(FIRMWARE_RS690);
  57. MODULE_FIRMWARE(FIRMWARE_RS600);
  58. MODULE_FIRMWARE(FIRMWARE_R520);
  59. #include "r100_track.h"
  60. /* This files gather functions specifics to:
  61. * r100,rv100,rs100,rv200,rs200,r200,rv250,rs300,rv280
  62. * and others in some cases.
  63. */
  64. static bool r100_is_in_vblank(struct radeon_device *rdev, int crtc)
  65. {
  66. if (crtc == 0) {
  67. if (RREG32(RADEON_CRTC_STATUS) & RADEON_CRTC_VBLANK_CUR)
  68. return true;
  69. else
  70. return false;
  71. } else {
  72. if (RREG32(RADEON_CRTC2_STATUS) & RADEON_CRTC2_VBLANK_CUR)
  73. return true;
  74. else
  75. return false;
  76. }
  77. }
  78. static bool r100_is_counter_moving(struct radeon_device *rdev, int crtc)
  79. {
  80. u32 vline1, vline2;
  81. if (crtc == 0) {
  82. vline1 = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  83. vline2 = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  84. } else {
  85. vline1 = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  86. vline2 = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  87. }
  88. if (vline1 != vline2)
  89. return true;
  90. else
  91. return false;
  92. }
  93. /**
  94. * r100_wait_for_vblank - vblank wait asic callback.
  95. *
  96. * @rdev: radeon_device pointer
  97. * @crtc: crtc to wait for vblank on
  98. *
  99. * Wait for vblank on the requested crtc (r1xx-r4xx).
  100. */
  101. void r100_wait_for_vblank(struct radeon_device *rdev, int crtc)
  102. {
  103. unsigned i = 0;
  104. if (crtc >= rdev->num_crtc)
  105. return;
  106. if (crtc == 0) {
  107. if (!(RREG32(RADEON_CRTC_GEN_CNTL) & RADEON_CRTC_EN))
  108. return;
  109. } else {
  110. if (!(RREG32(RADEON_CRTC2_GEN_CNTL) & RADEON_CRTC2_EN))
  111. return;
  112. }
  113. /* depending on when we hit vblank, we may be close to active; if so,
  114. * wait for another frame.
  115. */
  116. while (r100_is_in_vblank(rdev, crtc)) {
  117. if (i++ % 100 == 0) {
  118. if (!r100_is_counter_moving(rdev, crtc))
  119. break;
  120. }
  121. }
  122. while (!r100_is_in_vblank(rdev, crtc)) {
  123. if (i++ % 100 == 0) {
  124. if (!r100_is_counter_moving(rdev, crtc))
  125. break;
  126. }
  127. }
  128. }
  129. /**
  130. * r100_page_flip - pageflip callback.
  131. *
  132. * @rdev: radeon_device pointer
  133. * @crtc_id: crtc to cleanup pageflip on
  134. * @crtc_base: new address of the crtc (GPU MC address)
  135. *
  136. * Does the actual pageflip (r1xx-r4xx).
  137. * During vblank we take the crtc lock and wait for the update_pending
  138. * bit to go high, when it does, we release the lock, and allow the
  139. * double buffered update to take place.
  140. */
  141. void r100_page_flip(struct radeon_device *rdev, int crtc_id, u64 crtc_base)
  142. {
  143. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
  144. u32 tmp = ((u32)crtc_base) | RADEON_CRTC_OFFSET__OFFSET_LOCK;
  145. int i;
  146. /* Lock the graphics update lock */
  147. /* update the scanout addresses */
  148. WREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset, tmp);
  149. /* Wait for update_pending to go high. */
  150. for (i = 0; i < rdev->usec_timeout; i++) {
  151. if (RREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset) & RADEON_CRTC_OFFSET__GUI_TRIG_OFFSET)
  152. break;
  153. udelay(1);
  154. }
  155. DRM_DEBUG("Update pending now high. Unlocking vupdate_lock.\n");
  156. /* Unlock the lock, so double-buffering can take place inside vblank */
  157. tmp &= ~RADEON_CRTC_OFFSET__OFFSET_LOCK;
  158. WREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset, tmp);
  159. }
  160. /**
  161. * r100_page_flip_pending - check if page flip is still pending
  162. *
  163. * @rdev: radeon_device pointer
  164. * @crtc_id: crtc to check
  165. *
  166. * Check if the last pagefilp is still pending (r1xx-r4xx).
  167. * Returns the current update pending status.
  168. */
  169. bool r100_page_flip_pending(struct radeon_device *rdev, int crtc_id)
  170. {
  171. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
  172. /* Return current update_pending status: */
  173. return !!(RREG32(RADEON_CRTC_OFFSET + radeon_crtc->crtc_offset) &
  174. RADEON_CRTC_OFFSET__GUI_TRIG_OFFSET);
  175. }
  176. /**
  177. * r100_pm_get_dynpm_state - look up dynpm power state callback.
  178. *
  179. * @rdev: radeon_device pointer
  180. *
  181. * Look up the optimal power state based on the
  182. * current state of the GPU (r1xx-r5xx).
  183. * Used for dynpm only.
  184. */
  185. void r100_pm_get_dynpm_state(struct radeon_device *rdev)
  186. {
  187. int i;
  188. rdev->pm.dynpm_can_upclock = true;
  189. rdev->pm.dynpm_can_downclock = true;
  190. switch (rdev->pm.dynpm_planned_action) {
  191. case DYNPM_ACTION_MINIMUM:
  192. rdev->pm.requested_power_state_index = 0;
  193. rdev->pm.dynpm_can_downclock = false;
  194. break;
  195. case DYNPM_ACTION_DOWNCLOCK:
  196. if (rdev->pm.current_power_state_index == 0) {
  197. rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
  198. rdev->pm.dynpm_can_downclock = false;
  199. } else {
  200. if (rdev->pm.active_crtc_count > 1) {
  201. for (i = 0; i < rdev->pm.num_power_states; i++) {
  202. if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
  203. continue;
  204. else if (i >= rdev->pm.current_power_state_index) {
  205. rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
  206. break;
  207. } else {
  208. rdev->pm.requested_power_state_index = i;
  209. break;
  210. }
  211. }
  212. } else
  213. rdev->pm.requested_power_state_index =
  214. rdev->pm.current_power_state_index - 1;
  215. }
  216. /* don't use the power state if crtcs are active and no display flag is set */
  217. if ((rdev->pm.active_crtc_count > 0) &&
  218. (rdev->pm.power_state[rdev->pm.requested_power_state_index].clock_info[0].flags &
  219. RADEON_PM_MODE_NO_DISPLAY)) {
  220. rdev->pm.requested_power_state_index++;
  221. }
  222. break;
  223. case DYNPM_ACTION_UPCLOCK:
  224. if (rdev->pm.current_power_state_index == (rdev->pm.num_power_states - 1)) {
  225. rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
  226. rdev->pm.dynpm_can_upclock = false;
  227. } else {
  228. if (rdev->pm.active_crtc_count > 1) {
  229. for (i = (rdev->pm.num_power_states - 1); i >= 0; i--) {
  230. if (rdev->pm.power_state[i].flags & RADEON_PM_STATE_SINGLE_DISPLAY_ONLY)
  231. continue;
  232. else if (i <= rdev->pm.current_power_state_index) {
  233. rdev->pm.requested_power_state_index = rdev->pm.current_power_state_index;
  234. break;
  235. } else {
  236. rdev->pm.requested_power_state_index = i;
  237. break;
  238. }
  239. }
  240. } else
  241. rdev->pm.requested_power_state_index =
  242. rdev->pm.current_power_state_index + 1;
  243. }
  244. break;
  245. case DYNPM_ACTION_DEFAULT:
  246. rdev->pm.requested_power_state_index = rdev->pm.default_power_state_index;
  247. rdev->pm.dynpm_can_upclock = false;
  248. break;
  249. case DYNPM_ACTION_NONE:
  250. default:
  251. DRM_ERROR("Requested mode for not defined action\n");
  252. return;
  253. }
  254. /* only one clock mode per power state */
  255. rdev->pm.requested_clock_mode_index = 0;
  256. DRM_DEBUG_DRIVER("Requested: e: %d m: %d p: %d\n",
  257. rdev->pm.power_state[rdev->pm.requested_power_state_index].
  258. clock_info[rdev->pm.requested_clock_mode_index].sclk,
  259. rdev->pm.power_state[rdev->pm.requested_power_state_index].
  260. clock_info[rdev->pm.requested_clock_mode_index].mclk,
  261. rdev->pm.power_state[rdev->pm.requested_power_state_index].
  262. pcie_lanes);
  263. }
  264. /**
  265. * r100_pm_init_profile - Initialize power profiles callback.
  266. *
  267. * @rdev: radeon_device pointer
  268. *
  269. * Initialize the power states used in profile mode
  270. * (r1xx-r3xx).
  271. * Used for profile mode only.
  272. */
  273. void r100_pm_init_profile(struct radeon_device *rdev)
  274. {
  275. /* default */
  276. rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_ps_idx = rdev->pm.default_power_state_index;
  277. rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
  278. rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_off_cm_idx = 0;
  279. rdev->pm.profiles[PM_PROFILE_DEFAULT_IDX].dpms_on_cm_idx = 0;
  280. /* low sh */
  281. rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_ps_idx = 0;
  282. rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_ps_idx = 0;
  283. rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_off_cm_idx = 0;
  284. rdev->pm.profiles[PM_PROFILE_LOW_SH_IDX].dpms_on_cm_idx = 0;
  285. /* mid sh */
  286. rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_ps_idx = 0;
  287. rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_ps_idx = 0;
  288. rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_off_cm_idx = 0;
  289. rdev->pm.profiles[PM_PROFILE_MID_SH_IDX].dpms_on_cm_idx = 0;
  290. /* high sh */
  291. rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_ps_idx = 0;
  292. rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
  293. rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_off_cm_idx = 0;
  294. rdev->pm.profiles[PM_PROFILE_HIGH_SH_IDX].dpms_on_cm_idx = 0;
  295. /* low mh */
  296. rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_ps_idx = 0;
  297. rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
  298. rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_off_cm_idx = 0;
  299. rdev->pm.profiles[PM_PROFILE_LOW_MH_IDX].dpms_on_cm_idx = 0;
  300. /* mid mh */
  301. rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_ps_idx = 0;
  302. rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
  303. rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_off_cm_idx = 0;
  304. rdev->pm.profiles[PM_PROFILE_MID_MH_IDX].dpms_on_cm_idx = 0;
  305. /* high mh */
  306. rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_ps_idx = 0;
  307. rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_ps_idx = rdev->pm.default_power_state_index;
  308. rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_off_cm_idx = 0;
  309. rdev->pm.profiles[PM_PROFILE_HIGH_MH_IDX].dpms_on_cm_idx = 0;
  310. }
  311. /**
  312. * r100_pm_misc - set additional pm hw parameters callback.
  313. *
  314. * @rdev: radeon_device pointer
  315. *
  316. * Set non-clock parameters associated with a power state
  317. * (voltage, pcie lanes, etc.) (r1xx-r4xx).
  318. */
  319. void r100_pm_misc(struct radeon_device *rdev)
  320. {
  321. int requested_index = rdev->pm.requested_power_state_index;
  322. struct radeon_power_state *ps = &rdev->pm.power_state[requested_index];
  323. struct radeon_voltage *voltage = &ps->clock_info[0].voltage;
  324. u32 tmp, sclk_cntl, sclk_cntl2, sclk_more_cntl;
  325. if ((voltage->type == VOLTAGE_GPIO) && (voltage->gpio.valid)) {
  326. if (ps->misc & ATOM_PM_MISCINFO_VOLTAGE_DROP_SUPPORT) {
  327. tmp = RREG32(voltage->gpio.reg);
  328. if (voltage->active_high)
  329. tmp |= voltage->gpio.mask;
  330. else
  331. tmp &= ~(voltage->gpio.mask);
  332. WREG32(voltage->gpio.reg, tmp);
  333. if (voltage->delay)
  334. udelay(voltage->delay);
  335. } else {
  336. tmp = RREG32(voltage->gpio.reg);
  337. if (voltage->active_high)
  338. tmp &= ~voltage->gpio.mask;
  339. else
  340. tmp |= voltage->gpio.mask;
  341. WREG32(voltage->gpio.reg, tmp);
  342. if (voltage->delay)
  343. udelay(voltage->delay);
  344. }
  345. }
  346. sclk_cntl = RREG32_PLL(SCLK_CNTL);
  347. sclk_cntl2 = RREG32_PLL(SCLK_CNTL2);
  348. sclk_cntl2 &= ~REDUCED_SPEED_SCLK_SEL(3);
  349. sclk_more_cntl = RREG32_PLL(SCLK_MORE_CNTL);
  350. sclk_more_cntl &= ~VOLTAGE_DELAY_SEL(3);
  351. if (ps->misc & ATOM_PM_MISCINFO_ASIC_REDUCED_SPEED_SCLK_EN) {
  352. sclk_more_cntl |= REDUCED_SPEED_SCLK_EN;
  353. if (ps->misc & ATOM_PM_MISCINFO_DYN_CLK_3D_IDLE)
  354. sclk_cntl2 |= REDUCED_SPEED_SCLK_MODE;
  355. else
  356. sclk_cntl2 &= ~REDUCED_SPEED_SCLK_MODE;
  357. if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_CLOCK_DIVIDER_BY_2)
  358. sclk_cntl2 |= REDUCED_SPEED_SCLK_SEL(0);
  359. else if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_CLOCK_DIVIDER_BY_4)
  360. sclk_cntl2 |= REDUCED_SPEED_SCLK_SEL(2);
  361. } else
  362. sclk_more_cntl &= ~REDUCED_SPEED_SCLK_EN;
  363. if (ps->misc & ATOM_PM_MISCINFO_ASIC_DYNAMIC_VOLTAGE_EN) {
  364. sclk_more_cntl |= IO_CG_VOLTAGE_DROP;
  365. if (voltage->delay) {
  366. sclk_more_cntl |= VOLTAGE_DROP_SYNC;
  367. switch (voltage->delay) {
  368. case 33:
  369. sclk_more_cntl |= VOLTAGE_DELAY_SEL(0);
  370. break;
  371. case 66:
  372. sclk_more_cntl |= VOLTAGE_DELAY_SEL(1);
  373. break;
  374. case 99:
  375. sclk_more_cntl |= VOLTAGE_DELAY_SEL(2);
  376. break;
  377. case 132:
  378. sclk_more_cntl |= VOLTAGE_DELAY_SEL(3);
  379. break;
  380. }
  381. } else
  382. sclk_more_cntl &= ~VOLTAGE_DROP_SYNC;
  383. } else
  384. sclk_more_cntl &= ~IO_CG_VOLTAGE_DROP;
  385. if (ps->misc & ATOM_PM_MISCINFO_DYNAMIC_HDP_BLOCK_EN)
  386. sclk_cntl &= ~FORCE_HDP;
  387. else
  388. sclk_cntl |= FORCE_HDP;
  389. WREG32_PLL(SCLK_CNTL, sclk_cntl);
  390. WREG32_PLL(SCLK_CNTL2, sclk_cntl2);
  391. WREG32_PLL(SCLK_MORE_CNTL, sclk_more_cntl);
  392. /* set pcie lanes */
  393. if ((rdev->flags & RADEON_IS_PCIE) &&
  394. !(rdev->flags & RADEON_IS_IGP) &&
  395. rdev->asic->pm.set_pcie_lanes &&
  396. (ps->pcie_lanes !=
  397. rdev->pm.power_state[rdev->pm.current_power_state_index].pcie_lanes)) {
  398. radeon_set_pcie_lanes(rdev,
  399. ps->pcie_lanes);
  400. DRM_DEBUG_DRIVER("Setting: p: %d\n", ps->pcie_lanes);
  401. }
  402. }
  403. /**
  404. * r100_pm_prepare - pre-power state change callback.
  405. *
  406. * @rdev: radeon_device pointer
  407. *
  408. * Prepare for a power state change (r1xx-r4xx).
  409. */
  410. void r100_pm_prepare(struct radeon_device *rdev)
  411. {
  412. struct drm_device *ddev = rdev->ddev;
  413. struct drm_crtc *crtc;
  414. struct radeon_crtc *radeon_crtc;
  415. u32 tmp;
  416. /* disable any active CRTCs */
  417. list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
  418. radeon_crtc = to_radeon_crtc(crtc);
  419. if (radeon_crtc->enabled) {
  420. if (radeon_crtc->crtc_id) {
  421. tmp = RREG32(RADEON_CRTC2_GEN_CNTL);
  422. tmp |= RADEON_CRTC2_DISP_REQ_EN_B;
  423. WREG32(RADEON_CRTC2_GEN_CNTL, tmp);
  424. } else {
  425. tmp = RREG32(RADEON_CRTC_GEN_CNTL);
  426. tmp |= RADEON_CRTC_DISP_REQ_EN_B;
  427. WREG32(RADEON_CRTC_GEN_CNTL, tmp);
  428. }
  429. }
  430. }
  431. }
  432. /**
  433. * r100_pm_finish - post-power state change callback.
  434. *
  435. * @rdev: radeon_device pointer
  436. *
  437. * Clean up after a power state change (r1xx-r4xx).
  438. */
  439. void r100_pm_finish(struct radeon_device *rdev)
  440. {
  441. struct drm_device *ddev = rdev->ddev;
  442. struct drm_crtc *crtc;
  443. struct radeon_crtc *radeon_crtc;
  444. u32 tmp;
  445. /* enable any active CRTCs */
  446. list_for_each_entry(crtc, &ddev->mode_config.crtc_list, head) {
  447. radeon_crtc = to_radeon_crtc(crtc);
  448. if (radeon_crtc->enabled) {
  449. if (radeon_crtc->crtc_id) {
  450. tmp = RREG32(RADEON_CRTC2_GEN_CNTL);
  451. tmp &= ~RADEON_CRTC2_DISP_REQ_EN_B;
  452. WREG32(RADEON_CRTC2_GEN_CNTL, tmp);
  453. } else {
  454. tmp = RREG32(RADEON_CRTC_GEN_CNTL);
  455. tmp &= ~RADEON_CRTC_DISP_REQ_EN_B;
  456. WREG32(RADEON_CRTC_GEN_CNTL, tmp);
  457. }
  458. }
  459. }
  460. }
  461. /**
  462. * r100_gui_idle - gui idle callback.
  463. *
  464. * @rdev: radeon_device pointer
  465. *
  466. * Check of the GUI (2D/3D engines) are idle (r1xx-r5xx).
  467. * Returns true if idle, false if not.
  468. */
  469. bool r100_gui_idle(struct radeon_device *rdev)
  470. {
  471. if (RREG32(RADEON_RBBM_STATUS) & RADEON_RBBM_ACTIVE)
  472. return false;
  473. else
  474. return true;
  475. }
  476. /* hpd for digital panel detect/disconnect */
  477. /**
  478. * r100_hpd_sense - hpd sense callback.
  479. *
  480. * @rdev: radeon_device pointer
  481. * @hpd: hpd (hotplug detect) pin
  482. *
  483. * Checks if a digital monitor is connected (r1xx-r4xx).
  484. * Returns true if connected, false if not connected.
  485. */
  486. bool r100_hpd_sense(struct radeon_device *rdev, enum radeon_hpd_id hpd)
  487. {
  488. bool connected = false;
  489. switch (hpd) {
  490. case RADEON_HPD_1:
  491. if (RREG32(RADEON_FP_GEN_CNTL) & RADEON_FP_DETECT_SENSE)
  492. connected = true;
  493. break;
  494. case RADEON_HPD_2:
  495. if (RREG32(RADEON_FP2_GEN_CNTL) & RADEON_FP2_DETECT_SENSE)
  496. connected = true;
  497. break;
  498. default:
  499. break;
  500. }
  501. return connected;
  502. }
  503. /**
  504. * r100_hpd_set_polarity - hpd set polarity callback.
  505. *
  506. * @rdev: radeon_device pointer
  507. * @hpd: hpd (hotplug detect) pin
  508. *
  509. * Set the polarity of the hpd pin (r1xx-r4xx).
  510. */
  511. void r100_hpd_set_polarity(struct radeon_device *rdev,
  512. enum radeon_hpd_id hpd)
  513. {
  514. u32 tmp;
  515. bool connected = r100_hpd_sense(rdev, hpd);
  516. switch (hpd) {
  517. case RADEON_HPD_1:
  518. tmp = RREG32(RADEON_FP_GEN_CNTL);
  519. if (connected)
  520. tmp &= ~RADEON_FP_DETECT_INT_POL;
  521. else
  522. tmp |= RADEON_FP_DETECT_INT_POL;
  523. WREG32(RADEON_FP_GEN_CNTL, tmp);
  524. break;
  525. case RADEON_HPD_2:
  526. tmp = RREG32(RADEON_FP2_GEN_CNTL);
  527. if (connected)
  528. tmp &= ~RADEON_FP2_DETECT_INT_POL;
  529. else
  530. tmp |= RADEON_FP2_DETECT_INT_POL;
  531. WREG32(RADEON_FP2_GEN_CNTL, tmp);
  532. break;
  533. default:
  534. break;
  535. }
  536. }
  537. /**
  538. * r100_hpd_init - hpd setup callback.
  539. *
  540. * @rdev: radeon_device pointer
  541. *
  542. * Setup the hpd pins used by the card (r1xx-r4xx).
  543. * Set the polarity, and enable the hpd interrupts.
  544. */
  545. void r100_hpd_init(struct radeon_device *rdev)
  546. {
  547. struct drm_device *dev = rdev->ddev;
  548. struct drm_connector *connector;
  549. unsigned enable = 0;
  550. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  551. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  552. enable |= 1 << radeon_connector->hpd.hpd;
  553. radeon_hpd_set_polarity(rdev, radeon_connector->hpd.hpd);
  554. }
  555. radeon_irq_kms_enable_hpd(rdev, enable);
  556. }
  557. /**
  558. * r100_hpd_fini - hpd tear down callback.
  559. *
  560. * @rdev: radeon_device pointer
  561. *
  562. * Tear down the hpd pins used by the card (r1xx-r4xx).
  563. * Disable the hpd interrupts.
  564. */
  565. void r100_hpd_fini(struct radeon_device *rdev)
  566. {
  567. struct drm_device *dev = rdev->ddev;
  568. struct drm_connector *connector;
  569. unsigned disable = 0;
  570. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  571. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  572. disable |= 1 << radeon_connector->hpd.hpd;
  573. }
  574. radeon_irq_kms_disable_hpd(rdev, disable);
  575. }
  576. /*
  577. * PCI GART
  578. */
  579. void r100_pci_gart_tlb_flush(struct radeon_device *rdev)
  580. {
  581. /* TODO: can we do somethings here ? */
  582. /* It seems hw only cache one entry so we should discard this
  583. * entry otherwise if first GPU GART read hit this entry it
  584. * could end up in wrong address. */
  585. }
  586. int r100_pci_gart_init(struct radeon_device *rdev)
  587. {
  588. int r;
  589. if (rdev->gart.ptr) {
  590. WARN(1, "R100 PCI GART already initialized\n");
  591. return 0;
  592. }
  593. /* Initialize common gart structure */
  594. r = radeon_gart_init(rdev);
  595. if (r)
  596. return r;
  597. rdev->gart.table_size = rdev->gart.num_gpu_pages * 4;
  598. rdev->asic->gart.tlb_flush = &r100_pci_gart_tlb_flush;
  599. rdev->asic->gart.get_page_entry = &r100_pci_gart_get_page_entry;
  600. rdev->asic->gart.set_page = &r100_pci_gart_set_page;
  601. return radeon_gart_table_ram_alloc(rdev);
  602. }
  603. int r100_pci_gart_enable(struct radeon_device *rdev)
  604. {
  605. uint32_t tmp;
  606. /* discard memory request outside of configured range */
  607. tmp = RREG32(RADEON_AIC_CNTL) | RADEON_DIS_OUT_OF_PCI_GART_ACCESS;
  608. WREG32(RADEON_AIC_CNTL, tmp);
  609. /* set address range for PCI address translate */
  610. WREG32(RADEON_AIC_LO_ADDR, rdev->mc.gtt_start);
  611. WREG32(RADEON_AIC_HI_ADDR, rdev->mc.gtt_end);
  612. /* set PCI GART page-table base address */
  613. WREG32(RADEON_AIC_PT_BASE, rdev->gart.table_addr);
  614. tmp = RREG32(RADEON_AIC_CNTL) | RADEON_PCIGART_TRANSLATE_EN;
  615. WREG32(RADEON_AIC_CNTL, tmp);
  616. r100_pci_gart_tlb_flush(rdev);
  617. DRM_INFO("PCI GART of %uM enabled (table at 0x%016llX).\n",
  618. (unsigned)(rdev->mc.gtt_size >> 20),
  619. (unsigned long long)rdev->gart.table_addr);
  620. rdev->gart.ready = true;
  621. return 0;
  622. }
  623. void r100_pci_gart_disable(struct radeon_device *rdev)
  624. {
  625. uint32_t tmp;
  626. /* discard memory request outside of configured range */
  627. tmp = RREG32(RADEON_AIC_CNTL) | RADEON_DIS_OUT_OF_PCI_GART_ACCESS;
  628. WREG32(RADEON_AIC_CNTL, tmp & ~RADEON_PCIGART_TRANSLATE_EN);
  629. WREG32(RADEON_AIC_LO_ADDR, 0);
  630. WREG32(RADEON_AIC_HI_ADDR, 0);
  631. }
  632. uint64_t r100_pci_gart_get_page_entry(uint64_t addr, uint32_t flags)
  633. {
  634. return addr;
  635. }
  636. void r100_pci_gart_set_page(struct radeon_device *rdev, unsigned i,
  637. uint64_t entry)
  638. {
  639. u32 *gtt = rdev->gart.ptr;
  640. gtt[i] = cpu_to_le32(lower_32_bits(entry));
  641. }
  642. void r100_pci_gart_fini(struct radeon_device *rdev)
  643. {
  644. radeon_gart_fini(rdev);
  645. r100_pci_gart_disable(rdev);
  646. radeon_gart_table_ram_free(rdev);
  647. }
  648. int r100_irq_set(struct radeon_device *rdev)
  649. {
  650. uint32_t tmp = 0;
  651. if (!rdev->irq.installed) {
  652. WARN(1, "Can't enable IRQ/MSI because no handler is installed\n");
  653. WREG32(R_000040_GEN_INT_CNTL, 0);
  654. return -EINVAL;
  655. }
  656. if (atomic_read(&rdev->irq.ring_int[RADEON_RING_TYPE_GFX_INDEX])) {
  657. tmp |= RADEON_SW_INT_ENABLE;
  658. }
  659. if (rdev->irq.crtc_vblank_int[0] ||
  660. atomic_read(&rdev->irq.pflip[0])) {
  661. tmp |= RADEON_CRTC_VBLANK_MASK;
  662. }
  663. if (rdev->irq.crtc_vblank_int[1] ||
  664. atomic_read(&rdev->irq.pflip[1])) {
  665. tmp |= RADEON_CRTC2_VBLANK_MASK;
  666. }
  667. if (rdev->irq.hpd[0]) {
  668. tmp |= RADEON_FP_DETECT_MASK;
  669. }
  670. if (rdev->irq.hpd[1]) {
  671. tmp |= RADEON_FP2_DETECT_MASK;
  672. }
  673. WREG32(RADEON_GEN_INT_CNTL, tmp);
  674. /* read back to post the write */
  675. RREG32(RADEON_GEN_INT_CNTL);
  676. return 0;
  677. }
  678. void r100_irq_disable(struct radeon_device *rdev)
  679. {
  680. u32 tmp;
  681. WREG32(R_000040_GEN_INT_CNTL, 0);
  682. /* Wait and acknowledge irq */
  683. mdelay(1);
  684. tmp = RREG32(R_000044_GEN_INT_STATUS);
  685. WREG32(R_000044_GEN_INT_STATUS, tmp);
  686. }
  687. static uint32_t r100_irq_ack(struct radeon_device *rdev)
  688. {
  689. uint32_t irqs = RREG32(RADEON_GEN_INT_STATUS);
  690. uint32_t irq_mask = RADEON_SW_INT_TEST |
  691. RADEON_CRTC_VBLANK_STAT | RADEON_CRTC2_VBLANK_STAT |
  692. RADEON_FP_DETECT_STAT | RADEON_FP2_DETECT_STAT;
  693. if (irqs) {
  694. WREG32(RADEON_GEN_INT_STATUS, irqs);
  695. }
  696. return irqs & irq_mask;
  697. }
  698. int r100_irq_process(struct radeon_device *rdev)
  699. {
  700. uint32_t status, msi_rearm;
  701. bool queue_hotplug = false;
  702. status = r100_irq_ack(rdev);
  703. if (!status) {
  704. return IRQ_NONE;
  705. }
  706. if (rdev->shutdown) {
  707. return IRQ_NONE;
  708. }
  709. while (status) {
  710. /* SW interrupt */
  711. if (status & RADEON_SW_INT_TEST) {
  712. radeon_fence_process(rdev, RADEON_RING_TYPE_GFX_INDEX);
  713. }
  714. /* Vertical blank interrupts */
  715. if (status & RADEON_CRTC_VBLANK_STAT) {
  716. if (rdev->irq.crtc_vblank_int[0]) {
  717. drm_handle_vblank(rdev->ddev, 0);
  718. rdev->pm.vblank_sync = true;
  719. wake_up(&rdev->irq.vblank_queue);
  720. }
  721. if (atomic_read(&rdev->irq.pflip[0]))
  722. radeon_crtc_handle_vblank(rdev, 0);
  723. }
  724. if (status & RADEON_CRTC2_VBLANK_STAT) {
  725. if (rdev->irq.crtc_vblank_int[1]) {
  726. drm_handle_vblank(rdev->ddev, 1);
  727. rdev->pm.vblank_sync = true;
  728. wake_up(&rdev->irq.vblank_queue);
  729. }
  730. if (atomic_read(&rdev->irq.pflip[1]))
  731. radeon_crtc_handle_vblank(rdev, 1);
  732. }
  733. if (status & RADEON_FP_DETECT_STAT) {
  734. queue_hotplug = true;
  735. DRM_DEBUG("HPD1\n");
  736. }
  737. if (status & RADEON_FP2_DETECT_STAT) {
  738. queue_hotplug = true;
  739. DRM_DEBUG("HPD2\n");
  740. }
  741. status = r100_irq_ack(rdev);
  742. }
  743. if (queue_hotplug)
  744. schedule_delayed_work(&rdev->hotplug_work, 0);
  745. if (rdev->msi_enabled) {
  746. switch (rdev->family) {
  747. case CHIP_RS400:
  748. case CHIP_RS480:
  749. msi_rearm = RREG32(RADEON_AIC_CNTL) & ~RS400_MSI_REARM;
  750. WREG32(RADEON_AIC_CNTL, msi_rearm);
  751. WREG32(RADEON_AIC_CNTL, msi_rearm | RS400_MSI_REARM);
  752. break;
  753. default:
  754. WREG32(RADEON_MSI_REARM_EN, RV370_MSI_REARM_EN);
  755. break;
  756. }
  757. }
  758. return IRQ_HANDLED;
  759. }
  760. u32 r100_get_vblank_counter(struct radeon_device *rdev, int crtc)
  761. {
  762. if (crtc == 0)
  763. return RREG32(RADEON_CRTC_CRNT_FRAME);
  764. else
  765. return RREG32(RADEON_CRTC2_CRNT_FRAME);
  766. }
  767. /**
  768. * r100_ring_hdp_flush - flush Host Data Path via the ring buffer
  769. * rdev: radeon device structure
  770. * ring: ring buffer struct for emitting packets
  771. */
  772. static void r100_ring_hdp_flush(struct radeon_device *rdev, struct radeon_ring *ring)
  773. {
  774. radeon_ring_write(ring, PACKET0(RADEON_HOST_PATH_CNTL, 0));
  775. radeon_ring_write(ring, rdev->config.r100.hdp_cntl |
  776. RADEON_HDP_READ_BUFFER_INVALIDATE);
  777. radeon_ring_write(ring, PACKET0(RADEON_HOST_PATH_CNTL, 0));
  778. radeon_ring_write(ring, rdev->config.r100.hdp_cntl);
  779. }
  780. /* Who ever call radeon_fence_emit should call ring_lock and ask
  781. * for enough space (today caller are ib schedule and buffer move) */
  782. void r100_fence_ring_emit(struct radeon_device *rdev,
  783. struct radeon_fence *fence)
  784. {
  785. struct radeon_ring *ring = &rdev->ring[fence->ring];
  786. /* We have to make sure that caches are flushed before
  787. * CPU might read something from VRAM. */
  788. radeon_ring_write(ring, PACKET0(RADEON_RB3D_DSTCACHE_CTLSTAT, 0));
  789. radeon_ring_write(ring, RADEON_RB3D_DC_FLUSH_ALL);
  790. radeon_ring_write(ring, PACKET0(RADEON_RB3D_ZCACHE_CTLSTAT, 0));
  791. radeon_ring_write(ring, RADEON_RB3D_ZC_FLUSH_ALL);
  792. /* Wait until IDLE & CLEAN */
  793. radeon_ring_write(ring, PACKET0(RADEON_WAIT_UNTIL, 0));
  794. radeon_ring_write(ring, RADEON_WAIT_2D_IDLECLEAN | RADEON_WAIT_3D_IDLECLEAN);
  795. r100_ring_hdp_flush(rdev, ring);
  796. /* Emit fence sequence & fire IRQ */
  797. radeon_ring_write(ring, PACKET0(rdev->fence_drv[fence->ring].scratch_reg, 0));
  798. radeon_ring_write(ring, fence->seq);
  799. radeon_ring_write(ring, PACKET0(RADEON_GEN_INT_STATUS, 0));
  800. radeon_ring_write(ring, RADEON_SW_INT_FIRE);
  801. }
  802. bool r100_semaphore_ring_emit(struct radeon_device *rdev,
  803. struct radeon_ring *ring,
  804. struct radeon_semaphore *semaphore,
  805. bool emit_wait)
  806. {
  807. /* Unused on older asics, since we don't have semaphores or multiple rings */
  808. BUG();
  809. return false;
  810. }
  811. struct radeon_fence *r100_copy_blit(struct radeon_device *rdev,
  812. uint64_t src_offset,
  813. uint64_t dst_offset,
  814. unsigned num_gpu_pages,
  815. struct reservation_object *resv)
  816. {
  817. struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
  818. struct radeon_fence *fence;
  819. uint32_t cur_pages;
  820. uint32_t stride_bytes = RADEON_GPU_PAGE_SIZE;
  821. uint32_t pitch;
  822. uint32_t stride_pixels;
  823. unsigned ndw;
  824. int num_loops;
  825. int r = 0;
  826. /* radeon limited to 16k stride */
  827. stride_bytes &= 0x3fff;
  828. /* radeon pitch is /64 */
  829. pitch = stride_bytes / 64;
  830. stride_pixels = stride_bytes / 4;
  831. num_loops = DIV_ROUND_UP(num_gpu_pages, 8191);
  832. /* Ask for enough room for blit + flush + fence */
  833. ndw = 64 + (10 * num_loops);
  834. r = radeon_ring_lock(rdev, ring, ndw);
  835. if (r) {
  836. DRM_ERROR("radeon: moving bo (%d) asking for %u dw.\n", r, ndw);
  837. return ERR_PTR(-EINVAL);
  838. }
  839. while (num_gpu_pages > 0) {
  840. cur_pages = num_gpu_pages;
  841. if (cur_pages > 8191) {
  842. cur_pages = 8191;
  843. }
  844. num_gpu_pages -= cur_pages;
  845. /* pages are in Y direction - height
  846. page width in X direction - width */
  847. radeon_ring_write(ring, PACKET3(PACKET3_BITBLT_MULTI, 8));
  848. radeon_ring_write(ring,
  849. RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
  850. RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  851. RADEON_GMC_SRC_CLIPPING |
  852. RADEON_GMC_DST_CLIPPING |
  853. RADEON_GMC_BRUSH_NONE |
  854. (RADEON_COLOR_FORMAT_ARGB8888 << 8) |
  855. RADEON_GMC_SRC_DATATYPE_COLOR |
  856. RADEON_ROP3_S |
  857. RADEON_DP_SRC_SOURCE_MEMORY |
  858. RADEON_GMC_CLR_CMP_CNTL_DIS |
  859. RADEON_GMC_WR_MSK_DIS);
  860. radeon_ring_write(ring, (pitch << 22) | (src_offset >> 10));
  861. radeon_ring_write(ring, (pitch << 22) | (dst_offset >> 10));
  862. radeon_ring_write(ring, (0x1fff) | (0x1fff << 16));
  863. radeon_ring_write(ring, 0);
  864. radeon_ring_write(ring, (0x1fff) | (0x1fff << 16));
  865. radeon_ring_write(ring, num_gpu_pages);
  866. radeon_ring_write(ring, num_gpu_pages);
  867. radeon_ring_write(ring, cur_pages | (stride_pixels << 16));
  868. }
  869. radeon_ring_write(ring, PACKET0(RADEON_DSTCACHE_CTLSTAT, 0));
  870. radeon_ring_write(ring, RADEON_RB2D_DC_FLUSH_ALL);
  871. radeon_ring_write(ring, PACKET0(RADEON_WAIT_UNTIL, 0));
  872. radeon_ring_write(ring,
  873. RADEON_WAIT_2D_IDLECLEAN |
  874. RADEON_WAIT_HOST_IDLECLEAN |
  875. RADEON_WAIT_DMA_GUI_IDLE);
  876. r = radeon_fence_emit(rdev, &fence, RADEON_RING_TYPE_GFX_INDEX);
  877. if (r) {
  878. radeon_ring_unlock_undo(rdev, ring);
  879. return ERR_PTR(r);
  880. }
  881. radeon_ring_unlock_commit(rdev, ring, false);
  882. return fence;
  883. }
  884. static int r100_cp_wait_for_idle(struct radeon_device *rdev)
  885. {
  886. unsigned i;
  887. u32 tmp;
  888. for (i = 0; i < rdev->usec_timeout; i++) {
  889. tmp = RREG32(R_000E40_RBBM_STATUS);
  890. if (!G_000E40_CP_CMDSTRM_BUSY(tmp)) {
  891. return 0;
  892. }
  893. udelay(1);
  894. }
  895. return -1;
  896. }
  897. void r100_ring_start(struct radeon_device *rdev, struct radeon_ring *ring)
  898. {
  899. int r;
  900. r = radeon_ring_lock(rdev, ring, 2);
  901. if (r) {
  902. return;
  903. }
  904. radeon_ring_write(ring, PACKET0(RADEON_ISYNC_CNTL, 0));
  905. radeon_ring_write(ring,
  906. RADEON_ISYNC_ANY2D_IDLE3D |
  907. RADEON_ISYNC_ANY3D_IDLE2D |
  908. RADEON_ISYNC_WAIT_IDLEGUI |
  909. RADEON_ISYNC_CPSCRATCH_IDLEGUI);
  910. radeon_ring_unlock_commit(rdev, ring, false);
  911. }
  912. /* Load the microcode for the CP */
  913. static int r100_cp_init_microcode(struct radeon_device *rdev)
  914. {
  915. const char *fw_name = NULL;
  916. int err;
  917. DRM_DEBUG_KMS("\n");
  918. if ((rdev->family == CHIP_R100) || (rdev->family == CHIP_RV100) ||
  919. (rdev->family == CHIP_RV200) || (rdev->family == CHIP_RS100) ||
  920. (rdev->family == CHIP_RS200)) {
  921. DRM_INFO("Loading R100 Microcode\n");
  922. fw_name = FIRMWARE_R100;
  923. } else if ((rdev->family == CHIP_R200) ||
  924. (rdev->family == CHIP_RV250) ||
  925. (rdev->family == CHIP_RV280) ||
  926. (rdev->family == CHIP_RS300)) {
  927. DRM_INFO("Loading R200 Microcode\n");
  928. fw_name = FIRMWARE_R200;
  929. } else if ((rdev->family == CHIP_R300) ||
  930. (rdev->family == CHIP_R350) ||
  931. (rdev->family == CHIP_RV350) ||
  932. (rdev->family == CHIP_RV380) ||
  933. (rdev->family == CHIP_RS400) ||
  934. (rdev->family == CHIP_RS480)) {
  935. DRM_INFO("Loading R300 Microcode\n");
  936. fw_name = FIRMWARE_R300;
  937. } else if ((rdev->family == CHIP_R420) ||
  938. (rdev->family == CHIP_R423) ||
  939. (rdev->family == CHIP_RV410)) {
  940. DRM_INFO("Loading R400 Microcode\n");
  941. fw_name = FIRMWARE_R420;
  942. } else if ((rdev->family == CHIP_RS690) ||
  943. (rdev->family == CHIP_RS740)) {
  944. DRM_INFO("Loading RS690/RS740 Microcode\n");
  945. fw_name = FIRMWARE_RS690;
  946. } else if (rdev->family == CHIP_RS600) {
  947. DRM_INFO("Loading RS600 Microcode\n");
  948. fw_name = FIRMWARE_RS600;
  949. } else if ((rdev->family == CHIP_RV515) ||
  950. (rdev->family == CHIP_R520) ||
  951. (rdev->family == CHIP_RV530) ||
  952. (rdev->family == CHIP_R580) ||
  953. (rdev->family == CHIP_RV560) ||
  954. (rdev->family == CHIP_RV570)) {
  955. DRM_INFO("Loading R500 Microcode\n");
  956. fw_name = FIRMWARE_R520;
  957. }
  958. err = request_firmware(&rdev->me_fw, fw_name, rdev->dev);
  959. if (err) {
  960. printk(KERN_ERR "radeon_cp: Failed to load firmware \"%s\"\n",
  961. fw_name);
  962. } else if (rdev->me_fw->size % 8) {
  963. printk(KERN_ERR
  964. "radeon_cp: Bogus length %zu in firmware \"%s\"\n",
  965. rdev->me_fw->size, fw_name);
  966. err = -EINVAL;
  967. release_firmware(rdev->me_fw);
  968. rdev->me_fw = NULL;
  969. }
  970. return err;
  971. }
  972. u32 r100_gfx_get_rptr(struct radeon_device *rdev,
  973. struct radeon_ring *ring)
  974. {
  975. u32 rptr;
  976. if (rdev->wb.enabled)
  977. rptr = le32_to_cpu(rdev->wb.wb[ring->rptr_offs/4]);
  978. else
  979. rptr = RREG32(RADEON_CP_RB_RPTR);
  980. return rptr;
  981. }
  982. u32 r100_gfx_get_wptr(struct radeon_device *rdev,
  983. struct radeon_ring *ring)
  984. {
  985. u32 wptr;
  986. wptr = RREG32(RADEON_CP_RB_WPTR);
  987. return wptr;
  988. }
  989. void r100_gfx_set_wptr(struct radeon_device *rdev,
  990. struct radeon_ring *ring)
  991. {
  992. WREG32(RADEON_CP_RB_WPTR, ring->wptr);
  993. (void)RREG32(RADEON_CP_RB_WPTR);
  994. }
  995. static void r100_cp_load_microcode(struct radeon_device *rdev)
  996. {
  997. const __be32 *fw_data;
  998. int i, size;
  999. if (r100_gui_wait_for_idle(rdev)) {
  1000. printk(KERN_WARNING "Failed to wait GUI idle while "
  1001. "programming pipes. Bad things might happen.\n");
  1002. }
  1003. if (rdev->me_fw) {
  1004. size = rdev->me_fw->size / 4;
  1005. fw_data = (const __be32 *)&rdev->me_fw->data[0];
  1006. WREG32(RADEON_CP_ME_RAM_ADDR, 0);
  1007. for (i = 0; i < size; i += 2) {
  1008. WREG32(RADEON_CP_ME_RAM_DATAH,
  1009. be32_to_cpup(&fw_data[i]));
  1010. WREG32(RADEON_CP_ME_RAM_DATAL,
  1011. be32_to_cpup(&fw_data[i + 1]));
  1012. }
  1013. }
  1014. }
  1015. int r100_cp_init(struct radeon_device *rdev, unsigned ring_size)
  1016. {
  1017. struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
  1018. unsigned rb_bufsz;
  1019. unsigned rb_blksz;
  1020. unsigned max_fetch;
  1021. unsigned pre_write_timer;
  1022. unsigned pre_write_limit;
  1023. unsigned indirect2_start;
  1024. unsigned indirect1_start;
  1025. uint32_t tmp;
  1026. int r;
  1027. if (r100_debugfs_cp_init(rdev)) {
  1028. DRM_ERROR("Failed to register debugfs file for CP !\n");
  1029. }
  1030. if (!rdev->me_fw) {
  1031. r = r100_cp_init_microcode(rdev);
  1032. if (r) {
  1033. DRM_ERROR("Failed to load firmware!\n");
  1034. return r;
  1035. }
  1036. }
  1037. /* Align ring size */
  1038. rb_bufsz = order_base_2(ring_size / 8);
  1039. ring_size = (1 << (rb_bufsz + 1)) * 4;
  1040. r100_cp_load_microcode(rdev);
  1041. r = radeon_ring_init(rdev, ring, ring_size, RADEON_WB_CP_RPTR_OFFSET,
  1042. RADEON_CP_PACKET2);
  1043. if (r) {
  1044. return r;
  1045. }
  1046. /* Each time the cp read 1024 bytes (16 dword/quadword) update
  1047. * the rptr copy in system ram */
  1048. rb_blksz = 9;
  1049. /* cp will read 128bytes at a time (4 dwords) */
  1050. max_fetch = 1;
  1051. ring->align_mask = 16 - 1;
  1052. /* Write to CP_RB_WPTR will be delayed for pre_write_timer clocks */
  1053. pre_write_timer = 64;
  1054. /* Force CP_RB_WPTR write if written more than one time before the
  1055. * delay expire
  1056. */
  1057. pre_write_limit = 0;
  1058. /* Setup the cp cache like this (cache size is 96 dwords) :
  1059. * RING 0 to 15
  1060. * INDIRECT1 16 to 79
  1061. * INDIRECT2 80 to 95
  1062. * So ring cache size is 16dwords (> (2 * max_fetch = 2 * 4dwords))
  1063. * indirect1 cache size is 64dwords (> (2 * max_fetch = 2 * 4dwords))
  1064. * indirect2 cache size is 16dwords (> (2 * max_fetch = 2 * 4dwords))
  1065. * Idea being that most of the gpu cmd will be through indirect1 buffer
  1066. * so it gets the bigger cache.
  1067. */
  1068. indirect2_start = 80;
  1069. indirect1_start = 16;
  1070. /* cp setup */
  1071. WREG32(0x718, pre_write_timer | (pre_write_limit << 28));
  1072. tmp = (REG_SET(RADEON_RB_BUFSZ, rb_bufsz) |
  1073. REG_SET(RADEON_RB_BLKSZ, rb_blksz) |
  1074. REG_SET(RADEON_MAX_FETCH, max_fetch));
  1075. #ifdef __BIG_ENDIAN
  1076. tmp |= RADEON_BUF_SWAP_32BIT;
  1077. #endif
  1078. WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_NO_UPDATE);
  1079. /* Set ring address */
  1080. DRM_INFO("radeon: ring at 0x%016lX\n", (unsigned long)ring->gpu_addr);
  1081. WREG32(RADEON_CP_RB_BASE, ring->gpu_addr);
  1082. /* Force read & write ptr to 0 */
  1083. WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA | RADEON_RB_NO_UPDATE);
  1084. WREG32(RADEON_CP_RB_RPTR_WR, 0);
  1085. ring->wptr = 0;
  1086. WREG32(RADEON_CP_RB_WPTR, ring->wptr);
  1087. /* set the wb address whether it's enabled or not */
  1088. WREG32(R_00070C_CP_RB_RPTR_ADDR,
  1089. S_00070C_RB_RPTR_ADDR((rdev->wb.gpu_addr + RADEON_WB_CP_RPTR_OFFSET) >> 2));
  1090. WREG32(R_000774_SCRATCH_ADDR, rdev->wb.gpu_addr + RADEON_WB_SCRATCH_OFFSET);
  1091. if (rdev->wb.enabled)
  1092. WREG32(R_000770_SCRATCH_UMSK, 0xff);
  1093. else {
  1094. tmp |= RADEON_RB_NO_UPDATE;
  1095. WREG32(R_000770_SCRATCH_UMSK, 0);
  1096. }
  1097. WREG32(RADEON_CP_RB_CNTL, tmp);
  1098. udelay(10);
  1099. /* Set cp mode to bus mastering & enable cp*/
  1100. WREG32(RADEON_CP_CSQ_MODE,
  1101. REG_SET(RADEON_INDIRECT2_START, indirect2_start) |
  1102. REG_SET(RADEON_INDIRECT1_START, indirect1_start));
  1103. WREG32(RADEON_CP_RB_WPTR_DELAY, 0);
  1104. WREG32(RADEON_CP_CSQ_MODE, 0x00004D4D);
  1105. WREG32(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIBM_INDBM);
  1106. /* at this point everything should be setup correctly to enable master */
  1107. pci_set_master(rdev->pdev);
  1108. radeon_ring_start(rdev, RADEON_RING_TYPE_GFX_INDEX, &rdev->ring[RADEON_RING_TYPE_GFX_INDEX]);
  1109. r = radeon_ring_test(rdev, RADEON_RING_TYPE_GFX_INDEX, ring);
  1110. if (r) {
  1111. DRM_ERROR("radeon: cp isn't working (%d).\n", r);
  1112. return r;
  1113. }
  1114. ring->ready = true;
  1115. radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
  1116. if (!ring->rptr_save_reg /* not resuming from suspend */
  1117. && radeon_ring_supports_scratch_reg(rdev, ring)) {
  1118. r = radeon_scratch_get(rdev, &ring->rptr_save_reg);
  1119. if (r) {
  1120. DRM_ERROR("failed to get scratch reg for rptr save (%d).\n", r);
  1121. ring->rptr_save_reg = 0;
  1122. }
  1123. }
  1124. return 0;
  1125. }
  1126. void r100_cp_fini(struct radeon_device *rdev)
  1127. {
  1128. if (r100_cp_wait_for_idle(rdev)) {
  1129. DRM_ERROR("Wait for CP idle timeout, shutting down CP.\n");
  1130. }
  1131. /* Disable ring */
  1132. r100_cp_disable(rdev);
  1133. radeon_scratch_free(rdev, rdev->ring[RADEON_RING_TYPE_GFX_INDEX].rptr_save_reg);
  1134. radeon_ring_fini(rdev, &rdev->ring[RADEON_RING_TYPE_GFX_INDEX]);
  1135. DRM_INFO("radeon: cp finalized\n");
  1136. }
  1137. void r100_cp_disable(struct radeon_device *rdev)
  1138. {
  1139. /* Disable ring */
  1140. radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
  1141. rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready = false;
  1142. WREG32(RADEON_CP_CSQ_MODE, 0);
  1143. WREG32(RADEON_CP_CSQ_CNTL, 0);
  1144. WREG32(R_000770_SCRATCH_UMSK, 0);
  1145. if (r100_gui_wait_for_idle(rdev)) {
  1146. printk(KERN_WARNING "Failed to wait GUI idle while "
  1147. "programming pipes. Bad things might happen.\n");
  1148. }
  1149. }
  1150. /*
  1151. * CS functions
  1152. */
  1153. int r100_reloc_pitch_offset(struct radeon_cs_parser *p,
  1154. struct radeon_cs_packet *pkt,
  1155. unsigned idx,
  1156. unsigned reg)
  1157. {
  1158. int r;
  1159. u32 tile_flags = 0;
  1160. u32 tmp;
  1161. struct radeon_bo_list *reloc;
  1162. u32 value;
  1163. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1164. if (r) {
  1165. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  1166. idx, reg);
  1167. radeon_cs_dump_packet(p, pkt);
  1168. return r;
  1169. }
  1170. value = radeon_get_ib_value(p, idx);
  1171. tmp = value & 0x003fffff;
  1172. tmp += (((u32)reloc->gpu_offset) >> 10);
  1173. if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
  1174. if (reloc->tiling_flags & RADEON_TILING_MACRO)
  1175. tile_flags |= RADEON_DST_TILE_MACRO;
  1176. if (reloc->tiling_flags & RADEON_TILING_MICRO) {
  1177. if (reg == RADEON_SRC_PITCH_OFFSET) {
  1178. DRM_ERROR("Cannot src blit from microtiled surface\n");
  1179. radeon_cs_dump_packet(p, pkt);
  1180. return -EINVAL;
  1181. }
  1182. tile_flags |= RADEON_DST_TILE_MICRO;
  1183. }
  1184. tmp |= tile_flags;
  1185. p->ib.ptr[idx] = (value & 0x3fc00000) | tmp;
  1186. } else
  1187. p->ib.ptr[idx] = (value & 0xffc00000) | tmp;
  1188. return 0;
  1189. }
  1190. int r100_packet3_load_vbpntr(struct radeon_cs_parser *p,
  1191. struct radeon_cs_packet *pkt,
  1192. int idx)
  1193. {
  1194. unsigned c, i;
  1195. struct radeon_bo_list *reloc;
  1196. struct r100_cs_track *track;
  1197. int r = 0;
  1198. volatile uint32_t *ib;
  1199. u32 idx_value;
  1200. ib = p->ib.ptr;
  1201. track = (struct r100_cs_track *)p->track;
  1202. c = radeon_get_ib_value(p, idx++) & 0x1F;
  1203. if (c > 16) {
  1204. DRM_ERROR("Only 16 vertex buffers are allowed %d\n",
  1205. pkt->opcode);
  1206. radeon_cs_dump_packet(p, pkt);
  1207. return -EINVAL;
  1208. }
  1209. track->num_arrays = c;
  1210. for (i = 0; i < (c - 1); i+=2, idx+=3) {
  1211. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1212. if (r) {
  1213. DRM_ERROR("No reloc for packet3 %d\n",
  1214. pkt->opcode);
  1215. radeon_cs_dump_packet(p, pkt);
  1216. return r;
  1217. }
  1218. idx_value = radeon_get_ib_value(p, idx);
  1219. ib[idx+1] = radeon_get_ib_value(p, idx + 1) + ((u32)reloc->gpu_offset);
  1220. track->arrays[i + 0].esize = idx_value >> 8;
  1221. track->arrays[i + 0].robj = reloc->robj;
  1222. track->arrays[i + 0].esize &= 0x7F;
  1223. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1224. if (r) {
  1225. DRM_ERROR("No reloc for packet3 %d\n",
  1226. pkt->opcode);
  1227. radeon_cs_dump_packet(p, pkt);
  1228. return r;
  1229. }
  1230. ib[idx+2] = radeon_get_ib_value(p, idx + 2) + ((u32)reloc->gpu_offset);
  1231. track->arrays[i + 1].robj = reloc->robj;
  1232. track->arrays[i + 1].esize = idx_value >> 24;
  1233. track->arrays[i + 1].esize &= 0x7F;
  1234. }
  1235. if (c & 1) {
  1236. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1237. if (r) {
  1238. DRM_ERROR("No reloc for packet3 %d\n",
  1239. pkt->opcode);
  1240. radeon_cs_dump_packet(p, pkt);
  1241. return r;
  1242. }
  1243. idx_value = radeon_get_ib_value(p, idx);
  1244. ib[idx+1] = radeon_get_ib_value(p, idx + 1) + ((u32)reloc->gpu_offset);
  1245. track->arrays[i + 0].robj = reloc->robj;
  1246. track->arrays[i + 0].esize = idx_value >> 8;
  1247. track->arrays[i + 0].esize &= 0x7F;
  1248. }
  1249. return r;
  1250. }
  1251. int r100_cs_parse_packet0(struct radeon_cs_parser *p,
  1252. struct radeon_cs_packet *pkt,
  1253. const unsigned *auth, unsigned n,
  1254. radeon_packet0_check_t check)
  1255. {
  1256. unsigned reg;
  1257. unsigned i, j, m;
  1258. unsigned idx;
  1259. int r;
  1260. idx = pkt->idx + 1;
  1261. reg = pkt->reg;
  1262. /* Check that register fall into register range
  1263. * determined by the number of entry (n) in the
  1264. * safe register bitmap.
  1265. */
  1266. if (pkt->one_reg_wr) {
  1267. if ((reg >> 7) > n) {
  1268. return -EINVAL;
  1269. }
  1270. } else {
  1271. if (((reg + (pkt->count << 2)) >> 7) > n) {
  1272. return -EINVAL;
  1273. }
  1274. }
  1275. for (i = 0; i <= pkt->count; i++, idx++) {
  1276. j = (reg >> 7);
  1277. m = 1 << ((reg >> 2) & 31);
  1278. if (auth[j] & m) {
  1279. r = check(p, pkt, idx, reg);
  1280. if (r) {
  1281. return r;
  1282. }
  1283. }
  1284. if (pkt->one_reg_wr) {
  1285. if (!(auth[j] & m)) {
  1286. break;
  1287. }
  1288. } else {
  1289. reg += 4;
  1290. }
  1291. }
  1292. return 0;
  1293. }
  1294. /**
  1295. * r100_cs_packet_next_vline() - parse userspace VLINE packet
  1296. * @parser: parser structure holding parsing context.
  1297. *
  1298. * Userspace sends a special sequence for VLINE waits.
  1299. * PACKET0 - VLINE_START_END + value
  1300. * PACKET0 - WAIT_UNTIL +_value
  1301. * RELOC (P3) - crtc_id in reloc.
  1302. *
  1303. * This function parses this and relocates the VLINE START END
  1304. * and WAIT UNTIL packets to the correct crtc.
  1305. * It also detects a switched off crtc and nulls out the
  1306. * wait in that case.
  1307. */
  1308. int r100_cs_packet_parse_vline(struct radeon_cs_parser *p)
  1309. {
  1310. struct drm_crtc *crtc;
  1311. struct radeon_crtc *radeon_crtc;
  1312. struct radeon_cs_packet p3reloc, waitreloc;
  1313. int crtc_id;
  1314. int r;
  1315. uint32_t header, h_idx, reg;
  1316. volatile uint32_t *ib;
  1317. ib = p->ib.ptr;
  1318. /* parse the wait until */
  1319. r = radeon_cs_packet_parse(p, &waitreloc, p->idx);
  1320. if (r)
  1321. return r;
  1322. /* check its a wait until and only 1 count */
  1323. if (waitreloc.reg != RADEON_WAIT_UNTIL ||
  1324. waitreloc.count != 0) {
  1325. DRM_ERROR("vline wait had illegal wait until segment\n");
  1326. return -EINVAL;
  1327. }
  1328. if (radeon_get_ib_value(p, waitreloc.idx + 1) != RADEON_WAIT_CRTC_VLINE) {
  1329. DRM_ERROR("vline wait had illegal wait until\n");
  1330. return -EINVAL;
  1331. }
  1332. /* jump over the NOP */
  1333. r = radeon_cs_packet_parse(p, &p3reloc, p->idx + waitreloc.count + 2);
  1334. if (r)
  1335. return r;
  1336. h_idx = p->idx - 2;
  1337. p->idx += waitreloc.count + 2;
  1338. p->idx += p3reloc.count + 2;
  1339. header = radeon_get_ib_value(p, h_idx);
  1340. crtc_id = radeon_get_ib_value(p, h_idx + 5);
  1341. reg = R100_CP_PACKET0_GET_REG(header);
  1342. crtc = drm_crtc_find(p->rdev->ddev, crtc_id);
  1343. if (!crtc) {
  1344. DRM_ERROR("cannot find crtc %d\n", crtc_id);
  1345. return -ENOENT;
  1346. }
  1347. radeon_crtc = to_radeon_crtc(crtc);
  1348. crtc_id = radeon_crtc->crtc_id;
  1349. if (!crtc->enabled) {
  1350. /* if the CRTC isn't enabled - we need to nop out the wait until */
  1351. ib[h_idx + 2] = PACKET2(0);
  1352. ib[h_idx + 3] = PACKET2(0);
  1353. } else if (crtc_id == 1) {
  1354. switch (reg) {
  1355. case AVIVO_D1MODE_VLINE_START_END:
  1356. header &= ~R300_CP_PACKET0_REG_MASK;
  1357. header |= AVIVO_D2MODE_VLINE_START_END >> 2;
  1358. break;
  1359. case RADEON_CRTC_GUI_TRIG_VLINE:
  1360. header &= ~R300_CP_PACKET0_REG_MASK;
  1361. header |= RADEON_CRTC2_GUI_TRIG_VLINE >> 2;
  1362. break;
  1363. default:
  1364. DRM_ERROR("unknown crtc reloc\n");
  1365. return -EINVAL;
  1366. }
  1367. ib[h_idx] = header;
  1368. ib[h_idx + 3] |= RADEON_ENG_DISPLAY_SELECT_CRTC1;
  1369. }
  1370. return 0;
  1371. }
  1372. static int r100_get_vtx_size(uint32_t vtx_fmt)
  1373. {
  1374. int vtx_size;
  1375. vtx_size = 2;
  1376. /* ordered according to bits in spec */
  1377. if (vtx_fmt & RADEON_SE_VTX_FMT_W0)
  1378. vtx_size++;
  1379. if (vtx_fmt & RADEON_SE_VTX_FMT_FPCOLOR)
  1380. vtx_size += 3;
  1381. if (vtx_fmt & RADEON_SE_VTX_FMT_FPALPHA)
  1382. vtx_size++;
  1383. if (vtx_fmt & RADEON_SE_VTX_FMT_PKCOLOR)
  1384. vtx_size++;
  1385. if (vtx_fmt & RADEON_SE_VTX_FMT_FPSPEC)
  1386. vtx_size += 3;
  1387. if (vtx_fmt & RADEON_SE_VTX_FMT_FPFOG)
  1388. vtx_size++;
  1389. if (vtx_fmt & RADEON_SE_VTX_FMT_PKSPEC)
  1390. vtx_size++;
  1391. if (vtx_fmt & RADEON_SE_VTX_FMT_ST0)
  1392. vtx_size += 2;
  1393. if (vtx_fmt & RADEON_SE_VTX_FMT_ST1)
  1394. vtx_size += 2;
  1395. if (vtx_fmt & RADEON_SE_VTX_FMT_Q1)
  1396. vtx_size++;
  1397. if (vtx_fmt & RADEON_SE_VTX_FMT_ST2)
  1398. vtx_size += 2;
  1399. if (vtx_fmt & RADEON_SE_VTX_FMT_Q2)
  1400. vtx_size++;
  1401. if (vtx_fmt & RADEON_SE_VTX_FMT_ST3)
  1402. vtx_size += 2;
  1403. if (vtx_fmt & RADEON_SE_VTX_FMT_Q3)
  1404. vtx_size++;
  1405. if (vtx_fmt & RADEON_SE_VTX_FMT_Q0)
  1406. vtx_size++;
  1407. /* blend weight */
  1408. if (vtx_fmt & (0x7 << 15))
  1409. vtx_size += (vtx_fmt >> 15) & 0x7;
  1410. if (vtx_fmt & RADEON_SE_VTX_FMT_N0)
  1411. vtx_size += 3;
  1412. if (vtx_fmt & RADEON_SE_VTX_FMT_XY1)
  1413. vtx_size += 2;
  1414. if (vtx_fmt & RADEON_SE_VTX_FMT_Z1)
  1415. vtx_size++;
  1416. if (vtx_fmt & RADEON_SE_VTX_FMT_W1)
  1417. vtx_size++;
  1418. if (vtx_fmt & RADEON_SE_VTX_FMT_N1)
  1419. vtx_size++;
  1420. if (vtx_fmt & RADEON_SE_VTX_FMT_Z)
  1421. vtx_size++;
  1422. return vtx_size;
  1423. }
  1424. static int r100_packet0_check(struct radeon_cs_parser *p,
  1425. struct radeon_cs_packet *pkt,
  1426. unsigned idx, unsigned reg)
  1427. {
  1428. struct radeon_bo_list *reloc;
  1429. struct r100_cs_track *track;
  1430. volatile uint32_t *ib;
  1431. uint32_t tmp;
  1432. int r;
  1433. int i, face;
  1434. u32 tile_flags = 0;
  1435. u32 idx_value;
  1436. ib = p->ib.ptr;
  1437. track = (struct r100_cs_track *)p->track;
  1438. idx_value = radeon_get_ib_value(p, idx);
  1439. switch (reg) {
  1440. case RADEON_CRTC_GUI_TRIG_VLINE:
  1441. r = r100_cs_packet_parse_vline(p);
  1442. if (r) {
  1443. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  1444. idx, reg);
  1445. radeon_cs_dump_packet(p, pkt);
  1446. return r;
  1447. }
  1448. break;
  1449. /* FIXME: only allow PACKET3 blit? easier to check for out of
  1450. * range access */
  1451. case RADEON_DST_PITCH_OFFSET:
  1452. case RADEON_SRC_PITCH_OFFSET:
  1453. r = r100_reloc_pitch_offset(p, pkt, idx, reg);
  1454. if (r)
  1455. return r;
  1456. break;
  1457. case RADEON_RB3D_DEPTHOFFSET:
  1458. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1459. if (r) {
  1460. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  1461. idx, reg);
  1462. radeon_cs_dump_packet(p, pkt);
  1463. return r;
  1464. }
  1465. track->zb.robj = reloc->robj;
  1466. track->zb.offset = idx_value;
  1467. track->zb_dirty = true;
  1468. ib[idx] = idx_value + ((u32)reloc->gpu_offset);
  1469. break;
  1470. case RADEON_RB3D_COLOROFFSET:
  1471. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1472. if (r) {
  1473. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  1474. idx, reg);
  1475. radeon_cs_dump_packet(p, pkt);
  1476. return r;
  1477. }
  1478. track->cb[0].robj = reloc->robj;
  1479. track->cb[0].offset = idx_value;
  1480. track->cb_dirty = true;
  1481. ib[idx] = idx_value + ((u32)reloc->gpu_offset);
  1482. break;
  1483. case RADEON_PP_TXOFFSET_0:
  1484. case RADEON_PP_TXOFFSET_1:
  1485. case RADEON_PP_TXOFFSET_2:
  1486. i = (reg - RADEON_PP_TXOFFSET_0) / 24;
  1487. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1488. if (r) {
  1489. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  1490. idx, reg);
  1491. radeon_cs_dump_packet(p, pkt);
  1492. return r;
  1493. }
  1494. if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
  1495. if (reloc->tiling_flags & RADEON_TILING_MACRO)
  1496. tile_flags |= RADEON_TXO_MACRO_TILE;
  1497. if (reloc->tiling_flags & RADEON_TILING_MICRO)
  1498. tile_flags |= RADEON_TXO_MICRO_TILE_X2;
  1499. tmp = idx_value & ~(0x7 << 2);
  1500. tmp |= tile_flags;
  1501. ib[idx] = tmp + ((u32)reloc->gpu_offset);
  1502. } else
  1503. ib[idx] = idx_value + ((u32)reloc->gpu_offset);
  1504. track->textures[i].robj = reloc->robj;
  1505. track->tex_dirty = true;
  1506. break;
  1507. case RADEON_PP_CUBIC_OFFSET_T0_0:
  1508. case RADEON_PP_CUBIC_OFFSET_T0_1:
  1509. case RADEON_PP_CUBIC_OFFSET_T0_2:
  1510. case RADEON_PP_CUBIC_OFFSET_T0_3:
  1511. case RADEON_PP_CUBIC_OFFSET_T0_4:
  1512. i = (reg - RADEON_PP_CUBIC_OFFSET_T0_0) / 4;
  1513. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1514. if (r) {
  1515. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  1516. idx, reg);
  1517. radeon_cs_dump_packet(p, pkt);
  1518. return r;
  1519. }
  1520. track->textures[0].cube_info[i].offset = idx_value;
  1521. ib[idx] = idx_value + ((u32)reloc->gpu_offset);
  1522. track->textures[0].cube_info[i].robj = reloc->robj;
  1523. track->tex_dirty = true;
  1524. break;
  1525. case RADEON_PP_CUBIC_OFFSET_T1_0:
  1526. case RADEON_PP_CUBIC_OFFSET_T1_1:
  1527. case RADEON_PP_CUBIC_OFFSET_T1_2:
  1528. case RADEON_PP_CUBIC_OFFSET_T1_3:
  1529. case RADEON_PP_CUBIC_OFFSET_T1_4:
  1530. i = (reg - RADEON_PP_CUBIC_OFFSET_T1_0) / 4;
  1531. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1532. if (r) {
  1533. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  1534. idx, reg);
  1535. radeon_cs_dump_packet(p, pkt);
  1536. return r;
  1537. }
  1538. track->textures[1].cube_info[i].offset = idx_value;
  1539. ib[idx] = idx_value + ((u32)reloc->gpu_offset);
  1540. track->textures[1].cube_info[i].robj = reloc->robj;
  1541. track->tex_dirty = true;
  1542. break;
  1543. case RADEON_PP_CUBIC_OFFSET_T2_0:
  1544. case RADEON_PP_CUBIC_OFFSET_T2_1:
  1545. case RADEON_PP_CUBIC_OFFSET_T2_2:
  1546. case RADEON_PP_CUBIC_OFFSET_T2_3:
  1547. case RADEON_PP_CUBIC_OFFSET_T2_4:
  1548. i = (reg - RADEON_PP_CUBIC_OFFSET_T2_0) / 4;
  1549. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1550. if (r) {
  1551. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  1552. idx, reg);
  1553. radeon_cs_dump_packet(p, pkt);
  1554. return r;
  1555. }
  1556. track->textures[2].cube_info[i].offset = idx_value;
  1557. ib[idx] = idx_value + ((u32)reloc->gpu_offset);
  1558. track->textures[2].cube_info[i].robj = reloc->robj;
  1559. track->tex_dirty = true;
  1560. break;
  1561. case RADEON_RE_WIDTH_HEIGHT:
  1562. track->maxy = ((idx_value >> 16) & 0x7FF);
  1563. track->cb_dirty = true;
  1564. track->zb_dirty = true;
  1565. break;
  1566. case RADEON_RB3D_COLORPITCH:
  1567. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1568. if (r) {
  1569. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  1570. idx, reg);
  1571. radeon_cs_dump_packet(p, pkt);
  1572. return r;
  1573. }
  1574. if (!(p->cs_flags & RADEON_CS_KEEP_TILING_FLAGS)) {
  1575. if (reloc->tiling_flags & RADEON_TILING_MACRO)
  1576. tile_flags |= RADEON_COLOR_TILE_ENABLE;
  1577. if (reloc->tiling_flags & RADEON_TILING_MICRO)
  1578. tile_flags |= RADEON_COLOR_MICROTILE_ENABLE;
  1579. tmp = idx_value & ~(0x7 << 16);
  1580. tmp |= tile_flags;
  1581. ib[idx] = tmp;
  1582. } else
  1583. ib[idx] = idx_value;
  1584. track->cb[0].pitch = idx_value & RADEON_COLORPITCH_MASK;
  1585. track->cb_dirty = true;
  1586. break;
  1587. case RADEON_RB3D_DEPTHPITCH:
  1588. track->zb.pitch = idx_value & RADEON_DEPTHPITCH_MASK;
  1589. track->zb_dirty = true;
  1590. break;
  1591. case RADEON_RB3D_CNTL:
  1592. switch ((idx_value >> RADEON_RB3D_COLOR_FORMAT_SHIFT) & 0x1f) {
  1593. case 7:
  1594. case 8:
  1595. case 9:
  1596. case 11:
  1597. case 12:
  1598. track->cb[0].cpp = 1;
  1599. break;
  1600. case 3:
  1601. case 4:
  1602. case 15:
  1603. track->cb[0].cpp = 2;
  1604. break;
  1605. case 6:
  1606. track->cb[0].cpp = 4;
  1607. break;
  1608. default:
  1609. DRM_ERROR("Invalid color buffer format (%d) !\n",
  1610. ((idx_value >> RADEON_RB3D_COLOR_FORMAT_SHIFT) & 0x1f));
  1611. return -EINVAL;
  1612. }
  1613. track->z_enabled = !!(idx_value & RADEON_Z_ENABLE);
  1614. track->cb_dirty = true;
  1615. track->zb_dirty = true;
  1616. break;
  1617. case RADEON_RB3D_ZSTENCILCNTL:
  1618. switch (idx_value & 0xf) {
  1619. case 0:
  1620. track->zb.cpp = 2;
  1621. break;
  1622. case 2:
  1623. case 3:
  1624. case 4:
  1625. case 5:
  1626. case 9:
  1627. case 11:
  1628. track->zb.cpp = 4;
  1629. break;
  1630. default:
  1631. break;
  1632. }
  1633. track->zb_dirty = true;
  1634. break;
  1635. case RADEON_RB3D_ZPASS_ADDR:
  1636. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1637. if (r) {
  1638. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  1639. idx, reg);
  1640. radeon_cs_dump_packet(p, pkt);
  1641. return r;
  1642. }
  1643. ib[idx] = idx_value + ((u32)reloc->gpu_offset);
  1644. break;
  1645. case RADEON_PP_CNTL:
  1646. {
  1647. uint32_t temp = idx_value >> 4;
  1648. for (i = 0; i < track->num_texture; i++)
  1649. track->textures[i].enabled = !!(temp & (1 << i));
  1650. track->tex_dirty = true;
  1651. }
  1652. break;
  1653. case RADEON_SE_VF_CNTL:
  1654. track->vap_vf_cntl = idx_value;
  1655. break;
  1656. case RADEON_SE_VTX_FMT:
  1657. track->vtx_size = r100_get_vtx_size(idx_value);
  1658. break;
  1659. case RADEON_PP_TEX_SIZE_0:
  1660. case RADEON_PP_TEX_SIZE_1:
  1661. case RADEON_PP_TEX_SIZE_2:
  1662. i = (reg - RADEON_PP_TEX_SIZE_0) / 8;
  1663. track->textures[i].width = (idx_value & RADEON_TEX_USIZE_MASK) + 1;
  1664. track->textures[i].height = ((idx_value & RADEON_TEX_VSIZE_MASK) >> RADEON_TEX_VSIZE_SHIFT) + 1;
  1665. track->tex_dirty = true;
  1666. break;
  1667. case RADEON_PP_TEX_PITCH_0:
  1668. case RADEON_PP_TEX_PITCH_1:
  1669. case RADEON_PP_TEX_PITCH_2:
  1670. i = (reg - RADEON_PP_TEX_PITCH_0) / 8;
  1671. track->textures[i].pitch = idx_value + 32;
  1672. track->tex_dirty = true;
  1673. break;
  1674. case RADEON_PP_TXFILTER_0:
  1675. case RADEON_PP_TXFILTER_1:
  1676. case RADEON_PP_TXFILTER_2:
  1677. i = (reg - RADEON_PP_TXFILTER_0) / 24;
  1678. track->textures[i].num_levels = ((idx_value & RADEON_MAX_MIP_LEVEL_MASK)
  1679. >> RADEON_MAX_MIP_LEVEL_SHIFT);
  1680. tmp = (idx_value >> 23) & 0x7;
  1681. if (tmp == 2 || tmp == 6)
  1682. track->textures[i].roundup_w = false;
  1683. tmp = (idx_value >> 27) & 0x7;
  1684. if (tmp == 2 || tmp == 6)
  1685. track->textures[i].roundup_h = false;
  1686. track->tex_dirty = true;
  1687. break;
  1688. case RADEON_PP_TXFORMAT_0:
  1689. case RADEON_PP_TXFORMAT_1:
  1690. case RADEON_PP_TXFORMAT_2:
  1691. i = (reg - RADEON_PP_TXFORMAT_0) / 24;
  1692. if (idx_value & RADEON_TXFORMAT_NON_POWER2) {
  1693. track->textures[i].use_pitch = 1;
  1694. } else {
  1695. track->textures[i].use_pitch = 0;
  1696. track->textures[i].width = 1 << ((idx_value >> RADEON_TXFORMAT_WIDTH_SHIFT) & RADEON_TXFORMAT_WIDTH_MASK);
  1697. track->textures[i].height = 1 << ((idx_value >> RADEON_TXFORMAT_HEIGHT_SHIFT) & RADEON_TXFORMAT_HEIGHT_MASK);
  1698. }
  1699. if (idx_value & RADEON_TXFORMAT_CUBIC_MAP_ENABLE)
  1700. track->textures[i].tex_coord_type = 2;
  1701. switch ((idx_value & RADEON_TXFORMAT_FORMAT_MASK)) {
  1702. case RADEON_TXFORMAT_I8:
  1703. case RADEON_TXFORMAT_RGB332:
  1704. case RADEON_TXFORMAT_Y8:
  1705. track->textures[i].cpp = 1;
  1706. track->textures[i].compress_format = R100_TRACK_COMP_NONE;
  1707. break;
  1708. case RADEON_TXFORMAT_AI88:
  1709. case RADEON_TXFORMAT_ARGB1555:
  1710. case RADEON_TXFORMAT_RGB565:
  1711. case RADEON_TXFORMAT_ARGB4444:
  1712. case RADEON_TXFORMAT_VYUY422:
  1713. case RADEON_TXFORMAT_YVYU422:
  1714. case RADEON_TXFORMAT_SHADOW16:
  1715. case RADEON_TXFORMAT_LDUDV655:
  1716. case RADEON_TXFORMAT_DUDV88:
  1717. track->textures[i].cpp = 2;
  1718. track->textures[i].compress_format = R100_TRACK_COMP_NONE;
  1719. break;
  1720. case RADEON_TXFORMAT_ARGB8888:
  1721. case RADEON_TXFORMAT_RGBA8888:
  1722. case RADEON_TXFORMAT_SHADOW32:
  1723. case RADEON_TXFORMAT_LDUDUV8888:
  1724. track->textures[i].cpp = 4;
  1725. track->textures[i].compress_format = R100_TRACK_COMP_NONE;
  1726. break;
  1727. case RADEON_TXFORMAT_DXT1:
  1728. track->textures[i].cpp = 1;
  1729. track->textures[i].compress_format = R100_TRACK_COMP_DXT1;
  1730. break;
  1731. case RADEON_TXFORMAT_DXT23:
  1732. case RADEON_TXFORMAT_DXT45:
  1733. track->textures[i].cpp = 1;
  1734. track->textures[i].compress_format = R100_TRACK_COMP_DXT35;
  1735. break;
  1736. }
  1737. track->textures[i].cube_info[4].width = 1 << ((idx_value >> 16) & 0xf);
  1738. track->textures[i].cube_info[4].height = 1 << ((idx_value >> 20) & 0xf);
  1739. track->tex_dirty = true;
  1740. break;
  1741. case RADEON_PP_CUBIC_FACES_0:
  1742. case RADEON_PP_CUBIC_FACES_1:
  1743. case RADEON_PP_CUBIC_FACES_2:
  1744. tmp = idx_value;
  1745. i = (reg - RADEON_PP_CUBIC_FACES_0) / 4;
  1746. for (face = 0; face < 4; face++) {
  1747. track->textures[i].cube_info[face].width = 1 << ((tmp >> (face * 8)) & 0xf);
  1748. track->textures[i].cube_info[face].height = 1 << ((tmp >> ((face * 8) + 4)) & 0xf);
  1749. }
  1750. track->tex_dirty = true;
  1751. break;
  1752. default:
  1753. printk(KERN_ERR "Forbidden register 0x%04X in cs at %d\n",
  1754. reg, idx);
  1755. return -EINVAL;
  1756. }
  1757. return 0;
  1758. }
  1759. int r100_cs_track_check_pkt3_indx_buffer(struct radeon_cs_parser *p,
  1760. struct radeon_cs_packet *pkt,
  1761. struct radeon_bo *robj)
  1762. {
  1763. unsigned idx;
  1764. u32 value;
  1765. idx = pkt->idx + 1;
  1766. value = radeon_get_ib_value(p, idx + 2);
  1767. if ((value + 1) > radeon_bo_size(robj)) {
  1768. DRM_ERROR("[drm] Buffer too small for PACKET3 INDX_BUFFER "
  1769. "(need %u have %lu) !\n",
  1770. value + 1,
  1771. radeon_bo_size(robj));
  1772. return -EINVAL;
  1773. }
  1774. return 0;
  1775. }
  1776. static int r100_packet3_check(struct radeon_cs_parser *p,
  1777. struct radeon_cs_packet *pkt)
  1778. {
  1779. struct radeon_bo_list *reloc;
  1780. struct r100_cs_track *track;
  1781. unsigned idx;
  1782. volatile uint32_t *ib;
  1783. int r;
  1784. ib = p->ib.ptr;
  1785. idx = pkt->idx + 1;
  1786. track = (struct r100_cs_track *)p->track;
  1787. switch (pkt->opcode) {
  1788. case PACKET3_3D_LOAD_VBPNTR:
  1789. r = r100_packet3_load_vbpntr(p, pkt, idx);
  1790. if (r)
  1791. return r;
  1792. break;
  1793. case PACKET3_INDX_BUFFER:
  1794. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1795. if (r) {
  1796. DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
  1797. radeon_cs_dump_packet(p, pkt);
  1798. return r;
  1799. }
  1800. ib[idx+1] = radeon_get_ib_value(p, idx+1) + ((u32)reloc->gpu_offset);
  1801. r = r100_cs_track_check_pkt3_indx_buffer(p, pkt, reloc->robj);
  1802. if (r) {
  1803. return r;
  1804. }
  1805. break;
  1806. case 0x23:
  1807. /* 3D_RNDR_GEN_INDX_PRIM on r100/r200 */
  1808. r = radeon_cs_packet_next_reloc(p, &reloc, 0);
  1809. if (r) {
  1810. DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
  1811. radeon_cs_dump_packet(p, pkt);
  1812. return r;
  1813. }
  1814. ib[idx] = radeon_get_ib_value(p, idx) + ((u32)reloc->gpu_offset);
  1815. track->num_arrays = 1;
  1816. track->vtx_size = r100_get_vtx_size(radeon_get_ib_value(p, idx + 2));
  1817. track->arrays[0].robj = reloc->robj;
  1818. track->arrays[0].esize = track->vtx_size;
  1819. track->max_indx = radeon_get_ib_value(p, idx+1);
  1820. track->vap_vf_cntl = radeon_get_ib_value(p, idx+3);
  1821. track->immd_dwords = pkt->count - 1;
  1822. r = r100_cs_track_check(p->rdev, track);
  1823. if (r)
  1824. return r;
  1825. break;
  1826. case PACKET3_3D_DRAW_IMMD:
  1827. if (((radeon_get_ib_value(p, idx + 1) >> 4) & 0x3) != 3) {
  1828. DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
  1829. return -EINVAL;
  1830. }
  1831. track->vtx_size = r100_get_vtx_size(radeon_get_ib_value(p, idx + 0));
  1832. track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
  1833. track->immd_dwords = pkt->count - 1;
  1834. r = r100_cs_track_check(p->rdev, track);
  1835. if (r)
  1836. return r;
  1837. break;
  1838. /* triggers drawing using in-packet vertex data */
  1839. case PACKET3_3D_DRAW_IMMD_2:
  1840. if (((radeon_get_ib_value(p, idx) >> 4) & 0x3) != 3) {
  1841. DRM_ERROR("PRIM_WALK must be 3 for IMMD draw\n");
  1842. return -EINVAL;
  1843. }
  1844. track->vap_vf_cntl = radeon_get_ib_value(p, idx);
  1845. track->immd_dwords = pkt->count;
  1846. r = r100_cs_track_check(p->rdev, track);
  1847. if (r)
  1848. return r;
  1849. break;
  1850. /* triggers drawing using in-packet vertex data */
  1851. case PACKET3_3D_DRAW_VBUF_2:
  1852. track->vap_vf_cntl = radeon_get_ib_value(p, idx);
  1853. r = r100_cs_track_check(p->rdev, track);
  1854. if (r)
  1855. return r;
  1856. break;
  1857. /* triggers drawing of vertex buffers setup elsewhere */
  1858. case PACKET3_3D_DRAW_INDX_2:
  1859. track->vap_vf_cntl = radeon_get_ib_value(p, idx);
  1860. r = r100_cs_track_check(p->rdev, track);
  1861. if (r)
  1862. return r;
  1863. break;
  1864. /* triggers drawing using indices to vertex buffer */
  1865. case PACKET3_3D_DRAW_VBUF:
  1866. track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
  1867. r = r100_cs_track_check(p->rdev, track);
  1868. if (r)
  1869. return r;
  1870. break;
  1871. /* triggers drawing of vertex buffers setup elsewhere */
  1872. case PACKET3_3D_DRAW_INDX:
  1873. track->vap_vf_cntl = radeon_get_ib_value(p, idx + 1);
  1874. r = r100_cs_track_check(p->rdev, track);
  1875. if (r)
  1876. return r;
  1877. break;
  1878. /* triggers drawing using indices to vertex buffer */
  1879. case PACKET3_3D_CLEAR_HIZ:
  1880. case PACKET3_3D_CLEAR_ZMASK:
  1881. if (p->rdev->hyperz_filp != p->filp)
  1882. return -EINVAL;
  1883. break;
  1884. case PACKET3_NOP:
  1885. break;
  1886. default:
  1887. DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
  1888. return -EINVAL;
  1889. }
  1890. return 0;
  1891. }
  1892. int r100_cs_parse(struct radeon_cs_parser *p)
  1893. {
  1894. struct radeon_cs_packet pkt;
  1895. struct r100_cs_track *track;
  1896. int r;
  1897. track = kzalloc(sizeof(*track), GFP_KERNEL);
  1898. if (!track)
  1899. return -ENOMEM;
  1900. r100_cs_track_clear(p->rdev, track);
  1901. p->track = track;
  1902. do {
  1903. r = radeon_cs_packet_parse(p, &pkt, p->idx);
  1904. if (r) {
  1905. return r;
  1906. }
  1907. p->idx += pkt.count + 2;
  1908. switch (pkt.type) {
  1909. case RADEON_PACKET_TYPE0:
  1910. if (p->rdev->family >= CHIP_R200)
  1911. r = r100_cs_parse_packet0(p, &pkt,
  1912. p->rdev->config.r100.reg_safe_bm,
  1913. p->rdev->config.r100.reg_safe_bm_size,
  1914. &r200_packet0_check);
  1915. else
  1916. r = r100_cs_parse_packet0(p, &pkt,
  1917. p->rdev->config.r100.reg_safe_bm,
  1918. p->rdev->config.r100.reg_safe_bm_size,
  1919. &r100_packet0_check);
  1920. break;
  1921. case RADEON_PACKET_TYPE2:
  1922. break;
  1923. case RADEON_PACKET_TYPE3:
  1924. r = r100_packet3_check(p, &pkt);
  1925. break;
  1926. default:
  1927. DRM_ERROR("Unknown packet type %d !\n",
  1928. pkt.type);
  1929. return -EINVAL;
  1930. }
  1931. if (r)
  1932. return r;
  1933. } while (p->idx < p->chunk_ib->length_dw);
  1934. return 0;
  1935. }
  1936. static void r100_cs_track_texture_print(struct r100_cs_track_texture *t)
  1937. {
  1938. DRM_ERROR("pitch %d\n", t->pitch);
  1939. DRM_ERROR("use_pitch %d\n", t->use_pitch);
  1940. DRM_ERROR("width %d\n", t->width);
  1941. DRM_ERROR("width_11 %d\n", t->width_11);
  1942. DRM_ERROR("height %d\n", t->height);
  1943. DRM_ERROR("height_11 %d\n", t->height_11);
  1944. DRM_ERROR("num levels %d\n", t->num_levels);
  1945. DRM_ERROR("depth %d\n", t->txdepth);
  1946. DRM_ERROR("bpp %d\n", t->cpp);
  1947. DRM_ERROR("coordinate type %d\n", t->tex_coord_type);
  1948. DRM_ERROR("width round to power of 2 %d\n", t->roundup_w);
  1949. DRM_ERROR("height round to power of 2 %d\n", t->roundup_h);
  1950. DRM_ERROR("compress format %d\n", t->compress_format);
  1951. }
  1952. static int r100_track_compress_size(int compress_format, int w, int h)
  1953. {
  1954. int block_width, block_height, block_bytes;
  1955. int wblocks, hblocks;
  1956. int min_wblocks;
  1957. int sz;
  1958. block_width = 4;
  1959. block_height = 4;
  1960. switch (compress_format) {
  1961. case R100_TRACK_COMP_DXT1:
  1962. block_bytes = 8;
  1963. min_wblocks = 4;
  1964. break;
  1965. default:
  1966. case R100_TRACK_COMP_DXT35:
  1967. block_bytes = 16;
  1968. min_wblocks = 2;
  1969. break;
  1970. }
  1971. hblocks = (h + block_height - 1) / block_height;
  1972. wblocks = (w + block_width - 1) / block_width;
  1973. if (wblocks < min_wblocks)
  1974. wblocks = min_wblocks;
  1975. sz = wblocks * hblocks * block_bytes;
  1976. return sz;
  1977. }
  1978. static int r100_cs_track_cube(struct radeon_device *rdev,
  1979. struct r100_cs_track *track, unsigned idx)
  1980. {
  1981. unsigned face, w, h;
  1982. struct radeon_bo *cube_robj;
  1983. unsigned long size;
  1984. unsigned compress_format = track->textures[idx].compress_format;
  1985. for (face = 0; face < 5; face++) {
  1986. cube_robj = track->textures[idx].cube_info[face].robj;
  1987. w = track->textures[idx].cube_info[face].width;
  1988. h = track->textures[idx].cube_info[face].height;
  1989. if (compress_format) {
  1990. size = r100_track_compress_size(compress_format, w, h);
  1991. } else
  1992. size = w * h;
  1993. size *= track->textures[idx].cpp;
  1994. size += track->textures[idx].cube_info[face].offset;
  1995. if (size > radeon_bo_size(cube_robj)) {
  1996. DRM_ERROR("Cube texture offset greater than object size %lu %lu\n",
  1997. size, radeon_bo_size(cube_robj));
  1998. r100_cs_track_texture_print(&track->textures[idx]);
  1999. return -1;
  2000. }
  2001. }
  2002. return 0;
  2003. }
  2004. static int r100_cs_track_texture_check(struct radeon_device *rdev,
  2005. struct r100_cs_track *track)
  2006. {
  2007. struct radeon_bo *robj;
  2008. unsigned long size;
  2009. unsigned u, i, w, h, d;
  2010. int ret;
  2011. for (u = 0; u < track->num_texture; u++) {
  2012. if (!track->textures[u].enabled)
  2013. continue;
  2014. if (track->textures[u].lookup_disable)
  2015. continue;
  2016. robj = track->textures[u].robj;
  2017. if (robj == NULL) {
  2018. DRM_ERROR("No texture bound to unit %u\n", u);
  2019. return -EINVAL;
  2020. }
  2021. size = 0;
  2022. for (i = 0; i <= track->textures[u].num_levels; i++) {
  2023. if (track->textures[u].use_pitch) {
  2024. if (rdev->family < CHIP_R300)
  2025. w = (track->textures[u].pitch / track->textures[u].cpp) / (1 << i);
  2026. else
  2027. w = track->textures[u].pitch / (1 << i);
  2028. } else {
  2029. w = track->textures[u].width;
  2030. if (rdev->family >= CHIP_RV515)
  2031. w |= track->textures[u].width_11;
  2032. w = w / (1 << i);
  2033. if (track->textures[u].roundup_w)
  2034. w = roundup_pow_of_two(w);
  2035. }
  2036. h = track->textures[u].height;
  2037. if (rdev->family >= CHIP_RV515)
  2038. h |= track->textures[u].height_11;
  2039. h = h / (1 << i);
  2040. if (track->textures[u].roundup_h)
  2041. h = roundup_pow_of_two(h);
  2042. if (track->textures[u].tex_coord_type == 1) {
  2043. d = (1 << track->textures[u].txdepth) / (1 << i);
  2044. if (!d)
  2045. d = 1;
  2046. } else {
  2047. d = 1;
  2048. }
  2049. if (track->textures[u].compress_format) {
  2050. size += r100_track_compress_size(track->textures[u].compress_format, w, h) * d;
  2051. /* compressed textures are block based */
  2052. } else
  2053. size += w * h * d;
  2054. }
  2055. size *= track->textures[u].cpp;
  2056. switch (track->textures[u].tex_coord_type) {
  2057. case 0:
  2058. case 1:
  2059. break;
  2060. case 2:
  2061. if (track->separate_cube) {
  2062. ret = r100_cs_track_cube(rdev, track, u);
  2063. if (ret)
  2064. return ret;
  2065. } else
  2066. size *= 6;
  2067. break;
  2068. default:
  2069. DRM_ERROR("Invalid texture coordinate type %u for unit "
  2070. "%u\n", track->textures[u].tex_coord_type, u);
  2071. return -EINVAL;
  2072. }
  2073. if (size > radeon_bo_size(robj)) {
  2074. DRM_ERROR("Texture of unit %u needs %lu bytes but is "
  2075. "%lu\n", u, size, radeon_bo_size(robj));
  2076. r100_cs_track_texture_print(&track->textures[u]);
  2077. return -EINVAL;
  2078. }
  2079. }
  2080. return 0;
  2081. }
  2082. int r100_cs_track_check(struct radeon_device *rdev, struct r100_cs_track *track)
  2083. {
  2084. unsigned i;
  2085. unsigned long size;
  2086. unsigned prim_walk;
  2087. unsigned nverts;
  2088. unsigned num_cb = track->cb_dirty ? track->num_cb : 0;
  2089. if (num_cb && !track->zb_cb_clear && !track->color_channel_mask &&
  2090. !track->blend_read_enable)
  2091. num_cb = 0;
  2092. for (i = 0; i < num_cb; i++) {
  2093. if (track->cb[i].robj == NULL) {
  2094. DRM_ERROR("[drm] No buffer for color buffer %d !\n", i);
  2095. return -EINVAL;
  2096. }
  2097. size = track->cb[i].pitch * track->cb[i].cpp * track->maxy;
  2098. size += track->cb[i].offset;
  2099. if (size > radeon_bo_size(track->cb[i].robj)) {
  2100. DRM_ERROR("[drm] Buffer too small for color buffer %d "
  2101. "(need %lu have %lu) !\n", i, size,
  2102. radeon_bo_size(track->cb[i].robj));
  2103. DRM_ERROR("[drm] color buffer %d (%u %u %u %u)\n",
  2104. i, track->cb[i].pitch, track->cb[i].cpp,
  2105. track->cb[i].offset, track->maxy);
  2106. return -EINVAL;
  2107. }
  2108. }
  2109. track->cb_dirty = false;
  2110. if (track->zb_dirty && track->z_enabled) {
  2111. if (track->zb.robj == NULL) {
  2112. DRM_ERROR("[drm] No buffer for z buffer !\n");
  2113. return -EINVAL;
  2114. }
  2115. size = track->zb.pitch * track->zb.cpp * track->maxy;
  2116. size += track->zb.offset;
  2117. if (size > radeon_bo_size(track->zb.robj)) {
  2118. DRM_ERROR("[drm] Buffer too small for z buffer "
  2119. "(need %lu have %lu) !\n", size,
  2120. radeon_bo_size(track->zb.robj));
  2121. DRM_ERROR("[drm] zbuffer (%u %u %u %u)\n",
  2122. track->zb.pitch, track->zb.cpp,
  2123. track->zb.offset, track->maxy);
  2124. return -EINVAL;
  2125. }
  2126. }
  2127. track->zb_dirty = false;
  2128. if (track->aa_dirty && track->aaresolve) {
  2129. if (track->aa.robj == NULL) {
  2130. DRM_ERROR("[drm] No buffer for AA resolve buffer %d !\n", i);
  2131. return -EINVAL;
  2132. }
  2133. /* I believe the format comes from colorbuffer0. */
  2134. size = track->aa.pitch * track->cb[0].cpp * track->maxy;
  2135. size += track->aa.offset;
  2136. if (size > radeon_bo_size(track->aa.robj)) {
  2137. DRM_ERROR("[drm] Buffer too small for AA resolve buffer %d "
  2138. "(need %lu have %lu) !\n", i, size,
  2139. radeon_bo_size(track->aa.robj));
  2140. DRM_ERROR("[drm] AA resolve buffer %d (%u %u %u %u)\n",
  2141. i, track->aa.pitch, track->cb[0].cpp,
  2142. track->aa.offset, track->maxy);
  2143. return -EINVAL;
  2144. }
  2145. }
  2146. track->aa_dirty = false;
  2147. prim_walk = (track->vap_vf_cntl >> 4) & 0x3;
  2148. if (track->vap_vf_cntl & (1 << 14)) {
  2149. nverts = track->vap_alt_nverts;
  2150. } else {
  2151. nverts = (track->vap_vf_cntl >> 16) & 0xFFFF;
  2152. }
  2153. switch (prim_walk) {
  2154. case 1:
  2155. for (i = 0; i < track->num_arrays; i++) {
  2156. size = track->arrays[i].esize * track->max_indx * 4;
  2157. if (track->arrays[i].robj == NULL) {
  2158. DRM_ERROR("(PW %u) Vertex array %u no buffer "
  2159. "bound\n", prim_walk, i);
  2160. return -EINVAL;
  2161. }
  2162. if (size > radeon_bo_size(track->arrays[i].robj)) {
  2163. dev_err(rdev->dev, "(PW %u) Vertex array %u "
  2164. "need %lu dwords have %lu dwords\n",
  2165. prim_walk, i, size >> 2,
  2166. radeon_bo_size(track->arrays[i].robj)
  2167. >> 2);
  2168. DRM_ERROR("Max indices %u\n", track->max_indx);
  2169. return -EINVAL;
  2170. }
  2171. }
  2172. break;
  2173. case 2:
  2174. for (i = 0; i < track->num_arrays; i++) {
  2175. size = track->arrays[i].esize * (nverts - 1) * 4;
  2176. if (track->arrays[i].robj == NULL) {
  2177. DRM_ERROR("(PW %u) Vertex array %u no buffer "
  2178. "bound\n", prim_walk, i);
  2179. return -EINVAL;
  2180. }
  2181. if (size > radeon_bo_size(track->arrays[i].robj)) {
  2182. dev_err(rdev->dev, "(PW %u) Vertex array %u "
  2183. "need %lu dwords have %lu dwords\n",
  2184. prim_walk, i, size >> 2,
  2185. radeon_bo_size(track->arrays[i].robj)
  2186. >> 2);
  2187. return -EINVAL;
  2188. }
  2189. }
  2190. break;
  2191. case 3:
  2192. size = track->vtx_size * nverts;
  2193. if (size != track->immd_dwords) {
  2194. DRM_ERROR("IMMD draw %u dwors but needs %lu dwords\n",
  2195. track->immd_dwords, size);
  2196. DRM_ERROR("VAP_VF_CNTL.NUM_VERTICES %u, VTX_SIZE %u\n",
  2197. nverts, track->vtx_size);
  2198. return -EINVAL;
  2199. }
  2200. break;
  2201. default:
  2202. DRM_ERROR("[drm] Invalid primitive walk %d for VAP_VF_CNTL\n",
  2203. prim_walk);
  2204. return -EINVAL;
  2205. }
  2206. if (track->tex_dirty) {
  2207. track->tex_dirty = false;
  2208. return r100_cs_track_texture_check(rdev, track);
  2209. }
  2210. return 0;
  2211. }
  2212. void r100_cs_track_clear(struct radeon_device *rdev, struct r100_cs_track *track)
  2213. {
  2214. unsigned i, face;
  2215. track->cb_dirty = true;
  2216. track->zb_dirty = true;
  2217. track->tex_dirty = true;
  2218. track->aa_dirty = true;
  2219. if (rdev->family < CHIP_R300) {
  2220. track->num_cb = 1;
  2221. if (rdev->family <= CHIP_RS200)
  2222. track->num_texture = 3;
  2223. else
  2224. track->num_texture = 6;
  2225. track->maxy = 2048;
  2226. track->separate_cube = 1;
  2227. } else {
  2228. track->num_cb = 4;
  2229. track->num_texture = 16;
  2230. track->maxy = 4096;
  2231. track->separate_cube = 0;
  2232. track->aaresolve = false;
  2233. track->aa.robj = NULL;
  2234. }
  2235. for (i = 0; i < track->num_cb; i++) {
  2236. track->cb[i].robj = NULL;
  2237. track->cb[i].pitch = 8192;
  2238. track->cb[i].cpp = 16;
  2239. track->cb[i].offset = 0;
  2240. }
  2241. track->z_enabled = true;
  2242. track->zb.robj = NULL;
  2243. track->zb.pitch = 8192;
  2244. track->zb.cpp = 4;
  2245. track->zb.offset = 0;
  2246. track->vtx_size = 0x7F;
  2247. track->immd_dwords = 0xFFFFFFFFUL;
  2248. track->num_arrays = 11;
  2249. track->max_indx = 0x00FFFFFFUL;
  2250. for (i = 0; i < track->num_arrays; i++) {
  2251. track->arrays[i].robj = NULL;
  2252. track->arrays[i].esize = 0x7F;
  2253. }
  2254. for (i = 0; i < track->num_texture; i++) {
  2255. track->textures[i].compress_format = R100_TRACK_COMP_NONE;
  2256. track->textures[i].pitch = 16536;
  2257. track->textures[i].width = 16536;
  2258. track->textures[i].height = 16536;
  2259. track->textures[i].width_11 = 1 << 11;
  2260. track->textures[i].height_11 = 1 << 11;
  2261. track->textures[i].num_levels = 12;
  2262. if (rdev->family <= CHIP_RS200) {
  2263. track->textures[i].tex_coord_type = 0;
  2264. track->textures[i].txdepth = 0;
  2265. } else {
  2266. track->textures[i].txdepth = 16;
  2267. track->textures[i].tex_coord_type = 1;
  2268. }
  2269. track->textures[i].cpp = 64;
  2270. track->textures[i].robj = NULL;
  2271. /* CS IB emission code makes sure texture unit are disabled */
  2272. track->textures[i].enabled = false;
  2273. track->textures[i].lookup_disable = false;
  2274. track->textures[i].roundup_w = true;
  2275. track->textures[i].roundup_h = true;
  2276. if (track->separate_cube)
  2277. for (face = 0; face < 5; face++) {
  2278. track->textures[i].cube_info[face].robj = NULL;
  2279. track->textures[i].cube_info[face].width = 16536;
  2280. track->textures[i].cube_info[face].height = 16536;
  2281. track->textures[i].cube_info[face].offset = 0;
  2282. }
  2283. }
  2284. }
  2285. /*
  2286. * Global GPU functions
  2287. */
  2288. static void r100_errata(struct radeon_device *rdev)
  2289. {
  2290. rdev->pll_errata = 0;
  2291. if (rdev->family == CHIP_RV200 || rdev->family == CHIP_RS200) {
  2292. rdev->pll_errata |= CHIP_ERRATA_PLL_DUMMYREADS;
  2293. }
  2294. if (rdev->family == CHIP_RV100 ||
  2295. rdev->family == CHIP_RS100 ||
  2296. rdev->family == CHIP_RS200) {
  2297. rdev->pll_errata |= CHIP_ERRATA_PLL_DELAY;
  2298. }
  2299. }
  2300. static int r100_rbbm_fifo_wait_for_entry(struct radeon_device *rdev, unsigned n)
  2301. {
  2302. unsigned i;
  2303. uint32_t tmp;
  2304. for (i = 0; i < rdev->usec_timeout; i++) {
  2305. tmp = RREG32(RADEON_RBBM_STATUS) & RADEON_RBBM_FIFOCNT_MASK;
  2306. if (tmp >= n) {
  2307. return 0;
  2308. }
  2309. DRM_UDELAY(1);
  2310. }
  2311. return -1;
  2312. }
  2313. int r100_gui_wait_for_idle(struct radeon_device *rdev)
  2314. {
  2315. unsigned i;
  2316. uint32_t tmp;
  2317. if (r100_rbbm_fifo_wait_for_entry(rdev, 64)) {
  2318. printk(KERN_WARNING "radeon: wait for empty RBBM fifo failed !"
  2319. " Bad things might happen.\n");
  2320. }
  2321. for (i = 0; i < rdev->usec_timeout; i++) {
  2322. tmp = RREG32(RADEON_RBBM_STATUS);
  2323. if (!(tmp & RADEON_RBBM_ACTIVE)) {
  2324. return 0;
  2325. }
  2326. DRM_UDELAY(1);
  2327. }
  2328. return -1;
  2329. }
  2330. int r100_mc_wait_for_idle(struct radeon_device *rdev)
  2331. {
  2332. unsigned i;
  2333. uint32_t tmp;
  2334. for (i = 0; i < rdev->usec_timeout; i++) {
  2335. /* read MC_STATUS */
  2336. tmp = RREG32(RADEON_MC_STATUS);
  2337. if (tmp & RADEON_MC_IDLE) {
  2338. return 0;
  2339. }
  2340. DRM_UDELAY(1);
  2341. }
  2342. return -1;
  2343. }
  2344. bool r100_gpu_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
  2345. {
  2346. u32 rbbm_status;
  2347. rbbm_status = RREG32(R_000E40_RBBM_STATUS);
  2348. if (!G_000E40_GUI_ACTIVE(rbbm_status)) {
  2349. radeon_ring_lockup_update(rdev, ring);
  2350. return false;
  2351. }
  2352. return radeon_ring_test_lockup(rdev, ring);
  2353. }
  2354. /* required on r1xx, r2xx, r300, r(v)350, r420/r481, rs400/rs480 */
  2355. void r100_enable_bm(struct radeon_device *rdev)
  2356. {
  2357. uint32_t tmp;
  2358. /* Enable bus mastering */
  2359. tmp = RREG32(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
  2360. WREG32(RADEON_BUS_CNTL, tmp);
  2361. }
  2362. void r100_bm_disable(struct radeon_device *rdev)
  2363. {
  2364. u32 tmp;
  2365. /* disable bus mastering */
  2366. tmp = RREG32(R_000030_BUS_CNTL);
  2367. WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000044);
  2368. mdelay(1);
  2369. WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000042);
  2370. mdelay(1);
  2371. WREG32(R_000030_BUS_CNTL, (tmp & 0xFFFFFFFF) | 0x00000040);
  2372. tmp = RREG32(RADEON_BUS_CNTL);
  2373. mdelay(1);
  2374. pci_clear_master(rdev->pdev);
  2375. mdelay(1);
  2376. }
  2377. int r100_asic_reset(struct radeon_device *rdev)
  2378. {
  2379. struct r100_mc_save save;
  2380. u32 status, tmp;
  2381. int ret = 0;
  2382. status = RREG32(R_000E40_RBBM_STATUS);
  2383. if (!G_000E40_GUI_ACTIVE(status)) {
  2384. return 0;
  2385. }
  2386. r100_mc_stop(rdev, &save);
  2387. status = RREG32(R_000E40_RBBM_STATUS);
  2388. dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
  2389. /* stop CP */
  2390. WREG32(RADEON_CP_CSQ_CNTL, 0);
  2391. tmp = RREG32(RADEON_CP_RB_CNTL);
  2392. WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA);
  2393. WREG32(RADEON_CP_RB_RPTR_WR, 0);
  2394. WREG32(RADEON_CP_RB_WPTR, 0);
  2395. WREG32(RADEON_CP_RB_CNTL, tmp);
  2396. /* save PCI state */
  2397. pci_save_state(rdev->pdev);
  2398. /* disable bus mastering */
  2399. r100_bm_disable(rdev);
  2400. WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_SE(1) |
  2401. S_0000F0_SOFT_RESET_RE(1) |
  2402. S_0000F0_SOFT_RESET_PP(1) |
  2403. S_0000F0_SOFT_RESET_RB(1));
  2404. RREG32(R_0000F0_RBBM_SOFT_RESET);
  2405. mdelay(500);
  2406. WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
  2407. mdelay(1);
  2408. status = RREG32(R_000E40_RBBM_STATUS);
  2409. dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
  2410. /* reset CP */
  2411. WREG32(R_0000F0_RBBM_SOFT_RESET, S_0000F0_SOFT_RESET_CP(1));
  2412. RREG32(R_0000F0_RBBM_SOFT_RESET);
  2413. mdelay(500);
  2414. WREG32(R_0000F0_RBBM_SOFT_RESET, 0);
  2415. mdelay(1);
  2416. status = RREG32(R_000E40_RBBM_STATUS);
  2417. dev_info(rdev->dev, "(%s:%d) RBBM_STATUS=0x%08X\n", __func__, __LINE__, status);
  2418. /* restore PCI & busmastering */
  2419. pci_restore_state(rdev->pdev);
  2420. r100_enable_bm(rdev);
  2421. /* Check if GPU is idle */
  2422. if (G_000E40_SE_BUSY(status) || G_000E40_RE_BUSY(status) ||
  2423. G_000E40_TAM_BUSY(status) || G_000E40_PB_BUSY(status)) {
  2424. dev_err(rdev->dev, "failed to reset GPU\n");
  2425. ret = -1;
  2426. } else
  2427. dev_info(rdev->dev, "GPU reset succeed\n");
  2428. r100_mc_resume(rdev, &save);
  2429. return ret;
  2430. }
  2431. void r100_set_common_regs(struct radeon_device *rdev)
  2432. {
  2433. struct drm_device *dev = rdev->ddev;
  2434. bool force_dac2 = false;
  2435. u32 tmp;
  2436. /* set these so they don't interfere with anything */
  2437. WREG32(RADEON_OV0_SCALE_CNTL, 0);
  2438. WREG32(RADEON_SUBPIC_CNTL, 0);
  2439. WREG32(RADEON_VIPH_CONTROL, 0);
  2440. WREG32(RADEON_I2C_CNTL_1, 0);
  2441. WREG32(RADEON_DVI_I2C_CNTL_1, 0);
  2442. WREG32(RADEON_CAP0_TRIG_CNTL, 0);
  2443. WREG32(RADEON_CAP1_TRIG_CNTL, 0);
  2444. /* always set up dac2 on rn50 and some rv100 as lots
  2445. * of servers seem to wire it up to a VGA port but
  2446. * don't report it in the bios connector
  2447. * table.
  2448. */
  2449. switch (dev->pdev->device) {
  2450. /* RN50 */
  2451. case 0x515e:
  2452. case 0x5969:
  2453. force_dac2 = true;
  2454. break;
  2455. /* RV100*/
  2456. case 0x5159:
  2457. case 0x515a:
  2458. /* DELL triple head servers */
  2459. if ((dev->pdev->subsystem_vendor == 0x1028 /* DELL */) &&
  2460. ((dev->pdev->subsystem_device == 0x016c) ||
  2461. (dev->pdev->subsystem_device == 0x016d) ||
  2462. (dev->pdev->subsystem_device == 0x016e) ||
  2463. (dev->pdev->subsystem_device == 0x016f) ||
  2464. (dev->pdev->subsystem_device == 0x0170) ||
  2465. (dev->pdev->subsystem_device == 0x017d) ||
  2466. (dev->pdev->subsystem_device == 0x017e) ||
  2467. (dev->pdev->subsystem_device == 0x0183) ||
  2468. (dev->pdev->subsystem_device == 0x018a) ||
  2469. (dev->pdev->subsystem_device == 0x019a)))
  2470. force_dac2 = true;
  2471. break;
  2472. }
  2473. if (force_dac2) {
  2474. u32 disp_hw_debug = RREG32(RADEON_DISP_HW_DEBUG);
  2475. u32 tv_dac_cntl = RREG32(RADEON_TV_DAC_CNTL);
  2476. u32 dac2_cntl = RREG32(RADEON_DAC_CNTL2);
  2477. /* For CRT on DAC2, don't turn it on if BIOS didn't
  2478. enable it, even it's detected.
  2479. */
  2480. /* force it to crtc0 */
  2481. dac2_cntl &= ~RADEON_DAC2_DAC_CLK_SEL;
  2482. dac2_cntl |= RADEON_DAC2_DAC2_CLK_SEL;
  2483. disp_hw_debug |= RADEON_CRT2_DISP1_SEL;
  2484. /* set up the TV DAC */
  2485. tv_dac_cntl &= ~(RADEON_TV_DAC_PEDESTAL |
  2486. RADEON_TV_DAC_STD_MASK |
  2487. RADEON_TV_DAC_RDACPD |
  2488. RADEON_TV_DAC_GDACPD |
  2489. RADEON_TV_DAC_BDACPD |
  2490. RADEON_TV_DAC_BGADJ_MASK |
  2491. RADEON_TV_DAC_DACADJ_MASK);
  2492. tv_dac_cntl |= (RADEON_TV_DAC_NBLANK |
  2493. RADEON_TV_DAC_NHOLD |
  2494. RADEON_TV_DAC_STD_PS2 |
  2495. (0x58 << 16));
  2496. WREG32(RADEON_TV_DAC_CNTL, tv_dac_cntl);
  2497. WREG32(RADEON_DISP_HW_DEBUG, disp_hw_debug);
  2498. WREG32(RADEON_DAC_CNTL2, dac2_cntl);
  2499. }
  2500. /* switch PM block to ACPI mode */
  2501. tmp = RREG32_PLL(RADEON_PLL_PWRMGT_CNTL);
  2502. tmp &= ~RADEON_PM_MODE_SEL;
  2503. WREG32_PLL(RADEON_PLL_PWRMGT_CNTL, tmp);
  2504. }
  2505. /*
  2506. * VRAM info
  2507. */
  2508. static void r100_vram_get_type(struct radeon_device *rdev)
  2509. {
  2510. uint32_t tmp;
  2511. rdev->mc.vram_is_ddr = false;
  2512. if (rdev->flags & RADEON_IS_IGP)
  2513. rdev->mc.vram_is_ddr = true;
  2514. else if (RREG32(RADEON_MEM_SDRAM_MODE_REG) & RADEON_MEM_CFG_TYPE_DDR)
  2515. rdev->mc.vram_is_ddr = true;
  2516. if ((rdev->family == CHIP_RV100) ||
  2517. (rdev->family == CHIP_RS100) ||
  2518. (rdev->family == CHIP_RS200)) {
  2519. tmp = RREG32(RADEON_MEM_CNTL);
  2520. if (tmp & RV100_HALF_MODE) {
  2521. rdev->mc.vram_width = 32;
  2522. } else {
  2523. rdev->mc.vram_width = 64;
  2524. }
  2525. if (rdev->flags & RADEON_SINGLE_CRTC) {
  2526. rdev->mc.vram_width /= 4;
  2527. rdev->mc.vram_is_ddr = true;
  2528. }
  2529. } else if (rdev->family <= CHIP_RV280) {
  2530. tmp = RREG32(RADEON_MEM_CNTL);
  2531. if (tmp & RADEON_MEM_NUM_CHANNELS_MASK) {
  2532. rdev->mc.vram_width = 128;
  2533. } else {
  2534. rdev->mc.vram_width = 64;
  2535. }
  2536. } else {
  2537. /* newer IGPs */
  2538. rdev->mc.vram_width = 128;
  2539. }
  2540. }
  2541. static u32 r100_get_accessible_vram(struct radeon_device *rdev)
  2542. {
  2543. u32 aper_size;
  2544. u8 byte;
  2545. aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
  2546. /* Set HDP_APER_CNTL only on cards that are known not to be broken,
  2547. * that is has the 2nd generation multifunction PCI interface
  2548. */
  2549. if (rdev->family == CHIP_RV280 ||
  2550. rdev->family >= CHIP_RV350) {
  2551. WREG32_P(RADEON_HOST_PATH_CNTL, RADEON_HDP_APER_CNTL,
  2552. ~RADEON_HDP_APER_CNTL);
  2553. DRM_INFO("Generation 2 PCI interface, using max accessible memory\n");
  2554. return aper_size * 2;
  2555. }
  2556. /* Older cards have all sorts of funny issues to deal with. First
  2557. * check if it's a multifunction card by reading the PCI config
  2558. * header type... Limit those to one aperture size
  2559. */
  2560. pci_read_config_byte(rdev->pdev, 0xe, &byte);
  2561. if (byte & 0x80) {
  2562. DRM_INFO("Generation 1 PCI interface in multifunction mode\n");
  2563. DRM_INFO("Limiting VRAM to one aperture\n");
  2564. return aper_size;
  2565. }
  2566. /* Single function older card. We read HDP_APER_CNTL to see how the BIOS
  2567. * have set it up. We don't write this as it's broken on some ASICs but
  2568. * we expect the BIOS to have done the right thing (might be too optimistic...)
  2569. */
  2570. if (RREG32(RADEON_HOST_PATH_CNTL) & RADEON_HDP_APER_CNTL)
  2571. return aper_size * 2;
  2572. return aper_size;
  2573. }
  2574. void r100_vram_init_sizes(struct radeon_device *rdev)
  2575. {
  2576. u64 config_aper_size;
  2577. /* work out accessible VRAM */
  2578. rdev->mc.aper_base = pci_resource_start(rdev->pdev, 0);
  2579. rdev->mc.aper_size = pci_resource_len(rdev->pdev, 0);
  2580. rdev->mc.visible_vram_size = r100_get_accessible_vram(rdev);
  2581. /* FIXME we don't use the second aperture yet when we could use it */
  2582. if (rdev->mc.visible_vram_size > rdev->mc.aper_size)
  2583. rdev->mc.visible_vram_size = rdev->mc.aper_size;
  2584. config_aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
  2585. if (rdev->flags & RADEON_IS_IGP) {
  2586. uint32_t tom;
  2587. /* read NB_TOM to get the amount of ram stolen for the GPU */
  2588. tom = RREG32(RADEON_NB_TOM);
  2589. rdev->mc.real_vram_size = (((tom >> 16) - (tom & 0xffff) + 1) << 16);
  2590. WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
  2591. rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
  2592. } else {
  2593. rdev->mc.real_vram_size = RREG32(RADEON_CONFIG_MEMSIZE);
  2594. /* Some production boards of m6 will report 0
  2595. * if it's 8 MB
  2596. */
  2597. if (rdev->mc.real_vram_size == 0) {
  2598. rdev->mc.real_vram_size = 8192 * 1024;
  2599. WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
  2600. }
  2601. /* Fix for RN50, M6, M7 with 8/16/32(??) MBs of VRAM -
  2602. * Novell bug 204882 + along with lots of ubuntu ones
  2603. */
  2604. if (rdev->mc.aper_size > config_aper_size)
  2605. config_aper_size = rdev->mc.aper_size;
  2606. if (config_aper_size > rdev->mc.real_vram_size)
  2607. rdev->mc.mc_vram_size = config_aper_size;
  2608. else
  2609. rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
  2610. }
  2611. }
  2612. void r100_vga_set_state(struct radeon_device *rdev, bool state)
  2613. {
  2614. uint32_t temp;
  2615. temp = RREG32(RADEON_CONFIG_CNTL);
  2616. if (state == false) {
  2617. temp &= ~RADEON_CFG_VGA_RAM_EN;
  2618. temp |= RADEON_CFG_VGA_IO_DIS;
  2619. } else {
  2620. temp &= ~RADEON_CFG_VGA_IO_DIS;
  2621. }
  2622. WREG32(RADEON_CONFIG_CNTL, temp);
  2623. }
  2624. static void r100_mc_init(struct radeon_device *rdev)
  2625. {
  2626. u64 base;
  2627. r100_vram_get_type(rdev);
  2628. r100_vram_init_sizes(rdev);
  2629. base = rdev->mc.aper_base;
  2630. if (rdev->flags & RADEON_IS_IGP)
  2631. base = (RREG32(RADEON_NB_TOM) & 0xffff) << 16;
  2632. radeon_vram_location(rdev, &rdev->mc, base);
  2633. rdev->mc.gtt_base_align = 0;
  2634. if (!(rdev->flags & RADEON_IS_AGP))
  2635. radeon_gtt_location(rdev, &rdev->mc);
  2636. radeon_update_bandwidth_info(rdev);
  2637. }
  2638. /*
  2639. * Indirect registers accessor
  2640. */
  2641. void r100_pll_errata_after_index(struct radeon_device *rdev)
  2642. {
  2643. if (rdev->pll_errata & CHIP_ERRATA_PLL_DUMMYREADS) {
  2644. (void)RREG32(RADEON_CLOCK_CNTL_DATA);
  2645. (void)RREG32(RADEON_CRTC_GEN_CNTL);
  2646. }
  2647. }
  2648. static void r100_pll_errata_after_data(struct radeon_device *rdev)
  2649. {
  2650. /* This workarounds is necessary on RV100, RS100 and RS200 chips
  2651. * or the chip could hang on a subsequent access
  2652. */
  2653. if (rdev->pll_errata & CHIP_ERRATA_PLL_DELAY) {
  2654. mdelay(5);
  2655. }
  2656. /* This function is required to workaround a hardware bug in some (all?)
  2657. * revisions of the R300. This workaround should be called after every
  2658. * CLOCK_CNTL_INDEX register access. If not, register reads afterward
  2659. * may not be correct.
  2660. */
  2661. if (rdev->pll_errata & CHIP_ERRATA_R300_CG) {
  2662. uint32_t save, tmp;
  2663. save = RREG32(RADEON_CLOCK_CNTL_INDEX);
  2664. tmp = save & ~(0x3f | RADEON_PLL_WR_EN);
  2665. WREG32(RADEON_CLOCK_CNTL_INDEX, tmp);
  2666. tmp = RREG32(RADEON_CLOCK_CNTL_DATA);
  2667. WREG32(RADEON_CLOCK_CNTL_INDEX, save);
  2668. }
  2669. }
  2670. uint32_t r100_pll_rreg(struct radeon_device *rdev, uint32_t reg)
  2671. {
  2672. unsigned long flags;
  2673. uint32_t data;
  2674. spin_lock_irqsave(&rdev->pll_idx_lock, flags);
  2675. WREG8(RADEON_CLOCK_CNTL_INDEX, reg & 0x3f);
  2676. r100_pll_errata_after_index(rdev);
  2677. data = RREG32(RADEON_CLOCK_CNTL_DATA);
  2678. r100_pll_errata_after_data(rdev);
  2679. spin_unlock_irqrestore(&rdev->pll_idx_lock, flags);
  2680. return data;
  2681. }
  2682. void r100_pll_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
  2683. {
  2684. unsigned long flags;
  2685. spin_lock_irqsave(&rdev->pll_idx_lock, flags);
  2686. WREG8(RADEON_CLOCK_CNTL_INDEX, ((reg & 0x3f) | RADEON_PLL_WR_EN));
  2687. r100_pll_errata_after_index(rdev);
  2688. WREG32(RADEON_CLOCK_CNTL_DATA, v);
  2689. r100_pll_errata_after_data(rdev);
  2690. spin_unlock_irqrestore(&rdev->pll_idx_lock, flags);
  2691. }
  2692. static void r100_set_safe_registers(struct radeon_device *rdev)
  2693. {
  2694. if (ASIC_IS_RN50(rdev)) {
  2695. rdev->config.r100.reg_safe_bm = rn50_reg_safe_bm;
  2696. rdev->config.r100.reg_safe_bm_size = ARRAY_SIZE(rn50_reg_safe_bm);
  2697. } else if (rdev->family < CHIP_R200) {
  2698. rdev->config.r100.reg_safe_bm = r100_reg_safe_bm;
  2699. rdev->config.r100.reg_safe_bm_size = ARRAY_SIZE(r100_reg_safe_bm);
  2700. } else {
  2701. r200_set_safe_registers(rdev);
  2702. }
  2703. }
  2704. /*
  2705. * Debugfs info
  2706. */
  2707. #if defined(CONFIG_DEBUG_FS)
  2708. static int r100_debugfs_rbbm_info(struct seq_file *m, void *data)
  2709. {
  2710. struct drm_info_node *node = (struct drm_info_node *) m->private;
  2711. struct drm_device *dev = node->minor->dev;
  2712. struct radeon_device *rdev = dev->dev_private;
  2713. uint32_t reg, value;
  2714. unsigned i;
  2715. seq_printf(m, "RBBM_STATUS 0x%08x\n", RREG32(RADEON_RBBM_STATUS));
  2716. seq_printf(m, "RBBM_CMDFIFO_STAT 0x%08x\n", RREG32(0xE7C));
  2717. seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
  2718. for (i = 0; i < 64; i++) {
  2719. WREG32(RADEON_RBBM_CMDFIFO_ADDR, i | 0x100);
  2720. reg = (RREG32(RADEON_RBBM_CMDFIFO_DATA) - 1) >> 2;
  2721. WREG32(RADEON_RBBM_CMDFIFO_ADDR, i);
  2722. value = RREG32(RADEON_RBBM_CMDFIFO_DATA);
  2723. seq_printf(m, "[0x%03X] 0x%04X=0x%08X\n", i, reg, value);
  2724. }
  2725. return 0;
  2726. }
  2727. static int r100_debugfs_cp_ring_info(struct seq_file *m, void *data)
  2728. {
  2729. struct drm_info_node *node = (struct drm_info_node *) m->private;
  2730. struct drm_device *dev = node->minor->dev;
  2731. struct radeon_device *rdev = dev->dev_private;
  2732. struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
  2733. uint32_t rdp, wdp;
  2734. unsigned count, i, j;
  2735. radeon_ring_free_size(rdev, ring);
  2736. rdp = RREG32(RADEON_CP_RB_RPTR);
  2737. wdp = RREG32(RADEON_CP_RB_WPTR);
  2738. count = (rdp + ring->ring_size - wdp) & ring->ptr_mask;
  2739. seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
  2740. seq_printf(m, "CP_RB_WPTR 0x%08x\n", wdp);
  2741. seq_printf(m, "CP_RB_RPTR 0x%08x\n", rdp);
  2742. seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
  2743. seq_printf(m, "%u dwords in ring\n", count);
  2744. if (ring->ready) {
  2745. for (j = 0; j <= count; j++) {
  2746. i = (rdp + j) & ring->ptr_mask;
  2747. seq_printf(m, "r[%04d]=0x%08x\n", i, ring->ring[i]);
  2748. }
  2749. }
  2750. return 0;
  2751. }
  2752. static int r100_debugfs_cp_csq_fifo(struct seq_file *m, void *data)
  2753. {
  2754. struct drm_info_node *node = (struct drm_info_node *) m->private;
  2755. struct drm_device *dev = node->minor->dev;
  2756. struct radeon_device *rdev = dev->dev_private;
  2757. uint32_t csq_stat, csq2_stat, tmp;
  2758. unsigned r_rptr, r_wptr, ib1_rptr, ib1_wptr, ib2_rptr, ib2_wptr;
  2759. unsigned i;
  2760. seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
  2761. seq_printf(m, "CP_CSQ_MODE 0x%08x\n", RREG32(RADEON_CP_CSQ_MODE));
  2762. csq_stat = RREG32(RADEON_CP_CSQ_STAT);
  2763. csq2_stat = RREG32(RADEON_CP_CSQ2_STAT);
  2764. r_rptr = (csq_stat >> 0) & 0x3ff;
  2765. r_wptr = (csq_stat >> 10) & 0x3ff;
  2766. ib1_rptr = (csq_stat >> 20) & 0x3ff;
  2767. ib1_wptr = (csq2_stat >> 0) & 0x3ff;
  2768. ib2_rptr = (csq2_stat >> 10) & 0x3ff;
  2769. ib2_wptr = (csq2_stat >> 20) & 0x3ff;
  2770. seq_printf(m, "CP_CSQ_STAT 0x%08x\n", csq_stat);
  2771. seq_printf(m, "CP_CSQ2_STAT 0x%08x\n", csq2_stat);
  2772. seq_printf(m, "Ring rptr %u\n", r_rptr);
  2773. seq_printf(m, "Ring wptr %u\n", r_wptr);
  2774. seq_printf(m, "Indirect1 rptr %u\n", ib1_rptr);
  2775. seq_printf(m, "Indirect1 wptr %u\n", ib1_wptr);
  2776. seq_printf(m, "Indirect2 rptr %u\n", ib2_rptr);
  2777. seq_printf(m, "Indirect2 wptr %u\n", ib2_wptr);
  2778. /* FIXME: 0, 128, 640 depends on fifo setup see cp_init_kms
  2779. * 128 = indirect1_start * 8 & 640 = indirect2_start * 8 */
  2780. seq_printf(m, "Ring fifo:\n");
  2781. for (i = 0; i < 256; i++) {
  2782. WREG32(RADEON_CP_CSQ_ADDR, i << 2);
  2783. tmp = RREG32(RADEON_CP_CSQ_DATA);
  2784. seq_printf(m, "rfifo[%04d]=0x%08X\n", i, tmp);
  2785. }
  2786. seq_printf(m, "Indirect1 fifo:\n");
  2787. for (i = 256; i <= 512; i++) {
  2788. WREG32(RADEON_CP_CSQ_ADDR, i << 2);
  2789. tmp = RREG32(RADEON_CP_CSQ_DATA);
  2790. seq_printf(m, "ib1fifo[%04d]=0x%08X\n", i, tmp);
  2791. }
  2792. seq_printf(m, "Indirect2 fifo:\n");
  2793. for (i = 640; i < ib1_wptr; i++) {
  2794. WREG32(RADEON_CP_CSQ_ADDR, i << 2);
  2795. tmp = RREG32(RADEON_CP_CSQ_DATA);
  2796. seq_printf(m, "ib2fifo[%04d]=0x%08X\n", i, tmp);
  2797. }
  2798. return 0;
  2799. }
  2800. static int r100_debugfs_mc_info(struct seq_file *m, void *data)
  2801. {
  2802. struct drm_info_node *node = (struct drm_info_node *) m->private;
  2803. struct drm_device *dev = node->minor->dev;
  2804. struct radeon_device *rdev = dev->dev_private;
  2805. uint32_t tmp;
  2806. tmp = RREG32(RADEON_CONFIG_MEMSIZE);
  2807. seq_printf(m, "CONFIG_MEMSIZE 0x%08x\n", tmp);
  2808. tmp = RREG32(RADEON_MC_FB_LOCATION);
  2809. seq_printf(m, "MC_FB_LOCATION 0x%08x\n", tmp);
  2810. tmp = RREG32(RADEON_BUS_CNTL);
  2811. seq_printf(m, "BUS_CNTL 0x%08x\n", tmp);
  2812. tmp = RREG32(RADEON_MC_AGP_LOCATION);
  2813. seq_printf(m, "MC_AGP_LOCATION 0x%08x\n", tmp);
  2814. tmp = RREG32(RADEON_AGP_BASE);
  2815. seq_printf(m, "AGP_BASE 0x%08x\n", tmp);
  2816. tmp = RREG32(RADEON_HOST_PATH_CNTL);
  2817. seq_printf(m, "HOST_PATH_CNTL 0x%08x\n", tmp);
  2818. tmp = RREG32(0x01D0);
  2819. seq_printf(m, "AIC_CTRL 0x%08x\n", tmp);
  2820. tmp = RREG32(RADEON_AIC_LO_ADDR);
  2821. seq_printf(m, "AIC_LO_ADDR 0x%08x\n", tmp);
  2822. tmp = RREG32(RADEON_AIC_HI_ADDR);
  2823. seq_printf(m, "AIC_HI_ADDR 0x%08x\n", tmp);
  2824. tmp = RREG32(0x01E4);
  2825. seq_printf(m, "AIC_TLB_ADDR 0x%08x\n", tmp);
  2826. return 0;
  2827. }
  2828. static struct drm_info_list r100_debugfs_rbbm_list[] = {
  2829. {"r100_rbbm_info", r100_debugfs_rbbm_info, 0, NULL},
  2830. };
  2831. static struct drm_info_list r100_debugfs_cp_list[] = {
  2832. {"r100_cp_ring_info", r100_debugfs_cp_ring_info, 0, NULL},
  2833. {"r100_cp_csq_fifo", r100_debugfs_cp_csq_fifo, 0, NULL},
  2834. };
  2835. static struct drm_info_list r100_debugfs_mc_info_list[] = {
  2836. {"r100_mc_info", r100_debugfs_mc_info, 0, NULL},
  2837. };
  2838. #endif
  2839. int r100_debugfs_rbbm_init(struct radeon_device *rdev)
  2840. {
  2841. #if defined(CONFIG_DEBUG_FS)
  2842. return radeon_debugfs_add_files(rdev, r100_debugfs_rbbm_list, 1);
  2843. #else
  2844. return 0;
  2845. #endif
  2846. }
  2847. int r100_debugfs_cp_init(struct radeon_device *rdev)
  2848. {
  2849. #if defined(CONFIG_DEBUG_FS)
  2850. return radeon_debugfs_add_files(rdev, r100_debugfs_cp_list, 2);
  2851. #else
  2852. return 0;
  2853. #endif
  2854. }
  2855. int r100_debugfs_mc_info_init(struct radeon_device *rdev)
  2856. {
  2857. #if defined(CONFIG_DEBUG_FS)
  2858. return radeon_debugfs_add_files(rdev, r100_debugfs_mc_info_list, 1);
  2859. #else
  2860. return 0;
  2861. #endif
  2862. }
  2863. int r100_set_surface_reg(struct radeon_device *rdev, int reg,
  2864. uint32_t tiling_flags, uint32_t pitch,
  2865. uint32_t offset, uint32_t obj_size)
  2866. {
  2867. int surf_index = reg * 16;
  2868. int flags = 0;
  2869. if (rdev->family <= CHIP_RS200) {
  2870. if ((tiling_flags & (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
  2871. == (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
  2872. flags |= RADEON_SURF_TILE_COLOR_BOTH;
  2873. if (tiling_flags & RADEON_TILING_MACRO)
  2874. flags |= RADEON_SURF_TILE_COLOR_MACRO;
  2875. /* setting pitch to 0 disables tiling */
  2876. if ((tiling_flags & (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
  2877. == 0)
  2878. pitch = 0;
  2879. } else if (rdev->family <= CHIP_RV280) {
  2880. if (tiling_flags & (RADEON_TILING_MACRO))
  2881. flags |= R200_SURF_TILE_COLOR_MACRO;
  2882. if (tiling_flags & RADEON_TILING_MICRO)
  2883. flags |= R200_SURF_TILE_COLOR_MICRO;
  2884. } else {
  2885. if (tiling_flags & RADEON_TILING_MACRO)
  2886. flags |= R300_SURF_TILE_MACRO;
  2887. if (tiling_flags & RADEON_TILING_MICRO)
  2888. flags |= R300_SURF_TILE_MICRO;
  2889. }
  2890. if (tiling_flags & RADEON_TILING_SWAP_16BIT)
  2891. flags |= RADEON_SURF_AP0_SWP_16BPP | RADEON_SURF_AP1_SWP_16BPP;
  2892. if (tiling_flags & RADEON_TILING_SWAP_32BIT)
  2893. flags |= RADEON_SURF_AP0_SWP_32BPP | RADEON_SURF_AP1_SWP_32BPP;
  2894. /* r100/r200 divide by 16 */
  2895. if (rdev->family < CHIP_R300)
  2896. flags |= pitch / 16;
  2897. else
  2898. flags |= pitch / 8;
  2899. DRM_DEBUG_KMS("writing surface %d %d %x %x\n", reg, flags, offset, offset+obj_size-1);
  2900. WREG32(RADEON_SURFACE0_INFO + surf_index, flags);
  2901. WREG32(RADEON_SURFACE0_LOWER_BOUND + surf_index, offset);
  2902. WREG32(RADEON_SURFACE0_UPPER_BOUND + surf_index, offset + obj_size - 1);
  2903. return 0;
  2904. }
  2905. void r100_clear_surface_reg(struct radeon_device *rdev, int reg)
  2906. {
  2907. int surf_index = reg * 16;
  2908. WREG32(RADEON_SURFACE0_INFO + surf_index, 0);
  2909. }
  2910. void r100_bandwidth_update(struct radeon_device *rdev)
  2911. {
  2912. fixed20_12 trcd_ff, trp_ff, tras_ff, trbs_ff, tcas_ff;
  2913. fixed20_12 sclk_ff, mclk_ff, sclk_eff_ff, sclk_delay_ff;
  2914. fixed20_12 peak_disp_bw, mem_bw, pix_clk, pix_clk2, temp_ff, crit_point_ff;
  2915. uint32_t temp, data, mem_trcd, mem_trp, mem_tras;
  2916. fixed20_12 memtcas_ff[8] = {
  2917. dfixed_init(1),
  2918. dfixed_init(2),
  2919. dfixed_init(3),
  2920. dfixed_init(0),
  2921. dfixed_init_half(1),
  2922. dfixed_init_half(2),
  2923. dfixed_init(0),
  2924. };
  2925. fixed20_12 memtcas_rs480_ff[8] = {
  2926. dfixed_init(0),
  2927. dfixed_init(1),
  2928. dfixed_init(2),
  2929. dfixed_init(3),
  2930. dfixed_init(0),
  2931. dfixed_init_half(1),
  2932. dfixed_init_half(2),
  2933. dfixed_init_half(3),
  2934. };
  2935. fixed20_12 memtcas2_ff[8] = {
  2936. dfixed_init(0),
  2937. dfixed_init(1),
  2938. dfixed_init(2),
  2939. dfixed_init(3),
  2940. dfixed_init(4),
  2941. dfixed_init(5),
  2942. dfixed_init(6),
  2943. dfixed_init(7),
  2944. };
  2945. fixed20_12 memtrbs[8] = {
  2946. dfixed_init(1),
  2947. dfixed_init_half(1),
  2948. dfixed_init(2),
  2949. dfixed_init_half(2),
  2950. dfixed_init(3),
  2951. dfixed_init_half(3),
  2952. dfixed_init(4),
  2953. dfixed_init_half(4)
  2954. };
  2955. fixed20_12 memtrbs_r4xx[8] = {
  2956. dfixed_init(4),
  2957. dfixed_init(5),
  2958. dfixed_init(6),
  2959. dfixed_init(7),
  2960. dfixed_init(8),
  2961. dfixed_init(9),
  2962. dfixed_init(10),
  2963. dfixed_init(11)
  2964. };
  2965. fixed20_12 min_mem_eff;
  2966. fixed20_12 mc_latency_sclk, mc_latency_mclk, k1;
  2967. fixed20_12 cur_latency_mclk, cur_latency_sclk;
  2968. fixed20_12 disp_latency, disp_latency_overhead, disp_drain_rate,
  2969. disp_drain_rate2, read_return_rate;
  2970. fixed20_12 time_disp1_drop_priority;
  2971. int c;
  2972. int cur_size = 16; /* in octawords */
  2973. int critical_point = 0, critical_point2;
  2974. /* uint32_t read_return_rate, time_disp1_drop_priority; */
  2975. int stop_req, max_stop_req;
  2976. struct drm_display_mode *mode1 = NULL;
  2977. struct drm_display_mode *mode2 = NULL;
  2978. uint32_t pixel_bytes1 = 0;
  2979. uint32_t pixel_bytes2 = 0;
  2980. /* Guess line buffer size to be 8192 pixels */
  2981. u32 lb_size = 8192;
  2982. if (!rdev->mode_info.mode_config_initialized)
  2983. return;
  2984. radeon_update_display_priority(rdev);
  2985. if (rdev->mode_info.crtcs[0]->base.enabled) {
  2986. mode1 = &rdev->mode_info.crtcs[0]->base.mode;
  2987. pixel_bytes1 = rdev->mode_info.crtcs[0]->base.primary->fb->bits_per_pixel / 8;
  2988. }
  2989. if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
  2990. if (rdev->mode_info.crtcs[1]->base.enabled) {
  2991. mode2 = &rdev->mode_info.crtcs[1]->base.mode;
  2992. pixel_bytes2 = rdev->mode_info.crtcs[1]->base.primary->fb->bits_per_pixel / 8;
  2993. }
  2994. }
  2995. min_mem_eff.full = dfixed_const_8(0);
  2996. /* get modes */
  2997. if ((rdev->disp_priority == 2) && ASIC_IS_R300(rdev)) {
  2998. uint32_t mc_init_misc_lat_timer = RREG32(R300_MC_INIT_MISC_LAT_TIMER);
  2999. mc_init_misc_lat_timer &= ~(R300_MC_DISP1R_INIT_LAT_MASK << R300_MC_DISP1R_INIT_LAT_SHIFT);
  3000. mc_init_misc_lat_timer &= ~(R300_MC_DISP0R_INIT_LAT_MASK << R300_MC_DISP0R_INIT_LAT_SHIFT);
  3001. /* check crtc enables */
  3002. if (mode2)
  3003. mc_init_misc_lat_timer |= (1 << R300_MC_DISP1R_INIT_LAT_SHIFT);
  3004. if (mode1)
  3005. mc_init_misc_lat_timer |= (1 << R300_MC_DISP0R_INIT_LAT_SHIFT);
  3006. WREG32(R300_MC_INIT_MISC_LAT_TIMER, mc_init_misc_lat_timer);
  3007. }
  3008. /*
  3009. * determine is there is enough bw for current mode
  3010. */
  3011. sclk_ff = rdev->pm.sclk;
  3012. mclk_ff = rdev->pm.mclk;
  3013. temp = (rdev->mc.vram_width / 8) * (rdev->mc.vram_is_ddr ? 2 : 1);
  3014. temp_ff.full = dfixed_const(temp);
  3015. mem_bw.full = dfixed_mul(mclk_ff, temp_ff);
  3016. pix_clk.full = 0;
  3017. pix_clk2.full = 0;
  3018. peak_disp_bw.full = 0;
  3019. if (mode1) {
  3020. temp_ff.full = dfixed_const(1000);
  3021. pix_clk.full = dfixed_const(mode1->clock); /* convert to fixed point */
  3022. pix_clk.full = dfixed_div(pix_clk, temp_ff);
  3023. temp_ff.full = dfixed_const(pixel_bytes1);
  3024. peak_disp_bw.full += dfixed_mul(pix_clk, temp_ff);
  3025. }
  3026. if (mode2) {
  3027. temp_ff.full = dfixed_const(1000);
  3028. pix_clk2.full = dfixed_const(mode2->clock); /* convert to fixed point */
  3029. pix_clk2.full = dfixed_div(pix_clk2, temp_ff);
  3030. temp_ff.full = dfixed_const(pixel_bytes2);
  3031. peak_disp_bw.full += dfixed_mul(pix_clk2, temp_ff);
  3032. }
  3033. mem_bw.full = dfixed_mul(mem_bw, min_mem_eff);
  3034. if (peak_disp_bw.full >= mem_bw.full) {
  3035. DRM_ERROR("You may not have enough display bandwidth for current mode\n"
  3036. "If you have flickering problem, try to lower resolution, refresh rate, or color depth\n");
  3037. }
  3038. /* Get values from the EXT_MEM_CNTL register...converting its contents. */
  3039. temp = RREG32(RADEON_MEM_TIMING_CNTL);
  3040. if ((rdev->family == CHIP_RV100) || (rdev->flags & RADEON_IS_IGP)) { /* RV100, M6, IGPs */
  3041. mem_trcd = ((temp >> 2) & 0x3) + 1;
  3042. mem_trp = ((temp & 0x3)) + 1;
  3043. mem_tras = ((temp & 0x70) >> 4) + 1;
  3044. } else if (rdev->family == CHIP_R300 ||
  3045. rdev->family == CHIP_R350) { /* r300, r350 */
  3046. mem_trcd = (temp & 0x7) + 1;
  3047. mem_trp = ((temp >> 8) & 0x7) + 1;
  3048. mem_tras = ((temp >> 11) & 0xf) + 4;
  3049. } else if (rdev->family == CHIP_RV350 ||
  3050. rdev->family <= CHIP_RV380) {
  3051. /* rv3x0 */
  3052. mem_trcd = (temp & 0x7) + 3;
  3053. mem_trp = ((temp >> 8) & 0x7) + 3;
  3054. mem_tras = ((temp >> 11) & 0xf) + 6;
  3055. } else if (rdev->family == CHIP_R420 ||
  3056. rdev->family == CHIP_R423 ||
  3057. rdev->family == CHIP_RV410) {
  3058. /* r4xx */
  3059. mem_trcd = (temp & 0xf) + 3;
  3060. if (mem_trcd > 15)
  3061. mem_trcd = 15;
  3062. mem_trp = ((temp >> 8) & 0xf) + 3;
  3063. if (mem_trp > 15)
  3064. mem_trp = 15;
  3065. mem_tras = ((temp >> 12) & 0x1f) + 6;
  3066. if (mem_tras > 31)
  3067. mem_tras = 31;
  3068. } else { /* RV200, R200 */
  3069. mem_trcd = (temp & 0x7) + 1;
  3070. mem_trp = ((temp >> 8) & 0x7) + 1;
  3071. mem_tras = ((temp >> 12) & 0xf) + 4;
  3072. }
  3073. /* convert to FF */
  3074. trcd_ff.full = dfixed_const(mem_trcd);
  3075. trp_ff.full = dfixed_const(mem_trp);
  3076. tras_ff.full = dfixed_const(mem_tras);
  3077. /* Get values from the MEM_SDRAM_MODE_REG register...converting its */
  3078. temp = RREG32(RADEON_MEM_SDRAM_MODE_REG);
  3079. data = (temp & (7 << 20)) >> 20;
  3080. if ((rdev->family == CHIP_RV100) || rdev->flags & RADEON_IS_IGP) {
  3081. if (rdev->family == CHIP_RS480) /* don't think rs400 */
  3082. tcas_ff = memtcas_rs480_ff[data];
  3083. else
  3084. tcas_ff = memtcas_ff[data];
  3085. } else
  3086. tcas_ff = memtcas2_ff[data];
  3087. if (rdev->family == CHIP_RS400 ||
  3088. rdev->family == CHIP_RS480) {
  3089. /* extra cas latency stored in bits 23-25 0-4 clocks */
  3090. data = (temp >> 23) & 0x7;
  3091. if (data < 5)
  3092. tcas_ff.full += dfixed_const(data);
  3093. }
  3094. if (ASIC_IS_R300(rdev) && !(rdev->flags & RADEON_IS_IGP)) {
  3095. /* on the R300, Tcas is included in Trbs.
  3096. */
  3097. temp = RREG32(RADEON_MEM_CNTL);
  3098. data = (R300_MEM_NUM_CHANNELS_MASK & temp);
  3099. if (data == 1) {
  3100. if (R300_MEM_USE_CD_CH_ONLY & temp) {
  3101. temp = RREG32(R300_MC_IND_INDEX);
  3102. temp &= ~R300_MC_IND_ADDR_MASK;
  3103. temp |= R300_MC_READ_CNTL_CD_mcind;
  3104. WREG32(R300_MC_IND_INDEX, temp);
  3105. temp = RREG32(R300_MC_IND_DATA);
  3106. data = (R300_MEM_RBS_POSITION_C_MASK & temp);
  3107. } else {
  3108. temp = RREG32(R300_MC_READ_CNTL_AB);
  3109. data = (R300_MEM_RBS_POSITION_A_MASK & temp);
  3110. }
  3111. } else {
  3112. temp = RREG32(R300_MC_READ_CNTL_AB);
  3113. data = (R300_MEM_RBS_POSITION_A_MASK & temp);
  3114. }
  3115. if (rdev->family == CHIP_RV410 ||
  3116. rdev->family == CHIP_R420 ||
  3117. rdev->family == CHIP_R423)
  3118. trbs_ff = memtrbs_r4xx[data];
  3119. else
  3120. trbs_ff = memtrbs[data];
  3121. tcas_ff.full += trbs_ff.full;
  3122. }
  3123. sclk_eff_ff.full = sclk_ff.full;
  3124. if (rdev->flags & RADEON_IS_AGP) {
  3125. fixed20_12 agpmode_ff;
  3126. agpmode_ff.full = dfixed_const(radeon_agpmode);
  3127. temp_ff.full = dfixed_const_666(16);
  3128. sclk_eff_ff.full -= dfixed_mul(agpmode_ff, temp_ff);
  3129. }
  3130. /* TODO PCIE lanes may affect this - agpmode == 16?? */
  3131. if (ASIC_IS_R300(rdev)) {
  3132. sclk_delay_ff.full = dfixed_const(250);
  3133. } else {
  3134. if ((rdev->family == CHIP_RV100) ||
  3135. rdev->flags & RADEON_IS_IGP) {
  3136. if (rdev->mc.vram_is_ddr)
  3137. sclk_delay_ff.full = dfixed_const(41);
  3138. else
  3139. sclk_delay_ff.full = dfixed_const(33);
  3140. } else {
  3141. if (rdev->mc.vram_width == 128)
  3142. sclk_delay_ff.full = dfixed_const(57);
  3143. else
  3144. sclk_delay_ff.full = dfixed_const(41);
  3145. }
  3146. }
  3147. mc_latency_sclk.full = dfixed_div(sclk_delay_ff, sclk_eff_ff);
  3148. if (rdev->mc.vram_is_ddr) {
  3149. if (rdev->mc.vram_width == 32) {
  3150. k1.full = dfixed_const(40);
  3151. c = 3;
  3152. } else {
  3153. k1.full = dfixed_const(20);
  3154. c = 1;
  3155. }
  3156. } else {
  3157. k1.full = dfixed_const(40);
  3158. c = 3;
  3159. }
  3160. temp_ff.full = dfixed_const(2);
  3161. mc_latency_mclk.full = dfixed_mul(trcd_ff, temp_ff);
  3162. temp_ff.full = dfixed_const(c);
  3163. mc_latency_mclk.full += dfixed_mul(tcas_ff, temp_ff);
  3164. temp_ff.full = dfixed_const(4);
  3165. mc_latency_mclk.full += dfixed_mul(tras_ff, temp_ff);
  3166. mc_latency_mclk.full += dfixed_mul(trp_ff, temp_ff);
  3167. mc_latency_mclk.full += k1.full;
  3168. mc_latency_mclk.full = dfixed_div(mc_latency_mclk, mclk_ff);
  3169. mc_latency_mclk.full += dfixed_div(temp_ff, sclk_eff_ff);
  3170. /*
  3171. HW cursor time assuming worst case of full size colour cursor.
  3172. */
  3173. temp_ff.full = dfixed_const((2 * (cur_size - (rdev->mc.vram_is_ddr + 1))));
  3174. temp_ff.full += trcd_ff.full;
  3175. if (temp_ff.full < tras_ff.full)
  3176. temp_ff.full = tras_ff.full;
  3177. cur_latency_mclk.full = dfixed_div(temp_ff, mclk_ff);
  3178. temp_ff.full = dfixed_const(cur_size);
  3179. cur_latency_sclk.full = dfixed_div(temp_ff, sclk_eff_ff);
  3180. /*
  3181. Find the total latency for the display data.
  3182. */
  3183. disp_latency_overhead.full = dfixed_const(8);
  3184. disp_latency_overhead.full = dfixed_div(disp_latency_overhead, sclk_ff);
  3185. mc_latency_mclk.full += disp_latency_overhead.full + cur_latency_mclk.full;
  3186. mc_latency_sclk.full += disp_latency_overhead.full + cur_latency_sclk.full;
  3187. if (mc_latency_mclk.full > mc_latency_sclk.full)
  3188. disp_latency.full = mc_latency_mclk.full;
  3189. else
  3190. disp_latency.full = mc_latency_sclk.full;
  3191. /* setup Max GRPH_STOP_REQ default value */
  3192. if (ASIC_IS_RV100(rdev))
  3193. max_stop_req = 0x5c;
  3194. else
  3195. max_stop_req = 0x7c;
  3196. if (mode1) {
  3197. /* CRTC1
  3198. Set GRPH_BUFFER_CNTL register using h/w defined optimal values.
  3199. GRPH_STOP_REQ <= MIN[ 0x7C, (CRTC_H_DISP + 1) * (bit depth) / 0x10 ]
  3200. */
  3201. stop_req = mode1->hdisplay * pixel_bytes1 / 16;
  3202. if (stop_req > max_stop_req)
  3203. stop_req = max_stop_req;
  3204. /*
  3205. Find the drain rate of the display buffer.
  3206. */
  3207. temp_ff.full = dfixed_const((16/pixel_bytes1));
  3208. disp_drain_rate.full = dfixed_div(pix_clk, temp_ff);
  3209. /*
  3210. Find the critical point of the display buffer.
  3211. */
  3212. crit_point_ff.full = dfixed_mul(disp_drain_rate, disp_latency);
  3213. crit_point_ff.full += dfixed_const_half(0);
  3214. critical_point = dfixed_trunc(crit_point_ff);
  3215. if (rdev->disp_priority == 2) {
  3216. critical_point = 0;
  3217. }
  3218. /*
  3219. The critical point should never be above max_stop_req-4. Setting
  3220. GRPH_CRITICAL_CNTL = 0 will thus force high priority all the time.
  3221. */
  3222. if (max_stop_req - critical_point < 4)
  3223. critical_point = 0;
  3224. if (critical_point == 0 && mode2 && rdev->family == CHIP_R300) {
  3225. /* some R300 cards have problem with this set to 0, when CRTC2 is enabled.*/
  3226. critical_point = 0x10;
  3227. }
  3228. temp = RREG32(RADEON_GRPH_BUFFER_CNTL);
  3229. temp &= ~(RADEON_GRPH_STOP_REQ_MASK);
  3230. temp |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
  3231. temp &= ~(RADEON_GRPH_START_REQ_MASK);
  3232. if ((rdev->family == CHIP_R350) &&
  3233. (stop_req > 0x15)) {
  3234. stop_req -= 0x10;
  3235. }
  3236. temp |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
  3237. temp |= RADEON_GRPH_BUFFER_SIZE;
  3238. temp &= ~(RADEON_GRPH_CRITICAL_CNTL |
  3239. RADEON_GRPH_CRITICAL_AT_SOF |
  3240. RADEON_GRPH_STOP_CNTL);
  3241. /*
  3242. Write the result into the register.
  3243. */
  3244. WREG32(RADEON_GRPH_BUFFER_CNTL, ((temp & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
  3245. (critical_point << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
  3246. #if 0
  3247. if ((rdev->family == CHIP_RS400) ||
  3248. (rdev->family == CHIP_RS480)) {
  3249. /* attempt to program RS400 disp regs correctly ??? */
  3250. temp = RREG32(RS400_DISP1_REG_CNTL);
  3251. temp &= ~(RS400_DISP1_START_REQ_LEVEL_MASK |
  3252. RS400_DISP1_STOP_REQ_LEVEL_MASK);
  3253. WREG32(RS400_DISP1_REQ_CNTL1, (temp |
  3254. (critical_point << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
  3255. (critical_point << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
  3256. temp = RREG32(RS400_DMIF_MEM_CNTL1);
  3257. temp &= ~(RS400_DISP1_CRITICAL_POINT_START_MASK |
  3258. RS400_DISP1_CRITICAL_POINT_STOP_MASK);
  3259. WREG32(RS400_DMIF_MEM_CNTL1, (temp |
  3260. (critical_point << RS400_DISP1_CRITICAL_POINT_START_SHIFT) |
  3261. (critical_point << RS400_DISP1_CRITICAL_POINT_STOP_SHIFT)));
  3262. }
  3263. #endif
  3264. DRM_DEBUG_KMS("GRPH_BUFFER_CNTL from to %x\n",
  3265. /* (unsigned int)info->SavedReg->grph_buffer_cntl, */
  3266. (unsigned int)RREG32(RADEON_GRPH_BUFFER_CNTL));
  3267. }
  3268. if (mode2) {
  3269. u32 grph2_cntl;
  3270. stop_req = mode2->hdisplay * pixel_bytes2 / 16;
  3271. if (stop_req > max_stop_req)
  3272. stop_req = max_stop_req;
  3273. /*
  3274. Find the drain rate of the display buffer.
  3275. */
  3276. temp_ff.full = dfixed_const((16/pixel_bytes2));
  3277. disp_drain_rate2.full = dfixed_div(pix_clk2, temp_ff);
  3278. grph2_cntl = RREG32(RADEON_GRPH2_BUFFER_CNTL);
  3279. grph2_cntl &= ~(RADEON_GRPH_STOP_REQ_MASK);
  3280. grph2_cntl |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
  3281. grph2_cntl &= ~(RADEON_GRPH_START_REQ_MASK);
  3282. if ((rdev->family == CHIP_R350) &&
  3283. (stop_req > 0x15)) {
  3284. stop_req -= 0x10;
  3285. }
  3286. grph2_cntl |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
  3287. grph2_cntl |= RADEON_GRPH_BUFFER_SIZE;
  3288. grph2_cntl &= ~(RADEON_GRPH_CRITICAL_CNTL |
  3289. RADEON_GRPH_CRITICAL_AT_SOF |
  3290. RADEON_GRPH_STOP_CNTL);
  3291. if ((rdev->family == CHIP_RS100) ||
  3292. (rdev->family == CHIP_RS200))
  3293. critical_point2 = 0;
  3294. else {
  3295. temp = (rdev->mc.vram_width * rdev->mc.vram_is_ddr + 1)/128;
  3296. temp_ff.full = dfixed_const(temp);
  3297. temp_ff.full = dfixed_mul(mclk_ff, temp_ff);
  3298. if (sclk_ff.full < temp_ff.full)
  3299. temp_ff.full = sclk_ff.full;
  3300. read_return_rate.full = temp_ff.full;
  3301. if (mode1) {
  3302. temp_ff.full = read_return_rate.full - disp_drain_rate.full;
  3303. time_disp1_drop_priority.full = dfixed_div(crit_point_ff, temp_ff);
  3304. } else {
  3305. time_disp1_drop_priority.full = 0;
  3306. }
  3307. crit_point_ff.full = disp_latency.full + time_disp1_drop_priority.full + disp_latency.full;
  3308. crit_point_ff.full = dfixed_mul(crit_point_ff, disp_drain_rate2);
  3309. crit_point_ff.full += dfixed_const_half(0);
  3310. critical_point2 = dfixed_trunc(crit_point_ff);
  3311. if (rdev->disp_priority == 2) {
  3312. critical_point2 = 0;
  3313. }
  3314. if (max_stop_req - critical_point2 < 4)
  3315. critical_point2 = 0;
  3316. }
  3317. if (critical_point2 == 0 && rdev->family == CHIP_R300) {
  3318. /* some R300 cards have problem with this set to 0 */
  3319. critical_point2 = 0x10;
  3320. }
  3321. WREG32(RADEON_GRPH2_BUFFER_CNTL, ((grph2_cntl & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
  3322. (critical_point2 << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
  3323. if ((rdev->family == CHIP_RS400) ||
  3324. (rdev->family == CHIP_RS480)) {
  3325. #if 0
  3326. /* attempt to program RS400 disp2 regs correctly ??? */
  3327. temp = RREG32(RS400_DISP2_REQ_CNTL1);
  3328. temp &= ~(RS400_DISP2_START_REQ_LEVEL_MASK |
  3329. RS400_DISP2_STOP_REQ_LEVEL_MASK);
  3330. WREG32(RS400_DISP2_REQ_CNTL1, (temp |
  3331. (critical_point2 << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
  3332. (critical_point2 << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
  3333. temp = RREG32(RS400_DISP2_REQ_CNTL2);
  3334. temp &= ~(RS400_DISP2_CRITICAL_POINT_START_MASK |
  3335. RS400_DISP2_CRITICAL_POINT_STOP_MASK);
  3336. WREG32(RS400_DISP2_REQ_CNTL2, (temp |
  3337. (critical_point2 << RS400_DISP2_CRITICAL_POINT_START_SHIFT) |
  3338. (critical_point2 << RS400_DISP2_CRITICAL_POINT_STOP_SHIFT)));
  3339. #endif
  3340. WREG32(RS400_DISP2_REQ_CNTL1, 0x105DC1CC);
  3341. WREG32(RS400_DISP2_REQ_CNTL2, 0x2749D000);
  3342. WREG32(RS400_DMIF_MEM_CNTL1, 0x29CA71DC);
  3343. WREG32(RS400_DISP1_REQ_CNTL1, 0x28FBC3AC);
  3344. }
  3345. DRM_DEBUG_KMS("GRPH2_BUFFER_CNTL from to %x\n",
  3346. (unsigned int)RREG32(RADEON_GRPH2_BUFFER_CNTL));
  3347. }
  3348. /* Save number of lines the linebuffer leads before the scanout */
  3349. if (mode1)
  3350. rdev->mode_info.crtcs[0]->lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode1->crtc_hdisplay);
  3351. if (mode2)
  3352. rdev->mode_info.crtcs[1]->lb_vblank_lead_lines = DIV_ROUND_UP(lb_size, mode2->crtc_hdisplay);
  3353. }
  3354. int r100_ring_test(struct radeon_device *rdev, struct radeon_ring *ring)
  3355. {
  3356. uint32_t scratch;
  3357. uint32_t tmp = 0;
  3358. unsigned i;
  3359. int r;
  3360. r = radeon_scratch_get(rdev, &scratch);
  3361. if (r) {
  3362. DRM_ERROR("radeon: cp failed to get scratch reg (%d).\n", r);
  3363. return r;
  3364. }
  3365. WREG32(scratch, 0xCAFEDEAD);
  3366. r = radeon_ring_lock(rdev, ring, 2);
  3367. if (r) {
  3368. DRM_ERROR("radeon: cp failed to lock ring (%d).\n", r);
  3369. radeon_scratch_free(rdev, scratch);
  3370. return r;
  3371. }
  3372. radeon_ring_write(ring, PACKET0(scratch, 0));
  3373. radeon_ring_write(ring, 0xDEADBEEF);
  3374. radeon_ring_unlock_commit(rdev, ring, false);
  3375. for (i = 0; i < rdev->usec_timeout; i++) {
  3376. tmp = RREG32(scratch);
  3377. if (tmp == 0xDEADBEEF) {
  3378. break;
  3379. }
  3380. DRM_UDELAY(1);
  3381. }
  3382. if (i < rdev->usec_timeout) {
  3383. DRM_INFO("ring test succeeded in %d usecs\n", i);
  3384. } else {
  3385. DRM_ERROR("radeon: ring test failed (scratch(0x%04X)=0x%08X)\n",
  3386. scratch, tmp);
  3387. r = -EINVAL;
  3388. }
  3389. radeon_scratch_free(rdev, scratch);
  3390. return r;
  3391. }
  3392. void r100_ring_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
  3393. {
  3394. struct radeon_ring *ring = &rdev->ring[RADEON_RING_TYPE_GFX_INDEX];
  3395. if (ring->rptr_save_reg) {
  3396. u32 next_rptr = ring->wptr + 2 + 3;
  3397. radeon_ring_write(ring, PACKET0(ring->rptr_save_reg, 0));
  3398. radeon_ring_write(ring, next_rptr);
  3399. }
  3400. radeon_ring_write(ring, PACKET0(RADEON_CP_IB_BASE, 1));
  3401. radeon_ring_write(ring, ib->gpu_addr);
  3402. radeon_ring_write(ring, ib->length_dw);
  3403. }
  3404. int r100_ib_test(struct radeon_device *rdev, struct radeon_ring *ring)
  3405. {
  3406. struct radeon_ib ib;
  3407. uint32_t scratch;
  3408. uint32_t tmp = 0;
  3409. unsigned i;
  3410. int r;
  3411. r = radeon_scratch_get(rdev, &scratch);
  3412. if (r) {
  3413. DRM_ERROR("radeon: failed to get scratch reg (%d).\n", r);
  3414. return r;
  3415. }
  3416. WREG32(scratch, 0xCAFEDEAD);
  3417. r = radeon_ib_get(rdev, RADEON_RING_TYPE_GFX_INDEX, &ib, NULL, 256);
  3418. if (r) {
  3419. DRM_ERROR("radeon: failed to get ib (%d).\n", r);
  3420. goto free_scratch;
  3421. }
  3422. ib.ptr[0] = PACKET0(scratch, 0);
  3423. ib.ptr[1] = 0xDEADBEEF;
  3424. ib.ptr[2] = PACKET2(0);
  3425. ib.ptr[3] = PACKET2(0);
  3426. ib.ptr[4] = PACKET2(0);
  3427. ib.ptr[5] = PACKET2(0);
  3428. ib.ptr[6] = PACKET2(0);
  3429. ib.ptr[7] = PACKET2(0);
  3430. ib.length_dw = 8;
  3431. r = radeon_ib_schedule(rdev, &ib, NULL, false);
  3432. if (r) {
  3433. DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
  3434. goto free_ib;
  3435. }
  3436. r = radeon_fence_wait(ib.fence, false);
  3437. if (r) {
  3438. DRM_ERROR("radeon: fence wait failed (%d).\n", r);
  3439. goto free_ib;
  3440. }
  3441. for (i = 0; i < rdev->usec_timeout; i++) {
  3442. tmp = RREG32(scratch);
  3443. if (tmp == 0xDEADBEEF) {
  3444. break;
  3445. }
  3446. DRM_UDELAY(1);
  3447. }
  3448. if (i < rdev->usec_timeout) {
  3449. DRM_INFO("ib test succeeded in %u usecs\n", i);
  3450. } else {
  3451. DRM_ERROR("radeon: ib test failed (scratch(0x%04X)=0x%08X)\n",
  3452. scratch, tmp);
  3453. r = -EINVAL;
  3454. }
  3455. free_ib:
  3456. radeon_ib_free(rdev, &ib);
  3457. free_scratch:
  3458. radeon_scratch_free(rdev, scratch);
  3459. return r;
  3460. }
  3461. void r100_mc_stop(struct radeon_device *rdev, struct r100_mc_save *save)
  3462. {
  3463. /* Shutdown CP we shouldn't need to do that but better be safe than
  3464. * sorry
  3465. */
  3466. rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready = false;
  3467. WREG32(R_000740_CP_CSQ_CNTL, 0);
  3468. /* Save few CRTC registers */
  3469. save->GENMO_WT = RREG8(R_0003C2_GENMO_WT);
  3470. save->CRTC_EXT_CNTL = RREG32(R_000054_CRTC_EXT_CNTL);
  3471. save->CRTC_GEN_CNTL = RREG32(R_000050_CRTC_GEN_CNTL);
  3472. save->CUR_OFFSET = RREG32(R_000260_CUR_OFFSET);
  3473. if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
  3474. save->CRTC2_GEN_CNTL = RREG32(R_0003F8_CRTC2_GEN_CNTL);
  3475. save->CUR2_OFFSET = RREG32(R_000360_CUR2_OFFSET);
  3476. }
  3477. /* Disable VGA aperture access */
  3478. WREG8(R_0003C2_GENMO_WT, C_0003C2_VGA_RAM_EN & save->GENMO_WT);
  3479. /* Disable cursor, overlay, crtc */
  3480. WREG32(R_000260_CUR_OFFSET, save->CUR_OFFSET | S_000260_CUR_LOCK(1));
  3481. WREG32(R_000054_CRTC_EXT_CNTL, save->CRTC_EXT_CNTL |
  3482. S_000054_CRTC_DISPLAY_DIS(1));
  3483. WREG32(R_000050_CRTC_GEN_CNTL,
  3484. (C_000050_CRTC_CUR_EN & save->CRTC_GEN_CNTL) |
  3485. S_000050_CRTC_DISP_REQ_EN_B(1));
  3486. WREG32(R_000420_OV0_SCALE_CNTL,
  3487. C_000420_OV0_OVERLAY_EN & RREG32(R_000420_OV0_SCALE_CNTL));
  3488. WREG32(R_000260_CUR_OFFSET, C_000260_CUR_LOCK & save->CUR_OFFSET);
  3489. if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
  3490. WREG32(R_000360_CUR2_OFFSET, save->CUR2_OFFSET |
  3491. S_000360_CUR2_LOCK(1));
  3492. WREG32(R_0003F8_CRTC2_GEN_CNTL,
  3493. (C_0003F8_CRTC2_CUR_EN & save->CRTC2_GEN_CNTL) |
  3494. S_0003F8_CRTC2_DISPLAY_DIS(1) |
  3495. S_0003F8_CRTC2_DISP_REQ_EN_B(1));
  3496. WREG32(R_000360_CUR2_OFFSET,
  3497. C_000360_CUR2_LOCK & save->CUR2_OFFSET);
  3498. }
  3499. }
  3500. void r100_mc_resume(struct radeon_device *rdev, struct r100_mc_save *save)
  3501. {
  3502. /* Update base address for crtc */
  3503. WREG32(R_00023C_DISPLAY_BASE_ADDR, rdev->mc.vram_start);
  3504. if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
  3505. WREG32(R_00033C_CRTC2_DISPLAY_BASE_ADDR, rdev->mc.vram_start);
  3506. }
  3507. /* Restore CRTC registers */
  3508. WREG8(R_0003C2_GENMO_WT, save->GENMO_WT);
  3509. WREG32(R_000054_CRTC_EXT_CNTL, save->CRTC_EXT_CNTL);
  3510. WREG32(R_000050_CRTC_GEN_CNTL, save->CRTC_GEN_CNTL);
  3511. if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
  3512. WREG32(R_0003F8_CRTC2_GEN_CNTL, save->CRTC2_GEN_CNTL);
  3513. }
  3514. }
  3515. void r100_vga_render_disable(struct radeon_device *rdev)
  3516. {
  3517. u32 tmp;
  3518. tmp = RREG8(R_0003C2_GENMO_WT);
  3519. WREG8(R_0003C2_GENMO_WT, C_0003C2_VGA_RAM_EN & tmp);
  3520. }
  3521. static void r100_debugfs(struct radeon_device *rdev)
  3522. {
  3523. int r;
  3524. r = r100_debugfs_mc_info_init(rdev);
  3525. if (r)
  3526. dev_warn(rdev->dev, "Failed to create r100_mc debugfs file.\n");
  3527. }
  3528. static void r100_mc_program(struct radeon_device *rdev)
  3529. {
  3530. struct r100_mc_save save;
  3531. /* Stops all mc clients */
  3532. r100_mc_stop(rdev, &save);
  3533. if (rdev->flags & RADEON_IS_AGP) {
  3534. WREG32(R_00014C_MC_AGP_LOCATION,
  3535. S_00014C_MC_AGP_START(rdev->mc.gtt_start >> 16) |
  3536. S_00014C_MC_AGP_TOP(rdev->mc.gtt_end >> 16));
  3537. WREG32(R_000170_AGP_BASE, lower_32_bits(rdev->mc.agp_base));
  3538. if (rdev->family > CHIP_RV200)
  3539. WREG32(R_00015C_AGP_BASE_2,
  3540. upper_32_bits(rdev->mc.agp_base) & 0xff);
  3541. } else {
  3542. WREG32(R_00014C_MC_AGP_LOCATION, 0x0FFFFFFF);
  3543. WREG32(R_000170_AGP_BASE, 0);
  3544. if (rdev->family > CHIP_RV200)
  3545. WREG32(R_00015C_AGP_BASE_2, 0);
  3546. }
  3547. /* Wait for mc idle */
  3548. if (r100_mc_wait_for_idle(rdev))
  3549. dev_warn(rdev->dev, "Wait for MC idle timeout.\n");
  3550. /* Program MC, should be a 32bits limited address space */
  3551. WREG32(R_000148_MC_FB_LOCATION,
  3552. S_000148_MC_FB_START(rdev->mc.vram_start >> 16) |
  3553. S_000148_MC_FB_TOP(rdev->mc.vram_end >> 16));
  3554. r100_mc_resume(rdev, &save);
  3555. }
  3556. static void r100_clock_startup(struct radeon_device *rdev)
  3557. {
  3558. u32 tmp;
  3559. if (radeon_dynclks != -1 && radeon_dynclks)
  3560. radeon_legacy_set_clock_gating(rdev, 1);
  3561. /* We need to force on some of the block */
  3562. tmp = RREG32_PLL(R_00000D_SCLK_CNTL);
  3563. tmp |= S_00000D_FORCE_CP(1) | S_00000D_FORCE_VIP(1);
  3564. if ((rdev->family == CHIP_RV250) || (rdev->family == CHIP_RV280))
  3565. tmp |= S_00000D_FORCE_DISP1(1) | S_00000D_FORCE_DISP2(1);
  3566. WREG32_PLL(R_00000D_SCLK_CNTL, tmp);
  3567. }
  3568. static int r100_startup(struct radeon_device *rdev)
  3569. {
  3570. int r;
  3571. /* set common regs */
  3572. r100_set_common_regs(rdev);
  3573. /* program mc */
  3574. r100_mc_program(rdev);
  3575. /* Resume clock */
  3576. r100_clock_startup(rdev);
  3577. /* Initialize GART (initialize after TTM so we can allocate
  3578. * memory through TTM but finalize after TTM) */
  3579. r100_enable_bm(rdev);
  3580. if (rdev->flags & RADEON_IS_PCI) {
  3581. r = r100_pci_gart_enable(rdev);
  3582. if (r)
  3583. return r;
  3584. }
  3585. /* allocate wb buffer */
  3586. r = radeon_wb_init(rdev);
  3587. if (r)
  3588. return r;
  3589. r = radeon_fence_driver_start_ring(rdev, RADEON_RING_TYPE_GFX_INDEX);
  3590. if (r) {
  3591. dev_err(rdev->dev, "failed initializing CP fences (%d).\n", r);
  3592. return r;
  3593. }
  3594. /* Enable IRQ */
  3595. if (!rdev->irq.installed) {
  3596. r = radeon_irq_kms_init(rdev);
  3597. if (r)
  3598. return r;
  3599. }
  3600. r100_irq_set(rdev);
  3601. rdev->config.r100.hdp_cntl = RREG32(RADEON_HOST_PATH_CNTL);
  3602. /* 1M ring buffer */
  3603. r = r100_cp_init(rdev, 1024 * 1024);
  3604. if (r) {
  3605. dev_err(rdev->dev, "failed initializing CP (%d).\n", r);
  3606. return r;
  3607. }
  3608. r = radeon_ib_pool_init(rdev);
  3609. if (r) {
  3610. dev_err(rdev->dev, "IB initialization failed (%d).\n", r);
  3611. return r;
  3612. }
  3613. return 0;
  3614. }
  3615. int r100_resume(struct radeon_device *rdev)
  3616. {
  3617. int r;
  3618. /* Make sur GART are not working */
  3619. if (rdev->flags & RADEON_IS_PCI)
  3620. r100_pci_gart_disable(rdev);
  3621. /* Resume clock before doing reset */
  3622. r100_clock_startup(rdev);
  3623. /* Reset gpu before posting otherwise ATOM will enter infinite loop */
  3624. if (radeon_asic_reset(rdev)) {
  3625. dev_warn(rdev->dev, "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
  3626. RREG32(R_000E40_RBBM_STATUS),
  3627. RREG32(R_0007C0_CP_STAT));
  3628. }
  3629. /* post */
  3630. radeon_combios_asic_init(rdev->ddev);
  3631. /* Resume clock after posting */
  3632. r100_clock_startup(rdev);
  3633. /* Initialize surface registers */
  3634. radeon_surface_init(rdev);
  3635. rdev->accel_working = true;
  3636. r = r100_startup(rdev);
  3637. if (r) {
  3638. rdev->accel_working = false;
  3639. }
  3640. return r;
  3641. }
  3642. int r100_suspend(struct radeon_device *rdev)
  3643. {
  3644. radeon_pm_suspend(rdev);
  3645. r100_cp_disable(rdev);
  3646. radeon_wb_disable(rdev);
  3647. r100_irq_disable(rdev);
  3648. if (rdev->flags & RADEON_IS_PCI)
  3649. r100_pci_gart_disable(rdev);
  3650. return 0;
  3651. }
  3652. void r100_fini(struct radeon_device *rdev)
  3653. {
  3654. radeon_pm_fini(rdev);
  3655. r100_cp_fini(rdev);
  3656. radeon_wb_fini(rdev);
  3657. radeon_ib_pool_fini(rdev);
  3658. radeon_gem_fini(rdev);
  3659. if (rdev->flags & RADEON_IS_PCI)
  3660. r100_pci_gart_fini(rdev);
  3661. radeon_agp_fini(rdev);
  3662. radeon_irq_kms_fini(rdev);
  3663. radeon_fence_driver_fini(rdev);
  3664. radeon_bo_fini(rdev);
  3665. radeon_atombios_fini(rdev);
  3666. kfree(rdev->bios);
  3667. rdev->bios = NULL;
  3668. }
  3669. /*
  3670. * Due to how kexec works, it can leave the hw fully initialised when it
  3671. * boots the new kernel. However doing our init sequence with the CP and
  3672. * WB stuff setup causes GPU hangs on the RN50 at least. So at startup
  3673. * do some quick sanity checks and restore sane values to avoid this
  3674. * problem.
  3675. */
  3676. void r100_restore_sanity(struct radeon_device *rdev)
  3677. {
  3678. u32 tmp;
  3679. tmp = RREG32(RADEON_CP_CSQ_CNTL);
  3680. if (tmp) {
  3681. WREG32(RADEON_CP_CSQ_CNTL, 0);
  3682. }
  3683. tmp = RREG32(RADEON_CP_RB_CNTL);
  3684. if (tmp) {
  3685. WREG32(RADEON_CP_RB_CNTL, 0);
  3686. }
  3687. tmp = RREG32(RADEON_SCRATCH_UMSK);
  3688. if (tmp) {
  3689. WREG32(RADEON_SCRATCH_UMSK, 0);
  3690. }
  3691. }
  3692. int r100_init(struct radeon_device *rdev)
  3693. {
  3694. int r;
  3695. /* Register debugfs file specific to this group of asics */
  3696. r100_debugfs(rdev);
  3697. /* Disable VGA */
  3698. r100_vga_render_disable(rdev);
  3699. /* Initialize scratch registers */
  3700. radeon_scratch_init(rdev);
  3701. /* Initialize surface registers */
  3702. radeon_surface_init(rdev);
  3703. /* sanity check some register to avoid hangs like after kexec */
  3704. r100_restore_sanity(rdev);
  3705. /* TODO: disable VGA need to use VGA request */
  3706. /* BIOS*/
  3707. if (!radeon_get_bios(rdev)) {
  3708. if (ASIC_IS_AVIVO(rdev))
  3709. return -EINVAL;
  3710. }
  3711. if (rdev->is_atom_bios) {
  3712. dev_err(rdev->dev, "Expecting combios for RS400/RS480 GPU\n");
  3713. return -EINVAL;
  3714. } else {
  3715. r = radeon_combios_init(rdev);
  3716. if (r)
  3717. return r;
  3718. }
  3719. /* Reset gpu before posting otherwise ATOM will enter infinite loop */
  3720. if (radeon_asic_reset(rdev)) {
  3721. dev_warn(rdev->dev,
  3722. "GPU reset failed ! (0xE40=0x%08X, 0x7C0=0x%08X)\n",
  3723. RREG32(R_000E40_RBBM_STATUS),
  3724. RREG32(R_0007C0_CP_STAT));
  3725. }
  3726. /* check if cards are posted or not */
  3727. if (radeon_boot_test_post_card(rdev) == false)
  3728. return -EINVAL;
  3729. /* Set asic errata */
  3730. r100_errata(rdev);
  3731. /* Initialize clocks */
  3732. radeon_get_clock_info(rdev->ddev);
  3733. /* initialize AGP */
  3734. if (rdev->flags & RADEON_IS_AGP) {
  3735. r = radeon_agp_init(rdev);
  3736. if (r) {
  3737. radeon_agp_disable(rdev);
  3738. }
  3739. }
  3740. /* initialize VRAM */
  3741. r100_mc_init(rdev);
  3742. /* Fence driver */
  3743. r = radeon_fence_driver_init(rdev);
  3744. if (r)
  3745. return r;
  3746. /* Memory manager */
  3747. r = radeon_bo_init(rdev);
  3748. if (r)
  3749. return r;
  3750. if (rdev->flags & RADEON_IS_PCI) {
  3751. r = r100_pci_gart_init(rdev);
  3752. if (r)
  3753. return r;
  3754. }
  3755. r100_set_safe_registers(rdev);
  3756. /* Initialize power management */
  3757. radeon_pm_init(rdev);
  3758. rdev->accel_working = true;
  3759. r = r100_startup(rdev);
  3760. if (r) {
  3761. /* Somethings want wront with the accel init stop accel */
  3762. dev_err(rdev->dev, "Disabling GPU acceleration\n");
  3763. r100_cp_fini(rdev);
  3764. radeon_wb_fini(rdev);
  3765. radeon_ib_pool_fini(rdev);
  3766. radeon_irq_kms_fini(rdev);
  3767. if (rdev->flags & RADEON_IS_PCI)
  3768. r100_pci_gart_fini(rdev);
  3769. rdev->accel_working = false;
  3770. }
  3771. return 0;
  3772. }
  3773. uint32_t r100_mm_rreg_slow(struct radeon_device *rdev, uint32_t reg)
  3774. {
  3775. unsigned long flags;
  3776. uint32_t ret;
  3777. spin_lock_irqsave(&rdev->mmio_idx_lock, flags);
  3778. writel(reg, ((void __iomem *)rdev->rmmio) + RADEON_MM_INDEX);
  3779. ret = readl(((void __iomem *)rdev->rmmio) + RADEON_MM_DATA);
  3780. spin_unlock_irqrestore(&rdev->mmio_idx_lock, flags);
  3781. return ret;
  3782. }
  3783. void r100_mm_wreg_slow(struct radeon_device *rdev, uint32_t reg, uint32_t v)
  3784. {
  3785. unsigned long flags;
  3786. spin_lock_irqsave(&rdev->mmio_idx_lock, flags);
  3787. writel(reg, ((void __iomem *)rdev->rmmio) + RADEON_MM_INDEX);
  3788. writel(v, ((void __iomem *)rdev->rmmio) + RADEON_MM_DATA);
  3789. spin_unlock_irqrestore(&rdev->mmio_idx_lock, flags);
  3790. }
  3791. u32 r100_io_rreg(struct radeon_device *rdev, u32 reg)
  3792. {
  3793. if (reg < rdev->rio_mem_size)
  3794. return ioread32(rdev->rio_mem + reg);
  3795. else {
  3796. iowrite32(reg, rdev->rio_mem + RADEON_MM_INDEX);
  3797. return ioread32(rdev->rio_mem + RADEON_MM_DATA);
  3798. }
  3799. }
  3800. void r100_io_wreg(struct radeon_device *rdev, u32 reg, u32 v)
  3801. {
  3802. if (reg < rdev->rio_mem_size)
  3803. iowrite32(v, rdev->rio_mem + reg);
  3804. else {
  3805. iowrite32(reg, rdev->rio_mem + RADEON_MM_INDEX);
  3806. iowrite32(v, rdev->rio_mem + RADEON_MM_DATA);
  3807. }
  3808. }