intel_display.c 253 KB

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