intel_display.c 250 KB

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