intel_display.c 252 KB

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