intel_display.c 270 KB

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