r100.c 119 KB

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