intel_display.c 231 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/cpufreq.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include "drmP.h"
  34. #include "intel_drv.h"
  35. #include "i915_drm.h"
  36. #include "i915_drv.h"
  37. #include "i915_trace.h"
  38. #include "drm_dp_helper.h"
  39. #include "drm_crtc_helper.h"
  40. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  41. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  42. static void intel_update_watermarks(struct drm_device *dev);
  43. static void intel_increase_pllclock(struct drm_crtc *crtc);
  44. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  45. typedef struct {
  46. /* given values */
  47. int n;
  48. int m1, m2;
  49. int p1, p2;
  50. /* derived values */
  51. int dot;
  52. int vco;
  53. int m;
  54. int p;
  55. } intel_clock_t;
  56. typedef struct {
  57. int min, max;
  58. } intel_range_t;
  59. typedef struct {
  60. int dot_limit;
  61. int p2_slow, p2_fast;
  62. } intel_p2_t;
  63. #define INTEL_P2_NUM 2
  64. typedef struct intel_limit intel_limit_t;
  65. struct intel_limit {
  66. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  67. intel_p2_t p2;
  68. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  69. int, int, intel_clock_t *);
  70. };
  71. /* FDI */
  72. #define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
  73. static bool
  74. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  75. int target, int refclk, intel_clock_t *best_clock);
  76. static bool
  77. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  78. int target, int refclk, intel_clock_t *best_clock);
  79. static bool
  80. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  81. int target, int refclk, intel_clock_t *best_clock);
  82. static bool
  83. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  84. int target, int refclk, intel_clock_t *best_clock);
  85. static inline u32 /* units of 100MHz */
  86. intel_fdi_link_freq(struct drm_device *dev)
  87. {
  88. if (IS_GEN5(dev)) {
  89. struct drm_i915_private *dev_priv = dev->dev_private;
  90. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  91. } else
  92. return 27;
  93. }
  94. static const intel_limit_t intel_limits_i8xx_dvo = {
  95. .dot = { .min = 25000, .max = 350000 },
  96. .vco = { .min = 930000, .max = 1400000 },
  97. .n = { .min = 3, .max = 16 },
  98. .m = { .min = 96, .max = 140 },
  99. .m1 = { .min = 18, .max = 26 },
  100. .m2 = { .min = 6, .max = 16 },
  101. .p = { .min = 4, .max = 128 },
  102. .p1 = { .min = 2, .max = 33 },
  103. .p2 = { .dot_limit = 165000,
  104. .p2_slow = 4, .p2_fast = 2 },
  105. .find_pll = intel_find_best_PLL,
  106. };
  107. static const intel_limit_t intel_limits_i8xx_lvds = {
  108. .dot = { .min = 25000, .max = 350000 },
  109. .vco = { .min = 930000, .max = 1400000 },
  110. .n = { .min = 3, .max = 16 },
  111. .m = { .min = 96, .max = 140 },
  112. .m1 = { .min = 18, .max = 26 },
  113. .m2 = { .min = 6, .max = 16 },
  114. .p = { .min = 4, .max = 128 },
  115. .p1 = { .min = 1, .max = 6 },
  116. .p2 = { .dot_limit = 165000,
  117. .p2_slow = 14, .p2_fast = 7 },
  118. .find_pll = intel_find_best_PLL,
  119. };
  120. static const intel_limit_t intel_limits_i9xx_sdvo = {
  121. .dot = { .min = 20000, .max = 400000 },
  122. .vco = { .min = 1400000, .max = 2800000 },
  123. .n = { .min = 1, .max = 6 },
  124. .m = { .min = 70, .max = 120 },
  125. .m1 = { .min = 10, .max = 22 },
  126. .m2 = { .min = 5, .max = 9 },
  127. .p = { .min = 5, .max = 80 },
  128. .p1 = { .min = 1, .max = 8 },
  129. .p2 = { .dot_limit = 200000,
  130. .p2_slow = 10, .p2_fast = 5 },
  131. .find_pll = intel_find_best_PLL,
  132. };
  133. static const intel_limit_t intel_limits_i9xx_lvds = {
  134. .dot = { .min = 20000, .max = 400000 },
  135. .vco = { .min = 1400000, .max = 2800000 },
  136. .n = { .min = 1, .max = 6 },
  137. .m = { .min = 70, .max = 120 },
  138. .m1 = { .min = 10, .max = 22 },
  139. .m2 = { .min = 5, .max = 9 },
  140. .p = { .min = 7, .max = 98 },
  141. .p1 = { .min = 1, .max = 8 },
  142. .p2 = { .dot_limit = 112000,
  143. .p2_slow = 14, .p2_fast = 7 },
  144. .find_pll = intel_find_best_PLL,
  145. };
  146. static const intel_limit_t intel_limits_g4x_sdvo = {
  147. .dot = { .min = 25000, .max = 270000 },
  148. .vco = { .min = 1750000, .max = 3500000},
  149. .n = { .min = 1, .max = 4 },
  150. .m = { .min = 104, .max = 138 },
  151. .m1 = { .min = 17, .max = 23 },
  152. .m2 = { .min = 5, .max = 11 },
  153. .p = { .min = 10, .max = 30 },
  154. .p1 = { .min = 1, .max = 3},
  155. .p2 = { .dot_limit = 270000,
  156. .p2_slow = 10,
  157. .p2_fast = 10
  158. },
  159. .find_pll = intel_g4x_find_best_PLL,
  160. };
  161. static const intel_limit_t intel_limits_g4x_hdmi = {
  162. .dot = { .min = 22000, .max = 400000 },
  163. .vco = { .min = 1750000, .max = 3500000},
  164. .n = { .min = 1, .max = 4 },
  165. .m = { .min = 104, .max = 138 },
  166. .m1 = { .min = 16, .max = 23 },
  167. .m2 = { .min = 5, .max = 11 },
  168. .p = { .min = 5, .max = 80 },
  169. .p1 = { .min = 1, .max = 8},
  170. .p2 = { .dot_limit = 165000,
  171. .p2_slow = 10, .p2_fast = 5 },
  172. .find_pll = intel_g4x_find_best_PLL,
  173. };
  174. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  175. .dot = { .min = 20000, .max = 115000 },
  176. .vco = { .min = 1750000, .max = 3500000 },
  177. .n = { .min = 1, .max = 3 },
  178. .m = { .min = 104, .max = 138 },
  179. .m1 = { .min = 17, .max = 23 },
  180. .m2 = { .min = 5, .max = 11 },
  181. .p = { .min = 28, .max = 112 },
  182. .p1 = { .min = 2, .max = 8 },
  183. .p2 = { .dot_limit = 0,
  184. .p2_slow = 14, .p2_fast = 14
  185. },
  186. .find_pll = intel_g4x_find_best_PLL,
  187. };
  188. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  189. .dot = { .min = 80000, .max = 224000 },
  190. .vco = { .min = 1750000, .max = 3500000 },
  191. .n = { .min = 1, .max = 3 },
  192. .m = { .min = 104, .max = 138 },
  193. .m1 = { .min = 17, .max = 23 },
  194. .m2 = { .min = 5, .max = 11 },
  195. .p = { .min = 14, .max = 42 },
  196. .p1 = { .min = 2, .max = 6 },
  197. .p2 = { .dot_limit = 0,
  198. .p2_slow = 7, .p2_fast = 7
  199. },
  200. .find_pll = intel_g4x_find_best_PLL,
  201. };
  202. static const intel_limit_t intel_limits_g4x_display_port = {
  203. .dot = { .min = 161670, .max = 227000 },
  204. .vco = { .min = 1750000, .max = 3500000},
  205. .n = { .min = 1, .max = 2 },
  206. .m = { .min = 97, .max = 108 },
  207. .m1 = { .min = 0x10, .max = 0x12 },
  208. .m2 = { .min = 0x05, .max = 0x06 },
  209. .p = { .min = 10, .max = 20 },
  210. .p1 = { .min = 1, .max = 2},
  211. .p2 = { .dot_limit = 0,
  212. .p2_slow = 10, .p2_fast = 10 },
  213. .find_pll = intel_find_pll_g4x_dp,
  214. };
  215. static const intel_limit_t intel_limits_pineview_sdvo = {
  216. .dot = { .min = 20000, .max = 400000},
  217. .vco = { .min = 1700000, .max = 3500000 },
  218. /* Pineview's Ncounter is a ring counter */
  219. .n = { .min = 3, .max = 6 },
  220. .m = { .min = 2, .max = 256 },
  221. /* Pineview only has one combined m divider, which we treat as m2. */
  222. .m1 = { .min = 0, .max = 0 },
  223. .m2 = { .min = 0, .max = 254 },
  224. .p = { .min = 5, .max = 80 },
  225. .p1 = { .min = 1, .max = 8 },
  226. .p2 = { .dot_limit = 200000,
  227. .p2_slow = 10, .p2_fast = 5 },
  228. .find_pll = intel_find_best_PLL,
  229. };
  230. static const intel_limit_t intel_limits_pineview_lvds = {
  231. .dot = { .min = 20000, .max = 400000 },
  232. .vco = { .min = 1700000, .max = 3500000 },
  233. .n = { .min = 3, .max = 6 },
  234. .m = { .min = 2, .max = 256 },
  235. .m1 = { .min = 0, .max = 0 },
  236. .m2 = { .min = 0, .max = 254 },
  237. .p = { .min = 7, .max = 112 },
  238. .p1 = { .min = 1, .max = 8 },
  239. .p2 = { .dot_limit = 112000,
  240. .p2_slow = 14, .p2_fast = 14 },
  241. .find_pll = intel_find_best_PLL,
  242. };
  243. /* Ironlake / Sandybridge
  244. *
  245. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  246. * the range value for them is (actual_value - 2).
  247. */
  248. static const intel_limit_t intel_limits_ironlake_dac = {
  249. .dot = { .min = 25000, .max = 350000 },
  250. .vco = { .min = 1760000, .max = 3510000 },
  251. .n = { .min = 1, .max = 5 },
  252. .m = { .min = 79, .max = 127 },
  253. .m1 = { .min = 12, .max = 22 },
  254. .m2 = { .min = 5, .max = 9 },
  255. .p = { .min = 5, .max = 80 },
  256. .p1 = { .min = 1, .max = 8 },
  257. .p2 = { .dot_limit = 225000,
  258. .p2_slow = 10, .p2_fast = 5 },
  259. .find_pll = intel_g4x_find_best_PLL,
  260. };
  261. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  262. .dot = { .min = 25000, .max = 350000 },
  263. .vco = { .min = 1760000, .max = 3510000 },
  264. .n = { .min = 1, .max = 3 },
  265. .m = { .min = 79, .max = 118 },
  266. .m1 = { .min = 12, .max = 22 },
  267. .m2 = { .min = 5, .max = 9 },
  268. .p = { .min = 28, .max = 112 },
  269. .p1 = { .min = 2, .max = 8 },
  270. .p2 = { .dot_limit = 225000,
  271. .p2_slow = 14, .p2_fast = 14 },
  272. .find_pll = intel_g4x_find_best_PLL,
  273. };
  274. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  275. .dot = { .min = 25000, .max = 350000 },
  276. .vco = { .min = 1760000, .max = 3510000 },
  277. .n = { .min = 1, .max = 3 },
  278. .m = { .min = 79, .max = 127 },
  279. .m1 = { .min = 12, .max = 22 },
  280. .m2 = { .min = 5, .max = 9 },
  281. .p = { .min = 14, .max = 56 },
  282. .p1 = { .min = 2, .max = 8 },
  283. .p2 = { .dot_limit = 225000,
  284. .p2_slow = 7, .p2_fast = 7 },
  285. .find_pll = intel_g4x_find_best_PLL,
  286. };
  287. /* LVDS 100mhz refclk limits. */
  288. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  289. .dot = { .min = 25000, .max = 350000 },
  290. .vco = { .min = 1760000, .max = 3510000 },
  291. .n = { .min = 1, .max = 2 },
  292. .m = { .min = 79, .max = 126 },
  293. .m1 = { .min = 12, .max = 22 },
  294. .m2 = { .min = 5, .max = 9 },
  295. .p = { .min = 28, .max = 112 },
  296. .p1 = { .min = 2,.max = 8 },
  297. .p2 = { .dot_limit = 225000,
  298. .p2_slow = 14, .p2_fast = 14 },
  299. .find_pll = intel_g4x_find_best_PLL,
  300. };
  301. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  302. .dot = { .min = 25000, .max = 350000 },
  303. .vco = { .min = 1760000, .max = 3510000 },
  304. .n = { .min = 1, .max = 3 },
  305. .m = { .min = 79, .max = 126 },
  306. .m1 = { .min = 12, .max = 22 },
  307. .m2 = { .min = 5, .max = 9 },
  308. .p = { .min = 14, .max = 42 },
  309. .p1 = { .min = 2,.max = 6 },
  310. .p2 = { .dot_limit = 225000,
  311. .p2_slow = 7, .p2_fast = 7 },
  312. .find_pll = intel_g4x_find_best_PLL,
  313. };
  314. static const intel_limit_t intel_limits_ironlake_display_port = {
  315. .dot = { .min = 25000, .max = 350000 },
  316. .vco = { .min = 1760000, .max = 3510000},
  317. .n = { .min = 1, .max = 2 },
  318. .m = { .min = 81, .max = 90 },
  319. .m1 = { .min = 12, .max = 22 },
  320. .m2 = { .min = 5, .max = 9 },
  321. .p = { .min = 10, .max = 20 },
  322. .p1 = { .min = 1, .max = 2},
  323. .p2 = { .dot_limit = 0,
  324. .p2_slow = 10, .p2_fast = 10 },
  325. .find_pll = intel_find_pll_ironlake_dp,
  326. };
  327. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  328. int refclk)
  329. {
  330. struct drm_device *dev = crtc->dev;
  331. struct drm_i915_private *dev_priv = dev->dev_private;
  332. const intel_limit_t *limit;
  333. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  334. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  335. LVDS_CLKB_POWER_UP) {
  336. /* LVDS dual channel */
  337. if (refclk == 100000)
  338. limit = &intel_limits_ironlake_dual_lvds_100m;
  339. else
  340. limit = &intel_limits_ironlake_dual_lvds;
  341. } else {
  342. if (refclk == 100000)
  343. limit = &intel_limits_ironlake_single_lvds_100m;
  344. else
  345. limit = &intel_limits_ironlake_single_lvds;
  346. }
  347. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  348. HAS_eDP)
  349. limit = &intel_limits_ironlake_display_port;
  350. else
  351. limit = &intel_limits_ironlake_dac;
  352. return limit;
  353. }
  354. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  355. {
  356. struct drm_device *dev = crtc->dev;
  357. struct drm_i915_private *dev_priv = dev->dev_private;
  358. const intel_limit_t *limit;
  359. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  360. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  361. LVDS_CLKB_POWER_UP)
  362. /* LVDS with dual channel */
  363. limit = &intel_limits_g4x_dual_channel_lvds;
  364. else
  365. /* LVDS with dual channel */
  366. limit = &intel_limits_g4x_single_channel_lvds;
  367. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  368. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  369. limit = &intel_limits_g4x_hdmi;
  370. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  371. limit = &intel_limits_g4x_sdvo;
  372. } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  373. limit = &intel_limits_g4x_display_port;
  374. } else /* The option is for other outputs */
  375. limit = &intel_limits_i9xx_sdvo;
  376. return limit;
  377. }
  378. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  379. {
  380. struct drm_device *dev = crtc->dev;
  381. const intel_limit_t *limit;
  382. if (HAS_PCH_SPLIT(dev))
  383. limit = intel_ironlake_limit(crtc, refclk);
  384. else if (IS_G4X(dev)) {
  385. limit = intel_g4x_limit(crtc);
  386. } else if (IS_PINEVIEW(dev)) {
  387. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  388. limit = &intel_limits_pineview_lvds;
  389. else
  390. limit = &intel_limits_pineview_sdvo;
  391. } else if (!IS_GEN2(dev)) {
  392. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  393. limit = &intel_limits_i9xx_lvds;
  394. else
  395. limit = &intel_limits_i9xx_sdvo;
  396. } else {
  397. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  398. limit = &intel_limits_i8xx_lvds;
  399. else
  400. limit = &intel_limits_i8xx_dvo;
  401. }
  402. return limit;
  403. }
  404. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  405. static void pineview_clock(int refclk, intel_clock_t *clock)
  406. {
  407. clock->m = clock->m2 + 2;
  408. clock->p = clock->p1 * clock->p2;
  409. clock->vco = refclk * clock->m / clock->n;
  410. clock->dot = clock->vco / clock->p;
  411. }
  412. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  413. {
  414. if (IS_PINEVIEW(dev)) {
  415. pineview_clock(refclk, clock);
  416. return;
  417. }
  418. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  419. clock->p = clock->p1 * clock->p2;
  420. clock->vco = refclk * clock->m / (clock->n + 2);
  421. clock->dot = clock->vco / clock->p;
  422. }
  423. /**
  424. * Returns whether any output on the specified pipe is of the specified type
  425. */
  426. bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  427. {
  428. struct drm_device *dev = crtc->dev;
  429. struct drm_mode_config *mode_config = &dev->mode_config;
  430. struct intel_encoder *encoder;
  431. list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
  432. if (encoder->base.crtc == crtc && encoder->type == type)
  433. return true;
  434. return false;
  435. }
  436. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  437. /**
  438. * Returns whether the given set of divisors are valid for a given refclk with
  439. * the given connectors.
  440. */
  441. static bool intel_PLL_is_valid(struct drm_device *dev,
  442. const intel_limit_t *limit,
  443. const intel_clock_t *clock)
  444. {
  445. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  446. INTELPllInvalid ("p1 out of range\n");
  447. if (clock->p < limit->p.min || limit->p.max < clock->p)
  448. INTELPllInvalid ("p out of range\n");
  449. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  450. INTELPllInvalid ("m2 out of range\n");
  451. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  452. INTELPllInvalid ("m1 out of range\n");
  453. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  454. INTELPllInvalid ("m1 <= m2\n");
  455. if (clock->m < limit->m.min || limit->m.max < clock->m)
  456. INTELPllInvalid ("m out of range\n");
  457. if (clock->n < limit->n.min || limit->n.max < clock->n)
  458. INTELPllInvalid ("n out of range\n");
  459. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  460. INTELPllInvalid ("vco out of range\n");
  461. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  462. * connector, etc., rather than just a single range.
  463. */
  464. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  465. INTELPllInvalid ("dot out of range\n");
  466. return true;
  467. }
  468. static bool
  469. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  470. int target, int refclk, intel_clock_t *best_clock)
  471. {
  472. struct drm_device *dev = crtc->dev;
  473. struct drm_i915_private *dev_priv = dev->dev_private;
  474. intel_clock_t clock;
  475. int err = target;
  476. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  477. (I915_READ(LVDS)) != 0) {
  478. /*
  479. * For LVDS, if the panel is on, just rely on its current
  480. * settings for dual-channel. We haven't figured out how to
  481. * reliably set up different single/dual channel state, if we
  482. * even can.
  483. */
  484. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  485. LVDS_CLKB_POWER_UP)
  486. clock.p2 = limit->p2.p2_fast;
  487. else
  488. clock.p2 = limit->p2.p2_slow;
  489. } else {
  490. if (target < limit->p2.dot_limit)
  491. clock.p2 = limit->p2.p2_slow;
  492. else
  493. clock.p2 = limit->p2.p2_fast;
  494. }
  495. memset (best_clock, 0, sizeof (*best_clock));
  496. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  497. clock.m1++) {
  498. for (clock.m2 = limit->m2.min;
  499. clock.m2 <= limit->m2.max; clock.m2++) {
  500. /* m1 is always 0 in Pineview */
  501. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  502. break;
  503. for (clock.n = limit->n.min;
  504. clock.n <= limit->n.max; clock.n++) {
  505. for (clock.p1 = limit->p1.min;
  506. clock.p1 <= limit->p1.max; clock.p1++) {
  507. int this_err;
  508. intel_clock(dev, refclk, &clock);
  509. if (!intel_PLL_is_valid(dev, limit,
  510. &clock))
  511. continue;
  512. this_err = abs(clock.dot - target);
  513. if (this_err < err) {
  514. *best_clock = clock;
  515. err = this_err;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. return (err != target);
  522. }
  523. static bool
  524. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  525. int target, int refclk, intel_clock_t *best_clock)
  526. {
  527. struct drm_device *dev = crtc->dev;
  528. struct drm_i915_private *dev_priv = dev->dev_private;
  529. intel_clock_t clock;
  530. int max_n;
  531. bool found;
  532. /* approximately equals target * 0.00585 */
  533. int err_most = (target >> 8) + (target >> 9);
  534. found = false;
  535. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  536. int lvds_reg;
  537. if (HAS_PCH_SPLIT(dev))
  538. lvds_reg = PCH_LVDS;
  539. else
  540. lvds_reg = LVDS;
  541. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  542. LVDS_CLKB_POWER_UP)
  543. clock.p2 = limit->p2.p2_fast;
  544. else
  545. clock.p2 = limit->p2.p2_slow;
  546. } else {
  547. if (target < limit->p2.dot_limit)
  548. clock.p2 = limit->p2.p2_slow;
  549. else
  550. clock.p2 = limit->p2.p2_fast;
  551. }
  552. memset(best_clock, 0, sizeof(*best_clock));
  553. max_n = limit->n.max;
  554. /* based on hardware requirement, prefer smaller n to precision */
  555. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  556. /* based on hardware requirement, prefere larger m1,m2 */
  557. for (clock.m1 = limit->m1.max;
  558. clock.m1 >= limit->m1.min; clock.m1--) {
  559. for (clock.m2 = limit->m2.max;
  560. clock.m2 >= limit->m2.min; clock.m2--) {
  561. for (clock.p1 = limit->p1.max;
  562. clock.p1 >= limit->p1.min; clock.p1--) {
  563. int this_err;
  564. intel_clock(dev, refclk, &clock);
  565. if (!intel_PLL_is_valid(dev, limit,
  566. &clock))
  567. continue;
  568. this_err = abs(clock.dot - target);
  569. if (this_err < err_most) {
  570. *best_clock = clock;
  571. err_most = this_err;
  572. max_n = clock.n;
  573. found = true;
  574. }
  575. }
  576. }
  577. }
  578. }
  579. return found;
  580. }
  581. static bool
  582. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  583. int target, int refclk, intel_clock_t *best_clock)
  584. {
  585. struct drm_device *dev = crtc->dev;
  586. intel_clock_t clock;
  587. if (target < 200000) {
  588. clock.n = 1;
  589. clock.p1 = 2;
  590. clock.p2 = 10;
  591. clock.m1 = 12;
  592. clock.m2 = 9;
  593. } else {
  594. clock.n = 2;
  595. clock.p1 = 1;
  596. clock.p2 = 10;
  597. clock.m1 = 14;
  598. clock.m2 = 8;
  599. }
  600. intel_clock(dev, refclk, &clock);
  601. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  602. return true;
  603. }
  604. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  605. static bool
  606. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  607. int target, int refclk, intel_clock_t *best_clock)
  608. {
  609. intel_clock_t clock;
  610. if (target < 200000) {
  611. clock.p1 = 2;
  612. clock.p2 = 10;
  613. clock.n = 2;
  614. clock.m1 = 23;
  615. clock.m2 = 8;
  616. } else {
  617. clock.p1 = 1;
  618. clock.p2 = 10;
  619. clock.n = 1;
  620. clock.m1 = 14;
  621. clock.m2 = 2;
  622. }
  623. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  624. clock.p = (clock.p1 * clock.p2);
  625. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  626. clock.vco = 0;
  627. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  628. return true;
  629. }
  630. /**
  631. * intel_wait_for_vblank - wait for vblank on a given pipe
  632. * @dev: drm device
  633. * @pipe: pipe to wait for
  634. *
  635. * Wait for vblank to occur on a given pipe. Needed for various bits of
  636. * mode setting code.
  637. */
  638. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  639. {
  640. struct drm_i915_private *dev_priv = dev->dev_private;
  641. int pipestat_reg = PIPESTAT(pipe);
  642. /* Clear existing vblank status. Note this will clear any other
  643. * sticky status fields as well.
  644. *
  645. * This races with i915_driver_irq_handler() with the result
  646. * that either function could miss a vblank event. Here it is not
  647. * fatal, as we will either wait upon the next vblank interrupt or
  648. * timeout. Generally speaking intel_wait_for_vblank() is only
  649. * called during modeset at which time the GPU should be idle and
  650. * should *not* be performing page flips and thus not waiting on
  651. * vblanks...
  652. * Currently, the result of us stealing a vblank from the irq
  653. * handler is that a single frame will be skipped during swapbuffers.
  654. */
  655. I915_WRITE(pipestat_reg,
  656. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  657. /* Wait for vblank interrupt bit to set */
  658. if (wait_for(I915_READ(pipestat_reg) &
  659. PIPE_VBLANK_INTERRUPT_STATUS,
  660. 50))
  661. DRM_DEBUG_KMS("vblank wait timed out\n");
  662. }
  663. /*
  664. * intel_wait_for_pipe_off - wait for pipe to turn off
  665. * @dev: drm device
  666. * @pipe: pipe to wait for
  667. *
  668. * After disabling a pipe, we can't wait for vblank in the usual way,
  669. * spinning on the vblank interrupt status bit, since we won't actually
  670. * see an interrupt when the pipe is disabled.
  671. *
  672. * On Gen4 and above:
  673. * wait for the pipe register state bit to turn off
  674. *
  675. * Otherwise:
  676. * wait for the display line value to settle (it usually
  677. * ends up stopping at the start of the next frame).
  678. *
  679. */
  680. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  681. {
  682. struct drm_i915_private *dev_priv = dev->dev_private;
  683. if (INTEL_INFO(dev)->gen >= 4) {
  684. int reg = PIPECONF(pipe);
  685. /* Wait for the Pipe State to go off */
  686. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  687. 100))
  688. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  689. } else {
  690. u32 last_line;
  691. int reg = PIPEDSL(pipe);
  692. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  693. /* Wait for the display line to settle */
  694. do {
  695. last_line = I915_READ(reg) & DSL_LINEMASK;
  696. mdelay(5);
  697. } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
  698. time_after(timeout, jiffies));
  699. if (time_after(jiffies, timeout))
  700. DRM_DEBUG_KMS("pipe_off wait timed out\n");
  701. }
  702. }
  703. static const char *state_string(bool enabled)
  704. {
  705. return enabled ? "on" : "off";
  706. }
  707. /* Only for pre-ILK configs */
  708. static void assert_pll(struct drm_i915_private *dev_priv,
  709. enum pipe pipe, bool state)
  710. {
  711. int reg;
  712. u32 val;
  713. bool cur_state;
  714. reg = DPLL(pipe);
  715. val = I915_READ(reg);
  716. cur_state = !!(val & DPLL_VCO_ENABLE);
  717. WARN(cur_state != state,
  718. "PLL state assertion failure (expected %s, current %s)\n",
  719. state_string(state), state_string(cur_state));
  720. }
  721. #define assert_pll_enabled(d, p) assert_pll(d, p, true)
  722. #define assert_pll_disabled(d, p) assert_pll(d, p, false)
  723. /* For ILK+ */
  724. static void assert_pch_pll(struct drm_i915_private *dev_priv,
  725. enum pipe pipe, bool state)
  726. {
  727. int reg;
  728. u32 val;
  729. bool cur_state;
  730. reg = PCH_DPLL(pipe);
  731. val = I915_READ(reg);
  732. cur_state = !!(val & DPLL_VCO_ENABLE);
  733. WARN(cur_state != state,
  734. "PCH PLL state assertion failure (expected %s, current %s)\n",
  735. state_string(state), state_string(cur_state));
  736. }
  737. #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
  738. #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
  739. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  740. enum pipe pipe, bool state)
  741. {
  742. int reg;
  743. u32 val;
  744. bool cur_state;
  745. reg = FDI_TX_CTL(pipe);
  746. val = I915_READ(reg);
  747. cur_state = !!(val & FDI_TX_ENABLE);
  748. WARN(cur_state != state,
  749. "FDI TX state assertion failure (expected %s, current %s)\n",
  750. state_string(state), state_string(cur_state));
  751. }
  752. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  753. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  754. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  755. enum pipe pipe, bool state)
  756. {
  757. int reg;
  758. u32 val;
  759. bool cur_state;
  760. reg = FDI_RX_CTL(pipe);
  761. val = I915_READ(reg);
  762. cur_state = !!(val & FDI_RX_ENABLE);
  763. WARN(cur_state != state,
  764. "FDI RX state assertion failure (expected %s, current %s)\n",
  765. state_string(state), state_string(cur_state));
  766. }
  767. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  768. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  769. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  770. enum pipe pipe)
  771. {
  772. int reg;
  773. u32 val;
  774. /* ILK FDI PLL is always enabled */
  775. if (dev_priv->info->gen == 5)
  776. return;
  777. reg = FDI_TX_CTL(pipe);
  778. val = I915_READ(reg);
  779. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  780. }
  781. static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
  782. enum pipe pipe)
  783. {
  784. int reg;
  785. u32 val;
  786. reg = FDI_RX_CTL(pipe);
  787. val = I915_READ(reg);
  788. WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
  789. }
  790. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  791. enum pipe pipe)
  792. {
  793. int pp_reg, lvds_reg;
  794. u32 val;
  795. enum pipe panel_pipe = PIPE_A;
  796. bool locked = locked;
  797. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  798. pp_reg = PCH_PP_CONTROL;
  799. lvds_reg = PCH_LVDS;
  800. } else {
  801. pp_reg = PP_CONTROL;
  802. lvds_reg = LVDS;
  803. }
  804. val = I915_READ(pp_reg);
  805. if (!(val & PANEL_POWER_ON) ||
  806. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  807. locked = false;
  808. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  809. panel_pipe = PIPE_B;
  810. WARN(panel_pipe == pipe && locked,
  811. "panel assertion failure, pipe %c regs locked\n",
  812. pipe_name(pipe));
  813. }
  814. static void assert_pipe(struct drm_i915_private *dev_priv,
  815. enum pipe pipe, bool state)
  816. {
  817. int reg;
  818. u32 val;
  819. bool cur_state;
  820. reg = PIPECONF(pipe);
  821. val = I915_READ(reg);
  822. cur_state = !!(val & PIPECONF_ENABLE);
  823. WARN(cur_state != state,
  824. "pipe %c assertion failure (expected %s, current %s)\n",
  825. pipe_name(pipe), state_string(state), state_string(cur_state));
  826. }
  827. #define assert_pipe_enabled(d, p) assert_pipe(d, p, true)
  828. #define assert_pipe_disabled(d, p) assert_pipe(d, p, false)
  829. static void assert_plane_enabled(struct drm_i915_private *dev_priv,
  830. enum plane plane)
  831. {
  832. int reg;
  833. u32 val;
  834. reg = DSPCNTR(plane);
  835. val = I915_READ(reg);
  836. WARN(!(val & DISPLAY_PLANE_ENABLE),
  837. "plane %c assertion failure, should be active but is disabled\n",
  838. plane_name(plane));
  839. }
  840. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  841. enum pipe pipe)
  842. {
  843. int reg, i;
  844. u32 val;
  845. int cur_pipe;
  846. /* Planes are fixed to pipes on ILK+ */
  847. if (HAS_PCH_SPLIT(dev_priv->dev))
  848. return;
  849. /* Need to check both planes against the pipe */
  850. for (i = 0; i < 2; i++) {
  851. reg = DSPCNTR(i);
  852. val = I915_READ(reg);
  853. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  854. DISPPLANE_SEL_PIPE_SHIFT;
  855. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  856. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  857. plane_name(i), pipe_name(pipe));
  858. }
  859. }
  860. static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  861. {
  862. u32 val;
  863. bool enabled;
  864. val = I915_READ(PCH_DREF_CONTROL);
  865. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  866. DREF_SUPERSPREAD_SOURCE_MASK));
  867. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  868. }
  869. static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
  870. enum pipe pipe)
  871. {
  872. int reg;
  873. u32 val;
  874. bool enabled;
  875. reg = TRANSCONF(pipe);
  876. val = I915_READ(reg);
  877. enabled = !!(val & TRANS_ENABLE);
  878. WARN(enabled,
  879. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  880. pipe_name(pipe));
  881. }
  882. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv, enum pipe pipe,
  883. int reg, u32 port_sel, u32 val)
  884. {
  885. if ((val & DP_PORT_EN) == 0)
  886. return false;
  887. if (HAS_PCH_CPT(dev_priv->dev)) {
  888. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  889. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  890. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  891. return false;
  892. } else {
  893. if ((val & DP_PIPE_MASK) != (pipe << 30))
  894. return false;
  895. }
  896. return true;
  897. }
  898. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  899. enum pipe pipe, int reg, u32 port_sel)
  900. {
  901. u32 val = I915_READ(reg);
  902. WARN(dp_pipe_enabled(dev_priv, pipe, reg, port_sel, val),
  903. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  904. reg, pipe_name(pipe));
  905. }
  906. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  907. enum pipe pipe, int reg)
  908. {
  909. u32 val = I915_READ(reg);
  910. WARN(HDMI_PIPE_ENABLED(val, pipe),
  911. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  912. reg, pipe_name(pipe));
  913. }
  914. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  915. enum pipe pipe)
  916. {
  917. int reg;
  918. u32 val;
  919. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  920. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  921. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  922. reg = PCH_ADPA;
  923. val = I915_READ(reg);
  924. WARN(ADPA_PIPE_ENABLED(val, pipe),
  925. "PCH VGA enabled on transcoder %c, should be disabled\n",
  926. pipe_name(pipe));
  927. reg = PCH_LVDS;
  928. val = I915_READ(reg);
  929. WARN(LVDS_PIPE_ENABLED(val, pipe),
  930. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  931. pipe_name(pipe));
  932. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
  933. assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
  934. assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
  935. }
  936. /**
  937. * intel_enable_pll - enable a PLL
  938. * @dev_priv: i915 private structure
  939. * @pipe: pipe PLL to enable
  940. *
  941. * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
  942. * make sure the PLL reg is writable first though, since the panel write
  943. * protect mechanism may be enabled.
  944. *
  945. * Note! This is for pre-ILK only.
  946. */
  947. static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  948. {
  949. int reg;
  950. u32 val;
  951. /* No really, not for ILK+ */
  952. BUG_ON(dev_priv->info->gen >= 5);
  953. /* PLL is protected by panel, make sure we can write it */
  954. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  955. assert_panel_unlocked(dev_priv, pipe);
  956. reg = DPLL(pipe);
  957. val = I915_READ(reg);
  958. val |= DPLL_VCO_ENABLE;
  959. /* We do this three times for luck */
  960. I915_WRITE(reg, val);
  961. POSTING_READ(reg);
  962. udelay(150); /* wait for warmup */
  963. I915_WRITE(reg, val);
  964. POSTING_READ(reg);
  965. udelay(150); /* wait for warmup */
  966. I915_WRITE(reg, val);
  967. POSTING_READ(reg);
  968. udelay(150); /* wait for warmup */
  969. }
  970. /**
  971. * intel_disable_pll - disable a PLL
  972. * @dev_priv: i915 private structure
  973. * @pipe: pipe PLL to disable
  974. *
  975. * Disable the PLL for @pipe, making sure the pipe is off first.
  976. *
  977. * Note! This is for pre-ILK only.
  978. */
  979. static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  980. {
  981. int reg;
  982. u32 val;
  983. /* Don't disable pipe A or pipe A PLLs if needed */
  984. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  985. return;
  986. /* Make sure the pipe isn't still relying on us */
  987. assert_pipe_disabled(dev_priv, pipe);
  988. reg = DPLL(pipe);
  989. val = I915_READ(reg);
  990. val &= ~DPLL_VCO_ENABLE;
  991. I915_WRITE(reg, val);
  992. POSTING_READ(reg);
  993. }
  994. /**
  995. * intel_enable_pch_pll - enable PCH PLL
  996. * @dev_priv: i915 private structure
  997. * @pipe: pipe PLL to enable
  998. *
  999. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1000. * drives the transcoder clock.
  1001. */
  1002. static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
  1003. enum pipe pipe)
  1004. {
  1005. int reg;
  1006. u32 val;
  1007. /* PCH only available on ILK+ */
  1008. BUG_ON(dev_priv->info->gen < 5);
  1009. /* PCH refclock must be enabled first */
  1010. assert_pch_refclk_enabled(dev_priv);
  1011. reg = PCH_DPLL(pipe);
  1012. val = I915_READ(reg);
  1013. val |= DPLL_VCO_ENABLE;
  1014. I915_WRITE(reg, val);
  1015. POSTING_READ(reg);
  1016. udelay(200);
  1017. }
  1018. static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
  1019. enum pipe pipe)
  1020. {
  1021. int reg;
  1022. u32 val;
  1023. /* PCH only available on ILK+ */
  1024. BUG_ON(dev_priv->info->gen < 5);
  1025. /* Make sure transcoder isn't still depending on us */
  1026. assert_transcoder_disabled(dev_priv, pipe);
  1027. reg = PCH_DPLL(pipe);
  1028. val = I915_READ(reg);
  1029. val &= ~DPLL_VCO_ENABLE;
  1030. I915_WRITE(reg, val);
  1031. POSTING_READ(reg);
  1032. udelay(200);
  1033. }
  1034. static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
  1035. enum pipe pipe)
  1036. {
  1037. int reg;
  1038. u32 val;
  1039. /* PCH only available on ILK+ */
  1040. BUG_ON(dev_priv->info->gen < 5);
  1041. /* Make sure PCH DPLL is enabled */
  1042. assert_pch_pll_enabled(dev_priv, pipe);
  1043. /* FDI must be feeding us bits for PCH ports */
  1044. assert_fdi_tx_enabled(dev_priv, pipe);
  1045. assert_fdi_rx_enabled(dev_priv, pipe);
  1046. reg = TRANSCONF(pipe);
  1047. val = I915_READ(reg);
  1048. if (HAS_PCH_IBX(dev_priv->dev)) {
  1049. /*
  1050. * make the BPC in transcoder be consistent with
  1051. * that in pipeconf reg.
  1052. */
  1053. val &= ~PIPE_BPC_MASK;
  1054. val |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
  1055. }
  1056. I915_WRITE(reg, val | TRANS_ENABLE);
  1057. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1058. DRM_ERROR("failed to enable transcoder %d\n", pipe);
  1059. }
  1060. static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
  1061. enum pipe pipe)
  1062. {
  1063. int reg;
  1064. u32 val;
  1065. /* FDI relies on the transcoder */
  1066. assert_fdi_tx_disabled(dev_priv, pipe);
  1067. assert_fdi_rx_disabled(dev_priv, pipe);
  1068. /* Ports must be off as well */
  1069. assert_pch_ports_disabled(dev_priv, pipe);
  1070. reg = TRANSCONF(pipe);
  1071. val = I915_READ(reg);
  1072. val &= ~TRANS_ENABLE;
  1073. I915_WRITE(reg, val);
  1074. /* wait for PCH transcoder off, transcoder state */
  1075. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1076. DRM_ERROR("failed to disable transcoder\n");
  1077. }
  1078. /**
  1079. * intel_enable_pipe - enable a pipe, asserting requirements
  1080. * @dev_priv: i915 private structure
  1081. * @pipe: pipe to enable
  1082. * @pch_port: on ILK+, is this pipe driving a PCH port or not
  1083. *
  1084. * Enable @pipe, making sure that various hardware specific requirements
  1085. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1086. *
  1087. * @pipe should be %PIPE_A or %PIPE_B.
  1088. *
  1089. * Will wait until the pipe is actually running (i.e. first vblank) before
  1090. * returning.
  1091. */
  1092. static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
  1093. bool pch_port)
  1094. {
  1095. int reg;
  1096. u32 val;
  1097. /*
  1098. * A pipe without a PLL won't actually be able to drive bits from
  1099. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1100. * need the check.
  1101. */
  1102. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1103. assert_pll_enabled(dev_priv, pipe);
  1104. else {
  1105. if (pch_port) {
  1106. /* if driving the PCH, we need FDI enabled */
  1107. assert_fdi_rx_pll_enabled(dev_priv, pipe);
  1108. assert_fdi_tx_pll_enabled(dev_priv, pipe);
  1109. }
  1110. /* FIXME: assert CPU port conditions for SNB+ */
  1111. }
  1112. reg = PIPECONF(pipe);
  1113. val = I915_READ(reg);
  1114. if (val & PIPECONF_ENABLE)
  1115. return;
  1116. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1117. intel_wait_for_vblank(dev_priv->dev, pipe);
  1118. }
  1119. /**
  1120. * intel_disable_pipe - disable a pipe, asserting requirements
  1121. * @dev_priv: i915 private structure
  1122. * @pipe: pipe to disable
  1123. *
  1124. * Disable @pipe, making sure that various hardware specific requirements
  1125. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1126. *
  1127. * @pipe should be %PIPE_A or %PIPE_B.
  1128. *
  1129. * Will wait until the pipe has shut down before returning.
  1130. */
  1131. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1132. enum pipe pipe)
  1133. {
  1134. int reg;
  1135. u32 val;
  1136. /*
  1137. * Make sure planes won't keep trying to pump pixels to us,
  1138. * or we might hang the display.
  1139. */
  1140. assert_planes_disabled(dev_priv, pipe);
  1141. /* Don't disable pipe A or pipe A PLLs if needed */
  1142. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1143. return;
  1144. reg = PIPECONF(pipe);
  1145. val = I915_READ(reg);
  1146. if ((val & PIPECONF_ENABLE) == 0)
  1147. return;
  1148. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1149. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1150. }
  1151. /*
  1152. * Plane regs are double buffered, going from enabled->disabled needs a
  1153. * trigger in order to latch. The display address reg provides this.
  1154. */
  1155. static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
  1156. enum plane plane)
  1157. {
  1158. I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
  1159. I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
  1160. }
  1161. /**
  1162. * intel_enable_plane - enable a display plane on a given pipe
  1163. * @dev_priv: i915 private structure
  1164. * @plane: plane to enable
  1165. * @pipe: pipe being fed
  1166. *
  1167. * Enable @plane on @pipe, making sure that @pipe is running first.
  1168. */
  1169. static void intel_enable_plane(struct drm_i915_private *dev_priv,
  1170. enum plane plane, enum pipe pipe)
  1171. {
  1172. int reg;
  1173. u32 val;
  1174. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1175. assert_pipe_enabled(dev_priv, pipe);
  1176. reg = DSPCNTR(plane);
  1177. val = I915_READ(reg);
  1178. if (val & DISPLAY_PLANE_ENABLE)
  1179. return;
  1180. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1181. intel_flush_display_plane(dev_priv, plane);
  1182. intel_wait_for_vblank(dev_priv->dev, pipe);
  1183. }
  1184. /**
  1185. * intel_disable_plane - disable a display plane
  1186. * @dev_priv: i915 private structure
  1187. * @plane: plane to disable
  1188. * @pipe: pipe consuming the data
  1189. *
  1190. * Disable @plane; should be an independent operation.
  1191. */
  1192. static void intel_disable_plane(struct drm_i915_private *dev_priv,
  1193. enum plane plane, enum pipe pipe)
  1194. {
  1195. int reg;
  1196. u32 val;
  1197. reg = DSPCNTR(plane);
  1198. val = I915_READ(reg);
  1199. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  1200. return;
  1201. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1202. intel_flush_display_plane(dev_priv, plane);
  1203. intel_wait_for_vblank(dev_priv->dev, pipe);
  1204. }
  1205. static void disable_pch_dp(struct drm_i915_private *dev_priv,
  1206. enum pipe pipe, int reg, u32 port_sel)
  1207. {
  1208. u32 val = I915_READ(reg);
  1209. if (dp_pipe_enabled(dev_priv, pipe, reg, port_sel, val)) {
  1210. DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
  1211. I915_WRITE(reg, val & ~DP_PORT_EN);
  1212. }
  1213. }
  1214. static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
  1215. enum pipe pipe, int reg)
  1216. {
  1217. u32 val = I915_READ(reg);
  1218. if (HDMI_PIPE_ENABLED(val, pipe)) {
  1219. DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
  1220. reg, pipe);
  1221. I915_WRITE(reg, val & ~PORT_ENABLE);
  1222. }
  1223. }
  1224. /* Disable any ports connected to this transcoder */
  1225. static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
  1226. enum pipe pipe)
  1227. {
  1228. u32 reg, val;
  1229. val = I915_READ(PCH_PP_CONTROL);
  1230. I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
  1231. disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1232. disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1233. disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1234. reg = PCH_ADPA;
  1235. val = I915_READ(reg);
  1236. if (ADPA_PIPE_ENABLED(val, pipe))
  1237. I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
  1238. reg = PCH_LVDS;
  1239. val = I915_READ(reg);
  1240. if (LVDS_PIPE_ENABLED(val, pipe)) {
  1241. I915_WRITE(reg, val & ~LVDS_PORT_EN);
  1242. POSTING_READ(reg);
  1243. udelay(100);
  1244. }
  1245. disable_pch_hdmi(dev_priv, pipe, HDMIB);
  1246. disable_pch_hdmi(dev_priv, pipe, HDMIC);
  1247. disable_pch_hdmi(dev_priv, pipe, HDMID);
  1248. }
  1249. static void i8xx_disable_fbc(struct drm_device *dev)
  1250. {
  1251. struct drm_i915_private *dev_priv = dev->dev_private;
  1252. u32 fbc_ctl;
  1253. /* Disable compression */
  1254. fbc_ctl = I915_READ(FBC_CONTROL);
  1255. if ((fbc_ctl & FBC_CTL_EN) == 0)
  1256. return;
  1257. fbc_ctl &= ~FBC_CTL_EN;
  1258. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1259. /* Wait for compressing bit to clear */
  1260. if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
  1261. DRM_DEBUG_KMS("FBC idle timed out\n");
  1262. return;
  1263. }
  1264. DRM_DEBUG_KMS("disabled FBC\n");
  1265. }
  1266. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1267. {
  1268. struct drm_device *dev = crtc->dev;
  1269. struct drm_i915_private *dev_priv = dev->dev_private;
  1270. struct drm_framebuffer *fb = crtc->fb;
  1271. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1272. struct drm_i915_gem_object *obj = intel_fb->obj;
  1273. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1274. int cfb_pitch;
  1275. int plane, i;
  1276. u32 fbc_ctl, fbc_ctl2;
  1277. cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  1278. if (fb->pitch < cfb_pitch)
  1279. cfb_pitch = fb->pitch;
  1280. /* FBC_CTL wants 64B units */
  1281. cfb_pitch = (cfb_pitch / 64) - 1;
  1282. plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  1283. /* Clear old tags */
  1284. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  1285. I915_WRITE(FBC_TAG + (i * 4), 0);
  1286. /* Set it up... */
  1287. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
  1288. fbc_ctl2 |= plane;
  1289. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  1290. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  1291. /* enable it... */
  1292. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  1293. if (IS_I945GM(dev))
  1294. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  1295. fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  1296. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  1297. fbc_ctl |= obj->fence_reg;
  1298. I915_WRITE(FBC_CONTROL, fbc_ctl);
  1299. DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
  1300. cfb_pitch, crtc->y, intel_crtc->plane);
  1301. }
  1302. static bool i8xx_fbc_enabled(struct drm_device *dev)
  1303. {
  1304. struct drm_i915_private *dev_priv = dev->dev_private;
  1305. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  1306. }
  1307. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1308. {
  1309. struct drm_device *dev = crtc->dev;
  1310. struct drm_i915_private *dev_priv = dev->dev_private;
  1311. struct drm_framebuffer *fb = crtc->fb;
  1312. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1313. struct drm_i915_gem_object *obj = intel_fb->obj;
  1314. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1315. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1316. unsigned long stall_watermark = 200;
  1317. u32 dpfc_ctl;
  1318. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  1319. dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
  1320. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  1321. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1322. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1323. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1324. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1325. /* enable it... */
  1326. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1327. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1328. }
  1329. static void g4x_disable_fbc(struct drm_device *dev)
  1330. {
  1331. struct drm_i915_private *dev_priv = dev->dev_private;
  1332. u32 dpfc_ctl;
  1333. /* Disable compression */
  1334. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1335. if (dpfc_ctl & DPFC_CTL_EN) {
  1336. dpfc_ctl &= ~DPFC_CTL_EN;
  1337. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1338. DRM_DEBUG_KMS("disabled FBC\n");
  1339. }
  1340. }
  1341. static bool g4x_fbc_enabled(struct drm_device *dev)
  1342. {
  1343. struct drm_i915_private *dev_priv = dev->dev_private;
  1344. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1345. }
  1346. static void sandybridge_blit_fbc_update(struct drm_device *dev)
  1347. {
  1348. struct drm_i915_private *dev_priv = dev->dev_private;
  1349. u32 blt_ecoskpd;
  1350. /* Make sure blitter notifies FBC of writes */
  1351. gen6_gt_force_wake_get(dev_priv);
  1352. blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
  1353. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
  1354. GEN6_BLITTER_LOCK_SHIFT;
  1355. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1356. blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
  1357. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1358. blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
  1359. GEN6_BLITTER_LOCK_SHIFT);
  1360. I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
  1361. POSTING_READ(GEN6_BLITTER_ECOSKPD);
  1362. gen6_gt_force_wake_put(dev_priv);
  1363. }
  1364. static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1365. {
  1366. struct drm_device *dev = crtc->dev;
  1367. struct drm_i915_private *dev_priv = dev->dev_private;
  1368. struct drm_framebuffer *fb = crtc->fb;
  1369. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  1370. struct drm_i915_gem_object *obj = intel_fb->obj;
  1371. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1372. int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
  1373. unsigned long stall_watermark = 200;
  1374. u32 dpfc_ctl;
  1375. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1376. dpfc_ctl &= DPFC_RESERVED;
  1377. dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
  1378. /* Set persistent mode for front-buffer rendering, ala X. */
  1379. dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
  1380. dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
  1381. I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
  1382. I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  1383. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  1384. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1385. I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
  1386. I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
  1387. /* enable it... */
  1388. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
  1389. if (IS_GEN6(dev)) {
  1390. I915_WRITE(SNB_DPFC_CTL_SA,
  1391. SNB_CPU_FENCE_ENABLE | obj->fence_reg);
  1392. I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
  1393. sandybridge_blit_fbc_update(dev);
  1394. }
  1395. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1396. }
  1397. static void ironlake_disable_fbc(struct drm_device *dev)
  1398. {
  1399. struct drm_i915_private *dev_priv = dev->dev_private;
  1400. u32 dpfc_ctl;
  1401. /* Disable compression */
  1402. dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
  1403. if (dpfc_ctl & DPFC_CTL_EN) {
  1404. dpfc_ctl &= ~DPFC_CTL_EN;
  1405. I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
  1406. DRM_DEBUG_KMS("disabled FBC\n");
  1407. }
  1408. }
  1409. static bool ironlake_fbc_enabled(struct drm_device *dev)
  1410. {
  1411. struct drm_i915_private *dev_priv = dev->dev_private;
  1412. return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
  1413. }
  1414. bool intel_fbc_enabled(struct drm_device *dev)
  1415. {
  1416. struct drm_i915_private *dev_priv = dev->dev_private;
  1417. if (!dev_priv->display.fbc_enabled)
  1418. return false;
  1419. return dev_priv->display.fbc_enabled(dev);
  1420. }
  1421. static void intel_fbc_work_fn(struct work_struct *__work)
  1422. {
  1423. struct intel_fbc_work *work =
  1424. container_of(to_delayed_work(__work),
  1425. struct intel_fbc_work, work);
  1426. struct drm_device *dev = work->crtc->dev;
  1427. struct drm_i915_private *dev_priv = dev->dev_private;
  1428. mutex_lock(&dev->struct_mutex);
  1429. if (work == dev_priv->fbc_work) {
  1430. /* Double check that we haven't switched fb without cancelling
  1431. * the prior work.
  1432. */
  1433. if (work->crtc->fb == work->fb) {
  1434. dev_priv->display.enable_fbc(work->crtc,
  1435. work->interval);
  1436. dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
  1437. dev_priv->cfb_fb = work->crtc->fb->base.id;
  1438. dev_priv->cfb_y = work->crtc->y;
  1439. }
  1440. dev_priv->fbc_work = NULL;
  1441. }
  1442. mutex_unlock(&dev->struct_mutex);
  1443. kfree(work);
  1444. }
  1445. static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
  1446. {
  1447. if (dev_priv->fbc_work == NULL)
  1448. return;
  1449. DRM_DEBUG_KMS("cancelling pending FBC enable\n");
  1450. /* Synchronisation is provided by struct_mutex and checking of
  1451. * dev_priv->fbc_work, so we can perform the cancellation
  1452. * entirely asynchronously.
  1453. */
  1454. if (cancel_delayed_work(&dev_priv->fbc_work->work))
  1455. /* tasklet was killed before being run, clean up */
  1456. kfree(dev_priv->fbc_work);
  1457. /* Mark the work as no longer wanted so that if it does
  1458. * wake-up (because the work was already running and waiting
  1459. * for our mutex), it will discover that is no longer
  1460. * necessary to run.
  1461. */
  1462. dev_priv->fbc_work = NULL;
  1463. }
  1464. static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1465. {
  1466. struct intel_fbc_work *work;
  1467. struct drm_device *dev = crtc->dev;
  1468. struct drm_i915_private *dev_priv = dev->dev_private;
  1469. if (!dev_priv->display.enable_fbc)
  1470. return;
  1471. intel_cancel_fbc_work(dev_priv);
  1472. work = kzalloc(sizeof *work, GFP_KERNEL);
  1473. if (work == NULL) {
  1474. dev_priv->display.enable_fbc(crtc, interval);
  1475. return;
  1476. }
  1477. work->crtc = crtc;
  1478. work->fb = crtc->fb;
  1479. work->interval = interval;
  1480. INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
  1481. dev_priv->fbc_work = work;
  1482. DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
  1483. /* Delay the actual enabling to let pageflipping cease and the
  1484. * display to settle before starting the compression. Note that
  1485. * this delay also serves a second purpose: it allows for a
  1486. * vblank to pass after disabling the FBC before we attempt
  1487. * to modify the control registers.
  1488. *
  1489. * A more complicated solution would involve tracking vblanks
  1490. * following the termination of the page-flipping sequence
  1491. * and indeed performing the enable as a co-routine and not
  1492. * waiting synchronously upon the vblank.
  1493. */
  1494. schedule_delayed_work(&work->work, msecs_to_jiffies(50));
  1495. }
  1496. void intel_disable_fbc(struct drm_device *dev)
  1497. {
  1498. struct drm_i915_private *dev_priv = dev->dev_private;
  1499. intel_cancel_fbc_work(dev_priv);
  1500. if (!dev_priv->display.disable_fbc)
  1501. return;
  1502. dev_priv->display.disable_fbc(dev);
  1503. dev_priv->cfb_plane = -1;
  1504. }
  1505. /**
  1506. * intel_update_fbc - enable/disable FBC as needed
  1507. * @dev: the drm_device
  1508. *
  1509. * Set up the framebuffer compression hardware at mode set time. We
  1510. * enable it if possible:
  1511. * - plane A only (on pre-965)
  1512. * - no pixel mulitply/line duplication
  1513. * - no alpha buffer discard
  1514. * - no dual wide
  1515. * - framebuffer <= 2048 in width, 1536 in height
  1516. *
  1517. * We can't assume that any compression will take place (worst case),
  1518. * so the compressed buffer has to be the same size as the uncompressed
  1519. * one. It also must reside (along with the line length buffer) in
  1520. * stolen memory.
  1521. *
  1522. * We need to enable/disable FBC on a global basis.
  1523. */
  1524. static void intel_update_fbc(struct drm_device *dev)
  1525. {
  1526. struct drm_i915_private *dev_priv = dev->dev_private;
  1527. struct drm_crtc *crtc = NULL, *tmp_crtc;
  1528. struct intel_crtc *intel_crtc;
  1529. struct drm_framebuffer *fb;
  1530. struct intel_framebuffer *intel_fb;
  1531. struct drm_i915_gem_object *obj;
  1532. DRM_DEBUG_KMS("\n");
  1533. if (!i915_powersave)
  1534. return;
  1535. if (!I915_HAS_FBC(dev))
  1536. return;
  1537. /*
  1538. * If FBC is already on, we just have to verify that we can
  1539. * keep it that way...
  1540. * Need to disable if:
  1541. * - more than one pipe is active
  1542. * - changing FBC params (stride, fence, mode)
  1543. * - new fb is too large to fit in compressed buffer
  1544. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1545. */
  1546. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1547. if (tmp_crtc->enabled && tmp_crtc->fb) {
  1548. if (crtc) {
  1549. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1550. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1551. goto out_disable;
  1552. }
  1553. crtc = tmp_crtc;
  1554. }
  1555. }
  1556. if (!crtc || crtc->fb == NULL) {
  1557. DRM_DEBUG_KMS("no output, disabling\n");
  1558. dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
  1559. goto out_disable;
  1560. }
  1561. intel_crtc = to_intel_crtc(crtc);
  1562. fb = crtc->fb;
  1563. intel_fb = to_intel_framebuffer(fb);
  1564. obj = intel_fb->obj;
  1565. if (!i915_enable_fbc) {
  1566. DRM_DEBUG_KMS("fbc disabled per module param (default off)\n");
  1567. dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
  1568. goto out_disable;
  1569. }
  1570. if (intel_fb->obj->base.size > dev_priv->cfb_size) {
  1571. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1572. "compression\n");
  1573. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1574. goto out_disable;
  1575. }
  1576. if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
  1577. (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
  1578. DRM_DEBUG_KMS("mode incompatible with compression, "
  1579. "disabling\n");
  1580. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1581. goto out_disable;
  1582. }
  1583. if ((crtc->mode.hdisplay > 2048) ||
  1584. (crtc->mode.vdisplay > 1536)) {
  1585. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1586. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1587. goto out_disable;
  1588. }
  1589. if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
  1590. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1591. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1592. goto out_disable;
  1593. }
  1594. /* The use of a CPU fence is mandatory in order to detect writes
  1595. * by the CPU to the scanout and trigger updates to the FBC.
  1596. */
  1597. if (obj->tiling_mode != I915_TILING_X ||
  1598. obj->fence_reg == I915_FENCE_REG_NONE) {
  1599. DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
  1600. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1601. goto out_disable;
  1602. }
  1603. /* If the kernel debugger is active, always disable compression */
  1604. if (in_dbg_master())
  1605. goto out_disable;
  1606. /* If the scanout has not changed, don't modify the FBC settings.
  1607. * Note that we make the fundamental assumption that the fb->obj
  1608. * cannot be unpinned (and have its GTT offset and fence revoked)
  1609. * without first being decoupled from the scanout and FBC disabled.
  1610. */
  1611. if (dev_priv->cfb_plane == intel_crtc->plane &&
  1612. dev_priv->cfb_fb == fb->base.id &&
  1613. dev_priv->cfb_y == crtc->y)
  1614. return;
  1615. if (intel_fbc_enabled(dev)) {
  1616. /* We update FBC along two paths, after changing fb/crtc
  1617. * configuration (modeswitching) and after page-flipping
  1618. * finishes. For the latter, we know that not only did
  1619. * we disable the FBC at the start of the page-flip
  1620. * sequence, but also more than one vblank has passed.
  1621. *
  1622. * For the former case of modeswitching, it is possible
  1623. * to switch between two FBC valid configurations
  1624. * instantaneously so we do need to disable the FBC
  1625. * before we can modify its control registers. We also
  1626. * have to wait for the next vblank for that to take
  1627. * effect. However, since we delay enabling FBC we can
  1628. * assume that a vblank has passed since disabling and
  1629. * that we can safely alter the registers in the deferred
  1630. * callback.
  1631. *
  1632. * In the scenario that we go from a valid to invalid
  1633. * and then back to valid FBC configuration we have
  1634. * no strict enforcement that a vblank occurred since
  1635. * disabling the FBC. However, along all current pipe
  1636. * disabling paths we do need to wait for a vblank at
  1637. * some point. And we wait before enabling FBC anyway.
  1638. */
  1639. DRM_DEBUG_KMS("disabling active FBC for update\n");
  1640. intel_disable_fbc(dev);
  1641. }
  1642. intel_enable_fbc(crtc, 500);
  1643. return;
  1644. out_disable:
  1645. /* Multiple disables should be harmless */
  1646. if (intel_fbc_enabled(dev)) {
  1647. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1648. intel_disable_fbc(dev);
  1649. }
  1650. }
  1651. int
  1652. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1653. struct drm_i915_gem_object *obj,
  1654. struct intel_ring_buffer *pipelined)
  1655. {
  1656. struct drm_i915_private *dev_priv = dev->dev_private;
  1657. u32 alignment;
  1658. int ret;
  1659. switch (obj->tiling_mode) {
  1660. case I915_TILING_NONE:
  1661. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1662. alignment = 128 * 1024;
  1663. else if (INTEL_INFO(dev)->gen >= 4)
  1664. alignment = 4 * 1024;
  1665. else
  1666. alignment = 64 * 1024;
  1667. break;
  1668. case I915_TILING_X:
  1669. /* pin() will align the object as required by fence */
  1670. alignment = 0;
  1671. break;
  1672. case I915_TILING_Y:
  1673. /* FIXME: Is this true? */
  1674. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1675. return -EINVAL;
  1676. default:
  1677. BUG();
  1678. }
  1679. dev_priv->mm.interruptible = false;
  1680. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1681. if (ret)
  1682. goto err_interruptible;
  1683. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1684. * fence, whereas 965+ only requires a fence if using
  1685. * framebuffer compression. For simplicity, we always install
  1686. * a fence as the cost is not that onerous.
  1687. */
  1688. if (obj->tiling_mode != I915_TILING_NONE) {
  1689. ret = i915_gem_object_get_fence(obj, pipelined);
  1690. if (ret)
  1691. goto err_unpin;
  1692. }
  1693. dev_priv->mm.interruptible = true;
  1694. return 0;
  1695. err_unpin:
  1696. i915_gem_object_unpin(obj);
  1697. err_interruptible:
  1698. dev_priv->mm.interruptible = true;
  1699. return ret;
  1700. }
  1701. static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1702. int x, int y)
  1703. {
  1704. struct drm_device *dev = crtc->dev;
  1705. struct drm_i915_private *dev_priv = dev->dev_private;
  1706. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1707. struct intel_framebuffer *intel_fb;
  1708. struct drm_i915_gem_object *obj;
  1709. int plane = intel_crtc->plane;
  1710. unsigned long Start, Offset;
  1711. u32 dspcntr;
  1712. u32 reg;
  1713. switch (plane) {
  1714. case 0:
  1715. case 1:
  1716. break;
  1717. default:
  1718. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1719. return -EINVAL;
  1720. }
  1721. intel_fb = to_intel_framebuffer(fb);
  1722. obj = intel_fb->obj;
  1723. reg = DSPCNTR(plane);
  1724. dspcntr = I915_READ(reg);
  1725. /* Mask out pixel format bits in case we change it */
  1726. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1727. switch (fb->bits_per_pixel) {
  1728. case 8:
  1729. dspcntr |= DISPPLANE_8BPP;
  1730. break;
  1731. case 16:
  1732. if (fb->depth == 15)
  1733. dspcntr |= DISPPLANE_15_16BPP;
  1734. else
  1735. dspcntr |= DISPPLANE_16BPP;
  1736. break;
  1737. case 24:
  1738. case 32:
  1739. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1740. break;
  1741. default:
  1742. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1743. return -EINVAL;
  1744. }
  1745. if (INTEL_INFO(dev)->gen >= 4) {
  1746. if (obj->tiling_mode != I915_TILING_NONE)
  1747. dspcntr |= DISPPLANE_TILED;
  1748. else
  1749. dspcntr &= ~DISPPLANE_TILED;
  1750. }
  1751. I915_WRITE(reg, dspcntr);
  1752. Start = obj->gtt_offset;
  1753. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1754. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1755. Start, Offset, x, y, fb->pitch);
  1756. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1757. if (INTEL_INFO(dev)->gen >= 4) {
  1758. I915_WRITE(DSPSURF(plane), Start);
  1759. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1760. I915_WRITE(DSPADDR(plane), Offset);
  1761. } else
  1762. I915_WRITE(DSPADDR(plane), Start + Offset);
  1763. POSTING_READ(reg);
  1764. return 0;
  1765. }
  1766. static int ironlake_update_plane(struct drm_crtc *crtc,
  1767. struct drm_framebuffer *fb, int x, int y)
  1768. {
  1769. struct drm_device *dev = crtc->dev;
  1770. struct drm_i915_private *dev_priv = dev->dev_private;
  1771. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1772. struct intel_framebuffer *intel_fb;
  1773. struct drm_i915_gem_object *obj;
  1774. int plane = intel_crtc->plane;
  1775. unsigned long Start, Offset;
  1776. u32 dspcntr;
  1777. u32 reg;
  1778. switch (plane) {
  1779. case 0:
  1780. case 1:
  1781. break;
  1782. default:
  1783. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1784. return -EINVAL;
  1785. }
  1786. intel_fb = to_intel_framebuffer(fb);
  1787. obj = intel_fb->obj;
  1788. reg = DSPCNTR(plane);
  1789. dspcntr = I915_READ(reg);
  1790. /* Mask out pixel format bits in case we change it */
  1791. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1792. switch (fb->bits_per_pixel) {
  1793. case 8:
  1794. dspcntr |= DISPPLANE_8BPP;
  1795. break;
  1796. case 16:
  1797. if (fb->depth != 16)
  1798. return -EINVAL;
  1799. dspcntr |= DISPPLANE_16BPP;
  1800. break;
  1801. case 24:
  1802. case 32:
  1803. if (fb->depth == 24)
  1804. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1805. else if (fb->depth == 30)
  1806. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  1807. else
  1808. return -EINVAL;
  1809. break;
  1810. default:
  1811. DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
  1812. return -EINVAL;
  1813. }
  1814. if (obj->tiling_mode != I915_TILING_NONE)
  1815. dspcntr |= DISPPLANE_TILED;
  1816. else
  1817. dspcntr &= ~DISPPLANE_TILED;
  1818. /* must disable */
  1819. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1820. I915_WRITE(reg, dspcntr);
  1821. Start = obj->gtt_offset;
  1822. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1823. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1824. Start, Offset, x, y, fb->pitch);
  1825. I915_WRITE(DSPSTRIDE(plane), fb->pitch);
  1826. I915_WRITE(DSPSURF(plane), Start);
  1827. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  1828. I915_WRITE(DSPADDR(plane), Offset);
  1829. POSTING_READ(reg);
  1830. return 0;
  1831. }
  1832. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1833. static int
  1834. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1835. int x, int y, enum mode_set_atomic state)
  1836. {
  1837. struct drm_device *dev = crtc->dev;
  1838. struct drm_i915_private *dev_priv = dev->dev_private;
  1839. int ret;
  1840. ret = dev_priv->display.update_plane(crtc, fb, x, y);
  1841. if (ret)
  1842. return ret;
  1843. intel_update_fbc(dev);
  1844. intel_increase_pllclock(crtc);
  1845. return 0;
  1846. }
  1847. static int
  1848. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1849. struct drm_framebuffer *old_fb)
  1850. {
  1851. struct drm_device *dev = crtc->dev;
  1852. struct drm_i915_master_private *master_priv;
  1853. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1854. int ret;
  1855. /* no fb bound */
  1856. if (!crtc->fb) {
  1857. DRM_ERROR("No FB bound\n");
  1858. return 0;
  1859. }
  1860. switch (intel_crtc->plane) {
  1861. case 0:
  1862. case 1:
  1863. break;
  1864. default:
  1865. DRM_ERROR("no plane for crtc\n");
  1866. return -EINVAL;
  1867. }
  1868. mutex_lock(&dev->struct_mutex);
  1869. ret = intel_pin_and_fence_fb_obj(dev,
  1870. to_intel_framebuffer(crtc->fb)->obj,
  1871. NULL);
  1872. if (ret != 0) {
  1873. mutex_unlock(&dev->struct_mutex);
  1874. DRM_ERROR("pin & fence failed\n");
  1875. return ret;
  1876. }
  1877. if (old_fb) {
  1878. struct drm_i915_private *dev_priv = dev->dev_private;
  1879. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  1880. wait_event(dev_priv->pending_flip_queue,
  1881. atomic_read(&dev_priv->mm.wedged) ||
  1882. atomic_read(&obj->pending_flip) == 0);
  1883. /* Big Hammer, we also need to ensure that any pending
  1884. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  1885. * current scanout is retired before unpinning the old
  1886. * framebuffer.
  1887. *
  1888. * This should only fail upon a hung GPU, in which case we
  1889. * can safely continue.
  1890. */
  1891. ret = i915_gem_object_finish_gpu(obj);
  1892. (void) ret;
  1893. }
  1894. ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
  1895. LEAVE_ATOMIC_MODE_SET);
  1896. if (ret) {
  1897. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  1898. mutex_unlock(&dev->struct_mutex);
  1899. DRM_ERROR("failed to update base address\n");
  1900. return ret;
  1901. }
  1902. if (old_fb) {
  1903. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1904. i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
  1905. }
  1906. mutex_unlock(&dev->struct_mutex);
  1907. if (!dev->primary->master)
  1908. return 0;
  1909. master_priv = dev->primary->master->driver_priv;
  1910. if (!master_priv->sarea_priv)
  1911. return 0;
  1912. if (intel_crtc->pipe) {
  1913. master_priv->sarea_priv->pipeB_x = x;
  1914. master_priv->sarea_priv->pipeB_y = y;
  1915. } else {
  1916. master_priv->sarea_priv->pipeA_x = x;
  1917. master_priv->sarea_priv->pipeA_y = y;
  1918. }
  1919. return 0;
  1920. }
  1921. static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
  1922. {
  1923. struct drm_device *dev = crtc->dev;
  1924. struct drm_i915_private *dev_priv = dev->dev_private;
  1925. u32 dpa_ctl;
  1926. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1927. dpa_ctl = I915_READ(DP_A);
  1928. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1929. if (clock < 200000) {
  1930. u32 temp;
  1931. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1932. /* workaround for 160Mhz:
  1933. 1) program 0x4600c bits 15:0 = 0x8124
  1934. 2) program 0x46010 bit 0 = 1
  1935. 3) program 0x46034 bit 24 = 1
  1936. 4) program 0x64000 bit 14 = 1
  1937. */
  1938. temp = I915_READ(0x4600c);
  1939. temp &= 0xffff0000;
  1940. I915_WRITE(0x4600c, temp | 0x8124);
  1941. temp = I915_READ(0x46010);
  1942. I915_WRITE(0x46010, temp | 1);
  1943. temp = I915_READ(0x46034);
  1944. I915_WRITE(0x46034, temp | (1 << 24));
  1945. } else {
  1946. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1947. }
  1948. I915_WRITE(DP_A, dpa_ctl);
  1949. POSTING_READ(DP_A);
  1950. udelay(500);
  1951. }
  1952. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  1953. {
  1954. struct drm_device *dev = crtc->dev;
  1955. struct drm_i915_private *dev_priv = dev->dev_private;
  1956. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1957. int pipe = intel_crtc->pipe;
  1958. u32 reg, temp;
  1959. /* enable normal train */
  1960. reg = FDI_TX_CTL(pipe);
  1961. temp = I915_READ(reg);
  1962. if (IS_IVYBRIDGE(dev)) {
  1963. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  1964. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  1965. } else {
  1966. temp &= ~FDI_LINK_TRAIN_NONE;
  1967. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  1968. }
  1969. I915_WRITE(reg, temp);
  1970. reg = FDI_RX_CTL(pipe);
  1971. temp = I915_READ(reg);
  1972. if (HAS_PCH_CPT(dev)) {
  1973. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1974. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1975. } else {
  1976. temp &= ~FDI_LINK_TRAIN_NONE;
  1977. temp |= FDI_LINK_TRAIN_NONE;
  1978. }
  1979. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1980. /* wait one idle pattern time */
  1981. POSTING_READ(reg);
  1982. udelay(1000);
  1983. /* IVB wants error correction enabled */
  1984. if (IS_IVYBRIDGE(dev))
  1985. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  1986. FDI_FE_ERRC_ENABLE);
  1987. }
  1988. static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
  1989. {
  1990. struct drm_i915_private *dev_priv = dev->dev_private;
  1991. u32 flags = I915_READ(SOUTH_CHICKEN1);
  1992. flags |= FDI_PHASE_SYNC_OVR(pipe);
  1993. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
  1994. flags |= FDI_PHASE_SYNC_EN(pipe);
  1995. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
  1996. POSTING_READ(SOUTH_CHICKEN1);
  1997. }
  1998. /* The FDI link training functions for ILK/Ibexpeak. */
  1999. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2000. {
  2001. struct drm_device *dev = crtc->dev;
  2002. struct drm_i915_private *dev_priv = dev->dev_private;
  2003. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2004. int pipe = intel_crtc->pipe;
  2005. int plane = intel_crtc->plane;
  2006. u32 reg, temp, tries;
  2007. /* FDI needs bits from pipe & plane first */
  2008. assert_pipe_enabled(dev_priv, pipe);
  2009. assert_plane_enabled(dev_priv, plane);
  2010. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2011. for train result */
  2012. reg = FDI_RX_IMR(pipe);
  2013. temp = I915_READ(reg);
  2014. temp &= ~FDI_RX_SYMBOL_LOCK;
  2015. temp &= ~FDI_RX_BIT_LOCK;
  2016. I915_WRITE(reg, temp);
  2017. I915_READ(reg);
  2018. udelay(150);
  2019. /* enable CPU FDI TX and PCH FDI RX */
  2020. reg = FDI_TX_CTL(pipe);
  2021. temp = I915_READ(reg);
  2022. temp &= ~(7 << 19);
  2023. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2024. temp &= ~FDI_LINK_TRAIN_NONE;
  2025. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2026. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2027. reg = FDI_RX_CTL(pipe);
  2028. temp = I915_READ(reg);
  2029. temp &= ~FDI_LINK_TRAIN_NONE;
  2030. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2031. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2032. POSTING_READ(reg);
  2033. udelay(150);
  2034. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2035. if (HAS_PCH_IBX(dev)) {
  2036. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2037. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2038. FDI_RX_PHASE_SYNC_POINTER_EN);
  2039. }
  2040. reg = FDI_RX_IIR(pipe);
  2041. for (tries = 0; tries < 5; tries++) {
  2042. temp = I915_READ(reg);
  2043. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2044. if ((temp & FDI_RX_BIT_LOCK)) {
  2045. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2046. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2047. break;
  2048. }
  2049. }
  2050. if (tries == 5)
  2051. DRM_ERROR("FDI train 1 fail!\n");
  2052. /* Train 2 */
  2053. reg = FDI_TX_CTL(pipe);
  2054. temp = I915_READ(reg);
  2055. temp &= ~FDI_LINK_TRAIN_NONE;
  2056. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2057. I915_WRITE(reg, temp);
  2058. reg = FDI_RX_CTL(pipe);
  2059. temp = I915_READ(reg);
  2060. temp &= ~FDI_LINK_TRAIN_NONE;
  2061. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2062. I915_WRITE(reg, temp);
  2063. POSTING_READ(reg);
  2064. udelay(150);
  2065. reg = FDI_RX_IIR(pipe);
  2066. for (tries = 0; tries < 5; tries++) {
  2067. temp = I915_READ(reg);
  2068. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2069. if (temp & FDI_RX_SYMBOL_LOCK) {
  2070. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2071. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2072. break;
  2073. }
  2074. }
  2075. if (tries == 5)
  2076. DRM_ERROR("FDI train 2 fail!\n");
  2077. DRM_DEBUG_KMS("FDI train done\n");
  2078. }
  2079. static const int snb_b_fdi_train_param [] = {
  2080. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2081. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2082. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2083. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2084. };
  2085. /* The FDI link training functions for SNB/Cougarpoint. */
  2086. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2087. {
  2088. struct drm_device *dev = crtc->dev;
  2089. struct drm_i915_private *dev_priv = dev->dev_private;
  2090. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2091. int pipe = intel_crtc->pipe;
  2092. u32 reg, temp, i;
  2093. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2094. for train result */
  2095. reg = FDI_RX_IMR(pipe);
  2096. temp = I915_READ(reg);
  2097. temp &= ~FDI_RX_SYMBOL_LOCK;
  2098. temp &= ~FDI_RX_BIT_LOCK;
  2099. I915_WRITE(reg, temp);
  2100. POSTING_READ(reg);
  2101. udelay(150);
  2102. /* enable CPU FDI TX and PCH FDI RX */
  2103. reg = FDI_TX_CTL(pipe);
  2104. temp = I915_READ(reg);
  2105. temp &= ~(7 << 19);
  2106. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2107. temp &= ~FDI_LINK_TRAIN_NONE;
  2108. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2109. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2110. /* SNB-B */
  2111. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2112. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2113. reg = FDI_RX_CTL(pipe);
  2114. temp = I915_READ(reg);
  2115. if (HAS_PCH_CPT(dev)) {
  2116. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2117. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2118. } else {
  2119. temp &= ~FDI_LINK_TRAIN_NONE;
  2120. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2121. }
  2122. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2123. POSTING_READ(reg);
  2124. udelay(150);
  2125. if (HAS_PCH_CPT(dev))
  2126. cpt_phase_pointer_enable(dev, pipe);
  2127. for (i = 0; i < 4; i++ ) {
  2128. reg = FDI_TX_CTL(pipe);
  2129. temp = I915_READ(reg);
  2130. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2131. temp |= snb_b_fdi_train_param[i];
  2132. I915_WRITE(reg, temp);
  2133. POSTING_READ(reg);
  2134. udelay(500);
  2135. reg = FDI_RX_IIR(pipe);
  2136. temp = I915_READ(reg);
  2137. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2138. if (temp & FDI_RX_BIT_LOCK) {
  2139. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2140. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2141. break;
  2142. }
  2143. }
  2144. if (i == 4)
  2145. DRM_ERROR("FDI train 1 fail!\n");
  2146. /* Train 2 */
  2147. reg = FDI_TX_CTL(pipe);
  2148. temp = I915_READ(reg);
  2149. temp &= ~FDI_LINK_TRAIN_NONE;
  2150. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2151. if (IS_GEN6(dev)) {
  2152. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2153. /* SNB-B */
  2154. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2155. }
  2156. I915_WRITE(reg, temp);
  2157. reg = FDI_RX_CTL(pipe);
  2158. temp = I915_READ(reg);
  2159. if (HAS_PCH_CPT(dev)) {
  2160. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2161. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2162. } else {
  2163. temp &= ~FDI_LINK_TRAIN_NONE;
  2164. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2165. }
  2166. I915_WRITE(reg, temp);
  2167. POSTING_READ(reg);
  2168. udelay(150);
  2169. for (i = 0; i < 4; i++ ) {
  2170. reg = FDI_TX_CTL(pipe);
  2171. temp = I915_READ(reg);
  2172. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2173. temp |= snb_b_fdi_train_param[i];
  2174. I915_WRITE(reg, temp);
  2175. POSTING_READ(reg);
  2176. udelay(500);
  2177. reg = FDI_RX_IIR(pipe);
  2178. temp = I915_READ(reg);
  2179. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2180. if (temp & FDI_RX_SYMBOL_LOCK) {
  2181. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2182. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2183. break;
  2184. }
  2185. }
  2186. if (i == 4)
  2187. DRM_ERROR("FDI train 2 fail!\n");
  2188. DRM_DEBUG_KMS("FDI train done.\n");
  2189. }
  2190. /* Manual link training for Ivy Bridge A0 parts */
  2191. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2192. {
  2193. struct drm_device *dev = crtc->dev;
  2194. struct drm_i915_private *dev_priv = dev->dev_private;
  2195. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2196. int pipe = intel_crtc->pipe;
  2197. u32 reg, temp, i;
  2198. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2199. for train result */
  2200. reg = FDI_RX_IMR(pipe);
  2201. temp = I915_READ(reg);
  2202. temp &= ~FDI_RX_SYMBOL_LOCK;
  2203. temp &= ~FDI_RX_BIT_LOCK;
  2204. I915_WRITE(reg, temp);
  2205. POSTING_READ(reg);
  2206. udelay(150);
  2207. /* enable CPU FDI TX and PCH FDI RX */
  2208. reg = FDI_TX_CTL(pipe);
  2209. temp = I915_READ(reg);
  2210. temp &= ~(7 << 19);
  2211. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2212. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2213. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2214. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2215. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2216. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2217. reg = FDI_RX_CTL(pipe);
  2218. temp = I915_READ(reg);
  2219. temp &= ~FDI_LINK_TRAIN_AUTO;
  2220. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2221. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2222. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2223. POSTING_READ(reg);
  2224. udelay(150);
  2225. if (HAS_PCH_CPT(dev))
  2226. cpt_phase_pointer_enable(dev, pipe);
  2227. for (i = 0; i < 4; i++ ) {
  2228. reg = FDI_TX_CTL(pipe);
  2229. temp = I915_READ(reg);
  2230. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2231. temp |= snb_b_fdi_train_param[i];
  2232. I915_WRITE(reg, temp);
  2233. POSTING_READ(reg);
  2234. udelay(500);
  2235. reg = FDI_RX_IIR(pipe);
  2236. temp = I915_READ(reg);
  2237. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2238. if (temp & FDI_RX_BIT_LOCK ||
  2239. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2240. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2241. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2242. break;
  2243. }
  2244. }
  2245. if (i == 4)
  2246. DRM_ERROR("FDI train 1 fail!\n");
  2247. /* Train 2 */
  2248. reg = FDI_TX_CTL(pipe);
  2249. temp = I915_READ(reg);
  2250. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2251. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2252. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2253. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2254. I915_WRITE(reg, temp);
  2255. reg = FDI_RX_CTL(pipe);
  2256. temp = I915_READ(reg);
  2257. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2258. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2259. I915_WRITE(reg, temp);
  2260. POSTING_READ(reg);
  2261. udelay(150);
  2262. for (i = 0; i < 4; i++ ) {
  2263. reg = FDI_TX_CTL(pipe);
  2264. temp = I915_READ(reg);
  2265. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2266. temp |= snb_b_fdi_train_param[i];
  2267. I915_WRITE(reg, temp);
  2268. POSTING_READ(reg);
  2269. udelay(500);
  2270. reg = FDI_RX_IIR(pipe);
  2271. temp = I915_READ(reg);
  2272. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2273. if (temp & FDI_RX_SYMBOL_LOCK) {
  2274. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2275. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2276. break;
  2277. }
  2278. }
  2279. if (i == 4)
  2280. DRM_ERROR("FDI train 2 fail!\n");
  2281. DRM_DEBUG_KMS("FDI train done.\n");
  2282. }
  2283. static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
  2284. {
  2285. struct drm_device *dev = crtc->dev;
  2286. struct drm_i915_private *dev_priv = dev->dev_private;
  2287. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2288. int pipe = intel_crtc->pipe;
  2289. u32 reg, temp;
  2290. /* Write the TU size bits so error detection works */
  2291. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2292. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2293. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2294. reg = FDI_RX_CTL(pipe);
  2295. temp = I915_READ(reg);
  2296. temp &= ~((0x7 << 19) | (0x7 << 16));
  2297. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  2298. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2299. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2300. POSTING_READ(reg);
  2301. udelay(200);
  2302. /* Switch from Rawclk to PCDclk */
  2303. temp = I915_READ(reg);
  2304. I915_WRITE(reg, temp | FDI_PCDCLK);
  2305. POSTING_READ(reg);
  2306. udelay(200);
  2307. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2308. reg = FDI_TX_CTL(pipe);
  2309. temp = I915_READ(reg);
  2310. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2311. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2312. POSTING_READ(reg);
  2313. udelay(100);
  2314. }
  2315. }
  2316. static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
  2317. {
  2318. struct drm_i915_private *dev_priv = dev->dev_private;
  2319. u32 flags = I915_READ(SOUTH_CHICKEN1);
  2320. flags &= ~(FDI_PHASE_SYNC_EN(pipe));
  2321. I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
  2322. flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
  2323. I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
  2324. POSTING_READ(SOUTH_CHICKEN1);
  2325. }
  2326. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2327. {
  2328. struct drm_device *dev = crtc->dev;
  2329. struct drm_i915_private *dev_priv = dev->dev_private;
  2330. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2331. int pipe = intel_crtc->pipe;
  2332. u32 reg, temp;
  2333. /* disable CPU FDI tx and PCH FDI rx */
  2334. reg = FDI_TX_CTL(pipe);
  2335. temp = I915_READ(reg);
  2336. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2337. POSTING_READ(reg);
  2338. reg = FDI_RX_CTL(pipe);
  2339. temp = I915_READ(reg);
  2340. temp &= ~(0x7 << 16);
  2341. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2342. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2343. POSTING_READ(reg);
  2344. udelay(100);
  2345. /* Ironlake workaround, disable clock pointer after downing FDI */
  2346. if (HAS_PCH_IBX(dev)) {
  2347. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2348. I915_WRITE(FDI_RX_CHICKEN(pipe),
  2349. I915_READ(FDI_RX_CHICKEN(pipe) &
  2350. ~FDI_RX_PHASE_SYNC_POINTER_EN));
  2351. } else if (HAS_PCH_CPT(dev)) {
  2352. cpt_phase_pointer_disable(dev, pipe);
  2353. }
  2354. /* still set train pattern 1 */
  2355. reg = FDI_TX_CTL(pipe);
  2356. temp = I915_READ(reg);
  2357. temp &= ~FDI_LINK_TRAIN_NONE;
  2358. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2359. I915_WRITE(reg, temp);
  2360. reg = FDI_RX_CTL(pipe);
  2361. temp = I915_READ(reg);
  2362. if (HAS_PCH_CPT(dev)) {
  2363. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2364. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2365. } else {
  2366. temp &= ~FDI_LINK_TRAIN_NONE;
  2367. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2368. }
  2369. /* BPC in FDI rx is consistent with that in PIPECONF */
  2370. temp &= ~(0x07 << 16);
  2371. temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
  2372. I915_WRITE(reg, temp);
  2373. POSTING_READ(reg);
  2374. udelay(100);
  2375. }
  2376. /*
  2377. * When we disable a pipe, we need to clear any pending scanline wait events
  2378. * to avoid hanging the ring, which we assume we are waiting on.
  2379. */
  2380. static void intel_clear_scanline_wait(struct drm_device *dev)
  2381. {
  2382. struct drm_i915_private *dev_priv = dev->dev_private;
  2383. struct intel_ring_buffer *ring;
  2384. u32 tmp;
  2385. if (IS_GEN2(dev))
  2386. /* Can't break the hang on i8xx */
  2387. return;
  2388. ring = LP_RING(dev_priv);
  2389. tmp = I915_READ_CTL(ring);
  2390. if (tmp & RING_WAIT)
  2391. I915_WRITE_CTL(ring, tmp);
  2392. }
  2393. static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2394. {
  2395. struct drm_i915_gem_object *obj;
  2396. struct drm_i915_private *dev_priv;
  2397. if (crtc->fb == NULL)
  2398. return;
  2399. obj = to_intel_framebuffer(crtc->fb)->obj;
  2400. dev_priv = crtc->dev->dev_private;
  2401. wait_event(dev_priv->pending_flip_queue,
  2402. atomic_read(&obj->pending_flip) == 0);
  2403. }
  2404. static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
  2405. {
  2406. struct drm_device *dev = crtc->dev;
  2407. struct drm_mode_config *mode_config = &dev->mode_config;
  2408. struct intel_encoder *encoder;
  2409. /*
  2410. * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
  2411. * must be driven by its own crtc; no sharing is possible.
  2412. */
  2413. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  2414. if (encoder->base.crtc != crtc)
  2415. continue;
  2416. switch (encoder->type) {
  2417. case INTEL_OUTPUT_EDP:
  2418. if (!intel_encoder_is_pch_edp(&encoder->base))
  2419. return false;
  2420. continue;
  2421. }
  2422. }
  2423. return true;
  2424. }
  2425. /*
  2426. * Enable PCH resources required for PCH ports:
  2427. * - PCH PLLs
  2428. * - FDI training & RX/TX
  2429. * - update transcoder timings
  2430. * - DP transcoding bits
  2431. * - transcoder
  2432. */
  2433. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2434. {
  2435. struct drm_device *dev = crtc->dev;
  2436. struct drm_i915_private *dev_priv = dev->dev_private;
  2437. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2438. int pipe = intel_crtc->pipe;
  2439. u32 reg, temp;
  2440. /* For PCH output, training FDI link */
  2441. dev_priv->display.fdi_link_train(crtc);
  2442. intel_enable_pch_pll(dev_priv, pipe);
  2443. if (HAS_PCH_CPT(dev)) {
  2444. /* Be sure PCH DPLL SEL is set */
  2445. temp = I915_READ(PCH_DPLL_SEL);
  2446. if (pipe == 0 && (temp & TRANSA_DPLL_ENABLE) == 0)
  2447. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  2448. else if (pipe == 1 && (temp & TRANSB_DPLL_ENABLE) == 0)
  2449. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2450. I915_WRITE(PCH_DPLL_SEL, temp);
  2451. }
  2452. /* set transcoder timing, panel must allow it */
  2453. assert_panel_unlocked(dev_priv, pipe);
  2454. I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
  2455. I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
  2456. I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
  2457. I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
  2458. I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
  2459. I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
  2460. intel_fdi_normal_train(crtc);
  2461. /* For PCH DP, enable TRANS_DP_CTL */
  2462. if (HAS_PCH_CPT(dev) &&
  2463. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  2464. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
  2465. reg = TRANS_DP_CTL(pipe);
  2466. temp = I915_READ(reg);
  2467. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  2468. TRANS_DP_SYNC_MASK |
  2469. TRANS_DP_BPC_MASK);
  2470. temp |= (TRANS_DP_OUTPUT_ENABLE |
  2471. TRANS_DP_ENH_FRAMING);
  2472. temp |= bpc << 9; /* same format but at 11:9 */
  2473. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  2474. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  2475. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  2476. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  2477. switch (intel_trans_dp_port_sel(crtc)) {
  2478. case PCH_DP_B:
  2479. temp |= TRANS_DP_PORT_SEL_B;
  2480. break;
  2481. case PCH_DP_C:
  2482. temp |= TRANS_DP_PORT_SEL_C;
  2483. break;
  2484. case PCH_DP_D:
  2485. temp |= TRANS_DP_PORT_SEL_D;
  2486. break;
  2487. default:
  2488. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  2489. temp |= TRANS_DP_PORT_SEL_B;
  2490. break;
  2491. }
  2492. I915_WRITE(reg, temp);
  2493. }
  2494. intel_enable_transcoder(dev_priv, pipe);
  2495. }
  2496. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  2497. {
  2498. struct drm_device *dev = crtc->dev;
  2499. struct drm_i915_private *dev_priv = dev->dev_private;
  2500. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2501. int pipe = intel_crtc->pipe;
  2502. int plane = intel_crtc->plane;
  2503. u32 temp;
  2504. bool is_pch_port;
  2505. if (intel_crtc->active)
  2506. return;
  2507. intel_crtc->active = true;
  2508. intel_update_watermarks(dev);
  2509. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  2510. temp = I915_READ(PCH_LVDS);
  2511. if ((temp & LVDS_PORT_EN) == 0)
  2512. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  2513. }
  2514. is_pch_port = intel_crtc_driving_pch(crtc);
  2515. if (is_pch_port)
  2516. ironlake_fdi_pll_enable(crtc);
  2517. else
  2518. ironlake_fdi_disable(crtc);
  2519. /* Enable panel fitting for LVDS */
  2520. if (dev_priv->pch_pf_size &&
  2521. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
  2522. /* Force use of hard-coded filter coefficients
  2523. * as some pre-programmed values are broken,
  2524. * e.g. x201.
  2525. */
  2526. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  2527. I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
  2528. I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
  2529. }
  2530. /*
  2531. * On ILK+ LUT must be loaded before the pipe is running but with
  2532. * clocks enabled
  2533. */
  2534. intel_crtc_load_lut(crtc);
  2535. intel_enable_pipe(dev_priv, pipe, is_pch_port);
  2536. intel_enable_plane(dev_priv, plane, pipe);
  2537. if (is_pch_port)
  2538. ironlake_pch_enable(crtc);
  2539. mutex_lock(&dev->struct_mutex);
  2540. intel_update_fbc(dev);
  2541. mutex_unlock(&dev->struct_mutex);
  2542. intel_crtc_update_cursor(crtc, true);
  2543. }
  2544. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  2545. {
  2546. struct drm_device *dev = crtc->dev;
  2547. struct drm_i915_private *dev_priv = dev->dev_private;
  2548. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2549. int pipe = intel_crtc->pipe;
  2550. int plane = intel_crtc->plane;
  2551. u32 reg, temp;
  2552. if (!intel_crtc->active)
  2553. return;
  2554. intel_crtc_wait_for_pending_flips(crtc);
  2555. drm_vblank_off(dev, pipe);
  2556. intel_crtc_update_cursor(crtc, false);
  2557. intel_disable_plane(dev_priv, plane, pipe);
  2558. if (dev_priv->cfb_plane == plane)
  2559. intel_disable_fbc(dev);
  2560. intel_disable_pipe(dev_priv, pipe);
  2561. /* Disable PF */
  2562. I915_WRITE(PF_CTL(pipe), 0);
  2563. I915_WRITE(PF_WIN_SZ(pipe), 0);
  2564. ironlake_fdi_disable(crtc);
  2565. /* This is a horrible layering violation; we should be doing this in
  2566. * the connector/encoder ->prepare instead, but we don't always have
  2567. * enough information there about the config to know whether it will
  2568. * actually be necessary or just cause undesired flicker.
  2569. */
  2570. intel_disable_pch_ports(dev_priv, pipe);
  2571. intel_disable_transcoder(dev_priv, pipe);
  2572. if (HAS_PCH_CPT(dev)) {
  2573. /* disable TRANS_DP_CTL */
  2574. reg = TRANS_DP_CTL(pipe);
  2575. temp = I915_READ(reg);
  2576. temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  2577. temp |= TRANS_DP_PORT_SEL_NONE;
  2578. I915_WRITE(reg, temp);
  2579. /* disable DPLL_SEL */
  2580. temp = I915_READ(PCH_DPLL_SEL);
  2581. switch (pipe) {
  2582. case 0:
  2583. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  2584. break;
  2585. case 1:
  2586. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  2587. break;
  2588. case 2:
  2589. /* FIXME: manage transcoder PLLs? */
  2590. temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
  2591. break;
  2592. default:
  2593. BUG(); /* wtf */
  2594. }
  2595. I915_WRITE(PCH_DPLL_SEL, temp);
  2596. }
  2597. /* disable PCH DPLL */
  2598. intel_disable_pch_pll(dev_priv, pipe);
  2599. /* Switch from PCDclk to Rawclk */
  2600. reg = FDI_RX_CTL(pipe);
  2601. temp = I915_READ(reg);
  2602. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2603. /* Disable CPU FDI TX PLL */
  2604. reg = FDI_TX_CTL(pipe);
  2605. temp = I915_READ(reg);
  2606. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2607. POSTING_READ(reg);
  2608. udelay(100);
  2609. reg = FDI_RX_CTL(pipe);
  2610. temp = I915_READ(reg);
  2611. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2612. /* Wait for the clocks to turn off. */
  2613. POSTING_READ(reg);
  2614. udelay(100);
  2615. intel_crtc->active = false;
  2616. intel_update_watermarks(dev);
  2617. mutex_lock(&dev->struct_mutex);
  2618. intel_update_fbc(dev);
  2619. intel_clear_scanline_wait(dev);
  2620. mutex_unlock(&dev->struct_mutex);
  2621. }
  2622. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  2623. {
  2624. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2625. int pipe = intel_crtc->pipe;
  2626. int plane = intel_crtc->plane;
  2627. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2628. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2629. */
  2630. switch (mode) {
  2631. case DRM_MODE_DPMS_ON:
  2632. case DRM_MODE_DPMS_STANDBY:
  2633. case DRM_MODE_DPMS_SUSPEND:
  2634. DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
  2635. ironlake_crtc_enable(crtc);
  2636. break;
  2637. case DRM_MODE_DPMS_OFF:
  2638. DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
  2639. ironlake_crtc_disable(crtc);
  2640. break;
  2641. }
  2642. }
  2643. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  2644. {
  2645. if (!enable && intel_crtc->overlay) {
  2646. struct drm_device *dev = intel_crtc->base.dev;
  2647. struct drm_i915_private *dev_priv = dev->dev_private;
  2648. mutex_lock(&dev->struct_mutex);
  2649. dev_priv->mm.interruptible = false;
  2650. (void) intel_overlay_switch_off(intel_crtc->overlay);
  2651. dev_priv->mm.interruptible = true;
  2652. mutex_unlock(&dev->struct_mutex);
  2653. }
  2654. /* Let userspace switch the overlay on again. In most cases userspace
  2655. * has to recompute where to put it anyway.
  2656. */
  2657. }
  2658. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  2659. {
  2660. struct drm_device *dev = crtc->dev;
  2661. struct drm_i915_private *dev_priv = dev->dev_private;
  2662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2663. int pipe = intel_crtc->pipe;
  2664. int plane = intel_crtc->plane;
  2665. if (intel_crtc->active)
  2666. return;
  2667. intel_crtc->active = true;
  2668. intel_update_watermarks(dev);
  2669. intel_enable_pll(dev_priv, pipe);
  2670. intel_enable_pipe(dev_priv, pipe, false);
  2671. intel_enable_plane(dev_priv, plane, pipe);
  2672. intel_crtc_load_lut(crtc);
  2673. intel_update_fbc(dev);
  2674. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2675. intel_crtc_dpms_overlay(intel_crtc, true);
  2676. intel_crtc_update_cursor(crtc, true);
  2677. }
  2678. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  2679. {
  2680. struct drm_device *dev = crtc->dev;
  2681. struct drm_i915_private *dev_priv = dev->dev_private;
  2682. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2683. int pipe = intel_crtc->pipe;
  2684. int plane = intel_crtc->plane;
  2685. if (!intel_crtc->active)
  2686. return;
  2687. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2688. intel_crtc_wait_for_pending_flips(crtc);
  2689. drm_vblank_off(dev, pipe);
  2690. intel_crtc_dpms_overlay(intel_crtc, false);
  2691. intel_crtc_update_cursor(crtc, false);
  2692. if (dev_priv->cfb_plane == plane)
  2693. intel_disable_fbc(dev);
  2694. intel_disable_plane(dev_priv, plane, pipe);
  2695. intel_disable_pipe(dev_priv, pipe);
  2696. intel_disable_pll(dev_priv, pipe);
  2697. intel_crtc->active = false;
  2698. intel_update_fbc(dev);
  2699. intel_update_watermarks(dev);
  2700. intel_clear_scanline_wait(dev);
  2701. }
  2702. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2703. {
  2704. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2705. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2706. */
  2707. switch (mode) {
  2708. case DRM_MODE_DPMS_ON:
  2709. case DRM_MODE_DPMS_STANDBY:
  2710. case DRM_MODE_DPMS_SUSPEND:
  2711. i9xx_crtc_enable(crtc);
  2712. break;
  2713. case DRM_MODE_DPMS_OFF:
  2714. i9xx_crtc_disable(crtc);
  2715. break;
  2716. }
  2717. }
  2718. /**
  2719. * Sets the power management mode of the pipe and plane.
  2720. */
  2721. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2722. {
  2723. struct drm_device *dev = crtc->dev;
  2724. struct drm_i915_private *dev_priv = dev->dev_private;
  2725. struct drm_i915_master_private *master_priv;
  2726. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2727. int pipe = intel_crtc->pipe;
  2728. bool enabled;
  2729. if (intel_crtc->dpms_mode == mode)
  2730. return;
  2731. intel_crtc->dpms_mode = mode;
  2732. dev_priv->display.dpms(crtc, mode);
  2733. if (!dev->primary->master)
  2734. return;
  2735. master_priv = dev->primary->master->driver_priv;
  2736. if (!master_priv->sarea_priv)
  2737. return;
  2738. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2739. switch (pipe) {
  2740. case 0:
  2741. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2742. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2743. break;
  2744. case 1:
  2745. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2746. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2747. break;
  2748. default:
  2749. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  2750. break;
  2751. }
  2752. }
  2753. static void intel_crtc_disable(struct drm_crtc *crtc)
  2754. {
  2755. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2756. struct drm_device *dev = crtc->dev;
  2757. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2758. if (crtc->fb) {
  2759. mutex_lock(&dev->struct_mutex);
  2760. i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
  2761. mutex_unlock(&dev->struct_mutex);
  2762. }
  2763. }
  2764. /* Prepare for a mode set.
  2765. *
  2766. * Note we could be a lot smarter here. We need to figure out which outputs
  2767. * will be enabled, which disabled (in short, how the config will changes)
  2768. * and perform the minimum necessary steps to accomplish that, e.g. updating
  2769. * watermarks, FBC configuration, making sure PLLs are programmed correctly,
  2770. * panel fitting is in the proper state, etc.
  2771. */
  2772. static void i9xx_crtc_prepare(struct drm_crtc *crtc)
  2773. {
  2774. i9xx_crtc_disable(crtc);
  2775. }
  2776. static void i9xx_crtc_commit(struct drm_crtc *crtc)
  2777. {
  2778. i9xx_crtc_enable(crtc);
  2779. }
  2780. static void ironlake_crtc_prepare(struct drm_crtc *crtc)
  2781. {
  2782. ironlake_crtc_disable(crtc);
  2783. }
  2784. static void ironlake_crtc_commit(struct drm_crtc *crtc)
  2785. {
  2786. ironlake_crtc_enable(crtc);
  2787. }
  2788. void intel_encoder_prepare (struct drm_encoder *encoder)
  2789. {
  2790. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2791. /* lvds has its own version of prepare see intel_lvds_prepare */
  2792. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2793. }
  2794. void intel_encoder_commit (struct drm_encoder *encoder)
  2795. {
  2796. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2797. /* lvds has its own version of commit see intel_lvds_commit */
  2798. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2799. }
  2800. void intel_encoder_destroy(struct drm_encoder *encoder)
  2801. {
  2802. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  2803. drm_encoder_cleanup(encoder);
  2804. kfree(intel_encoder);
  2805. }
  2806. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2807. struct drm_display_mode *mode,
  2808. struct drm_display_mode *adjusted_mode)
  2809. {
  2810. struct drm_device *dev = crtc->dev;
  2811. if (HAS_PCH_SPLIT(dev)) {
  2812. /* FDI link clock is fixed at 2.7G */
  2813. if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
  2814. return false;
  2815. }
  2816. /* XXX some encoders set the crtcinfo, others don't.
  2817. * Obviously we need some form of conflict resolution here...
  2818. */
  2819. if (adjusted_mode->crtc_htotal == 0)
  2820. drm_mode_set_crtcinfo(adjusted_mode, 0);
  2821. return true;
  2822. }
  2823. static int i945_get_display_clock_speed(struct drm_device *dev)
  2824. {
  2825. return 400000;
  2826. }
  2827. static int i915_get_display_clock_speed(struct drm_device *dev)
  2828. {
  2829. return 333000;
  2830. }
  2831. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2832. {
  2833. return 200000;
  2834. }
  2835. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2836. {
  2837. u16 gcfgc = 0;
  2838. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2839. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2840. return 133000;
  2841. else {
  2842. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2843. case GC_DISPLAY_CLOCK_333_MHZ:
  2844. return 333000;
  2845. default:
  2846. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2847. return 190000;
  2848. }
  2849. }
  2850. }
  2851. static int i865_get_display_clock_speed(struct drm_device *dev)
  2852. {
  2853. return 266000;
  2854. }
  2855. static int i855_get_display_clock_speed(struct drm_device *dev)
  2856. {
  2857. u16 hpllcc = 0;
  2858. /* Assume that the hardware is in the high speed state. This
  2859. * should be the default.
  2860. */
  2861. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2862. case GC_CLOCK_133_200:
  2863. case GC_CLOCK_100_200:
  2864. return 200000;
  2865. case GC_CLOCK_166_250:
  2866. return 250000;
  2867. case GC_CLOCK_100_133:
  2868. return 133000;
  2869. }
  2870. /* Shouldn't happen */
  2871. return 0;
  2872. }
  2873. static int i830_get_display_clock_speed(struct drm_device *dev)
  2874. {
  2875. return 133000;
  2876. }
  2877. struct fdi_m_n {
  2878. u32 tu;
  2879. u32 gmch_m;
  2880. u32 gmch_n;
  2881. u32 link_m;
  2882. u32 link_n;
  2883. };
  2884. static void
  2885. fdi_reduce_ratio(u32 *num, u32 *den)
  2886. {
  2887. while (*num > 0xffffff || *den > 0xffffff) {
  2888. *num >>= 1;
  2889. *den >>= 1;
  2890. }
  2891. }
  2892. static void
  2893. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2894. int link_clock, struct fdi_m_n *m_n)
  2895. {
  2896. m_n->tu = 64; /* default size */
  2897. /* BUG_ON(pixel_clock > INT_MAX / 36); */
  2898. m_n->gmch_m = bits_per_pixel * pixel_clock;
  2899. m_n->gmch_n = link_clock * nlanes * 8;
  2900. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2901. m_n->link_m = pixel_clock;
  2902. m_n->link_n = link_clock;
  2903. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2904. }
  2905. struct intel_watermark_params {
  2906. unsigned long fifo_size;
  2907. unsigned long max_wm;
  2908. unsigned long default_wm;
  2909. unsigned long guard_size;
  2910. unsigned long cacheline_size;
  2911. };
  2912. /* Pineview has different values for various configs */
  2913. static const struct intel_watermark_params pineview_display_wm = {
  2914. PINEVIEW_DISPLAY_FIFO,
  2915. PINEVIEW_MAX_WM,
  2916. PINEVIEW_DFT_WM,
  2917. PINEVIEW_GUARD_WM,
  2918. PINEVIEW_FIFO_LINE_SIZE
  2919. };
  2920. static const struct intel_watermark_params pineview_display_hplloff_wm = {
  2921. PINEVIEW_DISPLAY_FIFO,
  2922. PINEVIEW_MAX_WM,
  2923. PINEVIEW_DFT_HPLLOFF_WM,
  2924. PINEVIEW_GUARD_WM,
  2925. PINEVIEW_FIFO_LINE_SIZE
  2926. };
  2927. static const struct intel_watermark_params pineview_cursor_wm = {
  2928. PINEVIEW_CURSOR_FIFO,
  2929. PINEVIEW_CURSOR_MAX_WM,
  2930. PINEVIEW_CURSOR_DFT_WM,
  2931. PINEVIEW_CURSOR_GUARD_WM,
  2932. PINEVIEW_FIFO_LINE_SIZE,
  2933. };
  2934. static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2935. PINEVIEW_CURSOR_FIFO,
  2936. PINEVIEW_CURSOR_MAX_WM,
  2937. PINEVIEW_CURSOR_DFT_WM,
  2938. PINEVIEW_CURSOR_GUARD_WM,
  2939. PINEVIEW_FIFO_LINE_SIZE
  2940. };
  2941. static const struct intel_watermark_params g4x_wm_info = {
  2942. G4X_FIFO_SIZE,
  2943. G4X_MAX_WM,
  2944. G4X_MAX_WM,
  2945. 2,
  2946. G4X_FIFO_LINE_SIZE,
  2947. };
  2948. static const struct intel_watermark_params g4x_cursor_wm_info = {
  2949. I965_CURSOR_FIFO,
  2950. I965_CURSOR_MAX_WM,
  2951. I965_CURSOR_DFT_WM,
  2952. 2,
  2953. G4X_FIFO_LINE_SIZE,
  2954. };
  2955. static const struct intel_watermark_params i965_cursor_wm_info = {
  2956. I965_CURSOR_FIFO,
  2957. I965_CURSOR_MAX_WM,
  2958. I965_CURSOR_DFT_WM,
  2959. 2,
  2960. I915_FIFO_LINE_SIZE,
  2961. };
  2962. static const struct intel_watermark_params i945_wm_info = {
  2963. I945_FIFO_SIZE,
  2964. I915_MAX_WM,
  2965. 1,
  2966. 2,
  2967. I915_FIFO_LINE_SIZE
  2968. };
  2969. static const struct intel_watermark_params i915_wm_info = {
  2970. I915_FIFO_SIZE,
  2971. I915_MAX_WM,
  2972. 1,
  2973. 2,
  2974. I915_FIFO_LINE_SIZE
  2975. };
  2976. static const struct intel_watermark_params i855_wm_info = {
  2977. I855GM_FIFO_SIZE,
  2978. I915_MAX_WM,
  2979. 1,
  2980. 2,
  2981. I830_FIFO_LINE_SIZE
  2982. };
  2983. static const struct intel_watermark_params i830_wm_info = {
  2984. I830_FIFO_SIZE,
  2985. I915_MAX_WM,
  2986. 1,
  2987. 2,
  2988. I830_FIFO_LINE_SIZE
  2989. };
  2990. static const struct intel_watermark_params ironlake_display_wm_info = {
  2991. ILK_DISPLAY_FIFO,
  2992. ILK_DISPLAY_MAXWM,
  2993. ILK_DISPLAY_DFTWM,
  2994. 2,
  2995. ILK_FIFO_LINE_SIZE
  2996. };
  2997. static const struct intel_watermark_params ironlake_cursor_wm_info = {
  2998. ILK_CURSOR_FIFO,
  2999. ILK_CURSOR_MAXWM,
  3000. ILK_CURSOR_DFTWM,
  3001. 2,
  3002. ILK_FIFO_LINE_SIZE
  3003. };
  3004. static const struct intel_watermark_params ironlake_display_srwm_info = {
  3005. ILK_DISPLAY_SR_FIFO,
  3006. ILK_DISPLAY_MAX_SRWM,
  3007. ILK_DISPLAY_DFT_SRWM,
  3008. 2,
  3009. ILK_FIFO_LINE_SIZE
  3010. };
  3011. static const struct intel_watermark_params ironlake_cursor_srwm_info = {
  3012. ILK_CURSOR_SR_FIFO,
  3013. ILK_CURSOR_MAX_SRWM,
  3014. ILK_CURSOR_DFT_SRWM,
  3015. 2,
  3016. ILK_FIFO_LINE_SIZE
  3017. };
  3018. static const struct intel_watermark_params sandybridge_display_wm_info = {
  3019. SNB_DISPLAY_FIFO,
  3020. SNB_DISPLAY_MAXWM,
  3021. SNB_DISPLAY_DFTWM,
  3022. 2,
  3023. SNB_FIFO_LINE_SIZE
  3024. };
  3025. static const struct intel_watermark_params sandybridge_cursor_wm_info = {
  3026. SNB_CURSOR_FIFO,
  3027. SNB_CURSOR_MAXWM,
  3028. SNB_CURSOR_DFTWM,
  3029. 2,
  3030. SNB_FIFO_LINE_SIZE
  3031. };
  3032. static const struct intel_watermark_params sandybridge_display_srwm_info = {
  3033. SNB_DISPLAY_SR_FIFO,
  3034. SNB_DISPLAY_MAX_SRWM,
  3035. SNB_DISPLAY_DFT_SRWM,
  3036. 2,
  3037. SNB_FIFO_LINE_SIZE
  3038. };
  3039. static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
  3040. SNB_CURSOR_SR_FIFO,
  3041. SNB_CURSOR_MAX_SRWM,
  3042. SNB_CURSOR_DFT_SRWM,
  3043. 2,
  3044. SNB_FIFO_LINE_SIZE
  3045. };
  3046. /**
  3047. * intel_calculate_wm - calculate watermark level
  3048. * @clock_in_khz: pixel clock
  3049. * @wm: chip FIFO params
  3050. * @pixel_size: display pixel size
  3051. * @latency_ns: memory latency for the platform
  3052. *
  3053. * Calculate the watermark level (the level at which the display plane will
  3054. * start fetching from memory again). Each chip has a different display
  3055. * FIFO size and allocation, so the caller needs to figure that out and pass
  3056. * in the correct intel_watermark_params structure.
  3057. *
  3058. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  3059. * on the pixel size. When it reaches the watermark level, it'll start
  3060. * fetching FIFO line sized based chunks from memory until the FIFO fills
  3061. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  3062. * will occur, and a display engine hang could result.
  3063. */
  3064. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  3065. const struct intel_watermark_params *wm,
  3066. int fifo_size,
  3067. int pixel_size,
  3068. unsigned long latency_ns)
  3069. {
  3070. long entries_required, wm_size;
  3071. /*
  3072. * Note: we need to make sure we don't overflow for various clock &
  3073. * latency values.
  3074. * clocks go from a few thousand to several hundred thousand.
  3075. * latency is usually a few thousand
  3076. */
  3077. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  3078. 1000;
  3079. entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
  3080. DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
  3081. wm_size = fifo_size - (entries_required + wm->guard_size);
  3082. DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
  3083. /* Don't promote wm_size to unsigned... */
  3084. if (wm_size > (long)wm->max_wm)
  3085. wm_size = wm->max_wm;
  3086. if (wm_size <= 0)
  3087. wm_size = wm->default_wm;
  3088. return wm_size;
  3089. }
  3090. struct cxsr_latency {
  3091. int is_desktop;
  3092. int is_ddr3;
  3093. unsigned long fsb_freq;
  3094. unsigned long mem_freq;
  3095. unsigned long display_sr;
  3096. unsigned long display_hpll_disable;
  3097. unsigned long cursor_sr;
  3098. unsigned long cursor_hpll_disable;
  3099. };
  3100. static const struct cxsr_latency cxsr_latency_table[] = {
  3101. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  3102. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  3103. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  3104. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  3105. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  3106. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  3107. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  3108. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  3109. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  3110. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  3111. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  3112. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  3113. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  3114. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  3115. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  3116. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  3117. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  3118. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  3119. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  3120. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  3121. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  3122. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  3123. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  3124. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  3125. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  3126. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  3127. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  3128. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  3129. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  3130. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  3131. };
  3132. static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
  3133. int is_ddr3,
  3134. int fsb,
  3135. int mem)
  3136. {
  3137. const struct cxsr_latency *latency;
  3138. int i;
  3139. if (fsb == 0 || mem == 0)
  3140. return NULL;
  3141. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  3142. latency = &cxsr_latency_table[i];
  3143. if (is_desktop == latency->is_desktop &&
  3144. is_ddr3 == latency->is_ddr3 &&
  3145. fsb == latency->fsb_freq && mem == latency->mem_freq)
  3146. return latency;
  3147. }
  3148. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3149. return NULL;
  3150. }
  3151. static void pineview_disable_cxsr(struct drm_device *dev)
  3152. {
  3153. struct drm_i915_private *dev_priv = dev->dev_private;
  3154. /* deactivate cxsr */
  3155. I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
  3156. }
  3157. /*
  3158. * Latency for FIFO fetches is dependent on several factors:
  3159. * - memory configuration (speed, channels)
  3160. * - chipset
  3161. * - current MCH state
  3162. * It can be fairly high in some situations, so here we assume a fairly
  3163. * pessimal value. It's a tradeoff between extra memory fetches (if we
  3164. * set this value too high, the FIFO will fetch frequently to stay full)
  3165. * and power consumption (set it too low to save power and we might see
  3166. * FIFO underruns and display "flicker").
  3167. *
  3168. * A value of 5us seems to be a good balance; safe for very low end
  3169. * platforms but not overly aggressive on lower latency configs.
  3170. */
  3171. static const int latency_ns = 5000;
  3172. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  3173. {
  3174. struct drm_i915_private *dev_priv = dev->dev_private;
  3175. uint32_t dsparb = I915_READ(DSPARB);
  3176. int size;
  3177. size = dsparb & 0x7f;
  3178. if (plane)
  3179. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
  3180. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3181. plane ? "B" : "A", size);
  3182. return size;
  3183. }
  3184. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  3185. {
  3186. struct drm_i915_private *dev_priv = dev->dev_private;
  3187. uint32_t dsparb = I915_READ(DSPARB);
  3188. int size;
  3189. size = dsparb & 0x1ff;
  3190. if (plane)
  3191. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
  3192. size >>= 1; /* Convert to cachelines */
  3193. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3194. plane ? "B" : "A", size);
  3195. return size;
  3196. }
  3197. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  3198. {
  3199. struct drm_i915_private *dev_priv = dev->dev_private;
  3200. uint32_t dsparb = I915_READ(DSPARB);
  3201. int size;
  3202. size = dsparb & 0x7f;
  3203. size >>= 2; /* Convert to cachelines */
  3204. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3205. plane ? "B" : "A",
  3206. size);
  3207. return size;
  3208. }
  3209. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  3210. {
  3211. struct drm_i915_private *dev_priv = dev->dev_private;
  3212. uint32_t dsparb = I915_READ(DSPARB);
  3213. int size;
  3214. size = dsparb & 0x7f;
  3215. size >>= 1; /* Convert to cachelines */
  3216. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  3217. plane ? "B" : "A", size);
  3218. return size;
  3219. }
  3220. static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
  3221. {
  3222. struct drm_crtc *crtc, *enabled = NULL;
  3223. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  3224. if (crtc->enabled && crtc->fb) {
  3225. if (enabled)
  3226. return NULL;
  3227. enabled = crtc;
  3228. }
  3229. }
  3230. return enabled;
  3231. }
  3232. static void pineview_update_wm(struct drm_device *dev)
  3233. {
  3234. struct drm_i915_private *dev_priv = dev->dev_private;
  3235. struct drm_crtc *crtc;
  3236. const struct cxsr_latency *latency;
  3237. u32 reg;
  3238. unsigned long wm;
  3239. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  3240. dev_priv->fsb_freq, dev_priv->mem_freq);
  3241. if (!latency) {
  3242. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  3243. pineview_disable_cxsr(dev);
  3244. return;
  3245. }
  3246. crtc = single_enabled_crtc(dev);
  3247. if (crtc) {
  3248. int clock = crtc->mode.clock;
  3249. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3250. /* Display SR */
  3251. wm = intel_calculate_wm(clock, &pineview_display_wm,
  3252. pineview_display_wm.fifo_size,
  3253. pixel_size, latency->display_sr);
  3254. reg = I915_READ(DSPFW1);
  3255. reg &= ~DSPFW_SR_MASK;
  3256. reg |= wm << DSPFW_SR_SHIFT;
  3257. I915_WRITE(DSPFW1, reg);
  3258. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  3259. /* cursor SR */
  3260. wm = intel_calculate_wm(clock, &pineview_cursor_wm,
  3261. pineview_display_wm.fifo_size,
  3262. pixel_size, latency->cursor_sr);
  3263. reg = I915_READ(DSPFW3);
  3264. reg &= ~DSPFW_CURSOR_SR_MASK;
  3265. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  3266. I915_WRITE(DSPFW3, reg);
  3267. /* Display HPLL off SR */
  3268. wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
  3269. pineview_display_hplloff_wm.fifo_size,
  3270. pixel_size, latency->display_hpll_disable);
  3271. reg = I915_READ(DSPFW3);
  3272. reg &= ~DSPFW_HPLL_SR_MASK;
  3273. reg |= wm & DSPFW_HPLL_SR_MASK;
  3274. I915_WRITE(DSPFW3, reg);
  3275. /* cursor HPLL off SR */
  3276. wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
  3277. pineview_display_hplloff_wm.fifo_size,
  3278. pixel_size, latency->cursor_hpll_disable);
  3279. reg = I915_READ(DSPFW3);
  3280. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  3281. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  3282. I915_WRITE(DSPFW3, reg);
  3283. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  3284. /* activate cxsr */
  3285. I915_WRITE(DSPFW3,
  3286. I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
  3287. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  3288. } else {
  3289. pineview_disable_cxsr(dev);
  3290. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  3291. }
  3292. }
  3293. static bool g4x_compute_wm0(struct drm_device *dev,
  3294. int plane,
  3295. const struct intel_watermark_params *display,
  3296. int display_latency_ns,
  3297. const struct intel_watermark_params *cursor,
  3298. int cursor_latency_ns,
  3299. int *plane_wm,
  3300. int *cursor_wm)
  3301. {
  3302. struct drm_crtc *crtc;
  3303. int htotal, hdisplay, clock, pixel_size;
  3304. int line_time_us, line_count;
  3305. int entries, tlb_miss;
  3306. crtc = intel_get_crtc_for_plane(dev, plane);
  3307. if (crtc->fb == NULL || !crtc->enabled) {
  3308. *cursor_wm = cursor->guard_size;
  3309. *plane_wm = display->guard_size;
  3310. return false;
  3311. }
  3312. htotal = crtc->mode.htotal;
  3313. hdisplay = crtc->mode.hdisplay;
  3314. clock = crtc->mode.clock;
  3315. pixel_size = crtc->fb->bits_per_pixel / 8;
  3316. /* Use the small buffer method to calculate plane watermark */
  3317. entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
  3318. tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
  3319. if (tlb_miss > 0)
  3320. entries += tlb_miss;
  3321. entries = DIV_ROUND_UP(entries, display->cacheline_size);
  3322. *plane_wm = entries + display->guard_size;
  3323. if (*plane_wm > (int)display->max_wm)
  3324. *plane_wm = display->max_wm;
  3325. /* Use the large buffer method to calculate cursor watermark */
  3326. line_time_us = ((htotal * 1000) / clock);
  3327. line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
  3328. entries = line_count * 64 * pixel_size;
  3329. tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
  3330. if (tlb_miss > 0)
  3331. entries += tlb_miss;
  3332. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3333. *cursor_wm = entries + cursor->guard_size;
  3334. if (*cursor_wm > (int)cursor->max_wm)
  3335. *cursor_wm = (int)cursor->max_wm;
  3336. return true;
  3337. }
  3338. /*
  3339. * Check the wm result.
  3340. *
  3341. * If any calculated watermark values is larger than the maximum value that
  3342. * can be programmed into the associated watermark register, that watermark
  3343. * must be disabled.
  3344. */
  3345. static bool g4x_check_srwm(struct drm_device *dev,
  3346. int display_wm, int cursor_wm,
  3347. const struct intel_watermark_params *display,
  3348. const struct intel_watermark_params *cursor)
  3349. {
  3350. DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
  3351. display_wm, cursor_wm);
  3352. if (display_wm > display->max_wm) {
  3353. DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
  3354. display_wm, display->max_wm);
  3355. return false;
  3356. }
  3357. if (cursor_wm > cursor->max_wm) {
  3358. DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
  3359. cursor_wm, cursor->max_wm);
  3360. return false;
  3361. }
  3362. if (!(display_wm || cursor_wm)) {
  3363. DRM_DEBUG_KMS("SR latency is 0, disabling\n");
  3364. return false;
  3365. }
  3366. return true;
  3367. }
  3368. static bool g4x_compute_srwm(struct drm_device *dev,
  3369. int plane,
  3370. int latency_ns,
  3371. const struct intel_watermark_params *display,
  3372. const struct intel_watermark_params *cursor,
  3373. int *display_wm, int *cursor_wm)
  3374. {
  3375. struct drm_crtc *crtc;
  3376. int hdisplay, htotal, pixel_size, clock;
  3377. unsigned long line_time_us;
  3378. int line_count, line_size;
  3379. int small, large;
  3380. int entries;
  3381. if (!latency_ns) {
  3382. *display_wm = *cursor_wm = 0;
  3383. return false;
  3384. }
  3385. crtc = intel_get_crtc_for_plane(dev, plane);
  3386. hdisplay = crtc->mode.hdisplay;
  3387. htotal = crtc->mode.htotal;
  3388. clock = crtc->mode.clock;
  3389. pixel_size = crtc->fb->bits_per_pixel / 8;
  3390. line_time_us = (htotal * 1000) / clock;
  3391. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3392. line_size = hdisplay * pixel_size;
  3393. /* Use the minimum of the small and large buffer method for primary */
  3394. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3395. large = line_count * line_size;
  3396. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3397. *display_wm = entries + display->guard_size;
  3398. /* calculate the self-refresh watermark for display cursor */
  3399. entries = line_count * pixel_size * 64;
  3400. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3401. *cursor_wm = entries + cursor->guard_size;
  3402. return g4x_check_srwm(dev,
  3403. *display_wm, *cursor_wm,
  3404. display, cursor);
  3405. }
  3406. #define single_plane_enabled(mask) is_power_of_2(mask)
  3407. static void g4x_update_wm(struct drm_device *dev)
  3408. {
  3409. static const int sr_latency_ns = 12000;
  3410. struct drm_i915_private *dev_priv = dev->dev_private;
  3411. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  3412. int plane_sr, cursor_sr;
  3413. unsigned int enabled = 0;
  3414. if (g4x_compute_wm0(dev, 0,
  3415. &g4x_wm_info, latency_ns,
  3416. &g4x_cursor_wm_info, latency_ns,
  3417. &planea_wm, &cursora_wm))
  3418. enabled |= 1;
  3419. if (g4x_compute_wm0(dev, 1,
  3420. &g4x_wm_info, latency_ns,
  3421. &g4x_cursor_wm_info, latency_ns,
  3422. &planeb_wm, &cursorb_wm))
  3423. enabled |= 2;
  3424. plane_sr = cursor_sr = 0;
  3425. if (single_plane_enabled(enabled) &&
  3426. g4x_compute_srwm(dev, ffs(enabled) - 1,
  3427. sr_latency_ns,
  3428. &g4x_wm_info,
  3429. &g4x_cursor_wm_info,
  3430. &plane_sr, &cursor_sr))
  3431. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3432. else
  3433. I915_WRITE(FW_BLC_SELF,
  3434. I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
  3435. DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
  3436. planea_wm, cursora_wm,
  3437. planeb_wm, cursorb_wm,
  3438. plane_sr, cursor_sr);
  3439. I915_WRITE(DSPFW1,
  3440. (plane_sr << DSPFW_SR_SHIFT) |
  3441. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  3442. (planeb_wm << DSPFW_PLANEB_SHIFT) |
  3443. planea_wm);
  3444. I915_WRITE(DSPFW2,
  3445. (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  3446. (cursora_wm << DSPFW_CURSORA_SHIFT));
  3447. /* HPLL off in SR has some issues on G4x... disable it */
  3448. I915_WRITE(DSPFW3,
  3449. (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  3450. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3451. }
  3452. static void i965_update_wm(struct drm_device *dev)
  3453. {
  3454. struct drm_i915_private *dev_priv = dev->dev_private;
  3455. struct drm_crtc *crtc;
  3456. int srwm = 1;
  3457. int cursor_sr = 16;
  3458. /* Calc sr entries for one plane configs */
  3459. crtc = single_enabled_crtc(dev);
  3460. if (crtc) {
  3461. /* self-refresh has much higher latency */
  3462. static const int sr_latency_ns = 12000;
  3463. int clock = crtc->mode.clock;
  3464. int htotal = crtc->mode.htotal;
  3465. int hdisplay = crtc->mode.hdisplay;
  3466. int pixel_size = crtc->fb->bits_per_pixel / 8;
  3467. unsigned long line_time_us;
  3468. int entries;
  3469. line_time_us = ((htotal * 1000) / clock);
  3470. /* Use ns/us then divide to preserve precision */
  3471. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3472. pixel_size * hdisplay;
  3473. entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
  3474. srwm = I965_FIFO_SIZE - entries;
  3475. if (srwm < 0)
  3476. srwm = 1;
  3477. srwm &= 0x1ff;
  3478. DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
  3479. entries, srwm);
  3480. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3481. pixel_size * 64;
  3482. entries = DIV_ROUND_UP(entries,
  3483. i965_cursor_wm_info.cacheline_size);
  3484. cursor_sr = i965_cursor_wm_info.fifo_size -
  3485. (entries + i965_cursor_wm_info.guard_size);
  3486. if (cursor_sr > i965_cursor_wm_info.max_wm)
  3487. cursor_sr = i965_cursor_wm_info.max_wm;
  3488. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  3489. "cursor %d\n", srwm, cursor_sr);
  3490. if (IS_CRESTLINE(dev))
  3491. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  3492. } else {
  3493. /* Turn off self refresh if both pipes are enabled */
  3494. if (IS_CRESTLINE(dev))
  3495. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  3496. & ~FW_BLC_SELF_EN);
  3497. }
  3498. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  3499. srwm);
  3500. /* 965 has limitations... */
  3501. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
  3502. (8 << 16) | (8 << 8) | (8 << 0));
  3503. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  3504. /* update cursor SR watermark */
  3505. I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  3506. }
  3507. static void i9xx_update_wm(struct drm_device *dev)
  3508. {
  3509. struct drm_i915_private *dev_priv = dev->dev_private;
  3510. const struct intel_watermark_params *wm_info;
  3511. uint32_t fwater_lo;
  3512. uint32_t fwater_hi;
  3513. int cwm, srwm = 1;
  3514. int fifo_size;
  3515. int planea_wm, planeb_wm;
  3516. struct drm_crtc *crtc, *enabled = NULL;
  3517. if (IS_I945GM(dev))
  3518. wm_info = &i945_wm_info;
  3519. else if (!IS_GEN2(dev))
  3520. wm_info = &i915_wm_info;
  3521. else
  3522. wm_info = &i855_wm_info;
  3523. fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  3524. crtc = intel_get_crtc_for_plane(dev, 0);
  3525. if (crtc->enabled && crtc->fb) {
  3526. planea_wm = intel_calculate_wm(crtc->mode.clock,
  3527. wm_info, fifo_size,
  3528. crtc->fb->bits_per_pixel / 8,
  3529. latency_ns);
  3530. enabled = crtc;
  3531. } else
  3532. planea_wm = fifo_size - wm_info->guard_size;
  3533. fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  3534. crtc = intel_get_crtc_for_plane(dev, 1);
  3535. if (crtc->enabled && crtc->fb) {
  3536. planeb_wm = intel_calculate_wm(crtc->mode.clock,
  3537. wm_info, fifo_size,
  3538. crtc->fb->bits_per_pixel / 8,
  3539. latency_ns);
  3540. if (enabled == NULL)
  3541. enabled = crtc;
  3542. else
  3543. enabled = NULL;
  3544. } else
  3545. planeb_wm = fifo_size - wm_info->guard_size;
  3546. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  3547. /*
  3548. * Overlay gets an aggressive default since video jitter is bad.
  3549. */
  3550. cwm = 2;
  3551. /* Play safe and disable self-refresh before adjusting watermarks. */
  3552. if (IS_I945G(dev) || IS_I945GM(dev))
  3553. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
  3554. else if (IS_I915GM(dev))
  3555. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  3556. /* Calc sr entries for one plane configs */
  3557. if (HAS_FW_BLC(dev) && enabled) {
  3558. /* self-refresh has much higher latency */
  3559. static const int sr_latency_ns = 6000;
  3560. int clock = enabled->mode.clock;
  3561. int htotal = enabled->mode.htotal;
  3562. int hdisplay = enabled->mode.hdisplay;
  3563. int pixel_size = enabled->fb->bits_per_pixel / 8;
  3564. unsigned long line_time_us;
  3565. int entries;
  3566. line_time_us = (htotal * 1000) / clock;
  3567. /* Use ns/us then divide to preserve precision */
  3568. entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
  3569. pixel_size * hdisplay;
  3570. entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
  3571. DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
  3572. srwm = wm_info->fifo_size - entries;
  3573. if (srwm < 0)
  3574. srwm = 1;
  3575. if (IS_I945G(dev) || IS_I945GM(dev))
  3576. I915_WRITE(FW_BLC_SELF,
  3577. FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  3578. else if (IS_I915GM(dev))
  3579. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  3580. }
  3581. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  3582. planea_wm, planeb_wm, cwm, srwm);
  3583. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  3584. fwater_hi = (cwm & 0x1f);
  3585. /* Set request length to 8 cachelines per fetch */
  3586. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  3587. fwater_hi = fwater_hi | (1 << 8);
  3588. I915_WRITE(FW_BLC, fwater_lo);
  3589. I915_WRITE(FW_BLC2, fwater_hi);
  3590. if (HAS_FW_BLC(dev)) {
  3591. if (enabled) {
  3592. if (IS_I945G(dev) || IS_I945GM(dev))
  3593. I915_WRITE(FW_BLC_SELF,
  3594. FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  3595. else if (IS_I915GM(dev))
  3596. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  3597. DRM_DEBUG_KMS("memory self refresh enabled\n");
  3598. } else
  3599. DRM_DEBUG_KMS("memory self refresh disabled\n");
  3600. }
  3601. }
  3602. static void i830_update_wm(struct drm_device *dev)
  3603. {
  3604. struct drm_i915_private *dev_priv = dev->dev_private;
  3605. struct drm_crtc *crtc;
  3606. uint32_t fwater_lo;
  3607. int planea_wm;
  3608. crtc = single_enabled_crtc(dev);
  3609. if (crtc == NULL)
  3610. return;
  3611. planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
  3612. dev_priv->display.get_fifo_size(dev, 0),
  3613. crtc->fb->bits_per_pixel / 8,
  3614. latency_ns);
  3615. fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  3616. fwater_lo |= (3<<8) | planea_wm;
  3617. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  3618. I915_WRITE(FW_BLC, fwater_lo);
  3619. }
  3620. #define ILK_LP0_PLANE_LATENCY 700
  3621. #define ILK_LP0_CURSOR_LATENCY 1300
  3622. /*
  3623. * Check the wm result.
  3624. *
  3625. * If any calculated watermark values is larger than the maximum value that
  3626. * can be programmed into the associated watermark register, that watermark
  3627. * must be disabled.
  3628. */
  3629. static bool ironlake_check_srwm(struct drm_device *dev, int level,
  3630. int fbc_wm, int display_wm, int cursor_wm,
  3631. const struct intel_watermark_params *display,
  3632. const struct intel_watermark_params *cursor)
  3633. {
  3634. struct drm_i915_private *dev_priv = dev->dev_private;
  3635. DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
  3636. " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
  3637. if (fbc_wm > SNB_FBC_MAX_SRWM) {
  3638. DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
  3639. fbc_wm, SNB_FBC_MAX_SRWM, level);
  3640. /* fbc has it's own way to disable FBC WM */
  3641. I915_WRITE(DISP_ARB_CTL,
  3642. I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
  3643. return false;
  3644. }
  3645. if (display_wm > display->max_wm) {
  3646. DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
  3647. display_wm, SNB_DISPLAY_MAX_SRWM, level);
  3648. return false;
  3649. }
  3650. if (cursor_wm > cursor->max_wm) {
  3651. DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
  3652. cursor_wm, SNB_CURSOR_MAX_SRWM, level);
  3653. return false;
  3654. }
  3655. if (!(fbc_wm || display_wm || cursor_wm)) {
  3656. DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
  3657. return false;
  3658. }
  3659. return true;
  3660. }
  3661. /*
  3662. * Compute watermark values of WM[1-3],
  3663. */
  3664. static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
  3665. int latency_ns,
  3666. const struct intel_watermark_params *display,
  3667. const struct intel_watermark_params *cursor,
  3668. int *fbc_wm, int *display_wm, int *cursor_wm)
  3669. {
  3670. struct drm_crtc *crtc;
  3671. unsigned long line_time_us;
  3672. int hdisplay, htotal, pixel_size, clock;
  3673. int line_count, line_size;
  3674. int small, large;
  3675. int entries;
  3676. if (!latency_ns) {
  3677. *fbc_wm = *display_wm = *cursor_wm = 0;
  3678. return false;
  3679. }
  3680. crtc = intel_get_crtc_for_plane(dev, plane);
  3681. hdisplay = crtc->mode.hdisplay;
  3682. htotal = crtc->mode.htotal;
  3683. clock = crtc->mode.clock;
  3684. pixel_size = crtc->fb->bits_per_pixel / 8;
  3685. line_time_us = (htotal * 1000) / clock;
  3686. line_count = (latency_ns / line_time_us + 1000) / 1000;
  3687. line_size = hdisplay * pixel_size;
  3688. /* Use the minimum of the small and large buffer method for primary */
  3689. small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
  3690. large = line_count * line_size;
  3691. entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
  3692. *display_wm = entries + display->guard_size;
  3693. /*
  3694. * Spec says:
  3695. * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
  3696. */
  3697. *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
  3698. /* calculate the self-refresh watermark for display cursor */
  3699. entries = line_count * pixel_size * 64;
  3700. entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
  3701. *cursor_wm = entries + cursor->guard_size;
  3702. return ironlake_check_srwm(dev, level,
  3703. *fbc_wm, *display_wm, *cursor_wm,
  3704. display, cursor);
  3705. }
  3706. static void ironlake_update_wm(struct drm_device *dev)
  3707. {
  3708. struct drm_i915_private *dev_priv = dev->dev_private;
  3709. int fbc_wm, plane_wm, cursor_wm;
  3710. unsigned int enabled;
  3711. enabled = 0;
  3712. if (g4x_compute_wm0(dev, 0,
  3713. &ironlake_display_wm_info,
  3714. ILK_LP0_PLANE_LATENCY,
  3715. &ironlake_cursor_wm_info,
  3716. ILK_LP0_CURSOR_LATENCY,
  3717. &plane_wm, &cursor_wm)) {
  3718. I915_WRITE(WM0_PIPEA_ILK,
  3719. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3720. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3721. " plane %d, " "cursor: %d\n",
  3722. plane_wm, cursor_wm);
  3723. enabled |= 1;
  3724. }
  3725. if (g4x_compute_wm0(dev, 1,
  3726. &ironlake_display_wm_info,
  3727. ILK_LP0_PLANE_LATENCY,
  3728. &ironlake_cursor_wm_info,
  3729. ILK_LP0_CURSOR_LATENCY,
  3730. &plane_wm, &cursor_wm)) {
  3731. I915_WRITE(WM0_PIPEB_ILK,
  3732. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3733. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3734. " plane %d, cursor: %d\n",
  3735. plane_wm, cursor_wm);
  3736. enabled |= 2;
  3737. }
  3738. /*
  3739. * Calculate and update the self-refresh watermark only when one
  3740. * display plane is used.
  3741. */
  3742. I915_WRITE(WM3_LP_ILK, 0);
  3743. I915_WRITE(WM2_LP_ILK, 0);
  3744. I915_WRITE(WM1_LP_ILK, 0);
  3745. if (!single_plane_enabled(enabled))
  3746. return;
  3747. enabled = ffs(enabled) - 1;
  3748. /* WM1 */
  3749. if (!ironlake_compute_srwm(dev, 1, enabled,
  3750. ILK_READ_WM1_LATENCY() * 500,
  3751. &ironlake_display_srwm_info,
  3752. &ironlake_cursor_srwm_info,
  3753. &fbc_wm, &plane_wm, &cursor_wm))
  3754. return;
  3755. I915_WRITE(WM1_LP_ILK,
  3756. WM1_LP_SR_EN |
  3757. (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3758. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3759. (plane_wm << WM1_LP_SR_SHIFT) |
  3760. cursor_wm);
  3761. /* WM2 */
  3762. if (!ironlake_compute_srwm(dev, 2, enabled,
  3763. ILK_READ_WM2_LATENCY() * 500,
  3764. &ironlake_display_srwm_info,
  3765. &ironlake_cursor_srwm_info,
  3766. &fbc_wm, &plane_wm, &cursor_wm))
  3767. return;
  3768. I915_WRITE(WM2_LP_ILK,
  3769. WM2_LP_EN |
  3770. (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3771. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3772. (plane_wm << WM1_LP_SR_SHIFT) |
  3773. cursor_wm);
  3774. /*
  3775. * WM3 is unsupported on ILK, probably because we don't have latency
  3776. * data for that power state
  3777. */
  3778. }
  3779. static void sandybridge_update_wm(struct drm_device *dev)
  3780. {
  3781. struct drm_i915_private *dev_priv = dev->dev_private;
  3782. int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
  3783. int fbc_wm, plane_wm, cursor_wm;
  3784. unsigned int enabled;
  3785. enabled = 0;
  3786. if (g4x_compute_wm0(dev, 0,
  3787. &sandybridge_display_wm_info, latency,
  3788. &sandybridge_cursor_wm_info, latency,
  3789. &plane_wm, &cursor_wm)) {
  3790. I915_WRITE(WM0_PIPEA_ILK,
  3791. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3792. DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
  3793. " plane %d, " "cursor: %d\n",
  3794. plane_wm, cursor_wm);
  3795. enabled |= 1;
  3796. }
  3797. if (g4x_compute_wm0(dev, 1,
  3798. &sandybridge_display_wm_info, latency,
  3799. &sandybridge_cursor_wm_info, latency,
  3800. &plane_wm, &cursor_wm)) {
  3801. I915_WRITE(WM0_PIPEB_ILK,
  3802. (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
  3803. DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
  3804. " plane %d, cursor: %d\n",
  3805. plane_wm, cursor_wm);
  3806. enabled |= 2;
  3807. }
  3808. /*
  3809. * Calculate and update the self-refresh watermark only when one
  3810. * display plane is used.
  3811. *
  3812. * SNB support 3 levels of watermark.
  3813. *
  3814. * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
  3815. * and disabled in the descending order
  3816. *
  3817. */
  3818. I915_WRITE(WM3_LP_ILK, 0);
  3819. I915_WRITE(WM2_LP_ILK, 0);
  3820. I915_WRITE(WM1_LP_ILK, 0);
  3821. if (!single_plane_enabled(enabled))
  3822. return;
  3823. enabled = ffs(enabled) - 1;
  3824. /* WM1 */
  3825. if (!ironlake_compute_srwm(dev, 1, enabled,
  3826. SNB_READ_WM1_LATENCY() * 500,
  3827. &sandybridge_display_srwm_info,
  3828. &sandybridge_cursor_srwm_info,
  3829. &fbc_wm, &plane_wm, &cursor_wm))
  3830. return;
  3831. I915_WRITE(WM1_LP_ILK,
  3832. WM1_LP_SR_EN |
  3833. (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3834. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3835. (plane_wm << WM1_LP_SR_SHIFT) |
  3836. cursor_wm);
  3837. /* WM2 */
  3838. if (!ironlake_compute_srwm(dev, 2, enabled,
  3839. SNB_READ_WM2_LATENCY() * 500,
  3840. &sandybridge_display_srwm_info,
  3841. &sandybridge_cursor_srwm_info,
  3842. &fbc_wm, &plane_wm, &cursor_wm))
  3843. return;
  3844. I915_WRITE(WM2_LP_ILK,
  3845. WM2_LP_EN |
  3846. (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3847. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3848. (plane_wm << WM1_LP_SR_SHIFT) |
  3849. cursor_wm);
  3850. /* WM3 */
  3851. if (!ironlake_compute_srwm(dev, 3, enabled,
  3852. SNB_READ_WM3_LATENCY() * 500,
  3853. &sandybridge_display_srwm_info,
  3854. &sandybridge_cursor_srwm_info,
  3855. &fbc_wm, &plane_wm, &cursor_wm))
  3856. return;
  3857. I915_WRITE(WM3_LP_ILK,
  3858. WM3_LP_EN |
  3859. (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
  3860. (fbc_wm << WM1_LP_FBC_SHIFT) |
  3861. (plane_wm << WM1_LP_SR_SHIFT) |
  3862. cursor_wm);
  3863. }
  3864. /**
  3865. * intel_update_watermarks - update FIFO watermark values based on current modes
  3866. *
  3867. * Calculate watermark values for the various WM regs based on current mode
  3868. * and plane configuration.
  3869. *
  3870. * There are several cases to deal with here:
  3871. * - normal (i.e. non-self-refresh)
  3872. * - self-refresh (SR) mode
  3873. * - lines are large relative to FIFO size (buffer can hold up to 2)
  3874. * - lines are small relative to FIFO size (buffer can hold more than 2
  3875. * lines), so need to account for TLB latency
  3876. *
  3877. * The normal calculation is:
  3878. * watermark = dotclock * bytes per pixel * latency
  3879. * where latency is platform & configuration dependent (we assume pessimal
  3880. * values here).
  3881. *
  3882. * The SR calculation is:
  3883. * watermark = (trunc(latency/line time)+1) * surface width *
  3884. * bytes per pixel
  3885. * where
  3886. * line time = htotal / dotclock
  3887. * surface width = hdisplay for normal plane and 64 for cursor
  3888. * and latency is assumed to be high, as above.
  3889. *
  3890. * The final value programmed to the register should always be rounded up,
  3891. * and include an extra 2 entries to account for clock crossings.
  3892. *
  3893. * We don't use the sprite, so we can ignore that. And on Crestline we have
  3894. * to set the non-SR watermarks to 8.
  3895. */
  3896. static void intel_update_watermarks(struct drm_device *dev)
  3897. {
  3898. struct drm_i915_private *dev_priv = dev->dev_private;
  3899. if (dev_priv->display.update_wm)
  3900. dev_priv->display.update_wm(dev);
  3901. }
  3902. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3903. {
  3904. return dev_priv->lvds_use_ssc && i915_panel_use_ssc
  3905. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  3906. }
  3907. /**
  3908. * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
  3909. * @crtc: CRTC structure
  3910. *
  3911. * A pipe may be connected to one or more outputs. Based on the depth of the
  3912. * attached framebuffer, choose a good color depth to use on the pipe.
  3913. *
  3914. * If possible, match the pipe depth to the fb depth. In some cases, this
  3915. * isn't ideal, because the connected output supports a lesser or restricted
  3916. * set of depths. Resolve that here:
  3917. * LVDS typically supports only 6bpc, so clamp down in that case
  3918. * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
  3919. * Displays may support a restricted set as well, check EDID and clamp as
  3920. * appropriate.
  3921. *
  3922. * RETURNS:
  3923. * Dithering requirement (i.e. false if display bpc and pipe bpc match,
  3924. * true if they don't match).
  3925. */
  3926. static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
  3927. unsigned int *pipe_bpp)
  3928. {
  3929. struct drm_device *dev = crtc->dev;
  3930. struct drm_i915_private *dev_priv = dev->dev_private;
  3931. struct drm_encoder *encoder;
  3932. struct drm_connector *connector;
  3933. unsigned int display_bpc = UINT_MAX, bpc;
  3934. /* Walk the encoders & connectors on this crtc, get min bpc */
  3935. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  3936. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  3937. if (encoder->crtc != crtc)
  3938. continue;
  3939. if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
  3940. unsigned int lvds_bpc;
  3941. if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
  3942. LVDS_A3_POWER_UP)
  3943. lvds_bpc = 8;
  3944. else
  3945. lvds_bpc = 6;
  3946. if (lvds_bpc < display_bpc) {
  3947. DRM_DEBUG_DRIVER("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
  3948. display_bpc = lvds_bpc;
  3949. }
  3950. continue;
  3951. }
  3952. if (intel_encoder->type == INTEL_OUTPUT_EDP) {
  3953. /* Use VBT settings if we have an eDP panel */
  3954. unsigned int edp_bpc = dev_priv->edp.bpp / 3;
  3955. if (edp_bpc < display_bpc) {
  3956. DRM_DEBUG_DRIVER("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
  3957. display_bpc = edp_bpc;
  3958. }
  3959. continue;
  3960. }
  3961. /* Not one of the known troublemakers, check the EDID */
  3962. list_for_each_entry(connector, &dev->mode_config.connector_list,
  3963. head) {
  3964. if (connector->encoder != encoder)
  3965. continue;
  3966. /* Don't use an invalid EDID bpc value */
  3967. if (connector->display_info.bpc &&
  3968. connector->display_info.bpc < display_bpc) {
  3969. DRM_DEBUG_DRIVER("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
  3970. display_bpc = connector->display_info.bpc;
  3971. }
  3972. }
  3973. /*
  3974. * HDMI is either 12 or 8, so if the display lets 10bpc sneak
  3975. * through, clamp it down. (Note: >12bpc will be caught below.)
  3976. */
  3977. if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
  3978. if (display_bpc > 8 && display_bpc < 12) {
  3979. DRM_DEBUG_DRIVER("forcing bpc to 12 for HDMI\n");
  3980. display_bpc = 12;
  3981. } else {
  3982. DRM_DEBUG_DRIVER("forcing bpc to 8 for HDMI\n");
  3983. display_bpc = 8;
  3984. }
  3985. }
  3986. }
  3987. /*
  3988. * We could just drive the pipe at the highest bpc all the time and
  3989. * enable dithering as needed, but that costs bandwidth. So choose
  3990. * the minimum value that expresses the full color range of the fb but
  3991. * also stays within the max display bpc discovered above.
  3992. */
  3993. switch (crtc->fb->depth) {
  3994. case 8:
  3995. bpc = 8; /* since we go through a colormap */
  3996. break;
  3997. case 15:
  3998. case 16:
  3999. bpc = 6; /* min is 18bpp */
  4000. break;
  4001. case 24:
  4002. bpc = min((unsigned int)8, display_bpc);
  4003. break;
  4004. case 30:
  4005. bpc = min((unsigned int)10, display_bpc);
  4006. break;
  4007. case 48:
  4008. bpc = min((unsigned int)12, display_bpc);
  4009. break;
  4010. default:
  4011. DRM_DEBUG("unsupported depth, assuming 24 bits\n");
  4012. bpc = min((unsigned int)8, display_bpc);
  4013. break;
  4014. }
  4015. DRM_DEBUG_DRIVER("setting pipe bpc to %d (max display bpc %d)\n",
  4016. bpc, display_bpc);
  4017. *pipe_bpp = bpc * 3;
  4018. return display_bpc != bpc;
  4019. }
  4020. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4021. struct drm_display_mode *mode,
  4022. struct drm_display_mode *adjusted_mode,
  4023. int x, int y,
  4024. struct drm_framebuffer *old_fb)
  4025. {
  4026. struct drm_device *dev = crtc->dev;
  4027. struct drm_i915_private *dev_priv = dev->dev_private;
  4028. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4029. int pipe = intel_crtc->pipe;
  4030. int plane = intel_crtc->plane;
  4031. int refclk, num_connectors = 0;
  4032. intel_clock_t clock, reduced_clock;
  4033. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  4034. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  4035. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  4036. struct drm_mode_config *mode_config = &dev->mode_config;
  4037. struct intel_encoder *encoder;
  4038. const intel_limit_t *limit;
  4039. int ret;
  4040. u32 temp;
  4041. u32 lvds_sync = 0;
  4042. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4043. if (encoder->base.crtc != crtc)
  4044. continue;
  4045. switch (encoder->type) {
  4046. case INTEL_OUTPUT_LVDS:
  4047. is_lvds = true;
  4048. break;
  4049. case INTEL_OUTPUT_SDVO:
  4050. case INTEL_OUTPUT_HDMI:
  4051. is_sdvo = true;
  4052. if (encoder->needs_tv_clock)
  4053. is_tv = true;
  4054. break;
  4055. case INTEL_OUTPUT_DVO:
  4056. is_dvo = true;
  4057. break;
  4058. case INTEL_OUTPUT_TVOUT:
  4059. is_tv = true;
  4060. break;
  4061. case INTEL_OUTPUT_ANALOG:
  4062. is_crt = true;
  4063. break;
  4064. case INTEL_OUTPUT_DISPLAYPORT:
  4065. is_dp = true;
  4066. break;
  4067. }
  4068. num_connectors++;
  4069. }
  4070. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4071. refclk = dev_priv->lvds_ssc_freq * 1000;
  4072. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4073. refclk / 1000);
  4074. } else if (!IS_GEN2(dev)) {
  4075. refclk = 96000;
  4076. } else {
  4077. refclk = 48000;
  4078. }
  4079. /*
  4080. * Returns a set of divisors for the desired target clock with the given
  4081. * refclk, or FALSE. The returned values represent the clock equation:
  4082. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4083. */
  4084. limit = intel_limit(crtc, refclk);
  4085. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  4086. if (!ok) {
  4087. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4088. return -EINVAL;
  4089. }
  4090. /* Ensure that the cursor is valid for the new mode before changing... */
  4091. intel_crtc_update_cursor(crtc, true);
  4092. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4093. has_reduced_clock = limit->find_pll(limit, crtc,
  4094. dev_priv->lvds_downclock,
  4095. refclk,
  4096. &reduced_clock);
  4097. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  4098. /*
  4099. * If the different P is found, it means that we can't
  4100. * switch the display clock by using the FP0/FP1.
  4101. * In such case we will disable the LVDS downclock
  4102. * feature.
  4103. */
  4104. DRM_DEBUG_KMS("Different P is found for "
  4105. "LVDS clock/downclock\n");
  4106. has_reduced_clock = 0;
  4107. }
  4108. }
  4109. /* SDVO TV has fixed PLL values depend on its clock range,
  4110. this mirrors vbios setting. */
  4111. if (is_sdvo && is_tv) {
  4112. if (adjusted_mode->clock >= 100000
  4113. && adjusted_mode->clock < 140500) {
  4114. clock.p1 = 2;
  4115. clock.p2 = 10;
  4116. clock.n = 3;
  4117. clock.m1 = 16;
  4118. clock.m2 = 8;
  4119. } else if (adjusted_mode->clock >= 140500
  4120. && adjusted_mode->clock <= 200000) {
  4121. clock.p1 = 1;
  4122. clock.p2 = 10;
  4123. clock.n = 6;
  4124. clock.m1 = 12;
  4125. clock.m2 = 8;
  4126. }
  4127. }
  4128. if (IS_PINEVIEW(dev)) {
  4129. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  4130. if (has_reduced_clock)
  4131. fp2 = (1 << reduced_clock.n) << 16 |
  4132. reduced_clock.m1 << 8 | reduced_clock.m2;
  4133. } else {
  4134. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4135. if (has_reduced_clock)
  4136. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4137. reduced_clock.m2;
  4138. }
  4139. dpll = DPLL_VGA_MODE_DIS;
  4140. if (!IS_GEN2(dev)) {
  4141. if (is_lvds)
  4142. dpll |= DPLLB_MODE_LVDS;
  4143. else
  4144. dpll |= DPLLB_MODE_DAC_SERIAL;
  4145. if (is_sdvo) {
  4146. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4147. if (pixel_multiplier > 1) {
  4148. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4149. dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  4150. }
  4151. dpll |= DPLL_DVO_HIGH_SPEED;
  4152. }
  4153. if (is_dp)
  4154. dpll |= DPLL_DVO_HIGH_SPEED;
  4155. /* compute bitmask from p1 value */
  4156. if (IS_PINEVIEW(dev))
  4157. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4158. else {
  4159. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4160. if (IS_G4X(dev) && has_reduced_clock)
  4161. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4162. }
  4163. switch (clock.p2) {
  4164. case 5:
  4165. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4166. break;
  4167. case 7:
  4168. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4169. break;
  4170. case 10:
  4171. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4172. break;
  4173. case 14:
  4174. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4175. break;
  4176. }
  4177. if (INTEL_INFO(dev)->gen >= 4)
  4178. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4179. } else {
  4180. if (is_lvds) {
  4181. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4182. } else {
  4183. if (clock.p1 == 2)
  4184. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4185. else
  4186. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4187. if (clock.p2 == 4)
  4188. dpll |= PLL_P2_DIVIDE_BY_4;
  4189. }
  4190. }
  4191. if (is_sdvo && is_tv)
  4192. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4193. else if (is_tv)
  4194. /* XXX: just matching BIOS for now */
  4195. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4196. dpll |= 3;
  4197. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4198. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4199. else
  4200. dpll |= PLL_REF_INPUT_DREFCLK;
  4201. /* setup pipeconf */
  4202. pipeconf = I915_READ(PIPECONF(pipe));
  4203. /* Set up the display plane register */
  4204. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4205. /* Ironlake's plane is forced to pipe, bit 24 is to
  4206. enable color space conversion */
  4207. if (pipe == 0)
  4208. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  4209. else
  4210. dspcntr |= DISPPLANE_SEL_PIPE_B;
  4211. if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  4212. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  4213. * core speed.
  4214. *
  4215. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  4216. * pipe == 0 check?
  4217. */
  4218. if (mode->clock >
  4219. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  4220. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4221. else
  4222. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  4223. }
  4224. dpll |= DPLL_VCO_ENABLE;
  4225. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  4226. drm_mode_debug_printmodeline(mode);
  4227. I915_WRITE(FP0(pipe), fp);
  4228. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  4229. POSTING_READ(DPLL(pipe));
  4230. udelay(150);
  4231. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4232. * This is an exception to the general rule that mode_set doesn't turn
  4233. * things on.
  4234. */
  4235. if (is_lvds) {
  4236. temp = I915_READ(LVDS);
  4237. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  4238. if (pipe == 1) {
  4239. temp |= LVDS_PIPEB_SELECT;
  4240. } else {
  4241. temp &= ~LVDS_PIPEB_SELECT;
  4242. }
  4243. /* set the corresponsding LVDS_BORDER bit */
  4244. temp |= dev_priv->lvds_border_bits;
  4245. /* Set the B0-B3 data pairs corresponding to whether we're going to
  4246. * set the DPLLs for dual-channel mode or not.
  4247. */
  4248. if (clock.p2 == 7)
  4249. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  4250. else
  4251. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  4252. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  4253. * appropriately here, but we need to look more thoroughly into how
  4254. * panels behave in the two modes.
  4255. */
  4256. /* set the dithering flag on LVDS as needed */
  4257. if (INTEL_INFO(dev)->gen >= 4) {
  4258. if (dev_priv->lvds_dither)
  4259. temp |= LVDS_ENABLE_DITHER;
  4260. else
  4261. temp &= ~LVDS_ENABLE_DITHER;
  4262. }
  4263. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  4264. lvds_sync |= LVDS_HSYNC_POLARITY;
  4265. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4266. lvds_sync |= LVDS_VSYNC_POLARITY;
  4267. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  4268. != lvds_sync) {
  4269. char flags[2] = "-+";
  4270. DRM_INFO("Changing LVDS panel from "
  4271. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  4272. flags[!(temp & LVDS_HSYNC_POLARITY)],
  4273. flags[!(temp & LVDS_VSYNC_POLARITY)],
  4274. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  4275. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  4276. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4277. temp |= lvds_sync;
  4278. }
  4279. I915_WRITE(LVDS, temp);
  4280. }
  4281. if (is_dp) {
  4282. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4283. }
  4284. I915_WRITE(DPLL(pipe), dpll);
  4285. /* Wait for the clocks to stabilize. */
  4286. POSTING_READ(DPLL(pipe));
  4287. udelay(150);
  4288. if (INTEL_INFO(dev)->gen >= 4) {
  4289. temp = 0;
  4290. if (is_sdvo) {
  4291. temp = intel_mode_get_pixel_multiplier(adjusted_mode);
  4292. if (temp > 1)
  4293. temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4294. else
  4295. temp = 0;
  4296. }
  4297. I915_WRITE(DPLL_MD(pipe), temp);
  4298. } else {
  4299. /* The pixel multiplier can only be updated once the
  4300. * DPLL is enabled and the clocks are stable.
  4301. *
  4302. * So write it again.
  4303. */
  4304. I915_WRITE(DPLL(pipe), dpll);
  4305. }
  4306. intel_crtc->lowfreq_avail = false;
  4307. if (is_lvds && has_reduced_clock && i915_powersave) {
  4308. I915_WRITE(FP1(pipe), fp2);
  4309. intel_crtc->lowfreq_avail = true;
  4310. if (HAS_PIPE_CXSR(dev)) {
  4311. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4312. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4313. }
  4314. } else {
  4315. I915_WRITE(FP1(pipe), fp);
  4316. if (HAS_PIPE_CXSR(dev)) {
  4317. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4318. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4319. }
  4320. }
  4321. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4322. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4323. /* the chip adds 2 halflines automatically */
  4324. adjusted_mode->crtc_vdisplay -= 1;
  4325. adjusted_mode->crtc_vtotal -= 1;
  4326. adjusted_mode->crtc_vblank_start -= 1;
  4327. adjusted_mode->crtc_vblank_end -= 1;
  4328. adjusted_mode->crtc_vsync_end -= 1;
  4329. adjusted_mode->crtc_vsync_start -= 1;
  4330. } else
  4331. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  4332. I915_WRITE(HTOTAL(pipe),
  4333. (adjusted_mode->crtc_hdisplay - 1) |
  4334. ((adjusted_mode->crtc_htotal - 1) << 16));
  4335. I915_WRITE(HBLANK(pipe),
  4336. (adjusted_mode->crtc_hblank_start - 1) |
  4337. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4338. I915_WRITE(HSYNC(pipe),
  4339. (adjusted_mode->crtc_hsync_start - 1) |
  4340. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4341. I915_WRITE(VTOTAL(pipe),
  4342. (adjusted_mode->crtc_vdisplay - 1) |
  4343. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4344. I915_WRITE(VBLANK(pipe),
  4345. (adjusted_mode->crtc_vblank_start - 1) |
  4346. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4347. I915_WRITE(VSYNC(pipe),
  4348. (adjusted_mode->crtc_vsync_start - 1) |
  4349. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4350. /* pipesrc and dspsize control the size that is scaled from,
  4351. * which should always be the user's requested size.
  4352. */
  4353. I915_WRITE(DSPSIZE(plane),
  4354. ((mode->vdisplay - 1) << 16) |
  4355. (mode->hdisplay - 1));
  4356. I915_WRITE(DSPPOS(plane), 0);
  4357. I915_WRITE(PIPESRC(pipe),
  4358. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4359. I915_WRITE(PIPECONF(pipe), pipeconf);
  4360. POSTING_READ(PIPECONF(pipe));
  4361. intel_enable_pipe(dev_priv, pipe, false);
  4362. intel_wait_for_vblank(dev, pipe);
  4363. I915_WRITE(DSPCNTR(plane), dspcntr);
  4364. POSTING_READ(DSPCNTR(plane));
  4365. intel_enable_plane(dev_priv, plane, pipe);
  4366. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4367. intel_update_watermarks(dev);
  4368. return ret;
  4369. }
  4370. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  4371. struct drm_display_mode *mode,
  4372. struct drm_display_mode *adjusted_mode,
  4373. int x, int y,
  4374. struct drm_framebuffer *old_fb)
  4375. {
  4376. struct drm_device *dev = crtc->dev;
  4377. struct drm_i915_private *dev_priv = dev->dev_private;
  4378. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4379. int pipe = intel_crtc->pipe;
  4380. int plane = intel_crtc->plane;
  4381. int refclk, num_connectors = 0;
  4382. intel_clock_t clock, reduced_clock;
  4383. u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
  4384. bool ok, has_reduced_clock = false, is_sdvo = false;
  4385. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  4386. struct intel_encoder *has_edp_encoder = NULL;
  4387. struct drm_mode_config *mode_config = &dev->mode_config;
  4388. struct intel_encoder *encoder;
  4389. const intel_limit_t *limit;
  4390. int ret;
  4391. struct fdi_m_n m_n = {0};
  4392. u32 temp;
  4393. u32 lvds_sync = 0;
  4394. int target_clock, pixel_multiplier, lane, link_bw, factor;
  4395. unsigned int pipe_bpp;
  4396. bool dither;
  4397. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4398. if (encoder->base.crtc != crtc)
  4399. continue;
  4400. switch (encoder->type) {
  4401. case INTEL_OUTPUT_LVDS:
  4402. is_lvds = true;
  4403. break;
  4404. case INTEL_OUTPUT_SDVO:
  4405. case INTEL_OUTPUT_HDMI:
  4406. is_sdvo = true;
  4407. if (encoder->needs_tv_clock)
  4408. is_tv = true;
  4409. break;
  4410. case INTEL_OUTPUT_TVOUT:
  4411. is_tv = true;
  4412. break;
  4413. case INTEL_OUTPUT_ANALOG:
  4414. is_crt = true;
  4415. break;
  4416. case INTEL_OUTPUT_DISPLAYPORT:
  4417. is_dp = true;
  4418. break;
  4419. case INTEL_OUTPUT_EDP:
  4420. has_edp_encoder = encoder;
  4421. break;
  4422. }
  4423. num_connectors++;
  4424. }
  4425. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4426. refclk = dev_priv->lvds_ssc_freq * 1000;
  4427. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4428. refclk / 1000);
  4429. } else {
  4430. refclk = 96000;
  4431. if (!has_edp_encoder ||
  4432. intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4433. refclk = 120000; /* 120Mhz refclk */
  4434. }
  4435. /*
  4436. * Returns a set of divisors for the desired target clock with the given
  4437. * refclk, or FALSE. The returned values represent the clock equation:
  4438. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4439. */
  4440. limit = intel_limit(crtc, refclk);
  4441. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  4442. if (!ok) {
  4443. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4444. return -EINVAL;
  4445. }
  4446. /* Ensure that the cursor is valid for the new mode before changing... */
  4447. intel_crtc_update_cursor(crtc, true);
  4448. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4449. has_reduced_clock = limit->find_pll(limit, crtc,
  4450. dev_priv->lvds_downclock,
  4451. refclk,
  4452. &reduced_clock);
  4453. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  4454. /*
  4455. * If the different P is found, it means that we can't
  4456. * switch the display clock by using the FP0/FP1.
  4457. * In such case we will disable the LVDS downclock
  4458. * feature.
  4459. */
  4460. DRM_DEBUG_KMS("Different P is found for "
  4461. "LVDS clock/downclock\n");
  4462. has_reduced_clock = 0;
  4463. }
  4464. }
  4465. /* SDVO TV has fixed PLL values depend on its clock range,
  4466. this mirrors vbios setting. */
  4467. if (is_sdvo && is_tv) {
  4468. if (adjusted_mode->clock >= 100000
  4469. && adjusted_mode->clock < 140500) {
  4470. clock.p1 = 2;
  4471. clock.p2 = 10;
  4472. clock.n = 3;
  4473. clock.m1 = 16;
  4474. clock.m2 = 8;
  4475. } else if (adjusted_mode->clock >= 140500
  4476. && adjusted_mode->clock <= 200000) {
  4477. clock.p1 = 1;
  4478. clock.p2 = 10;
  4479. clock.n = 6;
  4480. clock.m1 = 12;
  4481. clock.m2 = 8;
  4482. }
  4483. }
  4484. /* FDI link */
  4485. pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4486. lane = 0;
  4487. /* CPU eDP doesn't require FDI link, so just set DP M/N
  4488. according to current link config */
  4489. if (has_edp_encoder &&
  4490. !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4491. target_clock = mode->clock;
  4492. intel_edp_link_config(has_edp_encoder,
  4493. &lane, &link_bw);
  4494. } else {
  4495. /* [e]DP over FDI requires target mode clock
  4496. instead of link clock */
  4497. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4498. target_clock = mode->clock;
  4499. else
  4500. target_clock = adjusted_mode->clock;
  4501. /* FDI is a binary signal running at ~2.7GHz, encoding
  4502. * each output octet as 10 bits. The actual frequency
  4503. * is stored as a divider into a 100MHz clock, and the
  4504. * mode pixel clock is stored in units of 1KHz.
  4505. * Hence the bw of each lane in terms of the mode signal
  4506. * is:
  4507. */
  4508. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4509. }
  4510. /* determine panel color depth */
  4511. temp = I915_READ(PIPECONF(pipe));
  4512. temp &= ~PIPE_BPC_MASK;
  4513. dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp);
  4514. switch (pipe_bpp) {
  4515. case 18:
  4516. temp |= PIPE_6BPC;
  4517. break;
  4518. case 24:
  4519. temp |= PIPE_8BPC;
  4520. break;
  4521. case 30:
  4522. temp |= PIPE_10BPC;
  4523. break;
  4524. case 36:
  4525. temp |= PIPE_12BPC;
  4526. break;
  4527. default:
  4528. WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
  4529. pipe_bpp);
  4530. temp |= PIPE_8BPC;
  4531. pipe_bpp = 24;
  4532. break;
  4533. }
  4534. intel_crtc->bpp = pipe_bpp;
  4535. I915_WRITE(PIPECONF(pipe), temp);
  4536. if (!lane) {
  4537. /*
  4538. * Account for spread spectrum to avoid
  4539. * oversubscribing the link. Max center spread
  4540. * is 2.5%; use 5% for safety's sake.
  4541. */
  4542. u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
  4543. lane = bps / (link_bw * 8) + 1;
  4544. }
  4545. intel_crtc->fdi_lanes = lane;
  4546. if (pixel_multiplier > 1)
  4547. link_bw *= pixel_multiplier;
  4548. ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
  4549. &m_n);
  4550. /* Ironlake: try to setup display ref clock before DPLL
  4551. * enabling. This is only under driver's control after
  4552. * PCH B stepping, previous chipset stepping should be
  4553. * ignoring this setting.
  4554. */
  4555. temp = I915_READ(PCH_DREF_CONTROL);
  4556. /* Always enable nonspread source */
  4557. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  4558. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  4559. temp &= ~DREF_SSC_SOURCE_MASK;
  4560. temp |= DREF_SSC_SOURCE_ENABLE;
  4561. I915_WRITE(PCH_DREF_CONTROL, temp);
  4562. POSTING_READ(PCH_DREF_CONTROL);
  4563. udelay(200);
  4564. if (has_edp_encoder) {
  4565. if (intel_panel_use_ssc(dev_priv)) {
  4566. temp |= DREF_SSC1_ENABLE;
  4567. I915_WRITE(PCH_DREF_CONTROL, temp);
  4568. POSTING_READ(PCH_DREF_CONTROL);
  4569. udelay(200);
  4570. }
  4571. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4572. /* Enable CPU source on CPU attached eDP */
  4573. if (!intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4574. if (intel_panel_use_ssc(dev_priv))
  4575. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4576. else
  4577. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4578. } else {
  4579. /* Enable SSC on PCH eDP if needed */
  4580. if (intel_panel_use_ssc(dev_priv)) {
  4581. DRM_ERROR("enabling SSC on PCH\n");
  4582. temp |= DREF_SUPERSPREAD_SOURCE_ENABLE;
  4583. }
  4584. }
  4585. I915_WRITE(PCH_DREF_CONTROL, temp);
  4586. POSTING_READ(PCH_DREF_CONTROL);
  4587. udelay(200);
  4588. }
  4589. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4590. if (has_reduced_clock)
  4591. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4592. reduced_clock.m2;
  4593. /* Enable autotuning of the PLL clock (if permissible) */
  4594. factor = 21;
  4595. if (is_lvds) {
  4596. if ((intel_panel_use_ssc(dev_priv) &&
  4597. dev_priv->lvds_ssc_freq == 100) ||
  4598. (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
  4599. factor = 25;
  4600. } else if (is_sdvo && is_tv)
  4601. factor = 20;
  4602. if (clock.m < factor * clock.n)
  4603. fp |= FP_CB_TUNE;
  4604. dpll = 0;
  4605. if (is_lvds)
  4606. dpll |= DPLLB_MODE_LVDS;
  4607. else
  4608. dpll |= DPLLB_MODE_DAC_SERIAL;
  4609. if (is_sdvo) {
  4610. int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
  4611. if (pixel_multiplier > 1) {
  4612. dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  4613. }
  4614. dpll |= DPLL_DVO_HIGH_SPEED;
  4615. }
  4616. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
  4617. dpll |= DPLL_DVO_HIGH_SPEED;
  4618. /* compute bitmask from p1 value */
  4619. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4620. /* also FPA1 */
  4621. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4622. switch (clock.p2) {
  4623. case 5:
  4624. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4625. break;
  4626. case 7:
  4627. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4628. break;
  4629. case 10:
  4630. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4631. break;
  4632. case 14:
  4633. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4634. break;
  4635. }
  4636. if (is_sdvo && is_tv)
  4637. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4638. else if (is_tv)
  4639. /* XXX: just matching BIOS for now */
  4640. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4641. dpll |= 3;
  4642. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4643. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4644. else
  4645. dpll |= PLL_REF_INPUT_DREFCLK;
  4646. /* setup pipeconf */
  4647. pipeconf = I915_READ(PIPECONF(pipe));
  4648. /* Set up the display plane register */
  4649. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4650. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  4651. drm_mode_debug_printmodeline(mode);
  4652. /* PCH eDP needs FDI, but CPU eDP does not */
  4653. if (!has_edp_encoder || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4654. I915_WRITE(PCH_FP0(pipe), fp);
  4655. I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  4656. POSTING_READ(PCH_DPLL(pipe));
  4657. udelay(150);
  4658. }
  4659. /* enable transcoder DPLL */
  4660. if (HAS_PCH_CPT(dev)) {
  4661. temp = I915_READ(PCH_DPLL_SEL);
  4662. switch (pipe) {
  4663. case 0:
  4664. temp |= TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL;
  4665. break;
  4666. case 1:
  4667. temp |= TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL;
  4668. break;
  4669. case 2:
  4670. /* FIXME: manage transcoder PLLs? */
  4671. temp |= TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL;
  4672. break;
  4673. default:
  4674. BUG();
  4675. }
  4676. I915_WRITE(PCH_DPLL_SEL, temp);
  4677. POSTING_READ(PCH_DPLL_SEL);
  4678. udelay(150);
  4679. }
  4680. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  4681. * This is an exception to the general rule that mode_set doesn't turn
  4682. * things on.
  4683. */
  4684. if (is_lvds) {
  4685. temp = I915_READ(PCH_LVDS);
  4686. temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  4687. if (pipe == 1) {
  4688. if (HAS_PCH_CPT(dev))
  4689. temp |= PORT_TRANS_B_SEL_CPT;
  4690. else
  4691. temp |= LVDS_PIPEB_SELECT;
  4692. } else {
  4693. if (HAS_PCH_CPT(dev))
  4694. temp &= ~PORT_TRANS_SEL_MASK;
  4695. else
  4696. temp &= ~LVDS_PIPEB_SELECT;
  4697. }
  4698. /* set the corresponsding LVDS_BORDER bit */
  4699. temp |= dev_priv->lvds_border_bits;
  4700. /* Set the B0-B3 data pairs corresponding to whether we're going to
  4701. * set the DPLLs for dual-channel mode or not.
  4702. */
  4703. if (clock.p2 == 7)
  4704. temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  4705. else
  4706. temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  4707. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  4708. * appropriately here, but we need to look more thoroughly into how
  4709. * panels behave in the two modes.
  4710. */
  4711. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  4712. lvds_sync |= LVDS_HSYNC_POLARITY;
  4713. if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
  4714. lvds_sync |= LVDS_VSYNC_POLARITY;
  4715. if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
  4716. != lvds_sync) {
  4717. char flags[2] = "-+";
  4718. DRM_INFO("Changing LVDS panel from "
  4719. "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
  4720. flags[!(temp & LVDS_HSYNC_POLARITY)],
  4721. flags[!(temp & LVDS_VSYNC_POLARITY)],
  4722. flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
  4723. flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
  4724. temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
  4725. temp |= lvds_sync;
  4726. }
  4727. I915_WRITE(PCH_LVDS, temp);
  4728. }
  4729. pipeconf &= ~PIPECONF_DITHER_EN;
  4730. pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
  4731. if ((is_lvds && dev_priv->lvds_dither) || dither) {
  4732. pipeconf |= PIPECONF_DITHER_EN;
  4733. pipeconf |= PIPECONF_DITHER_TYPE_ST1;
  4734. }
  4735. if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4736. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  4737. } else {
  4738. /* For non-DP output, clear any trans DP clock recovery setting.*/
  4739. I915_WRITE(TRANSDATA_M1(pipe), 0);
  4740. I915_WRITE(TRANSDATA_N1(pipe), 0);
  4741. I915_WRITE(TRANSDPLINK_M1(pipe), 0);
  4742. I915_WRITE(TRANSDPLINK_N1(pipe), 0);
  4743. }
  4744. if (!has_edp_encoder ||
  4745. intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4746. I915_WRITE(PCH_DPLL(pipe), dpll);
  4747. /* Wait for the clocks to stabilize. */
  4748. POSTING_READ(PCH_DPLL(pipe));
  4749. udelay(150);
  4750. /* The pixel multiplier can only be updated once the
  4751. * DPLL is enabled and the clocks are stable.
  4752. *
  4753. * So write it again.
  4754. */
  4755. I915_WRITE(PCH_DPLL(pipe), dpll);
  4756. }
  4757. intel_crtc->lowfreq_avail = false;
  4758. if (is_lvds && has_reduced_clock && i915_powersave) {
  4759. I915_WRITE(PCH_FP1(pipe), fp2);
  4760. intel_crtc->lowfreq_avail = true;
  4761. if (HAS_PIPE_CXSR(dev)) {
  4762. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4763. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4764. }
  4765. } else {
  4766. I915_WRITE(PCH_FP1(pipe), fp);
  4767. if (HAS_PIPE_CXSR(dev)) {
  4768. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4769. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  4770. }
  4771. }
  4772. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4773. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4774. /* the chip adds 2 halflines automatically */
  4775. adjusted_mode->crtc_vdisplay -= 1;
  4776. adjusted_mode->crtc_vtotal -= 1;
  4777. adjusted_mode->crtc_vblank_start -= 1;
  4778. adjusted_mode->crtc_vblank_end -= 1;
  4779. adjusted_mode->crtc_vsync_end -= 1;
  4780. adjusted_mode->crtc_vsync_start -= 1;
  4781. } else
  4782. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  4783. I915_WRITE(HTOTAL(pipe),
  4784. (adjusted_mode->crtc_hdisplay - 1) |
  4785. ((adjusted_mode->crtc_htotal - 1) << 16));
  4786. I915_WRITE(HBLANK(pipe),
  4787. (adjusted_mode->crtc_hblank_start - 1) |
  4788. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4789. I915_WRITE(HSYNC(pipe),
  4790. (adjusted_mode->crtc_hsync_start - 1) |
  4791. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4792. I915_WRITE(VTOTAL(pipe),
  4793. (adjusted_mode->crtc_vdisplay - 1) |
  4794. ((adjusted_mode->crtc_vtotal - 1) << 16));
  4795. I915_WRITE(VBLANK(pipe),
  4796. (adjusted_mode->crtc_vblank_start - 1) |
  4797. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  4798. I915_WRITE(VSYNC(pipe),
  4799. (adjusted_mode->crtc_vsync_start - 1) |
  4800. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4801. /* pipesrc controls the size that is scaled from, which should
  4802. * always be the user's requested size.
  4803. */
  4804. I915_WRITE(PIPESRC(pipe),
  4805. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  4806. I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
  4807. I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
  4808. I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
  4809. I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
  4810. if (has_edp_encoder &&
  4811. !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
  4812. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  4813. }
  4814. I915_WRITE(PIPECONF(pipe), pipeconf);
  4815. POSTING_READ(PIPECONF(pipe));
  4816. intel_wait_for_vblank(dev, pipe);
  4817. if (IS_GEN5(dev)) {
  4818. /* enable address swizzle for tiling buffer */
  4819. temp = I915_READ(DISP_ARB_CTL);
  4820. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  4821. }
  4822. I915_WRITE(DSPCNTR(plane), dspcntr);
  4823. POSTING_READ(DSPCNTR(plane));
  4824. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  4825. intel_update_watermarks(dev);
  4826. return ret;
  4827. }
  4828. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  4829. struct drm_display_mode *mode,
  4830. struct drm_display_mode *adjusted_mode,
  4831. int x, int y,
  4832. struct drm_framebuffer *old_fb)
  4833. {
  4834. struct drm_device *dev = crtc->dev;
  4835. struct drm_i915_private *dev_priv = dev->dev_private;
  4836. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4837. int pipe = intel_crtc->pipe;
  4838. int ret;
  4839. drm_vblank_pre_modeset(dev, pipe);
  4840. ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
  4841. x, y, old_fb);
  4842. drm_vblank_post_modeset(dev, pipe);
  4843. intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
  4844. return ret;
  4845. }
  4846. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  4847. void intel_crtc_load_lut(struct drm_crtc *crtc)
  4848. {
  4849. struct drm_device *dev = crtc->dev;
  4850. struct drm_i915_private *dev_priv = dev->dev_private;
  4851. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4852. int palreg = PALETTE(intel_crtc->pipe);
  4853. int i;
  4854. /* The clocks have to be on to load the palette. */
  4855. if (!crtc->enabled)
  4856. return;
  4857. /* use legacy palette for Ironlake */
  4858. if (HAS_PCH_SPLIT(dev))
  4859. palreg = LGC_PALETTE(intel_crtc->pipe);
  4860. for (i = 0; i < 256; i++) {
  4861. I915_WRITE(palreg + 4 * i,
  4862. (intel_crtc->lut_r[i] << 16) |
  4863. (intel_crtc->lut_g[i] << 8) |
  4864. intel_crtc->lut_b[i]);
  4865. }
  4866. }
  4867. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  4868. {
  4869. struct drm_device *dev = crtc->dev;
  4870. struct drm_i915_private *dev_priv = dev->dev_private;
  4871. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4872. bool visible = base != 0;
  4873. u32 cntl;
  4874. if (intel_crtc->cursor_visible == visible)
  4875. return;
  4876. cntl = I915_READ(_CURACNTR);
  4877. if (visible) {
  4878. /* On these chipsets we can only modify the base whilst
  4879. * the cursor is disabled.
  4880. */
  4881. I915_WRITE(_CURABASE, base);
  4882. cntl &= ~(CURSOR_FORMAT_MASK);
  4883. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  4884. cntl |= CURSOR_ENABLE |
  4885. CURSOR_GAMMA_ENABLE |
  4886. CURSOR_FORMAT_ARGB;
  4887. } else
  4888. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  4889. I915_WRITE(_CURACNTR, cntl);
  4890. intel_crtc->cursor_visible = visible;
  4891. }
  4892. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  4893. {
  4894. struct drm_device *dev = crtc->dev;
  4895. struct drm_i915_private *dev_priv = dev->dev_private;
  4896. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4897. int pipe = intel_crtc->pipe;
  4898. bool visible = base != 0;
  4899. if (intel_crtc->cursor_visible != visible) {
  4900. uint32_t cntl = I915_READ(CURCNTR(pipe));
  4901. if (base) {
  4902. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  4903. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  4904. cntl |= pipe << 28; /* Connect to correct pipe */
  4905. } else {
  4906. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  4907. cntl |= CURSOR_MODE_DISABLE;
  4908. }
  4909. I915_WRITE(CURCNTR(pipe), cntl);
  4910. intel_crtc->cursor_visible = visible;
  4911. }
  4912. /* and commit changes on next vblank */
  4913. I915_WRITE(CURBASE(pipe), base);
  4914. }
  4915. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  4916. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  4917. bool on)
  4918. {
  4919. struct drm_device *dev = crtc->dev;
  4920. struct drm_i915_private *dev_priv = dev->dev_private;
  4921. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4922. int pipe = intel_crtc->pipe;
  4923. int x = intel_crtc->cursor_x;
  4924. int y = intel_crtc->cursor_y;
  4925. u32 base, pos;
  4926. bool visible;
  4927. pos = 0;
  4928. if (on && crtc->enabled && crtc->fb) {
  4929. base = intel_crtc->cursor_addr;
  4930. if (x > (int) crtc->fb->width)
  4931. base = 0;
  4932. if (y > (int) crtc->fb->height)
  4933. base = 0;
  4934. } else
  4935. base = 0;
  4936. if (x < 0) {
  4937. if (x + intel_crtc->cursor_width < 0)
  4938. base = 0;
  4939. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  4940. x = -x;
  4941. }
  4942. pos |= x << CURSOR_X_SHIFT;
  4943. if (y < 0) {
  4944. if (y + intel_crtc->cursor_height < 0)
  4945. base = 0;
  4946. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  4947. y = -y;
  4948. }
  4949. pos |= y << CURSOR_Y_SHIFT;
  4950. visible = base != 0;
  4951. if (!visible && !intel_crtc->cursor_visible)
  4952. return;
  4953. I915_WRITE(CURPOS(pipe), pos);
  4954. if (IS_845G(dev) || IS_I865G(dev))
  4955. i845_update_cursor(crtc, base);
  4956. else
  4957. i9xx_update_cursor(crtc, base);
  4958. if (visible)
  4959. intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
  4960. }
  4961. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  4962. struct drm_file *file,
  4963. uint32_t handle,
  4964. uint32_t width, uint32_t height)
  4965. {
  4966. struct drm_device *dev = crtc->dev;
  4967. struct drm_i915_private *dev_priv = dev->dev_private;
  4968. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4969. struct drm_i915_gem_object *obj;
  4970. uint32_t addr;
  4971. int ret;
  4972. DRM_DEBUG_KMS("\n");
  4973. /* if we want to turn off the cursor ignore width and height */
  4974. if (!handle) {
  4975. DRM_DEBUG_KMS("cursor off\n");
  4976. addr = 0;
  4977. obj = NULL;
  4978. mutex_lock(&dev->struct_mutex);
  4979. goto finish;
  4980. }
  4981. /* Currently we only support 64x64 cursors */
  4982. if (width != 64 || height != 64) {
  4983. DRM_ERROR("we currently only support 64x64 cursors\n");
  4984. return -EINVAL;
  4985. }
  4986. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  4987. if (&obj->base == NULL)
  4988. return -ENOENT;
  4989. if (obj->base.size < width * height * 4) {
  4990. DRM_ERROR("buffer is to small\n");
  4991. ret = -ENOMEM;
  4992. goto fail;
  4993. }
  4994. /* we only need to pin inside GTT if cursor is non-phy */
  4995. mutex_lock(&dev->struct_mutex);
  4996. if (!dev_priv->info->cursor_needs_physical) {
  4997. if (obj->tiling_mode) {
  4998. DRM_ERROR("cursor cannot be tiled\n");
  4999. ret = -EINVAL;
  5000. goto fail_locked;
  5001. }
  5002. ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
  5003. if (ret) {
  5004. DRM_ERROR("failed to move cursor bo into the GTT\n");
  5005. goto fail_locked;
  5006. }
  5007. ret = i915_gem_object_put_fence(obj);
  5008. if (ret) {
  5009. DRM_ERROR("failed to release fence for cursor");
  5010. goto fail_unpin;
  5011. }
  5012. addr = obj->gtt_offset;
  5013. } else {
  5014. int align = IS_I830(dev) ? 16 * 1024 : 256;
  5015. ret = i915_gem_attach_phys_object(dev, obj,
  5016. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  5017. align);
  5018. if (ret) {
  5019. DRM_ERROR("failed to attach phys object\n");
  5020. goto fail_locked;
  5021. }
  5022. addr = obj->phys_obj->handle->busaddr;
  5023. }
  5024. if (IS_GEN2(dev))
  5025. I915_WRITE(CURSIZE, (height << 12) | width);
  5026. finish:
  5027. if (intel_crtc->cursor_bo) {
  5028. if (dev_priv->info->cursor_needs_physical) {
  5029. if (intel_crtc->cursor_bo != obj)
  5030. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  5031. } else
  5032. i915_gem_object_unpin(intel_crtc->cursor_bo);
  5033. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  5034. }
  5035. mutex_unlock(&dev->struct_mutex);
  5036. intel_crtc->cursor_addr = addr;
  5037. intel_crtc->cursor_bo = obj;
  5038. intel_crtc->cursor_width = width;
  5039. intel_crtc->cursor_height = height;
  5040. intel_crtc_update_cursor(crtc, true);
  5041. return 0;
  5042. fail_unpin:
  5043. i915_gem_object_unpin(obj);
  5044. fail_locked:
  5045. mutex_unlock(&dev->struct_mutex);
  5046. fail:
  5047. drm_gem_object_unreference_unlocked(&obj->base);
  5048. return ret;
  5049. }
  5050. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  5051. {
  5052. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5053. intel_crtc->cursor_x = x;
  5054. intel_crtc->cursor_y = y;
  5055. intel_crtc_update_cursor(crtc, true);
  5056. return 0;
  5057. }
  5058. /** Sets the color ramps on behalf of RandR */
  5059. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  5060. u16 blue, int regno)
  5061. {
  5062. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5063. intel_crtc->lut_r[regno] = red >> 8;
  5064. intel_crtc->lut_g[regno] = green >> 8;
  5065. intel_crtc->lut_b[regno] = blue >> 8;
  5066. }
  5067. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  5068. u16 *blue, int regno)
  5069. {
  5070. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5071. *red = intel_crtc->lut_r[regno] << 8;
  5072. *green = intel_crtc->lut_g[regno] << 8;
  5073. *blue = intel_crtc->lut_b[regno] << 8;
  5074. }
  5075. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  5076. u16 *blue, uint32_t start, uint32_t size)
  5077. {
  5078. int end = (start + size > 256) ? 256 : start + size, i;
  5079. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5080. for (i = start; i < end; i++) {
  5081. intel_crtc->lut_r[i] = red[i] >> 8;
  5082. intel_crtc->lut_g[i] = green[i] >> 8;
  5083. intel_crtc->lut_b[i] = blue[i] >> 8;
  5084. }
  5085. intel_crtc_load_lut(crtc);
  5086. }
  5087. /**
  5088. * Get a pipe with a simple mode set on it for doing load-based monitor
  5089. * detection.
  5090. *
  5091. * It will be up to the load-detect code to adjust the pipe as appropriate for
  5092. * its requirements. The pipe will be connected to no other encoders.
  5093. *
  5094. * Currently this code will only succeed if there is a pipe with no encoders
  5095. * configured for it. In the future, it could choose to temporarily disable
  5096. * some outputs to free up a pipe for its use.
  5097. *
  5098. * \return crtc, or NULL if no pipes are available.
  5099. */
  5100. /* VESA 640x480x72Hz mode to set on the pipe */
  5101. static struct drm_display_mode load_detect_mode = {
  5102. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  5103. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  5104. };
  5105. static struct drm_framebuffer *
  5106. intel_framebuffer_create(struct drm_device *dev,
  5107. struct drm_mode_fb_cmd *mode_cmd,
  5108. struct drm_i915_gem_object *obj)
  5109. {
  5110. struct intel_framebuffer *intel_fb;
  5111. int ret;
  5112. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5113. if (!intel_fb) {
  5114. drm_gem_object_unreference_unlocked(&obj->base);
  5115. return ERR_PTR(-ENOMEM);
  5116. }
  5117. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  5118. if (ret) {
  5119. drm_gem_object_unreference_unlocked(&obj->base);
  5120. kfree(intel_fb);
  5121. return ERR_PTR(ret);
  5122. }
  5123. return &intel_fb->base;
  5124. }
  5125. static u32
  5126. intel_framebuffer_pitch_for_width(int width, int bpp)
  5127. {
  5128. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  5129. return ALIGN(pitch, 64);
  5130. }
  5131. static u32
  5132. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  5133. {
  5134. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  5135. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  5136. }
  5137. static struct drm_framebuffer *
  5138. intel_framebuffer_create_for_mode(struct drm_device *dev,
  5139. struct drm_display_mode *mode,
  5140. int depth, int bpp)
  5141. {
  5142. struct drm_i915_gem_object *obj;
  5143. struct drm_mode_fb_cmd mode_cmd;
  5144. obj = i915_gem_alloc_object(dev,
  5145. intel_framebuffer_size_for_mode(mode, bpp));
  5146. if (obj == NULL)
  5147. return ERR_PTR(-ENOMEM);
  5148. mode_cmd.width = mode->hdisplay;
  5149. mode_cmd.height = mode->vdisplay;
  5150. mode_cmd.depth = depth;
  5151. mode_cmd.bpp = bpp;
  5152. mode_cmd.pitch = intel_framebuffer_pitch_for_width(mode_cmd.width, bpp);
  5153. return intel_framebuffer_create(dev, &mode_cmd, obj);
  5154. }
  5155. static struct drm_framebuffer *
  5156. mode_fits_in_fbdev(struct drm_device *dev,
  5157. struct drm_display_mode *mode)
  5158. {
  5159. struct drm_i915_private *dev_priv = dev->dev_private;
  5160. struct drm_i915_gem_object *obj;
  5161. struct drm_framebuffer *fb;
  5162. if (dev_priv->fbdev == NULL)
  5163. return NULL;
  5164. obj = dev_priv->fbdev->ifb.obj;
  5165. if (obj == NULL)
  5166. return NULL;
  5167. fb = &dev_priv->fbdev->ifb.base;
  5168. if (fb->pitch < intel_framebuffer_pitch_for_width(mode->hdisplay,
  5169. fb->bits_per_pixel))
  5170. return NULL;
  5171. if (obj->base.size < mode->vdisplay * fb->pitch)
  5172. return NULL;
  5173. return fb;
  5174. }
  5175. bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  5176. struct drm_connector *connector,
  5177. struct drm_display_mode *mode,
  5178. struct intel_load_detect_pipe *old)
  5179. {
  5180. struct intel_crtc *intel_crtc;
  5181. struct drm_crtc *possible_crtc;
  5182. struct drm_encoder *encoder = &intel_encoder->base;
  5183. struct drm_crtc *crtc = NULL;
  5184. struct drm_device *dev = encoder->dev;
  5185. struct drm_framebuffer *old_fb;
  5186. int i = -1;
  5187. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5188. connector->base.id, drm_get_connector_name(connector),
  5189. encoder->base.id, drm_get_encoder_name(encoder));
  5190. /*
  5191. * Algorithm gets a little messy:
  5192. *
  5193. * - if the connector already has an assigned crtc, use it (but make
  5194. * sure it's on first)
  5195. *
  5196. * - try to find the first unused crtc that can drive this connector,
  5197. * and use that if we find one
  5198. */
  5199. /* See if we already have a CRTC for this connector */
  5200. if (encoder->crtc) {
  5201. crtc = encoder->crtc;
  5202. intel_crtc = to_intel_crtc(crtc);
  5203. old->dpms_mode = intel_crtc->dpms_mode;
  5204. old->load_detect_temp = false;
  5205. /* Make sure the crtc and connector are running */
  5206. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  5207. struct drm_encoder_helper_funcs *encoder_funcs;
  5208. struct drm_crtc_helper_funcs *crtc_funcs;
  5209. crtc_funcs = crtc->helper_private;
  5210. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  5211. encoder_funcs = encoder->helper_private;
  5212. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  5213. }
  5214. return true;
  5215. }
  5216. /* Find an unused one (if possible) */
  5217. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  5218. i++;
  5219. if (!(encoder->possible_crtcs & (1 << i)))
  5220. continue;
  5221. if (!possible_crtc->enabled) {
  5222. crtc = possible_crtc;
  5223. break;
  5224. }
  5225. }
  5226. /*
  5227. * If we didn't find an unused CRTC, don't use any.
  5228. */
  5229. if (!crtc) {
  5230. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  5231. return false;
  5232. }
  5233. encoder->crtc = crtc;
  5234. connector->encoder = encoder;
  5235. intel_crtc = to_intel_crtc(crtc);
  5236. old->dpms_mode = intel_crtc->dpms_mode;
  5237. old->load_detect_temp = true;
  5238. old->release_fb = NULL;
  5239. if (!mode)
  5240. mode = &load_detect_mode;
  5241. old_fb = crtc->fb;
  5242. /* We need a framebuffer large enough to accommodate all accesses
  5243. * that the plane may generate whilst we perform load detection.
  5244. * We can not rely on the fbcon either being present (we get called
  5245. * during its initialisation to detect all boot displays, or it may
  5246. * not even exist) or that it is large enough to satisfy the
  5247. * requested mode.
  5248. */
  5249. crtc->fb = mode_fits_in_fbdev(dev, mode);
  5250. if (crtc->fb == NULL) {
  5251. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  5252. crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  5253. old->release_fb = crtc->fb;
  5254. } else
  5255. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  5256. if (IS_ERR(crtc->fb)) {
  5257. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  5258. crtc->fb = old_fb;
  5259. return false;
  5260. }
  5261. if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
  5262. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  5263. if (old->release_fb)
  5264. old->release_fb->funcs->destroy(old->release_fb);
  5265. crtc->fb = old_fb;
  5266. return false;
  5267. }
  5268. /* let the connector get through one full cycle before testing */
  5269. intel_wait_for_vblank(dev, intel_crtc->pipe);
  5270. return true;
  5271. }
  5272. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  5273. struct drm_connector *connector,
  5274. struct intel_load_detect_pipe *old)
  5275. {
  5276. struct drm_encoder *encoder = &intel_encoder->base;
  5277. struct drm_device *dev = encoder->dev;
  5278. struct drm_crtc *crtc = encoder->crtc;
  5279. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  5280. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  5281. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5282. connector->base.id, drm_get_connector_name(connector),
  5283. encoder->base.id, drm_get_encoder_name(encoder));
  5284. if (old->load_detect_temp) {
  5285. connector->encoder = NULL;
  5286. drm_helper_disable_unused_functions(dev);
  5287. if (old->release_fb)
  5288. old->release_fb->funcs->destroy(old->release_fb);
  5289. return;
  5290. }
  5291. /* Switch crtc and encoder back off if necessary */
  5292. if (old->dpms_mode != DRM_MODE_DPMS_ON) {
  5293. encoder_funcs->dpms(encoder, old->dpms_mode);
  5294. crtc_funcs->dpms(crtc, old->dpms_mode);
  5295. }
  5296. }
  5297. /* Returns the clock of the currently programmed mode of the given pipe. */
  5298. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  5299. {
  5300. struct drm_i915_private *dev_priv = dev->dev_private;
  5301. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5302. int pipe = intel_crtc->pipe;
  5303. u32 dpll = I915_READ(DPLL(pipe));
  5304. u32 fp;
  5305. intel_clock_t clock;
  5306. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  5307. fp = I915_READ(FP0(pipe));
  5308. else
  5309. fp = I915_READ(FP1(pipe));
  5310. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  5311. if (IS_PINEVIEW(dev)) {
  5312. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  5313. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5314. } else {
  5315. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  5316. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5317. }
  5318. if (!IS_GEN2(dev)) {
  5319. if (IS_PINEVIEW(dev))
  5320. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  5321. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  5322. else
  5323. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  5324. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5325. switch (dpll & DPLL_MODE_MASK) {
  5326. case DPLLB_MODE_DAC_SERIAL:
  5327. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  5328. 5 : 10;
  5329. break;
  5330. case DPLLB_MODE_LVDS:
  5331. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  5332. 7 : 14;
  5333. break;
  5334. default:
  5335. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  5336. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  5337. return 0;
  5338. }
  5339. /* XXX: Handle the 100Mhz refclk */
  5340. intel_clock(dev, 96000, &clock);
  5341. } else {
  5342. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  5343. if (is_lvds) {
  5344. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  5345. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5346. clock.p2 = 14;
  5347. if ((dpll & PLL_REF_INPUT_MASK) ==
  5348. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  5349. /* XXX: might not be 66MHz */
  5350. intel_clock(dev, 66000, &clock);
  5351. } else
  5352. intel_clock(dev, 48000, &clock);
  5353. } else {
  5354. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  5355. clock.p1 = 2;
  5356. else {
  5357. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  5358. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  5359. }
  5360. if (dpll & PLL_P2_DIVIDE_BY_4)
  5361. clock.p2 = 4;
  5362. else
  5363. clock.p2 = 2;
  5364. intel_clock(dev, 48000, &clock);
  5365. }
  5366. }
  5367. /* XXX: It would be nice to validate the clocks, but we can't reuse
  5368. * i830PllIsValid() because it relies on the xf86_config connector
  5369. * configuration being accurate, which it isn't necessarily.
  5370. */
  5371. return clock.dot;
  5372. }
  5373. /** Returns the currently programmed mode of the given pipe. */
  5374. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  5375. struct drm_crtc *crtc)
  5376. {
  5377. struct drm_i915_private *dev_priv = dev->dev_private;
  5378. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5379. int pipe = intel_crtc->pipe;
  5380. struct drm_display_mode *mode;
  5381. int htot = I915_READ(HTOTAL(pipe));
  5382. int hsync = I915_READ(HSYNC(pipe));
  5383. int vtot = I915_READ(VTOTAL(pipe));
  5384. int vsync = I915_READ(VSYNC(pipe));
  5385. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  5386. if (!mode)
  5387. return NULL;
  5388. mode->clock = intel_crtc_clock_get(dev, crtc);
  5389. mode->hdisplay = (htot & 0xffff) + 1;
  5390. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  5391. mode->hsync_start = (hsync & 0xffff) + 1;
  5392. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  5393. mode->vdisplay = (vtot & 0xffff) + 1;
  5394. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  5395. mode->vsync_start = (vsync & 0xffff) + 1;
  5396. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  5397. drm_mode_set_name(mode);
  5398. drm_mode_set_crtcinfo(mode, 0);
  5399. return mode;
  5400. }
  5401. #define GPU_IDLE_TIMEOUT 500 /* ms */
  5402. /* When this timer fires, we've been idle for awhile */
  5403. static void intel_gpu_idle_timer(unsigned long arg)
  5404. {
  5405. struct drm_device *dev = (struct drm_device *)arg;
  5406. drm_i915_private_t *dev_priv = dev->dev_private;
  5407. if (!list_empty(&dev_priv->mm.active_list)) {
  5408. /* Still processing requests, so just re-arm the timer. */
  5409. mod_timer(&dev_priv->idle_timer, jiffies +
  5410. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  5411. return;
  5412. }
  5413. dev_priv->busy = false;
  5414. queue_work(dev_priv->wq, &dev_priv->idle_work);
  5415. }
  5416. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  5417. static void intel_crtc_idle_timer(unsigned long arg)
  5418. {
  5419. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  5420. struct drm_crtc *crtc = &intel_crtc->base;
  5421. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  5422. struct intel_framebuffer *intel_fb;
  5423. intel_fb = to_intel_framebuffer(crtc->fb);
  5424. if (intel_fb && intel_fb->obj->active) {
  5425. /* The framebuffer is still being accessed by the GPU. */
  5426. mod_timer(&intel_crtc->idle_timer, jiffies +
  5427. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5428. return;
  5429. }
  5430. intel_crtc->busy = false;
  5431. queue_work(dev_priv->wq, &dev_priv->idle_work);
  5432. }
  5433. static void intel_increase_pllclock(struct drm_crtc *crtc)
  5434. {
  5435. struct drm_device *dev = crtc->dev;
  5436. drm_i915_private_t *dev_priv = dev->dev_private;
  5437. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5438. int pipe = intel_crtc->pipe;
  5439. int dpll_reg = DPLL(pipe);
  5440. int dpll;
  5441. if (HAS_PCH_SPLIT(dev))
  5442. return;
  5443. if (!dev_priv->lvds_downclock_avail)
  5444. return;
  5445. dpll = I915_READ(dpll_reg);
  5446. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  5447. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  5448. /* Unlock panel regs */
  5449. I915_WRITE(PP_CONTROL,
  5450. I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
  5451. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  5452. I915_WRITE(dpll_reg, dpll);
  5453. intel_wait_for_vblank(dev, pipe);
  5454. dpll = I915_READ(dpll_reg);
  5455. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  5456. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  5457. /* ...and lock them again */
  5458. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  5459. }
  5460. /* Schedule downclock */
  5461. mod_timer(&intel_crtc->idle_timer, jiffies +
  5462. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5463. }
  5464. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  5465. {
  5466. struct drm_device *dev = crtc->dev;
  5467. drm_i915_private_t *dev_priv = dev->dev_private;
  5468. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5469. int pipe = intel_crtc->pipe;
  5470. int dpll_reg = DPLL(pipe);
  5471. int dpll = I915_READ(dpll_reg);
  5472. if (HAS_PCH_SPLIT(dev))
  5473. return;
  5474. if (!dev_priv->lvds_downclock_avail)
  5475. return;
  5476. /*
  5477. * Since this is called by a timer, we should never get here in
  5478. * the manual case.
  5479. */
  5480. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  5481. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  5482. /* Unlock panel regs */
  5483. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  5484. PANEL_UNLOCK_REGS);
  5485. dpll |= DISPLAY_RATE_SELECT_FPA1;
  5486. I915_WRITE(dpll_reg, dpll);
  5487. intel_wait_for_vblank(dev, pipe);
  5488. dpll = I915_READ(dpll_reg);
  5489. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  5490. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  5491. /* ...and lock them again */
  5492. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  5493. }
  5494. }
  5495. /**
  5496. * intel_idle_update - adjust clocks for idleness
  5497. * @work: work struct
  5498. *
  5499. * Either the GPU or display (or both) went idle. Check the busy status
  5500. * here and adjust the CRTC and GPU clocks as necessary.
  5501. */
  5502. static void intel_idle_update(struct work_struct *work)
  5503. {
  5504. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  5505. idle_work);
  5506. struct drm_device *dev = dev_priv->dev;
  5507. struct drm_crtc *crtc;
  5508. struct intel_crtc *intel_crtc;
  5509. if (!i915_powersave)
  5510. return;
  5511. mutex_lock(&dev->struct_mutex);
  5512. i915_update_gfx_val(dev_priv);
  5513. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5514. /* Skip inactive CRTCs */
  5515. if (!crtc->fb)
  5516. continue;
  5517. intel_crtc = to_intel_crtc(crtc);
  5518. if (!intel_crtc->busy)
  5519. intel_decrease_pllclock(crtc);
  5520. }
  5521. mutex_unlock(&dev->struct_mutex);
  5522. }
  5523. /**
  5524. * intel_mark_busy - mark the GPU and possibly the display busy
  5525. * @dev: drm device
  5526. * @obj: object we're operating on
  5527. *
  5528. * Callers can use this function to indicate that the GPU is busy processing
  5529. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  5530. * buffer), we'll also mark the display as busy, so we know to increase its
  5531. * clock frequency.
  5532. */
  5533. void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
  5534. {
  5535. drm_i915_private_t *dev_priv = dev->dev_private;
  5536. struct drm_crtc *crtc = NULL;
  5537. struct intel_framebuffer *intel_fb;
  5538. struct intel_crtc *intel_crtc;
  5539. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  5540. return;
  5541. if (!dev_priv->busy)
  5542. dev_priv->busy = true;
  5543. else
  5544. mod_timer(&dev_priv->idle_timer, jiffies +
  5545. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  5546. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5547. if (!crtc->fb)
  5548. continue;
  5549. intel_crtc = to_intel_crtc(crtc);
  5550. intel_fb = to_intel_framebuffer(crtc->fb);
  5551. if (intel_fb->obj == obj) {
  5552. if (!intel_crtc->busy) {
  5553. /* Non-busy -> busy, upclock */
  5554. intel_increase_pllclock(crtc);
  5555. intel_crtc->busy = true;
  5556. } else {
  5557. /* Busy -> busy, put off timer */
  5558. mod_timer(&intel_crtc->idle_timer, jiffies +
  5559. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  5560. }
  5561. }
  5562. }
  5563. }
  5564. static void intel_crtc_destroy(struct drm_crtc *crtc)
  5565. {
  5566. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5567. struct drm_device *dev = crtc->dev;
  5568. struct intel_unpin_work *work;
  5569. unsigned long flags;
  5570. spin_lock_irqsave(&dev->event_lock, flags);
  5571. work = intel_crtc->unpin_work;
  5572. intel_crtc->unpin_work = NULL;
  5573. spin_unlock_irqrestore(&dev->event_lock, flags);
  5574. if (work) {
  5575. cancel_work_sync(&work->work);
  5576. kfree(work);
  5577. }
  5578. drm_crtc_cleanup(crtc);
  5579. kfree(intel_crtc);
  5580. }
  5581. static void intel_unpin_work_fn(struct work_struct *__work)
  5582. {
  5583. struct intel_unpin_work *work =
  5584. container_of(__work, struct intel_unpin_work, work);
  5585. mutex_lock(&work->dev->struct_mutex);
  5586. i915_gem_object_unpin(work->old_fb_obj);
  5587. drm_gem_object_unreference(&work->pending_flip_obj->base);
  5588. drm_gem_object_unreference(&work->old_fb_obj->base);
  5589. intel_update_fbc(work->dev);
  5590. mutex_unlock(&work->dev->struct_mutex);
  5591. kfree(work);
  5592. }
  5593. static void do_intel_finish_page_flip(struct drm_device *dev,
  5594. struct drm_crtc *crtc)
  5595. {
  5596. drm_i915_private_t *dev_priv = dev->dev_private;
  5597. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5598. struct intel_unpin_work *work;
  5599. struct drm_i915_gem_object *obj;
  5600. struct drm_pending_vblank_event *e;
  5601. struct timeval tnow, tvbl;
  5602. unsigned long flags;
  5603. /* Ignore early vblank irqs */
  5604. if (intel_crtc == NULL)
  5605. return;
  5606. do_gettimeofday(&tnow);
  5607. spin_lock_irqsave(&dev->event_lock, flags);
  5608. work = intel_crtc->unpin_work;
  5609. if (work == NULL || !work->pending) {
  5610. spin_unlock_irqrestore(&dev->event_lock, flags);
  5611. return;
  5612. }
  5613. intel_crtc->unpin_work = NULL;
  5614. if (work->event) {
  5615. e = work->event;
  5616. e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
  5617. /* Called before vblank count and timestamps have
  5618. * been updated for the vblank interval of flip
  5619. * completion? Need to increment vblank count and
  5620. * add one videorefresh duration to returned timestamp
  5621. * to account for this. We assume this happened if we
  5622. * get called over 0.9 frame durations after the last
  5623. * timestamped vblank.
  5624. *
  5625. * This calculation can not be used with vrefresh rates
  5626. * below 5Hz (10Hz to be on the safe side) without
  5627. * promoting to 64 integers.
  5628. */
  5629. if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
  5630. 9 * crtc->framedur_ns) {
  5631. e->event.sequence++;
  5632. tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
  5633. crtc->framedur_ns);
  5634. }
  5635. e->event.tv_sec = tvbl.tv_sec;
  5636. e->event.tv_usec = tvbl.tv_usec;
  5637. list_add_tail(&e->base.link,
  5638. &e->base.file_priv->event_list);
  5639. wake_up_interruptible(&e->base.file_priv->event_wait);
  5640. }
  5641. drm_vblank_put(dev, intel_crtc->pipe);
  5642. spin_unlock_irqrestore(&dev->event_lock, flags);
  5643. obj = work->old_fb_obj;
  5644. atomic_clear_mask(1 << intel_crtc->plane,
  5645. &obj->pending_flip.counter);
  5646. if (atomic_read(&obj->pending_flip) == 0)
  5647. wake_up(&dev_priv->pending_flip_queue);
  5648. schedule_work(&work->work);
  5649. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  5650. }
  5651. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  5652. {
  5653. drm_i915_private_t *dev_priv = dev->dev_private;
  5654. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  5655. do_intel_finish_page_flip(dev, crtc);
  5656. }
  5657. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  5658. {
  5659. drm_i915_private_t *dev_priv = dev->dev_private;
  5660. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  5661. do_intel_finish_page_flip(dev, crtc);
  5662. }
  5663. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  5664. {
  5665. drm_i915_private_t *dev_priv = dev->dev_private;
  5666. struct intel_crtc *intel_crtc =
  5667. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  5668. unsigned long flags;
  5669. spin_lock_irqsave(&dev->event_lock, flags);
  5670. if (intel_crtc->unpin_work) {
  5671. if ((++intel_crtc->unpin_work->pending) > 1)
  5672. DRM_ERROR("Prepared flip multiple times\n");
  5673. } else {
  5674. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  5675. }
  5676. spin_unlock_irqrestore(&dev->event_lock, flags);
  5677. }
  5678. static int intel_gen2_queue_flip(struct drm_device *dev,
  5679. struct drm_crtc *crtc,
  5680. struct drm_framebuffer *fb,
  5681. struct drm_i915_gem_object *obj)
  5682. {
  5683. struct drm_i915_private *dev_priv = dev->dev_private;
  5684. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5685. unsigned long offset;
  5686. u32 flip_mask;
  5687. int ret;
  5688. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5689. if (ret)
  5690. goto out;
  5691. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  5692. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  5693. ret = BEGIN_LP_RING(6);
  5694. if (ret)
  5695. goto out;
  5696. /* Can't queue multiple flips, so wait for the previous
  5697. * one to finish before executing the next.
  5698. */
  5699. if (intel_crtc->plane)
  5700. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5701. else
  5702. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5703. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  5704. OUT_RING(MI_NOOP);
  5705. OUT_RING(MI_DISPLAY_FLIP |
  5706. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5707. OUT_RING(fb->pitch);
  5708. OUT_RING(obj->gtt_offset + offset);
  5709. OUT_RING(MI_NOOP);
  5710. ADVANCE_LP_RING();
  5711. out:
  5712. return ret;
  5713. }
  5714. static int intel_gen3_queue_flip(struct drm_device *dev,
  5715. struct drm_crtc *crtc,
  5716. struct drm_framebuffer *fb,
  5717. struct drm_i915_gem_object *obj)
  5718. {
  5719. struct drm_i915_private *dev_priv = dev->dev_private;
  5720. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5721. unsigned long offset;
  5722. u32 flip_mask;
  5723. int ret;
  5724. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5725. if (ret)
  5726. goto out;
  5727. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  5728. offset = crtc->y * fb->pitch + crtc->x * fb->bits_per_pixel/8;
  5729. ret = BEGIN_LP_RING(6);
  5730. if (ret)
  5731. goto out;
  5732. if (intel_crtc->plane)
  5733. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5734. else
  5735. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5736. OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
  5737. OUT_RING(MI_NOOP);
  5738. OUT_RING(MI_DISPLAY_FLIP_I915 |
  5739. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5740. OUT_RING(fb->pitch);
  5741. OUT_RING(obj->gtt_offset + offset);
  5742. OUT_RING(MI_NOOP);
  5743. ADVANCE_LP_RING();
  5744. out:
  5745. return ret;
  5746. }
  5747. static int intel_gen4_queue_flip(struct drm_device *dev,
  5748. struct drm_crtc *crtc,
  5749. struct drm_framebuffer *fb,
  5750. struct drm_i915_gem_object *obj)
  5751. {
  5752. struct drm_i915_private *dev_priv = dev->dev_private;
  5753. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5754. uint32_t pf, pipesrc;
  5755. int ret;
  5756. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5757. if (ret)
  5758. goto out;
  5759. ret = BEGIN_LP_RING(4);
  5760. if (ret)
  5761. goto out;
  5762. /* i965+ uses the linear or tiled offsets from the
  5763. * Display Registers (which do not change across a page-flip)
  5764. * so we need only reprogram the base address.
  5765. */
  5766. OUT_RING(MI_DISPLAY_FLIP |
  5767. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5768. OUT_RING(fb->pitch);
  5769. OUT_RING(obj->gtt_offset | obj->tiling_mode);
  5770. /* XXX Enabling the panel-fitter across page-flip is so far
  5771. * untested on non-native modes, so ignore it for now.
  5772. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  5773. */
  5774. pf = 0;
  5775. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5776. OUT_RING(pf | pipesrc);
  5777. ADVANCE_LP_RING();
  5778. out:
  5779. return ret;
  5780. }
  5781. static int intel_gen6_queue_flip(struct drm_device *dev,
  5782. struct drm_crtc *crtc,
  5783. struct drm_framebuffer *fb,
  5784. struct drm_i915_gem_object *obj)
  5785. {
  5786. struct drm_i915_private *dev_priv = dev->dev_private;
  5787. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5788. uint32_t pf, pipesrc;
  5789. int ret;
  5790. ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
  5791. if (ret)
  5792. goto out;
  5793. ret = BEGIN_LP_RING(4);
  5794. if (ret)
  5795. goto out;
  5796. OUT_RING(MI_DISPLAY_FLIP |
  5797. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5798. OUT_RING(fb->pitch | obj->tiling_mode);
  5799. OUT_RING(obj->gtt_offset);
  5800. pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  5801. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  5802. OUT_RING(pf | pipesrc);
  5803. ADVANCE_LP_RING();
  5804. out:
  5805. return ret;
  5806. }
  5807. /*
  5808. * On gen7 we currently use the blit ring because (in early silicon at least)
  5809. * the render ring doesn't give us interrpts for page flip completion, which
  5810. * means clients will hang after the first flip is queued. Fortunately the
  5811. * blit ring generates interrupts properly, so use it instead.
  5812. */
  5813. static int intel_gen7_queue_flip(struct drm_device *dev,
  5814. struct drm_crtc *crtc,
  5815. struct drm_framebuffer *fb,
  5816. struct drm_i915_gem_object *obj)
  5817. {
  5818. struct drm_i915_private *dev_priv = dev->dev_private;
  5819. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5820. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  5821. int ret;
  5822. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5823. if (ret)
  5824. goto out;
  5825. ret = intel_ring_begin(ring, 4);
  5826. if (ret)
  5827. goto out;
  5828. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
  5829. intel_ring_emit(ring, (fb->pitch | obj->tiling_mode));
  5830. intel_ring_emit(ring, (obj->gtt_offset));
  5831. intel_ring_emit(ring, (MI_NOOP));
  5832. intel_ring_advance(ring);
  5833. out:
  5834. return ret;
  5835. }
  5836. static int intel_default_queue_flip(struct drm_device *dev,
  5837. struct drm_crtc *crtc,
  5838. struct drm_framebuffer *fb,
  5839. struct drm_i915_gem_object *obj)
  5840. {
  5841. return -ENODEV;
  5842. }
  5843. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  5844. struct drm_framebuffer *fb,
  5845. struct drm_pending_vblank_event *event)
  5846. {
  5847. struct drm_device *dev = crtc->dev;
  5848. struct drm_i915_private *dev_priv = dev->dev_private;
  5849. struct intel_framebuffer *intel_fb;
  5850. struct drm_i915_gem_object *obj;
  5851. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5852. struct intel_unpin_work *work;
  5853. unsigned long flags;
  5854. int ret;
  5855. work = kzalloc(sizeof *work, GFP_KERNEL);
  5856. if (work == NULL)
  5857. return -ENOMEM;
  5858. work->event = event;
  5859. work->dev = crtc->dev;
  5860. intel_fb = to_intel_framebuffer(crtc->fb);
  5861. work->old_fb_obj = intel_fb->obj;
  5862. INIT_WORK(&work->work, intel_unpin_work_fn);
  5863. /* We borrow the event spin lock for protecting unpin_work */
  5864. spin_lock_irqsave(&dev->event_lock, flags);
  5865. if (intel_crtc->unpin_work) {
  5866. spin_unlock_irqrestore(&dev->event_lock, flags);
  5867. kfree(work);
  5868. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  5869. return -EBUSY;
  5870. }
  5871. intel_crtc->unpin_work = work;
  5872. spin_unlock_irqrestore(&dev->event_lock, flags);
  5873. intel_fb = to_intel_framebuffer(fb);
  5874. obj = intel_fb->obj;
  5875. mutex_lock(&dev->struct_mutex);
  5876. /* Reference the objects for the scheduled work. */
  5877. drm_gem_object_reference(&work->old_fb_obj->base);
  5878. drm_gem_object_reference(&obj->base);
  5879. crtc->fb = fb;
  5880. ret = drm_vblank_get(dev, intel_crtc->pipe);
  5881. if (ret)
  5882. goto cleanup_objs;
  5883. work->pending_flip_obj = obj;
  5884. work->enable_stall_check = true;
  5885. /* Block clients from rendering to the new back buffer until
  5886. * the flip occurs and the object is no longer visible.
  5887. */
  5888. atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5889. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  5890. if (ret)
  5891. goto cleanup_pending;
  5892. intel_disable_fbc(dev);
  5893. mutex_unlock(&dev->struct_mutex);
  5894. trace_i915_flip_request(intel_crtc->plane, obj);
  5895. return 0;
  5896. cleanup_pending:
  5897. atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
  5898. cleanup_objs:
  5899. drm_gem_object_unreference(&work->old_fb_obj->base);
  5900. drm_gem_object_unreference(&obj->base);
  5901. mutex_unlock(&dev->struct_mutex);
  5902. spin_lock_irqsave(&dev->event_lock, flags);
  5903. intel_crtc->unpin_work = NULL;
  5904. spin_unlock_irqrestore(&dev->event_lock, flags);
  5905. kfree(work);
  5906. return ret;
  5907. }
  5908. static void intel_sanitize_modesetting(struct drm_device *dev,
  5909. int pipe, int plane)
  5910. {
  5911. struct drm_i915_private *dev_priv = dev->dev_private;
  5912. u32 reg, val;
  5913. if (HAS_PCH_SPLIT(dev))
  5914. return;
  5915. /* Who knows what state these registers were left in by the BIOS or
  5916. * grub?
  5917. *
  5918. * If we leave the registers in a conflicting state (e.g. with the
  5919. * display plane reading from the other pipe than the one we intend
  5920. * to use) then when we attempt to teardown the active mode, we will
  5921. * not disable the pipes and planes in the correct order -- leaving
  5922. * a plane reading from a disabled pipe and possibly leading to
  5923. * undefined behaviour.
  5924. */
  5925. reg = DSPCNTR(plane);
  5926. val = I915_READ(reg);
  5927. if ((val & DISPLAY_PLANE_ENABLE) == 0)
  5928. return;
  5929. if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
  5930. return;
  5931. /* This display plane is active and attached to the other CPU pipe. */
  5932. pipe = !pipe;
  5933. /* Disable the plane and wait for it to stop reading from the pipe. */
  5934. intel_disable_plane(dev_priv, plane, pipe);
  5935. intel_disable_pipe(dev_priv, pipe);
  5936. }
  5937. static void intel_crtc_reset(struct drm_crtc *crtc)
  5938. {
  5939. struct drm_device *dev = crtc->dev;
  5940. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5941. /* Reset flags back to the 'unknown' status so that they
  5942. * will be correctly set on the initial modeset.
  5943. */
  5944. intel_crtc->dpms_mode = -1;
  5945. /* We need to fix up any BIOS configuration that conflicts with
  5946. * our expectations.
  5947. */
  5948. intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
  5949. }
  5950. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  5951. .dpms = intel_crtc_dpms,
  5952. .mode_fixup = intel_crtc_mode_fixup,
  5953. .mode_set = intel_crtc_mode_set,
  5954. .mode_set_base = intel_pipe_set_base,
  5955. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  5956. .load_lut = intel_crtc_load_lut,
  5957. .disable = intel_crtc_disable,
  5958. };
  5959. static const struct drm_crtc_funcs intel_crtc_funcs = {
  5960. .reset = intel_crtc_reset,
  5961. .cursor_set = intel_crtc_cursor_set,
  5962. .cursor_move = intel_crtc_cursor_move,
  5963. .gamma_set = intel_crtc_gamma_set,
  5964. .set_config = drm_crtc_helper_set_config,
  5965. .destroy = intel_crtc_destroy,
  5966. .page_flip = intel_crtc_page_flip,
  5967. };
  5968. static void intel_crtc_init(struct drm_device *dev, int pipe)
  5969. {
  5970. drm_i915_private_t *dev_priv = dev->dev_private;
  5971. struct intel_crtc *intel_crtc;
  5972. int i;
  5973. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  5974. if (intel_crtc == NULL)
  5975. return;
  5976. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  5977. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  5978. for (i = 0; i < 256; i++) {
  5979. intel_crtc->lut_r[i] = i;
  5980. intel_crtc->lut_g[i] = i;
  5981. intel_crtc->lut_b[i] = i;
  5982. }
  5983. /* Swap pipes & planes for FBC on pre-965 */
  5984. intel_crtc->pipe = pipe;
  5985. intel_crtc->plane = pipe;
  5986. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  5987. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  5988. intel_crtc->plane = !pipe;
  5989. }
  5990. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  5991. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  5992. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  5993. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  5994. intel_crtc_reset(&intel_crtc->base);
  5995. intel_crtc->active = true; /* force the pipe off on setup_init_config */
  5996. intel_crtc->bpp = 24; /* default for pre-Ironlake */
  5997. if (HAS_PCH_SPLIT(dev)) {
  5998. intel_helper_funcs.prepare = ironlake_crtc_prepare;
  5999. intel_helper_funcs.commit = ironlake_crtc_commit;
  6000. } else {
  6001. intel_helper_funcs.prepare = i9xx_crtc_prepare;
  6002. intel_helper_funcs.commit = i9xx_crtc_commit;
  6003. }
  6004. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  6005. intel_crtc->busy = false;
  6006. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  6007. (unsigned long)intel_crtc);
  6008. }
  6009. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  6010. struct drm_file *file)
  6011. {
  6012. drm_i915_private_t *dev_priv = dev->dev_private;
  6013. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  6014. struct drm_mode_object *drmmode_obj;
  6015. struct intel_crtc *crtc;
  6016. if (!dev_priv) {
  6017. DRM_ERROR("called with no initialization\n");
  6018. return -EINVAL;
  6019. }
  6020. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  6021. DRM_MODE_OBJECT_CRTC);
  6022. if (!drmmode_obj) {
  6023. DRM_ERROR("no such CRTC id\n");
  6024. return -EINVAL;
  6025. }
  6026. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  6027. pipe_from_crtc_id->pipe = crtc->pipe;
  6028. return 0;
  6029. }
  6030. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  6031. {
  6032. struct intel_encoder *encoder;
  6033. int index_mask = 0;
  6034. int entry = 0;
  6035. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  6036. if (type_mask & encoder->clone_mask)
  6037. index_mask |= (1 << entry);
  6038. entry++;
  6039. }
  6040. return index_mask;
  6041. }
  6042. static bool has_edp_a(struct drm_device *dev)
  6043. {
  6044. struct drm_i915_private *dev_priv = dev->dev_private;
  6045. if (!IS_MOBILE(dev))
  6046. return false;
  6047. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  6048. return false;
  6049. if (IS_GEN5(dev) &&
  6050. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  6051. return false;
  6052. return true;
  6053. }
  6054. static void intel_setup_outputs(struct drm_device *dev)
  6055. {
  6056. struct drm_i915_private *dev_priv = dev->dev_private;
  6057. struct intel_encoder *encoder;
  6058. bool dpd_is_edp = false;
  6059. bool has_lvds = false;
  6060. if (IS_MOBILE(dev) && !IS_I830(dev))
  6061. has_lvds = intel_lvds_init(dev);
  6062. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  6063. /* disable the panel fitter on everything but LVDS */
  6064. I915_WRITE(PFIT_CONTROL, 0);
  6065. }
  6066. if (HAS_PCH_SPLIT(dev)) {
  6067. dpd_is_edp = intel_dpd_is_edp(dev);
  6068. if (has_edp_a(dev))
  6069. intel_dp_init(dev, DP_A);
  6070. if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  6071. intel_dp_init(dev, PCH_DP_D);
  6072. }
  6073. intel_crt_init(dev);
  6074. if (HAS_PCH_SPLIT(dev)) {
  6075. int found;
  6076. if (I915_READ(HDMIB) & PORT_DETECTED) {
  6077. /* PCH SDVOB multiplex with HDMIB */
  6078. found = intel_sdvo_init(dev, PCH_SDVOB);
  6079. if (!found)
  6080. intel_hdmi_init(dev, HDMIB);
  6081. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  6082. intel_dp_init(dev, PCH_DP_B);
  6083. }
  6084. if (I915_READ(HDMIC) & PORT_DETECTED)
  6085. intel_hdmi_init(dev, HDMIC);
  6086. if (I915_READ(HDMID) & PORT_DETECTED)
  6087. intel_hdmi_init(dev, HDMID);
  6088. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  6089. intel_dp_init(dev, PCH_DP_C);
  6090. if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
  6091. intel_dp_init(dev, PCH_DP_D);
  6092. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  6093. bool found = false;
  6094. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  6095. DRM_DEBUG_KMS("probing SDVOB\n");
  6096. found = intel_sdvo_init(dev, SDVOB);
  6097. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  6098. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  6099. intel_hdmi_init(dev, SDVOB);
  6100. }
  6101. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  6102. DRM_DEBUG_KMS("probing DP_B\n");
  6103. intel_dp_init(dev, DP_B);
  6104. }
  6105. }
  6106. /* Before G4X SDVOC doesn't have its own detect register */
  6107. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  6108. DRM_DEBUG_KMS("probing SDVOC\n");
  6109. found = intel_sdvo_init(dev, SDVOC);
  6110. }
  6111. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  6112. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  6113. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  6114. intel_hdmi_init(dev, SDVOC);
  6115. }
  6116. if (SUPPORTS_INTEGRATED_DP(dev)) {
  6117. DRM_DEBUG_KMS("probing DP_C\n");
  6118. intel_dp_init(dev, DP_C);
  6119. }
  6120. }
  6121. if (SUPPORTS_INTEGRATED_DP(dev) &&
  6122. (I915_READ(DP_D) & DP_DETECTED)) {
  6123. DRM_DEBUG_KMS("probing DP_D\n");
  6124. intel_dp_init(dev, DP_D);
  6125. }
  6126. } else if (IS_GEN2(dev))
  6127. intel_dvo_init(dev);
  6128. if (SUPPORTS_TV(dev))
  6129. intel_tv_init(dev);
  6130. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  6131. encoder->base.possible_crtcs = encoder->crtc_mask;
  6132. encoder->base.possible_clones =
  6133. intel_encoder_clones(dev, encoder->clone_mask);
  6134. }
  6135. intel_panel_setup_backlight(dev);
  6136. /* disable all the possible outputs/crtcs before entering KMS mode */
  6137. drm_helper_disable_unused_functions(dev);
  6138. }
  6139. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  6140. {
  6141. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  6142. drm_framebuffer_cleanup(fb);
  6143. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  6144. kfree(intel_fb);
  6145. }
  6146. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  6147. struct drm_file *file,
  6148. unsigned int *handle)
  6149. {
  6150. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  6151. struct drm_i915_gem_object *obj = intel_fb->obj;
  6152. return drm_gem_handle_create(file, &obj->base, handle);
  6153. }
  6154. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  6155. .destroy = intel_user_framebuffer_destroy,
  6156. .create_handle = intel_user_framebuffer_create_handle,
  6157. };
  6158. int intel_framebuffer_init(struct drm_device *dev,
  6159. struct intel_framebuffer *intel_fb,
  6160. struct drm_mode_fb_cmd *mode_cmd,
  6161. struct drm_i915_gem_object *obj)
  6162. {
  6163. int ret;
  6164. if (obj->tiling_mode == I915_TILING_Y)
  6165. return -EINVAL;
  6166. if (mode_cmd->pitch & 63)
  6167. return -EINVAL;
  6168. switch (mode_cmd->bpp) {
  6169. case 8:
  6170. case 16:
  6171. /* Only pre-ILK can handle 5:5:5 */
  6172. if (mode_cmd->depth == 15 && !HAS_PCH_SPLIT(dev))
  6173. return -EINVAL;
  6174. break;
  6175. case 24:
  6176. case 32:
  6177. break;
  6178. default:
  6179. return -EINVAL;
  6180. }
  6181. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  6182. if (ret) {
  6183. DRM_ERROR("framebuffer init failed %d\n", ret);
  6184. return ret;
  6185. }
  6186. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  6187. intel_fb->obj = obj;
  6188. return 0;
  6189. }
  6190. static struct drm_framebuffer *
  6191. intel_user_framebuffer_create(struct drm_device *dev,
  6192. struct drm_file *filp,
  6193. struct drm_mode_fb_cmd *mode_cmd)
  6194. {
  6195. struct drm_i915_gem_object *obj;
  6196. obj = to_intel_bo(drm_gem_object_lookup(dev, filp, mode_cmd->handle));
  6197. if (&obj->base == NULL)
  6198. return ERR_PTR(-ENOENT);
  6199. return intel_framebuffer_create(dev, mode_cmd, obj);
  6200. }
  6201. static const struct drm_mode_config_funcs intel_mode_funcs = {
  6202. .fb_create = intel_user_framebuffer_create,
  6203. .output_poll_changed = intel_fb_output_poll_changed,
  6204. };
  6205. static struct drm_i915_gem_object *
  6206. intel_alloc_context_page(struct drm_device *dev)
  6207. {
  6208. struct drm_i915_gem_object *ctx;
  6209. int ret;
  6210. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  6211. ctx = i915_gem_alloc_object(dev, 4096);
  6212. if (!ctx) {
  6213. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  6214. return NULL;
  6215. }
  6216. ret = i915_gem_object_pin(ctx, 4096, true);
  6217. if (ret) {
  6218. DRM_ERROR("failed to pin power context: %d\n", ret);
  6219. goto err_unref;
  6220. }
  6221. ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
  6222. if (ret) {
  6223. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  6224. goto err_unpin;
  6225. }
  6226. return ctx;
  6227. err_unpin:
  6228. i915_gem_object_unpin(ctx);
  6229. err_unref:
  6230. drm_gem_object_unreference(&ctx->base);
  6231. mutex_unlock(&dev->struct_mutex);
  6232. return NULL;
  6233. }
  6234. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  6235. {
  6236. struct drm_i915_private *dev_priv = dev->dev_private;
  6237. u16 rgvswctl;
  6238. rgvswctl = I915_READ16(MEMSWCTL);
  6239. if (rgvswctl & MEMCTL_CMD_STS) {
  6240. DRM_DEBUG("gpu busy, RCS change rejected\n");
  6241. return false; /* still busy with another command */
  6242. }
  6243. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  6244. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  6245. I915_WRITE16(MEMSWCTL, rgvswctl);
  6246. POSTING_READ16(MEMSWCTL);
  6247. rgvswctl |= MEMCTL_CMD_STS;
  6248. I915_WRITE16(MEMSWCTL, rgvswctl);
  6249. return true;
  6250. }
  6251. void ironlake_enable_drps(struct drm_device *dev)
  6252. {
  6253. struct drm_i915_private *dev_priv = dev->dev_private;
  6254. u32 rgvmodectl = I915_READ(MEMMODECTL);
  6255. u8 fmax, fmin, fstart, vstart;
  6256. /* Enable temp reporting */
  6257. I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
  6258. I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
  6259. /* 100ms RC evaluation intervals */
  6260. I915_WRITE(RCUPEI, 100000);
  6261. I915_WRITE(RCDNEI, 100000);
  6262. /* Set max/min thresholds to 90ms and 80ms respectively */
  6263. I915_WRITE(RCBMAXAVG, 90000);
  6264. I915_WRITE(RCBMINAVG, 80000);
  6265. I915_WRITE(MEMIHYST, 1);
  6266. /* Set up min, max, and cur for interrupt handling */
  6267. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  6268. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  6269. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  6270. MEMMODE_FSTART_SHIFT;
  6271. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  6272. PXVFREQ_PX_SHIFT;
  6273. dev_priv->fmax = fmax; /* IPS callback will increase this */
  6274. dev_priv->fstart = fstart;
  6275. dev_priv->max_delay = fstart;
  6276. dev_priv->min_delay = fmin;
  6277. dev_priv->cur_delay = fstart;
  6278. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
  6279. fmax, fmin, fstart);
  6280. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  6281. /*
  6282. * Interrupts will be enabled in ironlake_irq_postinstall
  6283. */
  6284. I915_WRITE(VIDSTART, vstart);
  6285. POSTING_READ(VIDSTART);
  6286. rgvmodectl |= MEMMODE_SWMODE_EN;
  6287. I915_WRITE(MEMMODECTL, rgvmodectl);
  6288. if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
  6289. DRM_ERROR("stuck trying to change perf mode\n");
  6290. msleep(1);
  6291. ironlake_set_drps(dev, fstart);
  6292. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  6293. I915_READ(0x112e0);
  6294. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  6295. dev_priv->last_count2 = I915_READ(0x112f4);
  6296. getrawmonotonic(&dev_priv->last_time2);
  6297. }
  6298. void ironlake_disable_drps(struct drm_device *dev)
  6299. {
  6300. struct drm_i915_private *dev_priv = dev->dev_private;
  6301. u16 rgvswctl = I915_READ16(MEMSWCTL);
  6302. /* Ack interrupts, disable EFC interrupt */
  6303. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  6304. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  6305. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  6306. I915_WRITE(DEIIR, DE_PCU_EVENT);
  6307. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  6308. /* Go back to the starting frequency */
  6309. ironlake_set_drps(dev, dev_priv->fstart);
  6310. msleep(1);
  6311. rgvswctl |= MEMCTL_CMD_STS;
  6312. I915_WRITE(MEMSWCTL, rgvswctl);
  6313. msleep(1);
  6314. }
  6315. void gen6_set_rps(struct drm_device *dev, u8 val)
  6316. {
  6317. struct drm_i915_private *dev_priv = dev->dev_private;
  6318. u32 swreq;
  6319. swreq = (val & 0x3ff) << 25;
  6320. I915_WRITE(GEN6_RPNSWREQ, swreq);
  6321. }
  6322. void gen6_disable_rps(struct drm_device *dev)
  6323. {
  6324. struct drm_i915_private *dev_priv = dev->dev_private;
  6325. I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
  6326. I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
  6327. I915_WRITE(GEN6_PMIER, 0);
  6328. spin_lock_irq(&dev_priv->rps_lock);
  6329. dev_priv->pm_iir = 0;
  6330. spin_unlock_irq(&dev_priv->rps_lock);
  6331. I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
  6332. }
  6333. static unsigned long intel_pxfreq(u32 vidfreq)
  6334. {
  6335. unsigned long freq;
  6336. int div = (vidfreq & 0x3f0000) >> 16;
  6337. int post = (vidfreq & 0x3000) >> 12;
  6338. int pre = (vidfreq & 0x7);
  6339. if (!pre)
  6340. return 0;
  6341. freq = ((div * 133333) / ((1<<post) * pre));
  6342. return freq;
  6343. }
  6344. void intel_init_emon(struct drm_device *dev)
  6345. {
  6346. struct drm_i915_private *dev_priv = dev->dev_private;
  6347. u32 lcfuse;
  6348. u8 pxw[16];
  6349. int i;
  6350. /* Disable to program */
  6351. I915_WRITE(ECR, 0);
  6352. POSTING_READ(ECR);
  6353. /* Program energy weights for various events */
  6354. I915_WRITE(SDEW, 0x15040d00);
  6355. I915_WRITE(CSIEW0, 0x007f0000);
  6356. I915_WRITE(CSIEW1, 0x1e220004);
  6357. I915_WRITE(CSIEW2, 0x04000004);
  6358. for (i = 0; i < 5; i++)
  6359. I915_WRITE(PEW + (i * 4), 0);
  6360. for (i = 0; i < 3; i++)
  6361. I915_WRITE(DEW + (i * 4), 0);
  6362. /* Program P-state weights to account for frequency power adjustment */
  6363. for (i = 0; i < 16; i++) {
  6364. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  6365. unsigned long freq = intel_pxfreq(pxvidfreq);
  6366. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  6367. PXVFREQ_PX_SHIFT;
  6368. unsigned long val;
  6369. val = vid * vid;
  6370. val *= (freq / 1000);
  6371. val *= 255;
  6372. val /= (127*127*900);
  6373. if (val > 0xff)
  6374. DRM_ERROR("bad pxval: %ld\n", val);
  6375. pxw[i] = val;
  6376. }
  6377. /* Render standby states get 0 weight */
  6378. pxw[14] = 0;
  6379. pxw[15] = 0;
  6380. for (i = 0; i < 4; i++) {
  6381. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  6382. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  6383. I915_WRITE(PXW + (i * 4), val);
  6384. }
  6385. /* Adjust magic regs to magic values (more experimental results) */
  6386. I915_WRITE(OGW0, 0);
  6387. I915_WRITE(OGW1, 0);
  6388. I915_WRITE(EG0, 0x00007f00);
  6389. I915_WRITE(EG1, 0x0000000e);
  6390. I915_WRITE(EG2, 0x000e0000);
  6391. I915_WRITE(EG3, 0x68000300);
  6392. I915_WRITE(EG4, 0x42000000);
  6393. I915_WRITE(EG5, 0x00140031);
  6394. I915_WRITE(EG6, 0);
  6395. I915_WRITE(EG7, 0);
  6396. for (i = 0; i < 8; i++)
  6397. I915_WRITE(PXWL + (i * 4), 0);
  6398. /* Enable PMON + select events */
  6399. I915_WRITE(ECR, 0x80000019);
  6400. lcfuse = I915_READ(LCFUSE02);
  6401. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  6402. }
  6403. void gen6_enable_rps(struct drm_i915_private *dev_priv)
  6404. {
  6405. u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
  6406. u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
  6407. u32 pcu_mbox, rc6_mask = 0;
  6408. int cur_freq, min_freq, max_freq;
  6409. int i;
  6410. /* Here begins a magic sequence of register writes to enable
  6411. * auto-downclocking.
  6412. *
  6413. * Perhaps there might be some value in exposing these to
  6414. * userspace...
  6415. */
  6416. I915_WRITE(GEN6_RC_STATE, 0);
  6417. mutex_lock(&dev_priv->dev->struct_mutex);
  6418. gen6_gt_force_wake_get(dev_priv);
  6419. /* disable the counters and set deterministic thresholds */
  6420. I915_WRITE(GEN6_RC_CONTROL, 0);
  6421. I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
  6422. I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
  6423. I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
  6424. I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
  6425. I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
  6426. for (i = 0; i < I915_NUM_RINGS; i++)
  6427. I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
  6428. I915_WRITE(GEN6_RC_SLEEP, 0);
  6429. I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
  6430. I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
  6431. I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
  6432. I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
  6433. if (i915_enable_rc6)
  6434. rc6_mask = GEN6_RC_CTL_RC6p_ENABLE |
  6435. GEN6_RC_CTL_RC6_ENABLE;
  6436. I915_WRITE(GEN6_RC_CONTROL,
  6437. rc6_mask |
  6438. GEN6_RC_CTL_EI_MODE(1) |
  6439. GEN6_RC_CTL_HW_ENABLE);
  6440. I915_WRITE(GEN6_RPNSWREQ,
  6441. GEN6_FREQUENCY(10) |
  6442. GEN6_OFFSET(0) |
  6443. GEN6_AGGRESSIVE_TURBO);
  6444. I915_WRITE(GEN6_RC_VIDEO_FREQ,
  6445. GEN6_FREQUENCY(12));
  6446. I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
  6447. I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
  6448. 18 << 24 |
  6449. 6 << 16);
  6450. I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
  6451. I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
  6452. I915_WRITE(GEN6_RP_UP_EI, 100000);
  6453. I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
  6454. I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
  6455. I915_WRITE(GEN6_RP_CONTROL,
  6456. GEN6_RP_MEDIA_TURBO |
  6457. GEN6_RP_USE_NORMAL_FREQ |
  6458. GEN6_RP_MEDIA_IS_GFX |
  6459. GEN6_RP_ENABLE |
  6460. GEN6_RP_UP_BUSY_AVG |
  6461. GEN6_RP_DOWN_IDLE_CONT);
  6462. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6463. 500))
  6464. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  6465. I915_WRITE(GEN6_PCODE_DATA, 0);
  6466. I915_WRITE(GEN6_PCODE_MAILBOX,
  6467. GEN6_PCODE_READY |
  6468. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  6469. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6470. 500))
  6471. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  6472. min_freq = (rp_state_cap & 0xff0000) >> 16;
  6473. max_freq = rp_state_cap & 0xff;
  6474. cur_freq = (gt_perf_status & 0xff00) >> 8;
  6475. /* Check for overclock support */
  6476. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6477. 500))
  6478. DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
  6479. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
  6480. pcu_mbox = I915_READ(GEN6_PCODE_DATA);
  6481. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
  6482. 500))
  6483. DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
  6484. if (pcu_mbox & (1<<31)) { /* OC supported */
  6485. max_freq = pcu_mbox & 0xff;
  6486. DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
  6487. }
  6488. /* In units of 100MHz */
  6489. dev_priv->max_delay = max_freq;
  6490. dev_priv->min_delay = min_freq;
  6491. dev_priv->cur_delay = cur_freq;
  6492. /* requires MSI enabled */
  6493. I915_WRITE(GEN6_PMIER,
  6494. GEN6_PM_MBOX_EVENT |
  6495. GEN6_PM_THERMAL_EVENT |
  6496. GEN6_PM_RP_DOWN_TIMEOUT |
  6497. GEN6_PM_RP_UP_THRESHOLD |
  6498. GEN6_PM_RP_DOWN_THRESHOLD |
  6499. GEN6_PM_RP_UP_EI_EXPIRED |
  6500. GEN6_PM_RP_DOWN_EI_EXPIRED);
  6501. spin_lock_irq(&dev_priv->rps_lock);
  6502. WARN_ON(dev_priv->pm_iir != 0);
  6503. I915_WRITE(GEN6_PMIMR, 0);
  6504. spin_unlock_irq(&dev_priv->rps_lock);
  6505. /* enable all PM interrupts */
  6506. I915_WRITE(GEN6_PMINTRMSK, 0);
  6507. gen6_gt_force_wake_put(dev_priv);
  6508. mutex_unlock(&dev_priv->dev->struct_mutex);
  6509. }
  6510. void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
  6511. {
  6512. int min_freq = 15;
  6513. int gpu_freq, ia_freq, max_ia_freq;
  6514. int scaling_factor = 180;
  6515. max_ia_freq = cpufreq_quick_get_max(0);
  6516. /*
  6517. * Default to measured freq if none found, PCU will ensure we don't go
  6518. * over
  6519. */
  6520. if (!max_ia_freq)
  6521. max_ia_freq = tsc_khz;
  6522. /* Convert from kHz to MHz */
  6523. max_ia_freq /= 1000;
  6524. mutex_lock(&dev_priv->dev->struct_mutex);
  6525. /*
  6526. * For each potential GPU frequency, load a ring frequency we'd like
  6527. * to use for memory access. We do this by specifying the IA frequency
  6528. * the PCU should use as a reference to determine the ring frequency.
  6529. */
  6530. for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
  6531. gpu_freq--) {
  6532. int diff = dev_priv->max_delay - gpu_freq;
  6533. /*
  6534. * For GPU frequencies less than 750MHz, just use the lowest
  6535. * ring freq.
  6536. */
  6537. if (gpu_freq < min_freq)
  6538. ia_freq = 800;
  6539. else
  6540. ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
  6541. ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
  6542. I915_WRITE(GEN6_PCODE_DATA,
  6543. (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
  6544. gpu_freq);
  6545. I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
  6546. GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
  6547. if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
  6548. GEN6_PCODE_READY) == 0, 10)) {
  6549. DRM_ERROR("pcode write of freq table timed out\n");
  6550. continue;
  6551. }
  6552. }
  6553. mutex_unlock(&dev_priv->dev->struct_mutex);
  6554. }
  6555. static void ironlake_init_clock_gating(struct drm_device *dev)
  6556. {
  6557. struct drm_i915_private *dev_priv = dev->dev_private;
  6558. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6559. /* Required for FBC */
  6560. dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
  6561. DPFCRUNIT_CLOCK_GATE_DISABLE |
  6562. DPFDUNIT_CLOCK_GATE_DISABLE;
  6563. /* Required for CxSR */
  6564. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  6565. I915_WRITE(PCH_3DCGDIS0,
  6566. MARIUNIT_CLOCK_GATE_DISABLE |
  6567. SVSMUNIT_CLOCK_GATE_DISABLE);
  6568. I915_WRITE(PCH_3DCGDIS1,
  6569. VFMUNIT_CLOCK_GATE_DISABLE);
  6570. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6571. /*
  6572. * According to the spec the following bits should be set in
  6573. * order to enable memory self-refresh
  6574. * The bit 22/21 of 0x42004
  6575. * The bit 5 of 0x42020
  6576. * The bit 15 of 0x45000
  6577. */
  6578. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6579. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  6580. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  6581. I915_WRITE(ILK_DSPCLK_GATE,
  6582. (I915_READ(ILK_DSPCLK_GATE) |
  6583. ILK_DPARB_CLK_GATE));
  6584. I915_WRITE(DISP_ARB_CTL,
  6585. (I915_READ(DISP_ARB_CTL) |
  6586. DISP_FBC_WM_DIS));
  6587. I915_WRITE(WM3_LP_ILK, 0);
  6588. I915_WRITE(WM2_LP_ILK, 0);
  6589. I915_WRITE(WM1_LP_ILK, 0);
  6590. /*
  6591. * Based on the document from hardware guys the following bits
  6592. * should be set unconditionally in order to enable FBC.
  6593. * The bit 22 of 0x42000
  6594. * The bit 22 of 0x42004
  6595. * The bit 7,8,9 of 0x42020.
  6596. */
  6597. if (IS_IRONLAKE_M(dev)) {
  6598. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  6599. I915_READ(ILK_DISPLAY_CHICKEN1) |
  6600. ILK_FBCQ_DIS);
  6601. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6602. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6603. ILK_DPARB_GATE);
  6604. I915_WRITE(ILK_DSPCLK_GATE,
  6605. I915_READ(ILK_DSPCLK_GATE) |
  6606. ILK_DPFC_DIS1 |
  6607. ILK_DPFC_DIS2 |
  6608. ILK_CLK_FBC);
  6609. }
  6610. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6611. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6612. ILK_ELPIN_409_SELECT);
  6613. I915_WRITE(_3D_CHICKEN2,
  6614. _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
  6615. _3D_CHICKEN2_WM_READ_PIPELINED);
  6616. }
  6617. static void gen6_init_clock_gating(struct drm_device *dev)
  6618. {
  6619. struct drm_i915_private *dev_priv = dev->dev_private;
  6620. int pipe;
  6621. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6622. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6623. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6624. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6625. ILK_ELPIN_409_SELECT);
  6626. I915_WRITE(WM3_LP_ILK, 0);
  6627. I915_WRITE(WM2_LP_ILK, 0);
  6628. I915_WRITE(WM1_LP_ILK, 0);
  6629. /*
  6630. * According to the spec the following bits should be
  6631. * set in order to enable memory self-refresh and fbc:
  6632. * The bit21 and bit22 of 0x42000
  6633. * The bit21 and bit22 of 0x42004
  6634. * The bit5 and bit7 of 0x42020
  6635. * The bit14 of 0x70180
  6636. * The bit14 of 0x71180
  6637. */
  6638. I915_WRITE(ILK_DISPLAY_CHICKEN1,
  6639. I915_READ(ILK_DISPLAY_CHICKEN1) |
  6640. ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
  6641. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  6642. I915_READ(ILK_DISPLAY_CHICKEN2) |
  6643. ILK_DPARB_GATE | ILK_VSDPFD_FULL);
  6644. I915_WRITE(ILK_DSPCLK_GATE,
  6645. I915_READ(ILK_DSPCLK_GATE) |
  6646. ILK_DPARB_CLK_GATE |
  6647. ILK_DPFD_CLK_GATE);
  6648. for_each_pipe(pipe) {
  6649. I915_WRITE(DSPCNTR(pipe),
  6650. I915_READ(DSPCNTR(pipe)) |
  6651. DISPPLANE_TRICKLE_FEED_DISABLE);
  6652. intel_flush_display_plane(dev_priv, pipe);
  6653. }
  6654. }
  6655. static void ivybridge_init_clock_gating(struct drm_device *dev)
  6656. {
  6657. struct drm_i915_private *dev_priv = dev->dev_private;
  6658. int pipe;
  6659. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  6660. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  6661. I915_WRITE(WM3_LP_ILK, 0);
  6662. I915_WRITE(WM2_LP_ILK, 0);
  6663. I915_WRITE(WM1_LP_ILK, 0);
  6664. I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
  6665. for_each_pipe(pipe) {
  6666. I915_WRITE(DSPCNTR(pipe),
  6667. I915_READ(DSPCNTR(pipe)) |
  6668. DISPPLANE_TRICKLE_FEED_DISABLE);
  6669. intel_flush_display_plane(dev_priv, pipe);
  6670. }
  6671. }
  6672. static void g4x_init_clock_gating(struct drm_device *dev)
  6673. {
  6674. struct drm_i915_private *dev_priv = dev->dev_private;
  6675. uint32_t dspclk_gate;
  6676. I915_WRITE(RENCLK_GATE_D1, 0);
  6677. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  6678. GS_UNIT_CLOCK_GATE_DISABLE |
  6679. CL_UNIT_CLOCK_GATE_DISABLE);
  6680. I915_WRITE(RAMCLK_GATE_D, 0);
  6681. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  6682. OVRUNIT_CLOCK_GATE_DISABLE |
  6683. OVCUNIT_CLOCK_GATE_DISABLE;
  6684. if (IS_GM45(dev))
  6685. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  6686. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  6687. }
  6688. static void crestline_init_clock_gating(struct drm_device *dev)
  6689. {
  6690. struct drm_i915_private *dev_priv = dev->dev_private;
  6691. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  6692. I915_WRITE(RENCLK_GATE_D2, 0);
  6693. I915_WRITE(DSPCLK_GATE_D, 0);
  6694. I915_WRITE(RAMCLK_GATE_D, 0);
  6695. I915_WRITE16(DEUC, 0);
  6696. }
  6697. static void broadwater_init_clock_gating(struct drm_device *dev)
  6698. {
  6699. struct drm_i915_private *dev_priv = dev->dev_private;
  6700. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  6701. I965_RCC_CLOCK_GATE_DISABLE |
  6702. I965_RCPB_CLOCK_GATE_DISABLE |
  6703. I965_ISC_CLOCK_GATE_DISABLE |
  6704. I965_FBC_CLOCK_GATE_DISABLE);
  6705. I915_WRITE(RENCLK_GATE_D2, 0);
  6706. }
  6707. static void gen3_init_clock_gating(struct drm_device *dev)
  6708. {
  6709. struct drm_i915_private *dev_priv = dev->dev_private;
  6710. u32 dstate = I915_READ(D_STATE);
  6711. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  6712. DSTATE_DOT_CLOCK_GATING;
  6713. I915_WRITE(D_STATE, dstate);
  6714. }
  6715. static void i85x_init_clock_gating(struct drm_device *dev)
  6716. {
  6717. struct drm_i915_private *dev_priv = dev->dev_private;
  6718. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  6719. }
  6720. static void i830_init_clock_gating(struct drm_device *dev)
  6721. {
  6722. struct drm_i915_private *dev_priv = dev->dev_private;
  6723. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  6724. }
  6725. static void ibx_init_clock_gating(struct drm_device *dev)
  6726. {
  6727. struct drm_i915_private *dev_priv = dev->dev_private;
  6728. /*
  6729. * On Ibex Peak and Cougar Point, we need to disable clock
  6730. * gating for the panel power sequencer or it will fail to
  6731. * start up when no ports are active.
  6732. */
  6733. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  6734. }
  6735. static void cpt_init_clock_gating(struct drm_device *dev)
  6736. {
  6737. struct drm_i915_private *dev_priv = dev->dev_private;
  6738. int pipe;
  6739. /*
  6740. * On Ibex Peak and Cougar Point, we need to disable clock
  6741. * gating for the panel power sequencer or it will fail to
  6742. * start up when no ports are active.
  6743. */
  6744. I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
  6745. I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
  6746. DPLS_EDP_PPS_FIX_DIS);
  6747. /* Without this, mode sets may fail silently on FDI */
  6748. for_each_pipe(pipe)
  6749. I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_AUTOTRAIN_GEN_STALL_DIS);
  6750. }
  6751. static void ironlake_teardown_rc6(struct drm_device *dev)
  6752. {
  6753. struct drm_i915_private *dev_priv = dev->dev_private;
  6754. if (dev_priv->renderctx) {
  6755. i915_gem_object_unpin(dev_priv->renderctx);
  6756. drm_gem_object_unreference(&dev_priv->renderctx->base);
  6757. dev_priv->renderctx = NULL;
  6758. }
  6759. if (dev_priv->pwrctx) {
  6760. i915_gem_object_unpin(dev_priv->pwrctx);
  6761. drm_gem_object_unreference(&dev_priv->pwrctx->base);
  6762. dev_priv->pwrctx = NULL;
  6763. }
  6764. }
  6765. static void ironlake_disable_rc6(struct drm_device *dev)
  6766. {
  6767. struct drm_i915_private *dev_priv = dev->dev_private;
  6768. if (I915_READ(PWRCTXA)) {
  6769. /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
  6770. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
  6771. wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
  6772. 50);
  6773. I915_WRITE(PWRCTXA, 0);
  6774. POSTING_READ(PWRCTXA);
  6775. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  6776. POSTING_READ(RSTDBYCTL);
  6777. }
  6778. ironlake_teardown_rc6(dev);
  6779. }
  6780. static int ironlake_setup_rc6(struct drm_device *dev)
  6781. {
  6782. struct drm_i915_private *dev_priv = dev->dev_private;
  6783. if (dev_priv->renderctx == NULL)
  6784. dev_priv->renderctx = intel_alloc_context_page(dev);
  6785. if (!dev_priv->renderctx)
  6786. return -ENOMEM;
  6787. if (dev_priv->pwrctx == NULL)
  6788. dev_priv->pwrctx = intel_alloc_context_page(dev);
  6789. if (!dev_priv->pwrctx) {
  6790. ironlake_teardown_rc6(dev);
  6791. return -ENOMEM;
  6792. }
  6793. return 0;
  6794. }
  6795. void ironlake_enable_rc6(struct drm_device *dev)
  6796. {
  6797. struct drm_i915_private *dev_priv = dev->dev_private;
  6798. int ret;
  6799. /* rc6 disabled by default due to repeated reports of hanging during
  6800. * boot and resume.
  6801. */
  6802. if (!i915_enable_rc6)
  6803. return;
  6804. mutex_lock(&dev->struct_mutex);
  6805. ret = ironlake_setup_rc6(dev);
  6806. if (ret) {
  6807. mutex_unlock(&dev->struct_mutex);
  6808. return;
  6809. }
  6810. /*
  6811. * GPU can automatically power down the render unit if given a page
  6812. * to save state.
  6813. */
  6814. ret = BEGIN_LP_RING(6);
  6815. if (ret) {
  6816. ironlake_teardown_rc6(dev);
  6817. mutex_unlock(&dev->struct_mutex);
  6818. return;
  6819. }
  6820. OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
  6821. OUT_RING(MI_SET_CONTEXT);
  6822. OUT_RING(dev_priv->renderctx->gtt_offset |
  6823. MI_MM_SPACE_GTT |
  6824. MI_SAVE_EXT_STATE_EN |
  6825. MI_RESTORE_EXT_STATE_EN |
  6826. MI_RESTORE_INHIBIT);
  6827. OUT_RING(MI_SUSPEND_FLUSH);
  6828. OUT_RING(MI_NOOP);
  6829. OUT_RING(MI_FLUSH);
  6830. ADVANCE_LP_RING();
  6831. /*
  6832. * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
  6833. * does an implicit flush, combined with MI_FLUSH above, it should be
  6834. * safe to assume that renderctx is valid
  6835. */
  6836. ret = intel_wait_ring_idle(LP_RING(dev_priv));
  6837. if (ret) {
  6838. DRM_ERROR("failed to enable ironlake power power savings\n");
  6839. ironlake_teardown_rc6(dev);
  6840. mutex_unlock(&dev->struct_mutex);
  6841. return;
  6842. }
  6843. I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
  6844. I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
  6845. mutex_unlock(&dev->struct_mutex);
  6846. }
  6847. void intel_init_clock_gating(struct drm_device *dev)
  6848. {
  6849. struct drm_i915_private *dev_priv = dev->dev_private;
  6850. dev_priv->display.init_clock_gating(dev);
  6851. if (dev_priv->display.init_pch_clock_gating)
  6852. dev_priv->display.init_pch_clock_gating(dev);
  6853. }
  6854. /* Set up chip specific display functions */
  6855. static void intel_init_display(struct drm_device *dev)
  6856. {
  6857. struct drm_i915_private *dev_priv = dev->dev_private;
  6858. /* We always want a DPMS function */
  6859. if (HAS_PCH_SPLIT(dev)) {
  6860. dev_priv->display.dpms = ironlake_crtc_dpms;
  6861. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  6862. dev_priv->display.update_plane = ironlake_update_plane;
  6863. } else {
  6864. dev_priv->display.dpms = i9xx_crtc_dpms;
  6865. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  6866. dev_priv->display.update_plane = i9xx_update_plane;
  6867. }
  6868. if (I915_HAS_FBC(dev)) {
  6869. if (HAS_PCH_SPLIT(dev)) {
  6870. dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
  6871. dev_priv->display.enable_fbc = ironlake_enable_fbc;
  6872. dev_priv->display.disable_fbc = ironlake_disable_fbc;
  6873. } else if (IS_GM45(dev)) {
  6874. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  6875. dev_priv->display.enable_fbc = g4x_enable_fbc;
  6876. dev_priv->display.disable_fbc = g4x_disable_fbc;
  6877. } else if (IS_CRESTLINE(dev)) {
  6878. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  6879. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  6880. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  6881. }
  6882. /* 855GM needs testing */
  6883. }
  6884. /* Returns the core display clock speed */
  6885. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  6886. dev_priv->display.get_display_clock_speed =
  6887. i945_get_display_clock_speed;
  6888. else if (IS_I915G(dev))
  6889. dev_priv->display.get_display_clock_speed =
  6890. i915_get_display_clock_speed;
  6891. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  6892. dev_priv->display.get_display_clock_speed =
  6893. i9xx_misc_get_display_clock_speed;
  6894. else if (IS_I915GM(dev))
  6895. dev_priv->display.get_display_clock_speed =
  6896. i915gm_get_display_clock_speed;
  6897. else if (IS_I865G(dev))
  6898. dev_priv->display.get_display_clock_speed =
  6899. i865_get_display_clock_speed;
  6900. else if (IS_I85X(dev))
  6901. dev_priv->display.get_display_clock_speed =
  6902. i855_get_display_clock_speed;
  6903. else /* 852, 830 */
  6904. dev_priv->display.get_display_clock_speed =
  6905. i830_get_display_clock_speed;
  6906. /* For FIFO watermark updates */
  6907. if (HAS_PCH_SPLIT(dev)) {
  6908. if (HAS_PCH_IBX(dev))
  6909. dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
  6910. else if (HAS_PCH_CPT(dev))
  6911. dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
  6912. if (IS_GEN5(dev)) {
  6913. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  6914. dev_priv->display.update_wm = ironlake_update_wm;
  6915. else {
  6916. DRM_DEBUG_KMS("Failed to get proper latency. "
  6917. "Disable CxSR\n");
  6918. dev_priv->display.update_wm = NULL;
  6919. }
  6920. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  6921. dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
  6922. } else if (IS_GEN6(dev)) {
  6923. if (SNB_READ_WM0_LATENCY()) {
  6924. dev_priv->display.update_wm = sandybridge_update_wm;
  6925. } else {
  6926. DRM_DEBUG_KMS("Failed to read display plane latency. "
  6927. "Disable CxSR\n");
  6928. dev_priv->display.update_wm = NULL;
  6929. }
  6930. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  6931. dev_priv->display.init_clock_gating = gen6_init_clock_gating;
  6932. } else if (IS_IVYBRIDGE(dev)) {
  6933. /* FIXME: detect B0+ stepping and use auto training */
  6934. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  6935. if (SNB_READ_WM0_LATENCY()) {
  6936. dev_priv->display.update_wm = sandybridge_update_wm;
  6937. } else {
  6938. DRM_DEBUG_KMS("Failed to read display plane latency. "
  6939. "Disable CxSR\n");
  6940. dev_priv->display.update_wm = NULL;
  6941. }
  6942. dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
  6943. } else
  6944. dev_priv->display.update_wm = NULL;
  6945. } else if (IS_PINEVIEW(dev)) {
  6946. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  6947. dev_priv->is_ddr3,
  6948. dev_priv->fsb_freq,
  6949. dev_priv->mem_freq)) {
  6950. DRM_INFO("failed to find known CxSR latency "
  6951. "(found ddr%s fsb freq %d, mem freq %d), "
  6952. "disabling CxSR\n",
  6953. (dev_priv->is_ddr3 == 1) ? "3": "2",
  6954. dev_priv->fsb_freq, dev_priv->mem_freq);
  6955. /* Disable CxSR and never update its watermark again */
  6956. pineview_disable_cxsr(dev);
  6957. dev_priv->display.update_wm = NULL;
  6958. } else
  6959. dev_priv->display.update_wm = pineview_update_wm;
  6960. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  6961. } else if (IS_G4X(dev)) {
  6962. dev_priv->display.update_wm = g4x_update_wm;
  6963. dev_priv->display.init_clock_gating = g4x_init_clock_gating;
  6964. } else if (IS_GEN4(dev)) {
  6965. dev_priv->display.update_wm = i965_update_wm;
  6966. if (IS_CRESTLINE(dev))
  6967. dev_priv->display.init_clock_gating = crestline_init_clock_gating;
  6968. else if (IS_BROADWATER(dev))
  6969. dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
  6970. } else if (IS_GEN3(dev)) {
  6971. dev_priv->display.update_wm = i9xx_update_wm;
  6972. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  6973. dev_priv->display.init_clock_gating = gen3_init_clock_gating;
  6974. } else if (IS_I865G(dev)) {
  6975. dev_priv->display.update_wm = i830_update_wm;
  6976. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  6977. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  6978. } else if (IS_I85X(dev)) {
  6979. dev_priv->display.update_wm = i9xx_update_wm;
  6980. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  6981. dev_priv->display.init_clock_gating = i85x_init_clock_gating;
  6982. } else {
  6983. dev_priv->display.update_wm = i830_update_wm;
  6984. dev_priv->display.init_clock_gating = i830_init_clock_gating;
  6985. if (IS_845G(dev))
  6986. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  6987. else
  6988. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  6989. }
  6990. /* Default just returns -ENODEV to indicate unsupported */
  6991. dev_priv->display.queue_flip = intel_default_queue_flip;
  6992. switch (INTEL_INFO(dev)->gen) {
  6993. case 2:
  6994. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  6995. break;
  6996. case 3:
  6997. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  6998. break;
  6999. case 4:
  7000. case 5:
  7001. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  7002. break;
  7003. case 6:
  7004. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  7005. break;
  7006. case 7:
  7007. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  7008. break;
  7009. }
  7010. }
  7011. /*
  7012. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  7013. * resume, or other times. This quirk makes sure that's the case for
  7014. * affected systems.
  7015. */
  7016. static void quirk_pipea_force (struct drm_device *dev)
  7017. {
  7018. struct drm_i915_private *dev_priv = dev->dev_private;
  7019. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  7020. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  7021. }
  7022. /*
  7023. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  7024. */
  7025. static void quirk_ssc_force_disable(struct drm_device *dev)
  7026. {
  7027. struct drm_i915_private *dev_priv = dev->dev_private;
  7028. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  7029. }
  7030. struct intel_quirk {
  7031. int device;
  7032. int subsystem_vendor;
  7033. int subsystem_device;
  7034. void (*hook)(struct drm_device *dev);
  7035. };
  7036. struct intel_quirk intel_quirks[] = {
  7037. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  7038. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  7039. /* HP Mini needs pipe A force quirk (LP: #322104) */
  7040. { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
  7041. /* Thinkpad R31 needs pipe A force quirk */
  7042. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  7043. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  7044. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  7045. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  7046. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  7047. /* ThinkPad X40 needs pipe A force quirk */
  7048. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  7049. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  7050. /* 855 & before need to leave pipe A & dpll A up */
  7051. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7052. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7053. /* Lenovo U160 cannot use SSC on LVDS */
  7054. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  7055. /* Sony Vaio Y cannot use SSC on LVDS */
  7056. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  7057. };
  7058. static void intel_init_quirks(struct drm_device *dev)
  7059. {
  7060. struct pci_dev *d = dev->pdev;
  7061. int i;
  7062. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  7063. struct intel_quirk *q = &intel_quirks[i];
  7064. if (d->device == q->device &&
  7065. (d->subsystem_vendor == q->subsystem_vendor ||
  7066. q->subsystem_vendor == PCI_ANY_ID) &&
  7067. (d->subsystem_device == q->subsystem_device ||
  7068. q->subsystem_device == PCI_ANY_ID))
  7069. q->hook(dev);
  7070. }
  7071. }
  7072. /* Disable the VGA plane that we never use */
  7073. static void i915_disable_vga(struct drm_device *dev)
  7074. {
  7075. struct drm_i915_private *dev_priv = dev->dev_private;
  7076. u8 sr1;
  7077. u32 vga_reg;
  7078. if (HAS_PCH_SPLIT(dev))
  7079. vga_reg = CPU_VGACNTRL;
  7080. else
  7081. vga_reg = VGACNTRL;
  7082. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  7083. outb(1, VGA_SR_INDEX);
  7084. sr1 = inb(VGA_SR_DATA);
  7085. outb(sr1 | 1<<5, VGA_SR_DATA);
  7086. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  7087. udelay(300);
  7088. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  7089. POSTING_READ(vga_reg);
  7090. }
  7091. void intel_modeset_init(struct drm_device *dev)
  7092. {
  7093. struct drm_i915_private *dev_priv = dev->dev_private;
  7094. int i;
  7095. drm_mode_config_init(dev);
  7096. dev->mode_config.min_width = 0;
  7097. dev->mode_config.min_height = 0;
  7098. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  7099. intel_init_quirks(dev);
  7100. intel_init_display(dev);
  7101. if (IS_GEN2(dev)) {
  7102. dev->mode_config.max_width = 2048;
  7103. dev->mode_config.max_height = 2048;
  7104. } else if (IS_GEN3(dev)) {
  7105. dev->mode_config.max_width = 4096;
  7106. dev->mode_config.max_height = 4096;
  7107. } else {
  7108. dev->mode_config.max_width = 8192;
  7109. dev->mode_config.max_height = 8192;
  7110. }
  7111. dev->mode_config.fb_base = dev->agp->base;
  7112. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  7113. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  7114. for (i = 0; i < dev_priv->num_pipe; i++) {
  7115. intel_crtc_init(dev, i);
  7116. }
  7117. /* Just disable it once at startup */
  7118. i915_disable_vga(dev);
  7119. intel_setup_outputs(dev);
  7120. intel_init_clock_gating(dev);
  7121. if (IS_IRONLAKE_M(dev)) {
  7122. ironlake_enable_drps(dev);
  7123. intel_init_emon(dev);
  7124. }
  7125. if (IS_GEN6(dev) || IS_GEN7(dev)) {
  7126. gen6_enable_rps(dev_priv);
  7127. gen6_update_ring_freq(dev_priv);
  7128. }
  7129. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  7130. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  7131. (unsigned long)dev);
  7132. }
  7133. void intel_modeset_gem_init(struct drm_device *dev)
  7134. {
  7135. if (IS_IRONLAKE_M(dev))
  7136. ironlake_enable_rc6(dev);
  7137. intel_setup_overlay(dev);
  7138. }
  7139. void intel_modeset_cleanup(struct drm_device *dev)
  7140. {
  7141. struct drm_i915_private *dev_priv = dev->dev_private;
  7142. struct drm_crtc *crtc;
  7143. struct intel_crtc *intel_crtc;
  7144. drm_kms_helper_poll_fini(dev);
  7145. mutex_lock(&dev->struct_mutex);
  7146. intel_unregister_dsm_handler();
  7147. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  7148. /* Skip inactive CRTCs */
  7149. if (!crtc->fb)
  7150. continue;
  7151. intel_crtc = to_intel_crtc(crtc);
  7152. intel_increase_pllclock(crtc);
  7153. }
  7154. intel_disable_fbc(dev);
  7155. if (IS_IRONLAKE_M(dev))
  7156. ironlake_disable_drps(dev);
  7157. if (IS_GEN6(dev) || IS_GEN7(dev))
  7158. gen6_disable_rps(dev);
  7159. if (IS_IRONLAKE_M(dev))
  7160. ironlake_disable_rc6(dev);
  7161. mutex_unlock(&dev->struct_mutex);
  7162. /* Disable the irq before mode object teardown, for the irq might
  7163. * enqueue unpin/hotplug work. */
  7164. drm_irq_uninstall(dev);
  7165. cancel_work_sync(&dev_priv->hotplug_work);
  7166. /* flush any delayed tasks or pending work */
  7167. flush_scheduled_work();
  7168. /* Shut off idle work before the crtcs get freed. */
  7169. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  7170. intel_crtc = to_intel_crtc(crtc);
  7171. del_timer_sync(&intel_crtc->idle_timer);
  7172. }
  7173. del_timer_sync(&dev_priv->idle_timer);
  7174. cancel_work_sync(&dev_priv->idle_work);
  7175. drm_mode_config_cleanup(dev);
  7176. }
  7177. /*
  7178. * Return which encoder is currently attached for connector.
  7179. */
  7180. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  7181. {
  7182. return &intel_attached_encoder(connector)->base;
  7183. }
  7184. void intel_connector_attach_encoder(struct intel_connector *connector,
  7185. struct intel_encoder *encoder)
  7186. {
  7187. connector->encoder = encoder;
  7188. drm_mode_connector_attach_encoder(&connector->base,
  7189. &encoder->base);
  7190. }
  7191. /*
  7192. * set vga decode state - true == enable VGA decode
  7193. */
  7194. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  7195. {
  7196. struct drm_i915_private *dev_priv = dev->dev_private;
  7197. u16 gmch_ctrl;
  7198. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  7199. if (state)
  7200. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  7201. else
  7202. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  7203. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  7204. return 0;
  7205. }
  7206. #ifdef CONFIG_DEBUG_FS
  7207. #include <linux/seq_file.h>
  7208. struct intel_display_error_state {
  7209. struct intel_cursor_error_state {
  7210. u32 control;
  7211. u32 position;
  7212. u32 base;
  7213. u32 size;
  7214. } cursor[2];
  7215. struct intel_pipe_error_state {
  7216. u32 conf;
  7217. u32 source;
  7218. u32 htotal;
  7219. u32 hblank;
  7220. u32 hsync;
  7221. u32 vtotal;
  7222. u32 vblank;
  7223. u32 vsync;
  7224. } pipe[2];
  7225. struct intel_plane_error_state {
  7226. u32 control;
  7227. u32 stride;
  7228. u32 size;
  7229. u32 pos;
  7230. u32 addr;
  7231. u32 surface;
  7232. u32 tile_offset;
  7233. } plane[2];
  7234. };
  7235. struct intel_display_error_state *
  7236. intel_display_capture_error_state(struct drm_device *dev)
  7237. {
  7238. drm_i915_private_t *dev_priv = dev->dev_private;
  7239. struct intel_display_error_state *error;
  7240. int i;
  7241. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  7242. if (error == NULL)
  7243. return NULL;
  7244. for (i = 0; i < 2; i++) {
  7245. error->cursor[i].control = I915_READ(CURCNTR(i));
  7246. error->cursor[i].position = I915_READ(CURPOS(i));
  7247. error->cursor[i].base = I915_READ(CURBASE(i));
  7248. error->plane[i].control = I915_READ(DSPCNTR(i));
  7249. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  7250. error->plane[i].size = I915_READ(DSPSIZE(i));
  7251. error->plane[i].pos= I915_READ(DSPPOS(i));
  7252. error->plane[i].addr = I915_READ(DSPADDR(i));
  7253. if (INTEL_INFO(dev)->gen >= 4) {
  7254. error->plane[i].surface = I915_READ(DSPSURF(i));
  7255. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  7256. }
  7257. error->pipe[i].conf = I915_READ(PIPECONF(i));
  7258. error->pipe[i].source = I915_READ(PIPESRC(i));
  7259. error->pipe[i].htotal = I915_READ(HTOTAL(i));
  7260. error->pipe[i].hblank = I915_READ(HBLANK(i));
  7261. error->pipe[i].hsync = I915_READ(HSYNC(i));
  7262. error->pipe[i].vtotal = I915_READ(VTOTAL(i));
  7263. error->pipe[i].vblank = I915_READ(VBLANK(i));
  7264. error->pipe[i].vsync = I915_READ(VSYNC(i));
  7265. }
  7266. return error;
  7267. }
  7268. void
  7269. intel_display_print_error_state(struct seq_file *m,
  7270. struct drm_device *dev,
  7271. struct intel_display_error_state *error)
  7272. {
  7273. int i;
  7274. for (i = 0; i < 2; i++) {
  7275. seq_printf(m, "Pipe [%d]:\n", i);
  7276. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  7277. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  7278. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  7279. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  7280. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  7281. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  7282. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  7283. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  7284. seq_printf(m, "Plane [%d]:\n", i);
  7285. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  7286. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  7287. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  7288. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  7289. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  7290. if (INTEL_INFO(dev)->gen >= 4) {
  7291. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  7292. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  7293. }
  7294. seq_printf(m, "Cursor [%d]:\n", i);
  7295. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  7296. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  7297. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  7298. }
  7299. }
  7300. #endif