intel_display.c 345 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562
  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/dmi.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 <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include <drm/i915_drm.h>
  37. #include "i915_drv.h"
  38. #include "i915_trace.h"
  39. #include <drm/drm_dp_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <linux/dma_remapping.h>
  42. #define DIV_ROUND_CLOSEST_ULL(ll, d) \
  43. ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
  44. static void intel_increase_pllclock(struct drm_crtc *crtc);
  45. static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
  46. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  47. struct intel_crtc_config *pipe_config);
  48. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  49. struct intel_crtc_config *pipe_config);
  50. static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
  51. int x, int y, struct drm_framebuffer *old_fb);
  52. static int intel_framebuffer_init(struct drm_device *dev,
  53. struct intel_framebuffer *ifb,
  54. struct drm_mode_fb_cmd2 *mode_cmd,
  55. struct drm_i915_gem_object *obj);
  56. static void intel_dp_set_m_n(struct intel_crtc *crtc);
  57. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  58. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  59. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  60. struct intel_link_m_n *m_n);
  61. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  62. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  63. static void intel_set_pipe_csc(struct drm_crtc *crtc);
  64. static void vlv_prepare_pll(struct intel_crtc *crtc);
  65. typedef struct {
  66. int min, max;
  67. } intel_range_t;
  68. typedef struct {
  69. int dot_limit;
  70. int p2_slow, p2_fast;
  71. } intel_p2_t;
  72. typedef struct intel_limit intel_limit_t;
  73. struct intel_limit {
  74. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  75. intel_p2_t p2;
  76. };
  77. int
  78. intel_pch_rawclk(struct drm_device *dev)
  79. {
  80. struct drm_i915_private *dev_priv = dev->dev_private;
  81. WARN_ON(!HAS_PCH_SPLIT(dev));
  82. return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
  83. }
  84. static inline u32 /* units of 100MHz */
  85. intel_fdi_link_freq(struct drm_device *dev)
  86. {
  87. if (IS_GEN5(dev)) {
  88. struct drm_i915_private *dev_priv = dev->dev_private;
  89. return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
  90. } else
  91. return 27;
  92. }
  93. static const intel_limit_t intel_limits_i8xx_dac = {
  94. .dot = { .min = 25000, .max = 350000 },
  95. .vco = { .min = 908000, .max = 1512000 },
  96. .n = { .min = 2, .max = 16 },
  97. .m = { .min = 96, .max = 140 },
  98. .m1 = { .min = 18, .max = 26 },
  99. .m2 = { .min = 6, .max = 16 },
  100. .p = { .min = 4, .max = 128 },
  101. .p1 = { .min = 2, .max = 33 },
  102. .p2 = { .dot_limit = 165000,
  103. .p2_slow = 4, .p2_fast = 2 },
  104. };
  105. static const intel_limit_t intel_limits_i8xx_dvo = {
  106. .dot = { .min = 25000, .max = 350000 },
  107. .vco = { .min = 908000, .max = 1512000 },
  108. .n = { .min = 2, .max = 16 },
  109. .m = { .min = 96, .max = 140 },
  110. .m1 = { .min = 18, .max = 26 },
  111. .m2 = { .min = 6, .max = 16 },
  112. .p = { .min = 4, .max = 128 },
  113. .p1 = { .min = 2, .max = 33 },
  114. .p2 = { .dot_limit = 165000,
  115. .p2_slow = 4, .p2_fast = 4 },
  116. };
  117. static const intel_limit_t intel_limits_i8xx_lvds = {
  118. .dot = { .min = 25000, .max = 350000 },
  119. .vco = { .min = 908000, .max = 1512000 },
  120. .n = { .min = 2, .max = 16 },
  121. .m = { .min = 96, .max = 140 },
  122. .m1 = { .min = 18, .max = 26 },
  123. .m2 = { .min = 6, .max = 16 },
  124. .p = { .min = 4, .max = 128 },
  125. .p1 = { .min = 1, .max = 6 },
  126. .p2 = { .dot_limit = 165000,
  127. .p2_slow = 14, .p2_fast = 7 },
  128. };
  129. static const intel_limit_t intel_limits_i9xx_sdvo = {
  130. .dot = { .min = 20000, .max = 400000 },
  131. .vco = { .min = 1400000, .max = 2800000 },
  132. .n = { .min = 1, .max = 6 },
  133. .m = { .min = 70, .max = 120 },
  134. .m1 = { .min = 8, .max = 18 },
  135. .m2 = { .min = 3, .max = 7 },
  136. .p = { .min = 5, .max = 80 },
  137. .p1 = { .min = 1, .max = 8 },
  138. .p2 = { .dot_limit = 200000,
  139. .p2_slow = 10, .p2_fast = 5 },
  140. };
  141. static const intel_limit_t intel_limits_i9xx_lvds = {
  142. .dot = { .min = 20000, .max = 400000 },
  143. .vco = { .min = 1400000, .max = 2800000 },
  144. .n = { .min = 1, .max = 6 },
  145. .m = { .min = 70, .max = 120 },
  146. .m1 = { .min = 8, .max = 18 },
  147. .m2 = { .min = 3, .max = 7 },
  148. .p = { .min = 7, .max = 98 },
  149. .p1 = { .min = 1, .max = 8 },
  150. .p2 = { .dot_limit = 112000,
  151. .p2_slow = 14, .p2_fast = 7 },
  152. };
  153. static const intel_limit_t intel_limits_g4x_sdvo = {
  154. .dot = { .min = 25000, .max = 270000 },
  155. .vco = { .min = 1750000, .max = 3500000},
  156. .n = { .min = 1, .max = 4 },
  157. .m = { .min = 104, .max = 138 },
  158. .m1 = { .min = 17, .max = 23 },
  159. .m2 = { .min = 5, .max = 11 },
  160. .p = { .min = 10, .max = 30 },
  161. .p1 = { .min = 1, .max = 3},
  162. .p2 = { .dot_limit = 270000,
  163. .p2_slow = 10,
  164. .p2_fast = 10
  165. },
  166. };
  167. static const intel_limit_t intel_limits_g4x_hdmi = {
  168. .dot = { .min = 22000, .max = 400000 },
  169. .vco = { .min = 1750000, .max = 3500000},
  170. .n = { .min = 1, .max = 4 },
  171. .m = { .min = 104, .max = 138 },
  172. .m1 = { .min = 16, .max = 23 },
  173. .m2 = { .min = 5, .max = 11 },
  174. .p = { .min = 5, .max = 80 },
  175. .p1 = { .min = 1, .max = 8},
  176. .p2 = { .dot_limit = 165000,
  177. .p2_slow = 10, .p2_fast = 5 },
  178. };
  179. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  180. .dot = { .min = 20000, .max = 115000 },
  181. .vco = { .min = 1750000, .max = 3500000 },
  182. .n = { .min = 1, .max = 3 },
  183. .m = { .min = 104, .max = 138 },
  184. .m1 = { .min = 17, .max = 23 },
  185. .m2 = { .min = 5, .max = 11 },
  186. .p = { .min = 28, .max = 112 },
  187. .p1 = { .min = 2, .max = 8 },
  188. .p2 = { .dot_limit = 0,
  189. .p2_slow = 14, .p2_fast = 14
  190. },
  191. };
  192. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  193. .dot = { .min = 80000, .max = 224000 },
  194. .vco = { .min = 1750000, .max = 3500000 },
  195. .n = { .min = 1, .max = 3 },
  196. .m = { .min = 104, .max = 138 },
  197. .m1 = { .min = 17, .max = 23 },
  198. .m2 = { .min = 5, .max = 11 },
  199. .p = { .min = 14, .max = 42 },
  200. .p1 = { .min = 2, .max = 6 },
  201. .p2 = { .dot_limit = 0,
  202. .p2_slow = 7, .p2_fast = 7
  203. },
  204. };
  205. static const intel_limit_t intel_limits_pineview_sdvo = {
  206. .dot = { .min = 20000, .max = 400000},
  207. .vco = { .min = 1700000, .max = 3500000 },
  208. /* Pineview's Ncounter is a ring counter */
  209. .n = { .min = 3, .max = 6 },
  210. .m = { .min = 2, .max = 256 },
  211. /* Pineview only has one combined m divider, which we treat as m2. */
  212. .m1 = { .min = 0, .max = 0 },
  213. .m2 = { .min = 0, .max = 254 },
  214. .p = { .min = 5, .max = 80 },
  215. .p1 = { .min = 1, .max = 8 },
  216. .p2 = { .dot_limit = 200000,
  217. .p2_slow = 10, .p2_fast = 5 },
  218. };
  219. static const intel_limit_t intel_limits_pineview_lvds = {
  220. .dot = { .min = 20000, .max = 400000 },
  221. .vco = { .min = 1700000, .max = 3500000 },
  222. .n = { .min = 3, .max = 6 },
  223. .m = { .min = 2, .max = 256 },
  224. .m1 = { .min = 0, .max = 0 },
  225. .m2 = { .min = 0, .max = 254 },
  226. .p = { .min = 7, .max = 112 },
  227. .p1 = { .min = 1, .max = 8 },
  228. .p2 = { .dot_limit = 112000,
  229. .p2_slow = 14, .p2_fast = 14 },
  230. };
  231. /* Ironlake / Sandybridge
  232. *
  233. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  234. * the range value for them is (actual_value - 2).
  235. */
  236. static const intel_limit_t intel_limits_ironlake_dac = {
  237. .dot = { .min = 25000, .max = 350000 },
  238. .vco = { .min = 1760000, .max = 3510000 },
  239. .n = { .min = 1, .max = 5 },
  240. .m = { .min = 79, .max = 127 },
  241. .m1 = { .min = 12, .max = 22 },
  242. .m2 = { .min = 5, .max = 9 },
  243. .p = { .min = 5, .max = 80 },
  244. .p1 = { .min = 1, .max = 8 },
  245. .p2 = { .dot_limit = 225000,
  246. .p2_slow = 10, .p2_fast = 5 },
  247. };
  248. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  249. .dot = { .min = 25000, .max = 350000 },
  250. .vco = { .min = 1760000, .max = 3510000 },
  251. .n = { .min = 1, .max = 3 },
  252. .m = { .min = 79, .max = 118 },
  253. .m1 = { .min = 12, .max = 22 },
  254. .m2 = { .min = 5, .max = 9 },
  255. .p = { .min = 28, .max = 112 },
  256. .p1 = { .min = 2, .max = 8 },
  257. .p2 = { .dot_limit = 225000,
  258. .p2_slow = 14, .p2_fast = 14 },
  259. };
  260. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  261. .dot = { .min = 25000, .max = 350000 },
  262. .vco = { .min = 1760000, .max = 3510000 },
  263. .n = { .min = 1, .max = 3 },
  264. .m = { .min = 79, .max = 127 },
  265. .m1 = { .min = 12, .max = 22 },
  266. .m2 = { .min = 5, .max = 9 },
  267. .p = { .min = 14, .max = 56 },
  268. .p1 = { .min = 2, .max = 8 },
  269. .p2 = { .dot_limit = 225000,
  270. .p2_slow = 7, .p2_fast = 7 },
  271. };
  272. /* LVDS 100mhz refclk limits. */
  273. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  274. .dot = { .min = 25000, .max = 350000 },
  275. .vco = { .min = 1760000, .max = 3510000 },
  276. .n = { .min = 1, .max = 2 },
  277. .m = { .min = 79, .max = 126 },
  278. .m1 = { .min = 12, .max = 22 },
  279. .m2 = { .min = 5, .max = 9 },
  280. .p = { .min = 28, .max = 112 },
  281. .p1 = { .min = 2, .max = 8 },
  282. .p2 = { .dot_limit = 225000,
  283. .p2_slow = 14, .p2_fast = 14 },
  284. };
  285. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  286. .dot = { .min = 25000, .max = 350000 },
  287. .vco = { .min = 1760000, .max = 3510000 },
  288. .n = { .min = 1, .max = 3 },
  289. .m = { .min = 79, .max = 126 },
  290. .m1 = { .min = 12, .max = 22 },
  291. .m2 = { .min = 5, .max = 9 },
  292. .p = { .min = 14, .max = 42 },
  293. .p1 = { .min = 2, .max = 6 },
  294. .p2 = { .dot_limit = 225000,
  295. .p2_slow = 7, .p2_fast = 7 },
  296. };
  297. static const intel_limit_t intel_limits_vlv = {
  298. /*
  299. * These are the data rate limits (measured in fast clocks)
  300. * since those are the strictest limits we have. The fast
  301. * clock and actual rate limits are more relaxed, so checking
  302. * them would make no difference.
  303. */
  304. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  305. .vco = { .min = 4000000, .max = 6000000 },
  306. .n = { .min = 1, .max = 7 },
  307. .m1 = { .min = 2, .max = 3 },
  308. .m2 = { .min = 11, .max = 156 },
  309. .p1 = { .min = 2, .max = 3 },
  310. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  311. };
  312. static const intel_limit_t intel_limits_chv = {
  313. /*
  314. * These are the data rate limits (measured in fast clocks)
  315. * since those are the strictest limits we have. The fast
  316. * clock and actual rate limits are more relaxed, so checking
  317. * them would make no difference.
  318. */
  319. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  320. .vco = { .min = 4860000, .max = 6700000 },
  321. .n = { .min = 1, .max = 1 },
  322. .m1 = { .min = 2, .max = 2 },
  323. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  324. .p1 = { .min = 2, .max = 4 },
  325. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  326. };
  327. static void vlv_clock(int refclk, intel_clock_t *clock)
  328. {
  329. clock->m = clock->m1 * clock->m2;
  330. clock->p = clock->p1 * clock->p2;
  331. if (WARN_ON(clock->n == 0 || clock->p == 0))
  332. return;
  333. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  334. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  335. }
  336. /**
  337. * Returns whether any output on the specified pipe is of the specified type
  338. */
  339. static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
  340. {
  341. struct drm_device *dev = crtc->dev;
  342. struct intel_encoder *encoder;
  343. for_each_encoder_on_crtc(dev, crtc, encoder)
  344. if (encoder->type == type)
  345. return true;
  346. return false;
  347. }
  348. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
  349. int refclk)
  350. {
  351. struct drm_device *dev = crtc->dev;
  352. const intel_limit_t *limit;
  353. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  354. if (intel_is_dual_link_lvds(dev)) {
  355. if (refclk == 100000)
  356. limit = &intel_limits_ironlake_dual_lvds_100m;
  357. else
  358. limit = &intel_limits_ironlake_dual_lvds;
  359. } else {
  360. if (refclk == 100000)
  361. limit = &intel_limits_ironlake_single_lvds_100m;
  362. else
  363. limit = &intel_limits_ironlake_single_lvds;
  364. }
  365. } else
  366. limit = &intel_limits_ironlake_dac;
  367. return limit;
  368. }
  369. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  370. {
  371. struct drm_device *dev = crtc->dev;
  372. const intel_limit_t *limit;
  373. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  374. if (intel_is_dual_link_lvds(dev))
  375. limit = &intel_limits_g4x_dual_channel_lvds;
  376. else
  377. limit = &intel_limits_g4x_single_channel_lvds;
  378. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  379. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  380. limit = &intel_limits_g4x_hdmi;
  381. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  382. limit = &intel_limits_g4x_sdvo;
  383. } else /* The option is for other outputs */
  384. limit = &intel_limits_i9xx_sdvo;
  385. return limit;
  386. }
  387. static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
  388. {
  389. struct drm_device *dev = crtc->dev;
  390. const intel_limit_t *limit;
  391. if (HAS_PCH_SPLIT(dev))
  392. limit = intel_ironlake_limit(crtc, refclk);
  393. else if (IS_G4X(dev)) {
  394. limit = intel_g4x_limit(crtc);
  395. } else if (IS_PINEVIEW(dev)) {
  396. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  397. limit = &intel_limits_pineview_lvds;
  398. else
  399. limit = &intel_limits_pineview_sdvo;
  400. } else if (IS_CHERRYVIEW(dev)) {
  401. limit = &intel_limits_chv;
  402. } else if (IS_VALLEYVIEW(dev)) {
  403. limit = &intel_limits_vlv;
  404. } else if (!IS_GEN2(dev)) {
  405. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  406. limit = &intel_limits_i9xx_lvds;
  407. else
  408. limit = &intel_limits_i9xx_sdvo;
  409. } else {
  410. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  411. limit = &intel_limits_i8xx_lvds;
  412. else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
  413. limit = &intel_limits_i8xx_dvo;
  414. else
  415. limit = &intel_limits_i8xx_dac;
  416. }
  417. return limit;
  418. }
  419. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  420. static void pineview_clock(int refclk, intel_clock_t *clock)
  421. {
  422. clock->m = clock->m2 + 2;
  423. clock->p = clock->p1 * clock->p2;
  424. if (WARN_ON(clock->n == 0 || clock->p == 0))
  425. return;
  426. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  427. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  428. }
  429. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  430. {
  431. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  432. }
  433. static void i9xx_clock(int refclk, intel_clock_t *clock)
  434. {
  435. clock->m = i9xx_dpll_compute_m(clock);
  436. clock->p = clock->p1 * clock->p2;
  437. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  438. return;
  439. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  440. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  441. }
  442. static void chv_clock(int refclk, intel_clock_t *clock)
  443. {
  444. clock->m = clock->m1 * clock->m2;
  445. clock->p = clock->p1 * clock->p2;
  446. if (WARN_ON(clock->n == 0 || clock->p == 0))
  447. return;
  448. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  449. clock->n << 22);
  450. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  451. }
  452. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  453. /**
  454. * Returns whether the given set of divisors are valid for a given refclk with
  455. * the given connectors.
  456. */
  457. static bool intel_PLL_is_valid(struct drm_device *dev,
  458. const intel_limit_t *limit,
  459. const intel_clock_t *clock)
  460. {
  461. if (clock->n < limit->n.min || limit->n.max < clock->n)
  462. INTELPllInvalid("n out of range\n");
  463. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  464. INTELPllInvalid("p1 out of range\n");
  465. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  466. INTELPllInvalid("m2 out of range\n");
  467. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  468. INTELPllInvalid("m1 out of range\n");
  469. if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
  470. if (clock->m1 <= clock->m2)
  471. INTELPllInvalid("m1 <= m2\n");
  472. if (!IS_VALLEYVIEW(dev)) {
  473. if (clock->p < limit->p.min || limit->p.max < clock->p)
  474. INTELPllInvalid("p out of range\n");
  475. if (clock->m < limit->m.min || limit->m.max < clock->m)
  476. INTELPllInvalid("m out of range\n");
  477. }
  478. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  479. INTELPllInvalid("vco out of range\n");
  480. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  481. * connector, etc., rather than just a single range.
  482. */
  483. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  484. INTELPllInvalid("dot out of range\n");
  485. return true;
  486. }
  487. static bool
  488. i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  489. int target, int refclk, intel_clock_t *match_clock,
  490. intel_clock_t *best_clock)
  491. {
  492. struct drm_device *dev = crtc->dev;
  493. intel_clock_t clock;
  494. int err = target;
  495. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  496. /*
  497. * For LVDS just rely on its current settings for dual-channel.
  498. * We haven't figured out how to reliably set up different
  499. * single/dual channel state, if we even can.
  500. */
  501. if (intel_is_dual_link_lvds(dev))
  502. clock.p2 = limit->p2.p2_fast;
  503. else
  504. clock.p2 = limit->p2.p2_slow;
  505. } else {
  506. if (target < limit->p2.dot_limit)
  507. clock.p2 = limit->p2.p2_slow;
  508. else
  509. clock.p2 = limit->p2.p2_fast;
  510. }
  511. memset(best_clock, 0, sizeof(*best_clock));
  512. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  513. clock.m1++) {
  514. for (clock.m2 = limit->m2.min;
  515. clock.m2 <= limit->m2.max; clock.m2++) {
  516. if (clock.m2 >= clock.m1)
  517. break;
  518. for (clock.n = limit->n.min;
  519. clock.n <= limit->n.max; clock.n++) {
  520. for (clock.p1 = limit->p1.min;
  521. clock.p1 <= limit->p1.max; clock.p1++) {
  522. int this_err;
  523. i9xx_clock(refclk, &clock);
  524. if (!intel_PLL_is_valid(dev, limit,
  525. &clock))
  526. continue;
  527. if (match_clock &&
  528. clock.p != match_clock->p)
  529. continue;
  530. this_err = abs(clock.dot - target);
  531. if (this_err < err) {
  532. *best_clock = clock;
  533. err = this_err;
  534. }
  535. }
  536. }
  537. }
  538. }
  539. return (err != target);
  540. }
  541. static bool
  542. pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  543. int target, int refclk, intel_clock_t *match_clock,
  544. intel_clock_t *best_clock)
  545. {
  546. struct drm_device *dev = crtc->dev;
  547. intel_clock_t clock;
  548. int err = target;
  549. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  550. /*
  551. * For LVDS just rely on its current settings for dual-channel.
  552. * We haven't figured out how to reliably set up different
  553. * single/dual channel state, if we even can.
  554. */
  555. if (intel_is_dual_link_lvds(dev))
  556. clock.p2 = limit->p2.p2_fast;
  557. else
  558. clock.p2 = limit->p2.p2_slow;
  559. } else {
  560. if (target < limit->p2.dot_limit)
  561. clock.p2 = limit->p2.p2_slow;
  562. else
  563. clock.p2 = limit->p2.p2_fast;
  564. }
  565. memset(best_clock, 0, sizeof(*best_clock));
  566. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  567. clock.m1++) {
  568. for (clock.m2 = limit->m2.min;
  569. clock.m2 <= limit->m2.max; clock.m2++) {
  570. for (clock.n = limit->n.min;
  571. clock.n <= limit->n.max; clock.n++) {
  572. for (clock.p1 = limit->p1.min;
  573. clock.p1 <= limit->p1.max; clock.p1++) {
  574. int this_err;
  575. pineview_clock(refclk, &clock);
  576. if (!intel_PLL_is_valid(dev, limit,
  577. &clock))
  578. continue;
  579. if (match_clock &&
  580. clock.p != match_clock->p)
  581. continue;
  582. this_err = abs(clock.dot - target);
  583. if (this_err < err) {
  584. *best_clock = clock;
  585. err = this_err;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. return (err != target);
  592. }
  593. static bool
  594. g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  595. int target, int refclk, intel_clock_t *match_clock,
  596. intel_clock_t *best_clock)
  597. {
  598. struct drm_device *dev = crtc->dev;
  599. intel_clock_t clock;
  600. int max_n;
  601. bool found;
  602. /* approximately equals target * 0.00585 */
  603. int err_most = (target >> 8) + (target >> 9);
  604. found = false;
  605. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  606. if (intel_is_dual_link_lvds(dev))
  607. clock.p2 = limit->p2.p2_fast;
  608. else
  609. clock.p2 = limit->p2.p2_slow;
  610. } else {
  611. if (target < limit->p2.dot_limit)
  612. clock.p2 = limit->p2.p2_slow;
  613. else
  614. clock.p2 = limit->p2.p2_fast;
  615. }
  616. memset(best_clock, 0, sizeof(*best_clock));
  617. max_n = limit->n.max;
  618. /* based on hardware requirement, prefer smaller n to precision */
  619. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  620. /* based on hardware requirement, prefere larger m1,m2 */
  621. for (clock.m1 = limit->m1.max;
  622. clock.m1 >= limit->m1.min; clock.m1--) {
  623. for (clock.m2 = limit->m2.max;
  624. clock.m2 >= limit->m2.min; clock.m2--) {
  625. for (clock.p1 = limit->p1.max;
  626. clock.p1 >= limit->p1.min; clock.p1--) {
  627. int this_err;
  628. i9xx_clock(refclk, &clock);
  629. if (!intel_PLL_is_valid(dev, limit,
  630. &clock))
  631. continue;
  632. this_err = abs(clock.dot - target);
  633. if (this_err < err_most) {
  634. *best_clock = clock;
  635. err_most = this_err;
  636. max_n = clock.n;
  637. found = true;
  638. }
  639. }
  640. }
  641. }
  642. }
  643. return found;
  644. }
  645. static bool
  646. vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  647. int target, int refclk, intel_clock_t *match_clock,
  648. intel_clock_t *best_clock)
  649. {
  650. struct drm_device *dev = crtc->dev;
  651. intel_clock_t clock;
  652. unsigned int bestppm = 1000000;
  653. /* min update 19.2 MHz */
  654. int max_n = min(limit->n.max, refclk / 19200);
  655. bool found = false;
  656. target *= 5; /* fast clock */
  657. memset(best_clock, 0, sizeof(*best_clock));
  658. /* based on hardware requirement, prefer smaller n to precision */
  659. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  660. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  661. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  662. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  663. clock.p = clock.p1 * clock.p2;
  664. /* based on hardware requirement, prefer bigger m1,m2 values */
  665. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  666. unsigned int ppm, diff;
  667. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  668. refclk * clock.m1);
  669. vlv_clock(refclk, &clock);
  670. if (!intel_PLL_is_valid(dev, limit,
  671. &clock))
  672. continue;
  673. diff = abs(clock.dot - target);
  674. ppm = div_u64(1000000ULL * diff, target);
  675. if (ppm < 100 && clock.p > best_clock->p) {
  676. bestppm = 0;
  677. *best_clock = clock;
  678. found = true;
  679. }
  680. if (bestppm >= 10 && ppm < bestppm - 10) {
  681. bestppm = ppm;
  682. *best_clock = clock;
  683. found = true;
  684. }
  685. }
  686. }
  687. }
  688. }
  689. return found;
  690. }
  691. static bool
  692. chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
  693. int target, int refclk, intel_clock_t *match_clock,
  694. intel_clock_t *best_clock)
  695. {
  696. struct drm_device *dev = crtc->dev;
  697. intel_clock_t clock;
  698. uint64_t m2;
  699. int found = false;
  700. memset(best_clock, 0, sizeof(*best_clock));
  701. /*
  702. * Based on hardware doc, the n always set to 1, and m1 always
  703. * set to 2. If requires to support 200Mhz refclk, we need to
  704. * revisit this because n may not 1 anymore.
  705. */
  706. clock.n = 1, clock.m1 = 2;
  707. target *= 5; /* fast clock */
  708. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  709. for (clock.p2 = limit->p2.p2_fast;
  710. clock.p2 >= limit->p2.p2_slow;
  711. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  712. clock.p = clock.p1 * clock.p2;
  713. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  714. clock.n) << 22, refclk * clock.m1);
  715. if (m2 > INT_MAX/clock.m1)
  716. continue;
  717. clock.m2 = m2;
  718. chv_clock(refclk, &clock);
  719. if (!intel_PLL_is_valid(dev, limit, &clock))
  720. continue;
  721. /* based on hardware requirement, prefer bigger p
  722. */
  723. if (clock.p > best_clock->p) {
  724. *best_clock = clock;
  725. found = true;
  726. }
  727. }
  728. }
  729. return found;
  730. }
  731. bool intel_crtc_active(struct drm_crtc *crtc)
  732. {
  733. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  734. /* Be paranoid as we can arrive here with only partial
  735. * state retrieved from the hardware during setup.
  736. *
  737. * We can ditch the adjusted_mode.crtc_clock check as soon
  738. * as Haswell has gained clock readout/fastboot support.
  739. *
  740. * We can ditch the crtc->primary->fb check as soon as we can
  741. * properly reconstruct framebuffers.
  742. */
  743. return intel_crtc->active && crtc->primary->fb &&
  744. intel_crtc->config.adjusted_mode.crtc_clock;
  745. }
  746. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  747. enum pipe pipe)
  748. {
  749. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  750. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  751. return intel_crtc->config.cpu_transcoder;
  752. }
  753. static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
  754. {
  755. struct drm_i915_private *dev_priv = dev->dev_private;
  756. u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
  757. frame = I915_READ(frame_reg);
  758. if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
  759. WARN(1, "vblank wait timed out\n");
  760. }
  761. /**
  762. * intel_wait_for_vblank - wait for vblank on a given pipe
  763. * @dev: drm device
  764. * @pipe: pipe to wait for
  765. *
  766. * Wait for vblank to occur on a given pipe. Needed for various bits of
  767. * mode setting code.
  768. */
  769. void intel_wait_for_vblank(struct drm_device *dev, int pipe)
  770. {
  771. struct drm_i915_private *dev_priv = dev->dev_private;
  772. int pipestat_reg = PIPESTAT(pipe);
  773. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
  774. g4x_wait_for_vblank(dev, pipe);
  775. return;
  776. }
  777. /* Clear existing vblank status. Note this will clear any other
  778. * sticky status fields as well.
  779. *
  780. * This races with i915_driver_irq_handler() with the result
  781. * that either function could miss a vblank event. Here it is not
  782. * fatal, as we will either wait upon the next vblank interrupt or
  783. * timeout. Generally speaking intel_wait_for_vblank() is only
  784. * called during modeset at which time the GPU should be idle and
  785. * should *not* be performing page flips and thus not waiting on
  786. * vblanks...
  787. * Currently, the result of us stealing a vblank from the irq
  788. * handler is that a single frame will be skipped during swapbuffers.
  789. */
  790. I915_WRITE(pipestat_reg,
  791. I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
  792. /* Wait for vblank interrupt bit to set */
  793. if (wait_for(I915_READ(pipestat_reg) &
  794. PIPE_VBLANK_INTERRUPT_STATUS,
  795. 50))
  796. DRM_DEBUG_KMS("vblank wait timed out\n");
  797. }
  798. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  799. {
  800. struct drm_i915_private *dev_priv = dev->dev_private;
  801. u32 reg = PIPEDSL(pipe);
  802. u32 line1, line2;
  803. u32 line_mask;
  804. if (IS_GEN2(dev))
  805. line_mask = DSL_LINEMASK_GEN2;
  806. else
  807. line_mask = DSL_LINEMASK_GEN3;
  808. line1 = I915_READ(reg) & line_mask;
  809. mdelay(5);
  810. line2 = I915_READ(reg) & line_mask;
  811. return line1 == line2;
  812. }
  813. /*
  814. * intel_wait_for_pipe_off - wait for pipe to turn off
  815. * @dev: drm device
  816. * @pipe: pipe to wait for
  817. *
  818. * After disabling a pipe, we can't wait for vblank in the usual way,
  819. * spinning on the vblank interrupt status bit, since we won't actually
  820. * see an interrupt when the pipe is disabled.
  821. *
  822. * On Gen4 and above:
  823. * wait for the pipe register state bit to turn off
  824. *
  825. * Otherwise:
  826. * wait for the display line value to settle (it usually
  827. * ends up stopping at the start of the next frame).
  828. *
  829. */
  830. void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
  831. {
  832. struct drm_i915_private *dev_priv = dev->dev_private;
  833. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  834. pipe);
  835. if (INTEL_INFO(dev)->gen >= 4) {
  836. int reg = PIPECONF(cpu_transcoder);
  837. /* Wait for the Pipe State to go off */
  838. if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
  839. 100))
  840. WARN(1, "pipe_off wait timed out\n");
  841. } else {
  842. /* Wait for the display line to settle */
  843. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  844. WARN(1, "pipe_off wait timed out\n");
  845. }
  846. }
  847. /*
  848. * ibx_digital_port_connected - is the specified port connected?
  849. * @dev_priv: i915 private structure
  850. * @port: the port to test
  851. *
  852. * Returns true if @port is connected, false otherwise.
  853. */
  854. bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
  855. struct intel_digital_port *port)
  856. {
  857. u32 bit;
  858. if (HAS_PCH_IBX(dev_priv->dev)) {
  859. switch (port->port) {
  860. case PORT_B:
  861. bit = SDE_PORTB_HOTPLUG;
  862. break;
  863. case PORT_C:
  864. bit = SDE_PORTC_HOTPLUG;
  865. break;
  866. case PORT_D:
  867. bit = SDE_PORTD_HOTPLUG;
  868. break;
  869. default:
  870. return true;
  871. }
  872. } else {
  873. switch (port->port) {
  874. case PORT_B:
  875. bit = SDE_PORTB_HOTPLUG_CPT;
  876. break;
  877. case PORT_C:
  878. bit = SDE_PORTC_HOTPLUG_CPT;
  879. break;
  880. case PORT_D:
  881. bit = SDE_PORTD_HOTPLUG_CPT;
  882. break;
  883. default:
  884. return true;
  885. }
  886. }
  887. return I915_READ(SDEISR) & bit;
  888. }
  889. static const char *state_string(bool enabled)
  890. {
  891. return enabled ? "on" : "off";
  892. }
  893. /* Only for pre-ILK configs */
  894. void assert_pll(struct drm_i915_private *dev_priv,
  895. enum pipe pipe, bool state)
  896. {
  897. int reg;
  898. u32 val;
  899. bool cur_state;
  900. reg = DPLL(pipe);
  901. val = I915_READ(reg);
  902. cur_state = !!(val & DPLL_VCO_ENABLE);
  903. WARN(cur_state != state,
  904. "PLL state assertion failure (expected %s, current %s)\n",
  905. state_string(state), state_string(cur_state));
  906. }
  907. /* XXX: the dsi pll is shared between MIPI DSI ports */
  908. static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  909. {
  910. u32 val;
  911. bool cur_state;
  912. mutex_lock(&dev_priv->dpio_lock);
  913. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  914. mutex_unlock(&dev_priv->dpio_lock);
  915. cur_state = val & DSI_PLL_VCO_EN;
  916. WARN(cur_state != state,
  917. "DSI PLL state assertion failure (expected %s, current %s)\n",
  918. state_string(state), state_string(cur_state));
  919. }
  920. #define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
  921. #define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
  922. struct intel_shared_dpll *
  923. intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
  924. {
  925. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  926. if (crtc->config.shared_dpll < 0)
  927. return NULL;
  928. return &dev_priv->shared_dplls[crtc->config.shared_dpll];
  929. }
  930. /* For ILK+ */
  931. void assert_shared_dpll(struct drm_i915_private *dev_priv,
  932. struct intel_shared_dpll *pll,
  933. bool state)
  934. {
  935. bool cur_state;
  936. struct intel_dpll_hw_state hw_state;
  937. if (HAS_PCH_LPT(dev_priv->dev)) {
  938. DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
  939. return;
  940. }
  941. if (WARN (!pll,
  942. "asserting DPLL %s with no DPLL\n", state_string(state)))
  943. return;
  944. cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
  945. WARN(cur_state != state,
  946. "%s assertion failure (expected %s, current %s)\n",
  947. pll->name, state_string(state), state_string(cur_state));
  948. }
  949. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  950. enum pipe pipe, bool state)
  951. {
  952. int reg;
  953. u32 val;
  954. bool cur_state;
  955. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  956. pipe);
  957. if (HAS_DDI(dev_priv->dev)) {
  958. /* DDI does not have a specific FDI_TX register */
  959. reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
  960. val = I915_READ(reg);
  961. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  962. } else {
  963. reg = FDI_TX_CTL(pipe);
  964. val = I915_READ(reg);
  965. cur_state = !!(val & FDI_TX_ENABLE);
  966. }
  967. WARN(cur_state != state,
  968. "FDI TX state assertion failure (expected %s, current %s)\n",
  969. state_string(state), state_string(cur_state));
  970. }
  971. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  972. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  973. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  974. enum pipe pipe, bool state)
  975. {
  976. int reg;
  977. u32 val;
  978. bool cur_state;
  979. reg = FDI_RX_CTL(pipe);
  980. val = I915_READ(reg);
  981. cur_state = !!(val & FDI_RX_ENABLE);
  982. WARN(cur_state != state,
  983. "FDI RX state assertion failure (expected %s, current %s)\n",
  984. state_string(state), state_string(cur_state));
  985. }
  986. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  987. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  988. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  989. enum pipe pipe)
  990. {
  991. int reg;
  992. u32 val;
  993. /* ILK FDI PLL is always enabled */
  994. if (INTEL_INFO(dev_priv->dev)->gen == 5)
  995. return;
  996. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  997. if (HAS_DDI(dev_priv->dev))
  998. return;
  999. reg = FDI_TX_CTL(pipe);
  1000. val = I915_READ(reg);
  1001. WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1002. }
  1003. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1004. enum pipe pipe, bool state)
  1005. {
  1006. int reg;
  1007. u32 val;
  1008. bool cur_state;
  1009. reg = FDI_RX_CTL(pipe);
  1010. val = I915_READ(reg);
  1011. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1012. WARN(cur_state != state,
  1013. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1014. state_string(state), state_string(cur_state));
  1015. }
  1016. static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
  1017. enum pipe pipe)
  1018. {
  1019. int pp_reg, lvds_reg;
  1020. u32 val;
  1021. enum pipe panel_pipe = PIPE_A;
  1022. bool locked = true;
  1023. if (HAS_PCH_SPLIT(dev_priv->dev)) {
  1024. pp_reg = PCH_PP_CONTROL;
  1025. lvds_reg = PCH_LVDS;
  1026. } else {
  1027. pp_reg = PP_CONTROL;
  1028. lvds_reg = LVDS;
  1029. }
  1030. val = I915_READ(pp_reg);
  1031. if (!(val & PANEL_POWER_ON) ||
  1032. ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
  1033. locked = false;
  1034. if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
  1035. panel_pipe = PIPE_B;
  1036. WARN(panel_pipe == pipe && locked,
  1037. "panel assertion failure, pipe %c regs locked\n",
  1038. pipe_name(pipe));
  1039. }
  1040. static void assert_cursor(struct drm_i915_private *dev_priv,
  1041. enum pipe pipe, bool state)
  1042. {
  1043. struct drm_device *dev = dev_priv->dev;
  1044. bool cur_state;
  1045. if (IS_845G(dev) || IS_I865G(dev))
  1046. cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
  1047. else
  1048. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1049. WARN(cur_state != state,
  1050. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1051. pipe_name(pipe), state_string(state), state_string(cur_state));
  1052. }
  1053. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1054. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1055. void assert_pipe(struct drm_i915_private *dev_priv,
  1056. enum pipe pipe, bool state)
  1057. {
  1058. int reg;
  1059. u32 val;
  1060. bool cur_state;
  1061. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1062. pipe);
  1063. /* if we need the pipe A quirk it must be always on */
  1064. if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  1065. state = true;
  1066. if (!intel_display_power_enabled(dev_priv,
  1067. POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
  1068. cur_state = false;
  1069. } else {
  1070. reg = PIPECONF(cpu_transcoder);
  1071. val = I915_READ(reg);
  1072. cur_state = !!(val & PIPECONF_ENABLE);
  1073. }
  1074. WARN(cur_state != state,
  1075. "pipe %c assertion failure (expected %s, current %s)\n",
  1076. pipe_name(pipe), state_string(state), state_string(cur_state));
  1077. }
  1078. static void assert_plane(struct drm_i915_private *dev_priv,
  1079. enum plane plane, bool state)
  1080. {
  1081. int reg;
  1082. u32 val;
  1083. bool cur_state;
  1084. reg = DSPCNTR(plane);
  1085. val = I915_READ(reg);
  1086. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1087. WARN(cur_state != state,
  1088. "plane %c assertion failure (expected %s, current %s)\n",
  1089. plane_name(plane), state_string(state), state_string(cur_state));
  1090. }
  1091. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1092. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1093. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1094. enum pipe pipe)
  1095. {
  1096. struct drm_device *dev = dev_priv->dev;
  1097. int reg, i;
  1098. u32 val;
  1099. int cur_pipe;
  1100. /* Primary planes are fixed to pipes on gen4+ */
  1101. if (INTEL_INFO(dev)->gen >= 4) {
  1102. reg = DSPCNTR(pipe);
  1103. val = I915_READ(reg);
  1104. WARN(val & DISPLAY_PLANE_ENABLE,
  1105. "plane %c assertion failure, should be disabled but not\n",
  1106. plane_name(pipe));
  1107. return;
  1108. }
  1109. /* Need to check both planes against the pipe */
  1110. for_each_pipe(i) {
  1111. reg = DSPCNTR(i);
  1112. val = I915_READ(reg);
  1113. cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1114. DISPPLANE_SEL_PIPE_SHIFT;
  1115. WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1116. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1117. plane_name(i), pipe_name(pipe));
  1118. }
  1119. }
  1120. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1121. enum pipe pipe)
  1122. {
  1123. struct drm_device *dev = dev_priv->dev;
  1124. int reg, sprite;
  1125. u32 val;
  1126. if (IS_VALLEYVIEW(dev)) {
  1127. for_each_sprite(pipe, sprite) {
  1128. reg = SPCNTR(pipe, sprite);
  1129. val = I915_READ(reg);
  1130. WARN(val & SP_ENABLE,
  1131. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1132. sprite_name(pipe, sprite), pipe_name(pipe));
  1133. }
  1134. } else if (INTEL_INFO(dev)->gen >= 7) {
  1135. reg = SPRCTL(pipe);
  1136. val = I915_READ(reg);
  1137. WARN(val & SPRITE_ENABLE,
  1138. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1139. plane_name(pipe), pipe_name(pipe));
  1140. } else if (INTEL_INFO(dev)->gen >= 5) {
  1141. reg = DVSCNTR(pipe);
  1142. val = I915_READ(reg);
  1143. WARN(val & DVS_ENABLE,
  1144. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1145. plane_name(pipe), pipe_name(pipe));
  1146. }
  1147. }
  1148. static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
  1149. {
  1150. u32 val;
  1151. bool enabled;
  1152. WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
  1153. val = I915_READ(PCH_DREF_CONTROL);
  1154. enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
  1155. DREF_SUPERSPREAD_SOURCE_MASK));
  1156. WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
  1157. }
  1158. static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1159. enum pipe pipe)
  1160. {
  1161. int reg;
  1162. u32 val;
  1163. bool enabled;
  1164. reg = PCH_TRANSCONF(pipe);
  1165. val = I915_READ(reg);
  1166. enabled = !!(val & TRANS_ENABLE);
  1167. WARN(enabled,
  1168. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1169. pipe_name(pipe));
  1170. }
  1171. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1172. enum pipe pipe, u32 port_sel, u32 val)
  1173. {
  1174. if ((val & DP_PORT_EN) == 0)
  1175. return false;
  1176. if (HAS_PCH_CPT(dev_priv->dev)) {
  1177. u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
  1178. u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
  1179. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1180. return false;
  1181. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1182. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1183. return false;
  1184. } else {
  1185. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1186. return false;
  1187. }
  1188. return true;
  1189. }
  1190. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1191. enum pipe pipe, u32 val)
  1192. {
  1193. if ((val & SDVO_ENABLE) == 0)
  1194. return false;
  1195. if (HAS_PCH_CPT(dev_priv->dev)) {
  1196. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1197. return false;
  1198. } else if (IS_CHERRYVIEW(dev_priv->dev)) {
  1199. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1200. return false;
  1201. } else {
  1202. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1203. return false;
  1204. }
  1205. return true;
  1206. }
  1207. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1208. enum pipe pipe, u32 val)
  1209. {
  1210. if ((val & LVDS_PORT_EN) == 0)
  1211. return false;
  1212. if (HAS_PCH_CPT(dev_priv->dev)) {
  1213. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1214. return false;
  1215. } else {
  1216. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1217. return false;
  1218. }
  1219. return true;
  1220. }
  1221. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1222. enum pipe pipe, u32 val)
  1223. {
  1224. if ((val & ADPA_DAC_ENABLE) == 0)
  1225. return false;
  1226. if (HAS_PCH_CPT(dev_priv->dev)) {
  1227. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1228. return false;
  1229. } else {
  1230. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1231. return false;
  1232. }
  1233. return true;
  1234. }
  1235. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, int reg, u32 port_sel)
  1237. {
  1238. u32 val = I915_READ(reg);
  1239. WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1240. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1241. reg, pipe_name(pipe));
  1242. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
  1243. && (val & DP_PIPEB_SELECT),
  1244. "IBX PCH dp port still using transcoder B\n");
  1245. }
  1246. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1247. enum pipe pipe, int reg)
  1248. {
  1249. u32 val = I915_READ(reg);
  1250. WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1251. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1252. reg, pipe_name(pipe));
  1253. WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
  1254. && (val & SDVO_PIPE_B_SELECT),
  1255. "IBX PCH hdmi port still using transcoder B\n");
  1256. }
  1257. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1258. enum pipe pipe)
  1259. {
  1260. int reg;
  1261. u32 val;
  1262. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1263. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1264. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1265. reg = PCH_ADPA;
  1266. val = I915_READ(reg);
  1267. WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1268. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1269. pipe_name(pipe));
  1270. reg = PCH_LVDS;
  1271. val = I915_READ(reg);
  1272. WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1273. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1274. pipe_name(pipe));
  1275. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1276. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1277. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1278. }
  1279. static void intel_init_dpio(struct drm_device *dev)
  1280. {
  1281. struct drm_i915_private *dev_priv = dev->dev_private;
  1282. if (!IS_VALLEYVIEW(dev))
  1283. return;
  1284. /*
  1285. * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
  1286. * CHV x1 PHY (DP/HDMI D)
  1287. * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
  1288. */
  1289. if (IS_CHERRYVIEW(dev)) {
  1290. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
  1291. DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
  1292. } else {
  1293. DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
  1294. }
  1295. }
  1296. static void intel_reset_dpio(struct drm_device *dev)
  1297. {
  1298. struct drm_i915_private *dev_priv = dev->dev_private;
  1299. if (!IS_VALLEYVIEW(dev))
  1300. return;
  1301. if (IS_CHERRYVIEW(dev)) {
  1302. enum dpio_phy phy;
  1303. u32 val;
  1304. for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
  1305. /* Poll for phypwrgood signal */
  1306. if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
  1307. PHY_POWERGOOD(phy), 1))
  1308. DRM_ERROR("Display PHY %d is not power up\n", phy);
  1309. /*
  1310. * Deassert common lane reset for PHY.
  1311. *
  1312. * This should only be done on init and resume from S3
  1313. * with both PLLs disabled, or we risk losing DPIO and
  1314. * PLL synchronization.
  1315. */
  1316. val = I915_READ(DISPLAY_PHY_CONTROL);
  1317. I915_WRITE(DISPLAY_PHY_CONTROL,
  1318. PHY_COM_LANE_RESET_DEASSERT(phy, val));
  1319. }
  1320. } else {
  1321. /*
  1322. * If DPIO has already been reset, e.g. by BIOS, just skip all
  1323. * this.
  1324. */
  1325. if (I915_READ(DPIO_CTL) & DPIO_CMNRST)
  1326. return;
  1327. /*
  1328. * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
  1329. * Need to assert and de-assert PHY SB reset by gating the
  1330. * common lane power, then un-gating it.
  1331. * Simply ungating isn't enough to reset the PHY enough to get
  1332. * ports and lanes running.
  1333. */
  1334. __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
  1335. false);
  1336. __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
  1337. true);
  1338. }
  1339. }
  1340. static void vlv_enable_pll(struct intel_crtc *crtc)
  1341. {
  1342. struct drm_device *dev = crtc->base.dev;
  1343. struct drm_i915_private *dev_priv = dev->dev_private;
  1344. int reg = DPLL(crtc->pipe);
  1345. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1346. assert_pipe_disabled(dev_priv, crtc->pipe);
  1347. /* No really, not for ILK+ */
  1348. BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
  1349. /* PLL is protected by panel, make sure we can write it */
  1350. if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
  1351. assert_panel_unlocked(dev_priv, crtc->pipe);
  1352. I915_WRITE(reg, dpll);
  1353. POSTING_READ(reg);
  1354. udelay(150);
  1355. if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1356. DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
  1357. I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
  1358. POSTING_READ(DPLL_MD(crtc->pipe));
  1359. /* We do this three times for luck */
  1360. I915_WRITE(reg, dpll);
  1361. POSTING_READ(reg);
  1362. udelay(150); /* wait for warmup */
  1363. I915_WRITE(reg, dpll);
  1364. POSTING_READ(reg);
  1365. udelay(150); /* wait for warmup */
  1366. I915_WRITE(reg, dpll);
  1367. POSTING_READ(reg);
  1368. udelay(150); /* wait for warmup */
  1369. }
  1370. static void chv_enable_pll(struct intel_crtc *crtc)
  1371. {
  1372. struct drm_device *dev = crtc->base.dev;
  1373. struct drm_i915_private *dev_priv = dev->dev_private;
  1374. int pipe = crtc->pipe;
  1375. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1376. u32 tmp;
  1377. assert_pipe_disabled(dev_priv, crtc->pipe);
  1378. BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
  1379. mutex_lock(&dev_priv->dpio_lock);
  1380. /* Enable back the 10bit clock to display controller */
  1381. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1382. tmp |= DPIO_DCLKP_EN;
  1383. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1384. /*
  1385. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1386. */
  1387. udelay(1);
  1388. /* Enable PLL */
  1389. I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
  1390. /* Check PLL is locked */
  1391. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  1392. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1393. /* not sure when this should be written */
  1394. I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
  1395. POSTING_READ(DPLL_MD(pipe));
  1396. mutex_unlock(&dev_priv->dpio_lock);
  1397. }
  1398. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1399. {
  1400. struct drm_device *dev = crtc->base.dev;
  1401. struct drm_i915_private *dev_priv = dev->dev_private;
  1402. int reg = DPLL(crtc->pipe);
  1403. u32 dpll = crtc->config.dpll_hw_state.dpll;
  1404. assert_pipe_disabled(dev_priv, crtc->pipe);
  1405. /* No really, not for ILK+ */
  1406. BUG_ON(INTEL_INFO(dev)->gen >= 5);
  1407. /* PLL is protected by panel, make sure we can write it */
  1408. if (IS_MOBILE(dev) && !IS_I830(dev))
  1409. assert_panel_unlocked(dev_priv, crtc->pipe);
  1410. I915_WRITE(reg, dpll);
  1411. /* Wait for the clocks to stabilize. */
  1412. POSTING_READ(reg);
  1413. udelay(150);
  1414. if (INTEL_INFO(dev)->gen >= 4) {
  1415. I915_WRITE(DPLL_MD(crtc->pipe),
  1416. crtc->config.dpll_hw_state.dpll_md);
  1417. } else {
  1418. /* The pixel multiplier can only be updated once the
  1419. * DPLL is enabled and the clocks are stable.
  1420. *
  1421. * So write it again.
  1422. */
  1423. I915_WRITE(reg, dpll);
  1424. }
  1425. /* We do this three times for luck */
  1426. I915_WRITE(reg, dpll);
  1427. POSTING_READ(reg);
  1428. udelay(150); /* wait for warmup */
  1429. I915_WRITE(reg, dpll);
  1430. POSTING_READ(reg);
  1431. udelay(150); /* wait for warmup */
  1432. I915_WRITE(reg, dpll);
  1433. POSTING_READ(reg);
  1434. udelay(150); /* wait for warmup */
  1435. }
  1436. /**
  1437. * i9xx_disable_pll - disable a PLL
  1438. * @dev_priv: i915 private structure
  1439. * @pipe: pipe PLL to disable
  1440. *
  1441. * Disable the PLL for @pipe, making sure the pipe is off first.
  1442. *
  1443. * Note! This is for pre-ILK only.
  1444. */
  1445. static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1446. {
  1447. /* Don't disable pipe A or pipe A PLLs if needed */
  1448. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1449. return;
  1450. /* Make sure the pipe isn't still relying on us */
  1451. assert_pipe_disabled(dev_priv, pipe);
  1452. I915_WRITE(DPLL(pipe), 0);
  1453. POSTING_READ(DPLL(pipe));
  1454. }
  1455. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1456. {
  1457. u32 val = 0;
  1458. /* Make sure the pipe isn't still relying on us */
  1459. assert_pipe_disabled(dev_priv, pipe);
  1460. /*
  1461. * Leave integrated clock source and reference clock enabled for pipe B.
  1462. * The latter is needed for VGA hotplug / manual detection.
  1463. */
  1464. if (pipe == PIPE_B)
  1465. val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
  1466. I915_WRITE(DPLL(pipe), val);
  1467. POSTING_READ(DPLL(pipe));
  1468. }
  1469. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1470. {
  1471. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1472. u32 val;
  1473. /* Make sure the pipe isn't still relying on us */
  1474. assert_pipe_disabled(dev_priv, pipe);
  1475. /* Set PLL en = 0 */
  1476. val = DPLL_SSC_REF_CLOCK_CHV;
  1477. if (pipe != PIPE_A)
  1478. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1479. I915_WRITE(DPLL(pipe), val);
  1480. POSTING_READ(DPLL(pipe));
  1481. mutex_lock(&dev_priv->dpio_lock);
  1482. /* Disable 10bit clock to display controller */
  1483. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1484. val &= ~DPIO_DCLKP_EN;
  1485. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1486. mutex_unlock(&dev_priv->dpio_lock);
  1487. }
  1488. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1489. struct intel_digital_port *dport)
  1490. {
  1491. u32 port_mask;
  1492. int dpll_reg;
  1493. switch (dport->port) {
  1494. case PORT_B:
  1495. port_mask = DPLL_PORTB_READY_MASK;
  1496. dpll_reg = DPLL(0);
  1497. break;
  1498. case PORT_C:
  1499. port_mask = DPLL_PORTC_READY_MASK;
  1500. dpll_reg = DPLL(0);
  1501. break;
  1502. case PORT_D:
  1503. port_mask = DPLL_PORTD_READY_MASK;
  1504. dpll_reg = DPIO_PHY_STATUS;
  1505. break;
  1506. default:
  1507. BUG();
  1508. }
  1509. if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
  1510. WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
  1511. port_name(dport->port), I915_READ(dpll_reg));
  1512. }
  1513. static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
  1514. {
  1515. struct drm_device *dev = crtc->base.dev;
  1516. struct drm_i915_private *dev_priv = dev->dev_private;
  1517. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1518. WARN_ON(!pll->refcount);
  1519. if (pll->active == 0) {
  1520. DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
  1521. WARN_ON(pll->on);
  1522. assert_shared_dpll_disabled(dev_priv, pll);
  1523. pll->mode_set(dev_priv, pll);
  1524. }
  1525. }
  1526. /**
  1527. * intel_enable_shared_dpll - enable PCH PLL
  1528. * @dev_priv: i915 private structure
  1529. * @pipe: pipe PLL to enable
  1530. *
  1531. * The PCH PLL needs to be enabled before the PCH transcoder, since it
  1532. * drives the transcoder clock.
  1533. */
  1534. static void intel_enable_shared_dpll(struct intel_crtc *crtc)
  1535. {
  1536. struct drm_device *dev = crtc->base.dev;
  1537. struct drm_i915_private *dev_priv = dev->dev_private;
  1538. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1539. if (WARN_ON(pll == NULL))
  1540. return;
  1541. if (WARN_ON(pll->refcount == 0))
  1542. return;
  1543. DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
  1544. pll->name, pll->active, pll->on,
  1545. crtc->base.base.id);
  1546. if (pll->active++) {
  1547. WARN_ON(!pll->on);
  1548. assert_shared_dpll_enabled(dev_priv, pll);
  1549. return;
  1550. }
  1551. WARN_ON(pll->on);
  1552. DRM_DEBUG_KMS("enabling %s\n", pll->name);
  1553. pll->enable(dev_priv, pll);
  1554. pll->on = true;
  1555. }
  1556. static void intel_disable_shared_dpll(struct intel_crtc *crtc)
  1557. {
  1558. struct drm_device *dev = crtc->base.dev;
  1559. struct drm_i915_private *dev_priv = dev->dev_private;
  1560. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  1561. /* PCH only available on ILK+ */
  1562. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1563. if (WARN_ON(pll == NULL))
  1564. return;
  1565. if (WARN_ON(pll->refcount == 0))
  1566. return;
  1567. DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
  1568. pll->name, pll->active, pll->on,
  1569. crtc->base.base.id);
  1570. if (WARN_ON(pll->active == 0)) {
  1571. assert_shared_dpll_disabled(dev_priv, pll);
  1572. return;
  1573. }
  1574. assert_shared_dpll_enabled(dev_priv, pll);
  1575. WARN_ON(!pll->on);
  1576. if (--pll->active)
  1577. return;
  1578. DRM_DEBUG_KMS("disabling %s\n", pll->name);
  1579. pll->disable(dev_priv, pll);
  1580. pll->on = false;
  1581. }
  1582. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1583. enum pipe pipe)
  1584. {
  1585. struct drm_device *dev = dev_priv->dev;
  1586. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1587. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1588. uint32_t reg, val, pipeconf_val;
  1589. /* PCH only available on ILK+ */
  1590. BUG_ON(INTEL_INFO(dev)->gen < 5);
  1591. /* Make sure PCH DPLL is enabled */
  1592. assert_shared_dpll_enabled(dev_priv,
  1593. intel_crtc_to_shared_dpll(intel_crtc));
  1594. /* FDI must be feeding us bits for PCH ports */
  1595. assert_fdi_tx_enabled(dev_priv, pipe);
  1596. assert_fdi_rx_enabled(dev_priv, pipe);
  1597. if (HAS_PCH_CPT(dev)) {
  1598. /* Workaround: Set the timing override bit before enabling the
  1599. * pch transcoder. */
  1600. reg = TRANS_CHICKEN2(pipe);
  1601. val = I915_READ(reg);
  1602. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1603. I915_WRITE(reg, val);
  1604. }
  1605. reg = PCH_TRANSCONF(pipe);
  1606. val = I915_READ(reg);
  1607. pipeconf_val = I915_READ(PIPECONF(pipe));
  1608. if (HAS_PCH_IBX(dev_priv->dev)) {
  1609. /*
  1610. * make the BPC in transcoder be consistent with
  1611. * that in pipeconf reg.
  1612. */
  1613. val &= ~PIPECONF_BPC_MASK;
  1614. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1615. }
  1616. val &= ~TRANS_INTERLACE_MASK;
  1617. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1618. if (HAS_PCH_IBX(dev_priv->dev) &&
  1619. intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
  1620. val |= TRANS_LEGACY_INTERLACED_ILK;
  1621. else
  1622. val |= TRANS_INTERLACED;
  1623. else
  1624. val |= TRANS_PROGRESSIVE;
  1625. I915_WRITE(reg, val | TRANS_ENABLE);
  1626. if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
  1627. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1628. }
  1629. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1630. enum transcoder cpu_transcoder)
  1631. {
  1632. u32 val, pipeconf_val;
  1633. /* PCH only available on ILK+ */
  1634. BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
  1635. /* FDI must be feeding us bits for PCH ports */
  1636. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1637. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1638. /* Workaround: set timing override bit. */
  1639. val = I915_READ(_TRANSA_CHICKEN2);
  1640. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1641. I915_WRITE(_TRANSA_CHICKEN2, val);
  1642. val = TRANS_ENABLE;
  1643. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1644. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1645. PIPECONF_INTERLACED_ILK)
  1646. val |= TRANS_INTERLACED;
  1647. else
  1648. val |= TRANS_PROGRESSIVE;
  1649. I915_WRITE(LPT_TRANSCONF, val);
  1650. if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
  1651. DRM_ERROR("Failed to enable PCH transcoder\n");
  1652. }
  1653. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1654. enum pipe pipe)
  1655. {
  1656. struct drm_device *dev = dev_priv->dev;
  1657. uint32_t reg, val;
  1658. /* FDI relies on the transcoder */
  1659. assert_fdi_tx_disabled(dev_priv, pipe);
  1660. assert_fdi_rx_disabled(dev_priv, pipe);
  1661. /* Ports must be off as well */
  1662. assert_pch_ports_disabled(dev_priv, pipe);
  1663. reg = PCH_TRANSCONF(pipe);
  1664. val = I915_READ(reg);
  1665. val &= ~TRANS_ENABLE;
  1666. I915_WRITE(reg, val);
  1667. /* wait for PCH transcoder off, transcoder state */
  1668. if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
  1669. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1670. if (!HAS_PCH_IBX(dev)) {
  1671. /* Workaround: Clear the timing override chicken bit again. */
  1672. reg = TRANS_CHICKEN2(pipe);
  1673. val = I915_READ(reg);
  1674. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1675. I915_WRITE(reg, val);
  1676. }
  1677. }
  1678. static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1679. {
  1680. u32 val;
  1681. val = I915_READ(LPT_TRANSCONF);
  1682. val &= ~TRANS_ENABLE;
  1683. I915_WRITE(LPT_TRANSCONF, val);
  1684. /* wait for PCH transcoder off, transcoder state */
  1685. if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
  1686. DRM_ERROR("Failed to disable PCH transcoder\n");
  1687. /* Workaround: clear timing override bit. */
  1688. val = I915_READ(_TRANSA_CHICKEN2);
  1689. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1690. I915_WRITE(_TRANSA_CHICKEN2, val);
  1691. }
  1692. /**
  1693. * intel_enable_pipe - enable a pipe, asserting requirements
  1694. * @crtc: crtc responsible for the pipe
  1695. *
  1696. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1697. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1698. */
  1699. static void intel_enable_pipe(struct intel_crtc *crtc)
  1700. {
  1701. struct drm_device *dev = crtc->base.dev;
  1702. struct drm_i915_private *dev_priv = dev->dev_private;
  1703. enum pipe pipe = crtc->pipe;
  1704. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1705. pipe);
  1706. enum pipe pch_transcoder;
  1707. int reg;
  1708. u32 val;
  1709. assert_planes_disabled(dev_priv, pipe);
  1710. assert_cursor_disabled(dev_priv, pipe);
  1711. assert_sprites_disabled(dev_priv, pipe);
  1712. if (HAS_PCH_LPT(dev_priv->dev))
  1713. pch_transcoder = TRANSCODER_A;
  1714. else
  1715. pch_transcoder = pipe;
  1716. /*
  1717. * A pipe without a PLL won't actually be able to drive bits from
  1718. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1719. * need the check.
  1720. */
  1721. if (!HAS_PCH_SPLIT(dev_priv->dev))
  1722. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
  1723. assert_dsi_pll_enabled(dev_priv);
  1724. else
  1725. assert_pll_enabled(dev_priv, pipe);
  1726. else {
  1727. if (crtc->config.has_pch_encoder) {
  1728. /* if driving the PCH, we need FDI enabled */
  1729. assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
  1730. assert_fdi_tx_pll_enabled(dev_priv,
  1731. (enum pipe) cpu_transcoder);
  1732. }
  1733. /* FIXME: assert CPU port conditions for SNB+ */
  1734. }
  1735. reg = PIPECONF(cpu_transcoder);
  1736. val = I915_READ(reg);
  1737. if (val & PIPECONF_ENABLE) {
  1738. WARN_ON(!(pipe == PIPE_A &&
  1739. dev_priv->quirks & QUIRK_PIPEA_FORCE));
  1740. return;
  1741. }
  1742. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1743. POSTING_READ(reg);
  1744. }
  1745. /**
  1746. * intel_disable_pipe - disable a pipe, asserting requirements
  1747. * @dev_priv: i915 private structure
  1748. * @pipe: pipe to disable
  1749. *
  1750. * Disable @pipe, making sure that various hardware specific requirements
  1751. * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
  1752. *
  1753. * @pipe should be %PIPE_A or %PIPE_B.
  1754. *
  1755. * Will wait until the pipe has shut down before returning.
  1756. */
  1757. static void intel_disable_pipe(struct drm_i915_private *dev_priv,
  1758. enum pipe pipe)
  1759. {
  1760. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1761. pipe);
  1762. int reg;
  1763. u32 val;
  1764. /*
  1765. * Make sure planes won't keep trying to pump pixels to us,
  1766. * or we might hang the display.
  1767. */
  1768. assert_planes_disabled(dev_priv, pipe);
  1769. assert_cursor_disabled(dev_priv, pipe);
  1770. assert_sprites_disabled(dev_priv, pipe);
  1771. /* Don't disable pipe A or pipe A PLLs if needed */
  1772. if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  1773. return;
  1774. reg = PIPECONF(cpu_transcoder);
  1775. val = I915_READ(reg);
  1776. if ((val & PIPECONF_ENABLE) == 0)
  1777. return;
  1778. I915_WRITE(reg, val & ~PIPECONF_ENABLE);
  1779. intel_wait_for_pipe_off(dev_priv->dev, pipe);
  1780. }
  1781. /*
  1782. * Plane regs are double buffered, going from enabled->disabled needs a
  1783. * trigger in order to latch. The display address reg provides this.
  1784. */
  1785. void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
  1786. enum plane plane)
  1787. {
  1788. struct drm_device *dev = dev_priv->dev;
  1789. u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
  1790. I915_WRITE(reg, I915_READ(reg));
  1791. POSTING_READ(reg);
  1792. }
  1793. /**
  1794. * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
  1795. * @dev_priv: i915 private structure
  1796. * @plane: plane to enable
  1797. * @pipe: pipe being fed
  1798. *
  1799. * Enable @plane on @pipe, making sure that @pipe is running first.
  1800. */
  1801. static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1802. enum plane plane, enum pipe pipe)
  1803. {
  1804. struct drm_device *dev = dev_priv->dev;
  1805. struct intel_crtc *intel_crtc =
  1806. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1807. int reg;
  1808. u32 val;
  1809. /* If the pipe isn't enabled, we can't pump pixels and may hang */
  1810. assert_pipe_enabled(dev_priv, pipe);
  1811. if (intel_crtc->primary_enabled)
  1812. return;
  1813. intel_crtc->primary_enabled = true;
  1814. reg = DSPCNTR(plane);
  1815. val = I915_READ(reg);
  1816. WARN_ON(val & DISPLAY_PLANE_ENABLE);
  1817. I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
  1818. intel_flush_primary_plane(dev_priv, plane);
  1819. /*
  1820. * BDW signals flip done immediately if the plane
  1821. * is disabled, even if the plane enable is already
  1822. * armed to occur at the next vblank :(
  1823. */
  1824. if (IS_BROADWELL(dev))
  1825. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1826. }
  1827. /**
  1828. * intel_disable_primary_hw_plane - disable the primary hardware plane
  1829. * @dev_priv: i915 private structure
  1830. * @plane: plane to disable
  1831. * @pipe: pipe consuming the data
  1832. *
  1833. * Disable @plane; should be an independent operation.
  1834. */
  1835. static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
  1836. enum plane plane, enum pipe pipe)
  1837. {
  1838. struct intel_crtc *intel_crtc =
  1839. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  1840. int reg;
  1841. u32 val;
  1842. if (!intel_crtc->primary_enabled)
  1843. return;
  1844. intel_crtc->primary_enabled = false;
  1845. reg = DSPCNTR(plane);
  1846. val = I915_READ(reg);
  1847. WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
  1848. I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
  1849. intel_flush_primary_plane(dev_priv, plane);
  1850. }
  1851. static bool need_vtd_wa(struct drm_device *dev)
  1852. {
  1853. #ifdef CONFIG_INTEL_IOMMU
  1854. if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
  1855. return true;
  1856. #endif
  1857. return false;
  1858. }
  1859. static int intel_align_height(struct drm_device *dev, int height, bool tiled)
  1860. {
  1861. int tile_height;
  1862. tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
  1863. return ALIGN(height, tile_height);
  1864. }
  1865. int
  1866. intel_pin_and_fence_fb_obj(struct drm_device *dev,
  1867. struct drm_i915_gem_object *obj,
  1868. struct intel_engine_cs *pipelined)
  1869. {
  1870. struct drm_i915_private *dev_priv = dev->dev_private;
  1871. u32 alignment;
  1872. int ret;
  1873. switch (obj->tiling_mode) {
  1874. case I915_TILING_NONE:
  1875. if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
  1876. alignment = 128 * 1024;
  1877. else if (INTEL_INFO(dev)->gen >= 4)
  1878. alignment = 4 * 1024;
  1879. else
  1880. alignment = 64 * 1024;
  1881. break;
  1882. case I915_TILING_X:
  1883. /* pin() will align the object as required by fence */
  1884. alignment = 0;
  1885. break;
  1886. case I915_TILING_Y:
  1887. WARN(1, "Y tiled bo slipped through, driver bug!\n");
  1888. return -EINVAL;
  1889. default:
  1890. BUG();
  1891. }
  1892. /* Note that the w/a also requires 64 PTE of padding following the
  1893. * bo. We currently fill all unused PTE with the shadow page and so
  1894. * we should always have valid PTE following the scanout preventing
  1895. * the VT-d warning.
  1896. */
  1897. if (need_vtd_wa(dev) && alignment < 256 * 1024)
  1898. alignment = 256 * 1024;
  1899. dev_priv->mm.interruptible = false;
  1900. ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
  1901. if (ret)
  1902. goto err_interruptible;
  1903. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1904. * fence, whereas 965+ only requires a fence if using
  1905. * framebuffer compression. For simplicity, we always install
  1906. * a fence as the cost is not that onerous.
  1907. */
  1908. ret = i915_gem_object_get_fence(obj);
  1909. if (ret)
  1910. goto err_unpin;
  1911. i915_gem_object_pin_fence(obj);
  1912. dev_priv->mm.interruptible = true;
  1913. return 0;
  1914. err_unpin:
  1915. i915_gem_object_unpin_from_display_plane(obj);
  1916. err_interruptible:
  1917. dev_priv->mm.interruptible = true;
  1918. return ret;
  1919. }
  1920. void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
  1921. {
  1922. i915_gem_object_unpin_fence(obj);
  1923. i915_gem_object_unpin_from_display_plane(obj);
  1924. }
  1925. /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
  1926. * is assumed to be a power-of-two. */
  1927. unsigned long intel_gen4_compute_page_offset(int *x, int *y,
  1928. unsigned int tiling_mode,
  1929. unsigned int cpp,
  1930. unsigned int pitch)
  1931. {
  1932. if (tiling_mode != I915_TILING_NONE) {
  1933. unsigned int tile_rows, tiles;
  1934. tile_rows = *y / 8;
  1935. *y %= 8;
  1936. tiles = *x / (512/cpp);
  1937. *x %= 512/cpp;
  1938. return tile_rows * pitch * 8 + tiles * 4096;
  1939. } else {
  1940. unsigned int offset;
  1941. offset = *y * pitch + *x * cpp;
  1942. *y = 0;
  1943. *x = (offset & 4095) / cpp;
  1944. return offset & -4096;
  1945. }
  1946. }
  1947. int intel_format_to_fourcc(int format)
  1948. {
  1949. switch (format) {
  1950. case DISPPLANE_8BPP:
  1951. return DRM_FORMAT_C8;
  1952. case DISPPLANE_BGRX555:
  1953. return DRM_FORMAT_XRGB1555;
  1954. case DISPPLANE_BGRX565:
  1955. return DRM_FORMAT_RGB565;
  1956. default:
  1957. case DISPPLANE_BGRX888:
  1958. return DRM_FORMAT_XRGB8888;
  1959. case DISPPLANE_RGBX888:
  1960. return DRM_FORMAT_XBGR8888;
  1961. case DISPPLANE_BGRX101010:
  1962. return DRM_FORMAT_XRGB2101010;
  1963. case DISPPLANE_RGBX101010:
  1964. return DRM_FORMAT_XBGR2101010;
  1965. }
  1966. }
  1967. static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
  1968. struct intel_plane_config *plane_config)
  1969. {
  1970. struct drm_device *dev = crtc->base.dev;
  1971. struct drm_i915_gem_object *obj = NULL;
  1972. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  1973. u32 base = plane_config->base;
  1974. if (plane_config->size == 0)
  1975. return false;
  1976. obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
  1977. plane_config->size);
  1978. if (!obj)
  1979. return false;
  1980. if (plane_config->tiled) {
  1981. obj->tiling_mode = I915_TILING_X;
  1982. obj->stride = crtc->base.primary->fb->pitches[0];
  1983. }
  1984. mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
  1985. mode_cmd.width = crtc->base.primary->fb->width;
  1986. mode_cmd.height = crtc->base.primary->fb->height;
  1987. mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
  1988. mutex_lock(&dev->struct_mutex);
  1989. if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
  1990. &mode_cmd, obj)) {
  1991. DRM_DEBUG_KMS("intel fb init failed\n");
  1992. goto out_unref_obj;
  1993. }
  1994. mutex_unlock(&dev->struct_mutex);
  1995. DRM_DEBUG_KMS("plane fb obj %p\n", obj);
  1996. return true;
  1997. out_unref_obj:
  1998. drm_gem_object_unreference(&obj->base);
  1999. mutex_unlock(&dev->struct_mutex);
  2000. return false;
  2001. }
  2002. static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
  2003. struct intel_plane_config *plane_config)
  2004. {
  2005. struct drm_device *dev = intel_crtc->base.dev;
  2006. struct drm_crtc *c;
  2007. struct intel_crtc *i;
  2008. struct intel_framebuffer *fb;
  2009. if (!intel_crtc->base.primary->fb)
  2010. return;
  2011. if (intel_alloc_plane_obj(intel_crtc, plane_config))
  2012. return;
  2013. kfree(intel_crtc->base.primary->fb);
  2014. intel_crtc->base.primary->fb = NULL;
  2015. /*
  2016. * Failed to alloc the obj, check to see if we should share
  2017. * an fb with another CRTC instead
  2018. */
  2019. for_each_crtc(dev, c) {
  2020. i = to_intel_crtc(c);
  2021. if (c == &intel_crtc->base)
  2022. continue;
  2023. if (!i->active || !c->primary->fb)
  2024. continue;
  2025. fb = to_intel_framebuffer(c->primary->fb);
  2026. if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
  2027. drm_framebuffer_reference(c->primary->fb);
  2028. intel_crtc->base.primary->fb = c->primary->fb;
  2029. break;
  2030. }
  2031. }
  2032. }
  2033. static void i9xx_update_primary_plane(struct drm_crtc *crtc,
  2034. struct drm_framebuffer *fb,
  2035. int x, int y)
  2036. {
  2037. struct drm_device *dev = crtc->dev;
  2038. struct drm_i915_private *dev_priv = dev->dev_private;
  2039. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2040. struct intel_framebuffer *intel_fb;
  2041. struct drm_i915_gem_object *obj;
  2042. int plane = intel_crtc->plane;
  2043. unsigned long linear_offset;
  2044. u32 dspcntr;
  2045. u32 reg;
  2046. intel_fb = to_intel_framebuffer(fb);
  2047. obj = intel_fb->obj;
  2048. reg = DSPCNTR(plane);
  2049. dspcntr = I915_READ(reg);
  2050. /* Mask out pixel format bits in case we change it */
  2051. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2052. switch (fb->pixel_format) {
  2053. case DRM_FORMAT_C8:
  2054. dspcntr |= DISPPLANE_8BPP;
  2055. break;
  2056. case DRM_FORMAT_XRGB1555:
  2057. case DRM_FORMAT_ARGB1555:
  2058. dspcntr |= DISPPLANE_BGRX555;
  2059. break;
  2060. case DRM_FORMAT_RGB565:
  2061. dspcntr |= DISPPLANE_BGRX565;
  2062. break;
  2063. case DRM_FORMAT_XRGB8888:
  2064. case DRM_FORMAT_ARGB8888:
  2065. dspcntr |= DISPPLANE_BGRX888;
  2066. break;
  2067. case DRM_FORMAT_XBGR8888:
  2068. case DRM_FORMAT_ABGR8888:
  2069. dspcntr |= DISPPLANE_RGBX888;
  2070. break;
  2071. case DRM_FORMAT_XRGB2101010:
  2072. case DRM_FORMAT_ARGB2101010:
  2073. dspcntr |= DISPPLANE_BGRX101010;
  2074. break;
  2075. case DRM_FORMAT_XBGR2101010:
  2076. case DRM_FORMAT_ABGR2101010:
  2077. dspcntr |= DISPPLANE_RGBX101010;
  2078. break;
  2079. default:
  2080. BUG();
  2081. }
  2082. if (INTEL_INFO(dev)->gen >= 4) {
  2083. if (obj->tiling_mode != I915_TILING_NONE)
  2084. dspcntr |= DISPPLANE_TILED;
  2085. else
  2086. dspcntr &= ~DISPPLANE_TILED;
  2087. }
  2088. if (IS_G4X(dev))
  2089. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2090. I915_WRITE(reg, dspcntr);
  2091. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2092. if (INTEL_INFO(dev)->gen >= 4) {
  2093. intel_crtc->dspaddr_offset =
  2094. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2095. fb->bits_per_pixel / 8,
  2096. fb->pitches[0]);
  2097. linear_offset -= intel_crtc->dspaddr_offset;
  2098. } else {
  2099. intel_crtc->dspaddr_offset = linear_offset;
  2100. }
  2101. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2102. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2103. fb->pitches[0]);
  2104. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2105. if (INTEL_INFO(dev)->gen >= 4) {
  2106. I915_WRITE(DSPSURF(plane),
  2107. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2108. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2109. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2110. } else
  2111. I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
  2112. POSTING_READ(reg);
  2113. }
  2114. static void ironlake_update_primary_plane(struct drm_crtc *crtc,
  2115. struct drm_framebuffer *fb,
  2116. int x, int y)
  2117. {
  2118. struct drm_device *dev = crtc->dev;
  2119. struct drm_i915_private *dev_priv = dev->dev_private;
  2120. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2121. struct intel_framebuffer *intel_fb;
  2122. struct drm_i915_gem_object *obj;
  2123. int plane = intel_crtc->plane;
  2124. unsigned long linear_offset;
  2125. u32 dspcntr;
  2126. u32 reg;
  2127. intel_fb = to_intel_framebuffer(fb);
  2128. obj = intel_fb->obj;
  2129. reg = DSPCNTR(plane);
  2130. dspcntr = I915_READ(reg);
  2131. /* Mask out pixel format bits in case we change it */
  2132. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  2133. switch (fb->pixel_format) {
  2134. case DRM_FORMAT_C8:
  2135. dspcntr |= DISPPLANE_8BPP;
  2136. break;
  2137. case DRM_FORMAT_RGB565:
  2138. dspcntr |= DISPPLANE_BGRX565;
  2139. break;
  2140. case DRM_FORMAT_XRGB8888:
  2141. case DRM_FORMAT_ARGB8888:
  2142. dspcntr |= DISPPLANE_BGRX888;
  2143. break;
  2144. case DRM_FORMAT_XBGR8888:
  2145. case DRM_FORMAT_ABGR8888:
  2146. dspcntr |= DISPPLANE_RGBX888;
  2147. break;
  2148. case DRM_FORMAT_XRGB2101010:
  2149. case DRM_FORMAT_ARGB2101010:
  2150. dspcntr |= DISPPLANE_BGRX101010;
  2151. break;
  2152. case DRM_FORMAT_XBGR2101010:
  2153. case DRM_FORMAT_ABGR2101010:
  2154. dspcntr |= DISPPLANE_RGBX101010;
  2155. break;
  2156. default:
  2157. BUG();
  2158. }
  2159. if (obj->tiling_mode != I915_TILING_NONE)
  2160. dspcntr |= DISPPLANE_TILED;
  2161. else
  2162. dspcntr &= ~DISPPLANE_TILED;
  2163. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  2164. dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
  2165. else
  2166. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2167. I915_WRITE(reg, dspcntr);
  2168. linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
  2169. intel_crtc->dspaddr_offset =
  2170. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  2171. fb->bits_per_pixel / 8,
  2172. fb->pitches[0]);
  2173. linear_offset -= intel_crtc->dspaddr_offset;
  2174. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  2175. i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
  2176. fb->pitches[0]);
  2177. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2178. I915_WRITE(DSPSURF(plane),
  2179. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
  2180. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  2181. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2182. } else {
  2183. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2184. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2185. }
  2186. POSTING_READ(reg);
  2187. }
  2188. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2189. static int
  2190. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2191. int x, int y, enum mode_set_atomic state)
  2192. {
  2193. struct drm_device *dev = crtc->dev;
  2194. struct drm_i915_private *dev_priv = dev->dev_private;
  2195. if (dev_priv->display.disable_fbc)
  2196. dev_priv->display.disable_fbc(dev);
  2197. intel_increase_pllclock(crtc);
  2198. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2199. return 0;
  2200. }
  2201. void intel_display_handle_reset(struct drm_device *dev)
  2202. {
  2203. struct drm_i915_private *dev_priv = dev->dev_private;
  2204. struct drm_crtc *crtc;
  2205. /*
  2206. * Flips in the rings have been nuked by the reset,
  2207. * so complete all pending flips so that user space
  2208. * will get its events and not get stuck.
  2209. *
  2210. * Also update the base address of all primary
  2211. * planes to the the last fb to make sure we're
  2212. * showing the correct fb after a reset.
  2213. *
  2214. * Need to make two loops over the crtcs so that we
  2215. * don't try to grab a crtc mutex before the
  2216. * pending_flip_queue really got woken up.
  2217. */
  2218. for_each_crtc(dev, crtc) {
  2219. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2220. enum plane plane = intel_crtc->plane;
  2221. intel_prepare_page_flip(dev, plane);
  2222. intel_finish_page_flip_plane(dev, plane);
  2223. }
  2224. for_each_crtc(dev, crtc) {
  2225. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2226. drm_modeset_lock(&crtc->mutex, NULL);
  2227. /*
  2228. * FIXME: Once we have proper support for primary planes (and
  2229. * disabling them without disabling the entire crtc) allow again
  2230. * a NULL crtc->primary->fb.
  2231. */
  2232. if (intel_crtc->active && crtc->primary->fb)
  2233. dev_priv->display.update_primary_plane(crtc,
  2234. crtc->primary->fb,
  2235. crtc->x,
  2236. crtc->y);
  2237. drm_modeset_unlock(&crtc->mutex);
  2238. }
  2239. }
  2240. static int
  2241. intel_finish_fb(struct drm_framebuffer *old_fb)
  2242. {
  2243. struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
  2244. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2245. bool was_interruptible = dev_priv->mm.interruptible;
  2246. int ret;
  2247. /* Big Hammer, we also need to ensure that any pending
  2248. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  2249. * current scanout is retired before unpinning the old
  2250. * framebuffer.
  2251. *
  2252. * This should only fail upon a hung GPU, in which case we
  2253. * can safely continue.
  2254. */
  2255. dev_priv->mm.interruptible = false;
  2256. ret = i915_gem_object_finish_gpu(obj);
  2257. dev_priv->mm.interruptible = was_interruptible;
  2258. return ret;
  2259. }
  2260. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  2261. {
  2262. struct drm_device *dev = crtc->dev;
  2263. struct drm_i915_private *dev_priv = dev->dev_private;
  2264. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2265. unsigned long flags;
  2266. bool pending;
  2267. if (i915_reset_in_progress(&dev_priv->gpu_error) ||
  2268. intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  2269. return false;
  2270. spin_lock_irqsave(&dev->event_lock, flags);
  2271. pending = to_intel_crtc(crtc)->unpin_work != NULL;
  2272. spin_unlock_irqrestore(&dev->event_lock, flags);
  2273. return pending;
  2274. }
  2275. static int
  2276. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  2277. struct drm_framebuffer *fb)
  2278. {
  2279. struct drm_device *dev = crtc->dev;
  2280. struct drm_i915_private *dev_priv = dev->dev_private;
  2281. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2282. struct drm_framebuffer *old_fb;
  2283. int ret;
  2284. if (intel_crtc_has_pending_flip(crtc)) {
  2285. DRM_ERROR("pipe is still busy with an old pageflip\n");
  2286. return -EBUSY;
  2287. }
  2288. /* no fb bound */
  2289. if (!fb) {
  2290. DRM_ERROR("No FB bound\n");
  2291. return 0;
  2292. }
  2293. if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
  2294. DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
  2295. plane_name(intel_crtc->plane),
  2296. INTEL_INFO(dev)->num_pipes);
  2297. return -EINVAL;
  2298. }
  2299. mutex_lock(&dev->struct_mutex);
  2300. ret = intel_pin_and_fence_fb_obj(dev,
  2301. to_intel_framebuffer(fb)->obj,
  2302. NULL);
  2303. mutex_unlock(&dev->struct_mutex);
  2304. if (ret != 0) {
  2305. DRM_ERROR("pin & fence failed\n");
  2306. return ret;
  2307. }
  2308. /*
  2309. * Update pipe size and adjust fitter if needed: the reason for this is
  2310. * that in compute_mode_changes we check the native mode (not the pfit
  2311. * mode) to see if we can flip rather than do a full mode set. In the
  2312. * fastboot case, we'll flip, but if we don't update the pipesrc and
  2313. * pfit state, we'll end up with a big fb scanned out into the wrong
  2314. * sized surface.
  2315. *
  2316. * To fix this properly, we need to hoist the checks up into
  2317. * compute_mode_changes (or above), check the actual pfit state and
  2318. * whether the platform allows pfit disable with pipe active, and only
  2319. * then update the pipesrc and pfit state, even on the flip path.
  2320. */
  2321. if (i915.fastboot) {
  2322. const struct drm_display_mode *adjusted_mode =
  2323. &intel_crtc->config.adjusted_mode;
  2324. I915_WRITE(PIPESRC(intel_crtc->pipe),
  2325. ((adjusted_mode->crtc_hdisplay - 1) << 16) |
  2326. (adjusted_mode->crtc_vdisplay - 1));
  2327. if (!intel_crtc->config.pch_pfit.enabled &&
  2328. (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
  2329. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  2330. I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
  2331. I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
  2332. I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
  2333. }
  2334. intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
  2335. intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
  2336. }
  2337. dev_priv->display.update_primary_plane(crtc, fb, x, y);
  2338. old_fb = crtc->primary->fb;
  2339. crtc->primary->fb = fb;
  2340. crtc->x = x;
  2341. crtc->y = y;
  2342. if (old_fb) {
  2343. if (intel_crtc->active && old_fb != fb)
  2344. intel_wait_for_vblank(dev, intel_crtc->pipe);
  2345. mutex_lock(&dev->struct_mutex);
  2346. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  2347. mutex_unlock(&dev->struct_mutex);
  2348. }
  2349. mutex_lock(&dev->struct_mutex);
  2350. intel_update_fbc(dev);
  2351. intel_edp_psr_update(dev);
  2352. mutex_unlock(&dev->struct_mutex);
  2353. return 0;
  2354. }
  2355. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  2356. {
  2357. struct drm_device *dev = crtc->dev;
  2358. struct drm_i915_private *dev_priv = dev->dev_private;
  2359. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2360. int pipe = intel_crtc->pipe;
  2361. u32 reg, temp;
  2362. /* enable normal train */
  2363. reg = FDI_TX_CTL(pipe);
  2364. temp = I915_READ(reg);
  2365. if (IS_IVYBRIDGE(dev)) {
  2366. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2367. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  2368. } else {
  2369. temp &= ~FDI_LINK_TRAIN_NONE;
  2370. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  2371. }
  2372. I915_WRITE(reg, temp);
  2373. reg = FDI_RX_CTL(pipe);
  2374. temp = I915_READ(reg);
  2375. if (HAS_PCH_CPT(dev)) {
  2376. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2377. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  2378. } else {
  2379. temp &= ~FDI_LINK_TRAIN_NONE;
  2380. temp |= FDI_LINK_TRAIN_NONE;
  2381. }
  2382. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  2383. /* wait one idle pattern time */
  2384. POSTING_READ(reg);
  2385. udelay(1000);
  2386. /* IVB wants error correction enabled */
  2387. if (IS_IVYBRIDGE(dev))
  2388. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  2389. FDI_FE_ERRC_ENABLE);
  2390. }
  2391. static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
  2392. {
  2393. return crtc->base.enabled && crtc->active &&
  2394. crtc->config.has_pch_encoder;
  2395. }
  2396. static void ivb_modeset_global_resources(struct drm_device *dev)
  2397. {
  2398. struct drm_i915_private *dev_priv = dev->dev_private;
  2399. struct intel_crtc *pipe_B_crtc =
  2400. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  2401. struct intel_crtc *pipe_C_crtc =
  2402. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
  2403. uint32_t temp;
  2404. /*
  2405. * When everything is off disable fdi C so that we could enable fdi B
  2406. * with all lanes. Note that we don't care about enabled pipes without
  2407. * an enabled pch encoder.
  2408. */
  2409. if (!pipe_has_enabled_pch(pipe_B_crtc) &&
  2410. !pipe_has_enabled_pch(pipe_C_crtc)) {
  2411. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2412. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2413. temp = I915_READ(SOUTH_CHICKEN1);
  2414. temp &= ~FDI_BC_BIFURCATION_SELECT;
  2415. DRM_DEBUG_KMS("disabling fdi C rx\n");
  2416. I915_WRITE(SOUTH_CHICKEN1, temp);
  2417. }
  2418. }
  2419. /* The FDI link training functions for ILK/Ibexpeak. */
  2420. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  2421. {
  2422. struct drm_device *dev = crtc->dev;
  2423. struct drm_i915_private *dev_priv = dev->dev_private;
  2424. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2425. int pipe = intel_crtc->pipe;
  2426. u32 reg, temp, tries;
  2427. /* FDI needs bits from pipe first */
  2428. assert_pipe_enabled(dev_priv, pipe);
  2429. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2430. for train result */
  2431. reg = FDI_RX_IMR(pipe);
  2432. temp = I915_READ(reg);
  2433. temp &= ~FDI_RX_SYMBOL_LOCK;
  2434. temp &= ~FDI_RX_BIT_LOCK;
  2435. I915_WRITE(reg, temp);
  2436. I915_READ(reg);
  2437. udelay(150);
  2438. /* enable CPU FDI TX and PCH FDI RX */
  2439. reg = FDI_TX_CTL(pipe);
  2440. temp = I915_READ(reg);
  2441. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2442. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2443. temp &= ~FDI_LINK_TRAIN_NONE;
  2444. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2445. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2446. reg = FDI_RX_CTL(pipe);
  2447. temp = I915_READ(reg);
  2448. temp &= ~FDI_LINK_TRAIN_NONE;
  2449. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2450. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2451. POSTING_READ(reg);
  2452. udelay(150);
  2453. /* Ironlake workaround, enable clock pointer after FDI enable*/
  2454. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2455. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  2456. FDI_RX_PHASE_SYNC_POINTER_EN);
  2457. reg = FDI_RX_IIR(pipe);
  2458. for (tries = 0; tries < 5; tries++) {
  2459. temp = I915_READ(reg);
  2460. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2461. if ((temp & FDI_RX_BIT_LOCK)) {
  2462. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2463. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2464. break;
  2465. }
  2466. }
  2467. if (tries == 5)
  2468. DRM_ERROR("FDI train 1 fail!\n");
  2469. /* Train 2 */
  2470. reg = FDI_TX_CTL(pipe);
  2471. temp = I915_READ(reg);
  2472. temp &= ~FDI_LINK_TRAIN_NONE;
  2473. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2474. I915_WRITE(reg, temp);
  2475. reg = FDI_RX_CTL(pipe);
  2476. temp = I915_READ(reg);
  2477. temp &= ~FDI_LINK_TRAIN_NONE;
  2478. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2479. I915_WRITE(reg, temp);
  2480. POSTING_READ(reg);
  2481. udelay(150);
  2482. reg = FDI_RX_IIR(pipe);
  2483. for (tries = 0; tries < 5; tries++) {
  2484. temp = I915_READ(reg);
  2485. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2486. if (temp & FDI_RX_SYMBOL_LOCK) {
  2487. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2488. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2489. break;
  2490. }
  2491. }
  2492. if (tries == 5)
  2493. DRM_ERROR("FDI train 2 fail!\n");
  2494. DRM_DEBUG_KMS("FDI train done\n");
  2495. }
  2496. static const int snb_b_fdi_train_param[] = {
  2497. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  2498. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  2499. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  2500. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  2501. };
  2502. /* The FDI link training functions for SNB/Cougarpoint. */
  2503. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  2504. {
  2505. struct drm_device *dev = crtc->dev;
  2506. struct drm_i915_private *dev_priv = dev->dev_private;
  2507. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2508. int pipe = intel_crtc->pipe;
  2509. u32 reg, temp, i, retry;
  2510. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2511. for train result */
  2512. reg = FDI_RX_IMR(pipe);
  2513. temp = I915_READ(reg);
  2514. temp &= ~FDI_RX_SYMBOL_LOCK;
  2515. temp &= ~FDI_RX_BIT_LOCK;
  2516. I915_WRITE(reg, temp);
  2517. POSTING_READ(reg);
  2518. udelay(150);
  2519. /* enable CPU FDI TX and PCH FDI RX */
  2520. reg = FDI_TX_CTL(pipe);
  2521. temp = I915_READ(reg);
  2522. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2523. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2524. temp &= ~FDI_LINK_TRAIN_NONE;
  2525. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2526. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2527. /* SNB-B */
  2528. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2529. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2530. I915_WRITE(FDI_RX_MISC(pipe),
  2531. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2532. reg = FDI_RX_CTL(pipe);
  2533. temp = I915_READ(reg);
  2534. if (HAS_PCH_CPT(dev)) {
  2535. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2536. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2537. } else {
  2538. temp &= ~FDI_LINK_TRAIN_NONE;
  2539. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2540. }
  2541. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2542. POSTING_READ(reg);
  2543. udelay(150);
  2544. for (i = 0; i < 4; i++) {
  2545. reg = FDI_TX_CTL(pipe);
  2546. temp = I915_READ(reg);
  2547. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2548. temp |= snb_b_fdi_train_param[i];
  2549. I915_WRITE(reg, temp);
  2550. POSTING_READ(reg);
  2551. udelay(500);
  2552. for (retry = 0; retry < 5; retry++) {
  2553. reg = FDI_RX_IIR(pipe);
  2554. temp = I915_READ(reg);
  2555. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2556. if (temp & FDI_RX_BIT_LOCK) {
  2557. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2558. DRM_DEBUG_KMS("FDI train 1 done.\n");
  2559. break;
  2560. }
  2561. udelay(50);
  2562. }
  2563. if (retry < 5)
  2564. break;
  2565. }
  2566. if (i == 4)
  2567. DRM_ERROR("FDI train 1 fail!\n");
  2568. /* Train 2 */
  2569. reg = FDI_TX_CTL(pipe);
  2570. temp = I915_READ(reg);
  2571. temp &= ~FDI_LINK_TRAIN_NONE;
  2572. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2573. if (IS_GEN6(dev)) {
  2574. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2575. /* SNB-B */
  2576. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  2577. }
  2578. I915_WRITE(reg, temp);
  2579. reg = FDI_RX_CTL(pipe);
  2580. temp = I915_READ(reg);
  2581. if (HAS_PCH_CPT(dev)) {
  2582. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2583. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2584. } else {
  2585. temp &= ~FDI_LINK_TRAIN_NONE;
  2586. temp |= FDI_LINK_TRAIN_PATTERN_2;
  2587. }
  2588. I915_WRITE(reg, temp);
  2589. POSTING_READ(reg);
  2590. udelay(150);
  2591. for (i = 0; i < 4; i++) {
  2592. reg = FDI_TX_CTL(pipe);
  2593. temp = I915_READ(reg);
  2594. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2595. temp |= snb_b_fdi_train_param[i];
  2596. I915_WRITE(reg, temp);
  2597. POSTING_READ(reg);
  2598. udelay(500);
  2599. for (retry = 0; retry < 5; retry++) {
  2600. reg = FDI_RX_IIR(pipe);
  2601. temp = I915_READ(reg);
  2602. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2603. if (temp & FDI_RX_SYMBOL_LOCK) {
  2604. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2605. DRM_DEBUG_KMS("FDI train 2 done.\n");
  2606. break;
  2607. }
  2608. udelay(50);
  2609. }
  2610. if (retry < 5)
  2611. break;
  2612. }
  2613. if (i == 4)
  2614. DRM_ERROR("FDI train 2 fail!\n");
  2615. DRM_DEBUG_KMS("FDI train done.\n");
  2616. }
  2617. /* Manual link training for Ivy Bridge A0 parts */
  2618. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  2619. {
  2620. struct drm_device *dev = crtc->dev;
  2621. struct drm_i915_private *dev_priv = dev->dev_private;
  2622. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2623. int pipe = intel_crtc->pipe;
  2624. u32 reg, temp, i, j;
  2625. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  2626. for train result */
  2627. reg = FDI_RX_IMR(pipe);
  2628. temp = I915_READ(reg);
  2629. temp &= ~FDI_RX_SYMBOL_LOCK;
  2630. temp &= ~FDI_RX_BIT_LOCK;
  2631. I915_WRITE(reg, temp);
  2632. POSTING_READ(reg);
  2633. udelay(150);
  2634. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  2635. I915_READ(FDI_RX_IIR(pipe)));
  2636. /* Try each vswing and preemphasis setting twice before moving on */
  2637. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  2638. /* disable first in case we need to retry */
  2639. reg = FDI_TX_CTL(pipe);
  2640. temp = I915_READ(reg);
  2641. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  2642. temp &= ~FDI_TX_ENABLE;
  2643. I915_WRITE(reg, temp);
  2644. reg = FDI_RX_CTL(pipe);
  2645. temp = I915_READ(reg);
  2646. temp &= ~FDI_LINK_TRAIN_AUTO;
  2647. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2648. temp &= ~FDI_RX_ENABLE;
  2649. I915_WRITE(reg, temp);
  2650. /* enable CPU FDI TX and PCH FDI RX */
  2651. reg = FDI_TX_CTL(pipe);
  2652. temp = I915_READ(reg);
  2653. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  2654. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2655. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  2656. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  2657. temp |= snb_b_fdi_train_param[j/2];
  2658. temp |= FDI_COMPOSITE_SYNC;
  2659. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  2660. I915_WRITE(FDI_RX_MISC(pipe),
  2661. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  2662. reg = FDI_RX_CTL(pipe);
  2663. temp = I915_READ(reg);
  2664. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2665. temp |= FDI_COMPOSITE_SYNC;
  2666. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  2667. POSTING_READ(reg);
  2668. udelay(1); /* should be 0.5us */
  2669. for (i = 0; i < 4; i++) {
  2670. reg = FDI_RX_IIR(pipe);
  2671. temp = I915_READ(reg);
  2672. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2673. if (temp & FDI_RX_BIT_LOCK ||
  2674. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  2675. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  2676. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  2677. i);
  2678. break;
  2679. }
  2680. udelay(1); /* should be 0.5us */
  2681. }
  2682. if (i == 4) {
  2683. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  2684. continue;
  2685. }
  2686. /* Train 2 */
  2687. reg = FDI_TX_CTL(pipe);
  2688. temp = I915_READ(reg);
  2689. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  2690. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  2691. I915_WRITE(reg, temp);
  2692. reg = FDI_RX_CTL(pipe);
  2693. temp = I915_READ(reg);
  2694. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2695. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  2696. I915_WRITE(reg, temp);
  2697. POSTING_READ(reg);
  2698. udelay(2); /* should be 1.5us */
  2699. for (i = 0; i < 4; i++) {
  2700. reg = FDI_RX_IIR(pipe);
  2701. temp = I915_READ(reg);
  2702. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  2703. if (temp & FDI_RX_SYMBOL_LOCK ||
  2704. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  2705. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  2706. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  2707. i);
  2708. goto train_done;
  2709. }
  2710. udelay(2); /* should be 1.5us */
  2711. }
  2712. if (i == 4)
  2713. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  2714. }
  2715. train_done:
  2716. DRM_DEBUG_KMS("FDI train done.\n");
  2717. }
  2718. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  2719. {
  2720. struct drm_device *dev = intel_crtc->base.dev;
  2721. struct drm_i915_private *dev_priv = dev->dev_private;
  2722. int pipe = intel_crtc->pipe;
  2723. u32 reg, temp;
  2724. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  2725. reg = FDI_RX_CTL(pipe);
  2726. temp = I915_READ(reg);
  2727. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  2728. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
  2729. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2730. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  2731. POSTING_READ(reg);
  2732. udelay(200);
  2733. /* Switch from Rawclk to PCDclk */
  2734. temp = I915_READ(reg);
  2735. I915_WRITE(reg, temp | FDI_PCDCLK);
  2736. POSTING_READ(reg);
  2737. udelay(200);
  2738. /* Enable CPU FDI TX PLL, always on for Ironlake */
  2739. reg = FDI_TX_CTL(pipe);
  2740. temp = I915_READ(reg);
  2741. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  2742. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  2743. POSTING_READ(reg);
  2744. udelay(100);
  2745. }
  2746. }
  2747. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  2748. {
  2749. struct drm_device *dev = intel_crtc->base.dev;
  2750. struct drm_i915_private *dev_priv = dev->dev_private;
  2751. int pipe = intel_crtc->pipe;
  2752. u32 reg, temp;
  2753. /* Switch from PCDclk to Rawclk */
  2754. reg = FDI_RX_CTL(pipe);
  2755. temp = I915_READ(reg);
  2756. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  2757. /* Disable CPU FDI TX PLL */
  2758. reg = FDI_TX_CTL(pipe);
  2759. temp = I915_READ(reg);
  2760. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  2761. POSTING_READ(reg);
  2762. udelay(100);
  2763. reg = FDI_RX_CTL(pipe);
  2764. temp = I915_READ(reg);
  2765. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  2766. /* Wait for the clocks to turn off. */
  2767. POSTING_READ(reg);
  2768. udelay(100);
  2769. }
  2770. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  2771. {
  2772. struct drm_device *dev = crtc->dev;
  2773. struct drm_i915_private *dev_priv = dev->dev_private;
  2774. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2775. int pipe = intel_crtc->pipe;
  2776. u32 reg, temp;
  2777. /* disable CPU FDI tx and PCH FDI rx */
  2778. reg = FDI_TX_CTL(pipe);
  2779. temp = I915_READ(reg);
  2780. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  2781. POSTING_READ(reg);
  2782. reg = FDI_RX_CTL(pipe);
  2783. temp = I915_READ(reg);
  2784. temp &= ~(0x7 << 16);
  2785. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2786. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  2787. POSTING_READ(reg);
  2788. udelay(100);
  2789. /* Ironlake workaround, disable clock pointer after downing FDI */
  2790. if (HAS_PCH_IBX(dev))
  2791. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  2792. /* still set train pattern 1 */
  2793. reg = FDI_TX_CTL(pipe);
  2794. temp = I915_READ(reg);
  2795. temp &= ~FDI_LINK_TRAIN_NONE;
  2796. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2797. I915_WRITE(reg, temp);
  2798. reg = FDI_RX_CTL(pipe);
  2799. temp = I915_READ(reg);
  2800. if (HAS_PCH_CPT(dev)) {
  2801. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  2802. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  2803. } else {
  2804. temp &= ~FDI_LINK_TRAIN_NONE;
  2805. temp |= FDI_LINK_TRAIN_PATTERN_1;
  2806. }
  2807. /* BPC in FDI rx is consistent with that in PIPECONF */
  2808. temp &= ~(0x07 << 16);
  2809. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  2810. I915_WRITE(reg, temp);
  2811. POSTING_READ(reg);
  2812. udelay(100);
  2813. }
  2814. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  2815. {
  2816. struct intel_crtc *crtc;
  2817. /* Note that we don't need to be called with mode_config.lock here
  2818. * as our list of CRTC objects is static for the lifetime of the
  2819. * device and so cannot disappear as we iterate. Similarly, we can
  2820. * happily treat the predicates as racy, atomic checks as userspace
  2821. * cannot claim and pin a new fb without at least acquring the
  2822. * struct_mutex and so serialising with us.
  2823. */
  2824. for_each_intel_crtc(dev, crtc) {
  2825. if (atomic_read(&crtc->unpin_work_count) == 0)
  2826. continue;
  2827. if (crtc->unpin_work)
  2828. intel_wait_for_vblank(dev, crtc->pipe);
  2829. return true;
  2830. }
  2831. return false;
  2832. }
  2833. void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  2834. {
  2835. struct drm_device *dev = crtc->dev;
  2836. struct drm_i915_private *dev_priv = dev->dev_private;
  2837. if (crtc->primary->fb == NULL)
  2838. return;
  2839. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  2840. WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
  2841. !intel_crtc_has_pending_flip(crtc),
  2842. 60*HZ) == 0);
  2843. mutex_lock(&dev->struct_mutex);
  2844. intel_finish_fb(crtc->primary->fb);
  2845. mutex_unlock(&dev->struct_mutex);
  2846. }
  2847. /* Program iCLKIP clock to the desired frequency */
  2848. static void lpt_program_iclkip(struct drm_crtc *crtc)
  2849. {
  2850. struct drm_device *dev = crtc->dev;
  2851. struct drm_i915_private *dev_priv = dev->dev_private;
  2852. int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
  2853. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  2854. u32 temp;
  2855. mutex_lock(&dev_priv->dpio_lock);
  2856. /* It is necessary to ungate the pixclk gate prior to programming
  2857. * the divisors, and gate it back when it is done.
  2858. */
  2859. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  2860. /* Disable SSCCTL */
  2861. intel_sbi_write(dev_priv, SBI_SSCCTL6,
  2862. intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
  2863. SBI_SSCCTL_DISABLE,
  2864. SBI_ICLK);
  2865. /* 20MHz is a corner case which is out of range for the 7-bit divisor */
  2866. if (clock == 20000) {
  2867. auxdiv = 1;
  2868. divsel = 0x41;
  2869. phaseinc = 0x20;
  2870. } else {
  2871. /* The iCLK virtual clock root frequency is in MHz,
  2872. * but the adjusted_mode->crtc_clock in in KHz. To get the
  2873. * divisors, it is necessary to divide one by another, so we
  2874. * convert the virtual clock precision to KHz here for higher
  2875. * precision.
  2876. */
  2877. u32 iclk_virtual_root_freq = 172800 * 1000;
  2878. u32 iclk_pi_range = 64;
  2879. u32 desired_divisor, msb_divisor_value, pi_value;
  2880. desired_divisor = (iclk_virtual_root_freq / clock);
  2881. msb_divisor_value = desired_divisor / iclk_pi_range;
  2882. pi_value = desired_divisor % iclk_pi_range;
  2883. auxdiv = 0;
  2884. divsel = msb_divisor_value - 2;
  2885. phaseinc = pi_value;
  2886. }
  2887. /* This should not happen with any sane values */
  2888. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  2889. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  2890. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  2891. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  2892. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  2893. clock,
  2894. auxdiv,
  2895. divsel,
  2896. phasedir,
  2897. phaseinc);
  2898. /* Program SSCDIVINTPHASE6 */
  2899. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  2900. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  2901. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  2902. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  2903. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  2904. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  2905. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  2906. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  2907. /* Program SSCAUXDIV */
  2908. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  2909. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  2910. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  2911. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  2912. /* Enable modulator and associated divider */
  2913. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  2914. temp &= ~SBI_SSCCTL_DISABLE;
  2915. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  2916. /* Wait for initialization time */
  2917. udelay(24);
  2918. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  2919. mutex_unlock(&dev_priv->dpio_lock);
  2920. }
  2921. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  2922. enum pipe pch_transcoder)
  2923. {
  2924. struct drm_device *dev = crtc->base.dev;
  2925. struct drm_i915_private *dev_priv = dev->dev_private;
  2926. enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
  2927. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  2928. I915_READ(HTOTAL(cpu_transcoder)));
  2929. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  2930. I915_READ(HBLANK(cpu_transcoder)));
  2931. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  2932. I915_READ(HSYNC(cpu_transcoder)));
  2933. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  2934. I915_READ(VTOTAL(cpu_transcoder)));
  2935. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  2936. I915_READ(VBLANK(cpu_transcoder)));
  2937. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  2938. I915_READ(VSYNC(cpu_transcoder)));
  2939. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  2940. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  2941. }
  2942. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  2943. {
  2944. struct drm_i915_private *dev_priv = dev->dev_private;
  2945. uint32_t temp;
  2946. temp = I915_READ(SOUTH_CHICKEN1);
  2947. if (temp & FDI_BC_BIFURCATION_SELECT)
  2948. return;
  2949. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  2950. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  2951. temp |= FDI_BC_BIFURCATION_SELECT;
  2952. DRM_DEBUG_KMS("enabling fdi C rx\n");
  2953. I915_WRITE(SOUTH_CHICKEN1, temp);
  2954. POSTING_READ(SOUTH_CHICKEN1);
  2955. }
  2956. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  2957. {
  2958. struct drm_device *dev = intel_crtc->base.dev;
  2959. struct drm_i915_private *dev_priv = dev->dev_private;
  2960. switch (intel_crtc->pipe) {
  2961. case PIPE_A:
  2962. break;
  2963. case PIPE_B:
  2964. if (intel_crtc->config.fdi_lanes > 2)
  2965. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  2966. else
  2967. cpt_enable_fdi_bc_bifurcation(dev);
  2968. break;
  2969. case PIPE_C:
  2970. cpt_enable_fdi_bc_bifurcation(dev);
  2971. break;
  2972. default:
  2973. BUG();
  2974. }
  2975. }
  2976. /*
  2977. * Enable PCH resources required for PCH ports:
  2978. * - PCH PLLs
  2979. * - FDI training & RX/TX
  2980. * - update transcoder timings
  2981. * - DP transcoding bits
  2982. * - transcoder
  2983. */
  2984. static void ironlake_pch_enable(struct drm_crtc *crtc)
  2985. {
  2986. struct drm_device *dev = crtc->dev;
  2987. struct drm_i915_private *dev_priv = dev->dev_private;
  2988. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2989. int pipe = intel_crtc->pipe;
  2990. u32 reg, temp;
  2991. assert_pch_transcoder_disabled(dev_priv, pipe);
  2992. if (IS_IVYBRIDGE(dev))
  2993. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  2994. /* Write the TU size bits before fdi link training, so that error
  2995. * detection works. */
  2996. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  2997. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  2998. /* For PCH output, training FDI link */
  2999. dev_priv->display.fdi_link_train(crtc);
  3000. /* We need to program the right clock selection before writing the pixel
  3001. * mutliplier into the DPLL. */
  3002. if (HAS_PCH_CPT(dev)) {
  3003. u32 sel;
  3004. temp = I915_READ(PCH_DPLL_SEL);
  3005. temp |= TRANS_DPLL_ENABLE(pipe);
  3006. sel = TRANS_DPLLB_SEL(pipe);
  3007. if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
  3008. temp |= sel;
  3009. else
  3010. temp &= ~sel;
  3011. I915_WRITE(PCH_DPLL_SEL, temp);
  3012. }
  3013. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3014. * transcoder, and we actually should do this to not upset any PCH
  3015. * transcoder that already use the clock when we share it.
  3016. *
  3017. * Note that enable_shared_dpll tries to do the right thing, but
  3018. * get_shared_dpll unconditionally resets the pll - we need that to have
  3019. * the right LVDS enable sequence. */
  3020. intel_enable_shared_dpll(intel_crtc);
  3021. /* set transcoder timing, panel must allow it */
  3022. assert_panel_unlocked(dev_priv, pipe);
  3023. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3024. intel_fdi_normal_train(crtc);
  3025. /* For PCH DP, enable TRANS_DP_CTL */
  3026. if (HAS_PCH_CPT(dev) &&
  3027. (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  3028. intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
  3029. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3030. reg = TRANS_DP_CTL(pipe);
  3031. temp = I915_READ(reg);
  3032. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3033. TRANS_DP_SYNC_MASK |
  3034. TRANS_DP_BPC_MASK);
  3035. temp |= (TRANS_DP_OUTPUT_ENABLE |
  3036. TRANS_DP_ENH_FRAMING);
  3037. temp |= bpc << 9; /* same format but at 11:9 */
  3038. if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
  3039. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3040. if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
  3041. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3042. switch (intel_trans_dp_port_sel(crtc)) {
  3043. case PCH_DP_B:
  3044. temp |= TRANS_DP_PORT_SEL_B;
  3045. break;
  3046. case PCH_DP_C:
  3047. temp |= TRANS_DP_PORT_SEL_C;
  3048. break;
  3049. case PCH_DP_D:
  3050. temp |= TRANS_DP_PORT_SEL_D;
  3051. break;
  3052. default:
  3053. BUG();
  3054. }
  3055. I915_WRITE(reg, temp);
  3056. }
  3057. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3058. }
  3059. static void lpt_pch_enable(struct drm_crtc *crtc)
  3060. {
  3061. struct drm_device *dev = crtc->dev;
  3062. struct drm_i915_private *dev_priv = dev->dev_private;
  3063. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3064. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3065. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3066. lpt_program_iclkip(crtc);
  3067. /* Set transcoder timing. */
  3068. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3069. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3070. }
  3071. static void intel_put_shared_dpll(struct intel_crtc *crtc)
  3072. {
  3073. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3074. if (pll == NULL)
  3075. return;
  3076. if (pll->refcount == 0) {
  3077. WARN(1, "bad %s refcount\n", pll->name);
  3078. return;
  3079. }
  3080. if (--pll->refcount == 0) {
  3081. WARN_ON(pll->on);
  3082. WARN_ON(pll->active);
  3083. }
  3084. crtc->config.shared_dpll = DPLL_ID_PRIVATE;
  3085. }
  3086. static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
  3087. {
  3088. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  3089. struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
  3090. enum intel_dpll_id i;
  3091. if (pll) {
  3092. DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
  3093. crtc->base.base.id, pll->name);
  3094. intel_put_shared_dpll(crtc);
  3095. }
  3096. if (HAS_PCH_IBX(dev_priv->dev)) {
  3097. /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
  3098. i = (enum intel_dpll_id) crtc->pipe;
  3099. pll = &dev_priv->shared_dplls[i];
  3100. DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
  3101. crtc->base.base.id, pll->name);
  3102. WARN_ON(pll->refcount);
  3103. goto found;
  3104. }
  3105. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3106. pll = &dev_priv->shared_dplls[i];
  3107. /* Only want to check enabled timings first */
  3108. if (pll->refcount == 0)
  3109. continue;
  3110. if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
  3111. sizeof(pll->hw_state)) == 0) {
  3112. DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
  3113. crtc->base.base.id,
  3114. pll->name, pll->refcount, pll->active);
  3115. goto found;
  3116. }
  3117. }
  3118. /* Ok no matching timings, maybe there's a free one? */
  3119. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  3120. pll = &dev_priv->shared_dplls[i];
  3121. if (pll->refcount == 0) {
  3122. DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
  3123. crtc->base.base.id, pll->name);
  3124. goto found;
  3125. }
  3126. }
  3127. return NULL;
  3128. found:
  3129. if (pll->refcount == 0)
  3130. pll->hw_state = crtc->config.dpll_hw_state;
  3131. crtc->config.shared_dpll = i;
  3132. DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
  3133. pipe_name(crtc->pipe));
  3134. pll->refcount++;
  3135. return pll;
  3136. }
  3137. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3138. {
  3139. struct drm_i915_private *dev_priv = dev->dev_private;
  3140. int dslreg = PIPEDSL(pipe);
  3141. u32 temp;
  3142. temp = I915_READ(dslreg);
  3143. udelay(500);
  3144. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3145. if (wait_for(I915_READ(dslreg) != temp, 5))
  3146. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3147. }
  3148. }
  3149. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  3150. {
  3151. struct drm_device *dev = crtc->base.dev;
  3152. struct drm_i915_private *dev_priv = dev->dev_private;
  3153. int pipe = crtc->pipe;
  3154. if (crtc->config.pch_pfit.enabled) {
  3155. /* Force use of hard-coded filter coefficients
  3156. * as some pre-programmed values are broken,
  3157. * e.g. x201.
  3158. */
  3159. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
  3160. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  3161. PF_PIPE_SEL_IVB(pipe));
  3162. else
  3163. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  3164. I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
  3165. I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
  3166. }
  3167. }
  3168. static void intel_enable_planes(struct drm_crtc *crtc)
  3169. {
  3170. struct drm_device *dev = crtc->dev;
  3171. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3172. struct drm_plane *plane;
  3173. struct intel_plane *intel_plane;
  3174. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3175. intel_plane = to_intel_plane(plane);
  3176. if (intel_plane->pipe == pipe)
  3177. intel_plane_restore(&intel_plane->base);
  3178. }
  3179. }
  3180. static void intel_disable_planes(struct drm_crtc *crtc)
  3181. {
  3182. struct drm_device *dev = crtc->dev;
  3183. enum pipe pipe = to_intel_crtc(crtc)->pipe;
  3184. struct drm_plane *plane;
  3185. struct intel_plane *intel_plane;
  3186. drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
  3187. intel_plane = to_intel_plane(plane);
  3188. if (intel_plane->pipe == pipe)
  3189. intel_plane_disable(&intel_plane->base);
  3190. }
  3191. }
  3192. void hsw_enable_ips(struct intel_crtc *crtc)
  3193. {
  3194. struct drm_device *dev = crtc->base.dev;
  3195. struct drm_i915_private *dev_priv = dev->dev_private;
  3196. if (!crtc->config.ips_enabled)
  3197. return;
  3198. /* We can only enable IPS after we enable a plane and wait for a vblank */
  3199. intel_wait_for_vblank(dev, crtc->pipe);
  3200. assert_plane_enabled(dev_priv, crtc->plane);
  3201. if (IS_BROADWELL(dev)) {
  3202. mutex_lock(&dev_priv->rps.hw_lock);
  3203. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  3204. mutex_unlock(&dev_priv->rps.hw_lock);
  3205. /* Quoting Art Runyan: "its not safe to expect any particular
  3206. * value in IPS_CTL bit 31 after enabling IPS through the
  3207. * mailbox." Moreover, the mailbox may return a bogus state,
  3208. * so we need to just enable it and continue on.
  3209. */
  3210. } else {
  3211. I915_WRITE(IPS_CTL, IPS_ENABLE);
  3212. /* The bit only becomes 1 in the next vblank, so this wait here
  3213. * is essentially intel_wait_for_vblank. If we don't have this
  3214. * and don't wait for vblanks until the end of crtc_enable, then
  3215. * the HW state readout code will complain that the expected
  3216. * IPS_CTL value is not the one we read. */
  3217. if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
  3218. DRM_ERROR("Timed out waiting for IPS enable\n");
  3219. }
  3220. }
  3221. void hsw_disable_ips(struct intel_crtc *crtc)
  3222. {
  3223. struct drm_device *dev = crtc->base.dev;
  3224. struct drm_i915_private *dev_priv = dev->dev_private;
  3225. if (!crtc->config.ips_enabled)
  3226. return;
  3227. assert_plane_enabled(dev_priv, crtc->plane);
  3228. if (IS_BROADWELL(dev)) {
  3229. mutex_lock(&dev_priv->rps.hw_lock);
  3230. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  3231. mutex_unlock(&dev_priv->rps.hw_lock);
  3232. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  3233. if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
  3234. DRM_ERROR("Timed out waiting for IPS disable\n");
  3235. } else {
  3236. I915_WRITE(IPS_CTL, 0);
  3237. POSTING_READ(IPS_CTL);
  3238. }
  3239. /* We need to wait for a vblank before we can disable the plane. */
  3240. intel_wait_for_vblank(dev, crtc->pipe);
  3241. }
  3242. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3243. static void intel_crtc_load_lut(struct drm_crtc *crtc)
  3244. {
  3245. struct drm_device *dev = crtc->dev;
  3246. struct drm_i915_private *dev_priv = dev->dev_private;
  3247. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3248. enum pipe pipe = intel_crtc->pipe;
  3249. int palreg = PALETTE(pipe);
  3250. int i;
  3251. bool reenable_ips = false;
  3252. /* The clocks have to be on to load the palette. */
  3253. if (!crtc->enabled || !intel_crtc->active)
  3254. return;
  3255. if (!HAS_PCH_SPLIT(dev_priv->dev)) {
  3256. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
  3257. assert_dsi_pll_enabled(dev_priv);
  3258. else
  3259. assert_pll_enabled(dev_priv, pipe);
  3260. }
  3261. /* use legacy palette for Ironlake */
  3262. if (HAS_PCH_SPLIT(dev))
  3263. palreg = LGC_PALETTE(pipe);
  3264. /* Workaround : Do not read or write the pipe palette/gamma data while
  3265. * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
  3266. */
  3267. if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
  3268. ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
  3269. GAMMA_MODE_MODE_SPLIT)) {
  3270. hsw_disable_ips(intel_crtc);
  3271. reenable_ips = true;
  3272. }
  3273. for (i = 0; i < 256; i++) {
  3274. I915_WRITE(palreg + 4 * i,
  3275. (intel_crtc->lut_r[i] << 16) |
  3276. (intel_crtc->lut_g[i] << 8) |
  3277. intel_crtc->lut_b[i]);
  3278. }
  3279. if (reenable_ips)
  3280. hsw_enable_ips(intel_crtc);
  3281. }
  3282. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  3283. {
  3284. if (!enable && intel_crtc->overlay) {
  3285. struct drm_device *dev = intel_crtc->base.dev;
  3286. struct drm_i915_private *dev_priv = dev->dev_private;
  3287. mutex_lock(&dev->struct_mutex);
  3288. dev_priv->mm.interruptible = false;
  3289. (void) intel_overlay_switch_off(intel_crtc->overlay);
  3290. dev_priv->mm.interruptible = true;
  3291. mutex_unlock(&dev->struct_mutex);
  3292. }
  3293. /* Let userspace switch the overlay on again. In most cases userspace
  3294. * has to recompute where to put it anyway.
  3295. */
  3296. }
  3297. /**
  3298. * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
  3299. * cursor plane briefly if not already running after enabling the display
  3300. * plane.
  3301. * This workaround avoids occasional blank screens when self refresh is
  3302. * enabled.
  3303. */
  3304. static void
  3305. g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
  3306. {
  3307. u32 cntl = I915_READ(CURCNTR(pipe));
  3308. if ((cntl & CURSOR_MODE) == 0) {
  3309. u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
  3310. I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
  3311. I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
  3312. intel_wait_for_vblank(dev_priv->dev, pipe);
  3313. I915_WRITE(CURCNTR(pipe), cntl);
  3314. I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
  3315. I915_WRITE(FW_BLC_SELF, fw_bcl_self);
  3316. }
  3317. }
  3318. static void intel_crtc_enable_planes(struct drm_crtc *crtc)
  3319. {
  3320. struct drm_device *dev = crtc->dev;
  3321. struct drm_i915_private *dev_priv = dev->dev_private;
  3322. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3323. int pipe = intel_crtc->pipe;
  3324. int plane = intel_crtc->plane;
  3325. intel_enable_primary_hw_plane(dev_priv, plane, pipe);
  3326. intel_enable_planes(crtc);
  3327. /* The fixup needs to happen before cursor is enabled */
  3328. if (IS_G4X(dev))
  3329. g4x_fixup_plane(dev_priv, pipe);
  3330. intel_crtc_update_cursor(crtc, true);
  3331. intel_crtc_dpms_overlay(intel_crtc, true);
  3332. hsw_enable_ips(intel_crtc);
  3333. mutex_lock(&dev->struct_mutex);
  3334. intel_update_fbc(dev);
  3335. intel_edp_psr_update(dev);
  3336. mutex_unlock(&dev->struct_mutex);
  3337. }
  3338. static void intel_crtc_disable_planes(struct drm_crtc *crtc)
  3339. {
  3340. struct drm_device *dev = crtc->dev;
  3341. struct drm_i915_private *dev_priv = dev->dev_private;
  3342. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3343. int pipe = intel_crtc->pipe;
  3344. int plane = intel_crtc->plane;
  3345. intel_crtc_wait_for_pending_flips(crtc);
  3346. drm_crtc_vblank_off(crtc);
  3347. if (dev_priv->fbc.plane == plane)
  3348. intel_disable_fbc(dev);
  3349. hsw_disable_ips(intel_crtc);
  3350. intel_crtc_dpms_overlay(intel_crtc, false);
  3351. intel_crtc_update_cursor(crtc, false);
  3352. intel_disable_planes(crtc);
  3353. intel_disable_primary_hw_plane(dev_priv, plane, pipe);
  3354. }
  3355. static void ironlake_crtc_enable(struct drm_crtc *crtc)
  3356. {
  3357. struct drm_device *dev = crtc->dev;
  3358. struct drm_i915_private *dev_priv = dev->dev_private;
  3359. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3360. struct intel_encoder *encoder;
  3361. int pipe = intel_crtc->pipe;
  3362. enum plane plane = intel_crtc->plane;
  3363. WARN_ON(!crtc->enabled);
  3364. if (intel_crtc->active)
  3365. return;
  3366. if (intel_crtc->config.has_pch_encoder)
  3367. intel_prepare_shared_dpll(intel_crtc);
  3368. if (intel_crtc->config.has_dp_encoder)
  3369. intel_dp_set_m_n(intel_crtc);
  3370. intel_set_pipe_timings(intel_crtc);
  3371. if (intel_crtc->config.has_pch_encoder) {
  3372. intel_cpu_transcoder_set_m_n(intel_crtc,
  3373. &intel_crtc->config.fdi_m_n);
  3374. }
  3375. ironlake_set_pipeconf(crtc);
  3376. /* Set up the display plane register */
  3377. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  3378. POSTING_READ(DSPCNTR(plane));
  3379. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3380. crtc->x, crtc->y);
  3381. intel_crtc->active = true;
  3382. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3383. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3384. for_each_encoder_on_crtc(dev, crtc, encoder)
  3385. if (encoder->pre_enable)
  3386. encoder->pre_enable(encoder);
  3387. if (intel_crtc->config.has_pch_encoder) {
  3388. /* Note: FDI PLL enabling _must_ be done before we enable the
  3389. * cpu pipes, hence this is separate from all the other fdi/pch
  3390. * enabling. */
  3391. ironlake_fdi_pll_enable(intel_crtc);
  3392. } else {
  3393. assert_fdi_tx_disabled(dev_priv, pipe);
  3394. assert_fdi_rx_disabled(dev_priv, pipe);
  3395. }
  3396. ironlake_pfit_enable(intel_crtc);
  3397. /*
  3398. * On ILK+ LUT must be loaded before the pipe is running but with
  3399. * clocks enabled
  3400. */
  3401. intel_crtc_load_lut(crtc);
  3402. intel_update_watermarks(crtc);
  3403. intel_enable_pipe(intel_crtc);
  3404. if (intel_crtc->config.has_pch_encoder)
  3405. ironlake_pch_enable(crtc);
  3406. for_each_encoder_on_crtc(dev, crtc, encoder)
  3407. encoder->enable(encoder);
  3408. if (HAS_PCH_CPT(dev))
  3409. cpt_verify_modeset(dev, intel_crtc->pipe);
  3410. intel_crtc_enable_planes(crtc);
  3411. drm_crtc_vblank_on(crtc);
  3412. }
  3413. /* IPS only exists on ULT machines and is tied to pipe A. */
  3414. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  3415. {
  3416. return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
  3417. }
  3418. /*
  3419. * This implements the workaround described in the "notes" section of the mode
  3420. * set sequence documentation. When going from no pipes or single pipe to
  3421. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  3422. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  3423. */
  3424. static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
  3425. {
  3426. struct drm_device *dev = crtc->base.dev;
  3427. struct intel_crtc *crtc_it, *other_active_crtc = NULL;
  3428. /* We want to get the other_active_crtc only if there's only 1 other
  3429. * active crtc. */
  3430. for_each_intel_crtc(dev, crtc_it) {
  3431. if (!crtc_it->active || crtc_it == crtc)
  3432. continue;
  3433. if (other_active_crtc)
  3434. return;
  3435. other_active_crtc = crtc_it;
  3436. }
  3437. if (!other_active_crtc)
  3438. return;
  3439. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3440. intel_wait_for_vblank(dev, other_active_crtc->pipe);
  3441. }
  3442. static void haswell_crtc_enable(struct drm_crtc *crtc)
  3443. {
  3444. struct drm_device *dev = crtc->dev;
  3445. struct drm_i915_private *dev_priv = dev->dev_private;
  3446. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3447. struct intel_encoder *encoder;
  3448. int pipe = intel_crtc->pipe;
  3449. enum plane plane = intel_crtc->plane;
  3450. WARN_ON(!crtc->enabled);
  3451. if (intel_crtc->active)
  3452. return;
  3453. if (intel_crtc->config.has_dp_encoder)
  3454. intel_dp_set_m_n(intel_crtc);
  3455. intel_set_pipe_timings(intel_crtc);
  3456. if (intel_crtc->config.has_pch_encoder) {
  3457. intel_cpu_transcoder_set_m_n(intel_crtc,
  3458. &intel_crtc->config.fdi_m_n);
  3459. }
  3460. haswell_set_pipeconf(crtc);
  3461. intel_set_pipe_csc(crtc);
  3462. /* Set up the display plane register */
  3463. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  3464. POSTING_READ(DSPCNTR(plane));
  3465. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3466. crtc->x, crtc->y);
  3467. intel_crtc->active = true;
  3468. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3469. if (intel_crtc->config.has_pch_encoder)
  3470. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3471. if (intel_crtc->config.has_pch_encoder)
  3472. dev_priv->display.fdi_link_train(crtc);
  3473. for_each_encoder_on_crtc(dev, crtc, encoder)
  3474. if (encoder->pre_enable)
  3475. encoder->pre_enable(encoder);
  3476. intel_ddi_enable_pipe_clock(intel_crtc);
  3477. ironlake_pfit_enable(intel_crtc);
  3478. /*
  3479. * On ILK+ LUT must be loaded before the pipe is running but with
  3480. * clocks enabled
  3481. */
  3482. intel_crtc_load_lut(crtc);
  3483. intel_ddi_set_pipe_settings(crtc);
  3484. intel_ddi_enable_transcoder_func(crtc);
  3485. intel_update_watermarks(crtc);
  3486. intel_enable_pipe(intel_crtc);
  3487. if (intel_crtc->config.has_pch_encoder)
  3488. lpt_pch_enable(crtc);
  3489. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3490. encoder->enable(encoder);
  3491. intel_opregion_notify_encoder(encoder, true);
  3492. }
  3493. /* If we change the relative order between pipe/planes enabling, we need
  3494. * to change the workaround. */
  3495. haswell_mode_set_planes_workaround(intel_crtc);
  3496. intel_crtc_enable_planes(crtc);
  3497. drm_crtc_vblank_on(crtc);
  3498. }
  3499. static void ironlake_pfit_disable(struct intel_crtc *crtc)
  3500. {
  3501. struct drm_device *dev = crtc->base.dev;
  3502. struct drm_i915_private *dev_priv = dev->dev_private;
  3503. int pipe = crtc->pipe;
  3504. /* To avoid upsetting the power well on haswell only disable the pfit if
  3505. * it's in use. The hw state code will make sure we get this right. */
  3506. if (crtc->config.pch_pfit.enabled) {
  3507. I915_WRITE(PF_CTL(pipe), 0);
  3508. I915_WRITE(PF_WIN_POS(pipe), 0);
  3509. I915_WRITE(PF_WIN_SZ(pipe), 0);
  3510. }
  3511. }
  3512. static void ironlake_crtc_disable(struct drm_crtc *crtc)
  3513. {
  3514. struct drm_device *dev = crtc->dev;
  3515. struct drm_i915_private *dev_priv = dev->dev_private;
  3516. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3517. struct intel_encoder *encoder;
  3518. int pipe = intel_crtc->pipe;
  3519. u32 reg, temp;
  3520. if (!intel_crtc->active)
  3521. return;
  3522. intel_crtc_disable_planes(crtc);
  3523. for_each_encoder_on_crtc(dev, crtc, encoder)
  3524. encoder->disable(encoder);
  3525. if (intel_crtc->config.has_pch_encoder)
  3526. intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
  3527. intel_disable_pipe(dev_priv, pipe);
  3528. ironlake_pfit_disable(intel_crtc);
  3529. for_each_encoder_on_crtc(dev, crtc, encoder)
  3530. if (encoder->post_disable)
  3531. encoder->post_disable(encoder);
  3532. if (intel_crtc->config.has_pch_encoder) {
  3533. ironlake_fdi_disable(crtc);
  3534. ironlake_disable_pch_transcoder(dev_priv, pipe);
  3535. intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
  3536. if (HAS_PCH_CPT(dev)) {
  3537. /* disable TRANS_DP_CTL */
  3538. reg = TRANS_DP_CTL(pipe);
  3539. temp = I915_READ(reg);
  3540. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  3541. TRANS_DP_PORT_SEL_MASK);
  3542. temp |= TRANS_DP_PORT_SEL_NONE;
  3543. I915_WRITE(reg, temp);
  3544. /* disable DPLL_SEL */
  3545. temp = I915_READ(PCH_DPLL_SEL);
  3546. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  3547. I915_WRITE(PCH_DPLL_SEL, temp);
  3548. }
  3549. /* disable PCH DPLL */
  3550. intel_disable_shared_dpll(intel_crtc);
  3551. ironlake_fdi_pll_disable(intel_crtc);
  3552. }
  3553. intel_crtc->active = false;
  3554. intel_update_watermarks(crtc);
  3555. mutex_lock(&dev->struct_mutex);
  3556. intel_update_fbc(dev);
  3557. intel_edp_psr_update(dev);
  3558. mutex_unlock(&dev->struct_mutex);
  3559. }
  3560. static void haswell_crtc_disable(struct drm_crtc *crtc)
  3561. {
  3562. struct drm_device *dev = crtc->dev;
  3563. struct drm_i915_private *dev_priv = dev->dev_private;
  3564. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3565. struct intel_encoder *encoder;
  3566. int pipe = intel_crtc->pipe;
  3567. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3568. if (!intel_crtc->active)
  3569. return;
  3570. intel_crtc_disable_planes(crtc);
  3571. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3572. intel_opregion_notify_encoder(encoder, false);
  3573. encoder->disable(encoder);
  3574. }
  3575. if (intel_crtc->config.has_pch_encoder)
  3576. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
  3577. intel_disable_pipe(dev_priv, pipe);
  3578. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  3579. ironlake_pfit_disable(intel_crtc);
  3580. intel_ddi_disable_pipe_clock(intel_crtc);
  3581. for_each_encoder_on_crtc(dev, crtc, encoder)
  3582. if (encoder->post_disable)
  3583. encoder->post_disable(encoder);
  3584. if (intel_crtc->config.has_pch_encoder) {
  3585. lpt_disable_pch_transcoder(dev_priv);
  3586. intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
  3587. intel_ddi_fdi_disable(crtc);
  3588. }
  3589. intel_crtc->active = false;
  3590. intel_update_watermarks(crtc);
  3591. mutex_lock(&dev->struct_mutex);
  3592. intel_update_fbc(dev);
  3593. intel_edp_psr_update(dev);
  3594. mutex_unlock(&dev->struct_mutex);
  3595. }
  3596. static void ironlake_crtc_off(struct drm_crtc *crtc)
  3597. {
  3598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3599. intel_put_shared_dpll(intel_crtc);
  3600. }
  3601. static void haswell_crtc_off(struct drm_crtc *crtc)
  3602. {
  3603. intel_ddi_put_crtc_pll(crtc);
  3604. }
  3605. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  3606. {
  3607. struct drm_device *dev = crtc->base.dev;
  3608. struct drm_i915_private *dev_priv = dev->dev_private;
  3609. struct intel_crtc_config *pipe_config = &crtc->config;
  3610. if (!crtc->config.gmch_pfit.control)
  3611. return;
  3612. /*
  3613. * The panel fitter should only be adjusted whilst the pipe is disabled,
  3614. * according to register description and PRM.
  3615. */
  3616. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  3617. assert_pipe_disabled(dev_priv, crtc->pipe);
  3618. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  3619. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  3620. /* Border color in case we don't scale up to the full screen. Black by
  3621. * default, change to something else for debugging. */
  3622. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  3623. }
  3624. #define for_each_power_domain(domain, mask) \
  3625. for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
  3626. if ((1 << (domain)) & (mask))
  3627. enum intel_display_power_domain
  3628. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  3629. {
  3630. struct drm_device *dev = intel_encoder->base.dev;
  3631. struct intel_digital_port *intel_dig_port;
  3632. switch (intel_encoder->type) {
  3633. case INTEL_OUTPUT_UNKNOWN:
  3634. /* Only DDI platforms should ever use this output type */
  3635. WARN_ON_ONCE(!HAS_DDI(dev));
  3636. case INTEL_OUTPUT_DISPLAYPORT:
  3637. case INTEL_OUTPUT_HDMI:
  3638. case INTEL_OUTPUT_EDP:
  3639. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  3640. switch (intel_dig_port->port) {
  3641. case PORT_A:
  3642. return POWER_DOMAIN_PORT_DDI_A_4_LANES;
  3643. case PORT_B:
  3644. return POWER_DOMAIN_PORT_DDI_B_4_LANES;
  3645. case PORT_C:
  3646. return POWER_DOMAIN_PORT_DDI_C_4_LANES;
  3647. case PORT_D:
  3648. return POWER_DOMAIN_PORT_DDI_D_4_LANES;
  3649. default:
  3650. WARN_ON_ONCE(1);
  3651. return POWER_DOMAIN_PORT_OTHER;
  3652. }
  3653. case INTEL_OUTPUT_ANALOG:
  3654. return POWER_DOMAIN_PORT_CRT;
  3655. case INTEL_OUTPUT_DSI:
  3656. return POWER_DOMAIN_PORT_DSI;
  3657. default:
  3658. return POWER_DOMAIN_PORT_OTHER;
  3659. }
  3660. }
  3661. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
  3662. {
  3663. struct drm_device *dev = crtc->dev;
  3664. struct intel_encoder *intel_encoder;
  3665. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3666. enum pipe pipe = intel_crtc->pipe;
  3667. bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
  3668. unsigned long mask;
  3669. enum transcoder transcoder;
  3670. transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
  3671. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  3672. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  3673. if (pfit_enabled)
  3674. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  3675. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  3676. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  3677. return mask;
  3678. }
  3679. void intel_display_set_init_power(struct drm_i915_private *dev_priv,
  3680. bool enable)
  3681. {
  3682. if (dev_priv->power_domains.init_power_on == enable)
  3683. return;
  3684. if (enable)
  3685. intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
  3686. else
  3687. intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
  3688. dev_priv->power_domains.init_power_on = enable;
  3689. }
  3690. static void modeset_update_crtc_power_domains(struct drm_device *dev)
  3691. {
  3692. struct drm_i915_private *dev_priv = dev->dev_private;
  3693. unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
  3694. struct intel_crtc *crtc;
  3695. /*
  3696. * First get all needed power domains, then put all unneeded, to avoid
  3697. * any unnecessary toggling of the power wells.
  3698. */
  3699. for_each_intel_crtc(dev, crtc) {
  3700. enum intel_display_power_domain domain;
  3701. if (!crtc->base.enabled)
  3702. continue;
  3703. pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
  3704. for_each_power_domain(domain, pipe_domains[crtc->pipe])
  3705. intel_display_power_get(dev_priv, domain);
  3706. }
  3707. for_each_intel_crtc(dev, crtc) {
  3708. enum intel_display_power_domain domain;
  3709. for_each_power_domain(domain, crtc->enabled_power_domains)
  3710. intel_display_power_put(dev_priv, domain);
  3711. crtc->enabled_power_domains = pipe_domains[crtc->pipe];
  3712. }
  3713. intel_display_set_init_power(dev_priv, false);
  3714. }
  3715. int valleyview_get_vco(struct drm_i915_private *dev_priv)
  3716. {
  3717. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  3718. /* Obtain SKU information */
  3719. mutex_lock(&dev_priv->dpio_lock);
  3720. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  3721. CCK_FUSE_HPLL_FREQ_MASK;
  3722. mutex_unlock(&dev_priv->dpio_lock);
  3723. return vco_freq[hpll_freq];
  3724. }
  3725. /* Adjust CDclk dividers to allow high res or save power if possible */
  3726. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  3727. {
  3728. struct drm_i915_private *dev_priv = dev->dev_private;
  3729. u32 val, cmd;
  3730. WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
  3731. dev_priv->vlv_cdclk_freq = cdclk;
  3732. if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
  3733. cmd = 2;
  3734. else if (cdclk == 266)
  3735. cmd = 1;
  3736. else
  3737. cmd = 0;
  3738. mutex_lock(&dev_priv->rps.hw_lock);
  3739. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  3740. val &= ~DSPFREQGUAR_MASK;
  3741. val |= (cmd << DSPFREQGUAR_SHIFT);
  3742. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  3743. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  3744. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  3745. 50)) {
  3746. DRM_ERROR("timed out waiting for CDclk change\n");
  3747. }
  3748. mutex_unlock(&dev_priv->rps.hw_lock);
  3749. if (cdclk == 400) {
  3750. u32 divider, vco;
  3751. vco = valleyview_get_vco(dev_priv);
  3752. divider = ((vco << 1) / cdclk) - 1;
  3753. mutex_lock(&dev_priv->dpio_lock);
  3754. /* adjust cdclk divider */
  3755. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3756. val &= ~0xf;
  3757. val |= divider;
  3758. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  3759. mutex_unlock(&dev_priv->dpio_lock);
  3760. }
  3761. mutex_lock(&dev_priv->dpio_lock);
  3762. /* adjust self-refresh exit latency value */
  3763. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  3764. val &= ~0x7f;
  3765. /*
  3766. * For high bandwidth configs, we set a higher latency in the bunit
  3767. * so that the core display fetch happens in time to avoid underruns.
  3768. */
  3769. if (cdclk == 400)
  3770. val |= 4500 / 250; /* 4.5 usec */
  3771. else
  3772. val |= 3000 / 250; /* 3.0 usec */
  3773. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  3774. mutex_unlock(&dev_priv->dpio_lock);
  3775. /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
  3776. intel_i2c_reset(dev);
  3777. }
  3778. int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
  3779. {
  3780. int cur_cdclk, vco;
  3781. int divider;
  3782. vco = valleyview_get_vco(dev_priv);
  3783. mutex_lock(&dev_priv->dpio_lock);
  3784. divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  3785. mutex_unlock(&dev_priv->dpio_lock);
  3786. divider &= 0xf;
  3787. cur_cdclk = (vco << 1) / (divider + 1);
  3788. return cur_cdclk;
  3789. }
  3790. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  3791. int max_pixclk)
  3792. {
  3793. /*
  3794. * Really only a few cases to deal with, as only 4 CDclks are supported:
  3795. * 200MHz
  3796. * 267MHz
  3797. * 320MHz
  3798. * 400MHz
  3799. * So we check to see whether we're above 90% of the lower bin and
  3800. * adjust if needed.
  3801. */
  3802. if (max_pixclk > 288000) {
  3803. return 400;
  3804. } else if (max_pixclk > 240000) {
  3805. return 320;
  3806. } else
  3807. return 266;
  3808. /* Looks like the 200MHz CDclk freq doesn't work on some configs */
  3809. }
  3810. /* compute the max pixel clock for new configuration */
  3811. static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
  3812. {
  3813. struct drm_device *dev = dev_priv->dev;
  3814. struct intel_crtc *intel_crtc;
  3815. int max_pixclk = 0;
  3816. for_each_intel_crtc(dev, intel_crtc) {
  3817. if (intel_crtc->new_enabled)
  3818. max_pixclk = max(max_pixclk,
  3819. intel_crtc->new_config->adjusted_mode.crtc_clock);
  3820. }
  3821. return max_pixclk;
  3822. }
  3823. static void valleyview_modeset_global_pipes(struct drm_device *dev,
  3824. unsigned *prepare_pipes)
  3825. {
  3826. struct drm_i915_private *dev_priv = dev->dev_private;
  3827. struct intel_crtc *intel_crtc;
  3828. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3829. if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
  3830. dev_priv->vlv_cdclk_freq)
  3831. return;
  3832. /* disable/enable all currently active pipes while we change cdclk */
  3833. for_each_intel_crtc(dev, intel_crtc)
  3834. if (intel_crtc->base.enabled)
  3835. *prepare_pipes |= (1 << intel_crtc->pipe);
  3836. }
  3837. static void valleyview_modeset_global_resources(struct drm_device *dev)
  3838. {
  3839. struct drm_i915_private *dev_priv = dev->dev_private;
  3840. int max_pixclk = intel_mode_max_pixclk(dev_priv);
  3841. int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
  3842. if (req_cdclk != dev_priv->vlv_cdclk_freq)
  3843. valleyview_set_cdclk(dev, req_cdclk);
  3844. modeset_update_crtc_power_domains(dev);
  3845. }
  3846. static void valleyview_crtc_enable(struct drm_crtc *crtc)
  3847. {
  3848. struct drm_device *dev = crtc->dev;
  3849. struct drm_i915_private *dev_priv = dev->dev_private;
  3850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3851. struct intel_encoder *encoder;
  3852. int pipe = intel_crtc->pipe;
  3853. int plane = intel_crtc->plane;
  3854. bool is_dsi;
  3855. u32 dspcntr;
  3856. WARN_ON(!crtc->enabled);
  3857. if (intel_crtc->active)
  3858. return;
  3859. is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
  3860. if (!is_dsi && !IS_CHERRYVIEW(dev))
  3861. vlv_prepare_pll(intel_crtc);
  3862. /* Set up the display plane register */
  3863. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3864. if (intel_crtc->config.has_dp_encoder)
  3865. intel_dp_set_m_n(intel_crtc);
  3866. intel_set_pipe_timings(intel_crtc);
  3867. /* pipesrc and dspsize control the size that is scaled from,
  3868. * which should always be the user's requested size.
  3869. */
  3870. I915_WRITE(DSPSIZE(plane),
  3871. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3872. (intel_crtc->config.pipe_src_w - 1));
  3873. I915_WRITE(DSPPOS(plane), 0);
  3874. i9xx_set_pipeconf(intel_crtc);
  3875. I915_WRITE(DSPCNTR(plane), dspcntr);
  3876. POSTING_READ(DSPCNTR(plane));
  3877. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3878. crtc->x, crtc->y);
  3879. intel_crtc->active = true;
  3880. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3881. for_each_encoder_on_crtc(dev, crtc, encoder)
  3882. if (encoder->pre_pll_enable)
  3883. encoder->pre_pll_enable(encoder);
  3884. if (!is_dsi) {
  3885. if (IS_CHERRYVIEW(dev))
  3886. chv_enable_pll(intel_crtc);
  3887. else
  3888. vlv_enable_pll(intel_crtc);
  3889. }
  3890. for_each_encoder_on_crtc(dev, crtc, encoder)
  3891. if (encoder->pre_enable)
  3892. encoder->pre_enable(encoder);
  3893. i9xx_pfit_enable(intel_crtc);
  3894. intel_crtc_load_lut(crtc);
  3895. intel_update_watermarks(crtc);
  3896. intel_enable_pipe(intel_crtc);
  3897. for_each_encoder_on_crtc(dev, crtc, encoder)
  3898. encoder->enable(encoder);
  3899. intel_crtc_enable_planes(crtc);
  3900. drm_crtc_vblank_on(crtc);
  3901. /* Underruns don't raise interrupts, so check manually. */
  3902. i9xx_check_fifo_underruns(dev);
  3903. }
  3904. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  3905. {
  3906. struct drm_device *dev = crtc->base.dev;
  3907. struct drm_i915_private *dev_priv = dev->dev_private;
  3908. I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
  3909. I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
  3910. }
  3911. static void i9xx_crtc_enable(struct drm_crtc *crtc)
  3912. {
  3913. struct drm_device *dev = crtc->dev;
  3914. struct drm_i915_private *dev_priv = dev->dev_private;
  3915. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3916. struct intel_encoder *encoder;
  3917. int pipe = intel_crtc->pipe;
  3918. int plane = intel_crtc->plane;
  3919. u32 dspcntr;
  3920. WARN_ON(!crtc->enabled);
  3921. if (intel_crtc->active)
  3922. return;
  3923. i9xx_set_pll_dividers(intel_crtc);
  3924. /* Set up the display plane register */
  3925. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3926. if (pipe == 0)
  3927. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3928. else
  3929. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3930. if (intel_crtc->config.has_dp_encoder)
  3931. intel_dp_set_m_n(intel_crtc);
  3932. intel_set_pipe_timings(intel_crtc);
  3933. /* pipesrc and dspsize control the size that is scaled from,
  3934. * which should always be the user's requested size.
  3935. */
  3936. I915_WRITE(DSPSIZE(plane),
  3937. ((intel_crtc->config.pipe_src_h - 1) << 16) |
  3938. (intel_crtc->config.pipe_src_w - 1));
  3939. I915_WRITE(DSPPOS(plane), 0);
  3940. i9xx_set_pipeconf(intel_crtc);
  3941. I915_WRITE(DSPCNTR(plane), dspcntr);
  3942. POSTING_READ(DSPCNTR(plane));
  3943. dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
  3944. crtc->x, crtc->y);
  3945. intel_crtc->active = true;
  3946. if (!IS_GEN2(dev))
  3947. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3948. for_each_encoder_on_crtc(dev, crtc, encoder)
  3949. if (encoder->pre_enable)
  3950. encoder->pre_enable(encoder);
  3951. i9xx_enable_pll(intel_crtc);
  3952. i9xx_pfit_enable(intel_crtc);
  3953. intel_crtc_load_lut(crtc);
  3954. intel_update_watermarks(crtc);
  3955. intel_enable_pipe(intel_crtc);
  3956. for_each_encoder_on_crtc(dev, crtc, encoder)
  3957. encoder->enable(encoder);
  3958. intel_crtc_enable_planes(crtc);
  3959. /*
  3960. * Gen2 reports pipe underruns whenever all planes are disabled.
  3961. * So don't enable underrun reporting before at least some planes
  3962. * are enabled.
  3963. * FIXME: Need to fix the logic to work when we turn off all planes
  3964. * but leave the pipe running.
  3965. */
  3966. if (IS_GEN2(dev))
  3967. intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
  3968. drm_crtc_vblank_on(crtc);
  3969. /* Underruns don't raise interrupts, so check manually. */
  3970. i9xx_check_fifo_underruns(dev);
  3971. }
  3972. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  3973. {
  3974. struct drm_device *dev = crtc->base.dev;
  3975. struct drm_i915_private *dev_priv = dev->dev_private;
  3976. if (!crtc->config.gmch_pfit.control)
  3977. return;
  3978. assert_pipe_disabled(dev_priv, crtc->pipe);
  3979. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  3980. I915_READ(PFIT_CONTROL));
  3981. I915_WRITE(PFIT_CONTROL, 0);
  3982. }
  3983. static void i9xx_crtc_disable(struct drm_crtc *crtc)
  3984. {
  3985. struct drm_device *dev = crtc->dev;
  3986. struct drm_i915_private *dev_priv = dev->dev_private;
  3987. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3988. struct intel_encoder *encoder;
  3989. int pipe = intel_crtc->pipe;
  3990. if (!intel_crtc->active)
  3991. return;
  3992. /*
  3993. * Gen2 reports pipe underruns whenever all planes are disabled.
  3994. * So diasble underrun reporting before all the planes get disabled.
  3995. * FIXME: Need to fix the logic to work when we turn off all planes
  3996. * but leave the pipe running.
  3997. */
  3998. if (IS_GEN2(dev))
  3999. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4000. intel_crtc_disable_planes(crtc);
  4001. for_each_encoder_on_crtc(dev, crtc, encoder)
  4002. encoder->disable(encoder);
  4003. /*
  4004. * On gen2 planes are double buffered but the pipe isn't, so we must
  4005. * wait for planes to fully turn off before disabling the pipe.
  4006. */
  4007. if (IS_GEN2(dev))
  4008. intel_wait_for_vblank(dev, pipe);
  4009. intel_disable_pipe(dev_priv, pipe);
  4010. i9xx_pfit_disable(intel_crtc);
  4011. for_each_encoder_on_crtc(dev, crtc, encoder)
  4012. if (encoder->post_disable)
  4013. encoder->post_disable(encoder);
  4014. if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
  4015. if (IS_CHERRYVIEW(dev))
  4016. chv_disable_pll(dev_priv, pipe);
  4017. else if (IS_VALLEYVIEW(dev))
  4018. vlv_disable_pll(dev_priv, pipe);
  4019. else
  4020. i9xx_disable_pll(dev_priv, pipe);
  4021. }
  4022. if (!IS_GEN2(dev))
  4023. intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
  4024. intel_crtc->active = false;
  4025. intel_update_watermarks(crtc);
  4026. mutex_lock(&dev->struct_mutex);
  4027. intel_update_fbc(dev);
  4028. intel_edp_psr_update(dev);
  4029. mutex_unlock(&dev->struct_mutex);
  4030. }
  4031. static void i9xx_crtc_off(struct drm_crtc *crtc)
  4032. {
  4033. }
  4034. static void intel_crtc_update_sarea(struct drm_crtc *crtc,
  4035. bool enabled)
  4036. {
  4037. struct drm_device *dev = crtc->dev;
  4038. struct drm_i915_master_private *master_priv;
  4039. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4040. int pipe = intel_crtc->pipe;
  4041. if (!dev->primary->master)
  4042. return;
  4043. master_priv = dev->primary->master->driver_priv;
  4044. if (!master_priv->sarea_priv)
  4045. return;
  4046. switch (pipe) {
  4047. case 0:
  4048. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  4049. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  4050. break;
  4051. case 1:
  4052. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  4053. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  4054. break;
  4055. default:
  4056. DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
  4057. break;
  4058. }
  4059. }
  4060. /**
  4061. * Sets the power management mode of the pipe and plane.
  4062. */
  4063. void intel_crtc_update_dpms(struct drm_crtc *crtc)
  4064. {
  4065. struct drm_device *dev = crtc->dev;
  4066. struct drm_i915_private *dev_priv = dev->dev_private;
  4067. struct intel_encoder *intel_encoder;
  4068. bool enable = false;
  4069. for_each_encoder_on_crtc(dev, crtc, intel_encoder)
  4070. enable |= intel_encoder->connectors_active;
  4071. if (enable)
  4072. dev_priv->display.crtc_enable(crtc);
  4073. else
  4074. dev_priv->display.crtc_disable(crtc);
  4075. intel_crtc_update_sarea(crtc, enable);
  4076. }
  4077. static void intel_crtc_disable(struct drm_crtc *crtc)
  4078. {
  4079. struct drm_device *dev = crtc->dev;
  4080. struct drm_connector *connector;
  4081. struct drm_i915_private *dev_priv = dev->dev_private;
  4082. /* crtc should still be enabled when we disable it. */
  4083. WARN_ON(!crtc->enabled);
  4084. dev_priv->display.crtc_disable(crtc);
  4085. intel_crtc_update_sarea(crtc, false);
  4086. dev_priv->display.off(crtc);
  4087. assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
  4088. assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  4089. assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
  4090. if (crtc->primary->fb) {
  4091. mutex_lock(&dev->struct_mutex);
  4092. intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
  4093. mutex_unlock(&dev->struct_mutex);
  4094. crtc->primary->fb = NULL;
  4095. }
  4096. /* Update computed state. */
  4097. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4098. if (!connector->encoder || !connector->encoder->crtc)
  4099. continue;
  4100. if (connector->encoder->crtc != crtc)
  4101. continue;
  4102. connector->dpms = DRM_MODE_DPMS_OFF;
  4103. to_intel_encoder(connector->encoder)->connectors_active = false;
  4104. }
  4105. }
  4106. void intel_encoder_destroy(struct drm_encoder *encoder)
  4107. {
  4108. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4109. drm_encoder_cleanup(encoder);
  4110. kfree(intel_encoder);
  4111. }
  4112. /* Simple dpms helper for encoders with just one connector, no cloning and only
  4113. * one kind of off state. It clamps all !ON modes to fully OFF and changes the
  4114. * state of the entire output pipe. */
  4115. static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
  4116. {
  4117. if (mode == DRM_MODE_DPMS_ON) {
  4118. encoder->connectors_active = true;
  4119. intel_crtc_update_dpms(encoder->base.crtc);
  4120. } else {
  4121. encoder->connectors_active = false;
  4122. intel_crtc_update_dpms(encoder->base.crtc);
  4123. }
  4124. }
  4125. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4126. * internal consistency). */
  4127. static void intel_connector_check_state(struct intel_connector *connector)
  4128. {
  4129. if (connector->get_hw_state(connector)) {
  4130. struct intel_encoder *encoder = connector->encoder;
  4131. struct drm_crtc *crtc;
  4132. bool encoder_enabled;
  4133. enum pipe pipe;
  4134. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4135. connector->base.base.id,
  4136. connector->base.name);
  4137. WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
  4138. "wrong connector dpms state\n");
  4139. WARN(connector->base.encoder != &encoder->base,
  4140. "active connector not linked to encoder\n");
  4141. WARN(!encoder->connectors_active,
  4142. "encoder->connectors_active not set\n");
  4143. encoder_enabled = encoder->get_hw_state(encoder, &pipe);
  4144. WARN(!encoder_enabled, "encoder not enabled\n");
  4145. if (WARN_ON(!encoder->base.crtc))
  4146. return;
  4147. crtc = encoder->base.crtc;
  4148. WARN(!crtc->enabled, "crtc not enabled\n");
  4149. WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
  4150. WARN(pipe != to_intel_crtc(crtc)->pipe,
  4151. "encoder active on the wrong pipe\n");
  4152. }
  4153. }
  4154. /* Even simpler default implementation, if there's really no special case to
  4155. * consider. */
  4156. void intel_connector_dpms(struct drm_connector *connector, int mode)
  4157. {
  4158. /* All the simple cases only support two dpms states. */
  4159. if (mode != DRM_MODE_DPMS_ON)
  4160. mode = DRM_MODE_DPMS_OFF;
  4161. if (mode == connector->dpms)
  4162. return;
  4163. connector->dpms = mode;
  4164. /* Only need to change hw state when actually enabled */
  4165. if (connector->encoder)
  4166. intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
  4167. intel_modeset_check_state(connector->dev);
  4168. }
  4169. /* Simple connector->get_hw_state implementation for encoders that support only
  4170. * one connector and no cloning and hence the encoder state determines the state
  4171. * of the connector. */
  4172. bool intel_connector_get_hw_state(struct intel_connector *connector)
  4173. {
  4174. enum pipe pipe = 0;
  4175. struct intel_encoder *encoder = connector->encoder;
  4176. return encoder->get_hw_state(encoder, &pipe);
  4177. }
  4178. static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  4179. struct intel_crtc_config *pipe_config)
  4180. {
  4181. struct drm_i915_private *dev_priv = dev->dev_private;
  4182. struct intel_crtc *pipe_B_crtc =
  4183. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4184. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  4185. pipe_name(pipe), pipe_config->fdi_lanes);
  4186. if (pipe_config->fdi_lanes > 4) {
  4187. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  4188. pipe_name(pipe), pipe_config->fdi_lanes);
  4189. return false;
  4190. }
  4191. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  4192. if (pipe_config->fdi_lanes > 2) {
  4193. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  4194. pipe_config->fdi_lanes);
  4195. return false;
  4196. } else {
  4197. return true;
  4198. }
  4199. }
  4200. if (INTEL_INFO(dev)->num_pipes == 2)
  4201. return true;
  4202. /* Ivybridge 3 pipe is really complicated */
  4203. switch (pipe) {
  4204. case PIPE_A:
  4205. return true;
  4206. case PIPE_B:
  4207. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4208. pipe_config->fdi_lanes > 2) {
  4209. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4210. pipe_name(pipe), pipe_config->fdi_lanes);
  4211. return false;
  4212. }
  4213. return true;
  4214. case PIPE_C:
  4215. if (!pipe_has_enabled_pch(pipe_B_crtc) ||
  4216. pipe_B_crtc->config.fdi_lanes <= 2) {
  4217. if (pipe_config->fdi_lanes > 2) {
  4218. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  4219. pipe_name(pipe), pipe_config->fdi_lanes);
  4220. return false;
  4221. }
  4222. } else {
  4223. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4224. return false;
  4225. }
  4226. return true;
  4227. default:
  4228. BUG();
  4229. }
  4230. }
  4231. #define RETRY 1
  4232. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  4233. struct intel_crtc_config *pipe_config)
  4234. {
  4235. struct drm_device *dev = intel_crtc->base.dev;
  4236. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4237. int lane, link_bw, fdi_dotclock;
  4238. bool setup_ok, needs_recompute = false;
  4239. retry:
  4240. /* FDI is a binary signal running at ~2.7GHz, encoding
  4241. * each output octet as 10 bits. The actual frequency
  4242. * is stored as a divider into a 100MHz clock, and the
  4243. * mode pixel clock is stored in units of 1KHz.
  4244. * Hence the bw of each lane in terms of the mode signal
  4245. * is:
  4246. */
  4247. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4248. fdi_dotclock = adjusted_mode->crtc_clock;
  4249. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  4250. pipe_config->pipe_bpp);
  4251. pipe_config->fdi_lanes = lane;
  4252. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  4253. link_bw, &pipe_config->fdi_m_n);
  4254. setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
  4255. intel_crtc->pipe, pipe_config);
  4256. if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
  4257. pipe_config->pipe_bpp -= 2*3;
  4258. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  4259. pipe_config->pipe_bpp);
  4260. needs_recompute = true;
  4261. pipe_config->bw_constrained = true;
  4262. goto retry;
  4263. }
  4264. if (needs_recompute)
  4265. return RETRY;
  4266. return setup_ok ? 0 : -EINVAL;
  4267. }
  4268. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  4269. struct intel_crtc_config *pipe_config)
  4270. {
  4271. pipe_config->ips_enabled = i915.enable_ips &&
  4272. hsw_crtc_supports_ips(crtc) &&
  4273. pipe_config->pipe_bpp <= 24;
  4274. }
  4275. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  4276. struct intel_crtc_config *pipe_config)
  4277. {
  4278. struct drm_device *dev = crtc->base.dev;
  4279. struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
  4280. /* FIXME should check pixel clock limits on all platforms */
  4281. if (INTEL_INFO(dev)->gen < 4) {
  4282. struct drm_i915_private *dev_priv = dev->dev_private;
  4283. int clock_limit =
  4284. dev_priv->display.get_display_clock_speed(dev);
  4285. /*
  4286. * Enable pixel doubling when the dot clock
  4287. * is > 90% of the (display) core speed.
  4288. *
  4289. * GDG double wide on either pipe,
  4290. * otherwise pipe A only.
  4291. */
  4292. if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
  4293. adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
  4294. clock_limit *= 2;
  4295. pipe_config->double_wide = true;
  4296. }
  4297. if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
  4298. return -EINVAL;
  4299. }
  4300. /*
  4301. * Pipe horizontal size must be even in:
  4302. * - DVO ganged mode
  4303. * - LVDS dual channel mode
  4304. * - Double wide pipe
  4305. */
  4306. if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4307. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  4308. pipe_config->pipe_src_w &= ~1;
  4309. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  4310. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  4311. */
  4312. if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
  4313. adjusted_mode->hsync_start == adjusted_mode->hdisplay)
  4314. return -EINVAL;
  4315. if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
  4316. pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
  4317. } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
  4318. /* only a 8bpc pipe, with 6bpc dither through the panel fitter
  4319. * for lvds. */
  4320. pipe_config->pipe_bpp = 8*3;
  4321. }
  4322. if (HAS_IPS(dev))
  4323. hsw_compute_ips_config(crtc, pipe_config);
  4324. /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
  4325. * clock survives for now. */
  4326. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  4327. pipe_config->shared_dpll = crtc->config.shared_dpll;
  4328. if (pipe_config->has_pch_encoder)
  4329. return ironlake_fdi_compute_config(crtc, pipe_config);
  4330. return 0;
  4331. }
  4332. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  4333. {
  4334. return 400000; /* FIXME */
  4335. }
  4336. static int i945_get_display_clock_speed(struct drm_device *dev)
  4337. {
  4338. return 400000;
  4339. }
  4340. static int i915_get_display_clock_speed(struct drm_device *dev)
  4341. {
  4342. return 333000;
  4343. }
  4344. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  4345. {
  4346. return 200000;
  4347. }
  4348. static int pnv_get_display_clock_speed(struct drm_device *dev)
  4349. {
  4350. u16 gcfgc = 0;
  4351. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4352. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4353. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  4354. return 267000;
  4355. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  4356. return 333000;
  4357. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  4358. return 444000;
  4359. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  4360. return 200000;
  4361. default:
  4362. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  4363. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  4364. return 133000;
  4365. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  4366. return 167000;
  4367. }
  4368. }
  4369. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  4370. {
  4371. u16 gcfgc = 0;
  4372. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  4373. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  4374. return 133000;
  4375. else {
  4376. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  4377. case GC_DISPLAY_CLOCK_333_MHZ:
  4378. return 333000;
  4379. default:
  4380. case GC_DISPLAY_CLOCK_190_200_MHZ:
  4381. return 190000;
  4382. }
  4383. }
  4384. }
  4385. static int i865_get_display_clock_speed(struct drm_device *dev)
  4386. {
  4387. return 266000;
  4388. }
  4389. static int i855_get_display_clock_speed(struct drm_device *dev)
  4390. {
  4391. u16 hpllcc = 0;
  4392. /* Assume that the hardware is in the high speed state. This
  4393. * should be the default.
  4394. */
  4395. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  4396. case GC_CLOCK_133_200:
  4397. case GC_CLOCK_100_200:
  4398. return 200000;
  4399. case GC_CLOCK_166_250:
  4400. return 250000;
  4401. case GC_CLOCK_100_133:
  4402. return 133000;
  4403. }
  4404. /* Shouldn't happen */
  4405. return 0;
  4406. }
  4407. static int i830_get_display_clock_speed(struct drm_device *dev)
  4408. {
  4409. return 133000;
  4410. }
  4411. static void
  4412. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  4413. {
  4414. while (*num > DATA_LINK_M_N_MASK ||
  4415. *den > DATA_LINK_M_N_MASK) {
  4416. *num >>= 1;
  4417. *den >>= 1;
  4418. }
  4419. }
  4420. static void compute_m_n(unsigned int m, unsigned int n,
  4421. uint32_t *ret_m, uint32_t *ret_n)
  4422. {
  4423. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  4424. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  4425. intel_reduce_m_n_ratio(ret_m, ret_n);
  4426. }
  4427. void
  4428. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  4429. int pixel_clock, int link_clock,
  4430. struct intel_link_m_n *m_n)
  4431. {
  4432. m_n->tu = 64;
  4433. compute_m_n(bits_per_pixel * pixel_clock,
  4434. link_clock * nlanes * 8,
  4435. &m_n->gmch_m, &m_n->gmch_n);
  4436. compute_m_n(pixel_clock, link_clock,
  4437. &m_n->link_m, &m_n->link_n);
  4438. }
  4439. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  4440. {
  4441. if (i915.panel_use_ssc >= 0)
  4442. return i915.panel_use_ssc != 0;
  4443. return dev_priv->vbt.lvds_use_ssc
  4444. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  4445. }
  4446. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  4447. {
  4448. struct drm_device *dev = crtc->dev;
  4449. struct drm_i915_private *dev_priv = dev->dev_private;
  4450. int refclk;
  4451. if (IS_VALLEYVIEW(dev)) {
  4452. refclk = 100000;
  4453. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  4454. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4455. refclk = dev_priv->vbt.lvds_ssc_freq;
  4456. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  4457. } else if (!IS_GEN2(dev)) {
  4458. refclk = 96000;
  4459. } else {
  4460. refclk = 48000;
  4461. }
  4462. return refclk;
  4463. }
  4464. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  4465. {
  4466. return (1 << dpll->n) << 16 | dpll->m2;
  4467. }
  4468. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  4469. {
  4470. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  4471. }
  4472. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  4473. intel_clock_t *reduced_clock)
  4474. {
  4475. struct drm_device *dev = crtc->base.dev;
  4476. u32 fp, fp2 = 0;
  4477. if (IS_PINEVIEW(dev)) {
  4478. fp = pnv_dpll_compute_fp(&crtc->config.dpll);
  4479. if (reduced_clock)
  4480. fp2 = pnv_dpll_compute_fp(reduced_clock);
  4481. } else {
  4482. fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
  4483. if (reduced_clock)
  4484. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  4485. }
  4486. crtc->config.dpll_hw_state.fp0 = fp;
  4487. crtc->lowfreq_avail = false;
  4488. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4489. reduced_clock && i915.powersave) {
  4490. crtc->config.dpll_hw_state.fp1 = fp2;
  4491. crtc->lowfreq_avail = true;
  4492. } else {
  4493. crtc->config.dpll_hw_state.fp1 = fp;
  4494. }
  4495. }
  4496. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  4497. pipe)
  4498. {
  4499. u32 reg_val;
  4500. /*
  4501. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  4502. * and set it to a reasonable value instead.
  4503. */
  4504. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4505. reg_val &= 0xffffff00;
  4506. reg_val |= 0x00000030;
  4507. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4508. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4509. reg_val &= 0x8cffffff;
  4510. reg_val = 0x8c000000;
  4511. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4512. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  4513. reg_val &= 0xffffff00;
  4514. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  4515. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  4516. reg_val &= 0x00ffffff;
  4517. reg_val |= 0xb0000000;
  4518. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  4519. }
  4520. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4521. struct intel_link_m_n *m_n)
  4522. {
  4523. struct drm_device *dev = crtc->base.dev;
  4524. struct drm_i915_private *dev_priv = dev->dev_private;
  4525. int pipe = crtc->pipe;
  4526. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4527. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  4528. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  4529. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  4530. }
  4531. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4532. struct intel_link_m_n *m_n)
  4533. {
  4534. struct drm_device *dev = crtc->base.dev;
  4535. struct drm_i915_private *dev_priv = dev->dev_private;
  4536. int pipe = crtc->pipe;
  4537. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4538. if (INTEL_INFO(dev)->gen >= 5) {
  4539. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4540. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4541. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4542. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4543. } else {
  4544. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4545. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  4546. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  4547. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  4548. }
  4549. }
  4550. static void intel_dp_set_m_n(struct intel_crtc *crtc)
  4551. {
  4552. if (crtc->config.has_pch_encoder)
  4553. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4554. else
  4555. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  4556. }
  4557. static void vlv_update_pll(struct intel_crtc *crtc)
  4558. {
  4559. u32 dpll, dpll_md;
  4560. /*
  4561. * Enable DPIO clock input. We should never disable the reference
  4562. * clock for pipe B, since VGA hotplug / manual detection depends
  4563. * on it.
  4564. */
  4565. dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
  4566. DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
  4567. /* We should never disable this, set it here for state tracking */
  4568. if (crtc->pipe == PIPE_B)
  4569. dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4570. dpll |= DPLL_VCO_ENABLE;
  4571. crtc->config.dpll_hw_state.dpll = dpll;
  4572. dpll_md = (crtc->config.pixel_multiplier - 1)
  4573. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4574. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4575. }
  4576. static void vlv_prepare_pll(struct intel_crtc *crtc)
  4577. {
  4578. struct drm_device *dev = crtc->base.dev;
  4579. struct drm_i915_private *dev_priv = dev->dev_private;
  4580. int pipe = crtc->pipe;
  4581. u32 mdiv;
  4582. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  4583. u32 coreclk, reg_val;
  4584. mutex_lock(&dev_priv->dpio_lock);
  4585. bestn = crtc->config.dpll.n;
  4586. bestm1 = crtc->config.dpll.m1;
  4587. bestm2 = crtc->config.dpll.m2;
  4588. bestp1 = crtc->config.dpll.p1;
  4589. bestp2 = crtc->config.dpll.p2;
  4590. /* See eDP HDMI DPIO driver vbios notes doc */
  4591. /* PLL B needs special handling */
  4592. if (pipe == PIPE_B)
  4593. vlv_pllb_recal_opamp(dev_priv, pipe);
  4594. /* Set up Tx target for periodic Rcomp update */
  4595. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  4596. /* Disable target IRef on PLL */
  4597. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  4598. reg_val &= 0x00ffffff;
  4599. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  4600. /* Disable fast lock */
  4601. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  4602. /* Set idtafcrecal before PLL is enabled */
  4603. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  4604. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  4605. mdiv |= ((bestn << DPIO_N_SHIFT));
  4606. mdiv |= (1 << DPIO_K_SHIFT);
  4607. /*
  4608. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  4609. * but we don't support that).
  4610. * Note: don't use the DAC post divider as it seems unstable.
  4611. */
  4612. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  4613. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4614. mdiv |= DPIO_ENABLE_CALIBRATION;
  4615. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  4616. /* Set HBR and RBR LPF coefficients */
  4617. if (crtc->config.port_clock == 162000 ||
  4618. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
  4619. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
  4620. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4621. 0x009f0003);
  4622. else
  4623. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  4624. 0x00d0000f);
  4625. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
  4626. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
  4627. /* Use SSC source */
  4628. if (pipe == PIPE_A)
  4629. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4630. 0x0df40000);
  4631. else
  4632. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4633. 0x0df70000);
  4634. } else { /* HDMI or VGA */
  4635. /* Use bend source */
  4636. if (pipe == PIPE_A)
  4637. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4638. 0x0df70000);
  4639. else
  4640. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  4641. 0x0df40000);
  4642. }
  4643. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  4644. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  4645. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
  4646. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
  4647. coreclk |= 0x01000000;
  4648. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  4649. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  4650. mutex_unlock(&dev_priv->dpio_lock);
  4651. }
  4652. static void chv_update_pll(struct intel_crtc *crtc)
  4653. {
  4654. struct drm_device *dev = crtc->base.dev;
  4655. struct drm_i915_private *dev_priv = dev->dev_private;
  4656. int pipe = crtc->pipe;
  4657. int dpll_reg = DPLL(crtc->pipe);
  4658. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  4659. u32 loopfilter, intcoeff;
  4660. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  4661. int refclk;
  4662. crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
  4663. DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
  4664. DPLL_VCO_ENABLE;
  4665. if (pipe != PIPE_A)
  4666. crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  4667. crtc->config.dpll_hw_state.dpll_md =
  4668. (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4669. bestn = crtc->config.dpll.n;
  4670. bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
  4671. bestm1 = crtc->config.dpll.m1;
  4672. bestm2 = crtc->config.dpll.m2 >> 22;
  4673. bestp1 = crtc->config.dpll.p1;
  4674. bestp2 = crtc->config.dpll.p2;
  4675. /*
  4676. * Enable Refclk and SSC
  4677. */
  4678. I915_WRITE(dpll_reg,
  4679. crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  4680. mutex_lock(&dev_priv->dpio_lock);
  4681. /* p1 and p2 divider */
  4682. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  4683. 5 << DPIO_CHV_S1_DIV_SHIFT |
  4684. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  4685. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  4686. 1 << DPIO_CHV_K_DIV_SHIFT);
  4687. /* Feedback post-divider - m2 */
  4688. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  4689. /* Feedback refclk divider - n and m1 */
  4690. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  4691. DPIO_CHV_M1_DIV_BY_2 |
  4692. 1 << DPIO_CHV_N_DIV_SHIFT);
  4693. /* M2 fraction division */
  4694. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  4695. /* M2 fraction division enable */
  4696. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
  4697. DPIO_CHV_FRAC_DIV_EN |
  4698. (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
  4699. /* Loop filter */
  4700. refclk = i9xx_get_refclk(&crtc->base, 0);
  4701. loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
  4702. 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
  4703. if (refclk == 100000)
  4704. intcoeff = 11;
  4705. else if (refclk == 38400)
  4706. intcoeff = 10;
  4707. else
  4708. intcoeff = 9;
  4709. loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
  4710. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  4711. /* AFC Recal */
  4712. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  4713. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  4714. DPIO_AFC_RECAL);
  4715. mutex_unlock(&dev_priv->dpio_lock);
  4716. }
  4717. static void i9xx_update_pll(struct intel_crtc *crtc,
  4718. intel_clock_t *reduced_clock,
  4719. int num_connectors)
  4720. {
  4721. struct drm_device *dev = crtc->base.dev;
  4722. struct drm_i915_private *dev_priv = dev->dev_private;
  4723. u32 dpll;
  4724. bool is_sdvo;
  4725. struct dpll *clock = &crtc->config.dpll;
  4726. i9xx_update_pll_dividers(crtc, reduced_clock);
  4727. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  4728. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  4729. dpll = DPLL_VGA_MODE_DIS;
  4730. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  4731. dpll |= DPLLB_MODE_LVDS;
  4732. else
  4733. dpll |= DPLLB_MODE_DAC_SERIAL;
  4734. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  4735. dpll |= (crtc->config.pixel_multiplier - 1)
  4736. << SDVO_MULTIPLIER_SHIFT_HIRES;
  4737. }
  4738. if (is_sdvo)
  4739. dpll |= DPLL_SDVO_HIGH_SPEED;
  4740. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  4741. dpll |= DPLL_SDVO_HIGH_SPEED;
  4742. /* compute bitmask from p1 value */
  4743. if (IS_PINEVIEW(dev))
  4744. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  4745. else {
  4746. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4747. if (IS_G4X(dev) && reduced_clock)
  4748. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4749. }
  4750. switch (clock->p2) {
  4751. case 5:
  4752. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4753. break;
  4754. case 7:
  4755. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4756. break;
  4757. case 10:
  4758. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4759. break;
  4760. case 14:
  4761. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4762. break;
  4763. }
  4764. if (INTEL_INFO(dev)->gen >= 4)
  4765. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  4766. if (crtc->config.sdvo_tv_clock)
  4767. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4768. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4769. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4770. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4771. else
  4772. dpll |= PLL_REF_INPUT_DREFCLK;
  4773. dpll |= DPLL_VCO_ENABLE;
  4774. crtc->config.dpll_hw_state.dpll = dpll;
  4775. if (INTEL_INFO(dev)->gen >= 4) {
  4776. u32 dpll_md = (crtc->config.pixel_multiplier - 1)
  4777. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  4778. crtc->config.dpll_hw_state.dpll_md = dpll_md;
  4779. }
  4780. }
  4781. static void i8xx_update_pll(struct intel_crtc *crtc,
  4782. intel_clock_t *reduced_clock,
  4783. int num_connectors)
  4784. {
  4785. struct drm_device *dev = crtc->base.dev;
  4786. struct drm_i915_private *dev_priv = dev->dev_private;
  4787. u32 dpll;
  4788. struct dpll *clock = &crtc->config.dpll;
  4789. i9xx_update_pll_dividers(crtc, reduced_clock);
  4790. dpll = DPLL_VGA_MODE_DIS;
  4791. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  4792. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4793. } else {
  4794. if (clock->p1 == 2)
  4795. dpll |= PLL_P1_DIVIDE_BY_TWO;
  4796. else
  4797. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4798. if (clock->p2 == 4)
  4799. dpll |= PLL_P2_DIVIDE_BY_4;
  4800. }
  4801. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
  4802. dpll |= DPLL_DVO_2X_MODE;
  4803. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  4804. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4805. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4806. else
  4807. dpll |= PLL_REF_INPUT_DREFCLK;
  4808. dpll |= DPLL_VCO_ENABLE;
  4809. crtc->config.dpll_hw_state.dpll = dpll;
  4810. }
  4811. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  4812. {
  4813. struct drm_device *dev = intel_crtc->base.dev;
  4814. struct drm_i915_private *dev_priv = dev->dev_private;
  4815. enum pipe pipe = intel_crtc->pipe;
  4816. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4817. struct drm_display_mode *adjusted_mode =
  4818. &intel_crtc->config.adjusted_mode;
  4819. uint32_t crtc_vtotal, crtc_vblank_end;
  4820. int vsyncshift = 0;
  4821. /* We need to be careful not to changed the adjusted mode, for otherwise
  4822. * the hw state checker will get angry at the mismatch. */
  4823. crtc_vtotal = adjusted_mode->crtc_vtotal;
  4824. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  4825. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  4826. /* the chip adds 2 halflines automatically */
  4827. crtc_vtotal -= 1;
  4828. crtc_vblank_end -= 1;
  4829. if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4830. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  4831. else
  4832. vsyncshift = adjusted_mode->crtc_hsync_start -
  4833. adjusted_mode->crtc_htotal / 2;
  4834. if (vsyncshift < 0)
  4835. vsyncshift += adjusted_mode->crtc_htotal;
  4836. }
  4837. if (INTEL_INFO(dev)->gen > 3)
  4838. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  4839. I915_WRITE(HTOTAL(cpu_transcoder),
  4840. (adjusted_mode->crtc_hdisplay - 1) |
  4841. ((adjusted_mode->crtc_htotal - 1) << 16));
  4842. I915_WRITE(HBLANK(cpu_transcoder),
  4843. (adjusted_mode->crtc_hblank_start - 1) |
  4844. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  4845. I915_WRITE(HSYNC(cpu_transcoder),
  4846. (adjusted_mode->crtc_hsync_start - 1) |
  4847. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  4848. I915_WRITE(VTOTAL(cpu_transcoder),
  4849. (adjusted_mode->crtc_vdisplay - 1) |
  4850. ((crtc_vtotal - 1) << 16));
  4851. I915_WRITE(VBLANK(cpu_transcoder),
  4852. (adjusted_mode->crtc_vblank_start - 1) |
  4853. ((crtc_vblank_end - 1) << 16));
  4854. I915_WRITE(VSYNC(cpu_transcoder),
  4855. (adjusted_mode->crtc_vsync_start - 1) |
  4856. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  4857. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  4858. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  4859. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  4860. * bits. */
  4861. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  4862. (pipe == PIPE_B || pipe == PIPE_C))
  4863. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  4864. /* pipesrc controls the size that is scaled from, which should
  4865. * always be the user's requested size.
  4866. */
  4867. I915_WRITE(PIPESRC(pipe),
  4868. ((intel_crtc->config.pipe_src_w - 1) << 16) |
  4869. (intel_crtc->config.pipe_src_h - 1));
  4870. }
  4871. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  4872. struct intel_crtc_config *pipe_config)
  4873. {
  4874. struct drm_device *dev = crtc->base.dev;
  4875. struct drm_i915_private *dev_priv = dev->dev_private;
  4876. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  4877. uint32_t tmp;
  4878. tmp = I915_READ(HTOTAL(cpu_transcoder));
  4879. pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  4880. pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  4881. tmp = I915_READ(HBLANK(cpu_transcoder));
  4882. pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  4883. pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  4884. tmp = I915_READ(HSYNC(cpu_transcoder));
  4885. pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  4886. pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  4887. tmp = I915_READ(VTOTAL(cpu_transcoder));
  4888. pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  4889. pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  4890. tmp = I915_READ(VBLANK(cpu_transcoder));
  4891. pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  4892. pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  4893. tmp = I915_READ(VSYNC(cpu_transcoder));
  4894. pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  4895. pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  4896. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  4897. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  4898. pipe_config->adjusted_mode.crtc_vtotal += 1;
  4899. pipe_config->adjusted_mode.crtc_vblank_end += 1;
  4900. }
  4901. tmp = I915_READ(PIPESRC(crtc->pipe));
  4902. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  4903. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  4904. pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
  4905. pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
  4906. }
  4907. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  4908. struct intel_crtc_config *pipe_config)
  4909. {
  4910. mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
  4911. mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
  4912. mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
  4913. mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
  4914. mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
  4915. mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
  4916. mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
  4917. mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
  4918. mode->flags = pipe_config->adjusted_mode.flags;
  4919. mode->clock = pipe_config->adjusted_mode.crtc_clock;
  4920. mode->flags |= pipe_config->adjusted_mode.flags;
  4921. }
  4922. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  4923. {
  4924. struct drm_device *dev = intel_crtc->base.dev;
  4925. struct drm_i915_private *dev_priv = dev->dev_private;
  4926. uint32_t pipeconf;
  4927. pipeconf = 0;
  4928. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  4929. I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
  4930. pipeconf |= PIPECONF_ENABLE;
  4931. if (intel_crtc->config.double_wide)
  4932. pipeconf |= PIPECONF_DOUBLE_WIDE;
  4933. /* only g4x and later have fancy bpc/dither controls */
  4934. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  4935. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  4936. if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
  4937. pipeconf |= PIPECONF_DITHER_EN |
  4938. PIPECONF_DITHER_TYPE_SP;
  4939. switch (intel_crtc->config.pipe_bpp) {
  4940. case 18:
  4941. pipeconf |= PIPECONF_6BPC;
  4942. break;
  4943. case 24:
  4944. pipeconf |= PIPECONF_8BPC;
  4945. break;
  4946. case 30:
  4947. pipeconf |= PIPECONF_10BPC;
  4948. break;
  4949. default:
  4950. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4951. BUG();
  4952. }
  4953. }
  4954. if (HAS_PIPE_CXSR(dev)) {
  4955. if (intel_crtc->lowfreq_avail) {
  4956. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  4957. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  4958. } else {
  4959. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  4960. }
  4961. }
  4962. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  4963. if (INTEL_INFO(dev)->gen < 4 ||
  4964. intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
  4965. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  4966. else
  4967. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  4968. } else
  4969. pipeconf |= PIPECONF_PROGRESSIVE;
  4970. if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
  4971. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  4972. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  4973. POSTING_READ(PIPECONF(intel_crtc->pipe));
  4974. }
  4975. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  4976. int x, int y,
  4977. struct drm_framebuffer *fb)
  4978. {
  4979. struct drm_device *dev = crtc->dev;
  4980. struct drm_i915_private *dev_priv = dev->dev_private;
  4981. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4982. int refclk, num_connectors = 0;
  4983. intel_clock_t clock, reduced_clock;
  4984. bool ok, has_reduced_clock = false;
  4985. bool is_lvds = false, is_dsi = false;
  4986. struct intel_encoder *encoder;
  4987. const intel_limit_t *limit;
  4988. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4989. switch (encoder->type) {
  4990. case INTEL_OUTPUT_LVDS:
  4991. is_lvds = true;
  4992. break;
  4993. case INTEL_OUTPUT_DSI:
  4994. is_dsi = true;
  4995. break;
  4996. }
  4997. num_connectors++;
  4998. }
  4999. if (is_dsi)
  5000. return 0;
  5001. if (!intel_crtc->config.clock_set) {
  5002. refclk = i9xx_get_refclk(crtc, num_connectors);
  5003. /*
  5004. * Returns a set of divisors for the desired target clock with
  5005. * the given refclk, or FALSE. The returned values represent
  5006. * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
  5007. * 2) / p1 / p2.
  5008. */
  5009. limit = intel_limit(crtc, refclk);
  5010. ok = dev_priv->display.find_dpll(limit, crtc,
  5011. intel_crtc->config.port_clock,
  5012. refclk, NULL, &clock);
  5013. if (!ok) {
  5014. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5015. return -EINVAL;
  5016. }
  5017. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5018. /*
  5019. * Ensure we match the reduced clock's P to the target
  5020. * clock. If the clocks don't match, we can't switch
  5021. * the display clock by using the FP0/FP1. In such case
  5022. * we will disable the LVDS downclock feature.
  5023. */
  5024. has_reduced_clock =
  5025. dev_priv->display.find_dpll(limit, crtc,
  5026. dev_priv->lvds_downclock,
  5027. refclk, &clock,
  5028. &reduced_clock);
  5029. }
  5030. /* Compat-code for transition, will disappear. */
  5031. intel_crtc->config.dpll.n = clock.n;
  5032. intel_crtc->config.dpll.m1 = clock.m1;
  5033. intel_crtc->config.dpll.m2 = clock.m2;
  5034. intel_crtc->config.dpll.p1 = clock.p1;
  5035. intel_crtc->config.dpll.p2 = clock.p2;
  5036. }
  5037. if (IS_GEN2(dev)) {
  5038. i8xx_update_pll(intel_crtc,
  5039. has_reduced_clock ? &reduced_clock : NULL,
  5040. num_connectors);
  5041. } else if (IS_CHERRYVIEW(dev)) {
  5042. chv_update_pll(intel_crtc);
  5043. } else if (IS_VALLEYVIEW(dev)) {
  5044. vlv_update_pll(intel_crtc);
  5045. } else {
  5046. i9xx_update_pll(intel_crtc,
  5047. has_reduced_clock ? &reduced_clock : NULL,
  5048. num_connectors);
  5049. }
  5050. return 0;
  5051. }
  5052. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  5053. struct intel_crtc_config *pipe_config)
  5054. {
  5055. struct drm_device *dev = crtc->base.dev;
  5056. struct drm_i915_private *dev_priv = dev->dev_private;
  5057. uint32_t tmp;
  5058. if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
  5059. return;
  5060. tmp = I915_READ(PFIT_CONTROL);
  5061. if (!(tmp & PFIT_ENABLE))
  5062. return;
  5063. /* Check whether the pfit is attached to our pipe. */
  5064. if (INTEL_INFO(dev)->gen < 4) {
  5065. if (crtc->pipe != PIPE_B)
  5066. return;
  5067. } else {
  5068. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  5069. return;
  5070. }
  5071. pipe_config->gmch_pfit.control = tmp;
  5072. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  5073. if (INTEL_INFO(dev)->gen < 5)
  5074. pipe_config->gmch_pfit.lvds_border_bits =
  5075. I915_READ(LVDS) & LVDS_BORDER_ENABLE;
  5076. }
  5077. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  5078. struct intel_crtc_config *pipe_config)
  5079. {
  5080. struct drm_device *dev = crtc->base.dev;
  5081. struct drm_i915_private *dev_priv = dev->dev_private;
  5082. int pipe = pipe_config->cpu_transcoder;
  5083. intel_clock_t clock;
  5084. u32 mdiv;
  5085. int refclk = 100000;
  5086. mutex_lock(&dev_priv->dpio_lock);
  5087. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  5088. mutex_unlock(&dev_priv->dpio_lock);
  5089. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  5090. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  5091. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  5092. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  5093. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  5094. vlv_clock(refclk, &clock);
  5095. /* clock.dot is the fast clock */
  5096. pipe_config->port_clock = clock.dot / 5;
  5097. }
  5098. static void i9xx_get_plane_config(struct intel_crtc *crtc,
  5099. struct intel_plane_config *plane_config)
  5100. {
  5101. struct drm_device *dev = crtc->base.dev;
  5102. struct drm_i915_private *dev_priv = dev->dev_private;
  5103. u32 val, base, offset;
  5104. int pipe = crtc->pipe, plane = crtc->plane;
  5105. int fourcc, pixel_format;
  5106. int aligned_height;
  5107. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5108. if (!crtc->base.primary->fb) {
  5109. DRM_DEBUG_KMS("failed to alloc fb\n");
  5110. return;
  5111. }
  5112. val = I915_READ(DSPCNTR(plane));
  5113. if (INTEL_INFO(dev)->gen >= 4)
  5114. if (val & DISPPLANE_TILED)
  5115. plane_config->tiled = true;
  5116. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5117. fourcc = intel_format_to_fourcc(pixel_format);
  5118. crtc->base.primary->fb->pixel_format = fourcc;
  5119. crtc->base.primary->fb->bits_per_pixel =
  5120. drm_format_plane_cpp(fourcc, 0) * 8;
  5121. if (INTEL_INFO(dev)->gen >= 4) {
  5122. if (plane_config->tiled)
  5123. offset = I915_READ(DSPTILEOFF(plane));
  5124. else
  5125. offset = I915_READ(DSPLINOFF(plane));
  5126. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5127. } else {
  5128. base = I915_READ(DSPADDR(plane));
  5129. }
  5130. plane_config->base = base;
  5131. val = I915_READ(PIPESRC(pipe));
  5132. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5133. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5134. val = I915_READ(DSPSTRIDE(pipe));
  5135. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  5136. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5137. plane_config->tiled);
  5138. plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
  5139. aligned_height, PAGE_SIZE);
  5140. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5141. pipe, plane, crtc->base.primary->fb->width,
  5142. crtc->base.primary->fb->height,
  5143. crtc->base.primary->fb->bits_per_pixel, base,
  5144. crtc->base.primary->fb->pitches[0],
  5145. plane_config->size);
  5146. }
  5147. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  5148. struct intel_crtc_config *pipe_config)
  5149. {
  5150. struct drm_device *dev = crtc->base.dev;
  5151. struct drm_i915_private *dev_priv = dev->dev_private;
  5152. int pipe = pipe_config->cpu_transcoder;
  5153. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5154. intel_clock_t clock;
  5155. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
  5156. int refclk = 100000;
  5157. mutex_lock(&dev_priv->dpio_lock);
  5158. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  5159. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  5160. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  5161. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  5162. mutex_unlock(&dev_priv->dpio_lock);
  5163. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  5164. clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
  5165. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  5166. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  5167. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  5168. chv_clock(refclk, &clock);
  5169. /* clock.dot is the fast clock */
  5170. pipe_config->port_clock = clock.dot / 5;
  5171. }
  5172. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  5173. struct intel_crtc_config *pipe_config)
  5174. {
  5175. struct drm_device *dev = crtc->base.dev;
  5176. struct drm_i915_private *dev_priv = dev->dev_private;
  5177. uint32_t tmp;
  5178. if (!intel_display_power_enabled(dev_priv,
  5179. POWER_DOMAIN_PIPE(crtc->pipe)))
  5180. return false;
  5181. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  5182. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  5183. tmp = I915_READ(PIPECONF(crtc->pipe));
  5184. if (!(tmp & PIPECONF_ENABLE))
  5185. return false;
  5186. if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
  5187. switch (tmp & PIPECONF_BPC_MASK) {
  5188. case PIPECONF_6BPC:
  5189. pipe_config->pipe_bpp = 18;
  5190. break;
  5191. case PIPECONF_8BPC:
  5192. pipe_config->pipe_bpp = 24;
  5193. break;
  5194. case PIPECONF_10BPC:
  5195. pipe_config->pipe_bpp = 30;
  5196. break;
  5197. default:
  5198. break;
  5199. }
  5200. }
  5201. if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
  5202. pipe_config->limited_color_range = true;
  5203. if (INTEL_INFO(dev)->gen < 4)
  5204. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  5205. intel_get_pipe_timings(crtc, pipe_config);
  5206. i9xx_get_pfit_config(crtc, pipe_config);
  5207. if (INTEL_INFO(dev)->gen >= 4) {
  5208. tmp = I915_READ(DPLL_MD(crtc->pipe));
  5209. pipe_config->pixel_multiplier =
  5210. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  5211. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  5212. pipe_config->dpll_hw_state.dpll_md = tmp;
  5213. } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
  5214. tmp = I915_READ(DPLL(crtc->pipe));
  5215. pipe_config->pixel_multiplier =
  5216. ((tmp & SDVO_MULTIPLIER_MASK)
  5217. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  5218. } else {
  5219. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  5220. * port and will be fixed up in the encoder->get_config
  5221. * function. */
  5222. pipe_config->pixel_multiplier = 1;
  5223. }
  5224. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  5225. if (!IS_VALLEYVIEW(dev)) {
  5226. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  5227. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  5228. } else {
  5229. /* Mask out read-only status bits. */
  5230. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  5231. DPLL_PORTC_READY_MASK |
  5232. DPLL_PORTB_READY_MASK);
  5233. }
  5234. if (IS_CHERRYVIEW(dev))
  5235. chv_crtc_clock_get(crtc, pipe_config);
  5236. else if (IS_VALLEYVIEW(dev))
  5237. vlv_crtc_clock_get(crtc, pipe_config);
  5238. else
  5239. i9xx_crtc_clock_get(crtc, pipe_config);
  5240. return true;
  5241. }
  5242. static void ironlake_init_pch_refclk(struct drm_device *dev)
  5243. {
  5244. struct drm_i915_private *dev_priv = dev->dev_private;
  5245. struct drm_mode_config *mode_config = &dev->mode_config;
  5246. struct intel_encoder *encoder;
  5247. u32 val, final;
  5248. bool has_lvds = false;
  5249. bool has_cpu_edp = false;
  5250. bool has_panel = false;
  5251. bool has_ck505 = false;
  5252. bool can_ssc = false;
  5253. /* We need to take the global config into account */
  5254. list_for_each_entry(encoder, &mode_config->encoder_list,
  5255. base.head) {
  5256. switch (encoder->type) {
  5257. case INTEL_OUTPUT_LVDS:
  5258. has_panel = true;
  5259. has_lvds = true;
  5260. break;
  5261. case INTEL_OUTPUT_EDP:
  5262. has_panel = true;
  5263. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  5264. has_cpu_edp = true;
  5265. break;
  5266. }
  5267. }
  5268. if (HAS_PCH_IBX(dev)) {
  5269. has_ck505 = dev_priv->vbt.display_clock_mode;
  5270. can_ssc = has_ck505;
  5271. } else {
  5272. has_ck505 = false;
  5273. can_ssc = true;
  5274. }
  5275. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
  5276. has_panel, has_lvds, has_ck505);
  5277. /* Ironlake: try to setup display ref clock before DPLL
  5278. * enabling. This is only under driver's control after
  5279. * PCH B stepping, previous chipset stepping should be
  5280. * ignoring this setting.
  5281. */
  5282. val = I915_READ(PCH_DREF_CONTROL);
  5283. /* As we must carefully and slowly disable/enable each source in turn,
  5284. * compute the final state we want first and check if we need to
  5285. * make any changes at all.
  5286. */
  5287. final = val;
  5288. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  5289. if (has_ck505)
  5290. final |= DREF_NONSPREAD_CK505_ENABLE;
  5291. else
  5292. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  5293. final &= ~DREF_SSC_SOURCE_MASK;
  5294. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5295. final &= ~DREF_SSC1_ENABLE;
  5296. if (has_panel) {
  5297. final |= DREF_SSC_SOURCE_ENABLE;
  5298. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5299. final |= DREF_SSC1_ENABLE;
  5300. if (has_cpu_edp) {
  5301. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  5302. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5303. else
  5304. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5305. } else
  5306. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5307. } else {
  5308. final |= DREF_SSC_SOURCE_DISABLE;
  5309. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5310. }
  5311. if (final == val)
  5312. return;
  5313. /* Always enable nonspread source */
  5314. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  5315. if (has_ck505)
  5316. val |= DREF_NONSPREAD_CK505_ENABLE;
  5317. else
  5318. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  5319. if (has_panel) {
  5320. val &= ~DREF_SSC_SOURCE_MASK;
  5321. val |= DREF_SSC_SOURCE_ENABLE;
  5322. /* SSC must be turned on before enabling the CPU output */
  5323. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5324. DRM_DEBUG_KMS("Using SSC on panel\n");
  5325. val |= DREF_SSC1_ENABLE;
  5326. } else
  5327. val &= ~DREF_SSC1_ENABLE;
  5328. /* Get SSC going before enabling the outputs */
  5329. I915_WRITE(PCH_DREF_CONTROL, val);
  5330. POSTING_READ(PCH_DREF_CONTROL);
  5331. udelay(200);
  5332. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5333. /* Enable CPU source on CPU attached eDP */
  5334. if (has_cpu_edp) {
  5335. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  5336. DRM_DEBUG_KMS("Using SSC on eDP\n");
  5337. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  5338. } else
  5339. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  5340. } else
  5341. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5342. I915_WRITE(PCH_DREF_CONTROL, val);
  5343. POSTING_READ(PCH_DREF_CONTROL);
  5344. udelay(200);
  5345. } else {
  5346. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  5347. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  5348. /* Turn off CPU output */
  5349. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  5350. I915_WRITE(PCH_DREF_CONTROL, val);
  5351. POSTING_READ(PCH_DREF_CONTROL);
  5352. udelay(200);
  5353. /* Turn off the SSC source */
  5354. val &= ~DREF_SSC_SOURCE_MASK;
  5355. val |= DREF_SSC_SOURCE_DISABLE;
  5356. /* Turn off SSC1 */
  5357. val &= ~DREF_SSC1_ENABLE;
  5358. I915_WRITE(PCH_DREF_CONTROL, val);
  5359. POSTING_READ(PCH_DREF_CONTROL);
  5360. udelay(200);
  5361. }
  5362. BUG_ON(val != final);
  5363. }
  5364. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  5365. {
  5366. uint32_t tmp;
  5367. tmp = I915_READ(SOUTH_CHICKEN2);
  5368. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  5369. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5370. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  5371. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  5372. DRM_ERROR("FDI mPHY reset assert timeout\n");
  5373. tmp = I915_READ(SOUTH_CHICKEN2);
  5374. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  5375. I915_WRITE(SOUTH_CHICKEN2, tmp);
  5376. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  5377. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  5378. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  5379. }
  5380. /* WaMPhyProgramming:hsw */
  5381. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  5382. {
  5383. uint32_t tmp;
  5384. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  5385. tmp &= ~(0xFF << 24);
  5386. tmp |= (0x12 << 24);
  5387. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  5388. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  5389. tmp |= (1 << 11);
  5390. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  5391. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  5392. tmp |= (1 << 11);
  5393. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  5394. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  5395. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5396. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  5397. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  5398. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  5399. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  5400. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  5401. tmp &= ~(7 << 13);
  5402. tmp |= (5 << 13);
  5403. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  5404. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  5405. tmp &= ~(7 << 13);
  5406. tmp |= (5 << 13);
  5407. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  5408. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  5409. tmp &= ~0xFF;
  5410. tmp |= 0x1C;
  5411. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  5412. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  5413. tmp &= ~0xFF;
  5414. tmp |= 0x1C;
  5415. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  5416. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  5417. tmp &= ~(0xFF << 16);
  5418. tmp |= (0x1C << 16);
  5419. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  5420. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  5421. tmp &= ~(0xFF << 16);
  5422. tmp |= (0x1C << 16);
  5423. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  5424. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  5425. tmp |= (1 << 27);
  5426. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  5427. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  5428. tmp |= (1 << 27);
  5429. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  5430. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  5431. tmp &= ~(0xF << 28);
  5432. tmp |= (4 << 28);
  5433. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  5434. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  5435. tmp &= ~(0xF << 28);
  5436. tmp |= (4 << 28);
  5437. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  5438. }
  5439. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  5440. * Programming" based on the parameters passed:
  5441. * - Sequence to enable CLKOUT_DP
  5442. * - Sequence to enable CLKOUT_DP without spread
  5443. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  5444. */
  5445. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  5446. bool with_fdi)
  5447. {
  5448. struct drm_i915_private *dev_priv = dev->dev_private;
  5449. uint32_t reg, tmp;
  5450. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  5451. with_spread = true;
  5452. if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
  5453. with_fdi, "LP PCH doesn't have FDI\n"))
  5454. with_fdi = false;
  5455. mutex_lock(&dev_priv->dpio_lock);
  5456. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5457. tmp &= ~SBI_SSCCTL_DISABLE;
  5458. tmp |= SBI_SSCCTL_PATHALT;
  5459. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5460. udelay(24);
  5461. if (with_spread) {
  5462. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5463. tmp &= ~SBI_SSCCTL_PATHALT;
  5464. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5465. if (with_fdi) {
  5466. lpt_reset_fdi_mphy(dev_priv);
  5467. lpt_program_fdi_mphy(dev_priv);
  5468. }
  5469. }
  5470. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5471. SBI_GEN0 : SBI_DBUFF0;
  5472. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5473. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5474. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5475. mutex_unlock(&dev_priv->dpio_lock);
  5476. }
  5477. /* Sequence to disable CLKOUT_DP */
  5478. static void lpt_disable_clkout_dp(struct drm_device *dev)
  5479. {
  5480. struct drm_i915_private *dev_priv = dev->dev_private;
  5481. uint32_t reg, tmp;
  5482. mutex_lock(&dev_priv->dpio_lock);
  5483. reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
  5484. SBI_GEN0 : SBI_DBUFF0;
  5485. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  5486. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  5487. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  5488. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  5489. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  5490. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  5491. tmp |= SBI_SSCCTL_PATHALT;
  5492. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5493. udelay(32);
  5494. }
  5495. tmp |= SBI_SSCCTL_DISABLE;
  5496. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  5497. }
  5498. mutex_unlock(&dev_priv->dpio_lock);
  5499. }
  5500. static void lpt_init_pch_refclk(struct drm_device *dev)
  5501. {
  5502. struct drm_mode_config *mode_config = &dev->mode_config;
  5503. struct intel_encoder *encoder;
  5504. bool has_vga = false;
  5505. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  5506. switch (encoder->type) {
  5507. case INTEL_OUTPUT_ANALOG:
  5508. has_vga = true;
  5509. break;
  5510. }
  5511. }
  5512. if (has_vga)
  5513. lpt_enable_clkout_dp(dev, true, true);
  5514. else
  5515. lpt_disable_clkout_dp(dev);
  5516. }
  5517. /*
  5518. * Initialize reference clocks when the driver loads
  5519. */
  5520. void intel_init_pch_refclk(struct drm_device *dev)
  5521. {
  5522. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  5523. ironlake_init_pch_refclk(dev);
  5524. else if (HAS_PCH_LPT(dev))
  5525. lpt_init_pch_refclk(dev);
  5526. }
  5527. static int ironlake_get_refclk(struct drm_crtc *crtc)
  5528. {
  5529. struct drm_device *dev = crtc->dev;
  5530. struct drm_i915_private *dev_priv = dev->dev_private;
  5531. struct intel_encoder *encoder;
  5532. int num_connectors = 0;
  5533. bool is_lvds = false;
  5534. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5535. switch (encoder->type) {
  5536. case INTEL_OUTPUT_LVDS:
  5537. is_lvds = true;
  5538. break;
  5539. }
  5540. num_connectors++;
  5541. }
  5542. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  5543. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  5544. dev_priv->vbt.lvds_ssc_freq);
  5545. return dev_priv->vbt.lvds_ssc_freq;
  5546. }
  5547. return 120000;
  5548. }
  5549. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  5550. {
  5551. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  5552. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5553. int pipe = intel_crtc->pipe;
  5554. uint32_t val;
  5555. val = 0;
  5556. switch (intel_crtc->config.pipe_bpp) {
  5557. case 18:
  5558. val |= PIPECONF_6BPC;
  5559. break;
  5560. case 24:
  5561. val |= PIPECONF_8BPC;
  5562. break;
  5563. case 30:
  5564. val |= PIPECONF_10BPC;
  5565. break;
  5566. case 36:
  5567. val |= PIPECONF_12BPC;
  5568. break;
  5569. default:
  5570. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5571. BUG();
  5572. }
  5573. if (intel_crtc->config.dither)
  5574. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5575. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5576. val |= PIPECONF_INTERLACED_ILK;
  5577. else
  5578. val |= PIPECONF_PROGRESSIVE;
  5579. if (intel_crtc->config.limited_color_range)
  5580. val |= PIPECONF_COLOR_RANGE_SELECT;
  5581. I915_WRITE(PIPECONF(pipe), val);
  5582. POSTING_READ(PIPECONF(pipe));
  5583. }
  5584. /*
  5585. * Set up the pipe CSC unit.
  5586. *
  5587. * Currently only full range RGB to limited range RGB conversion
  5588. * is supported, but eventually this should handle various
  5589. * RGB<->YCbCr scenarios as well.
  5590. */
  5591. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  5592. {
  5593. struct drm_device *dev = crtc->dev;
  5594. struct drm_i915_private *dev_priv = dev->dev_private;
  5595. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5596. int pipe = intel_crtc->pipe;
  5597. uint16_t coeff = 0x7800; /* 1.0 */
  5598. /*
  5599. * TODO: Check what kind of values actually come out of the pipe
  5600. * with these coeff/postoff values and adjust to get the best
  5601. * accuracy. Perhaps we even need to take the bpc value into
  5602. * consideration.
  5603. */
  5604. if (intel_crtc->config.limited_color_range)
  5605. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  5606. /*
  5607. * GY/GU and RY/RU should be the other way around according
  5608. * to BSpec, but reality doesn't agree. Just set them up in
  5609. * a way that results in the correct picture.
  5610. */
  5611. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  5612. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  5613. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  5614. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  5615. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  5616. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  5617. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  5618. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  5619. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  5620. if (INTEL_INFO(dev)->gen > 6) {
  5621. uint16_t postoff = 0;
  5622. if (intel_crtc->config.limited_color_range)
  5623. postoff = (16 * (1 << 12) / 255) & 0x1fff;
  5624. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  5625. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  5626. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  5627. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  5628. } else {
  5629. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  5630. if (intel_crtc->config.limited_color_range)
  5631. mode |= CSC_BLACK_SCREEN_OFFSET;
  5632. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  5633. }
  5634. }
  5635. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  5636. {
  5637. struct drm_device *dev = crtc->dev;
  5638. struct drm_i915_private *dev_priv = dev->dev_private;
  5639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5640. enum pipe pipe = intel_crtc->pipe;
  5641. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5642. uint32_t val;
  5643. val = 0;
  5644. if (IS_HASWELL(dev) && intel_crtc->config.dither)
  5645. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  5646. if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  5647. val |= PIPECONF_INTERLACED_ILK;
  5648. else
  5649. val |= PIPECONF_PROGRESSIVE;
  5650. I915_WRITE(PIPECONF(cpu_transcoder), val);
  5651. POSTING_READ(PIPECONF(cpu_transcoder));
  5652. I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
  5653. POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
  5654. if (IS_BROADWELL(dev)) {
  5655. val = 0;
  5656. switch (intel_crtc->config.pipe_bpp) {
  5657. case 18:
  5658. val |= PIPEMISC_DITHER_6_BPC;
  5659. break;
  5660. case 24:
  5661. val |= PIPEMISC_DITHER_8_BPC;
  5662. break;
  5663. case 30:
  5664. val |= PIPEMISC_DITHER_10_BPC;
  5665. break;
  5666. case 36:
  5667. val |= PIPEMISC_DITHER_12_BPC;
  5668. break;
  5669. default:
  5670. /* Case prevented by pipe_config_set_bpp. */
  5671. BUG();
  5672. }
  5673. if (intel_crtc->config.dither)
  5674. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  5675. I915_WRITE(PIPEMISC(pipe), val);
  5676. }
  5677. }
  5678. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  5679. intel_clock_t *clock,
  5680. bool *has_reduced_clock,
  5681. intel_clock_t *reduced_clock)
  5682. {
  5683. struct drm_device *dev = crtc->dev;
  5684. struct drm_i915_private *dev_priv = dev->dev_private;
  5685. struct intel_encoder *intel_encoder;
  5686. int refclk;
  5687. const intel_limit_t *limit;
  5688. bool ret, is_lvds = false;
  5689. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5690. switch (intel_encoder->type) {
  5691. case INTEL_OUTPUT_LVDS:
  5692. is_lvds = true;
  5693. break;
  5694. }
  5695. }
  5696. refclk = ironlake_get_refclk(crtc);
  5697. /*
  5698. * Returns a set of divisors for the desired target clock with the given
  5699. * refclk, or FALSE. The returned values represent the clock equation:
  5700. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  5701. */
  5702. limit = intel_limit(crtc, refclk);
  5703. ret = dev_priv->display.find_dpll(limit, crtc,
  5704. to_intel_crtc(crtc)->config.port_clock,
  5705. refclk, NULL, clock);
  5706. if (!ret)
  5707. return false;
  5708. if (is_lvds && dev_priv->lvds_downclock_avail) {
  5709. /*
  5710. * Ensure we match the reduced clock's P to the target clock.
  5711. * If the clocks don't match, we can't switch the display clock
  5712. * by using the FP0/FP1. In such case we will disable the LVDS
  5713. * downclock feature.
  5714. */
  5715. *has_reduced_clock =
  5716. dev_priv->display.find_dpll(limit, crtc,
  5717. dev_priv->lvds_downclock,
  5718. refclk, clock,
  5719. reduced_clock);
  5720. }
  5721. return true;
  5722. }
  5723. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  5724. {
  5725. /*
  5726. * Account for spread spectrum to avoid
  5727. * oversubscribing the link. Max center spread
  5728. * is 2.5%; use 5% for safety's sake.
  5729. */
  5730. u32 bps = target_clock * bpp * 21 / 20;
  5731. return DIV_ROUND_UP(bps, link_bw * 8);
  5732. }
  5733. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  5734. {
  5735. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  5736. }
  5737. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  5738. u32 *fp,
  5739. intel_clock_t *reduced_clock, u32 *fp2)
  5740. {
  5741. struct drm_crtc *crtc = &intel_crtc->base;
  5742. struct drm_device *dev = crtc->dev;
  5743. struct drm_i915_private *dev_priv = dev->dev_private;
  5744. struct intel_encoder *intel_encoder;
  5745. uint32_t dpll;
  5746. int factor, num_connectors = 0;
  5747. bool is_lvds = false, is_sdvo = false;
  5748. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  5749. switch (intel_encoder->type) {
  5750. case INTEL_OUTPUT_LVDS:
  5751. is_lvds = true;
  5752. break;
  5753. case INTEL_OUTPUT_SDVO:
  5754. case INTEL_OUTPUT_HDMI:
  5755. is_sdvo = true;
  5756. break;
  5757. }
  5758. num_connectors++;
  5759. }
  5760. /* Enable autotuning of the PLL clock (if permissible) */
  5761. factor = 21;
  5762. if (is_lvds) {
  5763. if ((intel_panel_use_ssc(dev_priv) &&
  5764. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  5765. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  5766. factor = 25;
  5767. } else if (intel_crtc->config.sdvo_tv_clock)
  5768. factor = 20;
  5769. if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
  5770. *fp |= FP_CB_TUNE;
  5771. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  5772. *fp2 |= FP_CB_TUNE;
  5773. dpll = 0;
  5774. if (is_lvds)
  5775. dpll |= DPLLB_MODE_LVDS;
  5776. else
  5777. dpll |= DPLLB_MODE_DAC_SERIAL;
  5778. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  5779. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  5780. if (is_sdvo)
  5781. dpll |= DPLL_SDVO_HIGH_SPEED;
  5782. if (intel_crtc->config.has_dp_encoder)
  5783. dpll |= DPLL_SDVO_HIGH_SPEED;
  5784. /* compute bitmask from p1 value */
  5785. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5786. /* also FPA1 */
  5787. dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5788. switch (intel_crtc->config.dpll.p2) {
  5789. case 5:
  5790. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5791. break;
  5792. case 7:
  5793. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5794. break;
  5795. case 10:
  5796. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5797. break;
  5798. case 14:
  5799. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5800. break;
  5801. }
  5802. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  5803. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5804. else
  5805. dpll |= PLL_REF_INPUT_DREFCLK;
  5806. return dpll | DPLL_VCO_ENABLE;
  5807. }
  5808. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  5809. int x, int y,
  5810. struct drm_framebuffer *fb)
  5811. {
  5812. struct drm_device *dev = crtc->dev;
  5813. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5814. int num_connectors = 0;
  5815. intel_clock_t clock, reduced_clock;
  5816. u32 dpll = 0, fp = 0, fp2 = 0;
  5817. bool ok, has_reduced_clock = false;
  5818. bool is_lvds = false;
  5819. struct intel_encoder *encoder;
  5820. struct intel_shared_dpll *pll;
  5821. for_each_encoder_on_crtc(dev, crtc, encoder) {
  5822. switch (encoder->type) {
  5823. case INTEL_OUTPUT_LVDS:
  5824. is_lvds = true;
  5825. break;
  5826. }
  5827. num_connectors++;
  5828. }
  5829. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  5830. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  5831. ok = ironlake_compute_clocks(crtc, &clock,
  5832. &has_reduced_clock, &reduced_clock);
  5833. if (!ok && !intel_crtc->config.clock_set) {
  5834. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5835. return -EINVAL;
  5836. }
  5837. /* Compat-code for transition, will disappear. */
  5838. if (!intel_crtc->config.clock_set) {
  5839. intel_crtc->config.dpll.n = clock.n;
  5840. intel_crtc->config.dpll.m1 = clock.m1;
  5841. intel_crtc->config.dpll.m2 = clock.m2;
  5842. intel_crtc->config.dpll.p1 = clock.p1;
  5843. intel_crtc->config.dpll.p2 = clock.p2;
  5844. }
  5845. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  5846. if (intel_crtc->config.has_pch_encoder) {
  5847. fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
  5848. if (has_reduced_clock)
  5849. fp2 = i9xx_dpll_compute_fp(&reduced_clock);
  5850. dpll = ironlake_compute_dpll(intel_crtc,
  5851. &fp, &reduced_clock,
  5852. has_reduced_clock ? &fp2 : NULL);
  5853. intel_crtc->config.dpll_hw_state.dpll = dpll;
  5854. intel_crtc->config.dpll_hw_state.fp0 = fp;
  5855. if (has_reduced_clock)
  5856. intel_crtc->config.dpll_hw_state.fp1 = fp2;
  5857. else
  5858. intel_crtc->config.dpll_hw_state.fp1 = fp;
  5859. pll = intel_get_shared_dpll(intel_crtc);
  5860. if (pll == NULL) {
  5861. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  5862. pipe_name(intel_crtc->pipe));
  5863. return -EINVAL;
  5864. }
  5865. } else
  5866. intel_put_shared_dpll(intel_crtc);
  5867. if (is_lvds && has_reduced_clock && i915.powersave)
  5868. intel_crtc->lowfreq_avail = true;
  5869. else
  5870. intel_crtc->lowfreq_avail = false;
  5871. return 0;
  5872. }
  5873. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  5874. struct intel_link_m_n *m_n)
  5875. {
  5876. struct drm_device *dev = crtc->base.dev;
  5877. struct drm_i915_private *dev_priv = dev->dev_private;
  5878. enum pipe pipe = crtc->pipe;
  5879. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  5880. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  5881. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  5882. & ~TU_SIZE_MASK;
  5883. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  5884. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  5885. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5886. }
  5887. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  5888. enum transcoder transcoder,
  5889. struct intel_link_m_n *m_n)
  5890. {
  5891. struct drm_device *dev = crtc->base.dev;
  5892. struct drm_i915_private *dev_priv = dev->dev_private;
  5893. enum pipe pipe = crtc->pipe;
  5894. if (INTEL_INFO(dev)->gen >= 5) {
  5895. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  5896. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  5897. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  5898. & ~TU_SIZE_MASK;
  5899. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  5900. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  5901. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5902. } else {
  5903. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  5904. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  5905. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  5906. & ~TU_SIZE_MASK;
  5907. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  5908. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  5909. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  5910. }
  5911. }
  5912. void intel_dp_get_m_n(struct intel_crtc *crtc,
  5913. struct intel_crtc_config *pipe_config)
  5914. {
  5915. if (crtc->config.has_pch_encoder)
  5916. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  5917. else
  5918. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5919. &pipe_config->dp_m_n);
  5920. }
  5921. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  5922. struct intel_crtc_config *pipe_config)
  5923. {
  5924. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  5925. &pipe_config->fdi_m_n);
  5926. }
  5927. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  5928. struct intel_crtc_config *pipe_config)
  5929. {
  5930. struct drm_device *dev = crtc->base.dev;
  5931. struct drm_i915_private *dev_priv = dev->dev_private;
  5932. uint32_t tmp;
  5933. tmp = I915_READ(PF_CTL(crtc->pipe));
  5934. if (tmp & PF_ENABLE) {
  5935. pipe_config->pch_pfit.enabled = true;
  5936. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  5937. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  5938. /* We currently do not free assignements of panel fitters on
  5939. * ivb/hsw (since we don't use the higher upscaling modes which
  5940. * differentiates them) so just WARN about this case for now. */
  5941. if (IS_GEN7(dev)) {
  5942. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  5943. PF_PIPE_SEL_IVB(crtc->pipe));
  5944. }
  5945. }
  5946. }
  5947. static void ironlake_get_plane_config(struct intel_crtc *crtc,
  5948. struct intel_plane_config *plane_config)
  5949. {
  5950. struct drm_device *dev = crtc->base.dev;
  5951. struct drm_i915_private *dev_priv = dev->dev_private;
  5952. u32 val, base, offset;
  5953. int pipe = crtc->pipe, plane = crtc->plane;
  5954. int fourcc, pixel_format;
  5955. int aligned_height;
  5956. crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
  5957. if (!crtc->base.primary->fb) {
  5958. DRM_DEBUG_KMS("failed to alloc fb\n");
  5959. return;
  5960. }
  5961. val = I915_READ(DSPCNTR(plane));
  5962. if (INTEL_INFO(dev)->gen >= 4)
  5963. if (val & DISPPLANE_TILED)
  5964. plane_config->tiled = true;
  5965. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  5966. fourcc = intel_format_to_fourcc(pixel_format);
  5967. crtc->base.primary->fb->pixel_format = fourcc;
  5968. crtc->base.primary->fb->bits_per_pixel =
  5969. drm_format_plane_cpp(fourcc, 0) * 8;
  5970. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  5971. if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
  5972. offset = I915_READ(DSPOFFSET(plane));
  5973. } else {
  5974. if (plane_config->tiled)
  5975. offset = I915_READ(DSPTILEOFF(plane));
  5976. else
  5977. offset = I915_READ(DSPLINOFF(plane));
  5978. }
  5979. plane_config->base = base;
  5980. val = I915_READ(PIPESRC(pipe));
  5981. crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
  5982. crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
  5983. val = I915_READ(DSPSTRIDE(pipe));
  5984. crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
  5985. aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
  5986. plane_config->tiled);
  5987. plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
  5988. aligned_height, PAGE_SIZE);
  5989. DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  5990. pipe, plane, crtc->base.primary->fb->width,
  5991. crtc->base.primary->fb->height,
  5992. crtc->base.primary->fb->bits_per_pixel, base,
  5993. crtc->base.primary->fb->pitches[0],
  5994. plane_config->size);
  5995. }
  5996. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  5997. struct intel_crtc_config *pipe_config)
  5998. {
  5999. struct drm_device *dev = crtc->base.dev;
  6000. struct drm_i915_private *dev_priv = dev->dev_private;
  6001. uint32_t tmp;
  6002. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6003. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6004. tmp = I915_READ(PIPECONF(crtc->pipe));
  6005. if (!(tmp & PIPECONF_ENABLE))
  6006. return false;
  6007. switch (tmp & PIPECONF_BPC_MASK) {
  6008. case PIPECONF_6BPC:
  6009. pipe_config->pipe_bpp = 18;
  6010. break;
  6011. case PIPECONF_8BPC:
  6012. pipe_config->pipe_bpp = 24;
  6013. break;
  6014. case PIPECONF_10BPC:
  6015. pipe_config->pipe_bpp = 30;
  6016. break;
  6017. case PIPECONF_12BPC:
  6018. pipe_config->pipe_bpp = 36;
  6019. break;
  6020. default:
  6021. break;
  6022. }
  6023. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  6024. pipe_config->limited_color_range = true;
  6025. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  6026. struct intel_shared_dpll *pll;
  6027. pipe_config->has_pch_encoder = true;
  6028. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  6029. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6030. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6031. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6032. if (HAS_PCH_IBX(dev_priv->dev)) {
  6033. pipe_config->shared_dpll =
  6034. (enum intel_dpll_id) crtc->pipe;
  6035. } else {
  6036. tmp = I915_READ(PCH_DPLL_SEL);
  6037. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  6038. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
  6039. else
  6040. pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
  6041. }
  6042. pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
  6043. WARN_ON(!pll->get_hw_state(dev_priv, pll,
  6044. &pipe_config->dpll_hw_state));
  6045. tmp = pipe_config->dpll_hw_state.dpll;
  6046. pipe_config->pixel_multiplier =
  6047. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  6048. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  6049. ironlake_pch_clock_get(crtc, pipe_config);
  6050. } else {
  6051. pipe_config->pixel_multiplier = 1;
  6052. }
  6053. intel_get_pipe_timings(crtc, pipe_config);
  6054. ironlake_get_pfit_config(crtc, pipe_config);
  6055. return true;
  6056. }
  6057. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  6058. {
  6059. struct drm_device *dev = dev_priv->dev;
  6060. struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
  6061. struct intel_crtc *crtc;
  6062. for_each_intel_crtc(dev, crtc)
  6063. WARN(crtc->active, "CRTC for pipe %c enabled\n",
  6064. pipe_name(crtc->pipe));
  6065. WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  6066. WARN(plls->spll_refcount, "SPLL enabled\n");
  6067. WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
  6068. WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
  6069. WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
  6070. WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  6071. "CPU PWM1 enabled\n");
  6072. WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  6073. "CPU PWM2 enabled\n");
  6074. WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  6075. "PCH PWM1 enabled\n");
  6076. WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  6077. "Utility pin enabled\n");
  6078. WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  6079. /*
  6080. * In theory we can still leave IRQs enabled, as long as only the HPD
  6081. * interrupts remain enabled. We used to check for that, but since it's
  6082. * gen-specific and since we only disable LCPLL after we fully disable
  6083. * the interrupts, the check below should be enough.
  6084. */
  6085. WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
  6086. }
  6087. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  6088. {
  6089. struct drm_device *dev = dev_priv->dev;
  6090. if (IS_HASWELL(dev)) {
  6091. mutex_lock(&dev_priv->rps.hw_lock);
  6092. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  6093. val))
  6094. DRM_ERROR("Failed to disable D_COMP\n");
  6095. mutex_unlock(&dev_priv->rps.hw_lock);
  6096. } else {
  6097. I915_WRITE(D_COMP, val);
  6098. }
  6099. POSTING_READ(D_COMP);
  6100. }
  6101. /*
  6102. * This function implements pieces of two sequences from BSpec:
  6103. * - Sequence for display software to disable LCPLL
  6104. * - Sequence for display software to allow package C8+
  6105. * The steps implemented here are just the steps that actually touch the LCPLL
  6106. * register. Callers should take care of disabling all the display engine
  6107. * functions, doing the mode unset, fixing interrupts, etc.
  6108. */
  6109. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  6110. bool switch_to_fclk, bool allow_power_down)
  6111. {
  6112. uint32_t val;
  6113. assert_can_disable_lcpll(dev_priv);
  6114. val = I915_READ(LCPLL_CTL);
  6115. if (switch_to_fclk) {
  6116. val |= LCPLL_CD_SOURCE_FCLK;
  6117. I915_WRITE(LCPLL_CTL, val);
  6118. if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
  6119. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  6120. DRM_ERROR("Switching to FCLK failed\n");
  6121. val = I915_READ(LCPLL_CTL);
  6122. }
  6123. val |= LCPLL_PLL_DISABLE;
  6124. I915_WRITE(LCPLL_CTL, val);
  6125. POSTING_READ(LCPLL_CTL);
  6126. if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
  6127. DRM_ERROR("LCPLL still locked\n");
  6128. val = I915_READ(D_COMP);
  6129. val |= D_COMP_COMP_DISABLE;
  6130. hsw_write_dcomp(dev_priv, val);
  6131. ndelay(100);
  6132. if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
  6133. DRM_ERROR("D_COMP RCOMP still in progress\n");
  6134. if (allow_power_down) {
  6135. val = I915_READ(LCPLL_CTL);
  6136. val |= LCPLL_POWER_DOWN_ALLOW;
  6137. I915_WRITE(LCPLL_CTL, val);
  6138. POSTING_READ(LCPLL_CTL);
  6139. }
  6140. }
  6141. /*
  6142. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  6143. * source.
  6144. */
  6145. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  6146. {
  6147. uint32_t val;
  6148. unsigned long irqflags;
  6149. val = I915_READ(LCPLL_CTL);
  6150. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  6151. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  6152. return;
  6153. /*
  6154. * Make sure we're not on PC8 state before disabling PC8, otherwise
  6155. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  6156. *
  6157. * The other problem is that hsw_restore_lcpll() is called as part of
  6158. * the runtime PM resume sequence, so we can't just call
  6159. * gen6_gt_force_wake_get() because that function calls
  6160. * intel_runtime_pm_get(), and we can't change the runtime PM refcount
  6161. * while we are on the resume sequence. So to solve this problem we have
  6162. * to call special forcewake code that doesn't touch runtime PM and
  6163. * doesn't enable the forcewake delayed work.
  6164. */
  6165. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6166. if (dev_priv->uncore.forcewake_count++ == 0)
  6167. dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
  6168. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6169. if (val & LCPLL_POWER_DOWN_ALLOW) {
  6170. val &= ~LCPLL_POWER_DOWN_ALLOW;
  6171. I915_WRITE(LCPLL_CTL, val);
  6172. POSTING_READ(LCPLL_CTL);
  6173. }
  6174. val = I915_READ(D_COMP);
  6175. val |= D_COMP_COMP_FORCE;
  6176. val &= ~D_COMP_COMP_DISABLE;
  6177. hsw_write_dcomp(dev_priv, val);
  6178. val = I915_READ(LCPLL_CTL);
  6179. val &= ~LCPLL_PLL_DISABLE;
  6180. I915_WRITE(LCPLL_CTL, val);
  6181. if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
  6182. DRM_ERROR("LCPLL not locked yet\n");
  6183. if (val & LCPLL_CD_SOURCE_FCLK) {
  6184. val = I915_READ(LCPLL_CTL);
  6185. val &= ~LCPLL_CD_SOURCE_FCLK;
  6186. I915_WRITE(LCPLL_CTL, val);
  6187. if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
  6188. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  6189. DRM_ERROR("Switching back to LCPLL failed\n");
  6190. }
  6191. /* See the big comment above. */
  6192. spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
  6193. if (--dev_priv->uncore.forcewake_count == 0)
  6194. dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
  6195. spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
  6196. }
  6197. /*
  6198. * Package states C8 and deeper are really deep PC states that can only be
  6199. * reached when all the devices on the system allow it, so even if the graphics
  6200. * device allows PC8+, it doesn't mean the system will actually get to these
  6201. * states. Our driver only allows PC8+ when going into runtime PM.
  6202. *
  6203. * The requirements for PC8+ are that all the outputs are disabled, the power
  6204. * well is disabled and most interrupts are disabled, and these are also
  6205. * requirements for runtime PM. When these conditions are met, we manually do
  6206. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  6207. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  6208. * hang the machine.
  6209. *
  6210. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  6211. * the state of some registers, so when we come back from PC8+ we need to
  6212. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  6213. * need to take care of the registers kept by RC6. Notice that this happens even
  6214. * if we don't put the device in PCI D3 state (which is what currently happens
  6215. * because of the runtime PM support).
  6216. *
  6217. * For more, read "Display Sequences for Package C8" on the hardware
  6218. * documentation.
  6219. */
  6220. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  6221. {
  6222. struct drm_device *dev = dev_priv->dev;
  6223. uint32_t val;
  6224. DRM_DEBUG_KMS("Enabling package C8+\n");
  6225. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6226. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6227. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  6228. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6229. }
  6230. lpt_disable_clkout_dp(dev);
  6231. hsw_disable_lcpll(dev_priv, true, true);
  6232. }
  6233. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  6234. {
  6235. struct drm_device *dev = dev_priv->dev;
  6236. uint32_t val;
  6237. DRM_DEBUG_KMS("Disabling package C8+\n");
  6238. hsw_restore_lcpll(dev_priv);
  6239. lpt_init_pch_refclk(dev);
  6240. if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
  6241. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  6242. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  6243. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  6244. }
  6245. intel_prepare_ddi(dev);
  6246. }
  6247. static void snb_modeset_global_resources(struct drm_device *dev)
  6248. {
  6249. modeset_update_crtc_power_domains(dev);
  6250. }
  6251. static void haswell_modeset_global_resources(struct drm_device *dev)
  6252. {
  6253. modeset_update_crtc_power_domains(dev);
  6254. }
  6255. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  6256. int x, int y,
  6257. struct drm_framebuffer *fb)
  6258. {
  6259. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6260. if (!intel_ddi_pll_select(intel_crtc))
  6261. return -EINVAL;
  6262. intel_ddi_pll_enable(intel_crtc);
  6263. intel_crtc->lowfreq_avail = false;
  6264. return 0;
  6265. }
  6266. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  6267. struct intel_crtc_config *pipe_config)
  6268. {
  6269. struct drm_device *dev = crtc->base.dev;
  6270. struct drm_i915_private *dev_priv = dev->dev_private;
  6271. enum intel_display_power_domain pfit_domain;
  6272. uint32_t tmp;
  6273. if (!intel_display_power_enabled(dev_priv,
  6274. POWER_DOMAIN_PIPE(crtc->pipe)))
  6275. return false;
  6276. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6277. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  6278. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  6279. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  6280. enum pipe trans_edp_pipe;
  6281. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  6282. default:
  6283. WARN(1, "unknown pipe linked to edp transcoder\n");
  6284. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  6285. case TRANS_DDI_EDP_INPUT_A_ON:
  6286. trans_edp_pipe = PIPE_A;
  6287. break;
  6288. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  6289. trans_edp_pipe = PIPE_B;
  6290. break;
  6291. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  6292. trans_edp_pipe = PIPE_C;
  6293. break;
  6294. }
  6295. if (trans_edp_pipe == crtc->pipe)
  6296. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  6297. }
  6298. if (!intel_display_power_enabled(dev_priv,
  6299. POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
  6300. return false;
  6301. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  6302. if (!(tmp & PIPECONF_ENABLE))
  6303. return false;
  6304. /*
  6305. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  6306. * DDI E. So just check whether this pipe is wired to DDI E and whether
  6307. * the PCH transcoder is on.
  6308. */
  6309. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  6310. if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
  6311. I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  6312. pipe_config->has_pch_encoder = true;
  6313. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  6314. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  6315. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  6316. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  6317. }
  6318. intel_get_pipe_timings(crtc, pipe_config);
  6319. pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  6320. if (intel_display_power_enabled(dev_priv, pfit_domain))
  6321. ironlake_get_pfit_config(crtc, pipe_config);
  6322. if (IS_HASWELL(dev))
  6323. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  6324. (I915_READ(IPS_CTL) & IPS_ENABLE);
  6325. pipe_config->pixel_multiplier = 1;
  6326. return true;
  6327. }
  6328. static struct {
  6329. int clock;
  6330. u32 config;
  6331. } hdmi_audio_clock[] = {
  6332. { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
  6333. { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
  6334. { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
  6335. { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
  6336. { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
  6337. { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
  6338. { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
  6339. { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
  6340. { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
  6341. { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
  6342. };
  6343. /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
  6344. static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
  6345. {
  6346. int i;
  6347. for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
  6348. if (mode->clock == hdmi_audio_clock[i].clock)
  6349. break;
  6350. }
  6351. if (i == ARRAY_SIZE(hdmi_audio_clock)) {
  6352. DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
  6353. i = 1;
  6354. }
  6355. DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
  6356. hdmi_audio_clock[i].clock,
  6357. hdmi_audio_clock[i].config);
  6358. return hdmi_audio_clock[i].config;
  6359. }
  6360. static bool intel_eld_uptodate(struct drm_connector *connector,
  6361. int reg_eldv, uint32_t bits_eldv,
  6362. int reg_elda, uint32_t bits_elda,
  6363. int reg_edid)
  6364. {
  6365. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6366. uint8_t *eld = connector->eld;
  6367. uint32_t i;
  6368. i = I915_READ(reg_eldv);
  6369. i &= bits_eldv;
  6370. if (!eld[0])
  6371. return !i;
  6372. if (!i)
  6373. return false;
  6374. i = I915_READ(reg_elda);
  6375. i &= ~bits_elda;
  6376. I915_WRITE(reg_elda, i);
  6377. for (i = 0; i < eld[2]; i++)
  6378. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  6379. return false;
  6380. return true;
  6381. }
  6382. static void g4x_write_eld(struct drm_connector *connector,
  6383. struct drm_crtc *crtc,
  6384. struct drm_display_mode *mode)
  6385. {
  6386. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6387. uint8_t *eld = connector->eld;
  6388. uint32_t eldv;
  6389. uint32_t len;
  6390. uint32_t i;
  6391. i = I915_READ(G4X_AUD_VID_DID);
  6392. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  6393. eldv = G4X_ELDV_DEVCL_DEVBLC;
  6394. else
  6395. eldv = G4X_ELDV_DEVCTG;
  6396. if (intel_eld_uptodate(connector,
  6397. G4X_AUD_CNTL_ST, eldv,
  6398. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  6399. G4X_HDMIW_HDMIEDID))
  6400. return;
  6401. i = I915_READ(G4X_AUD_CNTL_ST);
  6402. i &= ~(eldv | G4X_ELD_ADDR);
  6403. len = (i >> 9) & 0x1f; /* ELD buffer size */
  6404. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6405. if (!eld[0])
  6406. return;
  6407. len = min_t(uint8_t, eld[2], len);
  6408. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6409. for (i = 0; i < len; i++)
  6410. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  6411. i = I915_READ(G4X_AUD_CNTL_ST);
  6412. i |= eldv;
  6413. I915_WRITE(G4X_AUD_CNTL_ST, i);
  6414. }
  6415. static void haswell_write_eld(struct drm_connector *connector,
  6416. struct drm_crtc *crtc,
  6417. struct drm_display_mode *mode)
  6418. {
  6419. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6420. uint8_t *eld = connector->eld;
  6421. uint32_t eldv;
  6422. uint32_t i;
  6423. int len;
  6424. int pipe = to_intel_crtc(crtc)->pipe;
  6425. int tmp;
  6426. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  6427. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  6428. int aud_config = HSW_AUD_CFG(pipe);
  6429. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  6430. /* Audio output enable */
  6431. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  6432. tmp = I915_READ(aud_cntrl_st2);
  6433. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  6434. I915_WRITE(aud_cntrl_st2, tmp);
  6435. POSTING_READ(aud_cntrl_st2);
  6436. assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
  6437. /* Set ELD valid state */
  6438. tmp = I915_READ(aud_cntrl_st2);
  6439. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
  6440. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  6441. I915_WRITE(aud_cntrl_st2, tmp);
  6442. tmp = I915_READ(aud_cntrl_st2);
  6443. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
  6444. /* Enable HDMI mode */
  6445. tmp = I915_READ(aud_config);
  6446. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
  6447. /* clear N_programing_enable and N_value_index */
  6448. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  6449. I915_WRITE(aud_config, tmp);
  6450. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6451. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  6452. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6453. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6454. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6455. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6456. } else {
  6457. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6458. }
  6459. if (intel_eld_uptodate(connector,
  6460. aud_cntrl_st2, eldv,
  6461. aud_cntl_st, IBX_ELD_ADDRESS,
  6462. hdmiw_hdmiedid))
  6463. return;
  6464. i = I915_READ(aud_cntrl_st2);
  6465. i &= ~eldv;
  6466. I915_WRITE(aud_cntrl_st2, i);
  6467. if (!eld[0])
  6468. return;
  6469. i = I915_READ(aud_cntl_st);
  6470. i &= ~IBX_ELD_ADDRESS;
  6471. I915_WRITE(aud_cntl_st, i);
  6472. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  6473. DRM_DEBUG_DRIVER("port num:%d\n", i);
  6474. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6475. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6476. for (i = 0; i < len; i++)
  6477. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6478. i = I915_READ(aud_cntrl_st2);
  6479. i |= eldv;
  6480. I915_WRITE(aud_cntrl_st2, i);
  6481. }
  6482. static void ironlake_write_eld(struct drm_connector *connector,
  6483. struct drm_crtc *crtc,
  6484. struct drm_display_mode *mode)
  6485. {
  6486. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  6487. uint8_t *eld = connector->eld;
  6488. uint32_t eldv;
  6489. uint32_t i;
  6490. int len;
  6491. int hdmiw_hdmiedid;
  6492. int aud_config;
  6493. int aud_cntl_st;
  6494. int aud_cntrl_st2;
  6495. int pipe = to_intel_crtc(crtc)->pipe;
  6496. if (HAS_PCH_IBX(connector->dev)) {
  6497. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  6498. aud_config = IBX_AUD_CFG(pipe);
  6499. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  6500. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  6501. } else if (IS_VALLEYVIEW(connector->dev)) {
  6502. hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
  6503. aud_config = VLV_AUD_CFG(pipe);
  6504. aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
  6505. aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
  6506. } else {
  6507. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  6508. aud_config = CPT_AUD_CFG(pipe);
  6509. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  6510. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  6511. }
  6512. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  6513. if (IS_VALLEYVIEW(connector->dev)) {
  6514. struct intel_encoder *intel_encoder;
  6515. struct intel_digital_port *intel_dig_port;
  6516. intel_encoder = intel_attached_encoder(connector);
  6517. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  6518. i = intel_dig_port->port;
  6519. } else {
  6520. i = I915_READ(aud_cntl_st);
  6521. i = (i >> 29) & DIP_PORT_SEL_MASK;
  6522. /* DIP_Port_Select, 0x1 = PortB */
  6523. }
  6524. if (!i) {
  6525. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  6526. /* operate blindly on all ports */
  6527. eldv = IBX_ELD_VALIDB;
  6528. eldv |= IBX_ELD_VALIDB << 4;
  6529. eldv |= IBX_ELD_VALIDB << 8;
  6530. } else {
  6531. DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
  6532. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  6533. }
  6534. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  6535. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  6536. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  6537. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  6538. } else {
  6539. I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
  6540. }
  6541. if (intel_eld_uptodate(connector,
  6542. aud_cntrl_st2, eldv,
  6543. aud_cntl_st, IBX_ELD_ADDRESS,
  6544. hdmiw_hdmiedid))
  6545. return;
  6546. i = I915_READ(aud_cntrl_st2);
  6547. i &= ~eldv;
  6548. I915_WRITE(aud_cntrl_st2, i);
  6549. if (!eld[0])
  6550. return;
  6551. i = I915_READ(aud_cntl_st);
  6552. i &= ~IBX_ELD_ADDRESS;
  6553. I915_WRITE(aud_cntl_st, i);
  6554. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  6555. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  6556. for (i = 0; i < len; i++)
  6557. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  6558. i = I915_READ(aud_cntrl_st2);
  6559. i |= eldv;
  6560. I915_WRITE(aud_cntrl_st2, i);
  6561. }
  6562. void intel_write_eld(struct drm_encoder *encoder,
  6563. struct drm_display_mode *mode)
  6564. {
  6565. struct drm_crtc *crtc = encoder->crtc;
  6566. struct drm_connector *connector;
  6567. struct drm_device *dev = encoder->dev;
  6568. struct drm_i915_private *dev_priv = dev->dev_private;
  6569. connector = drm_select_eld(encoder, mode);
  6570. if (!connector)
  6571. return;
  6572. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6573. connector->base.id,
  6574. connector->name,
  6575. connector->encoder->base.id,
  6576. connector->encoder->name);
  6577. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  6578. if (dev_priv->display.write_eld)
  6579. dev_priv->display.write_eld(connector, crtc, mode);
  6580. }
  6581. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  6582. {
  6583. struct drm_device *dev = crtc->dev;
  6584. struct drm_i915_private *dev_priv = dev->dev_private;
  6585. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6586. uint32_t cntl;
  6587. if (base != intel_crtc->cursor_base) {
  6588. /* On these chipsets we can only modify the base whilst
  6589. * the cursor is disabled.
  6590. */
  6591. if (intel_crtc->cursor_cntl) {
  6592. I915_WRITE(_CURACNTR, 0);
  6593. POSTING_READ(_CURACNTR);
  6594. intel_crtc->cursor_cntl = 0;
  6595. }
  6596. I915_WRITE(_CURABASE, base);
  6597. POSTING_READ(_CURABASE);
  6598. }
  6599. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  6600. cntl = 0;
  6601. if (base)
  6602. cntl = (CURSOR_ENABLE |
  6603. CURSOR_GAMMA_ENABLE |
  6604. CURSOR_FORMAT_ARGB);
  6605. if (intel_crtc->cursor_cntl != cntl) {
  6606. I915_WRITE(_CURACNTR, cntl);
  6607. POSTING_READ(_CURACNTR);
  6608. intel_crtc->cursor_cntl = cntl;
  6609. }
  6610. }
  6611. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  6612. {
  6613. struct drm_device *dev = crtc->dev;
  6614. struct drm_i915_private *dev_priv = dev->dev_private;
  6615. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6616. int pipe = intel_crtc->pipe;
  6617. uint32_t cntl;
  6618. cntl = 0;
  6619. if (base) {
  6620. cntl = MCURSOR_GAMMA_ENABLE;
  6621. switch (intel_crtc->cursor_width) {
  6622. case 64:
  6623. cntl |= CURSOR_MODE_64_ARGB_AX;
  6624. break;
  6625. case 128:
  6626. cntl |= CURSOR_MODE_128_ARGB_AX;
  6627. break;
  6628. case 256:
  6629. cntl |= CURSOR_MODE_256_ARGB_AX;
  6630. break;
  6631. default:
  6632. WARN_ON(1);
  6633. return;
  6634. }
  6635. cntl |= pipe << 28; /* Connect to correct pipe */
  6636. }
  6637. if (intel_crtc->cursor_cntl != cntl) {
  6638. I915_WRITE(CURCNTR(pipe), cntl);
  6639. POSTING_READ(CURCNTR(pipe));
  6640. intel_crtc->cursor_cntl = cntl;
  6641. }
  6642. /* and commit changes on next vblank */
  6643. I915_WRITE(CURBASE(pipe), base);
  6644. POSTING_READ(CURBASE(pipe));
  6645. }
  6646. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  6647. {
  6648. struct drm_device *dev = crtc->dev;
  6649. struct drm_i915_private *dev_priv = dev->dev_private;
  6650. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6651. int pipe = intel_crtc->pipe;
  6652. uint32_t cntl;
  6653. cntl = 0;
  6654. if (base) {
  6655. cntl = MCURSOR_GAMMA_ENABLE;
  6656. switch (intel_crtc->cursor_width) {
  6657. case 64:
  6658. cntl |= CURSOR_MODE_64_ARGB_AX;
  6659. break;
  6660. case 128:
  6661. cntl |= CURSOR_MODE_128_ARGB_AX;
  6662. break;
  6663. case 256:
  6664. cntl |= CURSOR_MODE_256_ARGB_AX;
  6665. break;
  6666. default:
  6667. WARN_ON(1);
  6668. return;
  6669. }
  6670. }
  6671. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  6672. cntl |= CURSOR_PIPE_CSC_ENABLE;
  6673. if (intel_crtc->cursor_cntl != cntl) {
  6674. I915_WRITE(CURCNTR(pipe), cntl);
  6675. POSTING_READ(CURCNTR(pipe));
  6676. intel_crtc->cursor_cntl = cntl;
  6677. }
  6678. /* and commit changes on next vblank */
  6679. I915_WRITE(CURBASE(pipe), base);
  6680. POSTING_READ(CURBASE(pipe));
  6681. }
  6682. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  6683. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  6684. bool on)
  6685. {
  6686. struct drm_device *dev = crtc->dev;
  6687. struct drm_i915_private *dev_priv = dev->dev_private;
  6688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6689. int pipe = intel_crtc->pipe;
  6690. int x = intel_crtc->cursor_x;
  6691. int y = intel_crtc->cursor_y;
  6692. u32 base = 0, pos = 0;
  6693. if (on)
  6694. base = intel_crtc->cursor_addr;
  6695. if (x >= intel_crtc->config.pipe_src_w)
  6696. base = 0;
  6697. if (y >= intel_crtc->config.pipe_src_h)
  6698. base = 0;
  6699. if (x < 0) {
  6700. if (x + intel_crtc->cursor_width <= 0)
  6701. base = 0;
  6702. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  6703. x = -x;
  6704. }
  6705. pos |= x << CURSOR_X_SHIFT;
  6706. if (y < 0) {
  6707. if (y + intel_crtc->cursor_height <= 0)
  6708. base = 0;
  6709. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  6710. y = -y;
  6711. }
  6712. pos |= y << CURSOR_Y_SHIFT;
  6713. if (base == 0 && intel_crtc->cursor_base == 0)
  6714. return;
  6715. I915_WRITE(CURPOS(pipe), pos);
  6716. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
  6717. ivb_update_cursor(crtc, base);
  6718. else if (IS_845G(dev) || IS_I865G(dev))
  6719. i845_update_cursor(crtc, base);
  6720. else
  6721. i9xx_update_cursor(crtc, base);
  6722. intel_crtc->cursor_base = base;
  6723. }
  6724. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  6725. struct drm_file *file,
  6726. uint32_t handle,
  6727. uint32_t width, uint32_t height)
  6728. {
  6729. struct drm_device *dev = crtc->dev;
  6730. struct drm_i915_private *dev_priv = dev->dev_private;
  6731. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6732. struct drm_i915_gem_object *obj;
  6733. unsigned old_width;
  6734. uint32_t addr;
  6735. int ret;
  6736. /* if we want to turn off the cursor ignore width and height */
  6737. if (!handle) {
  6738. DRM_DEBUG_KMS("cursor off\n");
  6739. addr = 0;
  6740. obj = NULL;
  6741. mutex_lock(&dev->struct_mutex);
  6742. goto finish;
  6743. }
  6744. /* Check for which cursor types we support */
  6745. if (!((width == 64 && height == 64) ||
  6746. (width == 128 && height == 128 && !IS_GEN2(dev)) ||
  6747. (width == 256 && height == 256 && !IS_GEN2(dev)))) {
  6748. DRM_DEBUG("Cursor dimension not supported\n");
  6749. return -EINVAL;
  6750. }
  6751. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  6752. if (&obj->base == NULL)
  6753. return -ENOENT;
  6754. if (obj->base.size < width * height * 4) {
  6755. DRM_DEBUG_KMS("buffer is to small\n");
  6756. ret = -ENOMEM;
  6757. goto fail;
  6758. }
  6759. /* we only need to pin inside GTT if cursor is non-phy */
  6760. mutex_lock(&dev->struct_mutex);
  6761. if (!INTEL_INFO(dev)->cursor_needs_physical) {
  6762. unsigned alignment;
  6763. if (obj->tiling_mode) {
  6764. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  6765. ret = -EINVAL;
  6766. goto fail_locked;
  6767. }
  6768. /* Note that the w/a also requires 2 PTE of padding following
  6769. * the bo. We currently fill all unused PTE with the shadow
  6770. * page and so we should always have valid PTE following the
  6771. * cursor preventing the VT-d warning.
  6772. */
  6773. alignment = 0;
  6774. if (need_vtd_wa(dev))
  6775. alignment = 64*1024;
  6776. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  6777. if (ret) {
  6778. DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
  6779. goto fail_locked;
  6780. }
  6781. ret = i915_gem_object_put_fence(obj);
  6782. if (ret) {
  6783. DRM_DEBUG_KMS("failed to release fence for cursor");
  6784. goto fail_unpin;
  6785. }
  6786. addr = i915_gem_obj_ggtt_offset(obj);
  6787. } else {
  6788. int align = IS_I830(dev) ? 16 * 1024 : 256;
  6789. ret = i915_gem_object_attach_phys(obj, align);
  6790. if (ret) {
  6791. DRM_DEBUG_KMS("failed to attach phys object\n");
  6792. goto fail_locked;
  6793. }
  6794. addr = obj->phys_handle->busaddr;
  6795. }
  6796. if (IS_GEN2(dev))
  6797. I915_WRITE(CURSIZE, (height << 12) | width);
  6798. finish:
  6799. if (intel_crtc->cursor_bo) {
  6800. if (!INTEL_INFO(dev)->cursor_needs_physical)
  6801. i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
  6802. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  6803. }
  6804. mutex_unlock(&dev->struct_mutex);
  6805. old_width = intel_crtc->cursor_width;
  6806. intel_crtc->cursor_addr = addr;
  6807. intel_crtc->cursor_bo = obj;
  6808. intel_crtc->cursor_width = width;
  6809. intel_crtc->cursor_height = height;
  6810. if (intel_crtc->active) {
  6811. if (old_width != width)
  6812. intel_update_watermarks(crtc);
  6813. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6814. }
  6815. return 0;
  6816. fail_unpin:
  6817. i915_gem_object_unpin_from_display_plane(obj);
  6818. fail_locked:
  6819. mutex_unlock(&dev->struct_mutex);
  6820. fail:
  6821. drm_gem_object_unreference_unlocked(&obj->base);
  6822. return ret;
  6823. }
  6824. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  6825. {
  6826. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6827. intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
  6828. intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
  6829. if (intel_crtc->active)
  6830. intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
  6831. return 0;
  6832. }
  6833. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  6834. u16 *blue, uint32_t start, uint32_t size)
  6835. {
  6836. int end = (start + size > 256) ? 256 : start + size, i;
  6837. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6838. for (i = start; i < end; i++) {
  6839. intel_crtc->lut_r[i] = red[i] >> 8;
  6840. intel_crtc->lut_g[i] = green[i] >> 8;
  6841. intel_crtc->lut_b[i] = blue[i] >> 8;
  6842. }
  6843. intel_crtc_load_lut(crtc);
  6844. }
  6845. /* VESA 640x480x72Hz mode to set on the pipe */
  6846. static struct drm_display_mode load_detect_mode = {
  6847. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  6848. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  6849. };
  6850. struct drm_framebuffer *
  6851. __intel_framebuffer_create(struct drm_device *dev,
  6852. struct drm_mode_fb_cmd2 *mode_cmd,
  6853. struct drm_i915_gem_object *obj)
  6854. {
  6855. struct intel_framebuffer *intel_fb;
  6856. int ret;
  6857. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6858. if (!intel_fb) {
  6859. drm_gem_object_unreference_unlocked(&obj->base);
  6860. return ERR_PTR(-ENOMEM);
  6861. }
  6862. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  6863. if (ret)
  6864. goto err;
  6865. return &intel_fb->base;
  6866. err:
  6867. drm_gem_object_unreference_unlocked(&obj->base);
  6868. kfree(intel_fb);
  6869. return ERR_PTR(ret);
  6870. }
  6871. static struct drm_framebuffer *
  6872. intel_framebuffer_create(struct drm_device *dev,
  6873. struct drm_mode_fb_cmd2 *mode_cmd,
  6874. struct drm_i915_gem_object *obj)
  6875. {
  6876. struct drm_framebuffer *fb;
  6877. int ret;
  6878. ret = i915_mutex_lock_interruptible(dev);
  6879. if (ret)
  6880. return ERR_PTR(ret);
  6881. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  6882. mutex_unlock(&dev->struct_mutex);
  6883. return fb;
  6884. }
  6885. static u32
  6886. intel_framebuffer_pitch_for_width(int width, int bpp)
  6887. {
  6888. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  6889. return ALIGN(pitch, 64);
  6890. }
  6891. static u32
  6892. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  6893. {
  6894. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  6895. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  6896. }
  6897. static struct drm_framebuffer *
  6898. intel_framebuffer_create_for_mode(struct drm_device *dev,
  6899. struct drm_display_mode *mode,
  6900. int depth, int bpp)
  6901. {
  6902. struct drm_i915_gem_object *obj;
  6903. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  6904. obj = i915_gem_alloc_object(dev,
  6905. intel_framebuffer_size_for_mode(mode, bpp));
  6906. if (obj == NULL)
  6907. return ERR_PTR(-ENOMEM);
  6908. mode_cmd.width = mode->hdisplay;
  6909. mode_cmd.height = mode->vdisplay;
  6910. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  6911. bpp);
  6912. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  6913. return intel_framebuffer_create(dev, &mode_cmd, obj);
  6914. }
  6915. static struct drm_framebuffer *
  6916. mode_fits_in_fbdev(struct drm_device *dev,
  6917. struct drm_display_mode *mode)
  6918. {
  6919. #ifdef CONFIG_DRM_I915_FBDEV
  6920. struct drm_i915_private *dev_priv = dev->dev_private;
  6921. struct drm_i915_gem_object *obj;
  6922. struct drm_framebuffer *fb;
  6923. if (!dev_priv->fbdev)
  6924. return NULL;
  6925. if (!dev_priv->fbdev->fb)
  6926. return NULL;
  6927. obj = dev_priv->fbdev->fb->obj;
  6928. BUG_ON(!obj);
  6929. fb = &dev_priv->fbdev->fb->base;
  6930. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  6931. fb->bits_per_pixel))
  6932. return NULL;
  6933. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  6934. return NULL;
  6935. return fb;
  6936. #else
  6937. return NULL;
  6938. #endif
  6939. }
  6940. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  6941. struct drm_display_mode *mode,
  6942. struct intel_load_detect_pipe *old,
  6943. struct drm_modeset_acquire_ctx *ctx)
  6944. {
  6945. struct intel_crtc *intel_crtc;
  6946. struct intel_encoder *intel_encoder =
  6947. intel_attached_encoder(connector);
  6948. struct drm_crtc *possible_crtc;
  6949. struct drm_encoder *encoder = &intel_encoder->base;
  6950. struct drm_crtc *crtc = NULL;
  6951. struct drm_device *dev = encoder->dev;
  6952. struct drm_framebuffer *fb;
  6953. struct drm_mode_config *config = &dev->mode_config;
  6954. int ret, i = -1;
  6955. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  6956. connector->base.id, connector->name,
  6957. encoder->base.id, encoder->name);
  6958. drm_modeset_acquire_init(ctx, 0);
  6959. retry:
  6960. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  6961. if (ret)
  6962. goto fail_unlock;
  6963. /*
  6964. * Algorithm gets a little messy:
  6965. *
  6966. * - if the connector already has an assigned crtc, use it (but make
  6967. * sure it's on first)
  6968. *
  6969. * - try to find the first unused crtc that can drive this connector,
  6970. * and use that if we find one
  6971. */
  6972. /* See if we already have a CRTC for this connector */
  6973. if (encoder->crtc) {
  6974. crtc = encoder->crtc;
  6975. ret = drm_modeset_lock(&crtc->mutex, ctx);
  6976. if (ret)
  6977. goto fail_unlock;
  6978. old->dpms_mode = connector->dpms;
  6979. old->load_detect_temp = false;
  6980. /* Make sure the crtc and connector are running */
  6981. if (connector->dpms != DRM_MODE_DPMS_ON)
  6982. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  6983. return true;
  6984. }
  6985. /* Find an unused one (if possible) */
  6986. for_each_crtc(dev, possible_crtc) {
  6987. i++;
  6988. if (!(encoder->possible_crtcs & (1 << i)))
  6989. continue;
  6990. if (!possible_crtc->enabled) {
  6991. crtc = possible_crtc;
  6992. break;
  6993. }
  6994. }
  6995. /*
  6996. * If we didn't find an unused CRTC, don't use any.
  6997. */
  6998. if (!crtc) {
  6999. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7000. goto fail_unlock;
  7001. }
  7002. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7003. if (ret)
  7004. goto fail_unlock;
  7005. intel_encoder->new_crtc = to_intel_crtc(crtc);
  7006. to_intel_connector(connector)->new_encoder = intel_encoder;
  7007. intel_crtc = to_intel_crtc(crtc);
  7008. intel_crtc->new_enabled = true;
  7009. intel_crtc->new_config = &intel_crtc->config;
  7010. old->dpms_mode = connector->dpms;
  7011. old->load_detect_temp = true;
  7012. old->release_fb = NULL;
  7013. if (!mode)
  7014. mode = &load_detect_mode;
  7015. /* We need a framebuffer large enough to accommodate all accesses
  7016. * that the plane may generate whilst we perform load detection.
  7017. * We can not rely on the fbcon either being present (we get called
  7018. * during its initialisation to detect all boot displays, or it may
  7019. * not even exist) or that it is large enough to satisfy the
  7020. * requested mode.
  7021. */
  7022. fb = mode_fits_in_fbdev(dev, mode);
  7023. if (fb == NULL) {
  7024. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  7025. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  7026. old->release_fb = fb;
  7027. } else
  7028. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  7029. if (IS_ERR(fb)) {
  7030. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  7031. goto fail;
  7032. }
  7033. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  7034. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  7035. if (old->release_fb)
  7036. old->release_fb->funcs->destroy(old->release_fb);
  7037. goto fail;
  7038. }
  7039. /* let the connector get through one full cycle before testing */
  7040. intel_wait_for_vblank(dev, intel_crtc->pipe);
  7041. return true;
  7042. fail:
  7043. intel_crtc->new_enabled = crtc->enabled;
  7044. if (intel_crtc->new_enabled)
  7045. intel_crtc->new_config = &intel_crtc->config;
  7046. else
  7047. intel_crtc->new_config = NULL;
  7048. fail_unlock:
  7049. if (ret == -EDEADLK) {
  7050. drm_modeset_backoff(ctx);
  7051. goto retry;
  7052. }
  7053. drm_modeset_drop_locks(ctx);
  7054. drm_modeset_acquire_fini(ctx);
  7055. return false;
  7056. }
  7057. void intel_release_load_detect_pipe(struct drm_connector *connector,
  7058. struct intel_load_detect_pipe *old,
  7059. struct drm_modeset_acquire_ctx *ctx)
  7060. {
  7061. struct intel_encoder *intel_encoder =
  7062. intel_attached_encoder(connector);
  7063. struct drm_encoder *encoder = &intel_encoder->base;
  7064. struct drm_crtc *crtc = encoder->crtc;
  7065. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7066. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7067. connector->base.id, connector->name,
  7068. encoder->base.id, encoder->name);
  7069. if (old->load_detect_temp) {
  7070. to_intel_connector(connector)->new_encoder = NULL;
  7071. intel_encoder->new_crtc = NULL;
  7072. intel_crtc->new_enabled = false;
  7073. intel_crtc->new_config = NULL;
  7074. intel_set_mode(crtc, NULL, 0, 0, NULL);
  7075. if (old->release_fb) {
  7076. drm_framebuffer_unregister_private(old->release_fb);
  7077. drm_framebuffer_unreference(old->release_fb);
  7078. }
  7079. goto unlock;
  7080. return;
  7081. }
  7082. /* Switch crtc and encoder back off if necessary */
  7083. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  7084. connector->funcs->dpms(connector, old->dpms_mode);
  7085. unlock:
  7086. drm_modeset_drop_locks(ctx);
  7087. drm_modeset_acquire_fini(ctx);
  7088. }
  7089. static int i9xx_pll_refclk(struct drm_device *dev,
  7090. const struct intel_crtc_config *pipe_config)
  7091. {
  7092. struct drm_i915_private *dev_priv = dev->dev_private;
  7093. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7094. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  7095. return dev_priv->vbt.lvds_ssc_freq;
  7096. else if (HAS_PCH_SPLIT(dev))
  7097. return 120000;
  7098. else if (!IS_GEN2(dev))
  7099. return 96000;
  7100. else
  7101. return 48000;
  7102. }
  7103. /* Returns the clock of the currently programmed mode of the given pipe. */
  7104. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  7105. struct intel_crtc_config *pipe_config)
  7106. {
  7107. struct drm_device *dev = crtc->base.dev;
  7108. struct drm_i915_private *dev_priv = dev->dev_private;
  7109. int pipe = pipe_config->cpu_transcoder;
  7110. u32 dpll = pipe_config->dpll_hw_state.dpll;
  7111. u32 fp;
  7112. intel_clock_t clock;
  7113. int refclk = i9xx_pll_refclk(dev, pipe_config);
  7114. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  7115. fp = pipe_config->dpll_hw_state.fp0;
  7116. else
  7117. fp = pipe_config->dpll_hw_state.fp1;
  7118. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  7119. if (IS_PINEVIEW(dev)) {
  7120. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  7121. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7122. } else {
  7123. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  7124. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  7125. }
  7126. if (!IS_GEN2(dev)) {
  7127. if (IS_PINEVIEW(dev))
  7128. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  7129. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  7130. else
  7131. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  7132. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7133. switch (dpll & DPLL_MODE_MASK) {
  7134. case DPLLB_MODE_DAC_SERIAL:
  7135. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  7136. 5 : 10;
  7137. break;
  7138. case DPLLB_MODE_LVDS:
  7139. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  7140. 7 : 14;
  7141. break;
  7142. default:
  7143. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  7144. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  7145. return;
  7146. }
  7147. if (IS_PINEVIEW(dev))
  7148. pineview_clock(refclk, &clock);
  7149. else
  7150. i9xx_clock(refclk, &clock);
  7151. } else {
  7152. u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
  7153. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  7154. if (is_lvds) {
  7155. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  7156. DPLL_FPA01_P1_POST_DIV_SHIFT);
  7157. if (lvds & LVDS_CLKB_POWER_UP)
  7158. clock.p2 = 7;
  7159. else
  7160. clock.p2 = 14;
  7161. } else {
  7162. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  7163. clock.p1 = 2;
  7164. else {
  7165. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  7166. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  7167. }
  7168. if (dpll & PLL_P2_DIVIDE_BY_4)
  7169. clock.p2 = 4;
  7170. else
  7171. clock.p2 = 2;
  7172. }
  7173. i9xx_clock(refclk, &clock);
  7174. }
  7175. /*
  7176. * This value includes pixel_multiplier. We will use
  7177. * port_clock to compute adjusted_mode.crtc_clock in the
  7178. * encoder's get_config() function.
  7179. */
  7180. pipe_config->port_clock = clock.dot;
  7181. }
  7182. int intel_dotclock_calculate(int link_freq,
  7183. const struct intel_link_m_n *m_n)
  7184. {
  7185. /*
  7186. * The calculation for the data clock is:
  7187. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  7188. * But we want to avoid losing precison if possible, so:
  7189. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  7190. *
  7191. * and the link clock is simpler:
  7192. * link_clock = (m * link_clock) / n
  7193. */
  7194. if (!m_n->link_n)
  7195. return 0;
  7196. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  7197. }
  7198. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  7199. struct intel_crtc_config *pipe_config)
  7200. {
  7201. struct drm_device *dev = crtc->base.dev;
  7202. /* read out port_clock from the DPLL */
  7203. i9xx_crtc_clock_get(crtc, pipe_config);
  7204. /*
  7205. * This value does not include pixel_multiplier.
  7206. * We will check that port_clock and adjusted_mode.crtc_clock
  7207. * agree once we know their relationship in the encoder's
  7208. * get_config() function.
  7209. */
  7210. pipe_config->adjusted_mode.crtc_clock =
  7211. intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
  7212. &pipe_config->fdi_m_n);
  7213. }
  7214. /** Returns the currently programmed mode of the given pipe. */
  7215. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  7216. struct drm_crtc *crtc)
  7217. {
  7218. struct drm_i915_private *dev_priv = dev->dev_private;
  7219. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7220. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  7221. struct drm_display_mode *mode;
  7222. struct intel_crtc_config pipe_config;
  7223. int htot = I915_READ(HTOTAL(cpu_transcoder));
  7224. int hsync = I915_READ(HSYNC(cpu_transcoder));
  7225. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  7226. int vsync = I915_READ(VSYNC(cpu_transcoder));
  7227. enum pipe pipe = intel_crtc->pipe;
  7228. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  7229. if (!mode)
  7230. return NULL;
  7231. /*
  7232. * Construct a pipe_config sufficient for getting the clock info
  7233. * back out of crtc_clock_get.
  7234. *
  7235. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  7236. * to use a real value here instead.
  7237. */
  7238. pipe_config.cpu_transcoder = (enum transcoder) pipe;
  7239. pipe_config.pixel_multiplier = 1;
  7240. pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  7241. pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  7242. pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  7243. i9xx_crtc_clock_get(intel_crtc, &pipe_config);
  7244. mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
  7245. mode->hdisplay = (htot & 0xffff) + 1;
  7246. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  7247. mode->hsync_start = (hsync & 0xffff) + 1;
  7248. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  7249. mode->vdisplay = (vtot & 0xffff) + 1;
  7250. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  7251. mode->vsync_start = (vsync & 0xffff) + 1;
  7252. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  7253. drm_mode_set_name(mode);
  7254. return mode;
  7255. }
  7256. static void intel_increase_pllclock(struct drm_crtc *crtc)
  7257. {
  7258. struct drm_device *dev = crtc->dev;
  7259. struct drm_i915_private *dev_priv = dev->dev_private;
  7260. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7261. int pipe = intel_crtc->pipe;
  7262. int dpll_reg = DPLL(pipe);
  7263. int dpll;
  7264. if (HAS_PCH_SPLIT(dev))
  7265. return;
  7266. if (!dev_priv->lvds_downclock_avail)
  7267. return;
  7268. dpll = I915_READ(dpll_reg);
  7269. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  7270. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  7271. assert_panel_unlocked(dev_priv, pipe);
  7272. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  7273. I915_WRITE(dpll_reg, dpll);
  7274. intel_wait_for_vblank(dev, pipe);
  7275. dpll = I915_READ(dpll_reg);
  7276. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  7277. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  7278. }
  7279. }
  7280. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  7281. {
  7282. struct drm_device *dev = crtc->dev;
  7283. struct drm_i915_private *dev_priv = dev->dev_private;
  7284. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7285. if (HAS_PCH_SPLIT(dev))
  7286. return;
  7287. if (!dev_priv->lvds_downclock_avail)
  7288. return;
  7289. /*
  7290. * Since this is called by a timer, we should never get here in
  7291. * the manual case.
  7292. */
  7293. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  7294. int pipe = intel_crtc->pipe;
  7295. int dpll_reg = DPLL(pipe);
  7296. int dpll;
  7297. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  7298. assert_panel_unlocked(dev_priv, pipe);
  7299. dpll = I915_READ(dpll_reg);
  7300. dpll |= DISPLAY_RATE_SELECT_FPA1;
  7301. I915_WRITE(dpll_reg, dpll);
  7302. intel_wait_for_vblank(dev, pipe);
  7303. dpll = I915_READ(dpll_reg);
  7304. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  7305. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  7306. }
  7307. }
  7308. void intel_mark_busy(struct drm_device *dev)
  7309. {
  7310. struct drm_i915_private *dev_priv = dev->dev_private;
  7311. if (dev_priv->mm.busy)
  7312. return;
  7313. intel_runtime_pm_get(dev_priv);
  7314. i915_update_gfx_val(dev_priv);
  7315. dev_priv->mm.busy = true;
  7316. }
  7317. void intel_mark_idle(struct drm_device *dev)
  7318. {
  7319. struct drm_i915_private *dev_priv = dev->dev_private;
  7320. struct drm_crtc *crtc;
  7321. if (!dev_priv->mm.busy)
  7322. return;
  7323. dev_priv->mm.busy = false;
  7324. if (!i915.powersave)
  7325. goto out;
  7326. for_each_crtc(dev, crtc) {
  7327. if (!crtc->primary->fb)
  7328. continue;
  7329. intel_decrease_pllclock(crtc);
  7330. }
  7331. if (INTEL_INFO(dev)->gen >= 6)
  7332. gen6_rps_idle(dev->dev_private);
  7333. out:
  7334. intel_runtime_pm_put(dev_priv);
  7335. }
  7336. void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
  7337. struct intel_engine_cs *ring)
  7338. {
  7339. struct drm_device *dev = obj->base.dev;
  7340. struct drm_crtc *crtc;
  7341. if (!i915.powersave)
  7342. return;
  7343. for_each_crtc(dev, crtc) {
  7344. if (!crtc->primary->fb)
  7345. continue;
  7346. if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
  7347. continue;
  7348. intel_increase_pllclock(crtc);
  7349. if (ring && intel_fbc_enabled(dev))
  7350. ring->fbc_dirty = true;
  7351. }
  7352. }
  7353. static void intel_crtc_destroy(struct drm_crtc *crtc)
  7354. {
  7355. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7356. struct drm_device *dev = crtc->dev;
  7357. struct intel_unpin_work *work;
  7358. unsigned long flags;
  7359. spin_lock_irqsave(&dev->event_lock, flags);
  7360. work = intel_crtc->unpin_work;
  7361. intel_crtc->unpin_work = NULL;
  7362. spin_unlock_irqrestore(&dev->event_lock, flags);
  7363. if (work) {
  7364. cancel_work_sync(&work->work);
  7365. kfree(work);
  7366. }
  7367. intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
  7368. drm_crtc_cleanup(crtc);
  7369. kfree(intel_crtc);
  7370. }
  7371. static void intel_unpin_work_fn(struct work_struct *__work)
  7372. {
  7373. struct intel_unpin_work *work =
  7374. container_of(__work, struct intel_unpin_work, work);
  7375. struct drm_device *dev = work->crtc->dev;
  7376. mutex_lock(&dev->struct_mutex);
  7377. intel_unpin_fb_obj(work->old_fb_obj);
  7378. drm_gem_object_unreference(&work->pending_flip_obj->base);
  7379. drm_gem_object_unreference(&work->old_fb_obj->base);
  7380. intel_update_fbc(dev);
  7381. mutex_unlock(&dev->struct_mutex);
  7382. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  7383. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  7384. kfree(work);
  7385. }
  7386. static void do_intel_finish_page_flip(struct drm_device *dev,
  7387. struct drm_crtc *crtc)
  7388. {
  7389. struct drm_i915_private *dev_priv = dev->dev_private;
  7390. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7391. struct intel_unpin_work *work;
  7392. unsigned long flags;
  7393. /* Ignore early vblank irqs */
  7394. if (intel_crtc == NULL)
  7395. return;
  7396. spin_lock_irqsave(&dev->event_lock, flags);
  7397. work = intel_crtc->unpin_work;
  7398. /* Ensure we don't miss a work->pending update ... */
  7399. smp_rmb();
  7400. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  7401. spin_unlock_irqrestore(&dev->event_lock, flags);
  7402. return;
  7403. }
  7404. /* and that the unpin work is consistent wrt ->pending. */
  7405. smp_rmb();
  7406. intel_crtc->unpin_work = NULL;
  7407. if (work->event)
  7408. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  7409. drm_crtc_vblank_put(crtc);
  7410. spin_unlock_irqrestore(&dev->event_lock, flags);
  7411. wake_up_all(&dev_priv->pending_flip_queue);
  7412. queue_work(dev_priv->wq, &work->work);
  7413. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  7414. }
  7415. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  7416. {
  7417. struct drm_i915_private *dev_priv = dev->dev_private;
  7418. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  7419. do_intel_finish_page_flip(dev, crtc);
  7420. }
  7421. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  7422. {
  7423. struct drm_i915_private *dev_priv = dev->dev_private;
  7424. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  7425. do_intel_finish_page_flip(dev, crtc);
  7426. }
  7427. /* Is 'a' after or equal to 'b'? */
  7428. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  7429. {
  7430. return !((a - b) & 0x80000000);
  7431. }
  7432. static bool page_flip_finished(struct intel_crtc *crtc)
  7433. {
  7434. struct drm_device *dev = crtc->base.dev;
  7435. struct drm_i915_private *dev_priv = dev->dev_private;
  7436. /*
  7437. * The relevant registers doen't exist on pre-ctg.
  7438. * As the flip done interrupt doesn't trigger for mmio
  7439. * flips on gmch platforms, a flip count check isn't
  7440. * really needed there. But since ctg has the registers,
  7441. * include it in the check anyway.
  7442. */
  7443. if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
  7444. return true;
  7445. /*
  7446. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  7447. * used the same base address. In that case the mmio flip might
  7448. * have completed, but the CS hasn't even executed the flip yet.
  7449. *
  7450. * A flip count check isn't enough as the CS might have updated
  7451. * the base address just after start of vblank, but before we
  7452. * managed to process the interrupt. This means we'd complete the
  7453. * CS flip too soon.
  7454. *
  7455. * Combining both checks should get us a good enough result. It may
  7456. * still happen that the CS flip has been executed, but has not
  7457. * yet actually completed. But in case the base address is the same
  7458. * anyway, we don't really care.
  7459. */
  7460. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  7461. crtc->unpin_work->gtt_offset &&
  7462. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
  7463. crtc->unpin_work->flip_count);
  7464. }
  7465. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  7466. {
  7467. struct drm_i915_private *dev_priv = dev->dev_private;
  7468. struct intel_crtc *intel_crtc =
  7469. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  7470. unsigned long flags;
  7471. /* NB: An MMIO update of the plane base pointer will also
  7472. * generate a page-flip completion irq, i.e. every modeset
  7473. * is also accompanied by a spurious intel_prepare_page_flip().
  7474. */
  7475. spin_lock_irqsave(&dev->event_lock, flags);
  7476. if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
  7477. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  7478. spin_unlock_irqrestore(&dev->event_lock, flags);
  7479. }
  7480. static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  7481. {
  7482. /* Ensure that the work item is consistent when activating it ... */
  7483. smp_wmb();
  7484. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  7485. /* and that it is marked active as soon as the irq could fire. */
  7486. smp_wmb();
  7487. }
  7488. static int intel_gen2_queue_flip(struct drm_device *dev,
  7489. struct drm_crtc *crtc,
  7490. struct drm_framebuffer *fb,
  7491. struct drm_i915_gem_object *obj,
  7492. struct intel_engine_cs *ring,
  7493. uint32_t flags)
  7494. {
  7495. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7496. u32 flip_mask;
  7497. int ret;
  7498. ret = intel_ring_begin(ring, 6);
  7499. if (ret)
  7500. return ret;
  7501. /* Can't queue multiple flips, so wait for the previous
  7502. * one to finish before executing the next.
  7503. */
  7504. if (intel_crtc->plane)
  7505. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7506. else
  7507. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7508. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7509. intel_ring_emit(ring, MI_NOOP);
  7510. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7511. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7512. intel_ring_emit(ring, fb->pitches[0]);
  7513. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7514. intel_ring_emit(ring, 0); /* aux display base address, unused */
  7515. intel_mark_page_flip_active(intel_crtc);
  7516. __intel_ring_advance(ring);
  7517. return 0;
  7518. }
  7519. static int intel_gen3_queue_flip(struct drm_device *dev,
  7520. struct drm_crtc *crtc,
  7521. struct drm_framebuffer *fb,
  7522. struct drm_i915_gem_object *obj,
  7523. struct intel_engine_cs *ring,
  7524. uint32_t flags)
  7525. {
  7526. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7527. u32 flip_mask;
  7528. int ret;
  7529. ret = intel_ring_begin(ring, 6);
  7530. if (ret)
  7531. return ret;
  7532. if (intel_crtc->plane)
  7533. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  7534. else
  7535. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  7536. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  7537. intel_ring_emit(ring, MI_NOOP);
  7538. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  7539. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7540. intel_ring_emit(ring, fb->pitches[0]);
  7541. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7542. intel_ring_emit(ring, MI_NOOP);
  7543. intel_mark_page_flip_active(intel_crtc);
  7544. __intel_ring_advance(ring);
  7545. return 0;
  7546. }
  7547. static int intel_gen4_queue_flip(struct drm_device *dev,
  7548. struct drm_crtc *crtc,
  7549. struct drm_framebuffer *fb,
  7550. struct drm_i915_gem_object *obj,
  7551. struct intel_engine_cs *ring,
  7552. uint32_t flags)
  7553. {
  7554. struct drm_i915_private *dev_priv = dev->dev_private;
  7555. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7556. uint32_t pf, pipesrc;
  7557. int ret;
  7558. ret = intel_ring_begin(ring, 4);
  7559. if (ret)
  7560. return ret;
  7561. /* i965+ uses the linear or tiled offsets from the
  7562. * Display Registers (which do not change across a page-flip)
  7563. * so we need only reprogram the base address.
  7564. */
  7565. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7566. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7567. intel_ring_emit(ring, fb->pitches[0]);
  7568. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
  7569. obj->tiling_mode);
  7570. /* XXX Enabling the panel-fitter across page-flip is so far
  7571. * untested on non-native modes, so ignore it for now.
  7572. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  7573. */
  7574. pf = 0;
  7575. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7576. intel_ring_emit(ring, pf | pipesrc);
  7577. intel_mark_page_flip_active(intel_crtc);
  7578. __intel_ring_advance(ring);
  7579. return 0;
  7580. }
  7581. static int intel_gen6_queue_flip(struct drm_device *dev,
  7582. struct drm_crtc *crtc,
  7583. struct drm_framebuffer *fb,
  7584. struct drm_i915_gem_object *obj,
  7585. struct intel_engine_cs *ring,
  7586. uint32_t flags)
  7587. {
  7588. struct drm_i915_private *dev_priv = dev->dev_private;
  7589. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7590. uint32_t pf, pipesrc;
  7591. int ret;
  7592. ret = intel_ring_begin(ring, 4);
  7593. if (ret)
  7594. return ret;
  7595. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  7596. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  7597. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  7598. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7599. /* Contrary to the suggestions in the documentation,
  7600. * "Enable Panel Fitter" does not seem to be required when page
  7601. * flipping with a non-native mode, and worse causes a normal
  7602. * modeset to fail.
  7603. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  7604. */
  7605. pf = 0;
  7606. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  7607. intel_ring_emit(ring, pf | pipesrc);
  7608. intel_mark_page_flip_active(intel_crtc);
  7609. __intel_ring_advance(ring);
  7610. return 0;
  7611. }
  7612. static int intel_gen7_queue_flip(struct drm_device *dev,
  7613. struct drm_crtc *crtc,
  7614. struct drm_framebuffer *fb,
  7615. struct drm_i915_gem_object *obj,
  7616. struct intel_engine_cs *ring,
  7617. uint32_t flags)
  7618. {
  7619. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7620. uint32_t plane_bit = 0;
  7621. int len, ret;
  7622. switch (intel_crtc->plane) {
  7623. case PLANE_A:
  7624. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  7625. break;
  7626. case PLANE_B:
  7627. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  7628. break;
  7629. case PLANE_C:
  7630. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  7631. break;
  7632. default:
  7633. WARN_ONCE(1, "unknown plane in flip command\n");
  7634. return -ENODEV;
  7635. }
  7636. len = 4;
  7637. if (ring->id == RCS) {
  7638. len += 6;
  7639. /*
  7640. * On Gen 8, SRM is now taking an extra dword to accommodate
  7641. * 48bits addresses, and we need a NOOP for the batch size to
  7642. * stay even.
  7643. */
  7644. if (IS_GEN8(dev))
  7645. len += 2;
  7646. }
  7647. /*
  7648. * BSpec MI_DISPLAY_FLIP for IVB:
  7649. * "The full packet must be contained within the same cache line."
  7650. *
  7651. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  7652. * cacheline, if we ever start emitting more commands before
  7653. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  7654. * then do the cacheline alignment, and finally emit the
  7655. * MI_DISPLAY_FLIP.
  7656. */
  7657. ret = intel_ring_cacheline_align(ring);
  7658. if (ret)
  7659. return ret;
  7660. ret = intel_ring_begin(ring, len);
  7661. if (ret)
  7662. return ret;
  7663. /* Unmask the flip-done completion message. Note that the bspec says that
  7664. * we should do this for both the BCS and RCS, and that we must not unmask
  7665. * more than one flip event at any time (or ensure that one flip message
  7666. * can be sent by waiting for flip-done prior to queueing new flips).
  7667. * Experimentation says that BCS works despite DERRMR masking all
  7668. * flip-done completion events and that unmasking all planes at once
  7669. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  7670. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  7671. */
  7672. if (ring->id == RCS) {
  7673. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  7674. intel_ring_emit(ring, DERRMR);
  7675. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  7676. DERRMR_PIPEB_PRI_FLIP_DONE |
  7677. DERRMR_PIPEC_PRI_FLIP_DONE));
  7678. if (IS_GEN8(dev))
  7679. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
  7680. MI_SRM_LRM_GLOBAL_GTT);
  7681. else
  7682. intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
  7683. MI_SRM_LRM_GLOBAL_GTT);
  7684. intel_ring_emit(ring, DERRMR);
  7685. intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
  7686. if (IS_GEN8(dev)) {
  7687. intel_ring_emit(ring, 0);
  7688. intel_ring_emit(ring, MI_NOOP);
  7689. }
  7690. }
  7691. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  7692. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  7693. intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
  7694. intel_ring_emit(ring, (MI_NOOP));
  7695. intel_mark_page_flip_active(intel_crtc);
  7696. __intel_ring_advance(ring);
  7697. return 0;
  7698. }
  7699. static int intel_default_queue_flip(struct drm_device *dev,
  7700. struct drm_crtc *crtc,
  7701. struct drm_framebuffer *fb,
  7702. struct drm_i915_gem_object *obj,
  7703. struct intel_engine_cs *ring,
  7704. uint32_t flags)
  7705. {
  7706. return -ENODEV;
  7707. }
  7708. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  7709. struct drm_framebuffer *fb,
  7710. struct drm_pending_vblank_event *event,
  7711. uint32_t page_flip_flags)
  7712. {
  7713. struct drm_device *dev = crtc->dev;
  7714. struct drm_i915_private *dev_priv = dev->dev_private;
  7715. struct drm_framebuffer *old_fb = crtc->primary->fb;
  7716. struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
  7717. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7718. struct intel_unpin_work *work;
  7719. struct intel_engine_cs *ring;
  7720. unsigned long flags;
  7721. int ret;
  7722. /* Can't change pixel format via MI display flips. */
  7723. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  7724. return -EINVAL;
  7725. /*
  7726. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  7727. * Note that pitch changes could also affect these register.
  7728. */
  7729. if (INTEL_INFO(dev)->gen > 3 &&
  7730. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  7731. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  7732. return -EINVAL;
  7733. if (i915_terminally_wedged(&dev_priv->gpu_error))
  7734. goto out_hang;
  7735. work = kzalloc(sizeof(*work), GFP_KERNEL);
  7736. if (work == NULL)
  7737. return -ENOMEM;
  7738. work->event = event;
  7739. work->crtc = crtc;
  7740. work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
  7741. INIT_WORK(&work->work, intel_unpin_work_fn);
  7742. ret = drm_crtc_vblank_get(crtc);
  7743. if (ret)
  7744. goto free_work;
  7745. /* We borrow the event spin lock for protecting unpin_work */
  7746. spin_lock_irqsave(&dev->event_lock, flags);
  7747. if (intel_crtc->unpin_work) {
  7748. spin_unlock_irqrestore(&dev->event_lock, flags);
  7749. kfree(work);
  7750. drm_crtc_vblank_put(crtc);
  7751. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  7752. return -EBUSY;
  7753. }
  7754. intel_crtc->unpin_work = work;
  7755. spin_unlock_irqrestore(&dev->event_lock, flags);
  7756. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  7757. flush_workqueue(dev_priv->wq);
  7758. ret = i915_mutex_lock_interruptible(dev);
  7759. if (ret)
  7760. goto cleanup;
  7761. /* Reference the objects for the scheduled work. */
  7762. drm_gem_object_reference(&work->old_fb_obj->base);
  7763. drm_gem_object_reference(&obj->base);
  7764. crtc->primary->fb = fb;
  7765. work->pending_flip_obj = obj;
  7766. work->enable_stall_check = true;
  7767. atomic_inc(&intel_crtc->unpin_work_count);
  7768. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  7769. if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
  7770. work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(intel_crtc->pipe)) + 1;
  7771. if (IS_VALLEYVIEW(dev)) {
  7772. ring = &dev_priv->ring[BCS];
  7773. } else if (INTEL_INFO(dev)->gen >= 7) {
  7774. ring = obj->ring;
  7775. if (ring == NULL || ring->id != RCS)
  7776. ring = &dev_priv->ring[BCS];
  7777. } else {
  7778. ring = &dev_priv->ring[RCS];
  7779. }
  7780. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  7781. if (ret)
  7782. goto cleanup_pending;
  7783. work->gtt_offset =
  7784. i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
  7785. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring, page_flip_flags);
  7786. if (ret)
  7787. goto cleanup_unpin;
  7788. intel_disable_fbc(dev);
  7789. intel_mark_fb_busy(obj, NULL);
  7790. mutex_unlock(&dev->struct_mutex);
  7791. trace_i915_flip_request(intel_crtc->plane, obj);
  7792. return 0;
  7793. cleanup_unpin:
  7794. intel_unpin_fb_obj(obj);
  7795. cleanup_pending:
  7796. atomic_dec(&intel_crtc->unpin_work_count);
  7797. crtc->primary->fb = old_fb;
  7798. drm_gem_object_unreference(&work->old_fb_obj->base);
  7799. drm_gem_object_unreference(&obj->base);
  7800. mutex_unlock(&dev->struct_mutex);
  7801. cleanup:
  7802. spin_lock_irqsave(&dev->event_lock, flags);
  7803. intel_crtc->unpin_work = NULL;
  7804. spin_unlock_irqrestore(&dev->event_lock, flags);
  7805. drm_crtc_vblank_put(crtc);
  7806. free_work:
  7807. kfree(work);
  7808. if (ret == -EIO) {
  7809. out_hang:
  7810. intel_crtc_wait_for_pending_flips(crtc);
  7811. ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
  7812. if (ret == 0 && event)
  7813. drm_send_vblank_event(dev, intel_crtc->pipe, event);
  7814. }
  7815. return ret;
  7816. }
  7817. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  7818. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  7819. .load_lut = intel_crtc_load_lut,
  7820. };
  7821. /**
  7822. * intel_modeset_update_staged_output_state
  7823. *
  7824. * Updates the staged output configuration state, e.g. after we've read out the
  7825. * current hw state.
  7826. */
  7827. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  7828. {
  7829. struct intel_crtc *crtc;
  7830. struct intel_encoder *encoder;
  7831. struct intel_connector *connector;
  7832. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7833. base.head) {
  7834. connector->new_encoder =
  7835. to_intel_encoder(connector->base.encoder);
  7836. }
  7837. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7838. base.head) {
  7839. encoder->new_crtc =
  7840. to_intel_crtc(encoder->base.crtc);
  7841. }
  7842. for_each_intel_crtc(dev, crtc) {
  7843. crtc->new_enabled = crtc->base.enabled;
  7844. if (crtc->new_enabled)
  7845. crtc->new_config = &crtc->config;
  7846. else
  7847. crtc->new_config = NULL;
  7848. }
  7849. }
  7850. /**
  7851. * intel_modeset_commit_output_state
  7852. *
  7853. * This function copies the stage display pipe configuration to the real one.
  7854. */
  7855. static void intel_modeset_commit_output_state(struct drm_device *dev)
  7856. {
  7857. struct intel_crtc *crtc;
  7858. struct intel_encoder *encoder;
  7859. struct intel_connector *connector;
  7860. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7861. base.head) {
  7862. connector->base.encoder = &connector->new_encoder->base;
  7863. }
  7864. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7865. base.head) {
  7866. encoder->base.crtc = &encoder->new_crtc->base;
  7867. }
  7868. for_each_intel_crtc(dev, crtc) {
  7869. crtc->base.enabled = crtc->new_enabled;
  7870. }
  7871. }
  7872. static void
  7873. connected_sink_compute_bpp(struct intel_connector *connector,
  7874. struct intel_crtc_config *pipe_config)
  7875. {
  7876. int bpp = pipe_config->pipe_bpp;
  7877. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  7878. connector->base.base.id,
  7879. connector->base.name);
  7880. /* Don't use an invalid EDID bpc value */
  7881. if (connector->base.display_info.bpc &&
  7882. connector->base.display_info.bpc * 3 < bpp) {
  7883. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  7884. bpp, connector->base.display_info.bpc*3);
  7885. pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
  7886. }
  7887. /* Clamp bpp to 8 on screens without EDID 1.4 */
  7888. if (connector->base.display_info.bpc == 0 && bpp > 24) {
  7889. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  7890. bpp);
  7891. pipe_config->pipe_bpp = 24;
  7892. }
  7893. }
  7894. static int
  7895. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  7896. struct drm_framebuffer *fb,
  7897. struct intel_crtc_config *pipe_config)
  7898. {
  7899. struct drm_device *dev = crtc->base.dev;
  7900. struct intel_connector *connector;
  7901. int bpp;
  7902. switch (fb->pixel_format) {
  7903. case DRM_FORMAT_C8:
  7904. bpp = 8*3; /* since we go through a colormap */
  7905. break;
  7906. case DRM_FORMAT_XRGB1555:
  7907. case DRM_FORMAT_ARGB1555:
  7908. /* checked in intel_framebuffer_init already */
  7909. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  7910. return -EINVAL;
  7911. case DRM_FORMAT_RGB565:
  7912. bpp = 6*3; /* min is 18bpp */
  7913. break;
  7914. case DRM_FORMAT_XBGR8888:
  7915. case DRM_FORMAT_ABGR8888:
  7916. /* checked in intel_framebuffer_init already */
  7917. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7918. return -EINVAL;
  7919. case DRM_FORMAT_XRGB8888:
  7920. case DRM_FORMAT_ARGB8888:
  7921. bpp = 8*3;
  7922. break;
  7923. case DRM_FORMAT_XRGB2101010:
  7924. case DRM_FORMAT_ARGB2101010:
  7925. case DRM_FORMAT_XBGR2101010:
  7926. case DRM_FORMAT_ABGR2101010:
  7927. /* checked in intel_framebuffer_init already */
  7928. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  7929. return -EINVAL;
  7930. bpp = 10*3;
  7931. break;
  7932. /* TODO: gen4+ supports 16 bpc floating point, too. */
  7933. default:
  7934. DRM_DEBUG_KMS("unsupported depth\n");
  7935. return -EINVAL;
  7936. }
  7937. pipe_config->pipe_bpp = bpp;
  7938. /* Clamp display bpp to EDID value */
  7939. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7940. base.head) {
  7941. if (!connector->new_encoder ||
  7942. connector->new_encoder->new_crtc != crtc)
  7943. continue;
  7944. connected_sink_compute_bpp(connector, pipe_config);
  7945. }
  7946. return bpp;
  7947. }
  7948. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  7949. {
  7950. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  7951. "type: 0x%x flags: 0x%x\n",
  7952. mode->crtc_clock,
  7953. mode->crtc_hdisplay, mode->crtc_hsync_start,
  7954. mode->crtc_hsync_end, mode->crtc_htotal,
  7955. mode->crtc_vdisplay, mode->crtc_vsync_start,
  7956. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  7957. }
  7958. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  7959. struct intel_crtc_config *pipe_config,
  7960. const char *context)
  7961. {
  7962. DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
  7963. context, pipe_name(crtc->pipe));
  7964. DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
  7965. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  7966. pipe_config->pipe_bpp, pipe_config->dither);
  7967. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7968. pipe_config->has_pch_encoder,
  7969. pipe_config->fdi_lanes,
  7970. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  7971. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  7972. pipe_config->fdi_m_n.tu);
  7973. DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  7974. pipe_config->has_dp_encoder,
  7975. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  7976. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  7977. pipe_config->dp_m_n.tu);
  7978. DRM_DEBUG_KMS("requested mode:\n");
  7979. drm_mode_debug_printmodeline(&pipe_config->requested_mode);
  7980. DRM_DEBUG_KMS("adjusted mode:\n");
  7981. drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
  7982. intel_dump_crtc_timings(&pipe_config->adjusted_mode);
  7983. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  7984. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  7985. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  7986. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  7987. pipe_config->gmch_pfit.control,
  7988. pipe_config->gmch_pfit.pgm_ratios,
  7989. pipe_config->gmch_pfit.lvds_border_bits);
  7990. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  7991. pipe_config->pch_pfit.pos,
  7992. pipe_config->pch_pfit.size,
  7993. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  7994. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  7995. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  7996. }
  7997. static bool encoders_cloneable(const struct intel_encoder *a,
  7998. const struct intel_encoder *b)
  7999. {
  8000. /* masks could be asymmetric, so check both ways */
  8001. return a == b || (a->cloneable & (1 << b->type) &&
  8002. b->cloneable & (1 << a->type));
  8003. }
  8004. static bool check_single_encoder_cloning(struct intel_crtc *crtc,
  8005. struct intel_encoder *encoder)
  8006. {
  8007. struct drm_device *dev = crtc->base.dev;
  8008. struct intel_encoder *source_encoder;
  8009. list_for_each_entry(source_encoder,
  8010. &dev->mode_config.encoder_list, base.head) {
  8011. if (source_encoder->new_crtc != crtc)
  8012. continue;
  8013. if (!encoders_cloneable(encoder, source_encoder))
  8014. return false;
  8015. }
  8016. return true;
  8017. }
  8018. static bool check_encoder_cloning(struct intel_crtc *crtc)
  8019. {
  8020. struct drm_device *dev = crtc->base.dev;
  8021. struct intel_encoder *encoder;
  8022. list_for_each_entry(encoder,
  8023. &dev->mode_config.encoder_list, base.head) {
  8024. if (encoder->new_crtc != crtc)
  8025. continue;
  8026. if (!check_single_encoder_cloning(crtc, encoder))
  8027. return false;
  8028. }
  8029. return true;
  8030. }
  8031. static struct intel_crtc_config *
  8032. intel_modeset_pipe_config(struct drm_crtc *crtc,
  8033. struct drm_framebuffer *fb,
  8034. struct drm_display_mode *mode)
  8035. {
  8036. struct drm_device *dev = crtc->dev;
  8037. struct intel_encoder *encoder;
  8038. struct intel_crtc_config *pipe_config;
  8039. int plane_bpp, ret = -EINVAL;
  8040. bool retry = true;
  8041. if (!check_encoder_cloning(to_intel_crtc(crtc))) {
  8042. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  8043. return ERR_PTR(-EINVAL);
  8044. }
  8045. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8046. if (!pipe_config)
  8047. return ERR_PTR(-ENOMEM);
  8048. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  8049. drm_mode_copy(&pipe_config->requested_mode, mode);
  8050. pipe_config->cpu_transcoder =
  8051. (enum transcoder) to_intel_crtc(crtc)->pipe;
  8052. pipe_config->shared_dpll = DPLL_ID_PRIVATE;
  8053. /*
  8054. * Sanitize sync polarity flags based on requested ones. If neither
  8055. * positive or negative polarity is requested, treat this as meaning
  8056. * negative polarity.
  8057. */
  8058. if (!(pipe_config->adjusted_mode.flags &
  8059. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  8060. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  8061. if (!(pipe_config->adjusted_mode.flags &
  8062. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  8063. pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  8064. /* Compute a starting value for pipe_config->pipe_bpp taking the source
  8065. * plane pixel format and any sink constraints into account. Returns the
  8066. * source plane bpp so that dithering can be selected on mismatches
  8067. * after encoders and crtc also have had their say. */
  8068. plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  8069. fb, pipe_config);
  8070. if (plane_bpp < 0)
  8071. goto fail;
  8072. /*
  8073. * Determine the real pipe dimensions. Note that stereo modes can
  8074. * increase the actual pipe size due to the frame doubling and
  8075. * insertion of additional space for blanks between the frame. This
  8076. * is stored in the crtc timings. We use the requested mode to do this
  8077. * computation to clearly distinguish it from the adjusted mode, which
  8078. * can be changed by the connectors in the below retry loop.
  8079. */
  8080. drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
  8081. pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
  8082. pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
  8083. encoder_retry:
  8084. /* Ensure the port clock defaults are reset when retrying. */
  8085. pipe_config->port_clock = 0;
  8086. pipe_config->pixel_multiplier = 1;
  8087. /* Fill in default crtc timings, allow encoders to overwrite them. */
  8088. drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
  8089. /* Pass our mode to the connectors and the CRTC to give them a chance to
  8090. * adjust it according to limitations or connector properties, and also
  8091. * a chance to reject the mode entirely.
  8092. */
  8093. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8094. base.head) {
  8095. if (&encoder->new_crtc->base != crtc)
  8096. continue;
  8097. if (!(encoder->compute_config(encoder, pipe_config))) {
  8098. DRM_DEBUG_KMS("Encoder config failure\n");
  8099. goto fail;
  8100. }
  8101. }
  8102. /* Set default port clock if not overwritten by the encoder. Needs to be
  8103. * done afterwards in case the encoder adjusts the mode. */
  8104. if (!pipe_config->port_clock)
  8105. pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
  8106. * pipe_config->pixel_multiplier;
  8107. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  8108. if (ret < 0) {
  8109. DRM_DEBUG_KMS("CRTC fixup failed\n");
  8110. goto fail;
  8111. }
  8112. if (ret == RETRY) {
  8113. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  8114. ret = -EINVAL;
  8115. goto fail;
  8116. }
  8117. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  8118. retry = false;
  8119. goto encoder_retry;
  8120. }
  8121. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  8122. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  8123. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  8124. return pipe_config;
  8125. fail:
  8126. kfree(pipe_config);
  8127. return ERR_PTR(ret);
  8128. }
  8129. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  8130. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  8131. static void
  8132. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  8133. unsigned *prepare_pipes, unsigned *disable_pipes)
  8134. {
  8135. struct intel_crtc *intel_crtc;
  8136. struct drm_device *dev = crtc->dev;
  8137. struct intel_encoder *encoder;
  8138. struct intel_connector *connector;
  8139. struct drm_crtc *tmp_crtc;
  8140. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  8141. /* Check which crtcs have changed outputs connected to them, these need
  8142. * to be part of the prepare_pipes mask. We don't (yet) support global
  8143. * modeset across multiple crtcs, so modeset_pipes will only have one
  8144. * bit set at most. */
  8145. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8146. base.head) {
  8147. if (connector->base.encoder == &connector->new_encoder->base)
  8148. continue;
  8149. if (connector->base.encoder) {
  8150. tmp_crtc = connector->base.encoder->crtc;
  8151. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8152. }
  8153. if (connector->new_encoder)
  8154. *prepare_pipes |=
  8155. 1 << connector->new_encoder->new_crtc->pipe;
  8156. }
  8157. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8158. base.head) {
  8159. if (encoder->base.crtc == &encoder->new_crtc->base)
  8160. continue;
  8161. if (encoder->base.crtc) {
  8162. tmp_crtc = encoder->base.crtc;
  8163. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  8164. }
  8165. if (encoder->new_crtc)
  8166. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  8167. }
  8168. /* Check for pipes that will be enabled/disabled ... */
  8169. for_each_intel_crtc(dev, intel_crtc) {
  8170. if (intel_crtc->base.enabled == intel_crtc->new_enabled)
  8171. continue;
  8172. if (!intel_crtc->new_enabled)
  8173. *disable_pipes |= 1 << intel_crtc->pipe;
  8174. else
  8175. *prepare_pipes |= 1 << intel_crtc->pipe;
  8176. }
  8177. /* set_mode is also used to update properties on life display pipes. */
  8178. intel_crtc = to_intel_crtc(crtc);
  8179. if (intel_crtc->new_enabled)
  8180. *prepare_pipes |= 1 << intel_crtc->pipe;
  8181. /*
  8182. * For simplicity do a full modeset on any pipe where the output routing
  8183. * changed. We could be more clever, but that would require us to be
  8184. * more careful with calling the relevant encoder->mode_set functions.
  8185. */
  8186. if (*prepare_pipes)
  8187. *modeset_pipes = *prepare_pipes;
  8188. /* ... and mask these out. */
  8189. *modeset_pipes &= ~(*disable_pipes);
  8190. *prepare_pipes &= ~(*disable_pipes);
  8191. /*
  8192. * HACK: We don't (yet) fully support global modesets. intel_set_config
  8193. * obies this rule, but the modeset restore mode of
  8194. * intel_modeset_setup_hw_state does not.
  8195. */
  8196. *modeset_pipes &= 1 << intel_crtc->pipe;
  8197. *prepare_pipes &= 1 << intel_crtc->pipe;
  8198. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  8199. *modeset_pipes, *prepare_pipes, *disable_pipes);
  8200. }
  8201. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  8202. {
  8203. struct drm_encoder *encoder;
  8204. struct drm_device *dev = crtc->dev;
  8205. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  8206. if (encoder->crtc == crtc)
  8207. return true;
  8208. return false;
  8209. }
  8210. static void
  8211. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  8212. {
  8213. struct intel_encoder *intel_encoder;
  8214. struct intel_crtc *intel_crtc;
  8215. struct drm_connector *connector;
  8216. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  8217. base.head) {
  8218. if (!intel_encoder->base.crtc)
  8219. continue;
  8220. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  8221. if (prepare_pipes & (1 << intel_crtc->pipe))
  8222. intel_encoder->connectors_active = false;
  8223. }
  8224. intel_modeset_commit_output_state(dev);
  8225. /* Double check state. */
  8226. for_each_intel_crtc(dev, intel_crtc) {
  8227. WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
  8228. WARN_ON(intel_crtc->new_config &&
  8229. intel_crtc->new_config != &intel_crtc->config);
  8230. WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
  8231. }
  8232. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8233. if (!connector->encoder || !connector->encoder->crtc)
  8234. continue;
  8235. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  8236. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  8237. struct drm_property *dpms_property =
  8238. dev->mode_config.dpms_property;
  8239. connector->dpms = DRM_MODE_DPMS_ON;
  8240. drm_object_property_set_value(&connector->base,
  8241. dpms_property,
  8242. DRM_MODE_DPMS_ON);
  8243. intel_encoder = to_intel_encoder(connector->encoder);
  8244. intel_encoder->connectors_active = true;
  8245. }
  8246. }
  8247. }
  8248. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  8249. {
  8250. int diff;
  8251. if (clock1 == clock2)
  8252. return true;
  8253. if (!clock1 || !clock2)
  8254. return false;
  8255. diff = abs(clock1 - clock2);
  8256. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  8257. return true;
  8258. return false;
  8259. }
  8260. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  8261. list_for_each_entry((intel_crtc), \
  8262. &(dev)->mode_config.crtc_list, \
  8263. base.head) \
  8264. if (mask & (1 <<(intel_crtc)->pipe))
  8265. static bool
  8266. intel_pipe_config_compare(struct drm_device *dev,
  8267. struct intel_crtc_config *current_config,
  8268. struct intel_crtc_config *pipe_config)
  8269. {
  8270. #define PIPE_CONF_CHECK_X(name) \
  8271. if (current_config->name != pipe_config->name) { \
  8272. DRM_ERROR("mismatch in " #name " " \
  8273. "(expected 0x%08x, found 0x%08x)\n", \
  8274. current_config->name, \
  8275. pipe_config->name); \
  8276. return false; \
  8277. }
  8278. #define PIPE_CONF_CHECK_I(name) \
  8279. if (current_config->name != pipe_config->name) { \
  8280. DRM_ERROR("mismatch in " #name " " \
  8281. "(expected %i, found %i)\n", \
  8282. current_config->name, \
  8283. pipe_config->name); \
  8284. return false; \
  8285. }
  8286. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  8287. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  8288. DRM_ERROR("mismatch in " #name "(" #mask ") " \
  8289. "(expected %i, found %i)\n", \
  8290. current_config->name & (mask), \
  8291. pipe_config->name & (mask)); \
  8292. return false; \
  8293. }
  8294. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  8295. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  8296. DRM_ERROR("mismatch in " #name " " \
  8297. "(expected %i, found %i)\n", \
  8298. current_config->name, \
  8299. pipe_config->name); \
  8300. return false; \
  8301. }
  8302. #define PIPE_CONF_QUIRK(quirk) \
  8303. ((current_config->quirks | pipe_config->quirks) & (quirk))
  8304. PIPE_CONF_CHECK_I(cpu_transcoder);
  8305. PIPE_CONF_CHECK_I(has_pch_encoder);
  8306. PIPE_CONF_CHECK_I(fdi_lanes);
  8307. PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
  8308. PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
  8309. PIPE_CONF_CHECK_I(fdi_m_n.link_m);
  8310. PIPE_CONF_CHECK_I(fdi_m_n.link_n);
  8311. PIPE_CONF_CHECK_I(fdi_m_n.tu);
  8312. PIPE_CONF_CHECK_I(has_dp_encoder);
  8313. PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
  8314. PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
  8315. PIPE_CONF_CHECK_I(dp_m_n.link_m);
  8316. PIPE_CONF_CHECK_I(dp_m_n.link_n);
  8317. PIPE_CONF_CHECK_I(dp_m_n.tu);
  8318. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
  8319. PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
  8320. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
  8321. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
  8322. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
  8323. PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
  8324. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
  8325. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
  8326. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
  8327. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
  8328. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
  8329. PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
  8330. PIPE_CONF_CHECK_I(pixel_multiplier);
  8331. PIPE_CONF_CHECK_I(has_hdmi_sink);
  8332. if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
  8333. IS_VALLEYVIEW(dev))
  8334. PIPE_CONF_CHECK_I(limited_color_range);
  8335. PIPE_CONF_CHECK_I(has_audio);
  8336. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8337. DRM_MODE_FLAG_INTERLACE);
  8338. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  8339. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8340. DRM_MODE_FLAG_PHSYNC);
  8341. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8342. DRM_MODE_FLAG_NHSYNC);
  8343. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8344. DRM_MODE_FLAG_PVSYNC);
  8345. PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
  8346. DRM_MODE_FLAG_NVSYNC);
  8347. }
  8348. PIPE_CONF_CHECK_I(pipe_src_w);
  8349. PIPE_CONF_CHECK_I(pipe_src_h);
  8350. /*
  8351. * FIXME: BIOS likes to set up a cloned config with lvds+external
  8352. * screen. Since we don't yet re-compute the pipe config when moving
  8353. * just the lvds port away to another pipe the sw tracking won't match.
  8354. *
  8355. * Proper atomic modesets with recomputed global state will fix this.
  8356. * Until then just don't check gmch state for inherited modes.
  8357. */
  8358. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
  8359. PIPE_CONF_CHECK_I(gmch_pfit.control);
  8360. /* pfit ratios are autocomputed by the hw on gen4+ */
  8361. if (INTEL_INFO(dev)->gen < 4)
  8362. PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
  8363. PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
  8364. }
  8365. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  8366. if (current_config->pch_pfit.enabled) {
  8367. PIPE_CONF_CHECK_I(pch_pfit.pos);
  8368. PIPE_CONF_CHECK_I(pch_pfit.size);
  8369. }
  8370. /* BDW+ don't expose a synchronous way to read the state */
  8371. if (IS_HASWELL(dev))
  8372. PIPE_CONF_CHECK_I(ips_enabled);
  8373. PIPE_CONF_CHECK_I(double_wide);
  8374. PIPE_CONF_CHECK_I(shared_dpll);
  8375. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  8376. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  8377. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  8378. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  8379. if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
  8380. PIPE_CONF_CHECK_I(pipe_bpp);
  8381. PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
  8382. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  8383. #undef PIPE_CONF_CHECK_X
  8384. #undef PIPE_CONF_CHECK_I
  8385. #undef PIPE_CONF_CHECK_FLAGS
  8386. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  8387. #undef PIPE_CONF_QUIRK
  8388. return true;
  8389. }
  8390. static void
  8391. check_connector_state(struct drm_device *dev)
  8392. {
  8393. struct intel_connector *connector;
  8394. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8395. base.head) {
  8396. /* This also checks the encoder/connector hw state with the
  8397. * ->get_hw_state callbacks. */
  8398. intel_connector_check_state(connector);
  8399. WARN(&connector->new_encoder->base != connector->base.encoder,
  8400. "connector's staged encoder doesn't match current encoder\n");
  8401. }
  8402. }
  8403. static void
  8404. check_encoder_state(struct drm_device *dev)
  8405. {
  8406. struct intel_encoder *encoder;
  8407. struct intel_connector *connector;
  8408. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8409. base.head) {
  8410. bool enabled = false;
  8411. bool active = false;
  8412. enum pipe pipe, tracked_pipe;
  8413. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  8414. encoder->base.base.id,
  8415. encoder->base.name);
  8416. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  8417. "encoder's stage crtc doesn't match current crtc\n");
  8418. WARN(encoder->connectors_active && !encoder->base.crtc,
  8419. "encoder's active_connectors set, but no crtc\n");
  8420. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8421. base.head) {
  8422. if (connector->base.encoder != &encoder->base)
  8423. continue;
  8424. enabled = true;
  8425. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  8426. active = true;
  8427. }
  8428. WARN(!!encoder->base.crtc != enabled,
  8429. "encoder's enabled state mismatch "
  8430. "(expected %i, found %i)\n",
  8431. !!encoder->base.crtc, enabled);
  8432. WARN(active && !encoder->base.crtc,
  8433. "active encoder with no crtc\n");
  8434. WARN(encoder->connectors_active != active,
  8435. "encoder's computed active state doesn't match tracked active state "
  8436. "(expected %i, found %i)\n", active, encoder->connectors_active);
  8437. active = encoder->get_hw_state(encoder, &pipe);
  8438. WARN(active != encoder->connectors_active,
  8439. "encoder's hw state doesn't match sw tracking "
  8440. "(expected %i, found %i)\n",
  8441. encoder->connectors_active, active);
  8442. if (!encoder->base.crtc)
  8443. continue;
  8444. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  8445. WARN(active && pipe != tracked_pipe,
  8446. "active encoder's pipe doesn't match"
  8447. "(expected %i, found %i)\n",
  8448. tracked_pipe, pipe);
  8449. }
  8450. }
  8451. static void
  8452. check_crtc_state(struct drm_device *dev)
  8453. {
  8454. struct drm_i915_private *dev_priv = dev->dev_private;
  8455. struct intel_crtc *crtc;
  8456. struct intel_encoder *encoder;
  8457. struct intel_crtc_config pipe_config;
  8458. for_each_intel_crtc(dev, crtc) {
  8459. bool enabled = false;
  8460. bool active = false;
  8461. memset(&pipe_config, 0, sizeof(pipe_config));
  8462. DRM_DEBUG_KMS("[CRTC:%d]\n",
  8463. crtc->base.base.id);
  8464. WARN(crtc->active && !crtc->base.enabled,
  8465. "active crtc, but not enabled in sw tracking\n");
  8466. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8467. base.head) {
  8468. if (encoder->base.crtc != &crtc->base)
  8469. continue;
  8470. enabled = true;
  8471. if (encoder->connectors_active)
  8472. active = true;
  8473. }
  8474. WARN(active != crtc->active,
  8475. "crtc's computed active state doesn't match tracked active state "
  8476. "(expected %i, found %i)\n", active, crtc->active);
  8477. WARN(enabled != crtc->base.enabled,
  8478. "crtc's computed enabled state doesn't match tracked enabled state "
  8479. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  8480. active = dev_priv->display.get_pipe_config(crtc,
  8481. &pipe_config);
  8482. /* hw state is inconsistent with the pipe A quirk */
  8483. if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
  8484. active = crtc->active;
  8485. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8486. base.head) {
  8487. enum pipe pipe;
  8488. if (encoder->base.crtc != &crtc->base)
  8489. continue;
  8490. if (encoder->get_hw_state(encoder, &pipe))
  8491. encoder->get_config(encoder, &pipe_config);
  8492. }
  8493. WARN(crtc->active != active,
  8494. "crtc active state doesn't match with hw state "
  8495. "(expected %i, found %i)\n", crtc->active, active);
  8496. if (active &&
  8497. !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
  8498. WARN(1, "pipe state doesn't match!\n");
  8499. intel_dump_pipe_config(crtc, &pipe_config,
  8500. "[hw state]");
  8501. intel_dump_pipe_config(crtc, &crtc->config,
  8502. "[sw state]");
  8503. }
  8504. }
  8505. }
  8506. static void
  8507. check_shared_dpll_state(struct drm_device *dev)
  8508. {
  8509. struct drm_i915_private *dev_priv = dev->dev_private;
  8510. struct intel_crtc *crtc;
  8511. struct intel_dpll_hw_state dpll_hw_state;
  8512. int i;
  8513. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  8514. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  8515. int enabled_crtcs = 0, active_crtcs = 0;
  8516. bool active;
  8517. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  8518. DRM_DEBUG_KMS("%s\n", pll->name);
  8519. active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
  8520. WARN(pll->active > pll->refcount,
  8521. "more active pll users than references: %i vs %i\n",
  8522. pll->active, pll->refcount);
  8523. WARN(pll->active && !pll->on,
  8524. "pll in active use but not on in sw tracking\n");
  8525. WARN(pll->on && !pll->active,
  8526. "pll in on but not on in use in sw tracking\n");
  8527. WARN(pll->on != active,
  8528. "pll on state mismatch (expected %i, found %i)\n",
  8529. pll->on, active);
  8530. for_each_intel_crtc(dev, crtc) {
  8531. if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
  8532. enabled_crtcs++;
  8533. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  8534. active_crtcs++;
  8535. }
  8536. WARN(pll->active != active_crtcs,
  8537. "pll active crtcs mismatch (expected %i, found %i)\n",
  8538. pll->active, active_crtcs);
  8539. WARN(pll->refcount != enabled_crtcs,
  8540. "pll enabled crtcs mismatch (expected %i, found %i)\n",
  8541. pll->refcount, enabled_crtcs);
  8542. WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
  8543. sizeof(dpll_hw_state)),
  8544. "pll hw state mismatch\n");
  8545. }
  8546. }
  8547. void
  8548. intel_modeset_check_state(struct drm_device *dev)
  8549. {
  8550. check_connector_state(dev);
  8551. check_encoder_state(dev);
  8552. check_crtc_state(dev);
  8553. check_shared_dpll_state(dev);
  8554. }
  8555. void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
  8556. int dotclock)
  8557. {
  8558. /*
  8559. * FDI already provided one idea for the dotclock.
  8560. * Yell if the encoder disagrees.
  8561. */
  8562. WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
  8563. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  8564. pipe_config->adjusted_mode.crtc_clock, dotclock);
  8565. }
  8566. static void update_scanline_offset(struct intel_crtc *crtc)
  8567. {
  8568. struct drm_device *dev = crtc->base.dev;
  8569. /*
  8570. * The scanline counter increments at the leading edge of hsync.
  8571. *
  8572. * On most platforms it starts counting from vtotal-1 on the
  8573. * first active line. That means the scanline counter value is
  8574. * always one less than what we would expect. Ie. just after
  8575. * start of vblank, which also occurs at start of hsync (on the
  8576. * last active line), the scanline counter will read vblank_start-1.
  8577. *
  8578. * On gen2 the scanline counter starts counting from 1 instead
  8579. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  8580. * to keep the value positive), instead of adding one.
  8581. *
  8582. * On HSW+ the behaviour of the scanline counter depends on the output
  8583. * type. For DP ports it behaves like most other platforms, but on HDMI
  8584. * there's an extra 1 line difference. So we need to add two instead of
  8585. * one to the value.
  8586. */
  8587. if (IS_GEN2(dev)) {
  8588. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  8589. int vtotal;
  8590. vtotal = mode->crtc_vtotal;
  8591. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  8592. vtotal /= 2;
  8593. crtc->scanline_offset = vtotal - 1;
  8594. } else if (HAS_DDI(dev) &&
  8595. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
  8596. crtc->scanline_offset = 2;
  8597. } else
  8598. crtc->scanline_offset = 1;
  8599. }
  8600. static int __intel_set_mode(struct drm_crtc *crtc,
  8601. struct drm_display_mode *mode,
  8602. int x, int y, struct drm_framebuffer *fb)
  8603. {
  8604. struct drm_device *dev = crtc->dev;
  8605. struct drm_i915_private *dev_priv = dev->dev_private;
  8606. struct drm_display_mode *saved_mode;
  8607. struct intel_crtc_config *pipe_config = NULL;
  8608. struct intel_crtc *intel_crtc;
  8609. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  8610. int ret = 0;
  8611. saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
  8612. if (!saved_mode)
  8613. return -ENOMEM;
  8614. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  8615. &prepare_pipes, &disable_pipes);
  8616. *saved_mode = crtc->mode;
  8617. /* Hack: Because we don't (yet) support global modeset on multiple
  8618. * crtcs, we don't keep track of the new mode for more than one crtc.
  8619. * Hence simply check whether any bit is set in modeset_pipes in all the
  8620. * pieces of code that are not yet converted to deal with mutliple crtcs
  8621. * changing their mode at the same time. */
  8622. if (modeset_pipes) {
  8623. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  8624. if (IS_ERR(pipe_config)) {
  8625. ret = PTR_ERR(pipe_config);
  8626. pipe_config = NULL;
  8627. goto out;
  8628. }
  8629. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  8630. "[modeset]");
  8631. to_intel_crtc(crtc)->new_config = pipe_config;
  8632. }
  8633. /*
  8634. * See if the config requires any additional preparation, e.g.
  8635. * to adjust global state with pipes off. We need to do this
  8636. * here so we can get the modeset_pipe updated config for the new
  8637. * mode set on this crtc. For other crtcs we need to use the
  8638. * adjusted_mode bits in the crtc directly.
  8639. */
  8640. if (IS_VALLEYVIEW(dev)) {
  8641. valleyview_modeset_global_pipes(dev, &prepare_pipes);
  8642. /* may have added more to prepare_pipes than we should */
  8643. prepare_pipes &= ~disable_pipes;
  8644. }
  8645. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  8646. intel_crtc_disable(&intel_crtc->base);
  8647. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  8648. if (intel_crtc->base.enabled)
  8649. dev_priv->display.crtc_disable(&intel_crtc->base);
  8650. }
  8651. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  8652. * to set it here already despite that we pass it down the callchain.
  8653. */
  8654. if (modeset_pipes) {
  8655. crtc->mode = *mode;
  8656. /* mode_set/enable/disable functions rely on a correct pipe
  8657. * config. */
  8658. to_intel_crtc(crtc)->config = *pipe_config;
  8659. to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
  8660. /*
  8661. * Calculate and store various constants which
  8662. * are later needed by vblank and swap-completion
  8663. * timestamping. They are derived from true hwmode.
  8664. */
  8665. drm_calc_timestamping_constants(crtc,
  8666. &pipe_config->adjusted_mode);
  8667. }
  8668. /* Only after disabling all output pipelines that will be changed can we
  8669. * update the the output configuration. */
  8670. intel_modeset_update_state(dev, prepare_pipes);
  8671. if (dev_priv->display.modeset_global_resources)
  8672. dev_priv->display.modeset_global_resources(dev);
  8673. /* Set up the DPLL and any encoders state that needs to adjust or depend
  8674. * on the DPLL.
  8675. */
  8676. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  8677. struct drm_framebuffer *old_fb;
  8678. mutex_lock(&dev->struct_mutex);
  8679. ret = intel_pin_and_fence_fb_obj(dev,
  8680. to_intel_framebuffer(fb)->obj,
  8681. NULL);
  8682. if (ret != 0) {
  8683. DRM_ERROR("pin & fence failed\n");
  8684. mutex_unlock(&dev->struct_mutex);
  8685. goto done;
  8686. }
  8687. old_fb = crtc->primary->fb;
  8688. if (old_fb)
  8689. intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
  8690. mutex_unlock(&dev->struct_mutex);
  8691. crtc->primary->fb = fb;
  8692. crtc->x = x;
  8693. crtc->y = y;
  8694. ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
  8695. x, y, fb);
  8696. if (ret)
  8697. goto done;
  8698. }
  8699. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  8700. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  8701. update_scanline_offset(intel_crtc);
  8702. dev_priv->display.crtc_enable(&intel_crtc->base);
  8703. }
  8704. /* FIXME: add subpixel order */
  8705. done:
  8706. if (ret && crtc->enabled)
  8707. crtc->mode = *saved_mode;
  8708. out:
  8709. kfree(pipe_config);
  8710. kfree(saved_mode);
  8711. return ret;
  8712. }
  8713. static int intel_set_mode(struct drm_crtc *crtc,
  8714. struct drm_display_mode *mode,
  8715. int x, int y, struct drm_framebuffer *fb)
  8716. {
  8717. int ret;
  8718. ret = __intel_set_mode(crtc, mode, x, y, fb);
  8719. if (ret == 0)
  8720. intel_modeset_check_state(crtc->dev);
  8721. return ret;
  8722. }
  8723. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  8724. {
  8725. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
  8726. }
  8727. #undef for_each_intel_crtc_masked
  8728. static void intel_set_config_free(struct intel_set_config *config)
  8729. {
  8730. if (!config)
  8731. return;
  8732. kfree(config->save_connector_encoders);
  8733. kfree(config->save_encoder_crtcs);
  8734. kfree(config->save_crtc_enabled);
  8735. kfree(config);
  8736. }
  8737. static int intel_set_config_save_state(struct drm_device *dev,
  8738. struct intel_set_config *config)
  8739. {
  8740. struct drm_crtc *crtc;
  8741. struct drm_encoder *encoder;
  8742. struct drm_connector *connector;
  8743. int count;
  8744. config->save_crtc_enabled =
  8745. kcalloc(dev->mode_config.num_crtc,
  8746. sizeof(bool), GFP_KERNEL);
  8747. if (!config->save_crtc_enabled)
  8748. return -ENOMEM;
  8749. config->save_encoder_crtcs =
  8750. kcalloc(dev->mode_config.num_encoder,
  8751. sizeof(struct drm_crtc *), GFP_KERNEL);
  8752. if (!config->save_encoder_crtcs)
  8753. return -ENOMEM;
  8754. config->save_connector_encoders =
  8755. kcalloc(dev->mode_config.num_connector,
  8756. sizeof(struct drm_encoder *), GFP_KERNEL);
  8757. if (!config->save_connector_encoders)
  8758. return -ENOMEM;
  8759. /* Copy data. Note that driver private data is not affected.
  8760. * Should anything bad happen only the expected state is
  8761. * restored, not the drivers personal bookkeeping.
  8762. */
  8763. count = 0;
  8764. for_each_crtc(dev, crtc) {
  8765. config->save_crtc_enabled[count++] = crtc->enabled;
  8766. }
  8767. count = 0;
  8768. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  8769. config->save_encoder_crtcs[count++] = encoder->crtc;
  8770. }
  8771. count = 0;
  8772. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  8773. config->save_connector_encoders[count++] = connector->encoder;
  8774. }
  8775. return 0;
  8776. }
  8777. static void intel_set_config_restore_state(struct drm_device *dev,
  8778. struct intel_set_config *config)
  8779. {
  8780. struct intel_crtc *crtc;
  8781. struct intel_encoder *encoder;
  8782. struct intel_connector *connector;
  8783. int count;
  8784. count = 0;
  8785. for_each_intel_crtc(dev, crtc) {
  8786. crtc->new_enabled = config->save_crtc_enabled[count++];
  8787. if (crtc->new_enabled)
  8788. crtc->new_config = &crtc->config;
  8789. else
  8790. crtc->new_config = NULL;
  8791. }
  8792. count = 0;
  8793. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8794. encoder->new_crtc =
  8795. to_intel_crtc(config->save_encoder_crtcs[count++]);
  8796. }
  8797. count = 0;
  8798. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  8799. connector->new_encoder =
  8800. to_intel_encoder(config->save_connector_encoders[count++]);
  8801. }
  8802. }
  8803. static bool
  8804. is_crtc_connector_off(struct drm_mode_set *set)
  8805. {
  8806. int i;
  8807. if (set->num_connectors == 0)
  8808. return false;
  8809. if (WARN_ON(set->connectors == NULL))
  8810. return false;
  8811. for (i = 0; i < set->num_connectors; i++)
  8812. if (set->connectors[i]->encoder &&
  8813. set->connectors[i]->encoder->crtc == set->crtc &&
  8814. set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
  8815. return true;
  8816. return false;
  8817. }
  8818. static void
  8819. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  8820. struct intel_set_config *config)
  8821. {
  8822. /* We should be able to check here if the fb has the same properties
  8823. * and then just flip_or_move it */
  8824. if (is_crtc_connector_off(set)) {
  8825. config->mode_changed = true;
  8826. } else if (set->crtc->primary->fb != set->fb) {
  8827. /* If we have no fb then treat it as a full mode set */
  8828. if (set->crtc->primary->fb == NULL) {
  8829. struct intel_crtc *intel_crtc =
  8830. to_intel_crtc(set->crtc);
  8831. if (intel_crtc->active && i915.fastboot) {
  8832. DRM_DEBUG_KMS("crtc has no fb, will flip\n");
  8833. config->fb_changed = true;
  8834. } else {
  8835. DRM_DEBUG_KMS("inactive crtc, full mode set\n");
  8836. config->mode_changed = true;
  8837. }
  8838. } else if (set->fb == NULL) {
  8839. config->mode_changed = true;
  8840. } else if (set->fb->pixel_format !=
  8841. set->crtc->primary->fb->pixel_format) {
  8842. config->mode_changed = true;
  8843. } else {
  8844. config->fb_changed = true;
  8845. }
  8846. }
  8847. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  8848. config->fb_changed = true;
  8849. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  8850. DRM_DEBUG_KMS("modes are different, full mode set\n");
  8851. drm_mode_debug_printmodeline(&set->crtc->mode);
  8852. drm_mode_debug_printmodeline(set->mode);
  8853. config->mode_changed = true;
  8854. }
  8855. DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
  8856. set->crtc->base.id, config->mode_changed, config->fb_changed);
  8857. }
  8858. static int
  8859. intel_modeset_stage_output_state(struct drm_device *dev,
  8860. struct drm_mode_set *set,
  8861. struct intel_set_config *config)
  8862. {
  8863. struct intel_connector *connector;
  8864. struct intel_encoder *encoder;
  8865. struct intel_crtc *crtc;
  8866. int ro;
  8867. /* The upper layers ensure that we either disable a crtc or have a list
  8868. * of connectors. For paranoia, double-check this. */
  8869. WARN_ON(!set->fb && (set->num_connectors != 0));
  8870. WARN_ON(set->fb && (set->num_connectors == 0));
  8871. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8872. base.head) {
  8873. /* Otherwise traverse passed in connector list and get encoders
  8874. * for them. */
  8875. for (ro = 0; ro < set->num_connectors; ro++) {
  8876. if (set->connectors[ro] == &connector->base) {
  8877. connector->new_encoder = connector->encoder;
  8878. break;
  8879. }
  8880. }
  8881. /* If we disable the crtc, disable all its connectors. Also, if
  8882. * the connector is on the changing crtc but not on the new
  8883. * connector list, disable it. */
  8884. if ((!set->fb || ro == set->num_connectors) &&
  8885. connector->base.encoder &&
  8886. connector->base.encoder->crtc == set->crtc) {
  8887. connector->new_encoder = NULL;
  8888. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  8889. connector->base.base.id,
  8890. connector->base.name);
  8891. }
  8892. if (&connector->new_encoder->base != connector->base.encoder) {
  8893. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  8894. config->mode_changed = true;
  8895. }
  8896. }
  8897. /* connector->new_encoder is now updated for all connectors. */
  8898. /* Update crtc of enabled connectors. */
  8899. list_for_each_entry(connector, &dev->mode_config.connector_list,
  8900. base.head) {
  8901. struct drm_crtc *new_crtc;
  8902. if (!connector->new_encoder)
  8903. continue;
  8904. new_crtc = connector->new_encoder->base.crtc;
  8905. for (ro = 0; ro < set->num_connectors; ro++) {
  8906. if (set->connectors[ro] == &connector->base)
  8907. new_crtc = set->crtc;
  8908. }
  8909. /* Make sure the new CRTC will work with the encoder */
  8910. if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
  8911. new_crtc)) {
  8912. return -EINVAL;
  8913. }
  8914. connector->encoder->new_crtc = to_intel_crtc(new_crtc);
  8915. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  8916. connector->base.base.id,
  8917. connector->base.name,
  8918. new_crtc->base.id);
  8919. }
  8920. /* Check for any encoders that needs to be disabled. */
  8921. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  8922. base.head) {
  8923. int num_connectors = 0;
  8924. list_for_each_entry(connector,
  8925. &dev->mode_config.connector_list,
  8926. base.head) {
  8927. if (connector->new_encoder == encoder) {
  8928. WARN_ON(!connector->new_encoder->new_crtc);
  8929. num_connectors++;
  8930. }
  8931. }
  8932. if (num_connectors == 0)
  8933. encoder->new_crtc = NULL;
  8934. else if (num_connectors > 1)
  8935. return -EINVAL;
  8936. /* Only now check for crtc changes so we don't miss encoders
  8937. * that will be disabled. */
  8938. if (&encoder->new_crtc->base != encoder->base.crtc) {
  8939. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  8940. config->mode_changed = true;
  8941. }
  8942. }
  8943. /* Now we've also updated encoder->new_crtc for all encoders. */
  8944. for_each_intel_crtc(dev, crtc) {
  8945. crtc->new_enabled = false;
  8946. list_for_each_entry(encoder,
  8947. &dev->mode_config.encoder_list,
  8948. base.head) {
  8949. if (encoder->new_crtc == crtc) {
  8950. crtc->new_enabled = true;
  8951. break;
  8952. }
  8953. }
  8954. if (crtc->new_enabled != crtc->base.enabled) {
  8955. DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
  8956. crtc->new_enabled ? "en" : "dis");
  8957. config->mode_changed = true;
  8958. }
  8959. if (crtc->new_enabled)
  8960. crtc->new_config = &crtc->config;
  8961. else
  8962. crtc->new_config = NULL;
  8963. }
  8964. return 0;
  8965. }
  8966. static void disable_crtc_nofb(struct intel_crtc *crtc)
  8967. {
  8968. struct drm_device *dev = crtc->base.dev;
  8969. struct intel_encoder *encoder;
  8970. struct intel_connector *connector;
  8971. DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
  8972. pipe_name(crtc->pipe));
  8973. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  8974. if (connector->new_encoder &&
  8975. connector->new_encoder->new_crtc == crtc)
  8976. connector->new_encoder = NULL;
  8977. }
  8978. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  8979. if (encoder->new_crtc == crtc)
  8980. encoder->new_crtc = NULL;
  8981. }
  8982. crtc->new_enabled = false;
  8983. crtc->new_config = NULL;
  8984. }
  8985. static int intel_crtc_set_config(struct drm_mode_set *set)
  8986. {
  8987. struct drm_device *dev;
  8988. struct drm_mode_set save_set;
  8989. struct intel_set_config *config;
  8990. int ret;
  8991. BUG_ON(!set);
  8992. BUG_ON(!set->crtc);
  8993. BUG_ON(!set->crtc->helper_private);
  8994. /* Enforce sane interface api - has been abused by the fb helper. */
  8995. BUG_ON(!set->mode && set->fb);
  8996. BUG_ON(set->fb && set->num_connectors == 0);
  8997. if (set->fb) {
  8998. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  8999. set->crtc->base.id, set->fb->base.id,
  9000. (int)set->num_connectors, set->x, set->y);
  9001. } else {
  9002. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  9003. }
  9004. dev = set->crtc->dev;
  9005. ret = -ENOMEM;
  9006. config = kzalloc(sizeof(*config), GFP_KERNEL);
  9007. if (!config)
  9008. goto out_config;
  9009. ret = intel_set_config_save_state(dev, config);
  9010. if (ret)
  9011. goto out_config;
  9012. save_set.crtc = set->crtc;
  9013. save_set.mode = &set->crtc->mode;
  9014. save_set.x = set->crtc->x;
  9015. save_set.y = set->crtc->y;
  9016. save_set.fb = set->crtc->primary->fb;
  9017. /* Compute whether we need a full modeset, only an fb base update or no
  9018. * change at all. In the future we might also check whether only the
  9019. * mode changed, e.g. for LVDS where we only change the panel fitter in
  9020. * such cases. */
  9021. intel_set_config_compute_mode_changes(set, config);
  9022. ret = intel_modeset_stage_output_state(dev, set, config);
  9023. if (ret)
  9024. goto fail;
  9025. if (config->mode_changed) {
  9026. ret = intel_set_mode(set->crtc, set->mode,
  9027. set->x, set->y, set->fb);
  9028. } else if (config->fb_changed) {
  9029. intel_crtc_wait_for_pending_flips(set->crtc);
  9030. ret = intel_pipe_set_base(set->crtc,
  9031. set->x, set->y, set->fb);
  9032. /*
  9033. * In the fastboot case this may be our only check of the
  9034. * state after boot. It would be better to only do it on
  9035. * the first update, but we don't have a nice way of doing that
  9036. * (and really, set_config isn't used much for high freq page
  9037. * flipping, so increasing its cost here shouldn't be a big
  9038. * deal).
  9039. */
  9040. if (i915.fastboot && ret == 0)
  9041. intel_modeset_check_state(set->crtc->dev);
  9042. }
  9043. if (ret) {
  9044. DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
  9045. set->crtc->base.id, ret);
  9046. fail:
  9047. intel_set_config_restore_state(dev, config);
  9048. /*
  9049. * HACK: if the pipe was on, but we didn't have a framebuffer,
  9050. * force the pipe off to avoid oopsing in the modeset code
  9051. * due to fb==NULL. This should only happen during boot since
  9052. * we don't yet reconstruct the FB from the hardware state.
  9053. */
  9054. if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
  9055. disable_crtc_nofb(to_intel_crtc(save_set.crtc));
  9056. /* Try to restore the config */
  9057. if (config->mode_changed &&
  9058. intel_set_mode(save_set.crtc, save_set.mode,
  9059. save_set.x, save_set.y, save_set.fb))
  9060. DRM_ERROR("failed to restore config after modeset failure\n");
  9061. }
  9062. out_config:
  9063. intel_set_config_free(config);
  9064. return ret;
  9065. }
  9066. static const struct drm_crtc_funcs intel_crtc_funcs = {
  9067. .cursor_set = intel_crtc_cursor_set,
  9068. .cursor_move = intel_crtc_cursor_move,
  9069. .gamma_set = intel_crtc_gamma_set,
  9070. .set_config = intel_crtc_set_config,
  9071. .destroy = intel_crtc_destroy,
  9072. .page_flip = intel_crtc_page_flip,
  9073. };
  9074. static void intel_cpu_pll_init(struct drm_device *dev)
  9075. {
  9076. if (HAS_DDI(dev))
  9077. intel_ddi_pll_init(dev);
  9078. }
  9079. static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
  9080. struct intel_shared_dpll *pll,
  9081. struct intel_dpll_hw_state *hw_state)
  9082. {
  9083. uint32_t val;
  9084. val = I915_READ(PCH_DPLL(pll->id));
  9085. hw_state->dpll = val;
  9086. hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
  9087. hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
  9088. return val & DPLL_VCO_ENABLE;
  9089. }
  9090. static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
  9091. struct intel_shared_dpll *pll)
  9092. {
  9093. I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
  9094. I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
  9095. }
  9096. static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
  9097. struct intel_shared_dpll *pll)
  9098. {
  9099. /* PCH refclock must be enabled first */
  9100. ibx_assert_pch_refclk_enabled(dev_priv);
  9101. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9102. /* Wait for the clocks to stabilize. */
  9103. POSTING_READ(PCH_DPLL(pll->id));
  9104. udelay(150);
  9105. /* The pixel multiplier can only be updated once the
  9106. * DPLL is enabled and the clocks are stable.
  9107. *
  9108. * So write it again.
  9109. */
  9110. I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
  9111. POSTING_READ(PCH_DPLL(pll->id));
  9112. udelay(200);
  9113. }
  9114. static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
  9115. struct intel_shared_dpll *pll)
  9116. {
  9117. struct drm_device *dev = dev_priv->dev;
  9118. struct intel_crtc *crtc;
  9119. /* Make sure no transcoder isn't still depending on us. */
  9120. for_each_intel_crtc(dev, crtc) {
  9121. if (intel_crtc_to_shared_dpll(crtc) == pll)
  9122. assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
  9123. }
  9124. I915_WRITE(PCH_DPLL(pll->id), 0);
  9125. POSTING_READ(PCH_DPLL(pll->id));
  9126. udelay(200);
  9127. }
  9128. static char *ibx_pch_dpll_names[] = {
  9129. "PCH DPLL A",
  9130. "PCH DPLL B",
  9131. };
  9132. static void ibx_pch_dpll_init(struct drm_device *dev)
  9133. {
  9134. struct drm_i915_private *dev_priv = dev->dev_private;
  9135. int i;
  9136. dev_priv->num_shared_dpll = 2;
  9137. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  9138. dev_priv->shared_dplls[i].id = i;
  9139. dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
  9140. dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
  9141. dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
  9142. dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
  9143. dev_priv->shared_dplls[i].get_hw_state =
  9144. ibx_pch_dpll_get_hw_state;
  9145. }
  9146. }
  9147. static void intel_shared_dpll_init(struct drm_device *dev)
  9148. {
  9149. struct drm_i915_private *dev_priv = dev->dev_private;
  9150. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  9151. ibx_pch_dpll_init(dev);
  9152. else
  9153. dev_priv->num_shared_dpll = 0;
  9154. BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
  9155. }
  9156. static void intel_crtc_init(struct drm_device *dev, int pipe)
  9157. {
  9158. struct drm_i915_private *dev_priv = dev->dev_private;
  9159. struct intel_crtc *intel_crtc;
  9160. int i;
  9161. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  9162. if (intel_crtc == NULL)
  9163. return;
  9164. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  9165. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  9166. for (i = 0; i < 256; i++) {
  9167. intel_crtc->lut_r[i] = i;
  9168. intel_crtc->lut_g[i] = i;
  9169. intel_crtc->lut_b[i] = i;
  9170. }
  9171. /*
  9172. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  9173. * is hooked to plane B. Hence we want plane A feeding pipe B.
  9174. */
  9175. intel_crtc->pipe = pipe;
  9176. intel_crtc->plane = pipe;
  9177. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  9178. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  9179. intel_crtc->plane = !pipe;
  9180. }
  9181. intel_crtc->cursor_base = ~0;
  9182. intel_crtc->cursor_cntl = ~0;
  9183. init_waitqueue_head(&intel_crtc->vbl_wait);
  9184. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  9185. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  9186. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  9187. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  9188. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  9189. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  9190. }
  9191. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  9192. {
  9193. struct drm_encoder *encoder = connector->base.encoder;
  9194. struct drm_device *dev = connector->base.dev;
  9195. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  9196. if (!encoder)
  9197. return INVALID_PIPE;
  9198. return to_intel_crtc(encoder->crtc)->pipe;
  9199. }
  9200. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  9201. struct drm_file *file)
  9202. {
  9203. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  9204. struct drm_mode_object *drmmode_obj;
  9205. struct intel_crtc *crtc;
  9206. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  9207. return -ENODEV;
  9208. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  9209. DRM_MODE_OBJECT_CRTC);
  9210. if (!drmmode_obj) {
  9211. DRM_ERROR("no such CRTC id\n");
  9212. return -ENOENT;
  9213. }
  9214. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  9215. pipe_from_crtc_id->pipe = crtc->pipe;
  9216. return 0;
  9217. }
  9218. static int intel_encoder_clones(struct intel_encoder *encoder)
  9219. {
  9220. struct drm_device *dev = encoder->base.dev;
  9221. struct intel_encoder *source_encoder;
  9222. int index_mask = 0;
  9223. int entry = 0;
  9224. list_for_each_entry(source_encoder,
  9225. &dev->mode_config.encoder_list, base.head) {
  9226. if (encoders_cloneable(encoder, source_encoder))
  9227. index_mask |= (1 << entry);
  9228. entry++;
  9229. }
  9230. return index_mask;
  9231. }
  9232. static bool has_edp_a(struct drm_device *dev)
  9233. {
  9234. struct drm_i915_private *dev_priv = dev->dev_private;
  9235. if (!IS_MOBILE(dev))
  9236. return false;
  9237. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  9238. return false;
  9239. if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  9240. return false;
  9241. return true;
  9242. }
  9243. const char *intel_output_name(int output)
  9244. {
  9245. static const char *names[] = {
  9246. [INTEL_OUTPUT_UNUSED] = "Unused",
  9247. [INTEL_OUTPUT_ANALOG] = "Analog",
  9248. [INTEL_OUTPUT_DVO] = "DVO",
  9249. [INTEL_OUTPUT_SDVO] = "SDVO",
  9250. [INTEL_OUTPUT_LVDS] = "LVDS",
  9251. [INTEL_OUTPUT_TVOUT] = "TV",
  9252. [INTEL_OUTPUT_HDMI] = "HDMI",
  9253. [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
  9254. [INTEL_OUTPUT_EDP] = "eDP",
  9255. [INTEL_OUTPUT_DSI] = "DSI",
  9256. [INTEL_OUTPUT_UNKNOWN] = "Unknown",
  9257. };
  9258. if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
  9259. return "Invalid";
  9260. return names[output];
  9261. }
  9262. static bool intel_crt_present(struct drm_device *dev)
  9263. {
  9264. struct drm_i915_private *dev_priv = dev->dev_private;
  9265. if (IS_ULT(dev))
  9266. return false;
  9267. if (IS_CHERRYVIEW(dev))
  9268. return false;
  9269. if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
  9270. return false;
  9271. return true;
  9272. }
  9273. static void intel_setup_outputs(struct drm_device *dev)
  9274. {
  9275. struct drm_i915_private *dev_priv = dev->dev_private;
  9276. struct intel_encoder *encoder;
  9277. bool dpd_is_edp = false;
  9278. intel_lvds_init(dev);
  9279. if (intel_crt_present(dev))
  9280. intel_crt_init(dev);
  9281. if (HAS_DDI(dev)) {
  9282. int found;
  9283. /* Haswell uses DDI functions to detect digital outputs */
  9284. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  9285. /* DDI A only supports eDP */
  9286. if (found)
  9287. intel_ddi_init(dev, PORT_A);
  9288. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  9289. * register */
  9290. found = I915_READ(SFUSE_STRAP);
  9291. if (found & SFUSE_STRAP_DDIB_DETECTED)
  9292. intel_ddi_init(dev, PORT_B);
  9293. if (found & SFUSE_STRAP_DDIC_DETECTED)
  9294. intel_ddi_init(dev, PORT_C);
  9295. if (found & SFUSE_STRAP_DDID_DETECTED)
  9296. intel_ddi_init(dev, PORT_D);
  9297. } else if (HAS_PCH_SPLIT(dev)) {
  9298. int found;
  9299. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  9300. if (has_edp_a(dev))
  9301. intel_dp_init(dev, DP_A, PORT_A);
  9302. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  9303. /* PCH SDVOB multiplex with HDMIB */
  9304. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  9305. if (!found)
  9306. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  9307. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  9308. intel_dp_init(dev, PCH_DP_B, PORT_B);
  9309. }
  9310. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  9311. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  9312. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  9313. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  9314. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  9315. intel_dp_init(dev, PCH_DP_C, PORT_C);
  9316. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  9317. intel_dp_init(dev, PCH_DP_D, PORT_D);
  9318. } else if (IS_VALLEYVIEW(dev)) {
  9319. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  9320. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  9321. PORT_B);
  9322. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  9323. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  9324. }
  9325. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
  9326. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
  9327. PORT_C);
  9328. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  9329. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  9330. }
  9331. if (IS_CHERRYVIEW(dev)) {
  9332. if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
  9333. intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
  9334. PORT_D);
  9335. if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
  9336. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
  9337. }
  9338. }
  9339. intel_dsi_init(dev);
  9340. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  9341. bool found = false;
  9342. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  9343. DRM_DEBUG_KMS("probing SDVOB\n");
  9344. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  9345. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  9346. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  9347. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  9348. }
  9349. if (!found && SUPPORTS_INTEGRATED_DP(dev))
  9350. intel_dp_init(dev, DP_B, PORT_B);
  9351. }
  9352. /* Before G4X SDVOC doesn't have its own detect register */
  9353. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  9354. DRM_DEBUG_KMS("probing SDVOC\n");
  9355. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  9356. }
  9357. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  9358. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  9359. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  9360. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  9361. }
  9362. if (SUPPORTS_INTEGRATED_DP(dev))
  9363. intel_dp_init(dev, DP_C, PORT_C);
  9364. }
  9365. if (SUPPORTS_INTEGRATED_DP(dev) &&
  9366. (I915_READ(DP_D) & DP_DETECTED))
  9367. intel_dp_init(dev, DP_D, PORT_D);
  9368. } else if (IS_GEN2(dev))
  9369. intel_dvo_init(dev);
  9370. if (SUPPORTS_TV(dev))
  9371. intel_tv_init(dev);
  9372. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  9373. encoder->base.possible_crtcs = encoder->crtc_mask;
  9374. encoder->base.possible_clones =
  9375. intel_encoder_clones(encoder);
  9376. }
  9377. intel_init_pch_refclk(dev);
  9378. drm_helper_move_panel_connectors_to_head(dev);
  9379. }
  9380. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  9381. {
  9382. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9383. drm_framebuffer_cleanup(fb);
  9384. WARN_ON(!intel_fb->obj->framebuffer_references--);
  9385. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  9386. kfree(intel_fb);
  9387. }
  9388. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  9389. struct drm_file *file,
  9390. unsigned int *handle)
  9391. {
  9392. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  9393. struct drm_i915_gem_object *obj = intel_fb->obj;
  9394. return drm_gem_handle_create(file, &obj->base, handle);
  9395. }
  9396. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  9397. .destroy = intel_user_framebuffer_destroy,
  9398. .create_handle = intel_user_framebuffer_create_handle,
  9399. };
  9400. static int intel_framebuffer_init(struct drm_device *dev,
  9401. struct intel_framebuffer *intel_fb,
  9402. struct drm_mode_fb_cmd2 *mode_cmd,
  9403. struct drm_i915_gem_object *obj)
  9404. {
  9405. int aligned_height;
  9406. int pitch_limit;
  9407. int ret;
  9408. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  9409. if (obj->tiling_mode == I915_TILING_Y) {
  9410. DRM_DEBUG("hardware does not support tiling Y\n");
  9411. return -EINVAL;
  9412. }
  9413. if (mode_cmd->pitches[0] & 63) {
  9414. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  9415. mode_cmd->pitches[0]);
  9416. return -EINVAL;
  9417. }
  9418. if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
  9419. pitch_limit = 32*1024;
  9420. } else if (INTEL_INFO(dev)->gen >= 4) {
  9421. if (obj->tiling_mode)
  9422. pitch_limit = 16*1024;
  9423. else
  9424. pitch_limit = 32*1024;
  9425. } else if (INTEL_INFO(dev)->gen >= 3) {
  9426. if (obj->tiling_mode)
  9427. pitch_limit = 8*1024;
  9428. else
  9429. pitch_limit = 16*1024;
  9430. } else
  9431. /* XXX DSPC is limited to 4k tiled */
  9432. pitch_limit = 8*1024;
  9433. if (mode_cmd->pitches[0] > pitch_limit) {
  9434. DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
  9435. obj->tiling_mode ? "tiled" : "linear",
  9436. mode_cmd->pitches[0], pitch_limit);
  9437. return -EINVAL;
  9438. }
  9439. if (obj->tiling_mode != I915_TILING_NONE &&
  9440. mode_cmd->pitches[0] != obj->stride) {
  9441. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  9442. mode_cmd->pitches[0], obj->stride);
  9443. return -EINVAL;
  9444. }
  9445. /* Reject formats not supported by any plane early. */
  9446. switch (mode_cmd->pixel_format) {
  9447. case DRM_FORMAT_C8:
  9448. case DRM_FORMAT_RGB565:
  9449. case DRM_FORMAT_XRGB8888:
  9450. case DRM_FORMAT_ARGB8888:
  9451. break;
  9452. case DRM_FORMAT_XRGB1555:
  9453. case DRM_FORMAT_ARGB1555:
  9454. if (INTEL_INFO(dev)->gen > 3) {
  9455. DRM_DEBUG("unsupported pixel format: %s\n",
  9456. drm_get_format_name(mode_cmd->pixel_format));
  9457. return -EINVAL;
  9458. }
  9459. break;
  9460. case DRM_FORMAT_XBGR8888:
  9461. case DRM_FORMAT_ABGR8888:
  9462. case DRM_FORMAT_XRGB2101010:
  9463. case DRM_FORMAT_ARGB2101010:
  9464. case DRM_FORMAT_XBGR2101010:
  9465. case DRM_FORMAT_ABGR2101010:
  9466. if (INTEL_INFO(dev)->gen < 4) {
  9467. DRM_DEBUG("unsupported pixel format: %s\n",
  9468. drm_get_format_name(mode_cmd->pixel_format));
  9469. return -EINVAL;
  9470. }
  9471. break;
  9472. case DRM_FORMAT_YUYV:
  9473. case DRM_FORMAT_UYVY:
  9474. case DRM_FORMAT_YVYU:
  9475. case DRM_FORMAT_VYUY:
  9476. if (INTEL_INFO(dev)->gen < 5) {
  9477. DRM_DEBUG("unsupported pixel format: %s\n",
  9478. drm_get_format_name(mode_cmd->pixel_format));
  9479. return -EINVAL;
  9480. }
  9481. break;
  9482. default:
  9483. DRM_DEBUG("unsupported pixel format: %s\n",
  9484. drm_get_format_name(mode_cmd->pixel_format));
  9485. return -EINVAL;
  9486. }
  9487. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  9488. if (mode_cmd->offsets[0] != 0)
  9489. return -EINVAL;
  9490. aligned_height = intel_align_height(dev, mode_cmd->height,
  9491. obj->tiling_mode);
  9492. /* FIXME drm helper for size checks (especially planar formats)? */
  9493. if (obj->base.size < aligned_height * mode_cmd->pitches[0])
  9494. return -EINVAL;
  9495. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  9496. intel_fb->obj = obj;
  9497. intel_fb->obj->framebuffer_references++;
  9498. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  9499. if (ret) {
  9500. DRM_ERROR("framebuffer init failed %d\n", ret);
  9501. return ret;
  9502. }
  9503. return 0;
  9504. }
  9505. static struct drm_framebuffer *
  9506. intel_user_framebuffer_create(struct drm_device *dev,
  9507. struct drm_file *filp,
  9508. struct drm_mode_fb_cmd2 *mode_cmd)
  9509. {
  9510. struct drm_i915_gem_object *obj;
  9511. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  9512. mode_cmd->handles[0]));
  9513. if (&obj->base == NULL)
  9514. return ERR_PTR(-ENOENT);
  9515. return intel_framebuffer_create(dev, mode_cmd, obj);
  9516. }
  9517. #ifndef CONFIG_DRM_I915_FBDEV
  9518. static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
  9519. {
  9520. }
  9521. #endif
  9522. static const struct drm_mode_config_funcs intel_mode_funcs = {
  9523. .fb_create = intel_user_framebuffer_create,
  9524. .output_poll_changed = intel_fbdev_output_poll_changed,
  9525. };
  9526. /* Set up chip specific display functions */
  9527. static void intel_init_display(struct drm_device *dev)
  9528. {
  9529. struct drm_i915_private *dev_priv = dev->dev_private;
  9530. if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
  9531. dev_priv->display.find_dpll = g4x_find_best_dpll;
  9532. else if (IS_CHERRYVIEW(dev))
  9533. dev_priv->display.find_dpll = chv_find_best_dpll;
  9534. else if (IS_VALLEYVIEW(dev))
  9535. dev_priv->display.find_dpll = vlv_find_best_dpll;
  9536. else if (IS_PINEVIEW(dev))
  9537. dev_priv->display.find_dpll = pnv_find_best_dpll;
  9538. else
  9539. dev_priv->display.find_dpll = i9xx_find_best_dpll;
  9540. if (HAS_DDI(dev)) {
  9541. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  9542. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  9543. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  9544. dev_priv->display.crtc_enable = haswell_crtc_enable;
  9545. dev_priv->display.crtc_disable = haswell_crtc_disable;
  9546. dev_priv->display.off = haswell_crtc_off;
  9547. dev_priv->display.update_primary_plane =
  9548. ironlake_update_primary_plane;
  9549. } else if (HAS_PCH_SPLIT(dev)) {
  9550. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  9551. dev_priv->display.get_plane_config = ironlake_get_plane_config;
  9552. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  9553. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  9554. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  9555. dev_priv->display.off = ironlake_crtc_off;
  9556. dev_priv->display.update_primary_plane =
  9557. ironlake_update_primary_plane;
  9558. } else if (IS_VALLEYVIEW(dev)) {
  9559. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  9560. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  9561. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  9562. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  9563. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  9564. dev_priv->display.off = i9xx_crtc_off;
  9565. dev_priv->display.update_primary_plane =
  9566. i9xx_update_primary_plane;
  9567. } else {
  9568. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  9569. dev_priv->display.get_plane_config = i9xx_get_plane_config;
  9570. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  9571. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  9572. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  9573. dev_priv->display.off = i9xx_crtc_off;
  9574. dev_priv->display.update_primary_plane =
  9575. i9xx_update_primary_plane;
  9576. }
  9577. /* Returns the core display clock speed */
  9578. if (IS_VALLEYVIEW(dev))
  9579. dev_priv->display.get_display_clock_speed =
  9580. valleyview_get_display_clock_speed;
  9581. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  9582. dev_priv->display.get_display_clock_speed =
  9583. i945_get_display_clock_speed;
  9584. else if (IS_I915G(dev))
  9585. dev_priv->display.get_display_clock_speed =
  9586. i915_get_display_clock_speed;
  9587. else if (IS_I945GM(dev) || IS_845G(dev))
  9588. dev_priv->display.get_display_clock_speed =
  9589. i9xx_misc_get_display_clock_speed;
  9590. else if (IS_PINEVIEW(dev))
  9591. dev_priv->display.get_display_clock_speed =
  9592. pnv_get_display_clock_speed;
  9593. else if (IS_I915GM(dev))
  9594. dev_priv->display.get_display_clock_speed =
  9595. i915gm_get_display_clock_speed;
  9596. else if (IS_I865G(dev))
  9597. dev_priv->display.get_display_clock_speed =
  9598. i865_get_display_clock_speed;
  9599. else if (IS_I85X(dev))
  9600. dev_priv->display.get_display_clock_speed =
  9601. i855_get_display_clock_speed;
  9602. else /* 852, 830 */
  9603. dev_priv->display.get_display_clock_speed =
  9604. i830_get_display_clock_speed;
  9605. if (HAS_PCH_SPLIT(dev)) {
  9606. if (IS_GEN5(dev)) {
  9607. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  9608. dev_priv->display.write_eld = ironlake_write_eld;
  9609. } else if (IS_GEN6(dev)) {
  9610. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  9611. dev_priv->display.write_eld = ironlake_write_eld;
  9612. dev_priv->display.modeset_global_resources =
  9613. snb_modeset_global_resources;
  9614. } else if (IS_IVYBRIDGE(dev)) {
  9615. /* FIXME: detect B0+ stepping and use auto training */
  9616. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  9617. dev_priv->display.write_eld = ironlake_write_eld;
  9618. dev_priv->display.modeset_global_resources =
  9619. ivb_modeset_global_resources;
  9620. } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
  9621. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  9622. dev_priv->display.write_eld = haswell_write_eld;
  9623. dev_priv->display.modeset_global_resources =
  9624. haswell_modeset_global_resources;
  9625. }
  9626. } else if (IS_G4X(dev)) {
  9627. dev_priv->display.write_eld = g4x_write_eld;
  9628. } else if (IS_VALLEYVIEW(dev)) {
  9629. dev_priv->display.modeset_global_resources =
  9630. valleyview_modeset_global_resources;
  9631. dev_priv->display.write_eld = ironlake_write_eld;
  9632. }
  9633. /* Default just returns -ENODEV to indicate unsupported */
  9634. dev_priv->display.queue_flip = intel_default_queue_flip;
  9635. switch (INTEL_INFO(dev)->gen) {
  9636. case 2:
  9637. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  9638. break;
  9639. case 3:
  9640. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  9641. break;
  9642. case 4:
  9643. case 5:
  9644. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  9645. break;
  9646. case 6:
  9647. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  9648. break;
  9649. case 7:
  9650. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  9651. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  9652. break;
  9653. }
  9654. intel_panel_init_backlight_funcs(dev);
  9655. }
  9656. /*
  9657. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  9658. * resume, or other times. This quirk makes sure that's the case for
  9659. * affected systems.
  9660. */
  9661. static void quirk_pipea_force(struct drm_device *dev)
  9662. {
  9663. struct drm_i915_private *dev_priv = dev->dev_private;
  9664. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  9665. DRM_INFO("applying pipe a force quirk\n");
  9666. }
  9667. /*
  9668. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  9669. */
  9670. static void quirk_ssc_force_disable(struct drm_device *dev)
  9671. {
  9672. struct drm_i915_private *dev_priv = dev->dev_private;
  9673. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  9674. DRM_INFO("applying lvds SSC disable quirk\n");
  9675. }
  9676. /*
  9677. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  9678. * brightness value
  9679. */
  9680. static void quirk_invert_brightness(struct drm_device *dev)
  9681. {
  9682. struct drm_i915_private *dev_priv = dev->dev_private;
  9683. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  9684. DRM_INFO("applying inverted panel brightness quirk\n");
  9685. }
  9686. /* Some VBT's incorrectly indicate no backlight is present */
  9687. static void quirk_backlight_present(struct drm_device *dev)
  9688. {
  9689. struct drm_i915_private *dev_priv = dev->dev_private;
  9690. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  9691. DRM_INFO("applying backlight present quirk\n");
  9692. }
  9693. struct intel_quirk {
  9694. int device;
  9695. int subsystem_vendor;
  9696. int subsystem_device;
  9697. void (*hook)(struct drm_device *dev);
  9698. };
  9699. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  9700. struct intel_dmi_quirk {
  9701. void (*hook)(struct drm_device *dev);
  9702. const struct dmi_system_id (*dmi_id_list)[];
  9703. };
  9704. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  9705. {
  9706. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  9707. return 1;
  9708. }
  9709. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  9710. {
  9711. .dmi_id_list = &(const struct dmi_system_id[]) {
  9712. {
  9713. .callback = intel_dmi_reverse_brightness,
  9714. .ident = "NCR Corporation",
  9715. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  9716. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  9717. },
  9718. },
  9719. { } /* terminating entry */
  9720. },
  9721. .hook = quirk_invert_brightness,
  9722. },
  9723. };
  9724. static struct intel_quirk intel_quirks[] = {
  9725. /* HP Mini needs pipe A force quirk (LP: #322104) */
  9726. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  9727. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  9728. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  9729. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  9730. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  9731. /* Lenovo U160 cannot use SSC on LVDS */
  9732. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  9733. /* Sony Vaio Y cannot use SSC on LVDS */
  9734. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  9735. /* Acer Aspire 5734Z must invert backlight brightness */
  9736. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  9737. /* Acer/eMachines G725 */
  9738. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  9739. /* Acer/eMachines e725 */
  9740. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  9741. /* Acer/Packard Bell NCL20 */
  9742. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  9743. /* Acer Aspire 4736Z */
  9744. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  9745. /* Acer Aspire 5336 */
  9746. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  9747. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  9748. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  9749. };
  9750. static void intel_init_quirks(struct drm_device *dev)
  9751. {
  9752. struct pci_dev *d = dev->pdev;
  9753. int i;
  9754. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  9755. struct intel_quirk *q = &intel_quirks[i];
  9756. if (d->device == q->device &&
  9757. (d->subsystem_vendor == q->subsystem_vendor ||
  9758. q->subsystem_vendor == PCI_ANY_ID) &&
  9759. (d->subsystem_device == q->subsystem_device ||
  9760. q->subsystem_device == PCI_ANY_ID))
  9761. q->hook(dev);
  9762. }
  9763. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  9764. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  9765. intel_dmi_quirks[i].hook(dev);
  9766. }
  9767. }
  9768. /* Disable the VGA plane that we never use */
  9769. static void i915_disable_vga(struct drm_device *dev)
  9770. {
  9771. struct drm_i915_private *dev_priv = dev->dev_private;
  9772. u8 sr1;
  9773. u32 vga_reg = i915_vgacntrl_reg(dev);
  9774. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  9775. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  9776. outb(SR01, VGA_SR_INDEX);
  9777. sr1 = inb(VGA_SR_DATA);
  9778. outb(sr1 | 1<<5, VGA_SR_DATA);
  9779. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  9780. udelay(300);
  9781. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  9782. POSTING_READ(vga_reg);
  9783. }
  9784. void intel_modeset_init_hw(struct drm_device *dev)
  9785. {
  9786. intel_prepare_ddi(dev);
  9787. intel_init_clock_gating(dev);
  9788. intel_reset_dpio(dev);
  9789. intel_enable_gt_powersave(dev);
  9790. }
  9791. void intel_modeset_suspend_hw(struct drm_device *dev)
  9792. {
  9793. intel_suspend_hw(dev);
  9794. }
  9795. void intel_modeset_init(struct drm_device *dev)
  9796. {
  9797. struct drm_i915_private *dev_priv = dev->dev_private;
  9798. int sprite, ret;
  9799. enum pipe pipe;
  9800. struct intel_crtc *crtc;
  9801. drm_mode_config_init(dev);
  9802. dev->mode_config.min_width = 0;
  9803. dev->mode_config.min_height = 0;
  9804. dev->mode_config.preferred_depth = 24;
  9805. dev->mode_config.prefer_shadow = 1;
  9806. dev->mode_config.funcs = &intel_mode_funcs;
  9807. intel_init_quirks(dev);
  9808. intel_init_pm(dev);
  9809. if (INTEL_INFO(dev)->num_pipes == 0)
  9810. return;
  9811. intel_init_display(dev);
  9812. if (IS_GEN2(dev)) {
  9813. dev->mode_config.max_width = 2048;
  9814. dev->mode_config.max_height = 2048;
  9815. } else if (IS_GEN3(dev)) {
  9816. dev->mode_config.max_width = 4096;
  9817. dev->mode_config.max_height = 4096;
  9818. } else {
  9819. dev->mode_config.max_width = 8192;
  9820. dev->mode_config.max_height = 8192;
  9821. }
  9822. if (IS_GEN2(dev)) {
  9823. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  9824. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  9825. } else {
  9826. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  9827. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  9828. }
  9829. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  9830. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  9831. INTEL_INFO(dev)->num_pipes,
  9832. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  9833. for_each_pipe(pipe) {
  9834. intel_crtc_init(dev, pipe);
  9835. for_each_sprite(pipe, sprite) {
  9836. ret = intel_plane_init(dev, pipe, sprite);
  9837. if (ret)
  9838. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  9839. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  9840. }
  9841. }
  9842. intel_init_dpio(dev);
  9843. intel_reset_dpio(dev);
  9844. intel_cpu_pll_init(dev);
  9845. intel_shared_dpll_init(dev);
  9846. /* Just disable it once at startup */
  9847. i915_disable_vga(dev);
  9848. intel_setup_outputs(dev);
  9849. /* Just in case the BIOS is doing something questionable. */
  9850. intel_disable_fbc(dev);
  9851. drm_modeset_lock_all(dev);
  9852. intel_modeset_setup_hw_state(dev, false);
  9853. drm_modeset_unlock_all(dev);
  9854. for_each_intel_crtc(dev, crtc) {
  9855. if (!crtc->active)
  9856. continue;
  9857. /*
  9858. * Note that reserving the BIOS fb up front prevents us
  9859. * from stuffing other stolen allocations like the ring
  9860. * on top. This prevents some ugliness at boot time, and
  9861. * can even allow for smooth boot transitions if the BIOS
  9862. * fb is large enough for the active pipe configuration.
  9863. */
  9864. if (dev_priv->display.get_plane_config) {
  9865. dev_priv->display.get_plane_config(crtc,
  9866. &crtc->plane_config);
  9867. /*
  9868. * If the fb is shared between multiple heads, we'll
  9869. * just get the first one.
  9870. */
  9871. intel_find_plane_obj(crtc, &crtc->plane_config);
  9872. }
  9873. }
  9874. }
  9875. static void intel_enable_pipe_a(struct drm_device *dev)
  9876. {
  9877. struct intel_connector *connector;
  9878. struct drm_connector *crt = NULL;
  9879. struct intel_load_detect_pipe load_detect_temp;
  9880. struct drm_modeset_acquire_ctx ctx;
  9881. /* We can't just switch on the pipe A, we need to set things up with a
  9882. * proper mode and output configuration. As a gross hack, enable pipe A
  9883. * by enabling the load detect pipe once. */
  9884. list_for_each_entry(connector,
  9885. &dev->mode_config.connector_list,
  9886. base.head) {
  9887. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  9888. crt = &connector->base;
  9889. break;
  9890. }
  9891. }
  9892. if (!crt)
  9893. return;
  9894. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
  9895. intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
  9896. }
  9897. static bool
  9898. intel_check_plane_mapping(struct intel_crtc *crtc)
  9899. {
  9900. struct drm_device *dev = crtc->base.dev;
  9901. struct drm_i915_private *dev_priv = dev->dev_private;
  9902. u32 reg, val;
  9903. if (INTEL_INFO(dev)->num_pipes == 1)
  9904. return true;
  9905. reg = DSPCNTR(!crtc->plane);
  9906. val = I915_READ(reg);
  9907. if ((val & DISPLAY_PLANE_ENABLE) &&
  9908. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  9909. return false;
  9910. return true;
  9911. }
  9912. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  9913. {
  9914. struct drm_device *dev = crtc->base.dev;
  9915. struct drm_i915_private *dev_priv = dev->dev_private;
  9916. u32 reg;
  9917. /* Clear any frame start delays used for debugging left by the BIOS */
  9918. reg = PIPECONF(crtc->config.cpu_transcoder);
  9919. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  9920. /* restore vblank interrupts to correct state */
  9921. if (crtc->active)
  9922. drm_vblank_on(dev, crtc->pipe);
  9923. else
  9924. drm_vblank_off(dev, crtc->pipe);
  9925. /* We need to sanitize the plane -> pipe mapping first because this will
  9926. * disable the crtc (and hence change the state) if it is wrong. Note
  9927. * that gen4+ has a fixed plane -> pipe mapping. */
  9928. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  9929. struct intel_connector *connector;
  9930. bool plane;
  9931. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  9932. crtc->base.base.id);
  9933. /* Pipe has the wrong plane attached and the plane is active.
  9934. * Temporarily change the plane mapping and disable everything
  9935. * ... */
  9936. plane = crtc->plane;
  9937. crtc->plane = !plane;
  9938. dev_priv->display.crtc_disable(&crtc->base);
  9939. crtc->plane = plane;
  9940. /* ... and break all links. */
  9941. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9942. base.head) {
  9943. if (connector->encoder->base.crtc != &crtc->base)
  9944. continue;
  9945. connector->base.dpms = DRM_MODE_DPMS_OFF;
  9946. connector->base.encoder = NULL;
  9947. }
  9948. /* multiple connectors may have the same encoder:
  9949. * handle them and break crtc link separately */
  9950. list_for_each_entry(connector, &dev->mode_config.connector_list,
  9951. base.head)
  9952. if (connector->encoder->base.crtc == &crtc->base) {
  9953. connector->encoder->base.crtc = NULL;
  9954. connector->encoder->connectors_active = false;
  9955. }
  9956. WARN_ON(crtc->active);
  9957. crtc->base.enabled = false;
  9958. }
  9959. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  9960. crtc->pipe == PIPE_A && !crtc->active) {
  9961. /* BIOS forgot to enable pipe A, this mostly happens after
  9962. * resume. Force-enable the pipe to fix this, the update_dpms
  9963. * call below we restore the pipe to the right state, but leave
  9964. * the required bits on. */
  9965. intel_enable_pipe_a(dev);
  9966. }
  9967. /* Adjust the state of the output pipe according to whether we
  9968. * have active connectors/encoders. */
  9969. intel_crtc_update_dpms(&crtc->base);
  9970. if (crtc->active != crtc->base.enabled) {
  9971. struct intel_encoder *encoder;
  9972. /* This can happen either due to bugs in the get_hw_state
  9973. * functions or because the pipe is force-enabled due to the
  9974. * pipe A quirk. */
  9975. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  9976. crtc->base.base.id,
  9977. crtc->base.enabled ? "enabled" : "disabled",
  9978. crtc->active ? "enabled" : "disabled");
  9979. crtc->base.enabled = crtc->active;
  9980. /* Because we only establish the connector -> encoder ->
  9981. * crtc links if something is active, this means the
  9982. * crtc is now deactivated. Break the links. connector
  9983. * -> encoder links are only establish when things are
  9984. * actually up, hence no need to break them. */
  9985. WARN_ON(crtc->active);
  9986. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  9987. WARN_ON(encoder->connectors_active);
  9988. encoder->base.crtc = NULL;
  9989. }
  9990. }
  9991. if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
  9992. /*
  9993. * We start out with underrun reporting disabled to avoid races.
  9994. * For correct bookkeeping mark this on active crtcs.
  9995. *
  9996. * Also on gmch platforms we dont have any hardware bits to
  9997. * disable the underrun reporting. Which means we need to start
  9998. * out with underrun reporting disabled also on inactive pipes,
  9999. * since otherwise we'll complain about the garbage we read when
  10000. * e.g. coming up after runtime pm.
  10001. *
  10002. * No protection against concurrent access is required - at
  10003. * worst a fifo underrun happens which also sets this to false.
  10004. */
  10005. crtc->cpu_fifo_underrun_disabled = true;
  10006. crtc->pch_fifo_underrun_disabled = true;
  10007. update_scanline_offset(crtc);
  10008. }
  10009. }
  10010. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  10011. {
  10012. struct intel_connector *connector;
  10013. struct drm_device *dev = encoder->base.dev;
  10014. /* We need to check both for a crtc link (meaning that the
  10015. * encoder is active and trying to read from a pipe) and the
  10016. * pipe itself being active. */
  10017. bool has_active_crtc = encoder->base.crtc &&
  10018. to_intel_crtc(encoder->base.crtc)->active;
  10019. if (encoder->connectors_active && !has_active_crtc) {
  10020. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  10021. encoder->base.base.id,
  10022. encoder->base.name);
  10023. /* Connector is active, but has no active pipe. This is
  10024. * fallout from our resume register restoring. Disable
  10025. * the encoder manually again. */
  10026. if (encoder->base.crtc) {
  10027. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  10028. encoder->base.base.id,
  10029. encoder->base.name);
  10030. encoder->disable(encoder);
  10031. }
  10032. encoder->base.crtc = NULL;
  10033. encoder->connectors_active = false;
  10034. /* Inconsistent output/port/pipe state happens presumably due to
  10035. * a bug in one of the get_hw_state functions. Or someplace else
  10036. * in our code, like the register restore mess on resume. Clamp
  10037. * things to off as a safer default. */
  10038. list_for_each_entry(connector,
  10039. &dev->mode_config.connector_list,
  10040. base.head) {
  10041. if (connector->encoder != encoder)
  10042. continue;
  10043. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10044. connector->base.encoder = NULL;
  10045. }
  10046. }
  10047. /* Enabled encoders without active connectors will be fixed in
  10048. * the crtc fixup. */
  10049. }
  10050. void i915_redisable_vga_power_on(struct drm_device *dev)
  10051. {
  10052. struct drm_i915_private *dev_priv = dev->dev_private;
  10053. u32 vga_reg = i915_vgacntrl_reg(dev);
  10054. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  10055. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  10056. i915_disable_vga(dev);
  10057. }
  10058. }
  10059. void i915_redisable_vga(struct drm_device *dev)
  10060. {
  10061. struct drm_i915_private *dev_priv = dev->dev_private;
  10062. /* This function can be called both from intel_modeset_setup_hw_state or
  10063. * at a very early point in our resume sequence, where the power well
  10064. * structures are not yet restored. Since this function is at a very
  10065. * paranoid "someone might have enabled VGA while we were not looking"
  10066. * level, just check if the power well is enabled instead of trying to
  10067. * follow the "don't touch the power well if we don't need it" policy
  10068. * the rest of the driver uses. */
  10069. if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
  10070. return;
  10071. i915_redisable_vga_power_on(dev);
  10072. }
  10073. static bool primary_get_hw_state(struct intel_crtc *crtc)
  10074. {
  10075. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  10076. if (!crtc->active)
  10077. return false;
  10078. return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
  10079. }
  10080. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  10081. {
  10082. struct drm_i915_private *dev_priv = dev->dev_private;
  10083. enum pipe pipe;
  10084. struct intel_crtc *crtc;
  10085. struct intel_encoder *encoder;
  10086. struct intel_connector *connector;
  10087. int i;
  10088. for_each_intel_crtc(dev, crtc) {
  10089. memset(&crtc->config, 0, sizeof(crtc->config));
  10090. crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
  10091. crtc->active = dev_priv->display.get_pipe_config(crtc,
  10092. &crtc->config);
  10093. crtc->base.enabled = crtc->active;
  10094. crtc->primary_enabled = primary_get_hw_state(crtc);
  10095. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  10096. crtc->base.base.id,
  10097. crtc->active ? "enabled" : "disabled");
  10098. }
  10099. /* FIXME: Smash this into the new shared dpll infrastructure. */
  10100. if (HAS_DDI(dev))
  10101. intel_ddi_setup_hw_pll_state(dev);
  10102. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10103. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10104. pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
  10105. pll->active = 0;
  10106. for_each_intel_crtc(dev, crtc) {
  10107. if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
  10108. pll->active++;
  10109. }
  10110. pll->refcount = pll->active;
  10111. DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
  10112. pll->name, pll->refcount, pll->on);
  10113. }
  10114. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10115. base.head) {
  10116. pipe = 0;
  10117. if (encoder->get_hw_state(encoder, &pipe)) {
  10118. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10119. encoder->base.crtc = &crtc->base;
  10120. encoder->get_config(encoder, &crtc->config);
  10121. } else {
  10122. encoder->base.crtc = NULL;
  10123. }
  10124. encoder->connectors_active = false;
  10125. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  10126. encoder->base.base.id,
  10127. encoder->base.name,
  10128. encoder->base.crtc ? "enabled" : "disabled",
  10129. pipe_name(pipe));
  10130. }
  10131. list_for_each_entry(connector, &dev->mode_config.connector_list,
  10132. base.head) {
  10133. if (connector->get_hw_state(connector)) {
  10134. connector->base.dpms = DRM_MODE_DPMS_ON;
  10135. connector->encoder->connectors_active = true;
  10136. connector->base.encoder = &connector->encoder->base;
  10137. } else {
  10138. connector->base.dpms = DRM_MODE_DPMS_OFF;
  10139. connector->base.encoder = NULL;
  10140. }
  10141. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  10142. connector->base.base.id,
  10143. connector->base.name,
  10144. connector->base.encoder ? "enabled" : "disabled");
  10145. }
  10146. }
  10147. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  10148. * and i915 state tracking structures. */
  10149. void intel_modeset_setup_hw_state(struct drm_device *dev,
  10150. bool force_restore)
  10151. {
  10152. struct drm_i915_private *dev_priv = dev->dev_private;
  10153. enum pipe pipe;
  10154. struct intel_crtc *crtc;
  10155. struct intel_encoder *encoder;
  10156. int i;
  10157. intel_modeset_readout_hw_state(dev);
  10158. /*
  10159. * Now that we have the config, copy it to each CRTC struct
  10160. * Note that this could go away if we move to using crtc_config
  10161. * checking everywhere.
  10162. */
  10163. for_each_intel_crtc(dev, crtc) {
  10164. if (crtc->active && i915.fastboot) {
  10165. intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
  10166. DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
  10167. crtc->base.base.id);
  10168. drm_mode_debug_printmodeline(&crtc->base.mode);
  10169. }
  10170. }
  10171. /* HW state is read out, now we need to sanitize this mess. */
  10172. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  10173. base.head) {
  10174. intel_sanitize_encoder(encoder);
  10175. }
  10176. for_each_pipe(pipe) {
  10177. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  10178. intel_sanitize_crtc(crtc);
  10179. intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
  10180. }
  10181. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  10182. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  10183. if (!pll->on || pll->active)
  10184. continue;
  10185. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  10186. pll->disable(dev_priv, pll);
  10187. pll->on = false;
  10188. }
  10189. if (HAS_PCH_SPLIT(dev))
  10190. ilk_wm_get_hw_state(dev);
  10191. if (force_restore) {
  10192. i915_redisable_vga(dev);
  10193. /*
  10194. * We need to use raw interfaces for restoring state to avoid
  10195. * checking (bogus) intermediate states.
  10196. */
  10197. for_each_pipe(pipe) {
  10198. struct drm_crtc *crtc =
  10199. dev_priv->pipe_to_crtc_mapping[pipe];
  10200. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  10201. crtc->primary->fb);
  10202. }
  10203. } else {
  10204. intel_modeset_update_staged_output_state(dev);
  10205. }
  10206. intel_modeset_check_state(dev);
  10207. }
  10208. void intel_modeset_gem_init(struct drm_device *dev)
  10209. {
  10210. struct drm_crtc *c;
  10211. struct intel_framebuffer *fb;
  10212. mutex_lock(&dev->struct_mutex);
  10213. intel_init_gt_powersave(dev);
  10214. mutex_unlock(&dev->struct_mutex);
  10215. intel_modeset_init_hw(dev);
  10216. intel_setup_overlay(dev);
  10217. /*
  10218. * Make sure any fbs we allocated at startup are properly
  10219. * pinned & fenced. When we do the allocation it's too early
  10220. * for this.
  10221. */
  10222. mutex_lock(&dev->struct_mutex);
  10223. for_each_crtc(dev, c) {
  10224. if (!c->primary->fb)
  10225. continue;
  10226. fb = to_intel_framebuffer(c->primary->fb);
  10227. if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
  10228. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  10229. to_intel_crtc(c)->pipe);
  10230. drm_framebuffer_unreference(c->primary->fb);
  10231. c->primary->fb = NULL;
  10232. }
  10233. }
  10234. mutex_unlock(&dev->struct_mutex);
  10235. }
  10236. void intel_connector_unregister(struct intel_connector *intel_connector)
  10237. {
  10238. struct drm_connector *connector = &intel_connector->base;
  10239. intel_panel_destroy_backlight(connector);
  10240. drm_sysfs_connector_remove(connector);
  10241. }
  10242. void intel_modeset_cleanup(struct drm_device *dev)
  10243. {
  10244. struct drm_i915_private *dev_priv = dev->dev_private;
  10245. struct drm_crtc *crtc;
  10246. struct drm_connector *connector;
  10247. /*
  10248. * Interrupts and polling as the first thing to avoid creating havoc.
  10249. * Too much stuff here (turning of rps, connectors, ...) would
  10250. * experience fancy races otherwise.
  10251. */
  10252. drm_irq_uninstall(dev);
  10253. cancel_work_sync(&dev_priv->hotplug_work);
  10254. /*
  10255. * Due to the hpd irq storm handling the hotplug work can re-arm the
  10256. * poll handlers. Hence disable polling after hpd handling is shut down.
  10257. */
  10258. drm_kms_helper_poll_fini(dev);
  10259. mutex_lock(&dev->struct_mutex);
  10260. intel_unregister_dsm_handler();
  10261. for_each_crtc(dev, crtc) {
  10262. /* Skip inactive CRTCs */
  10263. if (!crtc->primary->fb)
  10264. continue;
  10265. intel_increase_pllclock(crtc);
  10266. }
  10267. intel_disable_fbc(dev);
  10268. intel_disable_gt_powersave(dev);
  10269. ironlake_teardown_rc6(dev);
  10270. mutex_unlock(&dev->struct_mutex);
  10271. /* flush any delayed tasks or pending work */
  10272. flush_scheduled_work();
  10273. /* destroy the backlight and sysfs files before encoders/connectors */
  10274. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  10275. struct intel_connector *intel_connector;
  10276. intel_connector = to_intel_connector(connector);
  10277. intel_connector->unregister(intel_connector);
  10278. }
  10279. drm_mode_config_cleanup(dev);
  10280. intel_cleanup_overlay(dev);
  10281. mutex_lock(&dev->struct_mutex);
  10282. intel_cleanup_gt_powersave(dev);
  10283. mutex_unlock(&dev->struct_mutex);
  10284. }
  10285. /*
  10286. * Return which encoder is currently attached for connector.
  10287. */
  10288. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  10289. {
  10290. return &intel_attached_encoder(connector)->base;
  10291. }
  10292. void intel_connector_attach_encoder(struct intel_connector *connector,
  10293. struct intel_encoder *encoder)
  10294. {
  10295. connector->encoder = encoder;
  10296. drm_mode_connector_attach_encoder(&connector->base,
  10297. &encoder->base);
  10298. }
  10299. /*
  10300. * set vga decode state - true == enable VGA decode
  10301. */
  10302. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  10303. {
  10304. struct drm_i915_private *dev_priv = dev->dev_private;
  10305. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  10306. u16 gmch_ctrl;
  10307. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  10308. DRM_ERROR("failed to read control word\n");
  10309. return -EIO;
  10310. }
  10311. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  10312. return 0;
  10313. if (state)
  10314. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  10315. else
  10316. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  10317. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  10318. DRM_ERROR("failed to write control word\n");
  10319. return -EIO;
  10320. }
  10321. return 0;
  10322. }
  10323. struct intel_display_error_state {
  10324. u32 power_well_driver;
  10325. int num_transcoders;
  10326. struct intel_cursor_error_state {
  10327. u32 control;
  10328. u32 position;
  10329. u32 base;
  10330. u32 size;
  10331. } cursor[I915_MAX_PIPES];
  10332. struct intel_pipe_error_state {
  10333. bool power_domain_on;
  10334. u32 source;
  10335. u32 stat;
  10336. } pipe[I915_MAX_PIPES];
  10337. struct intel_plane_error_state {
  10338. u32 control;
  10339. u32 stride;
  10340. u32 size;
  10341. u32 pos;
  10342. u32 addr;
  10343. u32 surface;
  10344. u32 tile_offset;
  10345. } plane[I915_MAX_PIPES];
  10346. struct intel_transcoder_error_state {
  10347. bool power_domain_on;
  10348. enum transcoder cpu_transcoder;
  10349. u32 conf;
  10350. u32 htotal;
  10351. u32 hblank;
  10352. u32 hsync;
  10353. u32 vtotal;
  10354. u32 vblank;
  10355. u32 vsync;
  10356. } transcoder[4];
  10357. };
  10358. struct intel_display_error_state *
  10359. intel_display_capture_error_state(struct drm_device *dev)
  10360. {
  10361. struct drm_i915_private *dev_priv = dev->dev_private;
  10362. struct intel_display_error_state *error;
  10363. int transcoders[] = {
  10364. TRANSCODER_A,
  10365. TRANSCODER_B,
  10366. TRANSCODER_C,
  10367. TRANSCODER_EDP,
  10368. };
  10369. int i;
  10370. if (INTEL_INFO(dev)->num_pipes == 0)
  10371. return NULL;
  10372. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  10373. if (error == NULL)
  10374. return NULL;
  10375. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  10376. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  10377. for_each_pipe(i) {
  10378. error->pipe[i].power_domain_on =
  10379. intel_display_power_enabled_unlocked(dev_priv,
  10380. POWER_DOMAIN_PIPE(i));
  10381. if (!error->pipe[i].power_domain_on)
  10382. continue;
  10383. error->cursor[i].control = I915_READ(CURCNTR(i));
  10384. error->cursor[i].position = I915_READ(CURPOS(i));
  10385. error->cursor[i].base = I915_READ(CURBASE(i));
  10386. error->plane[i].control = I915_READ(DSPCNTR(i));
  10387. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  10388. if (INTEL_INFO(dev)->gen <= 3) {
  10389. error->plane[i].size = I915_READ(DSPSIZE(i));
  10390. error->plane[i].pos = I915_READ(DSPPOS(i));
  10391. }
  10392. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  10393. error->plane[i].addr = I915_READ(DSPADDR(i));
  10394. if (INTEL_INFO(dev)->gen >= 4) {
  10395. error->plane[i].surface = I915_READ(DSPSURF(i));
  10396. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  10397. }
  10398. error->pipe[i].source = I915_READ(PIPESRC(i));
  10399. if (!HAS_PCH_SPLIT(dev))
  10400. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  10401. }
  10402. error->num_transcoders = INTEL_INFO(dev)->num_pipes;
  10403. if (HAS_DDI(dev_priv->dev))
  10404. error->num_transcoders++; /* Account for eDP. */
  10405. for (i = 0; i < error->num_transcoders; i++) {
  10406. enum transcoder cpu_transcoder = transcoders[i];
  10407. error->transcoder[i].power_domain_on =
  10408. intel_display_power_enabled_unlocked(dev_priv,
  10409. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  10410. if (!error->transcoder[i].power_domain_on)
  10411. continue;
  10412. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  10413. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  10414. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  10415. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  10416. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  10417. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  10418. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  10419. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  10420. }
  10421. return error;
  10422. }
  10423. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  10424. void
  10425. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  10426. struct drm_device *dev,
  10427. struct intel_display_error_state *error)
  10428. {
  10429. int i;
  10430. if (!error)
  10431. return;
  10432. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  10433. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  10434. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  10435. error->power_well_driver);
  10436. for_each_pipe(i) {
  10437. err_printf(m, "Pipe [%d]:\n", i);
  10438. err_printf(m, " Power: %s\n",
  10439. error->pipe[i].power_domain_on ? "on" : "off");
  10440. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  10441. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  10442. err_printf(m, "Plane [%d]:\n", i);
  10443. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  10444. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  10445. if (INTEL_INFO(dev)->gen <= 3) {
  10446. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  10447. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  10448. }
  10449. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  10450. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  10451. if (INTEL_INFO(dev)->gen >= 4) {
  10452. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  10453. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  10454. }
  10455. err_printf(m, "Cursor [%d]:\n", i);
  10456. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  10457. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  10458. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  10459. }
  10460. for (i = 0; i < error->num_transcoders; i++) {
  10461. err_printf(m, "CPU transcoder: %c\n",
  10462. transcoder_name(error->transcoder[i].cpu_transcoder));
  10463. err_printf(m, " Power: %s\n",
  10464. error->transcoder[i].power_domain_on ? "on" : "off");
  10465. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  10466. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  10467. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  10468. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  10469. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  10470. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  10471. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  10472. }
  10473. }